diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/js/node/stream/node-stream.test.js | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/test/js/node/stream/node-stream.test.js b/test/js/node/stream/node-stream.test.js index 6bff28b94..51252a5f6 100644 --- a/test/js/node/stream/node-stream.test.js +++ b/test/js/node/stream/node-stream.test.js @@ -1,5 +1,8 @@ import { expect, describe, it } from "bun:test"; import { Readable, Writable, Duplex, Transform, PassThrough } from "node:stream"; +import { createReadStream } from "node:fs"; +import { tmpdir } from "node:os"; +import { writeFileSync } from "node:fs"; describe("Readable", () => { it("should be able to be created without _construct method defined", done => { @@ -38,6 +41,85 @@ describe("Readable", () => { readable.pipe(writable); }); + it("should be able to be piped via .pipe, issue #3668", done => { + const path = `${tmpdir()}/${Date.now()}.testReadStream.txt`; + writeFileSync(path, "12345"); + const stream = createReadStream(path, { start: 0, end: 4 }); + + const writable = new Writable({ + write(chunk, encoding, callback) { + try { + expect(chunk.toString()).toBe("12345"); + } catch (err) { + done(err); + return; + } + callback(); + done(); + }, + }); + + stream.on("error", err => { + done(err); + }); + + stream.pipe(writable); + }); + it("should be able to be piped via .pipe, both start and end are 0", done => { + const path = `${tmpdir()}/${Date.now()}.testReadStream2.txt`; + writeFileSync(path, "12345"); + const stream = createReadStream(path, { start: 0, end: 0 }); + + const writable = new Writable({ + write(chunk, encoding, callback) { + try { + // Both start and end are inclusive and start counting at 0. + expect(chunk.toString()).toBe("1"); + } catch (err) { + done(err); + return; + } + callback(); + done(); + }, + }); + + stream.on("error", err => { + done(err); + }); + + stream.pipe(writable); + }); + it("should be able to be piped via .pipe with a large file", done => { + const length = 128 * 1024; + const data = "B".repeat(length); + const path = `${tmpdir()}/${Date.now()}.testReadStreamLargeFile.txt`; + writeFileSync(path, data); + const stream = createReadStream(path, { start: 0, end: length - 1 }); + + let res = ""; + let count = 0; + const writable = new Writable({ + write(chunk, encoding, callback) { + count += 1; + res += chunk; + callback(); + }, + }); + writable.on("finish", () => { + try { + expect(res).toEqual(data); + expect(count).toBeGreaterThan(1); + } catch (err) { + return done(err); + } + done(); + }); + stream.on("error", err => { + done(err); + }); + stream.pipe(writable); + }); }); describe("Duplex", () => { |