File tree Expand file tree Collapse file tree 2 files changed +11
-1
lines changed Expand file tree Collapse file tree 2 files changed +11
-1
lines changed Original file line number Diff line number Diff line change @@ -107,6 +107,10 @@ class DefaultJSON implements JSON {
107
107
}
108
108
109
109
async get < T > ( cid : CID , options : Partial < GetOptions > = { } ) : Promise < T > {
110
+ if ( cid . code !== jsonCodec . code ) {
111
+ throw new TypeError ( 'The passed CID had an incorrect codec, it may correspond to a non-JSON block' )
112
+ }
113
+
110
114
const buf = await this . components . blockstore . get ( cid , options )
111
115
112
116
return jsonCodec . decode ( buf )
Original file line number Diff line number Diff line change 2
2
3
3
import { expect } from 'aegir/chai'
4
4
import { MemoryBlockstore } from 'blockstore-core'
5
+ import { CID } from 'multiformats/cid'
5
6
import { identity } from 'multiformats/hashes/identity'
6
7
import { json , type JSON } from '../src/index.js'
7
8
import type { Blockstore } from 'interface-blockstore'
8
- import type { CID } from 'multiformats/cid'
9
9
10
10
describe ( 'get' , ( ) => {
11
11
let blockstore : Blockstore
@@ -39,4 +39,10 @@ describe('get', () => {
39
39
40
40
await expect ( j . get ( cid ) ) . to . eventually . deep . equal ( input )
41
41
} )
42
+
43
+ it ( 'rejects if CID codec is not equal to JSON codec' , async ( ) => {
44
+ const rawCID = CID . createV1 ( 0x55 , cid . multihash )
45
+ await expect ( j . get ( rawCID ) ) . to . eventually . be . rejected
46
+ . with . property ( 'message' , 'The passed CID had an incorrect codec, it may correspond to a non-JSON block' )
47
+ } )
42
48
} )
You can’t perform that action at this time.
0 commit comments