@hedystia/ws 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +50 -0
- package/LICENSE +21 -0
- package/dist/_virtual/_rolldown/runtime.mjs +5 -0
- package/dist/client.cjs +148 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.cts +120 -0
- package/dist/client.d.mts +120 -0
- package/dist/client.mjs +146 -0
- package/dist/client.mjs.map +1 -0
- package/dist/index.cjs +22 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +9 -0
- package/dist/index.mjs.map +1 -0
- package/dist/runtime.cjs +56 -0
- package/dist/runtime.cjs.map +1 -0
- package/dist/runtime.d.cts +47 -0
- package/dist/runtime.d.mts +47 -0
- package/dist/runtime.mjs +52 -0
- package/dist/runtime.mjs.map +1 -0
- package/dist/server.cjs +237 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.cts +119 -0
- package/dist/server.d.mts +119 -0
- package/dist/server.mjs +236 -0
- package/dist/server.mjs.map +1 -0
- package/dist/types.d.cts +208 -0
- package/dist/types.d.mts +208 -0
- package/package.json +62 -0
- package/readme.md +102 -0
- package/src/client.ts +161 -0
- package/src/index.ts +19 -0
- package/src/runtime.ts +66 -0
- package/src/server.ts +313 -0
- package/src/types.ts +226 -0
- package/tsconfig.json +3 -0
- package/tsdown.config.ts +12 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
$ tsdown --config-loader unrun
|
|
2
|
+
[34mℹ[39m [34mtsdown v0.22.0[39m powered by [38;2;255;126;23mrolldown v1.0.0[39m
|
|
3
|
+
[34mℹ[39m config file: [4m/home/runner/actions-runner/_work/Hedystia/Hedystia/Packages/websocket/tsdown.config.ts[24m (unrun)
|
|
4
|
+
[34mℹ[39m entry: [34msrc/index.ts, src/client.ts, src/server.ts[39m
|
|
5
|
+
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
6
|
+
[34mℹ[39m Build start
|
|
7
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mserver.cjs[22m [2m 7.36 kB[22m [2m│ gzip: 2.69 kB[22m
|
|
8
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mclient.cjs[22m [2m 4.12 kB[22m [2m│ gzip: 1.53 kB[22m
|
|
9
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mindex.cjs[22m [2m 0.87 kB[22m [2m│ gzip: 0.34 kB[22m
|
|
10
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22mserver.cjs.map [2m12.54 kB[22m [2m│ gzip: 4.06 kB[22m
|
|
11
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22mclient.cjs.map [2m 5.86 kB[22m [2m│ gzip: 2.05 kB[22m
|
|
12
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22mruntime.cjs.map [2m 2.06 kB[22m [2m│ gzip: 0.78 kB[22m
|
|
13
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22mruntime.cjs [2m 1.36 kB[22m [2m│ gzip: 0.55 kB[22m
|
|
14
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22mindex.cjs.map [2m 0.71 kB[22m [2m│ gzip: 0.33 kB[22m
|
|
15
|
+
[34mℹ[39m [33m[CJS][39m 8 files, total: 34.86 kB
|
|
16
|
+
[33m[PLUGIN_TIMINGS] Warning:[0m Your build spent significant time in plugin `tsdown:report`. See https://rolldown.rs/options/checks#plugintimings for more details.
|
|
17
|
+
|
|
18
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mserver.d.cts[22m[39m [2m4.05 kB[22m [2m│ gzip: 1.65 kB[22m
|
|
19
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mclient.d.cts[22m[39m [2m3.80 kB[22m [2m│ gzip: 1.40 kB[22m
|
|
20
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mindex.d.cts[22m[39m [2m0.73 kB[22m [2m│ gzip: 0.27 kB[22m
|
|
21
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32mtypes.d.cts[39m [2m7.25 kB[22m [2m│ gzip: 2.64 kB[22m
|
|
22
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32mruntime.d.cts[39m [2m1.13 kB[22m [2m│ gzip: 0.48 kB[22m
|
|
23
|
+
[34mℹ[39m [33m[CJS][39m 5 files, total: 16.96 kB
|
|
24
|
+
[33m[PLUGIN_TIMINGS] Warning:[0m Your build spent significant time in plugins. Here is a breakdown:
|
|
25
|
+
- rolldown-plugin-dts:generate (63%)
|
|
26
|
+
- tsdown:deps (28%)
|
|
27
|
+
See https://rolldown.rs/options/checks#plugintimings for more details.
|
|
28
|
+
|
|
29
|
+
[32m✔[39m Build complete in [32m4736ms[39m
|
|
30
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mserver.mjs[22m [2m 7.30 kB[22m [2m│ gzip: 2.64 kB[22m
|
|
31
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mclient.mjs[22m [2m 4.04 kB[22m [2m│ gzip: 1.50 kB[22m
|
|
32
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mindex.mjs[22m [2m 0.47 kB[22m [2m│ gzip: 0.23 kB[22m
|
|
33
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22mserver.mjs.map [2m12.53 kB[22m [2m│ gzip: 4.05 kB[22m
|
|
34
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22mclient.mjs.map [2m 5.86 kB[22m [2m│ gzip: 2.05 kB[22m
|
|
35
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22mruntime.mjs.map [2m 2.06 kB[22m [2m│ gzip: 0.78 kB[22m
|
|
36
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22mruntime.mjs [2m 1.27 kB[22m [2m│ gzip: 0.53 kB[22m
|
|
37
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22mindex.mjs.map [2m 0.68 kB[22m [2m│ gzip: 0.32 kB[22m
|
|
38
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m_virtual/_rolldown/runtime.mjs [2m 0.18 kB[22m [2m│ gzip: 0.15 kB[22m
|
|
39
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mserver.d.mts[22m[39m [2m 4.05 kB[22m [2m│ gzip: 1.65 kB[22m
|
|
40
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mclient.d.mts[22m[39m [2m 3.80 kB[22m [2m│ gzip: 1.40 kB[22m
|
|
41
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m 0.73 kB[22m [2m│ gzip: 0.27 kB[22m
|
|
42
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32mtypes.d.mts[39m [2m 7.25 kB[22m [2m│ gzip: 2.64 kB[22m
|
|
43
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32mruntime.d.mts[39m [2m 1.13 kB[22m [2m│ gzip: 0.48 kB[22m
|
|
44
|
+
[34mℹ[39m [34m[ESM][39m 14 files, total: 51.34 kB
|
|
45
|
+
[33m[PLUGIN_TIMINGS] Warning:[0m Your build spent significant time in plugins. Here is a breakdown:
|
|
46
|
+
- tsdown:deps (51%)
|
|
47
|
+
- rolldown-plugin-dts:generate (39%)
|
|
48
|
+
See https://rolldown.rs/options/checks#plugintimings for more details.
|
|
49
|
+
|
|
50
|
+
[32m✔[39m Build complete in [32m4846ms[39m
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Hedystia
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/client.ts
|
|
3
|
+
/**
|
|
4
|
+
* Resolve the best `WebSocket` constructor for the current runtime.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* - Bun, Deno, browsers and Node ≥ 22 expose `globalThis.WebSocket`.
|
|
8
|
+
* - Older Node falls back to the [`ws`](https://github.com/websockets/ws)
|
|
9
|
+
* package, which mirrors the WHATWG `WebSocket` API.
|
|
10
|
+
*
|
|
11
|
+
* @returns A `WebSocket` constructor compatible with the WHATWG interface.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { resolveWebSocket } from "@hedystia/ws/client";
|
|
16
|
+
*
|
|
17
|
+
* const WS = resolveWebSocket();
|
|
18
|
+
* const socket = new WS("ws://localhost:3000");
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
function resolveWebSocket() {
|
|
22
|
+
if (typeof globalThis !== "undefined" && globalThis.WebSocket) return globalThis.WebSocket;
|
|
23
|
+
const mod = require("ws");
|
|
24
|
+
return mod.WebSocket ?? mod;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Create a `WebSocket` instance using the best available implementation
|
|
28
|
+
* for the current runtime.
|
|
29
|
+
*
|
|
30
|
+
* @remarks
|
|
31
|
+
* Custom request headers are honoured on Node via the `ws` package; on
|
|
32
|
+
* runtimes that ship a WHATWG-compliant global `WebSocket` (Bun, Deno,
|
|
33
|
+
* browsers, Node ≥ 22) headers are ignored — matching standard semantics.
|
|
34
|
+
*
|
|
35
|
+
* @param url - Absolute WebSocket URL (`ws://` or `wss://`)
|
|
36
|
+
* @param options - Optional protocols / headers
|
|
37
|
+
* @returns A connected (or connecting) `WebSocket` instance.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* import { createWebSocket } from "@hedystia/ws/client";
|
|
42
|
+
*
|
|
43
|
+
* const ws = createWebSocket("ws://localhost:3000", {
|
|
44
|
+
* protocols: "v1",
|
|
45
|
+
* headers: { authorization: "Bearer ..." },
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* ws.onopen = () => ws.send("hi");
|
|
49
|
+
* ws.onmessage = (event) => console.log(event.data);
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
function createWebSocket(url, options) {
|
|
53
|
+
const Ctor = resolveWebSocket();
|
|
54
|
+
if (typeof globalThis !== "undefined" && globalThis.WebSocket === Ctor) return options?.protocols ? new Ctor(url, options.protocols) : new Ctor(url);
|
|
55
|
+
const init = {};
|
|
56
|
+
if (options?.headers) init.headers = options.headers;
|
|
57
|
+
return new Ctor(url, options?.protocols, init);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Lightweight runtime-agnostic wrapper that mirrors a small, predictable
|
|
61
|
+
* subset of the WHATWG WebSocket interface.
|
|
62
|
+
*
|
|
63
|
+
* @remarks
|
|
64
|
+
* Useful for higher-level code that wants to assign event handlers by
|
|
65
|
+
* property (`socket.onmessage = ...`) without caring whether the underlying
|
|
66
|
+
* implementation comes from `globalThis.WebSocket` or the `ws` package.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* import { WebSocketClient } from "@hedystia/ws/client";
|
|
71
|
+
*
|
|
72
|
+
* const client = new WebSocketClient("ws://localhost:3000");
|
|
73
|
+
* client.onopen = () => client.send("hello");
|
|
74
|
+
* client.onmessage = (event) => console.log(event.data);
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
var WebSocketClient = class {
|
|
78
|
+
/**
|
|
79
|
+
* Underlying WebSocket instance produced by {@link createWebSocket}.
|
|
80
|
+
*
|
|
81
|
+
* @readonly
|
|
82
|
+
*/
|
|
83
|
+
socket;
|
|
84
|
+
/**
|
|
85
|
+
* Create a new client and immediately initiate the connection.
|
|
86
|
+
*
|
|
87
|
+
* @param url - Absolute WebSocket URL (`ws://` or `wss://`)
|
|
88
|
+
* @param options - Optional protocols / headers, see {@link ClientWebSocketOptions}
|
|
89
|
+
*/
|
|
90
|
+
constructor(url, options) {
|
|
91
|
+
this.socket = createWebSocket(url, options);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Current connection state, mirroring {@link WebSocket.readyState}.
|
|
95
|
+
*
|
|
96
|
+
* @returns `0` connecting, `1` open, `2` closing, `3` closed.
|
|
97
|
+
*/
|
|
98
|
+
get readyState() {
|
|
99
|
+
return this.socket.readyState;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Send a payload to the server.
|
|
103
|
+
*
|
|
104
|
+
* @param data - WHATWG-compatible payload
|
|
105
|
+
*/
|
|
106
|
+
send(data) {
|
|
107
|
+
this.socket.send(data);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Close the underlying socket.
|
|
111
|
+
*
|
|
112
|
+
* @param code - Close code (defaults to 1000)
|
|
113
|
+
* @param reason - Optional human-readable reason
|
|
114
|
+
*/
|
|
115
|
+
close(code, reason) {
|
|
116
|
+
this.socket.close(code, reason);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Assign the open-event listener.
|
|
120
|
+
*/
|
|
121
|
+
set onopen(cb) {
|
|
122
|
+
this.socket.onopen = cb;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Assign the message-event listener.
|
|
126
|
+
*/
|
|
127
|
+
set onmessage(cb) {
|
|
128
|
+
this.socket.onmessage = cb;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Assign the close-event listener.
|
|
132
|
+
*/
|
|
133
|
+
set onclose(cb) {
|
|
134
|
+
this.socket.onclose = cb;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Assign the error-event listener.
|
|
138
|
+
*/
|
|
139
|
+
set onerror(cb) {
|
|
140
|
+
this.socket.onerror = cb;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
//#endregion
|
|
144
|
+
exports.WebSocketClient = WebSocketClient;
|
|
145
|
+
exports.createWebSocket = createWebSocket;
|
|
146
|
+
exports.resolveWebSocket = resolveWebSocket;
|
|
147
|
+
|
|
148
|
+
//# sourceMappingURL=client.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.cjs","names":[],"sources":["../src/client.ts"],"sourcesContent":["import type { ClientWebSocketOptions } from \"./types\";\n\nexport type { ClientWebSocketOptions } from \"./types\";\n\n/**\n * Resolve the best `WebSocket` constructor for the current runtime.\n *\n * @remarks\n * - Bun, Deno, browsers and Node ≥ 22 expose `globalThis.WebSocket`.\n * - Older Node falls back to the [`ws`](https://github.com/websockets/ws)\n * package, which mirrors the WHATWG `WebSocket` API.\n *\n * @returns A `WebSocket` constructor compatible with the WHATWG interface.\n *\n * @example\n * ```ts\n * import { resolveWebSocket } from \"@hedystia/ws/client\";\n *\n * const WS = resolveWebSocket();\n * const socket = new WS(\"ws://localhost:3000\");\n * ```\n */\nexport function resolveWebSocket(): typeof WebSocket {\n if (typeof globalThis !== \"undefined\" && (globalThis as any).WebSocket) {\n return (globalThis as any).WebSocket as typeof WebSocket;\n }\n const mod = require(\"ws\");\n return (mod.WebSocket ?? mod) as typeof WebSocket;\n}\n\n/**\n * Create a `WebSocket` instance using the best available implementation\n * for the current runtime.\n *\n * @remarks\n * Custom request headers are honoured on Node via the `ws` package; on\n * runtimes that ship a WHATWG-compliant global `WebSocket` (Bun, Deno,\n * browsers, Node ≥ 22) headers are ignored — matching standard semantics.\n *\n * @param url - Absolute WebSocket URL (`ws://` or `wss://`)\n * @param options - Optional protocols / headers\n * @returns A connected (or connecting) `WebSocket` instance.\n *\n * @example\n * ```ts\n * import { createWebSocket } from \"@hedystia/ws/client\";\n *\n * const ws = createWebSocket(\"ws://localhost:3000\", {\n * protocols: \"v1\",\n * headers: { authorization: \"Bearer ...\" },\n * });\n *\n * ws.onopen = () => ws.send(\"hi\");\n * ws.onmessage = (event) => console.log(event.data);\n * ```\n */\nexport function createWebSocket(url: string, options?: ClientWebSocketOptions): WebSocket {\n const Ctor = resolveWebSocket();\n const isWhatwg =\n typeof globalThis !== \"undefined\" && (globalThis as any).WebSocket === (Ctor as any);\n\n if (isWhatwg) {\n return options?.protocols ? new Ctor(url, options.protocols as any) : new Ctor(url);\n }\n\n const init: any = {};\n if (options?.headers) {\n init.headers = options.headers;\n }\n return new (Ctor as any)(url, options?.protocols, init);\n}\n\n/**\n * Lightweight runtime-agnostic wrapper that mirrors a small, predictable\n * subset of the WHATWG WebSocket interface.\n *\n * @remarks\n * Useful for higher-level code that wants to assign event handlers by\n * property (`socket.onmessage = ...`) without caring whether the underlying\n * implementation comes from `globalThis.WebSocket` or the `ws` package.\n *\n * @example\n * ```ts\n * import { WebSocketClient } from \"@hedystia/ws/client\";\n *\n * const client = new WebSocketClient(\"ws://localhost:3000\");\n * client.onopen = () => client.send(\"hello\");\n * client.onmessage = (event) => console.log(event.data);\n * ```\n */\nexport class WebSocketClient {\n /**\n * Underlying WebSocket instance produced by {@link createWebSocket}.\n *\n * @readonly\n */\n readonly socket: WebSocket;\n\n /**\n * Create a new client and immediately initiate the connection.\n *\n * @param url - Absolute WebSocket URL (`ws://` or `wss://`)\n * @param options - Optional protocols / headers, see {@link ClientWebSocketOptions}\n */\n constructor(url: string, options?: ClientWebSocketOptions) {\n this.socket = createWebSocket(url, options);\n }\n\n /**\n * Current connection state, mirroring {@link WebSocket.readyState}.\n *\n * @returns `0` connecting, `1` open, `2` closing, `3` closed.\n */\n get readyState(): number {\n return this.socket.readyState;\n }\n\n /**\n * Send a payload to the server.\n *\n * @param data - WHATWG-compatible payload\n */\n send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void {\n this.socket.send(data as any);\n }\n\n /**\n * Close the underlying socket.\n *\n * @param code - Close code (defaults to 1000)\n * @param reason - Optional human-readable reason\n */\n close(code?: number, reason?: string): void {\n this.socket.close(code, reason);\n }\n\n /**\n * Assign the open-event listener.\n */\n set onopen(cb: ((ev: Event) => void) | null) {\n (this.socket as any).onopen = cb;\n }\n /**\n * Assign the message-event listener.\n */\n set onmessage(cb: ((ev: MessageEvent) => void) | null) {\n (this.socket as any).onmessage = cb;\n }\n /**\n * Assign the close-event listener.\n */\n set onclose(cb: ((ev: CloseEvent) => void) | null) {\n (this.socket as any).onclose = cb;\n }\n /**\n * Assign the error-event listener.\n */\n set onerror(cb: ((ev: Event) => void) | null) {\n (this.socket as any).onerror = cb;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,mBAAqC;CACnD,IAAI,OAAO,eAAe,eAAgB,WAAmB,WAC3D,OAAQ,WAAmB;CAE7B,MAAM,MAAM,QAAQ,KAAK;CACzB,OAAQ,IAAI,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6B3B,SAAgB,gBAAgB,KAAa,SAA6C;CACxF,MAAM,OAAO,kBAAkB;CAI/B,IAFE,OAAO,eAAe,eAAgB,WAAmB,cAAe,MAGxE,OAAO,SAAS,YAAY,IAAI,KAAK,KAAK,QAAQ,UAAiB,GAAG,IAAI,KAAK,IAAI;CAGrF,MAAM,OAAY,EAAE;CACpB,IAAI,SAAS,SACX,KAAK,UAAU,QAAQ;CAEzB,OAAO,IAAK,KAAa,KAAK,SAAS,WAAW,KAAK;;;;;;;;;;;;;;;;;;;;AAqBzD,IAAa,kBAAb,MAA6B;;;;;;CAM3B;;;;;;;CAQA,YAAY,KAAa,SAAkC;EACzD,KAAK,SAAS,gBAAgB,KAAK,QAAQ;;;;;;;CAQ7C,IAAI,aAAqB;EACvB,OAAO,KAAK,OAAO;;;;;;;CAQrB,KAAK,MAA+D;EAClE,KAAK,OAAO,KAAK,KAAY;;;;;;;;CAS/B,MAAM,MAAe,QAAuB;EAC1C,KAAK,OAAO,MAAM,MAAM,OAAO;;;;;CAMjC,IAAI,OAAO,IAAkC;EAC3C,KAAM,OAAe,SAAS;;;;;CAKhC,IAAI,UAAU,IAAyC;EACrD,KAAM,OAAe,YAAY;;;;;CAKnC,IAAI,QAAQ,IAAuC;EACjD,KAAM,OAAe,UAAU;;;;;CAKjC,IAAI,QAAQ,IAAkC;EAC5C,KAAM,OAAe,UAAU"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { ClientWebSocketOptions } from "./types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/client.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Resolve the best `WebSocket` constructor for the current runtime.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* - Bun, Deno, browsers and Node ≥ 22 expose `globalThis.WebSocket`.
|
|
9
|
+
* - Older Node falls back to the [`ws`](https://github.com/websockets/ws)
|
|
10
|
+
* package, which mirrors the WHATWG `WebSocket` API.
|
|
11
|
+
*
|
|
12
|
+
* @returns A `WebSocket` constructor compatible with the WHATWG interface.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { resolveWebSocket } from "@hedystia/ws/client";
|
|
17
|
+
*
|
|
18
|
+
* const WS = resolveWebSocket();
|
|
19
|
+
* const socket = new WS("ws://localhost:3000");
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
declare function resolveWebSocket(): typeof WebSocket;
|
|
23
|
+
/**
|
|
24
|
+
* Create a `WebSocket` instance using the best available implementation
|
|
25
|
+
* for the current runtime.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* Custom request headers are honoured on Node via the `ws` package; on
|
|
29
|
+
* runtimes that ship a WHATWG-compliant global `WebSocket` (Bun, Deno,
|
|
30
|
+
* browsers, Node ≥ 22) headers are ignored — matching standard semantics.
|
|
31
|
+
*
|
|
32
|
+
* @param url - Absolute WebSocket URL (`ws://` or `wss://`)
|
|
33
|
+
* @param options - Optional protocols / headers
|
|
34
|
+
* @returns A connected (or connecting) `WebSocket` instance.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* import { createWebSocket } from "@hedystia/ws/client";
|
|
39
|
+
*
|
|
40
|
+
* const ws = createWebSocket("ws://localhost:3000", {
|
|
41
|
+
* protocols: "v1",
|
|
42
|
+
* headers: { authorization: "Bearer ..." },
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* ws.onopen = () => ws.send("hi");
|
|
46
|
+
* ws.onmessage = (event) => console.log(event.data);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
declare function createWebSocket(url: string, options?: ClientWebSocketOptions): WebSocket;
|
|
50
|
+
/**
|
|
51
|
+
* Lightweight runtime-agnostic wrapper that mirrors a small, predictable
|
|
52
|
+
* subset of the WHATWG WebSocket interface.
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* Useful for higher-level code that wants to assign event handlers by
|
|
56
|
+
* property (`socket.onmessage = ...`) without caring whether the underlying
|
|
57
|
+
* implementation comes from `globalThis.WebSocket` or the `ws` package.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* import { WebSocketClient } from "@hedystia/ws/client";
|
|
62
|
+
*
|
|
63
|
+
* const client = new WebSocketClient("ws://localhost:3000");
|
|
64
|
+
* client.onopen = () => client.send("hello");
|
|
65
|
+
* client.onmessage = (event) => console.log(event.data);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
declare class WebSocketClient {
|
|
69
|
+
/**
|
|
70
|
+
* Underlying WebSocket instance produced by {@link createWebSocket}.
|
|
71
|
+
*
|
|
72
|
+
* @readonly
|
|
73
|
+
*/
|
|
74
|
+
readonly socket: WebSocket;
|
|
75
|
+
/**
|
|
76
|
+
* Create a new client and immediately initiate the connection.
|
|
77
|
+
*
|
|
78
|
+
* @param url - Absolute WebSocket URL (`ws://` or `wss://`)
|
|
79
|
+
* @param options - Optional protocols / headers, see {@link ClientWebSocketOptions}
|
|
80
|
+
*/
|
|
81
|
+
constructor(url: string, options?: ClientWebSocketOptions);
|
|
82
|
+
/**
|
|
83
|
+
* Current connection state, mirroring {@link WebSocket.readyState}.
|
|
84
|
+
*
|
|
85
|
+
* @returns `0` connecting, `1` open, `2` closing, `3` closed.
|
|
86
|
+
*/
|
|
87
|
+
get readyState(): number;
|
|
88
|
+
/**
|
|
89
|
+
* Send a payload to the server.
|
|
90
|
+
*
|
|
91
|
+
* @param data - WHATWG-compatible payload
|
|
92
|
+
*/
|
|
93
|
+
send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
|
|
94
|
+
/**
|
|
95
|
+
* Close the underlying socket.
|
|
96
|
+
*
|
|
97
|
+
* @param code - Close code (defaults to 1000)
|
|
98
|
+
* @param reason - Optional human-readable reason
|
|
99
|
+
*/
|
|
100
|
+
close(code?: number, reason?: string): void;
|
|
101
|
+
/**
|
|
102
|
+
* Assign the open-event listener.
|
|
103
|
+
*/
|
|
104
|
+
set onopen(cb: ((ev: Event) => void) | null);
|
|
105
|
+
/**
|
|
106
|
+
* Assign the message-event listener.
|
|
107
|
+
*/
|
|
108
|
+
set onmessage(cb: ((ev: MessageEvent) => void) | null);
|
|
109
|
+
/**
|
|
110
|
+
* Assign the close-event listener.
|
|
111
|
+
*/
|
|
112
|
+
set onclose(cb: ((ev: CloseEvent) => void) | null);
|
|
113
|
+
/**
|
|
114
|
+
* Assign the error-event listener.
|
|
115
|
+
*/
|
|
116
|
+
set onerror(cb: ((ev: Event) => void) | null);
|
|
117
|
+
}
|
|
118
|
+
//#endregion
|
|
119
|
+
export { type ClientWebSocketOptions, WebSocketClient, createWebSocket, resolveWebSocket };
|
|
120
|
+
//# sourceMappingURL=client.d.cts.map
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { ClientWebSocketOptions } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/client.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Resolve the best `WebSocket` constructor for the current runtime.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* - Bun, Deno, browsers and Node ≥ 22 expose `globalThis.WebSocket`.
|
|
9
|
+
* - Older Node falls back to the [`ws`](https://github.com/websockets/ws)
|
|
10
|
+
* package, which mirrors the WHATWG `WebSocket` API.
|
|
11
|
+
*
|
|
12
|
+
* @returns A `WebSocket` constructor compatible with the WHATWG interface.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { resolveWebSocket } from "@hedystia/ws/client";
|
|
17
|
+
*
|
|
18
|
+
* const WS = resolveWebSocket();
|
|
19
|
+
* const socket = new WS("ws://localhost:3000");
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
declare function resolveWebSocket(): typeof WebSocket;
|
|
23
|
+
/**
|
|
24
|
+
* Create a `WebSocket` instance using the best available implementation
|
|
25
|
+
* for the current runtime.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* Custom request headers are honoured on Node via the `ws` package; on
|
|
29
|
+
* runtimes that ship a WHATWG-compliant global `WebSocket` (Bun, Deno,
|
|
30
|
+
* browsers, Node ≥ 22) headers are ignored — matching standard semantics.
|
|
31
|
+
*
|
|
32
|
+
* @param url - Absolute WebSocket URL (`ws://` or `wss://`)
|
|
33
|
+
* @param options - Optional protocols / headers
|
|
34
|
+
* @returns A connected (or connecting) `WebSocket` instance.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* import { createWebSocket } from "@hedystia/ws/client";
|
|
39
|
+
*
|
|
40
|
+
* const ws = createWebSocket("ws://localhost:3000", {
|
|
41
|
+
* protocols: "v1",
|
|
42
|
+
* headers: { authorization: "Bearer ..." },
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* ws.onopen = () => ws.send("hi");
|
|
46
|
+
* ws.onmessage = (event) => console.log(event.data);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
declare function createWebSocket(url: string, options?: ClientWebSocketOptions): WebSocket;
|
|
50
|
+
/**
|
|
51
|
+
* Lightweight runtime-agnostic wrapper that mirrors a small, predictable
|
|
52
|
+
* subset of the WHATWG WebSocket interface.
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* Useful for higher-level code that wants to assign event handlers by
|
|
56
|
+
* property (`socket.onmessage = ...`) without caring whether the underlying
|
|
57
|
+
* implementation comes from `globalThis.WebSocket` or the `ws` package.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* import { WebSocketClient } from "@hedystia/ws/client";
|
|
62
|
+
*
|
|
63
|
+
* const client = new WebSocketClient("ws://localhost:3000");
|
|
64
|
+
* client.onopen = () => client.send("hello");
|
|
65
|
+
* client.onmessage = (event) => console.log(event.data);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
declare class WebSocketClient {
|
|
69
|
+
/**
|
|
70
|
+
* Underlying WebSocket instance produced by {@link createWebSocket}.
|
|
71
|
+
*
|
|
72
|
+
* @readonly
|
|
73
|
+
*/
|
|
74
|
+
readonly socket: WebSocket;
|
|
75
|
+
/**
|
|
76
|
+
* Create a new client and immediately initiate the connection.
|
|
77
|
+
*
|
|
78
|
+
* @param url - Absolute WebSocket URL (`ws://` or `wss://`)
|
|
79
|
+
* @param options - Optional protocols / headers, see {@link ClientWebSocketOptions}
|
|
80
|
+
*/
|
|
81
|
+
constructor(url: string, options?: ClientWebSocketOptions);
|
|
82
|
+
/**
|
|
83
|
+
* Current connection state, mirroring {@link WebSocket.readyState}.
|
|
84
|
+
*
|
|
85
|
+
* @returns `0` connecting, `1` open, `2` closing, `3` closed.
|
|
86
|
+
*/
|
|
87
|
+
get readyState(): number;
|
|
88
|
+
/**
|
|
89
|
+
* Send a payload to the server.
|
|
90
|
+
*
|
|
91
|
+
* @param data - WHATWG-compatible payload
|
|
92
|
+
*/
|
|
93
|
+
send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
|
|
94
|
+
/**
|
|
95
|
+
* Close the underlying socket.
|
|
96
|
+
*
|
|
97
|
+
* @param code - Close code (defaults to 1000)
|
|
98
|
+
* @param reason - Optional human-readable reason
|
|
99
|
+
*/
|
|
100
|
+
close(code?: number, reason?: string): void;
|
|
101
|
+
/**
|
|
102
|
+
* Assign the open-event listener.
|
|
103
|
+
*/
|
|
104
|
+
set onopen(cb: ((ev: Event) => void) | null);
|
|
105
|
+
/**
|
|
106
|
+
* Assign the message-event listener.
|
|
107
|
+
*/
|
|
108
|
+
set onmessage(cb: ((ev: MessageEvent) => void) | null);
|
|
109
|
+
/**
|
|
110
|
+
* Assign the close-event listener.
|
|
111
|
+
*/
|
|
112
|
+
set onclose(cb: ((ev: CloseEvent) => void) | null);
|
|
113
|
+
/**
|
|
114
|
+
* Assign the error-event listener.
|
|
115
|
+
*/
|
|
116
|
+
set onerror(cb: ((ev: Event) => void) | null);
|
|
117
|
+
}
|
|
118
|
+
//#endregion
|
|
119
|
+
export { type ClientWebSocketOptions, WebSocketClient, createWebSocket, resolveWebSocket };
|
|
120
|
+
//# sourceMappingURL=client.d.mts.map
|
package/dist/client.mjs
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { __require } from "./_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
//#region src/client.ts
|
|
3
|
+
/**
|
|
4
|
+
* Resolve the best `WebSocket` constructor for the current runtime.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* - Bun, Deno, browsers and Node ≥ 22 expose `globalThis.WebSocket`.
|
|
8
|
+
* - Older Node falls back to the [`ws`](https://github.com/websockets/ws)
|
|
9
|
+
* package, which mirrors the WHATWG `WebSocket` API.
|
|
10
|
+
*
|
|
11
|
+
* @returns A `WebSocket` constructor compatible with the WHATWG interface.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { resolveWebSocket } from "@hedystia/ws/client";
|
|
16
|
+
*
|
|
17
|
+
* const WS = resolveWebSocket();
|
|
18
|
+
* const socket = new WS("ws://localhost:3000");
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
function resolveWebSocket() {
|
|
22
|
+
if (typeof globalThis !== "undefined" && globalThis.WebSocket) return globalThis.WebSocket;
|
|
23
|
+
const mod = __require("ws");
|
|
24
|
+
return mod.WebSocket ?? mod;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Create a `WebSocket` instance using the best available implementation
|
|
28
|
+
* for the current runtime.
|
|
29
|
+
*
|
|
30
|
+
* @remarks
|
|
31
|
+
* Custom request headers are honoured on Node via the `ws` package; on
|
|
32
|
+
* runtimes that ship a WHATWG-compliant global `WebSocket` (Bun, Deno,
|
|
33
|
+
* browsers, Node ≥ 22) headers are ignored — matching standard semantics.
|
|
34
|
+
*
|
|
35
|
+
* @param url - Absolute WebSocket URL (`ws://` or `wss://`)
|
|
36
|
+
* @param options - Optional protocols / headers
|
|
37
|
+
* @returns A connected (or connecting) `WebSocket` instance.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* import { createWebSocket } from "@hedystia/ws/client";
|
|
42
|
+
*
|
|
43
|
+
* const ws = createWebSocket("ws://localhost:3000", {
|
|
44
|
+
* protocols: "v1",
|
|
45
|
+
* headers: { authorization: "Bearer ..." },
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* ws.onopen = () => ws.send("hi");
|
|
49
|
+
* ws.onmessage = (event) => console.log(event.data);
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
function createWebSocket(url, options) {
|
|
53
|
+
const Ctor = resolveWebSocket();
|
|
54
|
+
if (typeof globalThis !== "undefined" && globalThis.WebSocket === Ctor) return options?.protocols ? new Ctor(url, options.protocols) : new Ctor(url);
|
|
55
|
+
const init = {};
|
|
56
|
+
if (options?.headers) init.headers = options.headers;
|
|
57
|
+
return new Ctor(url, options?.protocols, init);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Lightweight runtime-agnostic wrapper that mirrors a small, predictable
|
|
61
|
+
* subset of the WHATWG WebSocket interface.
|
|
62
|
+
*
|
|
63
|
+
* @remarks
|
|
64
|
+
* Useful for higher-level code that wants to assign event handlers by
|
|
65
|
+
* property (`socket.onmessage = ...`) without caring whether the underlying
|
|
66
|
+
* implementation comes from `globalThis.WebSocket` or the `ws` package.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* import { WebSocketClient } from "@hedystia/ws/client";
|
|
71
|
+
*
|
|
72
|
+
* const client = new WebSocketClient("ws://localhost:3000");
|
|
73
|
+
* client.onopen = () => client.send("hello");
|
|
74
|
+
* client.onmessage = (event) => console.log(event.data);
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
var WebSocketClient = class {
|
|
78
|
+
/**
|
|
79
|
+
* Underlying WebSocket instance produced by {@link createWebSocket}.
|
|
80
|
+
*
|
|
81
|
+
* @readonly
|
|
82
|
+
*/
|
|
83
|
+
socket;
|
|
84
|
+
/**
|
|
85
|
+
* Create a new client and immediately initiate the connection.
|
|
86
|
+
*
|
|
87
|
+
* @param url - Absolute WebSocket URL (`ws://` or `wss://`)
|
|
88
|
+
* @param options - Optional protocols / headers, see {@link ClientWebSocketOptions}
|
|
89
|
+
*/
|
|
90
|
+
constructor(url, options) {
|
|
91
|
+
this.socket = createWebSocket(url, options);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Current connection state, mirroring {@link WebSocket.readyState}.
|
|
95
|
+
*
|
|
96
|
+
* @returns `0` connecting, `1` open, `2` closing, `3` closed.
|
|
97
|
+
*/
|
|
98
|
+
get readyState() {
|
|
99
|
+
return this.socket.readyState;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Send a payload to the server.
|
|
103
|
+
*
|
|
104
|
+
* @param data - WHATWG-compatible payload
|
|
105
|
+
*/
|
|
106
|
+
send(data) {
|
|
107
|
+
this.socket.send(data);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Close the underlying socket.
|
|
111
|
+
*
|
|
112
|
+
* @param code - Close code (defaults to 1000)
|
|
113
|
+
* @param reason - Optional human-readable reason
|
|
114
|
+
*/
|
|
115
|
+
close(code, reason) {
|
|
116
|
+
this.socket.close(code, reason);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Assign the open-event listener.
|
|
120
|
+
*/
|
|
121
|
+
set onopen(cb) {
|
|
122
|
+
this.socket.onopen = cb;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Assign the message-event listener.
|
|
126
|
+
*/
|
|
127
|
+
set onmessage(cb) {
|
|
128
|
+
this.socket.onmessage = cb;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Assign the close-event listener.
|
|
132
|
+
*/
|
|
133
|
+
set onclose(cb) {
|
|
134
|
+
this.socket.onclose = cb;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Assign the error-event listener.
|
|
138
|
+
*/
|
|
139
|
+
set onerror(cb) {
|
|
140
|
+
this.socket.onerror = cb;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
//#endregion
|
|
144
|
+
export { WebSocketClient, createWebSocket, resolveWebSocket };
|
|
145
|
+
|
|
146
|
+
//# sourceMappingURL=client.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.mjs","names":[],"sources":["../src/client.ts"],"sourcesContent":["import type { ClientWebSocketOptions } from \"./types\";\n\nexport type { ClientWebSocketOptions } from \"./types\";\n\n/**\n * Resolve the best `WebSocket` constructor for the current runtime.\n *\n * @remarks\n * - Bun, Deno, browsers and Node ≥ 22 expose `globalThis.WebSocket`.\n * - Older Node falls back to the [`ws`](https://github.com/websockets/ws)\n * package, which mirrors the WHATWG `WebSocket` API.\n *\n * @returns A `WebSocket` constructor compatible with the WHATWG interface.\n *\n * @example\n * ```ts\n * import { resolveWebSocket } from \"@hedystia/ws/client\";\n *\n * const WS = resolveWebSocket();\n * const socket = new WS(\"ws://localhost:3000\");\n * ```\n */\nexport function resolveWebSocket(): typeof WebSocket {\n if (typeof globalThis !== \"undefined\" && (globalThis as any).WebSocket) {\n return (globalThis as any).WebSocket as typeof WebSocket;\n }\n const mod = require(\"ws\");\n return (mod.WebSocket ?? mod) as typeof WebSocket;\n}\n\n/**\n * Create a `WebSocket` instance using the best available implementation\n * for the current runtime.\n *\n * @remarks\n * Custom request headers are honoured on Node via the `ws` package; on\n * runtimes that ship a WHATWG-compliant global `WebSocket` (Bun, Deno,\n * browsers, Node ≥ 22) headers are ignored — matching standard semantics.\n *\n * @param url - Absolute WebSocket URL (`ws://` or `wss://`)\n * @param options - Optional protocols / headers\n * @returns A connected (or connecting) `WebSocket` instance.\n *\n * @example\n * ```ts\n * import { createWebSocket } from \"@hedystia/ws/client\";\n *\n * const ws = createWebSocket(\"ws://localhost:3000\", {\n * protocols: \"v1\",\n * headers: { authorization: \"Bearer ...\" },\n * });\n *\n * ws.onopen = () => ws.send(\"hi\");\n * ws.onmessage = (event) => console.log(event.data);\n * ```\n */\nexport function createWebSocket(url: string, options?: ClientWebSocketOptions): WebSocket {\n const Ctor = resolveWebSocket();\n const isWhatwg =\n typeof globalThis !== \"undefined\" && (globalThis as any).WebSocket === (Ctor as any);\n\n if (isWhatwg) {\n return options?.protocols ? new Ctor(url, options.protocols as any) : new Ctor(url);\n }\n\n const init: any = {};\n if (options?.headers) {\n init.headers = options.headers;\n }\n return new (Ctor as any)(url, options?.protocols, init);\n}\n\n/**\n * Lightweight runtime-agnostic wrapper that mirrors a small, predictable\n * subset of the WHATWG WebSocket interface.\n *\n * @remarks\n * Useful for higher-level code that wants to assign event handlers by\n * property (`socket.onmessage = ...`) without caring whether the underlying\n * implementation comes from `globalThis.WebSocket` or the `ws` package.\n *\n * @example\n * ```ts\n * import { WebSocketClient } from \"@hedystia/ws/client\";\n *\n * const client = new WebSocketClient(\"ws://localhost:3000\");\n * client.onopen = () => client.send(\"hello\");\n * client.onmessage = (event) => console.log(event.data);\n * ```\n */\nexport class WebSocketClient {\n /**\n * Underlying WebSocket instance produced by {@link createWebSocket}.\n *\n * @readonly\n */\n readonly socket: WebSocket;\n\n /**\n * Create a new client and immediately initiate the connection.\n *\n * @param url - Absolute WebSocket URL (`ws://` or `wss://`)\n * @param options - Optional protocols / headers, see {@link ClientWebSocketOptions}\n */\n constructor(url: string, options?: ClientWebSocketOptions) {\n this.socket = createWebSocket(url, options);\n }\n\n /**\n * Current connection state, mirroring {@link WebSocket.readyState}.\n *\n * @returns `0` connecting, `1` open, `2` closing, `3` closed.\n */\n get readyState(): number {\n return this.socket.readyState;\n }\n\n /**\n * Send a payload to the server.\n *\n * @param data - WHATWG-compatible payload\n */\n send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void {\n this.socket.send(data as any);\n }\n\n /**\n * Close the underlying socket.\n *\n * @param code - Close code (defaults to 1000)\n * @param reason - Optional human-readable reason\n */\n close(code?: number, reason?: string): void {\n this.socket.close(code, reason);\n }\n\n /**\n * Assign the open-event listener.\n */\n set onopen(cb: ((ev: Event) => void) | null) {\n (this.socket as any).onopen = cb;\n }\n /**\n * Assign the message-event listener.\n */\n set onmessage(cb: ((ev: MessageEvent) => void) | null) {\n (this.socket as any).onmessage = cb;\n }\n /**\n * Assign the close-event listener.\n */\n set onclose(cb: ((ev: CloseEvent) => void) | null) {\n (this.socket as any).onclose = cb;\n }\n /**\n * Assign the error-event listener.\n */\n set onerror(cb: ((ev: Event) => void) | null) {\n (this.socket as any).onerror = cb;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,mBAAqC;CACnD,IAAI,OAAO,eAAe,eAAgB,WAAmB,WAC3D,OAAQ,WAAmB;CAE7B,MAAM,MAAA,UAAc,KAAK;CACzB,OAAQ,IAAI,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6B3B,SAAgB,gBAAgB,KAAa,SAA6C;CACxF,MAAM,OAAO,kBAAkB;CAI/B,IAFE,OAAO,eAAe,eAAgB,WAAmB,cAAe,MAGxE,OAAO,SAAS,YAAY,IAAI,KAAK,KAAK,QAAQ,UAAiB,GAAG,IAAI,KAAK,IAAI;CAGrF,MAAM,OAAY,EAAE;CACpB,IAAI,SAAS,SACX,KAAK,UAAU,QAAQ;CAEzB,OAAO,IAAK,KAAa,KAAK,SAAS,WAAW,KAAK;;;;;;;;;;;;;;;;;;;;AAqBzD,IAAa,kBAAb,MAA6B;;;;;;CAM3B;;;;;;;CAQA,YAAY,KAAa,SAAkC;EACzD,KAAK,SAAS,gBAAgB,KAAK,QAAQ;;;;;;;CAQ7C,IAAI,aAAqB;EACvB,OAAO,KAAK,OAAO;;;;;;;CAQrB,KAAK,MAA+D;EAClE,KAAK,OAAO,KAAK,KAAY;;;;;;;;CAS/B,MAAM,MAAe,QAAuB;EAC1C,KAAK,OAAO,MAAM,MAAM,OAAO;;;;;CAMjC,IAAI,OAAO,IAAkC;EAC3C,KAAM,OAAe,SAAS;;;;;CAKhC,IAAI,UAAU,IAAyC;EACrD,KAAM,OAAe,YAAY;;;;;CAKnC,IAAI,QAAQ,IAAuC;EACjD,KAAM,OAAe,UAAU;;;;;CAKjC,IAAI,QAAQ,IAAkC;EAC5C,KAAM,OAAe,UAAU"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
const require_client = require("./client.cjs");
|
|
6
|
+
const require_runtime = require("./runtime.cjs");
|
|
7
|
+
const require_server = require("./server.cjs");
|
|
8
|
+
//#region src/index.ts
|
|
9
|
+
var src_default = require_server.WebSocketServer;
|
|
10
|
+
//#endregion
|
|
11
|
+
exports.WebSocketClient = require_client.WebSocketClient;
|
|
12
|
+
exports.WebSocketServer = require_server.WebSocketServer;
|
|
13
|
+
exports.createWebSocket = require_client.createWebSocket;
|
|
14
|
+
exports.default = src_default;
|
|
15
|
+
exports.detectRuntime = require_runtime.detectRuntime;
|
|
16
|
+
exports.isBrowser = require_runtime.isBrowser;
|
|
17
|
+
exports.isBun = require_runtime.isBun;
|
|
18
|
+
exports.isDeno = require_runtime.isDeno;
|
|
19
|
+
exports.isNode = require_runtime.isNode;
|
|
20
|
+
exports.resolveWebSocket = require_client.resolveWebSocket;
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["WebSocketServer"],"sources":["../src/index.ts"],"sourcesContent":["export type { ClientWebSocketOptions } from \"./client\";\nexport { createWebSocket, resolveWebSocket, WebSocketClient } from \"./client\";\nexport type { Runtime } from \"./runtime\";\nexport { detectRuntime, isBrowser, isBun, isDeno, isNode } from \"./runtime\";\nexport type {\n ServerWebSocket,\n UpgradeOptions,\n UpgradeRequest,\n WebSocketHandlers,\n WebSocketServerOptions,\n WSData,\n WSMessage,\n} from \"./server\";\n\nimport { WebSocketServer } from \"./server\";\n\nexport { WebSocketServer };\n\nexport default WebSocketServer;\n"],"mappings":";;;;;;;;AAkBA,IAAA,cAAeA,eAAAA"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ClientWebSocketOptions, ServerWebSocket, UpgradeOptions, UpgradeRequest, WSData, WSMessage, WebSocketHandlers, WebSocketServerOptions } from "./types.cjs";
|
|
2
|
+
import { WebSocketClient, createWebSocket, resolveWebSocket } from "./client.cjs";
|
|
3
|
+
import { Runtime, detectRuntime, isBrowser, isBun, isDeno, isNode } from "./runtime.cjs";
|
|
4
|
+
import { WebSocketServer } from "./server.cjs";
|
|
5
|
+
export { type ClientWebSocketOptions, type Runtime, type ServerWebSocket, type UpgradeOptions, type UpgradeRequest, type WSData, type WSMessage, WebSocketClient, type WebSocketHandlers, WebSocketServer, WebSocketServer as default, type WebSocketServerOptions, createWebSocket, detectRuntime, isBrowser, isBun, isDeno, isNode, resolveWebSocket };
|