@statewalker/webrun-streams-libp2p 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/connect-serve.d.ts +0 -4
- package/dist/connect-serve.d.ts.map +1 -1
- package/dist/index.js +48 -3
- package/package.json +1 -1
- package/src/connect-serve.ts +82 -1
package/dist/connect-serve.d.ts
CHANGED
|
@@ -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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;;OAIG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B;;;;;;;OAOG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;CAClC;AAsED,eAAO,MAAM,OAAO,EAAE,OAAO,CAAC,mBAAmB,CA2GhD,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,37 @@ function definedOptions(fields) {
|
|
|
303
303
|
}
|
|
304
304
|
return any ? options : void 0;
|
|
305
305
|
}
|
|
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
|
+
proven: null,
|
|
318
|
+
firstOpening: null
|
|
319
|
+
};
|
|
320
|
+
gates.set(key, gate);
|
|
321
|
+
}
|
|
322
|
+
return gate;
|
|
323
|
+
}
|
|
324
|
+
function isProven(gate) {
|
|
325
|
+
if (gate.proven == null) return false;
|
|
326
|
+
return gate.proven.connection == null || gate.proven.connection.status === "open";
|
|
327
|
+
}
|
|
306
328
|
/**
|
|
307
|
-
*
|
|
308
|
-
*
|
|
329
|
+
* The connection carrying `stream`, when `node` can list its connections.
|
|
330
|
+
* Callers may pass a narrow stand-in that offers only `dialProtocol` (httpeers
|
|
331
|
+
* routes streams onto a kept relay circuit that way), so nothing else is required.
|
|
309
332
|
*/
|
|
333
|
+
function connectionOf(node, stream) {
|
|
334
|
+
if (typeof node.getConnections !== "function") return null;
|
|
335
|
+
return node.getConnections().find((connection) => connection.streams.includes(stream)) ?? null;
|
|
336
|
+
}
|
|
310
337
|
const connect = async ({ node, peer, protocol, drainTimeoutMs, maxOutboundStreams, runOnLimitedConnection }) => {
|
|
311
338
|
const proto = protocol ?? "/webrun-streams/1.0.0";
|
|
312
339
|
const dialOptions = definedOptions({
|
|
@@ -314,10 +341,28 @@ const connect = async ({ node, peer, protocol, drainTimeoutMs, maxOutboundStream
|
|
|
314
341
|
runOnLimitedConnection
|
|
315
342
|
});
|
|
316
343
|
const open = /* @__PURE__ */ new Set();
|
|
344
|
+
const gate = streamGateFor(node, peer);
|
|
345
|
+
const openStream = async () => {
|
|
346
|
+
while (!isProven(gate)) {
|
|
347
|
+
if (gate.firstOpening == null) {
|
|
348
|
+
const opening = node.dialProtocol(peer, [proto], dialOptions);
|
|
349
|
+
gate.firstOpening = opening;
|
|
350
|
+
try {
|
|
351
|
+
const stream = await opening;
|
|
352
|
+
gate.proven = { connection: connectionOf(node, stream) };
|
|
353
|
+
return stream;
|
|
354
|
+
} finally {
|
|
355
|
+
gate.firstOpening = null;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
await gate.firstOpening.catch(() => void 0);
|
|
359
|
+
}
|
|
360
|
+
return node.dialProtocol(peer, [proto], dialOptions);
|
|
361
|
+
};
|
|
317
362
|
const call = (input) => {
|
|
318
363
|
let streamRef = null;
|
|
319
364
|
const gen = (async function* () {
|
|
320
|
-
const stream = await
|
|
365
|
+
const stream = await openStream();
|
|
321
366
|
streamRef = stream;
|
|
322
367
|
open.add(stream);
|
|
323
368
|
let sourceCompleted = false;
|
package/package.json
CHANGED
package/src/connect-serve.ts
CHANGED
|
@@ -97,6 +97,51 @@ 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
|
+
/**
|
|
103
|
+
* A stream has negotiated a protocol on an open connection: the remote is ready.
|
|
104
|
+
* `connection` is that connection when the node can tell (`getConnections`), so a
|
|
105
|
+
* burst after it closes is gated again; a node that offers only `dialProtocol`
|
|
106
|
+
* stays proven.
|
|
107
|
+
*/
|
|
108
|
+
proven: { connection: Connection | null } | null;
|
|
109
|
+
/** The stream being opened while no connection is proven; the others wait for it. */
|
|
110
|
+
firstOpening: Promise<Stream> | null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const streamGates = new WeakMap<Libp2p, Map<string, StreamGate>>();
|
|
114
|
+
|
|
115
|
+
function streamGateFor(node: Libp2p, peer: PeerId | Multiaddr): StreamGate {
|
|
116
|
+
let gates = streamGates.get(node);
|
|
117
|
+
if (gates == null) {
|
|
118
|
+
gates = new Map();
|
|
119
|
+
streamGates.set(node, gates);
|
|
120
|
+
}
|
|
121
|
+
const key = peer.toString();
|
|
122
|
+
let gate = gates.get(key);
|
|
123
|
+
if (gate == null) {
|
|
124
|
+
gate = { proven: null, firstOpening: null };
|
|
125
|
+
gates.set(key, gate);
|
|
126
|
+
}
|
|
127
|
+
return gate;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function isProven(gate: StreamGate): boolean {
|
|
131
|
+
if (gate.proven == null) return false;
|
|
132
|
+
return gate.proven.connection == null || gate.proven.connection.status === "open";
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The connection carrying `stream`, when `node` can list its connections.
|
|
137
|
+
* Callers may pass a narrow stand-in that offers only `dialProtocol` (httpeers
|
|
138
|
+
* routes streams onto a kept relay circuit that way), so nothing else is required.
|
|
139
|
+
*/
|
|
140
|
+
function connectionOf(node: Libp2p, stream: Stream): Connection | null {
|
|
141
|
+
if (typeof node.getConnections !== "function") return null;
|
|
142
|
+
return node.getConnections().find((connection) => connection.streams.includes(stream)) ?? null;
|
|
143
|
+
}
|
|
144
|
+
|
|
100
145
|
export const connect: Connect<ConnectLibp2pParams> = async ({
|
|
101
146
|
node,
|
|
102
147
|
peer,
|
|
@@ -111,10 +156,46 @@ export const connect: Connect<ConnectLibp2pParams> = async ({
|
|
|
111
156
|
runOnLimitedConnection,
|
|
112
157
|
});
|
|
113
158
|
const open = new Set<Stream>();
|
|
159
|
+
|
|
160
|
+
// A burst of calls must not reach the remote before its muxer is ready.
|
|
161
|
+
//
|
|
162
|
+
// When there is no connection to the peer yet, every call dials it and they
|
|
163
|
+
// all share the one connection libp2p establishes. Their streams can arrive
|
|
164
|
+
// while the REMOTE side is still upgrading that connection, and yamux counts
|
|
165
|
+
// those "early streams": past `maxEarlyStreams` (10 by default) it aborts the
|
|
166
|
+
// whole muxer, so every stream on the connection dies — the remote sees EOF
|
|
167
|
+
// during protocol negotiation, this side sees `StreamResetError`. Raising the
|
|
168
|
+
// limit on our own nodes would not help against any other libp2p peer.
|
|
169
|
+
//
|
|
170
|
+
// So until one stream has negotiated its protocol on an open connection, the
|
|
171
|
+
// calls open their streams one at a time: a completed negotiation means the
|
|
172
|
+
// remote has finished its upgrade, and from then on streams are not early and
|
|
173
|
+
// open concurrently. If that connection closes, the next burst is gated again.
|
|
174
|
+
// The state is shared by every connect() to the same peer from the same node
|
|
175
|
+
// (callers often connect() per request), since they all share its connection.
|
|
176
|
+
const gate = streamGateFor(node, peer);
|
|
177
|
+
const openStream = async (): Promise<Stream> => {
|
|
178
|
+
while (!isProven(gate)) {
|
|
179
|
+
if (gate.firstOpening == null) {
|
|
180
|
+
const opening = node.dialProtocol(peer, [proto], dialOptions);
|
|
181
|
+
gate.firstOpening = opening;
|
|
182
|
+
try {
|
|
183
|
+
const stream = await opening;
|
|
184
|
+
gate.proven = { connection: connectionOf(node, stream) };
|
|
185
|
+
return stream;
|
|
186
|
+
} finally {
|
|
187
|
+
gate.firstOpening = null;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
await gate.firstOpening.catch(() => undefined);
|
|
191
|
+
}
|
|
192
|
+
return node.dialProtocol(peer, [proto], dialOptions);
|
|
193
|
+
};
|
|
194
|
+
|
|
114
195
|
const call: Duplex = (input) => {
|
|
115
196
|
let streamRef: Stream | null = null;
|
|
116
197
|
const gen = (async function* () {
|
|
117
|
-
const stream = await
|
|
198
|
+
const stream = await openStream();
|
|
118
199
|
streamRef = stream;
|
|
119
200
|
open.add(stream);
|
|
120
201
|
let sourceCompleted = false;
|