@helia/ipns 1.1.1 → 1.1.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.
package/src/index.ts CHANGED
@@ -62,22 +62,24 @@
62
62
  * ```
63
63
  */
64
64
 
65
- import type { AbortOptions } from '@libp2p/interfaces'
66
- import { isPeerId, PeerId } from '@libp2p/interface-peer-id'
65
+ import { isPeerId, type PeerId } from '@libp2p/interface-peer-id'
66
+ import { CodeError } from '@libp2p/interfaces/errors'
67
+ import { logger } from '@libp2p/logger'
68
+ import { peerIdFromString } from '@libp2p/peer-id'
67
69
  import { create, marshal, peerIdToRoutingKey, unmarshal } from 'ipns'
68
- import type { IPNSEntry } from 'ipns'
69
- import type { IPNSRouting, IPNSRoutingEvents } from './routing/index.js'
70
+ import { ipnsSelector } from 'ipns/selector'
70
71
  import { ipnsValidator } from 'ipns/validator'
71
72
  import { CID } from 'multiformats/cid'
72
- import { resolveDnslink } from './utils/resolve-dns-link.js'
73
- import { logger } from '@libp2p/logger'
74
- import { peerIdFromString } from '@libp2p/peer-id'
75
- import type { ProgressEvent, ProgressOptions } from 'progress-events'
76
73
  import { CustomProgressEvent } from 'progress-events'
77
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
78
74
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
75
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
76
+ import { localStore, type LocalStore } from './routing/local-store.js'
77
+ import { resolveDnslink } from './utils/resolve-dns-link.js'
78
+ import type { IPNSRouting, IPNSRoutingEvents } from './routing/index.js'
79
+ import type { AbortOptions } from '@libp2p/interfaces'
79
80
  import type { Datastore } from 'interface-datastore'
80
- import { localStore, LocalStore } from './routing/local-store.js'
81
+ import type { IPNSEntry } from 'ipns'
82
+ import type { ProgressEvent, ProgressOptions } from 'progress-events'
81
83
 
82
84
  const log = logger('helia:ipns')
83
85
 
@@ -221,13 +223,13 @@ class DefaultIPNS implements IPNS {
221
223
  const record = await this.#findIpnsRecord(routingKey, options)
222
224
  const str = uint8ArrayToString(record.value)
223
225
 
224
- return await this.#resolve(str, options)
226
+ return this.#resolve(str, options)
225
227
  }
226
228
 
227
229
  async resolveDns (domain: string, options: ResolveDNSOptions = {}): Promise<CID> {
228
230
  const dnslink = await resolveDnslink(domain, options)
229
231
 
230
- return await this.#resolve(dnslink, options)
232
+ return this.#resolve(dnslink, options)
231
233
  }
232
234
 
233
235
  republish (options: RepublishOptions = {}): void {
@@ -273,7 +275,7 @@ class DefaultIPNS implements IPNS {
273
275
  const scheme = parts[1]
274
276
 
275
277
  if (scheme === 'ipns') {
276
- return await this.resolve(peerIdFromString(parts[2]), options)
278
+ return this.resolve(peerIdFromString(parts[2]), options)
277
279
  } else if (scheme === 'ipfs') {
278
280
  return CID.parse(parts[2])
279
281
  }
@@ -295,16 +297,30 @@ class DefaultIPNS implements IPNS {
295
297
  ]
296
298
  }
297
299
 
298
- const unmarshaledRecord = await Promise.any(
299
- routers.map(async (router) => {
300
- const unmarshaledRecord = await router.get(routingKey, options)
301
- await ipnsValidator(routingKey, unmarshaledRecord)
300
+ const records: Uint8Array[] = []
302
301
 
303
- return unmarshaledRecord
302
+ await Promise.all(
303
+ routers.map(async (router) => {
304
+ try {
305
+ const record = await router.get(routingKey, options)
306
+ await ipnsValidator(routingKey, record)
307
+
308
+ records.push(record)
309
+ } catch (err) {
310
+ log.error('error finding IPNS record', err)
311
+ }
304
312
  })
305
313
  )
306
314
 
307
- return unmarshal(unmarshaledRecord)
315
+ if (records.length === 0) {
316
+ throw new CodeError('Could not find record for routing key', 'ERR_NOT_FOUND')
317
+ }
318
+
319
+ const record = records[ipnsSelector(routingKey, records)]
320
+
321
+ await this.localStore.put(routingKey, record, options)
322
+
323
+ return unmarshal(record)
308
324
  }
309
325
  }
310
326
 
@@ -1,6 +1,6 @@
1
- import type { IPNSRouting } from '../index.js'
1
+ import { CustomProgressEvent, type ProgressEvent } from 'progress-events'
2
2
  import type { GetOptions, PutOptions } from './index.js'
3
- import { CustomProgressEvent, ProgressEvent } from 'progress-events'
3
+ import type { IPNSRouting } from '../index.js'
4
4
  import type { ContentRouting } from '@libp2p/interface-content-routing'
5
5
 
6
6
  export interface DHTRoutingComponents {
@@ -1,8 +1,8 @@
1
- import type { ProgressOptions } from 'progress-events'
2
- import type { AbortOptions } from '@libp2p/interfaces'
3
1
  import type { DHTProgressEvents } from './dht.js'
4
2
  import type { DatastoreProgressEvents } from './local-store.js'
5
3
  import type { PubSubProgressEvents } from './pubsub.js'
4
+ import type { AbortOptions } from '@libp2p/interfaces'
5
+ import type { ProgressOptions } from 'progress-events'
6
6
 
7
7
  export interface PutOptions extends AbortOptions, ProgressOptions {
8
8
 
@@ -1,9 +1,9 @@
1
- import { CustomProgressEvent, ProgressEvent } from 'progress-events'
2
- import type { AbortOptions } from '@libp2p/interfaces'
3
1
  import { Libp2pRecord } from '@libp2p/record'
4
- import { Datastore, Key } from 'interface-datastore'
2
+ import { type Datastore, Key } from 'interface-datastore'
3
+ import { CustomProgressEvent, type ProgressEvent } from 'progress-events'
5
4
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
6
5
  import type { GetOptions, IPNSRouting, PutOptions } from '../routing'
6
+ import type { AbortOptions } from '@libp2p/interfaces'
7
7
 
8
8
  function dhtRoutingKey (key: Uint8Array): Key {
9
9
  return new Key('/dht/record/' + uint8ArrayToString(key, 'base32'), false)
@@ -25,12 +25,12 @@ export interface LocalStore extends IPNSRouting {
25
25
  */
26
26
  export function localStore (datastore: Datastore): LocalStore {
27
27
  return {
28
- async put (routingKey: Uint8Array, marshaledRecord: Uint8Array, options: PutOptions = {}) {
28
+ async put (routingKey: Uint8Array, marshalledRecord: Uint8Array, options: PutOptions = {}) {
29
29
  try {
30
30
  const key = dhtRoutingKey(routingKey)
31
31
 
32
32
  // Marshal to libp2p record as the DHT does
33
- const record = new Libp2pRecord(routingKey, marshaledRecord, new Date())
33
+ const record = new Libp2pRecord(routingKey, marshalledRecord, new Date())
34
34
 
35
35
  options.onProgress?.(new CustomProgressEvent('ipns:routing:datastore:put'))
36
36
  await datastore.put(key, record.serialize(), options)
@@ -57,7 +57,7 @@ export function localStore (datastore: Datastore): LocalStore {
57
57
  },
58
58
  async has (routingKey: Uint8Array, options: AbortOptions = {}): Promise<boolean> {
59
59
  const key = dhtRoutingKey(routingKey)
60
- return await datastore.has(key, options)
60
+ return datastore.has(key, options)
61
61
  }
62
62
  }
63
63
  }
@@ -1,17 +1,17 @@
1
+ import { CodeError } from '@libp2p/interfaces/errors'
2
+ import { logger } from '@libp2p/logger'
1
3
  import { peerIdToRoutingKey } from 'ipns'
4
+ import { ipnsSelector } from 'ipns/selector'
5
+ import { ipnsValidator } from 'ipns/validator'
6
+ import { CustomProgressEvent, type ProgressEvent } from 'progress-events'
7
+ import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
2
8
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
3
9
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
4
- import { logger } from '@libp2p/logger'
10
+ import { localStore, type LocalStore } from './local-store.js'
11
+ import type { GetOptions, IPNSRouting, PutOptions } from './index.js'
5
12
  import type { PeerId } from '@libp2p/interface-peer-id'
6
13
  import type { Message, PublishResult, PubSub } from '@libp2p/interface-pubsub'
7
14
  import type { Datastore } from 'interface-datastore'
8
- import type { GetOptions, IPNSRouting, PutOptions } from './index.js'
9
- import { CodeError } from '@libp2p/interfaces/errors'
10
- import { localStore, LocalStore } from './local-store.js'
11
- import { ipnsValidator } from 'ipns/validator'
12
- import { ipnsSelector } from 'ipns/selector'
13
- import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
14
- import { CustomProgressEvent, ProgressEvent } from 'progress-events'
15
15
 
16
16
  const log = logger('helia:ipns:routing:pubsub')
17
17
 
@@ -19,7 +19,9 @@ export interface PubsubRoutingComponents {
19
19
  datastore: Datastore
20
20
  libp2p: {
21
21
  peerId: PeerId
22
- pubsub: PubSub
22
+ services: {
23
+ pubsub: PubSub
24
+ }
23
25
  }
24
26
  }
25
27
 
@@ -46,7 +48,7 @@ class PubSubRouting implements IPNSRouting {
46
48
  this.subscriptions = []
47
49
  this.localStore = localStore(components.datastore)
48
50
  this.peerId = components.libp2p.peerId
49
- this.pubsub = components.libp2p.pubsub
51
+ this.pubsub = components.libp2p.services.pubsub
50
52
 
51
53
  this.pubsub.addEventListener('message', (evt) => {
52
54
  const message = evt.detail
@@ -1,7 +1,7 @@
1
1
  /* eslint-env browser */
2
2
 
3
- import { TLRU } from './tlru.js'
4
3
  import PQueue from 'p-queue'
4
+ import { TLRU } from './tlru.js'
5
5
  import type { AbortOptions } from '@libp2p/interfaces'
6
6
 
7
7
  // Avoid sending multiple queries for the same hostname by caching results
@@ -57,5 +57,5 @@ export async function resolveDnslink (fqdn: string, opts: ResolveDnsLinkOptions
57
57
  return ipfsPath(response)
58
58
  }
59
59
 
60
- return await resolve(fqdn, opts)
60
+ return resolve(fqdn, opts)
61
61
  }
@@ -1,12 +1,12 @@
1
1
  import dns from 'dns'
2
2
  import { promisify } from 'util'
3
- import type { AbortOptions } from '@libp2p/interfaces'
4
3
  import * as isIPFS from 'is-ipfs'
4
+ import type { AbortOptions } from '@libp2p/interfaces'
5
5
 
6
6
  const MAX_RECURSIVE_DEPTH = 32
7
7
 
8
8
  export async function resolveDnslink (domain: string, options: AbortOptions = {}): Promise<string> {
9
- return await recursiveResolveDnslink(domain, MAX_RECURSIVE_DEPTH, options)
9
+ return recursiveResolveDnslink(domain, MAX_RECURSIVE_DEPTH, options)
10
10
  }
11
11
 
12
12
  async function recursiveResolveDnslink (domain: string, depth: number, options: AbortOptions = {}): Promise<string> {
@@ -44,7 +44,7 @@ async function recursiveResolveDnslink (domain: string, depth: number, options:
44
44
  return result
45
45
  }
46
46
 
47
- return await recursiveResolveDnslink(domainOrCID, depth - 1, options)
47
+ return recursiveResolveDnslink(domainOrCID, depth - 1, options)
48
48
  }
49
49
 
50
50
  async function resolve (domain: string, options: AbortOptions = {}): Promise<string> {