@helia/utils 2.5.2-6f8165b5 → 2.5.2-9114743f

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 (63) hide show
  1. package/dist/index.min.js +22 -1
  2. package/dist/index.min.js.map +4 -4
  3. package/dist/src/abstract-session.d.ts +3 -3
  4. package/dist/src/abstract-session.d.ts.map +1 -1
  5. package/dist/src/abstract-session.js.map +1 -1
  6. package/dist/src/errors.d.ts +4 -0
  7. package/dist/src/errors.d.ts.map +1 -1
  8. package/dist/src/errors.js +4 -0
  9. package/dist/src/errors.js.map +1 -1
  10. package/dist/src/graph-walker.d.ts +3 -21
  11. package/dist/src/graph-walker.d.ts.map +1 -1
  12. package/dist/src/graph-walker.js +20 -17
  13. package/dist/src/graph-walker.js.map +1 -1
  14. package/dist/src/index.d.ts +39 -36
  15. package/dist/src/index.d.ts.map +1 -1
  16. package/dist/src/index.js +62 -30
  17. package/dist/src/index.js.map +1 -1
  18. package/dist/src/pins.d.ts.map +1 -1
  19. package/dist/src/pins.js +11 -39
  20. package/dist/src/pins.js.map +1 -1
  21. package/dist/src/routing.d.ts +7 -5
  22. package/dist/src/routing.d.ts.map +1 -1
  23. package/dist/src/routing.js +10 -4
  24. package/dist/src/routing.js.map +1 -1
  25. package/dist/src/utils/constants.d.ts +4 -0
  26. package/dist/src/utils/constants.d.ts.map +1 -0
  27. package/dist/src/utils/constants.js +4 -0
  28. package/dist/src/utils/constants.js.map +1 -0
  29. package/dist/src/utils/get-codec.d.ts +2 -1
  30. package/dist/src/utils/get-codec.d.ts.map +1 -1
  31. package/dist/src/utils/get-codec.js +0 -1
  32. package/dist/src/utils/get-codec.js.map +1 -1
  33. package/dist/src/utils/get-crypto.d.ts +4 -0
  34. package/dist/src/utils/get-crypto.d.ts.map +1 -0
  35. package/dist/src/utils/get-crypto.js +35 -0
  36. package/dist/src/utils/get-crypto.js.map +1 -0
  37. package/dist/src/utils/get-hasher.d.ts +2 -1
  38. package/dist/src/utils/get-hasher.d.ts.map +1 -1
  39. package/dist/src/utils/get-hasher.js.map +1 -1
  40. package/dist/src/utils/is-cid.d.ts +3 -0
  41. package/dist/src/utils/is-cid.d.ts.map +1 -0
  42. package/dist/src/utils/is-cid.js +8 -0
  43. package/dist/src/utils/is-cid.js.map +1 -0
  44. package/dist/src/utils/networked-storage.d.ts +2 -1
  45. package/dist/src/utils/networked-storage.d.ts.map +1 -1
  46. package/dist/src/utils/networked-storage.js.map +1 -1
  47. package/dist/src/utils/session-storage.d.ts +2 -2
  48. package/dist/src/utils/session-storage.d.ts.map +1 -1
  49. package/dist/src/utils/session-storage.js.map +1 -1
  50. package/package.json +21 -22
  51. package/src/abstract-session.ts +4 -4
  52. package/src/errors.ts +5 -0
  53. package/src/graph-walker.ts +30 -43
  54. package/src/index.ts +108 -73
  55. package/src/pins.ts +12 -50
  56. package/src/routing.ts +23 -10
  57. package/src/utils/constants.ts +3 -0
  58. package/src/utils/get-codec.ts +2 -3
  59. package/src/utils/get-crypto.ts +44 -0
  60. package/src/utils/get-hasher.ts +2 -1
  61. package/src/utils/is-cid.ts +9 -0
  62. package/src/utils/networked-storage.ts +2 -1
  63. package/src/utils/session-storage.ts +2 -2
@@ -0,0 +1,44 @@
1
+ import { UnknownCryptoError } from '@helia/interface'
2
+ import { ecdsaCrypto, ed25519Crypto, rsaCrypto } from '@ipshipyard/crypto'
3
+ import { isPromise } from './is-promise.ts'
4
+ import type { CryptoLoader } from '@helia/interface'
5
+ import type { Crypto } from '@ipshipyard/crypto'
6
+
7
+ export function getCrypto (initialCryptos: Array<Crypto> = [], loadCrypto?: CryptoLoader): CryptoLoader {
8
+ const cryptos: Record<string | number, Crypto> = {}
9
+
10
+ initialCryptos = [
11
+ ecdsaCrypto(),
12
+ ed25519Crypto(),
13
+ rsaCrypto(),
14
+ ...initialCryptos
15
+ ]
16
+
17
+ initialCryptos.forEach(crypto => {
18
+ cryptos[crypto.type] = crypto
19
+ cryptos[crypto.code] = crypto
20
+ })
21
+
22
+ return async (nameOrCode) => {
23
+ let crypto = cryptos[nameOrCode]
24
+
25
+ if (crypto == null && loadCrypto != null) {
26
+ const res = loadCrypto(nameOrCode)
27
+
28
+ if (isPromise(res)) {
29
+ crypto = await res
30
+ } else {
31
+ crypto = res
32
+ }
33
+
34
+ cryptos[crypto.type] = crypto
35
+ cryptos[crypto.code] = crypto
36
+ }
37
+
38
+ if (crypto != null) {
39
+ return crypto
40
+ }
41
+
42
+ throw new UnknownCryptoError(`Could not load crypto for ${crypto}`)
43
+ }
44
+ }
@@ -2,9 +2,10 @@ import { UnknownHashAlgorithmError } from '@helia/interface'
2
2
  import { identity } from 'multiformats/hashes/identity'
3
3
  import { sha256, sha512 } from 'multiformats/hashes/sha2'
4
4
  import { isPromise } from './is-promise.ts'
5
+ import type { HasherLoader } from '@helia/interface'
5
6
  import type { MultihashHasher } from 'multiformats/hashes/interface'
6
7
 
7
- export function getHasher (initialHashers: MultihashHasher[] = [], loadHasher?: (code: number) => MultihashHasher | Promise<MultihashHasher>): (code: number) => MultihashHasher | Promise<MultihashHasher> {
8
+ export function getHasher (initialHashers: MultihashHasher[] = [], loadHasher?: HasherLoader): HasherLoader {
8
9
  const hashers: Record<number, MultihashHasher> = {
9
10
  [sha256.code]: sha256,
10
11
  [sha512.code]: sha512,
@@ -0,0 +1,9 @@
1
+ import { CID } from 'multiformats/cid'
2
+
3
+ export function isCID (obj?: any): obj is CID {
4
+ if (obj == null) {
5
+ return false
6
+ }
7
+
8
+ return CID.asCID(obj) != null
9
+ }
@@ -4,7 +4,8 @@ import { SessionStorage } from './session-storage.ts'
4
4
  import { Storage } from './storage.ts'
5
5
  import type { StorageComponents, StorageInit } from './storage.ts'
6
6
  import type { BlockBroker, Blocks, CreateSessionOptions, SessionBlockstore } from '@helia/interface/blocks'
7
- import type { AbortOptions, Startable } from '@libp2p/interface'
7
+ import type { Startable } from '@libp2p/interface'
8
+ import type { AbortOptions } from 'abort-error'
8
9
  import type { Blockstore } from 'interface-blockstore'
9
10
  import type { CID } from 'multiformats/cid'
10
11
 
@@ -3,7 +3,7 @@ import { anySignal } from 'any-signal'
3
3
  import { Storage } from './storage.ts'
4
4
  import type { StorageComponents } from './storage.ts'
5
5
  import type { Pair, DeleteManyBlocksProgressEvents, DeleteBlockProgressEvents, GetBlockProgressEvents, GetManyBlocksProgressEvents, PutManyBlocksProgressEvents, PutBlockProgressEvents, GetAllBlocksProgressEvents, GetOfflineOptions, SessionBlockstore, SessionBlockBroker } from '@helia/interface/blocks'
6
- import type { AbortOptions, PeerId } from '@libp2p/interface'
6
+ import type { AbortOptions } from '@libp2p/interface'
7
7
  import type { Multiaddr } from '@multiformats/multiaddr'
8
8
  import type { InputPair } from 'interface-blockstore'
9
9
  import type { CID } from 'multiformats/cid'
@@ -35,7 +35,7 @@ export class SessionStorage extends Storage<SessionBlockBroker> implements Sessi
35
35
  this.closeController.abort()
36
36
  }
37
37
 
38
- async addPeer (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<void> {
38
+ async addPeer (peer: CID | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<void> {
39
39
  await Promise.all(
40
40
  this.blockBrokers
41
41
  .map(broker => broker.addPeer(peer, options))