Skip to content

Commit

Permalink
bugfix: replace destroy on faye websockets with close() (#472)
Browse files Browse the repository at this point in the history
interface to give faye same types as compliant ws with additional `pipe()` implementation
  • Loading branch information
JacobMGEvans authored Feb 16, 2022
1 parent 21cde50 commit 804523a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/few-cherries-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"wrangler": patch
---

bugfix: Replace `.destroy()` on `faye-websockets` with `.close()`
added: Interface to give faye same types as compliant `ws` with additional `.pipe()` implementation; `.on("message" => fn)`
14 changes: 10 additions & 4 deletions packages/wrangler/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import type {
Server,
} from "node:http";
import type { ClientHttp2Session, ServerHttp2Stream } from "node:http2";
import type ws from "ws";

interface IWebsocket extends ws {
// Pipe implements .on("message", ...)
pipe<T>(fn: T): IWebsocket;
}

/**
* `usePreviewServer` is a React hook that creates a local development
Expand Down Expand Up @@ -207,18 +213,18 @@ export function usePreviewServer({
const { headers, url } = message;
addCfPreviewTokenHeader(headers, previewToken.value);
headers["host"] = previewToken.host;
const localWebsocket = new WebSocket(message, socket, body);
const localWebsocket = new WebSocket(message, socket, body) as IWebsocket;
// TODO(soon): Custom WebSocket protocol is not working?
const remoteWebsocketClient = new WebSocket.Client(
`wss://${previewToken.host}${url}`,
[],
{ headers }
);
) as IWebsocket;
localWebsocket.pipe(remoteWebsocketClient).pipe(localWebsocket);
// We close down websockets whenever we refresh the token.
cleanupListeners.push(() => {
localWebsocket.destroy();
remoteWebsocketClient.destroy();
localWebsocket.close();
remoteWebsocketClient.close();
});
};
proxy.on("upgrade", handleUpgrade);
Expand Down

0 comments on commit 804523a

Please sign in to comment.