@@ -236,12 +236,13 @@ const inflt = (dat: Uint8Array, st: InflateState, buf?: Uint8Array, dict?: Uint8
236236 // source length dict length
237237 const sl = dat . length , dl = dict ? dict . length : 0 ;
238238 if ( ! sl || st . f && ! st . l ) return buf || new u8 ( 0 ) ;
239+ const noBuf = ! buf ;
239240 // have to estimate size
240- const noBuf = ! buf || st . i != 2 ;
241+ const resize = noBuf || st . i != 2 ;
241242 // no state
242243 const noSt = st . i ;
243244 // Assumes roughly 33% compression ratio average
244- if ( ! buf ) buf = new u8 ( sl * 3 ) ;
245+ if ( noBuf ) buf = new u8 ( sl * 3 ) ;
245246 // ensure buffer can fit at least l elements
246247 const cbuf = ( l : number ) => {
247248 let bl = buf . length ;
@@ -272,7 +273,7 @@ const inflt = (dat: Uint8Array, st: InflateState, buf?: Uint8Array, dict?: Uint8
272273 break ;
273274 }
274275 // ensure size
275- if ( noBuf ) cbuf ( bt + l ) ;
276+ if ( resize ) cbuf ( bt + l ) ;
276277 // Copy over uncompressed data
277278 buf . set ( dat . subarray ( s , t ) , bt ) ;
278279 // Get new bitpos, update byte count
@@ -332,7 +333,7 @@ const inflt = (dat: Uint8Array, st: InflateState, buf?: Uint8Array, dict?: Uint8
332333 }
333334 // Make sure the buffer can hold this + the largest possible addition
334335 // Maximum chunk size (practically, theoretically infinite) is 2^17
335- if ( noBuf ) cbuf ( bt + 131072 ) ;
336+ if ( resize ) cbuf ( bt + 131072 ) ;
336337 const lms = ( 1 << lbt ) - 1 , dms = ( 1 << dbt ) - 1 ;
337338 let lpos = pos ;
338339 for ( ; ; lpos = pos ) {
@@ -370,7 +371,7 @@ const inflt = (dat: Uint8Array, st: InflateState, buf?: Uint8Array, dict?: Uint8
370371 if ( noSt ) err ( 0 ) ;
371372 break ;
372373 }
373- if ( noBuf ) cbuf ( bt + 131072 ) ;
374+ if ( resize ) cbuf ( bt + 131072 ) ;
374375 const end = bt + add ;
375376 if ( bt < dt ) {
376377 const shift = dl - dt , dend = Math . min ( dt , end ) ;
@@ -383,7 +384,8 @@ const inflt = (dat: Uint8Array, st: InflateState, buf?: Uint8Array, dict?: Uint8
383384 st . l = lm , st . p = lpos , st . b = bt , st . f = final ;
384385 if ( lm ) final = 1 , st . m = lbt , st . d = dm , st . n = dbt ;
385386 } while ( ! final )
386- return bt == buf . length ? buf : noBuf ? slc ( buf , 0 , bt ) : buf . subarray ( 0 , bt ) ;
387+ // don't reallocate for streams or user buffers
388+ return bt != buf . length && noBuf ? slc ( buf , 0 , bt ) : buf . subarray ( 0 , bt ) ;
387389}
388390
389391// starting at p, write the minimum number of bits that can hold v to d
@@ -1431,7 +1433,7 @@ export class Inflate {
14311433 const bts = this . s . b ;
14321434 const dt = inflt ( this . p , this . s , this . o ) ;
14331435 this . ondata ( slc ( dt , bts , this . s . b ) , this . d ) ;
1434- this . o = dt == this . o ? dt . subarray ( this . s . b - 32768 ) : slc ( dt , this . s . b - 32768 ) , this . s . b = this . o . length ;
1436+ this . o = slc ( dt , this . s . b - 32768 ) , this . s . b = this . o . length ;
14351437 this . p = slc ( this . p , ( this . s . p / 8 ) | 0 ) , this . s . p &= 7 ;
14361438 }
14371439
0 commit comments