Skip to content

src: update for node-fetch v3 compatibility #2195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading