mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-09-03 12:54:29 +00:00
feat(cli): migrate cloud-render upload to /v3/assets/direct-uploads (200MB) (#1844)
* chore(cli): regenerate cloud client for createAssetUpload + completeAssetUpload
Regenerated from experiment-framework `master` at commit `e74815f7af` (the
merge of EF#41085, which added `/v3/assets/direct-uploads` +
`/v3/assets/{asset_id}/complete` to the `TARGET_ENDPOINTS` allowlist in
`scripts/generate_hyperframes_cli_client.py`).
The `sync-hyperframes-codegen.yml` workflow that normally auto-opens this
PR failed with a `gh: Not Found (HTTP 404)` on the PR-creation step (run
28556975483); regenerated manually with:
cd experiment-framework
PYTHONPATH=. python3 scripts/generate_hyperframes_cli_client.py \\
--out /path/to/hyperframes-oss
This commit is codegen-only — no hand edits. The direct-upload wire-up
that consumes the new `createAssetUpload` + `completeAssetUpload` methods
lands in the follow-up commit.
— Jerrai
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* feat(cli): migrate cloud-render upload to /v3/assets/direct-uploads (200MB)
Replaces the legacy `client.uploadAsset(...)` multipart POST to
`/v3/assets` (32 MB in-memory proxy path) with the three-step direct-to-
S3 flow that lifts the practical per-project ceiling to 200 MB:
1. `POST /v3/assets/direct-uploads` — declares filename, content-type,
size, and SHA256 checksum; returns `asset_id`, presigned
`upload_url`, and required `upload_headers`.
2. Raw `PUT` to `upload_url` with the zip bytes + `upload_headers`
verbatim. No CLI auth attached — the presigned URL signature carries
authorization, and any extra headers would break the signature.
3. `POST /v3/assets/{asset_id}/complete` — finalizes into a reusable
asset. Retried up to 5x on 409 ("Uploaded object not found yet"), a
documented race between S3 write consistency and the finalize check.
The returned `asset_id` is the same namespace the legacy path produced
(both write into `movio_asset`), so the downstream render submission at
`createRender({project: {type: "asset_id", asset_id}})` is unchanged.
Server-side context (EF#41085): the direct-upload endpoint now accepts
`application/zip` via a scoped `_ZIP_MIME_TO_EXT` map — the shared media/
PDF allowlist stays zip-free. The exact-MIME cross-check at the sniff
step guards against zip<->PDF confusion under the shared 'document'
category. Canonical S3 key layout matches the legacy proxy path
(`document/{asset_id}/original.zip`), so the render-side head_object
gate is transparent to which upload path produced the asset.
The prior codegen commit added the generated createAssetUpload +
completeAssetUpload methods this commit consumes.
— Jerrai
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
a7c3cc7d68
commit
a59ff0d91b
@@ -5,6 +5,10 @@
|
||||
* experiment-framework to regenerate.
|
||||
*/
|
||||
import type {
|
||||
CompleteAssetUploadRequest,
|
||||
CompleteAssetUploadResponse,
|
||||
CreateAssetUploadRequest,
|
||||
CreateAssetUploadResponse,
|
||||
CreateHyperframesRenderRequest,
|
||||
CreateHyperframesRenderResponse,
|
||||
DeleteHyperframesRenderResponse,
|
||||
@@ -225,6 +229,43 @@ export class HyperframesCloudClient {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Asset Upload
|
||||
*
|
||||
* Begin a direct-to-S3 upload. Returns an asset_id and a presigned upload_url; PUT the file bytes to upload_url, then call POST /v3/assets/{asset_id}/complete. Unlike POST /v3/assets (which proxies the bytes), this never sends the file through the API.
|
||||
*/
|
||||
async createAssetUpload(args: {
|
||||
body: CreateAssetUploadRequest;
|
||||
idempotencyKey?: string;
|
||||
signal?: AbortSignal;
|
||||
}): Promise<CreateAssetUploadResponse> {
|
||||
return await this.request<CreateAssetUploadResponse>({
|
||||
method: "POST",
|
||||
path: "/v3/assets/direct-uploads",
|
||||
body: args.body,
|
||||
idempotencyKey: args.idempotencyKey,
|
||||
signal: args.signal,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Complete Asset Upload
|
||||
*
|
||||
* Finalize a direct-to-S3 upload into a reusable asset. Call after the upload PUT returns 200. Idempotent: repeated calls return the same finalized asset.
|
||||
*/
|
||||
async completeAssetUpload(args: {
|
||||
asset_id: string;
|
||||
body: CompleteAssetUploadRequest;
|
||||
signal?: AbortSignal;
|
||||
}): Promise<CompleteAssetUploadResponse> {
|
||||
return await this.request<CompleteAssetUploadResponse>({
|
||||
method: "POST",
|
||||
path: `/v3/assets/${encodeURIComponent(args.asset_id)}/complete`,
|
||||
body: args.body,
|
||||
signal: args.signal,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create HyperFrames Render
|
||||
*
|
||||
|
||||
@@ -55,6 +55,100 @@ export interface AssetUrl {
|
||||
url: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finalize a presigned upload (POST /v3/assets/{asset_id}/complete).
|
||||
*/
|
||||
export interface CompleteAssetUploadRequest {
|
||||
/**
|
||||
* Optional SHA256 (hex) cross-check.
|
||||
*/
|
||||
checksum_sha256?: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Result of finalizing an upload.
|
||||
*/
|
||||
export interface CompleteAssetUploadResponse {
|
||||
/**
|
||||
* The reusable asset identifier.
|
||||
*/
|
||||
asset_id: string;
|
||||
/**
|
||||
* Public URL of the finalized asset.
|
||||
*/
|
||||
url: string;
|
||||
/**
|
||||
* MIME type detected from the stored bytes.
|
||||
*/
|
||||
mime_type: string;
|
||||
/**
|
||||
* Size of the stored object in bytes.
|
||||
*/
|
||||
size_bytes: number;
|
||||
/**
|
||||
* Asset status, e.g. 'processing'.
|
||||
*/
|
||||
status: "processing";
|
||||
}
|
||||
|
||||
/**
|
||||
* Request to begin a presigned direct-to-S3 upload (POST
|
||||
* /v3/assets/direct-uploads).
|
||||
*/
|
||||
export interface CreateAssetUploadRequest {
|
||||
/**
|
||||
* Original filename for reference/metadata. The stored object's extension is
|
||||
* derived from content_type.
|
||||
*/
|
||||
filename: string;
|
||||
/**
|
||||
* Declared MIME type (e.g. 'video/mp4', 'image/png', 'audio/mpeg',
|
||||
* 'application/pdf', 'application/zip'). Verified against the stored bytes at
|
||||
* completion.
|
||||
*/
|
||||
content_type: string;
|
||||
/**
|
||||
* Exact byte size of the file. Signed into the upload URL so it cannot be
|
||||
* exceeded.
|
||||
*/
|
||||
size_bytes: number;
|
||||
/**
|
||||
* Optional SHA256 of the file as hex. When provided, S3 enforces it on upload.
|
||||
*/
|
||||
checksum_sha256?: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Presigned upload instructions.
|
||||
*/
|
||||
export interface CreateAssetUploadResponse {
|
||||
/**
|
||||
* Reusable asset identifier. Becomes usable after POST
|
||||
* /v3/assets/{asset_id}/complete.
|
||||
*/
|
||||
asset_id: string;
|
||||
/**
|
||||
* Presigned S3 URL. PUT the raw file bytes here.
|
||||
*/
|
||||
upload_url: string;
|
||||
/**
|
||||
* Headers that must be sent verbatim on the PUT request.
|
||||
*/
|
||||
upload_headers: Record<string, unknown>;
|
||||
/**
|
||||
* Seconds until the upload URL expires.
|
||||
*/
|
||||
expires_in_seconds: number;
|
||||
/**
|
||||
* Maximum allowed upload size in bytes.
|
||||
*/
|
||||
max_bytes: number;
|
||||
/**
|
||||
* Upload lifecycle status. Always 'pending_upload' here.
|
||||
*/
|
||||
status: "pending_upload";
|
||||
}
|
||||
|
||||
/**
|
||||
* Request body for POST /v3/hyperframes/renders.
|
||||
*/
|
||||
|
||||
@@ -19,7 +19,7 @@ import { HyperframesApiError } from "./_gen/client.js";
|
||||
*/
|
||||
const ERROR_CODE_HINTS: Record<string, string> = {
|
||||
hyperframes_project_too_large:
|
||||
"The zip exceeded the 32 MB limit. Trim large media (or pre-host them and reference by URL), then try again.",
|
||||
"The zip exceeded the 200 MB limit. Trim large media (or pre-host them and reference by URL), then try again.",
|
||||
hyperframes_render_not_found:
|
||||
"The render_id no longer exists — either soft-deleted or never created.",
|
||||
invalid_parameter:
|
||||
|
||||
@@ -0,0 +1,234 @@
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { createHash } from "node:crypto";
|
||||
import { HyperframesApiError } from "./_gen/client.js";
|
||||
import type { HyperframesCloudClient } from "./_gen/client.js";
|
||||
import { uploadZipViaDirectUpload } from "./upload.js";
|
||||
|
||||
function sha256Hex(bytes: Uint8Array): string {
|
||||
return createHash("sha256").update(bytes).digest("hex");
|
||||
}
|
||||
|
||||
function makeClient(overrides: Partial<HyperframesCloudClient> = {}): HyperframesCloudClient {
|
||||
return {
|
||||
createAssetUpload: vi.fn(async () => ({
|
||||
asset_id: "asset_xyz",
|
||||
upload_url: "https://s3.example/asset_xyz?sig=abc",
|
||||
upload_headers: { "x-amz-checksum-sha256-b64": "..." },
|
||||
expires_in_seconds: 3600,
|
||||
max_bytes: 200 * 1024 * 1024,
|
||||
status: "pending_upload" as const,
|
||||
})),
|
||||
completeAssetUpload: vi.fn(async () => ({
|
||||
asset_id: "asset_xyz",
|
||||
url: "https://files.heygen.com/document/asset_xyz/original.zip",
|
||||
mime_type: "application/zip",
|
||||
size_bytes: 42,
|
||||
status: "processing" as const,
|
||||
})),
|
||||
...overrides,
|
||||
} as unknown as HyperframesCloudClient;
|
||||
}
|
||||
|
||||
function makeFetchOk(): typeof fetch {
|
||||
return vi.fn(async () => new Response("", { status: 200 })) as unknown as typeof fetch;
|
||||
}
|
||||
|
||||
describe("uploadZipViaDirectUpload", () => {
|
||||
it("sends the correct filename, content_type, size_bytes, and SHA256 to createAssetUpload", async () => {
|
||||
const bytes = new TextEncoder().encode("hello-hyperframes-zip");
|
||||
const expectedSha = sha256Hex(bytes);
|
||||
const client = makeClient();
|
||||
const fetchImpl = makeFetchOk();
|
||||
|
||||
await uploadZipViaDirectUpload({
|
||||
client,
|
||||
bytes,
|
||||
filename: "my-comp.zip",
|
||||
fetchImpl,
|
||||
});
|
||||
|
||||
expect(client.createAssetUpload).toHaveBeenCalledOnce();
|
||||
const arg = (client.createAssetUpload as ReturnType<typeof vi.fn>).mock.calls[0]![0];
|
||||
expect(arg.body).toEqual({
|
||||
filename: "my-comp.zip",
|
||||
content_type: "application/zip",
|
||||
size_bytes: bytes.byteLength,
|
||||
checksum_sha256: expectedSha,
|
||||
});
|
||||
});
|
||||
|
||||
it("PUTs to the returned upload_url with the returned upload_headers verbatim + content-type", async () => {
|
||||
const bytes = new Uint8Array([1, 2, 3, 4]);
|
||||
const client = makeClient({
|
||||
createAssetUpload: vi.fn(async () => ({
|
||||
asset_id: "asset_xyz",
|
||||
upload_url: "https://s3.example/target?sig=xxx",
|
||||
upload_headers: { "x-signed-header": "signed-value", "x-other": "other-value" },
|
||||
expires_in_seconds: 3600,
|
||||
max_bytes: 200 * 1024 * 1024,
|
||||
status: "pending_upload" as const,
|
||||
})) as HyperframesCloudClient["createAssetUpload"],
|
||||
});
|
||||
const fetchImpl = makeFetchOk();
|
||||
|
||||
await uploadZipViaDirectUpload({ client, bytes, filename: "x.zip", fetchImpl });
|
||||
|
||||
expect(fetchImpl).toHaveBeenCalledOnce();
|
||||
const [url, init] = (fetchImpl as ReturnType<typeof vi.fn>).mock.calls[0]!;
|
||||
expect(url).toBe("https://s3.example/target?sig=xxx");
|
||||
expect(init.method).toBe("PUT");
|
||||
expect(init.body).toBe(bytes);
|
||||
expect(init.headers).toEqual({
|
||||
"content-type": "application/zip",
|
||||
"x-signed-header": "signed-value",
|
||||
"x-other": "other-value",
|
||||
});
|
||||
});
|
||||
|
||||
it("does NOT attach CLI auth headers to the S3 PUT — presigned URL carries auth", async () => {
|
||||
const client = makeClient();
|
||||
const fetchImpl = makeFetchOk();
|
||||
|
||||
await uploadZipViaDirectUpload({
|
||||
client,
|
||||
bytes: new Uint8Array([0]),
|
||||
filename: "x.zip",
|
||||
fetchImpl,
|
||||
});
|
||||
|
||||
const init = (fetchImpl as ReturnType<typeof vi.fn>).mock.calls[0]![1];
|
||||
// No Authorization / x-api-key / Bearer header should be present.
|
||||
const headerKeys = Object.keys(init.headers as Record<string, string>).map((k) =>
|
||||
k.toLowerCase(),
|
||||
);
|
||||
expect(headerKeys).not.toContain("authorization");
|
||||
expect(headerKeys).not.toContain("x-api-key");
|
||||
});
|
||||
|
||||
it("calls completeAssetUpload with the initialize's asset_id + same checksum", async () => {
|
||||
const bytes = new TextEncoder().encode("determinism");
|
||||
const expectedSha = sha256Hex(bytes);
|
||||
const client = makeClient();
|
||||
const fetchImpl = makeFetchOk();
|
||||
|
||||
const result = await uploadZipViaDirectUpload({
|
||||
client,
|
||||
bytes,
|
||||
filename: "x.zip",
|
||||
fetchImpl,
|
||||
});
|
||||
|
||||
expect(client.completeAssetUpload).toHaveBeenCalledOnce();
|
||||
const completeArg = (client.completeAssetUpload as ReturnType<typeof vi.fn>).mock.calls[0]![0];
|
||||
expect(completeArg.asset_id).toBe("asset_xyz");
|
||||
expect(completeArg.body).toEqual({ checksum_sha256: expectedSha });
|
||||
expect(result.asset_id).toBe("asset_xyz");
|
||||
expect(result.size_bytes).toBe(bytes.byteLength);
|
||||
});
|
||||
|
||||
it("retries completeAssetUpload on 409 and succeeds on a later attempt", async () => {
|
||||
let completeCalls = 0;
|
||||
const completeAssetUpload = vi.fn(async () => {
|
||||
completeCalls++;
|
||||
if (completeCalls < 3) {
|
||||
throw new HyperframesApiError({
|
||||
status: 409,
|
||||
message: "Uploaded object not found yet. Retry after upload PUT returns 200.",
|
||||
code: "conflict",
|
||||
});
|
||||
}
|
||||
return {
|
||||
asset_id: "asset_xyz",
|
||||
url: "u",
|
||||
mime_type: "application/zip",
|
||||
size_bytes: 1,
|
||||
status: "processing" as const,
|
||||
};
|
||||
});
|
||||
const client = makeClient({
|
||||
completeAssetUpload:
|
||||
completeAssetUpload as unknown as HyperframesCloudClient["completeAssetUpload"],
|
||||
});
|
||||
|
||||
const result = await uploadZipViaDirectUpload({
|
||||
client,
|
||||
bytes: new Uint8Array([0]),
|
||||
filename: "x.zip",
|
||||
fetchImpl: makeFetchOk(),
|
||||
});
|
||||
|
||||
expect(completeCalls).toBe(3);
|
||||
expect(result.asset_id).toBe("asset_xyz");
|
||||
});
|
||||
|
||||
it("surfaces non-409 errors from complete without retrying", async () => {
|
||||
let completeCalls = 0;
|
||||
const completeAssetUpload = vi.fn(async () => {
|
||||
completeCalls++;
|
||||
throw new HyperframesApiError({
|
||||
status: 400,
|
||||
message: "invalid checksum",
|
||||
code: "invalid_parameter",
|
||||
});
|
||||
});
|
||||
const client = makeClient({
|
||||
completeAssetUpload:
|
||||
completeAssetUpload as unknown as HyperframesCloudClient["completeAssetUpload"],
|
||||
});
|
||||
|
||||
await expect(
|
||||
uploadZipViaDirectUpload({
|
||||
client,
|
||||
bytes: new Uint8Array([0]),
|
||||
filename: "x.zip",
|
||||
fetchImpl: makeFetchOk(),
|
||||
}),
|
||||
).rejects.toThrow(/invalid checksum/);
|
||||
expect(completeCalls).toBe(1);
|
||||
});
|
||||
|
||||
it("surfaces PUT failures with response body detail", async () => {
|
||||
const client = makeClient();
|
||||
const fetchImpl = vi.fn(
|
||||
async () =>
|
||||
new Response("SignatureDoesNotMatch: request signature we calculated does not match", {
|
||||
status: 403,
|
||||
}),
|
||||
) as unknown as typeof fetch;
|
||||
|
||||
await expect(
|
||||
uploadZipViaDirectUpload({
|
||||
client,
|
||||
bytes: new Uint8Array([0]),
|
||||
filename: "x.zip",
|
||||
fetchImpl,
|
||||
}),
|
||||
).rejects.toThrow(/Direct upload PUT failed: 403.*SignatureDoesNotMatch/);
|
||||
});
|
||||
|
||||
it("passes idempotencyKey through to createAssetUpload", async () => {
|
||||
const client = makeClient();
|
||||
await uploadZipViaDirectUpload({
|
||||
client,
|
||||
bytes: new Uint8Array([0]),
|
||||
filename: "x.zip",
|
||||
idempotencyKey: "test-key-123",
|
||||
fetchImpl: makeFetchOk(),
|
||||
});
|
||||
const arg = (client.createAssetUpload as ReturnType<typeof vi.fn>).mock.calls[0]![0];
|
||||
expect(arg.idempotencyKey).toBe("test-key-123");
|
||||
});
|
||||
|
||||
it("emits progress events in order", async () => {
|
||||
const client = makeClient();
|
||||
const events: Array<{ phase: string }> = [];
|
||||
await uploadZipViaDirectUpload({
|
||||
client,
|
||||
bytes: new Uint8Array([0]),
|
||||
filename: "x.zip",
|
||||
fetchImpl: makeFetchOk(),
|
||||
onProgress: (e) => events.push({ phase: e.phase }),
|
||||
});
|
||||
expect(events.map((e) => e.phase)).toEqual(["initialize", "upload", "upload", "complete"]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,165 @@
|
||||
/**
|
||||
* Direct-upload flow for `hyperframes cloud render` project asset uploads.
|
||||
*
|
||||
* The legacy `POST /v3/assets` path proxies bytes through the API and is
|
||||
* capped at 32 MB in-memory. This module implements the three-step
|
||||
* direct-to-S3 flow the CLI now uses instead, lifting the practical
|
||||
* per-project ceiling to 200 MB (the direct-upload cap enforced by the
|
||||
* signed presigned URL):
|
||||
*
|
||||
* 1. `client.createAssetUpload({filename, content_type, size_bytes,
|
||||
* checksum_sha256})` → returns `{asset_id, upload_url,
|
||||
* upload_headers, expires_in_seconds, max_bytes}`.
|
||||
* 2. Raw `PUT` to `upload_url` with the zip bytes + `upload_headers`
|
||||
* verbatim. No CLI auth headers on this call — the presigned URL
|
||||
* signature carries authorization.
|
||||
* 3. `client.completeAssetUpload({asset_id, body: {checksum_sha256}})`
|
||||
* to finalize. Docs explicitly note a 409 "Uploaded object not
|
||||
* found yet" is possible if the PUT hasn't been fully committed
|
||||
* server-side; a small retry loop absorbs that.
|
||||
*
|
||||
* The returned `asset_id` is the same id namespace the legacy path
|
||||
* produced, so `createRender({project: {type: "asset_id", asset_id}})`
|
||||
* on the render side is a drop-in swap.
|
||||
*/
|
||||
|
||||
import { createHash } from "node:crypto";
|
||||
import type { HyperframesCloudClient } from "./_gen/client.js";
|
||||
import { HyperframesApiError } from "./_gen/client.js";
|
||||
|
||||
export interface UploadZipViaDirectResult {
|
||||
asset_id: string;
|
||||
size_bytes: number;
|
||||
duration_ms: number;
|
||||
}
|
||||
|
||||
export type UploadProgressEvent =
|
||||
| { phase: "initialize" }
|
||||
| { phase: "upload"; percent: number }
|
||||
| { phase: "complete" };
|
||||
|
||||
export interface UploadZipViaDirectOptions {
|
||||
client: HyperframesCloudClient;
|
||||
bytes: Uint8Array;
|
||||
filename: string;
|
||||
idempotencyKey?: string;
|
||||
fetchImpl?: typeof fetch;
|
||||
onProgress?: (event: UploadProgressEvent) => void;
|
||||
}
|
||||
|
||||
// Per api-docs: completeAssetUpload can return 409 "Uploaded object not found
|
||||
// yet" if the S3 PUT's write hasn't propagated to the read plane by the time
|
||||
// complete runs. Small backoff loop absorbs it.
|
||||
const COMPLETE_MAX_RETRIES = 5;
|
||||
const COMPLETE_RETRY_BASE_MS = 500;
|
||||
const CONTENT_TYPE_ZIP = "application/zip";
|
||||
|
||||
function sha256Hex(bytes: Uint8Array): string {
|
||||
return createHash("sha256").update(bytes).digest("hex");
|
||||
}
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise((r) => setTimeout(r, ms));
|
||||
}
|
||||
|
||||
// upload_headers is generated as `Record<string, unknown>` from the
|
||||
// OpenAPI spec, but the values are always strings at runtime (HTTP header
|
||||
// values). Coerce defensively so a spec quirk can't slip a non-string in.
|
||||
function normalizeUploadHeaders(raw: Record<string, unknown>): Record<string, string> {
|
||||
const out: Record<string, string> = {};
|
||||
for (const [k, v] of Object.entries(raw ?? {})) {
|
||||
if (typeof v === "string") out[k] = v;
|
||||
else if (typeof v === "number" || typeof v === "boolean") out[k] = String(v);
|
||||
// Skip anything else — better to omit than send `[object Object]`.
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// PUT bytes to the presigned URL. Do NOT attach CLI auth headers; the
|
||||
// presigned URL signature carries authorization and the signature is
|
||||
// bound to the *headers signed at presign time* — extra headers invalidate
|
||||
// the signature (S3 returns 403). Content-Type must match the declared
|
||||
// content_type from step 1 (also signed).
|
||||
async function putBytesToPresignedUrl(
|
||||
fetchImpl: typeof fetch,
|
||||
uploadUrl: string,
|
||||
uploadHeaders: Record<string, unknown>,
|
||||
bytes: Uint8Array,
|
||||
): Promise<void> {
|
||||
const headers: Record<string, string> = {
|
||||
"content-type": CONTENT_TYPE_ZIP,
|
||||
...normalizeUploadHeaders(uploadHeaders),
|
||||
};
|
||||
// `Uint8Array<ArrayBufferLike>` is a valid `BodyInit` at runtime but
|
||||
// not strictly assignable per lib.dom.d.ts — cast rather than copy,
|
||||
// since a 200MB buffer copy would be wasteful.
|
||||
const res = await fetchImpl(uploadUrl, {
|
||||
method: "PUT",
|
||||
headers,
|
||||
body: bytes as unknown as BodyInit,
|
||||
});
|
||||
if (!res.ok) {
|
||||
const detail = await res.text().catch(() => "");
|
||||
throw new Error(
|
||||
`Direct upload PUT failed: ${res.status} ${res.statusText}${
|
||||
detail ? ` — ${detail.slice(0, 300)}` : ""
|
||||
}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Complete with retry-on-409. Retry ONLY on the documented "PUT not
|
||||
// visible yet" race between S3 write consistency and finalize; any other
|
||||
// error surfaces immediately. `completeAssetUpload` itself is idempotent,
|
||||
// so retrying an already-succeeded call is safe.
|
||||
async function completeWithRetry(
|
||||
client: HyperframesCloudClient,
|
||||
asset_id: string,
|
||||
checksum_sha256: string,
|
||||
): Promise<{ asset_id: string }> {
|
||||
let lastErr: unknown;
|
||||
for (let attempt = 0; attempt < COMPLETE_MAX_RETRIES; attempt++) {
|
||||
try {
|
||||
return await client.completeAssetUpload({
|
||||
asset_id,
|
||||
body: { checksum_sha256 },
|
||||
});
|
||||
} catch (err) {
|
||||
const retryable = err instanceof HyperframesApiError && err.status === 409;
|
||||
if (!retryable || attempt === COMPLETE_MAX_RETRIES - 1) {
|
||||
throw err;
|
||||
}
|
||||
lastErr = err;
|
||||
await sleep(COMPLETE_RETRY_BASE_MS * (attempt + 1));
|
||||
}
|
||||
}
|
||||
throw lastErr ?? new Error("completeAssetUpload retries exhausted");
|
||||
}
|
||||
|
||||
export async function uploadZipViaDirectUpload(
|
||||
opts: UploadZipViaDirectOptions,
|
||||
): Promise<UploadZipViaDirectResult> {
|
||||
const start = Date.now();
|
||||
const { bytes, filename, idempotencyKey } = opts;
|
||||
const size_bytes = bytes.byteLength;
|
||||
const checksum_sha256 = sha256Hex(bytes);
|
||||
const fetchImpl = opts.fetchImpl ?? fetch;
|
||||
|
||||
opts.onProgress?.({ phase: "initialize" });
|
||||
const initialize = await opts.client.createAssetUpload({
|
||||
body: { filename, content_type: CONTENT_TYPE_ZIP, size_bytes, checksum_sha256 },
|
||||
idempotencyKey,
|
||||
});
|
||||
|
||||
opts.onProgress?.({ phase: "upload", percent: 0 });
|
||||
await putBytesToPresignedUrl(fetchImpl, initialize.upload_url, initialize.upload_headers, bytes);
|
||||
opts.onProgress?.({ phase: "upload", percent: 100 });
|
||||
|
||||
opts.onProgress?.({ phase: "complete" });
|
||||
const completed = await completeWithRetry(opts.client, initialize.asset_id, checksum_sha256);
|
||||
return {
|
||||
asset_id: completed.asset_id,
|
||||
size_bytes,
|
||||
duration_ms: Date.now() - start,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user