Skip to content

"https" and "http" deno compatibility shims do not support WebSocket connections #17846

@zookatron

Description

@zookatron

If you run this code sample with NodeJS v18.12.1, it will output "upgraded":

import https from "https";

const req = https.request({
  hostname: "socketsbay.com",
  port: 443,
  path: "/wss/v2/1/demo/",
  method: "GET",
  headers: {
    "Sec-WebSocket-Version": 13,
    "Sec-WebSocket-Key": "xqaVaSr2HxSeH/R2p2J2Hw==",
    Connection: "Upgrade",
    Upgrade: "websocket",
  },
});

req.on("response", () => console.log("not upgraded"));
req.on("upgrade", () => console.log("upgraded"));

req.end();

If you run this equivalent code sample with Deno v1.29.3, it will output "not upgraded":

import https from "https://deno.land/std@0.172.0/node/https.ts";

const req = https.request({
  hostname: "socketsbay.com",
  port: 443,
  path: "/wss/v2/1/demo/",
  method: "GET",
  headers: {
    "Sec-WebSocket-Version": 13,
    "Sec-WebSocket-Key": "xqaVaSr2HxSeH/R2p2J2Hw==",
    Connection: "Upgrade",
    Upgrade: "websocket",
  },
});

req.on("response", () => console.log("not upgraded"));
req.on("upgrade", () => console.log("upgraded"));

req.end();

This looks like it is due to the fact that Deno NodeJS compatibility shims for the http and https NodeJS built-in packages always use the "fetch" method which doesn't support websocket upgrades: https://deno.land/std@0.172.0/node/http.ts?source#L137

It seems like this should be fixable without a huge amount of work just by modifying the _final function in ClientRequest to look at the headers that will be used for the request and have it use the WebSocket class instead of fetch if the necessary WebSocket headers like Upgrade: websocket are set. Let me know if this is something you would accept contributions on, this is something I could potentially help out with if this approach seems acceptable.

This seems to be the only blocker preventing the popular discord.js library from working with Deno NodeJS compatibility mode, as discussed here: #17391

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions