Skip to content

Commit 9653824

Browse files
committed
fix(cli): bound the in-memory project zip upload by progress like a file, watch the socket
1 parent 3161c0f commit 9653824

2 files changed

Lines changed: 57 additions & 28 deletions

File tree

‎packages/cli/src/cloud/upload.test.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ describe("a PUT that stalls or fails on the network is sent again", () => {
396396
}
397397
}
398398

399-
const upload = (url: string) => {
399+
const upload = (url: string, asBytes = false) => {
400400
const dir = mkdtempSync(join(tmpdir(), "hf-upload-retry-"));
401401
onTestFinished(() => rmSync(dir, { recursive: true, force: true }));
402402
writeFileSync(join(dir, "clip.mp4"), "bytes");
@@ -414,8 +414,7 @@ describe("a PUT that stalls or fails on the network is sent again", () => {
414414
status: "pending_upload" as const,
415415
})),
416416
} as Partial<HyperframesCloudClient>),
417-
path: join(dir, "clip.mp4"),
418-
size: 5,
417+
...(asBytes ? { bytes: Buffer.from("bytes") } : { path: join(dir, "clip.mp4"), size: 5 }),
419418
filename: "clip.mp4",
420419
contentType: "video/mp4",
421420
});
@@ -428,6 +427,13 @@ describe("a PUT that stalls or fails on the network is sent again", () => {
428427
});
429428
});
430429

430+
it("bounds the project zip, which is sent from memory, the same way", async () => {
431+
await withPlan(["hang", 200], async (url, requests) => {
432+
await upload(url, true);
433+
expect(requests()).toBe(2);
434+
});
435+
});
436+
431437
it("sends it again after a server error, and gives up after the retries with the last words", async () => {
432438
await withPlan([503, 200], async (url, requests) => {
433439
await upload(url);

‎packages/cli/src/cloud/upload.ts‎

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { createHash } from "node:crypto";
2727
import { createReadStream } from "node:fs";
2828
import { type IncomingMessage, request as httpRequest } from "node:http";
2929
import { request as httpsRequest } from "node:https";
30+
import { Readable } from "node:stream";
3031
import { pipeline } from "node:stream/promises";
3132
import type { HyperframesCloudClient } from "./_gen/client.js";
3233
import { HyperframesApiError } from "./_gen/client.js";
@@ -84,9 +85,13 @@ async function putWithRetry(attempt: () => Promise<void>, signal?: AbortSignal):
8485
try {
8586
return await attempt();
8687
} catch (err) {
87-
const final = err instanceof PutRefusedError && err.status < 500;
88+
const code = (err as { code?: unknown }).code;
89+
const final =
90+
(err instanceof PutRefusedError && err.status < 500) ||
91+
code === "ENOENT" ||
92+
code === "EACCES";
8893
if (final || signal?.aborted || tried >= putTimings.retries) throw err;
89-
await sleep(putTimings.backoffMs * 2 ** tried);
94+
await sleep(putTimings.backoffMs * 2 ** tried, signal);
9095
}
9196
}
9297
}
@@ -101,8 +106,11 @@ async function sha256OfFile(path: string): Promise<string> {
101106
return hash.digest("hex");
102107
}
103108

104-
function sleep(ms: number): Promise<void> {
105-
return new Promise((r) => setTimeout(r, ms));
109+
function sleep(ms: number, signal?: AbortSignal): Promise<void> {
110+
return new Promise((resolve) => {
111+
const timer = setTimeout(resolve, ms);
112+
signal?.addEventListener("abort", () => (clearTimeout(timer), resolve()), { once: true });
113+
});
106114
}
107115

108116
// upload_headers is generated as `Record<string, unknown>` from the
@@ -124,7 +132,7 @@ function normalizeUploadHeaders(raw: Record<string, unknown>): Record<string, st
124132
// the signature (S3 returns 403). Content-Type must match the declared
125133
// content_type from step 1 (also signed).
126134
async function putBytesToPresignedUrl(
127-
fetchImpl: typeof fetch,
135+
fetchImpl: typeof fetch | undefined,
128136
uploadUrl: string,
129137
uploadHeaders: Record<string, unknown>,
130138
body: Uint8Array | UploadFileSource,
@@ -137,9 +145,9 @@ async function putBytesToPresignedUrl(
137145
};
138146
return putWithRetry(
139147
() =>
140-
body instanceof Uint8Array
148+
fetchImpl && body instanceof Uint8Array
141149
? putBytesOnce(fetchImpl, uploadUrl, headers, body, signal)
142-
: putFileToPresignedUrl(uploadUrl, headers, body, signal),
150+
: putStreamToPresignedUrl(uploadUrl, headers, body, signal),
143151
signal,
144152
);
145153
}
@@ -171,34 +179,49 @@ async function putBytesOnce(
171179
}
172180
}
173181

174-
// fetch holds a streamed request body in memory whole, so a file is piped through node:http(s).
175-
async function putFileToPresignedUrl(
182+
// fetch holds a streamed request body in memory whole, so a file is piped through node:http(s). A buffer goes the
183+
// same way, in slices, so a stalled link ends its try the same way (a plain fetch has no progress to watch).
184+
const SLICE_BYTES = 64 * 1024;
185+
186+
function bodyStream(body: Uint8Array | UploadFileSource): Readable {
187+
if (!(body instanceof Uint8Array)) return createReadStream(body.path);
188+
return Readable.from(
189+
(function* () {
190+
for (let at = 0; at < body.byteLength; at += SLICE_BYTES)
191+
yield body.subarray(at, at + SLICE_BYTES);
192+
})(),
193+
);
194+
}
195+
196+
async function putStreamToPresignedUrl(
176197
uploadUrl: string,
177198
headers: Record<string, string>,
178-
file: UploadFileSource,
199+
body: Uint8Array | UploadFileSource,
179200
signal?: AbortSignal,
180201
): Promise<void> {
202+
const size = body instanceof Uint8Array ? body.byteLength : body.size;
181203
const send = new URL(uploadUrl).protocol === "https:" ? httpsRequest : httpRequest;
182204
const req = send(uploadUrl, {
183205
method: "PUT",
184206
signal,
185-
headers: { ...headers, "content-length": String(file.size) },
207+
headers: { ...headers, "content-length": String(size) },
186208
});
187209
const response = new Promise<IncomingMessage>((resolve, reject) => {
188210
req.once("response", resolve).once("error", reject);
189211
});
190-
// No byte moving for stallMs (the socket's buffer is full and nothing drains it) ends the try.
191-
let timer: NodeJS.Timeout | undefined;
192-
const arm = () => {
193-
clearTimeout(timer);
194-
timer = setTimeout(
195-
() =>
196-
req.destroy(new Error(`Upload stalled: no data sent for ${putTimings.stallMs / 1000} s`)),
197-
putTimings.stallMs,
198-
);
199-
};
200-
const source = createReadStream(file.path).on("data", arm).on("end", arm);
201-
arm();
212+
// No byte accepted by the socket for stallMs (its buffer is full and the link drains nothing) ends the try.
213+
let written = -1;
214+
let movedAt = Date.now();
215+
const watch = setInterval(
216+
() => {
217+
const now = req.socket?.bytesWritten ?? 0;
218+
if (now !== written) [written, movedAt] = [now, Date.now()];
219+
else if (Date.now() - movedAt >= putTimings.stallMs)
220+
req.destroy(new Error(`Upload stalled: no data sent for ${putTimings.stallMs / 1000} s`));
221+
},
222+
Math.min(1000, putTimings.stallMs / 3),
223+
);
224+
const source = bodyStream(body);
202225
const sent = pipeline(source, req);
203226
let res: IncomingMessage;
204227
try {
@@ -207,7 +230,7 @@ async function putFileToPresignedUrl(
207230
await sent.catch(() => undefined);
208231
throw err;
209232
} finally {
210-
clearTimeout(timer);
233+
clearInterval(watch);
211234
}
212235
const status = res.statusCode ?? 0;
213236
const ok = status >= 200 && status < 300;
@@ -280,7 +303,7 @@ export async function uploadAssetViaDirectUpload(
280303
const size_bytes = body instanceof Uint8Array ? body.byteLength : body.size;
281304
const checksum_sha256 =
282305
body instanceof Uint8Array ? sha256Hex(body) : await sha256OfFile(body.path);
283-
const fetchImpl = opts.fetchImpl ?? fetch;
306+
const fetchImpl = opts.fetchImpl;
284307

285308
opts.onProgress?.({ phase: "initialize" });
286309
const initialize = await opts.client.createAssetUpload({

0 commit comments

Comments
 (0)