mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
refactor(websocket-tester): use the native WebSocket instead of w-websocket-client (#558)
* refactor(websocket-tester): use the native WebSocket instead of w-websocket-client
w-websocket-client ships a single 1465 byte UMD that wraps the platform
WebSocket: it builds `${url}/?token=${token}`, wires four handlers and returns
the socket. It nevertheless declares wsemi and lodash-es as runtime
dependencies, which its published build never imports, and wsemi alone pulls in
20 packages. Installing all of that to avoid writing twenty lines is a bad
trade, so the wrapper now lives here, behind tests.
Removes 14 packages from the lockfile (w-websocket-client, wsemi, diff2html,
tippy.js, @popperjs/core, seamless-scroll-polyfill, tree-kill, eventemitter3,
xxhash-wasm and their own transitives), and drops the Node-only `ws` from the
browser app's dependency graph; `ws` now only comes in through ethers.
Two follow-on effects:
- wsemi was the only subdependency wanting xlsx, which is why the xlsx CDN
override forced pnpm's blockExoticSubdeps protection off repo-wide. Only this
package depends on xlsx now, so the protection goes back to its default. A
forced re-resolution of all 2484 packages passes with it enabled and leaves
the lockfile byte identical.
- Connecting with an invalid url used to return { error } from the wrapper
instead of a socket, so the next send() threw 'wsc.send is not a function'
and nothing was ever logged. The failure is now reported in the log.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Mf5V2jMpFFXxGVXZU5rHBq
* fix(websocket-tester): make connection errors readable and normalize the url
Both from review feedback on the pull request:
- The log showed 'Error: [object Event]' for a failed connection, because the
native onerror handler receives a bare Event whose reason the websocket spec
deliberately withholds. formatWebSocketError() turns it into 'connection
failed', keeps the message of a real Error, and stringifies anything else.
The old client had the same flaw, but this pr claims to report connection
failures in the log, so it should actually report something.
- buildWebSocketUrl() now trims a trailing slash, so 'ws://host/' produces
'ws://host/?token=...' rather than 'ws://host//?token=...'. This is a
deliberate deviation from the previous client, which concatenated blindly.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Mf5V2jMpFFXxGVXZU5rHBq
---------
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
5e4972e285
commit
903ffb8b90
7 files changed
+208
-160
No files matched your search
@@ -393,7 +393,6 @@
|
||||
"vue3-katex": "^0.8.1",
|
||||
"vuedraggable": "^4.1.0",
|
||||
"vuex": "^4.1.0",
|
||||
"w-websocket-client": "^1.0.29",
|
||||
"webcrack": "^2.16.0",
|
||||
"webcrypto-liner-shim": "^1.4.8",
|
||||
"webp-converter-browser": "^1.0.4",
|
||||
|
||||
Generated
-118
@@ -1068,9 +1068,6 @@ importers:
|
||||
vuex:
|
||||
specifier: ^4.1.0
|
||||
version: 4.1.0(vue@3.5.39(typescript@6.0.3))
|
||||
w-websocket-client:
|
||||
specifier: ^1.0.29
|
||||
version: 1.0.29
|
||||
webcrack:
|
||||
specifier: ^2.16.0
|
||||
version: 2.16.0
|
||||
@@ -3734,13 +3731,6 @@ packages:
|
||||
'@polka/url@1.0.0-next.29':
|
||||
resolution: {integrity: sha512-wwQAWhWSuHaag8c4q/KN/vCoeOJYshAIvMQwD4GpSb3OiZklFfvAgmj0VCBBImRpuF/aFgIRzllXlVX93Jevww==}
|
||||
|
||||
'@popperjs/core@2.11.8':
|
||||
resolution: {integrity: sha512-P1st0aksCrn9sGZhp8GMYwBnQsbvAWsZAX44oXNNvLHGqAOcoVxmjZiohstwQ7SqKnbR47akdNi+uleWD8+g6A==}
|
||||
|
||||
'@profoundlogic/hogan@3.0.4':
|
||||
resolution: {integrity: sha512-pmNVGuooS30Mm7YbZd5T7E5zYVO6D5Ct91sn4T39mUvMUc3sCGridcnhAufL1/Bz2QzAtzEn0agNrdk3+5yWzw==}
|
||||
hasBin: true
|
||||
|
||||
'@protobufjs/aspromise@1.1.2':
|
||||
resolution: {integrity: sha512-j+gKExEuLmKwvz3OgROXtrJ2UG2x8Ch2YZUxahh+s1F2HZ+wAceUNLkvy6zKCPVRkU++ZWQrdxsUeQXmcg4uoQ==}
|
||||
|
||||
@@ -5166,9 +5156,6 @@ packages:
|
||||
resolution: {integrity: sha512-INSv5cMPGvJ/jMLvIxRTT9CDQqxODOhoedaQ2+yWIwbxhKyOns9m/7aJjXVQjxp1rntkbiL4lBUtikuH5YK6Cg==}
|
||||
engines: {node: '>=16.6.0'}
|
||||
|
||||
abbrev@1.1.1:
|
||||
resolution: {integrity: sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==}
|
||||
|
||||
abbrev@2.0.0:
|
||||
resolution: {integrity: sha512-6/mh1E2u2YgEsCHdY0Yx5oW+61gZU+1vXaoiHHrpKeuRNNgFvS+/jrwHiQhB5apAf5oB7UB7E19ol2R2LKH8hQ==}
|
||||
engines: {node: ^14.17.0 || ^16.13.0 || >=18.0.0}
|
||||
@@ -6487,18 +6474,6 @@ packages:
|
||||
resolution: {integrity: sha512-EjePK1srD3P08o2j4f0ExnylqRs5B9tJjcp9t1krH2qRi8CCdsYfwe9JgSLurFBWwq4uOlipzfk5fHNvwFKr8Q==}
|
||||
engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0}
|
||||
|
||||
diff2html@3.4.56:
|
||||
resolution: {integrity: sha512-u9gfn+BlbHcyO7vItCIC4z49LJDUt31tODzOfAuJ5R1E7IdlRL6KjugcB9zOpejD+XiR+dDZbsnHSQ3g6A/u8A==}
|
||||
engines: {node: '>=12'}
|
||||
|
||||
diff@8.0.4:
|
||||
resolution: {integrity: sha512-DPi0FmjiSU5EvQV0++GFDOJ9ASQUVFh5kD+OzOnYdi7n3Wpm9hWWGfB/O2blfHcMVTL5WkQXSnRiK9makhrcnw==}
|
||||
engines: {node: '>=0.3.1'}
|
||||
|
||||
diff@9.0.0:
|
||||
resolution: {integrity: sha512-svtcdpS8CgJyqAjEQIXdb3OjhFVVYjzGAPO8WGCmRbrml64SPw/jJD4GoE98aR7r25A0XcgrK3F02yw9R/vhQw==}
|
||||
engines: {node: '>=0.3.1'}
|
||||
|
||||
diffie-hellman@5.0.3:
|
||||
resolution: {integrity: sha512-kqag/Nl+f3GwyK25fhUMYj81BUOrZ9IuJsjIcDE5icNM9FJHAVm3VcUDxdLPoQtTuUylWm6ZIknYJwwaPxsUzg==}
|
||||
|
||||
@@ -6884,9 +6859,6 @@ packages:
|
||||
resolution: {integrity: sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==}
|
||||
engines: {node: '>=6'}
|
||||
|
||||
eventemitter3@5.0.4:
|
||||
resolution: {integrity: sha512-mlsTRyGaPBjPedk6Bvw+aqbsXDtoAyAzm5MO7JgU+yVRyMQ5O8bD4Kcci7BS85f93veegeCPkL8R4GLClnjLFw==}
|
||||
|
||||
events-universal@1.0.1:
|
||||
resolution: {integrity: sha512-LUd5euvbMLpwOF8m6ivPCbhQeSiYVNb8Vs0fQ8QjXo0JTkEHpz8pxdQf0gStltaPpw0Cca8b39KxvK9cfKRiAw==}
|
||||
|
||||
@@ -8944,10 +8916,6 @@ packages:
|
||||
resolution: {integrity: sha512-Gwv8SQewT616ZM/URn0H54b8PWo/Wum7md3EW2aWy1lO27+WZCX+Xyak3J+NlmHUjDh5ME+uesJUDRbR3Ye8Bw==}
|
||||
engines: {node: '>=6.0.0'}
|
||||
|
||||
nopt@1.0.10:
|
||||
resolution: {integrity: sha512-NWmpvLSqUrgrAC9HCuxEvb+PSloHpqVu+FqcO4eeF2h5qYRhA7ev6KvelyQAKtegUbC6RypJnlEOhd8vloNKYg==}
|
||||
hasBin: true
|
||||
|
||||
nopt@10.0.1:
|
||||
resolution: {integrity: sha512-df3sBr/6ax9hSGuC3CspvLlbnX8cP5L5nZwXF8cGN8l0zSWR6BvzmQ6jPUKjvo6+/xdpkNvEcucBNUdBeeV13g==}
|
||||
engines: {node: ^22.22.2 || ^24.15.0 || >=26.0.0}
|
||||
@@ -9937,9 +9905,6 @@ packages:
|
||||
scule@1.3.0:
|
||||
resolution: {integrity: sha512-6FtHJEvt+pVMIB9IBY+IcCJ6Z5f1iQnytgyfKMhDKgmzYG+TeH/wx1y3l27rshSbLiSanrR9ffZDrEsmjlQF2g==}
|
||||
|
||||
seamless-scroll-polyfill@2.3.4:
|
||||
resolution: {integrity: sha512-Biobd4LDoeFODX1bfiru84GveSajzFELFB3ejMZsRR2TpD73W46uEZYAEqSx5RtKpN2umhGWaoSRivs0ZcHp9g==}
|
||||
|
||||
section-matter@1.0.0:
|
||||
resolution: {integrity: sha512-vfD3pmTzGpufjScBh50YHKzEu2lxBWhVEHsNGoEXmCmn2hKGfeNLYMzCJpe8cD7gqX7TJluOVpBkAequ6dgMmA==}
|
||||
engines: {node: '>=4'}
|
||||
@@ -10436,9 +10401,6 @@ packages:
|
||||
resolution: {integrity: sha512-Bf+ILmBgretUrdJxzXM0SgXLZ3XfiaUuOj/IKQHuTXip+05Xn+uyEYdVg0kYDipTBcLrCVyUzAPz7QmArb0mmw==}
|
||||
engines: {node: '>=14.0.0'}
|
||||
|
||||
tippy.js@6.3.7:
|
||||
resolution: {integrity: sha512-E1d3oP2emgJ9dRQZdf3Kkn0qJgI6ZLpyS5z6ZkY1DF3kaQaBsGZsndEpHwx+eC+tYM41HaSNvNtLx8tU57FzTQ==}
|
||||
|
||||
title-case@2.1.1:
|
||||
resolution: {integrity: sha512-EkJoZ2O3zdCz3zJsYCsxyq2OC5hrxR9mfdd5I+w8h/tmFfeOxJ+vvkxsKxdmN0WtS9zLdHEgfgVOiMVgv+Po4Q==}
|
||||
|
||||
@@ -10509,10 +10471,6 @@ packages:
|
||||
resolution: {integrity: sha512-bLVMLPtstlZ4iMQHpFHTR7GAGj2jxi8Dg0s2h2MafAE4uSWF98FC/3MomU51iQAMf8/qDUbKWf5GxuvvVcXEhw==}
|
||||
engines: {node: '>=20'}
|
||||
|
||||
tree-kill@1.2.2:
|
||||
resolution: {integrity: sha512-L0Orpi8qGpRG//Nd+H90vFB+3iHnue1zSSGmNOOCh1GLJ7rUKVwV2HvijphGQS2UmhUZewS9VgvxYIdgr+fG1A==}
|
||||
hasBin: true
|
||||
|
||||
tree-sitter-bash@0.23.3:
|
||||
resolution: {integrity: sha512-36cg/GQ2YmIbeiBeqeuh4fBJ6i4kgVouDaqTxqih5ysPag+zHufyIaxMOFeM8CeplwAK/Luj1o5XHqgdAfoCZg==}
|
||||
peerDependencies:
|
||||
@@ -11209,9 +11167,6 @@ packages:
|
||||
peerDependencies:
|
||||
vue: ^3.2.0
|
||||
|
||||
w-websocket-client@1.0.29:
|
||||
resolution: {integrity: sha512-zZg+dOo4EP3BmvpjlkpThKIgTAzCHoOFSSp6xSNY6EuywM2xDqfkiQU7Ga3QlzEMC9i8TwdXfkntRauA/D2KWA==}
|
||||
|
||||
w3c-keyname@2.2.8:
|
||||
resolution: {integrity: sha512-dpojBhNsCNN7T82Tm7k26A6G9ML3NkhDsnw9n/eoxSRlVBB4CEtIQ/KTCLI2Fwf3ataSXRhYFkQi3SlnFwPvPQ==}
|
||||
|
||||
@@ -11421,9 +11376,6 @@ packages:
|
||||
utf-8-validate:
|
||||
optional: true
|
||||
|
||||
wsemi@1.8.63:
|
||||
resolution: {integrity: sha512-Udfa1Eh1tGDIWzR8ysHeCIyhXO7hdV4dmdaECG4wN5MeZG0gdiIO5EONHxRL609uMFSoHfR4VowVaAYET6K0sg==}
|
||||
|
||||
wsl-utils@0.3.1:
|
||||
resolution: {integrity: sha512-g/eziiSUNBSsdDJtCLB8bdYEUMj4jR7AGeUo96p/3dTafgjHhpF4RiCFPiRILwjQoDXx5MqkBr4fwWtR3Ky4Wg==}
|
||||
engines: {node: '>=20'}
|
||||
@@ -11525,9 +11477,6 @@ packages:
|
||||
resolution: {integrity: sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==}
|
||||
engines: {node: '>=0.4'}
|
||||
|
||||
xxhash-wasm@1.1.0:
|
||||
resolution: {integrity: sha512-147y/6YNh+tlp6nd/2pWq38i9h6mz/EuQ6njIrmW8D1BS5nCqs0P6DG+m6zTGnNz5I+uhZ0SHxBs9BsPrwcKDA==}
|
||||
|
||||
y18n@4.0.3:
|
||||
resolution: {integrity: sha512-JKhqTOwSrqNA1NY5lSztJ1GrBiUodLMmIZuLiDaMRJ+itFd+ABVE8XBjOvIWL+rSqNDC74LCSFmlb/U4UZ4hJQ==}
|
||||
|
||||
@@ -14209,12 +14158,6 @@ snapshots:
|
||||
|
||||
'@polka/url@1.0.0-next.29': {}
|
||||
|
||||
'@popperjs/core@2.11.8': {}
|
||||
|
||||
'@profoundlogic/hogan@3.0.4':
|
||||
dependencies:
|
||||
nopt: 1.0.10
|
||||
|
||||
'@protobufjs/aspromise@1.1.2': {}
|
||||
|
||||
'@protobufjs/base64@1.1.2': {}
|
||||
@@ -15732,8 +15675,6 @@ snapshots:
|
||||
|
||||
a11y-color-contrast@0.2.0: {}
|
||||
|
||||
abbrev@1.1.1: {}
|
||||
|
||||
abbrev@2.0.0: {}
|
||||
|
||||
abbrev@5.0.0: {}
|
||||
@@ -17086,17 +17027,6 @@ snapshots:
|
||||
|
||||
diff-sequences@29.6.3: {}
|
||||
|
||||
diff2html@3.4.56:
|
||||
dependencies:
|
||||
'@profoundlogic/hogan': 3.0.4
|
||||
diff: 8.0.4
|
||||
optionalDependencies:
|
||||
highlight.js: 11.11.1
|
||||
|
||||
diff@8.0.4: {}
|
||||
|
||||
diff@9.0.0: {}
|
||||
|
||||
diffie-hellman@5.0.3:
|
||||
dependencies:
|
||||
bn.js: 4.12.4
|
||||
@@ -17630,8 +17560,6 @@ snapshots:
|
||||
|
||||
event-target-shim@5.0.1: {}
|
||||
|
||||
eventemitter3@5.0.4: {}
|
||||
|
||||
events-universal@1.0.1:
|
||||
dependencies:
|
||||
bare-events: 2.9.1
|
||||
@@ -19800,10 +19728,6 @@ snapshots:
|
||||
|
||||
nodemailer@9.0.1: {}
|
||||
|
||||
nopt@1.0.10:
|
||||
dependencies:
|
||||
abbrev: 1.1.1
|
||||
|
||||
nopt@10.0.1:
|
||||
dependencies:
|
||||
abbrev: 5.0.0
|
||||
@@ -20954,8 +20878,6 @@ snapshots:
|
||||
|
||||
scule@1.3.0: {}
|
||||
|
||||
seamless-scroll-polyfill@2.3.4: {}
|
||||
|
||||
section-matter@1.0.0:
|
||||
dependencies:
|
||||
extend-shallow: 2.0.1
|
||||
@@ -21606,10 +21528,6 @@ snapshots:
|
||||
|
||||
tinyrainbow@3.1.0: {}
|
||||
|
||||
tippy.js@6.3.7:
|
||||
dependencies:
|
||||
'@popperjs/core': 2.11.8
|
||||
|
||||
title-case@2.1.1:
|
||||
dependencies:
|
||||
no-case: 2.3.2
|
||||
@@ -21679,8 +21597,6 @@ snapshots:
|
||||
dependencies:
|
||||
punycode: 2.3.1
|
||||
|
||||
tree-kill@1.2.2: {}
|
||||
|
||||
tree-sitter-bash@0.23.3(tree-sitter@0.21.1):
|
||||
dependencies:
|
||||
node-addon-api: 8.9.0
|
||||
@@ -22424,15 +22340,6 @@ snapshots:
|
||||
'@vue/devtools-api': 6.6.4
|
||||
vue: 3.5.39(typescript@6.0.3)
|
||||
|
||||
w-websocket-client@1.0.29:
|
||||
dependencies:
|
||||
lodash-es: 4.18.1
|
||||
ws: 8.21.0
|
||||
wsemi: 1.8.63
|
||||
transitivePeerDependencies:
|
||||
- bufferutil
|
||||
- utf-8-validate
|
||||
|
||||
w3c-keyname@2.2.8: {}
|
||||
|
||||
w3c-xmlserializer@5.0.0:
|
||||
@@ -22770,29 +22677,6 @@ snapshots:
|
||||
|
||||
ws@8.21.0: {}
|
||||
|
||||
wsemi@1.8.63:
|
||||
dependencies:
|
||||
chokidar: 5.0.0
|
||||
crypto-js: 4.2.0
|
||||
dayjs: 1.11.21
|
||||
decimal.js: 10.6.0
|
||||
diff: 9.0.0
|
||||
diff2html: 3.4.56
|
||||
eventemitter3: 5.0.4
|
||||
fuse.js: 7.4.2
|
||||
hash-wasm: 4.12.0
|
||||
he: 1.2.0
|
||||
html-to-text: 10.0.0
|
||||
lodash-es: 4.18.1
|
||||
seamless-scroll-polyfill: 2.3.4
|
||||
tippy.js: 6.3.7
|
||||
tree-kill: 1.2.2
|
||||
ua-parser-js: 2.0.10
|
||||
uuid: 14.0.1
|
||||
xlsx: https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz
|
||||
xss: 1.0.15
|
||||
xxhash-wasm: 1.1.0
|
||||
|
||||
wsl-utils@0.3.1:
|
||||
dependencies:
|
||||
is-wsl: 3.1.1
|
||||
@@ -22881,8 +22765,6 @@ snapshots:
|
||||
|
||||
xtend@4.0.2: {}
|
||||
|
||||
xxhash-wasm@1.1.0: {}
|
||||
|
||||
y18n@4.0.3: {}
|
||||
|
||||
y18n@5.0.8: {}
|
||||
|
||||
+4
-6
@@ -1,9 +1,7 @@
|
||||
# The xlsx override below intentionally points the whole graph (including
|
||||
# wsemi's transitive xlsx) at the SheetJS CDN tarball, because the npm
|
||||
# registry version is stale (0.18.5) and carries known vulnerabilities.
|
||||
# pnpm 11 blocks URL tarballs in subdependencies by default and has no
|
||||
# per-package exception, so this protection must be disabled repo-wide.
|
||||
blockExoticSubdeps: false
|
||||
# The xlsx override below points the graph at the SheetJS CDN tarball, because
|
||||
# the npm registry version is stale (0.18.5) and carries known vulnerabilities.
|
||||
# Only this package depends on xlsx now, so pnpm's default protection against
|
||||
# URL tarballs in subdependencies can stay enabled.
|
||||
|
||||
overrides:
|
||||
'@vueuse/shared': 13.9.0
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
declare module "w-websocket-client/dist/w-websocket-client.umd.js" {
|
||||
export default class WSC {
|
||||
constructor(options: {
|
||||
url: string
|
||||
token: string
|
||||
open?: () => void
|
||||
close?: () => void
|
||||
message?: (data: any) => void
|
||||
error?: (err: any) => void
|
||||
});
|
||||
|
||||
send(data: any): void
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { buildWebSocketUrl, createWebSocketClient, formatWebSocketError } from './websocket-tester.service';
|
||||
|
||||
class FakeWebSocket {
|
||||
static instances: FakeWebSocket[] = [];
|
||||
|
||||
onopen: (() => void) | null = null;
|
||||
onclose: (() => void) | null = null;
|
||||
onmessage: ((event: { data: unknown }) => void) | null = null;
|
||||
onerror: ((event: unknown) => void) | null = null;
|
||||
close = vi.fn<() => void>();
|
||||
|
||||
constructor(public url: string) {
|
||||
FakeWebSocket.instances.push(this);
|
||||
}
|
||||
}
|
||||
|
||||
function createClient(options: Parameters<typeof createWebSocketClient>[0] = {}) {
|
||||
FakeWebSocket.instances = [];
|
||||
const socket = createWebSocketClient(options, FakeWebSocket as unknown as typeof WebSocket);
|
||||
|
||||
return socket as unknown as FakeWebSocket;
|
||||
}
|
||||
|
||||
describe('websocket-tester', () => {
|
||||
describe('buildWebSocketUrl', () => {
|
||||
it('passes the token as a query parameter', () => {
|
||||
expect(buildWebSocketUrl({ url: 'ws://example.com:9000', token: 'abc' })).toBe(
|
||||
'ws://example.com:9000/?token=abc',
|
||||
);
|
||||
});
|
||||
|
||||
it('falls back to the defaults when the url or the token is empty', () => {
|
||||
expect(buildWebSocketUrl({ url: '', token: '' })).toBe('ws://localhost:8080/?token=*');
|
||||
expect(buildWebSocketUrl({})).toBe('ws://localhost:8080/?token=*');
|
||||
expect(buildWebSocketUrl({ url: 'wss://example.com' })).toBe('wss://example.com/?token=*');
|
||||
expect(buildWebSocketUrl({ token: 'abc' })).toBe('ws://localhost:8080/?token=abc');
|
||||
});
|
||||
|
||||
it('does not double the slash when the url already ends with one', () => {
|
||||
expect(buildWebSocketUrl({ url: 'ws://example.com/', token: 'abc' })).toBe('ws://example.com/?token=abc');
|
||||
expect(buildWebSocketUrl({ url: 'ws://example.com///', token: 'abc' })).toBe('ws://example.com/?token=abc');
|
||||
expect(buildWebSocketUrl({ url: 'ws://example.com/path/', token: 'abc' })).toBe('ws://example.com/path/?token=abc');
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatWebSocketError', () => {
|
||||
it('uses the message of a real error', () => {
|
||||
expect(formatWebSocketError(new SyntaxError("The URL 'nope' is invalid"))).toBe("The URL 'nope' is invalid");
|
||||
});
|
||||
|
||||
it('does not stringify the bare event the native handler receives', () => {
|
||||
// `${new Event('error')}` would render as the useless '[object Event]'
|
||||
expect(formatWebSocketError(new Event('error'))).toBe('connection failed');
|
||||
});
|
||||
|
||||
it('falls back to the string form of anything else', () => {
|
||||
expect(formatWebSocketError('boom')).toBe('boom');
|
||||
expect(formatWebSocketError(undefined)).toBe('undefined');
|
||||
});
|
||||
});
|
||||
|
||||
describe('createWebSocketClient', () => {
|
||||
it('connects to the built url', () => {
|
||||
const socket = createClient({ url: 'ws://example.com:9000', token: 'abc' });
|
||||
|
||||
expect(socket.url).toBe('ws://example.com:9000/?token=abc');
|
||||
});
|
||||
|
||||
it('returns the socket itself, so callers can send on it', () => {
|
||||
const socket = createClient({ url: 'ws://example.com' });
|
||||
|
||||
expect(FakeWebSocket.instances).toHaveLength(1);
|
||||
expect(socket).toBe(FakeWebSocket.instances[0]);
|
||||
});
|
||||
|
||||
it('forwards the open and close events', () => {
|
||||
const open = vi.fn<() => void>();
|
||||
const close = vi.fn<() => void>();
|
||||
const socket = createClient({ open, close });
|
||||
|
||||
socket.onopen?.();
|
||||
socket.onclose?.();
|
||||
|
||||
expect(open).toHaveBeenCalledTimes(1);
|
||||
expect(close).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('forwards the message payload rather than the event', () => {
|
||||
const message = vi.fn<(data: unknown) => void>();
|
||||
const socket = createClient({ message });
|
||||
|
||||
socket.onmessage?.({ data: 'hello' });
|
||||
|
||||
expect(message).toHaveBeenCalledWith('hello');
|
||||
});
|
||||
|
||||
it('closes the socket after an error', () => {
|
||||
const error = vi.fn<(error: unknown) => void>();
|
||||
const socket = createClient({ error });
|
||||
const event = { type: 'error' };
|
||||
|
||||
socket.onerror?.(event);
|
||||
|
||||
expect(error).toHaveBeenCalledWith(event);
|
||||
expect(socket.close).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('does not throw when no handler is provided', () => {
|
||||
const socket = createClient({});
|
||||
|
||||
expect(() => {
|
||||
socket.onopen?.();
|
||||
socket.onmessage?.({ data: 'hello' });
|
||||
socket.onerror?.({ type: 'error' });
|
||||
socket.onclose?.();
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
export interface WebSocketClientOptions {
|
||||
url?: string
|
||||
token?: string
|
||||
open?: () => void
|
||||
close?: () => void
|
||||
message?: (data: unknown) => void
|
||||
error?: (error: unknown) => void
|
||||
}
|
||||
|
||||
const defaultUrl = 'ws://localhost:8080';
|
||||
const defaultToken = '*';
|
||||
|
||||
// The token is passed as a query parameter, the way the previous client built its url. Unlike it,
|
||||
// a trailing slash is trimmed first, so that `ws://host/` does not turn into `ws://host//?token=`.
|
||||
export function buildWebSocketUrl({ url, token }: { url?: string; token?: string }) {
|
||||
const base = (url || defaultUrl).replace(/\/+$/, '');
|
||||
|
||||
return `${base}/?token=${token || defaultToken}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* The native `onerror` handler receives a bare `Event`: the websocket spec deliberately withholds
|
||||
* the reason, so interpolating it would only ever log `[object Event]`.
|
||||
*/
|
||||
export function formatWebSocketError(error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
return error.message;
|
||||
}
|
||||
|
||||
if (typeof Event !== 'undefined' && error instanceof Event) {
|
||||
return 'connection failed';
|
||||
}
|
||||
|
||||
return String(error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a websocket and wires the handlers to it. The constructor is injectable so that tests can
|
||||
* drive the socket without a server; the browser passes its own global.
|
||||
*/
|
||||
export function createWebSocketClient(
|
||||
{ url, token, open, close, message, error }: WebSocketClientOptions,
|
||||
WebSocketImpl: typeof WebSocket = WebSocket,
|
||||
) {
|
||||
const socket = new WebSocketImpl(buildWebSocketUrl({ url, token }));
|
||||
|
||||
socket.onopen = () => open?.();
|
||||
socket.onclose = () => close?.();
|
||||
socket.onmessage = event => message?.(event.data);
|
||||
socket.onerror = (event) => {
|
||||
error?.(event);
|
||||
// an errored socket never recovers, so close it rather than leaving it half open
|
||||
socket.close();
|
||||
};
|
||||
|
||||
return socket;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import WSC from 'w-websocket-client/dist/w-websocket-client.umd.js';
|
||||
import { createWebSocketClient, formatWebSocketError } from './websocket-tester.service';
|
||||
import { useQueryParamOrStorage } from '@/composable/queryParams';
|
||||
|
||||
const { t } = useI18n();
|
||||
@@ -10,31 +10,37 @@ const token = useQueryParamOrStorage({ name: 'token', storageName: 'ws-tester:to
|
||||
const logs = ref<string[]>([]);
|
||||
const message = ref('');
|
||||
const connected = ref(false);
|
||||
let wsc: WSC;
|
||||
let wsc: WebSocket | null = null;
|
||||
|
||||
function send() {
|
||||
logs.value.push(`Sent: ${message.value}`);
|
||||
wsc.send(message.value);
|
||||
wsc?.send(message.value);
|
||||
}
|
||||
function connect() {
|
||||
wsc = new WSC({
|
||||
url: url.value,
|
||||
token: token.value,
|
||||
open() {
|
||||
logs.value.push('WebSocket Connection opened');
|
||||
connected.value = true;
|
||||
},
|
||||
close() {
|
||||
connected.value = false;
|
||||
logs.value.push('WebSocket Connection closed');
|
||||
},
|
||||
message(data: any) {
|
||||
logs.value.push(`Received: ${JSON.stringify(data)}`);
|
||||
},
|
||||
error(err: any) {
|
||||
logs.value.push(`Error: ${err}`);
|
||||
},
|
||||
});
|
||||
try {
|
||||
wsc = createWebSocketClient({
|
||||
url: url.value,
|
||||
token: token.value,
|
||||
open() {
|
||||
logs.value.push('WebSocket Connection opened');
|
||||
connected.value = true;
|
||||
},
|
||||
close() {
|
||||
connected.value = false;
|
||||
logs.value.push('WebSocket Connection closed');
|
||||
},
|
||||
message(data: any) {
|
||||
logs.value.push(`Received: ${JSON.stringify(data)}`);
|
||||
},
|
||||
error(err: unknown) {
|
||||
logs.value.push(`Error: ${formatWebSocketError(err)}`);
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
// an invalid url throws before the socket exists, so it never reaches the error handler above
|
||||
wsc = null;
|
||||
logs.value.push(`Error: ${formatWebSocketError(err)}`);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
Reference in new issue
Block a user