@helia/utils 0.3.3-ec8bf66 → 0.3.3-f5a03fc

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 +16 -11
  3. package/dist/src/index.d.ts.map +1 -1
  4. package/dist/src/index.js +9 -17
  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 +8 -9
  9. package/dist/src/pins.js.map +1 -1
  10. package/dist/src/utils/get-codec.d.ts +4 -0
  11. package/dist/src/utils/get-codec.d.ts.map +1 -0
  12. package/dist/src/utils/get-codec.js +38 -0
  13. package/dist/src/utils/get-codec.js.map +1 -0
  14. package/dist/src/utils/get-hasher.d.ts +4 -0
  15. package/dist/src/utils/get-hasher.d.ts.map +1 -0
  16. package/dist/src/utils/get-hasher.js +32 -0
  17. package/dist/src/utils/get-hasher.js.map +1 -0
  18. package/dist/src/utils/is-promise.d.ts +2 -0
  19. package/dist/src/utils/is-promise.d.ts.map +1 -0
  20. package/dist/src/utils/is-promise.js +4 -0
  21. package/dist/src/utils/is-promise.js.map +1 -0
  22. package/dist/src/utils/networked-storage.d.ts +3 -2
  23. package/dist/src/utils/networked-storage.d.ts.map +1 -1
  24. package/dist/src/utils/networked-storage.js +16 -6
  25. package/dist/src/utils/networked-storage.js.map +1 -1
  26. package/package.json +3 -3
  27. package/src/index.ts +27 -23
  28. package/src/pins.ts +9 -12
  29. package/src/utils/get-codec.ts +47 -0
  30. package/src/utils/get-hasher.ts +40 -0
  31. package/src/utils/is-promise.ts +3 -0
  32. package/src/utils/networked-storage.ts +21 -8
  33. package/dist/src/utils/dag-walkers.d.ts +0 -28
  34. package/dist/src/utils/dag-walkers.d.ts.map +0 -1
  35. package/dist/src/utils/dag-walkers.js +0 -171
  36. package/dist/src/utils/dag-walkers.js.map +0 -1
  37. package/dist/src/utils/default-hashers.d.ts +0 -3
  38. package/dist/src/utils/default-hashers.d.ts.map +0 -1
  39. package/dist/src/utils/default-hashers.js +0 -15
  40. package/dist/src/utils/default-hashers.js.map +0 -1
  41. package/src/utils/dag-walkers.ts +0 -198
  42. package/src/utils/default-hashers.ts +0 -18
@@ -1,198 +0,0 @@
1
- /* eslint max-depth: ["error", 7] */
2
-
3
- import * as dagCbor from '@ipld/dag-cbor'
4
- import * as dagJson from '@ipld/dag-json'
5
- import * as dagPb from '@ipld/dag-pb'
6
- import * as cborg from 'cborg'
7
- import { Type, Token } from 'cborg'
8
- import * as cborgJson from 'cborg/json'
9
- import { CID } from 'multiformats'
10
- import { base64 } from 'multiformats/bases/base64'
11
- import * as json from 'multiformats/codecs/json'
12
- import * as raw from 'multiformats/codecs/raw'
13
- import type { DAGWalker } from '@helia/interface'
14
-
15
- /**
16
- * Dag walker for dag-pb CIDs
17
- */
18
- export const dagPbWalker: DAGWalker = {
19
- codec: dagPb.code,
20
- * walk (block) {
21
- const node = dagPb.decode(block)
22
-
23
- yield * node.Links.map(l => l.Hash)
24
- }
25
- }
26
-
27
- /**
28
- * Dag walker for raw CIDs
29
- */
30
- export const rawWalker: DAGWalker = {
31
- codec: raw.code,
32
- * walk () {
33
- // no embedded CIDs in a raw block
34
- }
35
- }
36
-
37
- // https://github.com/ipfs/go-ipfs/issues/3570#issuecomment-273931692
38
- const CID_TAG = 42
39
-
40
- /**
41
- * Dag walker for dag-cbor CIDs. Does not actually use dag-cbor since
42
- * all we are interested in is extracting the the CIDs from the block
43
- * so we can just use cborg for that.
44
- */
45
- export const dagCborWalker: DAGWalker = {
46
- codec: dagCbor.code,
47
- * walk (block) {
48
- const cids: CID[] = []
49
- const tags: cborg.TagDecoder[] = []
50
- tags[CID_TAG] = (bytes) => {
51
- if (bytes[0] !== 0) {
52
- throw new Error('Invalid CID for CBOR tag 42; expected leading 0x00')
53
- }
54
-
55
- const cid = CID.decode(bytes.subarray(1)) // ignore leading 0x00
56
-
57
- cids.push(cid)
58
-
59
- return cid
60
- }
61
-
62
- cborg.decode(block, {
63
- tags
64
- })
65
-
66
- yield * cids
67
- }
68
- }
69
-
70
- /**
71
- * Borrowed from @ipld/dag-json
72
- */
73
- class DagJsonTokenizer extends cborgJson.Tokenizer {
74
- private readonly tokenBuffer: cborg.Token[]
75
-
76
- constructor (data: Uint8Array, options?: cborg.DecodeOptions) {
77
- super(data, options)
78
-
79
- this.tokenBuffer = []
80
- }
81
-
82
- done (): boolean {
83
- return this.tokenBuffer.length === 0 && super.done()
84
- }
85
-
86
- _next (): cborg.Token {
87
- if (this.tokenBuffer.length > 0) {
88
- // @ts-expect-error https://github.com/Microsoft/TypeScript/issues/30406
89
- return this.tokenBuffer.pop()
90
- }
91
- return super.next()
92
- }
93
-
94
- /**
95
- * Implements rules outlined in https://github.com/ipld/specs/pull/356
96
- */
97
- next (): cborg.Token {
98
- const token = this._next()
99
-
100
- if (token.type === Type.map) {
101
- const keyToken = this._next()
102
- if (keyToken.type === Type.string && keyToken.value === '/') {
103
- const valueToken = this._next()
104
- if (valueToken.type === Type.string) { // *must* be a CID
105
- const breakToken = this._next() // swallow the end-of-map token
106
- if (breakToken.type !== Type.break) {
107
- throw new Error('Invalid encoded CID form')
108
- }
109
- this.tokenBuffer.push(valueToken) // CID.parse will pick this up after our tag token
110
- return new Token(Type.tag, 42, 0)
111
- }
112
- if (valueToken.type === Type.map) {
113
- const innerKeyToken = this._next()
114
- if (innerKeyToken.type === Type.string && innerKeyToken.value === 'bytes') {
115
- const innerValueToken = this._next()
116
- if (innerValueToken.type === Type.string) { // *must* be Bytes
117
- for (let i = 0; i < 2; i++) {
118
- const breakToken = this._next() // swallow two end-of-map tokens
119
- if (breakToken.type !== Type.break) {
120
- throw new Error('Invalid encoded Bytes form')
121
- }
122
- }
123
- const bytes = base64.decode(`m${innerValueToken.value}`)
124
- return new Token(Type.bytes, bytes, innerValueToken.value.length)
125
- }
126
- this.tokenBuffer.push(innerValueToken) // bail
127
- }
128
- this.tokenBuffer.push(innerKeyToken) // bail
129
- }
130
- this.tokenBuffer.push(valueToken) // bail
131
- }
132
- this.tokenBuffer.push(keyToken) // bail
133
- }
134
- return token
135
- }
136
- }
137
-
138
- /**
139
- * Dag walker for dag-json CIDs. Does not actually use dag-json since
140
- * all we are interested in is extracting the the CIDs from the block
141
- * so we can just use cborg/json for that.
142
- */
143
- export const dagJsonWalker: DAGWalker = {
144
- codec: dagJson.code,
145
- * walk (block) {
146
- const cids: CID[] = []
147
- const tags: cborg.TagDecoder[] = []
148
- tags[CID_TAG] = (string) => {
149
- const cid = CID.parse(string)
150
-
151
- cids.push(cid)
152
-
153
- return cid
154
- }
155
-
156
- cborgJson.decode(block, {
157
- tags,
158
- tokenizer: new DagJsonTokenizer(block, {
159
- tags,
160
- allowIndefinite: true,
161
- allowUndefined: true,
162
- allowNaN: true,
163
- allowInfinity: true,
164
- allowBigInt: true,
165
- strict: false,
166
- rejectDuplicateMapKeys: false
167
- })
168
- })
169
-
170
- yield * cids
171
- }
172
- }
173
-
174
- /**
175
- * Dag walker for json CIDs. JSON has no facility for linking to
176
- * external blocks so the walker is a no-op.
177
- */
178
- export const jsonWalker: DAGWalker = {
179
- codec: json.code,
180
- * walk () {}
181
- }
182
-
183
- export function defaultDagWalkers (walkers: DAGWalker[] = []): Record<number, DAGWalker> {
184
- const output: Record<number, DAGWalker> = {}
185
-
186
- ;[
187
- dagPbWalker,
188
- rawWalker,
189
- dagCborWalker,
190
- dagJsonWalker,
191
- jsonWalker,
192
- ...walkers
193
- ].forEach(dagWalker => {
194
- output[dagWalker.codec] = dagWalker
195
- })
196
-
197
- return output
198
- }
@@ -1,18 +0,0 @@
1
- import { identity } from 'multiformats/hashes/identity'
2
- import { sha256, sha512 } from 'multiformats/hashes/sha2'
3
- import type { MultihashHasher } from 'multiformats/hashes/interface'
4
-
5
- export function defaultHashers (hashers: MultihashHasher[] = []): Record<number, MultihashHasher> {
6
- const output: Record<number, MultihashHasher> = {}
7
-
8
- ;[
9
- sha256,
10
- sha512,
11
- identity,
12
- ...hashers
13
- ].forEach(hasher => {
14
- output[hasher.code] = hasher
15
- })
16
-
17
- return output
18
- }