fastify / fastify-websocket

basic websocket support for fastify
MIT License
394 stars 72 forks source link

`injectWS` throws on node >= 20 #299

Closed MoLow closed 3 months ago

MoLow commented 3 months ago

Prerequisites

Fastify version

4.27.0

Plugin version

No response

Node.js version

20.x, 22.x

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

13

Description

I am running this simple code using injectWs:

const fastify = require("fastify")();
const message = "hi from client";
let _resolve;
const promise = new Promise((resolve) => {
  _resolve = resolve;
});

fastify.register(require("@fastify/websocket"));
fastify.register(async function (instance) {
  instance.get("/ws", { websocket: true }, function (conn) {
    conn.once("message", (chunk) => {
      _resolve(chunk.toString());
    });
  });
});

fastify.ready().then(async () => {
  const ws = await fastify.injectWS("/ws");
  ws.send(message);
  console.log(await promise);
  ws.terminate();
});

code runs ok on node 18 & 19 and fails on node > 20 (including node 22, and node main branch):

/Users/moshe/repos/whatslly/apps/messaging-websocket-consumer/node_modules/stream-shift/index.js:16
    return state.buffer[0].length
                           ^

TypeError: Cannot read properties of null (reading 'length')
    at getStateLength (/Users/moshe/repos/whatslly/apps/messaging-websocket-consumer/node_modules/stream-shift/index.js:16:28)
    at shift (/Users/moshe/repos/whatslly/apps/messaging-websocket-consumer/node_modules/stream-shift/index.js:6:99)
    at Duplexify._forward (/Users/moshe/repos/whatslly/apps/messaging-websocket-consumer/node_modules/duplexify/index.js:170:35)
    at PassThrough.onreadable (/Users/moshe/repos/whatslly/apps/messaging-websocket-consumer/node_modules/duplexify/index.js:136:10)
    at PassThrough.emit (node:events:520:28)
    at emitReadable_ (node:internal/streams/readable:832:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:81:21)

Link to code that reproduces the bug

No response

Expected Behavior

No response

climba03003 commented 3 months ago

Can you run npm update stream-shift and test again? From your reported error, it shows you are using the outdated package.

MoLow commented 3 months ago

thanks, that fixed my issue