From a5232b205700a5c925f67cf0f1a6e903cc36f014 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:10:14 -0600 Subject: [PATCH] fix(core): send Bun.file bodies from Code Mode fetch (#65917) --- packages/core/src/codemode/bun.ts | 13 ++++++++++--- packages/core/src/codemode/fetch.ts | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/packages/core/src/codemode/bun.ts b/packages/core/src/codemode/bun.ts index 2f2c4648afc..e1c5399bb5e 100644 --- a/packages/core/src/codemode/bun.ts +++ b/packages/core/src/codemode/bun.ts @@ -33,8 +33,8 @@ type Range = { readonly offset: number; readonly length: number } type Handle = { readonly name: string; readonly target: FileAccess.Target; readonly range?: Range } // A BunFile reaches a program as a copy and comes back as another, but its functions come back as themselves: its -// `bytes` function says which file a value passed to `Bun.write` is. -const handles = new WeakMap() +// `bytes` function says which file a value passed to `Bun.write` or `fetch` is, and its type. +const handles = new WeakMap() // A run gets `Bun` only with the session's file services (see `CodeModeTool.create`). const service = (key: Context.Key) => @@ -88,7 +88,7 @@ const later = // The part of Bun's BunFile that is implemented. const file = (handle: Handle, type: string, size: number, lastModified: number) => { const bytes = later(() => read(handle)) - handles.set(bytes, handle) + handles.set(bytes, { ...handle, type }) const unlink = later(() => remove(handle)) // As Blob.slice: a negative index counts from the end, and a string in any position is the new type. const index = (value: number | string | undefined, fallback: number) => { @@ -258,6 +258,13 @@ const typeOf = (files: Environment.Files, absolute: string) => const text = (bytes: Uint8Array) => isUtf8(bytes) ? Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString() : undefined +/** A BunFile given as a `fetch` body, read to a Blob of its type, which Bun sends as the Content-Type. Any other value is left as it is. */ +export const body = (value: A) => { + const from = handleOf(value) + if (!from) return Effect.succeed(value) + return Effect.map(read(from), (bytes) => new Blob([new Uint8Array(bytes)], { type: from.type })) +} + const handleOf = (value: unknown) => { const bytes = method(value, "bytes") return bytes && handles.get(bytes) diff --git a/packages/core/src/codemode/fetch.ts b/packages/core/src/codemode/fetch.ts index e7945aa2354..9e107f82f8f 100644 --- a/packages/core/src/codemode/fetch.ts +++ b/packages/core/src/codemode/fetch.ts @@ -6,6 +6,7 @@ import { Effect, Option } from "effect" import { Location } from "../location.js" import { Permission } from "../permission.js" import { CodeModeApi } from "./api.js" +import { CodeModeBun } from "./bun.js" import { CodeModeExecution } from "./execution.js" const TIMEOUT_MS = 30_000 @@ -13,11 +14,24 @@ const TIMEOUT_MS = 30_000 type Init = { readonly method?: string readonly headers?: Record | Array<[string, string]> - readonly body?: string | Uint8Array | URLSearchParams + readonly body?: string | Uint8Array | URLSearchParams | Blob } -const fetch = (input: string | URL, init: Init = {}) => +const fetch = (input: string | URL, given: Init = {}) => Effect.gen(function* () { + // A BunFile reaches here as a plain copy, which `globalThis.fetch` would send as "[object Object]", as it would any + // other object, such as a response or its stream. + const init: Init = { ...given, body: yield* CodeModeBun.body(given.body) } + if ( + typeof init.body === "object" && + init.body !== null && + !(init.body instanceof Uint8Array) && + !(init.body instanceof URLSearchParams) && + !(init.body instanceof Blob) + ) + return yield* Effect.fail( + new TypeError("A fetch body must be a string, a Uint8Array, URLSearchParams, or a file from Bun.file()."), + ) // Rules match the URL as written. The fragment is never sent, and would keep a rule for the exact URL from // matching. const url = String(input).split("#")[0]