Skip to content

v0.4.5

Latest

Choose a tag to compare

@pi0 pi0 released this 10 Apr 22:01
· 2 commits to main since this release

compare changes

🚀 Enhancements

createWebSocketProxy util (#184)

New createWebSocketProxy util returns crossws hooks that forward every incoming peer to an upstream ws:// or wss:// target. Text and binary messages, close, and error events are relayed bi-directionally, and client messages sent before the upstream is ready are buffered (up to a configurable maxBufferSize) and flushed once connected.

import crossws from "crossws/adapters/<adapter>";
import { createWebSocketProxy } from "crossws";

const ws = crossws({
  hooks: createWebSocketProxy("wss://echo.websocket.org"),
});

The target can also be a (peer) => url resolver for dynamic routing, and the returned hooks can be extended (for auth, logging, header forwarding, etc.) like any other crossws hooks. See the proxy guide for auth, dynamic targets, and SSRF hardening notes.

fromNodeUpgradeHandler util + socket.io support (#185)

New fromNodeUpgradeHandler util (exported from crossws/adapters/node) wraps a plain Node.js (req, socket, head) upgrade handler as a crossws hooks object. This makes it possible to mount existing ws, socket.io, or express-ws based WebSocket servers through the Node adapter while they keep full ownership of the socket lifecycle.

import { WebSocketServer } from "ws";
import { fromNodeUpgradeHandler } from "crossws/adapters/node";
import { serve } from "crossws/server/node";

const wss = new WebSocketServer({ noServer: true });
wss.on("connection", (ws) => {
  ws.on("message", (data) => ws.send(data));
});

serve({
  fetch: () => new Response("ok"),
  websocket: fromNodeUpgradeHandler((req, socket, head) => {
    wss.handleUpgrade(req, socket, head, (ws) => {
      wss.emit("connection", ws, req);
    });
  }),
});

Because the wrapped handler takes ownership of the socket, crossws lifecycle hooks (open/message/close/error) are not invoked for connections routed through it — the underlying library manages them. A full socket.io example combining WebSocket and HTTP long-polling transports is included in the repo.

Other

  • New bunny adapter (#179)

🩹 Fixes

  • node: Do not url-encode upgrade response headers (ffbed40)

📦 Build

  • Export ServerWithWSOptions and WSOptions types (#180)

🏡 Chore

✅ Tests

  • node: Add regression test for EADDRINUSE handling (#183)

❤️ Contributors