@statewalker/webrun-streams-libp2p 0.5.0 → 0.6.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/README.md CHANGED
@@ -57,7 +57,7 @@ runs (Node or browser).
57
57
  | `duplexOverStream(stream, input, options?)` | The primitive under both: runs one call over one libp2p `Stream`. Returns an `AsyncGenerator<Uint8Array>` of the peer's bytes. |
58
58
  | `PeerGoneError` | What a call fails with when the stream ended before both sides finished it: the peer died, the connection dropped, or the stream was reset after the peer's side ended. `cause` is libp2p's close error, if any. |
59
59
  | `messageTargetOverStream(stream, options?)` | `{ target, signal }`: one stream as a pipe of `Uint8Array` messages for `multiplexPort` / `overPipe`, and an `AbortSignal` that aborts when the stream is over. `options.maxMessageBytes` caps one message. See below. |
60
- | `libp2pPortMux(options)` | Native MessagePorts: a `webrun-rpc` `PortMux` (by shape) whose every port is its own libp2p stream, for `overPorts`. `options`: `node`, `codec` (a value codec: `{ encode: serialize, decode: deserialize }` from `webrun-msgpack`), `peer` (to open ports), `onPort(port, { remotePeer, connection })` (to accept them), `protocol`, `maxMessageSize`, stream limits. The port lifecycle (codec wrapping, messages held until a port's first listener, close signals, accept/refuse, the caller's signal, close-all) is the shared `newNativePortMux` from `@statewalker/webrun-streams`; `describePortMux` (`webrun-streams-conformance`) is its contract, run against this transport too. Messages that arrive before a port's first listener are now held for it, as over WebRTC. See "MessagePorts over libp2p" below. |
60
+ | `libp2pPortMux(options)` | Native MessagePorts: a `webrun-rpc` `PortMux` (by shape) whose every port is its own libp2p stream, for `overPorts`. `options`: `node`, `codec` (a value codec: `{ encode: serialize, decode: deserialize }` from `webrun-msgpack`), `peer` (to open ports), `onPort(port, { remotePeer, connection })` (to accept them), `protocol`, `maxMessageSize`, stream limits, `window`. The port lifecycle (codec wrapping, messages held until a port's first listener, close signals, accept/refuse, the caller's signal, close-all) is the shared `newNativePortMux` from `@statewalker/webrun-streams`; `describePortMux` (`webrun-streams-conformance`) is its contract, run against this transport too. Messages that arrive before a port's first listener are now held for it, as over WebRTC. See "MessagePorts over libp2p" below. |
61
61
  | `DEFAULT_PORT_PROTOCOL`, `DEFAULT_PORT_MAX_MESSAGE_SIZE` | `"/webrun-ports/1.0.0"`, 256 KiB. |
62
62
  | `DEFAULT_PROTOCOL` | `"/webrun-streams/1.0.0"`. |
63
63
  | `DEFAULT_DRAIN_TIMEOUT_MS` | `300_000` (5 minutes). |
@@ -336,6 +336,17 @@ Both pass the shared conformance suite, including L6 at a one-chunk bound
336
336
  | who is calling | `onPort` gets `{ remotePeer, connection }` | the stream's connection, once |
337
337
  | 4 MiB echo, in-process TCP, setup excluded (three runs) | 35–38 MiB/s (64 KiB chunks), 71–88 MiB/s (256 KiB) | 37–48 MiB/s, 79–87 MiB/s |
338
338
 
339
+ **A window per port.** By default the stream tier waits for each chunk's
340
+ confirmation on a native port, so one call moves one chunk (256 KiB) per round
341
+ trip. With `window: n` (bytes) on both ends, a port's stream negotiates
342
+ `windowProtocolOf(protocol)` (`protocol` + `/window`), each end grants the other
343
+ `n` bytes of messages delivered and not yet consumed, and the stream tier
344
+ pipelines its chunks up to the peer's window (statewalker/webrun-wire#153). A
345
+ mux with `window` dials the windowed protocol first and falls back to
346
+ `protocol`, and serves both, so it works against an older peer, unwindowed.
347
+ `describePortWindow` (`webrun-streams-conformance`) and L6 at the window's bound
348
+ run against it (`tests/port-window.test.ts`).
349
+
339
350
  Each native port carries `closeSignal`, aborted when its stream ends, the peer
340
351
  goes, or the port is closed here; `webrun-rpc`'s `getPortCloseSignal` reads it
341
352
  from the port, so a call on a dead stream fails instead of waiting, and this
package/dist/index.js CHANGED
@@ -515,10 +515,11 @@ function connectionOf(node, stream) {
515
515
  * the same peer from the same node, since they share its connection.
516
516
  */
517
517
  async function openGatedStream(node, peer, proto, dialOptions) {
518
+ const protocols = Array.isArray(proto) ? proto : [proto];
518
519
  const gate = streamGateFor(node, peer);
519
520
  while (!isProven(gate)) {
520
521
  if (gate.firstOpening == null) {
521
- const opening = node.dialProtocol(peer, [proto], dialOptions);
522
+ const opening = node.dialProtocol(peer, protocols, dialOptions);
522
523
  gate.firstOpening = opening;
523
524
  try {
524
525
  const stream = await opening;
@@ -530,7 +531,7 @@ async function openGatedStream(node, peer, proto, dialOptions) {
530
531
  }
531
532
  await gate.firstOpening.catch(() => void 0);
532
533
  }
533
- return node.dialProtocol(peer, [proto], dialOptions);
534
+ return node.dialProtocol(peer, protocols, dialOptions);
534
535
  }
535
536
  //#endregion
536
537
  //#region src/connect-serve.ts
@@ -861,6 +862,14 @@ function toBytes(message) {
861
862
  /** The protocol a port's stream negotiates unless told otherwise. */
862
863
  const DEFAULT_PORT_PROTOCOL = "/webrun-ports/1.0.0";
863
864
  /**
865
+ * The protocol a windowed port's stream negotiates, for a mux whose ports
866
+ * negotiate `protocol`: `protocol` with `/window` appended. A mux with
867
+ * `window` dials it first and falls back to `protocol`, and serves both.
868
+ */
869
+ function windowProtocolOf(protocol) {
870
+ return `${protocol}/window`;
871
+ }
872
+ /**
864
873
  * 256 KiB: what one message on a port may carry before `webrun-rpc`'s stream tier chunks a body.
865
874
  * A port waits for each chunk's confirmation, so a larger chunk means fewer round trips; well
866
875
  * under `messageTargetOverStream`'s 64 MiB frame ceiling.
@@ -895,10 +904,16 @@ async function libp2pPortMux(options) {
895
904
  const maxOutboundStreams = options.maxOutboundStreams ?? 1024;
896
905
  const protocol = options.protocol ?? "/webrun-ports/1.0.0";
897
906
  const maxMessageSize = options.maxMessageSize ?? 262144;
898
- /** A stream as a channel: framed bytes, and a reset to break it off (the peer hears it). */
907
+ const { window } = options;
908
+ const windowProtocol = windowProtocolOf(protocol);
909
+ /**
910
+ * A stream as a channel: framed bytes, and a reset to break it off (the peer hears it). The
911
+ * protocol it negotiated says whether both ends run the window.
912
+ */
899
913
  const channelOf = (stream) => ({
900
914
  pipe: messageTargetOverStream(stream, { maxMessageBytes }).target,
901
- fail: (reason) => stream.abort(reason)
915
+ fail: (reason) => stream.abort(reason),
916
+ ...window !== void 0 && stream.protocol === windowProtocol ? { window: "agreed" } : {}
902
917
  });
903
918
  const dialOptions = { maxOutboundStreams };
904
919
  if (runOnLimitedConnection !== void 0) dialOptions.runOnLimitedConnection = runOnLimitedConnection;
@@ -908,16 +923,19 @@ async function libp2pPortMux(options) {
908
923
  codec,
909
924
  maxMessageSize,
910
925
  onPort,
926
+ window,
911
927
  async open(signal) {
912
928
  if (!peer) throw new Error("webrun-streams-libp2p: openPort needs `peer`");
913
- const stream = await openGatedStream(node, peer, protocol, {
929
+ const stream = await openGatedStream(node, peer, window !== void 0 ? [windowProtocol, protocol] : protocol, {
914
930
  ...dialOptions,
915
931
  signal
916
932
  });
917
933
  return channelOf(stream);
918
934
  },
919
935
  async stopAccepting() {
920
- if (handled) await node.unhandle(protocol).catch(() => void 0);
936
+ if (!handled) return;
937
+ const served = window !== void 0 ? [protocol, windowProtocol] : [protocol];
938
+ await node.unhandle(served).catch(() => void 0);
921
939
  }
922
940
  });
923
941
  if (onPort) {
@@ -926,7 +944,8 @@ async function libp2pPortMux(options) {
926
944
  maxOutboundStreams
927
945
  };
928
946
  if (runOnLimitedConnection !== void 0) handleOptions.runOnLimitedConnection = runOnLimitedConnection;
929
- await node.handle(protocol, (stream, connection) => {
947
+ const served = window !== void 0 ? [protocol, windowProtocol] : [protocol];
948
+ await node.handle(served, (stream, connection) => {
930
949
  if (control.closed) {
931
950
  stream.abort(/* @__PURE__ */ new Error("webrun-streams-libp2p: port mux closed"));
932
951
  return;
@@ -4,6 +4,12 @@ import { type NativePort, type ValueCodec } from "@statewalker/webrun-streams";
4
4
  export type { ValueCodec };
5
5
  /** The protocol a port's stream negotiates unless told otherwise. */
6
6
  export declare const DEFAULT_PORT_PROTOCOL = "/webrun-ports/1.0.0";
7
+ /**
8
+ * The protocol a windowed port's stream negotiates, for a mux whose ports
9
+ * negotiate `protocol`: `protocol` with `/window` appended. A mux with
10
+ * `window` dials it first and falls back to `protocol`, and serves both.
11
+ */
12
+ export declare function windowProtocolOf(protocol: string): string;
7
13
  /**
8
14
  * 256 KiB: what one message on a port may carry before `webrun-rpc`'s stream tier chunks a body.
9
15
  * A port waits for each chunk's confirmation, so a larger chunk means fewer round trips; well
@@ -48,6 +54,19 @@ export interface Libp2pPortMuxOptions {
48
54
  /** Default {@link DEFAULT_PORT_STREAM_LIMIT}. */
49
55
  maxOutboundStreams?: number;
50
56
  runOnLimitedConnection?: boolean;
57
+ /**
58
+ * Per-port flow control, in bytes (off when unset; from 64 KiB,
59
+ * `MIN_PORT_WINDOW` in `@statewalker/webrun-streams`): the most this end holds of a port's messages
60
+ * delivered and not yet consumed (statewalker/webrun-wire#153). A port
61
+ * whose two ends both set it negotiates {@link windowProtocolOf} `protocol`,
62
+ * and `webrun-rpc`'s stream tier then pipelines its chunks, up to the
63
+ * peer's window in flight, instead of waiting for each chunk's
64
+ * confirmation; against a mux without it (an older peer) the port
65
+ * negotiates `protocol` and runs as before. 1-4 MiB is a reasonable range:
66
+ * the most one call moves per round trip, and the most a producer runs
67
+ * ahead of a consumer that stopped reading.
68
+ */
69
+ window?: number;
51
70
  }
52
71
  /** A `webrun-rpc` `PortMux` by shape; this package imports nothing from it. */
53
72
  export interface Libp2pPortMux {
@@ -1 +1 @@
1
- {"version":3,"file":"port-mux.d.ts","sourceRoot":"","sources":["../src/port-mux.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,UAAU,EAEV,MAAM,EACN,MAAM,EAGP,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAEL,KAAK,UAAU,EAEf,KAAK,UAAU,EAChB,MAAM,6BAA6B,CAAC;AAIrC,YAAY,EAAE,UAAU,EAAE,CAAC;AAE3B,qEAAqE;AACrE,eAAO,MAAM,qBAAqB,wBAAwB,CAAC;AAE3D;;;;GAIG;AACH,eAAO,MAAM,6BAA6B,QAAa,CAAC;AAExD;;;GAGG;AACH,eAAO,MAAM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BAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,sBAAsB,CAAC,EAAE,OAAO,CAAC;IACjC;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,+EAA+E;AAC/E,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,WAAW,CAAA;KAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAClF,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACvC,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;CACjC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,aAAa,CAAC,CAmEzF"}
@@ -6,5 +6,5 @@ import type { Multiaddr } from "@multiformats/multiaddr";
6
6
  * (`maxEarlyStreams`). Shared by `connect()` and `libp2pPortMux`, and by every caller dialling
7
7
  * the same peer from the same node, since they share its connection.
8
8
  */
9
- export declare function openGatedStream(node: Libp2p, peer: PeerId | Multiaddr, proto: string, dialOptions?: DialProtocolOptions): Promise<Stream>;
9
+ export declare function openGatedStream(node: Libp2p, peer: PeerId | Multiaddr, proto: string | string[], dialOptions?: DialProtocolOptions): Promise<Stream>;
10
10
  //# sourceMappingURL=stream-gate.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"stream-gate.d.ts","sourceRoot":"","sources":["../src/stream-gate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,mBAAmB,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACjG,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AA+CzD;;;;;GAKG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,GAAG,SAAS,EACxB,KAAK,EAAE,MAAM,EACb,WAAW,CAAC,EAAE,mBAAmB,GAChC,OAAO,CAAC,MAAM,CAAC,CAiBjB"}
1
+ {"version":3,"file":"stream-gate.d.ts","sourceRoot":"","sources":["../src/stream-gate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,mBAAmB,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACjG,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AA+CzD;;;;;GAKG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,GAAG,SAAS,EACxB,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,EACxB,WAAW,CAAC,EAAE,mBAAmB,GAChC,OAAO,CAAC,MAAM,CAAC,CAmBjB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statewalker/webrun-streams-libp2p",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "libp2p native multi-stream Connect/Serve adapter in the webrun-streams-* family",
@@ -30,7 +30,7 @@
30
30
  "src"
31
31
  ],
32
32
  "dependencies": {
33
- "@statewalker/webrun-streams": "^0.5.0"
33
+ "@statewalker/webrun-streams": "^0.6.0"
34
34
  },
35
35
  "peerDependencies": {
36
36
  "@libp2p/interface": "^3.0.0",
@@ -57,9 +57,9 @@
57
57
  "tsx": "^4.23.15",
58
58
  "typescript": "^7.0.2",
59
59
  "vitest": "^5.0.5",
60
- "@statewalker/webrun-rpc": "^0.13.0",
61
- "@statewalker/webrun-msgpack": "^0.12.0",
62
- "@statewalker/webrun-streams-conformance": "^0.6.0"
60
+ "@statewalker/webrun-msgpack": "^0.13.0",
61
+ "@statewalker/webrun-rpc": "^0.14.0",
62
+ "@statewalker/webrun-streams-conformance": "^0.7.0"
63
63
  },
64
64
  "sideEffects": false,
65
65
  "publishConfig": {
package/src/port-mux.ts CHANGED
@@ -21,6 +21,15 @@ export type { ValueCodec };
21
21
  /** The protocol a port's stream negotiates unless told otherwise. */
22
22
  export const DEFAULT_PORT_PROTOCOL = "/webrun-ports/1.0.0";
23
23
 
24
+ /**
25
+ * The protocol a windowed port's stream negotiates, for a mux whose ports
26
+ * negotiate `protocol`: `protocol` with `/window` appended. A mux with
27
+ * `window` dials it first and falls back to `protocol`, and serves both.
28
+ */
29
+ export function windowProtocolOf(protocol: string): string {
30
+ return `${protocol}/window`;
31
+ }
32
+
24
33
  /**
25
34
  * 256 KiB: what one message on a port may carry before `webrun-rpc`'s stream tier chunks a body.
26
35
  * A port waits for each chunk's confirmation, so a larger chunk means fewer round trips; well
@@ -69,6 +78,19 @@ export interface Libp2pPortMuxOptions {
69
78
  /** Default {@link DEFAULT_PORT_STREAM_LIMIT}. */
70
79
  maxOutboundStreams?: number;
71
80
  runOnLimitedConnection?: boolean;
81
+ /**
82
+ * Per-port flow control, in bytes (off when unset; from 64 KiB,
83
+ * `MIN_PORT_WINDOW` in `@statewalker/webrun-streams`): the most this end holds of a port's messages
84
+ * delivered and not yet consumed (statewalker/webrun-wire#153). A port
85
+ * whose two ends both set it negotiates {@link windowProtocolOf} `protocol`,
86
+ * and `webrun-rpc`'s stream tier then pipelines its chunks, up to the
87
+ * peer's window in flight, instead of waiting for each chunk's
88
+ * confirmation; against a mux without it (an older peer) the port
89
+ * negotiates `protocol` and runs as before. 1-4 MiB is a reasonable range:
90
+ * the most one call moves per round trip, and the most a producer runs
91
+ * ahead of a consumer that stopped reading.
92
+ */
93
+ window?: number;
72
94
  }
73
95
 
74
96
  /** A `webrun-rpc` `PortMux` by shape; this package imports nothing from it. */
@@ -102,11 +124,20 @@ export async function libp2pPortMux(options: Libp2pPortMuxOptions): Promise<Libp
102
124
  const maxOutboundStreams = options.maxOutboundStreams ?? DEFAULT_PORT_STREAM_LIMIT;
103
125
  const protocol = options.protocol ?? DEFAULT_PORT_PROTOCOL;
104
126
  const maxMessageSize = options.maxMessageSize ?? DEFAULT_PORT_MAX_MESSAGE_SIZE;
127
+ // Checked by the shared lifecycle, before the protocols are handled.
128
+ const { window } = options;
129
+ const windowProtocol = windowProtocolOf(protocol);
105
130
 
106
- /** A stream as a channel: framed bytes, and a reset to break it off (the peer hears it). */
131
+ /**
132
+ * A stream as a channel: framed bytes, and a reset to break it off (the peer hears it). The
133
+ * protocol it negotiated says whether both ends run the window.
134
+ */
107
135
  const channelOf = (stream: Stream): NativeChannel => ({
108
136
  pipe: messageTargetOverStream(stream, { maxMessageBytes }).target,
109
137
  fail: (reason) => stream.abort(reason),
138
+ ...(window !== undefined && stream.protocol === windowProtocol
139
+ ? { window: "agreed" as const }
140
+ : {}),
110
141
  });
111
142
 
112
143
  const dialOptions: DialProtocolOptions = { maxOutboundStreams };
@@ -119,13 +150,17 @@ export async function libp2pPortMux(options: Libp2pPortMuxOptions): Promise<Libp
119
150
  codec,
120
151
  maxMessageSize,
121
152
  onPort,
153
+ window,
122
154
  async open(signal) {
123
155
  if (!peer) throw new Error("webrun-streams-libp2p: openPort needs `peer`");
124
- const stream = await openGatedStream(node, peer, protocol, { ...dialOptions, signal });
156
+ const protocols = window !== undefined ? [windowProtocol, protocol] : protocol;
157
+ const stream = await openGatedStream(node, peer, protocols, { ...dialOptions, signal });
125
158
  return channelOf(stream);
126
159
  },
127
160
  async stopAccepting() {
128
- if (handled) await node.unhandle(protocol).catch(() => undefined);
161
+ if (!handled) return;
162
+ const served = window !== undefined ? [protocol, windowProtocol] : [protocol];
163
+ await node.unhandle(served).catch(() => undefined);
129
164
  },
130
165
  });
131
166
 
@@ -133,8 +168,9 @@ export async function libp2pPortMux(options: Libp2pPortMuxOptions): Promise<Libp
133
168
  const handleOptions: StreamHandlerOptions = { maxInboundStreams, maxOutboundStreams };
134
169
  if (runOnLimitedConnection !== undefined)
135
170
  handleOptions.runOnLimitedConnection = runOnLimitedConnection;
171
+ const served = window !== undefined ? [protocol, windowProtocol] : [protocol];
136
172
  await node.handle(
137
- protocol,
173
+ served,
138
174
  (stream: Stream, connection: Connection) => {
139
175
  if (control.closed) {
140
176
  stream.abort(new Error("webrun-streams-libp2p: port mux closed"));
@@ -55,13 +55,15 @@ function connectionOf(node: Libp2p, stream: Stream): Connection | null {
55
55
  export async function openGatedStream(
56
56
  node: Libp2p,
57
57
  peer: PeerId | Multiaddr,
58
- proto: string,
58
+ proto: string | string[],
59
59
  dialOptions?: DialProtocolOptions,
60
60
  ): Promise<Stream> {
61
+ // Several: the remote picks the first it speaks (a windowed port's protocol, then the plain one).
62
+ const protocols = Array.isArray(proto) ? proto : [proto];
61
63
  const gate = streamGateFor(node, peer);
62
64
  while (!isProven(gate)) {
63
65
  if (gate.firstOpening == null) {
64
- const opening = node.dialProtocol(peer, [proto], dialOptions);
66
+ const opening = node.dialProtocol(peer, protocols, dialOptions);
65
67
  gate.firstOpening = opening;
66
68
  try {
67
69
  const stream = await opening;
@@ -73,5 +75,5 @@ export async function openGatedStream(
73
75
  }
74
76
  await gate.firstOpening.catch(() => undefined);
75
77
  }
76
- return node.dialProtocol(peer, [proto], dialOptions);
78
+ return node.dialProtocol(peer, protocols, dialOptions);
77
79
  }