Skip to content

Commit 92ed766

Browse files
authored
fix(camera): publish URL downloads atomically (#122550)
1 parent 432ccac commit 92ed766

2 files changed

Lines changed: 69 additions & 33 deletions

File tree

‎src/cli/nodes-camera.test.ts‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ describe("nodes camera helpers", () => {
522522
expect(tracked.wasCanceled()).toBe(true);
523523
});
524524

525-
it("removes partially written file when url stream fails", async () => {
525+
it("preserves an existing file when url stream fails", async () => {
526526
const stream = new ReadableStream<Uint8Array>({
527527
start(controller) {
528528
controller.enqueue(new TextEncoder().encode("partial"));
@@ -533,14 +533,51 @@ describe("nodes camera helpers", () => {
533533

534534
await withCameraTempDir(async (dir) => {
535535
const out = path.join(dir, "broken.bin");
536+
const sentinel = Buffer.from("existing-camera");
537+
await fs.writeFile(out, sentinel);
538+
await fs.chmod(out, 0o640);
539+
536540
await expect(
537541
writeCameraPayloadToFile({
538542
filePath: out,
539543
payload: { url: "https://198.51.100.42/broken.bin" },
540544
expectedHost: "198.51.100.42",
541545
}),
542546
).rejects.toThrow(/stream exploded/i);
543-
await expectPathMissing(out);
547+
await expect(fs.readFile(out)).resolves.toEqual(sentinel);
548+
if (process.platform !== "win32") {
549+
expect((await fs.stat(out)).mode & 0o777).toBe(0o640);
550+
}
551+
expect(await fs.readdir(dir)).toEqual(["broken.bin"]);
552+
});
553+
});
554+
555+
it("rejects a url stream that closes without data", async () => {
556+
const stream = new ReadableStream<Uint8Array>({
557+
start(controller) {
558+
controller.close();
559+
},
560+
});
561+
stubFetchResponse(new Response(stream, { status: 200 }));
562+
563+
await withCameraTempDir(async (dir) => {
564+
const out = path.join(dir, "empty.bin");
565+
const sentinel = Buffer.from("existing-camera");
566+
await fs.writeFile(out, sentinel);
567+
await fs.chmod(out, 0o640);
568+
569+
await expect(
570+
writeCameraPayloadToFile({
571+
filePath: out,
572+
payload: { url: "https://198.51.100.42/empty.bin" },
573+
expectedHost: "198.51.100.42",
574+
}),
575+
).rejects.toThrow(/empty download/i);
576+
await expect(fs.readFile(out)).resolves.toEqual(sentinel);
577+
if (process.platform !== "win32") {
578+
expect((await fs.stat(out)).mode & 0o777).toBe(0o640);
579+
}
580+
expect(await fs.readdir(dir)).toEqual(["empty.bin"]);
544581
});
545582
});
546583
});

‎src/cli/nodes-camera.ts‎

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -195,39 +195,38 @@ async function writeUrlToFile(filePath: string, url: string, opts: { expectedHos
195195
throw new Error(`failed to download ${url}: empty response body`);
196196
}
197197

198-
const fileHandle = await fs.open(filePath, "w");
199-
let thrown: unknown;
200-
const reader = body.getReader();
201-
try {
202-
while (true) {
203-
const { done, value } = await reader.read();
204-
if (done) {
205-
break;
206-
}
207-
if (!value || value.byteLength === 0) {
208-
continue;
209-
}
210-
bytes += value.byteLength;
211-
if (bytes > MAX_CAMERA_URL_DOWNLOAD_BYTES) {
198+
await publishOutputFileAtomically({
199+
filePath,
200+
writeTemp: async (tempPath) => {
201+
const fileHandle = await fs.open(tempPath, "wx");
202+
const reader = body.getReader();
203+
try {
204+
while (true) {
205+
const { done, value } = await reader.read();
206+
if (done) {
207+
break;
208+
}
209+
bytes += value.byteLength;
210+
if (bytes > MAX_CAMERA_URL_DOWNLOAD_BYTES) {
211+
await reader.cancel().catch(() => undefined);
212+
throw new Error(
213+
`writeUrlToFile: downloaded ${bytes} bytes, exceeds max ${MAX_CAMERA_URL_DOWNLOAD_BYTES}`,
214+
);
215+
}
216+
await fileHandle.write(value);
217+
}
218+
} catch (err) {
212219
await reader.cancel().catch(() => undefined);
213-
throw new Error(
214-
`writeUrlToFile: downloaded ${bytes} bytes, exceeds max ${MAX_CAMERA_URL_DOWNLOAD_BYTES}`,
215-
);
220+
throw toErrorObject(err, "Non-Error thrown");
221+
} finally {
222+
reader.releaseLock();
223+
await fileHandle.close();
216224
}
217-
await fileHandle.write(value);
218-
}
219-
} catch (err) {
220-
thrown = err;
221-
await reader.cancel().catch(() => undefined);
222-
} finally {
223-
reader.releaseLock();
224-
await fileHandle.close();
225-
}
226-
227-
if (thrown) {
228-
await fs.unlink(filePath).catch(() => {});
229-
throw toErrorObject(thrown, "Non-Error thrown");
230-
}
225+
if (bytes === 0) {
226+
throw new Error(`writeUrlToFile: empty download from ${url}`);
227+
}
228+
},
229+
});
231230
} finally {
232231
await release();
233232
}

0 commit comments

Comments
 (0)