@libp2p/utils 4.0.2 → 4.0.3

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.
@@ -21,13 +21,19 @@
21
21
  */
22
22
  import type { Address } from '@libp2p/interface/peer-store';
23
23
  /**
24
- * Compare function for array.sort().
25
- * This sort aims to move the private addresses to the end of the array.
26
- * In case of equality, a certified address will come first.
24
+ * Compare function for array.sort() that moves public addresses to the start
25
+ * of the array.
27
26
  */
28
27
  export declare function publicAddressesFirst(a: Address, b: Address): -1 | 0 | 1;
29
28
  /**
30
- * A test thing
29
+ * Compare function for array.sort() that moves certified addresses to the start
30
+ * of the array.
31
31
  */
32
- export declare function something(): Promise<Uint8Array>;
32
+ export declare function certifiedAddressesFirst(a: Address, b: Address): -1 | 0 | 1;
33
+ /**
34
+ * Compare function for array.sort() that moves circuit relay addresses to the
35
+ * start of the array.
36
+ */
37
+ export declare function circuitRelayAddressesLast(a: Address, b: Address): -1 | 0 | 1;
38
+ export declare function defaultAddressSort(a: Address, b: Address): -1 | 0 | 1;
33
39
  //# sourceMappingURL=address-sort.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"address-sort.d.ts","sourceRoot":"","sources":["../../src/address-sort.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAGH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAA;AAE3D;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAiBxE;AAED;;GAEG;AACH,wBAAsB,SAAS,IAAK,OAAO,CAAC,UAAU,CAAC,CAEtD"}
1
+ {"version":3,"file":"address-sort.d.ts","sourceRoot":"","sources":["../../src/address-sort.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAIH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAA;AAE3D;;;GAGG;AACH,wBAAgB,oBAAoB,CAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAWxE;AAED;;;GAGG;AACH,wBAAgB,uBAAuB,CAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAQ3E;AAED;;;GAGG;AACH,wBAAgB,yBAAyB,CAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAW7E;AAED,wBAAgB,kBAAkB,CAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAgBtE"}
@@ -19,11 +19,11 @@
19
19
  * // ['/ip4/82.41.53.1/tcp/9000', '/ip4/127.0.0.1/tcp/9000']
20
20
  * ```
21
21
  */
22
+ import { Circuit } from '@multiformats/multiaddr-matcher';
22
23
  import { isPrivate } from './multiaddr/is-private.js';
23
24
  /**
24
- * Compare function for array.sort().
25
- * This sort aims to move the private addresses to the end of the array.
26
- * In case of equality, a certified address will come first.
25
+ * Compare function for array.sort() that moves public addresses to the start
26
+ * of the array.
27
27
  */
28
28
  export function publicAddressesFirst(a, b) {
29
29
  const isAPrivate = isPrivate(a.multiaddr);
@@ -34,7 +34,13 @@ export function publicAddressesFirst(a, b) {
34
34
  else if (!isAPrivate && isBPrivate) {
35
35
  return -1;
36
36
  }
37
- // Check certified?
37
+ return 0;
38
+ }
39
+ /**
40
+ * Compare function for array.sort() that moves certified addresses to the start
41
+ * of the array.
42
+ */
43
+ export function certifiedAddressesFirst(a, b) {
38
44
  if (a.isCertified && !b.isCertified) {
39
45
  return -1;
40
46
  }
@@ -44,9 +50,30 @@ export function publicAddressesFirst(a, b) {
44
50
  return 0;
45
51
  }
46
52
  /**
47
- * A test thing
53
+ * Compare function for array.sort() that moves circuit relay addresses to the
54
+ * start of the array.
48
55
  */
49
- export async function something() {
50
- return Uint8Array.from([0, 1, 2]);
56
+ export function circuitRelayAddressesLast(a, b) {
57
+ const isACircuit = Circuit.exactMatch(a.multiaddr);
58
+ const isBCircuit = Circuit.exactMatch(b.multiaddr);
59
+ if (isACircuit && !isBCircuit) {
60
+ return 1;
61
+ }
62
+ else if (!isACircuit && isBCircuit) {
63
+ return -1;
64
+ }
65
+ return 0;
66
+ }
67
+ export function defaultAddressSort(a, b) {
68
+ const publicResult = publicAddressesFirst(a, b);
69
+ if (publicResult !== 0) {
70
+ return publicResult;
71
+ }
72
+ const relayResult = circuitRelayAddressesLast(a, b);
73
+ if (relayResult !== 0) {
74
+ return relayResult;
75
+ }
76
+ const certifiedResult = certifiedAddressesFirst(a, b);
77
+ return certifiedResult;
51
78
  }
52
79
  //# sourceMappingURL=address-sort.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"address-sort.js","sourceRoot":"","sources":["../../src/address-sort.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AAGrD;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAAE,CAAU,EAAE,CAAU;IAC1D,MAAM,UAAU,GAAG,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;IACzC,MAAM,UAAU,GAAG,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;IAEzC,IAAI,UAAU,IAAI,CAAC,UAAU,EAAE;QAC7B,OAAO,CAAC,CAAA;KACT;SAAM,IAAI,CAAC,UAAU,IAAI,UAAU,EAAE;QACpC,OAAO,CAAC,CAAC,CAAA;KACV;IACD,mBAAmB;IACnB,IAAI,CAAC,CAAC,WAAW,IAAI,CAAC,CAAC,CAAC,WAAW,EAAE;QACnC,OAAO,CAAC,CAAC,CAAA;KACV;SAAM,IAAI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UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;IAClD,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;IAElD,IAAI,UAAU,IAAI,CAAC,UAAU,EAAE;QAC7B,OAAO,CAAC,CAAA;KACT;SAAM,IAAI,CAAC,UAAU,IAAI,UAAU,EAAE;QACpC,OAAO,CAAC,CAAC,CAAA;KACV;IAED,OAAO,CAAC,CAAA;AACV,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAE,CAAU,EAAE,CAAU;IACxD,MAAM,YAAY,GAAG,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IAE/C,IAAI,YAAY,KAAK,CAAC,EAAE;QACtB,OAAO,YAAY,CAAA;KACpB;IAED,MAAM,WAAW,GAAG,yBAAyB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IAEnD,IAAI,WAAW,KAAK,CAAC,EAAE;QACrB,OAAO,WAAW,CAAA;KACnB;IAED,MAAM,eAAe,GAAG,uBAAuB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IAErD,OAAO,eAAe,CAAA;AACxB,CAAC"}
@@ -1,11 +1,24 @@
1
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",
2
8
  "publicAddressesFirst": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.publicAddressesFirst.html",
3
- "something": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.something.html",
9
+ "./address-sort:publicAddressesFirst": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.address_sort.publicAddressesFirst.html",
4
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",
5
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",
6
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",
7
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",
8
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",
9
20
  "StreamProperties": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.stream_to_ma_conn.StreamProperties.html",
10
- "streamToMaConnection": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.stream_to_ma_conn.streamToMaConnection.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"
11
24
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/utils",
3
- "version": "4.0.2",
3
+ "version": "4.0.3",
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",
@@ -89,6 +89,7 @@
89
89
  "@libp2p/interface": "^0.1.2",
90
90
  "@libp2p/logger": "^3.0.2",
91
91
  "@multiformats/multiaddr": "^12.1.5",
92
+ "@multiformats/multiaddr-matcher": "^1.0.1",
92
93
  "is-loopback-addr": "^2.0.1",
93
94
  "it-stream-types": "^2.0.1",
94
95
  "private-ip": "^3.0.0",
@@ -20,13 +20,13 @@
20
20
  * ```
21
21
  */
22
22
 
23
+ import { Circuit } from '@multiformats/multiaddr-matcher'
23
24
  import { isPrivate } from './multiaddr/is-private.js'
24
25
  import type { Address } from '@libp2p/interface/peer-store'
25
26
 
26
27
  /**
27
- * Compare function for array.sort().
28
- * This sort aims to move the private addresses to the end of the array.
29
- * In case of equality, a certified address will come first.
28
+ * Compare function for array.sort() that moves public addresses to the start
29
+ * of the array.
30
30
  */
31
31
  export function publicAddressesFirst (a: Address, b: Address): -1 | 0 | 1 {
32
32
  const isAPrivate = isPrivate(a.multiaddr)
@@ -37,7 +37,15 @@ export function publicAddressesFirst (a: Address, b: Address): -1 | 0 | 1 {
37
37
  } else if (!isAPrivate && isBPrivate) {
38
38
  return -1
39
39
  }
40
- // Check certified?
40
+
41
+ return 0
42
+ }
43
+
44
+ /**
45
+ * Compare function for array.sort() that moves certified addresses to the start
46
+ * of the array.
47
+ */
48
+ export function certifiedAddressesFirst (a: Address, b: Address): -1 | 0 | 1 {
41
49
  if (a.isCertified && !b.isCertified) {
42
50
  return -1
43
51
  } else if (!a.isCertified && b.isCertified) {
@@ -48,8 +56,36 @@ export function publicAddressesFirst (a: Address, b: Address): -1 | 0 | 1 {
48
56
  }
49
57
 
50
58
  /**
51
- * A test thing
59
+ * Compare function for array.sort() that moves circuit relay addresses to the
60
+ * start of the array.
52
61
  */
53
- export async function something (): Promise<Uint8Array> {
54
- return Uint8Array.from([0, 1, 2])
62
+ export function circuitRelayAddressesLast (a: Address, b: Address): -1 | 0 | 1 {
63
+ const isACircuit = Circuit.exactMatch(a.multiaddr)
64
+ const isBCircuit = Circuit.exactMatch(b.multiaddr)
65
+
66
+ if (isACircuit && !isBCircuit) {
67
+ return 1
68
+ } else if (!isACircuit && isBCircuit) {
69
+ return -1
70
+ }
71
+
72
+ return 0
73
+ }
74
+
75
+ export function defaultAddressSort (a: Address, b: Address): -1 | 0 | 1 {
76
+ const publicResult = publicAddressesFirst(a, b)
77
+
78
+ if (publicResult !== 0) {
79
+ return publicResult
80
+ }
81
+
82
+ const relayResult = circuitRelayAddressesLast(a, b)
83
+
84
+ if (relayResult !== 0) {
85
+ return relayResult
86
+ }
87
+
88
+ const certifiedResult = certifiedAddressesFirst(a, b)
89
+
90
+ return certifiedResult
55
91
  }