@helia/utils 0.3.3-5ff6998 → 0.3.3-8805202

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 (42) hide show
  1. package/dist/index.min.js +1 -1
  2. package/dist/src/index.d.ts +4 -15
  3. package/dist/src/index.d.ts.map +1 -1
  4. package/dist/src/index.js +9 -9
  5. package/dist/src/index.js.map +1 -1
  6. package/dist/src/pins.d.ts +3 -3
  7. package/dist/src/pins.d.ts.map +1 -1
  8. package/dist/src/pins.js +9 -8
  9. package/dist/src/pins.js.map +1 -1
  10. package/dist/src/utils/dag-walkers.d.ts +28 -0
  11. package/dist/src/utils/dag-walkers.d.ts.map +1 -0
  12. package/dist/src/utils/dag-walkers.js +171 -0
  13. package/dist/src/utils/dag-walkers.js.map +1 -0
  14. package/dist/src/utils/default-hashers.d.ts +3 -0
  15. package/dist/src/utils/default-hashers.d.ts.map +1 -0
  16. package/dist/src/utils/default-hashers.js +15 -0
  17. package/dist/src/utils/default-hashers.js.map +1 -0
  18. package/dist/src/utils/networked-storage.d.ts +2 -3
  19. package/dist/src/utils/networked-storage.d.ts.map +1 -1
  20. package/dist/src/utils/networked-storage.js +6 -16
  21. package/dist/src/utils/networked-storage.js.map +1 -1
  22. package/package.json +2 -2
  23. package/src/index.ts +13 -26
  24. package/src/pins.ts +12 -9
  25. package/src/utils/dag-walkers.ts +198 -0
  26. package/src/utils/default-hashers.ts +18 -0
  27. package/src/utils/networked-storage.ts +8 -21
  28. package/dist/src/utils/get-codec.d.ts +0 -4
  29. package/dist/src/utils/get-codec.d.ts.map +0 -1
  30. package/dist/src/utils/get-codec.js +0 -38
  31. package/dist/src/utils/get-codec.js.map +0 -1
  32. package/dist/src/utils/get-hasher.d.ts +0 -4
  33. package/dist/src/utils/get-hasher.d.ts.map +0 -1
  34. package/dist/src/utils/get-hasher.js +0 -32
  35. package/dist/src/utils/get-hasher.js.map +0 -1
  36. package/dist/src/utils/is-promise.d.ts +0 -2
  37. package/dist/src/utils/is-promise.d.ts.map +0 -1
  38. package/dist/src/utils/is-promise.js +0 -4
  39. package/dist/src/utils/is-promise.js.map +0 -1
  40. package/src/utils/get-codec.ts +0 -47
  41. package/src/utils/get-hasher.ts +0 -40
  42. package/src/utils/is-promise.ts +0 -3
@@ -1,47 +0,0 @@
1
- /* eslint max-depth: ["error", 7] */
2
-
3
- import { UnknownCodecError } from '@helia/interface'
4
- import * as dagCbor from '@ipld/dag-cbor'
5
- import * as dagJson from '@ipld/dag-json'
6
- import * as dagPb from '@ipld/dag-pb'
7
- import * as json from 'multiformats/codecs/json'
8
- import * as raw from 'multiformats/codecs/raw'
9
- import { isPromise } from './is-promise.js'
10
- import type { Await } from '@helia/interface'
11
- import type { BlockCodec } from 'multiformats/codecs/interface'
12
-
13
- export function getCodec <T = any, Code extends number = any> (initialCodecs: Array<BlockCodec<any, any>> = [], loadCodec?: (code: number) => Await<BlockCodec<any, any>>): (code: Code) => Await<BlockCodec<Code, T>> {
14
- const codecs: Record<number, BlockCodec<any, any>> = {
15
- [dagPb.code]: dagPb,
16
- [raw.code]: raw,
17
- [dagCbor.code]: dagCbor,
18
- [dagJson.code]: dagJson,
19
- [json.code]: json
20
- }
21
-
22
- initialCodecs.forEach(codec => {
23
- codecs[codec.code] = codec
24
- })
25
-
26
- return async (code) => {
27
- let codec = codecs[code]
28
-
29
- if (codec == null && loadCodec != null) {
30
- const res = loadCodec(code)
31
-
32
- if (isPromise(res)) {
33
- codec = await res
34
- } else {
35
- codec = res
36
- }
37
-
38
- codecs[codec.code] = codec
39
- }
40
-
41
- if (codec != null) {
42
- return codec
43
- }
44
-
45
- throw new UnknownCodecError(`Could not load codec for ${code}`)
46
- }
47
- }
@@ -1,40 +0,0 @@
1
- import { UnknownHashAlgorithmError } from '@helia/interface'
2
- import { identity } from 'multiformats/hashes/identity'
3
- import { sha256, sha512 } from 'multiformats/hashes/sha2'
4
- import { isPromise } from './is-promise.js'
5
- import type { Await } from '@helia/interface'
6
- import type { MultihashHasher } from 'multiformats/hashes/interface'
7
-
8
- export function getHasher (initialHashers: MultihashHasher[] = [], loadHasher?: (code: number) => Await<MultihashHasher>): (code: number) => Await<MultihashHasher> {
9
- const hashers: Record<number, MultihashHasher> = {
10
- [sha256.code]: sha256,
11
- [sha512.code]: sha512,
12
- [identity.code]: identity
13
- }
14
-
15
- initialHashers.forEach(hasher => {
16
- hashers[hasher.code] = hasher
17
- })
18
-
19
- return async (code) => {
20
- let hasher = hashers[code]
21
-
22
- if (hasher == null && loadHasher != null) {
23
- const res = loadHasher(code)
24
-
25
- if (isPromise(res)) {
26
- hasher = await res
27
- } else {
28
- hasher = res
29
- }
30
-
31
- hashers[hasher.code] = hasher
32
- }
33
-
34
- if (hasher != null) {
35
- return hasher
36
- }
37
-
38
- throw new UnknownHashAlgorithmError(`No hasher configured for multihash code 0x${code.toString(16)}, please configure one. You can look up which hash this is at https://github.com/multiformats/multicodec/blob/master/table.csv`)
39
- }
40
- }
@@ -1,3 +0,0 @@
1
- export function isPromise <T> (p?: any): p is Promise<T> {
2
- return p?.then != null
3
- }