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

Abierto
#2,727 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
54/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Tranquilo
Stack tecnológico
typescript
Área
api

Línea de trabajo

Empieza en src/uploads.ts, especialmente en addFormValue(), e inspecciona cómo workers.assets.upload.create() determina los tipos de los valores del body y los añade. Revisa examples/workers/script-with-assets-upload.ts y el manejo de Uploadable descrito en el issue. Se considera completado cuando los llamadores de upload pueden conservar el tipo MIME de cada asset y el ejemplo deja de enviar strings simples que producen encabezados Content-Type vacíos.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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
Lenguaje dominante
TypeScript
Estrellas
835
Forks
201
Merge medio
41 min
PR fusionados (30 d)
1

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de cloudflare/cloudflare-typescript

Todos los issues de cloudflare/cloudflare-typescript

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.