@@ -61,6 +61,54 @@ describe("decodeArrayStream", () => {
6161 assert . deepStrictEqual ( result , [ 1 , 2 , 3 ] ) ;
6262 } ) ;
6363
64+ for ( const [ name , header ] of [
65+ [ "array16" , [ 0xdc , 0 , 1 ] ] ,
66+ [ "array32" , [ 0xdd , 0 , 0 , 0 , 1 ] ] ,
67+ ] as const ) {
68+ it ( `decodes ${ name } split at every header boundary` , async ( ) => {
69+ const object = { nested : [ 1 , "value" , null ] } ;
70+ const encoded = Uint8Array . from ( [ ...header , ...encode ( object ) ] ) ;
71+
72+ for ( let split = 1 ; split < header . length ; split ++ ) {
73+ const stream = async function * ( ) {
74+ yield encoded . subarray ( 0 , split ) ;
75+ yield encoded . subarray ( split ) ;
76+ } ;
77+ const result : Array < unknown > = [ ] ;
78+ for await ( const item of decodeArrayStream ( stream ( ) ) ) {
79+ result . push ( item ) ;
80+ }
81+ assert . deepStrictEqual ( result , [ object ] , `split at ${ split } ` ) ;
82+ }
83+ } ) ;
84+
85+ it ( `rejects ${ name } truncated in the header` , async ( ) => {
86+ for ( let length = 1 ; length < header . length ; length ++ ) {
87+ const stream = async function * ( ) {
88+ yield header . slice ( 0 , length ) ;
89+ yield new Uint8Array ( 0 ) ;
90+ } ;
91+ await assert . rejects ( decodeArrayStream ( stream ( ) ) . next ( ) , RangeError ) ;
92+ }
93+ } ) ;
94+
95+ it ( `decodes ${ name } byte-by-byte with empty chunks` , async ( ) => {
96+ const object = { nested : [ 1 , "value" , null ] } ;
97+ const encoded = Uint8Array . from ( [ ...header , ...encode ( object ) ] ) ;
98+ const stream = async function * ( ) {
99+ for ( const byte of encoded ) {
100+ yield new Uint8Array ( 0 ) ;
101+ yield Uint8Array . from ( [ 0xff , byte , 0xff ] ) . subarray ( 1 , 2 ) ;
102+ }
103+ } ;
104+ const result : Array < unknown > = [ ] ;
105+ for await ( const item of decodeArrayStream ( stream ( ) ) ) {
106+ result . push ( item ) ;
107+ }
108+ assert . deepStrictEqual ( result , [ object ] ) ;
109+ } ) ;
110+ }
111+
64112 it ( "decodes objects array" , async ( ) => {
65113 const objectsArrays : Array < any > = [ ] ;
66114
0 commit comments