Skip to content

Commit

Permalink
fix: @helia/json.get throws if CID codec not JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
tabcat committed Oct 3, 2024
1 parent c04dbf5 commit eb3da7c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/json/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class DefaultJSON implements JSON {
}

async get <T> (cid: CID, options: Partial<GetOptions> = {}): Promise<T> {
if (cid.code !== jsonCodec.code) {
throw new TypeError('The passed CID had an incorrect codec, it may correspond to a non-JSON block')
}

const buf = await this.components.blockstore.get(cid, options)

return jsonCodec.decode(buf)
Expand Down
8 changes: 7 additions & 1 deletion packages/json/test/get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import { expect } from 'aegir/chai'
import { MemoryBlockstore } from 'blockstore-core'
import { CID } from 'multiformats/cid'
import { identity } from 'multiformats/hashes/identity'
import { json, type JSON } from '../src/index.js'
import type { Blockstore } from 'interface-blockstore'
import type { CID } from 'multiformats/cid'

describe('get', () => {
let blockstore: Blockstore
Expand Down Expand Up @@ -39,4 +39,10 @@ describe('get', () => {

await expect(j.get(cid)).to.eventually.deep.equal(input)
})

it('rejects if CID codec is not equal to JSON codec', async () => {
const rawCID = CID.createV1(0x55, cid.multihash)
await expect(j.get(rawCID)).to.eventually.be.rejected
.with.property('message', 'The passed CID had an incorrect codec, it may correspond to a non-JSON block')
})
})

0 comments on commit eb3da7c

Please sign in to comment.