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

fs.createReadStream throws with start parameter #1090

Open
Indidev opened this issue Jan 20, 2025 · 0 comments
Open

fs.createReadStream throws with start parameter #1090

Indidev opened this issue Jan 20, 2025 · 0 comments

Comments

@Indidev
Copy link

Indidev commented Jan 20, 2025

Apparently the node implementation of Buffer.copy changed somewhere between v18.12 and v18.20, which leads to all copy calls with sourceStart > buffer.length throwing an Error.

This affects the fs.createReadStream method when called with a start parameter:

Example:

Replace the code inside of createReadstream.ts in the demo folder to:

import { fs } from '../src/index';

fs.writeFileSync('/streamTest', '# Hello World');
const rs = fs.createReadStream('/streamTest', {encoding: 'utf8', start: 0});
rs.on('data', data => {
  console.log(`data: "${data}"`)
});

Run with ts-node demo/createReadStream.ts (or transpile and run with node) results in:

 ts-node demo/createReadStream.ts 
data # Hello World
RangeError [ERR_OUT_OF_RANGE]: The value of "sourceStart" is out of range. It must be >= 0 && <= 13. Received 65536
    at new NodeError (node:internal/errors:405:5)
    at _copy (node:buffer:227:13)
    at Buffer.copy (node:buffer:821:12)
    at Node.read (/home/breitdo/dev/git/test/memfs/src/node.ts:195:14)
    at File.read (/home/breitdo/dev/git/test/memfs/src/node.ts:542:29)
    at Volume.readBase (/home/breitdo/dev/git/test/memfs/src/volume.ts:861:17)
    at Immediate.<anonymous> (/home/breitdo/dev/git/test/memfs/src/volume.ts:899:28)
    at processImmediate (node:internal/timers:476:21) {
  code: 'ERR_OUT_OF_RANGE'
}

Issue:
The culprit is in src/node.ts:194:

this.buf.copy(buf2, off, pos, pos + actualLen);
// First call: pos = 0, actualLen = 13
// Second call: pos = 65536 (default chunk size), actualLen = -65523 (buffer.length - pos)

Possible fix:
Inserting another check after line 185 fixes this issue:

if (pos >= this.buf.length) return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant