@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.
@@ -1,7 +1,7 @@
1
- import { Duplex } from 'node:stream';
2
- import { EmitterErrorHandler } from '@orkestrel/emitter';
3
- import { EmitterHooks } from '@orkestrel/emitter';
4
- import { EmitterInterface } from '@orkestrel/emitter';
1
+ import type { Duplex } from 'node:stream';
2
+ import type { EmitterErrorHandler } from '@orkestrel/emitter';
3
+ import type { EmitterHooks } from '@orkestrel/emitter';
4
+ import type { EmitterInterface } from '@orkestrel/emitter';
5
5
 
6
6
  /**
7
7
  * Computes the `Sec-WebSocket-Accept` response value for an RFC 6455 upgrade.
@@ -17,27 +17,25 @@ import { EmitterInterface } from '@orkestrel/emitter';
17
17
  export declare function computeWebSocketAccept(key: string): string;
18
18
 
19
19
  /**
20
- * Creates a server-native WebSocket over a raw upgraded `node:stream` Duplex socket.
20
+ * Creates a server-native WebSocket over a raw upgraded `node:stream` Duplex socket
21
+ * server mode when a `key` is given, client mode otherwise.
21
22
  *
22
23
  * @remarks
23
- * The construction entry point for the {@link NodeWebSocketInterface}. Pass
24
- * the upgraded `socket` plus the client's `Sec-WebSocket-Key` as `key` to run in SERVER
25
- * mode the wrapper writes the `101 Switching Protocols` handshake and sends unmasked
26
- * frames; omit `key` for CLIENT mode (no handshake, masked frames). This is the
27
- * lean-native handle; it speaks only the WebSocket wire protocol — an MCP transport (the
28
- * later chunk) is built ON it. It is the WebSocket counterpart to
29
- * `createSQLiteDatabase` / `createIndexedDBDatabase`.
24
+ * The construction entry point for the {@link NodeWebSocketInterface}. In server mode the
25
+ * wrapper writes the `101 Switching Protocols` handshake and sends unmasked frames; in
26
+ * client mode it writes no handshake and masks every outgoing frame. This is the
27
+ * lean-native handle: it speaks the WebSocket wire protocol and nothing above it, so a
28
+ * message transport is built on it rather than into it.
30
29
  *
31
30
  * @param options - The {@link NodeWebSocketOptions} (`socket`, optional `key` / `head` /
32
31
  * `protocol` / `on`)
33
32
  * @returns A typed {@link NodeWebSocketInterface}
34
33
  * @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`
35
34
  *
36
- * @example
35
+ * @example Accept an upgrade and echo messages (server mode)
37
36
  * ```ts
38
- * import { createNodeWebSocket } from '@src/server'
37
+ * import { createNodeWebSocket } from '@orkestrel/websocket'
39
38
  *
40
- * // In a node:http 'upgrade' handler — server mode, identified by the client key:
41
39
  * server.on('upgrade', (request, socket, head) => {
42
40
  * const key = request.headers['sec-websocket-key']
43
41
  * if (typeof key !== 'string') {
@@ -46,10 +44,12 @@ export declare function computeWebSocketAccept(key: string): string;
46
44
  * }
47
45
  * const ws = createNodeWebSocket({
48
46
  * socket,
49
- * key, // present => server mode + 101 handshake
50
- * head,
51
- * on: { message: (text) => ws.send(`echo: ${text}`) },
47
+ * key,
48
+ * head, // any bytes already buffered after the upgrade headers
49
+ * on: { message: (text) => ws.send(`echo: ${text}`) }, // wired before the first frame arrives
52
50
  * })
51
+ * ws.emitter.on('message', (text) => log('echoed', text)) // a second observer of the same event
52
+ * ws.emitter.on('close', (code, reason) => log('closed', code, reason))
53
53
  * })
54
54
  * ```
55
55
  */
@@ -64,7 +64,7 @@ export declare function createNodeWebSocket(options: NodeWebSocketOptions): Node
64
64
  * the 7-bit form below 126, the `126` + 16-bit form below 65 536, or the `127` +
65
65
  * 64-bit form beyond; when `masked` is set the mask bit is set, a 4-byte key (supplied
66
66
  * through `options.mask`, else random) is written, and the payload is XOR-masked. Server→
67
- * client frames are unmasked (the default); pass `masked: true` to encode a CLIENT
67
+ * client frames are unmasked (the default); pass `masked: true` to encode a client
68
68
  * frame (for example to feed the parser in a test). A `string` payload is encoded as
69
69
  * UTF-8. Returns one contiguous `Buffer` (header + payload), so the wrapper writes it
70
70
  * with a single `socket.write`. Pure.
@@ -78,7 +78,8 @@ export declare function createNodeWebSocket(options: NodeWebSocketOptions): Node
78
78
  export declare function encodeWebSocketFrame(opcode: number, payload: Buffer | string, options?: WebSocketEncodeOptions): Buffer;
79
79
 
80
80
  /**
81
- * Checks whether a numeric value is a valid RFC 6455 close status code to RECEIVE (§7.4.1).
81
+ * Checks whether a numeric value is a close status code an RFC 6455 endpoint may
82
+ * receive (§7.4.1).
82
83
  *
83
84
  * @remarks
84
85
  * True for `1000`–`1003`, `1007`–`1014`, and the application range `3000`–`4999`; false
@@ -101,7 +102,8 @@ export declare function encodeWebSocketFrame(opcode: number, payload: Buffer | s
101
102
  export declare function isCloseCode(code: number): boolean;
102
103
 
103
104
  /**
104
- * Checks whether a value is a {@link WebSocketError}.
105
+ * Checks whether a caught value is a {@link WebSocketError}, narrowing it so a `catch` can
106
+ * branch on `error.code`.
105
107
  *
106
108
  * @param value - The value to test (typically a `catch` binding)
107
109
  * @returns True if `value` is a `WebSocketError`; false otherwise
@@ -157,14 +159,14 @@ export declare function isWebSocketKey(key: string): boolean;
157
159
  export declare function isWebSocketProtocol(protocol: string): boolean;
158
160
 
159
161
  /**
160
- * Checks whether the next frame uses the shortest valid RFC 6455 payload-length encoding.
162
+ * Checks whether the next frame uses the shortest valid RFC 6455 payload-length
163
+ * encoding, answering `undefined` until its length prefix is complete.
161
164
  *
162
165
  * @remarks
163
- * Returns `undefined` until the complete length prefix is buffered. The 16-bit form
164
- * is canonical only for lengths at least 126; the 64-bit form only for lengths at
165
- * least 65,536 and with its most-significant bit clear (RFC 6455 §5.2). Reads the same
166
- * length prefix as {@link measureWebSocketFrame}, under the same incomplete-buffer
167
- * contract. Pure; never throws.
166
+ * The 16-bit form is canonical only for lengths at least 126; the 64-bit form only for
167
+ * lengths at least 65,536 and with its most-significant bit clear (RFC 6455 §5.2). Reads
168
+ * the same length prefix as {@link measureWebSocketFrame}, under the same
169
+ * incomplete-buffer contract. Pure; never throws.
168
170
  *
169
171
  * @param buffer - The accumulation buffer containing the next frame header
170
172
  * @returns Its canonicality, or `undefined` while the length prefix is incomplete
@@ -177,15 +179,15 @@ export declare function isWebSocketProtocol(protocol: string): boolean;
177
179
  export declare function matchesWebSocketCanonical(buffer: Buffer): boolean | undefined;
178
180
 
179
181
  /**
180
- * Reads the declared payload length off the front of a buffer, without buffering or
181
- * reading the payload itself.
182
+ * Reads the declared payload length off the front of a buffer without buffering or
183
+ * reading the payload itself, answering `undefined` until the length field is complete.
182
184
  *
183
185
  * @remarks
184
186
  * Decodes only byte 1's 7-bit length field, extended by the 16-bit (`126`) or 64-bit
185
187
  * (`127`) form exactly like `parseWebSocketFrame` — but stops there, so a caller
186
188
  * can reject an over-cap frame the moment its length is known, before the payload
187
- * bytes have even arrived. Returns `undefined` until the length field itself is fully
188
- * buffered (mirrors the parser's incomplete-buffer contract). Pure; never throws.
189
+ * bytes have even arrived. The incomplete-buffer contract mirrors the parser's. Pure;
190
+ * never throws.
189
191
  *
190
192
  * @param buffer - The accumulation buffer to read the next frame's length from
191
193
  * @returns The declared payload length, or `undefined` when the buffer is too short to know it yet
@@ -199,18 +201,19 @@ export declare function matchesWebSocketCanonical(buffer: Buffer): boolean | und
199
201
  export declare function measureWebSocketFrame(buffer: Buffer): number | undefined;
200
202
 
201
203
  /**
202
- * Represents a server-native WebSocket over a raw upgraded `node:stream` Duplex — the lean
203
- * wrapper around the RFC 6455 wire protocol.
204
+ * Implements the wrapper contract over a raw upgraded `node:stream` Duplex socket,
205
+ * driving the RFC 6455 handshake, the frame codec, auto-pong, and the close handshake,
206
+ * and surfacing every event on an owned `emitter`.
204
207
  *
205
208
  * @remarks
206
- * Created by `createNodeWebSocket`. When given a client `key` it runs in SERVER mode —
209
+ * Created by `createNodeWebSocket`. When given a client `key` it runs in server mode —
207
210
  * it writes the `101 Switching Protocols` handshake (`computeWebSocketAccept(key)`) and
208
- * emits `open`; given no key it runs in CLIENT mode (no handshake, frames masked). It
211
+ * emits `open`; given no key it runs in client mode (no handshake, frames masked). It
209
212
  * then listens on the socket's `data`, accumulating bytes in `#buffer` and decoding
210
213
  * every complete frame with {@link parseWebSocketFrame} (slicing `consumed` and
211
- * re-parsing the remainder): a TEXT frame — reassembling continuation fragments across
212
- * `fin: false` frames — decodes to UTF-8 and emits `message`; a PING is auto-answered
213
- * with a PONG and emits `ping`; a PONG emits `pong`; a CLOSE is echoed and ends the
214
+ * re-parsing the remainder): a text frame — reassembling continuation fragments across
215
+ * `fin: false` frames — decodes to UTF-8 and emits `message`; a ping is auto-answered
216
+ * with a pong and emits `ping`; a pong emits `pong`; a close frame is echoed and ends the
214
217
  * socket, emitting `close`. `send` writes a text frame, `ping` a ping, `close` a close
215
218
  * frame; `destroy` tears down immediately. It owns a typed `#emitter` by composition, and
216
219
  * the emitter isolates a throwing listener and routes the error to its own `error` handler
@@ -238,8 +241,8 @@ export declare class NodeWebSocket implements NodeWebSocketInterface {
238
241
  * Creates a WebSocket wrapper over an already-upgraded Duplex socket.
239
242
  *
240
243
  * @remarks
241
- * `key` selects the mode: present runs SERVER mode and writes the `101 Switching
242
- * Protocols` handshake, omitted runs CLIENT mode and masks every outgoing frame.
244
+ * `key` selects the mode: present runs server mode and writes the `101 Switching
245
+ * Protocols` handshake, omitted runs client mode and masks every outgoing frame.
243
246
  * {@link NodeWebSocketOptions} describes every member.
244
247
  *
245
248
  * @param options - The {@link NodeWebSocketOptions} the wrapper is built from
@@ -255,13 +258,13 @@ export declare class NodeWebSocket implements NodeWebSocketInterface {
255
258
  }
256
259
 
257
260
  /**
258
- * Represents the event map of a {@link NodeWebSocketInterface}.
261
+ * Represents the event map a {@link NodeWebSocketInterface} emitter carries.
259
262
  *
260
263
  * @remarks
261
264
  * `open` — the handshake completed and the socket is ready. `message` — a text frame
262
265
  * arrived (its decoded UTF-8 string). `close` — the connection ended, carrying the
263
266
  * labeled `[code, reason]` tuple (each `undefined` when the peer sent none). `error` —
264
- * the underlying socket faulted (a DOMAIN event and then terminates the wrapper).
267
+ * the underlying socket faulted (a domain event, and then terminates the wrapper).
265
268
  * `ping` / `pong` — a control frame arrived (a ping is auto-answered with a pong).
266
269
  * Listener isolation is the emitter's: a listener throw is routed to the emitter's
267
270
  * `error` handler (the `error` option), never onto this map, so a buggy observer
@@ -277,7 +280,8 @@ export declare type NodeWebSocketEventMap = {
277
280
  };
278
281
 
279
282
  /**
280
- * Represents a server-native WebSocket over a raw upgraded socket — the behavioral contract.
283
+ * Represents the behavioral contract a server-native WebSocket exposes over a raw
284
+ * upgraded socket.
281
285
  *
282
286
  * @remarks
283
287
  * Created by `createNodeWebSocket`. In server mode it writes the RFC 6455 handshake
@@ -297,25 +301,68 @@ export declare type NodeWebSocketEventMap = {
297
301
  export declare interface NodeWebSocketInterface {
298
302
  readonly emitter: EmitterInterface<NodeWebSocketEventMap>;
299
303
  readonly readyState: WebSocketReadyState;
304
+ /**
305
+ * Writes a message as a UTF-8 text frame, masked in client mode and unmasked in server
306
+ * mode, and does nothing unless `readyState` is open.
307
+ *
308
+ * @remarks
309
+ * The peer's reply arrives back as a `message` event.
310
+ *
311
+ * @param message - The text to carry as the frame's payload
312
+ */
300
313
  send(message: string): void;
314
+ /**
315
+ * Writes a ping frame with an optional payload, which the peer answers with a pong, and
316
+ * does nothing unless `readyState` is open.
317
+ *
318
+ * @remarks
319
+ * The answering pong arrives as the `pong` event.
320
+ *
321
+ * @param payload - The optional UTF-8 payload to carry
322
+ * @throws A `WebSocketError` coded `LIMIT` when the UTF-8 payload exceeds
323
+ * `WEBSOCKET_CONTROL_MAX_LENGTH`
324
+ */
301
325
  ping(payload?: string): void;
326
+ /**
327
+ * Starts the closing handshake: moves to the closing ready state, writes a close frame
328
+ * carrying the two-byte big-endian `code` and an optional `reason`, and ends the
329
+ * writable side.
330
+ *
331
+ * @remarks
332
+ * The final `close` event fires after the peer echoes or the socket ends, and a second
333
+ * call is a no-op. Each refusal leaves `readyState` unchanged.
334
+ *
335
+ * @param code - The close status code, defaulting to `WEBSOCKET_CLOSE_NORMAL`
336
+ * @param reason - The optional UTF-8 reason to carry after the code
337
+ * @throws A `WebSocketError` coded `CLOSE` for an invalid or fractional `code`, and one
338
+ * coded `LIMIT` for a `reason` over `WEBSOCKET_CLOSE_REASON_MAX_LENGTH`
339
+ */
302
340
  close(code?: number, reason?: string): void;
341
+ /**
342
+ * Tears the socket down immediately: detaches the wrapper's domain socket listeners,
343
+ * destroys the socket, emits a final `close`, and tears the emitter down.
344
+ *
345
+ * @remarks
346
+ * Idempotent, and a hard stop rather than a handshake.
347
+ */
303
348
  destroy(): void;
304
349
  }
305
350
 
306
351
  /**
307
- * Represents the options for `createNodeWebSocket`.
352
+ * Represents the options for `createNodeWebSocket` — the upgraded `socket`, the `key`
353
+ * that selects server or client mode, and the listeners, caps, and cancellation signal
354
+ * the wrapper runs under.
308
355
  *
309
356
  * @remarks
310
357
  * `socket` is the upgraded `node:stream` Duplex (the raw TCP stream after the HTTP
311
- * upgrade). `key` is the client's `Sec-WebSocket-Key`: present it to run in SERVER
312
- * mode — the wrapper writes the `101 Switching Protocols` handshake and sends UNMASKED
313
- * frames; omit it for CLIENT mode — no handshake is written and frames are MASKED (RFC
358
+ * upgrade). `key` is the client's `Sec-WebSocket-Key`: present it to run in server
359
+ * mode — the wrapper writes the `101 Switching Protocols` handshake and sends unmasked
360
+ * frames; omit it for client mode — no handshake is written and frames are masked (RFC
314
361
  * 6455 §5.3). `head` is any bytes buffered after the upgrade headers (replayed through
315
362
  * the parser). `protocol` is a negotiated subprotocol to echo in the handshake. `on`
316
363
  * wires initial listeners at construction — the reserved `on` option; `error` is the
317
364
  * emitter's listener-error handler, where a listener throw routes. `payload` caps
318
- * both a single inbound frame's declared length AND the total bytes of a reassembled
365
+ * both a single inbound frame's declared length and the total bytes of a reassembled
319
366
  * fragmented message (default `WEBSOCKET_MAX_PAYLOAD`) — a breach closes 1009. `timeout`
320
367
  * is how long the wrapper waits, after sending a close frame, for the peer's echo before
321
368
  * it gives up and tears the socket down (default `WEBSOCKET_CLOSE_TIMEOUT_MS`). `signal`
@@ -339,12 +386,13 @@ export declare interface NodeWebSocketOptions {
339
386
  }
340
387
 
341
388
  /**
342
- * Decodes a byte sequence as strict UTF-8, or signals it is malformed.
389
+ * Decodes a byte sequence as strict UTF-8, answering `undefined` when the sequence is
390
+ * malformed.
343
391
  *
344
392
  * @remarks
345
- * Wraps `TextDecoder('utf-8', { fatal: true })` in a try/catch so a malformed sequence
346
- * returns `undefined` instead of throwing — a guard-adjacent coercer never throws on bad
347
- * input. Pure.
393
+ * Wraps `TextDecoder('utf-8', { fatal: true })` in a try/catch, so a malformed sequence
394
+ * returns rather than throwing — a guard-adjacent coercer never throws on bad input.
395
+ * Pure.
348
396
  *
349
397
  * @param bytes - The raw bytes to decode
350
398
  * @returns The decoded string, or `undefined` when `bytes` is not valid UTF-8
@@ -358,18 +406,19 @@ export declare interface NodeWebSocketOptions {
358
406
  export declare function parseUTF8(bytes: Buffer): string | undefined;
359
407
 
360
408
  /**
361
- * Decodes a single RFC 6455 frame from the front of a buffer.
409
+ * Decodes a single RFC 6455 frame from the front of a buffer, answering `undefined`
410
+ * while the buffer is incomplete so the caller accumulates and retries.
362
411
  *
363
412
  * @remarks
364
413
  * Reads the FIN bit and opcode (byte 0), the mask bit and 7-bit payload length (byte
365
414
  * 1) — extended to a 16-bit length when the 7-bit field is `126`, or a 64-bit length
366
415
  * when it is `127` — the optional 4-byte mask key, then the payload, XOR-unmasking it
367
- * against the key when the mask bit is set (client→server frames MUST be masked, RFC
416
+ * against the key when the mask bit is set (client→server frames must be masked, RFC
368
417
  * 6455 §5.3; an unmasked frame still decodes, leaving the payload as-is, so the caller
369
- * can enforce policy). Returns `undefined` the moment the buffer is too short for the
370
- * part it is up to (the length prefix, the mask, or the full payload) — the signal to
371
- * the caller to read more bytes and retry. `consumed` is the total bytes the frame
372
- * occupied, so the caller slices the remainder. Pure; never throws on a short buffer.
418
+ * can enforce policy). The incomplete answer comes the moment the buffer is too short
419
+ * for the part it is up to: the length prefix, the mask, or the full payload.
420
+ * `consumed` is the total bytes the frame occupied, so the caller slices the remainder.
421
+ * Pure; never throws on a short buffer.
373
422
  *
374
423
  * @param buffer - The accumulation buffer to decode the next frame from
375
424
  * @returns The parsed {@link WebSocketFrame}, or `undefined` when the buffer is incomplete
@@ -382,85 +431,200 @@ export declare function parseUTF8(bytes: Buffer): string | undefined;
382
431
  */
383
432
  export declare function parseWebSocketFrame(buffer: Buffer): WebSocketFrame | undefined;
384
433
 
385
- /** Names the invalid-frame-payload-data status code (RFC 6455 §7.4.1) — for example non-UTF-8 text or an unparseable close reason. */
434
+ /**
435
+ * Names the invalid-frame-payload-data status code, 1007.
436
+ *
437
+ * @remarks
438
+ * Sent for non-UTF-8 text or an unparseable close reason (RFC 6455 §7.4.1).
439
+ */
386
440
  export declare const WEBSOCKET_CLOSE_INVALID = 1007;
387
441
 
388
- /** Names the normal-closure status code (RFC 6455 §7.4.1) — the default `close` code. */
442
+ /**
443
+ * Names the normal-closure status code, 1000.
444
+ *
445
+ * @remarks
446
+ * The default `close` code (RFC 6455 §7.4.1).
447
+ */
389
448
  export declare const WEBSOCKET_CLOSE_NORMAL = 1000;
390
449
 
391
- /** Names the protocol-error status code (RFC 6455 §7.4.1) — a framing/state rule was violated. */
450
+ /**
451
+ * Names the protocol-error status code, 1002.
452
+ *
453
+ * @remarks
454
+ * Sent when a framing or state rule was violated (RFC 6455 §7.4.1).
455
+ */
392
456
  export declare const WEBSOCKET_CLOSE_PROTOCOL = 1002;
393
457
 
394
- /** Names the maximum UTF-8 close-reason length after the two-byte status code. */
458
+ /**
459
+ * Names the maximum UTF-8 close-reason length after the two-byte status code, 123.
460
+ *
461
+ * @remarks
462
+ * What is left of {@link WEBSOCKET_CONTROL_MAX_LENGTH} after the close frame's status
463
+ * code.
464
+ */
395
465
  export declare const WEBSOCKET_CLOSE_REASON_MAX_LENGTH: number;
396
466
 
397
- /** Names the default close-handshake timeout in milliseconds — how long `close()` waits for the peer's echo before tearing the socket down. */
467
+ /**
468
+ * Names the default close-handshake timeout, 30,000 milliseconds — how long `close` waits
469
+ * for the peer's echo.
470
+ *
471
+ * @remarks
472
+ * After it expires the wrapper tears the socket down, so a silent peer cannot leak the
473
+ * handle open.
474
+ */
398
475
  export declare const WEBSOCKET_CLOSE_TIMEOUT_MS = 30000;
399
476
 
400
- /** Names the message-too-big status code (RFC 6455 §7.4.1) — a reassembled message exceeded the payload cap. */
477
+ /**
478
+ * Names the message-too-big status code, 1009.
479
+ *
480
+ * @remarks
481
+ * Sent when a reassembled message exceeded the payload cap (RFC 6455 §7.4.1).
482
+ */
401
483
  export declare const WEBSOCKET_CLOSE_TOO_BIG = 1009;
402
484
 
403
- /** 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. */
485
+ /**
486
+ * Names the unsupported-data status code, 1003.
487
+ *
488
+ * @remarks
489
+ * Sent when the endpoint received a data type it cannot accept (RFC 6455 §7.4.1), for
490
+ * example binary on a text-only endpoint.
491
+ */
404
492
  export declare const WEBSOCKET_CLOSE_UNSUPPORTED = 1003;
405
493
 
406
- /** Names the maximum control-frame payload length in bytes (RFC 6455 §5.5). */
494
+ /**
495
+ * Names the maximum control-frame payload length, 125 bytes.
496
+ *
497
+ * @remarks
498
+ * The cap RFC 6455 §5.5 sets on every control frame's payload.
499
+ */
407
500
  export declare const WEBSOCKET_CONTROL_MAX_LENGTH = 125;
408
501
 
409
- /** 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). */
502
+ /**
503
+ * Names the flush grace, 1,000 milliseconds, a validation-breach close frame is given
504
+ * before the hard teardown fallback destroys the socket.
505
+ *
506
+ * @remarks
507
+ * Armed after `#fail` writes the close frame, so the frame drains through the socket's
508
+ * write buffer rather than being discarded. The normal path destroys sooner, on the
509
+ * `end()` flush callback.
510
+ */
410
511
  export declare const WEBSOCKET_FAIL_TIMEOUT_MS = 1000;
411
512
 
412
513
  /**
413
- * Names the RFC 6455 GUID concatenated to a client's `Sec-WebSocket-Key` before the SHA-1
414
- * hash that yields the `Sec-WebSocket-Accept` response value.
514
+ * Names the accept GUID concatenated to a client's `Sec-WebSocket-Key` before the accept
515
+ * hash, '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'.
415
516
  *
416
517
  * @remarks
417
- * A fixed, spec-mandated constant (RFC 6455 §4.2.2) read only by
518
+ * The base64-encoded SHA-1 of that concatenation is the `Sec-WebSocket-Accept` response
519
+ * value. A fixed, spec-mandated constant (RFC 6455 §4.2.2) — read only by
418
520
  * {@link computeWebSocketAccept}.
419
521
  */
420
522
  export declare const WEBSOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
421
523
 
422
- /** Names the default maximum inbound single-frame length AND reassembled-message total byte count (100 MiB — the `ws` package default). */
524
+ /**
525
+ * Names the default cap on both an inbound frame's declared length and a reassembled
526
+ * message's total byte count, 104,857,600 bytes (100 MiB).
527
+ *
528
+ * @remarks
529
+ * The same value the `ws` package defaults to. Either breach closes
530
+ * {@link WEBSOCKET_CLOSE_TOO_BIG}.
531
+ */
423
532
  export declare const WEBSOCKET_MAX_PAYLOAD = 104857600;
424
533
 
425
- /** Names the binary frame opcode — a raw byte payload (RFC 6455 §5.6). */
534
+ /**
535
+ * Names the binary frame opcode, 0x02.
536
+ *
537
+ * @remarks
538
+ * A raw byte payload (RFC 6455 §5.6).
539
+ */
426
540
  export declare const WEBSOCKET_OPCODE_BINARY = 2;
427
541
 
428
- /** Names the close frame opcode — a control frame ending the connection (RFC 6455 §5.5.1). */
542
+ /**
543
+ * Names the close frame opcode, 0x08.
544
+ *
545
+ * @remarks
546
+ * A control frame ending the connection (RFC 6455 §5.5.1).
547
+ */
429
548
  export declare const WEBSOCKET_OPCODE_CLOSE = 8;
430
549
 
431
- /** Names the continuation frame opcode — the next fragment of an open data message (RFC 6455 §5.4). */
550
+ /**
551
+ * Names the continuation frame opcode, 0x00.
552
+ *
553
+ * @remarks
554
+ * The next fragment of an open data message (RFC 6455 §5.4).
555
+ */
432
556
  export declare const WEBSOCKET_OPCODE_CONTINUATION = 0;
433
557
 
434
- /** Names the ping frame opcode — a control frame the peer must answer with a pong (RFC 6455 §5.5.2). */
558
+ /**
559
+ * Names the ping frame opcode, 0x09.
560
+ *
561
+ * @remarks
562
+ * A control frame the peer must answer with a pong (RFC 6455 §5.5.2).
563
+ */
435
564
  export declare const WEBSOCKET_OPCODE_PING = 9;
436
565
 
437
- /** Names the pong frame opcode — a control frame answering a ping (RFC 6455 §5.5.3). */
566
+ /**
567
+ * Names the pong frame opcode, 0x0a.
568
+ *
569
+ * @remarks
570
+ * A control frame answering a ping (RFC 6455 §5.5.3).
571
+ */
438
572
  export declare const WEBSOCKET_OPCODE_PONG = 10;
439
573
 
440
- /** Names the text frame opcode — a UTF-8 payload (RFC 6455 §5.6). */
574
+ /**
575
+ * Names the text frame opcode, 0x01.
576
+ *
577
+ * @remarks
578
+ * A UTF-8 payload (RFC 6455 §5.6).
579
+ */
441
580
  export declare const WEBSOCKET_OPCODE_TEXT = 1;
442
581
 
443
- /** Names the ready state for a closed WebSocket (the socket ended). */
582
+ /**
583
+ * Names the closed ready state, 3.
584
+ *
585
+ * @remarks
586
+ * The state a WebSocket holds after the socket ends.
587
+ */
444
588
  export declare const WEBSOCKET_READY_CLOSED: WebSocketReadyState;
445
589
 
446
- /** Names the ready state for a closing WebSocket (a close frame was sent or received). */
590
+ /**
591
+ * Names the closing ready state, 2.
592
+ *
593
+ * @remarks
594
+ * The state a WebSocket holds after a close frame is sent or received.
595
+ */
447
596
  export declare const WEBSOCKET_READY_CLOSING: WebSocketReadyState;
448
597
 
449
- /** Names the ready state for a connecting WebSocket (before the handshake completes). */
598
+ /**
599
+ * Names the connecting ready state, 0.
600
+ *
601
+ * @remarks
602
+ * The state a WebSocket holds before its handshake completes.
603
+ */
450
604
  export declare const WEBSOCKET_READY_CONNECTING: WebSocketReadyState;
451
605
 
452
- /** Names the ready state for an open WebSocket (the handshake completed; frames flow). */
606
+ /**
607
+ * Names the open ready state, 1.
608
+ *
609
+ * @remarks
610
+ * The state a WebSocket holds after the handshake completes and while frames flow.
611
+ */
453
612
  export declare const WEBSOCKET_READY_OPEN: WebSocketReadyState;
454
613
 
455
- /** Names the WebSocket protocol version this wrapper speaks (`Sec-WebSocket-Version: 13`). */
614
+ /**
615
+ * Names the supported protocol version, '13'.
616
+ *
617
+ * @remarks
618
+ * The value this wrapper speaks, carried by the `Sec-WebSocket-Version` handshake header.
619
+ */
456
620
  export declare const WEBSOCKET_VERSION = "13";
457
621
 
458
622
  /**
459
623
  * Represents the options for {@link encodeWebSocketFrame} — how a frame is masked on the wire.
460
624
  *
461
625
  * @remarks
462
- * `masked` toggles the mask bit (server→client frames are NOT masked, the default;
463
- * client→server frames MUST be, RFC 6455 §5.3). `mask` supplies an explicit 4-byte
626
+ * `masked` toggles the mask bit (server→client frames are not masked, the default;
627
+ * client→server frames must be, RFC 6455 §5.3). `mask` supplies an explicit 4-byte
464
628
  * mask key (deterministic, for tests); when `masked` is true and `mask` is omitted a
465
629
  * random key is generated.
466
630
  */
@@ -470,15 +634,15 @@ export declare interface WebSocketEncodeOptions {
470
634
  }
471
635
 
472
636
  /**
473
- * Represents an error thrown by the WebSocket wrapper for a refused caller-supplied value.
637
+ * Represents an error the WebSocket wrapper throws for a refused caller-supplied value,
638
+ * carrying a machine-readable `code` and an optional `context`.
474
639
  *
475
640
  * @remarks
476
- * Carries a {@link WebSocketErrorCode} and an optional `context` record holding the
477
- * refused value under a key naming it: an `'OPTION'` carries the offending option
478
- * (`payload`, `timeout`, `key`, or `protocol`), a `'LIMIT'` carries `size` and the
479
- * `limit` it exceeded, a `'CLOSE'` carries the refused close `code`, and a `'FRAME'`
480
- * carries `opcode` or the mask's `size`. Narrow a caught value with
481
- * {@link isWebSocketError}.
641
+ * The `code` is a {@link WebSocketErrorCode}; the `context` record holds the refused
642
+ * value under a key naming it: an `'OPTION'` carries the offending option (`payload`,
643
+ * `timeout`, `key`, or `protocol`), a `'LIMIT'` carries `size` and the `limit` it
644
+ * exceeded, a `'CLOSE'` carries the refused close `code`, and a `'FRAME'` carries
645
+ * `opcode` or the mask's `size`. Narrow a caught value with {@link isWebSocketError}.
482
646
  *
483
647
  * @example
484
648
  * ```ts
@@ -505,7 +669,7 @@ export declare class WebSocketError extends Error {
505
669
  }
506
670
 
507
671
  /**
508
- * Represents the subject an {@link import('./errors.js').WebSocketError} names as refused.
672
+ * Represents the subject a `WebSocketError` names as refused.
509
673
  *
510
674
  * @remarks
511
675
  * `OPTION` — a {@link NodeWebSocketOptions} member was refused at construction
@@ -528,7 +692,7 @@ export declare type WebSocketErrorCode = 'OPTION' | 'LIMIT' | 'CLOSE' | 'FRAME';
528
692
  * the already-unmasked application data; `consumed` is the total byte count the frame
529
693
  * occupied (header + mask + payload), so the caller slices it off the front of its
530
694
  * accumulation buffer and re-parses the remainder. `masked` is the mask bit off byte 1
531
- * (client→server frames MUST be masked, RFC 6455 §5.1); `rsv` is the three reserved
695
+ * (client→server frames must be masked, RFC 6455 §5.1); `rsv` is the three reserved
532
696
  * bits off byte 0 packed into a single 0–7 value (RFC 6455 §5.2) — non-zero means an
533
697
  * extension the wrapper does not negotiate, so the caller rejects it. Produced by
534
698
  * {@link parseWebSocketFrame}.
@@ -543,12 +707,12 @@ export declare interface WebSocketFrame {
543
707
  }
544
708
 
545
709
  /**
546
- * Represents a WebSocket ready state — the four browser-compatible lifecycle values.
710
+ * Represents a WebSocket ready state — the stage a connection has reached between the
711
+ * handshake and the socket's end.
547
712
  *
548
713
  * @remarks
549
- * `0` connecting, `1` open, `2` closing, `3` closed the same numbering the DOM
550
- * `WebSocket.readyState` uses, so the wrapper reads like the platform API. The named
551
- * `WEBSOCKET_READY_*` constants spell each value.
714
+ * The same numbering the DOM `WebSocket.readyState` uses, so the wrapper reads like the
715
+ * platform API. The named `WEBSOCKET_READY_*` constants spell each value.
552
716
  */
553
717
  export declare type WebSocketReadyState = 0 | 1 | 2 | 3;
554
718