Allow 0-length ArrayBuffer & Blob in Bun.spawn stdio (#9557)

Co-authored-by: Zack Radisic <zack@theradisic.com>
This commit is contained in:
Jarred Sumner
2024-03-29 13:51:45 -07:00
committed by GitHub
parent e3bf906127
commit e80e61c9a3
2 changed files with 39 additions and 3 deletions

View File

@@ -409,9 +409,10 @@ pub const Stdio = union(enum) {
}
}
} else if (value.asArrayBuffer(globalThis)) |array_buffer| {
if (array_buffer.slice().len == 0) {
globalThis.throwInvalidArguments("ArrayBuffer cannot be empty", .{});
return false;
// Change in Bun v1.0.34: don't throw for empty ArrayBuffer
if (array_buffer.byteSlice().len == 0) {
out_stdio.* = .{ .ignore = {} };
return true;
}
out_stdio.* = .{
@@ -475,6 +476,12 @@ pub const Stdio = union(enum) {
return false;
}
// Instead of writing an empty blob, lets just make it /dev/null
if (blob.fastSize() == 0) {
stdio.* = .{ .ignore = {} };
return true;
}
stdio.* = .{ .blob = blob };
return true;
}

View File

@@ -0,0 +1,29 @@
import { describe, test, expect } from "bun:test";
import { bunEnv, bunExe } from "harness";
describe("spawn with empty", () => {
for (const [stdin, label] of [
[new ArrayBuffer(0), "ArrayBuffer"],
[new Uint8Array(0), "Uint8Array"],
[new Blob([]), "Blob"],
] as const) {
test(label + " for stdin", async () => {
const proc = Bun.spawn({
cmd: [bunExe(), "-e", "process.stdin.pipe(process.stdout)"],
stdin,
stdout: "pipe",
stderr: "pipe",
env: bunEnv,
});
const [exited, stdout, stderr] = await Promise.all([
proc.exited,
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
]);
expect(exited).toBe(0);
expect(stdout).toBeEmpty();
expect(stderr).toBeEmpty();
});
}
});