Skip to content

Commit

Permalink
fix: round-trip DAG-JSON and DAG-CBOR with embedded CIDs
Browse files Browse the repository at this point in the history
DAG-JSON and DAG-CBOR can have embeded CIDs which are
deserialized to CID objects by the `@ipld/dag-json` and
`@ipld/dag-cbor` modules.

This fixes a bug whereby we were JSON.stringifying them
which lead to rubbish being returned from `.json()`.
  • Loading branch information
achingbrain committed Feb 12, 2024
1 parent 6eeb44d commit 57f7345
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/verified-fetch/src/verified-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ export class VerifiedFetch {
onProgress: options?.onProgress
})
options?.onProgress?.(new CustomProgressEvent<CIDDetail>('verified-fetch:request:end', { cid, path }))
const response = okResponse(JSON.stringify(result))
// return body as binary
const body = await this.helia.blockstore.get(cid)
const response = okResponse(body)
// return pre-parsed object with embedded CIDs as objects
response.json = async () => result
response.headers.set('content-type', 'application/json')
return response
}
Expand All @@ -154,7 +158,11 @@ export class VerifiedFetch {
onProgress: options?.onProgress
})
options?.onProgress?.(new CustomProgressEvent<CIDDetail>('verified-fetch:request:end', { cid, path }))
const response = okResponse(JSON.stringify(result))
// return body as binary
const body = await this.helia.blockstore.get(cid)
const response = okResponse(body)
// return pre-parsed object with embedded CIDs as objects
response.json = async () => result
await this.setContentType(result, path, response)
return response
}
Expand Down
32 changes: 32 additions & 0 deletions packages/verified-fetch/test/verified-fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,22 @@ describe('@helia/verifed-fetch', () => {
await expect(resp.json()).to.eventually.deep.equal(obj)
})

it('should return dag-json data with embedded CID', async () => {
const obj = {
hello: 'world',
link: CID.parse('QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN')
}
const j = dagJson(helia)
const cid = await j.add(obj)

const resp = await verifiedFetch.fetch(cid)
const data = await resp.json()
expect(data).to.deep.equal({
hello: 'world',
link: CID.parse('QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN')
})
})

it('should handle dag-cbor block', async () => {
const obj = {
hello: 'world'
Expand All @@ -238,6 +254,22 @@ describe('@helia/verifed-fetch', () => {
await expect(resp.json()).to.eventually.deep.equal(obj)
})

it('should return dag-cbor data with embedded CID', async () => {
const obj = {
hello: 'world',
link: CID.parse('QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN')
}
const j = json(helia)
const cid = await j.add(obj)

const resp = await verifiedFetch.fetch(cid)
const data = await resp.json()
expect(data).to.deep.equal({
hello: 'world',
link: CID.parse('QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN')
})
})

it('should handle json block', async () => {
const obj = {
hello: 'world'
Expand Down

0 comments on commit 57f7345

Please sign in to comment.