Skip to content

Commit 166858b

Browse files
committed
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.
1 parent 3718aa6 commit 166858b

File tree

5 files changed

+22
-16
lines changed

5 files changed

+22
-16
lines changed

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class KubeConfig implements SecurityAuthentication {
168168
headers,
169169
method: opts.method,
170170
timeout: opts.timeout,
171-
};
171+
} as RequestInit;
172172
}
173173

174174
public async applyToHTTPSOptions(opts: https.RequestOptions | WebSocket.ClientOptions): Promise<void> {

src/config_test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,14 @@ describe('KubeConfig', () => {
284284
});
285285

286286
strictEqual(requestInit.method, 'POST');
287-
strictEqual(requestInit.timeout, 5);
288-
deepEqual((requestInit.headers as Headers).raw(), {
289-
Authorization: ['Basic Zm9vOmJhcg=='],
290-
list: ['a', 'b'],
291-
number: ['5'],
292-
string: ['str'],
293-
});
287+
// timeout has been removed from the spec.
288+
strictEqual((requestInit as any).timeout, 5);
289+
const headers = requestInit.headers as Headers;
290+
strictEqual(Array.from(headers).length, 4);
291+
strictEqual(headers.get('Authorization'), 'Basic Zm9vOmJhcg==');
292+
strictEqual(headers.get('list'), 'a, b');
293+
strictEqual(headers.get('number'), '5');
294+
strictEqual(headers.get('string'), 'str');
294295
assertRequestAgentsEqual(requestInit.agent as Agent, expectedAgent);
295296
});
296297
});

src/log.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export class Log {
140140
const status = response.status;
141141
if (status === 200) {
142142
// TODO: the follow search param still has the stream close prematurely based on my testing
143-
response.body.pipe(stream);
143+
response.body!.pipe(stream);
144144
} else if (status === 500) {
145145
const v1status = response.body as V1Status;
146146
const v1code = v1status.code;

src/watch.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { STATUS_CODES } from 'node:http';
12
import { createInterface } from 'node:readline';
23
import fetch from 'node-fetch';
34
import { KubeConfig } from './config.js';
@@ -54,11 +55,13 @@ export class Watch {
5455
const response = await fetch(watchURL, requestInit);
5556

5657
if (response.status === 200) {
57-
response.body.on('error', doneCallOnce);
58-
response.body.on('close', () => doneCallOnce(null));
59-
response.body.on('finish', () => doneCallOnce(null));
58+
const body = response.body!;
6059

61-
const lines = createInterface(response.body);
60+
body.on('error', doneCallOnce);
61+
body.on('close', () => doneCallOnce(null));
62+
body.on('finish', () => doneCallOnce(null));
63+
64+
const lines = createInterface(body);
6265
lines.on('error', doneCallOnce);
6366
lines.on('close', () => doneCallOnce(null));
6467
lines.on('finish', () => doneCallOnce(null));
@@ -71,7 +74,9 @@ export class Watch {
7174
}
7275
});
7376
} else {
74-
const error = new Error(response.statusText) as Error & {
77+
const statusText =
78+
response.statusText || STATUS_CODES[response.status] || 'Internal Server Error';
79+
const error = new Error(statusText) as Error & {
7580
statusCode: number | undefined;
7681
};
7782
error.statusCode = response.status;

src/watch_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ describe('Watch', () => {
154154
strictEqual(doneCalled, 0);
155155

156156
const errIn = new Error('err');
157-
(response as IncomingMessage).socket.destroy(errIn);
157+
(response as IncomingMessage).destroy(errIn);
158158

159159
await donePromise;
160160

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

234234
const errIn = new Error('err');
235-
(response as IncomingMessage).socket.destroy(errIn);
235+
(response as IncomingMessage).destroy(errIn);
236236

237237
await donePromise;
238238

0 commit comments

Comments
 (0)