From ed5666ec8dd4c6cf7a9c604876ab2d91ae370303 Mon Sep 17 00:00:00 2001 From: Yuttakhan Baingen Date: Sat, 8 Mar 2025 12:11:33 +0700 Subject: [PATCH 1/3] fix: add type support --- src/dag/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dag/index.ts b/src/dag/index.ts index d0e09b8cc..64889105e 100644 --- a/src/dag/index.ts +++ b/src/dag/index.ts @@ -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 | AsyncIterable | AsyncIterable> | Iterable>, options?: DAGImportOptions): AsyncIterable + import(sources: ReadableStream | Iterable | AsyncIterable | AsyncIterable> | Iterable>, options?: DAGImportOptions): AsyncIterable } export function createDAG (client: HTTPRPCClient, codecs: Codecs): DAGAPI { From 3dff1ac351db76268cc9fcd898a0a4f55aebb2fb Mon Sep 17 00:00:00 2001 From: Yuttakhan Baingen Date: Sun, 9 Mar 2025 10:18:13 +0700 Subject: [PATCH 2/3] test: add test on import car file as ReadableStream --- test/interface-tests/src/dag/import.ts | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/interface-tests/src/dag/import.ts b/test/interface-tests/src/dag/import.ts index 6927a56ff..5da0317b3 100644 --- a/test/interface-tests/src/dag/import.ts +++ b/test/interface-tests/src/dag/import.ts @@ -44,6 +44,23 @@ async function createCar (blocks: Array<{ cid: CID, bytes: Uint8Array }>): Promi return out } +async function createReadableStreamFromCar(car: AsyncIterable): Promise { + 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 */ @@ -179,5 +196,23 @@ export function testImport (factory: Factory, 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') + }) }) } From 50d7f96e1cd61760a9132c9abcf2db815bed1e95 Mon Sep 17 00:00:00 2001 From: Yuttakhan Baingen Date: Sun, 9 Mar 2025 10:57:20 +0700 Subject: [PATCH 3/3] lint: Fix linting --- test/interface-tests/src/dag/import.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/interface-tests/src/dag/import.ts b/test/interface-tests/src/dag/import.ts index 5da0317b3..6687030ce 100644 --- a/test/interface-tests/src/dag/import.ts +++ b/test/interface-tests/src/dag/import.ts @@ -44,7 +44,7 @@ async function createCar (blocks: Array<{ cid: CID, bytes: Uint8Array }>): Promi return out } -async function createReadableStreamFromCar(car: AsyncIterable): Promise { +async function createReadableStreamFromCar (car: AsyncIterable): Promise { const stream = new ReadableStream({ async start (controller) { try {