@helia/unixfs 3.0.0 → 3.0.1

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 (76) hide show
  1. package/README.md +15 -0
  2. package/dist/index.min.js +1 -1
  3. package/dist/src/commands/add.d.ts +6 -6
  4. package/dist/src/commands/add.d.ts.map +1 -1
  5. package/dist/src/commands/add.js.map +1 -1
  6. package/dist/src/commands/cat.d.ts +2 -2
  7. package/dist/src/commands/cat.d.ts.map +1 -1
  8. package/dist/src/commands/cat.js.map +1 -1
  9. package/dist/src/commands/chmod.d.ts +2 -2
  10. package/dist/src/commands/chmod.d.ts.map +1 -1
  11. package/dist/src/commands/chmod.js.map +1 -1
  12. package/dist/src/commands/cp.d.ts +2 -2
  13. package/dist/src/commands/cp.d.ts.map +1 -1
  14. package/dist/src/commands/cp.js.map +1 -1
  15. package/dist/src/commands/ls.d.ts +2 -2
  16. package/dist/src/commands/ls.d.ts.map +1 -1
  17. package/dist/src/commands/ls.js.map +1 -1
  18. package/dist/src/commands/mkdir.d.ts +2 -2
  19. package/dist/src/commands/mkdir.d.ts.map +1 -1
  20. package/dist/src/commands/mkdir.js.map +1 -1
  21. package/dist/src/commands/rm.d.ts +2 -2
  22. package/dist/src/commands/rm.d.ts.map +1 -1
  23. package/dist/src/commands/rm.js.map +1 -1
  24. package/dist/src/commands/stat.d.ts +2 -2
  25. package/dist/src/commands/stat.d.ts.map +1 -1
  26. package/dist/src/commands/stat.js.map +1 -1
  27. package/dist/src/commands/touch.d.ts +2 -2
  28. package/dist/src/commands/touch.d.ts.map +1 -1
  29. package/dist/src/commands/touch.js.map +1 -1
  30. package/dist/src/commands/utils/add-link.d.ts +2 -2
  31. package/dist/src/commands/utils/add-link.d.ts.map +1 -1
  32. package/dist/src/commands/utils/add-link.js.map +1 -1
  33. package/dist/src/commands/utils/cid-to-directory.d.ts +2 -2
  34. package/dist/src/commands/utils/cid-to-directory.d.ts.map +1 -1
  35. package/dist/src/commands/utils/cid-to-directory.js.map +1 -1
  36. package/dist/src/commands/utils/cid-to-pblink.d.ts +2 -2
  37. package/dist/src/commands/utils/cid-to-pblink.d.ts.map +1 -1
  38. package/dist/src/commands/utils/cid-to-pblink.js.map +1 -1
  39. package/dist/src/commands/utils/dir-sharded.d.ts +3 -3
  40. package/dist/src/commands/utils/dir-sharded.d.ts.map +1 -1
  41. package/dist/src/commands/utils/dir-sharded.js.map +1 -1
  42. package/dist/src/commands/utils/hamt-utils.d.ts +4 -4
  43. package/dist/src/commands/utils/hamt-utils.d.ts.map +1 -1
  44. package/dist/src/commands/utils/hamt-utils.js.map +1 -1
  45. package/dist/src/commands/utils/is-over-shard-threshold.d.ts +2 -2
  46. package/dist/src/commands/utils/is-over-shard-threshold.d.ts.map +1 -1
  47. package/dist/src/commands/utils/is-over-shard-threshold.js.map +1 -1
  48. package/dist/src/commands/utils/remove-link.d.ts +2 -2
  49. package/dist/src/commands/utils/remove-link.d.ts.map +1 -1
  50. package/dist/src/commands/utils/remove-link.js.map +1 -1
  51. package/dist/src/commands/utils/resolve.d.ts +3 -3
  52. package/dist/src/commands/utils/resolve.d.ts.map +1 -1
  53. package/dist/src/commands/utils/resolve.js +8 -41
  54. package/dist/src/commands/utils/resolve.js.map +1 -1
  55. package/dist/src/index.d.ts +4 -3
  56. package/dist/src/index.d.ts.map +1 -1
  57. package/dist/src/index.js.map +1 -1
  58. package/package.json +19 -19
  59. package/src/commands/add.ts +6 -6
  60. package/src/commands/cat.ts +2 -2
  61. package/src/commands/chmod.ts +2 -2
  62. package/src/commands/cp.ts +2 -2
  63. package/src/commands/ls.ts +2 -2
  64. package/src/commands/mkdir.ts +2 -2
  65. package/src/commands/rm.ts +2 -2
  66. package/src/commands/stat.ts +3 -3
  67. package/src/commands/touch.ts +2 -2
  68. package/src/commands/utils/add-link.ts +5 -5
  69. package/src/commands/utils/cid-to-directory.ts +2 -2
  70. package/src/commands/utils/cid-to-pblink.ts +2 -2
  71. package/src/commands/utils/dir-sharded.ts +4 -4
  72. package/src/commands/utils/hamt-utils.ts +4 -4
  73. package/src/commands/utils/is-over-shard-threshold.ts +3 -3
  74. package/src/commands/utils/remove-link.ts +5 -5
  75. package/src/commands/utils/resolve.ts +12 -51
  76. package/src/index.ts +4 -3
@@ -7,7 +7,7 @@ import {
7
7
  hamtHashFn
8
8
  } from './hamt-constants.js'
9
9
  import { persist, type PersistOptions } from './persist.js'
10
- import type { Blocks } from '@helia/interface/blocks'
10
+ import type { Blockstore } from 'interface-blockstore'
11
11
  import type { Mtime } from 'ipfs-unixfs'
12
12
 
13
13
  interface InProgressImportResult extends ImportResult {
@@ -69,7 +69,7 @@ abstract class Dir {
69
69
  abstract put (name: string, value: InProgressImportResult | Dir): Promise<void>
70
70
  abstract get (name: string): Promise<InProgressImportResult | Dir | undefined>
71
71
  abstract eachChildSeries (): AsyncIterable<{ key: string, child: InProgressImportResult | Dir }>
72
- abstract flush (blockstore: Blocks): AsyncGenerator<ImportResult>
72
+ abstract flush (blockstore: Blockstore): AsyncGenerator<ImportResult>
73
73
  abstract estimateNodeSize (): number
74
74
  abstract childCount (): number
75
75
  }
@@ -129,7 +129,7 @@ export class DirSharded extends Dir {
129
129
  return this.nodeSize
130
130
  }
131
131
 
132
- async * flush (blockstore: Blocks): AsyncGenerator<ImportResult> {
132
+ async * flush (blockstore: Blockstore): AsyncGenerator<ImportResult> {
133
133
  for await (const entry of flush(this._bucket, blockstore, this, this.options)) {
134
134
  yield {
135
135
  ...entry,
@@ -139,7 +139,7 @@ export class DirSharded extends Dir {
139
139
  }
140
140
  }
141
141
 
142
- async function * flush (bucket: Bucket<Dir | InProgressImportResult>, blockstore: Blocks, shardRoot: DirSharded | null, options: PersistOptions): AsyncIterable<ImportResult> {
142
+ async function * flush (bucket: Bucket<Dir | InProgressImportResult>, blockstore: Blockstore, shardRoot: DirSharded | null, options: PersistOptions): AsyncIterable<ImportResult> {
143
143
  const children = bucket._children
144
144
  const links: PBLink[] = []
145
145
  let childrenSize = 0n
@@ -14,8 +14,8 @@ import {
14
14
  } from './hamt-constants.js'
15
15
  import { persist } from './persist.js'
16
16
  import type { PersistOptions } from './persist.js'
17
- import type { Blocks } from '@helia/interface/blocks'
18
17
  import type { AbortOptions } from '@libp2p/interface'
18
+ import type { Blockstore } from 'interface-blockstore'
19
19
  import type { Mtime } from 'ipfs-unixfs'
20
20
  import type { ImportResult } from 'ipfs-unixfs-importer'
21
21
  import type { CID, Version } from 'multiformats/cid'
@@ -40,7 +40,7 @@ export interface CreateShardOptions {
40
40
  cidVersion: Version
41
41
  }
42
42
 
43
- export const createShard = async (blockstore: Blocks, contents: Array<{ name: string, size: bigint, cid: CID }>, options: CreateShardOptions): Promise<ImportResult> => {
43
+ export const createShard = async (blockstore: Blockstore, contents: Array<{ name: string, size: bigint, cid: CID }>, options: CreateShardOptions): Promise<ImportResult> => {
44
44
  const shard = new DirSharded({
45
45
  root: true,
46
46
  dir: true,
@@ -75,7 +75,7 @@ export interface HAMTPath {
75
75
  node: dagPB.PBNode
76
76
  }
77
77
 
78
- export const updateShardedDirectory = async (path: HAMTPath[], blockstore: Blocks, options: PersistOptions): Promise<{ cid: CID, node: dagPB.PBNode }> => {
78
+ export const updateShardedDirectory = async (path: HAMTPath[], blockstore: Blockstore, options: PersistOptions): Promise<{ cid: CID, node: dagPB.PBNode }> => {
79
79
  // persist any metadata on the shard root
80
80
  const shardRoot = UnixFS.unmarshal(path[0].node.Data ?? new Uint8Array(0))
81
81
 
@@ -142,7 +142,7 @@ export const updateShardedDirectory = async (path: HAMTPath[], blockstore: Block
142
142
  return { cid, node }
143
143
  }
144
144
 
145
- export const recreateShardedDirectory = async (cid: CID, fileName: string, blockstore: Blocks, options: AbortOptions): Promise<{ path: HAMTPath[], hash: InfiniteHash }> => {
145
+ export const recreateShardedDirectory = async (cid: CID, fileName: string, blockstore: Blockstore, options: AbortOptions): Promise<{ path: HAMTPath[], hash: InfiniteHash }> => {
146
146
  const wrapped = wrapHash(hamtHashFn)
147
147
  const hash = wrapped(uint8ArrayFromString(fileName))
148
148
  const path: HAMTPath[] = []
@@ -1,9 +1,9 @@
1
1
  import * as dagPb from '@ipld/dag-pb'
2
2
  import { UnixFS } from 'ipfs-unixfs'
3
3
  import { CID_V0, CID_V1 } from './dir-sharded.js'
4
- import type { Blocks } from '@helia/interface/blocks'
5
4
  import type { PBNode } from '@ipld/dag-pb'
6
5
  import type { AbortOptions } from '@libp2p/interface'
6
+ import type { Blockstore } from 'interface-blockstore'
7
7
 
8
8
  /**
9
9
  * Estimate node size only based on DAGLink name and CID byte lengths
@@ -11,7 +11,7 @@ import type { AbortOptions } from '@libp2p/interface'
11
11
  *
12
12
  * If the node is a hamt sharded directory the calculation is based on if it was a regular directory.
13
13
  */
14
- export async function isOverShardThreshold (node: PBNode, blockstore: Blocks, threshold: number, options: AbortOptions): Promise<boolean> {
14
+ export async function isOverShardThreshold (node: PBNode, blockstore: Blockstore, threshold: number, options: AbortOptions): Promise<boolean> {
15
15
  if (node.Data == null) {
16
16
  throw new Error('DagPB node had no data')
17
17
  }
@@ -43,7 +43,7 @@ function estimateNodeSize (node: PBNode): number {
43
43
  return size
44
44
  }
45
45
 
46
- async function estimateShardSize (node: PBNode, current: number, max: number, blockstore: Blocks, options: AbortOptions): Promise<number> {
46
+ async function estimateShardSize (node: PBNode, current: number, max: number, blockstore: Blockstore, options: AbortOptions): Promise<number> {
47
47
  if (current > max) {
48
48
  return max
49
49
  }
@@ -11,9 +11,9 @@ import {
11
11
  import { isOverShardThreshold } from './is-over-shard-threshold.js'
12
12
  import { persist } from './persist.js'
13
13
  import type { Directory } from './cid-to-directory.js'
14
- import type { Blocks } from '@helia/interface/blocks'
15
14
  import type { PBNode } from '@ipld/dag-pb'
16
15
  import type { AbortOptions } from '@libp2p/interface'
16
+ import type { Blockstore } from 'interface-blockstore'
17
17
  import type { CID, Version } from 'multiformats/cid'
18
18
 
19
19
  const log = logger('helia:unixfs:utils:remove-link')
@@ -28,7 +28,7 @@ export interface RemoveLinkResult {
28
28
  cid: CID
29
29
  }
30
30
 
31
- export async function removeLink (parent: Directory, name: string, blockstore: Blocks, options: RmLinkOptions): Promise<RemoveLinkResult> {
31
+ export async function removeLink (parent: Directory, name: string, blockstore: Blockstore, options: RmLinkOptions): Promise<RemoveLinkResult> {
32
32
  if (parent.node.Data == null) {
33
33
  throw new InvalidPBNodeError('Parent node had no data')
34
34
  }
@@ -54,7 +54,7 @@ export async function removeLink (parent: Directory, name: string, blockstore: B
54
54
  return removeFromDirectory(parent, name, blockstore, options)
55
55
  }
56
56
 
57
- const removeFromDirectory = async (parent: Directory, name: string, blockstore: Blocks, options: AbortOptions): Promise<RemoveLinkResult> => {
57
+ const removeFromDirectory = async (parent: Directory, name: string, blockstore: Blockstore, options: AbortOptions): Promise<RemoveLinkResult> => {
58
58
  // Remove existing link if it exists
59
59
  parent.node.Links = parent.node.Links.filter((link) => {
60
60
  return link.Name !== name
@@ -74,7 +74,7 @@ const removeFromDirectory = async (parent: Directory, name: string, blockstore:
74
74
  }
75
75
  }
76
76
 
77
- const removeFromShardedDirectory = async (parent: Directory, name: string, blockstore: Blocks, options: UpdateHamtDirectoryOptions): Promise<{ cid: CID, node: PBNode }> => {
77
+ const removeFromShardedDirectory = async (parent: Directory, name: string, blockstore: Blockstore, options: UpdateHamtDirectoryOptions): Promise<{ cid: CID, node: PBNode }> => {
78
78
  const { path } = await recreateShardedDirectory(parent.cid, name, blockstore, options)
79
79
  const finalSegment = path[path.length - 1]
80
80
 
@@ -131,7 +131,7 @@ const removeFromShardedDirectory = async (parent: Directory, name: string, block
131
131
  return updateShardedDirectory(path, blockstore, options)
132
132
  }
133
133
 
134
- const convertToFlatDirectory = async (parent: Directory, blockstore: Blocks, options: RmLinkOptions): Promise<RemoveLinkResult> => {
134
+ const convertToFlatDirectory = async (parent: Directory, blockstore: Blockstore, options: RmLinkOptions): Promise<RemoveLinkResult> => {
135
135
  if (parent.node.Data == null) {
136
136
  throw new InvalidParametersError('Invalid parent passed to convertToFlatDirectory')
137
137
  }
@@ -1,11 +1,12 @@
1
1
  import { logger } from '@libp2p/logger'
2
- import { exporter } from 'ipfs-unixfs-exporter'
3
- import { DoesNotExistError, InvalidParametersError } from '../../errors.js'
2
+ import { walkPath } from 'ipfs-unixfs-exporter'
3
+ import all from 'it-all'
4
+ import { DoesNotExistError } from '../../errors.js'
4
5
  import { addLink } from './add-link.js'
5
6
  import { cidToDirectory } from './cid-to-directory.js'
6
7
  import { cidToPBLink } from './cid-to-pblink.js'
7
- import type { Blocks } from '@helia/interface/blocks'
8
8
  import type { AbortOptions } from '@libp2p/interface'
9
+ import type { Blockstore } from 'interface-blockstore'
9
10
  import type { CID } from 'multiformats/cid'
10
11
 
11
12
  const log = logger('helia:unixfs:components:utils:resolve')
@@ -32,62 +33,22 @@ export interface ResolveResult {
32
33
  segments?: Segment[]
33
34
  }
34
35
 
35
- export async function resolve (cid: CID, path: string | undefined, blockstore: Blocks, options: AbortOptions): Promise<ResolveResult> {
36
+ export async function resolve (cid: CID, path: string | undefined, blockstore: Blockstore, options: AbortOptions): Promise<ResolveResult> {
36
37
  if (path == null || path === '') {
37
38
  return { cid }
38
39
  }
39
40
 
40
- log('resolve "%s" under %c', path, cid)
41
-
42
- const parts = path.split('/').filter(Boolean)
43
- const segments: Segment[] = [{
44
- name: '',
45
- cid,
46
- size: 0n
47
- }]
48
-
49
- for (let i = 0; i < parts.length; i++) {
50
- const part = parts[i]
51
- const result = await exporter(cid, blockstore, options)
52
-
53
- log('resolving "%s"', part, result)
54
-
55
- if (result.type === 'file') {
56
- if (i < parts.length - 1) {
57
- throw new InvalidParametersError('Path was invalid')
58
- }
59
-
60
- cid = result.cid
61
- } else if (result.type === 'directory') {
62
- let dirCid: CID | undefined
63
-
64
- for await (const entry of result.content()) {
65
- if (entry.name === part) {
66
- dirCid = entry.cid
67
- break
68
- }
69
- }
70
-
71
- if (dirCid == null) {
72
- throw new DoesNotExistError('Could not find path in directory')
73
- }
74
-
75
- cid = dirCid
76
-
77
- segments.push({
78
- name: part,
79
- cid,
80
- size: result.size
81
- })
82
- } else {
83
- throw new InvalidParametersError('Could not resolve path')
84
- }
41
+ const p = `/ipfs/${cid}${path == null ? '' : `/${path}`}`
42
+ const segments = await all(walkPath(p, blockstore, options))
43
+
44
+ if (segments.length === 0) {
45
+ throw new DoesNotExistError('Could not find path in directory')
85
46
  }
86
47
 
87
48
  log('resolved %s to %c', path, cid)
88
49
 
89
50
  return {
90
- cid,
51
+ cid: segments[segments.length - 1].cid,
91
52
  path,
92
53
  segments
93
54
  }
@@ -101,7 +62,7 @@ export interface UpdatePathCidsOptions extends AbortOptions {
101
62
  * Where we have descended into a DAG to update a child node, ascend up the DAG creating
102
63
  * new hashes and blocks for the changed content
103
64
  */
104
- export async function updatePathCids (cid: CID, result: ResolveResult, blockstore: Blocks, options: UpdatePathCidsOptions): Promise<CID> {
65
+ export async function updatePathCids (cid: CID, result: ResolveResult, blockstore: Blockstore, options: UpdatePathCidsOptions): Promise<CID> {
105
66
  if (result.segments == null || result.segments.length === 0) {
106
67
  return cid
107
68
  }
package/src/index.ts CHANGED
@@ -52,8 +52,9 @@ import { mkdir } from './commands/mkdir.js'
52
52
  import { rm } from './commands/rm.js'
53
53
  import { stat } from './commands/stat.js'
54
54
  import { touch } from './commands/touch.js'
55
- import type { Blocks, GetBlockProgressEvents, PutBlockProgressEvents } from '@helia/interface/blocks'
55
+ import type { GetBlockProgressEvents, PutBlockProgressEvents } from '@helia/interface/blocks'
56
56
  import type { AbortOptions } from '@libp2p/interface'
57
+ import type { Blockstore } from 'interface-blockstore'
57
58
  import type { Mtime, UnixFS as IPFSUnixFS } from 'ipfs-unixfs'
58
59
  import type { ExporterProgressEvents, UnixFSEntry } from 'ipfs-unixfs-exporter'
59
60
  import type { ByteStream, DirectoryCandidate, FileCandidate, ImportCandidateStream, ImporterOptions, ImporterProgressEvents, ImportResult } from 'ipfs-unixfs-importer'
@@ -61,7 +62,7 @@ import type { CID, Version } from 'multiformats/cid'
61
62
  import type { ProgressOptions } from 'progress-events'
62
63
 
63
64
  export interface UnixFSComponents {
64
- blockstore: Blocks
65
+ blockstore: Blockstore
65
66
  }
66
67
 
67
68
  export type AddEvents = PutBlockProgressEvents
@@ -616,7 +617,7 @@ class DefaultUnixFS implements UnixFS {
616
617
  /**
617
618
  * Create a {@link UnixFS} instance for use with {@link https://github.com/ipfs/helia Helia}
618
619
  */
619
- export function unixfs (helia: { blockstore: Blocks }): UnixFS {
620
+ export function unixfs (helia: { blockstore: Blockstore }): UnixFS {
620
621
  return new DefaultUnixFS(helia)
621
622
  }
622
623