@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.
@@ -0,0 +1,50 @@
1
+ $ tsdown --config-loader unrun
2
+ ℹ tsdown v0.22.0 powered by rolldown v1.0.0
3
+ ℹ config file: /home/runner/actions-runner/_work/Hedystia/Hedystia/Packages/websocket/tsdown.config.ts (unrun)
4
+ ℹ entry: src/index.ts, src/client.ts, src/server.ts
5
+ ℹ tsconfig: tsconfig.json
6
+ ℹ Build start
7
+ ℹ [CJS] dist/server.cjs  7.36 kB │ gzip: 2.69 kB
8
+ ℹ [CJS] dist/client.cjs  4.12 kB │ gzip: 1.53 kB
9
+ ℹ [CJS] dist/index.cjs  0.87 kB │ gzip: 0.34 kB
10
+ ℹ [CJS] dist/server.cjs.map 12.54 kB │ gzip: 4.06 kB
11
+ ℹ [CJS] dist/client.cjs.map  5.86 kB │ gzip: 2.05 kB
12
+ ℹ [CJS] dist/runtime.cjs.map  2.06 kB │ gzip: 0.78 kB
13
+ ℹ [CJS] dist/runtime.cjs  1.36 kB │ gzip: 0.55 kB
14
+ ℹ [CJS] dist/index.cjs.map  0.71 kB │ gzip: 0.33 kB
15
+ ℹ [CJS] 8 files, total: 34.86 kB
16
+ [PLUGIN_TIMINGS] Warning: Your build spent significant time in plugin `tsdown:report`. See https://rolldown.rs/options/checks#plugintimings for more details.
17
+
18
+ ℹ [CJS] dist/server.d.cts 4.05 kB │ gzip: 1.65 kB
19
+ ℹ [CJS] dist/client.d.cts 3.80 kB │ gzip: 1.40 kB
20
+ ℹ [CJS] dist/index.d.cts 0.73 kB │ gzip: 0.27 kB
21
+ ℹ [CJS] dist/types.d.cts 7.25 kB │ gzip: 2.64 kB
22
+ ℹ [CJS] dist/runtime.d.cts 1.13 kB │ gzip: 0.48 kB
23
+ ℹ [CJS] 5 files, total: 16.96 kB
24
+ [PLUGIN_TIMINGS] Warning: 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
+ ✔ Build complete in 4736ms
30
+ ℹ [ESM] dist/server.mjs  7.30 kB │ gzip: 2.64 kB
31
+ ℹ [ESM] dist/client.mjs  4.04 kB │ gzip: 1.50 kB
32
+ ℹ [ESM] dist/index.mjs  0.47 kB │ gzip: 0.23 kB
33
+ ℹ [ESM] dist/server.mjs.map 12.53 kB │ gzip: 4.05 kB
34
+ ℹ [ESM] dist/client.mjs.map  5.86 kB │ gzip: 2.05 kB
35
+ ℹ [ESM] dist/runtime.mjs.map  2.06 kB │ gzip: 0.78 kB
36
+ ℹ [ESM] dist/runtime.mjs  1.27 kB │ gzip: 0.53 kB
37
+ ℹ [ESM] dist/index.mjs.map  0.68 kB │ gzip: 0.32 kB
38
+ ℹ [ESM] dist/_virtual/_rolldown/runtime.mjs  0.18 kB │ gzip: 0.15 kB
39
+ ℹ [ESM] dist/server.d.mts  4.05 kB │ gzip: 1.65 kB
40
+ ℹ [ESM] dist/client.d.mts  3.80 kB │ gzip: 1.40 kB
41
+ ℹ [ESM] dist/index.d.mts  0.73 kB │ gzip: 0.27 kB
42
+ ℹ [ESM] dist/types.d.mts  7.25 kB │ gzip: 2.64 kB
43
+ ℹ [ESM] dist/runtime.d.mts  1.13 kB │ gzip: 0.48 kB
44
+ ℹ [ESM] 14 files, total: 51.34 kB
45
+ [PLUGIN_TIMINGS] Warning: 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
+ ✔ Build complete in 4846ms
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.
@@ -0,0 +1,5 @@
1
+ import { createRequire } from "node:module";
2
+ //#region \0rolldown/runtime.js
3
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
4
+ //#endregion
5
+ export { __require };
@@ -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
@@ -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"}
@@ -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 };