Skip to content

Commit

Permalink
Merge pull request #1665 from mstruebing/ms/websocket-master
Browse files Browse the repository at this point in the history
[MASTER] fix: use correct types for websocket
  • Loading branch information
k8s-ci-robot committed Apr 27, 2024
2 parents b8b0ec5 + ba17e29 commit 62e8b11
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/attach.ts
Expand Up @@ -23,7 +23,7 @@ export class Attach {
stderr: stream.Writable | any,
stdin: stream.Readable | any,
tty: boolean,
): Promise<WebSocket> {
): Promise<WebSocket.WebSocket> {
const query = {
container: containerName,
stderr: stderr != null,
Expand Down
4 changes: 2 additions & 2 deletions src/attach_test.ts
Expand Up @@ -49,7 +49,7 @@ describe('Attach', () => {
it('should correctly attach to streams', async () => {
const kc = new KubeConfig();
const fakeWebSocketInterface: WebSocketInterface = mock(WebSocketHandler);
const fakeWebSocket: WebSocket = mock(WebSocket);
const fakeWebSocket: WebSocket.WebSocket = mock(WebSocket);
const callAwaiter: CallAwaiter = new CallAwaiter();
const attach = new Attach(kc, instance(fakeWebSocketInterface));
const osStream = new ResizableWriteableStreamBuffer();
Expand All @@ -63,7 +63,7 @@ describe('Attach', () => {
const path = `/api/v1/namespaces/${namespace}/pods/${pod}/attach`;
const args = `container=${container}&stderr=true&stdin=true&stdout=true&tty=false`;

const fakeConn: WebSocket = instance(fakeWebSocket);
const fakeConn: WebSocket.WebSocket = instance(fakeWebSocket);
when(fakeWebSocketInterface.connect(`${path}?${args}`, null, anyFunction())).thenResolve(
fakeConn,
);
Expand Down
4 changes: 2 additions & 2 deletions src/cp_test.ts
Expand Up @@ -44,7 +44,7 @@ describe('Cp', () => {
it('should run extract tar command to a url', async () => {
const kc = new KubeConfig();
const fakeWebSocketInterface: WebSocketInterface = mock(WebSocketHandler);
const fakeWebSocket: WebSocket = mock(WebSocket) as WebSocket;
const fakeWebSocket: WebSocket.WebSocket = mock(WebSocket) as WebSocket.WebSocket;
const callAwaiter: CallAwaiter = new CallAwaiter();
const exec = new Exec(kc, instance(fakeWebSocketInterface));
const cp = new Cp(kc, exec);
Expand All @@ -67,7 +67,7 @@ describe('Cp', () => {
};
const queryStr = querystring.stringify(query);

const fakeConn: WebSocket = instance(fakeWebSocket);
const fakeConn: WebSocket.WebSocket = instance(fakeWebSocket);
when(fakeWebSocketInterface.connect(`${path}?${queryStr}`, null, anyFunction())).thenResolve(
fakeConn,
);
Expand Down
2 changes: 1 addition & 1 deletion src/exec.ts
Expand Up @@ -39,7 +39,7 @@ export class Exec {
stdin: stream.Readable | null,
tty: boolean,
statusCallback?: (status: V1Status) => void,
): Promise<WebSocket> {
): Promise<WebSocket.WebSocket> {
const query = {
stdout: stdout != null,
stderr: stderr != null,
Expand Down
4 changes: 2 additions & 2 deletions src/exec_test.ts
Expand Up @@ -56,7 +56,7 @@ describe('Exec', () => {
it('should correctly attach to streams', async () => {
const kc = new KubeConfig();
const fakeWebSocketInterface: WebSocketInterface = mock(WebSocketHandler);
const fakeWebSocket: WebSocket = mock(WebSocket);
const fakeWebSocket: WebSocket.WebSocket = mock(WebSocket);
const callAwaiter: CallAwaiter = new CallAwaiter();
const exec = new Exec(kc, instance(fakeWebSocketInterface));
const osStream = new ResizableWriteableStreamBuffer();
Expand All @@ -73,7 +73,7 @@ describe('Exec', () => {

let statusOut = {} as V1Status;

const fakeConn: WebSocket = instance(fakeWebSocket);
const fakeConn: WebSocket.WebSocket = instance(fakeWebSocket);
when(fakeWebSocketInterface.connect(`${path}?${args}`, null, anyFunction())).thenResolve(
fakeConn,
);
Expand Down
4 changes: 2 additions & 2 deletions src/portforward.ts
Expand Up @@ -24,7 +24,7 @@ export class PortForward {
err: stream.Writable | null,
input: stream.Readable,
retryCount: number = 0,
): Promise<WebSocket | (() => WebSocket | null)> {
): Promise<WebSocket.WebSocket | (() => WebSocket.WebSocket | null)> {
if (targetPorts.length === 0) {
throw new Error('You must provide at least one port to forward to.');
}
Expand All @@ -41,7 +41,7 @@ export class PortForward {
needsToReadPortNumber[index * 2 + 1] = true;
});
const path = `/api/v1/namespaces/${namespace}/pods/${podName}/portforward?${queryStr}`;
const createWebSocket = (): Promise<WebSocket> => {
const createWebSocket = (): Promise<WebSocket.WebSocket> => {
return this.handler.connect(path, null, (streamNum: number, buff: Buffer | string): boolean => {
if (streamNum >= targetPorts.length * 2) {
return !this.disconnectOnErr;
Expand Down
22 changes: 11 additions & 11 deletions src/web-socket-handler.ts
Expand Up @@ -11,7 +11,7 @@ export interface WebSocketInterface {
path: string,
textHandler: ((text: string) => boolean) | null,
binaryHandler: ((stream: number, buff: Buffer) => boolean) | null,
): Promise<WebSocket>;
): Promise<WebSocket.WebSocket>;
}

export class WebSocketHandler implements WebSocketInterface {
Expand Down Expand Up @@ -50,7 +50,7 @@ export class WebSocketHandler implements WebSocketInterface {
}

public static handleStandardInput(
ws: WebSocket,
ws: WebSocket.WebSocket,
stdin: stream.Readable | any,
streamNum: number = 0,
): boolean {
Expand All @@ -74,11 +74,11 @@ export class WebSocketHandler implements WebSocketInterface {

public static async processData(
data: string | Buffer,
ws: WebSocket | null,
createWS: () => Promise<WebSocket>,
ws: WebSocket.WebSocket | null,
createWS: () => Promise<WebSocket.WebSocket>,
streamNum: number = 0,
retryCount: number = 3,
): Promise<WebSocket | null> {
): Promise<WebSocket.WebSocket | null> {
const buff = Buffer.alloc(data.length + 1);

buff.writeInt8(streamNum, 0);
Expand Down Expand Up @@ -108,17 +108,17 @@ export class WebSocketHandler implements WebSocketInterface {
}

public static restartableHandleStandardInput(
createWS: () => Promise<WebSocket>,
createWS: () => Promise<WebSocket.WebSocket>,
stdin: stream.Readable | any,
streamNum: number = 0,
retryCount: number = 3,
): () => WebSocket | null {
): () => WebSocket.WebSocket | null {
if (retryCount < 0) {
throw new Error("retryCount can't be lower than 0.");
}

let queue: Promise<void> = Promise.resolve();
let ws: WebSocket | null = null;
let ws: WebSocket.WebSocket | null = null;

stdin.on('data', (data) => {
queue = queue.then(async () => {
Expand All @@ -138,7 +138,7 @@ export class WebSocketHandler implements WebSocketInterface {
// factory is really just for test injection
public constructor(
readonly config: KubeConfig,
readonly socketFactory?: (uri: string, opts: WebSocket.ClientOptions) => WebSocket,
readonly socketFactory?: (uri: string, opts: WebSocket.ClientOptions) => WebSocket.WebSocket,
) {}

/**
Expand All @@ -153,7 +153,7 @@ export class WebSocketHandler implements WebSocketInterface {
path: string,
textHandler: ((text: string) => boolean) | null,
binaryHandler: ((stream: number, buff: Buffer) => boolean) | null,
): Promise<WebSocket> {
): Promise<WebSocket.WebSocket> {
const cluster = this.config.getCurrentCluster();
if (!cluster) {
throw new Error('No cluster is defined.');
Expand All @@ -168,7 +168,7 @@ export class WebSocketHandler implements WebSocketInterface {

await this.config.applyToHTTPSOptions(opts);

return await new Promise<WebSocket>((resolve, reject) => {
return await new Promise<WebSocket.WebSocket>((resolve, reject) => {
const client = this.socketFactory
? this.socketFactory(uri, opts)
: new WebSocket(uri, protocols, opts);
Expand Down
47 changes: 28 additions & 19 deletions src/web-socket-handler_test.ts
Expand Up @@ -114,13 +114,16 @@ describe('WebSocket', () => {
} as User,
];

const mockWs = {} as WebSocket;
const mockWs = {} as WebSocket.WebSocket;
let uriOut = '';

const handler = new WebSocketHandler(kc, (uri: string, opts: WebSocket.ClientOptions): WebSocket => {
uriOut = uri;
return mockWs as WebSocket;
});
const handler = new WebSocketHandler(
kc,
(uri: string, opts: WebSocket.ClientOptions): WebSocket.WebSocket => {
uriOut = uri;
return mockWs as WebSocket.WebSocket;
},
);
const path = '/some/path';

const promise = handler.connect(path, null, null);
Expand Down Expand Up @@ -162,13 +165,16 @@ describe('WebSocket', () => {
} as User,
];

const mockWs = {} as WebSocket;
const mockWs = {} as WebSocket.WebSocket;
let uriOut = '';

const handler = new WebSocketHandler(kc, (uri: string, opts: WebSocket.ClientOptions): WebSocket => {
uriOut = uri;
return mockWs as WebSocket;
});
const handler = new WebSocketHandler(
kc,
(uri: string, opts: WebSocket.ClientOptions): WebSocket.WebSocket => {
uriOut = uri;
return mockWs as WebSocket.WebSocket;
},
);
const path = '/some/path';

const promise = handler.connect(path, null, null);
Expand Down Expand Up @@ -228,13 +234,16 @@ describe('WebSocket', () => {
close: () => {
closeCount++;
},
} as WebSocket;
} as WebSocket.WebSocket;
let uriOut = '';

const handler = new WebSocketHandler(kc, (uri: string, opts: WebSocket.ClientOptions): WebSocket => {
uriOut = uri;
return mockWs as WebSocket;
});
const handler = new WebSocketHandler(
kc,
(uri: string, opts: WebSocket.ClientOptions): WebSocket.WebSocket => {
uriOut = uri;
return mockWs as WebSocket.WebSocket;
},
);
const path = '/some/path';

let textReceived = '';
Expand Down Expand Up @@ -296,7 +305,7 @@ describe('WebSocket', () => {

describe('Restartable Handle Standard Input', () => {
it('should throw on negative retry', () => {
const p = new Promise<WebSocket>(() => {});
const p = new Promise<WebSocket.WebSocket>(() => {});
expect(() => WebSocketHandler.restartableHandleStandardInput(() => p, null, 0, -1)).to.throw(
"retryCount can't be lower than 0.",
);
Expand All @@ -311,10 +320,10 @@ describe('Restartable Handle Standard Input', () => {
WebSocketHandler.processData(
'some test data',
null,
(): Promise<WebSocket> => {
return new Promise<WebSocket>((resolve) => {
(): Promise<WebSocket.WebSocket> => {
return new Promise<WebSocket.WebSocket>((resolve) => {
count++;
resolve(ws as WebSocket);
resolve(ws as WebSocket.WebSocket);
});
},
0,
Expand Down

0 comments on commit 62e8b11

Please sign in to comment.