Skip to content

Commit 95f80e0

Browse files
committed
Lazy ASSETS binding check in upload endpoint
Only require ASSETS binding when the upload includes static assets. Allows deploying workers with functions/_routes.json without needing a storage binding configured.
1 parent f62e0c7 commit 95f80e0

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

src/routes/workers.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -281,19 +281,7 @@ workers.post('/:id/upload', async (c) => {
281281
return c.json({ error: 'Worker not found' }, 404);
282282
}
283283

284-
// 2. Check worker has ASSETS binding
285-
const assetsBinding = await findWorkerAssetsBinding(userId, worker.id);
286-
287-
if (!assetsBinding) {
288-
return c.json(
289-
{ error: 'Worker has no ASSETS binding. Add an assets binding to the worker environment first.' },
290-
400
291-
);
292-
}
293-
294-
console.log('Found ASSETS binding for worker:', assetsBinding);
295-
296-
// 3. Parse multipart form data
284+
// 2. Parse multipart form data
297285
const formData = await c.req.formData();
298286
const file = formData.get('file');
299287

@@ -310,13 +298,27 @@ workers.post('/:id/upload', async (c) => {
310298
return c.json({ error: 'File too large. Maximum size is 50MB.' }, 413);
311299
}
312300

313-
// 3b. Parse asset manifest from form data (sent separately from zip)
301+
// 3. Parse asset manifest from form data (sent separately from zip)
314302
const assetsManifest = formData.get('assets');
315303
const assetEntries: Array<{ path: string; size: number; contentType: string; hash: string }> = assetsManifest
316304
? JSON.parse(assetsManifest as string)
317305
: [];
318306

319-
// 4. Extract zip (code-only: worker script, routes, functions)
307+
// 4. Check ASSETS binding (only needed when uploading static assets)
308+
let assetsBinding: Awaited<ReturnType<typeof findWorkerAssetsBinding>> = null;
309+
310+
if (assetEntries.length > 0) {
311+
assetsBinding = await findWorkerAssetsBinding(userId, worker.id);
312+
313+
if (!assetsBinding) {
314+
return c.json(
315+
{ error: 'Worker has no ASSETS binding. Add an assets binding to the worker environment first.' },
316+
400
317+
);
318+
}
319+
}
320+
321+
// 5. Extract zip (code-only: worker script, routes, functions)
320322
const zipBuffer = await file.arrayBuffer();
321323
const unzipped = unzipSync(new Uint8Array(zipBuffer));
322324

@@ -369,7 +371,7 @@ workers.post('/:id/upload', async (c) => {
369371
// 7. Generate presigned URLs for asset uploads (if manifest provided)
370372
let presignedAssets: Array<{ path: string; headUrl: string; putUrl: string }> = [];
371373

372-
if (assetEntries.length > 0) {
374+
if (assetEntries.length > 0 && assetsBinding) {
373375
const endpoint = assetsBinding.endpoint ?? sharedStorage.endpoint;
374376

375377
if (!endpoint) {

0 commit comments

Comments
 (0)