Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the chunk boundary (node:stream:createReadStream) #3853

Merged
merged 3 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/js/node/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,15 +540,15 @@ ReadStream = (function (InternalReadStream) {
chunk = chunk.slice(-n);
var [_, ...rest] = arguments;
this.pos = this.bytesRead;
if (this.end && this.bytesRead >= this.end) {
chunk = chunk.slice(0, this.end - this.start);
if (this.end !== undefined && this.bytesRead > this.end) {
chunk = chunk.slice(0, this.end - this.start + 1);
}
return super.push(chunk, ...rest);
}
var end = this.end;
// This is multi-chunk read case where we go passed the end of the what we want to read in the last chunk
if (end && this.bytesRead >= end) {
chunk = chunk.slice(0, end - currPos);
if (end !== undefined && this.bytesRead > end) {
chunk = chunk.slice(0, end - currPos + 1);
var [_, ...rest] = arguments;
this.pos = this.bytesRead;
return super.push(chunk, ...rest);
Expand Down
8 changes: 4 additions & 4 deletions src/js/out/modules/node/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,13 @@ ReadStream = function(InternalReadStream) {
var n = this.bytesRead - currPos;
chunk = chunk.slice(-n);
var [_, ...rest] = arguments;
if (this.pos = this.bytesRead, this.end && this.bytesRead >= this.end)
chunk = chunk.slice(0, this.end - this.start);
if (this.pos = this.bytesRead, this.end !== void 0 && this.bytesRead > this.end)
chunk = chunk.slice(0, this.end - this.start + 1);
return super.push(chunk, ...rest);
}
var end = this.end;
if (end && this.bytesRead >= end) {
chunk = chunk.slice(0, end - currPos);
if (end !== void 0 && this.bytesRead > end) {
chunk = chunk.slice(0, end - currPos + 1);
var [_, ...rest] = arguments;
return this.pos = this.bytesRead, super.push(chunk, ...rest);
}
Expand Down
82 changes: 82 additions & 0 deletions test/js/node/stream/node-stream.test.js
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down Expand Up @@ -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", () => {
Expand Down