@libp2p/utils 6.6.3 → 6.6.4

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.
Files changed (48) hide show
  1. package/dist/src/abstract-stream.d.ts +1 -1
  2. package/dist/src/abstract-stream.d.ts.map +1 -1
  3. package/dist/src/abstract-stream.js +6 -7
  4. package/dist/src/abstract-stream.js.map +1 -1
  5. package/dist/src/adaptive-timeout.d.ts +1 -1
  6. package/dist/src/adaptive-timeout.d.ts.map +1 -1
  7. package/dist/src/adaptive-timeout.js.map +1 -1
  8. package/dist/src/debounce.js.map +1 -1
  9. package/dist/src/filters/cuckoo-filter.d.ts +1 -1
  10. package/dist/src/filters/cuckoo-filter.d.ts.map +1 -1
  11. package/dist/src/filters/cuckoo-filter.js.map +1 -1
  12. package/dist/src/filters/fingerprint.d.ts +1 -1
  13. package/dist/src/filters/fingerprint.d.ts.map +1 -1
  14. package/dist/src/filters/scalable-cuckoo-filter.d.ts +1 -1
  15. package/dist/src/filters/scalable-cuckoo-filter.d.ts.map +1 -1
  16. package/dist/src/filters/scalable-cuckoo-filter.js.map +1 -1
  17. package/dist/src/ip-port-to-multiaddr.d.ts +1 -1
  18. package/dist/src/ip-port-to-multiaddr.d.ts.map +1 -1
  19. package/dist/src/ip-port-to-multiaddr.js +1 -1
  20. package/dist/src/ip-port-to-multiaddr.js.map +1 -1
  21. package/dist/src/merge-options.d.ts +7 -0
  22. package/dist/src/merge-options.d.ts.map +1 -0
  23. package/dist/src/merge-options.js +128 -0
  24. package/dist/src/merge-options.js.map +1 -0
  25. package/dist/src/private-ip.d.ts.map +1 -1
  26. package/dist/src/private-ip.js +12 -6
  27. package/dist/src/private-ip.js.map +1 -1
  28. package/dist/src/queue/index.d.ts +9 -9
  29. package/dist/src/queue/index.d.ts.map +1 -1
  30. package/dist/src/repeating-task.d.ts +1 -1
  31. package/dist/src/repeating-task.d.ts.map +1 -1
  32. package/dist/src/repeating-task.js +1 -3
  33. package/dist/src/repeating-task.js.map +1 -1
  34. package/dist/src/stream-to-ma-conn.js.map +1 -1
  35. package/dist/typedoc-urls.json +96 -94
  36. package/package.json +20 -22
  37. package/src/abstract-stream.ts +11 -10
  38. package/src/adaptive-timeout.ts +2 -1
  39. package/src/debounce.ts +2 -2
  40. package/src/filters/cuckoo-filter.ts +2 -1
  41. package/src/filters/fingerprint.ts +1 -1
  42. package/src/filters/scalable-cuckoo-filter.ts +4 -2
  43. package/src/ip-port-to-multiaddr.ts +3 -2
  44. package/src/merge-options.ts +161 -0
  45. package/src/private-ip.ts +2 -6
  46. package/src/queue/index.ts +9 -9
  47. package/src/repeating-task.ts +6 -9
  48. package/src/stream-to-ma-conn.ts +4 -4
@@ -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
+ }
package/src/private-ip.ts CHANGED
@@ -32,7 +32,7 @@ const NETMASK_RANGES = PRIVATE_IP_RANGES.map(ipRange => new Netmask(ipRange))
32
32
 
33
33
  function ipv4Check (ipAddr: string): boolean {
34
34
  for (const r of NETMASK_RANGES) {
35
- if (r.contains(ipAddr)) return true
35
+ if (r.contains(ipAddr)) { return true }
36
36
  }
37
37
 
38
38
  return false
@@ -89,9 +89,5 @@ function ipv6Check (ipAddr: string): boolean {
89
89
  }
90
90
 
91
91
  export function isPrivateIp (ip: string): boolean | undefined {
92
- if (isIPv4(ip)) return ipv4Check(ip)
93
- else if (isIpv4MappedIpv6(ip)) return ipv4MappedIpv6Check(ip)
94
- else if (isIpv4EmbeddedIpv6(ip)) return ipv4EmbeddedIpv6Check(ip)
95
- else if (isIPv6(ip)) return ipv6Check(ip)
96
- else return undefined
92
+ if (isIPv4(ip)) { return ipv4Check(ip) } else if (isIpv4MappedIpv6(ip)) { return ipv4MappedIpv6Check(ip) } else if (isIpv4EmbeddedIpv6(ip)) { return ipv4EmbeddedIpv6Check(ip) } else if (isIPv6(ip)) { return ipv6Check(ip) } else { return undefined }
97
93
  }
@@ -71,49 +71,49 @@ export interface QueueEvents<JobReturnType, JobOptions extends AbortOptions = Ab
71
71
  /**
72
72
  * A job is about to start running
73
73
  */
74
- 'active': CustomEvent
74
+ active: CustomEvent
75
75
 
76
76
  /**
77
77
  * All jobs have finished and the queue is empty
78
78
  */
79
- 'idle': CustomEvent
79
+ idle: CustomEvent
80
80
 
81
81
  /**
82
82
  * The queue is empty, jobs may be running
83
83
  */
84
- 'empty': CustomEvent
84
+ empty: CustomEvent
85
85
 
86
86
  /**
87
87
  * A job was added to the queue
88
88
  */
89
- 'add': CustomEvent
89
+ add: CustomEvent
90
90
 
91
91
  /**
92
92
  * A job has finished or failed
93
93
  */
94
- 'next': CustomEvent
94
+ next: CustomEvent
95
95
 
96
96
  /**
97
97
  * A job has finished successfully
98
98
  */
99
- 'completed': CustomEvent<JobReturnType>
99
+ completed: CustomEvent<JobReturnType>
100
100
 
101
101
  /**
102
102
  * A job has failed
103
103
  */
104
- 'error': CustomEvent<Error>
104
+ error: CustomEvent<Error>
105
105
 
106
106
  /**
107
107
  * Emitted just after `"completed", a job has finished successfully - this
108
108
  * event gives access to the job and it's result
109
109
  */
110
- 'success': CustomEvent<QueueJobSuccess<JobReturnType, JobOptions>>
110
+ success: CustomEvent<QueueJobSuccess<JobReturnType, JobOptions>>
111
111
 
112
112
  /**
113
113
  * Emitted just after `"error", a job has failed - this event gives access to
114
114
  * the job and the thrown error
115
115
  */
116
- 'failure': CustomEvent<QueueJobFailure<JobReturnType, JobOptions>>
116
+ failure: CustomEvent<QueueJobFailure<JobReturnType, JobOptions>>
117
117
  }
118
118
 
119
119
  /**
@@ -18,7 +18,7 @@ export interface RepeatingTask {
18
18
  * Update the amount of time a task will run before the passed abort signal
19
19
  * will fire.
20
20
  *
21
- * * This only affects the next iteration of the task, if it is currently
21
+ * This only affects the next iteration of the task, if it is currently
22
22
  * running, that run will not be interrupted.
23
23
  */
24
24
  setTimeout(ms: number): void
@@ -83,7 +83,7 @@ export function repeatingTask (fn: (options?: AbortOptions) => void | Promise<vo
83
83
  let started = false
84
84
 
85
85
  return {
86
- setInterval: (ms) => {
86
+ setInterval: (ms): void => {
87
87
  interval = ms
88
88
 
89
89
  // maybe reschedule
@@ -92,14 +92,11 @@ export function repeatingTask (fn: (options?: AbortOptions) => void | Promise<vo
92
92
  timeout = setTimeout(runTask, interval)
93
93
  }
94
94
  },
95
- setTimeout: (ms) => {
96
- if (options == null) {
97
- options = {}
98
- }
99
-
95
+ setTimeout: (ms): void => {
96
+ options ??= {}
100
97
  options.timeout = ms
101
98
  },
102
- start: () => {
99
+ start: (): void => {
103
100
  if (started) {
104
101
  return
105
102
  }
@@ -118,7 +115,7 @@ export function repeatingTask (fn: (options?: AbortOptions) => void | Promise<vo
118
115
  timeout = setTimeout(runTask, interval)
119
116
  }
120
117
  },
121
- stop: () => {
118
+ stop: (): void => {
122
119
  clearTimeout(timeout)
123
120
  shutdownController?.abort()
124
121
  started = false
@@ -34,21 +34,21 @@ export function streamToMaConnection (props: StreamProperties): MultiaddrConnect
34
34
 
35
35
  // piggyback on `stream.close` invocations to close multiaddr connection
36
36
  const streamClose = stream.close.bind(stream)
37
- stream.close = async (options) => {
37
+ stream.close = async (options): Promise<void> => {
38
38
  await streamClose(options)
39
39
  close(true)
40
40
  }
41
41
 
42
42
  // piggyback on `stream.abort` invocations to close multiaddr connection
43
43
  const streamAbort = stream.abort.bind(stream)
44
- stream.abort = (err) => {
44
+ stream.abort = (err): void => {
45
45
  streamAbort(err)
46
46
  close(true)
47
47
  }
48
48
 
49
49
  // piggyback on `stream.sink` invocations to close multiaddr connection
50
50
  const streamSink = stream.sink.bind(stream)
51
- stream.sink = async (source) => {
51
+ stream.sink = async (source): Promise<void> => {
52
52
  try {
53
53
  await streamSink(
54
54
  pipe(
@@ -73,7 +73,7 @@ export function streamToMaConnection (props: StreamProperties): MultiaddrConnect
73
73
  const maConn: MultiaddrConnection = {
74
74
  log,
75
75
  sink: stream.sink,
76
- source: (async function * () {
76
+ source: (async function * (): AsyncGenerator<Uint8ArrayList> {
77
77
  try {
78
78
  for await (const buf of stream.source) {
79
79
  onDataRead?.(buf)