@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 CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"sdk.d.ts","sourceRoot":"","sources":["../src/sdk.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAwB,cAAc,EAAE,MAAM,cAAc,CAAC;AAiBzE,OAAO,KAAK,EACV,KAAK,EACL,WAAW,EACX,cAAc,EACd,cAAc,EACd,aAAa,EACb,aAAa,EACb,aAAa,EACb,YAAY,EACZ,iBAAiB,EACjB,aAAa,EAEb,YAAY,EACZ,oBAAoB,EACpB,WAAW,EACX,SAAS,EAET,IAAI,EAEJ,YAAY,EACZ,oBAAoB,EACpB,WAAW,EACX,cAAc,EACf,MAAM,YAAY,CAAC;AAkCpB,KAAK,SAAS,GAAG,MAAM,oBAAoB,CAAC;AAE5C,KAAK,YAAY,CAAC,IAAI,SAAS,SAAS,IAAI,CAC1C,OAAO,EAAE,oBAAoB,CAAC,IAAI,CAAC,KAChC,IAAI,CAAC;AACV,KAAK,eAAe,GAAG,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAC;AA2SrD,gFAAgF;AAChF,qBAAa,MAAM;;IACjB,0DAA0D;IAC1D,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,kDAAkD;IAClD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,kDAAkD;IAClD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,sDAAsD;IACtD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,kDAAkD;IAClD,QAAQ,CAAC,gBAAgB,EAAE,OAAO,CAAC;IAuBnC;;;;OAIG;IACH,kBAAkB,SAAiC;IAEnD,YACE,OAAO,EAAE,cAAc,EACvB,IAAI,EAAE,iBAAiB,EACvB,UAAU,EAAE,MAAM,IAAI,EAUvB;I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E,cAAmB,GAC3B,OAAO,CAAC,aAAa,CAAC,CAQxB;IAED;;;;;;;;;;;OAWG;IACH,iBAAiB,CACf,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAAC,aAAa,CAAC,CAoDxB;IAED;;;;;;;OAOG;IACH,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAIrC;IAED,gDAAgD;IAChD,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAG/B;CAwlBF"}
package/dist/sdk.js CHANGED
@@ -54,6 +54,35 @@ class BoundedQueue {
54
54
  this.#size = 0;
55
55
  }
56
56
  }
57
+ /** An unbounded queue whose `shift` is O(1), unlike `Array#shift`. */
58
+ class Fifo {
59
+ #items = [];
60
+ #head = 0;
61
+ get length() {
62
+ return this.#items.length - this.#head;
63
+ }
64
+ push(item) {
65
+ this.#items.push(item);
66
+ }
67
+ shift() {
68
+ if (this.#head === this.#items.length) {
69
+ return undefined;
70
+ }
71
+ const item = this.#items[this.#head];
72
+ this.#items[this.#head] = undefined;
73
+ this.#head += 1;
74
+ // Drop the consumed prefix once it is at least half the array.
75
+ if (this.#head * 2 >= this.#items.length) {
76
+ this.#items = this.#items.slice(this.#head);
77
+ this.#head = 0;
78
+ }
79
+ return item;
80
+ }
81
+ clear() {
82
+ this.#items = [];
83
+ this.#head = 0;
84
+ }
85
+ }
57
86
  /**
58
87
  * Native events that are never dropped, because stream and connection state
59
88
  * depends on each one (ADR 0012), plus the driver failure that ends them.
@@ -89,15 +118,57 @@ const coalesceData = (last, next) => {
89
118
  };
90
119
  return { event, source: "native" };
91
120
  };
92
- /** The stream a native per-stream event belongs to. */
93
- const streamEventKey = (item) => {
94
- if (item.source !== "native" ||
95
- (item.event.tag !== P2pEvent_Tags.StreamReady && !isStreamEvent(item.event))) {
96
- return undefined;
121
+ /**
122
+ * Values keyed by a stream's public connection and stream IDs, without
123
+ * building a key per lookup. Public connection IDs never repeat and each
124
+ * names one peer's connection, so the pair names the stream.
125
+ */
126
+ class StreamTable {
127
+ #byConnection = new Map();
128
+ get(connId, streamId) {
129
+ return this.#byConnection.get(connId)?.get(streamId);
130
+ }
131
+ has(connId, streamId) {
132
+ return this.#byConnection.get(connId)?.has(streamId) ?? false;
133
+ }
134
+ set(connId, streamId, value) {
135
+ let streams = this.#byConnection.get(connId);
136
+ if (streams === undefined) {
137
+ streams = new Map();
138
+ this.#byConnection.set(connId, streams);
139
+ }
140
+ streams.set(streamId, value);
141
+ }
142
+ delete(connId, streamId) {
143
+ const streams = this.#byConnection.get(connId);
144
+ if (streams?.delete(streamId) === true && streams.size === 0) {
145
+ this.#byConnection.delete(connId);
146
+ }
147
+ }
148
+ /** Removes and returns every value on `connId`. */
149
+ takeConnection(connId) {
150
+ const streams = this.#byConnection.get(connId);
151
+ this.#byConnection.delete(connId);
152
+ return streams === undefined ? [] : [...streams.values()];
153
+ }
154
+ /** A snapshot of every value. */
155
+ values() {
156
+ return [...this.#byConnection.values()].flatMap((streams) => [
157
+ ...streams.values(),
158
+ ]);
159
+ }
160
+ /** Removes and returns every value. */
161
+ takeAll() {
162
+ const values = this.values();
163
+ this.#byConnection.clear();
164
+ return values;
97
165
  }
98
- const { peerId, connId, streamId } = item.event.inner;
99
- return streamKey(peerId, connId, streamId);
100
- };
166
+ }
167
+ /** The native per-stream event an item carries, if any. */
168
+ const streamEventOf = (item) => item.source === "native" &&
169
+ (item.event.tag === P2pEvent_Tags.StreamReady || isStreamEvent(item.event))
170
+ ? item.event
171
+ : undefined;
101
172
  /**
102
173
  * The endpoint's shared event queue, in delivery order.
103
174
  *
@@ -119,7 +190,7 @@ class EventQueue {
119
190
  * Peer-opened streams whose `StreamReady` is still queued, with their
120
191
  * queued item ids; `undefined` once one carried data.
121
192
  */
122
- #unseen = new Map();
193
+ #unseen = new StreamTable();
123
194
  #capacity;
124
195
  #release;
125
196
  #next = 0;
@@ -168,7 +239,7 @@ class EventQueue {
168
239
  const item = this.#remove(oldest);
169
240
  if (item?.source === "native" &&
170
241
  item.event.tag === P2pEvent_Tags.StreamReady) {
171
- this.#unseen.delete(streamEventKey(item) ?? "");
242
+ this.#unseen.delete(item.event.inner.connId, item.event.inner.streamId);
172
243
  }
173
244
  return item;
174
245
  }
@@ -177,26 +248,26 @@ class EventQueue {
177
248
  * it closed one that carried no data, which leaves the queue whole.
178
249
  */
179
250
  #elideUnseen(item, id) {
180
- const key = streamEventKey(item);
181
- if (key === undefined || item.source !== "native") {
251
+ const event = streamEventOf(item);
252
+ if (event === undefined) {
182
253
  return false;
183
254
  }
184
- const { event } = item;
255
+ const { connId, streamId } = event.inner;
185
256
  if (event.tag === P2pEvent_Tags.StreamReady) {
186
257
  if (!event.inner.initiatedLocally) {
187
- this.#unseen.set(key, [id]);
258
+ this.#unseen.set(connId, streamId, [id]);
188
259
  }
189
260
  return false;
190
261
  }
191
- if (!this.#unseen.has(key)) {
262
+ if (!this.#unseen.has(connId, streamId)) {
192
263
  return false;
193
264
  }
194
- const ids = this.#unseen.get(key);
265
+ const ids = this.#unseen.get(connId, streamId);
195
266
  if (event.tag === P2pEvent_Tags.StreamData) {
196
- this.#unseen.set(key, undefined);
267
+ this.#unseen.set(connId, streamId, undefined);
197
268
  }
198
269
  else if (ids !== undefined && event.tag === P2pEvent_Tags.StreamClosed) {
199
- this.#unseen.delete(key);
270
+ this.#unseen.delete(connId, streamId);
200
271
  for (const queued of ids) {
201
272
  const removed = this.#remove(queued);
202
273
  if (removed !== undefined) {
@@ -238,7 +309,7 @@ export class Stream {
238
309
  #onTerminal;
239
310
  #consume;
240
311
  #listeners = new Map();
241
- #fifo = [];
312
+ #fifo = new Fifo();
242
313
  #reads = [];
243
314
  #fifoBytes = 0;
244
315
  #mode;
@@ -414,7 +485,7 @@ export class Stream {
414
485
  */
415
486
  #discardUnread() {
416
487
  this.#consumed(this.#fifoBytes);
417
- this.#fifo.length = 0;
488
+ this.#fifo.clear();
418
489
  this.#fifoBytes = 0;
419
490
  }
420
491
  [Symbol.dispose]() {
@@ -661,8 +732,8 @@ export class Minip2pBase {
661
732
  });
662
733
  #connects = new Map();
663
734
  #terminalConnects = new Set();
664
- #streams = new Map();
665
- #pendingOpens = new Map();
735
+ #streams = new StreamTable();
736
+ #pendingOpens = new StreamTable();
666
737
  #pings = new Map();
667
738
  #dropped = 0;
668
739
  #flushScheduled = false;
@@ -1009,8 +1080,7 @@ export class Minip2pBase {
1009
1080
  pending.removeAbort = listenAbort(options.signal, () => {
1010
1081
  this.#expireOpen(pending, new AbortError());
1011
1082
  });
1012
- const key = pendingOpenKey(peerId, identity.connId, identity.streamId);
1013
- const overwritten = this.#pendingOpens.get(key);
1083
+ const overwritten = this.#pendingOpens.get(identity.connId, identity.streamId);
1014
1084
  if (overwritten !== undefined) {
1015
1085
  clearPendingOpen(overwritten);
1016
1086
  overwritten.reject(new OpenStreamError({
@@ -1021,7 +1091,7 @@ export class Minip2pBase {
1021
1091
  streamId: overwritten.streamId,
1022
1092
  }));
1023
1093
  }
1024
- this.#pendingOpens.set(key, pending);
1094
+ this.#pendingOpens.set(identity.connId, identity.streamId, pending);
1025
1095
  });
1026
1096
  }
1027
1097
  /**
@@ -1209,7 +1279,7 @@ export class Minip2pBase {
1209
1279
  this.#connectResultsLost(event.inner.terminalConnectIds);
1210
1280
  }
1211
1281
  }
1212
- for (const pending of this.#pendingOpens.values()) {
1282
+ for (const pending of this.#pendingOpens.takeAll()) {
1213
1283
  clearPendingOpen(pending);
1214
1284
  pending.reject(error);
1215
1285
  try {
@@ -1219,7 +1289,6 @@ export class Minip2pBase {
1219
1289
  // The operation is already failed; native cleanup is best effort.
1220
1290
  }
1221
1291
  }
1222
- this.#pendingOpens.clear();
1223
1292
  }
1224
1293
  #enqueueNative(event) {
1225
1294
  if (this.#closed) {
@@ -1313,12 +1382,12 @@ export class Minip2pBase {
1313
1382
  this.#correlateOpenError(event.inner);
1314
1383
  }
1315
1384
  if (event.tag === P2pEvent_Tags.ConnectionClosed) {
1316
- this.#connectionEnded(event.inner.peerId, event.inner.connId, new PeerDisconnectedError(event.inner.peerId, "stream"), new PeerDisconnectedError(event.inner.peerId, "openStream"));
1385
+ this.#connectionEnded(event.inner.connId, new PeerDisconnectedError(event.inner.peerId, "stream"), new PeerDisconnectedError(event.inner.peerId, "openStream"));
1317
1386
  }
1318
1387
  if (event.tag === P2pEvent_Tags.ConnectionReplaced) {
1319
1388
  // Native ends the replaced connection's streams without per-stream
1320
1389
  // terminal events, so they end here.
1321
- this.#connectionEnded(event.inner.peerId, event.inner.oldConnId, new StreamClosedError("The stream's connection was replaced"), new StreamClosedError("The stream's connection was replaced before it became ready"));
1390
+ this.#connectionEnded(event.inner.oldConnId, new StreamClosedError("The stream's connection was replaced"), new StreamClosedError("The stream's connection was replaced before it became ready"));
1322
1391
  }
1323
1392
  if (isConnectTerminal(event)) {
1324
1393
  this.#connectTerminal(event);
@@ -1327,17 +1396,17 @@ export class Minip2pBase {
1327
1396
  this.#connectResultsLost(event.inner.terminalConnectIds);
1328
1397
  }
1329
1398
  const normalized = normalizeEvent(event);
1330
- if (normalized !== undefined) {
1331
- this.#dispatch(normalized.type, normalized.payload, normalized.payload);
1332
- }
1399
+ this.#dispatch(normalized.type, normalized.payload, normalized.payload);
1333
1400
  }
1334
1401
  #dispatch(type, payload, catchPayload) {
1335
1402
  let claimed = false;
1336
- const result = eventResult(type, payload);
1337
- for (const waiter of [...this.#waiters]) {
1403
+ // Built only once a waiter of this type is found.
1404
+ let result;
1405
+ for (const waiter of this.#waiters.size > 0 ? [...this.#waiters] : []) {
1338
1406
  if (waiter.type !== type) {
1339
1407
  continue;
1340
1408
  }
1409
+ result ??= eventResult(type, payload);
1341
1410
  let matched = false;
1342
1411
  try {
1343
1412
  matched = waiter.predicate(result);
@@ -1366,7 +1435,8 @@ export class Minip2pBase {
1366
1435
  this.#handlerFailed(type, payload, error);
1367
1436
  }
1368
1437
  }
1369
- if (type !== "stream") {
1438
+ if (type !== "stream" && this.#catchAll.size > 0) {
1439
+ // Its own object: a waiter's resolved event is never a catch-all's.
1370
1440
  const catchEvent = eventResult(type, catchPayload);
1371
1441
  for (const handler of [...this.#catchAll]) {
1372
1442
  try {
@@ -1392,15 +1462,13 @@ export class Minip2pBase {
1392
1462
  });
1393
1463
  }
1394
1464
  #streamReady(meta) {
1395
- const key = streamKey(meta.peerId, meta.connId, meta.streamId);
1396
1465
  const stream = new Stream(this.#backend, meta, () => {
1397
- this.#streams.delete(key);
1466
+ this.#streams.delete(meta.connId, meta.streamId);
1398
1467
  });
1399
- this.#streams.set(key, stream);
1400
- const pendingKey = pendingOpenKey(meta.peerId, meta.connId, meta.streamId);
1401
- const pending = this.#pendingOpens.get(pendingKey);
1468
+ this.#streams.set(meta.connId, meta.streamId, stream);
1469
+ const pending = this.#pendingOpens.get(meta.connId, meta.streamId);
1402
1470
  if (pending !== undefined && meta.initiatedLocally) {
1403
- this.#pendingOpens.delete(pendingKey);
1471
+ this.#pendingOpens.delete(meta.connId, meta.streamId);
1404
1472
  clearPendingOpen(pending);
1405
1473
  pending.resolve(stream);
1406
1474
  return;
@@ -1432,13 +1500,12 @@ export class Minip2pBase {
1432
1500
  }
1433
1501
  }
1434
1502
  #streamEvent(event) {
1435
- const { peerId, connId, streamId } = event.inner;
1436
- const stream = this.#streams.get(streamKey(peerId, connId, streamId));
1503
+ const { connId, streamId } = event.inner;
1504
+ const stream = this.#streams.get(connId, streamId);
1437
1505
  if (event.tag === P2pEvent_Tags.StreamClosed) {
1438
- const pendingKey = pendingOpenKey(peerId, connId, streamId);
1439
- const pending = this.#pendingOpens.get(pendingKey);
1506
+ const pending = this.#pendingOpens.get(connId, streamId);
1440
1507
  if (pending !== undefined) {
1441
- this.#pendingOpens.delete(pendingKey);
1508
+ this.#pendingOpens.delete(connId, streamId);
1442
1509
  clearPendingOpen(pending);
1443
1510
  pending.reject(new StreamClosedError());
1444
1511
  }
@@ -1471,7 +1538,9 @@ export class Minip2pBase {
1471
1538
  if (error.streamId === undefined) {
1472
1539
  return;
1473
1540
  }
1474
- const candidates = [...this.#pendingOpens.values()].filter((pending) => pending.streamId === error.streamId &&
1541
+ const candidates = this.#pendingOpens
1542
+ .values()
1543
+ .filter((pending) => pending.streamId === error.streamId &&
1475
1544
  (error.peerId === undefined || error.peerId === pending.peerId) &&
1476
1545
  (error.connId === undefined || error.connId === pending.connId));
1477
1546
  if (candidates.length !== 1) {
@@ -1481,7 +1550,7 @@ export class Minip2pBase {
1481
1550
  if (pending === undefined) {
1482
1551
  return;
1483
1552
  }
1484
- this.#pendingOpens.delete(pendingOpenKey(pending.peerId, pending.connId, pending.streamId));
1553
+ this.#pendingOpens.delete(pending.connId, pending.streamId);
1485
1554
  clearPendingOpen(pending);
1486
1555
  pending.reject(new OpenStreamError({
1487
1556
  connId: error.connId,
@@ -1492,19 +1561,13 @@ export class Minip2pBase {
1492
1561
  }));
1493
1562
  }
1494
1563
  /** Ends every stream and pending open on a closed or replaced connection. */
1495
- #connectionEnded(peerId, connId, streamError, openError) {
1496
- for (const [key, stream] of [...this.#streams]) {
1497
- if (stream.peerId === peerId && stream.connId === connId) {
1498
- stream.terminal(streamError);
1499
- this.#streams.delete(key);
1500
- }
1564
+ #connectionEnded(connId, streamError, openError) {
1565
+ for (const stream of this.#streams.takeConnection(connId)) {
1566
+ stream.terminal(streamError);
1501
1567
  }
1502
- for (const pending of [...this.#pendingOpens.values()]) {
1503
- if (pending.peerId === peerId && pending.connId === connId) {
1504
- this.#pendingOpens.delete(pendingOpenKey(pending.peerId, pending.connId, pending.streamId));
1505
- clearPendingOpen(pending);
1506
- pending.reject(openError);
1507
- }
1568
+ for (const pending of this.#pendingOpens.takeConnection(connId)) {
1569
+ clearPendingOpen(pending);
1570
+ pending.reject(openError);
1508
1571
  }
1509
1572
  }
1510
1573
  #connectTerminal(event) {
@@ -1582,11 +1645,10 @@ export class Minip2pBase {
1582
1645
  this.#terminalConnects.delete(connectId);
1583
1646
  }
1584
1647
  #expireOpen(pending, error) {
1585
- const key = pendingOpenKey(pending.peerId, pending.connId, pending.streamId);
1586
- if (this.#pendingOpens.get(key) !== pending) {
1648
+ if (this.#pendingOpens.get(pending.connId, pending.streamId) !== pending) {
1587
1649
  return;
1588
1650
  }
1589
- this.#pendingOpens.delete(key);
1651
+ this.#pendingOpens.delete(pending.connId, pending.streamId);
1590
1652
  clearPendingOpen(pending);
1591
1653
  pending.reject(error);
1592
1654
  try {
@@ -1633,15 +1695,13 @@ export class Minip2pBase {
1633
1695
  }
1634
1696
  this.#connects.clear();
1635
1697
  this.#terminalConnects.clear();
1636
- for (const pending of this.#pendingOpens.values()) {
1698
+ for (const pending of this.#pendingOpens.takeAll()) {
1637
1699
  clearPendingOpen(pending);
1638
1700
  pending.reject(error);
1639
1701
  }
1640
- this.#pendingOpens.clear();
1641
- for (const stream of this.#streams.values()) {
1702
+ for (const stream of this.#streams.takeAll()) {
1642
1703
  stream.terminal(error);
1643
1704
  }
1644
- this.#streams.clear();
1645
1705
  for (const ping of this.#pings.values()) {
1646
1706
  ping.cancel(error);
1647
1707
  }
@@ -1680,43 +1740,44 @@ function toBackendTarget(target) {
1680
1740
  ? { addresses: [target], kind: "addresses" }
1681
1741
  : { kind: "peer", peerId: target };
1682
1742
  }
1743
+ /**
1744
+ * SDK event names for every native event that dispatches by name. Each name
1745
+ * is its tag uncapitalized, so a wrong mapping fails to compile.
1746
+ */
1747
+ const SDK_EVENT_NAMES = {
1748
+ [P2pEvent_Tags.EventsDropped]: "eventsDropped",
1749
+ [P2pEvent_Tags.ConnectionEstablished]: "connectionEstablished",
1750
+ [P2pEvent_Tags.ConnectionClosed]: "connectionClosed",
1751
+ [P2pEvent_Tags.ConnectionReplaced]: "connectionReplaced",
1752
+ [P2pEvent_Tags.PeerReady]: "peerReady",
1753
+ [P2pEvent_Tags.IdentifyReceived]: "identifyReceived",
1754
+ [P2pEvent_Tags.PingRttMeasured]: "pingRttMeasured",
1755
+ [P2pEvent_Tags.PingTimeout]: "pingTimeout",
1756
+ [P2pEvent_Tags.EndpointError]: "endpointError",
1757
+ [P2pEvent_Tags.ReachabilityChanged]: "reachabilityChanged",
1758
+ [P2pEvent_Tags.PublicAddressesChanged]: "publicAddressesChanged",
1759
+ [P2pEvent_Tags.RelayReserved]: "relayReserved",
1760
+ [P2pEvent_Tags.RelayReservationLost]: "relayReservationLost",
1761
+ [P2pEvent_Tags.PathEstablished]: "pathEstablished",
1762
+ [P2pEvent_Tags.InboundPathEstablished]: "inboundPathEstablished",
1763
+ [P2pEvent_Tags.PathUpgraded]: "pathUpgraded",
1764
+ [P2pEvent_Tags.HolePunchFailed]: "holePunchFailed",
1765
+ [P2pEvent_Tags.ConnectFailed]: "connectFailed",
1766
+ [P2pEvent_Tags.ConnectCancelled]: "connectCancelled",
1767
+ [P2pEvent_Tags.InboundDirectUpgrade]: "inboundDirectUpgrade",
1768
+ [P2pEvent_Tags.Message]: "message",
1769
+ [P2pEvent_Tags.PeerSubscribed]: "peerSubscribed",
1770
+ [P2pEvent_Tags.PeerUnsubscribed]: "peerUnsubscribed",
1771
+ [P2pEvent_Tags.GossipsubOutboundFailure]: "gossipsubOutboundFailure",
1772
+ [P2pEvent_Tags.GossipsubProtocolViolation]: "gossipsubProtocolViolation",
1773
+ [P2pEvent_Tags.PeerDiscovered]: "peerDiscovered",
1774
+ [P2pEvent_Tags.PeerUpdated]: "peerUpdated",
1775
+ [P2pEvent_Tags.PeerExpired]: "peerExpired",
1776
+ [P2pEvent_Tags.DiscoveryDialFailed]: "discoveryDialFailed",
1777
+ [P2pEvent_Tags.DiscoveryProtocolViolation]: "discoveryProtocolViolation",
1778
+ };
1683
1779
  function normalizeEvent(event) {
1684
- const names = {
1685
- [P2pEvent_Tags.EventsDropped]: "eventsDropped",
1686
- [P2pEvent_Tags.ConnectionEstablished]: "connectionEstablished",
1687
- [P2pEvent_Tags.ConnectionClosed]: "connectionClosed",
1688
- [P2pEvent_Tags.ConnectionReplaced]: "connectionReplaced",
1689
- [P2pEvent_Tags.PeerReady]: "peerReady",
1690
- [P2pEvent_Tags.IdentifyReceived]: "identifyReceived",
1691
- [P2pEvent_Tags.PingRttMeasured]: "pingRttMeasured",
1692
- [P2pEvent_Tags.PingTimeout]: "pingTimeout",
1693
- [P2pEvent_Tags.EndpointError]: "endpointError",
1694
- [P2pEvent_Tags.ReachabilityChanged]: "reachabilityChanged",
1695
- [P2pEvent_Tags.PublicAddressesChanged]: "publicAddressesChanged",
1696
- [P2pEvent_Tags.RelayReserved]: "relayReserved",
1697
- [P2pEvent_Tags.RelayReservationLost]: "relayReservationLost",
1698
- [P2pEvent_Tags.PathEstablished]: "pathEstablished",
1699
- [P2pEvent_Tags.InboundPathEstablished]: "inboundPathEstablished",
1700
- [P2pEvent_Tags.PathUpgraded]: "pathUpgraded",
1701
- [P2pEvent_Tags.HolePunchFailed]: "holePunchFailed",
1702
- [P2pEvent_Tags.ConnectFailed]: "connectFailed",
1703
- [P2pEvent_Tags.ConnectCancelled]: "connectCancelled",
1704
- [P2pEvent_Tags.InboundDirectUpgrade]: "inboundDirectUpgrade",
1705
- [P2pEvent_Tags.Message]: "message",
1706
- [P2pEvent_Tags.PeerSubscribed]: "peerSubscribed",
1707
- [P2pEvent_Tags.PeerUnsubscribed]: "peerUnsubscribed",
1708
- [P2pEvent_Tags.GossipsubOutboundFailure]: "gossipsubOutboundFailure",
1709
- [P2pEvent_Tags.GossipsubProtocolViolation]: "gossipsubProtocolViolation",
1710
- [P2pEvent_Tags.PeerDiscovered]: "peerDiscovered",
1711
- [P2pEvent_Tags.PeerUpdated]: "peerUpdated",
1712
- [P2pEvent_Tags.PeerExpired]: "peerExpired",
1713
- [P2pEvent_Tags.DiscoveryDialFailed]: "discoveryDialFailed",
1714
- [P2pEvent_Tags.DiscoveryProtocolViolation]: "discoveryProtocolViolation",
1715
- };
1716
- const type = names[event.tag];
1717
- if (type === undefined) {
1718
- return undefined;
1719
- }
1780
+ const type = SDK_EVENT_NAMES[event.tag];
1720
1781
  let payload = event.inner;
1721
1782
  if (event.tag === P2pEvent_Tags.PathEstablished) {
1722
1783
  payload = { ...event.inner, path: normalizePath(event.inner.path) };
@@ -1731,7 +1792,7 @@ function normalizeEvent(event) {
1731
1792
  to: normalizePath(event.inner.to),
1732
1793
  };
1733
1794
  }
1734
- return { payload: payload, type: type };
1795
+ return { payload: payload, type };
1735
1796
  }
1736
1797
  function normalizePath(path) {
1737
1798
  switch (path.tag) {
@@ -1746,12 +1807,6 @@ function normalizePath(path) {
1746
1807
  }
1747
1808
  }
1748
1809
  }
1749
- function streamKey(peerId, connId, streamId) {
1750
- return `${peerId}\u0000${connId}\u0000${streamId}`;
1751
- }
1752
- function pendingOpenKey(peerId, connId, streamId) {
1753
- return streamKey(peerId, connId, streamId);
1754
- }
1755
1810
  function streamMeta(stream) {
1756
1811
  return {
1757
1812
  connId: stream.connId,