@minip2p/core 0.13.0 → 0.13.1
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/README.md +4 -0
- package/dist/backend.d.ts.map +1 -1
- package/dist/backend.js +0 -4
- package/dist/backend.js.map +1 -1
- package/dist/config.d.ts +1 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +7 -1
- package/dist/config.js.map +1 -1
- package/dist/sdk.d.ts.map +1 -1
- package/dist/sdk.js +0 -47
- package/dist/sdk.js.map +1 -1
- package/dist/serde-backend.d.ts.map +1 -1
- package/dist/serde-backend.js +0 -7
- package/dist/serde-backend.js.map +1 -1
- package/dist/types.d.ts +10 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
- package/src/backend.ts +0 -4
- package/src/config.ts +12 -1
- package/src/sdk.ts +0 -49
- package/src/serde-backend.ts +0 -7
- package/src/types.ts +10 -1
package/src/backend.ts
CHANGED
|
@@ -85,13 +85,10 @@ export class NativeCloseLatch {
|
|
|
85
85
|
let closed: BackendClosed;
|
|
86
86
|
try {
|
|
87
87
|
stop();
|
|
88
|
-
// Without a driver, native released the endpoint at once.
|
|
89
88
|
closed = started
|
|
90
89
|
? await this.#closed
|
|
91
90
|
: { outcome: CloseOutcome.Drained, unfinished: [] };
|
|
92
91
|
} catch {
|
|
93
|
-
// Native failed to stop (a trap): no `EndpointClosed` will come, but
|
|
94
|
-
// close still never rejects and still releases.
|
|
95
92
|
closed = { outcome: CloseOutcome.Failed, unfinished: [] };
|
|
96
93
|
}
|
|
97
94
|
try {
|
|
@@ -256,7 +253,6 @@ export interface NativeErrorFields {
|
|
|
256
253
|
readonly capability?: string | undefined;
|
|
257
254
|
}
|
|
258
255
|
|
|
259
|
-
// Each capability's name is its value, so an own key is a capability.
|
|
260
256
|
const isConfigCapability = (name: string): name is ConfigCapability =>
|
|
261
257
|
Object.hasOwn(ConfigCapability, name);
|
|
262
258
|
|
package/src/config.ts
CHANGED
|
@@ -30,6 +30,7 @@ export interface BackendEndpointConfig {
|
|
|
30
30
|
readonly topic: string;
|
|
31
31
|
};
|
|
32
32
|
readonly forceRelay: boolean;
|
|
33
|
+
readonly inboundStreamLimits: Record<string, number>;
|
|
33
34
|
readonly limits?: BackendLimits;
|
|
34
35
|
/** Listen multiaddresses, or `undefined` for the native QUIC dual-stack defaults. */
|
|
35
36
|
readonly listen?: string[];
|
|
@@ -109,6 +110,17 @@ export function resolveEndpointConfig(
|
|
|
109
110
|
autonatServers: [...(config.autonatServers ?? [])],
|
|
110
111
|
discovery: resolveDiscovery(config.discovery),
|
|
111
112
|
forceRelay: config.forceRelay ?? false,
|
|
113
|
+
inboundStreamLimits: Object.fromEntries(
|
|
114
|
+
Object.entries(config.inboundStreamLimits ?? {}).map(
|
|
115
|
+
([protocol, limit]) => {
|
|
116
|
+
const name = `inboundStreamLimits[${JSON.stringify(protocol)}]`;
|
|
117
|
+
if (u32(limit, name) === 0) {
|
|
118
|
+
throw new RangeError(`${name} must be a positive integer`);
|
|
119
|
+
}
|
|
120
|
+
return [protocol, limit];
|
|
121
|
+
}
|
|
122
|
+
)
|
|
123
|
+
),
|
|
112
124
|
limits: config.limits && resolveLimits(config.limits),
|
|
113
125
|
listen: config.listen && [...config.listen],
|
|
114
126
|
mdns: resolveMdns(config.mdns, config.discovery),
|
|
@@ -220,7 +232,6 @@ function resolveQuic(quic: Minip2pQuicLimits): BackendQuicLimits {
|
|
|
220
232
|
quic.streamReceiveWindow,
|
|
221
233
|
"quic.streamReceiveWindow"
|
|
222
234
|
),
|
|
223
|
-
// Range-checked natively, which names the field.
|
|
224
235
|
timeThreshold: quic.timeThreshold,
|
|
225
236
|
webtransportHandshakeTimeoutMs: optionalU64(
|
|
226
237
|
quic.webtransportHandshakeTimeoutMs,
|
package/src/sdk.ts
CHANGED
|
@@ -134,8 +134,6 @@ type ConnectTerminal =
|
|
|
134
134
|
readonly error: Error;
|
|
135
135
|
};
|
|
136
136
|
|
|
137
|
-
// A tracked Connection attempt holds either its not-yet-consumed terminal or
|
|
138
|
-
// at most one local wait. The attempt outlives a wait that ends early.
|
|
139
137
|
interface ConnectAttempt {
|
|
140
138
|
terminal?: ConnectTerminal;
|
|
141
139
|
settle?: (terminal: ConnectTerminal) => void;
|
|
@@ -231,7 +229,6 @@ class Fifo<Item> {
|
|
|
231
229
|
const item = this.#items[this.#head];
|
|
232
230
|
this.#items[this.#head] = undefined;
|
|
233
231
|
this.#head += 1;
|
|
234
|
-
// Drop the consumed prefix once it is at least half the array.
|
|
235
232
|
if (this.#head * 2 >= this.#items.length) {
|
|
236
233
|
this.#items = this.#items.slice(this.#head);
|
|
237
234
|
this.#head = 0;
|
|
@@ -451,7 +448,6 @@ class EventQueue {
|
|
|
451
448
|
newest === undefined ? undefined : coalesceData(newest, item);
|
|
452
449
|
if (merged !== undefined && this.#newest !== undefined) {
|
|
453
450
|
this.#items.set(this.#newest, merged);
|
|
454
|
-
// The absorbed event is handled: its adapter state goes now.
|
|
455
451
|
this.#release(item);
|
|
456
452
|
return undefined;
|
|
457
453
|
}
|
|
@@ -897,8 +893,6 @@ export class Stream {
|
|
|
897
893
|
this.#emitData(chunk);
|
|
898
894
|
return;
|
|
899
895
|
}
|
|
900
|
-
// Unbounded here, because native delivers at most one receive window
|
|
901
|
-
// that has not been consumed.
|
|
902
896
|
this.#fifo.push(chunk);
|
|
903
897
|
this.#fifoBytes += chunk.byteLength;
|
|
904
898
|
}
|
|
@@ -957,8 +951,6 @@ export class Stream {
|
|
|
957
951
|
}
|
|
958
952
|
this.#closed = true;
|
|
959
953
|
this.#failWrites(error);
|
|
960
|
-
// StreamClosed carries no cause. Once the remote FIN establishes EOF,
|
|
961
|
-
// later full closure cannot reinterpret that receive-side outcome.
|
|
962
954
|
const hasRemoteEof =
|
|
963
955
|
options.preserveRemoteEof === true && this.#receiveState.kind === "eof";
|
|
964
956
|
if (!hasRemoteEof) {
|
|
@@ -1049,7 +1041,6 @@ export class Stream {
|
|
|
1049
1041
|
|
|
1050
1042
|
/** Hands `chunk` to every `data` handler; it is consumed once they return. */
|
|
1051
1043
|
#emitData(chunk: Uint8Array): void {
|
|
1052
|
-
// Counted first: a handler may transfer the buffer, detaching it.
|
|
1053
1044
|
const bytes = chunk.byteLength;
|
|
1054
1045
|
this.#emit("data", chunk);
|
|
1055
1046
|
this.#consumed(bytes);
|
|
@@ -1519,7 +1510,6 @@ export class Minip2pBase {
|
|
|
1519
1510
|
readonly peerId: string;
|
|
1520
1511
|
readonly protocols: readonly string[];
|
|
1521
1512
|
}> {
|
|
1522
|
-
// One snapshot, so readiness, connection and protocols agree.
|
|
1523
1513
|
const info = this.connectionInfo(peerId);
|
|
1524
1514
|
if (info?.readyProtocols !== undefined) {
|
|
1525
1515
|
return Promise.resolve({ peerId, protocols: info.readyProtocols });
|
|
@@ -1532,9 +1522,6 @@ export class Minip2pBase {
|
|
|
1532
1522
|
controller.abort();
|
|
1533
1523
|
});
|
|
1534
1524
|
const timeoutMs = options.timeoutMs ?? this.#readyTimeoutMs;
|
|
1535
|
-
// Events trail the native state, so a queued `peerReady` counts only if
|
|
1536
|
-
// its connection is still the peer's current one; a replaced
|
|
1537
|
-
// connection's readiness never resolves the wait.
|
|
1538
1525
|
const ready = this.waitFor("peerReady", {
|
|
1539
1526
|
...options,
|
|
1540
1527
|
predicate: (event) =>
|
|
@@ -1543,9 +1530,6 @@ export class Minip2pBase {
|
|
|
1543
1530
|
signal: controller.signal,
|
|
1544
1531
|
timeoutMs,
|
|
1545
1532
|
}).then(({ protocols }) => ({ peerId, protocols }));
|
|
1546
|
-
// The failed connection may already be gone natively, so a failure
|
|
1547
|
-
// counts if it names the peer's current connection or, once native has
|
|
1548
|
-
// none, the last one this wait knew.
|
|
1549
1533
|
let knownConnId = info?.connId;
|
|
1550
1534
|
const identifyFailed = this.waitFor("identifyFailed", {
|
|
1551
1535
|
...options,
|
|
@@ -1591,13 +1575,8 @@ export class Minip2pBase {
|
|
|
1591
1575
|
this.#assertOpen();
|
|
1592
1576
|
let operation = this.#pings.get(peerId);
|
|
1593
1577
|
if (operation === undefined) {
|
|
1594
|
-
// Native events are flushed asynchronously, so none can arrive before
|
|
1595
|
-
// the operation below listens.
|
|
1596
1578
|
this.#backend.ping(peerId);
|
|
1597
1579
|
operation = this.#runPing(peerId);
|
|
1598
|
-
// Kept until its terminal event, even after every caller stops
|
|
1599
|
-
// waiting: a later call joins the native attempt still running, so an
|
|
1600
|
-
// older attempt's result never settles a newer one.
|
|
1601
1580
|
this.#pings.set(peerId, operation);
|
|
1602
1581
|
const current = operation;
|
|
1603
1582
|
void operation.promise.then(
|
|
@@ -1623,7 +1602,6 @@ export class Minip2pBase {
|
|
|
1623
1602
|
let identity: { readonly connId: number; readonly streamId: number };
|
|
1624
1603
|
try {
|
|
1625
1604
|
identity = this.#backend.openStream(peerId, protocolId);
|
|
1626
|
-
// The application now knows the connection: its end must reach it.
|
|
1627
1605
|
this.#queue.markConnectionSeen(identity.connId);
|
|
1628
1606
|
} catch (error) {
|
|
1629
1607
|
return Promise.reject(
|
|
@@ -1698,7 +1676,6 @@ export class Minip2pBase {
|
|
|
1698
1676
|
options: ConnectOptions = {}
|
|
1699
1677
|
): Promise<ConnectResult> {
|
|
1700
1678
|
this.#assertOpen();
|
|
1701
|
-
// Validate before admitting, so a bad option never orphans an attempt.
|
|
1702
1679
|
assertTimeout(options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
1703
1680
|
if (options.signal?.aborted === true) {
|
|
1704
1681
|
return Promise.reject(new AbortError());
|
|
@@ -1744,15 +1721,12 @@ export class Minip2pBase {
|
|
|
1744
1721
|
removeAbort?.();
|
|
1745
1722
|
attempt.settle = undefined;
|
|
1746
1723
|
};
|
|
1747
|
-
// Abort only requests cancellation. Drop the local timeout so it cannot
|
|
1748
|
-
// reject with TimeoutError before the terminal settles this wait.
|
|
1749
1724
|
const cancelForAbort = () => {
|
|
1750
1725
|
clearTimeout(timer);
|
|
1751
1726
|
this.#requestCancel(connectId);
|
|
1752
1727
|
};
|
|
1753
1728
|
const removeAbort = listenAbort(options.signal, cancelForAbort);
|
|
1754
1729
|
if (timeoutMs > 0) {
|
|
1755
|
-
// Ends this wait only; the attempt stays tracked for its terminal.
|
|
1756
1730
|
timer = setTimeout(() => {
|
|
1757
1731
|
stopWaiting();
|
|
1758
1732
|
reject(new TimeoutError(timeoutMs));
|
|
@@ -1861,8 +1835,6 @@ export class Minip2pBase {
|
|
|
1861
1835
|
ping.cancel(error);
|
|
1862
1836
|
}
|
|
1863
1837
|
this.#pings.clear();
|
|
1864
|
-
// Connection attempts correlate exactly: only a dropped terminal, or a
|
|
1865
|
-
// dropped native loss report naming it, settles its attempt.
|
|
1866
1838
|
if (dropped.source === "native") {
|
|
1867
1839
|
const { event } = dropped;
|
|
1868
1840
|
if (isConnectTerminal(event)) {
|
|
@@ -1961,7 +1933,6 @@ export class Minip2pBase {
|
|
|
1961
1933
|
}
|
|
1962
1934
|
|
|
1963
1935
|
#dispatchNative(event: P2pEvent): void {
|
|
1964
|
-
// Backends resolve `close` with it instead of delivering it.
|
|
1965
1936
|
if (event.tag === P2pEvent_Tags.EndpointClosed) {
|
|
1966
1937
|
return;
|
|
1967
1938
|
}
|
|
@@ -1994,8 +1965,6 @@ export class Minip2pBase {
|
|
|
1994
1965
|
);
|
|
1995
1966
|
}
|
|
1996
1967
|
if (event.tag === P2pEvent_Tags.ConnectionReplaced) {
|
|
1997
|
-
// Native ends the replaced connection's streams without per-stream
|
|
1998
|
-
// terminal events, so they end here.
|
|
1999
1968
|
this.#connectionEnded(
|
|
2000
1969
|
event.inner.oldConnId,
|
|
2001
1970
|
new StreamClosedError("The stream's connection was replaced"),
|
|
@@ -2009,8 +1978,6 @@ export class Minip2pBase {
|
|
|
2009
1978
|
}
|
|
2010
1979
|
if (event.tag === P2pEvent_Tags.EventsDropped) {
|
|
2011
1980
|
this.#connectResultsLost(event.inner.terminalConnectIds);
|
|
2012
|
-
// A ping's result may be among the lost events, and a ping kept
|
|
2013
|
-
// without it would never settle, so end every pending ping.
|
|
2014
1981
|
for (const ping of this.#pings.values()) {
|
|
2015
1982
|
ping.cancel(new EventQueueOverflowError());
|
|
2016
1983
|
}
|
|
@@ -2033,7 +2000,6 @@ export class Minip2pBase {
|
|
|
2033
2000
|
: never
|
|
2034
2001
|
): boolean {
|
|
2035
2002
|
let claimed = false;
|
|
2036
|
-
// Built only once a waiter of this type is found.
|
|
2037
2003
|
let result: ({ readonly type: Kind } & typeof payload) | undefined;
|
|
2038
2004
|
for (const waiter of this.#waiters.size > 0 ? [...this.#waiters] : []) {
|
|
2039
2005
|
if (waiter.type !== type) {
|
|
@@ -2068,7 +2034,6 @@ export class Minip2pBase {
|
|
|
2068
2034
|
}
|
|
2069
2035
|
}
|
|
2070
2036
|
if (type !== "stream" && this.#catchAll.size > 0) {
|
|
2071
|
-
// Its own object: a waiter's resolved event is never a catch-all's.
|
|
2072
2037
|
const catchEvent = eventResult(
|
|
2073
2038
|
type as keyof Minip2pCatchAllEventMap,
|
|
2074
2039
|
catchPayload as Minip2pCatchAllEventMap[keyof Minip2pCatchAllEventMap]
|
|
@@ -2155,9 +2120,6 @@ export class Minip2pBase {
|
|
|
2155
2120
|
});
|
|
2156
2121
|
} else if (event.tag === P2pEvent_Tags.StreamData) {
|
|
2157
2122
|
if (stream === undefined) {
|
|
2158
|
-
// Nothing will read it, so it is consumed now: an abandon that
|
|
2159
|
-
// native refused (the stream had already closed there) leaves the
|
|
2160
|
-
// bytes holding the native stream slot otherwise.
|
|
2161
2123
|
consumeUntracked(this.#backend, event.inner);
|
|
2162
2124
|
} else {
|
|
2163
2125
|
stream.receive(event.inner.data);
|
|
@@ -2252,9 +2214,6 @@ export class Minip2pBase {
|
|
|
2252
2214
|
this.#settleConnect(event.inner.connectId, attempt, terminal);
|
|
2253
2215
|
}
|
|
2254
2216
|
|
|
2255
|
-
// Terminals dropped by either event carry settle exactly their attempts
|
|
2256
|
-
// with a delivery-loss error so waits cannot hang. Both carries name every
|
|
2257
|
-
// dropped terminal, so every other attempt still receives its own.
|
|
2258
2217
|
#connectResultsLost(connectIds: readonly number[]): void {
|
|
2259
2218
|
for (const connectId of connectIds) {
|
|
2260
2219
|
const attempt = this.#connects.get(connectId);
|
|
@@ -2268,7 +2227,6 @@ export class Minip2pBase {
|
|
|
2268
2227
|
}
|
|
2269
2228
|
}
|
|
2270
2229
|
|
|
2271
|
-
// Hands a terminal to the pending wait, or keeps it for a later one.
|
|
2272
2230
|
#settleConnect(
|
|
2273
2231
|
connectId: number,
|
|
2274
2232
|
attempt: ConnectAttempt,
|
|
@@ -2292,9 +2250,6 @@ export class Minip2pBase {
|
|
|
2292
2250
|
}
|
|
2293
2251
|
}
|
|
2294
2252
|
|
|
2295
|
-
// Cancellation requested from a signal or timer has no caller to throw to.
|
|
2296
|
-
// Native cancel fails only once the endpoint stops, and teardown then
|
|
2297
|
-
// settles the wait.
|
|
2298
2253
|
#requestCancel(connectId: number): void {
|
|
2299
2254
|
if (this.#closed) {
|
|
2300
2255
|
return;
|
|
@@ -2363,8 +2318,6 @@ export class Minip2pBase {
|
|
|
2363
2318
|
this.#dropped = 0;
|
|
2364
2319
|
this.#named.clear();
|
|
2365
2320
|
this.#catchAll.clear();
|
|
2366
|
-
// Native is asked to close before anything below can call back in: a
|
|
2367
|
-
// re-entrant close returns this promise.
|
|
2368
2321
|
let closed: Promise<BackendClosed>;
|
|
2369
2322
|
try {
|
|
2370
2323
|
closed = this.#backend.close(timeoutMs);
|
|
@@ -2439,7 +2392,6 @@ function isConnectTerminal(event: P2pEvent): event is ConnectTerminalEvent {
|
|
|
2439
2392
|
);
|
|
2440
2393
|
}
|
|
2441
2394
|
|
|
2442
|
-
// A string starting with `/` is a multiaddress; any other string is a peer ID.
|
|
2443
2395
|
function toBackendTarget(target: ConnectTarget): BackendConnectTarget {
|
|
2444
2396
|
if (typeof target !== "string") {
|
|
2445
2397
|
return { addresses: [...target], kind: "addresses" };
|
|
@@ -2519,7 +2471,6 @@ function normalizeEvent(
|
|
|
2519
2471
|
payload: AnyPayload;
|
|
2520
2472
|
} {
|
|
2521
2473
|
const type = SDK_EVENT_NAMES[event.tag];
|
|
2522
|
-
// A native variant without fields arrives without `inner`.
|
|
2523
2474
|
let payload: unknown = "inner" in event ? event.inner : {};
|
|
2524
2475
|
if (event.tag === P2pEvent_Tags.PathEstablished) {
|
|
2525
2476
|
payload = { ...event.inner, path: normalizePath(event.inner.path) };
|
package/src/serde-backend.ts
CHANGED
|
@@ -156,7 +156,6 @@ export class SerdeBackend implements Minip2pBackend {
|
|
|
156
156
|
});
|
|
157
157
|
return;
|
|
158
158
|
}
|
|
159
|
-
// The SDK has stopped listening; only `EndpointClosed` matters now.
|
|
160
159
|
if (this.#close.requested) {
|
|
161
160
|
return;
|
|
162
161
|
}
|
|
@@ -393,8 +392,6 @@ export class SerdeBackend implements Minip2pBackend {
|
|
|
393
392
|
};
|
|
394
393
|
}
|
|
395
394
|
|
|
396
|
-
// Connect IDs are not mapped because they round-trip into native calls,
|
|
397
|
-
// and native allocates them well inside the safe integer range.
|
|
398
395
|
connect(target: BackendConnectTarget): number {
|
|
399
396
|
return u64ToNumber(
|
|
400
397
|
this.#native((native) =>
|
|
@@ -550,8 +547,6 @@ function normalizeNativeValue(
|
|
|
550
547
|
if (key !== undefined && CONNECTION_ID_KEYS.has(key)) {
|
|
551
548
|
return maps.connectionIds.toPublicInEvent(tag, key, BigInt(value));
|
|
552
549
|
}
|
|
553
|
-
// QUIC stop and close codes are 62-bit varints the remote picks: keep
|
|
554
|
-
// them whole.
|
|
555
550
|
if (key === "errorCode" || key === "code") {
|
|
556
551
|
return BigInt(value);
|
|
557
552
|
}
|
|
@@ -574,8 +569,6 @@ function normalizeNativeValue(
|
|
|
574
569
|
);
|
|
575
570
|
const { connId, streamId } = record;
|
|
576
571
|
if (typeof streamId === "bigint" || typeof streamId === "number") {
|
|
577
|
-
// Public stream IDs are allocated per connection. A record without a
|
|
578
|
-
// connection (an endpoint error, say) cannot name a public stream.
|
|
579
572
|
if (typeof connId === "number") {
|
|
580
573
|
record.streamId = maps.streamIds.toPublic(connId, BigInt(streamId));
|
|
581
574
|
} else {
|
package/src/types.ts
CHANGED
|
@@ -283,6 +283,16 @@ export interface Minip2pConfig {
|
|
|
283
283
|
readonly allowUnsigned?: boolean;
|
|
284
284
|
/** Application protocol IDs accepted on inbound streams. */
|
|
285
285
|
readonly protocols?: readonly string[];
|
|
286
|
+
/**
|
|
287
|
+
* Positive u32 inbound stream caps per protocol, across all peers. Entries
|
|
288
|
+
* also register their protocol. Excess streams wait FIFO until a slot opens;
|
|
289
|
+
* outbound streams do not count. Omitted protocols remain uncapped.
|
|
290
|
+
* Native QUIC requires `limits.quic.connectionReceiveWindow` to be unlimited
|
|
291
|
+
* (the default), so queued streams cannot exhaust shared credit and prevent
|
|
292
|
+
* admitted streams from finishing. TCP/Yamux has no connection-level receive
|
|
293
|
+
* window and is unaffected.
|
|
294
|
+
*/
|
|
295
|
+
readonly inboundStreamLimits?: Readonly<Record<string, number>>;
|
|
286
296
|
/** Signed-beacon discovery configuration. */
|
|
287
297
|
readonly discovery?: Minip2pDiscoveryOptions;
|
|
288
298
|
/** Enables mDNS with defaults or explicit options. */
|
|
@@ -937,7 +947,6 @@ export type P2pEvent =
|
|
|
937
947
|
typeof P2pEvent_Tags.MdnsFailed,
|
|
938
948
|
Minip2pNamedEventMap["mdnsFailed"]
|
|
939
949
|
>
|
|
940
|
-
// A variant without fields has no `inner`, as both runtimes deliver it.
|
|
941
950
|
| { readonly tag: typeof P2pEvent_Tags.MdnsRecovered }
|
|
942
951
|
| RawEvent<
|
|
943
952
|
typeof P2pEvent_Tags.DiscoveryProtocolViolation,
|