Skip to content

Commit db5ae30

Browse files
authored
fix(internal): Use Bun.stdin when using Bun to prevent process hanging (#760)
Bun's node:process stdin implementation has multiple issues related to raw mode, resuming, and ending it too early. To work around this, this adds a custom branch for the read function when running under Bun. It uses Bun's own stdin implementation to read one chunk and then dispose the reader. Fixes #759
1 parent dfb1762 commit db5ae30

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

internal/runtime/read.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@
44
* @internal
55
* @param data Uint8Array to store the data.
66
*/
7-
export function read(data: Uint8Array): Promise<number | null> {
7+
export async function read(data: Uint8Array): Promise<number | null> {
88
// deno-lint-ignore no-explicit-any
9-
const { Deno, process } = globalThis as any;
9+
const { Deno, Bun, process } = globalThis as any;
1010

1111
if (Deno) {
12-
return Deno.stdin.read(data);
12+
return await Deno.stdin.read(data);
13+
} else if (Bun) {
14+
const reader = Bun.stdin.stream().getReader();
15+
const { value: buffer } = await reader.read();
16+
await reader.cancel();
17+
for (let i = 0; i < buffer.length; i++) {
18+
data[i] = buffer[i];
19+
}
20+
return buffer.length;
1321
} else if (process) {
14-
return new Promise((resolve, reject) => {
22+
return await new Promise((resolve, reject) => {
1523
process.stdin.once("readable", () => {
1624
try {
1725
const buffer = process.stdin.read();

0 commit comments

Comments
 (0)