Skip to content

Commit 0ae6bb3

Browse files
refactor(client): take the WebSocket transport from dev-middleware
First step of moving the hot clients into webpack-dev-middleware: this package's `WebSocketClient` is a strict subset of the one that package now ships, and worse in three ways. It has no `close()` at all, despite declaring `@implements {CommunicationClient}`, which the interface requires. It has no guard against an event the socket had already queued reporting after the caller closed, so a close could schedule a reconnection nobody asked for. And it hands the url to `new WebSocket` unresolved, which throws on browsers before Chrome 125 / Firefox 124 / Safari 17.3 for a relative or `http(s):` url — this package always builds an absolute `ws:` url, so that one never bit here, but it is a trap for anyone reusing the class. Re-exported rather than deleted: `client.webSocketTransport` resolves to this path, so anything pointing at it keeps working. `import/no-unresolved` is switched off for the file, following the exemption already in place for `@changesets/get-github-info`: the import resolver cannot follow an `exports` subpath. TypeScript does resolve it, so `lint:types-client` still covers the import. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
1 parent 69077c9 commit 0ae6bb3

2 files changed

Lines changed: 18 additions & 42 deletions

File tree

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,9 @@
1-
import { log } from "../utils/log.js";
2-
3-
/** @typedef {import("../index.js").EXPECTED_ANY} EXPECTED_ANY */
4-
5-
/**
6-
* @implements {CommunicationClient}
7-
*/
8-
export default class WebSocketClient {
9-
/**
10-
* @param {string} url url to connect
11-
*/
12-
constructor(url) {
13-
this.client = new WebSocket(url);
14-
this.client.onerror = (error) => {
15-
log.error(error);
16-
};
17-
}
18-
19-
/**
20-
* @param {(...args: EXPECTED_ANY[]) => void} fn function
21-
*/
22-
onOpen(fn) {
23-
this.client.onopen = fn;
24-
}
25-
26-
/**
27-
* @param {(...args: EXPECTED_ANY[]) => void} fn function
28-
*/
29-
onClose(fn) {
30-
this.client.onclose = fn;
31-
}
32-
33-
// call f with the message string as the first argument
34-
/**
35-
* @param {(...args: EXPECTED_ANY[]) => void} fn function
36-
*/
37-
onMessage(fn) {
38-
this.client.onmessage = (err) => {
39-
fn(err.data);
40-
};
41-
}
42-
}
1+
// Re-exported rather than implemented: webpack-dev-middleware ships the same
2+
// transport, and one copy means one place for the things this one was missing —
3+
// a `close()` (which the interface declares and this file never had), the guard
4+
// that stops a queued event reporting after the caller closed, and resolving a
5+
// relative or `http(s):` url for browsers whose `WebSocket` will not.
6+
//
7+
// `client.webSocketTransport` resolves to this path, so anything pointing at it
8+
// keeps working.
9+
export { default } from "webpack-dev-middleware/client/ws";

‎eslint.config.mjs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,13 @@ export default defineConfig([
4949
"import/no-unresolved": "off",
5050
},
5151
},
52+
{
53+
// `webpack-dev-middleware/client/ws` is a subpath of that package's
54+
// `exports` map, which the import resolver cannot follow either. TypeScript
55+
// does resolve it, so `lint:types-client` still covers the import.
56+
files: ["client-src/clients/WebSocketClient.js"],
57+
rules: {
58+
"import/no-unresolved": "off",
59+
},
60+
},
5261
]);

0 commit comments

Comments
 (0)