-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: stream car file bytes from @helia/car (#444)
To better support streaming CAR files with a less confusing API, add a method to `@helia/car` that takes root CIDs and returns an AsyncGenerator that yields CAR file bytes.
- Loading branch information
1 parent
8db7792
commit 7c07e11
Showing
4 changed files
with
106 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as dagPb from '@ipld/dag-pb' | ||
import * as raw from 'multiformats/codecs/raw' | ||
import type { DAGWalker } from '@helia/interface' | ||
|
||
/** | ||
* Dag walker for dag-pb CIDs | ||
*/ | ||
const dagPbWalker: DAGWalker = { | ||
codec: dagPb.code, | ||
* walk (block) { | ||
const node = dagPb.decode(block) | ||
|
||
yield * node.Links.map(l => l.Hash) | ||
} | ||
} | ||
|
||
const rawWalker: DAGWalker = { | ||
codec: raw.code, | ||
* walk () { | ||
// no embedded CIDs in a raw block | ||
} | ||
} | ||
|
||
export const dagWalkers = { | ||
[dagPb.code]: dagPbWalker, | ||
[raw.code]: rawWalker | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* eslint-env mocha */ | ||
|
||
import { type UnixFS, unixfs } from '@helia/unixfs' | ||
import { expect } from 'aegir/chai' | ||
import { MemoryBlockstore } from 'blockstore-core' | ||
import toBuffer from 'it-to-buffer' | ||
import { car, type Car } from '../src/index.js' | ||
import { dagWalkers } from './fixtures/dag-walkers.js' | ||
import { smallFile } from './fixtures/files.js' | ||
import { memoryCarWriter } from './fixtures/memory-car.js' | ||
import type { Blockstore } from 'interface-blockstore' | ||
|
||
describe('stream car file', () => { | ||
let blockstore: Blockstore | ||
let c: Car | ||
let u: UnixFS | ||
|
||
beforeEach(async () => { | ||
blockstore = new MemoryBlockstore() | ||
|
||
c = car({ blockstore, dagWalkers }) | ||
u = unixfs({ blockstore }) | ||
}) | ||
|
||
it('streams car file', async () => { | ||
const cid = await u.addBytes(smallFile) | ||
|
||
const writer = memoryCarWriter(cid) | ||
await c.export(cid, writer) | ||
|
||
const bytes = await writer.bytes() | ||
|
||
const streamed = await toBuffer(c.stream(cid)) | ||
|
||
expect(bytes).to.equalBytes(streamed) | ||
}) | ||
}) |