Skip to content

Commit 99b0f18

Browse files
fix: properly precompute the WebSocket frames
The named import is not supported in some cases: > node_modules/socket.io-adapter/dist/index.js:170 > packetOpts.wsPreEncodedFrame = ws_1.WebSocket.Sender.frame(data, { > ^ > > TypeError: Cannot read properties of undefined (reading 'Sender') > at RedisAdapter._encode (/.../node_modules/socket.io-adapter/dist/index.js:170:59) > at RedisAdapter.broadcast (/.../node_modules/socket.io-adapter/dist/index.js:117:37) Related: socketio/socket.io-redis-adapter#478
1 parent 00a8e75 commit 99b0f18

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { EventEmitter } from "events";
22
import { yeast } from "./contrib/yeast";
3-
import { WebSocket } from "ws";
3+
import WebSocket = require("ws");
4+
5+
const canPreComputeFrame = typeof WebSocket?.Sender?.frame === "function";
46

57
/**
68
* A public ID, sent by the server at the beginning of the Socket.IO session and which can be used for private messaging
@@ -231,7 +233,11 @@ export class Adapter extends EventEmitter {
231233
private _encode(packet: unknown, packetOpts: Record<string, unknown>) {
232234
const encodedPackets = this.encoder.encode(packet);
233235

234-
if (encodedPackets.length === 1 && typeof encodedPackets[0] === "string") {
236+
if (
237+
canPreComputeFrame &&
238+
encodedPackets.length === 1 &&
239+
typeof encodedPackets[0] === "string"
240+
) {
235241
// "4" being the "message" packet type in the Engine.IO protocol
236242
const data = Buffer.from("4" + encodedPackets[0]);
237243
// see https://github.com/websockets/ws/issues/617#issuecomment-283002469

0 commit comments

Comments
 (0)