miniflare icon indicating copy to clipboard operation
miniflare copied to clipboard

"pair[kSend] is not a function" error after upgrading from 2.7.1 -> 2.9.0

Open broofa opened this issue 3 years ago • 4 comments

'Just upgraded from 2.7.1 to latest (2.9.0) and am now getting the following when trying to set up the WebSocketPair in my durable object:

TypeError: pair[kSend] is not a function
    at WebSocket.<anonymous> (/Users/kieffer/codepen/cp/node_modules/@miniflare/http-server/node_modules/@miniflare/web-sockets/src/couple.ts:37:12)
    at WebSocket.emit (node:events:526:28)
    at WebSocket.emit (node:domain:475:12)
    at Receiver.receiverOnMessage (/Users/kieffer/codepen/cp/node_modules/ws/lib/websocket.js:1178:20)
    at Receiver.emit (node:events:526:28)
    at Receiver.emit (node:domain:475:12)
    at Receiver.dataMessage (/Users/kieffer/codepen/cp/node_modules/ws/lib/receiver.js:513:14)
    at Receiver.getData (/Users/kieffer/codepen/cp/node_modules/ws/lib/receiver.js:446:17)
    at Receiver.startLoop (/Users/kieffer/codepen/cp/node_modules/ws/lib/receiver.js:148:22)
    at Receiver._write (/Users/kieffer/codepen/cp/node_modules/ws/lib/receiver.js:83:10)
error Command failed with exit code 1.

I don't have a simple way to repro at the moment, but here's the durable object code that set's up the WebSocketPair:

  async fetch(request) {
    if (request.headers.get('Upgrade') !== 'websocket') {
      return new Response('expected websocket', { status: 400 });
    }

    const { 0: wsRemote, 1: wsLocal } = new WebSocketPair();

    const cleanup = () => {
      // ... clean up state here
      wsLocal.removeEventListener('close', cleanup);
      wsLocal.removeEventListener('error', cleanup);
    };

    wsLocal.addEventListener('close', cleanup);
    wsLocal.addEventListener('error', cleanup);

    wsLocal.accept();

    // Info & lifecycle management (not intended for user-facing features)
    wsLocal.addEventListener('message', ({ data: msg }) => {
      // ... handle ws messages here
    });

    return new Response(null, { status: 101, webSocket: wsRemote });
  }

broofa avatar Sep 21 '22 20:09 broofa

Hey! 👋 Any chance you've got wrangler or jest-environment-miniflare installed too? I've seen similar issues before where multiple versions of Miniflare end up installed. Could you try removing your node_modules directory and package-lock.json file, then reinstall?

mrbbot avatar Sep 21 '22 20:09 mrbbot

Yes to both wrangler and jest-environment-miniflare.

Deleting node_modules and yarn.lock (we're using yarn) appears to fix the problem. Weird.

Feel free to close I guess. Or let me know what, if any, information I can provide to help track this issue down.

broofa avatar Sep 21 '22 21:09 broofa

Should've asked before, but any chance you've got your old yarn.lock? Might be able to get something from that. 🙂

mrbbot avatar Sep 21 '22 21:09 mrbbot

Should've asked before, but any chance you've got your old yarn.lock? Might be able to get something from that. 🙂

I do, but sharing that may be difficult due to security concerns. Let me see what I can do.

broofa avatar Sep 22 '22 13:09 broofa

Hey! Thanks for raising this issue! I'm going to close it for now since we haven't heard from you in a while, but if there's still outstanding questions feel free to comment with a minimal reproduction and we can open it again and investigate further.

penalosa avatar Jan 09 '23 16:01 penalosa