Skip to content

Commit a917137

Browse files
committed
Update ws and @types/node, update code to match.
1 parent be13ccb commit a917137

File tree

7 files changed

+65
-47
lines changed

7 files changed

+65
-47
lines changed

package-lock.json

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555
"license": "Apache-2.0",
5656
"dependencies": {
5757
"@types/js-yaml": "^4.0.1",
58-
"@types/node": "^10.12.0",
58+
"@types/node": "^18.11.17",
5959
"@types/request": "^2.47.1",
60-
"@types/ws": "^6.0.1",
60+
"@types/ws": "^8.5.3",
6161
"byline": "^5.0.0",
6262
"execa": "5.0.0",
6363
"isomorphic-ws": "^5.0.0",
@@ -71,7 +71,7 @@
7171
"tmp-promise": "^3.0.2",
7272
"tslib": "^2.4.1",
7373
"underscore": "^1.13.6",
74-
"ws": "^7.3.1"
74+
"ws": "^8.11.0"
7575
},
7676
"devDependencies": {
7777
"@types/byline": "^4.2.31",

src/auth.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import https = require('https');
22
import request = require('request');
3+
import WebSocket = require('ws');
34

45
import { User } from './config_types';
56

67
export interface Authenticator {
78
isAuthProvider(user: User): boolean;
8-
applyAuthentication(user: User, opts: request.Options | https.RequestOptions): Promise<void>;
9+
applyAuthentication(
10+
user: User,
11+
opts: request.Options | https.RequestOptions | WebSocket.ClientOptions,
12+
): Promise<void>;
913
}

src/config.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import net = require('net');
66
import path = require('path');
77

88
import request = require('request');
9+
import { base64 } from 'rfc4648';
910
import shelljs = require('shelljs');
11+
import WebSocket = require('ws');
1012

1113
import * as api from './api';
1214
import { Authenticator } from './auth';
@@ -130,18 +132,23 @@ export class KubeConfig {
130132
this.makePathsAbsolute(rootDirectory);
131133
}
132134

133-
public async applytoHTTPSOptions(opts: https.RequestOptions): Promise<void> {
134-
const user = this.getCurrentUser();
135-
const cluster = this.getCurrentCluster();
136-
135+
public async applytoHTTPSOptions(opts: https.RequestOptions | WebSocket.ClientOptions): Promise<void> {
137136
await this.applyOptions(opts);
138137

138+
const user = this.getCurrentUser();
139139
if (user && user.username) {
140-
opts.auth = `${user.username}:${user.password}`;
140+
// The ws docs say that it accepts anything that https.RequestOptions accepts,
141+
// but Typescript doesn't understand that idea (yet) probably could be fixed in
142+
// the typings, but for now just cast to any
143+
(opts as any).auth = `${user.username}:${user.password}`;
141144
}
142145

146+
const cluster = this.getCurrentCluster();
143147
if (cluster && cluster.tlsServerName) {
144-
opts.servername = cluster.tlsServerName;
148+
// The ws docs say that it accepts anything that https.RequestOptions accepts,
149+
// but Typescript doesn't understand that idea (yet) probably could be fixed in
150+
// the typings, but for now just cast to any
151+
(opts as any).servername = cluster.tlsServerName;
145152
}
146153
}
147154

@@ -163,7 +170,7 @@ export class KubeConfig {
163170
}
164171

165172
if (cluster && cluster.tlsServerName) {
166-
opts.agentOptions = { servername: cluster.tlsServerName } as https.RequestOptions;
173+
opts.agentOptions = { servername: cluster.tlsServerName } as https.AgentOptions;
167174
}
168175
}
169176

@@ -402,7 +409,7 @@ export class KubeConfig {
402409
return this.getContextObject(this.currentContext);
403410
}
404411

405-
private applyHTTPSOptions(opts: request.Options | https.RequestOptions): void {
412+
private applyHTTPSOptions(opts: request.Options | https.RequestOptions | WebSocket.ClientOptions): void {
406413
const cluster = this.getCurrentCluster();
407414
const user = this.getCurrentUser();
408415
if (!user) {
@@ -426,7 +433,9 @@ export class KubeConfig {
426433
}
427434
}
428435

429-
private async applyAuthorizationHeader(opts: request.Options | https.RequestOptions): Promise<void> {
436+
private async applyAuthorizationHeader(
437+
opts: request.Options | https.RequestOptions | WebSocket.ClientOptions,
438+
): Promise<void> {
430439
const user = this.getCurrentUser();
431440
if (!user) {
432441
return;
@@ -447,7 +456,9 @@ export class KubeConfig {
447456
}
448457
}
449458

450-
private async applyOptions(opts: request.Options | https.RequestOptions): Promise<void> {
459+
private async applyOptions(
460+
opts: request.Options | https.RequestOptions | WebSocket.ClientOptions,
461+
): Promise<void> {
451462
this.applyHTTPSOptions(opts);
452463
await this.applyAuthorizationHeader(opts);
453464
}

src/config_test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ describe('KubeConfig', () => {
263263

264264
const opts: requestlib.Options = {
265265
url: 'https://company.com',
266+
agentOptions: {} as https.AgentOptions,
266267
};
267268
await kc.applyToRequest(opts);
268269

src/file_auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class FileAuth implements Authenticator {
3030

3131
private refreshToken(filePath: string): void {
3232
// TODO make this async?
33-
this.token = fs.readFileSync(filePath).toString('UTF-8');
33+
this.token = fs.readFileSync(filePath).toString('utf8');
3434
this.lastRead = new Date();
3535
}
3636

src/web-socket-handler_test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { V1Status } from './api';
77
import { KubeConfig } from './config';
88
import { Cluster, Context, User } from './config_types';
99
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler';
10+
import { fail } from 'assert';
1011

1112
const setImmediatePromise = promisify(setImmediate);
1213

@@ -127,8 +128,7 @@ describe('WebSocket', () => {
127128

128129
const promise = handler.connect(path, null, null);
129130
await setImmediatePromise();
130-
131-
mockWs.onerror({
131+
mockWs.onerror!({
132132
error: 'some error',
133133
message: 'some message',
134134
type: 'type',
@@ -184,26 +184,27 @@ describe('WebSocket', () => {
184184

185185
const event = {
186186
target: mockWs,
187+
type: 'open',
187188
};
188-
mockWs.onopen(event);
189+
mockWs.onopen!(event);
189190
const errEvt = {
190191
error: {},
191192
message: 'some message',
192193
type: 'some type',
193194
target: mockWs,
194195
};
195-
mockWs.onmessage({
196+
mockWs.onmessage!({
196197
data: 'string data',
197198
type: 'type',
198199
target: mockWs,
199200
});
200201
const buff = Buffer.alloc(10, 100);
201-
mockWs.onmessage({
202+
mockWs.onmessage!({
202203
data: buff,
203204
type: 'type',
204205
target: mockWs,
205206
});
206-
mockWs.onerror(errEvt);
207+
mockWs.onerror!(errEvt);
207208
await promise;
208209
});
209210
it('should connect properly with handlers', async () => {
@@ -266,28 +267,29 @@ describe('WebSocket', () => {
266267

267268
const event = {
268269
target: mockWs,
270+
type: 'open',
269271
};
270-
mockWs.onopen(event);
272+
mockWs.onopen!(event);
271273
const errEvt = {
272274
error: {},
273275
message: 'some message',
274276
type: 'some type',
275277
target: mockWs,
276278
};
277-
mockWs.onmessage({
279+
mockWs.onmessage!({
278280
data: 'string data',
279281
type: 'type',
280282
target: mockWs,
281283
});
282284
const fill = 100;
283285
const size = 10;
284286
const buff = Buffer.alloc(size, fill);
285-
mockWs.onmessage({
287+
mockWs.onmessage!({
286288
data: buff,
287289
type: 'type',
288290
target: mockWs,
289291
});
290-
mockWs.onerror(errEvt);
292+
mockWs.onerror!(errEvt);
291293
await promise;
292294

293295
expect(closeCount).to.equal(2);

0 commit comments

Comments
 (0)