@minip2p/core 0.7.5 → 0.7.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/sdk.d.ts.map +1 -1
- package/dist/sdk.js +165 -110
- package/dist/sdk.js.map +1 -1
- package/package.json +1 -1
- package/src/sdk.ts +214 -149
package/src/sdk.ts
CHANGED
|
@@ -34,6 +34,7 @@ import type {
|
|
|
34
34
|
OnceOptions,
|
|
35
35
|
OpOptions,
|
|
36
36
|
P2pEvent,
|
|
37
|
+
P2pEventTag,
|
|
37
38
|
Path,
|
|
38
39
|
PathKind,
|
|
39
40
|
Reachability,
|
|
@@ -185,6 +186,40 @@ class BoundedQueue<Item> {
|
|
|
185
186
|
}
|
|
186
187
|
}
|
|
187
188
|
|
|
189
|
+
/** An unbounded queue whose `shift` is O(1), unlike `Array#shift`. */
|
|
190
|
+
class Fifo<Item> {
|
|
191
|
+
#items: (Item | undefined)[] = [];
|
|
192
|
+
#head = 0;
|
|
193
|
+
|
|
194
|
+
get length(): number {
|
|
195
|
+
return this.#items.length - this.#head;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
push(item: Item): void {
|
|
199
|
+
this.#items.push(item);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
shift(): Item | undefined {
|
|
203
|
+
if (this.#head === this.#items.length) {
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
const item = this.#items[this.#head];
|
|
207
|
+
this.#items[this.#head] = undefined;
|
|
208
|
+
this.#head += 1;
|
|
209
|
+
// Drop the consumed prefix once it is at least half the array.
|
|
210
|
+
if (this.#head * 2 >= this.#items.length) {
|
|
211
|
+
this.#items = this.#items.slice(this.#head);
|
|
212
|
+
this.#head = 0;
|
|
213
|
+
}
|
|
214
|
+
return item;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
clear(): void {
|
|
218
|
+
this.#items = [];
|
|
219
|
+
this.#head = 0;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
188
223
|
/**
|
|
189
224
|
* Native events that are never dropped, because stream and connection state
|
|
190
225
|
* depends on each one (ADR 0012), plus the driver failure that ends them.
|
|
@@ -197,6 +232,11 @@ const LOSSLESS_TAGS: ReadonlySet<string> = new Set<string>([
|
|
|
197
232
|
P2pEvent_Tags.DriverFailed,
|
|
198
233
|
]);
|
|
199
234
|
|
|
235
|
+
type StreamReadyEvent = Extract<
|
|
236
|
+
P2pEvent,
|
|
237
|
+
{ readonly tag: typeof P2pEvent_Tags.StreamReady }
|
|
238
|
+
>;
|
|
239
|
+
|
|
200
240
|
type StreamDataEvent = Extract<
|
|
201
241
|
P2pEvent,
|
|
202
242
|
{ readonly tag: typeof P2pEvent_Tags.StreamData }
|
|
@@ -234,17 +274,68 @@ const coalesceData = (
|
|
|
234
274
|
return { event, source: "native" };
|
|
235
275
|
};
|
|
236
276
|
|
|
237
|
-
/**
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
277
|
+
/**
|
|
278
|
+
* Values keyed by a stream's public connection and stream IDs, without
|
|
279
|
+
* building a key per lookup. Public connection IDs never repeat and each
|
|
280
|
+
* names one peer's connection, so the pair names the stream.
|
|
281
|
+
*/
|
|
282
|
+
class StreamTable<Value> {
|
|
283
|
+
readonly #byConnection = new Map<number, Map<number, Value>>();
|
|
284
|
+
|
|
285
|
+
get(connId: number, streamId: number): Value | undefined {
|
|
286
|
+
return this.#byConnection.get(connId)?.get(streamId);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
has(connId: number, streamId: number): boolean {
|
|
290
|
+
return this.#byConnection.get(connId)?.has(streamId) ?? false;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
set(connId: number, streamId: number, value: Value): void {
|
|
294
|
+
let streams = this.#byConnection.get(connId);
|
|
295
|
+
if (streams === undefined) {
|
|
296
|
+
streams = new Map();
|
|
297
|
+
this.#byConnection.set(connId, streams);
|
|
298
|
+
}
|
|
299
|
+
streams.set(streamId, value);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
delete(connId: number, streamId: number): void {
|
|
303
|
+
const streams = this.#byConnection.get(connId);
|
|
304
|
+
if (streams?.delete(streamId) === true && streams.size === 0) {
|
|
305
|
+
this.#byConnection.delete(connId);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** Removes and returns every value on `connId`. */
|
|
310
|
+
takeConnection(connId: number): Value[] {
|
|
311
|
+
const streams = this.#byConnection.get(connId);
|
|
312
|
+
this.#byConnection.delete(connId);
|
|
313
|
+
return streams === undefined ? [] : [...streams.values()];
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** A snapshot of every value. */
|
|
317
|
+
values(): Value[] {
|
|
318
|
+
return [...this.#byConnection.values()].flatMap((streams) => [
|
|
319
|
+
...streams.values(),
|
|
320
|
+
]);
|
|
244
321
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
322
|
+
|
|
323
|
+
/** Removes and returns every value. */
|
|
324
|
+
takeAll(): Value[] {
|
|
325
|
+
const values = this.values();
|
|
326
|
+
this.#byConnection.clear();
|
|
327
|
+
return values;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** The native per-stream event an item carries, if any. */
|
|
332
|
+
const streamEventOf = (
|
|
333
|
+
item: QueueItem
|
|
334
|
+
): StreamP2pEvent | StreamReadyEvent | undefined =>
|
|
335
|
+
item.source === "native" &&
|
|
336
|
+
(item.event.tag === P2pEvent_Tags.StreamReady || isStreamEvent(item.event))
|
|
337
|
+
? item.event
|
|
338
|
+
: undefined;
|
|
248
339
|
|
|
249
340
|
/**
|
|
250
341
|
* The endpoint's shared event queue, in delivery order.
|
|
@@ -267,7 +358,7 @@ class EventQueue {
|
|
|
267
358
|
* Peer-opened streams whose `StreamReady` is still queued, with their
|
|
268
359
|
* queued item ids; `undefined` once one carried data.
|
|
269
360
|
*/
|
|
270
|
-
readonly #unseen = new
|
|
361
|
+
readonly #unseen = new StreamTable<number[] | undefined>();
|
|
271
362
|
readonly #capacity: number;
|
|
272
363
|
readonly #release: (item: QueueItem) => void;
|
|
273
364
|
#next = 0;
|
|
@@ -323,7 +414,7 @@ class EventQueue {
|
|
|
323
414
|
item?.source === "native" &&
|
|
324
415
|
item.event.tag === P2pEvent_Tags.StreamReady
|
|
325
416
|
) {
|
|
326
|
-
this.#unseen.delete(
|
|
417
|
+
this.#unseen.delete(item.event.inner.connId, item.event.inner.streamId);
|
|
327
418
|
}
|
|
328
419
|
return item;
|
|
329
420
|
}
|
|
@@ -333,25 +424,25 @@ class EventQueue {
|
|
|
333
424
|
* it closed one that carried no data, which leaves the queue whole.
|
|
334
425
|
*/
|
|
335
426
|
#elideUnseen(item: QueueItem, id: number): boolean {
|
|
336
|
-
const
|
|
337
|
-
if (
|
|
427
|
+
const event = streamEventOf(item);
|
|
428
|
+
if (event === undefined) {
|
|
338
429
|
return false;
|
|
339
430
|
}
|
|
340
|
-
const {
|
|
431
|
+
const { connId, streamId } = event.inner;
|
|
341
432
|
if (event.tag === P2pEvent_Tags.StreamReady) {
|
|
342
433
|
if (!event.inner.initiatedLocally) {
|
|
343
|
-
this.#unseen.set(
|
|
434
|
+
this.#unseen.set(connId, streamId, [id]);
|
|
344
435
|
}
|
|
345
436
|
return false;
|
|
346
437
|
}
|
|
347
|
-
if (!this.#unseen.has(
|
|
438
|
+
if (!this.#unseen.has(connId, streamId)) {
|
|
348
439
|
return false;
|
|
349
440
|
}
|
|
350
|
-
const ids = this.#unseen.get(
|
|
441
|
+
const ids = this.#unseen.get(connId, streamId);
|
|
351
442
|
if (event.tag === P2pEvent_Tags.StreamData) {
|
|
352
|
-
this.#unseen.set(
|
|
443
|
+
this.#unseen.set(connId, streamId, undefined);
|
|
353
444
|
} else if (ids !== undefined && event.tag === P2pEvent_Tags.StreamClosed) {
|
|
354
|
-
this.#unseen.delete(
|
|
445
|
+
this.#unseen.delete(connId, streamId);
|
|
355
446
|
for (const queued of ids) {
|
|
356
447
|
const removed = this.#remove(queued);
|
|
357
448
|
if (removed !== undefined) {
|
|
@@ -397,7 +488,7 @@ export class Stream {
|
|
|
397
488
|
keyof StreamEventMap,
|
|
398
489
|
Set<(payload: StreamEventMap[keyof StreamEventMap]) => void>
|
|
399
490
|
>();
|
|
400
|
-
readonly #fifo
|
|
491
|
+
readonly #fifo = new Fifo<Uint8Array>();
|
|
401
492
|
readonly #reads: PendingRead[] = [];
|
|
402
493
|
#fifoBytes = 0;
|
|
403
494
|
#mode: "pull" | "flowing" | undefined;
|
|
@@ -598,7 +689,7 @@ export class Stream {
|
|
|
598
689
|
*/
|
|
599
690
|
#discardUnread(): void {
|
|
600
691
|
this.#consumed(this.#fifoBytes);
|
|
601
|
-
this.#fifo.
|
|
692
|
+
this.#fifo.clear();
|
|
602
693
|
this.#fifoBytes = 0;
|
|
603
694
|
}
|
|
604
695
|
|
|
@@ -892,8 +983,8 @@ export class Minip2pBase {
|
|
|
892
983
|
});
|
|
893
984
|
readonly #connects = new Map<number, ConnectAttempt>();
|
|
894
985
|
readonly #terminalConnects = new Set<number>();
|
|
895
|
-
readonly #streams = new
|
|
896
|
-
readonly #pendingOpens = new
|
|
986
|
+
readonly #streams = new StreamTable<Stream>();
|
|
987
|
+
readonly #pendingOpens = new StreamTable<PendingOpen>();
|
|
897
988
|
readonly #pings = new Map<string, PingOperation>();
|
|
898
989
|
#dropped = 0;
|
|
899
990
|
#flushScheduled = false;
|
|
@@ -1316,8 +1407,10 @@ export class Minip2pBase {
|
|
|
1316
1407
|
pending.removeAbort = listenAbort(options.signal, () => {
|
|
1317
1408
|
this.#expireOpen(pending, new AbortError());
|
|
1318
1409
|
});
|
|
1319
|
-
const
|
|
1320
|
-
|
|
1410
|
+
const overwritten = this.#pendingOpens.get(
|
|
1411
|
+
identity.connId,
|
|
1412
|
+
identity.streamId
|
|
1413
|
+
);
|
|
1321
1414
|
if (overwritten !== undefined) {
|
|
1322
1415
|
clearPendingOpen(overwritten);
|
|
1323
1416
|
overwritten.reject(
|
|
@@ -1330,7 +1423,7 @@ export class Minip2pBase {
|
|
|
1330
1423
|
})
|
|
1331
1424
|
);
|
|
1332
1425
|
}
|
|
1333
|
-
this.#pendingOpens.set(
|
|
1426
|
+
this.#pendingOpens.set(identity.connId, identity.streamId, pending);
|
|
1334
1427
|
});
|
|
1335
1428
|
}
|
|
1336
1429
|
|
|
@@ -1532,7 +1625,7 @@ export class Minip2pBase {
|
|
|
1532
1625
|
this.#connectResultsLost(event.inner.terminalConnectIds);
|
|
1533
1626
|
}
|
|
1534
1627
|
}
|
|
1535
|
-
for (const pending of this.#pendingOpens.
|
|
1628
|
+
for (const pending of this.#pendingOpens.takeAll()) {
|
|
1536
1629
|
clearPendingOpen(pending);
|
|
1537
1630
|
pending.reject(error);
|
|
1538
1631
|
try {
|
|
@@ -1545,7 +1638,6 @@ export class Minip2pBase {
|
|
|
1545
1638
|
// The operation is already failed; native cleanup is best effort.
|
|
1546
1639
|
}
|
|
1547
1640
|
}
|
|
1548
|
-
this.#pendingOpens.clear();
|
|
1549
1641
|
}
|
|
1550
1642
|
|
|
1551
1643
|
#enqueueNative(event: P2pEvent): void {
|
|
@@ -1650,7 +1742,6 @@ export class Minip2pBase {
|
|
|
1650
1742
|
}
|
|
1651
1743
|
if (event.tag === P2pEvent_Tags.ConnectionClosed) {
|
|
1652
1744
|
this.#connectionEnded(
|
|
1653
|
-
event.inner.peerId,
|
|
1654
1745
|
event.inner.connId,
|
|
1655
1746
|
new PeerDisconnectedError(event.inner.peerId, "stream"),
|
|
1656
1747
|
new PeerDisconnectedError(event.inner.peerId, "openStream")
|
|
@@ -1660,7 +1751,6 @@ export class Minip2pBase {
|
|
|
1660
1751
|
// Native ends the replaced connection's streams without per-stream
|
|
1661
1752
|
// terminal events, so they end here.
|
|
1662
1753
|
this.#connectionEnded(
|
|
1663
|
-
event.inner.peerId,
|
|
1664
1754
|
event.inner.oldConnId,
|
|
1665
1755
|
new StreamClosedError("The stream's connection was replaced"),
|
|
1666
1756
|
new StreamClosedError(
|
|
@@ -1676,13 +1766,11 @@ export class Minip2pBase {
|
|
|
1676
1766
|
}
|
|
1677
1767
|
|
|
1678
1768
|
const normalized = normalizeEvent(event);
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
);
|
|
1685
|
-
}
|
|
1769
|
+
this.#dispatch(
|
|
1770
|
+
normalized.type as never,
|
|
1771
|
+
normalized.payload as never,
|
|
1772
|
+
normalized.payload as never
|
|
1773
|
+
);
|
|
1686
1774
|
}
|
|
1687
1775
|
|
|
1688
1776
|
#dispatch<Kind extends EventKind>(
|
|
@@ -1693,11 +1781,13 @@ export class Minip2pBase {
|
|
|
1693
1781
|
: never
|
|
1694
1782
|
): boolean {
|
|
1695
1783
|
let claimed = false;
|
|
1696
|
-
|
|
1697
|
-
|
|
1784
|
+
// Built only once a waiter of this type is found.
|
|
1785
|
+
let result: ({ readonly type: Kind } & typeof payload) | undefined;
|
|
1786
|
+
for (const waiter of this.#waiters.size > 0 ? [...this.#waiters] : []) {
|
|
1698
1787
|
if (waiter.type !== type) {
|
|
1699
1788
|
continue;
|
|
1700
1789
|
}
|
|
1790
|
+
result ??= eventResult(type, payload);
|
|
1701
1791
|
let matched = false;
|
|
1702
1792
|
try {
|
|
1703
1793
|
matched = waiter.predicate(result as never);
|
|
@@ -1725,7 +1815,8 @@ export class Minip2pBase {
|
|
|
1725
1815
|
this.#handlerFailed(type, payload, error);
|
|
1726
1816
|
}
|
|
1727
1817
|
}
|
|
1728
|
-
if (type !== "stream") {
|
|
1818
|
+
if (type !== "stream" && this.#catchAll.size > 0) {
|
|
1819
|
+
// Its own object: a waiter's resolved event is never a catch-all's.
|
|
1729
1820
|
const catchEvent = eventResult(
|
|
1730
1821
|
type as keyof Minip2pCatchAllEventMap,
|
|
1731
1822
|
catchPayload as Minip2pCatchAllEventMap[keyof Minip2pCatchAllEventMap]
|
|
@@ -1756,15 +1847,13 @@ export class Minip2pBase {
|
|
|
1756
1847
|
}
|
|
1757
1848
|
|
|
1758
1849
|
#streamReady(meta: InboundStreamMeta): void {
|
|
1759
|
-
const key = streamKey(meta.peerId, meta.connId, meta.streamId);
|
|
1760
1850
|
const stream = new Stream(this.#backend, meta, () => {
|
|
1761
|
-
this.#streams.delete(
|
|
1851
|
+
this.#streams.delete(meta.connId, meta.streamId);
|
|
1762
1852
|
});
|
|
1763
|
-
this.#streams.set(
|
|
1764
|
-
const
|
|
1765
|
-
const pending = this.#pendingOpens.get(pendingKey);
|
|
1853
|
+
this.#streams.set(meta.connId, meta.streamId, stream);
|
|
1854
|
+
const pending = this.#pendingOpens.get(meta.connId, meta.streamId);
|
|
1766
1855
|
if (pending !== undefined && meta.initiatedLocally) {
|
|
1767
|
-
this.#pendingOpens.delete(
|
|
1856
|
+
this.#pendingOpens.delete(meta.connId, meta.streamId);
|
|
1768
1857
|
clearPendingOpen(pending);
|
|
1769
1858
|
pending.resolve(stream);
|
|
1770
1859
|
return;
|
|
@@ -1800,13 +1889,12 @@ export class Minip2pBase {
|
|
|
1800
1889
|
}
|
|
1801
1890
|
|
|
1802
1891
|
#streamEvent(event: StreamP2pEvent): void {
|
|
1803
|
-
const {
|
|
1804
|
-
const stream = this.#streams.get(
|
|
1892
|
+
const { connId, streamId } = event.inner;
|
|
1893
|
+
const stream = this.#streams.get(connId, streamId);
|
|
1805
1894
|
if (event.tag === P2pEvent_Tags.StreamClosed) {
|
|
1806
|
-
const
|
|
1807
|
-
const pending = this.#pendingOpens.get(pendingKey);
|
|
1895
|
+
const pending = this.#pendingOpens.get(connId, streamId);
|
|
1808
1896
|
if (pending !== undefined) {
|
|
1809
|
-
this.#pendingOpens.delete(
|
|
1897
|
+
this.#pendingOpens.delete(connId, streamId);
|
|
1810
1898
|
clearPendingOpen(pending);
|
|
1811
1899
|
pending.reject(new StreamClosedError());
|
|
1812
1900
|
}
|
|
@@ -1840,12 +1928,14 @@ export class Minip2pBase {
|
|
|
1840
1928
|
if (error.streamId === undefined) {
|
|
1841
1929
|
return;
|
|
1842
1930
|
}
|
|
1843
|
-
const candidates =
|
|
1844
|
-
(
|
|
1845
|
-
|
|
1846
|
-
(
|
|
1847
|
-
|
|
1848
|
-
|
|
1931
|
+
const candidates = this.#pendingOpens
|
|
1932
|
+
.values()
|
|
1933
|
+
.filter(
|
|
1934
|
+
(pending) =>
|
|
1935
|
+
pending.streamId === error.streamId &&
|
|
1936
|
+
(error.peerId === undefined || error.peerId === pending.peerId) &&
|
|
1937
|
+
(error.connId === undefined || error.connId === pending.connId)
|
|
1938
|
+
);
|
|
1849
1939
|
if (candidates.length !== 1) {
|
|
1850
1940
|
return;
|
|
1851
1941
|
}
|
|
@@ -1853,9 +1943,7 @@ export class Minip2pBase {
|
|
|
1853
1943
|
if (pending === undefined) {
|
|
1854
1944
|
return;
|
|
1855
1945
|
}
|
|
1856
|
-
this.#pendingOpens.delete(
|
|
1857
|
-
pendingOpenKey(pending.peerId, pending.connId, pending.streamId)
|
|
1858
|
-
);
|
|
1946
|
+
this.#pendingOpens.delete(pending.connId, pending.streamId);
|
|
1859
1947
|
clearPendingOpen(pending);
|
|
1860
1948
|
pending.reject(
|
|
1861
1949
|
new OpenStreamError({
|
|
@@ -1869,26 +1957,13 @@ export class Minip2pBase {
|
|
|
1869
1957
|
}
|
|
1870
1958
|
|
|
1871
1959
|
/** Ends every stream and pending open on a closed or replaced connection. */
|
|
1872
|
-
#connectionEnded(
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
streamError: Error,
|
|
1876
|
-
openError: Error
|
|
1877
|
-
): void {
|
|
1878
|
-
for (const [key, stream] of [...this.#streams]) {
|
|
1879
|
-
if (stream.peerId === peerId && stream.connId === connId) {
|
|
1880
|
-
stream.terminal(streamError);
|
|
1881
|
-
this.#streams.delete(key);
|
|
1882
|
-
}
|
|
1960
|
+
#connectionEnded(connId: number, streamError: Error, openError: Error): void {
|
|
1961
|
+
for (const stream of this.#streams.takeConnection(connId)) {
|
|
1962
|
+
stream.terminal(streamError);
|
|
1883
1963
|
}
|
|
1884
|
-
for (const pending of
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
pendingOpenKey(pending.peerId, pending.connId, pending.streamId)
|
|
1888
|
-
);
|
|
1889
|
-
clearPendingOpen(pending);
|
|
1890
|
-
pending.reject(openError);
|
|
1891
|
-
}
|
|
1964
|
+
for (const pending of this.#pendingOpens.takeConnection(connId)) {
|
|
1965
|
+
clearPendingOpen(pending);
|
|
1966
|
+
pending.reject(openError);
|
|
1892
1967
|
}
|
|
1893
1968
|
}
|
|
1894
1969
|
|
|
@@ -1985,15 +2060,10 @@ export class Minip2pBase {
|
|
|
1985
2060
|
}
|
|
1986
2061
|
|
|
1987
2062
|
#expireOpen(pending: PendingOpen, error: unknown): void {
|
|
1988
|
-
|
|
1989
|
-
pending.peerId,
|
|
1990
|
-
pending.connId,
|
|
1991
|
-
pending.streamId
|
|
1992
|
-
);
|
|
1993
|
-
if (this.#pendingOpens.get(key) !== pending) {
|
|
2063
|
+
if (this.#pendingOpens.get(pending.connId, pending.streamId) !== pending) {
|
|
1994
2064
|
return;
|
|
1995
2065
|
}
|
|
1996
|
-
this.#pendingOpens.delete(
|
|
2066
|
+
this.#pendingOpens.delete(pending.connId, pending.streamId);
|
|
1997
2067
|
clearPendingOpen(pending);
|
|
1998
2068
|
pending.reject(error);
|
|
1999
2069
|
try {
|
|
@@ -2047,15 +2117,13 @@ export class Minip2pBase {
|
|
|
2047
2117
|
}
|
|
2048
2118
|
this.#connects.clear();
|
|
2049
2119
|
this.#terminalConnects.clear();
|
|
2050
|
-
for (const pending of this.#pendingOpens.
|
|
2120
|
+
for (const pending of this.#pendingOpens.takeAll()) {
|
|
2051
2121
|
clearPendingOpen(pending);
|
|
2052
2122
|
pending.reject(error);
|
|
2053
2123
|
}
|
|
2054
|
-
this.#
|
|
2055
|
-
for (const stream of this.#streams.values()) {
|
|
2124
|
+
for (const stream of this.#streams.takeAll()) {
|
|
2056
2125
|
stream.terminal(error);
|
|
2057
2126
|
}
|
|
2058
|
-
this.#streams.clear();
|
|
2059
2127
|
for (const ping of this.#pings.values()) {
|
|
2060
2128
|
ping.cancel(error);
|
|
2061
2129
|
}
|
|
@@ -2108,6 +2176,52 @@ function toBackendTarget(target: ConnectTarget): BackendConnectTarget {
|
|
|
2108
2176
|
: { kind: "peer", peerId: target };
|
|
2109
2177
|
}
|
|
2110
2178
|
|
|
2179
|
+
/** Native events the SDK handles itself rather than dispatching by name. */
|
|
2180
|
+
type UnnamedEventTag =
|
|
2181
|
+
| StreamP2pEvent["tag"]
|
|
2182
|
+
| typeof P2pEvent_Tags.DriverFailed
|
|
2183
|
+
| typeof P2pEvent_Tags.StreamReady;
|
|
2184
|
+
type NamedEventTag = Exclude<P2pEventTag, UnnamedEventTag>;
|
|
2185
|
+
|
|
2186
|
+
/**
|
|
2187
|
+
* SDK event names for every native event that dispatches by name. Each name
|
|
2188
|
+
* is its tag uncapitalized, so a wrong mapping fails to compile.
|
|
2189
|
+
*/
|
|
2190
|
+
const SDK_EVENT_NAMES: {
|
|
2191
|
+
readonly [Tag in NamedEventTag]: Uncapitalize<Tag>;
|
|
2192
|
+
} = {
|
|
2193
|
+
[P2pEvent_Tags.EventsDropped]: "eventsDropped",
|
|
2194
|
+
[P2pEvent_Tags.ConnectionEstablished]: "connectionEstablished",
|
|
2195
|
+
[P2pEvent_Tags.ConnectionClosed]: "connectionClosed",
|
|
2196
|
+
[P2pEvent_Tags.ConnectionReplaced]: "connectionReplaced",
|
|
2197
|
+
[P2pEvent_Tags.PeerReady]: "peerReady",
|
|
2198
|
+
[P2pEvent_Tags.IdentifyReceived]: "identifyReceived",
|
|
2199
|
+
[P2pEvent_Tags.PingRttMeasured]: "pingRttMeasured",
|
|
2200
|
+
[P2pEvent_Tags.PingTimeout]: "pingTimeout",
|
|
2201
|
+
[P2pEvent_Tags.EndpointError]: "endpointError",
|
|
2202
|
+
[P2pEvent_Tags.ReachabilityChanged]: "reachabilityChanged",
|
|
2203
|
+
[P2pEvent_Tags.PublicAddressesChanged]: "publicAddressesChanged",
|
|
2204
|
+
[P2pEvent_Tags.RelayReserved]: "relayReserved",
|
|
2205
|
+
[P2pEvent_Tags.RelayReservationLost]: "relayReservationLost",
|
|
2206
|
+
[P2pEvent_Tags.PathEstablished]: "pathEstablished",
|
|
2207
|
+
[P2pEvent_Tags.InboundPathEstablished]: "inboundPathEstablished",
|
|
2208
|
+
[P2pEvent_Tags.PathUpgraded]: "pathUpgraded",
|
|
2209
|
+
[P2pEvent_Tags.HolePunchFailed]: "holePunchFailed",
|
|
2210
|
+
[P2pEvent_Tags.ConnectFailed]: "connectFailed",
|
|
2211
|
+
[P2pEvent_Tags.ConnectCancelled]: "connectCancelled",
|
|
2212
|
+
[P2pEvent_Tags.InboundDirectUpgrade]: "inboundDirectUpgrade",
|
|
2213
|
+
[P2pEvent_Tags.Message]: "message",
|
|
2214
|
+
[P2pEvent_Tags.PeerSubscribed]: "peerSubscribed",
|
|
2215
|
+
[P2pEvent_Tags.PeerUnsubscribed]: "peerUnsubscribed",
|
|
2216
|
+
[P2pEvent_Tags.GossipsubOutboundFailure]: "gossipsubOutboundFailure",
|
|
2217
|
+
[P2pEvent_Tags.GossipsubProtocolViolation]: "gossipsubProtocolViolation",
|
|
2218
|
+
[P2pEvent_Tags.PeerDiscovered]: "peerDiscovered",
|
|
2219
|
+
[P2pEvent_Tags.PeerUpdated]: "peerUpdated",
|
|
2220
|
+
[P2pEvent_Tags.PeerExpired]: "peerExpired",
|
|
2221
|
+
[P2pEvent_Tags.DiscoveryDialFailed]: "discoveryDialFailed",
|
|
2222
|
+
[P2pEvent_Tags.DiscoveryProtocolViolation]: "discoveryProtocolViolation",
|
|
2223
|
+
};
|
|
2224
|
+
|
|
2111
2225
|
function normalizeEvent(
|
|
2112
2226
|
event: Exclude<
|
|
2113
2227
|
P2pEvent,
|
|
@@ -2118,51 +2232,14 @@ function normalizeEvent(
|
|
|
2118
2232
|
| typeof P2pEvent_Tags.StreamReady;
|
|
2119
2233
|
}
|
|
2120
2234
|
>
|
|
2121
|
-
):
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
| undefined {
|
|
2130
|
-
const names: Partial<Record<string, EventKind>> = {
|
|
2131
|
-
[P2pEvent_Tags.EventsDropped]: "eventsDropped",
|
|
2132
|
-
[P2pEvent_Tags.ConnectionEstablished]: "connectionEstablished",
|
|
2133
|
-
[P2pEvent_Tags.ConnectionClosed]: "connectionClosed",
|
|
2134
|
-
[P2pEvent_Tags.ConnectionReplaced]: "connectionReplaced",
|
|
2135
|
-
[P2pEvent_Tags.PeerReady]: "peerReady",
|
|
2136
|
-
[P2pEvent_Tags.IdentifyReceived]: "identifyReceived",
|
|
2137
|
-
[P2pEvent_Tags.PingRttMeasured]: "pingRttMeasured",
|
|
2138
|
-
[P2pEvent_Tags.PingTimeout]: "pingTimeout",
|
|
2139
|
-
[P2pEvent_Tags.EndpointError]: "endpointError",
|
|
2140
|
-
[P2pEvent_Tags.ReachabilityChanged]: "reachabilityChanged",
|
|
2141
|
-
[P2pEvent_Tags.PublicAddressesChanged]: "publicAddressesChanged",
|
|
2142
|
-
[P2pEvent_Tags.RelayReserved]: "relayReserved",
|
|
2143
|
-
[P2pEvent_Tags.RelayReservationLost]: "relayReservationLost",
|
|
2144
|
-
[P2pEvent_Tags.PathEstablished]: "pathEstablished",
|
|
2145
|
-
[P2pEvent_Tags.InboundPathEstablished]: "inboundPathEstablished",
|
|
2146
|
-
[P2pEvent_Tags.PathUpgraded]: "pathUpgraded",
|
|
2147
|
-
[P2pEvent_Tags.HolePunchFailed]: "holePunchFailed",
|
|
2148
|
-
[P2pEvent_Tags.ConnectFailed]: "connectFailed",
|
|
2149
|
-
[P2pEvent_Tags.ConnectCancelled]: "connectCancelled",
|
|
2150
|
-
[P2pEvent_Tags.InboundDirectUpgrade]: "inboundDirectUpgrade",
|
|
2151
|
-
[P2pEvent_Tags.Message]: "message",
|
|
2152
|
-
[P2pEvent_Tags.PeerSubscribed]: "peerSubscribed",
|
|
2153
|
-
[P2pEvent_Tags.PeerUnsubscribed]: "peerUnsubscribed",
|
|
2154
|
-
[P2pEvent_Tags.GossipsubOutboundFailure]: "gossipsubOutboundFailure",
|
|
2155
|
-
[P2pEvent_Tags.GossipsubProtocolViolation]: "gossipsubProtocolViolation",
|
|
2156
|
-
[P2pEvent_Tags.PeerDiscovered]: "peerDiscovered",
|
|
2157
|
-
[P2pEvent_Tags.PeerUpdated]: "peerUpdated",
|
|
2158
|
-
[P2pEvent_Tags.PeerExpired]: "peerExpired",
|
|
2159
|
-
[P2pEvent_Tags.DiscoveryDialFailed]: "discoveryDialFailed",
|
|
2160
|
-
[P2pEvent_Tags.DiscoveryProtocolViolation]: "discoveryProtocolViolation",
|
|
2161
|
-
};
|
|
2162
|
-
const type = names[event.tag];
|
|
2163
|
-
if (type === undefined) {
|
|
2164
|
-
return undefined;
|
|
2165
|
-
}
|
|
2235
|
+
): {
|
|
2236
|
+
type: Exclude<
|
|
2237
|
+
EventKind,
|
|
2238
|
+
"stream" | "queueOverflow" | "handlerError" | "driverFailed"
|
|
2239
|
+
>;
|
|
2240
|
+
payload: AnyPayload;
|
|
2241
|
+
} {
|
|
2242
|
+
const type = SDK_EVENT_NAMES[event.tag];
|
|
2166
2243
|
let payload: unknown = event.inner;
|
|
2167
2244
|
if (event.tag === P2pEvent_Tags.PathEstablished) {
|
|
2168
2245
|
payload = { ...event.inner, path: normalizePath(event.inner.path) };
|
|
@@ -2175,7 +2252,7 @@ function normalizeEvent(
|
|
|
2175
2252
|
to: normalizePath(event.inner.to),
|
|
2176
2253
|
};
|
|
2177
2254
|
}
|
|
2178
|
-
return { payload: payload as AnyPayload, type
|
|
2255
|
+
return { payload: payload as AnyPayload, type };
|
|
2179
2256
|
}
|
|
2180
2257
|
|
|
2181
2258
|
function normalizePath(path: PathKind): Path {
|
|
@@ -2192,18 +2269,6 @@ function normalizePath(path: PathKind): Path {
|
|
|
2192
2269
|
}
|
|
2193
2270
|
}
|
|
2194
2271
|
|
|
2195
|
-
function streamKey(peerId: string, connId: number, streamId: number): string {
|
|
2196
|
-
return `${peerId}\u0000${connId}\u0000${streamId}`;
|
|
2197
|
-
}
|
|
2198
|
-
|
|
2199
|
-
function pendingOpenKey(
|
|
2200
|
-
peerId: string,
|
|
2201
|
-
connId: number,
|
|
2202
|
-
streamId: number
|
|
2203
|
-
): string {
|
|
2204
|
-
return streamKey(peerId, connId, streamId);
|
|
2205
|
-
}
|
|
2206
|
-
|
|
2207
2272
|
function streamMeta(stream: Stream): InboundStreamMeta {
|
|
2208
2273
|
return {
|
|
2209
2274
|
connId: stream.connId,
|