@helia/unixfs 7.0.4 → 7.1.0

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 (96) hide show
  1. package/dist/index.min.js +4 -4
  2. package/dist/index.min.js.map +4 -4
  3. package/dist/src/commands/add.d.ts +7 -7
  4. package/dist/src/commands/add.d.ts.map +1 -1
  5. package/dist/src/commands/add.js +12 -4
  6. package/dist/src/commands/add.js.map +1 -1
  7. package/dist/src/commands/cat.d.ts +3 -3
  8. package/dist/src/commands/cat.d.ts.map +1 -1
  9. package/dist/src/commands/cat.js +2 -2
  10. package/dist/src/commands/cat.js.map +1 -1
  11. package/dist/src/commands/chmod.d.ts +3 -3
  12. package/dist/src/commands/chmod.d.ts.map +1 -1
  13. package/dist/src/commands/chmod.js +3 -3
  14. package/dist/src/commands/chmod.js.map +1 -1
  15. package/dist/src/commands/cp.d.ts +3 -3
  16. package/dist/src/commands/cp.d.ts.map +1 -1
  17. package/dist/src/commands/cp.js +4 -4
  18. package/dist/src/commands/cp.js.map +1 -1
  19. package/dist/src/commands/ls.d.ts +3 -3
  20. package/dist/src/commands/ls.d.ts.map +1 -1
  21. package/dist/src/commands/ls.js +1 -1
  22. package/dist/src/commands/ls.js.map +1 -1
  23. package/dist/src/commands/mkdir.d.ts +3 -3
  24. package/dist/src/commands/mkdir.d.ts.map +1 -1
  25. package/dist/src/commands/mkdir.js +4 -4
  26. package/dist/src/commands/mkdir.js.map +1 -1
  27. package/dist/src/commands/rm.d.ts +3 -3
  28. package/dist/src/commands/rm.d.ts.map +1 -1
  29. package/dist/src/commands/rm.js +3 -3
  30. package/dist/src/commands/rm.js.map +1 -1
  31. package/dist/src/commands/stat.d.ts +2 -2
  32. package/dist/src/commands/stat.js +2 -2
  33. package/dist/src/commands/touch.d.ts +3 -3
  34. package/dist/src/commands/touch.d.ts.map +1 -1
  35. package/dist/src/commands/touch.js +3 -3
  36. package/dist/src/commands/touch.js.map +1 -1
  37. package/dist/src/commands/utils/add-link.d.ts +4 -6
  38. package/dist/src/commands/utils/add-link.d.ts.map +1 -1
  39. package/dist/src/commands/utils/add-link.js +23 -17
  40. package/dist/src/commands/utils/add-link.js.map +1 -1
  41. package/dist/src/commands/utils/cid-to-directory.d.ts +1 -1
  42. package/dist/src/commands/utils/cid-to-directory.js +1 -1
  43. package/dist/src/commands/utils/cid-to-pblink.d.ts +1 -1
  44. package/dist/src/commands/utils/cid-to-pblink.js +1 -1
  45. package/dist/src/commands/utils/hamt-utils.d.ts +9 -8
  46. package/dist/src/commands/utils/hamt-utils.d.ts.map +1 -1
  47. package/dist/src/commands/utils/hamt-utils.js +18 -16
  48. package/dist/src/commands/utils/hamt-utils.js.map +1 -1
  49. package/dist/src/commands/utils/is-over-shard-threshold.d.ts +6 -2
  50. package/dist/src/commands/utils/is-over-shard-threshold.d.ts.map +1 -1
  51. package/dist/src/commands/utils/is-over-shard-threshold.js +58 -13
  52. package/dist/src/commands/utils/is-over-shard-threshold.js.map +1 -1
  53. package/dist/src/commands/utils/persist.d.ts +3 -3
  54. package/dist/src/commands/utils/persist.d.ts.map +1 -1
  55. package/dist/src/commands/utils/remove-link.d.ts +5 -6
  56. package/dist/src/commands/utils/remove-link.d.ts.map +1 -1
  57. package/dist/src/commands/utils/remove-link.js +10 -9
  58. package/dist/src/commands/utils/remove-link.js.map +1 -1
  59. package/dist/src/commands/utils/resolve.d.ts +1 -1
  60. package/dist/src/commands/utils/resolve.js +4 -4
  61. package/dist/src/index.d.ts +24 -54
  62. package/dist/src/index.d.ts.map +1 -1
  63. package/dist/src/index.js +3 -3
  64. package/dist/src/index.js.map +1 -1
  65. package/dist/src/unixfs.d.ts +1 -1
  66. package/dist/src/unixfs.js +9 -9
  67. package/dist/src/utils/glob-source.js +2 -2
  68. package/dist/src/utils/url-source.d.ts +1 -1
  69. package/dist/src/utils/url-source.js +1 -1
  70. package/package.json +4 -5
  71. package/src/commands/add.ts +19 -11
  72. package/src/commands/cat.ts +5 -5
  73. package/src/commands/chmod.ts +6 -6
  74. package/src/commands/cp.ts +7 -7
  75. package/src/commands/ls.ts +4 -4
  76. package/src/commands/mkdir.ts +7 -7
  77. package/src/commands/rm.ts +6 -6
  78. package/src/commands/stat.ts +4 -4
  79. package/src/commands/touch.ts +6 -6
  80. package/src/commands/utils/add-link.ts +30 -24
  81. package/src/commands/utils/cid-to-directory.ts +2 -2
  82. package/src/commands/utils/cid-to-pblink.ts +2 -2
  83. package/src/commands/utils/hamt-utils.ts +29 -23
  84. package/src/commands/utils/is-over-shard-threshold.ts +88 -13
  85. package/src/commands/utils/persist.ts +3 -3
  86. package/src/commands/utils/remove-link.ts +17 -17
  87. package/src/commands/utils/resolve.ts +5 -5
  88. package/src/index.ts +25 -61
  89. package/src/unixfs.ts +10 -10
  90. package/src/utils/glob-source.ts +2 -2
  91. package/src/utils/url-source.ts +2 -2
  92. package/dist/src/commands/utils/dir-sharded.d.ts +0 -74
  93. package/dist/src/commands/utils/dir-sharded.d.ts.map +0 -1
  94. package/dist/src/commands/utils/dir-sharded.js +0 -235
  95. package/dist/src/commands/utils/dir-sharded.js.map +0 -1
  96. package/src/commands/utils/dir-sharded.ts +0 -321
@@ -1,321 +0,0 @@
1
- import { encode, prepare } from '@ipld/dag-pb'
2
- import { createHAMT, Bucket } from 'hamt-sharding'
3
- import { UnixFS } from 'ipfs-unixfs'
4
- import { CID } from 'multiformats/cid'
5
- import {
6
- hamtHashCode,
7
- hamtHashFn
8
- } from './hamt-constants.js'
9
- import { persist } from './persist.js'
10
- import type { PersistOptions } from './persist.js'
11
- import type { PutStore } from '../../unixfs.js'
12
- import type { PBLink } from '@ipld/dag-pb'
13
- import type { BucketChild } from 'hamt-sharding'
14
- import type { Mtime } from 'ipfs-unixfs'
15
-
16
- interface InProgressImportResult extends ImportResult {
17
- single?: boolean
18
- originalPath?: string
19
- }
20
-
21
- interface ImportResult {
22
- cid: CID
23
- size: bigint
24
- path?: string
25
- unixfs?: UnixFS
26
- }
27
-
28
- interface DirProps {
29
- root: boolean
30
- dir: boolean
31
- path: string
32
- dirty: boolean
33
- flat: boolean
34
- parent?: Dir
35
- parentKey?: string
36
- unixfs?: UnixFS
37
- mode?: number
38
- mtime?: Mtime
39
- }
40
-
41
- abstract class Dir {
42
- public options: PersistOptions
43
- public root: boolean
44
- public dir: boolean
45
- public path: string
46
- public dirty: boolean
47
- public flat: boolean
48
- public parent?: Dir
49
- public parentKey?: string
50
- public unixfs?: UnixFS
51
- public mode?: number
52
- public mtime?: Mtime
53
- public cid?: CID
54
- public size?: number
55
- public nodeSize?: number
56
-
57
- constructor (props: DirProps, options: PersistOptions) {
58
- this.options = options ?? {}
59
-
60
- this.root = props.root
61
- this.dir = props.dir
62
- this.path = props.path
63
- this.dirty = props.dirty
64
- this.flat = props.flat
65
- this.parent = props.parent
66
- this.parentKey = props.parentKey
67
- this.unixfs = props.unixfs
68
- this.mode = props.mode
69
- this.mtime = props.mtime
70
- }
71
-
72
- abstract put (name: string, value: InProgressImportResult | Dir): Promise<void>
73
- abstract get (name: string): Promise<InProgressImportResult | Dir | undefined>
74
- abstract eachChildSeries (): AsyncIterable<{ key: string, child: InProgressImportResult | Dir }>
75
- abstract flush (blockstore: PutStore): AsyncGenerator<ImportResult>
76
- abstract estimateNodeSize (): number
77
- abstract childCount (): number
78
- }
79
-
80
- export class DirSharded extends Dir {
81
- public _bucket: Bucket<InProgressImportResult | Dir>
82
-
83
- constructor (props: DirProps, options: PersistOptions) {
84
- super(props, options)
85
-
86
- this._bucket = createHAMT({
87
- hashFn: hamtHashFn,
88
- bits: 8
89
- })
90
- }
91
-
92
- async put (name: string, value: InProgressImportResult | Dir): Promise<void> {
93
- this.cid = undefined
94
- this.size = undefined
95
- this.nodeSize = undefined
96
-
97
- await this._bucket.put(name, value)
98
- }
99
-
100
- async get (name: string): Promise<InProgressImportResult | Dir | undefined> {
101
- return this._bucket.get(name)
102
- }
103
-
104
- childCount (): number {
105
- return this._bucket.leafCount()
106
- }
107
-
108
- directChildrenCount (): number {
109
- return this._bucket.childrenCount()
110
- }
111
-
112
- onlyChild (): Bucket<InProgressImportResult | Dir> | BucketChild<InProgressImportResult | Dir> {
113
- return this._bucket.onlyChild()
114
- }
115
-
116
- async * eachChildSeries (): AsyncGenerator<{ key: string, child: InProgressImportResult | Dir }> {
117
- for (const { key, value } of this._bucket.eachLeafSeries()) {
118
- yield {
119
- key,
120
- child: value
121
- }
122
- }
123
- }
124
-
125
- estimateNodeSize (): number {
126
- if (this.nodeSize !== undefined) {
127
- return this.nodeSize
128
- }
129
-
130
- this.nodeSize = calculateSize(this._bucket, this, this.options)
131
-
132
- return this.nodeSize
133
- }
134
-
135
- async * flush (blockstore: PutStore): AsyncGenerator<ImportResult> {
136
- for await (const entry of flush(this._bucket, blockstore, this, this.options)) {
137
- yield {
138
- ...entry,
139
- path: this.path
140
- }
141
- }
142
- }
143
- }
144
-
145
- async function * flush (bucket: Bucket<Dir | InProgressImportResult>, blockstore: PutStore, shardRoot: DirSharded | null, options: PersistOptions): AsyncIterable<ImportResult> {
146
- const children = bucket._children
147
- const links: PBLink[] = []
148
- let childrenSize = 0n
149
-
150
- for (let i = 0; i < children.length; i++) {
151
- const child = children.get(i)
152
-
153
- if (child == null) {
154
- continue
155
- }
156
-
157
- const labelPrefix = i.toString(16).toUpperCase().padStart(2, '0')
158
-
159
- if (child instanceof Bucket) {
160
- let shard
161
-
162
- for await (const subShard of flush(child, blockstore, null, options)) {
163
- shard = subShard
164
- }
165
-
166
- if (shard == null) {
167
- throw new Error('Could not flush sharded directory, no sub-shard found')
168
- }
169
-
170
- links.push({
171
- Name: labelPrefix,
172
- Tsize: Number(shard.size),
173
- Hash: shard.cid
174
- })
175
- childrenSize += shard.size
176
- } else if (isDir(child.value)) {
177
- const dir = child.value
178
- let flushedDir: ImportResult | undefined
179
-
180
- for await (const entry of dir.flush(blockstore)) {
181
- flushedDir = entry
182
-
183
- yield flushedDir
184
- }
185
-
186
- if (flushedDir == null) {
187
- throw new Error('Did not flush dir')
188
- }
189
-
190
- const label = labelPrefix + child.key
191
- links.push({
192
- Name: label,
193
- Tsize: Number(flushedDir.size),
194
- Hash: flushedDir.cid
195
- })
196
-
197
- childrenSize += flushedDir.size
198
- } else {
199
- const value = child.value
200
-
201
- if (value.cid == null) {
202
- continue
203
- }
204
-
205
- const label = labelPrefix + child.key
206
- const size = value.size
207
-
208
- links.push({
209
- Name: label,
210
- Tsize: Number(size),
211
- Hash: value.cid
212
- })
213
- childrenSize += BigInt(size ?? 0)
214
- }
215
- }
216
-
217
- // go-ipfs uses little endian, that's why we have to
218
- // reverse the bit field before storing it
219
- const data = Uint8Array.from(children.bitField().reverse())
220
- const dir = new UnixFS({
221
- type: 'hamt-sharded-directory',
222
- data,
223
- fanout: BigInt(bucket.tableSize()),
224
- hashType: hamtHashCode,
225
- mtime: shardRoot?.mtime,
226
- mode: shardRoot?.mode
227
- })
228
-
229
- const node = {
230
- Data: dir.marshal(),
231
- Links: links
232
- }
233
- const buffer = encode(prepare(node))
234
- const cid = await persist(buffer, blockstore, options)
235
- const size = BigInt(buffer.byteLength) + childrenSize
236
-
237
- yield {
238
- cid,
239
- unixfs: dir,
240
- size
241
- }
242
- }
243
-
244
- function isDir (obj: any): obj is Dir {
245
- return typeof obj.flush === 'function'
246
- }
247
-
248
- function calculateSize (bucket: Bucket<any>, shardRoot: DirSharded | null, options: PersistOptions): number {
249
- const children = bucket._children
250
- const links: PBLink[] = []
251
-
252
- for (let i = 0; i < children.length; i++) {
253
- const child = children.get(i)
254
-
255
- if (child == null) {
256
- continue
257
- }
258
-
259
- const labelPrefix = i.toString(16).toUpperCase().padStart(2, '0')
260
-
261
- if (child instanceof Bucket) {
262
- const size = calculateSize(child, null, options)
263
-
264
- links.push({
265
- Name: labelPrefix,
266
- Tsize: Number(size),
267
- Hash: options.cidVersion === 0 ? CID_V0 : CID_V1
268
- })
269
- } else if (typeof child.value.flush === 'function') {
270
- const dir = child.value
271
- const size = dir.nodeSize()
272
-
273
- links.push({
274
- Name: labelPrefix + child.key,
275
- Tsize: Number(size),
276
- Hash: options.cidVersion === 0 ? CID_V0 : CID_V1
277
- })
278
- } else {
279
- const value = child.value
280
-
281
- if (value.cid == null) {
282
- continue
283
- }
284
-
285
- const label = labelPrefix + child.key
286
- const size = value.size
287
-
288
- links.push({
289
- Name: label,
290
- Tsize: Number(size),
291
- Hash: value.cid
292
- })
293
- }
294
- }
295
-
296
- // go-ipfs uses little endian, that's why we have to
297
- // reverse the bit field before storing it
298
- const data = Uint8Array.from(children.bitField().reverse())
299
- const dir = new UnixFS({
300
- type: 'hamt-sharded-directory',
301
- data,
302
- fanout: BigInt(bucket.tableSize()),
303
- hashType: hamtHashCode,
304
- mtime: shardRoot?.mtime,
305
- mode: shardRoot?.mode
306
- })
307
-
308
- const buffer = encode(prepare({
309
- Data: dir.marshal(),
310
- Links: links
311
- }))
312
-
313
- return buffer.length
314
- }
315
-
316
- // we use these to calculate the node size to use as a check for whether a directory
317
- // should be sharded or not. Since CIDs have a constant length and We're only
318
- // interested in the data length and not the actual content identifier we can use
319
- // any old CID instead of having to hash the data which is expensive.
320
- export const CID_V0 = CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
321
- export const CID_V1 = CID.parse('zdj7WbTaiJT1fgatdet9Ei9iDB5hdCxkbVyhyh8YTUnXMiwYi')