Skip to content

fix: dag.import to support ReadableStream #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dag/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export interface DAGAPI {
* Import all blocks from one or more CARs and optionally recursively pin the roots identified
* within the CARs.
*/
import(sources: Iterable<Uint8Array> | AsyncIterable<Uint8Array> | AsyncIterable<AsyncIterable<Uint8Array>> | Iterable<AsyncIterable<Uint8Array>>, options?: DAGImportOptions): AsyncIterable<DAGImportResult>
import(sources: ReadableStream<Uint8Array> | Iterable<Uint8Array> | AsyncIterable<Uint8Array> | AsyncIterable<AsyncIterable<Uint8Array>> | Iterable<AsyncIterable<Uint8Array>>, options?: DAGImportOptions): AsyncIterable<DAGImportResult>
}

export function createDAG (client: HTTPRPCClient, codecs: Codecs): DAGAPI {
Expand Down
35 changes: 35 additions & 0 deletions test/interface-tests/src/dag/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ async function createCar (blocks: Array<{ cid: CID, bytes: Uint8Array }>): Promi
return out
}

async function createReadableStreamFromCar (car: AsyncIterable<Uint8Array>): Promise<ReadableStream> {
const stream = new ReadableStream({
async start (controller) {
try {
for await (const chunk of car) {
controller.enqueue(chunk)
}
controller.close()
} catch (err) {
controller.error(err)
}
}
})

return stream
}

/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
Expand Down Expand Up @@ -179,5 +196,23 @@ export function testImport (factory: Factory<KuboNode>, options: MochaConfig): v
const result = await all(ipfs.dag.import(async function * () { yield input }()))
expect(result).to.have.deep.nested.property('[0].root.cid', cids[0])
})

it('should be able to import car file as a ReadableStream', async () => {
const blocks = await createBlocks(5)
const car = await createCar(blocks)

const stream = await createReadableStreamFromCar(car)

const result = await all(ipfs.dag.import(stream))
expect(result).to.have.lengthOf(1)
expect(result).to.have.deep.nested.property('[0].root.cid', blocks[0].cid)

for (const { cid } of blocks) {
await expect(ipfs.block.get(cid)).to.eventually.be.ok()
}

await expect(all(ipfs.pin.ls({ paths: blocks[0].cid }))).to.eventually.have.lengthOf(1)
.and.have.nested.property('[0].type', 'recursive')
})
})
}