Skip to content

WebSocket client using npm:ws errors out with Bad resource ID #27802

Closed
@magurotuna

Description

Issue

WebSocket client created using npm:ws errors out when establishing a connection via TLS.

Reproducer

Server

Run this script in Node.js (it's runnable in Deno as well, but let's run it in Node so that we can be sure that this is a client side issue)

import { createServer } from "node:https";
import { readFileSync } from "node:fs";
import { WebSocketServer } from "ws";

const server = createServer({
  cert: readFileSync("<your-cert>"),
  key: readFileSync("<your-key>"),
});

const wss = new WebSocketServer({ server });

wss.on("connection", function connection(ws) {
  ws.on("error", console.error);

  ws.on("message", function message(data) {
    console.log("received: %s", data);
  });

  ws.send("something");
});

server.listen(8080);

Client

import WebSocket from "npm:ws@8.18.0";

const ws = new WebSocket("wss://<some-domain>:8080");

ws.on("error", console.error);

ws.on("open", function open() {
  ws.send("something");
});

Steps

Start the server in Node;

$ node server.mjs

Then run the client in Deno:

$ deno run -A client.ts
BadResource: Bad resource ID
    at node:http:312:27
    at HttpsClientRequest._writeHeader (node:http:404:7)
    at HttpsClientRequest._flushHeaders (node:_http_outgoing:382:12)
    at TLSSocket.onConnect (node:http:450:16)
    at TLSSocket.emit (ext:deno_node/_events.mjs:405:35)
    at TCPConnectWrap._afterConnect [as oncomplete] (node:net:159:12)
    at TCP.afterConnect (ext:deno_node/internal_binding/connection_wrap.ts:43:11)
    at ext:deno_node/internal_binding/tcp_wrap.ts:299:14
    at eventLoopTick (ext:core/01_core.js:177:7) {
  name: "BadResource"
}

Additional info

Version: Deno 2.1.7

I did a bisecting, and confirmed that 960776c introduced this issue.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions