From 3a995fbb8399dfb9d6751006ed9f2deb5c03c7a2 Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Wed, 21 Jun 2023 18:57:30 +0000 Subject: [PATCH] Update node typings. --- .github/workflows/test.yml | 2 +- package-lock.json | 47 ++++++-------------------------------- package.json | 3 +-- src/auth.ts | 3 ++- src/cache.ts | 1 - src/cache_test.ts | 1 - src/config.ts | 23 +++++++++++++------ src/file_auth.ts | 2 +- src/log.ts | 9 ++++---- src/watch.ts | 9 +++----- 10 files changed, 35 insertions(+), 65 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 969278165d..ca0ef3cc88 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - node: [ '18', '16', '14' ] + node: [ '20', '18', '16' ] name: Node ${{ matrix.node }} validation steps: - uses: actions/checkout@v2 diff --git a/package-lock.json b/package-lock.json index 48551d244b..219cbe1cec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,13 +10,12 @@ "license": "Apache-2.0", "dependencies": { "@types/js-yaml": "^4.0.1", - "@types/node": "^10.12.0", + "@types/node": "^20.3.1", "@types/node-fetch": "^2.6.3", "@types/stream-buffers": "^3.0.3", "@types/tar": "^6.1.1", "@types/underscore": "^1.8.9", "@types/ws": "^8.5.4", - "abort-controller": "^3.0.0", "byline": "^5.0.0", "execa": "5.0.0", "form-data": "^4.0.0", @@ -576,9 +575,9 @@ } }, "node_modules/@types/node": { - "version": "10.12.0", - "resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.0.tgz", - "integrity": "sha512-3TUHC3jsBAB7qVRGxT6lWyYo2v96BMmD2PTcl47H25Lu7UXtFH/2qqmKiVrnel6Ne//0TFYf6uvNX+HW2FRkLQ==" + "version": "20.3.1", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.3.1.tgz", + "integrity": "sha512-EhcH/wvidPy1WeML3TtYFGR83UzjxeWRen9V402T8aUGYsCHOmfoisV3ZSg03gAFIbLq8TnWOJ0f4cALtnSEUg==" }, "node_modules/@types/node-fetch": { "version": "2.6.3", @@ -640,17 +639,6 @@ "@types/node": "*" } }, - "node_modules/abort-controller": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", - "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", - "dependencies": { - "event-target-shim": "^5.0.0" - }, - "engines": { - "node": ">=6.5" - } - }, "node_modules/aggregate-error": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.1.0.tgz", @@ -1178,14 +1166,6 @@ "node": ">=0.10.0" } }, - "node_modules/event-target-shim": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", - "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==", - "engines": { - "node": ">=6" - } - }, "node_modules/execa": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/execa/-/execa-5.0.0.tgz", @@ -4326,9 +4306,9 @@ } }, "@types/node": { - "version": "10.12.0", - "resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.0.tgz", - "integrity": "sha512-3TUHC3jsBAB7qVRGxT6lWyYo2v96BMmD2PTcl47H25Lu7UXtFH/2qqmKiVrnel6Ne//0TFYf6uvNX+HW2FRkLQ==" + "version": "20.3.1", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.3.1.tgz", + "integrity": "sha512-EhcH/wvidPy1WeML3TtYFGR83UzjxeWRen9V402T8aUGYsCHOmfoisV3ZSg03gAFIbLq8TnWOJ0f4cALtnSEUg==" }, "@types/node-fetch": { "version": "2.6.3", @@ -4388,14 +4368,6 @@ "@types/node": "*" } }, - "abort-controller": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", - "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", - "requires": { - "event-target-shim": "^5.0.0" - } - }, "aggregate-error": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.1.0.tgz", @@ -4807,11 +4779,6 @@ "integrity": "sha1-Cr9PHKpbyx96nYrMbepPqqBLrJs=", "dev": true }, - "event-target-shim": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", - "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==" - }, "execa": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/execa/-/execa-5.0.0.tgz", diff --git a/package.json b/package.json index c2571cf9d1..28e4d547f5 100644 --- a/package.json +++ b/package.json @@ -55,13 +55,12 @@ "license": "Apache-2.0", "dependencies": { "@types/js-yaml": "^4.0.1", - "@types/node": "^10.12.0", + "@types/node": "^20.3.1", "@types/node-fetch": "^2.6.3", "@types/stream-buffers": "^3.0.3", "@types/tar": "^6.1.1", "@types/underscore": "^1.8.9", "@types/ws": "^8.5.4", - "abort-controller": "^3.0.0", "byline": "^5.0.0", "execa": "5.0.0", "form-data": "^4.0.0", diff --git a/src/auth.ts b/src/auth.ts index 78b226deea..f33cad6c67 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,8 +1,9 @@ import https = require('https'); import { User } from './config_types'; +import WebSocket = require('isomorphic-ws'); export interface Authenticator { isAuthProvider(user: User): boolean; - applyAuthentication(user: User, opts: https.RequestOptions): Promise; + applyAuthentication(user: User, opts: https.RequestOptions | WebSocket.ClientOptions): Promise; } diff --git a/src/cache.ts b/src/cache.ts index d5233a73d3..16d49cb5bc 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -1,4 +1,3 @@ -import AbortController from 'abort-controller'; import { ADD, CHANGE, diff --git a/src/cache_test.ts b/src/cache_test.ts index 559440d42d..a4eaee11c2 100644 --- a/src/cache_test.ts +++ b/src/cache_test.ts @@ -13,7 +13,6 @@ import { ListPromise } from './informer'; use(chaiAsPromised); -import AbortController from 'abort-controller'; import nock = require('nock'); import { Watch } from './watch'; diff --git a/src/config.ts b/src/config.ts index 4d6dbead97..23b32e89b6 100644 --- a/src/config.ts +++ b/src/config.ts @@ -34,6 +34,7 @@ import { ServerConfiguration, } from './gen'; import { OpenIDConnectAuth } from './oidc_auth'; +import WebSocket = require('isomorphic-ws'); const SERVICEACCOUNT_ROOT: string = '/var/run/secrets/kubernetes.io/serviceaccount'; const SERVICEACCOUNT_CA_PATH: string = SERVICEACCOUNT_ROOT + '/ca.crt'; @@ -170,13 +171,16 @@ export class KubeConfig implements SecurityAuthentication { }; } - public async applyToHTTPSOptions(opts: https.RequestOptions): Promise { + public async applyToHTTPSOptions(opts: https.RequestOptions | WebSocket.ClientOptions): Promise { const user = this.getCurrentUser(); await this.applyOptions(opts); if (user && user.username) { - opts.auth = `${user.username}:${user.password}`; + // The ws docs say that it accepts anything that https.RequestOptions accepts, + // but Typescript doesn't understand that idea (yet) probably could be fixed in + // the typings, but for now just cast to any + (opts as any).auth = `${user.username}:${user.password}`; } const agentOptions: https.AgentOptions = {}; @@ -188,8 +192,11 @@ export class KubeConfig implements SecurityAuthentication { agentOptions.pfx = opts.pfx; agentOptions.passphrase = opts.passphrase; agentOptions.rejectUnauthorized = opts.rejectUnauthorized; - agentOptions.timeout = opts.timeout; - agentOptions.servername = opts.servername; + // The ws docs say that it accepts anything that https.RequestOptions accepts, + // but Typescript doesn't understand that idea (yet) probably could be fixed in + // the typings, but for now just cast to any + agentOptions.timeout = (opts as any).timeout; + agentOptions.servername = (opts as any).servername; agentOptions.ciphers = opts.ciphers; agentOptions.honorCipherOrder = opts.honorCipherOrder; agentOptions.ecdhCurve = opts.ecdhCurve; @@ -493,7 +500,7 @@ export class KubeConfig implements SecurityAuthentication { return this.getContextObject(this.currentContext); } - private applyHTTPSOptions(opts: https.RequestOptions): void { + private applyHTTPSOptions(opts: https.RequestOptions | WebSocket.ClientOptions): void { const cluster = this.getCurrentCluster(); const user = this.getCurrentUser(); if (!user) { @@ -517,7 +524,9 @@ export class KubeConfig implements SecurityAuthentication { } } - private async applyAuthorizationHeader(opts: https.RequestOptions): Promise { + private async applyAuthorizationHeader( + opts: https.RequestOptions | WebSocket.ClientOptions, + ): Promise { const user = this.getCurrentUser(); if (!user) { return; @@ -538,7 +547,7 @@ export class KubeConfig implements SecurityAuthentication { } } - private async applyOptions(opts: https.RequestOptions): Promise { + private async applyOptions(opts: https.RequestOptions | WebSocket.ClientOptions): Promise { this.applyHTTPSOptions(opts); await this.applyAuthorizationHeader(opts); } diff --git a/src/file_auth.ts b/src/file_auth.ts index 83e12305dd..d24034eb54 100644 --- a/src/file_auth.ts +++ b/src/file_auth.ts @@ -26,7 +26,7 @@ export class FileAuth implements Authenticator { private refreshToken(filePath: string): void { // TODO make this async? - this.token = fs.readFileSync(filePath).toString('UTF-8'); + this.token = fs.readFileSync(filePath).toString('utf-8'); this.lastRead = new Date(); } diff --git a/src/log.ts b/src/log.ts index 412815a378..924f122c31 100644 --- a/src/log.ts +++ b/src/log.ts @@ -1,11 +1,12 @@ -import AbortController from 'abort-controller'; import fetch from 'node-fetch'; +import { AbortSignal } from 'node-fetch/externals'; import { Writable } from 'stream'; import { URL, URLSearchParams } from 'url'; import { ApiException } from './api'; import { KubeConfig } from './config'; import { V1Status } from './gen'; import { normalizeResponseHeaders } from './util'; + export interface LogOptions { /** * Follow the log stream of the pod. Defaults to false. @@ -101,8 +102,6 @@ export class Log { doneOrOptions?: ((err: any) => void) | LogOptions, options?: LogOptions, ): Promise { - const AbortControllerCtor = globalThis.AbortController || (await import('abort-controller')); - if (typeof doneOrOptions !== 'function') { options = doneOrOptions; } @@ -122,8 +121,8 @@ export class Log { const requestInit = await this.config.applyToFetchOptions({}); - const controller = new AbortControllerCtor(); - requestInit.signal = controller.signal; + const controller = new AbortController(); + requestInit.signal = controller.signal as AbortSignal; requestInit.method = 'GET'; await fetch(requestURL.toString(), requestInit) diff --git a/src/watch.ts b/src/watch.ts index 57e3921f96..5b1837e3d6 100644 --- a/src/watch.ts +++ b/src/watch.ts @@ -1,7 +1,7 @@ -import AbortController from 'abort-controller'; import byline = require('byline'); import { RequestOptions } from 'https'; import fetch from 'node-fetch'; +import { AbortSignal } from 'node-fetch/externals'; import { URL } from 'url'; import { KubeConfig } from './config'; @@ -25,9 +25,6 @@ export class Watch { callback: (phase: string, apiObj: any, watchObj?: any) => void, done: (err: any) => void, ): Promise { - const AbortControllerCtor = - globalThis.AbortController || (await import('abort-controller')).AbortController; - const cluster = this.config.getCurrentCluster(); if (!cluster) { throw new Error('No currently active cluster'); @@ -43,8 +40,8 @@ export class Watch { const requestInit = await this.config.applyToFetchOptions({}); - const controller = new AbortControllerCtor(); - requestInit.signal = controller.signal; + const controller = new AbortController(); + requestInit.signal = controller.signal as AbortSignal; requestInit.method = 'GET'; let doneCalled: boolean = false;