workers.assets.upload.create() drops per-file Content-Type — assets served with empty MIME type

Aperta
#2,727 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
54/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
typescript
Ambito
api

Direzione di ricerca

Inizia da src/uploads.ts, in particolare da addFormValue(), e analizza come workers.assets.upload.create() determina i tipi dei valori del body e li aggiunge. Esamina examples/workers/script-with-assets-upload.ts e la gestione di Uploadable descritta nell’issue. Il lavoro è completato quando i chiamanti di upload possono preservare il tipo MIME di ogni asset e l’esempio non invia più semplici stringhe che producono intestazioni Content-Type vuote.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Description

client.workers.assets.upload.create() accepts body: Record<string, string> (hash → base64 content). When these plain strings are appended to FormData via addFormValue() in src/uploads.ts, no Content-Type header is set per multipart part.

Per Cloudflare's direct upload docs:

"The provided Content-Type header of each file part will be attached when eventually serving the file."

This means assets uploaded via the SDK are served with an empty MIME type, causing browsers to reject JS module scripts:

Failed to load module script: Expected a JavaScript-or-Wasm module script 
but the server responded with a MIME type of "". 
Strict MIME type checking is enforced for module scripts per HTML spec.

How wrangler handles this

Wrangler wraps each file in a File object with the correct MIME type before appending to FormData (packages/wrangler/src/assets.ts):

payload.append(
  hash,
  new File([base64Content], hash, { type: getContentType(absFilePath) ?? "application/null" }),
  hash
);

The SDK's addFormValue() in src/uploads.ts already supports Uploadable types (File/Blob) and preserves their .type property. But the typed interface for UploadCreateParams.body is Record<string, string>, making it impossible to pass File objects without casting.

The SDK's own example has this bug

examples/workers/script-with-assets-upload.ts passes plain strings in the body — assets deployed using this example will also be served with empty MIME types.

Reproduction

import Cloudflare from 'cloudflare';

const client = new Cloudflare({ apiToken: '...' });

// Step 1: Create upload session (works fine)
const session = await client.workersForPlatforms.dispatch.namespaces.scripts.assetUpload.create(
  namespace, scriptName, { account_id, manifest }
);

// Step 2: Upload assets — THIS DROPS MIME TYPES
for (const bucket of session.buckets) {
  const body: Record<string, string> = {};
  for (const hash of bucket) {
    body[hash] = fileContent.toString('base64');
  }
  await client.workers.assets.upload.create(
    { account_id, base64: true, body },
    { headers: { Authorization: `Bearer ${session.jwt}` } },
  );
}

// Step 3: Deploy worker (works fine)
// ... but all assets are now served with empty Content-Type

Expected behavior

The SDK should either:

  1. Accept Record<string, File | Blob | string> for the body parameter, allowing users to pass File objects with MIME types
  2. Or accept a manifest/path mapping so the SDK can infer MIME types from file extensions (like wrangler does)

Workaround

Use raw fetch() with FormData + Blob for the upload step (matching wrangler/vibesdk):

const formData = new FormData();
for (const hash of bucket) {
  const blob = new Blob([base64Content], { type: mimeType });
  formData.append(hash, blob, hash);
}
await fetch(
  `https://api.cloudflare.com/client/v4/accounts/${accountId}/workers/assets/upload?base64=true`,
  { method: 'POST', headers: { Authorization: `Bearer ${jwt}` }, body: formData }
);

Environment

  • SDK version: [email protected]
  • Runtime: Bun 1.x (also reproducible with Node.js)
  • Also affects Workers for Platforms asset uploads via workersForPlatforms.dispatch.namespaces.scripts.assetUpload
Lingua principale
TypeScript
Stelle
835
Fork
201
Merge medio
41m
PR unite (30g)
1

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di cloudflare/cloudflare-typescript

Tutte le issue di cloudflare/cloudflare-typescript

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.