@helia/routers 1.1.1 → 2.0.0-1b2934b

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.
@@ -1 +1 @@
1
- {"version":3,"file":"delegated-http-routing.d.ts","sourceRoot":"","sources":["../../src/delegated-http-routing.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAY,OAAO,EAAkB,MAAM,kBAAkB,CAAA;AAkFzE;;GAEG;AACH,wBAAgB,oBAAoB,CAAE,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,CAEhE"}
1
+ {"version":3,"file":"delegated-http-routing.d.ts","sourceRoot":"","sources":["../../src/delegated-http-routing.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAY,OAAO,EAAkB,MAAM,kBAAkB,CAAA;AAoFzE;;GAEG;AACH,wBAAgB,oBAAoB,CAAE,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,CAEhE"}
@@ -1,8 +1,9 @@
1
1
  import { createDelegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client';
2
- import { CodeError } from '@libp2p/interface';
3
- import { marshal, unmarshal, peerIdFromRoutingKey } from 'ipns';
2
+ import { NotFoundError } from '@libp2p/interface';
3
+ import { marshalIPNSRecord, multihashFromIPNSRoutingKey, unmarshalIPNSRecord } from 'ipns';
4
4
  import first from 'it-first';
5
5
  import map from 'it-map';
6
+ import { CID } from 'multiformats/cid';
6
7
  import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
7
8
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
8
9
  const IPNS_PREFIX = uint8ArrayFromString('/ipns/');
@@ -30,24 +31,26 @@ class DelegatedHTTPRouter {
30
31
  if (!isIPNSKey(key)) {
31
32
  return;
32
33
  }
33
- const peerId = peerIdFromRoutingKey(key);
34
- const record = unmarshal(value);
35
- await this.client.putIPNS(peerId, record, options);
34
+ const digest = multihashFromIPNSRoutingKey(key);
35
+ const cid = CID.createV1(0x72, digest);
36
+ const record = unmarshalIPNSRecord(value);
37
+ await this.client.putIPNS(cid, record, options);
36
38
  }
37
39
  async get(key, options) {
38
40
  if (!isIPNSKey(key)) {
39
- throw new CodeError('Not found', 'ERR_NOT_FOUND');
41
+ throw new NotFoundError('Not found');
40
42
  }
41
- const peerId = peerIdFromRoutingKey(key);
43
+ const digest = multihashFromIPNSRoutingKey(key);
44
+ const cid = CID.createV1(0x72, digest);
42
45
  try {
43
- const record = await this.client.getIPNS(peerId, options);
44
- return marshal(record);
46
+ const record = await this.client.getIPNS(cid, options);
47
+ return marshalIPNSRecord(record);
45
48
  }
46
49
  catch (err) {
47
- // ERR_BAD_RESPONSE is thrown when the response had no body, which means
50
+ // BadResponseError is thrown when the response had no body, which means
48
51
  // the record couldn't be found
49
- if (err.code === 'ERR_BAD_RESPONSE') {
50
- throw new CodeError('Not found', 'ERR_NOT_FOUND');
52
+ if (err.name === 'BadResponseError') {
53
+ throw new NotFoundError('Not found');
51
54
  }
52
55
  throw err;
53
56
  }
@@ -60,7 +63,7 @@ class DelegatedHTTPRouter {
60
63
  multiaddrs: peer.Addrs ?? []
61
64
  };
62
65
  }
63
- throw new CodeError('Not found', 'ERR_NOT_FOUND');
66
+ throw new NotFoundError('Not found');
64
67
  }
65
68
  async *getClosestPeers(key, options) {
66
69
  // noop
@@ -1 +1 @@
1
- {"version":3,"file":"delegated-http-routing.js","sourceRoot":"","sources":["../../src/delegated-http-routing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qCAAqC,EAAE,MAAM,6CAA6C,CAAA;AACnG,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC7C,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,oBAAoB,EAAE,MAAM,MAAM,CAAA;AAC/D,OAAO,KAAK,MAAM,UAAU,CAAA;AAC5B,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAA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;QAClE,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA;QAE/D,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC;YACjB,OAAO;gBACL,EAAE,EAAE,IAAI,CAAC,EAAE;gBACX,UAAU,EAAE,IAAI,CAAC,KAAK,IAAI,EAAE;aAC7B,CAAA;QACH,CAAC;QAED,MAAM,IAAI,aAAa,CAAC,WAAW,CAAC,CAAA;IACtC,CAAC;IAED,KAAK,CAAC,CAAE,eAAe,CAAE,GAAe,EAAE,OAAoC;QAC5E,OAAO;IACT,CAAC;CACF;AAED;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAAE,GAAiB;IACrD,OAAO,IAAI,mBAAmB,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,CAAA;AAC9C,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"http-gateway-routing.d.ts","sourceRoot":"","sources":["../../src/http-gateway-routing.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAY,OAAO,EAAkB,MAAM,kBAAkB,CAAA;AAIzE,eAAO,MAAM,0BAA0B,UAMtC,CAAA;AAED,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,EAAE,KAAK,CAAC,GAAG,GAAG,MAAM,CAAC,CAAA;CAC/B;AA2ED;;GAEG;AACH,wBAAgB,kBAAkB,CAAE,IAAI,GAAE,oBAAyB,GAAG,OAAO,CAAC,OAAO,CAAC,CAErF"}
1
+ {"version":3,"file":"http-gateway-routing.d.ts","sourceRoot":"","sources":["../../src/http-gateway-routing.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAY,OAAO,EAAkB,MAAM,kBAAkB,CAAA;AAIzE,eAAO,MAAM,0BAA0B,UAMtC,CAAA;AAED,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,EAAE,KAAK,CAAC,GAAG,GAAG,MAAM,CAAC,CAAA;CAC/B;AA+BD;;GAEG;AACH,wBAAgB,kBAAkB,CAAE,IAAI,GAAE,oBAAyB,GAAG,OAAO,CAAC,OAAO,CAAC,CAErF"}
@@ -1,55 +1,20 @@
1
- import { peerIdSymbol } from '@libp2p/interface';
1
+ import { peerIdFromCID } from '@libp2p/peer-id';
2
2
  import { uriToMultiaddr } from '@multiformats/uri-to-multiaddr';
3
3
  import { CID } from 'multiformats/cid';
4
4
  import { identity } from 'multiformats/hashes/identity';
5
5
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
6
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
7
6
  export const DEFAULT_TRUSTLESS_GATEWAYS = [
8
7
  // 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/
9
8
  'https://trustless-gateway.link',
10
9
  // 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/
11
10
  'https://4everland.io'
12
11
  ];
13
- // these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
12
+ // this value is from https://github.com/multiformats/multicodec/blob/master/table.csv
14
13
  const TRANSPORT_IPFS_GATEWAY_HTTP_CODE = 0x0920;
15
- const inspect = Symbol.for('nodejs.util.inspect.custom');
16
- class URLPeerId {
17
- type = 'url';
18
- multihash;
19
- privateKey;
20
- publicKey;
21
- url;
22
- constructor(url) {
23
- this.url = url.toString();
24
- this.multihash = identity.digest(uint8ArrayFromString(this.url));
25
- }
26
- [inspect]() {
27
- return `PeerId(${this.url})`;
28
- }
29
- [peerIdSymbol] = true;
30
- toString() {
31
- return this.toCID().toString();
32
- }
33
- toCID() {
34
- return CID.createV1(TRANSPORT_IPFS_GATEWAY_HTTP_CODE, this.multihash);
35
- }
36
- toBytes() {
37
- return this.toCID().bytes;
38
- }
39
- equals(other) {
40
- if (other == null) {
41
- return false;
42
- }
43
- if (other instanceof Uint8Array) {
44
- other = uint8ArrayToString(other);
45
- }
46
- return other.toString() === this.toString();
47
- }
48
- }
49
14
  function toPeerInfo(url) {
50
15
  url = url.toString();
51
16
  return {
52
- id: new URLPeerId(new URL(url)),
17
+ id: peerIdFromCID(CID.createV1(TRANSPORT_IPFS_GATEWAY_HTTP_CODE, identity.digest(uint8ArrayFromString(url)))),
53
18
  multiaddrs: [
54
19
  uriToMultiaddr(url)
55
20
  ]
@@ -1 +1 @@
1
- {"version":3,"file":"http-gateway-routing.js","sourceRoot":"","sources":["../../src/http-gateway-routing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAA;AAC/D,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AACvD,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAKtE,MAAM,CAAC,MAAM,0BAA0B,GAAG;IACxC,wGAAwG;IACxG,gCAAgC;IAEhC,wGAAwG;IACxG,sBAAsB;CACvB,CAAA;AAMD,yFAAyF;AACzF,MAAM,gCAAgC,GAAG,MAAM,CAAA;AAC/C,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAA;AAExD,MAAM,SAAS;IACJ,IAAI,GAAG,KAAK,CAAA;IACZ,SAAS,CAAiB;IAC1B,UAAU,CAAa;IACvB,SAAS,CAAa;IACtB,GAAG,CAAQ;IAEpB,YAAa,GAAQ;QACnB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC,QAAQ,EAAE,CAAA;QACzB,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,oBAAoB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAA;IAClE,CAAC;IAED,CAAC,OAAO,CAAC;QACP,O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M,gBAAgB;IACH,QAAQ,CAAY;IAErC,YAAa,OAA6B,EAAE;QAC1C,IAAI,CAAC,QAAQ,GAAG,CAAC,IAAI,CAAC,QAAQ,IAAI,0BAA0B,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAA;IAC3F,CAAC;IAED,KAAK,CAAC,CAAE,aAAa,CAAE,GAA0C,EAAE,OAAoC;QACrG,KAAM,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YAC9E,GAAG,IAAI;YACP,SAAS,EAAE,CAAC,6BAA6B,CAAC;SAC3C,CAAC,CAAC,CAAA;IACL,CAAC;CACF;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAE,OAA6B,EAAE;IACjE,OAAO,IAAI,gBAAgB,CAAC,IAAI,CAAC,CAAA;AACnC,CAAC"}
@@ -5,5 +5,6 @@
5
5
  */
6
6
  export { delegatedHTTPRouting } from './delegated-http-routing.js';
7
7
  export { httpGatewayRouting } from './http-gateway-routing.js';
8
+ export type { HTTPGatwayRouterInit } from './http-gateway-routing.js';
8
9
  export { libp2pRouting } from './libp2p-routing.js';
9
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAA;AAClE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AAC9D,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAA;AAClE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AAC9D,YAAY,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAA;AAClE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AAC9D,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAA;AAClE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AAE9D,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@helia/routers",
3
- "version": "1.1.1",
3
+ "version": "2.0.0-1b2934b",
4
4
  "description": "Routers for Helia",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/ipfs/helia/tree/main/packages/routers#readme",
@@ -43,6 +43,7 @@
43
43
  "clean": "aegir clean",
44
44
  "lint": "aegir lint",
45
45
  "dep-check": "aegir dep-check",
46
+ "doc-check": "aegir doc-check",
46
47
  "build": "aegir build",
47
48
  "test": "aegir test",
48
49
  "test:chrome": "aegir test -t browser --cov",
@@ -53,19 +54,19 @@
53
54
  "test:electron-main": "aegir test -t electron-main"
54
55
  },
55
56
  "dependencies": {
56
- "@helia/delegated-routing-v1-http-api-client": "^3.0.0",
57
- "@helia/interface": "^4.3.1",
58
- "@libp2p/interface": "^1.1.4",
57
+ "@helia/delegated-routing-v1-http-api-client": "^4.0.0",
58
+ "@helia/interface": "5.0.0-1b2934b",
59
+ "@libp2p/peer-id": "^5.0.0",
60
+ "@libp2p/interface": "^2.0.0",
59
61
  "@multiformats/uri-to-multiaddr": "^8.0.0",
60
- "ipns": "^9.0.0",
62
+ "ipns": "^10.0.0",
61
63
  "it-first": "^3.0.4",
62
64
  "it-map": "^3.0.5",
63
65
  "multiformats": "^13.1.0",
64
66
  "uint8arrays": "^5.0.2"
65
67
  },
66
68
  "devDependencies": {
67
- "@libp2p/peer-id": "^4.0.7",
68
- "@libp2p/peer-id-factory": "^4.0.7",
69
+ "@libp2p/crypto": "^5.0.0",
69
70
  "aegir": "^44.0.1",
70
71
  "it-all": "^3.0.4",
71
72
  "it-drain": "^3.0.5",
@@ -1,14 +1,15 @@
1
1
  import { createDelegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
2
- import { CodeError } from '@libp2p/interface'
3
- import { marshal, unmarshal, peerIdFromRoutingKey } from 'ipns'
2
+ import { NotFoundError } from '@libp2p/interface'
3
+ import { marshalIPNSRecord, multihashFromIPNSRoutingKey, unmarshalIPNSRecord } from 'ipns'
4
4
  import first from 'it-first'
5
5
  import map from 'it-map'
6
+ import { CID } from 'multiformats/cid'
6
7
  import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
7
8
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
8
9
  import type { DelegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
9
10
  import type { Provider, Routing, RoutingOptions } from '@helia/interface'
10
11
  import type { PeerId, PeerInfo } from '@libp2p/interface'
11
- import type { CID, Version } from 'multiformats'
12
+ import type { Version } from 'multiformats'
12
13
 
13
14
  const IPNS_PREFIX = uint8ArrayFromString('/ipns/')
14
15
 
@@ -42,28 +43,30 @@ class DelegatedHTTPRouter implements Routing {
42
43
  return
43
44
  }
44
45
 
45
- const peerId = peerIdFromRoutingKey(key)
46
- const record = unmarshal(value)
46
+ const digest = multihashFromIPNSRoutingKey(key)
47
+ const cid = CID.createV1(0x72, digest)
48
+ const record = unmarshalIPNSRecord(value)
47
49
 
48
- await this.client.putIPNS(peerId, record, options)
50
+ await this.client.putIPNS(cid, record, options)
49
51
  }
50
52
 
51
53
  async get (key: Uint8Array, options?: RoutingOptions | undefined): Promise<Uint8Array> {
52
54
  if (!isIPNSKey(key)) {
53
- throw new CodeError('Not found', 'ERR_NOT_FOUND')
55
+ throw new NotFoundError('Not found')
54
56
  }
55
57
 
56
- const peerId = peerIdFromRoutingKey(key)
58
+ const digest = multihashFromIPNSRoutingKey(key)
59
+ const cid = CID.createV1(0x72, digest)
57
60
 
58
61
  try {
59
- const record = await this.client.getIPNS(peerId, options)
62
+ const record = await this.client.getIPNS(cid, options)
60
63
 
61
- return marshal(record)
64
+ return marshalIPNSRecord(record)
62
65
  } catch (err: any) {
63
- // ERR_BAD_RESPONSE is thrown when the response had no body, which means
66
+ // BadResponseError is thrown when the response had no body, which means
64
67
  // the record couldn't be found
65
- if (err.code === 'ERR_BAD_RESPONSE') {
66
- throw new CodeError('Not found', 'ERR_NOT_FOUND')
68
+ if (err.name === 'BadResponseError') {
69
+ throw new NotFoundError('Not found')
67
70
  }
68
71
 
69
72
  throw err
@@ -80,7 +83,7 @@ class DelegatedHTTPRouter implements Routing {
80
83
  }
81
84
  }
82
85
 
83
- throw new CodeError('Not found', 'ERR_NOT_FOUND')
86
+ throw new NotFoundError('Not found')
84
87
  }
85
88
 
86
89
  async * getClosestPeers (key: Uint8Array, options?: RoutingOptions | undefined): AsyncIterable<PeerInfo> {
@@ -1,12 +1,11 @@
1
- import { peerIdSymbol } from '@libp2p/interface'
1
+ import { peerIdFromCID } from '@libp2p/peer-id'
2
2
  import { uriToMultiaddr } from '@multiformats/uri-to-multiaddr'
3
3
  import { CID } from 'multiformats/cid'
4
4
  import { identity } from 'multiformats/hashes/identity'
5
5
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
6
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
7
6
  import type { Provider, Routing, RoutingOptions } from '@helia/interface'
8
- import type { PeerId, PeerInfo } from '@libp2p/interface'
9
- import type { MultihashDigest, Version } from 'multiformats'
7
+ import type { PeerInfo } from '@libp2p/interface'
8
+ import type { Version } from 'multiformats'
10
9
 
11
10
  export const DEFAULT_TRUSTLESS_GATEWAYS = [
12
11
  // 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs-public-gateway-checker.on.fleek.co/
@@ -20,58 +19,14 @@ export interface HTTPGatwayRouterInit {
20
19
  gateways?: Array<URL | string>
21
20
  }
22
21
 
23
- // these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
22
+ // this value is from https://github.com/multiformats/multicodec/blob/master/table.csv
24
23
  const TRANSPORT_IPFS_GATEWAY_HTTP_CODE = 0x0920
25
- const inspect = Symbol.for('nodejs.util.inspect.custom')
26
-
27
- class URLPeerId implements PeerId {
28
- readonly type = 'url'
29
- readonly multihash: MultihashDigest
30
- readonly privateKey?: Uint8Array
31
- readonly publicKey?: Uint8Array
32
- readonly url: string
33
-
34
- constructor (url: URL) {
35
- this.url = url.toString()
36
- this.multihash = identity.digest(uint8ArrayFromString(this.url))
37
- }
38
-
39
- [inspect] (): string {
40
- return `PeerId(${this.url})`
41
- }
42
-
43
- readonly [peerIdSymbol] = true
44
-
45
- toString (): string {
46
- return this.toCID().toString()
47
- }
48
-
49
- toCID (): CID {
50
- return CID.createV1(TRANSPORT_IPFS_GATEWAY_HTTP_CODE, this.multihash)
51
- }
52
-
53
- toBytes (): Uint8Array {
54
- return this.toCID().bytes
55
- }
56
-
57
- equals (other?: PeerId | Uint8Array | string): boolean {
58
- if (other == null) {
59
- return false
60
- }
61
-
62
- if (other instanceof Uint8Array) {
63
- other = uint8ArrayToString(other)
64
- }
65
-
66
- return other.toString() === this.toString()
67
- }
68
- }
69
24
 
70
25
  function toPeerInfo (url: string | URL): PeerInfo {
71
26
  url = url.toString()
72
27
 
73
28
  return {
74
- id: new URLPeerId(new URL(url)),
29
+ id: peerIdFromCID(CID.createV1(TRANSPORT_IPFS_GATEWAY_HTTP_CODE, identity.digest(uint8ArrayFromString(url)))),
75
30
  multiaddrs: [
76
31
  uriToMultiaddr(url)
77
32
  ]
package/src/index.ts CHANGED
@@ -5,4 +5,5 @@
5
5
  */
6
6
  export { delegatedHTTPRouting } from './delegated-http-routing.js'
7
7
  export { httpGatewayRouting } from './http-gateway-routing.js'
8
+ export type { HTTPGatwayRouterInit } from './http-gateway-routing.js'
8
9
  export { libp2pRouting } from './libp2p-routing.js'
@@ -1,5 +0,0 @@
1
- {
2
- "delegatedHTTPRouting": "https://ipfs.github.io/helia/functions/_helia_routers.delegatedHTTPRouting.html",
3
- "httpGatewayRouting": "https://ipfs.github.io/helia/functions/_helia_routers.httpGatewayRouting.html",
4
- "libp2pRouting": "https://ipfs.github.io/helia/functions/_helia_routers.libp2pRouting.html"
5
- }