Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { KubeConfig } from './config.js';
import { isResizable, ResizableStream, TerminalSizeQueue } from './terminal-size-queue.js';
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler.js';

export interface ExecOptions {
pingIntervalMs?: number;
}

export class Exec {
public 'handler': WebSocketInterface;

Expand Down Expand Up @@ -39,6 +43,7 @@ export class Exec {
stdin: stream.Readable | null,
tty: boolean,
statusCallback?: (status: V1Status) => void,
options?: ExecOptions,
): Promise<WebSocket.WebSocket> {
const query = {
stdout: stdout != null,
Expand All @@ -60,6 +65,10 @@ export class Exec {
}
return true;
});
const pingIntervalMs = options?.pingIntervalMs;
if (pingIntervalMs !== undefined && Number.isInteger(pingIntervalMs) && pingIntervalMs > 0) {
this.setupPing(conn, pingIntervalMs);
}
if (stdin != null) {
WebSocketHandler.handleStandardInput(conn, stdin, WebSocketHandler.StdinStream);
}
Expand All @@ -70,4 +79,23 @@ export class Exec {
}
return conn;
}

private setupPing(conn: WebSocket.WebSocket, pingIntervalMs: number): void {
const socket = conn as WebSocket.WebSocket & {
ping?: () => void;
on?: (event: string, listener: () => void) => void;
};
if (typeof socket.ping !== 'function') {
return;
}

const timer = setInterval(() => {
if (conn.readyState === WebSocket.OPEN) {
socket.ping!();
}
}, pingIntervalMs);
const clearKeepAlive = () => clearInterval(timer);
socket.on?.('close', clearKeepAlive);
socket.on?.('error', clearKeepAlive);
}
}
38 changes: 37 additions & 1 deletion src/exec_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it } from 'node:test';
import { deepStrictEqual, strictEqual } from 'node:assert';
import { deepStrictEqual, ok, strictEqual } from 'node:assert';
import { setTimeout as setTimeoutPromise } from 'node:timers/promises';
import WebSocket from 'isomorphic-ws';
import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers';
import { anyFunction, anything, capture, instance, mock, verify, when } from 'ts-mockito';
Expand Down Expand Up @@ -156,5 +157,40 @@ describe('Exec', () => {
await closePromise;
verify(fakeWebSocket.close()).called();
});

it('should optionally send websocket pings', async () => {
const pingIntervalMs = 5;
const waitForPingsMs = 25;
const kc = new KubeConfig();
const pingHandlers: Record<string, () => void> = {};
let pingCount = 0;
const fakeConn = {
readyState: WebSocket.OPEN,
ping: () => {
pingCount++;
},
on: (event: string, listener: () => void) => {
pingHandlers[event] = listener;
},
} as unknown as WebSocket.WebSocket;
const ws: WebSocketInterface = {
connect: async () => fakeConn,
};
const exec = new Exec(kc, ws);

await exec.exec('ns', 'pod', 'container', 'command', null, null, null, false, undefined, {
pingIntervalMs,
});
await setTimeoutPromise(waitForPingsMs);

ok(pingCount > 0);

strictEqual(typeof pingHandlers.close, 'function');
pingHandlers.close();
const pingCountAtClose = pingCount;
await setTimeoutPromise(waitForPingsMs);

strictEqual(pingCount, pingCountAtClose);
});
});
});