-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathdir.ts
47 lines (42 loc) · 1.02 KB
/
dir.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { encode, prepare } from '@ipld/dag-pb'
import { UnixFS } from 'ipfs-unixfs'
import { persist } from '../utils/persist.js'
import type {
Directory,
InProgressImportResult,
WritableStorage
} from '../index.js'
import type { Version } from 'multiformats/cid'
export interface DirBuilderOptions {
cidVersion: Version
signal?: AbortSignal
}
export interface DirBuilder {
(
dir: Directory,
blockstore: WritableStorage,
options: DirBuilderOptions
): Promise<InProgressImportResult>
}
export const defaultDirBuilder = async (
dir: Directory,
blockstore: WritableStorage,
options: DirBuilderOptions
): Promise<InProgressImportResult> => {
const unixfs = new UnixFS({
type: 'directory',
mtime: dir.mtime,
mode: dir.mode
})
const block = encode(prepare({ Data: unixfs.marshal() }))
const cid = await persist(block, blockstore, options)
const path = dir.path
return {
cid,
path,
unixfs,
size: BigInt(block.length),
originalPath: dir.originalPath,
block
}
}