diff --git a/src/js/node/stream.js b/src/js/node/stream.js index 74f2859f4bb757..f30f5763d4ad55 100644 --- a/src/js/node/stream.js +++ b/src/js/node/stream.js @@ -3388,9 +3388,21 @@ var require_readable = __commonJS({ }; Readable.fromWeb = function (readableStream, options) { + // We cache .stream() calls for file descriptors + // This won't create a new ReadableStream each time. + let bunStdinStream = Bun.stdin.stream(); + if (readableStream === bunStdinStream) { + return bunStdinStream; + } + return webStreamsAdapters.newStreamReadableFromReadableStream(readableStream, options); }; Readable.toWeb = function (streamReadable, options) { + // Workaround for https://github.com/oven-sh/bun/issues/9041 + if (streamReadable === process.stdin) { + return Bun.stdin.stream(); + } + return webStreamsAdapters.newReadableStreamFromStreamReadable(streamReadable, options); }; Readable.wrap = function (src, options) { diff --git a/test/regression/issue/09041.test.ts b/test/regression/issue/09041.test.ts new file mode 100644 index 00000000000000..caf673bc4c6866 --- /dev/null +++ b/test/regression/issue/09041.test.ts @@ -0,0 +1,26 @@ +import { test, expect } from "bun:test"; +import { bunEnv, bunExe, tempDirWithFiles } from "harness"; +import { join } from "path"; +import { $ } from "bun"; +import { cp, rm } from "fs/promises"; +test("09041", async () => { + const out = tempDirWithFiles("09041", { + "09041-fixture.mjs": await Bun.file(join(import.meta.dir, "09041", "09041-fixture.mjs")).text(), + "09041-fixture.test.js": await Bun.file(join(import.meta.dir, "09041", "09041-fixture-test.txt")).text(), + "package.json": `{}`, + }); + + let { exited, stderr, stdout } = Bun.spawn({ + cmd: [bunExe(), "test"], + cwd: out, + env: bunEnv, + stdio: ["ignore", "pipe", "inherit"], + }); + + expect(await exited).toBe(0); + const err = await new Response(stderr).text(); + expect(err).toContain("1 pass"); + expect(err).toContain("0 fail"); + + await rm(out, { force: true, recursive: true }); +}, 10000); diff --git a/test/regression/issue/09041/09041-fixture-test.txt b/test/regression/issue/09041/09041-fixture-test.txt new file mode 100644 index 00000000000000..c812d521f9e702 --- /dev/null +++ b/test/regression/issue/09041/09041-fixture-test.txt @@ -0,0 +1,18 @@ +import { $ } from "bun"; +import {join } from 'path'; +test("09041", async () => { + const buffer = Buffer.alloc(1024 * 1024); + buffer.fill("1234567890"); + const buns = Array.from( + { length: 5 }, + () => $`${process.argv0} run ${join(import.meta.dir, "09041-fixture.mjs")} < ${buffer}`, + ); + + const runs = await Promise.all(buns); + for (let i = 0; i < runs.length; i++) { + const run = runs[i]; + expect(run.exitCode).toBe(0); + expect(run.stdout).toHaveLength(1024 * 1024); + expect(run.stdout).toEqual(buffer); + } +}, 10000); diff --git a/test/regression/issue/09041/09041-fixture.mjs b/test/regression/issue/09041/09041-fixture.mjs new file mode 100644 index 00000000000000..931d2a56fb836f --- /dev/null +++ b/test/regression/issue/09041/09041-fixture.mjs @@ -0,0 +1,4 @@ +// This file is intended to be able to run in Node and Bun +import { Readable, pipeline, PassThrough } from "node:stream"; + +pipeline(Readable.toWeb(process.stdin), new PassThrough(), process.stdout, () => {});