This repository was archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathadd.ts
65 lines (55 loc) · 1.97 KB
/
add.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import type { CID } from 'multiformats/cid'
import type { Blocks } from '@helia/interface/blocks'
import { ByteStream, DirectoryCandidate, FileCandidate, importBytes, importByteStream, ImportCandidateStream, importDirectory, importer, ImporterOptions, importFile, ImportResult } from 'ipfs-unixfs-importer'
import { balanced } from 'ipfs-unixfs-importer/layout'
import { fixedSize } from 'ipfs-unixfs-importer/chunker'
/**
* Default importer settings match Filecoin
*/
const defaultImporterSettings: ImporterOptions = {
cidVersion: 1,
rawLeaves: true,
layout: balanced({
maxChildrenPerNode: 1024
}),
chunker: fixedSize({
chunkSize: 1048576
})
}
export async function * addAll (source: ImportCandidateStream, blockstore: Blocks, options: Partial<ImporterOptions> = {}): AsyncGenerator<ImportResult, void, unknown> {
yield * importer(source, blockstore, {
...defaultImporterSettings,
...options
})
}
export async function addBytes (bytes: Uint8Array, blockstore: Blocks, options: Partial<ImporterOptions> = {}): Promise<CID> {
const { cid } = await importBytes(bytes, blockstore, {
...defaultImporterSettings,
...options
})
return cid
}
export async function addByteStream (bytes: ByteStream, blockstore: Blocks, options: Partial<ImporterOptions> = {}): Promise<CID> {
const { cid } = await importByteStream(bytes, blockstore, {
...defaultImporterSettings,
...options
})
return cid
}
export async function addFile (file: FileCandidate, blockstore: Blocks, options: Partial<ImporterOptions> = {}): Promise<CID> {
const { cid } = await importFile(file, blockstore, {
...defaultImporterSettings,
...options
})
return cid
}
export async function addDirectory (dir: Partial<DirectoryCandidate>, blockstore: Blocks, options: Partial<ImporterOptions> = {}): Promise<CID> {
const { cid } = await importDirectory({
...dir,
path: dir.path ?? '-'
}, blockstore, {
...defaultImporterSettings,
...options
})
return cid
}