This repository was archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: create car file from a complete graph #3
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
91dad19
feat: create car file from a complete graph
mikeal aa14a74
fix: raw blocks don't have a reader
mikeal 6967eef
fix: use base58 for CIDv0 support
mikeal ab74f4e
feat: optional concurrency
mikeal 9500eba
fix: expose concurrency option
mikeal 01addc2
fix: style, coverage, docs for completeGraph
rvagg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,79 @@ | ||
/* eslint-env mocha */ | ||
|
||
const assert = require('assert') | ||
const { writeStream, readBuffer, completeGraph } = require('../') | ||
const Block = require('@ipld/block') | ||
const { PassThrough } = require('stream') | ||
|
||
const same = assert.deepStrictEqual | ||
|
||
function all (car) { | ||
const _traverse = async function * (link, seen = new Set()) { | ||
link = await link | ||
seen.add(link.toString('base64')) | ||
const encoded = await car.get(link) | ||
const block = Block.create(encoded, link) | ||
yield block | ||
const cid = await block.cid() | ||
if (cid.codec === 'raw') { | ||
return | ||
} | ||
|
||
for (const [, link] of block.reader().links()) { | ||
if (seen.has(link.toString('base64'))) { | ||
continue | ||
} | ||
yield * _traverse(link, seen) | ||
} | ||
} | ||
|
||
return _traverse(car.getRoots().then(([root]) => root)) | ||
} | ||
|
||
async function createGet (blocks) { | ||
const db = new Map() | ||
for (const block of blocks) { | ||
db.set((await block.cid()).toString('base64'), block) | ||
} | ||
return (cid) => new Promise((resolve) => resolve(db.get(cid.toString('base64')))) | ||
} | ||
|
||
async function concat (stream) { | ||
const buffers = [] | ||
for await (const buffer of stream) { | ||
buffers.push(buffer) | ||
} | ||
return Buffer.concat(buffers) | ||
} | ||
|
||
describe('Create car for full graph', () => { | ||
it('small graph', async () => { | ||
const leaf1 = Block.encoder({ hello: 'world' }, 'dag-cbor') | ||
const leaf2 = Block.encoder({ test: 1 }, 'dag-cbor') | ||
const raw = Block.encoder(Buffer.from('test'), 'raw') | ||
const root = Block.encoder( | ||
{ | ||
one: await leaf1.cid(), | ||
two: await leaf2.cid(), | ||
three: await leaf1.cid(), | ||
zraw: await raw.cid() | ||
}, | ||
'dag-cbor') | ||
const expected = [root, leaf1, leaf2, raw] | ||
const get = await createGet(expected) | ||
const stream = new PassThrough() | ||
const car = await writeStream(stream) | ||
await completeGraph(await root.cid(), get, car) | ||
const data = await concat(stream) | ||
|
||
const reader = await readBuffer(data) | ||
const [readRoot, ...more] = await reader.getRoots() | ||
same(more.length, 0) | ||
assert.ok(readRoot.equals(await root.cid())) | ||
|
||
for await (const block of all(reader)) { | ||
const expectedBlock = expected.shift() | ||
assert.ok((await expectedBlock.cid()).equals(await block.cid())) | ||
} | ||
}) | ||
}) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.