@ac-kit/format-websocket 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,27 @@
1
+ # @ac-kit/format-websocket
2
+
3
+ ## 0.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [47e39a2]
8
+ - Updated dependencies [8ee20f3]
9
+ - @ac-kit/core@0.6.0
10
+ - @ac-kit/format-core@0.2.0
11
+
12
+ ## 0.1.0
13
+
14
+ ### Minor Changes
15
+
16
+ - 1af504e: New packages: WebSocket (RFC 6455).
17
+
18
+ - `format-websocket`: `WebSocketFrameCodec` with streamed data payloads,
19
+ masking, close codes and the close payload.
20
+ - `net-websocket`: `WebSocketPeer` over any `Transport`, and the opening
21
+ handshake on both sides — `acceptWebSocket` inside an `@ac-kit/net-http`
22
+ handler, `connectWebSocket` through an `HttpAgent`.
23
+
24
+ ### Patch Changes
25
+
26
+ - Updated dependencies [1af504e]
27
+ - @ac-kit/format-core@0.1.6
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024-2026 Anthony Champagne and contributors
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -1,3 +1,45 @@
1
- # Temporary Holding Version
1
+ # @ac-kit/format-websocket
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The WebSocket wire format of RFC 6455: frames, masking, close codes and the
4
+ close payload, as an `@ac-kit/format-core` codec.
5
+
6
+ ```ts
7
+ import { WebSocketFrameCodec } from "@ac-kit/format-websocket";
8
+
9
+ const codec = new WebSocketFrameCodec({ role: "server", maxPayloadLength: 1 << 20 });
10
+ const [header, payload] = codec.encode({
11
+ opcode: "text",
12
+ payload: new TextEncoder().encode("Hello"),
13
+ });
14
+ // header: 0x81 0x05 — a final text frame of five bytes, unmasked
15
+ ```
16
+
17
+ ## What it exposes
18
+
19
+ - `WebSocketFrameCodec` — one end of a connection. Decodes frame headers, with
20
+ a data frame's payload streamed as the value's body rather than buffered, and
21
+ a control frame's payload inline and unmasked. Encodes a frame as a header and
22
+ a payload for one vectored write, masking it on the client side.
23
+ - `maskWebSocketPayload` and `createWebSocketMaskStream` — masking and
24
+ unmasking, whole or as a payload streams in.
25
+ - `encodeWebSocketClosePayload`, `decodeWebSocketClosePayload` and
26
+ `isWebSocketCloseCodeSendable` — the close frame's code and reason.
27
+ - `WebSocketFrameError` — a violation, carrying the close code the endpoint
28
+ must fail the connection with.
29
+
30
+ The decoder checks every framing rule of §5.2 — reserved opcodes, RSV bits no
31
+ extension allows, masking against the role, fragmented or oversized control
32
+ frames, a length not in its shortest form — and `maxPayloadLength` before any
33
+ payload byte is read. Rules spanning several frames, such as continuation order
34
+ and UTF-8 text, are the endpoint's: see `@ac-kit/net-websocket`.
35
+
36
+ Portable: no Node.js built-ins.
37
+
38
+ See the
39
+ [generated API docs](https://anthochamp.pages.grenadine.io/node-essentials/api/format-websocket/)
40
+ for the full reference.
41
+
42
+ ## Further reading
43
+
44
+ - [RFC 6455](https://www.rfc-editor.org/rfc/rfc6455) — the WebSocket protocol
45
+ (authoritative).
@@ -0,0 +1,38 @@
1
+ import { WebSocketFrameError } from "../errors.js";
2
+ /** The payload of a close frame. */
3
+ export type WebSocketClose = {
4
+ /**
5
+ * The status code, or `null` when the frame carried none — reported to the
6
+ * application as `1005` (no status received), which is never sent.
7
+ */
8
+ readonly code: number | null;
9
+ /** UTF-8 text with no meaning to the protocol; at most 123 bytes. */
10
+ readonly reason: string;
11
+ };
12
+ /**
13
+ * Whether `code` may be sent in a close frame (RFC 6455 §7.4, IANA WebSocket
14
+ * Close Code Number Registry): the defined codes `1000`–`1003` and
15
+ * `1007`–`1014`, and the `3000`–`4999` ranges for libraries and applications.
16
+ *
17
+ * `1004` is reserved, and `1005`, `1006` and `1015` exist only to report what
18
+ * happened locally — they must never appear on the wire.
19
+ */
20
+ export declare function isWebSocketCloseCodeSendable(code: number): boolean;
21
+ /**
22
+ * Encodes a close payload: the status code, then the reason as UTF-8.
23
+ *
24
+ * @param close `code` `null` sends an empty payload, and then `reason` must be
25
+ * empty too.
26
+ * @throws {RangeError} When the code may not be sent, a reason comes without a
27
+ * code, or the reason exceeds 123 UTF-8 bytes.
28
+ */
29
+ export declare function encodeWebSocketClosePayload(close: WebSocketClose): Uint8Array<ArrayBuffer>;
30
+ /**
31
+ * Decodes a close payload received from the peer.
32
+ *
33
+ * @returns The close, or a {@link WebSocketFrameError} — `1002` for a one-byte
34
+ * payload or a code that may not be sent, `1007` for a reason that is not
35
+ * UTF-8.
36
+ */
37
+ export declare function decodeWebSocketClosePayload(payload: Uint8Array): WebSocketClose | WebSocketFrameError;
38
+ //# sourceMappingURL=websocket-close.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket-close.d.ts","sourceRoot":"","sources":["../../src/close/websocket-close.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAEnD,oCAAoC;AACpC,MAAM,MAAM,cAAc,GAAG;IAC5B;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAE7B,qEAAqE;IACrE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACxB,CAAC;AAIF;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAOlE;AAED;;;;;;;GAOG;AACH,wBAAgB,2BAA2B,CAC1C,KAAK,EAAE,cAAc,GACnB,UAAU,CAAC,WAAW,CAAC,CAoBzB;AAED;;;;;;GAMG;AACH,wBAAgB,2BAA2B,CAC1C,OAAO,EAAE,UAAU,GACjB,cAAc,GAAG,mBAAmB,CAgBtC"}
@@ -0,0 +1,71 @@
1
+ import { decodeText, encodeTextUtf8, isValidText } from "@ac-kit/core";
2
+ import { WebSocketFrameError } from "../errors.js";
3
+ const MAX_REASON_BYTES_ = 123;
4
+ /**
5
+ * Whether `code` may be sent in a close frame (RFC 6455 §7.4, IANA WebSocket
6
+ * Close Code Number Registry): the defined codes `1000`–`1003` and
7
+ * `1007`–`1014`, and the `3000`–`4999` ranges for libraries and applications.
8
+ *
9
+ * `1004` is reserved, and `1005`, `1006` and `1015` exist only to report what
10
+ * happened locally — they must never appear on the wire.
11
+ */
12
+ export function isWebSocketCloseCodeSendable(code) {
13
+ return (Number.isInteger(code) &&
14
+ ((code >= 1000 && code <= 1003) ||
15
+ (code >= 1007 && code <= 1014) ||
16
+ (code >= 3000 && code <= 4999)));
17
+ }
18
+ /**
19
+ * Encodes a close payload: the status code, then the reason as UTF-8.
20
+ *
21
+ * @param close `code` `null` sends an empty payload, and then `reason` must be
22
+ * empty too.
23
+ * @throws {RangeError} When the code may not be sent, a reason comes without a
24
+ * code, or the reason exceeds 123 UTF-8 bytes.
25
+ */
26
+ export function encodeWebSocketClosePayload(close) {
27
+ const { code, reason } = close;
28
+ if (code === null) {
29
+ if (reason !== "") {
30
+ throw new RangeError("A close reason needs a close code");
31
+ }
32
+ return new Uint8Array(0);
33
+ }
34
+ if (!isWebSocketCloseCodeSendable(code)) {
35
+ throw new RangeError(`Close code ${code} may not be sent`);
36
+ }
37
+ const text = encodeTextUtf8(reason);
38
+ if (text.length > MAX_REASON_BYTES_) {
39
+ throw new RangeError("Close reason longer than 123 bytes");
40
+ }
41
+ const payload = new Uint8Array(2 + text.length);
42
+ payload[0] = code >>> 8;
43
+ payload[1] = code & 0xff;
44
+ payload.set(text, 2);
45
+ return payload;
46
+ }
47
+ /**
48
+ * Decodes a close payload received from the peer.
49
+ *
50
+ * @returns The close, or a {@link WebSocketFrameError} — `1002` for a one-byte
51
+ * payload or a code that may not be sent, `1007` for a reason that is not
52
+ * UTF-8.
53
+ */
54
+ export function decodeWebSocketClosePayload(payload) {
55
+ if (payload.length === 0) {
56
+ return { code: null, reason: "" };
57
+ }
58
+ if (payload.length === 1) {
59
+ return new WebSocketFrameError("One-byte close payload", 1002);
60
+ }
61
+ const code = (payload[0] << 8) | payload[1];
62
+ if (!isWebSocketCloseCodeSendable(code)) {
63
+ return new WebSocketFrameError(`Close code ${code} may not be sent`, 1002);
64
+ }
65
+ const text = payload.subarray(2);
66
+ if (!isValidText(text, "utf-8")) {
67
+ return new WebSocketFrameError("Close reason is not UTF-8", 1007);
68
+ }
69
+ return { code, reason: decodeText(text, "utf-8") };
70
+ }
71
+ //# sourceMappingURL=websocket-close.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket-close.js","sourceRoot":"","sources":["../../src/close/websocket-close.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEvE,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAcnD,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAE9B;;;;;;;GAOG;AACH,MAAM,UAAU,4BAA4B,CAAC,IAAY;IACxD,OAAO,CACN,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC;QACtB,CAAC,CAAC,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI,CAAC;YAC9B,CAAC,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI,CAAC;YAC9B,CAAC,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI,CAAC,CAAC,CAChC,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,2BAA2B,CAC1C,KAAqB;IAErB,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC;IAC/B,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;QACnB,IAAI,MAAM,KAAK,EAAE,EAAE,CAAC;YACnB,MAAM,IAAI,UAAU,CAAC,mCAAmC,CAAC,CAAC;QAC3D,CAAC;QACD,OAAO,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;IAC1B,CAAC;IACD,IAAI,CAAC,4BAA4B,CAAC,IAAI,CAAC,EAAE,CAAC;QACzC,MAAM,IAAI,UAAU,CAAC,cAAc,IAAI,kBAAkB,CAAC,CAAC;IAC5D,CAAC;IACD,MAAM,IAAI,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC;IACpC,IAAI,IAAI,CAAC,MAAM,GAAG,iBAAiB,EAAE,CAAC;QACrC,MAAM,IAA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pD,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Why a frame cannot be accepted, as the close code the endpoint must then fail
3
+ * the connection with (RFC 6455 §7.4.1).
4
+ *
5
+ * - `1002` — protocol error: malformed or illegal framing.
6
+ * - `1007` — a close reason (or, at the message layer, a text message) that is
7
+ * not UTF-8.
8
+ * - `1009` — a payload larger than the limit.
9
+ */
10
+ export type WebSocketFrameErrorCode = 1002 | 1007 | 1009;
11
+ /**
12
+ * A frame that breaks RFC 6455. The connection cannot continue: the endpoint
13
+ * sends a close frame with {@link closeCode} and closes.
14
+ */
15
+ export declare class WebSocketFrameError extends Error {
16
+ readonly closeCode: WebSocketFrameErrorCode;
17
+ constructor(message: string, closeCode: WebSocketFrameErrorCode);
18
+ }
19
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,MAAM,uBAAuB,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;AAEzD;;;GAGG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;IAG5C,QAAQ,CAAC,SAAS,EAAE,uBAAuB;IAF5C,YACC,OAAO,EAAE,MAAM,EACN,SAAS,EAAE,uBAAuB,EAI3C;CACD"}
package/dist/errors.js ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * A frame that breaks RFC 6455. The connection cannot continue: the endpoint
3
+ * sends a close frame with {@link closeCode} and closes.
4
+ */
5
+ export class WebSocketFrameError extends Error {
6
+ closeCode;
7
+ constructor(message, closeCode) {
8
+ super(message);
9
+ this.closeCode = closeCode;
10
+ this.name = "WebSocketFrameError";
11
+ }
12
+ }
13
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAWA;;;GAGG;AACH,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAGnC,SAAS;IAFnB,YACC,OAAe,EACN,SAAkC;QAE3C,KAAK,CAAC,OAAO,CAAC,CAAC;yBAFN,SAAS;QAGlB,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACnC,CAAC;CACD"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * XOR `payload` with the 4-byte masking key (RFC 6455 §5.3), into a new array.
3
+ * Masking and unmasking are the same operation.
4
+ *
5
+ * Runs in `O(n)` time and allocates `O(n)` in `payload.length`; the input is
6
+ * never written to.
7
+ *
8
+ * @param offset Position of `payload[0]` within the whole frame payload, so a
9
+ * payload that arrives in pieces can be unmasked piece by piece.
10
+ * @returns The masked (or unmasked) bytes.
11
+ */
12
+ export declare function maskWebSocketPayload(payload: Uint8Array, maskKey: Uint8Array, offset?: number): Uint8Array<ArrayBuffer>;
13
+ /**
14
+ * A stream that unmasks (or masks) a frame payload as it arrives — the
15
+ * counterpart of a decoded frame's body when it carries a masking key.
16
+ *
17
+ * One stream per frame: the key position restarts with every frame.
18
+ */
19
+ export declare function createWebSocketMaskStream(maskKey: Uint8Array): TransformStream<Uint8Array, Uint8Array>;
20
+ //# sourceMappingURL=mask.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mask.d.ts","sourceRoot":"","sources":["../../src/frame/mask.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CACnC,OAAO,EAAE,UAAU,EACnB,OAAO,EAAE,UAAU,EACnB,MAAM,SAAI,GACR,UAAU,CAAC,WAAW,CAAC,CAMzB;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,CACxC,OAAO,EAAE,UAAU,GACjB,eAAe,CAAC,UAAU,EAAE,UAAU,CAAC,CAQzC"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * XOR `payload` with the 4-byte masking key (RFC 6455 §5.3), into a new array.
3
+ * Masking and unmasking are the same operation.
4
+ *
5
+ * Runs in `O(n)` time and allocates `O(n)` in `payload.length`; the input is
6
+ * never written to.
7
+ *
8
+ * @param offset Position of `payload[0]` within the whole frame payload, so a
9
+ * payload that arrives in pieces can be unmasked piece by piece.
10
+ * @returns The masked (or unmasked) bytes.
11
+ */
12
+ export function maskWebSocketPayload(payload, maskKey, offset = 0) {
13
+ const result = new Uint8Array(payload.length);
14
+ for (let index = 0; index < payload.length; index++) {
15
+ result[index] = payload[index] ^ maskKey[(offset + index) & 3];
16
+ }
17
+ return result;
18
+ }
19
+ /**
20
+ * A stream that unmasks (or masks) a frame payload as it arrives — the
21
+ * counterpart of a decoded frame's body when it carries a masking key.
22
+ *
23
+ * One stream per frame: the key position restarts with every frame.
24
+ */
25
+ export function createWebSocketMaskStream(maskKey) {
26
+ let offset = 0;
27
+ return new TransformStream({
28
+ transform(chunk, controller) {
29
+ controller.enqueue(maskWebSocketPayload(chunk, maskKey, offset));
30
+ offset += chunk.length;
31
+ },
32
+ });
33
+ }
34
+ //# sourceMappingURL=mask.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mask.js","sourceRoot":"","sources":["../../src/frame/mask.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,UAAU,oBAAoB,CACnC,OAAmB,EACnB,OAAmB,EACnB,MAAM,GAAG,CAAC;IAEV,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAC9C,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,OAAO,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACrD,MAAM,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,KAAK,CAAE,GAAG,OAAO,CAAC,CAAC,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,CAAE,CAAC;IAClE,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,yBAAyB,CACxC,OAAmB;IAEnB,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,OAAO,IAAI,eAAe,CAAC;QAC1B,SAAS,CAAC,KAAK,EAAE,UAAU;YAC1B,UAAU,CAAC,OAAO,CAAC,oBAAoB,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC;YACjE,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC;QACxB,CAAC;KACD,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,41 @@
1
+ import { type Codec, type DecodeResult } from "@ac-kit/format-core";
2
+ import { type WebSocketFrame, type WebSocketFrameCodecOptions, type WebSocketOutboundFrame } from "./websocket-frame.js";
3
+ /**
4
+ * RFC 6455 framing, for one end of a connection.
5
+ *
6
+ * Decoding yields one {@link WebSocketFrame} per frame. A data frame's payload
7
+ * is declared as the value's body, so it is streamed rather than buffered — at
8
+ * most `maxPayloadLength` bytes, still masked when the frame carries a key (see
9
+ * `createWebSocketMaskStream`). A control frame's payload, at most 125 bytes,
10
+ * comes inline and unmasked.
11
+ *
12
+ * Every rule of §5.2 is checked: reserved opcodes, RSV bits no extension
13
+ * allows, masking against the role, fragmented or oversized control frames, a
14
+ * 64-bit length with its top bit set, and a length not in its shortest form. A
15
+ * violation is reported once as an `error` carrying a
16
+ * {@link WebSocketFrameError}, after which everything is discarded: the endpoint
17
+ * must send a close frame with its `closeCode` and close.
18
+ *
19
+ * Encoding writes a header and the payload as two buffers for one vectored
20
+ * write. A server's payload is passed through untouched; a client's is masked
21
+ * with a fresh key from `crypto.getRandomValues` into a new buffer, so the
22
+ * caller's bytes are never modified.
23
+ *
24
+ * Message-level rules — continuation order, UTF-8 text — are the endpoint's,
25
+ * since they span frames.
26
+ */
27
+ export declare class WebSocketFrameCodec implements Codec<WebSocketFrame, WebSocketOutboundFrame> {
28
+ private readonly options;
29
+ private broken;
30
+ private readonly allowedRsv;
31
+ constructor(options: WebSocketFrameCodecOptions);
32
+ decode(view: Uint8Array): DecodeResult<WebSocketFrame>;
33
+ /**
34
+ * @throws {RangeError} When a control frame is fragmented or carries more
35
+ * than 125 bytes, or `rsv` is not a 3-bit number.
36
+ */
37
+ encode(frame: WebSocketOutboundFrame): readonly Uint8Array[];
38
+ reset(): void;
39
+ private decodeFrame;
40
+ }
41
+ //# sourceMappingURL=websocket-frame-codec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket-frame-codec.d.ts","sourceRoot":"","sources":["../../src/frame/websocket-frame-codec.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,KAAK,EACV,KAAK,YAAY,EAEjB,MAAM,qBAAqB,CAAC;AAI7B,OAAO,EAIN,KAAK,cAAc,EACnB,KAAK,0BAA0B,EAC/B,KAAK,sBAAsB,EAC3B,MAAM,sBAAsB,CAAC;AAO9B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,mBAAoB,YAAW,KAAK,CAChD,cAAc,EACd,sBAAsB,CACtB;IAIY,OAAO,CAAC,QAAQ,CAAC,OAAO;IAHpC,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IAEpC,YAA6B,OAAO,EAAE,0BAA0B,EAE/D;IAED,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,YAAY,CAAC,cAAc,CAAC,CAUrD;IAED;;;OAGG;IACH,MAAM,CAAC,KAAK,EAAE,sBAAsB,GAAG,SAAS,UAAU,EAAE,CA0C3D;IAED,KAAK,IAAI,IAAI,CAEZ;IAED,OAAO,CAAC,WAAW;CAgFnB"}
@@ -0,0 +1,189 @@
1
+ import { getUint32Be } from "@ac-kit/core";
2
+ import { decodeIncomplete, } from "@ac-kit/format-core";
3
+ import { WebSocketFrameError } from "../errors.js";
4
+ import { maskWebSocketPayload } from "./mask.js";
5
+ import { decodeWebSocketOpcode_, encodeWebSocketOpcode_, isWebSocketControlOpcode, } from "./websocket-frame.js";
6
+ const MAX_CONTROL_PAYLOAD_ = 125;
7
+ const TWO_POW_32_ = 4294967296;
8
+ // The high word of a 64-bit length above this exceeds Number.MAX_SAFE_INTEGER.
9
+ const MAX_SAFE_HIGH_WORD_ = 2097151;
10
+ /**
11
+ * RFC 6455 framing, for one end of a connection.
12
+ *
13
+ * Decoding yields one {@link WebSocketFrame} per frame. A data frame's payload
14
+ * is declared as the value's body, so it is streamed rather than buffered — at
15
+ * most `maxPayloadLength` bytes, still masked when the frame carries a key (see
16
+ * `createWebSocketMaskStream`). A control frame's payload, at most 125 bytes,
17
+ * comes inline and unmasked.
18
+ *
19
+ * Every rule of §5.2 is checked: reserved opcodes, RSV bits no extension
20
+ * allows, masking against the role, fragmented or oversized control frames, a
21
+ * 64-bit length with its top bit set, and a length not in its shortest form. A
22
+ * violation is reported once as an `error` carrying a
23
+ * {@link WebSocketFrameError}, after which everything is discarded: the endpoint
24
+ * must send a close frame with its `closeCode` and close.
25
+ *
26
+ * Encoding writes a header and the payload as two buffers for one vectored
27
+ * write. A server's payload is passed through untouched; a client's is masked
28
+ * with a fresh key from `crypto.getRandomValues` into a new buffer, so the
29
+ * caller's bytes are never modified.
30
+ *
31
+ * Message-level rules — continuation order, UTF-8 text — are the endpoint's,
32
+ * since they span frames.
33
+ */
34
+ export class WebSocketFrameCodec {
35
+ options;
36
+ broken = false;
37
+ allowedRsv;
38
+ constructor(options) {
39
+ this.options = options;
40
+ this.allowedRsv = options.allowedRsv ?? 0;
41
+ }
42
+ decode(view) {
43
+ if (this.broken) {
44
+ return { status: "skip", consumed: view.length };
45
+ }
46
+ const result = this.decodeFrame(view);
47
+ if (result instanceof WebSocketFrameError) {
48
+ this.broken = true;
49
+ return { status: "error", error: result, consumed: view.length };
50
+ }
51
+ return result;
52
+ }
53
+ /**
54
+ * @throws {RangeError} When a control frame is fragmented or carries more
55
+ * than 125 bytes, or `rsv` is not a 3-bit number.
56
+ */
57
+ encode(frame) {
58
+ const fin = frame.fin ?? true;
59
+ const rsv = frame.rsv ?? 0;
60
+ const { payload } = frame;
61
+ if (!Number.isInteger(rsv) || rsv < 0 || rsv > 7) {
62
+ throw new RangeError(`Invalid RSV bits: ${rsv}`);
63
+ }
64
+ if (isWebSocketControlOpcode(frame.opcode) &&
65
+ (!fin || payload.length > MAX_CONTROL_PAYLOAD_)) {
66
+ throw new RangeError("A control frame is unfragmented and at most 125 bytes");
67
+ }
68
+ const masked = this.options.role === "client";
69
+ const length = payload.length;
70
+ const lengthBytes = length < 126 ? 0 : length < 65536 ? 2 : 8;
71
+ const header = new Uint8Array(2 + lengthBytes + (masked ? 4 : 0));
72
+ header[0] =
73
+ (fin ? 0x80 : 0) | (rsv << 4) | encodeWebSocketOpcode_(frame.opcode);
74
+ header[1] =
75
+ (masked ? 0x80 : 0) |
76
+ (lengthBytes === 0 ? length : lengthBytes === 2 ? 126 : 127);
77
+ if (lengthBytes === 2) {
78
+ header[2] = length >>> 8;
79
+ header[3] = length & 0xff;
80
+ }
81
+ else if (lengthBytes === 8) {
82
+ const view = new DataView(header.buffer);
83
+ view.setUint32(2, Math.floor(length / TWO_POW_32_));
84
+ view.setUint32(6, length >>> 0);
85
+ }
86
+ if (!masked) {
87
+ return length === 0 ? [header] : [header, payload];
88
+ }
89
+ const maskKey = header.subarray(2 + lengthBytes);
90
+ crypto.getRandomValues(maskKey);
91
+ return length === 0
92
+ ? [header]
93
+ : [header, maskWebSocketPayload(payload, maskKey)];
94
+ }
95
+ reset() {
96
+ this.broken = false;
97
+ }
98
+ decodeFrame(view) {
99
+ if (view.length < 2) {
100
+ return decodeIncomplete(2);
101
+ }
102
+ const first = view[0];
103
+ const second = view[1];
104
+ const opcode = decodeWebSocketOpcode_(first & 0x0f);
105
+ if (opcode === undefined) {
106
+ return new WebSocketFrameError("Reserved opcode", 1002);
107
+ }
108
+ const rsv = (first >> 4) & 0x7;
109
+ if ((rsv & ~this.allowedRsv) !== 0) {
110
+ return new WebSocketFrameError("RSV bit set without an extension", 1002);
111
+ }
112
+ const masked = (second & 0x80) !== 0;
113
+ if (masked !== (this.options.role === "server")) {
114
+ return new WebSocketFrameError(masked ? "Masked frame from a server" : "Unmasked frame from a client", 1002);
115
+ }
116
+ const shortLength = second & 0x7f;
117
+ const lengthBytes = shortLength === 126 ? 2 : shortLength === 127 ? 8 : 0;
118
+ const headerLength = 2 + lengthBytes + (masked ? 4 : 0);
119
+ if (view.length < headerLength) {
120
+ return decodeIncomplete(headerLength);
121
+ }
122
+ const payloadLength = readPayloadLength_(view, shortLength);
123
+ if (payloadLength instanceof WebSocketFrameError) {
124
+ return payloadLength;
125
+ }
126
+ const fin = (first & 0x80) !== 0;
127
+ const control = isWebSocketControlOpcode(opcode);
128
+ if (control && (!fin || payloadLength > MAX_CONTROL_PAYLOAD_)) {
129
+ return new WebSocketFrameError("Fragmented or oversized control frame", 1002);
130
+ }
131
+ if (payloadLength > this.options.maxPayloadLength) {
132
+ return new WebSocketFrameError("Frame payload too large", 1009);
133
+ }
134
+ const maskKey = masked ? view.slice(headerLength - 4, headerLength) : null;
135
+ if (!control) {
136
+ const value = {
137
+ fin,
138
+ rsv,
139
+ opcode,
140
+ payloadLength,
141
+ maskKey,
142
+ payload: null,
143
+ };
144
+ return payloadLength === 0
145
+ ? { status: "decoded", value, consumed: headerLength }
146
+ : {
147
+ status: "decoded",
148
+ value,
149
+ consumed: headerLength,
150
+ body: { mode: "length", byteLength: payloadLength },
151
+ };
152
+ }
153
+ const frameLength = headerLength + payloadLength;
154
+ if (view.length < frameLength) {
155
+ return decodeIncomplete(frameLength);
156
+ }
157
+ const raw = view.subarray(headerLength, frameLength);
158
+ const payload = maskKey ? maskWebSocketPayload(raw, maskKey) : raw.slice();
159
+ return {
160
+ status: "decoded",
161
+ value: { fin, rsv, opcode, payloadLength, maskKey, payload },
162
+ consumed: frameLength,
163
+ };
164
+ }
165
+ }
166
+ /** RFC 6455 §5.2: the extended length must use the shortest form that fits. */
167
+ function readPayloadLength_(view, shortLength) {
168
+ if (shortLength < 126) {
169
+ return shortLength;
170
+ }
171
+ if (shortLength === 126) {
172
+ const length = (view[2] << 8) | view[3];
173
+ return length < 126
174
+ ? new WebSocketFrameError("Payload length not in shortest form", 1002)
175
+ : length;
176
+ }
177
+ const high = getUint32Be(view, 2);
178
+ if (high > 2147483647) {
179
+ return new WebSocketFrameError("Payload length has its top bit set", 1002);
180
+ }
181
+ if (high > MAX_SAFE_HIGH_WORD_) {
182
+ return new WebSocketFrameError("Frame payload too large", 1009);
183
+ }
184
+ const length = high * TWO_POW_32_ + getUint32Be(view, 6);
185
+ return length < 65536
186
+ ? new WebSocketFrameError("Payload length not in shortest form", 1002)
187
+ : length;
188
+ }
189
+ //# sourceMappingURL=websocket-frame-codec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket-frame-codec.js","sourceRoot":"","sources":["../../src/frame/websocket-frame-codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAGN,gBAAgB,GAChB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AACjD,OAAO,EACN,sBAAsB,EACtB,sBAAsB,EACtB,wBAAwB,GAIxB,MAAM,sBAAsB,CAAC;AAE9B,MAAM,oBAAoB,GAAG,GAAG,CAAC;AACjC,MAAM,WAAW,GAAG,UAAa,CAAC;AAClC,+EAA+E;AAC/E,MAAM,mBAAmB,GAAG,OAAS,CAAC;AAEtC;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,mBAAmB;IAOF,OAAO;IAH5B,MAAM,GAAG,KAAK,CAAC;IACN,UAAU,CAAS;IAEpC,YAA6B,OAAmC;uBAAnC,OAAO;QACnC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,CAAC,CAAC;IAC3C,CAAC;IAED,MAAM,CAAC,IAAgB;QACtB,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YACjB,OAAO,EAAE,MAAM,EAAE,MAAM,EAA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AC,IAAI,CAAC,CAAC,CAAE,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAE,CAAC;QAC1C,OAAO,MAAM,GAAG,GAAG;YAClB,CAAC,CAAC,IAAI,mBAAmB,CAAC,qCAAqC,EAAE,IAAI,CAAC;YACtE,CAAC,CAAC,MAAM,CAAC;IACX,CAAC;IACD,MAAM,IAAI,GAAG,WAAW,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;IAClC,IAAI,IAAI,GAAG,UAAW,EAAE,CAAC;QACxB,OAAO,IAAI,mBAAmB,CAAC,oCAAoC,EAAE,IAAI,CAAC,CAAC;IAC5E,CAAC;IACD,IAAI,IAAI,GAAG,mBAAmB,EAAE,CAAC;QAChC,OAAO,IAAI,mBAAmB,CAAC,yBAAyB,EAAE,IAAI,CAAC,CAAC;IACjE,CAAC;IACD,MAAM,MAAM,GAAG,IAAI,GAAG,WAAW,GAAG,WAAW,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;IACzD,OAAO,MAAM,GAAG,KAAQ;QACvB,CAAC,CAAC,IAAI,mBAAmB,CAAC,qCAAqC,EAAE,IAAI,CAAC;QACtE,CAAC,CAAC,MAAM,CAAC;AACX,CAAC"}
@@ -0,0 +1,65 @@
1
+ /**
2
+ * A frame opcode (RFC 6455 §5.2), by name.
3
+ *
4
+ * `text` and `binary` start a message, `continuation` carries its next
5
+ * fragment; `close`, `ping` and `pong` are control frames and are never
6
+ * fragmented.
7
+ */
8
+ export type WebSocketOpcode = "continuation" | "text" | "binary" | "close" | "ping" | "pong";
9
+ /** A decoded frame header, and the payload of a control frame. */
10
+ export type WebSocketFrame = {
11
+ /** Whether this is the last fragment of its message. */
12
+ readonly fin: boolean;
13
+ /**
14
+ * The RSV1, RSV2 and RSV3 bits as a 3-bit number, RSV1 the highest. Zero
15
+ * unless an extension negotiated them (`permessage-deflate` uses RSV1).
16
+ */
17
+ readonly rsv: number;
18
+ readonly opcode: WebSocketOpcode;
19
+ /** The payload length the header declares. */
20
+ readonly payloadLength: number;
21
+ /** The masking key, present exactly when the sender is a client. */
22
+ readonly maskKey: Uint8Array | null;
23
+ /**
24
+ * A control frame's payload, already unmasked. `null` for a data frame, whose
25
+ * payload follows as the decoded value's body — streamed, and still masked
26
+ * when {@link maskKey} is set.
27
+ */
28
+ readonly payload: Uint8Array | null;
29
+ };
30
+ /** A frame to encode. */
31
+ export type WebSocketOutboundFrame = {
32
+ /** Defaults to `true`: an unfragmented message, or a control frame. */
33
+ readonly fin?: boolean;
34
+ /** RSV bits as in {@link WebSocketFrame.rsv}. Defaults to `0`. */
35
+ readonly rsv?: number;
36
+ readonly opcode: WebSocketOpcode;
37
+ /** The unmasked payload; the encoder masks it when the role requires. */
38
+ readonly payload: Uint8Array;
39
+ };
40
+ /**
41
+ * Which end of the connection the codec serves.
42
+ *
43
+ * A client masks every frame it sends and must receive none masked; a server is
44
+ * the opposite (RFC 6455 §5.1).
45
+ */
46
+ export type WebSocketRole = "client" | "server";
47
+ /** Options for {@link WebSocketFrameCodec}. */
48
+ export type WebSocketFrameCodecOptions = {
49
+ readonly role: WebSocketRole;
50
+ /**
51
+ * Largest payload accepted in one frame, in bytes. Required: a header can
52
+ * declare up to 2^63 bytes, and every caller must say what it will take.
53
+ */
54
+ readonly maxPayloadLength: number;
55
+ /**
56
+ * RSV bits a negotiated extension gives meaning to, as a 3-bit mask. Any
57
+ * other set bit fails the connection. Defaults to `0`.
58
+ */
59
+ readonly allowedRsv?: number;
60
+ };
61
+ export declare function decodeWebSocketOpcode_(value: number): WebSocketOpcode | undefined;
62
+ export declare function encodeWebSocketOpcode_(opcode: WebSocketOpcode): number;
63
+ /** Whether an opcode names a control frame. */
64
+ export declare function isWebSocketControlOpcode(opcode: WebSocketOpcode): boolean;
65
+ //# sourceMappingURL=websocket-frame.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket-frame.d.ts","sourceRoot":"","sources":["../../src/frame/websocket-frame.ts"],"names":[],"mappings":"AAAA;;;;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E,eAAe,GAAG,MAAM,CAEtE;AAED,+CAA+C;AAC/C,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAEzE"}
@@ -0,0 +1,32 @@
1
+ const OPCODES_ = [
2
+ "continuation",
3
+ "text",
4
+ "binary",
5
+ undefined,
6
+ undefined,
7
+ undefined,
8
+ undefined,
9
+ undefined,
10
+ "close",
11
+ "ping",
12
+ "pong",
13
+ ];
14
+ const OPCODE_VALUES_ = {
15
+ continuation: 0x0,
16
+ text: 0x1,
17
+ binary: 0x2,
18
+ close: 0x8,
19
+ ping: 0x9,
20
+ pong: 0xa,
21
+ };
22
+ export function decodeWebSocketOpcode_(value) {
23
+ return OPCODES_[value];
24
+ }
25
+ export function encodeWebSocketOpcode_(opcode) {
26
+ return OPCODE_VALUES_[opcode];
27
+ }
28
+ /** Whether an opcode names a control frame. */
29
+ export function isWebSocketControlOpcode(opcode) {
30
+ return opcode === "close" || opcode === "ping" || opcode === "pong";
31
+ }
32
+ //# sourceMappingURL=websocket-frame.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket-frame.js","sourceRoot":"","sources":["../../src/frame/websocket-frame.ts"],"names":[],"mappings":"AAiFA,MAAM,QAAQ,GAA6C;IAC1D,cAAc;IACd,MAAM;IACN,QAAQ;IACR,SAAS;IACT,SAAS;IACT,SAAS;IACT,SAAS;IACT,SAAS;IACT,OAAO;IACP,MAAM;IACN,MAAM;CACN,CAAC;AAEF,MAAM,cAAc,GAA8C;IACjE,YAAY,EAAE,GAAG;IACjB,IAAI,EAAE,GAAG;IACT,MAAM,EAAE,GAAG;IACX,KAAK,EAAE,GAAG;IACV,IAAI,EAAE,GAAG;IACT,IAAI,EAAE,GAAG;CACT,CAAC;AAEF,MAAM,UAAU,sBAAsB,CACrC,KAAa;IAEb,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC;AACxB,CAAC;AAED,MAAM,UAAU,sBAAsB,CAAC,MAAuB;IAC7D,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC;AAC/B,CAAC;AAED,+CAA+C;AAC/C,MAAM,UAAU,wBAAwB,CAAC,MAAuB;IAC/D,OAAO,MAAM,KAAK,OAAO,IAAI,MAAM,KAAK,MAAM,IAAI,MAAM,KAAK,MAAM,CAAC;AACrE,CAAC"}
@@ -0,0 +1,6 @@
1
+ export * from "./close/websocket-close.js";
2
+ export * from "./errors.js";
3
+ export * from "./frame/mask.js";
4
+ export * from "./frame/websocket-frame-codec.js";
5
+ export * from "./frame/websocket-frame.js";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,kCAAkC,CAAC;AACjD,cAAc,4BAA4B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * from "./close/websocket-close.js";
2
+ export * from "./errors.js";
3
+ export * from "./frame/mask.js";
4
+ export * from "./frame/websocket-frame-codec.js";
5
+ export * from "./frame/websocket-frame.js";
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,kCAAkC,CAAC;AACjD,cAAc,4BAA4B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,63 @@
1
1
  {
2
2
  "name": "@ac-kit/format-websocket",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.1",
4
+ "description": "The WebSocket wire format (RFC 6455): frame codec with streamed payloads, masking, close codes and the close payload.",
5
+ "keywords": [
6
+ "rfc6455",
7
+ "websocket",
8
+ "websocket-frame"
9
+ ],
10
+ "homepage": "https://anthochamp.pages.grenadine.io/node-essentials/api/format-websocket/",
11
+ "bugs": {
12
+ "url": "https://git.grenadine.io/anthochamp/node-essentials/issues"
13
+ },
14
+ "license": "BSD-3-Clause",
15
+ "author": "Anthony Champagne <dev@anthonychampagne.fr>",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://git.grenadine.io/anthochamp/node-essentials.git",
19
+ "directory": "packages/format-websocket"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "type": "module",
25
+ "sideEffects": false,
26
+ "main": "./dist/index.js",
27
+ "exports": {
28
+ ".": "./dist/index.js"
29
+ },
30
+ "publishConfig": {
31
+ "main": "./dist/index.js",
32
+ "exports": {
33
+ ".": "./dist/index.js"
34
+ },
35
+ "access": "public"
36
+ },
37
+ "scripts": {
38
+ "compile": "tsc -p ./tsconfig.build.json -outDir dist",
39
+ "compile:check": "tsc -noEmit -p ./tsconfig.json",
40
+ "clean": "rimraf dist",
41
+ "build": "yarn clean && yarn compile",
42
+ "test": "yarn run -T vitest run",
43
+ "prepack": "yarn build"
44
+ },
45
+ "dependencies": {
46
+ "@ac-kit/core": "^0.6.0",
47
+ "@ac-kit/format-core": "^0.2.0",
48
+ "@types/web": "^0.0.362"
49
+ },
50
+ "devDependencies": {
51
+ "@ac-kit/math-random": "0.1.5",
52
+ "@ac-kit/tsconfig": "0.1.0",
53
+ "@ac-kit/typedoc-config": "0.1.0",
54
+ "@ac-kit/vitest-config": "0.1.1",
55
+ "@typescript/native": "typescript@7.0.2",
56
+ "rimraf": "6.1.4",
57
+ "typedoc": "0.28.20",
58
+ "typescript": "@typescript/typescript6@6.0.2"
59
+ },
60
+ "engines": {
61
+ "node": ">=24"
62
+ }
6
63
  }