-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathindex.ts
265 lines (242 loc) · 7.93 KB
/
index.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* @packageDocumentation
*
* `@helia/car` provides `import` and `export` methods to read/write Car files to {@link https://github.com/ipfs/helia Helia}'s blockstore.
*
* See the {@link Car} interface for all available operations.
*
* By default it supports `dag-pb`, `dag-cbor`, `dag-json` and `raw` CIDs, more esoteric DAG walkers can be passed as an init option.
*
* @example Exporting a DAG as a CAR file
*
* ```typescript
* import { createHelia } from 'helia'
* import { unixfs } from '@helia/unixfs'
* import { car } from '@helia/car'
* import { CarWriter } from '@ipld/car'
* import { Readable } from 'node:stream'
* import nodeFs from 'node:fs'
*
* const helia = await createHelia({
* // ... helia config
* })
* const fs = unixfs(helia)
*
* // add some UnixFS data
* const cid = await fs.addBytes(Uint8Array.from([0, 1, 2, 3, 4]))
*
* // export it as a Car
* const c = car(helia)
* const { writer, out } = await CarWriter.create(cid)
*
* // `out` needs to be directed somewhere, see the @ipld/car docs for more information
* Readable.from(out).pipe(nodeFs.createWriteStream('example.car'))
*
* // write the DAG behind `cid` into the writer
* await c.export(cid, writer)
* ```
*
* @example Importing all blocks from a CAR file
*
* ```typescript
* import { createHelia } from 'helia'
* import { unixfs } from '@helia/unixfs'
* import { car } from '@helia/car'
* import { CarReader } from '@ipld/car'
* import { Readable } from 'node:stream'
* import nodeFs from 'node:fs'
*
* const helia = await createHelia({
* // ... helia config
* })
*
* // import the car
* const inStream = nodeFs.createReadStream('example.car')
* const reader = await CarReader.fromIterable(inStream)
*
* const c = car(helia)
* await c.import(reader)
* ```
*/
import { CarWriter } from '@ipld/car'
import drain from 'it-drain'
import map from 'it-map'
import { createUnsafe } from 'multiformats/block'
import defer from 'p-defer'
import PQueue from 'p-queue'
import type { CodecLoader } from '@helia/interface'
import type { GetBlockProgressEvents, PutManyBlocksProgressEvents } from '@helia/interface/blocks'
import type { CarReader } from '@ipld/car'
import type { AbortOptions } from '@libp2p/interface'
import type { Filter } from '@libp2p/utils/filters'
import type { Blockstore } from 'interface-blockstore'
import type { CID } from 'multiformats/cid'
import type { ProgressOptions } from 'progress-events'
export interface CarComponents {
blockstore: Blockstore
getCodec: CodecLoader
}
export interface ExportCarOptions extends AbortOptions, ProgressOptions<GetBlockProgressEvents> {
/**
* If a filter is passed it will be used to deduplicate blocks exported in the car file
*/
blockFilter?: Filter
}
/**
* The Car interface provides operations for importing and exporting Car files
* from Helia's underlying blockstore.
*/
export interface Car {
/**
* Add all blocks in the passed CarReader to the blockstore.
*
* @example
*
* ```typescript
* import fs from 'node:fs'
* import { createHelia } from 'helia'
* import { car } from '@helia/car
* import { CarReader } from '@ipld/car'
*
* const helia = await createHelia()
*
* const inStream = fs.createReadStream('example.car')
* const reader = await CarReader.fromIterable(inStream)
*
* const c = car(helia)
* await c.import(reader)
* ```
*/
import(reader: Pick<CarReader, 'blocks'>, options?: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents>): Promise<void>
/**
* Store all blocks that make up one or more DAGs in a car file.
*
* @example
*
* ```typescript
* import fs from 'node:fs'
* import { Readable } from 'node:stream'
* import { car } from '@helia/car'
* import { CarWriter } from '@ipld/car'
* import { createHelia } from 'helia'
* import { CID } from 'multiformats/cid'
* import { pEvent } from 'p-event'
*
* const helia = await createHelia()
* const cid = CID.parse('QmFoo...')
*
* const c = car(helia)
* const { writer, out } = CarWriter.create(cid)
* const output = fs.createWriteStream('example.car')
* const stream = Readable.from(out).pipe(output)
*
* await Promise.all([
* c.export(cid, writer),
* pEvent(stream, 'close')
* ])
* ```
*
* @deprecated Use `stream` instead. In a future release `stream` will be renamed `export`.
*/
export(root: CID | CID[], writer: Pick<CarWriter, 'put' | 'close'>, options?: ExportCarOptions): Promise<void>
/**
* Returns an AsyncGenerator that yields CAR file bytes.
*
* @example
*
* ```typescript
* import { createHelia } from 'helia'
* import { car } from '@helia/car
* import { CID } from 'multiformats/cid'
*
* const helia = await createHelia()
* const cid = CID.parse('QmFoo...')
*
* const c = car(helia)
*
* for (const buf of c.stream(cid)) {
* // store or send `buf` somewhere
* }
* ```
*/
stream(root: CID | CID[], options?: AbortOptions & ProgressOptions<GetBlockProgressEvents>): AsyncGenerator<Uint8Array>
}
const DAG_WALK_QUEUE_CONCURRENCY = 1
class DefaultCar implements Car {
private readonly components: CarComponents
constructor (components: CarComponents, init: any) {
this.components = components
}
async import (reader: Pick<CarReader, 'blocks'>, options?: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents>): Promise<void> {
await drain(this.components.blockstore.putMany(
map(reader.blocks(), ({ cid, bytes }) => ({ cid, block: bytes })),
options
))
}
async export (root: CID | CID[], writer: Pick<CarWriter, 'put' | 'close'>, options?: ExportCarOptions): Promise<void> {
const deferred = defer<Error | undefined>()
const roots = Array.isArray(root) ? root : [root]
// use a queue to walk the DAG instead of recursion so we can traverse very large DAGs
const queue = new PQueue({
concurrency: DAG_WALK_QUEUE_CONCURRENCY
})
queue.on('idle', () => {
deferred.resolve()
})
queue.on('error', (err) => {
queue.clear()
deferred.reject(err)
})
for (const root of roots) {
void queue.add(async () => {
await this.#walkDag(root, queue, async (cid, bytes) => {
// if a filter has been passed, skip blocks that have already been written
if (options?.blockFilter?.has(cid.multihash.bytes) === true) {
return
}
options?.blockFilter?.add(cid.multihash.bytes)
await writer.put({ cid, bytes })
}, options)
})
.catch(() => {})
}
// wait for the writer to end
try {
await deferred.promise
} finally {
await writer.close()
}
}
async * stream (root: CID | CID[], options?: ExportCarOptions): AsyncGenerator<Uint8Array, void, undefined> {
const { writer, out } = CarWriter.create(root)
// has to be done async so we write to `writer` and read from `out` at the
// same time
this.export(root, writer, options)
.catch(() => {})
for await (const buf of out) {
yield buf
}
}
/**
* Walk the DAG behind the passed CID, ensure all blocks are present in the blockstore
* and update the pin count for them
*/
async #walkDag (cid: CID, queue: PQueue, withBlock: (cid: CID, block: Uint8Array) => Promise<void>, options?: AbortOptions & ProgressOptions<GetBlockProgressEvents>): Promise<void> {
const codec = await this.components.getCodec(cid.code)
const bytes = await this.components.blockstore.get(cid, options)
await withBlock(cid, bytes)
const block = createUnsafe({ bytes, cid, codec })
// walk dag, ensure all blocks are present
for await (const [,cid] of block.links()) {
void queue.add(async () => {
await this.#walkDag(cid, queue, withBlock, options)
})
}
}
}
/**
* Create a {@link Car} instance for use with {@link https://github.com/ipfs/helia Helia}
*/
export function car (helia: CarComponents, init: any = {}): Car {
return new DefaultCar(helia, init)
}