Skip to content

fix: list links of a block that _is a_ CID #226

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

Merged
merged 2 commits into from
Jan 2, 2023
Merged
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
4 changes: 4 additions & 0 deletions src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ function * links (source, base) {
if (source == null || source instanceof Uint8Array) {
return
}
const cid = CID.asCID(source)
if (cid) {
yield [base.join('/'), cid]
}
for (const [key, value] of Object.entries(source)) {
const path = /** @type {[string|number, string]} */ ([...base, key])
yield * linksWithin(path, value)
Expand Down
11 changes: 11 additions & 0 deletions test/test-block.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ describe('block', () => {
})
})

it('links of a block that is a CID', async () => {
const block = await main.encode({ value: link, codec, hasher })
const links = []
for (const link of block.links()) {
links.push(link)
}
assert.equal(links.length, 1)
assert.equal(links[0][0], '')
assert.equal(links[0][1].toString(), link.toString())
})

it('kitchen sink', () => {
const sink = { one: { two: { arr: [true, false, null], three: 3, buff, link } } }
const block = main.createUnsafe({
Expand Down