Skip to content

Commit

Permalink
fix(internal): Use Bun.stdin when using Bun to prevent process hanging (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
annervisser authored Nov 5, 2024
1 parent dfb1762 commit db5ae30
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions internal/runtime/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@
* @internal
* @param data Uint8Array to store the data.
*/
export function read(data: Uint8Array): Promise<number | null> {
export async function read(data: Uint8Array): Promise<number | null> {
// deno-lint-ignore no-explicit-any
const { Deno, process } = globalThis as any;
const { Deno, Bun, process } = globalThis as any;

if (Deno) {
return Deno.stdin.read(data);
return await Deno.stdin.read(data);
} else if (Bun) {
const reader = Bun.stdin.stream().getReader();
const { value: buffer } = await reader.read();
await reader.cancel();
for (let i = 0; i < buffer.length; i++) {
data[i] = buffer[i];
}
return buffer.length;
} else if (process) {
return new Promise((resolve, reject) => {
return await new Promise((resolve, reject) => {
process.stdin.once("readable", () => {
try {
const buffer = process.stdin.read();
Expand Down

0 comments on commit db5ae30

Please sign in to comment.