@avatarsd-llc/libtracer-ws 0.3.0 → 0.4.0
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/dist/index.d.ts +19 -0
- package/dist/index.js +19 -16
- package/dist/transport.d.ts +45 -21
- package/dist/transport.js +19 -14
- package/dist/ws.d.ts +42 -15
- package/dist/ws.js +39 -30
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @brief @avatarsd-llc/libtracer-ws — WebSocket transport for libtracer (#54,
|
|
3
|
+
* ADR-0029).
|
|
4
|
+
*
|
|
5
|
+
* A transport is a vertex behind a swappable seam (ADR-0027); WebSocket is the
|
|
6
|
+
* first reliable transport (ADR-0029). This package carries libtracer TLV frames
|
|
7
|
+
* over RFC 6455 WebSocket, wire-compatible with the C++ `tr::net::transport_ws`.
|
|
8
|
+
*
|
|
9
|
+
* Two entry points, deliberately separate:
|
|
10
|
+
* - this barrel (`.`): the production `TransportWs` client, which frames over the
|
|
11
|
+
* runtime's own WebSocket — it does NOT pull in the hand-rolled codec; and
|
|
12
|
+
* - the `./ws` subpath: the pure, socket-free RFC 6455 frame codec, cross-validated
|
|
13
|
+
* byte-for-byte against the C++ codec. It is the cross-implementation-agreement
|
|
14
|
+
* oracle (ws_diff_fuzz, the ws-codec tests) and the fallback for runtimes without
|
|
15
|
+
* a native WebSocket — not the production framing path. It is imported explicitly
|
|
16
|
+
* from `@avatarsd-llc/libtracer-ws/ws` so it tree-shakes out of every consumer that
|
|
17
|
+
* only wants the transport (it is NOT re-exported here).
|
|
18
|
+
* The core (`@avatarsd-llc/libtracer`) is a peerDependency (ADR-0033).
|
|
19
|
+
*/
|
|
1
20
|
export declare const TRANSPORT: "ws";
|
|
2
21
|
export { TransportWs } from './transport.js';
|
|
3
22
|
export type { FrameReceiver, CloseHandler, WebSocketLike, WebSocketCtor, TransportWsOptions, } from './transport.js';
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
// SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @brief @avatarsd-llc/libtracer-ws — WebSocket transport for libtracer (#54,
|
|
5
|
+
* ADR-0029).
|
|
6
|
+
*
|
|
7
|
+
* A transport is a vertex behind a swappable seam (ADR-0027); WebSocket is the
|
|
8
|
+
* first reliable transport (ADR-0029). This package carries libtracer TLV frames
|
|
9
|
+
* over RFC 6455 WebSocket, wire-compatible with the C++ `tr::net::transport_ws`.
|
|
10
|
+
*
|
|
11
|
+
* Two entry points, deliberately separate:
|
|
12
|
+
* - this barrel (`.`): the production `TransportWs` client, which frames over the
|
|
13
|
+
* runtime's own WebSocket — it does NOT pull in the hand-rolled codec; and
|
|
14
|
+
* - the `./ws` subpath: the pure, socket-free RFC 6455 frame codec, cross-validated
|
|
15
|
+
* byte-for-byte against the C++ codec. It is the cross-implementation-agreement
|
|
16
|
+
* oracle (ws_diff_fuzz, the ws-codec tests) and the fallback for runtimes without
|
|
17
|
+
* a native WebSocket — not the production framing path. It is imported explicitly
|
|
18
|
+
* from `@avatarsd-llc/libtracer-ws/ws` so it tree-shakes out of every consumer that
|
|
19
|
+
* only wants the transport (it is NOT re-exported here).
|
|
20
|
+
* The core (`@avatarsd-llc/libtracer`) is a peerDependency (ADR-0033).
|
|
21
|
+
*/
|
|
19
22
|
export const TRANSPORT = 'ws';
|
|
20
23
|
export { TransportWs } from './transport.js';
|
package/dist/transport.d.ts
CHANGED
|
@@ -1,8 +1,26 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* @brief TransportWs — the dial-out WebSocket transport for libtracer (#54,
|
|
3
|
+
* ADR-0029).
|
|
4
|
+
*
|
|
5
|
+
* It mirrors the seam of the C++ `tr::net::transport_ws_client`
|
|
6
|
+
* (core/src/transport_ws.cpp): a `send(bytes)` that puts a complete libtracer
|
|
7
|
+
* TLV onto the wire as ONE binary WebSocket frame, plus a receiver registration
|
|
8
|
+
* for inbound frames. A libtracer TLV is carried as a single RFC 6455 BINARY
|
|
9
|
+
* frame (opcode 0x2): the C++ transport sends the whole TLV as one masked client
|
|
10
|
+
* frame and reassembles inbound frames the same way, so this transport stays
|
|
11
|
+
* wire-compatible with the C++ `transport_ws_server`.
|
|
12
|
+
*
|
|
13
|
+
* Framing is delegated to the runtime's WebSocket (native in the browser and in
|
|
14
|
+
* Node >= 22; the `ws` package in older Node). That implementation performs the
|
|
15
|
+
* RFC 6455 masking/framing a client must do — identical on the wire to the
|
|
16
|
+
* hand-rolled codec in ./ws.ts, which exists for cross-implementation agreement
|
|
17
|
+
* tests and for environments without a WebSocket.
|
|
18
|
+
*/
|
|
19
|
+
/** @brief A received libtracer frame: the unmasked payload of one inbound BINARY frame. */
|
|
2
20
|
export type FrameReceiver = (bytes: Uint8Array) => void;
|
|
3
|
-
/** Invoked once when an OPEN connection closes/errors, with the cause when known. */
|
|
21
|
+
/** @brief Invoked once when an OPEN connection closes/errors, with the cause when known. */
|
|
4
22
|
export type CloseHandler = (cause?: Error) => void;
|
|
5
|
-
/** The minimal WHATWG-WebSocket surface this transport relies on. */
|
|
23
|
+
/** @brief The minimal WHATWG-WebSocket surface this transport relies on. */
|
|
6
24
|
export interface WebSocketLike {
|
|
7
25
|
binaryType: string;
|
|
8
26
|
send(data: Uint8Array): void;
|
|
@@ -14,19 +32,20 @@ export interface WebSocketLike {
|
|
|
14
32
|
onclose: ((ev: unknown) => void) | null;
|
|
15
33
|
onerror: ((ev: unknown) => void) | null;
|
|
16
34
|
}
|
|
17
|
-
/** Constructs a {@link WebSocketLike} for a `ws://` / `wss://` URL. */
|
|
35
|
+
/** @brief Constructs a {@link WebSocketLike} for a `ws://` / `wss://` URL. */
|
|
18
36
|
export type WebSocketCtor = new (url: string) => WebSocketLike;
|
|
19
|
-
/** Options for {@link TransportWs}. */
|
|
37
|
+
/** @brief Options for {@link TransportWs}. */
|
|
20
38
|
export interface TransportWsOptions {
|
|
21
39
|
/**
|
|
22
|
-
* The WebSocket implementation to dial with.
|
|
23
|
-
*
|
|
24
|
-
*
|
|
40
|
+
* @brief The WebSocket implementation to dial with.
|
|
41
|
+
*
|
|
42
|
+
* Defaults to the runtime global `WebSocket` (browser, Node >= 22). Pass the
|
|
43
|
+
* `ws` package's `WebSocket` when running on a Node without a global one.
|
|
25
44
|
*/
|
|
26
45
|
WebSocket?: WebSocketCtor;
|
|
27
46
|
}
|
|
28
47
|
/**
|
|
29
|
-
* A WebSocket-backed libtracer transport client.
|
|
48
|
+
* @brief A WebSocket-backed libtracer transport client.
|
|
30
49
|
*
|
|
31
50
|
* Lifecycle: construct with a URL, `await connect()`, then `send()` TLV bytes and
|
|
32
51
|
* receive inbound TLV bytes via {@link onFrame}. Each `send()` emits exactly one
|
|
@@ -40,28 +59,32 @@ export declare class TransportWs {
|
|
|
40
59
|
private closeHandler;
|
|
41
60
|
private closeNotified;
|
|
42
61
|
/**
|
|
62
|
+
* @brief Resolve the WebSocket implementation and remember the endpoint.
|
|
43
63
|
* @param url The `ws://host:port` (or `wss://`) endpoint to dial.
|
|
44
64
|
* @param options Transport options; see {@link TransportWsOptions}.
|
|
45
65
|
*/
|
|
46
66
|
constructor(url: string, options?: TransportWsOptions);
|
|
47
67
|
/**
|
|
48
|
-
* Register (or clear) the inbound-frame receiver.
|
|
49
|
-
*
|
|
50
|
-
*
|
|
68
|
+
* @brief Register (or clear) the inbound-frame receiver.
|
|
69
|
+
*
|
|
70
|
+
* Mirrors the C++ `transport_ws_client::set_receiver`. The callback is
|
|
71
|
+
* invoked once per inbound BINARY frame with that frame's payload bytes.
|
|
51
72
|
*/
|
|
52
73
|
onFrame(receiver: FrameReceiver | null): void;
|
|
53
74
|
/**
|
|
54
|
-
* Register (or clear) the connection-closed notifier (the
|
|
55
|
-
* seam's optional close hook).
|
|
56
|
-
*
|
|
75
|
+
* @brief Register (or clear) the connection-closed notifier (the
|
|
76
|
+
* `ClientTransport` seam's optional close hook).
|
|
77
|
+
*
|
|
78
|
+
* Invoked at most once per connection, when an OPEN socket closes — remotely,
|
|
79
|
+
* on error, or via {@link close}.
|
|
57
80
|
*/
|
|
58
81
|
onClose(handler: CloseHandler | null): void;
|
|
59
|
-
/** Fire the close notifier exactly once for the current connection. */
|
|
82
|
+
/** @brief Fire the close notifier exactly once for the current connection. */
|
|
60
83
|
private notifyClose;
|
|
61
|
-
/** True once the underlying WebSocket has opened and not yet closed. */
|
|
84
|
+
/** @brief True once the underlying WebSocket has opened and not yet closed. */
|
|
62
85
|
get connected(): boolean;
|
|
63
86
|
/**
|
|
64
|
-
* Open the WebSocket and complete the RFC 6455 opening handshake.
|
|
87
|
+
* @brief Open the WebSocket and complete the RFC 6455 opening handshake.
|
|
65
88
|
*
|
|
66
89
|
* @param deadlineMs Reject if the socket has not opened within this many
|
|
67
90
|
* milliseconds (default 5000). Uses events, never a sleep.
|
|
@@ -69,11 +92,12 @@ export declare class TransportWs {
|
|
|
69
92
|
*/
|
|
70
93
|
connect(deadlineMs?: number): Promise<void>;
|
|
71
94
|
/**
|
|
72
|
-
* Send one complete libtracer TLV as a single BINARY WebSocket frame.
|
|
73
|
-
*
|
|
95
|
+
* @brief Send one complete libtracer TLV as a single BINARY WebSocket frame.
|
|
96
|
+
*
|
|
97
|
+
* The underlying WebSocket applies the RFC 6455 client masking. Throws if not
|
|
74
98
|
* connected.
|
|
75
99
|
*/
|
|
76
100
|
send(frame: Uint8Array): void;
|
|
77
|
-
/** Close the connection. Resolves once the socket has fully closed. */
|
|
101
|
+
/** @brief Close the connection. Resolves once the socket has fully closed. */
|
|
78
102
|
close(): Promise<void>;
|
|
79
103
|
}
|
package/dist/transport.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
// SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
|
|
3
|
-
/** Normalize an inbound WebSocket message payload to a `Uint8Array` of its bytes. */
|
|
3
|
+
/** @brief Normalize an inbound WebSocket message payload to a `Uint8Array` of its bytes. */
|
|
4
4
|
function toBytes(data) {
|
|
5
5
|
if (data instanceof Uint8Array) {
|
|
6
6
|
// Copy out of the (possibly pooled) backing buffer so callers own their bytes.
|
|
@@ -16,7 +16,7 @@ function toBytes(data) {
|
|
|
16
16
|
return null; // text / unsupported payloads are ignored (BINARY-only transport)
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
|
-
* A WebSocket-backed libtracer transport client.
|
|
19
|
+
* @brief A WebSocket-backed libtracer transport client.
|
|
20
20
|
*
|
|
21
21
|
* Lifecycle: construct with a URL, `await connect()`, then `send()` TLV bytes and
|
|
22
22
|
* receive inbound TLV bytes via {@link onFrame}. Each `send()` emits exactly one
|
|
@@ -24,6 +24,7 @@ function toBytes(data) {
|
|
|
24
24
|
*/
|
|
25
25
|
export class TransportWs {
|
|
26
26
|
/**
|
|
27
|
+
* @brief Resolve the WebSocket implementation and remember the endpoint.
|
|
27
28
|
* @param url The `ws://host:port` (or `wss://`) endpoint to dial.
|
|
28
29
|
* @param options Transport options; see {@link TransportWsOptions}.
|
|
29
30
|
*/
|
|
@@ -41,22 +42,25 @@ export class TransportWs {
|
|
|
41
42
|
this.ctor = ctor;
|
|
42
43
|
}
|
|
43
44
|
/**
|
|
44
|
-
* Register (or clear) the inbound-frame receiver.
|
|
45
|
-
*
|
|
46
|
-
*
|
|
45
|
+
* @brief Register (or clear) the inbound-frame receiver.
|
|
46
|
+
*
|
|
47
|
+
* Mirrors the C++ `transport_ws_client::set_receiver`. The callback is
|
|
48
|
+
* invoked once per inbound BINARY frame with that frame's payload bytes.
|
|
47
49
|
*/
|
|
48
50
|
onFrame(receiver) {
|
|
49
51
|
this.receiver = receiver;
|
|
50
52
|
}
|
|
51
53
|
/**
|
|
52
|
-
* Register (or clear) the connection-closed notifier (the
|
|
53
|
-
* seam's optional close hook).
|
|
54
|
-
*
|
|
54
|
+
* @brief Register (or clear) the connection-closed notifier (the
|
|
55
|
+
* `ClientTransport` seam's optional close hook).
|
|
56
|
+
*
|
|
57
|
+
* Invoked at most once per connection, when an OPEN socket closes — remotely,
|
|
58
|
+
* on error, or via {@link close}.
|
|
55
59
|
*/
|
|
56
60
|
onClose(handler) {
|
|
57
61
|
this.closeHandler = handler;
|
|
58
62
|
}
|
|
59
|
-
/** Fire the close notifier exactly once for the current connection. */
|
|
63
|
+
/** @brief Fire the close notifier exactly once for the current connection. */
|
|
60
64
|
notifyClose(cause) {
|
|
61
65
|
if (this.closeNotified)
|
|
62
66
|
return;
|
|
@@ -64,12 +68,12 @@ export class TransportWs {
|
|
|
64
68
|
if (this.closeHandler)
|
|
65
69
|
this.closeHandler(cause);
|
|
66
70
|
}
|
|
67
|
-
/** True once the underlying WebSocket has opened and not yet closed. */
|
|
71
|
+
/** @brief True once the underlying WebSocket has opened and not yet closed. */
|
|
68
72
|
get connected() {
|
|
69
73
|
return this.ws !== null;
|
|
70
74
|
}
|
|
71
75
|
/**
|
|
72
|
-
* Open the WebSocket and complete the RFC 6455 opening handshake.
|
|
76
|
+
* @brief Open the WebSocket and complete the RFC 6455 opening handshake.
|
|
73
77
|
*
|
|
74
78
|
* @param deadlineMs Reject if the socket has not opened within this many
|
|
75
79
|
* milliseconds (default 5000). Uses events, never a sleep.
|
|
@@ -130,8 +134,9 @@ export class TransportWs {
|
|
|
130
134
|
});
|
|
131
135
|
}
|
|
132
136
|
/**
|
|
133
|
-
* Send one complete libtracer TLV as a single BINARY WebSocket frame.
|
|
134
|
-
*
|
|
137
|
+
* @brief Send one complete libtracer TLV as a single BINARY WebSocket frame.
|
|
138
|
+
*
|
|
139
|
+
* The underlying WebSocket applies the RFC 6455 client masking. Throws if not
|
|
135
140
|
* connected.
|
|
136
141
|
*/
|
|
137
142
|
send(frame) {
|
|
@@ -139,7 +144,7 @@ export class TransportWs {
|
|
|
139
144
|
throw new Error('TransportWs.send called before connect()');
|
|
140
145
|
this.ws.send(frame);
|
|
141
146
|
}
|
|
142
|
-
/** Close the connection. Resolves once the socket has fully closed. */
|
|
147
|
+
/** @brief Close the connection. Resolves once the socket has fully closed. */
|
|
143
148
|
close() {
|
|
144
149
|
return new Promise((resolve) => {
|
|
145
150
|
const ws = this.ws;
|
package/dist/ws.d.ts
CHANGED
|
@@ -1,4 +1,25 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* @brief RFC 6455 WebSocket frame codec — a pure-TS port of the C++ reference
|
|
3
|
+
* codec in core/include/libtracer/ws.hpp (`tr::net::ws`).
|
|
4
|
+
*
|
|
5
|
+
* It is socket-free and has no I/O; correctness is gated byte-for-byte against
|
|
6
|
+
* the same known vectors the C++ codec is tested with (core/tests/ws_test.cpp).
|
|
7
|
+
* This is the cross-implementation agreement layer: a TS client framed by this
|
|
8
|
+
* codec interoperates with the C++ `tr::net::transport_ws` server, and vice versa.
|
|
9
|
+
*
|
|
10
|
+
* Reachable only via the `./ws` subpath export (`@avatarsd-llc/libtracer-ws/ws`) —
|
|
11
|
+
* deliberately NOT re-exported from the package barrel. Production `TransportWs`
|
|
12
|
+
* frames over the runtime's own WebSocket and never imports this, so a consumer that
|
|
13
|
+
* only wants the transport tree-shakes the codec (SHA-1, base64, frame (de)coder) out
|
|
14
|
+
* of its bundle. Its consumers are the interop oracle (ws_diff_fuzz, ws-codec tests)
|
|
15
|
+
* and runtimes without a native WebSocket.
|
|
16
|
+
*
|
|
17
|
+
* Two concerns only, mirroring the C++ header:
|
|
18
|
+
* 1. the opening-handshake key derivation (SHA-1 + base64 -> Sec-WebSocket-Accept), and
|
|
19
|
+
* 2. the data-frame codec (decode one masked/unmasked frame; encode one
|
|
20
|
+
* server frame unmasked, or one client frame masked, FIN=1).
|
|
21
|
+
*/
|
|
22
|
+
/** @brief RFC 6455 frame opcodes (the subset libtracer cares about). */
|
|
2
23
|
export declare enum Opcode {
|
|
3
24
|
CONT = 0,
|
|
4
25
|
TEXT = 1,
|
|
@@ -7,39 +28,44 @@ export declare enum Opcode {
|
|
|
7
28
|
PING = 9,
|
|
8
29
|
PONG = 10
|
|
9
30
|
}
|
|
10
|
-
/** One decoded RFC 6455 data/control frame (payload already unmasked). */
|
|
31
|
+
/** @brief One decoded RFC 6455 data/control frame (payload already unmasked). */
|
|
11
32
|
export interface Frame {
|
|
12
|
-
/** Frame opcode. */
|
|
33
|
+
/** @brief Frame opcode. */
|
|
13
34
|
op: Opcode;
|
|
14
|
-
/** FIN bit (true = final fragment). */
|
|
35
|
+
/** @brief FIN bit (true = final fragment). */
|
|
15
36
|
fin: boolean;
|
|
16
|
-
/** Unmasked application payload. */
|
|
37
|
+
/** @brief Unmasked application payload. */
|
|
17
38
|
payload: Uint8Array;
|
|
18
39
|
}
|
|
19
40
|
/**
|
|
20
|
-
* Standard SHA-1 over an arbitrary byte array (RFC 3174 / FIPS 180-1).
|
|
21
|
-
*
|
|
41
|
+
* @brief Standard SHA-1 over an arbitrary byte array (RFC 3174 / FIPS 180-1).
|
|
42
|
+
*
|
|
43
|
+
* Returns the 20-byte digest. Used by {@link acceptKey} for the RFC 6455 opening
|
|
22
44
|
* handshake; SHA-1 is required there for protocol compatibility, not security.
|
|
23
45
|
*/
|
|
24
46
|
export declare function sha1(data: Uint8Array): Uint8Array;
|
|
25
|
-
/** Standard base64 encoding (RFC 4648, with '=' padding). */
|
|
47
|
+
/** @brief Standard base64 encoding (RFC 4648, with '=' padding). */
|
|
26
48
|
export declare function base64(data: Uint8Array): string;
|
|
27
49
|
/**
|
|
28
|
-
* Compute the RFC 6455 Sec-WebSocket-Accept value for a client key:
|
|
29
|
-
* `base64(sha1(clientKey + GUID))`.
|
|
30
|
-
*
|
|
50
|
+
* @brief Compute the RFC 6455 Sec-WebSocket-Accept value for a client key:
|
|
51
|
+
* `base64(sha1(clientKey + GUID))`.
|
|
52
|
+
*
|
|
53
|
+
* The server returns this in the 101 Switching Protocols response to prove it
|
|
54
|
+
* spoke RFC 6455.
|
|
31
55
|
*
|
|
32
56
|
* @param clientKey The raw Sec-WebSocket-Key header value sent by the client.
|
|
33
57
|
*/
|
|
34
58
|
export declare function acceptKey(clientKey: string): string;
|
|
35
59
|
/**
|
|
36
|
-
* Encode one server->client RFC 6455 frame: FIN=1, given opcode, UNMASKED.
|
|
60
|
+
* @brief Encode one server->client RFC 6455 frame: FIN=1, given opcode, UNMASKED.
|
|
61
|
+
*
|
|
37
62
|
* Server frames MUST NOT be masked (RFC 6455 §5.1). The length uses the smallest
|
|
38
63
|
* legal encoding (7-bit, then 126 + 2-byte marker, then 127 + 8-byte marker).
|
|
39
64
|
*/
|
|
40
65
|
export declare function encodeFrame(op: Opcode, payload: Uint8Array): Uint8Array;
|
|
41
66
|
/**
|
|
42
|
-
* Encode one client->server RFC 6455 frame: FIN=1, given opcode, MASKED.
|
|
67
|
+
* @brief Encode one client->server RFC 6455 frame: FIN=1, given opcode, MASKED.
|
|
68
|
+
*
|
|
43
69
|
* Client frames MUST be masked (RFC 6455 §5.1): the MASK bit is set, a 4-byte
|
|
44
70
|
* masking key is emitted big-endian after the length, and every payload byte is
|
|
45
71
|
* XOR'd with `maskKey[i % 4]`. `maskKey` is a 32-bit value whose 4 big-endian
|
|
@@ -48,8 +74,9 @@ export declare function encodeFrame(op: Opcode, payload: Uint8Array): Uint8Array
|
|
|
48
74
|
*/
|
|
49
75
|
export declare function encodeClientFrame(op: Opcode, payload: Uint8Array, maskKey: number): Uint8Array;
|
|
50
76
|
/**
|
|
51
|
-
* Decode exactly one RFC 6455 frame from the front of `buf`.
|
|
52
|
-
*
|
|
77
|
+
* @brief Decode exactly one RFC 6455 frame from the front of `buf`.
|
|
78
|
+
*
|
|
79
|
+
* Handles the FIN bit, opcode, the MASK bit with its 4-byte key (client->server frames are
|
|
53
80
|
* masked; the payload is unmasked here), and the 7 / 16 / 64-bit extended length
|
|
54
81
|
* encodings. Overflow-safe: a 64-bit over-long length on a short buffer yields
|
|
55
82
|
* need-more (`null`), never an out-of-bounds read.
|
package/dist/ws.js
CHANGED
|
@@ -1,24 +1,27 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
// SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @brief RFC 6455 WebSocket frame codec — a pure-TS port of the C++ reference
|
|
5
|
+
* codec in core/include/libtracer/ws.hpp (`tr::net::ws`).
|
|
6
|
+
*
|
|
7
|
+
* It is socket-free and has no I/O; correctness is gated byte-for-byte against
|
|
8
|
+
* the same known vectors the C++ codec is tested with (core/tests/ws_test.cpp).
|
|
9
|
+
* This is the cross-implementation agreement layer: a TS client framed by this
|
|
10
|
+
* codec interoperates with the C++ `tr::net::transport_ws` server, and vice versa.
|
|
11
|
+
*
|
|
12
|
+
* Reachable only via the `./ws` subpath export (`@avatarsd-llc/libtracer-ws/ws`) —
|
|
13
|
+
* deliberately NOT re-exported from the package barrel. Production `TransportWs`
|
|
14
|
+
* frames over the runtime's own WebSocket and never imports this, so a consumer that
|
|
15
|
+
* only wants the transport tree-shakes the codec (SHA-1, base64, frame (de)coder) out
|
|
16
|
+
* of its bundle. Its consumers are the interop oracle (ws_diff_fuzz, ws-codec tests)
|
|
17
|
+
* and runtimes without a native WebSocket.
|
|
18
|
+
*
|
|
19
|
+
* Two concerns only, mirroring the C++ header:
|
|
20
|
+
* 1. the opening-handshake key derivation (SHA-1 + base64 -> Sec-WebSocket-Accept), and
|
|
21
|
+
* 2. the data-frame codec (decode one masked/unmasked frame; encode one
|
|
22
|
+
* server frame unmasked, or one client frame masked, FIN=1).
|
|
23
|
+
*/
|
|
24
|
+
/** @brief RFC 6455 frame opcodes (the subset libtracer cares about). */
|
|
22
25
|
export var Opcode;
|
|
23
26
|
(function (Opcode) {
|
|
24
27
|
Opcode[Opcode["CONT"] = 0] = "CONT";
|
|
@@ -33,8 +36,9 @@ function rotl32(v, n) {
|
|
|
33
36
|
return ((v << n) | (v >>> (32 - n))) >>> 0;
|
|
34
37
|
}
|
|
35
38
|
/**
|
|
36
|
-
* Standard SHA-1 over an arbitrary byte array (RFC 3174 / FIPS 180-1).
|
|
37
|
-
*
|
|
39
|
+
* @brief Standard SHA-1 over an arbitrary byte array (RFC 3174 / FIPS 180-1).
|
|
40
|
+
*
|
|
41
|
+
* Returns the 20-byte digest. Used by {@link acceptKey} for the RFC 6455 opening
|
|
38
42
|
* handshake; SHA-1 is required there for protocol compatibility, not security.
|
|
39
43
|
*/
|
|
40
44
|
export function sha1(data) {
|
|
@@ -119,7 +123,7 @@ export function sha1(data) {
|
|
|
119
123
|
}
|
|
120
124
|
/* -------------------------------------------------------------- base64 --- */
|
|
121
125
|
const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
122
|
-
/** Standard base64 encoding (RFC 4648, with '=' padding). */
|
|
126
|
+
/** @brief Standard base64 encoding (RFC 4648, with '=' padding). */
|
|
123
127
|
export function base64(data) {
|
|
124
128
|
let out = '';
|
|
125
129
|
let i = 0;
|
|
@@ -141,9 +145,11 @@ export function base64(data) {
|
|
|
141
145
|
/* ----------------------------------------------------------- accept_key --- */
|
|
142
146
|
const ACCEPT_GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
|
143
147
|
/**
|
|
144
|
-
* Compute the RFC 6455 Sec-WebSocket-Accept value for a client key:
|
|
145
|
-
* `base64(sha1(clientKey + GUID))`.
|
|
146
|
-
*
|
|
148
|
+
* @brief Compute the RFC 6455 Sec-WebSocket-Accept value for a client key:
|
|
149
|
+
* `base64(sha1(clientKey + GUID))`.
|
|
150
|
+
*
|
|
151
|
+
* The server returns this in the 101 Switching Protocols response to prove it
|
|
152
|
+
* spoke RFC 6455.
|
|
147
153
|
*
|
|
148
154
|
* @param clientKey The raw Sec-WebSocket-Key header value sent by the client.
|
|
149
155
|
*/
|
|
@@ -156,7 +162,8 @@ export function acceptKey(clientKey) {
|
|
|
156
162
|
}
|
|
157
163
|
/* ------------------------------------------------------------- framing --- */
|
|
158
164
|
/**
|
|
159
|
-
* Encode one server->client RFC 6455 frame: FIN=1, given opcode, UNMASKED.
|
|
165
|
+
* @brief Encode one server->client RFC 6455 frame: FIN=1, given opcode, UNMASKED.
|
|
166
|
+
*
|
|
160
167
|
* Server frames MUST NOT be masked (RFC 6455 §5.1). The length uses the smallest
|
|
161
168
|
* legal encoding (7-bit, then 126 + 2-byte marker, then 127 + 8-byte marker).
|
|
162
169
|
*/
|
|
@@ -181,7 +188,8 @@ export function encodeFrame(op, payload) {
|
|
|
181
188
|
return Uint8Array.from(out);
|
|
182
189
|
}
|
|
183
190
|
/**
|
|
184
|
-
* Encode one client->server RFC 6455 frame: FIN=1, given opcode, MASKED.
|
|
191
|
+
* @brief Encode one client->server RFC 6455 frame: FIN=1, given opcode, MASKED.
|
|
192
|
+
*
|
|
185
193
|
* Client frames MUST be masked (RFC 6455 §5.1): the MASK bit is set, a 4-byte
|
|
186
194
|
* masking key is emitted big-endian after the length, and every payload byte is
|
|
187
195
|
* XOR'd with `maskKey[i % 4]`. `maskKey` is a 32-bit value whose 4 big-endian
|
|
@@ -217,8 +225,9 @@ export function encodeClientFrame(op, payload, maskKey) {
|
|
|
217
225
|
return Uint8Array.from(out);
|
|
218
226
|
}
|
|
219
227
|
/**
|
|
220
|
-
* Decode exactly one RFC 6455 frame from the front of `buf`.
|
|
221
|
-
*
|
|
228
|
+
* @brief Decode exactly one RFC 6455 frame from the front of `buf`.
|
|
229
|
+
*
|
|
230
|
+
* Handles the FIN bit, opcode, the MASK bit with its 4-byte key (client->server frames are
|
|
222
231
|
* masked; the payload is unmasked here), and the 7 / 16 / 64-bit extended length
|
|
223
232
|
* encodings. Overflow-safe: a 64-bit over-long length on a short buffer yields
|
|
224
233
|
* need-more (`null`), never an out-of-bounds read.
|
|
@@ -272,7 +281,7 @@ export function decodeFrame(buf) {
|
|
|
272
281
|
}
|
|
273
282
|
return { frame: { op, fin, payload }, consumed: pos + len };
|
|
274
283
|
}
|
|
275
|
-
/** Append the 8 big-endian bytes of a (<= 2^53) length to `out`. */
|
|
284
|
+
/** @brief Append the 8 big-endian bytes of a (<= 2^53) length to `out`. */
|
|
276
285
|
function pushU64Be(out, len) {
|
|
277
286
|
const hi = Math.floor(len / 0x100000000);
|
|
278
287
|
const lo = len >>> 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@avatarsd-llc/libtracer-ws",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "WebSocket transport for libtracer (ADR-0029) — an RFC 6455 frame codec plus a TransportWs client that carries libtracer TLV frames over WebSocket, wire-compatible with the C++ tr::net::transport_ws. The core (@avatarsd-llc/libtracer) is a peerDependency. See docs/adr/0033-npm-subpackage-monorepo.md.",
|
|
5
5
|
"author": "avatarsd LLC",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"access": "public"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"@avatarsd-llc/libtracer": ">=0.
|
|
40
|
+
"@avatarsd-llc/libtracer": ">=0.4.0 <0.5.0"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"build": "tsc",
|