@@ -23,13 +23,71 @@ describe("Buffered Readable stream", () => {
2323 }
2424 return Readable . from ( generate ( ) ) ;
2525 }
26+
27+ function patternedByteStream ( size : number , chunkSize : number ) {
28+ let n = 0 ;
29+ const data = Array ( size ) ;
30+ for ( let i = 0 ; i < size ; ++ i ) {
31+ data [ i ] = n ++ % 255 ;
32+ }
33+ let dataCursor = 0 ;
34+
35+ async function * generate ( ) {
36+ while ( size > 0 ) {
37+ const z = Math . min ( size , chunkSize ) ;
38+ const bytes = new Uint8Array ( data . slice ( dataCursor , dataCursor + z ) ) ;
39+ size -= z ;
40+ dataCursor += z ;
41+ yield bytes ;
42+ }
43+ }
44+ return {
45+ stream : Readable . from ( size === 0 ? Buffer . from ( "" ) : generate ( ) ) ,
46+ array : new Uint8Array ( data ) ,
47+ } ;
48+ }
49+
2650 const logger = {
2751 debug : vi . fn ( ) ,
2852 info : vi . fn ( ) ,
2953 warn : vi . fn ( ) ,
3054 error ( ) { } ,
3155 } ;
3256
57+ const KB = 1024 ;
58+
59+ const dataSizes = [ 0 , 10 , 101 , 1_001 , 10_001 , 100_001 ] ;
60+ const chunkSizes = [ 1 , 8 , 16 , 32 , 64 , 128 , 1024 , 8 * 1024 , 64 * 1024 , 1024 * 1024 ] ;
61+ const bufferSizes = [ 0 , 1024 , 8 * 1024 , 32 * 1024 , 64 * 1024 , 1024 * 1024 ] ;
62+
63+ for ( const dataSize of dataSizes ) {
64+ for ( const chunkSize of chunkSizes ) {
65+ for ( const bufferSize of bufferSizes ) {
66+ it ( `should maintain data integrity for data=${ dataSize } chunk=${ chunkSize } min-buffer=${ bufferSize } ` , async ( ) => {
67+ const { stream, array } = patternedByteStream ( dataSize , chunkSize ) ;
68+ const bufferedStream = createBufferedReadable ( stream , bufferSize ) ;
69+ const collected = await headStream ( bufferedStream , Infinity ) ;
70+ expect ( collected ) . toEqual ( array ) ;
71+ } ) ;
72+ }
73+ }
74+ }
75+
76+ for ( const [ dataSize , chunkSize , bufferSize ] of [
77+ [ 10 * KB , 1 * KB , 0 * KB ] ,
78+ [ 10 * KB , 1 * KB , 1 * KB ] ,
79+ [ 10 * KB , 1 * KB , 2.1 * KB ] ,
80+ [ 10 * KB , 1 * KB , 4 * KB ] ,
81+ [ 10 * KB , 2 * KB , 1 * KB ] ,
82+ ] ) {
83+ it ( `should maintain data integrity for data=${ dataSize } chunk=${ chunkSize } min-buffer=${ bufferSize } ` , async ( ) => {
84+ const { stream, array } = patternedByteStream ( dataSize , chunkSize ) ;
85+ const bufferedStream = createBufferedReadable ( stream , bufferSize ) ;
86+ const collected = await headStream ( bufferedStream , Infinity ) ;
87+ expect ( collected ) . toEqual ( array ) ;
88+ } ) ;
89+ }
90+
3391 it ( "should join upstream chunks if they are too small (stringStream)" , async ( ) => {
3492 const upstream = stringStream ( 1024 , 8 ) ;
3593 const downstream = createBufferedReadable ( upstream , 64 ) ;
@@ -73,4 +131,4 @@ describe("Buffered Readable stream", () => {
73131 expect ( downstreamChunkCount ) . toEqual ( 22 ) ;
74132 expect ( logger . warn ) . toHaveBeenCalled ( ) ;
75133 } ) ;
76- } ) ;
134+ } , 30_000 ) ;
0 commit comments