Skip to content

Commit 0bf33cb

Browse files
committed
fix(cli): await stalled download cleanup
1 parent e3636db commit 0bf33cb

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎packages/cli/src/utils/download.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@ import type { ClientRequest, IncomingMessage } from "node:http";
88
import { afterEach, describe, expect, it, vi } from "vitest";
99
import { downloadFile } from "./download.js";
1010

11+
const { unlinkSyncMock } = vi.hoisted(() => ({
12+
unlinkSyncMock: vi.fn(),
13+
}));
14+
1115
vi.mock("node:https", () => ({ get: vi.fn() }));
16+
vi.mock("node:fs", async (importOriginal) => {
17+
const actual = await importOriginal<typeof import("node:fs")>();
18+
unlinkSyncMock.mockImplementation(actual.unlinkSync);
19+
return { ...actual, unlinkSync: unlinkSyncMock };
20+
});
1221

1322
const mockGet = vi.mocked(httpsGet);
1423
const tempDirs: string[] = [];
@@ -20,13 +29,27 @@ afterEach(() => {
2029

2130
describe("downloadFile", () => {
2231
it("rejects an idle response and removes the partial file", async () => {
32+
const actualFs = await vi.importActual<typeof import("node:fs")>("node:fs");
33+
let responseClosed = false;
34+
unlinkSyncMock.mockImplementation((path) => {
35+
if (!responseClosed) {
36+
const error = new Error("file is locked");
37+
Object.assign(error, { code: "EBUSY" });
38+
throw error;
39+
}
40+
actualFs.unlinkSync(path);
41+
});
42+
2343
mockGet.mockImplementation(((_url: string, callback: (response: IncomingMessage) => void) => {
2444
const response = new PassThrough() as PassThrough & {
2545
statusCode: number;
2646
headers: Record<string, string>;
2747
};
2848
response.statusCode = 200;
2949
response.headers = {};
50+
response.once("close", () => {
51+
responseClosed = true;
52+
});
3053

3154
class FakeRequest extends EventEmitter {
3255
private timeout: ReturnType<typeof setTimeout> | undefined;

‎packages/cli/src/utils/download.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createWriteStream, renameSync, unlinkSync } from "node:fs";
22
import { get as httpsGet } from "node:https";
3+
import type { IncomingMessage } from "node:http";
34
import { pipeline } from "node:stream/promises";
45

56
const DEFAULT_DOWNLOAD_TIMEOUT_MS = 30_000;
@@ -31,7 +32,11 @@ export function downloadFile(
3132
const timeoutMs = options.timeoutMs ?? DEFAULT_DOWNLOAD_TIMEOUT_MS;
3233
return new Promise((resolve, reject) => {
3334
const follow = (u: string) => {
35+
let activeResponse: IncomingMessage | undefined;
36+
let responsePipelineStarted = false;
37+
let requestError: Error | undefined;
3438
const request = httpsGet(u, (res) => {
39+
activeResponse = res;
3540
if (res.statusCode === 301 || res.statusCode === 302) {
3641
const location = res.headers.location;
3742
if (location) {
@@ -47,20 +52,26 @@ export function downloadFile(
4752
return;
4853
}
4954
const file = createWriteStream(tmp);
55+
responsePipelineStarted = true;
5056
pipeline(res, file)
5157
.then(() => {
5258
renameSync(tmp, dest);
5359
resolve();
5460
})
5561
.catch((err) => {
5662
removePartialFile(tmp);
57-
reject(err);
63+
reject(requestError ?? err);
5864
});
5965
});
6066
request.setTimeout(timeoutMs, () => {
6167
request.destroy(new Error(`Download timed out after ${timeoutMs}ms`));
6268
});
6369
request.on("error", (err) => {
70+
if (responsePipelineStarted) {
71+
requestError = err;
72+
activeResponse?.destroy(err);
73+
return;
74+
}
6475
removePartialFile(tmp);
6576
reject(err);
6677
});

0 commit comments

Comments
 (0)