@libp2p/utils 6.7.2-6059227cb → 6.7.2-87bc8d4fb
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 +1 -1
- package/dist/index.min.js +1 -7
- package/dist/index.min.js.map +4 -4
- package/dist/src/abort-options.d.ts +7 -0
- package/dist/src/abort-options.d.ts.map +1 -0
- package/dist/src/abort-options.js +14 -0
- package/dist/src/abort-options.js.map +1 -0
- package/dist/src/abstract-stream.d.ts +130 -14
- package/dist/src/abstract-stream.d.ts.map +1 -1
- package/dist/src/abstract-stream.js +321 -39
- package/dist/src/abstract-stream.js.map +1 -1
- package/dist/src/array-equals.d.ts +24 -0
- package/dist/src/array-equals.d.ts.map +1 -0
- package/dist/src/array-equals.js +31 -0
- package/dist/src/array-equals.js.map +1 -0
- package/dist/src/close-source.d.ts +4 -0
- package/dist/src/close-source.d.ts.map +1 -0
- package/dist/src/close-source.js +11 -0
- package/dist/src/close-source.js.map +1 -0
- package/dist/src/close.d.ts +21 -0
- package/dist/src/close.d.ts.map +1 -0
- package/dist/src/close.js +49 -0
- package/dist/src/close.js.map +1 -0
- package/dist/src/errors.d.ts +0 -8
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +0 -8
- package/dist/src/errors.js.map +1 -1
- package/dist/src/get-thin-waist-addresses.browser.d.ts +1 -1
- package/dist/src/get-thin-waist-addresses.browser.d.ts.map +1 -1
- package/dist/src/get-thin-waist-addresses.browser.js +3 -4
- package/dist/src/get-thin-waist-addresses.browser.js.map +1 -1
- package/dist/src/get-thin-waist-addresses.d.ts +1 -1
- package/dist/src/get-thin-waist-addresses.d.ts.map +1 -1
- package/dist/src/get-thin-waist-addresses.js +9 -7
- package/dist/src/get-thin-waist-addresses.js.map +1 -1
- package/dist/src/index.d.ts +1 -31
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -31
- package/dist/src/index.js.map +1 -1
- package/dist/src/merge-options.d.ts +7 -0
- package/dist/src/merge-options.d.ts.map +1 -0
- package/dist/src/merge-options.js +128 -0
- package/dist/src/merge-options.js.map +1 -0
- package/dist/src/multiaddr/is-global-unicast.d.ts.map +1 -1
- package/dist/src/multiaddr/is-global-unicast.js +9 -8
- package/dist/src/multiaddr/is-global-unicast.js.map +1 -1
- package/dist/src/multiaddr/is-ip-based.d.ts +6 -0
- package/dist/src/multiaddr/is-ip-based.d.ts.map +1 -0
- package/dist/src/multiaddr/is-ip-based.js +18 -0
- package/dist/src/multiaddr/is-ip-based.js.map +1 -0
- package/dist/src/multiaddr/is-link-local.d.ts.map +1 -1
- package/dist/src/multiaddr/is-link-local.js +16 -11
- package/dist/src/multiaddr/is-link-local.js.map +1 -1
- package/dist/src/multiaddr/is-loopback.d.ts.map +1 -1
- package/dist/src/multiaddr/is-loopback.js +5 -12
- package/dist/src/multiaddr/is-loopback.js.map +1 -1
- package/dist/src/multiaddr/is-network-address.d.ts.map +1 -1
- package/dist/src/multiaddr/is-network-address.js +16 -4
- package/dist/src/multiaddr/is-network-address.js.map +1 -1
- package/dist/src/multiaddr/is-private.d.ts.map +1 -1
- package/dist/src/multiaddr/is-private.js +10 -9
- package/dist/src/multiaddr/is-private.js.map +1 -1
- package/dist/src/queue/index.d.ts +6 -3
- package/dist/src/queue/index.d.ts.map +1 -1
- package/dist/src/queue/index.js +4 -20
- package/dist/src/queue/index.js.map +1 -1
- package/dist/src/rate-limiter.d.ts +15 -1
- package/dist/src/rate-limiter.d.ts.map +1 -1
- package/dist/src/rate-limiter.js +14 -1
- package/dist/src/rate-limiter.js.map +1 -1
- package/dist/src/stream-to-ma-conn.d.ts +23 -0
- package/dist/src/stream-to-ma-conn.d.ts.map +1 -0
- package/dist/src/stream-to-ma-conn.js +75 -0
- package/dist/src/stream-to-ma-conn.js.map +1 -0
- package/package.json +162 -15
- package/src/abort-options.ts +20 -0
- package/src/abstract-stream.ts +464 -51
- package/src/array-equals.ts +34 -0
- package/src/close-source.ts +14 -0
- package/src/close.ts +65 -0
- package/src/errors.ts +0 -10
- package/src/get-thin-waist-addresses.browser.ts +4 -5
- package/src/get-thin-waist-addresses.ts +12 -8
- package/src/index.ts +1 -31
- package/src/merge-options.ts +161 -0
- package/src/multiaddr/is-global-unicast.ts +11 -8
- package/src/multiaddr/is-ip-based.ts +21 -0
- package/src/multiaddr/is-link-local.ts +20 -11
- package/src/multiaddr/is-loopback.ts +7 -12
- package/src/multiaddr/is-network-address.ts +19 -4
- package/src/multiaddr/is-private.ts +14 -9
- package/src/queue/index.ts +11 -24
- package/src/rate-limiter.ts +30 -3
- package/src/stream-to-ma-conn.ts +106 -0
- package/dist/src/abstract-message-stream.d.ts +0 -129
- package/dist/src/abstract-message-stream.d.ts.map +0 -1
- package/dist/src/abstract-message-stream.js +0 -393
- package/dist/src/abstract-message-stream.js.map +0 -1
- package/dist/src/abstract-multiaddr-connection.d.ts +0 -26
- package/dist/src/abstract-multiaddr-connection.d.ts.map +0 -1
- package/dist/src/abstract-multiaddr-connection.js +0 -66
- package/dist/src/abstract-multiaddr-connection.js.map +0 -1
- package/dist/src/abstract-stream-muxer.d.ts +0 -53
- package/dist/src/abstract-stream-muxer.d.ts.map +0 -1
- package/dist/src/abstract-stream-muxer.js +0 -169
- package/dist/src/abstract-stream-muxer.js.map +0 -1
- package/dist/src/length-prefixed-decoder.d.ts +0 -37
- package/dist/src/length-prefixed-decoder.d.ts.map +0 -1
- package/dist/src/length-prefixed-decoder.js +0 -64
- package/dist/src/length-prefixed-decoder.js.map +0 -1
- package/dist/src/message-queue.d.ts +0 -61
- package/dist/src/message-queue.d.ts.map +0 -1
- package/dist/src/message-queue.js +0 -93
- package/dist/src/message-queue.js.map +0 -1
- package/dist/src/mock-muxer.d.ts +0 -57
- package/dist/src/mock-muxer.d.ts.map +0 -1
- package/dist/src/mock-muxer.js +0 -204
- package/dist/src/mock-muxer.js.map +0 -1
- package/dist/src/mock-stream.d.ts +0 -31
- package/dist/src/mock-stream.d.ts.map +0 -1
- package/dist/src/mock-stream.js +0 -69
- package/dist/src/mock-stream.js.map +0 -1
- package/dist/src/multiaddr/get-net-config.d.ts +0 -55
- package/dist/src/multiaddr/get-net-config.d.ts.map +0 -1
- package/dist/src/multiaddr/get-net-config.js +0 -54
- package/dist/src/multiaddr/get-net-config.js.map +0 -1
- package/dist/src/multiaddr/index.d.ts +0 -7
- package/dist/src/multiaddr/index.d.ts.map +0 -1
- package/dist/src/multiaddr/index.js +0 -7
- package/dist/src/multiaddr/index.js.map +0 -1
- package/dist/src/multiaddr/utils.d.ts +0 -5
- package/dist/src/multiaddr/utils.d.ts.map +0 -1
- package/dist/src/multiaddr/utils.js +0 -32
- package/dist/src/multiaddr/utils.js.map +0 -1
- package/dist/src/multiaddr-connection-pair.d.ts +0 -25
- package/dist/src/multiaddr-connection-pair.d.ts.map +0 -1
- package/dist/src/multiaddr-connection-pair.js +0 -103
- package/dist/src/multiaddr-connection-pair.js.map +0 -1
- package/dist/src/stream-pair.d.ts +0 -42
- package/dist/src/stream-pair.d.ts.map +0 -1
- package/dist/src/stream-pair.js +0 -40
- package/dist/src/stream-pair.js.map +0 -1
- package/dist/src/stream-utils.d.ts +0 -191
- package/dist/src/stream-utils.d.ts.map +0 -1
- package/dist/src/stream-utils.js +0 -370
- package/dist/src/stream-utils.js.map +0 -1
- package/src/abstract-message-stream.ts +0 -553
- package/src/abstract-multiaddr-connection.ts +0 -93
- package/src/abstract-stream-muxer.ts +0 -239
- package/src/length-prefixed-decoder.ts +0 -98
- package/src/message-queue.ts +0 -156
- package/src/mock-muxer.ts +0 -304
- package/src/mock-stream.ts +0 -101
- package/src/multiaddr/get-net-config.ts +0 -112
- package/src/multiaddr/index.ts +0 -6
- package/src/multiaddr/utils.ts +0 -46
- package/src/multiaddr-connection-pair.ts +0 -147
- package/src/stream-pair.ts +0 -90
- package/src/stream-utils.ts +0 -865
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { netConfigToMultiaddr } from './multiaddr/utils.ts'
|
|
1
|
+
import { multiaddr } from '@multiformats/multiaddr'
|
|
3
2
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
4
3
|
|
|
5
4
|
/**
|
|
@@ -8,14 +7,14 @@ import type { Multiaddr } from '@multiformats/multiaddr'
|
|
|
8
7
|
*
|
|
9
8
|
* Wildcard IP4/6 addresses will be expanded into all available interfaces.
|
|
10
9
|
*/
|
|
11
|
-
export function getThinWaistAddresses (ma?: Multiaddr, port?: number
|
|
10
|
+
export function getThinWaistAddresses (ma?: Multiaddr, port?: number): Multiaddr[] {
|
|
12
11
|
if (ma == null) {
|
|
13
12
|
return []
|
|
14
13
|
}
|
|
15
14
|
|
|
16
|
-
const
|
|
15
|
+
const options = ma.toOptions()
|
|
17
16
|
|
|
18
17
|
return [
|
|
19
|
-
|
|
18
|
+
multiaddr(`/ip${options.family}/${options.host}/${options.transport}/${port ?? options.port}`)
|
|
20
19
|
]
|
|
21
20
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import os from 'node:os'
|
|
2
|
+
import { multiaddr } from '@multiformats/multiaddr'
|
|
2
3
|
import { isLinkLocalIp } from './link-local-ip.js'
|
|
3
|
-
import { getNetConfig } from './multiaddr/get-net-config.ts'
|
|
4
|
-
import { netConfigToMultiaddr } from './multiaddr/utils.ts'
|
|
5
4
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
6
5
|
|
|
7
6
|
const FAMILIES = { 4: 'IPv4', 6: 'IPv6' }
|
|
@@ -37,19 +36,24 @@ function getNetworkAddrs (family: 4 | 6): string[] {
|
|
|
37
36
|
*
|
|
38
37
|
* Wildcard IP4/6 addresses will be expanded into all available interfaces.
|
|
39
38
|
*/
|
|
40
|
-
export function getThinWaistAddresses (ma?: Multiaddr, port?: number
|
|
39
|
+
export function getThinWaistAddresses (ma?: Multiaddr, port?: number): Multiaddr[] {
|
|
41
40
|
if (ma == null) {
|
|
42
41
|
return []
|
|
43
42
|
}
|
|
44
43
|
|
|
45
|
-
const
|
|
44
|
+
const options = ma.toOptions()
|
|
46
45
|
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
if (isWildcard(options.host)) {
|
|
47
|
+
const addrs = []
|
|
48
|
+
|
|
49
|
+
for (const host of getNetworkAddrs(options.family)) {
|
|
50
|
+
addrs.push(multiaddr(`/ip${options.family}/${host}/${options.transport}/${port ?? options.port}`))
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return addrs
|
|
50
54
|
}
|
|
51
55
|
|
|
52
56
|
return [
|
|
53
|
-
|
|
57
|
+
multiaddr(`/ip${options.family}/${options.host}/${options.transport}/${port ?? options.port}`)
|
|
54
58
|
]
|
|
55
59
|
}
|
package/src/index.ts
CHANGED
|
@@ -4,34 +4,4 @@
|
|
|
4
4
|
* This module contains utility functions used by libp2p modules.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
export
|
|
8
|
-
export * from './multiaddr/index.js'
|
|
9
|
-
export * from './queue/index.js'
|
|
10
|
-
export * from './abstract-message-stream.js'
|
|
11
|
-
export * from './abstract-multiaddr-connection.js'
|
|
12
|
-
export * from './abstract-stream-muxer.js'
|
|
13
|
-
export * from './abstract-stream.js'
|
|
14
|
-
export * from './adaptive-timeout.js'
|
|
15
|
-
export * from './debounce.js'
|
|
16
|
-
export * from './errors.js'
|
|
17
|
-
export * from './get-thin-waist-addresses.js'
|
|
18
|
-
export * from './global-unicast-ip.js'
|
|
19
|
-
export * from './ip-port-to-multiaddr.js'
|
|
20
|
-
export * from './is-async-generator.js'
|
|
21
|
-
export * from './is-generator.js'
|
|
22
|
-
export * from './is-promise.js'
|
|
23
|
-
export * from './length-prefixed-decoder.js'
|
|
24
|
-
export * from './link-local-ip.js'
|
|
25
|
-
export * from './mock-muxer.js'
|
|
26
|
-
export * from './mock-stream.js'
|
|
27
|
-
export * from './moving-average.js'
|
|
28
|
-
export * from './multiaddr-connection-pair.js'
|
|
29
|
-
export * from './peer-queue.js'
|
|
30
|
-
export * from './priority-queue.js'
|
|
31
|
-
export * from './private-ip.js'
|
|
32
|
-
export * from './rate-limiter.js'
|
|
33
|
-
export * from './repeating-task.js'
|
|
34
|
-
export * from './stream-pair.js'
|
|
35
|
-
export * from './stream-utils.js'
|
|
36
|
-
export * from './tracked-list.js'
|
|
37
|
-
export * from './tracked-map.js'
|
|
7
|
+
export {}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import isOptionObject from 'is-plain-obj'
|
|
2
|
+
|
|
3
|
+
const { hasOwnProperty } = Object.prototype
|
|
4
|
+
const { propertyIsEnumerable } = Object
|
|
5
|
+
const defineProperty = (object: any, name: any, value: any): void => {
|
|
6
|
+
Object.defineProperty(object, name, {
|
|
7
|
+
value,
|
|
8
|
+
writable: true,
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true
|
|
11
|
+
})
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const globalThis = this
|
|
15
|
+
const defaultMergeOptions = {
|
|
16
|
+
concatArrays: false,
|
|
17
|
+
ignoreUndefined: false
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const getEnumerableOwnPropertyKeys = (value: any): any[] => {
|
|
21
|
+
const keys = []
|
|
22
|
+
|
|
23
|
+
for (const key in value) {
|
|
24
|
+
if (hasOwnProperty.call(value, key)) {
|
|
25
|
+
keys.push(key)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/* istanbul ignore else */
|
|
30
|
+
if (Object.getOwnPropertySymbols) {
|
|
31
|
+
const symbols = Object.getOwnPropertySymbols(value)
|
|
32
|
+
|
|
33
|
+
for (const symbol of symbols) {
|
|
34
|
+
if (propertyIsEnumerable.call(value, symbol)) {
|
|
35
|
+
keys.push(symbol)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return keys
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function clone <T> (value: T): T
|
|
44
|
+
function clone <T> (value: T[]): T[]
|
|
45
|
+
function clone (value: any): any {
|
|
46
|
+
if (Array.isArray(value)) {
|
|
47
|
+
return cloneArray(value)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (isOptionObject(value)) {
|
|
51
|
+
return cloneOptionObject(value)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return value
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function cloneArray <T> (array: T[]): T[] {
|
|
58
|
+
const result = array.slice(0, 0)
|
|
59
|
+
|
|
60
|
+
getEnumerableOwnPropertyKeys(array).forEach(key => {
|
|
61
|
+
defineProperty(result, key, clone(array[key]))
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
return result
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function cloneOptionObject (object: any): any {
|
|
68
|
+
const result = Object.getPrototypeOf(object) === null ? Object.create(null) : {}
|
|
69
|
+
|
|
70
|
+
getEnumerableOwnPropertyKeys(object).forEach(key => {
|
|
71
|
+
defineProperty(result, key, clone(object[key]))
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
return result
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const mergeKeys = (merged: any, source: any, keys: any[], config: any): any => {
|
|
78
|
+
keys.forEach(key => {
|
|
79
|
+
if (typeof source[key] === 'undefined' && config.ignoreUndefined) {
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Do not recurse into prototype chain of merged
|
|
84
|
+
if (key in merged && merged[key] !== Object.getPrototypeOf(merged)) {
|
|
85
|
+
defineProperty(merged, key, merge(merged[key], source[key], config))
|
|
86
|
+
} else {
|
|
87
|
+
defineProperty(merged, key, clone(source[key]))
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
return merged
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* see [Array.prototype.concat ( ...arguments )](http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.concat)
|
|
96
|
+
*/
|
|
97
|
+
const concatArrays = (merged: any, source: any, config: any): any => {
|
|
98
|
+
let result = merged.slice(0, 0)
|
|
99
|
+
let resultIndex = 0;
|
|
100
|
+
|
|
101
|
+
[merged, source].forEach(array => {
|
|
102
|
+
const indices: any[] = []
|
|
103
|
+
|
|
104
|
+
// `result.concat(array)` with cloning
|
|
105
|
+
for (let k = 0; k < array.length; k++) {
|
|
106
|
+
if (!hasOwnProperty.call(array, k)) {
|
|
107
|
+
continue
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
indices.push(String(k))
|
|
111
|
+
|
|
112
|
+
if (array === merged) {
|
|
113
|
+
// Already cloned
|
|
114
|
+
defineProperty(result, resultIndex++, array[k])
|
|
115
|
+
} else {
|
|
116
|
+
defineProperty(result, resultIndex++, clone(array[k]))
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Merge non-index keys
|
|
121
|
+
result = mergeKeys(result, array, getEnumerableOwnPropertyKeys(array).filter(key => !indices.includes(key)), config)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
return result
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function merge (merged: any, source: any, config: any): any {
|
|
128
|
+
if (config.concatArrays && Array.isArray(merged) && Array.isArray(source)) {
|
|
129
|
+
return concatArrays(merged, source, config)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (!isOptionObject(source) || !isOptionObject(merged)) {
|
|
133
|
+
return clone(source)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return mergeKeys(merged, source, getEnumerableOwnPropertyKeys(source), config)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Port of `merge-options` to typescript
|
|
141
|
+
*
|
|
142
|
+
* @see https://github.com/schnittstabil/merge-options/pull/28
|
|
143
|
+
*/
|
|
144
|
+
export function mergeOptions (this: any, ...options: any[]): any {
|
|
145
|
+
const config = merge(clone(defaultMergeOptions), (this !== globalThis && this) || {}, defaultMergeOptions)
|
|
146
|
+
let merged = { _: {} }
|
|
147
|
+
|
|
148
|
+
for (const option of options) {
|
|
149
|
+
if (option === undefined) {
|
|
150
|
+
continue
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (!isOptionObject(option)) {
|
|
154
|
+
throw new TypeError('`' + option + '` is not an Option Object')
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
merged = merge(merged, { _: option }, config)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return merged._
|
|
161
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { cidrContains } from '@chainsafe/netmask'
|
|
2
|
-
import {
|
|
2
|
+
import { CODE_IP6 } from '@multiformats/multiaddr'
|
|
3
3
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -7,15 +7,18 @@ import type { Multiaddr } from '@multiformats/multiaddr'
|
|
|
7
7
|
*/
|
|
8
8
|
export function isGlobalUnicast (ma: Multiaddr): boolean {
|
|
9
9
|
try {
|
|
10
|
-
const
|
|
10
|
+
for (const { code, value } of ma.getComponents()) {
|
|
11
|
+
if (value == null) {
|
|
12
|
+
continue
|
|
13
|
+
}
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
default:
|
|
16
|
-
return false
|
|
15
|
+
if (code === CODE_IP6) {
|
|
16
|
+
return cidrContains('2000::/3', value)
|
|
17
|
+
}
|
|
17
18
|
}
|
|
18
19
|
} catch {
|
|
19
|
-
|
|
20
|
+
|
|
20
21
|
}
|
|
22
|
+
|
|
23
|
+
return false
|
|
21
24
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { CODE_IP4, CODE_IP6, CODE_IP6ZONE } from '@multiformats/multiaddr'
|
|
2
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Check if a given multiaddr is IP-based
|
|
6
|
+
*/
|
|
7
|
+
export function isIpBased (ma: Multiaddr): boolean {
|
|
8
|
+
try {
|
|
9
|
+
for (const { code } of ma.getComponents()) {
|
|
10
|
+
if (code === CODE_IP6ZONE) {
|
|
11
|
+
continue
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return code === CODE_IP4 || code === CODE_IP6
|
|
15
|
+
}
|
|
16
|
+
} catch {
|
|
17
|
+
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return false
|
|
21
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CODE_IP4, CODE_IP6, CODE_IP6ZONE } from '@multiformats/multiaddr'
|
|
2
2
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -6,17 +6,26 @@ import type { Multiaddr } from '@multiformats/multiaddr'
|
|
|
6
6
|
*/
|
|
7
7
|
export function isLinkLocal (ma: Multiaddr): boolean {
|
|
8
8
|
try {
|
|
9
|
-
const
|
|
9
|
+
for (const { code, value } of ma.getComponents()) {
|
|
10
|
+
if (code === CODE_IP6ZONE) {
|
|
11
|
+
continue
|
|
12
|
+
}
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
if (value == null) {
|
|
15
|
+
continue
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (code === CODE_IP4) {
|
|
19
|
+
return value.startsWith('169.254.')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (code === CODE_IP6) {
|
|
23
|
+
return value.toLowerCase().startsWith('fe80')
|
|
24
|
+
}
|
|
18
25
|
}
|
|
19
|
-
} catch
|
|
20
|
-
|
|
26
|
+
} catch {
|
|
27
|
+
|
|
21
28
|
}
|
|
29
|
+
|
|
30
|
+
return false
|
|
22
31
|
}
|
|
@@ -1,22 +1,17 @@
|
|
|
1
1
|
import { isLoopbackAddr } from 'is-loopback-addr'
|
|
2
|
-
import {
|
|
2
|
+
import { isIpBased } from './is-ip-based.js'
|
|
3
3
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Check if a given multiaddr is a loopback address.
|
|
7
7
|
*/
|
|
8
8
|
export function isLoopback (ma: Multiaddr): boolean {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
switch (config.type) {
|
|
13
|
-
case 'ip4':
|
|
14
|
-
case 'ip6':
|
|
15
|
-
return isLoopbackAddr(config.host)
|
|
16
|
-
default:
|
|
17
|
-
return false
|
|
18
|
-
}
|
|
19
|
-
} catch {
|
|
9
|
+
if (!isIpBased(ma)) {
|
|
10
|
+
// not an IP based multiaddr, cannot be private
|
|
20
11
|
return false
|
|
21
12
|
}
|
|
13
|
+
|
|
14
|
+
const { address } = ma.nodeAddress()
|
|
15
|
+
|
|
16
|
+
return isLoopbackAddr(address)
|
|
22
17
|
}
|
|
@@ -1,15 +1,30 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CODE_IP4, CODE_IP6, CODE_IP6ZONE, CODE_DNS, CODE_DNS4, CODE_DNS6, CODE_DNSADDR } from '@multiformats/multiaddr'
|
|
2
2
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
3
3
|
|
|
4
|
+
const NETWORK_CODECS = [
|
|
5
|
+
CODE_IP4,
|
|
6
|
+
CODE_IP6,
|
|
7
|
+
CODE_DNS,
|
|
8
|
+
CODE_DNS4,
|
|
9
|
+
CODE_DNS6,
|
|
10
|
+
CODE_DNSADDR
|
|
11
|
+
]
|
|
12
|
+
|
|
4
13
|
/**
|
|
5
14
|
* Check if a given multiaddr is a network address
|
|
6
15
|
*/
|
|
7
16
|
export function isNetworkAddress (ma: Multiaddr): boolean {
|
|
8
17
|
try {
|
|
9
|
-
|
|
18
|
+
for (const { code } of ma.getComponents()) {
|
|
19
|
+
if (code === CODE_IP6ZONE) {
|
|
20
|
+
continue
|
|
21
|
+
}
|
|
10
22
|
|
|
11
|
-
|
|
23
|
+
return NETWORK_CODECS.includes(code)
|
|
24
|
+
}
|
|
12
25
|
} catch {
|
|
13
|
-
|
|
26
|
+
|
|
14
27
|
}
|
|
28
|
+
|
|
29
|
+
return false
|
|
15
30
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isPrivateIp } from '../private-ip.js'
|
|
2
|
-
import {
|
|
2
|
+
import { isIpBased } from './is-ip-based.js'
|
|
3
3
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -7,16 +7,21 @@ import type { Multiaddr } from '@multiformats/multiaddr'
|
|
|
7
7
|
*/
|
|
8
8
|
export function isPrivate (ma: Multiaddr): boolean {
|
|
9
9
|
try {
|
|
10
|
-
|
|
10
|
+
if (!isIpBased(ma)) {
|
|
11
|
+
// not an IP based multiaddr, cannot be private
|
|
12
|
+
return false
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const [[, value]] = ma.stringTuples()
|
|
11
16
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
case 'ip6':
|
|
15
|
-
return isPrivateIp(config.host) ?? false
|
|
16
|
-
default:
|
|
17
|
-
return config.host === 'localhost'
|
|
17
|
+
if (value == null) {
|
|
18
|
+
return false
|
|
18
19
|
}
|
|
20
|
+
|
|
21
|
+
return isPrivateIp(value) ?? false
|
|
19
22
|
} catch {
|
|
20
|
-
|
|
23
|
+
|
|
21
24
|
}
|
|
25
|
+
|
|
26
|
+
return true
|
|
22
27
|
}
|
package/src/queue/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AbortError } from '@libp2p/interface'
|
|
2
2
|
import { pushable } from 'it-pushable'
|
|
3
3
|
import { TypedEventEmitter } from 'main-event'
|
|
4
|
-
import {
|
|
4
|
+
import { raceEvent } from 'race-event'
|
|
5
5
|
import { debounce } from '../debounce.js'
|
|
6
6
|
import { QueueFullError } from '../errors.js'
|
|
7
7
|
import { Job } from './job.js'
|
|
@@ -99,6 +99,13 @@ export interface QueueEvents<JobReturnType, JobOptions extends AbortOptions = Ab
|
|
|
99
99
|
*/
|
|
100
100
|
completed: CustomEvent<JobReturnType>
|
|
101
101
|
|
|
102
|
+
/**
|
|
103
|
+
* A job has failed
|
|
104
|
+
*
|
|
105
|
+
* @deprecated Listen for the 'failure' event instead - it gives more context and is generally more useful, this event will be removed in a future release
|
|
106
|
+
*/
|
|
107
|
+
error: CustomEvent<Error>
|
|
108
|
+
|
|
102
109
|
/**
|
|
103
110
|
* Emitted just after `"completed", a job has finished successfully - this
|
|
104
111
|
* event gives access to the job and it's result
|
|
@@ -124,7 +131,6 @@ export class Queue<JobReturnType = unknown, JobOptions extends AbortOptions = Ab
|
|
|
124
131
|
public queue: Array<Job<JobOptions, JobReturnType>>
|
|
125
132
|
private pending: number
|
|
126
133
|
private readonly sort?: Comparator<Job<JobOptions, JobReturnType>>
|
|
127
|
-
private paused: boolean
|
|
128
134
|
|
|
129
135
|
constructor (init: QueueInit<JobReturnType, JobOptions> = {}) {
|
|
130
136
|
super()
|
|
@@ -132,7 +138,6 @@ export class Queue<JobReturnType = unknown, JobOptions extends AbortOptions = Ab
|
|
|
132
138
|
this.concurrency = init.concurrency ?? Number.POSITIVE_INFINITY
|
|
133
139
|
this.maxSize = init.maxSize ?? Number.POSITIVE_INFINITY
|
|
134
140
|
this.pending = 0
|
|
135
|
-
this.paused = false
|
|
136
141
|
|
|
137
142
|
if (init.metricName != null) {
|
|
138
143
|
init.metrics?.registerMetricGroup(init.metricName, {
|
|
@@ -169,24 +174,7 @@ export class Queue<JobReturnType = unknown, JobOptions extends AbortOptions = Ab
|
|
|
169
174
|
this.safeDispatchEvent('idle')
|
|
170
175
|
}
|
|
171
176
|
|
|
172
|
-
pause (): void {
|
|
173
|
-
this.paused = true
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
resume (): void {
|
|
177
|
-
if (!this.paused) {
|
|
178
|
-
return
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
this.paused = false
|
|
182
|
-
this.tryToStartAnother()
|
|
183
|
-
}
|
|
184
|
-
|
|
185
177
|
private tryToStartAnother (): boolean {
|
|
186
|
-
if (this.paused) {
|
|
187
|
-
return false
|
|
188
|
-
}
|
|
189
|
-
|
|
190
178
|
if (this.size === 0) {
|
|
191
179
|
this.emitEmpty()
|
|
192
180
|
|
|
@@ -312,7 +300,7 @@ export class Queue<JobReturnType = unknown, JobOptions extends AbortOptions = Ab
|
|
|
312
300
|
return
|
|
313
301
|
}
|
|
314
302
|
|
|
315
|
-
await
|
|
303
|
+
await raceEvent(this, 'empty', options?.signal)
|
|
316
304
|
}
|
|
317
305
|
|
|
318
306
|
/**
|
|
@@ -332,8 +320,7 @@ export class Queue<JobReturnType = unknown, JobOptions extends AbortOptions = Ab
|
|
|
332
320
|
return
|
|
333
321
|
}
|
|
334
322
|
|
|
335
|
-
await
|
|
336
|
-
...options,
|
|
323
|
+
await raceEvent(this, 'next', options?.signal, {
|
|
337
324
|
filter: () => this.size < limit
|
|
338
325
|
})
|
|
339
326
|
}
|
|
@@ -352,7 +339,7 @@ export class Queue<JobReturnType = unknown, JobOptions extends AbortOptions = Ab
|
|
|
352
339
|
return
|
|
353
340
|
}
|
|
354
341
|
|
|
355
|
-
await
|
|
342
|
+
await raceEvent(this, 'idle', options?.signal)
|
|
356
343
|
}
|
|
357
344
|
|
|
358
345
|
/**
|
package/src/rate-limiter.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import delay from 'delay'
|
|
1
2
|
import { RateLimitError } from './errors.js'
|
|
2
3
|
|
|
3
4
|
export interface RateLimiterInit {
|
|
@@ -22,6 +23,20 @@ export interface RateLimiterInit {
|
|
|
22
23
|
*/
|
|
23
24
|
blockDuration?: number
|
|
24
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Execute allowed actions evenly over duration
|
|
28
|
+
*
|
|
29
|
+
* @default false
|
|
30
|
+
*/
|
|
31
|
+
execEvenly?: boolean
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* ms, works with execEvenly=true option
|
|
35
|
+
*
|
|
36
|
+
* @default duration * 1000 / points
|
|
37
|
+
*/
|
|
38
|
+
execEvenlyMinDelayMs?: number
|
|
39
|
+
|
|
25
40
|
/**
|
|
26
41
|
* @default "rlflx"
|
|
27
42
|
*/
|
|
@@ -50,17 +65,21 @@ export class RateLimiter {
|
|
|
50
65
|
protected points: number
|
|
51
66
|
protected duration: number
|
|
52
67
|
protected blockDuration: number
|
|
68
|
+
protected execEvenly: boolean
|
|
69
|
+
protected execEvenlyMinDelayMs: number
|
|
53
70
|
protected keyPrefix: string
|
|
54
71
|
|
|
55
72
|
constructor (opts: RateLimiterInit = {}) {
|
|
56
73
|
this.points = opts.points ?? 4
|
|
57
74
|
this.duration = opts.duration ?? 1
|
|
58
75
|
this.blockDuration = opts.blockDuration ?? 0
|
|
76
|
+
this.execEvenly = opts.execEvenly ?? false
|
|
77
|
+
this.execEvenlyMinDelayMs = opts.execEvenlyMinDelayMs ?? (this.duration * 1000 / this.points)
|
|
59
78
|
this.keyPrefix = opts.keyPrefix ?? 'rlflx'
|
|
60
79
|
this.memoryStorage = new MemoryStorage()
|
|
61
80
|
}
|
|
62
81
|
|
|
63
|
-
consume (key: string, pointsToConsume: number = 1, options: GetKeySecDurationOptions = {}): RateLimiterResult {
|
|
82
|
+
async consume (key: string, pointsToConsume: number = 1, options: GetKeySecDurationOptions = {}): Promise<RateLimiterResult> {
|
|
64
83
|
const rlKey = this.getKey(key)
|
|
65
84
|
const secDuration = this._getKeySecDuration(options)
|
|
66
85
|
let res = this.memoryStorage.incrby(rlKey, pointsToConsume, secDuration)
|
|
@@ -74,6 +93,14 @@ export class RateLimiter {
|
|
|
74
93
|
}
|
|
75
94
|
|
|
76
95
|
throw new RateLimitError('Rate limit exceeded', res)
|
|
96
|
+
} else if (this.execEvenly && res.msBeforeNext > 0 && !res.isFirstInDuration) {
|
|
97
|
+
// Execute evenly
|
|
98
|
+
let delayMs = Math.ceil(res.msBeforeNext / (res.remainingPoints + 2))
|
|
99
|
+
if (delayMs < this.execEvenlyMinDelayMs) {
|
|
100
|
+
delayMs = res.consumedPoints * this.execEvenlyMinDelayMs
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
await delay(delayMs)
|
|
77
104
|
}
|
|
78
105
|
|
|
79
106
|
return res
|
|
@@ -214,8 +241,8 @@ export class MemoryStorage {
|
|
|
214
241
|
this.storage.delete(key)
|
|
215
242
|
}, durationMs)
|
|
216
243
|
|
|
217
|
-
if (
|
|
218
|
-
|
|
244
|
+
if (record.timeoutId.unref != null) {
|
|
245
|
+
record.timeoutId.unref()
|
|
219
246
|
}
|
|
220
247
|
}
|
|
221
248
|
|