Skip to content

Commit

Permalink
src: update for node-fetch v3 compatibility
Browse files Browse the repository at this point in the history
While the generated code still needs to be updated to use
node-fetch v3, these changes make the library compatible with
it.
  • Loading branch information
cjihrig committed Feb 1, 2025
1 parent 3718aa6 commit 166858b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class KubeConfig implements SecurityAuthentication {
headers,
method: opts.method,
timeout: opts.timeout,
};
} as RequestInit;
}

public async applyToHTTPSOptions(opts: https.RequestOptions | WebSocket.ClientOptions): Promise<void> {
Expand Down
15 changes: 8 additions & 7 deletions src/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,14 @@ describe('KubeConfig', () => {
});

strictEqual(requestInit.method, 'POST');
strictEqual(requestInit.timeout, 5);
deepEqual((requestInit.headers as Headers).raw(), {
Authorization: ['Basic Zm9vOmJhcg=='],
list: ['a', 'b'],
number: ['5'],
string: ['str'],
});
// timeout has been removed from the spec.
strictEqual((requestInit as any).timeout, 5);
const headers = requestInit.headers as Headers;
strictEqual(Array.from(headers).length, 4);
strictEqual(headers.get('Authorization'), 'Basic Zm9vOmJhcg==');
strictEqual(headers.get('list'), 'a, b');
strictEqual(headers.get('number'), '5');
strictEqual(headers.get('string'), 'str');
assertRequestAgentsEqual(requestInit.agent as Agent, expectedAgent);
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class Log {
const status = response.status;
if (status === 200) {
// TODO: the follow search param still has the stream close prematurely based on my testing
response.body.pipe(stream);
response.body!.pipe(stream);
} else if (status === 500) {
const v1status = response.body as V1Status;
const v1code = v1status.code;
Expand Down
15 changes: 10 additions & 5 deletions src/watch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { STATUS_CODES } from 'node:http';
import { createInterface } from 'node:readline';
import fetch from 'node-fetch';
import { KubeConfig } from './config.js';
Expand Down Expand Up @@ -54,11 +55,13 @@ export class Watch {
const response = await fetch(watchURL, requestInit);

if (response.status === 200) {
response.body.on('error', doneCallOnce);
response.body.on('close', () => doneCallOnce(null));
response.body.on('finish', () => doneCallOnce(null));
const body = response.body!;

const lines = createInterface(response.body);
body.on('error', doneCallOnce);
body.on('close', () => doneCallOnce(null));
body.on('finish', () => doneCallOnce(null));

const lines = createInterface(body);
lines.on('error', doneCallOnce);
lines.on('close', () => doneCallOnce(null));
lines.on('finish', () => doneCallOnce(null));
Expand All @@ -71,7 +74,9 @@ export class Watch {
}
});
} else {
const error = new Error(response.statusText) as Error & {
const statusText =
response.statusText || STATUS_CODES[response.status] || 'Internal Server Error';
const error = new Error(statusText) as Error & {
statusCode: number | undefined;
};
error.statusCode = response.status;
Expand Down
4 changes: 2 additions & 2 deletions src/watch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe('Watch', () => {
strictEqual(doneCalled, 0);

const errIn = new Error('err');
(response as IncomingMessage).socket.destroy(errIn);
(response as IncomingMessage).destroy(errIn);

await donePromise;

Expand Down Expand Up @@ -232,7 +232,7 @@ describe('Watch', () => {
strictEqual(doneErr.length, 0);

const errIn = new Error('err');
(response as IncomingMessage).socket.destroy(errIn);
(response as IncomingMessage).destroy(errIn);

await donePromise;

Expand Down

0 comments on commit 166858b

Please sign in to comment.