Skip to content

Commit

Permalink
fix(node/fs): position option of fs.read and fs.readSync works the sa…
Browse files Browse the repository at this point in the history
…me as Node (#2669)
  • Loading branch information
PolarETech authored Sep 19, 2022
1 parent 8650ccd commit 1a8631d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 21 deletions.
24 changes: 18 additions & 6 deletions node/_fs/_fs_read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,18 @@ export function read(
let err: Error | null = null,
numberOfBytesRead: number | null = null;

if (position) {
Deno.seekSync(fd, position, Deno.SeekMode.Current);
}

try {
let currentPosition = 0;
if (typeof position === "number" && position >= 0) {
currentPosition = Deno.seekSync(fd, 0, Deno.SeekMode.Current);
Deno.seekSync(fd, position, Deno.SeekMode.Start);
}

numberOfBytesRead = Deno.readSync(fd, buffer);

if (typeof position === "number" && position >= 0) {
Deno.seekSync(fd, currentPosition, Deno.SeekMode.Start);
}
} catch (error) {
err = error instanceof Error ? error : new Error("[non-error thrown]");
}
Expand Down Expand Up @@ -168,11 +174,17 @@ export function readSync(
}`,
);

if (position) {
Deno.seekSync(fd, position, Deno.SeekMode.Current);
let currentPosition = 0;
if (typeof position === "number" && position >= 0) {
currentPosition = Deno.seekSync(fd, 0, Deno.SeekMode.Current);
Deno.seekSync(fd, position, Deno.SeekMode.Start);
}

const numberOfBytesRead = Deno.readSync(fd, buffer);

if (typeof position === "number" && position >= 0) {
Deno.seekSync(fd, currentPosition, Deno.SeekMode.Start);
}

return numberOfBytesRead ?? 0;
}
81 changes: 66 additions & 15 deletions node/_fs/_fs_read_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,43 @@ Deno.test({
});

Deno.test({
name: "[std/node/fs] Specifies where to begin reading from in the file",
name:
"[std/node/fs] position option of fs.read() specifies where to begin reading from in the file",
async fn() {
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
const testData = path.resolve(moduleDir, "testdata", "hello.txt");
const buf = Buffer.alloc(11);
await readTest(
testData,
buf,
buf.byteOffset,
buf.byteLength,
6,
(_fd, bytesRead, data) => {
assertStrictEquals(bytesRead, 5);
assertEquals(
data,
Buffer.from([119, 111, 114, 108, 100, 0, 0, 0, 0, 0, 0]),
const fd = openSync(testData);
const buf = Buffer.alloc(5);
const positions = [6, 0, -1, null];
const expected = [
[119, 111, 114, 108, 100],
[104, 101, 108, 108, 111],
[104, 101, 108, 108, 111],
[32, 119, 111, 114, 108],
];
for (const [i, position] of positions.entries()) {
await new Promise((resolve) => {
read(
fd,
{
buffer: buf,
offset: buf.byteOffset,
length: buf.byteLength,
position,
},
(err, bytesRead, data) => {
assertEquals(err, null);
assertStrictEquals(bytesRead, 5);
assertEquals(
data,
Buffer.from(expected[i]),
);
return resolve(true);
},
);
},
);
});
}
closeSync(fd);
},
});

Expand Down Expand Up @@ -184,6 +202,39 @@ Deno.test({
},
});

Deno.test({
name:
"[std/node/fs] position option of fs.readSync() specifies where to begin reading from in the file",
fn() {
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
const testData = path.resolve(moduleDir, "testdata", "hello.txt");
const fd = openSync(testData);
const buf = Buffer.alloc(5);
const positions = [6, 0, -1, null];
const expected = [
[119, 111, 114, 108, 100],
[104, 101, 108, 108, 111],
[104, 101, 108, 108, 111],
[32, 119, 111, 114, 108],
];
for (const [i, position] of positions.entries()) {
const bytesRead = readSync(
fd,
buf,
buf.byteOffset,
buf.byteLength,
position,
);
assertStrictEquals(bytesRead, 5);
assertEquals(
buf,
Buffer.from(expected[i]),
);
}
closeSync(fd);
},
});

Deno.test({
name: "[std/node/fs] Read fs.readSync(fd, buffer[, options]) signature",
fn() {
Expand Down

0 comments on commit 1a8631d

Please sign in to comment.