@@ -66,7 +66,7 @@ export function createTarDecoder(
6666 // Look for the next header.
6767 if ( ! bodyController ) {
6868 // Respect backpressure on the main stream.
69- if ( ! force && ( controller . desiredSize ?? 1 ) <= 0 ) break ;
69+ if ( ! force && ( controller . desiredSize ?? 0 ) < 0 ) break ;
7070
7171 const header = unpacker . readHeader ( ) ;
7272 if ( header === null ) break ; // Not enough data
@@ -126,16 +126,18 @@ export function createTarDecoder(
126126 try {
127127 // biome-ignore lint/style/noNonNullAssertion: body processing state.
128128 bodyController ! . enqueue ( chunk ) ;
129+
129130 // If the body stream's buffer is full, signal to pause the pump.
130131 // biome-ignore lint/style/noNonNullAssertion: body processing state.
131132 if ( ( bodyController ! . desiredSize ?? 1 ) <= 0 ) shouldPause = true ;
132133 } catch {
133134 return true ; // Body errored or closed, discard
134135 }
135- return true ; // Continue processing unpacker's buffer
136+
137+ return true ;
136138 } ) ;
137139
138- // No buffered data was available; wait for the next chunk to resume pumping.
140+ // No buffered data is available. Wait for the next chunk to resume pumping.
139141 if ( fed === 0 ) break ;
140142
141143 // Check again if the body is now complete after streaming.
@@ -160,46 +162,51 @@ export function createTarDecoder(
160162 }
161163 } ;
162164
163- return new TransformStream < Uint8Array , ParsedTarEntry > ( {
164- transform ( chunk , controller ) {
165- try {
166- // In strict mode, ensure EOF blocks are all zeroes.
167- if ( eofReached && options . strict && chunk . some ( ( byte ) => byte !== 0 ) )
168- throw new Error ( "Invalid EOF." ) ;
169-
170- // Write incoming data to the unpacker.
171- unpacker . write ( chunk ) ;
172- pump ( controller ) ;
173- } catch ( error ) {
174- abortAll ( error , controller ) ;
175- throw error ;
176- }
177- } ,
165+ return new TransformStream < Uint8Array , ParsedTarEntry > (
166+ {
167+ transform ( chunk , controller ) {
168+ try {
169+ // In strict mode, ensure EOF blocks are all zeroes.
170+ if ( eofReached && options . strict && chunk . some ( ( byte ) => byte !== 0 ) )
171+ throw new Error ( "Invalid EOF." ) ;
172+
173+ // Write incoming data to the unpacker.
174+ unpacker . write ( chunk ) ;
175+ pump ( controller ) ;
176+ } catch ( error ) {
177+ abortAll ( error , controller ) ;
178+ throw error ;
179+ }
180+ } ,
178181
179- flush ( controller ) {
180- try {
181- unpacker . end ( ) ;
182- pump ( controller , true ) ; // Force pump for remaining data
182+ flush ( controller ) {
183+ try {
184+ unpacker . end ( ) ;
185+ pump ( controller , true ) ; // Force pump for remaining data
186+
187+ // If a bodyController still exists, the archive was truncated mid-file.
188+ if ( bodyController ) {
189+ if ( options . strict ) throw new Error ( "Tar archive is truncated." ) ;
183190
184- // If a bodyController still exists, the archive was truncated mid-file.
185- if ( bodyController ) {
186- if ( options . strict ) {
187- throw new Error ( "Tar archive is truncated." ) ;
191+ // In non-strict mode, just close the partial stream.
192+ try {
193+ bodyController . close ( ) ;
194+ } catch { }
195+ bodyController = null ;
188196 }
189- // In non-strict mode, just close the partial stream.
190- try {
191- bodyController . close ( ) ;
192- } catch { }
193- bodyController = null ;
194- }
195197
196- unpacker . validateEOF ( ) ;
198+ unpacker . validateEOF ( ) ;
197199
198- if ( ! controllerTerminated ) controller . terminate ( ) ;
199- } catch ( error ) {
200- abortAll ( error , controller ) ;
201- throw error ;
202- }
200+ if ( ! controllerTerminated ) controller . terminate ( ) ;
201+ } catch ( error ) {
202+ abortAll ( error , controller ) ;
203+ throw error ;
204+ }
205+ } ,
206+ } ,
207+ undefined ,
208+ {
209+ highWaterMark : 1 ,
203210 } ,
204- } ) ;
211+ ) ;
205212}
0 commit comments