Skip to content

Commit c17673d

Browse files
author
Alan Shaw
authored
fix: list links of a block that _is a_ CID (#226)
1 parent df7a276 commit c17673d

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/block.js

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ function * links (source, base) {
4545
if (source == null || source instanceof Uint8Array) {
4646
return
4747
}
48+
const cid = CID.asCID(source)
49+
if (cid) {
50+
yield [base.join('/'), cid]
51+
}
4852
for (const [key, value] of Object.entries(source)) {
4953
const path = /** @type {[string|number, string]} */ ([...base, key])
5054
yield * linksWithin(path, value)

test/test-block.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ describe('block', () => {
8383
})
8484
})
8585

86+
it('links of a block that is a CID', async () => {
87+
const block = await main.encode({ value: link, codec, hasher })
88+
const links = []
89+
for (const link of block.links()) {
90+
links.push(link)
91+
}
92+
assert.equal(links.length, 1)
93+
assert.equal(links[0][0], '')
94+
assert.equal(links[0][1].toString(), link.toString())
95+
})
96+
8697
it('kitchen sink', () => {
8798
const sink = { one: { two: { arr: [true, false, null], three: 3, buff, link } } }
8899
const block = main.createUnsafe({

0 commit comments

Comments
 (0)