@avatarsd-llc/libtracer-ws 0.1.0 → 0.3.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 CHANGED
@@ -1,5 +1,3 @@
1
1
  export declare const TRANSPORT: "ws";
2
- export { Opcode, acceptKey, encodeFrame, encodeClientFrame, decodeFrame, sha1, base64, } from './ws.js';
3
- export type { Frame } from './ws.js';
4
2
  export { TransportWs } from './transport.js';
5
- export type { FrameReceiver, WebSocketLike, WebSocketCtor, TransportWsOptions, } from './transport.js';
3
+ export type { FrameReceiver, CloseHandler, WebSocketLike, WebSocketCtor, TransportWsOptions, } from './transport.js';
package/dist/index.js CHANGED
@@ -6,11 +6,15 @@
6
6
  // first reliable transport (ADR-0029). This package carries libtracer TLV frames
7
7
  // over RFC 6455 WebSocket, wire-compatible with the C++ `tr::net::transport_ws`.
8
8
  //
9
- // Two entry points:
10
- // - the `ws` subpath: a pure, socket-free RFC 6455 frame codec (cross-validated
11
- // byte-for-byte against the C++ codec); and
12
- // - this barrel: the `TransportWs` client plus a re-export of the codec.
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).
13
18
  // The core (`@avatarsd-llc/libtracer`) is a peerDependency (ADR-0033).
14
19
  export const TRANSPORT = 'ws';
15
- export { Opcode, acceptKey, encodeFrame, encodeClientFrame, decodeFrame, sha1, base64, } from './ws.js';
16
20
  export { TransportWs } from './transport.js';
@@ -1,5 +1,7 @@
1
1
  /** A received libtracer frame: the unmasked payload of one inbound BINARY frame. */
2
2
  export type FrameReceiver = (bytes: Uint8Array) => void;
3
+ /** Invoked once when an OPEN connection closes/errors, with the cause when known. */
4
+ export type CloseHandler = (cause?: Error) => void;
3
5
  /** The minimal WHATWG-WebSocket surface this transport relies on. */
4
6
  export interface WebSocketLike {
5
7
  binaryType: string;
@@ -35,6 +37,8 @@ export declare class TransportWs {
35
37
  private readonly ctor;
36
38
  private ws;
37
39
  private receiver;
40
+ private closeHandler;
41
+ private closeNotified;
38
42
  /**
39
43
  * @param url The `ws://host:port` (or `wss://`) endpoint to dial.
40
44
  * @param options Transport options; see {@link TransportWsOptions}.
@@ -46,6 +50,14 @@ export declare class TransportWs {
46
50
  * BINARY frame with that frame's payload bytes.
47
51
  */
48
52
  onFrame(receiver: FrameReceiver | null): void;
53
+ /**
54
+ * Register (or clear) the connection-closed notifier (the `ClientTransport`
55
+ * seam's optional close hook). Invoked at most once per connection, when an
56
+ * OPEN socket closes — remotely, on error, or via {@link close}.
57
+ */
58
+ onClose(handler: CloseHandler | null): void;
59
+ /** Fire the close notifier exactly once for the current connection. */
60
+ private notifyClose;
49
61
  /** True once the underlying WebSocket has opened and not yet closed. */
50
62
  get connected(): boolean;
51
63
  /**
package/dist/transport.js CHANGED
@@ -30,6 +30,8 @@ export class TransportWs {
30
30
  constructor(url, options = {}) {
31
31
  this.ws = null;
32
32
  this.receiver = null;
33
+ this.closeHandler = null;
34
+ this.closeNotified = false;
33
35
  const ctor = options.WebSocket ?? globalThis.WebSocket;
34
36
  if (!ctor) {
35
37
  throw new Error('No WebSocket implementation available: pass options.WebSocket (e.g. the `ws` package) ' +
@@ -46,6 +48,22 @@ export class TransportWs {
46
48
  onFrame(receiver) {
47
49
  this.receiver = receiver;
48
50
  }
51
+ /**
52
+ * Register (or clear) the connection-closed notifier (the `ClientTransport`
53
+ * seam's optional close hook). Invoked at most once per connection, when an
54
+ * OPEN socket closes — remotely, on error, or via {@link close}.
55
+ */
56
+ onClose(handler) {
57
+ this.closeHandler = handler;
58
+ }
59
+ /** Fire the close notifier exactly once for the current connection. */
60
+ notifyClose(cause) {
61
+ if (this.closeNotified)
62
+ return;
63
+ this.closeNotified = true;
64
+ if (this.closeHandler)
65
+ this.closeHandler(cause);
66
+ }
49
67
  /** True once the underlying WebSocket has opened and not yet closed. */
50
68
  get connected() {
51
69
  return this.ws !== null;
@@ -80,6 +98,7 @@ export class TransportWs {
80
98
  settled = true;
81
99
  clearTimeout(timer);
82
100
  this.ws = ws;
101
+ this.closeNotified = false; // arm the close notifier for this connection
83
102
  resolve();
84
103
  };
85
104
  ws.onmessage = (ev) => {
@@ -89,6 +108,9 @@ export class TransportWs {
89
108
  };
90
109
  ws.onerror = () => {
91
110
  if (settled) {
111
+ // Post-open socket error: the close notifier carries the cause (the
112
+ // subsequent onclose is deduped by notifyClose).
113
+ this.notifyClose(new Error('WebSocket error'));
92
114
  return;
93
115
  }
94
116
  settled = true;
@@ -97,8 +119,10 @@ export class TransportWs {
97
119
  };
98
120
  ws.onclose = () => {
99
121
  this.ws = null;
100
- if (settled)
122
+ if (settled) {
123
+ this.notifyClose();
101
124
  return;
125
+ }
102
126
  settled = true;
103
127
  clearTimeout(timer);
104
128
  reject(new Error('TransportWs.connect closed before opening'));
@@ -125,6 +149,7 @@ export class TransportWs {
125
149
  }
126
150
  ws.onclose = () => {
127
151
  this.ws = null;
152
+ this.notifyClose();
128
153
  resolve();
129
154
  };
130
155
  ws.close();
package/dist/ws.js CHANGED
@@ -7,6 +7,13 @@
7
7
  // agreement layer: a TS client framed by this codec interoperates with the C++
8
8
  // `tr::net::transport_ws` server, and vice versa.
9
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
+ //
10
17
  // Two concerns only, mirroring the C++ header:
11
18
  // 1. the opening-handshake key derivation (SHA-1 + base64 -> Sec-WebSocket-Accept), and
12
19
  // 2. the data-frame codec (decode one masked/unmasked frame; encode one
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@avatarsd-llc/libtracer-ws",
3
- "version": "0.1.0",
3
+ "version": "0.3.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.0.1"
40
+ "@avatarsd-llc/libtracer": ">=0.3.0 <0.4.0"
41
41
  },
42
42
  "scripts": {
43
43
  "build": "tsc",