ReadableStream .text(), .json(), .arrayBuffer(), .bytes() (#20694)

Co-authored-by: pfgithub <6010774+pfgithub@users.noreply.github.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
This commit is contained in:
pfg
2025-06-28 00:27:57 -07:00
committed by GitHub
parent dd67cda545
commit 3223da2734
11 changed files with 303 additions and 32 deletions

View File

@@ -34,7 +34,7 @@ const proc = Bun.spawn(["cat"], {
),
});
const text = await new Response(proc.stdout).text();
const text = await proc.stdout.text();
console.log(text); // "const input = "hello world".repeat(400); ..."
```
@@ -139,8 +139,8 @@ You can read results from the subprocess via the `stdout` and `stderr` propertie
```ts
const proc = Bun.spawn(["bun", "--version"]);
const text = await new Response(proc.stdout).text();
console.log(text); // => "$BUN_LATEST_VERSION"
const text = await proc.stdout.text();
console.log(text); // => "$BUN_LATEST_VERSION\n"
```
Configure the output stream by passing one of the following values to `stdout/stderr`:

View File

@@ -13,14 +13,14 @@ proc.stderr; // => ReadableStream
---
To read `stderr` until the child process exits, use the [`Bun.readableStreamToText()`](https://bun.sh/docs/api/utils#bun-readablestreamto) convenience function.
To read `stderr` until the child process exits, use .text()
```ts
const proc = Bun.spawn(["echo", "hello"], {
stderr: "pipe",
});
const errors: string = await Bun.readableStreamToText(proc.stderr);
const errors: string = await proc.stderr.text();
if (errors) {
// handle errors
}

View File

@@ -7,7 +7,7 @@ When using [`Bun.spawn()`](https://bun.sh/docs/api/spawn), the `stdout` of the c
```ts
const proc = Bun.spawn(["echo", "hello"]);
const output = await new Response(proc.stdout).text();
const output = await proc.stdout.text();
output; // => "hello"
```

View File

@@ -32,8 +32,8 @@ By default, the `stdout` of the child process can be consumed as a `ReadableStre
```ts
const proc = Bun.spawn(["echo", "hello"]);
const output = await new Response(proc.stdout).text();
output; // => "hello"
const output = await proc.stdout.text();
output; // => "hello\n"
```
---