@@ -42,20 +42,23 @@ async function decodeAll (entry, data, options, chunkSize = 64) {
4242 return count
4343}
4444
45- /** @param {Uint8Array[] } arrays */
46- function concat ( ...arrays ) {
47- const out = new Uint8Array ( arrays . reduce ( ( n , a ) => n + a . length , 0 ) )
48- let offset = 0
49- for ( const a of arrays ) {
50- out . set ( a , offset )
51- offset += a . length
45+ /**
46+ * @param {Uint8Array[] } chunks
47+ */
48+ function concatBytes ( chunks ) {
49+ const length = chunks . reduce ( ( p , c ) => p + c . length , 0 )
50+ const bytes = new Uint8Array ( length )
51+ let off = 0
52+ for ( const chunk of chunks ) {
53+ bytes . set ( chunk , off )
54+ off += chunk . length
5255 }
53- return out
56+ return bytes
5457}
5558
5659/** @param {Uint8Array } payload */
5760function lengthPrefixed ( payload ) {
58- return concat ( Uint8Array . from ( vEncode ( payload . length ) ) , payload )
61+ return concatBytes ( [ Uint8Array . from ( vEncode ( payload . length ) ) , payload ] )
5962}
6063
6164const validV1Header = lengthPrefixed ( cbEncode ( { version : 1 , roots : [ ] } ) )
@@ -65,17 +68,17 @@ describe('decode read-size limits', () => {
6568 for ( const entry of ENTRIES ) {
6669 it ( `${ entry . name } : header larger than maxHeaderLength` , async ( ) => {
6770 // carBytes' header length prefix declares 99 bytes; cap well below it
68- await assert . isRejected ( decodeAll ( entry , carBytes , { maxHeaderLength : 10 } ) , / m a x H e a d e r L e n g t h / )
71+ await assert . isRejected ( decodeAll ( entry , carBytes , { maxHeaderLength : 10 } ) , RangeError , ' maxHeaderLength' )
6972 } )
7073
7174 it ( `${ entry . name } : block larger than maxBlockLength` , async ( ) => {
7275 // every block in carBytes is at least 4 bytes; cap below that
73- await assert . isRejected ( decodeAll ( entry , carBytes , { maxBlockLength : 3 } ) , / m a x B l o c k L e n g t h / )
76+ await assert . isRejected ( decodeAll ( entry , carBytes , { maxBlockLength : 3 } ) , RangeError , ' maxBlockLength' )
7477 } )
7578
7679 it ( `${ entry . name } : CID larger than maxCidLength` , async ( ) => {
7780 // carBytes' section CIDs are CIDv1 sha2-256 (full CID 36 bytes); cap below that
78- await assert . isRejected ( decodeAll ( entry , carBytes , { maxCidLength : 10 } ) , / m a x C i d L e n g t h / )
81+ await assert . isRejected ( decodeAll ( entry , carBytes , { maxCidLength : 10 } ) , RangeError , ' maxCidLength' )
7982 } )
8083 }
8184 } )
@@ -107,7 +110,7 @@ describe('decode read-size limits', () => {
107110 // off-by-one that rejects the exactly-equal block fails this test.
108111 assert . strictEqual ( await decodeAll ( blockIter , carBytes , { maxBlockLength : max } ) , total )
109112 // one below: rejected
110- await assert . isRejected ( decodeAll ( blockIter , carBytes , { maxBlockLength : max - 1 } ) , / m a x B l o c k L e n g t h / )
113+ await assert . isRejected ( decodeAll ( blockIter , carBytes , { maxBlockLength : max - 1 } ) , RangeError , ' maxBlockLength' )
111114 } )
112115
113116 it ( 'measures the full CID, not just the multihash' , async ( ) => {
@@ -124,13 +127,7 @@ describe('decode read-size limits', () => {
124127 // equal to the full CID: allowed, everything decodes
125128 assert . ok ( await decodeAll ( blockIter , carBytes , { maxCidLength : maxCid } ) > 0 )
126129 // one below the full CID (still above the 34-byte multihash): rejected
127- await assert . isRejected ( decodeAll ( blockIter , carBytes , { maxCidLength : maxCid - 1 } ) , / m a x C i d L e n g t h / )
128- } )
129-
130- it ( 'size-limit violations reject with a RangeError' , async ( ) => {
131- await assert . isRejected ( decodeAll ( blockIter , carBytes , { maxHeaderLength : 10 } ) , RangeError )
132- await assert . isRejected ( decodeAll ( blockIter , carBytes , { maxBlockLength : 3 } ) , RangeError )
133- await assert . isRejected ( decodeAll ( blockIter , carBytes , { maxCidLength : 10 } ) , RangeError )
130+ await assert . isRejected ( decodeAll ( blockIter , carBytes , { maxCidLength : maxCid - 1 } ) , RangeError , 'maxCidLength' )
134131 } )
135132
136133 describe ( 'rejects from the length prefix before buffering the body' , ( ) => {
@@ -140,24 +137,24 @@ describe('decode read-size limits', () => {
140137 const huge = 1_000_000_000
141138
142139 it ( 'header' , async ( ) => {
143- const data = concat ( Uint8Array . from ( vEncode ( huge ) ) , Uint8Array . from ( [ 1 , 2 , 3 ] ) )
144- await assert . isRejected ( decodeAll ( blockIter , data , { maxHeaderLength : 1000 } ) , / m a x H e a d e r L e n g t h / )
140+ const data = concatBytes ( [ Uint8Array . from ( vEncode ( huge ) ) , Uint8Array . from ( [ 1 , 2 , 3 ] ) ] )
141+ await assert . isRejected ( decodeAll ( blockIter , data , { maxHeaderLength : 1000 } ) , RangeError , ' maxHeaderLength' )
145142 } )
146143
147144 it ( 'block' , async ( ) => {
148145 const cid = rndCid . bytes
149146 // declare a section of huge size but supply only the CID, no block body
150- const section = concat ( Uint8Array . from ( vEncode ( huge + cid . length ) ) , cid )
151- const data = concat ( validV1Header , section )
152- await assert . isRejected ( decodeAll ( blockIter , data , { maxBlockLength : 1000 } ) , / m a x B l o c k L e n g t h / )
147+ const section = concatBytes ( [ Uint8Array . from ( vEncode ( huge + cid . length ) ) , cid ] )
148+ const data = concatBytes ( [ validV1Header , section ] )
149+ await assert . isRejected ( decodeAll ( blockIter , data , { maxBlockLength : 1000 } ) , RangeError , ' maxBlockLength' )
153150 } )
154151
155152 it ( 'CID' , async ( ) => {
156153 // CIDv1, raw codec (0x55), sha2-256 (0x12), huge multihash digest length, no digest
157- const cid = concat ( Uint8Array . from ( [ 0x01 , 0x55 , 0x12 ] ) , Uint8Array . from ( vEncode ( huge ) ) )
158- const section = concat ( Uint8Array . from ( vEncode ( cid . length + 1 ) ) , cid )
159- const data = concat ( validV1Header , section )
160- await assert . isRejected ( decodeAll ( blockIter , data , { maxCidLength : 1000 } ) , / m a x C i d L e n g t h / )
154+ const cid = concatBytes ( [ Uint8Array . from ( [ 0x01 , 0x55 , 0x12 ] ) , Uint8Array . from ( vEncode ( huge ) ) ] )
155+ const section = concatBytes ( [ Uint8Array . from ( vEncode ( cid . length + 1 ) ) , cid ] )
156+ const data = concatBytes ( [ validV1Header , section ] )
157+ await assert . isRejected ( decodeAll ( blockIter , data , { maxCidLength : 1000 } ) , RangeError , ' maxCidLength' )
161158 } )
162159 } )
163160} )
0 commit comments