@statewalker/webrun-streams-libp2p 0.6.0 → 0.7.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.
@@ -1,47 +1,77 @@
1
- import type {
2
- Connection,
3
- DialProtocolOptions,
4
- Libp2p,
5
- PeerId,
6
- Stream,
7
- StreamHandlerOptions,
8
- } from "@libp2p/interface";
1
+ import type { Connection, Libp2p, PeerId } from "@libp2p/interface";
9
2
  import type { Multiaddr } from "@multiformats/multiaddr";
10
- import type { Connect, Duplex, Serve } from "@statewalker/webrun-streams";
3
+ import { deserialize, serialize } from "@statewalker/webrun-msgpack";
4
+ import { overPorts, connect as portConnect, serve as portServe } from "@statewalker/webrun-rpc";
5
+ import type { Connect, Duplex, Serve, ValueCodec } from "@statewalker/webrun-streams";
6
+ import { legacyConnect, legacyServeConnections } from "./legacy-connect-serve.js";
7
+ import { type InboundPortInfo, openLibp2pPortMux } from "./port-mux.js";
11
8
  import {
12
- closeStream,
13
- DEFAULT_READ_HIGH_WATER_BYTES,
14
- duplexOverStream,
15
- PeerGoneError,
16
- } from "./duplex-over-stream.js";
17
- import { openGatedStream } from "./stream-gate.js";
9
+ type ConnectionContext,
10
+ DEFAULT_PROTOCOL,
11
+ type ServeConnectionsHandler,
12
+ } from "./shared.js";
18
13
 
19
- export const DEFAULT_PROTOCOL = "/webrun-streams/1.0.0";
14
+ export { type ConnectionContext, DEFAULT_PROTOCOL, type ServeConnectionsHandler };
15
+
16
+ /**
17
+ * 4 MiB: the port window `connect`/`serve` grant unless told otherwise — the
18
+ * most one call moves per round trip, and the most a producer runs ahead of a
19
+ * consumer that stopped reading (statewalker/webrun-wire#153).
20
+ *
21
+ * A call moves at most `window / RTT`, so the window should be at least the
22
+ * link's bandwidth-delay product: 4 MiB covers 200 Mbit/s at 100 ms
23
+ * (2.5 MB). Raise it for faster or longer links; lower it (not below 64 KiB)
24
+ * to bound memory per call more tightly where the BDP is small — a window far
25
+ * above the bottleneck's queue only adds latency (bufferbloat).
26
+ */
27
+ export const DEFAULT_WINDOW = 4 * 1024 * 1024;
28
+
29
+ /**
30
+ * The protocol a call's stream negotiates for `connect`/`serve` configured
31
+ * with `protocol`: `protocol` with `/ports` appended, and `/window` after it
32
+ * when both ends run the window (always, between two releases from this one
33
+ * on). `protocol` itself is the deprecated protocol, still served, and dialled
34
+ * when a peer refuses this one.
35
+ */
36
+ export function portProtocolOf(protocol: string): string {
37
+ return `${protocol}/ports`;
38
+ }
39
+
40
+ /** What a call's messages are on the wire: msgpack, as `webrun-rpc`'s stream tier posts them. */
41
+ const codec: ValueCodec = { encode: serialize, decode: deserialize };
20
42
 
21
43
  export interface ConnectLibp2pParams {
22
44
  node: Libp2p;
23
45
  peer: PeerId | Multiaddr;
24
- /** libp2p protocol id; defaults to `/webrun-streams/1.0.0`. */
46
+ /**
47
+ * The protocol id both ends are configured with; defaults to
48
+ * {@link DEFAULT_PROTOCOL}. Calls negotiate {@link portProtocolOf} it, and
49
+ * fall back to it (the deprecated protocol) against an older server.
50
+ */
25
51
  protocol?: string;
26
52
  /**
27
- * How long to wait for a backpressured stream to drain before dropping the
28
- * peer; defaults to `DEFAULT_DRAIN_TIMEOUT_MS` (5 minutes).
53
+ * The port window in bytes (default {@link DEFAULT_WINDOW}, at least 64 KiB):
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}).
29
58
  */
30
- drainTimeoutMs?: number;
59
+ window?: number;
31
60
  /**
32
- * Response bytes held while the caller is not reading before the stream is
33
- * paused; see `DuplexOverStreamOptions.readHighWaterBytes` (default 64 KiB).
61
+ * Per-call inactivity timeout in ms, reset by any chunk in either direction
62
+ * (`webrun-rpc`'s `timeout`). Unset, the default, means none: a slow
63
+ * consumer is throttled, never failed.
34
64
  */
35
- readHighWaterBytes?: number;
65
+ timeout?: number;
36
66
  /**
37
- * The largest frame payload accepted from the peer; past it, or on a
38
- * malformed frame header, the call fails and the stream is aborted. See
39
- * `DuplexOverStreamOptions.maxFrameBytes` (default 64 MiB).
67
+ * The largest frame accepted from the peer on a call's stream; past it, or
68
+ * on a malformed frame header, the call fails and the stream is aborted.
69
+ * Default 64 MiB.
40
70
  */
41
71
  maxFrameBytes?: number;
42
72
  /**
43
- * How many outgoing streams for this protocol libp2p allows to be open at
44
- * the same time on one connection; passed through to `node.dialProtocol`.
73
+ * How many outgoing streams for the calls' protocol libp2p allows to be open
74
+ * at the same time on one connection; passed through to `node.dialProtocol`.
45
75
  * Left unset, libp2p's own default (64) applies.
46
76
  */
47
77
  maxOutboundStreams?: number;
@@ -49,384 +79,349 @@ export interface ConnectLibp2pParams {
49
79
  * Opt-in to dialing over a connection with limits on how much data can be
50
80
  * transferred or how long it can be open for (e.g. a relayed circuit);
51
81
  * passed through to `node.dialProtocol`. Left unset, libp2p refuses to open
52
- * this protocol's stream on such a connection — this package does not
53
- * decide that trade-off for the caller, it only exposes the knob.
82
+ * the calls' streams on such a connection — this package does not decide
83
+ * that trade-off for the caller, it only exposes the knob.
54
84
  */
55
85
  runOnLimitedConnection?: boolean;
56
- }
57
-
58
- export interface ServeLibp2pParams {
59
- node: Libp2p;
60
- /** libp2p protocol id; defaults to `/webrun-streams/1.0.0`. */
61
- protocol?: string;
62
86
  /**
63
- * How long to wait for a backpressured stream to drain before dropping the
64
- * peer; defaults to `DEFAULT_DRAIN_TIMEOUT_MS` (5 minutes). This is the only
65
- * bound on a peer that requests something and then stops reading without
66
- * closing, since the caller-side `.return`/abort escape does not exist here.
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.
67
92
  */
68
93
  drainTimeoutMs?: number;
69
94
  /**
70
- * Request bytes held while the handler is not reading before the stream is
71
- * paused; see `DuplexOverStreamOptions.readHighWaterBytes` (default 64 KiB).
95
+ * The deprecated protocol only (an older server): response bytes held while
96
+ * the caller is not reading before the stream is paused (default 64 KiB).
97
+ *
98
+ * @deprecated Removed with the deprecated protocol, in the next release.
72
99
  */
73
100
  readHighWaterBytes?: number;
101
+ }
102
+
103
+ export interface ServeLibp2pParams {
104
+ node: Libp2p;
74
105
  /**
75
- * The largest frame payload accepted from the peer; past it, or on a
76
- * malformed frame header, the call fails and the stream is aborted. See
77
- * `DuplexOverStreamOptions.maxFrameBytes` (default 64 MiB).
106
+ * The protocol id both ends are configured with; defaults to
107
+ * {@link DEFAULT_PROTOCOL}. Calls arrive on {@link portProtocolOf} it; the
108
+ * id itself is served too, for older callers (the deprecated protocol).
78
109
  */
110
+ protocol?: string;
111
+ /**
112
+ * The port window in bytes (default {@link DEFAULT_WINDOW}, at least 64 KiB):
113
+ * the most this end holds of a call's request delivered and not yet read by
114
+ * the handler. See {@link ConnectLibp2pParams.window}.
115
+ */
116
+ window?: number;
117
+ /** Per-call inactivity timeout in ms; see {@link ConnectLibp2pParams.timeout}. */
118
+ timeout?: number;
119
+ /** The largest frame accepted from the peer; see {@link ConnectLibp2pParams.maxFrameBytes}. */
79
120
  maxFrameBytes?: number;
80
121
  /**
81
- * How many incoming streams for this protocol libp2p allows to be open at
82
- * the same time on one connection; passed through to `node.handle`. Left
83
- * unset, libp2p's own default (32) applies — past it, a new inbound stream
84
- * is reset rather than queued, so a caller that opens one stream per
85
- * in-flight request will start seeing rejected calls above that count.
122
+ * How many incoming streams for each served protocol libp2p allows to be
123
+ * open at the same time on one connection; passed through to
124
+ * `node.handle`. Left unset, libp2p's own default (32) applies — past it, a
125
+ * new inbound stream is reset rather than queued, so a caller with more
126
+ * calls in flight starts seeing rejected calls above that count.
86
127
  */
87
128
  maxInboundStreams?: number;
88
129
  /**
89
- * How many outgoing streams for this protocol libp2p allows to be open at
90
- * the same time on one connection; passed through to `node.handle`. Left
91
- * unset, libp2p's own default (64) applies.
130
+ * How many outgoing streams for each served protocol libp2p allows to be
131
+ * open at the same time on one connection; passed through to `node.handle`.
132
+ * Left unset, libp2p's own default (64) applies.
92
133
  */
93
134
  maxOutboundStreams?: number;
94
135
  /**
95
- * Opt-in to accepting streams for this protocol over a connection with
96
- * limits on how much data can be transferred or how long it can be open
97
- * for (e.g. a relayed circuit); passed through to `node.handle`. Left
98
- * unset, libp2p refuses to open this protocol's stream on such a
99
- * connection — this package does not decide that trade-off for the
100
- * consumer, it only exposes the knob.
136
+ * Opt-in to accepting calls over a connection with limits on how much data
137
+ * can be transferred or how long it can be open for (e.g. a relayed
138
+ * circuit); passed through to `node.handle`.
101
139
  */
102
140
  runOnLimitedConnection?: boolean;
141
+ /**
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.
146
+ */
147
+ drainTimeoutMs?: number;
148
+ /**
149
+ * The deprecated protocol only (older callers): request bytes held while the
150
+ * handler is not reading before the stream is paused (default 64 KiB).
151
+ *
152
+ * @deprecated Removed with the deprecated protocol, in the next release.
153
+ */
154
+ readHighWaterBytes?: number;
103
155
  }
104
156
 
105
157
  /**
106
- * Builds the options object passed to `node.dialProtocol`/`node.handle`,
107
- * omitting any field the caller didn't supply rather than setting it to
108
- * `undefined` — so an unset field falls back to libp2p's own default instead
109
- * of an explicit `undefined` overriding it.
110
- */
111
- function definedOptions<T extends Record<string, unknown>>(fields: T): Partial<T> | undefined {
112
- const options: Partial<T> = {};
113
- let any = false;
114
- for (const key of Object.keys(fields) as (keyof T)[]) {
115
- const value = fields[key];
116
- if (value !== undefined) {
117
- options[key] = value;
118
- any = true;
119
- }
120
- }
121
- return any ? options : undefined;
122
- }
123
-
124
- /**
125
- * Caller-side: each `call(input)` opens a new libp2p `Stream` via
126
- * `node.dialProtocol(peer, [protocol])` and runs the call over it.
158
+ * Caller-side: each `call(input)` is one port of a windowed native port mux
159
+ * (`libp2pPortMux`): one libp2p stream negotiating {@link portProtocolOf}
160
+ * `protocol` with `/window`, over which `webrun-rpc`'s stream tier runs the
161
+ * call, pipelining up to the server's window per round trip. Streams are
162
+ * opened through the early-stream gate, so a burst of calls on a fresh
163
+ * connection does not kill its muxer.
164
+ *
165
+ * Against an older server, which refuses that protocol, the call falls back to
166
+ * the deprecated protocol on `protocol` itself, and the node remembers that
167
+ * for the connection, so later calls to that peer dial it directly. The
168
+ * fallback is removed in the next release.
127
169
  */
128
- export const connect: Connect<ConnectLibp2pParams> = async ({
129
- node,
130
- peer,
131
- protocol,
132
- drainTimeoutMs,
133
- readHighWaterBytes,
134
- maxFrameBytes,
135
- maxOutboundStreams,
136
- runOnLimitedConnection,
137
- }) => {
138
- const proto = protocol ?? DEFAULT_PROTOCOL;
139
- const dialOptions: DialProtocolOptions | undefined = definedOptions({
170
+ export const connect: Connect<ConnectLibp2pParams> = async (params) => {
171
+ const { node, peer, maxFrameBytes, maxOutboundStreams, runOnLimitedConnection } = params;
172
+ const protocol = params.protocol ?? DEFAULT_PROTOCOL;
173
+ const ports = await portConnect({
174
+ mux: overPorts(() =>
175
+ openLibp2pPortMux(
176
+ {
177
+ node,
178
+ peer,
179
+ codec,
180
+ protocol: portProtocolOf(protocol),
181
+ window: params.window ?? DEFAULT_WINDOW,
182
+ maxMessageBytes: maxFrameBytes,
183
+ maxOutboundStreams,
184
+ runOnLimitedConnection,
185
+ },
186
+ undefined,
187
+ ),
188
+ ),
189
+ timeout: params.timeout,
190
+ });
191
+ // The deprecated protocol, for a server that refuses the port protocol.
192
+ const legacy = await legacyConnect({
193
+ node,
194
+ peer,
195
+ protocol,
196
+ drainTimeoutMs: params.drainTimeoutMs,
197
+ readHighWaterBytes: params.readHighWaterBytes,
198
+ maxFrameBytes,
140
199
  maxOutboundStreams,
141
200
  runOnLimitedConnection,
142
201
  });
143
- const open = new Set<Stream>();
144
-
145
- // A burst of calls must not reach the remote before its muxer is ready.
146
- //
147
- // When there is no connection to the peer yet, every call dials it and they
148
- // all share the one connection libp2p establishes. Their streams can arrive
149
- // while the REMOTE side is still upgrading that connection, and yamux counts
150
- // those "early streams": past `maxEarlyStreams` (10 by default) it aborts the
151
- // whole muxer, so every stream on the connection dies — the remote sees EOF
152
- // during protocol negotiation, this side sees `StreamResetError`. Raising the
153
- // limit on our own nodes would not help against any other libp2p peer.
154
- //
155
- // So until one stream has negotiated its protocol on an open connection, the
156
- // calls open their streams one at a time: a completed negotiation means the
157
- // remote has finished its upgrade, and from then on streams are not early and
158
- // open concurrently. If that connection closes, the next burst is gated again.
159
- // The state is shared by every connect() to the same peer from the same node
160
- // (callers often connect() per request), since they all share its connection.
161
- const openStream = (): Promise<Stream> => openGatedStream(node, peer, proto, dialOptions);
162
202
 
163
203
  const call: Duplex = (input) => {
164
- let streamRef: Stream | null = null;
165
- const gen = (async function* () {
166
- const stream = await openStream();
167
- streamRef = stream;
168
- open.add(stream);
169
- let sourceCompleted = false;
170
- try {
171
- yield* duplexOverStream(stream, input, {
172
- drainTimeoutMs,
173
- readHighWaterBytes,
174
- maxFrameBytes,
175
- onSourceCompleted: () => {
176
- sourceCompleted = true;
177
- },
178
- });
179
- } finally {
180
- open.delete(stream);
181
- if (sourceCompleted) {
182
- // Natural end on both sides. Graceful close, bounded so a peer
183
- // that stops reading without resetting can't hang this forever.
184
- await closeStream(stream);
185
- }
186
- // Else: consumer cancelled. The .return override below has already
187
- // called stream.abort to send RST; nothing more to do here.
188
- }
189
- })();
190
-
191
- // Send a yamux RST to peer when the consumer cancels (i.e., calls .return
192
- // on this generator). Doing it here is essential because by the time the
193
- // generator's own finally runs, the for-await teardown chain has already
194
- // marked the stream's status as `closed` — and AbstractStream.abort is a
195
- // no-op on closed streams.
196
- const origReturn = gen.return.bind(gen);
197
- gen.return = async (value: unknown) => {
198
- if (streamRef) {
199
- try {
200
- streamRef.abort(new Error("call cancelled"));
201
- } catch {
202
- /* ignore */
203
- }
204
- }
205
- return origReturn(value as undefined);
206
- };
207
-
208
- return gen;
204
+ if (isLegacyPeer(node, peer, protocol)) return legacy.call(input);
205
+ return withFallback(
206
+ ports.call(input),
207
+ () => legacy.call(input),
208
+ (err) => {
209
+ if (!isUnsupportedProtocol(err)) return false;
210
+ markLegacyPeer(node, peer, protocol);
211
+ return true;
212
+ },
213
+ );
209
214
  };
215
+
210
216
  return {
211
217
  call,
212
218
  async close() {
213
- for (const s of open) {
214
- try {
215
- s.abort(new Error("connection close"));
216
- } catch {
217
- /* ignore */
218
- }
219
- }
219
+ await Promise.allSettled([ports.close(), legacy.close()]);
220
220
  },
221
221
  };
222
222
  };
223
223
 
224
224
  /**
225
- * Server-side: registers `node.handle(protocol, ...)`. Each inbound stream is
226
- * wrapped as a `Duplex` and handed to `handler`. Identity-unaware; use
227
- * `serveConnections` when the handler needs to know who is calling.
225
+ * Server-side: serves each call as one port of a windowed native port mux
226
+ * (`<protocol>/ports`, with and without `/window`), and runs `handler` on it.
227
+ * Older callers are still answered on `protocol` itself, the deprecated
228
+ * protocol, until the next release. Identity-unaware; use `serveConnections`
229
+ * when the handler needs to know who is calling.
228
230
  */
229
231
  export const serve: Serve<ServeLibp2pParams> = async (params, handler: Duplex) =>
230
232
  serveConnections(params, () => handler);
231
233
 
232
- /** What the serving side knows about the connection a stream arrived on. */
233
- export interface ConnectionContext {
234
- /**
235
- * The peer id libp2p's Noise handshake proved for this connection. This is
236
- * the only identity claim on the serving side that cannot be forged by the
237
- * request payload.
238
- */
239
- remotePeer: PeerId;
240
- }
241
-
242
234
  /**
243
- * Builds a handler for one inbound connection. Called once per stream, so the
244
- * connection stays reachable in the returned Duplex's closure — `Duplex` is
245
- * bytes-only (ADR-0004) and gains no new parameter.
246
- */
247
- export type ServeConnectionsHandler = (context: ConnectionContext) => Duplex;
248
-
249
- /**
250
- * Like `serve`, but the handler is built per inbound stream and is told which
251
- * peer libp2p proved on that connection.
235
+ * Like `serve`, but the handler is built per call and is told which peer
236
+ * libp2p proved on the connection the call came on.
252
237
  */
253
238
  export async function serveConnections(
254
- {
255
- node,
256
- protocol,
257
- drainTimeoutMs,
258
- readHighWaterBytes,
259
- maxFrameBytes,
260
- maxInboundStreams,
261
- maxOutboundStreams,
262
- runOnLimitedConnection,
263
- }: ServeLibp2pParams,
239
+ params: ServeLibp2pParams,
264
240
  makeHandler: ServeConnectionsHandler,
265
241
  ): Promise<() => Promise<void>> {
266
- const proto = protocol ?? DEFAULT_PROTOCOL;
267
- const handleOptions: StreamHandlerOptions | undefined = definedOptions({
268
- maxInboundStreams,
269
- maxOutboundStreams,
270
- runOnLimitedConnection,
271
- });
242
+ const { node, maxFrameBytes, maxInboundStreams, maxOutboundStreams, runOnLimitedConnection } =
243
+ params;
244
+ const protocol = params.protocol ?? DEFAULT_PROTOCOL;
272
245
 
273
- const onStream = (stream: Stream, connection: Connection): void => {
274
- // The caller sent its whole request; a stream that dies after that is a
275
- // caller that stopped reading the response — ordinary, not a fault.
276
- let requestComplete = false;
277
- void (async () => {
278
- const handler = makeHandler({ remotePeer: connection.remotePeer });
279
- const inputQueue = makeInputQueue(readHighWaterBytes ?? DEFAULT_READ_HIGH_WATER_BYTES);
280
- // once the handler's output has ended, input it never started to read is
281
- // dropped instead of waited for (input it is reading, perhaps in the
282
- // background, keeps flowing at its pace)
283
- const output = endsWith(handler(inputQueue.iter()), () => inputQueue.abandon());
284
- try {
285
- for await (const chunk of duplexOverStream(stream, output, {
286
- drainTimeoutMs,
287
- readHighWaterBytes,
288
- maxFrameBytes,
289
- onPeerInputEnd: (err) => {
290
- if (err === undefined) requestComplete = true;
291
- inputQueue.done(err);
246
+ // The connection of the port being accepted. The stream tier installs the
247
+ // handler synchronously, inside the mux's `onPort` (`webrun-rpc`'s `serve`
248
+ // calls `serveDuplexOverPort`, which calls the handler at once), so the
249
+ // handler below reads the info of its own port.
250
+ let arriving: InboundPortInfo | undefined;
251
+ const stopPorts = await portServe(
252
+ {
253
+ mux: overPorts((onPort) =>
254
+ openLibp2pPortMux(
255
+ {
256
+ node,
257
+ codec,
258
+ protocol: portProtocolOf(protocol),
259
+ window: params.window ?? DEFAULT_WINDOW,
260
+ maxMessageBytes: maxFrameBytes,
261
+ maxInboundStreams,
262
+ maxOutboundStreams,
263
+ runOnLimitedConnection,
264
+ onPort:
265
+ onPort &&
266
+ ((port, info) => {
267
+ arriving = info;
268
+ try {
269
+ return onPort(port, info);
270
+ } finally {
271
+ arriving = undefined;
272
+ }
273
+ }),
292
274
  },
293
- })) {
294
- // past readHighWaterBytes queued for the handler, the next chunk is
295
- // read only once it took this one: a handler that stops reading
296
- // pauses the stream (see readHighWaterBytes)
297
- await inputQueue.push(chunk);
298
- }
299
- } finally {
300
- inputQueue.done();
301
- await closeStream(stream);
302
- }
303
- })().catch((err: unknown) => {
304
- // One inbound stream failing must not take down the process serving
305
- // every other peer. `duplexOverStream` rejects on the read side
306
- // whenever the peer sends an ERROR frame or resets mid-request — which
307
- // happens in ordinary use, not just under attack (a browser tab closed
308
- // mid-request resets its streams). Without this catch that rejection is
309
- // unhandled, and Node's default for `unhandledRejection` is to
310
- // terminate. Logged rather than rethrown, and never rethrown from here:
311
- // there is no caller left to receive it.
312
- if (requestComplete && err instanceof PeerGoneError) return;
313
- const e = err instanceof Error ? err : new Error(String(err));
314
- console.warn(
315
- `[webrun-streams-libp2p] serve: inbound stream on protocol ${proto} failed: ${e.message}`,
316
- e,
317
- );
318
- });
319
- };
275
+ undefined,
276
+ ),
277
+ ),
278
+ timeout: params.timeout,
279
+ },
280
+ (input) => {
281
+ const info = arriving;
282
+ if (!info) throw new Error("webrun-streams-libp2p: a call arrived without its connection");
283
+ return makeHandler({ remotePeer: info.remotePeer })(input);
284
+ },
285
+ );
320
286
 
321
- await node.handle(proto, onStream, handleOptions);
287
+ // The deprecated protocol, for older callers.
288
+ let stopLegacy: () => Promise<void>;
289
+ try {
290
+ stopLegacy = await legacyServeConnections(
291
+ {
292
+ node,
293
+ protocol,
294
+ drainTimeoutMs: params.drainTimeoutMs,
295
+ readHighWaterBytes: params.readHighWaterBytes,
296
+ maxFrameBytes,
297
+ maxInboundStreams,
298
+ maxOutboundStreams,
299
+ runOnLimitedConnection,
300
+ },
301
+ makeHandler,
302
+ );
303
+ } catch (err) {
304
+ await stopPorts();
305
+ throw err;
306
+ }
322
307
 
323
308
  let torn = false;
324
309
  return async () => {
325
310
  if (torn) return;
326
311
  torn = true;
327
- await node.unhandle(proto);
312
+ await Promise.all([stopPorts(), stopLegacy()]);
328
313
  };
329
314
  }
330
315
 
331
- /** `source`, calling `onEnd` once when it ends, fails or is returned. */
332
- function endsWith(
333
- source: AsyncIterable<Uint8Array>,
334
- onEnd: () => void,
335
- ): AsyncGenerator<Uint8Array> {
336
- return (async function* () {
337
- try {
338
- yield* source;
339
- } finally {
340
- onEnd();
341
- }
342
- })();
343
- }
344
-
345
- interface InputQueue {
346
- iter(): AsyncGenerator<Uint8Array>;
347
- /**
348
- * resolves at once while the bytes queued for the handler stay within the
349
- * limit, else once the handler has read them back down to it (or will never
350
- * read on)
351
- */
352
- push(chunk: Uint8Array): Promise<void>;
353
- done(err?: Error): void;
354
- /**
355
- * the handler's output ended: if it never started reading its input, pending
356
- * and later chunks are dropped (an iterator started already is left to its
357
- * reader, which may go on reading after the output ended)
358
- */
359
- abandon(): void;
316
+ /** Whether `err` says the peer speaks none of the protocols dialled. */
317
+ function isUnsupportedProtocol(err: unknown): boolean {
318
+ return err instanceof Error && err.name === "UnsupportedProtocolError";
360
319
  }
361
320
 
362
- function makeInputQueue(limit: number): InputQueue {
363
- type Slot = { type: "value"; value: Uint8Array } | { type: "done"; err?: Error };
364
- const slots: Slot[] = [];
365
- let wake: (() => void) | null = null;
366
- let closed = false;
367
- let pulled = false;
368
- let queued = 0;
369
- // the one push waiting for room (the serving loop awaits each push)
370
- let room: (() => void) | null = null;
371
- const makeRoom = (): void => {
372
- const r = room;
373
- room = null;
374
- r?.();
375
- };
376
- const release = (): void => {
377
- slots.length = 0;
378
- queued = 0;
379
- makeRoom();
380
- };
381
- return {
382
- iter(): AsyncGenerator<Uint8Array> {
383
- return (async function* () {
384
- if (closed) return; // abandoned before anyone read: the input was dropped
385
- pulled = true;
386
- try {
387
- while (true) {
388
- if (slots.length === 0) {
389
- await new Promise<void>((r) => {
390
- wake = r;
391
- });
392
- wake = null;
393
- continue;
394
- }
395
- const s = slots.shift() as Slot;
396
- if (s.type === "done") {
397
- if (s.err) throw s.err;
398
- return;
399
- }
400
- queued -= s.value.byteLength;
401
- if (queued <= limit) makeRoom();
402
- yield s.value;
403
- }
404
- } finally {
405
- closed = true;
406
- release();
321
+ /**
322
+ * `primary`, except that when its first `next()` rejects with an error
323
+ * `fallBack` accepts — the port protocol refused, before any of the input was
324
+ * read — the call runs on `fallback()` instead. `return()` and `throw()` go
325
+ * straight to the current call, so a consumer that gives up on a pending
326
+ * `next()` is heard at once, as by the call itself.
327
+ */
328
+ function withFallback(
329
+ primary: AsyncGenerator<Uint8Array>,
330
+ fallback: () => AsyncGenerator<Uint8Array>,
331
+ fallBack: (err: unknown) => boolean,
332
+ ): AsyncGenerator<Uint8Array> {
333
+ let current = primary;
334
+ let settled = false;
335
+ const wrapped: Pick<
336
+ AsyncGenerator<Uint8Array>,
337
+ "next" | "return" | "throw" | typeof Symbol.asyncIterator
338
+ > = {
339
+ async next(...args: [] | [unknown]) {
340
+ if (settled) return current.next(...args);
341
+ try {
342
+ const result = await current.next(...args);
343
+ settled = true;
344
+ return result;
345
+ } catch (err) {
346
+ if (settled || !fallBack(err)) {
347
+ settled = true;
348
+ throw err;
407
349
  }
408
- })();
350
+ settled = true;
351
+ current = fallback();
352
+ return current.next(...args);
353
+ }
409
354
  },
410
- push(chunk: Uint8Array): Promise<void> {
411
- if (closed) return Promise.resolve();
412
- slots.push({ type: "value", value: chunk });
413
- queued += chunk.byteLength;
414
- wake?.();
415
- if (queued <= limit) return Promise.resolve();
416
- return new Promise<void>((resolve) => {
417
- room = resolve;
418
- });
355
+ return(value) {
356
+ settled = true;
357
+ return current.return(value);
419
358
  },
420
- done(err?: Error): void {
421
- if (closed) return;
422
- slots.push({ type: "done", err });
423
- wake?.();
359
+ throw(error) {
360
+ settled = true;
361
+ return current.throw(error);
424
362
  },
425
- abandon(): void {
426
- if (pulled) return;
427
- closed = true;
428
- release();
429
- wake?.();
363
+ [Symbol.asyncIterator]() {
364
+ return wrapped as AsyncGenerator<Uint8Array>;
430
365
  },
431
366
  };
367
+ // `await using` ends the call as leaving a `for await` early does.
368
+ const asyncDispose = (Symbol as { asyncDispose?: symbol }).asyncDispose;
369
+ if (asyncDispose) {
370
+ Object.defineProperty(wrapped, asyncDispose, {
371
+ value: async () => {
372
+ await wrapped.return(undefined);
373
+ },
374
+ configurable: true,
375
+ writable: true,
376
+ });
377
+ }
378
+ return wrapped as AsyncGenerator<Uint8Array>;
379
+ }
380
+
381
+ /**
382
+ * Peers known to refuse the port protocol, per local node, by peer and
383
+ * protocol: the connection the refusal came on (`null` when the node cannot
384
+ * tell), so a peer that restarts upgraded is probed again on its next
385
+ * connection.
386
+ */
387
+ const legacyPeers = new WeakMap<Libp2p, Map<string, { connection: Connection | null }>>();
388
+
389
+ const legacyKey = (peer: PeerId | Multiaddr, protocol: string) => `${peer.toString()} ${protocol}`;
390
+
391
+ function isLegacyPeer(node: Libp2p, peer: PeerId | Multiaddr, protocol: string): boolean {
392
+ const peers = legacyPeers.get(node);
393
+ const mark = peers?.get(legacyKey(peer, protocol));
394
+ if (!mark) return false;
395
+ if (mark.connection == null || mark.connection.status === "open") return true;
396
+ peers?.delete(legacyKey(peer, protocol));
397
+ return false;
398
+ }
399
+
400
+ function markLegacyPeer(node: Libp2p, peer: PeerId | Multiaddr, protocol: string): void {
401
+ let peers = legacyPeers.get(node);
402
+ if (!peers) {
403
+ peers = new Map();
404
+ legacyPeers.set(node, peers);
405
+ }
406
+ peers.set(legacyKey(peer, protocol), { connection: connectionTo(node, peer) });
407
+ }
408
+
409
+ /**
410
+ * The open connection to `peer`, when `node` can list its connections (a
411
+ * narrow stand-in may offer only `dialProtocol`).
412
+ */
413
+ function connectionTo(node: Libp2p, peer: PeerId | Multiaddr): Connection | null {
414
+ if (typeof node.getConnections !== "function") return null;
415
+ const target = peer.toString();
416
+ return (
417
+ node
418
+ .getConnections()
419
+ .find(
420
+ (c) =>
421
+ c.status === "open" &&
422
+ (c.remotePeer.toString() === target ||
423
+ c.remoteAddr.toString() === target ||
424
+ target.endsWith(`/p2p/${c.remotePeer.toString()}`)),
425
+ ) ?? null
426
+ );
432
427
  }