@statewalker/webrun-streams-libp2p 0.1.4 → 0.1.5

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.
@@ -62,10 +62,6 @@ export interface ServeLibp2pParams {
62
62
  */
63
63
  runOnLimitedConnection?: boolean;
64
64
  }
65
- /**
66
- * Caller-side: each `call(input)` opens a new libp2p `Stream` via
67
- * `node.dialProtocol(peer, [protocol])` and runs the call over it.
68
- */
69
65
  export declare const connect: Connect<ConnectLibp2pParams>;
70
66
  /**
71
67
  * Server-side: registers `node.handle(protocol, ...)`. Each inbound stream is
@@ -1 +1 @@
1
- {"version":3,"file":"connect-serve.d.ts","sourceRoot":"","sources":["../src/connect-serve.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,MAAM,EACN,MAAM,EAGP,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAC;AAG1E,eAAO,MAAM,gBAAgB,0BAA0B,CAAC;AAExD,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IACzB,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B;;;;;;OAMG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;CAClC;AAED,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;;;;OAMG;IACH,iBAAiB,CAAC,EA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;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B;;;;;;OAMG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;CAClC;AAED,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;;;;OAMG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B;;;;;;;OAOG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;CAClC;AAkDD,eAAO,MAAM,OAAO,EAAE,OAAO,CAAC,mBAAmB,CA4GhD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,KAAK,EAAE,KAAK,CAAC,iBAAiB,CACF,CAAC;AAE1C,4EAA4E;AAC5E,MAAM,WAAW,iBAAiB;IAChC;;;;OAIG;IACH,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;;;GAIG;AACH,MAAM,MAAM,uBAAuB,GAAG,CAAC,OAAO,EAAE,iBAAiB,KAAK,MAAM,CAAC;AAE7E;;;GAGG;AACH,wBAAsB,gBAAgB,CACpC,EACE,IAAI,EACJ,QAAQ,EACR,cAAc,EACd,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,GACvB,EAAE,iBAAiB,EACpB,WAAW,EAAE,uBAAuB,GACnC,OAAO,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,CAiD9B"}
package/dist/index.js CHANGED
@@ -303,10 +303,24 @@ function definedOptions(fields) {
303
303
  }
304
304
  return any ? options : void 0;
305
305
  }
306
- /**
307
- * Caller-side: each `call(input)` opens a new libp2p `Stream` via
308
- * `node.dialProtocol(peer, [protocol])` and runs the call over it.
309
- */
306
+ const streamGates = /* @__PURE__ */ new WeakMap();
307
+ function streamGateFor(node, peer) {
308
+ let gates = streamGates.get(node);
309
+ if (gates == null) {
310
+ gates = /* @__PURE__ */ new Map();
311
+ streamGates.set(node, gates);
312
+ }
313
+ const key = peer.toString();
314
+ let gate = gates.get(key);
315
+ if (gate == null) {
316
+ gate = {
317
+ provenConnection: null,
318
+ firstOpening: null
319
+ };
320
+ gates.set(key, gate);
321
+ }
322
+ return gate;
323
+ }
310
324
  const connect = async ({ node, peer, protocol, drainTimeoutMs, maxOutboundStreams, runOnLimitedConnection }) => {
311
325
  const proto = protocol ?? "/webrun-streams/1.0.0";
312
326
  const dialOptions = definedOptions({
@@ -314,10 +328,28 @@ const connect = async ({ node, peer, protocol, drainTimeoutMs, maxOutboundStream
314
328
  runOnLimitedConnection
315
329
  });
316
330
  const open = /* @__PURE__ */ new Set();
331
+ const gate = streamGateFor(node, peer);
332
+ const openStream = async () => {
333
+ while (gate.provenConnection?.status !== "open") {
334
+ if (gate.firstOpening == null) {
335
+ const opening = node.dialProtocol(peer, [proto], dialOptions);
336
+ gate.firstOpening = opening;
337
+ try {
338
+ const stream = await opening;
339
+ gate.provenConnection = node.getConnections().find((connection) => connection.streams.includes(stream)) ?? null;
340
+ return stream;
341
+ } finally {
342
+ gate.firstOpening = null;
343
+ }
344
+ }
345
+ await gate.firstOpening.catch(() => void 0);
346
+ }
347
+ return node.dialProtocol(peer, [proto], dialOptions);
348
+ };
317
349
  const call = (input) => {
318
350
  let streamRef = null;
319
351
  const gen = (async function* () {
320
- const stream = await node.dialProtocol(peer, [proto], dialOptions);
352
+ const stream = await openStream();
321
353
  streamRef = stream;
322
354
  open.add(stream);
323
355
  let sourceCompleted = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statewalker/webrun-streams-libp2p",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "libp2p native multi-stream Connect/Serve adapter in the webrun-streams-* family",
@@ -97,6 +97,31 @@ function definedOptions<T extends Record<string, unknown>>(fields: T): Partial<T
97
97
  * Caller-side: each `call(input)` opens a new libp2p `Stream` via
98
98
  * `node.dialProtocol(peer, [protocol])` and runs the call over it.
99
99
  */
100
+ /** The early-stream guard of `connect()`, per local node and remote peer. */
101
+ interface StreamGate {
102
+ /** A connection on which a stream has negotiated a protocol: the remote is ready. */
103
+ provenConnection: Connection | null;
104
+ /** The stream being opened while no connection is proven; the others wait for it. */
105
+ firstOpening: Promise<Stream> | null;
106
+ }
107
+
108
+ const streamGates = new WeakMap<Libp2p, Map<string, StreamGate>>();
109
+
110
+ function streamGateFor(node: Libp2p, peer: PeerId | Multiaddr): StreamGate {
111
+ let gates = streamGates.get(node);
112
+ if (gates == null) {
113
+ gates = new Map();
114
+ streamGates.set(node, gates);
115
+ }
116
+ const key = peer.toString();
117
+ let gate = gates.get(key);
118
+ if (gate == null) {
119
+ gate = { provenConnection: null, firstOpening: null };
120
+ gates.set(key, gate);
121
+ }
122
+ return gate;
123
+ }
124
+
100
125
  export const connect: Connect<ConnectLibp2pParams> = async ({
101
126
  node,
102
127
  peer,
@@ -111,10 +136,47 @@ export const connect: Connect<ConnectLibp2pParams> = async ({
111
136
  runOnLimitedConnection,
112
137
  });
113
138
  const open = new Set<Stream>();
139
+
140
+ // A burst of calls must not reach the remote before its muxer is ready.
141
+ //
142
+ // When there is no connection to the peer yet, every call dials it and they
143
+ // all share the one connection libp2p establishes. Their streams can arrive
144
+ // while the REMOTE side is still upgrading that connection, and yamux counts
145
+ // those "early streams": past `maxEarlyStreams` (10 by default) it aborts the
146
+ // whole muxer, so every stream on the connection dies — the remote sees EOF
147
+ // during protocol negotiation, this side sees `StreamResetError`. Raising the
148
+ // limit on our own nodes would not help against any other libp2p peer.
149
+ //
150
+ // So until one stream has negotiated its protocol on an open connection, the
151
+ // calls open their streams one at a time: a completed negotiation means the
152
+ // remote has finished its upgrade, and from then on streams are not early and
153
+ // open concurrently. If that connection closes, the next burst is gated again.
154
+ // The state is shared by every connect() to the same peer from the same node
155
+ // (callers often connect() per request), since they all share its connection.
156
+ const gate = streamGateFor(node, peer);
157
+ const openStream = async (): Promise<Stream> => {
158
+ while (gate.provenConnection?.status !== "open") {
159
+ if (gate.firstOpening == null) {
160
+ const opening = node.dialProtocol(peer, [proto], dialOptions);
161
+ gate.firstOpening = opening;
162
+ try {
163
+ const stream = await opening;
164
+ gate.provenConnection =
165
+ node.getConnections().find((connection) => connection.streams.includes(stream)) ?? null;
166
+ return stream;
167
+ } finally {
168
+ gate.firstOpening = null;
169
+ }
170
+ }
171
+ await gate.firstOpening.catch(() => undefined);
172
+ }
173
+ return node.dialProtocol(peer, [proto], dialOptions);
174
+ };
175
+
114
176
  const call: Duplex = (input) => {
115
177
  let streamRef: Stream | null = null;
116
178
  const gen = (async function* () {
117
- const stream = await node.dialProtocol(peer, [proto], dialOptions);
179
+ const stream = await openStream();
118
180
  streamRef = stream;
119
181
  open.add(stream);
120
182
  let sourceCompleted = false;