Skip to content

Commit

Permalink
fix: properly precompute the WebSocket frames
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darrachequesne committed Jan 6, 2023
1 parent 00a8e75 commit 99b0f18
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { EventEmitter } from "events";
import { yeast } from "./contrib/yeast";
import { WebSocket } from "ws";
import WebSocket = require("ws");

const canPreComputeFrame = typeof WebSocket?.Sender?.frame === "function";

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

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

0 comments on commit 99b0f18

Please sign in to comment.