mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 03:24:54 +00:00
fix(core): send Bun.file bodies from Code Mode fetch (#65917)
This commit is contained in:
parent
8c80aff92b
commit
a5232b2057
2 changed files with 26 additions and 5 deletions
|
|
@ -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<Function, Handle>()
|
||||
// `bytes` function says which file a value passed to `Bun.write` or `fetch` is, and its type.
|
||||
const handles = new WeakMap<Function, Handle & { readonly type: string }>()
|
||||
|
||||
// A run gets `Bun` only with the session's file services (see `CodeModeTool.create`).
|
||||
const service = <I, S>(key: Context.Key<I, S>) =>
|
||||
|
|
@ -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 = <A>(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)
|
||||
|
|
|
|||
|
|
@ -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<string, string> | Array<[string, string]>
|
||||
readonly body?: string | Uint8Array<ArrayBuffer> | URLSearchParams
|
||||
readonly body?: string | Uint8Array<ArrayBuffer> | 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]
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue