|
1 | 1 | import { createHash } from "crypto"; |
2 | 2 | import { open } from "fs/promises"; |
3 | 3 | import { join, relative, resolve, sep } from "path"; |
| 4 | +import { Readable } from "stream"; |
| 5 | +import { createWriteStream } from "fs"; |
4 | 6 | import { chmod, pathExists, readdir } from "fs-extra"; |
5 | 7 | import type { DirectoryResult } from "tmp-promise"; |
6 | 8 | import { dir } from "tmp-promise"; |
7 | 9 | import { |
| 10 | + copyStream, |
8 | 11 | excludeDirectories, |
9 | 12 | openZip, |
10 | 13 | openZipBuffer, |
@@ -276,3 +279,71 @@ async function computeHash(contents: Buffer) { |
276 | 279 |
|
277 | 280 | return hash.digest("hex"); |
278 | 281 | } |
| 282 | + |
| 283 | +describe("copyStream error handling", () => { |
| 284 | + let tmpDir: DirectoryResult; |
| 285 | + |
| 286 | + beforeEach(async () => { |
| 287 | + tmpDir = await dir({ |
| 288 | + unsafeCleanup: true, |
| 289 | + }); |
| 290 | + }); |
| 291 | + |
| 292 | + afterEach(async () => { |
| 293 | + await tmpDir?.cleanup(); |
| 294 | + }); |
| 295 | + |
| 296 | + it("rejects when the write stream errors mid-extraction", async () => { |
| 297 | + // Use a real zip to trigger unzip, but make the destination read-only |
| 298 | + // so the write stream fails. This verifies the promise rejects rather |
| 299 | + // than hanging indefinitely. |
| 300 | + const destPath = join(tmpDir.path, "output"); |
| 301 | + |
| 302 | + // Extract once to create the directory structure |
| 303 | + await unzipToDirectorySequentially(zipPath, destPath); |
| 304 | + |
| 305 | + // Make a file read-only so re-extraction will fail on write |
| 306 | + const targetFile = join(destPath, "directory", "file.txt"); |
| 307 | + await chmod(targetFile, 0o000); |
| 308 | + |
| 309 | + // On Windows, chmod doesn't prevent writes, so skip assertion there |
| 310 | + if (process.platform === "win32") { |
| 311 | + await chmod(targetFile, 0o644); |
| 312 | + return; |
| 313 | + } |
| 314 | + |
| 315 | + // Re-extract — should reject with a write error, not hang |
| 316 | + await expect( |
| 317 | + unzipToDirectorySequentially(zipPath, destPath), |
| 318 | + ).rejects.toThrow(); |
| 319 | + |
| 320 | + // Restore permissions for cleanup |
| 321 | + await chmod(targetFile, 0o644); |
| 322 | + }); |
| 323 | + |
| 324 | + it("rejects when the write stream is destroyed mid-copy", async () => { |
| 325 | + // A destroyed write stream should cause `copyStream` to reject, not hang. |
| 326 | + const destFile = join(tmpDir.path, "output.bin"); |
| 327 | + const writeStream = createWriteStream(destFile); |
| 328 | + |
| 329 | + // A readable that emits a chunk and then destroys the write stream, to |
| 330 | + // simulate a mid-copy write failure. |
| 331 | + let pushed = false; |
| 332 | + const readable = new Readable({ |
| 333 | + read() { |
| 334 | + if (pushed) { |
| 335 | + return; |
| 336 | + } |
| 337 | + pushed = true; |
| 338 | + this.push(Buffer.alloc(1024, "x")); |
| 339 | + setImmediate(() => { |
| 340 | + writeStream.destroy(new Error("simulated write failure")); |
| 341 | + }); |
| 342 | + }, |
| 343 | + }); |
| 344 | + |
| 345 | + await expect(copyStream(readable, writeStream)).rejects.toThrow( |
| 346 | + "simulated write failure", |
| 347 | + ); |
| 348 | + }); |
| 349 | +}); |
0 commit comments