diff options
Diffstat (limited to 'test/bun.js/streams.test.js')
| -rw-r--r-- | test/bun.js/streams.test.js | 66 |
1 files changed, 63 insertions, 3 deletions
diff --git a/test/bun.js/streams.test.js b/test/bun.js/streams.test.js index 1268a04c0..20aa5c270 100644 --- a/test/bun.js/streams.test.js +++ b/test/bun.js/streams.test.js @@ -1,11 +1,72 @@ -import { file, readableStreamToArrayBuffer, readableStreamToArray } from "bun"; +import { + file, + readableStreamToArrayBuffer, + readableStreamToArray, + readableStreamToText, +} from "bun"; import { expect, it, beforeEach, afterEach } from "bun:test"; -import { writeFileSync } from "node:fs"; +import { mkfifo } from "mkfifo"; +import { unlinkSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; import { gc } from "./gc"; new Uint8Array(); beforeEach(() => gc()); afterEach(() => gc()); +it("Bun.file() read text from pipe", async () => { + try { + unlinkSync("/tmp/fifo"); + } catch (e) {} + + mkfifo("/tmp/fifo", 0o666); + + const large = "HELLO!".repeat((((1024 * 512) / "HELLO!".length) | 0) + 1); + + const chunks = []; + var out = Bun.file("/tmp/fifo").stream(); + const proc = Bun.spawn({ + cmd: [ + "bash", + join(import.meta.dir + "/", "bun-streams-test-fifo.sh"), + "/tmp/fifo", + ], + stderr: "inherit", + env: { + FIFO_TEST: large, + }, + }); + const exited = proc.exited; + proc.ref(); + var prom = new Promise((resolve, reject) => { + setTimeout(() => { + (async function () { + var reader = out.getReader(); + while (true) { + const chunk = await reader.read(); + if (chunk.done) { + if (chunks.length == 0) { + out = Bun.file("/tmp/fifo").stream(); + reader = out.getReader(); + continue; + } + const output = new TextDecoder() + .decode(new Uint8Array(Buffer.concat(chunks))) + .trim(); + resolve(output); + break; + } + chunks.push(chunk.value); + } + })(); + }); + }); + + const [status, output] = await Promise.all([exited, prom]); + + expect(output.length).toBe(large.length); + expect(output).toBe(large); + expect(status).toBe(0); +}); it("exists globally", () => { expect(typeof ReadableStream).toBe("function"); @@ -191,7 +252,6 @@ it("ReadableStream for Blob", async () => { console.error(e); console.error(e.stack); } - if (chunk.done) break; chunks.push(new TextDecoder().decode(chunk.value)); } |
