From 166858bea570f15cca856555bcb17fc1e44576e8 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 9 Jan 2025 18:15:59 -0500 Subject: [PATCH] src: update for node-fetch v3 compatibility While the generated code still needs to be updated to use node-fetch v3, these changes make the library compatible with it. --- src/config.ts | 2 +- src/config_test.ts | 15 ++++++++------- src/log.ts | 2 +- src/watch.ts | 15 ++++++++++----- src/watch_test.ts | 4 ++-- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/config.ts b/src/config.ts index 6e3091d406..d7687315ff 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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 { diff --git a/src/config_test.ts b/src/config_test.ts index 969db82a54..07ad255b5e 100644 --- a/src/config_test.ts +++ b/src/config_test.ts @@ -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); }); }); diff --git a/src/log.ts b/src/log.ts index bb237cf4ed..e209c3dadc 100644 --- a/src/log.ts +++ b/src/log.ts @@ -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; diff --git a/src/watch.ts b/src/watch.ts index 475de9b1de..ac842180be 100644 --- a/src/watch.ts +++ b/src/watch.ts @@ -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'; @@ -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)); @@ -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; diff --git a/src/watch_test.ts b/src/watch_test.ts index 38e169d9fc..fea7e8b93f 100644 --- a/src/watch_test.ts +++ b/src/watch_test.ts @@ -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; @@ -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;