@statewalker/webrun-streams-libp2p 0.1.2 → 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.
- package/LICENSE +21 -0
- package/dist/connect-serve.d.ts +0 -4
- package/dist/connect-serve.d.ts.map +1 -1
- package/dist/index.js +37 -5
- package/package.json +15 -13
- package/src/connect-serve.ts +63 -1
- package/src/duplex-over-stream.ts +4 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-2026 statewalker
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
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,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,E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AW,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
|
-
|
|
308
|
-
|
|
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
|
|
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.
|
|
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",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"exports": {
|
|
18
18
|
".": {
|
|
19
|
+
"source": "./src/index.ts",
|
|
19
20
|
"types": "./dist/index.d.ts",
|
|
20
21
|
"import": "./dist/index.js"
|
|
21
22
|
}
|
|
@@ -25,7 +26,7 @@
|
|
|
25
26
|
"src"
|
|
26
27
|
],
|
|
27
28
|
"dependencies": {
|
|
28
|
-
"@statewalker/webrun-streams": "0.2.
|
|
29
|
+
"@statewalker/webrun-streams": "^0.2.1"
|
|
29
30
|
},
|
|
30
31
|
"peerDependencies": {
|
|
31
32
|
"@libp2p/interface": "^3.0.0",
|
|
@@ -38,19 +39,20 @@
|
|
|
38
39
|
}
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
|
-
"@
|
|
42
|
-
"@chainsafe/libp2p-
|
|
43
|
-
"@libp2p
|
|
44
|
-
"@libp2p/
|
|
45
|
-
"@libp2p/
|
|
46
|
-
"@
|
|
47
|
-
"@
|
|
48
|
-
"
|
|
42
|
+
"@biomejs/biome": "^2.5.15",
|
|
43
|
+
"@chainsafe/libp2p-noise": "^17.0.0",
|
|
44
|
+
"@chainsafe/libp2p-yamux": "^8.0.1",
|
|
45
|
+
"@libp2p/interface": "^3.3.0",
|
|
46
|
+
"@libp2p/tcp": "^11.0.28",
|
|
47
|
+
"@libp2p/utils": "^7.4.1",
|
|
48
|
+
"@multiformats/multiaddr": "^13.0.3",
|
|
49
|
+
"@types/node": "^26.6.4",
|
|
50
|
+
"libp2p": "^3.3.11",
|
|
49
51
|
"rimraf": "^6.1.3",
|
|
50
|
-
"rolldown": "^1.2.
|
|
52
|
+
"rolldown": "^1.2.12",
|
|
51
53
|
"typescript": "^7.0.2",
|
|
52
|
-
"vitest": "^
|
|
53
|
-
"@statewalker/webrun-streams-conformance": "0.
|
|
54
|
+
"vitest": "^5.0.3",
|
|
55
|
+
"@statewalker/webrun-streams-conformance": "^0.3.1"
|
|
54
56
|
},
|
|
55
57
|
"sideEffects": false,
|
|
56
58
|
"publishConfig": {
|
package/src/connect-serve.ts
CHANGED
|
@@ -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
|
|
179
|
+
const stream = await openStream();
|
|
118
180
|
streamRef = stream;
|
|
119
181
|
open.add(stream);
|
|
120
182
|
let sourceCompleted = false;
|
|
@@ -356,7 +356,8 @@ async function* parseFrames(
|
|
|
356
356
|
}
|
|
357
357
|
while (buf.byteLength > 0) {
|
|
358
358
|
if (buf.byteLength < 2) break; // need at least type + 1 varint byte
|
|
359
|
-
|
|
359
|
+
// biome-ignore lint/style/noNonNullAssertion: buf.byteLength >= 2, checked above, so index 0 exists.
|
|
360
|
+
const type = buf[0]!;
|
|
360
361
|
let lenInfo: { value: number; offset: number };
|
|
361
362
|
try {
|
|
362
363
|
lenInfo = decodeVarint(buf, 1);
|
|
@@ -428,7 +429,8 @@ function decodeVarint(buf: Uint8Array, start: number): { value: number; offset:
|
|
|
428
429
|
let shift = 0;
|
|
429
430
|
let i = start;
|
|
430
431
|
while (i < buf.length) {
|
|
431
|
-
|
|
432
|
+
// biome-ignore lint/style/noNonNullAssertion: i < buf.length, checked by the while condition, so this index exists.
|
|
433
|
+
const b = buf[i++]!;
|
|
432
434
|
value |= (b & 0x7f) << shift;
|
|
433
435
|
if ((b & 0x80) === 0) return { value: value >>> 0, offset: i };
|
|
434
436
|
shift += 7;
|