Skip to content
This repository was archived by the owner on Aug 31, 2026. It is now read-only.
Closed
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
67 changes: 67 additions & 0 deletions src/__tests__/package-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ const WEBSOCKET_MODULES = [
] as const;

const mockWsAsUnavailable = (): void => {
globalThis.WebSocket = undefined as unknown as typeof WebSocket;
jest.doMock('ws', () => {
throw new Error('ws is not available in this environment');
});
};

describe('package imports do not crash when `ws` is unavailable', () => {
const originalWebSocket = globalThis.WebSocket;

afterEach(() => {
globalThis.WebSocket = originalWebSocket;
jest.resetModules();
});

describe.each(WEBSOCKET_MODULES)('%s', (modulePath) => {
it('does not throw at module load', () => {
expect(() => {
Expand Down Expand Up @@ -130,3 +138,62 @@ describe('package imports do not crash when `ws` is unavailable', () => {
expect(captured?.message).toMatch(/WebSocket implementation not available/i);
});
});

describe('modern Node.js WebSocket support', () => {
const originalWebSocket = globalThis.WebSocket;

afterEach(() => {
globalThis.WebSocket = originalWebSocket;
jest.resetModules();
});

it.each([
{
modulePath: '../events/websocket-client',
exportName: 'WebSocketCallbackClient',
options: {
host: 'http://example.com',
conversationId: 'conv-1',
callback: () => {},
},
expectedUrl: 'ws://example.com/sockets/events/conv-1',
},
{
modulePath: '../events/bash-websocket-client',
exportName: 'BashWebSocketClient',
options: {
host: 'http://example.com',
callback: () => {},
},
expectedUrl: 'ws://example.com/sockets/bash-events',
},
])(
'$exportName uses globalThis.WebSocket',
({ modulePath, exportName, options, expectedUrl }) => {
const urls: string[] = [];
class FakeWebSocket {
onopen?: () => void;
onmessage?: (event: { data: unknown }) => void;
onclose?: () => void;
onerror?: () => void;

constructor(url: string) {
urls.push(url);
}

close(): void {}
}
globalThis.WebSocket = FakeWebSocket as unknown as typeof WebSocket;

jest.isolateModules(() => {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const module = require(modulePath);
const client = new module[exportName](options);
client.start();
client.stop();
});

expect(urls).toEqual([expectedUrl]);
}
);
});
6 changes: 4 additions & 2 deletions src/events/bash-websocket-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
import { BashEvent } from '../models/workspace';
import { ErrorCallbackType } from './websocket-client';

// Prefer the standards-compatible global in browsers and modern Node.js,
// falling back to ws where CommonJS require is available.
// IMPORTANT: this block must never throw. See the matching note in
// `events/websocket-client.ts` — the "no WebSocket implementation"
// condition is deferred to connect() time so importing this module does
// not crash consumers that never use bash event streaming.
let WebSocketImpl: any;

Check warning on line 14 in src/events/bash-websocket-client.ts

View workflow job for this annotation

GitHub Actions / test (22.12)

Unexpected any. Specify a different type

Check warning on line 14 in src/events/bash-websocket-client.ts

View workflow job for this annotation

GitHub Actions / test (24.x)

Unexpected any. Specify a different type

if (typeof window !== 'undefined' && window.WebSocket) {
WebSocketImpl = window.WebSocket;
if (typeof globalThis.WebSocket !== 'undefined') {
WebSocketImpl = globalThis.WebSocket;
} else {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
Expand All @@ -37,7 +39,7 @@
private apiKey?: string;
private resendMode?: 'all';
private onError?: ErrorCallbackType;
private ws?: any;

Check warning on line 42 in src/events/bash-websocket-client.ts

View workflow job for this annotation

GitHub Actions / test (22.12)

Unexpected any. Specify a different type

Check warning on line 42 in src/events/bash-websocket-client.ts

View workflow job for this annotation

GitHub Actions / test (24.x)

Unexpected any. Specify a different type
private reconnectDelay = 1000;
private maxReconnectDelay = 30000;
private currentDelay = 1000;
Expand Down
11 changes: 6 additions & 5 deletions src/events/websocket-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import { Event, ConversationCallbackType } from '../types/base';

// Use native WebSocket in browser, ws library in Node.js.
// Prefer the standards-compatible global in browsers and modern Node.js,
// falling back to ws where CommonJS require is available.
//
// IMPORTANT: this block must never throw. It runs whenever this file is
// imported, and this file is transitively imported by the package barrel
Expand All @@ -13,13 +14,13 @@
// even when they have no intent to open a WebSocket. The "no implementation
// available" condition is deferred to connect() time, where it is surfaced
// through the existing onError callback channel.
let WebSocketImpl: any;

Check warning on line 17 in src/events/websocket-client.ts

View workflow job for this annotation

GitHub Actions / test (22.12)

Unexpected any. Specify a different type

Check warning on line 17 in src/events/websocket-client.ts

View workflow job for this annotation

GitHub Actions / test (24.x)

Unexpected any. Specify a different type

if (typeof window !== 'undefined' && window.WebSocket) {
// Browser environment
WebSocketImpl = window.WebSocket;
if (typeof globalThis.WebSocket !== 'undefined') {
// Browser or modern Node.js environment
WebSocketImpl = globalThis.WebSocket;
} else {
// Node.js environment
// Older Node.js or another environment without a global WebSocket
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const ws = require('ws');
Expand Down Expand Up @@ -51,7 +52,7 @@
private callback: ConversationCallbackType;
private apiKey?: string;
private onError?: ErrorCallbackType;
private ws?: any; // WebSocket instance (browser or Node.js)

Check warning on line 55 in src/events/websocket-client.ts

View workflow job for this annotation

GitHub Actions / test (22.12)

Unexpected any. Specify a different type

Check warning on line 55 in src/events/websocket-client.ts

View workflow job for this annotation

GitHub Actions / test (24.x)

Unexpected any. Specify a different type
private reconnectDelay = 1000;
private maxReconnectDelay = 30000;
private currentDelay = 1000;
Expand Down Expand Up @@ -111,7 +112,7 @@
this.currentDelay = this.reconnectDelay;
};

this.ws.onmessage = (event: { data: any }) => {

Check warning on line 115 in src/events/websocket-client.ts

View workflow job for this annotation

GitHub Actions / test (22.12)

Unexpected any. Specify a different type

Check warning on line 115 in src/events/websocket-client.ts

View workflow job for this annotation

GitHub Actions / test (24.x)

Unexpected any. Specify a different type
try {
const message = typeof event.data === 'string' ? event.data : event.data.toString();
const eventData: Event = JSON.parse(message);
Expand Down
Loading