@libp2p/utils 7.0.17 → 7.1.0-15eeedba1

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.
@@ -0,0 +1,147 @@
1
+ import { Circuit, WebSockets, WebSocketsSecure, WebRTC, WebRTCDirect, WebTransport, TCP } from '@multiformats/multiaddr-matcher'
2
+ import { isLoopback } from './is-loopback.ts'
3
+ import { isPrivate } from './is-private.ts'
4
+ import type { Multiaddr } from '@multiformats/multiaddr'
5
+
6
+ /**
7
+ * Sorts addresses by order of reliability, where they have presented the fewest
8
+ * problems:
9
+ *
10
+ * TCP -> WebSockets/Secure -> WebRTC -> WebRTCDirect -> WebTransport
11
+ */
12
+ // eslint-disable-next-line complexity
13
+ export function reliableTransportsFirst (a: Multiaddr, b: Multiaddr): -1 | 0 | 1 {
14
+ const isATcp = TCP.exactMatch(a)
15
+ const isBTcp = TCP.exactMatch(b)
16
+
17
+ if (isATcp && !isBTcp) {
18
+ return -1
19
+ }
20
+
21
+ if (!isATcp && isBTcp) {
22
+ return 1
23
+ }
24
+
25
+ const isAWebSocketSecure = WebSocketsSecure.exactMatch(a)
26
+ const isBWebSocketSecure = WebSocketsSecure.exactMatch(b)
27
+
28
+ if (isAWebSocketSecure && !isBWebSocketSecure) {
29
+ return -1
30
+ }
31
+
32
+ if (!isAWebSocketSecure && isBWebSocketSecure) {
33
+ return 1
34
+ }
35
+
36
+ const isAWebSocket = WebSockets.exactMatch(a)
37
+ const isBWebSocket = WebSockets.exactMatch(b)
38
+
39
+ if (isAWebSocket && !isBWebSocket) {
40
+ return -1
41
+ }
42
+
43
+ if (!isAWebSocket && isBWebSocket) {
44
+ return 1
45
+ }
46
+
47
+ const isAWebRTC = WebRTC.exactMatch(a)
48
+ const isBWebRTC = WebRTC.exactMatch(b)
49
+
50
+ if (isAWebRTC && !isBWebRTC) {
51
+ return -1
52
+ }
53
+
54
+ if (!isAWebRTC && isBWebRTC) {
55
+ return 1
56
+ }
57
+
58
+ const isAWebRTCDirect = WebRTCDirect.exactMatch(a)
59
+ const isBWebRTCDirect = WebRTCDirect.exactMatch(b)
60
+
61
+ if (isAWebRTCDirect && !isBWebRTCDirect) {
62
+ return -1
63
+ }
64
+
65
+ if (!isAWebRTCDirect && isBWebRTCDirect) {
66
+ return 1
67
+ }
68
+
69
+ const isAWebTransport = WebTransport.exactMatch(a)
70
+ const isBWebTransport = WebTransport.exactMatch(b)
71
+
72
+ if (isAWebTransport && !isBWebTransport) {
73
+ return -1
74
+ }
75
+
76
+ if (!isAWebTransport && isBWebTransport) {
77
+ return 1
78
+ }
79
+
80
+ // ... everything else
81
+ return 0
82
+ }
83
+
84
+ /**
85
+ * Compare function for array.sort() that moves loopback addresses to the end
86
+ * of the array.
87
+ */
88
+ export function loopbackAddressLast (a: Multiaddr, b: Multiaddr): -1 | 0 | 1 {
89
+ const isALoopback = isLoopback(a)
90
+ const isBLoopback = isLoopback(b)
91
+
92
+ if (isALoopback && !isBLoopback) {
93
+ return 1
94
+ } else if (!isALoopback && isBLoopback) {
95
+ return -1
96
+ }
97
+
98
+ return 0
99
+ }
100
+
101
+ /**
102
+ * Compare function for array.sort() that moves public addresses to the start
103
+ * of the array.
104
+ */
105
+ export function publicAddressesFirst (a: Multiaddr, b: Multiaddr): -1 | 0 | 1 {
106
+ const isAPrivate = isPrivate(a)
107
+ const isBPrivate = isPrivate(b)
108
+
109
+ if (isAPrivate && !isBPrivate) {
110
+ return 1
111
+ } else if (!isAPrivate && isBPrivate) {
112
+ return -1
113
+ }
114
+
115
+ return 0
116
+ }
117
+
118
+ /**
119
+ * Compare function for array.sort() that moves circuit relay addresses to the
120
+ * end of the array.
121
+ */
122
+ export function circuitRelayAddressesLast (a: Multiaddr, b: Multiaddr): -1 | 0 | 1 {
123
+ const isACircuit = Circuit.exactMatch(a)
124
+ const isBCircuit = Circuit.exactMatch(b)
125
+
126
+ if (isACircuit && !isBCircuit) {
127
+ return 1
128
+ } else if (!isACircuit && isBCircuit) {
129
+ return -1
130
+ }
131
+
132
+ return 0
133
+ }
134
+
135
+ /**
136
+ * Sort multiaddrs by the default multiaddr-only ordering: loopback addresses
137
+ * last, public addresses first, circuit-relay addresses last, with reliable
138
+ * transports as the innermost tiebreaker.
139
+ */
140
+ export function defaultMultiaddrSorter (multiaddrs: Multiaddr[]): Multiaddr[] {
141
+ return multiaddrs.sort((a, b) =>
142
+ loopbackAddressLast(a, b) ||
143
+ publicAddressesFirst(a, b) ||
144
+ circuitRelayAddressesLast(a, b) ||
145
+ reliableTransportsFirst(a, b)
146
+ )
147
+ }
package/src/queue/job.ts CHANGED
@@ -27,6 +27,7 @@ export class Job <JobOptions extends AbortOptions & ProgressOptions = AbortOptio
27
27
  public status: JobStatus
28
28
  public readonly timeline: JobTimeline
29
29
  private readonly controller: AbortController
30
+ private dispatchingProgress: boolean
30
31
 
31
32
  constructor (fn: (options: JobOptions) => Promise<JobReturnType>, options: any) {
32
33
  this.id = randomId()
@@ -41,6 +42,8 @@ export class Job <JobOptions extends AbortOptions & ProgressOptions = AbortOptio
41
42
  this.controller = new AbortController()
42
43
  setMaxListeners(Infinity, this.controller.signal)
43
44
 
45
+ this.dispatchingProgress = false
46
+
44
47
  this.onAbort = this.onAbort.bind(this)
45
48
  }
46
49
 
@@ -80,9 +83,21 @@ export class Job <JobOptions extends AbortOptions & ProgressOptions = AbortOptio
80
83
  ...(this.options ?? {}),
81
84
  signal: this.controller.signal,
82
85
  onProgress: (evt: any): void => {
83
- this.recipients.forEach(recipient => {
84
- recipient.onProgress?.(evt)
85
- })
86
+ // Recipients can transitively re-enter this dispatcher; without
87
+ // this guard a single event recurses until the stack overflows.
88
+ if (this.dispatchingProgress) {
89
+ return
90
+ }
91
+
92
+ this.dispatchingProgress = true
93
+
94
+ try {
95
+ this.recipients.forEach(recipient => {
96
+ recipient.onProgress?.(evt)
97
+ })
98
+ } finally {
99
+ this.dispatchingProgress = false
100
+ }
86
101
  }
87
102
  }), this.controller.signal)
88
103
 
@@ -1,124 +0,0 @@
1
- {
2
- "AbstractMessageStream": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.AbstractMessageStream.html",
3
- "AbstractMultiaddrConnection": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.AbstractMultiaddrConnection.html",
4
- "AbstractStream": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.AbstractStream.html",
5
- "AbstractStreamMuxer": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.AbstractStreamMuxer.html",
6
- "AdaptiveTimeout": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.AdaptiveTimeout.html",
7
- "BloomFilter": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.BloomFilter.html",
8
- "CuckooFilter": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.CuckooFilter.html",
9
- "InvalidDataLengthError": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.InvalidDataLengthError.html",
10
- "InvalidDataLengthLengthError": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.InvalidDataLengthLengthError.html",
11
- "InvalidMessageLengthError": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.InvalidMessageLengthError.html",
12
- "LengthPrefixedDecoder": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.LengthPrefixedDecoder.html",
13
- "MaxEarlyStreamsError": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.MaxEarlyStreamsError.html",
14
- "MemoryStorage": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.MemoryStorage.html",
15
- "MockStream": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.MockStream.html",
16
- "MovingAverage": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.MovingAverage.html",
17
- "PeerQueue": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.PeerQueue.html",
18
- "PriorityQueue": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.PriorityQueue.html",
19
- "Queue": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.Queue.html",
20
- "QueueFullError": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.QueueFullError.html",
21
- "RateLimiter": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.RateLimiter.html",
22
- "RateLimitError": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.RateLimitError.html",
23
- "ScalableCuckooFilter": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.ScalableCuckooFilter.html",
24
- "StreamClosedError": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.StreamClosedError.html",
25
- "UnexpectedEOFError": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.UnexpectedEOFError.html",
26
- "UnwrappedError": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.UnwrappedError.html",
27
- "AbstractMultiaddrConnectionInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.AbstractMultiaddrConnectionInit.html",
28
- "AbstractStreamInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.AbstractStreamInit.html",
29
- "AbstractStreamMuxerInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.AbstractStreamMuxerInit.html",
30
- "AdaptiveTimeoutInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.AdaptiveTimeoutInit.html",
31
- "AdaptiveTimeoutSignal": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.AdaptiveTimeoutSignal.html",
32
- "BloomFilterOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.BloomFilterOptions.html",
33
- "Bucket": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.Bucket.html",
34
- "ByteStream": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.ByteStream.html",
35
- "ByteStreamOpts": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.ByteStreamOpts.html",
36
- "Comparator": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.Comparator.html",
37
- "CreateTrackedListInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.CreateTrackedListInit.html",
38
- "CreateTrackedMapInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.CreateTrackedMapInit.html",
39
- "CuckooFilterInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.CuckooFilterInit.html",
40
- "DebouncedFunction": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.DebouncedFunction.html",
41
- "DNS4NetConfig": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.DNS4NetConfig.html",
42
- "DNS6NetConfig": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.DNS6NetConfig.html",
43
- "DNSAddrNetConfig": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.DNSAddrNetConfig.html",
44
- "DNSNetConfig": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.DNSNetConfig.html",
45
- "Filter": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.Filter.html",
46
- "Fingerprint": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.Fingerprint.html",
47
- "GetKeySecDurationOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.GetKeySecDurationOptions.html",
48
- "GetTimeoutSignalOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.GetTimeoutSignalOptions.html",
49
- "Hash": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.Hash.html",
50
- "IP4NetConfig": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.IP4NetConfig.html",
51
- "IP6NetConfig": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.IP6NetConfig.html",
52
- "Job": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.Job.html",
53
- "JobMatcher": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.JobMatcher.html",
54
- "JobRecipient": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.JobRecipient.html",
55
- "JobTimeline": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.JobTimeline.html",
56
- "LengthPrefixedDecoderInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.LengthPrefixedDecoderInit.html",
57
- "LengthPrefixedStream": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.LengthPrefixedStream.html",
58
- "LengthPrefixedStreamOpts": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.LengthPrefixedStreamOpts.html",
59
- "MessageStreamInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.MessageStreamInit.html",
60
- "MockMultiaddrConnectionInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.MockMultiaddrConnectionInit.html",
61
- "MockMuxedStreamInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.MockMuxedStreamInit.html",
62
- "MultiaddrConnectionPairOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.MultiaddrConnectionPairOptions.html",
63
- "PeerQueueJobOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.PeerQueueJobOptions.html",
64
- "PriorityQueueJobOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.PriorityQueueJobOptions.html",
65
- "ProtobufDecoder": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.ProtobufDecoder.html",
66
- "ProtobufEncoder": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.ProtobufEncoder.html",
67
- "ProtobufMessageStream": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.ProtobufMessageStream.html",
68
- "ProtobufStream": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.ProtobufStream.html",
69
- "ProtobufStreamOpts": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.ProtobufStreamOpts.html",
70
- "QueueEvents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.QueueEvents.html",
71
- "QueueInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.QueueInit.html",
72
- "QueueJobFailure": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.QueueJobFailure.html",
73
- "QueueJobSuccess": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.QueueJobSuccess.html",
74
- "RateLimiterInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.RateLimiterInit.html",
75
- "RateLimiterResult": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.RateLimiterResult.html",
76
- "RateRecord": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.RateRecord.html",
77
- "ReadBytesOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.ReadBytesOptions.html",
78
- "RepeatingTask": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.RepeatingTask.html",
79
- "RepeatingTaskOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.RepeatingTaskOptions.html",
80
- "RunFunction": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.RunFunction.html",
81
- "ScalableCuckooFilterInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.ScalableCuckooFilterInit.html",
82
- "SendResult": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.SendResult.html",
83
- "StreamPairOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.StreamPairOptions.html",
84
- "TrackedMapInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.TrackedMapInit.html",
85
- "JobStatus": "https://libp2p.github.io/js-libp2p/types/_libp2p_utils.JobStatus.html",
86
- "NetConfig": "https://libp2p.github.io/js-libp2p/types/_libp2p_utils.NetConfig.html",
87
- "PipeInput": "https://libp2p.github.io/js-libp2p/types/_libp2p_utils.PipeInput.html",
88
- "DEFAULT_FAILURE_MULTIPLIER": "https://libp2p.github.io/js-libp2p/variables/_libp2p_utils.DEFAULT_FAILURE_MULTIPLIER.html",
89
- "DEFAULT_INTERVAL": "https://libp2p.github.io/js-libp2p/variables/_libp2p_utils.DEFAULT_INTERVAL.html",
90
- "DEFAULT_MAX_TIMEOUT": "https://libp2p.github.io/js-libp2p/variables/_libp2p_utils.DEFAULT_MAX_TIMEOUT.html",
91
- "DEFAULT_MIN_TIMEOUT": "https://libp2p.github.io/js-libp2p/variables/_libp2p_utils.DEFAULT_MIN_TIMEOUT.html",
92
- "DEFAULT_TIMEOUT_MULTIPLIER": "https://libp2p.github.io/js-libp2p/variables/_libp2p_utils.DEFAULT_TIMEOUT_MULTIPLIER.html",
93
- "byteStream": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.byteStream.html",
94
- "createBloomFilter": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.createBloomFilter.html",
95
- "createCuckooFilter": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.createCuckooFilter.html",
96
- "createScalableCuckooFilter": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.createScalableCuckooFilter.html",
97
- "debounce": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.debounce.html",
98
- "echo": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.echo.html",
99
- "echoStream": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.echoStream.html",
100
- "getNetConfig": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.getNetConfig.html",
101
- "getThinWaistAddresses": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.getThinWaistAddresses.html",
102
- "ipPortToMultiaddr": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.ipPortToMultiaddr.html",
103
- "isAsyncGenerator": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.isAsyncGenerator.html",
104
- "isGenerator": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.isGenerator.html",
105
- "isGlobalUnicast": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.isGlobalUnicast.html",
106
- "isGlobalUnicastIp": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.isGlobalUnicastIp.html",
107
- "isLinkLocal": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.isLinkLocal.html",
108
- "isLinkLocalIp": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.isLinkLocalIp.html",
109
- "isLoopback": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.isLoopback.html",
110
- "isNetworkAddress": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.isNetworkAddress.html",
111
- "isPrivate": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.isPrivate.html",
112
- "isPrivateIp": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.isPrivateIp.html",
113
- "isPromise": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.isPromise.html",
114
- "lpStream": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.lpStream.html",
115
- "messageStreamToDuplex": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.messageStreamToDuplex.html",
116
- "mockMuxer": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.mockMuxer.html",
117
- "multiaddrConnectionPair": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.multiaddrConnectionPair.html",
118
- "pbStream": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.pbStream.html",
119
- "pipe": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.pipe.html",
120
- "repeatingTask": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.repeatingTask.html",
121
- "streamPair": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.streamPair.html",
122
- "trackedList": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.trackedList.html",
123
- "trackedMap": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.trackedMap.html"
124
- }