Skip to content

Commit 5f7192a

Browse files
fix(ext/node/fs): position argument not applied
1 parent a0ddf73 commit 5f7192a

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

ext/node/polyfills/internal/fs/handle.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,27 @@ export class FileHandle extends EventEmitter {
2828
#rid: number;
2929
constructor(rid: number) {
3030
super();
31-
this.rid = rid;
31+
this.#rid = rid;
3232
}
3333

3434
get fd() {
35-
return this.rid;
35+
return this.#rid;
3636
}
3737

3838
read(
39-
buffer: Buffer,
39+
buffer: Uint8Array,
4040
offset?: number,
4141
length?: number,
4242
position?: number | null,
4343
): Promise<ReadResult>;
4444
read(options?: ReadOptions): Promise<ReadResult>;
4545
read(
46-
bufferOrOpt: Buffer | ReadOptions,
46+
bufferOrOpt: Uint8Array | ReadOptions,
4747
offset?: number,
4848
length?: number,
4949
position?: number | null,
5050
): Promise<ReadResult> {
51-
if (bufferOrOpt instanceof Buffer) {
51+
if (bufferOrOpt instanceof Uint8Array) {
5252
return new Promise((resolve, reject) => {
5353
read(
5454
this.fd,
@@ -90,12 +90,12 @@ export class FileHandle extends EventEmitter {
9090
encoding: string,
9191
): Promise<WriteResult>;
9292
write(
93-
bufferOrStr: Buffer | string,
93+
bufferOrStr: Uint8Array | string,
9494
offsetOrPosition: number,
9595
lengthOrEncoding: number | string,
9696
position?: number,
9797
): Promise<WriteResult> {
98-
if (bufferOrStr instanceof Buffer) {
98+
if (bufferOrStr instanceof Uint8Array) {
9999
const buffer = bufferOrStr;
100100
const offset = offsetOrPosition;
101101
const length = lengthOrEncoding;

tests/unit_node/_fs/_fs_write_test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,27 @@ Deno.test({
4949
assertEquals(decoder.decode(data), "hello");
5050
},
5151
});
52+
53+
Deno.test({
54+
name: "Data is padded if position > length",
55+
async fn() {
56+
const tempFile: string = Deno.makeTempFileSync();
57+
58+
using file = await Deno.open(tempFile, {
59+
create: true,
60+
write: true,
61+
read: true,
62+
});
63+
64+
const str = "hello world";
65+
const buffer = Buffer.from(str);
66+
const bytesWritten = writeSync(file.rid, buffer, 0, str.length, 4);
67+
68+
const data = Deno.readFileSync(tempFile);
69+
Deno.removeSync(tempFile);
70+
71+
assertEquals(bytesWritten, str.length);
72+
// Check if result is padded
73+
assertEquals(decoder.decode(data), "\x00\x00\x00\x00hello world");
74+
},
75+
});

tests/unit_node/fs_test.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ import {
1515
statSync,
1616
writeFileSync,
1717
} from "node:fs";
18-
import { constants as fsPromiseConstants, cp } from "node:fs/promises";
18+
import {
19+
constants as fsPromiseConstants,
20+
cp,
21+
FileHandle,
22+
open,
23+
writeFile,
24+
} from "node:fs/promises";
1925
import process from "node:process";
2026
import { pathToAbsoluteFileUrl } from "../unit/test_util.ts";
2127

@@ -165,3 +171,33 @@ Deno.test(
165171
assertEquals(result, undefined);
166172
},
167173
);
174+
175+
// Test for https://github.com/denoland/deno/issues/23707
176+
Deno.test(
177+
"[node/fs/promises read] respect position argument",
178+
async () => {
179+
const file = mkdtempSync(join(tmpdir(), "foo-")) + "/test.bin";
180+
await writeFile(file, "");
181+
182+
const res: number[] = [];
183+
let fd: FileHandle | undefined;
184+
try {
185+
fd = await open(file, "r+");
186+
187+
for (let i = 0; i <= 5; i++) {
188+
const buffer = new Uint8Array([i]);
189+
await fd.write(buffer, 0, 1, i + 10);
190+
}
191+
192+
for (let i = 10; i <= 15; i++) {
193+
const buffer = new Uint8Array(1);
194+
await fd.read(buffer, 0, 1, i);
195+
res.push(Number(buffer.toString()));
196+
}
197+
} finally {
198+
await fd?.close();
199+
}
200+
201+
assertEquals(res, [0, 1, 2, 3, 4, 5]);
202+
},
203+
);

0 commit comments

Comments
 (0)