Skip to content

Commit

Permalink
chore: reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Feb 21, 2024
1 parent 749d391 commit 1dded04
Showing 1 changed file with 7 additions and 34 deletions.
41 changes: 7 additions & 34 deletions packages/car/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,48 +206,21 @@ class DefaultCar implements Car {

async * stream (root: CID | CID[], options?: AbortOptions & ProgressOptions<GetBlockProgressEvents>): AsyncGenerator<Uint8Array> {
const { writer, out } = CarWriter.create(root)
const deferred = defer()
const roots = Array.isArray(root) ? root : [root]
let exportError: Error | undefined

// 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) => {
deferred.reject(err)
})

for (const root of roots) {
void queue.add(async () => {
await this.#walkDag(root, queue, async (cid, bytes) => {
await writer.put({ cid, bytes })
}, options)
})
}

let writerError: Error | undefined

// end the writer after the queue ends
deferred.promise
.then(async () => {
await writer.close()
}, async (err) => {
await writer.close()
throw err
})
this.export(root, writer, options)
.catch((err) => {
writerError = err
exportError = err

Check warning on line 213 in packages/car/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/car/src/index.ts#L213

Added line #L213 was not covered by tests
})

// out is AsyncIterable<Uint8Array> not AsyncIterator<Uint8Array> so we
// can't just `yield * out`
for await (const buf of out) {
yield buf
}

if (writerError != null) {
throw writerError
if (exportError != null) {
throw exportError
}

Check warning on line 224 in packages/car/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/car/src/index.ts#L223-L224

Added lines #L223 - L224 were not covered by tests
}

Expand Down

0 comments on commit 1dded04

Please sign in to comment.