fix: use browser-safe WebSocket close code

This commit is contained in:
2026-09-29 15:38:12 +09:00
parent 39b4a019c9
commit 5f19dcc93f
2 changed files with 10 additions and 2 deletions
@@ -32,6 +32,13 @@ class FakeWebSocket {
}
close(code?: number, reason?: string): void {
if (
code !== undefined &&
code !== 1000 &&
(code < 3000 || code > 4999)
) {
throw new DOMException('Invalid WebSocket close code', 'InvalidAccessError');
}
this.closes.push([code, reason]);
if (this.readyState === FakeWebSocket.CLOSED) return;
this.readyState = FakeWebSocket.CLOSED;
@@ -91,7 +98,7 @@ test('invalid inbound frame closes before dispatch and reconnects for a fresh sn
);
expect(onFrame).not.toHaveBeenCalled();
expect(first.closes).toEqual([[1002, 'Invalid workspace protocol frame']]);
expect(first.closes).toEqual([[4002, 'Invalid workspace protocol frame']]);
expect(onStatus).toHaveBeenLastCalledWith('closed', 'Workspace protocol frame rejected');
expect(JSON.stringify(onStatus.mock.calls)).not.toContain('untrusted detail');
@@ -30,6 +30,7 @@ export type WorkspaceMultiplexerSubscription = {
};
const multiplexers = new Map<string, WorkspaceMultiplexer>();
const INVALID_PROTOCOL_CLOSE_CODE = 4002;
let nextMultiplexerSequence = 0;
function nextMultiplexerId(kind: 'client' | 'request'): string {
@@ -205,7 +206,7 @@ export class WorkspaceMultiplexer {
#rejectInboundFrame(): void {
this.#disconnectMessage = 'Workspace protocol frame rejected';
this.#socket?.close(1002, 'Invalid workspace protocol frame');
this.#socket?.close(INVALID_PROTOCOL_CLOSE_CODE, 'Invalid workspace protocol frame');
}
#sendWorkerMethod(clientId: string, method: Method): void {