@orkestrel/websocket 0.0.11 → 0.0.12

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/README.md CHANGED
@@ -1,9 +1,14 @@
1
1
  # @orkestrel/websocket
2
2
 
3
- A dependency-light RFC 6455 WebSocket for Node native handshake and framing
4
- over duplex streams, with a typed emitter surface. Part of the `@orkestrel`
5
- line. Its sole runtime dependency is `@orkestrel/emitter`, used for the typed
6
- `emitter` every connection exposes.
3
+ > The server-native bidirectional transport: a lean, typed wrapper over a raw upgraded
4
+ > `node:stream` Duplex socket that speaks only the RFC 6455 wire protocol, owning the
5
+ > handshake, the masked and unmasked frame codec, ping and pong, and the close handshake,
6
+ > and surfacing every message on an owned `emitter`.
7
+
8
+ Take the socket a `node:http` upgrade handler gives you, pass it to the
9
+ `createNodeWebSocket` function, and read every message off the returned handle's
10
+ `emitter`. Its sole runtime dependency is `@orkestrel/emitter`, which supplies that
11
+ typed emitter. Part of the `@orkestrel` line.
7
12
 
8
13
  ## Install
9
14
 
@@ -24,7 +29,7 @@ import { createServer } from 'node:http'
24
29
  import { createNodeWebSocket } from '@orkestrel/websocket'
25
30
 
26
31
  // A node:http server hands every upgrade request a raw socket; this wrapper takes it
27
- // from there. Passing the client's `sec-websocket-key` selects SERVER mode — the
32
+ // from there. Passing the client's `sec-websocket-key` selects server mode — the
28
33
  // wrapper writes the 101 handshake, marks the connection open, and decodes frames.
29
34
  createServer().on('upgrade', (request, socket, head) => {
30
35
  const key = request.headers['sec-websocket-key']
@@ -3,68 +3,183 @@ let node_crypto = require("node:crypto");
3
3
  let _orkestrel_emitter = require("@orkestrel/emitter");
4
4
  //#region src/server/constants.ts
5
5
  /**
6
- * Names the RFC 6455 GUID concatenated to a client's `Sec-WebSocket-Key` before the SHA-1
7
- * hash that yields the `Sec-WebSocket-Accept` response value.
6
+ * Names the accept GUID concatenated to a client's `Sec-WebSocket-Key` before the accept
7
+ * hash, '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'.
8
8
  *
9
9
  * @remarks
10
- * A fixed, spec-mandated constant (RFC 6455 §4.2.2) read only by
10
+ * The base64-encoded SHA-1 of that concatenation is the `Sec-WebSocket-Accept` response
11
+ * value. A fixed, spec-mandated constant (RFC 6455 §4.2.2) — read only by
11
12
  * {@link computeWebSocketAccept}.
12
13
  */
13
14
  var WEBSOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
14
- /** Names the WebSocket protocol version this wrapper speaks (`Sec-WebSocket-Version: 13`). */
15
+ /**
16
+ * Names the supported protocol version, '13'.
17
+ *
18
+ * @remarks
19
+ * The value this wrapper speaks, carried by the `Sec-WebSocket-Version` handshake header.
20
+ */
15
21
  var WEBSOCKET_VERSION = "13";
16
- /** Names the text frame opcode — a UTF-8 payload (RFC 6455 §5.6). */
22
+ /**
23
+ * Names the text frame opcode, 0x01.
24
+ *
25
+ * @remarks
26
+ * A UTF-8 payload (RFC 6455 §5.6).
27
+ */
17
28
  var WEBSOCKET_OPCODE_TEXT = 1;
18
- /** Names the binary frame opcode — a raw byte payload (RFC 6455 §5.6). */
29
+ /**
30
+ * Names the binary frame opcode, 0x02.
31
+ *
32
+ * @remarks
33
+ * A raw byte payload (RFC 6455 §5.6).
34
+ */
19
35
  var WEBSOCKET_OPCODE_BINARY = 2;
20
- /** Names the continuation frame opcode — the next fragment of an open data message (RFC 6455 §5.4). */
36
+ /**
37
+ * Names the continuation frame opcode, 0x00.
38
+ *
39
+ * @remarks
40
+ * The next fragment of an open data message (RFC 6455 §5.4).
41
+ */
21
42
  var WEBSOCKET_OPCODE_CONTINUATION = 0;
22
- /** Names the close frame opcode — a control frame ending the connection (RFC 6455 §5.5.1). */
43
+ /**
44
+ * Names the close frame opcode, 0x08.
45
+ *
46
+ * @remarks
47
+ * A control frame ending the connection (RFC 6455 §5.5.1).
48
+ */
23
49
  var WEBSOCKET_OPCODE_CLOSE = 8;
24
- /** Names the ping frame opcode — a control frame the peer must answer with a pong (RFC 6455 §5.5.2). */
50
+ /**
51
+ * Names the ping frame opcode, 0x09.
52
+ *
53
+ * @remarks
54
+ * A control frame the peer must answer with a pong (RFC 6455 §5.5.2).
55
+ */
25
56
  var WEBSOCKET_OPCODE_PING = 9;
26
- /** Names the pong frame opcode — a control frame answering a ping (RFC 6455 §5.5.3). */
57
+ /**
58
+ * Names the pong frame opcode, 0x0a.
59
+ *
60
+ * @remarks
61
+ * A control frame answering a ping (RFC 6455 §5.5.3).
62
+ */
27
63
  var WEBSOCKET_OPCODE_PONG = 10;
28
- /** Names the ready state for a connecting WebSocket (before the handshake completes). */
64
+ /**
65
+ * Names the connecting ready state, 0.
66
+ *
67
+ * @remarks
68
+ * The state a WebSocket holds before its handshake completes.
69
+ */
29
70
  var WEBSOCKET_READY_CONNECTING = 0;
30
- /** Names the ready state for an open WebSocket (the handshake completed; frames flow). */
71
+ /**
72
+ * Names the open ready state, 1.
73
+ *
74
+ * @remarks
75
+ * The state a WebSocket holds after the handshake completes and while frames flow.
76
+ */
31
77
  var WEBSOCKET_READY_OPEN = 1;
32
- /** Names the ready state for a closing WebSocket (a close frame was sent or received). */
78
+ /**
79
+ * Names the closing ready state, 2.
80
+ *
81
+ * @remarks
82
+ * The state a WebSocket holds after a close frame is sent or received.
83
+ */
33
84
  var WEBSOCKET_READY_CLOSING = 2;
34
- /** Names the ready state for a closed WebSocket (the socket ended). */
85
+ /**
86
+ * Names the closed ready state, 3.
87
+ *
88
+ * @remarks
89
+ * The state a WebSocket holds after the socket ends.
90
+ */
35
91
  var WEBSOCKET_READY_CLOSED = 3;
36
- /** Names the normal-closure status code (RFC 6455 §7.4.1) — the default `close` code. */
92
+ /**
93
+ * Names the normal-closure status code, 1000.
94
+ *
95
+ * @remarks
96
+ * The default `close` code (RFC 6455 §7.4.1).
97
+ */
37
98
  var WEBSOCKET_CLOSE_NORMAL = 1e3;
38
- /** Names the protocol-error status code (RFC 6455 §7.4.1) — a framing/state rule was violated. */
99
+ /**
100
+ * Names the protocol-error status code, 1002.
101
+ *
102
+ * @remarks
103
+ * Sent when a framing or state rule was violated (RFC 6455 §7.4.1).
104
+ */
39
105
  var WEBSOCKET_CLOSE_PROTOCOL = 1002;
40
- /** Names the unsupported-data status code (RFC 6455 §7.4.1) — the endpoint received a data type it cannot accept, for example binary on a text-only endpoint. */
106
+ /**
107
+ * Names the unsupported-data status code, 1003.
108
+ *
109
+ * @remarks
110
+ * Sent when the endpoint received a data type it cannot accept (RFC 6455 §7.4.1), for
111
+ * example binary on a text-only endpoint.
112
+ */
41
113
  var WEBSOCKET_CLOSE_UNSUPPORTED = 1003;
42
- /** Names the invalid-frame-payload-data status code (RFC 6455 §7.4.1) — for example non-UTF-8 text or an unparseable close reason. */
114
+ /**
115
+ * Names the invalid-frame-payload-data status code, 1007.
116
+ *
117
+ * @remarks
118
+ * Sent for non-UTF-8 text or an unparseable close reason (RFC 6455 §7.4.1).
119
+ */
43
120
  var WEBSOCKET_CLOSE_INVALID = 1007;
44
- /** Names the message-too-big status code (RFC 6455 §7.4.1) — a reassembled message exceeded the payload cap. */
121
+ /**
122
+ * Names the message-too-big status code, 1009.
123
+ *
124
+ * @remarks
125
+ * Sent when a reassembled message exceeded the payload cap (RFC 6455 §7.4.1).
126
+ */
45
127
  var WEBSOCKET_CLOSE_TOO_BIG = 1009;
46
- /** Names the default maximum inbound single-frame length AND reassembled-message total byte count (100 MiB — the `ws` package default). */
128
+ /**
129
+ * Names the default cap on both an inbound frame's declared length and a reassembled
130
+ * message's total byte count, 104,857,600 bytes (100 MiB).
131
+ *
132
+ * @remarks
133
+ * The same value the `ws` package defaults to. Either breach closes
134
+ * {@link WEBSOCKET_CLOSE_TOO_BIG}.
135
+ */
47
136
  var WEBSOCKET_MAX_PAYLOAD = 104857600;
48
- /** Names the default close-handshake timeout in milliseconds — how long `close()` waits for the peer's echo before tearing the socket down. */
137
+ /**
138
+ * Names the default close-handshake timeout, 30,000 milliseconds — how long `close` waits
139
+ * for the peer's echo.
140
+ *
141
+ * @remarks
142
+ * After it expires the wrapper tears the socket down, so a silent peer cannot leak the
143
+ * handle open.
144
+ */
49
145
  var WEBSOCKET_CLOSE_TIMEOUT_MS = 3e4;
50
- /** Names the post-`#fail` flush grace in milliseconds — how long a validation-breach close frame is given to flush through the socket's write buffer before the hard `destroy()` fallback fires (the normal path destroys sooner, on the `end()` flush callback). */
146
+ /**
147
+ * Names the flush grace, 1,000 milliseconds, a validation-breach close frame is given
148
+ * before the hard teardown fallback destroys the socket.
149
+ *
150
+ * @remarks
151
+ * Armed after `#fail` writes the close frame, so the frame drains through the socket's
152
+ * write buffer rather than being discarded. The normal path destroys sooner, on the
153
+ * `end()` flush callback.
154
+ */
51
155
  var WEBSOCKET_FAIL_TIMEOUT_MS = 1e3;
52
- /** Names the maximum control-frame payload length in bytes (RFC 6455 §5.5). */
156
+ /**
157
+ * Names the maximum control-frame payload length, 125 bytes.
158
+ *
159
+ * @remarks
160
+ * The cap RFC 6455 §5.5 sets on every control frame's payload.
161
+ */
53
162
  var WEBSOCKET_CONTROL_MAX_LENGTH = 125;
54
- /** Names the maximum UTF-8 close-reason length after the two-byte status code. */
163
+ /**
164
+ * Names the maximum UTF-8 close-reason length after the two-byte status code, 123.
165
+ *
166
+ * @remarks
167
+ * What is left of {@link WEBSOCKET_CONTROL_MAX_LENGTH} after the close frame's status
168
+ * code.
169
+ */
55
170
  var WEBSOCKET_CLOSE_REASON_MAX_LENGTH = 123;
56
171
  //#endregion
57
172
  //#region src/server/errors.ts
58
173
  /**
59
- * Represents an error thrown by the WebSocket wrapper for a refused caller-supplied value.
174
+ * Represents an error the WebSocket wrapper throws for a refused caller-supplied value,
175
+ * carrying a machine-readable `code` and an optional `context`.
60
176
  *
61
177
  * @remarks
62
- * Carries a {@link WebSocketErrorCode} and an optional `context` record holding the
63
- * refused value under a key naming it: an `'OPTION'` carries the offending option
64
- * (`payload`, `timeout`, `key`, or `protocol`), a `'LIMIT'` carries `size` and the
65
- * `limit` it exceeded, a `'CLOSE'` carries the refused close `code`, and a `'FRAME'`
66
- * carries `opcode` or the mask's `size`. Narrow a caught value with
67
- * {@link isWebSocketError}.
178
+ * The `code` is a {@link WebSocketErrorCode}; the `context` record holds the refused
179
+ * value under a key naming it: an `'OPTION'` carries the offending option (`payload`,
180
+ * `timeout`, `key`, or `protocol`), a `'LIMIT'` carries `size` and the `limit` it
181
+ * exceeded, a `'CLOSE'` carries the refused close `code`, and a `'FRAME'` carries
182
+ * `opcode` or the mask's `size`. Narrow a caught value with {@link isWebSocketError}.
68
183
  *
69
184
  * @example
70
185
  * ```ts
@@ -95,7 +210,8 @@ var WebSocketError = class extends Error {
95
210
  }
96
211
  };
97
212
  /**
98
- * Checks whether a value is a {@link WebSocketError}.
213
+ * Checks whether a caught value is a {@link WebSocketError}, narrowing it so a `catch` can
214
+ * branch on `error.code`.
99
215
  *
100
216
  * @param value - The value to test (typically a `catch` binding)
101
217
  * @returns True if `value` is a `WebSocketError`; false otherwise
@@ -131,15 +247,15 @@ function computeWebSocketAccept(key) {
131
247
  return (0, node_crypto.createHash)("sha1").update(key + WEBSOCKET_GUID).digest("base64");
132
248
  }
133
249
  /**
134
- * Reads the declared payload length off the front of a buffer, without buffering or
135
- * reading the payload itself.
250
+ * Reads the declared payload length off the front of a buffer without buffering or
251
+ * reading the payload itself, answering `undefined` until the length field is complete.
136
252
  *
137
253
  * @remarks
138
254
  * Decodes only byte 1's 7-bit length field, extended by the 16-bit (`126`) or 64-bit
139
255
  * (`127`) form exactly like `parseWebSocketFrame` — but stops there, so a caller
140
256
  * can reject an over-cap frame the moment its length is known, before the payload
141
- * bytes have even arrived. Returns `undefined` until the length field itself is fully
142
- * buffered (mirrors the parser's incomplete-buffer contract). Pure; never throws.
257
+ * bytes have even arrived. The incomplete-buffer contract mirrors the parser's. Pure;
258
+ * never throws.
143
259
  *
144
260
  * @param buffer - The accumulation buffer to read the next frame's length from
145
261
  * @returns The declared payload length, or `undefined` when the buffer is too short to know it yet
@@ -166,14 +282,14 @@ function measureWebSocketFrame(buffer) {
166
282
  return length;
167
283
  }
168
284
  /**
169
- * Checks whether the next frame uses the shortest valid RFC 6455 payload-length encoding.
285
+ * Checks whether the next frame uses the shortest valid RFC 6455 payload-length
286
+ * encoding, answering `undefined` until its length prefix is complete.
170
287
  *
171
288
  * @remarks
172
- * Returns `undefined` until the complete length prefix is buffered. The 16-bit form
173
- * is canonical only for lengths at least 126; the 64-bit form only for lengths at
174
- * least 65,536 and with its most-significant bit clear (RFC 6455 §5.2). Reads the same
175
- * length prefix as {@link measureWebSocketFrame}, under the same incomplete-buffer
176
- * contract. Pure; never throws.
289
+ * The 16-bit form is canonical only for lengths at least 126; the 64-bit form only for
290
+ * lengths at least 65,536 and with its most-significant bit clear (RFC 6455 §5.2). Reads
291
+ * the same length prefix as {@link measureWebSocketFrame}, under the same
292
+ * incomplete-buffer contract. Pure; never throws.
177
293
  *
178
294
  * @param buffer - The accumulation buffer containing the next frame header
179
295
  * @returns Its canonicality, or `undefined` while the length prefix is incomplete
@@ -206,7 +322,7 @@ function matchesWebSocketCanonical(buffer) {
206
322
  * the 7-bit form below 126, the `126` + 16-bit form below 65 536, or the `127` +
207
323
  * 64-bit form beyond; when `masked` is set the mask bit is set, a 4-byte key (supplied
208
324
  * through `options.mask`, else random) is written, and the payload is XOR-masked. Server→
209
- * client frames are unmasked (the default); pass `masked: true` to encode a CLIENT
325
+ * client frames are unmasked (the default); pass `masked: true` to encode a client
210
326
  * frame (for example to feed the parser in a test). A `string` payload is encoded as
211
327
  * UTF-8. Returns one contiguous `Buffer` (header + payload), so the wrapper writes it
212
328
  * with a single `socket.write`. Pure.
@@ -285,7 +401,8 @@ function isWebSocketProtocol(protocol) {
285
401
  return /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/.test(protocol);
286
402
  }
287
403
  /**
288
- * Checks whether a numeric value is a valid RFC 6455 close status code to RECEIVE (§7.4.1).
404
+ * Checks whether a numeric value is a close status code an RFC 6455 endpoint may
405
+ * receive (§7.4.1).
289
406
  *
290
407
  * @remarks
291
408
  * True for `1000`–`1003`, `1007`–`1014`, and the application range `3000`–`4999`; false
@@ -315,18 +432,19 @@ function isCloseCode(code) {
315
432
  //#endregion
316
433
  //#region src/server/parsers.ts
317
434
  /**
318
- * Decodes a single RFC 6455 frame from the front of a buffer.
435
+ * Decodes a single RFC 6455 frame from the front of a buffer, answering `undefined`
436
+ * while the buffer is incomplete so the caller accumulates and retries.
319
437
  *
320
438
  * @remarks
321
439
  * Reads the FIN bit and opcode (byte 0), the mask bit and 7-bit payload length (byte
322
440
  * 1) — extended to a 16-bit length when the 7-bit field is `126`, or a 64-bit length
323
441
  * when it is `127` — the optional 4-byte mask key, then the payload, XOR-unmasking it
324
- * against the key when the mask bit is set (client→server frames MUST be masked, RFC
442
+ * against the key when the mask bit is set (client→server frames must be masked, RFC
325
443
  * 6455 §5.3; an unmasked frame still decodes, leaving the payload as-is, so the caller
326
- * can enforce policy). Returns `undefined` the moment the buffer is too short for the
327
- * part it is up to (the length prefix, the mask, or the full payload) — the signal to
328
- * the caller to read more bytes and retry. `consumed` is the total bytes the frame
329
- * occupied, so the caller slices the remainder. Pure; never throws on a short buffer.
444
+ * can enforce policy). The incomplete answer comes the moment the buffer is too short
445
+ * for the part it is up to: the length prefix, the mask, or the full payload.
446
+ * `consumed` is the total bytes the frame occupied, so the caller slices the remainder.
447
+ * Pure; never throws on a short buffer.
330
448
  *
331
449
  * @param buffer - The accumulation buffer to decode the next frame from
332
450
  * @returns The parsed {@link WebSocketFrame}, or `undefined` when the buffer is incomplete
@@ -378,12 +496,13 @@ function parseWebSocketFrame(buffer) {
378
496
  };
379
497
  }
380
498
  /**
381
- * Decodes a byte sequence as strict UTF-8, or signals it is malformed.
499
+ * Decodes a byte sequence as strict UTF-8, answering `undefined` when the sequence is
500
+ * malformed.
382
501
  *
383
502
  * @remarks
384
- * Wraps `TextDecoder('utf-8', { fatal: true })` in a try/catch so a malformed sequence
385
- * returns `undefined` instead of throwing — a guard-adjacent coercer never throws on bad
386
- * input. Pure.
503
+ * Wraps `TextDecoder('utf-8', { fatal: true })` in a try/catch, so a malformed sequence
504
+ * returns rather than throwing — a guard-adjacent coercer never throws on bad input.
505
+ * Pure.
387
506
  *
388
507
  * @param bytes - The raw bytes to decode
389
508
  * @returns The decoded string, or `undefined` when `bytes` is not valid UTF-8
@@ -404,18 +523,19 @@ function parseUTF8(bytes) {
404
523
  //#endregion
405
524
  //#region src/server/NodeWebSocket.ts
406
525
  /**
407
- * Represents a server-native WebSocket over a raw upgraded `node:stream` Duplex — the lean
408
- * wrapper around the RFC 6455 wire protocol.
526
+ * Implements the wrapper contract over a raw upgraded `node:stream` Duplex socket,
527
+ * driving the RFC 6455 handshake, the frame codec, auto-pong, and the close handshake,
528
+ * and surfacing every event on an owned `emitter`.
409
529
  *
410
530
  * @remarks
411
- * Created by `createNodeWebSocket`. When given a client `key` it runs in SERVER mode —
531
+ * Created by `createNodeWebSocket`. When given a client `key` it runs in server mode —
412
532
  * it writes the `101 Switching Protocols` handshake (`computeWebSocketAccept(key)`) and
413
- * emits `open`; given no key it runs in CLIENT mode (no handshake, frames masked). It
533
+ * emits `open`; given no key it runs in client mode (no handshake, frames masked). It
414
534
  * then listens on the socket's `data`, accumulating bytes in `#buffer` and decoding
415
535
  * every complete frame with {@link parseWebSocketFrame} (slicing `consumed` and
416
- * re-parsing the remainder): a TEXT frame — reassembling continuation fragments across
417
- * `fin: false` frames — decodes to UTF-8 and emits `message`; a PING is auto-answered
418
- * with a PONG and emits `ping`; a PONG emits `pong`; a CLOSE is echoed and ends the
536
+ * re-parsing the remainder): a text frame — reassembling continuation fragments across
537
+ * `fin: false` frames — decodes to UTF-8 and emits `message`; a ping is auto-answered
538
+ * with a pong and emits `ping`; a pong emits `pong`; a close frame is echoed and ends the
419
539
  * socket, emitting `close`. `send` writes a text frame, `ping` a ping, `close` a close
420
540
  * frame; `destroy` tears down immediately. It owns a typed `#emitter` by composition, and
421
541
  * the emitter isolates a throwing listener and routes the error to its own `error` handler
@@ -462,8 +582,8 @@ var NodeWebSocket = class {
462
582
  * Creates a WebSocket wrapper over an already-upgraded Duplex socket.
463
583
  *
464
584
  * @remarks
465
- * `key` selects the mode: present runs SERVER mode and writes the `101 Switching
466
- * Protocols` handshake, omitted runs CLIENT mode and masks every outgoing frame.
585
+ * `key` selects the mode: present runs server mode and writes the `101 Switching
586
+ * Protocols` handshake, omitted runs client mode and masks every outgoing frame.
467
587
  * {@link NodeWebSocketOptions} describes every member.
468
588
  *
469
589
  * @param options - The {@link NodeWebSocketOptions} the wrapper is built from
@@ -749,27 +869,25 @@ var NodeWebSocket = class {
749
869
  //#endregion
750
870
  //#region src/server/factories.ts
751
871
  /**
752
- * Creates a server-native WebSocket over a raw upgraded `node:stream` Duplex socket.
872
+ * Creates a server-native WebSocket over a raw upgraded `node:stream` Duplex socket
873
+ * server mode when a `key` is given, client mode otherwise.
753
874
  *
754
875
  * @remarks
755
- * The construction entry point for the {@link NodeWebSocketInterface}. Pass
756
- * the upgraded `socket` plus the client's `Sec-WebSocket-Key` as `key` to run in SERVER
757
- * mode the wrapper writes the `101 Switching Protocols` handshake and sends unmasked
758
- * frames; omit `key` for CLIENT mode (no handshake, masked frames). This is the
759
- * lean-native handle; it speaks only the WebSocket wire protocol — an MCP transport (the
760
- * later chunk) is built ON it. It is the WebSocket counterpart to
761
- * `createSQLiteDatabase` / `createIndexedDBDatabase`.
876
+ * The construction entry point for the {@link NodeWebSocketInterface}. In server mode the
877
+ * wrapper writes the `101 Switching Protocols` handshake and sends unmasked frames; in
878
+ * client mode it writes no handshake and masks every outgoing frame. This is the
879
+ * lean-native handle: it speaks the WebSocket wire protocol and nothing above it, so a
880
+ * message transport is built on it rather than into it.
762
881
  *
763
882
  * @param options - The {@link NodeWebSocketOptions} (`socket`, optional `key` / `head` /
764
883
  * `protocol` / `on`)
765
884
  * @returns A typed {@link NodeWebSocketInterface}
766
885
  * @throws A `WebSocketError` coded `OPTION` when `payload`, `timeout`, `key`, or `protocol` is refused, thrown before the wrapper writes to or assumes ownership of the `socket`
767
886
  *
768
- * @example
887
+ * @example Accept an upgrade and echo messages (server mode)
769
888
  * ```ts
770
- * import { createNodeWebSocket } from '@src/server'
889
+ * import { createNodeWebSocket } from '@orkestrel/websocket'
771
890
  *
772
- * // In a node:http 'upgrade' handler — server mode, identified by the client key:
773
891
  * server.on('upgrade', (request, socket, head) => {
774
892
  * const key = request.headers['sec-websocket-key']
775
893
  * if (typeof key !== 'string') {
@@ -778,10 +896,12 @@ var NodeWebSocket = class {
778
896
  * }
779
897
  * const ws = createNodeWebSocket({
780
898
  * socket,
781
- * key, // present => server mode + 101 handshake
782
- * head,
783
- * on: { message: (text) => ws.send(`echo: ${text}`) },
899
+ * key,
900
+ * head, // any bytes already buffered after the upgrade headers
901
+ * on: { message: (text) => ws.send(`echo: ${text}`) }, // wired before the first frame arrives
784
902
  * })
903
+ * ws.emitter.on('message', (text) => log('echoed', text)) // a second observer of the same event
904
+ * ws.emitter.on('close', (code, reason) => log('closed', code, reason))
785
905
  * })
786
906
  * ```
787
907
  */