Enable ReadableStream as stdin for Bun.spawn (#20582)

Co-authored-by: Jarred-Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: jarred <jarred@bun.sh>
Co-authored-by: pfg <pfg@pfg.pw>
This commit is contained in:
Jarred Sumner
2025-06-27 19:42:03 -07:00
committed by GitHub
parent 7839844abb
commit 1d48f91b5e
16 changed files with 2039 additions and 59 deletions

View File

@@ -113,6 +113,40 @@ proc.stdin.flush();
proc.stdin.end();
```
The `ReadableStream` option lets you pipe data from a JavaScript `ReadableStream` directly to the subprocess's input:
```ts
const stream = new ReadableStream({
start(controller) {
controller.enqueue("Hello from ");
controller.enqueue("ReadableStream!");
controller.close();
},
});
const proc = Bun.spawn(["cat"], {
stdin: stream,
stdout: "pipe",
});
const output = await new Response(proc.stdout).text();
console.log(output); // "Hello from ReadableStream!"
```
This is particularly useful for streaming data from HTTP responses, file streams, or any other source that provides a `ReadableStream`:
```ts
// Stream an HTTP response through a subprocess
const response = await fetch("https://example.com/large-file.txt");
const proc = Bun.spawn(["gzip"], {
stdin: response.body,
stdout: "pipe",
});
// Save the compressed output
await Bun.write("compressed.gz", proc.stdout);
```
## Output streams
You can read results from the subprocess via the `stdout` and `stderr` properties. By default these are instances of `ReadableStream`.