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

オープン
#2,727 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
54/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
typescript
領域
api

調査の方向性

src/uploads.ts、特に addFormValue() から始め、workers.assets.upload.create() が body の値の型をどのように判定して追加するかを調べてください。examples/workers/script-with-assets-upload.ts と、issue に記載されている Uploadable の処理を確認してください。完了の条件は、upload の呼び出し元が各 asset の MIME タイプを保持でき、例が空の Content-Type ヘッダーを生成する単純な文字列を送信しなくなることです。

索引モデルが issue の本文から書いたものです。

説明

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
主要言語
TypeScript
スター
835
フォーク
201
平均マージ
41分
マージ済み PR(30日)
1

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

cloudflare/cloudflare-typescript のほかの issue

cloudflare/cloudflare-typescript の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。