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(node/fs): position option of fs.read and fs.readSync works the same as Node #2669

Merged
merged 2 commits into from
Sep 19, 2022

Conversation

PolarETech
Copy link
Contributor

The behavior of position option in fs.read and fs.readSync is different between Deno and Node.
This PR fixes the behavior of Deno to be the same as Node's.

Actual Behavior

Node v18.9.0

If position is an integer,

  • Specifies the number of bytes to start reading counting from the beginning of the file
  • The file position will be unchanged

Ref: https://nodejs.org/api/fs.html#fsreadfd-buffer-offset-length-position-callback

Deno 1.25.3 with [email protected]

If position is an integer,

  • Specifies the number of bytes to start reading counting from the current position of the file
  • The file position will be updated

Sample code

// import for Node
import fs from 'fs';
import { Buffer } from 'node:buffer';
// or
// import for Deno
import fs from "https://deno.land/[email protected]/node/fs.ts";
import { Buffer } from "https://deno.land/[email protected]/node/buffer.ts";


const file = "./test.txt";
const text = "hello, world!";
const bufSize = 3;
const positions = [2, null, 2];
const data = [];

fs.writeFileSync(file, text);
const fd = fs.openSync(file);

for (const pos of positions) {
  const buf = Buffer.alloc(bufSize);
  fs.readSync(fd, buf, 0, buf.byteLength, pos);
  data.push(buf.toString("utf8"));
}

fs.closeSync(fd);
console.log(data);

Execution result in Node

[ 'llo', 'hel', 'llo' ] // [ 2-4 byte, 0-2 byte, 2-4 byte ]

Execution result in Deno

[ "llo", ", w", "ld!" ] // [ 2-4 byte, 5-7 byte, 10-12 byte ]

Tests

No applicable test for this issue has been found in the Node test suite. Unit tests have been updated.

Remarks

This issue has been found while implementing compatibility improvements to fs.ReadStream. #2653

@PolarETech
Copy link
Contributor Author

There are two ci / node failures, but they appear to be issues outside the scope of this PR.

@PolarETech PolarETech marked this pull request as ready for review September 18, 2022 08:03
@PolarETech
Copy link
Contributor Author

There was a lack of consideration regarding error handling.
A fix commit has been added.

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Good catch!

@kt3k
Copy link
Member

kt3k commented Sep 19, 2022

The CI failures should be fixed by denoland/deno#15954

@cjihrig cjihrig merged commit 1a8631d into denoland:main Sep 19, 2022
@PolarETech PolarETech deleted the fix-read-position branch September 20, 2022 05:56
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

Successfully merging this pull request may close these issues.

3 participants