@statewalker/webrun-streams-libp2p 0.7.0 → 0.7.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/README.md CHANGED
@@ -31,6 +31,16 @@ What the adapter adds on top of a raw stream:
31
31
  producer never runs more than about a window ahead of a consumer that stopped
32
32
  reading, whatever libp2p buffers (statewalker/webrun-wire#153). It is the
33
33
  same stack every other `webrun-rpc` transport runs.
34
+ - **A bounded send-ahead, and a stall bound** (statewalker/webrun-wire#195). A
35
+ sender runs at most min(the peer's window, its own window) ahead, whatever
36
+ window the peer announces (up to 4 GiB), and writes nothing while the stream
37
+ needs to drain, so a peer that stops reading at the transport holds about one
38
+ frame of this end's memory. A write that has waited for drain longer than
39
+ `drainTimeoutMs` (default 5 minutes) resets the stream; a quiet call (an event
40
+ stream between events) is never timed out. Credit for the peer is held back
41
+ while the stream needs to drain too, and sent as one frame after it, so a peer
42
+ that uploads and never reads cannot grow the buffer with credit frames either;
43
+ it is dropped after the same timeout.
34
44
  - **Error fidelity.** A handler's `Error` reaches the caller with `message`,
35
45
  `stack` and custom fields.
36
46
  - **Cancellation** in both directions: a caller's `.return()` ends the call at
@@ -70,9 +80,9 @@ runs (Node or browser).
70
80
  | `duplexOverStream(stream, input, options?)` | **Deprecated.** The deprecated protocol's primitive: runs one call over one libp2p `Stream`. Removed in the next release. |
71
81
  | `PeerGoneError` | What a call fails with when its stream ended before the call finished: the peer died, the connection dropped, or the stream was reset. `cause` is libp2p's close error, if any. |
72
82
  | `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. |
73
- | `libp2pPortMux(options)` | Native MessagePorts: a `webrun-rpc` `PortMux` (by shape) whose every port is its own libp2p stream, for `overPorts`; what `connect`/`serve` run on. `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. See "MessagePorts over libp2p" below. |
83
+ | `libp2pPortMux(options)` | Native MessagePorts: a `webrun-rpc` `PortMux` (by shape) whose every port is its own libp2p stream, for `overPorts`; what `connect`/`serve` run on. `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`, `drainTimeoutMs` (default `DEFAULT_DRAIN_TIMEOUT_MS`). 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. See "MessagePorts over libp2p" below. |
74
84
  | `DEFAULT_PORT_PROTOCOL`, `DEFAULT_PORT_MAX_MESSAGE_SIZE` | `"/webrun-ports/1.0.0"`, 256 KiB. |
75
- | `DEFAULT_DRAIN_TIMEOUT_MS` | **Deprecated** (the deprecated protocol only). `300_000` (5 minutes). |
85
+ | `DEFAULT_DRAIN_TIMEOUT_MS` | `300_000` (5 minutes): how long a write waits for a stream to drain before the stream is reset (`drainTimeoutMs`). |
76
86
  | `DEFAULT_CLOSE_TIMEOUT_MS` | **Deprecated** (the deprecated protocol only). `300_000` (5 minutes), the bound on a graceful close. |
77
87
  | `DEFAULT_MAX_MESSAGE_BYTES` | `67_108_864` (64 MiB), `messageTargetOverStream`'s cap on one message. |
78
88
  | `DEFAULT_MAX_FRAME_BYTES` | `67_108_864` (64 MiB), the default cap on one inbound frame (`maxFrameBytes`). |
@@ -85,19 +95,21 @@ runs (Node or browser).
85
95
  | `node` | `Libp2p` | required | The local node. Only `dialProtocol` is required; `getConnections` is used when present. |
86
96
  | `peer` | `PeerId \| Multiaddr` | required | Who to dial. |
87
97
  | `protocol` | `string` | `DEFAULT_PROTOCOL` | The protocol id both ends are configured with. Calls negotiate `portProtocolOf(protocol)`; `protocol` itself is the deprecated fallback. |
88
- | `window` | `number` | `DEFAULT_WINDOW` (4 MiB) | The port window in bytes, at least 64 KiB: the most this end holds of a response delivered and not yet read. See "Sizing the window". |
98
+ | `window` | `number` | `DEFAULT_WINDOW` (4 MiB) | The port window in bytes, at least 64 KiB: the most this end holds of a response delivered and not yet read, and the most it sends ahead of the server (min of both ends' windows). See "Sizing the window". |
89
99
  | `timeout` | `number` | none | Per-call inactivity timeout in ms, reset by any chunk in either direction. Unset, a slow consumer is throttled, never failed. |
90
100
  | `maxFrameBytes` | `number` | `DEFAULT_MAX_FRAME_BYTES` | The largest frame accepted from the peer. Past it, or on a malformed frame length, the call fails and the stream is aborted. |
91
101
  | `maxOutboundStreams` | `number` | libp2p's (64) | Passed to `dialProtocol`. |
92
102
  | `runOnLimitedConnection` | `boolean` | unset | Passed to `dialProtocol`. Allows relayed or otherwise limited connections. |
93
- | `drainTimeoutMs`, `readHighWaterBytes` | `number` | 5 min, 64 KiB | **Deprecated**: the deprecated protocol only (an older server). |
103
+ | `drainTimeoutMs` | `number` | `DEFAULT_DRAIN_TIMEOUT_MS` (5 min) | How long a write (or credit for the peer's upload) may wait for the stream to drain before the stream is reset and the call fails. Only what is held back for drain counts. Both protocols; on the port protocol 0 or `Infinity` means no bound (the deprecated protocol drops at once on 0). |
104
+ | `readHighWaterBytes` | `number` | 64 KiB | **Deprecated**: the deprecated protocol only (an older server). |
94
105
 
95
106
  `ServeLibp2pParams`: `node`, `protocol`, `window`, `timeout`, `maxFrameBytes`,
96
107
  `maxOutboundStreams` and `runOnLimitedConnection` as above (passed to
97
108
  `node.handle` for each served protocol), plus `maxInboundStreams` (libp2p
98
- default 32), and the deprecated `drainTimeoutMs` and `readHighWaterBytes`
99
- (older callers only). The server's `window` bounds what a call sends ahead of
100
- the handler.
109
+ default 32), `drainTimeoutMs` as above (a stalled caller is dropped after it),
110
+ and the deprecated `readHighWaterBytes` (older callers only). The server's
111
+ `window` bounds what a call sends ahead of the handler, and what a response
112
+ runs ahead of its caller, whatever window the caller announces.
101
113
 
102
114
  `ConnectionContext`: `{ remotePeer: PeerId }`.
103
115
  `ServeConnectionsHandler`: `(context: ConnectionContext) => Duplex`.
@@ -242,9 +254,11 @@ Behaviour that differs from the deprecated protocol:
242
254
  call ends: input the caller has not sent yet is abandoned, and a handler
243
255
  still reading its input is told the caller abandoned the stream. The
244
256
  deprecated protocol kept sending until the input ended too.
245
- - **No drain timeout.** A peer that stops reading holds at most a window per
246
- call; set `timeout` to fail idle calls. `drainTimeoutMs` and
247
- `readHighWaterBytes` apply to the deprecated protocol only.
257
+ - **The drain timeout counts only a blocked write.** A peer that stops
258
+ consuming holds at most a window per call (its credit stops); one that stops
259
+ reading at the transport holds about one frame, and is dropped after
260
+ `drainTimeoutMs`. Set `timeout` to fail idle calls. `readHighWaterBytes`
261
+ applies to the deprecated protocol only.
248
262
  - **Chunks.** A response arrives in chunks of up to 256 KiB (64 KiB before).
249
263
  - **Failures on the serving side are not logged**: they reach the handler
250
264
  (its input throws) and the caller.
@@ -35,9 +35,10 @@ export interface ConnectLibp2pParams {
35
35
  /**
36
36
  * The port window in bytes (default {@link DEFAULT_WINDOW}, at least 64 KiB):
37
37
  * the most this end holds of a call's response delivered and not yet read.
38
- * The server's window bounds what a call sends ahead of it. Size it at
39
- * least to the bandwidth-delay product of the link (see
40
- * {@link DEFAULT_WINDOW}).
38
+ * It is also the most this end sends ahead of the server: a call's request
39
+ * runs at most min(the server's window, this window) ahead
40
+ * (statewalker/webrun-wire#195). Size it at least to the bandwidth-delay
41
+ * product of the link (see {@link DEFAULT_WINDOW}).
41
42
  */
42
43
  window?: number;
43
44
  /**
@@ -67,11 +68,13 @@ export interface ConnectLibp2pParams {
67
68
  */
68
69
  runOnLimitedConnection?: boolean;
69
70
  /**
70
- * The deprecated protocol only (an older server): how long to wait for a
71
- * backpressured stream to drain before dropping the peer; defaults to
72
- * `DEFAULT_DRAIN_TIMEOUT_MS` (5 minutes).
73
- *
74
- * @deprecated Removed with the deprecated protocol, in the next release.
71
+ * How long, in ms, a write may wait for a call's stream to drain (the
72
+ * server stopped reading at the transport) before the stream is reset and
73
+ * the call fails; defaults to `DEFAULT_DRAIN_TIMEOUT_MS` (5 minutes). Only
74
+ * a write blocked on drain counts: a quiet call is never timed out. Applies
75
+ * to the port protocol (statewalker/webrun-wire#195) and to the deprecated
76
+ * one. On the port protocol, 0 or `Infinity` means no bound (the deprecated
77
+ * protocol drops a backpressured stream at once on 0).
75
78
  */
76
79
  drainTimeoutMs?: number;
77
80
  /**
@@ -93,7 +96,10 @@ export interface ServeLibp2pParams {
93
96
  /**
94
97
  * The port window in bytes (default {@link DEFAULT_WINDOW}, at least 64 KiB):
95
98
  * the most this end holds of a call's request delivered and not yet read by
96
- * the handler. See {@link ConnectLibp2pParams.window}.
99
+ * the handler. It is also the most a response runs ahead of its caller,
100
+ * whatever window the caller announces (up to 4 GiB): min(the caller's
101
+ * window, this window) (statewalker/webrun-wire#195). See
102
+ * {@link ConnectLibp2pParams.window}.
97
103
  */
98
104
  window?: number;
99
105
  /** Per-call inactivity timeout in ms; see {@link ConnectLibp2pParams.timeout}. */
@@ -121,10 +127,20 @@ export interface ServeLibp2pParams {
121
127
  */
122
128
  runOnLimitedConnection?: boolean;
123
129
  /**
124
- * The deprecated protocol only (older callers): how long to wait for a
125
- * backpressured stream to drain before dropping the peer (default 5 minutes).
126
- *
127
- * @deprecated Removed with the deprecated protocol, in the next release.
130
+ * How long, in ms, a write may wait for a call's stream to drain (the
131
+ * caller stopped reading at the transport) before the stream is reset and
132
+ * the handler's output is abandoned; defaults to `DEFAULT_DRAIN_TIMEOUT_MS`
133
+ * (5 minutes). This bounds what a stalled caller holds on the server: with
134
+ * the window (at most min(the caller's window, `window`) ahead) and the
135
+ * stream's backpressure (nothing written while it needs to drain), a
136
+ * stalled call holds about one frame, for at most this long. Only a write
137
+ * blocked on drain counts: a quiet call (an event stream between events) is
138
+ * never timed out. Credit for the caller's upload is held back while the
139
+ * stream needs to drain, and counts too (a caller that uploads and never
140
+ * reads is dropped). Applies to the port protocol
141
+ * (statewalker/webrun-wire#195) and to the deprecated one. On the port
142
+ * protocol, 0 or `Infinity` means no bound (the deprecated protocol drops a
143
+ * backpressured stream at once on 0).
128
144
  */
129
145
  drainTimeoutMs?: number;
130
146
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"connect-serve.d.ts","sourceRoot":"","sources":["../src/connect-serve.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,MAAM,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAGzD,OAAO,KAAK,EAAE,OAAO,EAAU,KAAK,EAAc,MAAM,6BAA6B,CAAC;AAGtF,OAAO,EACL,KAAK,iBAAiB,EACtB,gBAAgB,EAChB,KAAK,uBAAuB,EAC7B,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,KAAK,iBAAiB,EAAE,gBAAgB,EAAE,KAAK,uBAAuB,EAAE,CAAC;AAElF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,cAAc,QAAkB,CAAC;AAE9C;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEvD;AAKD,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IACzB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;;OAIG;IACH,a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AC,EAAE,MAAM,CAAC;IACvB;;;;;;OAMG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B;;;;OAIG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;IACjC;;;;;;;;;;;;;;;OAeG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;;;OAKG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,OAAO,EAAE,OAAO,CAAC,mBAAmB,CAqDhD,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,KAAK,EAAE,KAAK,CAAC,iBAAiB,CACF,CAAC;AAE1C;;;GAGG;AACH,wBAAsB,gBAAgB,CACpC,MAAM,EAAE,iBAAiB,EACzB,WAAW,EAAE,uBAAuB,GACnC,OAAO,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,CA0E9B"}
@@ -15,8 +15,9 @@ import type { Stream } from "@libp2p/interface";
15
15
  * indistinguishable from one that has stopped reading altogether. Override via
16
16
  * `drainTimeoutMs` when a deployment knows better.
17
17
  *
18
- * @deprecated The deprecated protocol's (statewalker/webrun-wire#153): kept for
19
- * one release, as the shim `connect`/`serve` run for older peers, then removed.
18
+ * Also the default stall bound of the port protocol (`connect`, `serve`,
19
+ * `serveConnections`, `libp2pPortMux`): a write on a call's stream that has
20
+ * waited for drain this long resets the stream (statewalker/webrun-wire#195).
20
21
  */
21
22
  export declare const DEFAULT_DRAIN_TIMEOUT_MS = 300000;
22
23
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"duplex-over-stream.d.ts","sourceRoot":"","sources":["../src/duplex-over-stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAoB,MAAM,mBAAmB,CAAC;AAMlE;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,wBAAwB,SAAU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IAAI,CAGvB;CACF;AAED;;;GAGG;AACH,wBAAgB,UAAU,CACxB,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,MAAM,GACX;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CAO/C;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,OAAO,GAAG,UAAU,CAOxD;AA8BD,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAYtD;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAa9F"}
package/dist/index.js CHANGED
@@ -20,8 +20,9 @@ const TYPE_ERROR = 2;
20
20
  * indistinguishable from one that has stopped reading altogether. Override via
21
21
  * `drainTimeoutMs` when a deployment knows better.
22
22
  *
23
- * @deprecated The deprecated protocol's (statewalker/webrun-wire#153): kept for
24
- * one release, as the shim `connect`/`serve` run for older peers, then removed.
23
+ * Also the default stall bound of the port protocol (`connect`, `serve`,
24
+ * `serveConnections`, `libp2pPortMux`): a write on a call's stream that has
25
+ * waited for drain this long resets the stream (statewalker/webrun-wire#195).
25
26
  */
26
27
  const DEFAULT_DRAIN_TIMEOUT_MS = 3e5;
27
28
  /**
@@ -775,8 +776,9 @@ const DEFAULT_MAX_MESSAGE_BYTES = 67108864;
775
776
  * as on a `MessagePort`. Every message the peer sent before closing is
776
777
  * delivered before `signal` aborts. `postMessage` hands the frame to
777
778
  * `stream.send()` and does not wait for room; libp2p buffers up to the
778
- * stream's `maxWriteBufferLength`, past which it resets the stream. A sender
779
- * that must wait awaits `whenDrained()`.
779
+ * stream's `maxWriteBufferLength` (unbounded with yamux), past which it
780
+ * resets the stream. A sender that must wait checks `needsDrain` and awaits
781
+ * `whenDrained()`, as a windowed native port does.
780
782
  */
781
783
  function messageTargetOverStream(stream, { maxMessageBytes = DEFAULT_MAX_MESSAGE_BYTES } = {}) {
782
784
  const listeners = /* @__PURE__ */ new Set();
@@ -853,6 +855,9 @@ function messageTargetOverStream(stream, { maxMessageBytes = DEFAULT_MAX_MESSAGE
853
855
  get bufferedAmount() {
854
856
  return stream.writeBufferLength;
855
857
  },
858
+ get needsDrain() {
859
+ return stream.writableNeedsDrain === true && !life.signal.aborted;
860
+ },
856
861
  whenDrained() {
857
862
  if (!stream.writableNeedsDrain || life.signal.aborted) return Promise.resolve();
858
863
  return new Promise((resolve) => {
@@ -930,6 +935,7 @@ async function openLibp2pPortMux(options, streamLimit) {
930
935
  const protocol = options.protocol ?? "/webrun-ports/1.0.0";
931
936
  const maxMessageSize = options.maxMessageSize ?? 262144;
932
937
  const { window } = options;
938
+ const drainTimeoutMs = options.drainTimeoutMs ?? 3e5;
933
939
  const windowProtocol = windowProtocolOf(protocol);
934
940
  /**
935
941
  * A stream as a channel: framed bytes, and a reset to break it off (the peer hears it). The
@@ -950,6 +956,7 @@ async function openLibp2pPortMux(options, streamLimit) {
950
956
  maxMessageSize,
951
957
  onPort,
952
958
  window,
959
+ drainTimeoutMs,
953
960
  async open(signal) {
954
961
  if (!peer) throw new Error("webrun-streams-libp2p: openPort needs `peer`");
955
962
  const stream = await openGatedStream(node, peer, window !== void 0 ? [windowProtocol, protocol] : protocol, {
@@ -1036,6 +1043,7 @@ const connect = async (params) => {
1036
1043
  codec,
1037
1044
  protocol: portProtocolOf(protocol),
1038
1045
  window: params.window ?? 4194304,
1046
+ drainTimeoutMs: params.drainTimeoutMs,
1039
1047
  maxMessageBytes: maxFrameBytes,
1040
1048
  maxOutboundStreams,
1041
1049
  runOnLimitedConnection
@@ -1089,6 +1097,7 @@ async function serveConnections(params, makeHandler) {
1089
1097
  codec,
1090
1098
  protocol: portProtocolOf(protocol),
1091
1099
  window: params.window ?? 4194304,
1100
+ drainTimeoutMs: params.drainTimeoutMs,
1092
1101
  maxMessageBytes: maxFrameBytes,
1093
1102
  maxInboundStreams,
1094
1103
  maxOutboundStreams,
@@ -19,6 +19,13 @@ export interface StreamMessageTarget {
19
19
  * closed; at once when it can now.
20
20
  */
21
21
  whenDrained(): Promise<void>;
22
+ /**
23
+ * Whether the stream needs to drain before it takes more (libp2p's
24
+ * `writableNeedsDrain`: the muxer's send window is spent and bytes are
25
+ * piling up in the write buffer). A windowed native port sends nothing
26
+ * while it is `true` (statewalker/webrun-wire#195).
27
+ */
28
+ readonly needsDrain: boolean;
22
29
  /** `signal` again, on the target: `webrun-rpc`'s `getPortCloseSignal` reads it from a port. */
23
30
  readonly closeSignal: AbortSignal;
24
31
  };
@@ -57,8 +64,9 @@ export declare const DEFAULT_MAX_MESSAGE_BYTES: number;
57
64
  * as on a `MessagePort`. Every message the peer sent before closing is
58
65
  * delivered before `signal` aborts. `postMessage` hands the frame to
59
66
  * `stream.send()` and does not wait for room; libp2p buffers up to the
60
- * stream's `maxWriteBufferLength`, past which it resets the stream. A sender
61
- * that must wait awaits `whenDrained()`.
67
+ * stream's `maxWriteBufferLength` (unbounded with yamux), past which it
68
+ * resets the stream. A sender that must wait checks `needsDrain` and awaits
69
+ * `whenDrained()`, as a windowed native port does.
62
70
  */
63
71
  export declare function messageTargetOverStream(stream: Stream, { maxMessageBytes }?: MessageTargetOverStreamOptions): StreamMessageTarget;
64
72
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"message-target-over-stream.d.ts","sourceRoot":"","sources":["../src/message-target-over-stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAoB,MAAM,mBAAmB,CAAC;AAUlE,KAAK,QAAQ,GAAG,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE9D,uEAAuE;AACvE,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,MAAM,EAAE;QACN,gBAAgB,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,GAAG,IAAI,CA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AAoE;QACpE,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;QACpC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;QACvB,2CAA2C;QAC3C,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;QAChC;;;WAGG;QACH,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;QAC7B;;;;;WAKG;QACH,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;QAC7B,+FAA+F;QAC/F,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;KACnC,CAAC;IACF;;;;;;;OAOG;IACH,MAAM,EAAE,WAAW,CAAC;CACrB;AAED,mDAAmD;AACnD,MAAM,WAAW,8BAA8B;IAC7C;;;;;OAKG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,6EAA6E;AAC7E,eAAO,MAAM,yBAAyB,QAAmB,CAAC;AAE1D;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,MAAM,EACd,EAAE,eAA2C,EAAE,GAAE,8BAAmC,GACnF,mBAAmB,CAsHrB"}
@@ -65,8 +65,22 @@ export interface Libp2pPortMuxOptions {
65
65
  * negotiates `protocol` and runs as before. 1-4 MiB is a reasonable range:
66
66
  * the most one call moves per round trip, and the most a producer runs
67
67
  * ahead of a consumer that stopped reading.
68
+ *
69
+ * A windowed port never runs further ahead than min(the peer's window,
70
+ * this window), and sends nothing while its stream needs to drain
71
+ * (statewalker/webrun-wire#195).
68
72
  */
69
73
  window?: number;
74
+ /**
75
+ * How long, in ms, a write on a windowed port may wait for its stream to
76
+ * drain (a peer that stopped reading at the transport) before the stream
77
+ * is reset and the port fails. Only a write blocked on drain counts: a
78
+ * quiet call (an event stream between events) is never timed out. Credit
79
+ * for the peer is held back while the stream needs to drain, and counts
80
+ * too. Default {@link DEFAULT_DRAIN_TIMEOUT_MS} (5 minutes); 0 or `Infinity`:
81
+ * no bound.
82
+ */
83
+ drainTimeoutMs?: number;
70
84
  }
71
85
  /** A `webrun-rpc` `PortMux` by shape; the port mux imports nothing from it. */
72
86
  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,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEzD;AAED;;;;GAIG;AACH,eAAO,MAAM,6BAA6B,QAAa,CAAC;AAExD;;;GAGG;AACH,eAAO,MAAM,yBAAyB,OAAO,CAAC;AAE9C;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG,UAAU,CAAC;AAEpC,iEAAiE;AACjE,MAAM,WAAW,eAAe;IAC9B,sFAAsF;IACtF,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,UAAU,CAAC;CACxB;AAED,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,uFAAuF;IACvF,KAAK,EAAE,UAAU,CAAC;IAClB,yFAAyF;IACzF,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,uFAAuF;IACvF,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1B;;;OAGG;IACH,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,eAAe,KAAK,OAAO,GAAG,SAAS,CAAC;IAC1E,mFAAmF;IACnF,cAAc,CAAC,EAAE,MAAM,CA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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,CAEzF;AAED;;;;GAIG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,oBAAoB,EAC7B,WAAW,EAAE,MAAM,GAAG,SAAS,GAC9B,OAAO,CAAC,aAAa,CAAC,CAwExB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statewalker/webrun-streams-libp2p",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "libp2p native multi-stream Connect/Serve adapter in the webrun-streams-* family",
@@ -30,9 +30,9 @@
30
30
  "src"
31
31
  ],
32
32
  "dependencies": {
33
- "@statewalker/webrun-rpc": "^0.15.0",
34
- "@statewalker/webrun-msgpack": "^0.14.0",
35
- "@statewalker/webrun-streams": "^0.6.0"
33
+ "@statewalker/webrun-msgpack": "^0.14.1",
34
+ "@statewalker/webrun-streams": "^0.6.1",
35
+ "@statewalker/webrun-rpc": "^0.15.1"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "@libp2p/interface": "^3.0.0",
@@ -60,7 +60,7 @@
60
60
  "typescript": "^7.0.2",
61
61
  "vitest": "^5.0.5",
62
62
  "webrun-streams-libp2p-0-6": "npm:@statewalker/webrun-streams-libp2p@0.6.0",
63
- "@statewalker/webrun-streams-conformance": "^0.7.0"
63
+ "@statewalker/webrun-streams-conformance": "^0.7.1"
64
64
  },
65
65
  "sideEffects": false,
66
66
  "publishConfig": {
@@ -52,9 +52,10 @@ export interface ConnectLibp2pParams {
52
52
  /**
53
53
  * The port window in bytes (default {@link DEFAULT_WINDOW}, at least 64 KiB):
54
54
  * the most this end holds of a call's response delivered and not yet read.
55
- * The server's window bounds what a call sends ahead of it. Size it at
56
- * least to the bandwidth-delay product of the link (see
57
- * {@link DEFAULT_WINDOW}).
55
+ * It is also the most this end sends ahead of the server: a call's request
56
+ * runs at most min(the server's window, this window) ahead
57
+ * (statewalker/webrun-wire#195). Size it at least to the bandwidth-delay
58
+ * product of the link (see {@link DEFAULT_WINDOW}).
58
59
  */
59
60
  window?: number;
60
61
  /**
@@ -84,11 +85,13 @@ export interface ConnectLibp2pParams {
84
85
  */
85
86
  runOnLimitedConnection?: boolean;
86
87
  /**
87
- * The deprecated protocol only (an older server): how long to wait for a
88
- * backpressured stream to drain before dropping the peer; defaults to
89
- * `DEFAULT_DRAIN_TIMEOUT_MS` (5 minutes).
90
- *
91
- * @deprecated Removed with the deprecated protocol, in the next release.
88
+ * How long, in ms, a write may wait for a call's stream to drain (the
89
+ * server stopped reading at the transport) before the stream is reset and
90
+ * the call fails; defaults to `DEFAULT_DRAIN_TIMEOUT_MS` (5 minutes). Only
91
+ * a write blocked on drain counts: a quiet call is never timed out. Applies
92
+ * to the port protocol (statewalker/webrun-wire#195) and to the deprecated
93
+ * one. On the port protocol, 0 or `Infinity` means no bound (the deprecated
94
+ * protocol drops a backpressured stream at once on 0).
92
95
  */
93
96
  drainTimeoutMs?: number;
94
97
  /**
@@ -111,7 +114,10 @@ export interface ServeLibp2pParams {
111
114
  /**
112
115
  * The port window in bytes (default {@link DEFAULT_WINDOW}, at least 64 KiB):
113
116
  * the most this end holds of a call's request delivered and not yet read by
114
- * the handler. See {@link ConnectLibp2pParams.window}.
117
+ * the handler. It is also the most a response runs ahead of its caller,
118
+ * whatever window the caller announces (up to 4 GiB): min(the caller's
119
+ * window, this window) (statewalker/webrun-wire#195). See
120
+ * {@link ConnectLibp2pParams.window}.
115
121
  */
116
122
  window?: number;
117
123
  /** Per-call inactivity timeout in ms; see {@link ConnectLibp2pParams.timeout}. */
@@ -139,10 +145,20 @@ export interface ServeLibp2pParams {
139
145
  */
140
146
  runOnLimitedConnection?: boolean;
141
147
  /**
142
- * The deprecated protocol only (older callers): how long to wait for a
143
- * backpressured stream to drain before dropping the peer (default 5 minutes).
144
- *
145
- * @deprecated Removed with the deprecated protocol, in the next release.
148
+ * How long, in ms, a write may wait for a call's stream to drain (the
149
+ * caller stopped reading at the transport) before the stream is reset and
150
+ * the handler's output is abandoned; defaults to `DEFAULT_DRAIN_TIMEOUT_MS`
151
+ * (5 minutes). This bounds what a stalled caller holds on the server: with
152
+ * the window (at most min(the caller's window, `window`) ahead) and the
153
+ * stream's backpressure (nothing written while it needs to drain), a
154
+ * stalled call holds about one frame, for at most this long. Only a write
155
+ * blocked on drain counts: a quiet call (an event stream between events) is
156
+ * never timed out. Credit for the caller's upload is held back while the
157
+ * stream needs to drain, and counts too (a caller that uploads and never
158
+ * reads is dropped). Applies to the port protocol
159
+ * (statewalker/webrun-wire#195) and to the deprecated one. On the port
160
+ * protocol, 0 or `Infinity` means no bound (the deprecated protocol drops a
161
+ * backpressured stream at once on 0).
146
162
  */
147
163
  drainTimeoutMs?: number;
148
164
  /**
@@ -179,6 +195,7 @@ export const connect: Connect<ConnectLibp2pParams> = async (params) => {
179
195
  codec,
180
196
  protocol: portProtocolOf(protocol),
181
197
  window: params.window ?? DEFAULT_WINDOW,
198
+ drainTimeoutMs: params.drainTimeoutMs,
182
199
  maxMessageBytes: maxFrameBytes,
183
200
  maxOutboundStreams,
184
201
  runOnLimitedConnection,
@@ -257,6 +274,7 @@ export async function serveConnections(
257
274
  codec,
258
275
  protocol: portProtocolOf(protocol),
259
276
  window: params.window ?? DEFAULT_WINDOW,
277
+ drainTimeoutMs: params.drainTimeoutMs,
260
278
  maxMessageBytes: maxFrameBytes,
261
279
  maxInboundStreams,
262
280
  maxOutboundStreams,
@@ -20,8 +20,9 @@ const TYPE_ERROR = 0x02;
20
20
  * indistinguishable from one that has stopped reading altogether. Override via
21
21
  * `drainTimeoutMs` when a deployment knows better.
22
22
  *
23
- * @deprecated The deprecated protocol's (statewalker/webrun-wire#153): kept for
24
- * one release, as the shim `connect`/`serve` run for older peers, then removed.
23
+ * Also the default stall bound of the port protocol (`connect`, `serve`,
24
+ * `serveConnections`, `libp2pPortMux`): a write on a call's stream that has
25
+ * waited for drain this long resets the stream (statewalker/webrun-wire#195).
25
26
  */
26
27
  export const DEFAULT_DRAIN_TIMEOUT_MS = 300_000;
27
28
 
@@ -29,6 +29,13 @@ export interface StreamMessageTarget {
29
29
  * closed; at once when it can now.
30
30
  */
31
31
  whenDrained(): Promise<void>;
32
+ /**
33
+ * Whether the stream needs to drain before it takes more (libp2p's
34
+ * `writableNeedsDrain`: the muxer's send window is spent and bytes are
35
+ * piling up in the write buffer). A windowed native port sends nothing
36
+ * while it is `true` (statewalker/webrun-wire#195).
37
+ */
38
+ readonly needsDrain: boolean;
32
39
  /** `signal` again, on the target: `webrun-rpc`'s `getPortCloseSignal` reads it from a port. */
33
40
  readonly closeSignal: AbortSignal;
34
41
  };
@@ -70,8 +77,9 @@ export const DEFAULT_MAX_MESSAGE_BYTES = 64 * 1024 * 1024;
70
77
  * as on a `MessagePort`. Every message the peer sent before closing is
71
78
  * delivered before `signal` aborts. `postMessage` hands the frame to
72
79
  * `stream.send()` and does not wait for room; libp2p buffers up to the
73
- * stream's `maxWriteBufferLength`, past which it resets the stream. A sender
74
- * that must wait awaits `whenDrained()`.
80
+ * stream's `maxWriteBufferLength` (unbounded with yamux), past which it
81
+ * resets the stream. A sender that must wait checks `needsDrain` and awaits
82
+ * `whenDrained()`, as a windowed native port does.
75
83
  */
76
84
  export function messageTargetOverStream(
77
85
  stream: Stream,
@@ -177,6 +185,9 @@ export function messageTargetOverStream(
177
185
  get bufferedAmount() {
178
186
  return stream.writeBufferLength;
179
187
  },
188
+ get needsDrain() {
189
+ return stream.writableNeedsDrain === true && !life.signal.aborted;
190
+ },
180
191
  whenDrained() {
181
192
  if (!stream.writableNeedsDrain || life.signal.aborted) return Promise.resolve();
182
193
  return new Promise<void>((resolve) => {
package/src/port-mux.ts CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  newNativePortMux,
14
14
  type ValueCodec,
15
15
  } from "@statewalker/webrun-streams";
16
+ import { DEFAULT_DRAIN_TIMEOUT_MS } from "./duplex-over-stream.js";
16
17
  import { messageTargetOverStream } from "./message-target-over-stream.js";
17
18
  import { openGatedStream } from "./stream-gate.js";
18
19
 
@@ -89,8 +90,22 @@ export interface Libp2pPortMuxOptions {
89
90
  * negotiates `protocol` and runs as before. 1-4 MiB is a reasonable range:
90
91
  * the most one call moves per round trip, and the most a producer runs
91
92
  * ahead of a consumer that stopped reading.
93
+ *
94
+ * A windowed port never runs further ahead than min(the peer's window,
95
+ * this window), and sends nothing while its stream needs to drain
96
+ * (statewalker/webrun-wire#195).
92
97
  */
93
98
  window?: number;
99
+ /**
100
+ * How long, in ms, a write on a windowed port may wait for its stream to
101
+ * drain (a peer that stopped reading at the transport) before the stream
102
+ * is reset and the port fails. Only a write blocked on drain counts: a
103
+ * quiet call (an event stream between events) is never timed out. Credit
104
+ * for the peer is held back while the stream needs to drain, and counts
105
+ * too. Default {@link DEFAULT_DRAIN_TIMEOUT_MS} (5 minutes); 0 or `Infinity`:
106
+ * no bound.
107
+ */
108
+ drainTimeoutMs?: number;
94
109
  }
95
110
 
96
111
  /** A `webrun-rpc` `PortMux` by shape; the port mux imports nothing from it. */
@@ -138,6 +153,7 @@ export async function openLibp2pPortMux(
138
153
  const maxMessageSize = options.maxMessageSize ?? DEFAULT_PORT_MAX_MESSAGE_SIZE;
139
154
  // Checked by the shared lifecycle, before the protocols are handled.
140
155
  const { window } = options;
156
+ const drainTimeoutMs = options.drainTimeoutMs ?? DEFAULT_DRAIN_TIMEOUT_MS;
141
157
  const windowProtocol = windowProtocolOf(protocol);
142
158
 
143
159
  /**
@@ -164,6 +180,7 @@ export async function openLibp2pPortMux(
164
180
  maxMessageSize,
165
181
  onPort,
166
182
  window,
183
+ drainTimeoutMs,
167
184
  async open(signal) {
168
185
  if (!peer) throw new Error("webrun-streams-libp2p: openPort needs `peer`");
169
186
  const protocols = window !== undefined ? [windowProtocol, protocol] : protocol;