* Generic factory for creating zlib transform objects.
( createZlib: () => zlib.Gzip | zlib.Gunzip | zlib.Deflate | zlib.Inflate | zlib.BrotliCompress | zlib.BrotliDecompress )
| 254 | * Generic factory for creating zlib transform objects. |
| 255 | */ |
| 256 | function createZlibTransformObject( |
| 257 | createZlib: () => zlib.Gzip | zlib.Gunzip | zlib.Deflate | zlib.Inflate | zlib.BrotliCompress | zlib.BrotliDecompress |
| 258 | ): TransformObject { |
| 259 | const zlibStream = createZlib(); |
| 260 | const pending: Uint8Array[] = []; |
| 261 | let error: Error | null = null; |
| 262 | let pendingReject: ((err: Error) => void) | null = null; |
| 263 | |
| 264 | zlibStream.on('data', (chunk: Buffer) => { |
| 265 | pending.push(new Uint8Array(chunk)); |
| 266 | }); |
| 267 | |
| 268 | zlibStream.on('error', (err) => { |
| 269 | error = err; |
| 270 | // Immediately reject any pending operation |
| 271 | if (pendingReject) { |
| 272 | pendingReject(err); |
| 273 | pendingReject = null; |
| 274 | } |
| 275 | }); |
| 276 | |
| 277 | async function processChunk(chunk: Uint8Array | null): Promise<Uint8Array[]> { |
| 278 | return new Promise((resolve, reject) => { |
| 279 | if (error) { |
| 280 | reject(error); |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | // Store reject for async error handling |
| 285 | pendingReject = reject; |
| 286 | |
| 287 | if (chunk === null) { |
| 288 | // Use 'end' event (not 'finish') because 'end' fires AFTER final data is emitted |
| 289 | zlibStream.once('end', () => { |
| 290 | pendingReject = null; |
| 291 | if (error) { |
| 292 | reject(error); |
| 293 | } else { |
| 294 | resolve(pending.splice(0)); |
| 295 | } |
| 296 | }); |
| 297 | zlibStream.end(); |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | // Write and flush to get all output |
| 302 | zlibStream.write(copyToBuffer(chunk), (err) => { |
| 303 | if (err) { |
| 304 | pendingReject = null; |
| 305 | reject(err); |
| 306 | return; |
| 307 | } |
| 308 | if (error) { |
| 309 | pendingReject = null; |
| 310 | reject(error); |
| 311 | return; |
| 312 | } |
| 313 | zlibStream.flush(() => { |
no test coverage detected