Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/js/builtins/ProcessObjectInternals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const enum BunProcessStdinFdType {
file = 0,
pipe = 1,
socket = 2,
// libuv's UV_UNKNOWN_HANDLE: a directory, a block device, or an fd fstat rejects.
unknown = 3,
}

export function getStdioWriteStream(
Expand Down Expand Up @@ -136,6 +138,14 @@ export function getStdinStream(
fdType: BunProcessStdinFdType,
) {
$assert(fd === 0);

if (!isTTY && fdType === BunProcessStdinFdType.unknown) {
// Node's getStdin() default branch. The native stream would throw EISDIR on first read.
const stream = require("internal/worker/stdio").makeEndedReadable();
stream.fd = fd;
return stream;
}

const native = Bun.stdin.stream();
const source = native.$bunNativePtr;

Expand Down
3 changes: 2 additions & 1 deletion src/jsc/bindings/BunProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2946,6 +2946,7 @@ enum class BunProcessStdinFdType : int32_t {
file = 0,
pipe = 1,
socket = 2,
unknown = 3,
};
extern "C" BunProcessStdinFdType Bun__Process__getStdinFdType(void*, int fd);

Expand Down Expand Up @@ -3008,7 +3009,7 @@ static JSValue constructStdioWriteStream(JSC::JSGlobalObject* globalObject, JSC:
// Pipes (and sockets): synchronous on Windows, asynchronous on POSIX
bool forceSync = false;
#if OS(WINDOWS)
forceSync = fdType == BunProcessStdinFdType::file || fdType == BunProcessStdinFdType::pipe;
forceSync = fdType != BunProcessStdinFdType::socket;
#else
// Note: files are always sync anyway.
// forceSync = fdType == BunProcessStdinFdType::file || bun_stdio_tty[fd];
Expand Down
5 changes: 4 additions & 1 deletion src/jsc/rare_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,8 @@ enum StdinFdType {
File = 0,
Pipe = 1,
Socket = 2,
/// libuv's `UV_UNKNOWN_HANDLE`: a directory, a block device, or a failed `fstat`.
Unknown = 3,
}

#[unsafe(no_mangle)]
Expand Down Expand Up @@ -1080,7 +1082,8 @@ extern "C" fn Bun__Process__getStdinFdType(vm: &VirtualMachine, fd: i32) -> Stdi
match bun_sys::kind_from_mode(mode) {
bun_sys::FileKind::NamedPipe => StdinFdType::Pipe,
bun_sys::FileKind::UnixDomainSocket => StdinFdType::Socket,
_ => StdinFdType::File,
bun_sys::FileKind::File | bun_sys::FileKind::CharacterDevice => StdinFdType::File,
_ => StdinFdType::Unknown,
}
}

Expand Down
33 changes: 33 additions & 0 deletions test/js/node/process/process-stdin.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe, isASAN, isDebug, isWindows, tempDir } from "harness";
import { exec } from "node:child_process";
import { closeSync, openSync } from "node:fs";

test.concurrent("pipe does the right thing", async () => {
// Note: Bun.spawnSync uses memfd_create on Linux for pipe, which means we see
Expand Down Expand Up @@ -420,6 +421,38 @@ test.concurrent("pause() and resume() churn while data is in flight never destro
expect(exitCode).toBe(0);
});

// Windows cannot hand a directory handle to a child as stdin.
test.concurrent.skipIf(isWindows)("process.stdin ends when fd 0 is a directory, like node", async () => {
using dir = tempDir("stdin-directory", {});
const dirFd = openSync(String(dir), "r");
try {
await using proc = Bun.spawn({
cmd: [
bunExe(),
"-e",
`const events = [];
process.stdin
.on("error", e => events.push("error:" + e.code))
.on("end", () => events.push("end"))
.on("close", () => events.push("close"))
.resume();
console.log(process.stdin.isTTY, process.stdin.fd);
process.on("exit", () => console.log(events.join(",")));`,
],
stdin: dirFd,
stdout: "pipe",
stderr: "pipe",
env: bunEnv,
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
expect(stdout).toBe("undefined 0\nend,close\n");
expect(exitCode).toBe(0);
} finally {
closeSync(dirFd);
}
});

// The native FileReader source over a pollable pipe used to drain the fd to
// EAGAIN regardless of JS demand, so an idle consumer still ingested the whole
// pipe into an internal buffer. The kernel pipe buffer filling up is the
Expand Down
Loading