Skip to content

Commit d24a218

Browse files
committed
correctly assign WebSocket constructor from ws in Node
1 parent b4b850c commit d24a218

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

lib/joiner.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
import {Joiner, ClientAuthenticator, Serializer, JSONSerializer} from 'wampproto';
22

3-
if (typeof globalThis.WebSocket === 'undefined') {
4-
import('ws').then((ws) => {
5-
(globalThis as any).WebSocket = ws.default;
6-
});
7-
}
8-
93
import {BaseSession} from './types';
104
import {getSubProtocol} from './helpers';
11-
5+
import "./ws-shim"
126

137
export class WAMPSessionJoiner {
148
private readonly _authenticator?: ClientAuthenticator;

lib/ws-shim.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// websocket-shim.ts
2+
if (typeof globalThis.WebSocket === "undefined") {
3+
const isNode = typeof process !== "undefined" && process.versions?.node;
4+
if (isNode) {
5+
const { createRequire } = await import("module");
6+
const require = createRequire(import.meta.url);
7+
8+
const ws = require("ws");
9+
(globalThis as any).WebSocket = ws.WebSocket || ws;
10+
}
11+
}

0 commit comments

Comments
 (0)