@statewalker/webrun-streams-libp2p 0.1.5 → 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.map +1 -1
- package/dist/index.js +16 -3
- package/package.json +1 -1
- package/src/connect-serve.ts +25 -6
|
@@ -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,EAAE,MAAM,CAAC;IAC5B;;;;;;;OAOG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;CAClC;
|
|
1
|
+
{"version":3,"file":"connect-ser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C,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
|
@@ -314,13 +314,26 @@ function streamGateFor(node, peer) {
|
|
|
314
314
|
let gate = gates.get(key);
|
|
315
315
|
if (gate == null) {
|
|
316
316
|
gate = {
|
|
317
|
-
|
|
317
|
+
proven: null,
|
|
318
318
|
firstOpening: null
|
|
319
319
|
};
|
|
320
320
|
gates.set(key, gate);
|
|
321
321
|
}
|
|
322
322
|
return gate;
|
|
323
323
|
}
|
|
324
|
+
function isProven(gate) {
|
|
325
|
+
if (gate.proven == null) return false;
|
|
326
|
+
return gate.proven.connection == null || gate.proven.connection.status === "open";
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
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.
|
|
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
|
+
}
|
|
324
337
|
const connect = async ({ node, peer, protocol, drainTimeoutMs, maxOutboundStreams, runOnLimitedConnection }) => {
|
|
325
338
|
const proto = protocol ?? "/webrun-streams/1.0.0";
|
|
326
339
|
const dialOptions = definedOptions({
|
|
@@ -330,13 +343,13 @@ const connect = async ({ node, peer, protocol, drainTimeoutMs, maxOutboundStream
|
|
|
330
343
|
const open = /* @__PURE__ */ new Set();
|
|
331
344
|
const gate = streamGateFor(node, peer);
|
|
332
345
|
const openStream = async () => {
|
|
333
|
-
while (gate
|
|
346
|
+
while (!isProven(gate)) {
|
|
334
347
|
if (gate.firstOpening == null) {
|
|
335
348
|
const opening = node.dialProtocol(peer, [proto], dialOptions);
|
|
336
349
|
gate.firstOpening = opening;
|
|
337
350
|
try {
|
|
338
351
|
const stream = await opening;
|
|
339
|
-
gate.
|
|
352
|
+
gate.proven = { connection: connectionOf(node, stream) };
|
|
340
353
|
return stream;
|
|
341
354
|
} finally {
|
|
342
355
|
gate.firstOpening = null;
|
package/package.json
CHANGED
package/src/connect-serve.ts
CHANGED
|
@@ -99,8 +99,13 @@ function definedOptions<T extends Record<string, unknown>>(fields: T): Partial<T
|
|
|
99
99
|
*/
|
|
100
100
|
/** The early-stream guard of `connect()`, per local node and remote peer. */
|
|
101
101
|
interface StreamGate {
|
|
102
|
-
/**
|
|
103
|
-
|
|
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;
|
|
104
109
|
/** The stream being opened while no connection is proven; the others wait for it. */
|
|
105
110
|
firstOpening: Promise<Stream> | null;
|
|
106
111
|
}
|
|
@@ -116,12 +121,27 @@ function streamGateFor(node: Libp2p, peer: PeerId | Multiaddr): StreamGate {
|
|
|
116
121
|
const key = peer.toString();
|
|
117
122
|
let gate = gates.get(key);
|
|
118
123
|
if (gate == null) {
|
|
119
|
-
gate = {
|
|
124
|
+
gate = { proven: null, firstOpening: null };
|
|
120
125
|
gates.set(key, gate);
|
|
121
126
|
}
|
|
122
127
|
return gate;
|
|
123
128
|
}
|
|
124
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
|
+
|
|
125
145
|
export const connect: Connect<ConnectLibp2pParams> = async ({
|
|
126
146
|
node,
|
|
127
147
|
peer,
|
|
@@ -155,14 +175,13 @@ export const connect: Connect<ConnectLibp2pParams> = async ({
|
|
|
155
175
|
// (callers often connect() per request), since they all share its connection.
|
|
156
176
|
const gate = streamGateFor(node, peer);
|
|
157
177
|
const openStream = async (): Promise<Stream> => {
|
|
158
|
-
while (gate
|
|
178
|
+
while (!isProven(gate)) {
|
|
159
179
|
if (gate.firstOpening == null) {
|
|
160
180
|
const opening = node.dialProtocol(peer, [proto], dialOptions);
|
|
161
181
|
gate.firstOpening = opening;
|
|
162
182
|
try {
|
|
163
183
|
const stream = await opening;
|
|
164
|
-
gate.
|
|
165
|
-
node.getConnections().find((connection) => connection.streams.includes(stream)) ?? null;
|
|
184
|
+
gate.proven = { connection: connectionOf(node, stream) };
|
|
166
185
|
return stream;
|
|
167
186
|
} finally {
|
|
168
187
|
gate.firstOpening = null;
|