fix(core): return a real ArrayBuffer from BunFile.arrayBuffer in Code Mode

This commit is contained in:
Aiden Cline 2026-11-18 17:09:06 +00:00
parent 9fda73df47
commit 5c17cdaea2
2 changed files with 18 additions and 6 deletions

View file

@ -111,8 +111,8 @@ const file = (handle: Handle, type: string, size: number, lastModified: number)
),
),
bytes,
// The runtime has no ArrayBuffer: one crosses to a program as a Uint8Array.
arrayBuffer: later(() => read(handle)),
// A copy, so the buffer holds exactly the file's bytes.
arrayBuffer: later(() => read(handle).pipe(Effect.map((bytes) => bytes.slice().buffer))),
exists: later(() => exists(handle)),
stat: later(() => stat(handle)),
slice: (...args: ReadonlyArray<number | string | undefined>): unknown => {
@ -277,7 +277,9 @@ const contents = (data: unknown) => {
}
const unwritable = () =>
new TypeError("Bun.write expects a string, a Uint8Array, a BunFile, or a Response as the data to write.")
new TypeError(
"Bun.write expects a string, an ArrayBuffer, a typed array, a BunFile, or a Response as the data to write.",
)
// A value from a program is a plain copy, so its functions are among its own entries.
const method = (value: unknown, key: string) => {
@ -291,6 +293,7 @@ const toBytes = (value: unknown) => {
if (typeof value === "string") return Buffer.from(value)
if (typeof value === "number" || typeof value === "boolean") return Buffer.from(String(value))
if (value instanceof Uint8Array) return value
if (ArrayBuffer.isView(value)) return new Uint8Array(value.buffer, value.byteOffset, value.byteLength)
if (value instanceof ArrayBuffer) return new Uint8Array(value)
}

View file

@ -235,8 +235,10 @@ describe("Bun", () => {
const missing = Bun.file("missing.txt")
let error
try { await missing.text() } catch (e) { error = [e.code, e.syscall, e.path] }
const buffer = await notes.slice(6).arrayBuffer()
return [await notes.text(), await Bun.file("data.json").json(), [...await notes.slice(0, 5).bytes()],
await notes.slice(-5).text(), [notes.size, notes.type], await missing.exists(), error]`,
await notes.slice(-5).text(), [notes.size, notes.type], await missing.exists(), error,
[buffer instanceof ArrayBuffer, buffer.byteLength, new TextDecoder().decode(buffer)]]`,
),
).toEqual([
"hello world",
@ -246,6 +248,7 @@ describe("Bun", () => {
[11, "text/plain;charset=utf-8"],
false,
["ENOENT", "open", "missing.txt"],
[true, 5, "world"],
])
})
@ -264,6 +267,8 @@ describe("Bun", () => {
return [
await Bun.write("deep/text.txt", "text"),
await Bun.write("bytes.bin", new Uint8Array([0xff, 0x00])),
await Bun.write("words.bin", new Uint16Array([0x6968]).buffer),
await Bun.file("words.bin").text(),
await Bun.write("copy.txt", Bun.file("notes.txt")),
await failure(() => Bun.write("missing/x.txt", "x", { createPath: false })),
await failure(() => Bun.write("mode.txt", "x", { mode: 0o755 })),
@ -273,13 +278,15 @@ describe("Bun", () => {
).toEqual([
4,
2,
2,
"hi",
5,
"ENOENT",
'Bun.write does not support the "mode" option in Code Mode. Supported options: createPath.',
"one\ntwo\n",
])
expect((await fs.readdir(tmp.path)).toSorted()).toEqual(
["bytes.bin", "copy.txt", "deep", "log.txt", "notes.txt"].toSorted(),
["bytes.bin", "copy.txt", "deep", "log.txt", "notes.txt", "words.bin"].toSorted(),
)
})
@ -454,7 +461,8 @@ describe("streaming fetch", () => {
const bytes = await (await fetch(res.url)).bytes()
const buffer = await (await fetch(res.url)).arrayBuffer()
const empty = await fetch(res.url, { method: "HEAD" })
return [res.headers.get("Content-Type"), before, number, res.bodyUsed, second, [...bytes], [...buffer], empty.body === null]
return [res.headers.get("Content-Type"), before, number, res.bodyUsed, second, [...bytes], buffer instanceof ArrayBuffer,
[...new Uint8Array(buffer)], empty.body === null]
`,
),
)
@ -465,6 +473,7 @@ describe("streaming fetch", () => {
true,
"TypeError",
[52, 50],
true,
[52, 50],
true,
])