@libp2p/utils 4.0.6 → 4.0.7-97ab31c0c
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/src/ip-port-to-multiaddr.d.ts.map +1 -1
- package/dist/src/ip-port-to-multiaddr.js +1 -5
- package/dist/src/ip-port-to-multiaddr.js.map +1 -1
- package/dist/src/peer-job-queue.d.ts +33 -0
- package/dist/src/peer-job-queue.d.ts.map +1 -0
- package/dist/src/peer-job-queue.js +81 -0
- package/dist/src/peer-job-queue.js.map +1 -0
- package/dist/src/stream-to-ma-conn.d.ts +2 -0
- package/dist/src/stream-to-ma-conn.d.ts.map +1 -1
- package/dist/src/stream-to-ma-conn.js +2 -3
- package/dist/src/stream-to-ma-conn.js.map +1 -1
- package/package.json +15 -8
- package/src/ip-port-to-multiaddr.ts +1 -6
- package/src/peer-job-queue.ts +118 -0
- package/src/stream-to-ma-conn.ts +4 -5
- package/dist/typedoc-urls.json +0 -24
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ip-port-to-multiaddr.d.ts","sourceRoot":"","sources":["../../src/ip-port-to-multiaddr.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ip-port-to-multiaddr.d.ts","sourceRoot":"","sources":["../../src/ip-port-to-multiaddr.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,SAAS,EAAa,MAAM,yBAAyB,CAAA;AAEnE,eAAO,MAAM,MAAM;;;;CAIlB,CAAA;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAsB/E"}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { isIPv4, isIPv6 } from '@chainsafe/is-ip';
|
|
2
2
|
import { CodeError } from '@libp2p/interface/errors';
|
|
3
|
-
import { logger } from '@libp2p/logger';
|
|
4
3
|
import { multiaddr } from '@multiformats/multiaddr';
|
|
5
|
-
const log = logger('libp2p:ip-port-to-multiaddr');
|
|
6
4
|
export const Errors = {
|
|
7
5
|
ERR_INVALID_IP_PARAMETER: 'ERR_INVALID_IP_PARAMETER',
|
|
8
6
|
ERR_INVALID_PORT_PARAMETER: 'ERR_INVALID_PORT_PARAMETER',
|
|
@@ -27,8 +25,6 @@ export function ipPortToMultiaddr(ip, port) {
|
|
|
27
25
|
if (isIPv6(ip)) {
|
|
28
26
|
return multiaddr(`/ip6/${ip}/tcp/${port}`);
|
|
29
27
|
}
|
|
30
|
-
|
|
31
|
-
log.error(errMsg);
|
|
32
|
-
throw new CodeError(errMsg, Errors.ERR_INVALID_IP);
|
|
28
|
+
throw new CodeError(`invalid ip:port for creating a multiaddr: ${ip}:${port}`, Errors.ERR_INVALID_IP);
|
|
33
29
|
}
|
|
34
30
|
//# sourceMappingURL=ip-port-to-multiaddr.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ip-port-to-multiaddr.js","sourceRoot":"","sources":["../../src/ip-port-to-multiaddr.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,
|
|
1
|
+
{"version":3,"file":"ip-port-to-multiaddr.js","sourceRoot":"","sources":["../../src/ip-port-to-multiaddr.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAkB,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAEnE,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB,wBAAwB,EAAE,0BAA0B;IACpD,0BAA0B,EAAE,4BAA4B;IACxD,cAAc,EAAE,gBAAgB;CACjC,CAAA;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAE,EAAU,EAAE,IAAqB;IAClE,IAAI,OAAO,EAAE,KAAK,QAAQ,EAAE;QAC1B,MAAM,IAAI,SAAS,CAAC,wBAAwB,EAAE,EAAE,EAAE,MAAM,CAAC,wBAAwB,CAAC,CAAA,CAAC,uEAAuE;KAC3J;IAED,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;QAC5B,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAA;KACtB;IAED,IAAI,KAAK,CAAC,IAAI,CAAC,EAAE;QACf,MAAM,IAAI,SAAS,CAAC,0BAA0B,IAAI,EAAE,EAAE,MAAM,CAAC,0BAA0B,CAAC,CAAA;KACzF;IAED,IAAI,MAAM,CAAC,EAAE,CAAC,EAAE;QACd,OAAO,SAAS,CAAC,QAAQ,EAAE,QAAQ,IAAI,EAAE,CAAC,CAAA;KAC3C;IAED,IAAI,MAAM,CAAC,EAAE,CAAC,EAAE;QACd,OAAO,SAAS,CAAC,QAAQ,EAAE,QAAQ,IAAI,EAAE,CAAC,CAAA;KAC3C;IAED,MAAM,IAAI,SAAS,CAAC,6CAA6C,EAAE,IAAI,IAAI,EAAE,EAAE,MAAM,CAAC,cAAc,CAAC,CAAA;AACvG,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import PQueue from 'p-queue';
|
|
2
|
+
import type { PeerId } from '@libp2p/interface/peer-id';
|
|
3
|
+
import type { QueueAddOptions, Options, Queue } from 'p-queue';
|
|
4
|
+
interface RunFunction {
|
|
5
|
+
(): Promise<unknown>;
|
|
6
|
+
}
|
|
7
|
+
export interface PeerPriorityQueueOptions extends QueueAddOptions {
|
|
8
|
+
peerId: PeerId;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Port of https://github.com/sindresorhus/p-queue/blob/main/source/priority-queue.ts
|
|
12
|
+
* that adds support for filtering jobs by peer id
|
|
13
|
+
*/
|
|
14
|
+
declare class PeerPriorityQueue implements Queue<RunFunction, PeerPriorityQueueOptions> {
|
|
15
|
+
#private;
|
|
16
|
+
enqueue(run: RunFunction, options?: Partial<PeerPriorityQueueOptions>): void;
|
|
17
|
+
dequeue(): RunFunction | undefined;
|
|
18
|
+
filter(options: Readonly<Partial<PeerPriorityQueueOptions>>): RunFunction[];
|
|
19
|
+
get size(): number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Extends PQueue to add support for querying queued jobs by peer id
|
|
23
|
+
*/
|
|
24
|
+
export declare class PeerJobQueue extends PQueue<PeerPriorityQueue, PeerPriorityQueueOptions> {
|
|
25
|
+
constructor(options?: Options<PeerPriorityQueue, PeerPriorityQueueOptions>);
|
|
26
|
+
/**
|
|
27
|
+
* Returns true if this queue has a job for the passed peer id that has not yet
|
|
28
|
+
* started to run
|
|
29
|
+
*/
|
|
30
|
+
hasJob(peerId: PeerId): boolean;
|
|
31
|
+
}
|
|
32
|
+
export {};
|
|
33
|
+
//# sourceMappingURL=peer-job-queue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"peer-job-queue.d.ts","sourceRoot":"","sources":["../../src/peer-job-queue.ts"],"names":[],"mappings":"AAGA,OAAO,MAAM,MAAM,SAAS,CAAA;AAC5B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAuB9D,UAAU,WAAW;IAAG,IAAI,OAAO,CAAC,OAAO,CAAC,CAAA;CAAE;AAE9C,MAAM,WAAW,wBAAyB,SAAQ,eAAe;IAC/D,MAAM,EAAE,MAAM,CAAA;CACf;AAQD;;;GAGG;AACH,cAAM,iBAAkB,YAAW,KAAK,CAAC,WAAW,EAAE,wBAAwB,CAAC;;IAG7E,OAAO,CAAE,GAAG,EAAE,WAAW,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,wBAAwB,CAAC,GAAG,IAAI;IA0B7E,OAAO,IAAK,WAAW,GAAG,SAAS;IAKnC,MAAM,CAAE,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,wBAAwB,CAAC,CAAC,GAAG,WAAW,EAAE;IAc5E,IAAI,IAAI,IAAK,MAAM,CAElB;CACF;AAED;;GAEG;AACH,qBAAa,YAAa,SAAQ,MAAM,CAAC,iBAAiB,EAAE,wBAAwB,CAAC;gBACtE,OAAO,GAAE,OAAO,CAAC,iBAAiB,EAAE,wBAAwB,CAAM;IAO/E;;;OAGG;IACH,MAAM,CAAE,MAAM,EAAE,MAAM,GAAG,OAAO;CAKjC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
+
import { CodeError, ERR_INVALID_PARAMETERS } from '@libp2p/interface/errors';
|
|
3
|
+
import PQueue from 'p-queue';
|
|
4
|
+
// Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound
|
|
5
|
+
// Used to compute insertion index to keep queue sorted after insertion
|
|
6
|
+
function lowerBound(array, value, comparator) {
|
|
7
|
+
let first = 0;
|
|
8
|
+
let count = array.length;
|
|
9
|
+
while (count > 0) {
|
|
10
|
+
const step = Math.trunc(count / 2);
|
|
11
|
+
let it = first + step;
|
|
12
|
+
if (comparator(array[it], value) <= 0) {
|
|
13
|
+
first = ++it;
|
|
14
|
+
count -= step + 1;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
count = step;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return first;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Port of https://github.com/sindresorhus/p-queue/blob/main/source/priority-queue.ts
|
|
24
|
+
* that adds support for filtering jobs by peer id
|
|
25
|
+
*/
|
|
26
|
+
class PeerPriorityQueue {
|
|
27
|
+
#queue = [];
|
|
28
|
+
enqueue(run, options) {
|
|
29
|
+
const peerId = options?.peerId;
|
|
30
|
+
const priority = options?.priority ?? 0;
|
|
31
|
+
if (peerId == null) {
|
|
32
|
+
throw new CodeError('missing peer id', ERR_INVALID_PARAMETERS);
|
|
33
|
+
}
|
|
34
|
+
const element = {
|
|
35
|
+
priority,
|
|
36
|
+
peerId,
|
|
37
|
+
run
|
|
38
|
+
};
|
|
39
|
+
if (this.size > 0 && this.#queue[this.size - 1].priority >= priority) {
|
|
40
|
+
this.#queue.push(element);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const index = lowerBound(this.#queue, element, (a, b) => b.priority - a.priority);
|
|
44
|
+
this.#queue.splice(index, 0, element);
|
|
45
|
+
}
|
|
46
|
+
dequeue() {
|
|
47
|
+
const item = this.#queue.shift();
|
|
48
|
+
return item?.run;
|
|
49
|
+
}
|
|
50
|
+
filter(options) {
|
|
51
|
+
if (options.peerId != null) {
|
|
52
|
+
const peerId = options.peerId;
|
|
53
|
+
return this.#queue.filter((element) => peerId.equals(element.peerId)).map((element) => element.run);
|
|
54
|
+
}
|
|
55
|
+
return this.#queue.filter((element) => element.priority === options.priority).map((element) => element.run);
|
|
56
|
+
}
|
|
57
|
+
get size() {
|
|
58
|
+
return this.#queue.length;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Extends PQueue to add support for querying queued jobs by peer id
|
|
63
|
+
*/
|
|
64
|
+
export class PeerJobQueue extends PQueue {
|
|
65
|
+
constructor(options = {}) {
|
|
66
|
+
super({
|
|
67
|
+
...options,
|
|
68
|
+
queueClass: PeerPriorityQueue
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Returns true if this queue has a job for the passed peer id that has not yet
|
|
73
|
+
* started to run
|
|
74
|
+
*/
|
|
75
|
+
hasJob(peerId) {
|
|
76
|
+
return this.sizeBy({
|
|
77
|
+
peerId
|
|
78
|
+
}) > 0;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=peer-job-queue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":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Aa,UAAgE,EAAE;QAC7E,KAAK,CAAC;YACJ,GAAG,OAAO;YACV,UAAU,EAAE,iBAAiB;SAC9B,CAAC,CAAA;IACJ,CAAC;IAED;;;OAGG;IACH,MAAM,CAAE,MAAc;QACpB,OAAO,IAAI,CAAC,MAAM,CAAC;YACjB,MAAM;SACP,CAAC,GAAG,CAAC,CAAA;IACR,CAAC;CACF"}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import type { ComponentLogger } from '@libp2p/interface';
|
|
1
2
|
import type { MultiaddrConnection, Stream } from '@libp2p/interface/connection';
|
|
2
3
|
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
3
4
|
export interface StreamProperties {
|
|
4
5
|
stream: Stream;
|
|
5
6
|
remoteAddr: Multiaddr;
|
|
6
7
|
localAddr: Multiaddr;
|
|
8
|
+
logger: ComponentLogger;
|
|
7
9
|
}
|
|
8
10
|
/**
|
|
9
11
|
* Convert a duplex iterable into a MultiaddrConnection.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-to-ma-conn.d.ts","sourceRoot":"","sources":["../../src/stream-to-ma-conn.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stream-to-ma-conn.d.ts","sourceRoot":"","sources":["../../src/stream-to-ma-conn.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAgB,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACtE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,EAAE,MAAM,8BAA8B,CAAA;AAC/E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAExD,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,SAAS,CAAA;IACrB,SAAS,EAAE,SAAS,CAAA;IACpB,MAAM,EAAE,eAAe,CAAA;CACxB;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAE,KAAK,EAAE,gBAAgB,GAAG,mBAAmB,CAkDlF"}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger';
|
|
2
|
-
const log = logger('libp2p:stream:converter');
|
|
3
1
|
/**
|
|
4
2
|
* Convert a duplex iterable into a MultiaddrConnection.
|
|
5
3
|
* https://github.com/libp2p/interface-transport#multiaddrconnection
|
|
6
4
|
*/
|
|
7
5
|
export function streamToMaConnection(props) {
|
|
8
|
-
const { stream, remoteAddr } = props;
|
|
6
|
+
const { stream, remoteAddr, logger } = props;
|
|
9
7
|
const { sink, source } = stream;
|
|
8
|
+
const log = logger.forComponent('libp2p:stream:converter');
|
|
10
9
|
const mapSource = (async function* () {
|
|
11
10
|
for await (const list of source) {
|
|
12
11
|
if (list instanceof Uint8Array) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-to-ma-conn.js","sourceRoot":"","sources":["../../src/stream-to-ma-conn.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stream-to-ma-conn.js","sourceRoot":"","sources":["../../src/stream-to-ma-conn.ts"],"names":[],"mappings":"AAWA;;;GAGG;AACH,MAAM,UAAU,oBAAoB,CAAE,KAAuB;IAC3D,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,KAAK,CAAA;IAC5C,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAAA;IAC/B,MAAM,GAAG,GAAG,MAAM,CAAC,YAAY,CAAC,yBAAyB,CAAC,CAAA;IAE1D,MAAM,SAAS,GAAG,CAAC,KAAK,SAAU,CAAC;QACjC,IAAI,KAAK,EAAE,MAAM,IAAI,IAAI,MAAM,EAAE;YAC/B,IAAI,IAAI,YAAY,UAAU,EAAE;gBAC9B,MAAM,IAAI,CAAA;aACX;iBAAM;gBACL,KAAM,CAAC,CAAC,IAAI,CAAA;aACb;SACF;IACH,CAAC,EAAE,CAAC,CAAA;IAEJ,MAAM,MAAM,GAAwB;QAClC,KAAK,CAAC,IAAI,CAAE,MAAM;YAChB,IAAI;gBACF,MAAM,IAAI,CAAC,MAAM,CAAC,CAAA;gBAClB,KAAK,EAAE,CAAA;aACR;YAAC,OAAO,GAAQ,EAAE;gBACjB,kCAAkC;gBAClC,IAAI,GAAG,CAAC,IAAI,KAAK,SAAS,EAAE;oBAC1B,uEAAuE;oBACvE,gEAAgE;oBAChE,uEAAuE;oBACvE,GAAG,CAAC,GAAG,CAAC,CAAA;iBACT;aACF;QACH,CAAC;QACD,MAAM,EAAE,SAAS;QACjB,UAAU;QACV,QAAQ,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,SAAS,EAAE;QAChD,KAAK,CAAC,KAAK,CAAE,OAAsB;YACjC,KAAK,EAAE,CAAA;YACP,MAAM,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QAC7B,CAAC;QACD,KAAK,CAAE,GAAU;YACf,KAAK,EAAE,CAAA;YACP,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACnB,CAAC;KACF,CAAA;IAED,SAAS,KAAK;QACZ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,IAAI,IAAI,EAAE;YACjC,MAAM,CAAC,QAAQ,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;SACnC;IACH,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/utils",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.7-97ab31c0c",
|
|
4
4
|
"description": "Package to aggregate shared logic and dependencies for the libp2p ecosystem",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/utils#readme",
|
|
@@ -60,6 +60,10 @@
|
|
|
60
60
|
"types": "./dist/src/multiaddr/is-private.d.ts",
|
|
61
61
|
"import": "./dist/src/multiaddr/is-private.js"
|
|
62
62
|
},
|
|
63
|
+
"./peer-job-queue": {
|
|
64
|
+
"types": "./dist/src/peer-job-queue.d.ts",
|
|
65
|
+
"import": "./dist/src/peer-job-queue.js"
|
|
66
|
+
},
|
|
63
67
|
"./stream-to-ma-conn": {
|
|
64
68
|
"types": "./dist/src/stream-to-ma-conn.d.ts",
|
|
65
69
|
"import": "./dist/src/stream-to-ma-conn.js"
|
|
@@ -87,20 +91,23 @@
|
|
|
87
91
|
},
|
|
88
92
|
"dependencies": {
|
|
89
93
|
"@chainsafe/is-ip": "^2.0.2",
|
|
90
|
-
"@libp2p/interface": "
|
|
91
|
-
"@
|
|
92
|
-
"@multiformats/multiaddr": "^
|
|
93
|
-
"@multiformats/multiaddr-matcher": "^1.0.1",
|
|
94
|
+
"@libp2p/interface": "0.1.6-97ab31c0c",
|
|
95
|
+
"@multiformats/multiaddr": "^12.1.10",
|
|
96
|
+
"@multiformats/multiaddr-matcher": "^1.1.0",
|
|
94
97
|
"is-loopback-addr": "^2.0.1",
|
|
95
98
|
"it-stream-types": "^2.0.1",
|
|
96
|
-
"
|
|
99
|
+
"p-queue": "^7.4.1",
|
|
100
|
+
"private-ip": "^3.0.1",
|
|
97
101
|
"uint8arraylist": "^2.4.3"
|
|
98
102
|
},
|
|
99
103
|
"devDependencies": {
|
|
104
|
+
"@libp2p/logger": "3.1.0-97ab31c0c",
|
|
105
|
+
"@libp2p/peer-id-factory": "3.0.8-97ab31c0c",
|
|
100
106
|
"aegir": "^41.0.2",
|
|
101
|
-
"it-all": "^3.0.
|
|
107
|
+
"it-all": "^3.0.3",
|
|
102
108
|
"it-pair": "^2.0.6",
|
|
103
109
|
"it-pipe": "^3.0.1",
|
|
104
|
-
"
|
|
110
|
+
"p-defer": "^4.0.0",
|
|
111
|
+
"uint8arrays": "^4.0.6"
|
|
105
112
|
}
|
|
106
113
|
}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { isIPv4, isIPv6 } from '@chainsafe/is-ip'
|
|
2
2
|
import { CodeError } from '@libp2p/interface/errors'
|
|
3
|
-
import { logger } from '@libp2p/logger'
|
|
4
3
|
import { type Multiaddr, multiaddr } from '@multiformats/multiaddr'
|
|
5
4
|
|
|
6
|
-
const log = logger('libp2p:ip-port-to-multiaddr')
|
|
7
|
-
|
|
8
5
|
export const Errors = {
|
|
9
6
|
ERR_INVALID_IP_PARAMETER: 'ERR_INVALID_IP_PARAMETER',
|
|
10
7
|
ERR_INVALID_PORT_PARAMETER: 'ERR_INVALID_PORT_PARAMETER',
|
|
@@ -35,7 +32,5 @@ export function ipPortToMultiaddr (ip: string, port: number | string): Multiaddr
|
|
|
35
32
|
return multiaddr(`/ip6/${ip}/tcp/${port}`)
|
|
36
33
|
}
|
|
37
34
|
|
|
38
|
-
|
|
39
|
-
log.error(errMsg)
|
|
40
|
-
throw new CodeError(errMsg, Errors.ERR_INVALID_IP)
|
|
35
|
+
throw new CodeError(`invalid ip:port for creating a multiaddr: ${ip}:${port}`, Errors.ERR_INVALID_IP)
|
|
41
36
|
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
+
|
|
3
|
+
import { CodeError, ERR_INVALID_PARAMETERS } from '@libp2p/interface/errors'
|
|
4
|
+
import PQueue from 'p-queue'
|
|
5
|
+
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
6
|
+
import type { QueueAddOptions, Options, Queue } from 'p-queue'
|
|
7
|
+
|
|
8
|
+
// Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound
|
|
9
|
+
// Used to compute insertion index to keep queue sorted after insertion
|
|
10
|
+
function lowerBound<T> (array: readonly T[], value: T, comparator: (a: T, b: T) => number): number {
|
|
11
|
+
let first = 0
|
|
12
|
+
let count = array.length
|
|
13
|
+
|
|
14
|
+
while (count > 0) {
|
|
15
|
+
const step = Math.trunc(count / 2)
|
|
16
|
+
let it = first + step
|
|
17
|
+
|
|
18
|
+
if (comparator(array[it]!, value) <= 0) {
|
|
19
|
+
first = ++it
|
|
20
|
+
count -= step + 1
|
|
21
|
+
} else {
|
|
22
|
+
count = step
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return first
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface RunFunction { (): Promise<unknown> }
|
|
30
|
+
|
|
31
|
+
export interface PeerPriorityQueueOptions extends QueueAddOptions {
|
|
32
|
+
peerId: PeerId
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface PeerJob {
|
|
36
|
+
priority: number
|
|
37
|
+
peerId: PeerId
|
|
38
|
+
run: RunFunction
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Port of https://github.com/sindresorhus/p-queue/blob/main/source/priority-queue.ts
|
|
43
|
+
* that adds support for filtering jobs by peer id
|
|
44
|
+
*/
|
|
45
|
+
class PeerPriorityQueue implements Queue<RunFunction, PeerPriorityQueueOptions> {
|
|
46
|
+
readonly #queue: PeerJob[] = []
|
|
47
|
+
|
|
48
|
+
enqueue (run: RunFunction, options?: Partial<PeerPriorityQueueOptions>): void {
|
|
49
|
+
const peerId = options?.peerId
|
|
50
|
+
const priority = options?.priority ?? 0
|
|
51
|
+
|
|
52
|
+
if (peerId == null) {
|
|
53
|
+
throw new CodeError('missing peer id', ERR_INVALID_PARAMETERS)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const element: PeerJob = {
|
|
57
|
+
priority,
|
|
58
|
+
peerId,
|
|
59
|
+
run
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (this.size > 0 && this.#queue[this.size - 1]!.priority >= priority) {
|
|
63
|
+
this.#queue.push(element)
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const index = lowerBound(
|
|
68
|
+
this.#queue, element,
|
|
69
|
+
(a: Readonly<PeerPriorityQueueOptions>, b: Readonly<PeerPriorityQueueOptions>) => b.priority! - a.priority!
|
|
70
|
+
)
|
|
71
|
+
this.#queue.splice(index, 0, element)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
dequeue (): RunFunction | undefined {
|
|
75
|
+
const item = this.#queue.shift()
|
|
76
|
+
return item?.run
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
filter (options: Readonly<Partial<PeerPriorityQueueOptions>>): RunFunction[] {
|
|
80
|
+
if (options.peerId != null) {
|
|
81
|
+
const peerId = options.peerId
|
|
82
|
+
|
|
83
|
+
return this.#queue.filter(
|
|
84
|
+
(element: Readonly<PeerPriorityQueueOptions>) => peerId.equals(element.peerId)
|
|
85
|
+
).map((element: Readonly<{ run: RunFunction }>) => element.run)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return this.#queue.filter(
|
|
89
|
+
(element: Readonly<PeerPriorityQueueOptions>) => element.priority === options.priority
|
|
90
|
+
).map((element: Readonly<{ run: RunFunction }>) => element.run)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
get size (): number {
|
|
94
|
+
return this.#queue.length
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Extends PQueue to add support for querying queued jobs by peer id
|
|
100
|
+
*/
|
|
101
|
+
export class PeerJobQueue extends PQueue<PeerPriorityQueue, PeerPriorityQueueOptions> {
|
|
102
|
+
constructor (options: Options<PeerPriorityQueue, PeerPriorityQueueOptions> = {}) {
|
|
103
|
+
super({
|
|
104
|
+
...options,
|
|
105
|
+
queueClass: PeerPriorityQueue
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Returns true if this queue has a job for the passed peer id that has not yet
|
|
111
|
+
* started to run
|
|
112
|
+
*/
|
|
113
|
+
hasJob (peerId: PeerId): boolean {
|
|
114
|
+
return this.sizeBy({
|
|
115
|
+
peerId
|
|
116
|
+
}) > 0
|
|
117
|
+
}
|
|
118
|
+
}
|
package/src/stream-to-ma-conn.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { AbortOptions } from '@libp2p/interface'
|
|
1
|
+
import type { AbortOptions, ComponentLogger } from '@libp2p/interface'
|
|
3
2
|
import type { MultiaddrConnection, Stream } from '@libp2p/interface/connection'
|
|
4
3
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
5
4
|
|
|
6
|
-
const log = logger('libp2p:stream:converter')
|
|
7
|
-
|
|
8
5
|
export interface StreamProperties {
|
|
9
6
|
stream: Stream
|
|
10
7
|
remoteAddr: Multiaddr
|
|
11
8
|
localAddr: Multiaddr
|
|
9
|
+
logger: ComponentLogger
|
|
12
10
|
}
|
|
13
11
|
|
|
14
12
|
/**
|
|
@@ -16,8 +14,9 @@ export interface StreamProperties {
|
|
|
16
14
|
* https://github.com/libp2p/interface-transport#multiaddrconnection
|
|
17
15
|
*/
|
|
18
16
|
export function streamToMaConnection (props: StreamProperties): MultiaddrConnection {
|
|
19
|
-
const { stream, remoteAddr } = props
|
|
17
|
+
const { stream, remoteAddr, logger } = props
|
|
20
18
|
const { sink, source } = stream
|
|
19
|
+
const log = logger.forComponent('libp2p:stream:converter')
|
|
21
20
|
|
|
22
21
|
const mapSource = (async function * () {
|
|
23
22
|
for await (const list of source) {
|
package/dist/typedoc-urls.json
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"certifiedAddressesFirst": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.certifiedAddressesFirst.html",
|
|
3
|
-
"./address-sort:certifiedAddressesFirst": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.certifiedAddressesFirst.html",
|
|
4
|
-
"circuitRelayAddressesLast": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.circuitRelayAddressesLast.html",
|
|
5
|
-
"./address-sort:circuitRelayAddressesLast": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.circuitRelayAddressesLast.html",
|
|
6
|
-
"defaultAddressSort": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.defaultAddressSort.html",
|
|
7
|
-
"./address-sort:defaultAddressSort": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.defaultAddressSort.html",
|
|
8
|
-
"publicAddressesFirst": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.publicAddressesFirst.html",
|
|
9
|
-
"./address-sort:publicAddressesFirst": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.publicAddressesFirst.html",
|
|
10
|
-
"arrayEquals": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.array_equals.arrayEquals.html",
|
|
11
|
-
"./array-equals:arrayEquals": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.array_equals.arrayEquals.html",
|
|
12
|
-
"Errors": "https://libp2p.github.io/js-libp2p/variables/_libp2p_utils.ip_port_to_multiaddr.Errors.html",
|
|
13
|
-
"./ip-port-to-multiaddr:Errors": "https://libp2p.github.io/js-libp2p/variables/_libp2p_utils.ip_port_to_multiaddr.Errors.html",
|
|
14
|
-
"ipPortToMultiaddr": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.ip_port_to_multiaddr.ipPortToMultiaddr.html",
|
|
15
|
-
"./ip-port-to-multiaddr:ipPortToMultiaddr": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.ip_port_to_multiaddr.ipPortToMultiaddr.html",
|
|
16
|
-
"isLoopback": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.multiaddr_is_loopback.isLoopback.html",
|
|
17
|
-
"./multiaddr/is-loopback:isLoopback": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.multiaddr_is_loopback.isLoopback.html",
|
|
18
|
-
"isPrivate": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.multiaddr_is_private.isPrivate.html",
|
|
19
|
-
"./multiaddr/is-private:isPrivate": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.multiaddr_is_private.isPrivate.html",
|
|
20
|
-
"StreamProperties": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.stream_to_ma_conn.StreamProperties.html",
|
|
21
|
-
"./stream-to-ma-conn:StreamProperties": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.stream_to_ma_conn.StreamProperties.html",
|
|
22
|
-
"streamToMaConnection": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.stream_to_ma_conn.streamToMaConnection.html",
|
|
23
|
-
"./stream-to-ma-conn:streamToMaConnection": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.stream_to_ma_conn.streamToMaConnection.html"
|
|
24
|
-
}
|