Skip to content

Commit

Permalink
Update node typings.
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Jun 21, 2023
1 parent e2e09ce commit 3a995fb
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 7 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
@@ -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<void>;
applyAuthentication(user: User, opts: https.RequestOptions | WebSocket.ClientOptions): Promise<void>;
}
1 change: 0 additions & 1 deletion src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import AbortController from 'abort-controller';
import {
ADD,
CHANGE,
Expand Down
1 change: 0 additions & 1 deletion src/cache_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { ListPromise } from './informer';

use(chaiAsPromised);

import AbortController from 'abort-controller';
import nock = require('nock');
import { Watch } from './watch';

Expand Down
23 changes: 16 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -170,13 +171,16 @@ export class KubeConfig implements SecurityAuthentication {
};
}

public async applyToHTTPSOptions(opts: https.RequestOptions): Promise<void> {
public async applyToHTTPSOptions(opts: https.RequestOptions | WebSocket.ClientOptions): Promise<void> {
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 = {};
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -517,7 +524,9 @@ export class KubeConfig implements SecurityAuthentication {
}
}

private async applyAuthorizationHeader(opts: https.RequestOptions): Promise<void> {
private async applyAuthorizationHeader(
opts: https.RequestOptions | WebSocket.ClientOptions,
): Promise<void> {
const user = this.getCurrentUser();
if (!user) {
return;
Expand All @@ -538,7 +547,7 @@ export class KubeConfig implements SecurityAuthentication {
}
}

private async applyOptions(opts: https.RequestOptions): Promise<void> {
private async applyOptions(opts: https.RequestOptions | WebSocket.ClientOptions): Promise<void> {
this.applyHTTPSOptions(opts);
await this.applyAuthorizationHeader(opts);
}
Expand Down
2 changes: 1 addition & 1 deletion src/file_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
9 changes: 4 additions & 5 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -101,8 +102,6 @@ export class Log {
doneOrOptions?: ((err: any) => void) | LogOptions,
options?: LogOptions,
): Promise<AbortController> {
const AbortControllerCtor = globalThis.AbortController || (await import('abort-controller'));

if (typeof doneOrOptions !== 'function') {
options = doneOrOptions;
}
Expand All @@ -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)
Expand Down
9 changes: 3 additions & 6 deletions src/watch.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -25,9 +25,6 @@ export class Watch {
callback: (phase: string, apiObj: any, watchObj?: any) => void,
done: (err: any) => void,
): Promise<AbortController> {
const AbortControllerCtor =
globalThis.AbortController || (await import('abort-controller')).AbortController;

const cluster = this.config.getCurrentCluster();
if (!cluster) {
throw new Error('No currently active cluster');
Expand All @@ -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;
Expand Down

0 comments on commit 3a995fb

Please sign in to comment.