diff options
author | sigmaSd <bedisnbiba@gmail.com> | 2023-10-22 09:02:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-22 08:02:55 +0000 |
commit | 9f9c3d9048108200a888c508328d2a19cbbd6164 (patch) | |
tree | 664847bcef81d34a26d5371378d460b61245eac3 | |
parent | fb73eb1e9dca3e93cc7efcf5c2244e0068733843 (diff) |
fix(polyfill): correctly handle flag when its equal 0 (#20953)
Fixes https://github.com/denoland/deno/issues/20910
-rw-r--r-- | cli/tests/unit_node/_fs/_fs_open_test.ts | 10 | ||||
-rw-r--r-- | cli/tests/unit_node/_fs/_fs_read_test.ts | 15 | ||||
-rw-r--r-- | ext/node/polyfills/_fs/_fs_common.ts | 2 | ||||
-rw-r--r-- | ext/node/polyfills/_fs/_fs_open.ts | 4 | ||||
-rw-r--r-- | ext/node/polyfills/_fs/_fs_read.ts | 2 |
5 files changed, 29 insertions, 4 deletions
diff --git a/cli/tests/unit_node/_fs/_fs_open_test.ts b/cli/tests/unit_node/_fs/_fs_open_test.ts index b64e6c4c5..d9f5e5796 100644 --- a/cli/tests/unit_node/_fs/_fs_open_test.ts +++ b/cli/tests/unit_node/_fs/_fs_open_test.ts @@ -397,4 +397,14 @@ Deno.test("[std/node/fs] open callback isn't called twice if error is thrown", a await Deno.remove(tempFile); }, }); + + Deno.test({ + name: "SYNC: open file with flag set to 0 (readonly)", + fn() { + const file = Deno.makeTempFileSync(); + const fd = openSync(file, 0); + assert(Deno.resources()[fd]); + closeSync(fd); + }, + }); }); diff --git a/cli/tests/unit_node/_fs/_fs_read_test.ts b/cli/tests/unit_node/_fs/_fs_read_test.ts index 66906ccbc..34b029d9f 100644 --- a/cli/tests/unit_node/_fs/_fs_read_test.ts +++ b/cli/tests/unit_node/_fs/_fs_read_test.ts @@ -306,3 +306,18 @@ Deno.test({ await Deno.remove(file); }, }); + +Deno.test({ + name: "SYNC: read with no offsetOropts argument", + fn() { + const moduleDir = path.dirname(path.fromFileUrl(import.meta.url)); + const testData = path.resolve(moduleDir, "testdata", "hello.txt"); + const buffer = Buffer.alloc(1024); + const fd = openSync(testData, "r"); + const _bytesRead = readSync( + fd, + buffer, + ); + closeSync(fd); + }, +}); diff --git a/ext/node/polyfills/_fs/_fs_common.ts b/ext/node/polyfills/_fs/_fs_common.ts index d1dfabc4f..3817ee9f8 100644 --- a/ext/node/polyfills/_fs/_fs_common.ts +++ b/ext/node/polyfills/_fs/_fs_common.ts @@ -108,7 +108,7 @@ export function checkEncoding(encoding: Encodings | null): Encodings | null { export function getOpenOptions( flag: string | number | undefined, ): Deno.OpenOptions { - if (!flag) { + if (flag === undefined) { return { create: true, append: true }; } diff --git a/ext/node/polyfills/_fs/_fs_open.ts b/ext/node/polyfills/_fs/_fs_open.ts index 571a06a5d..fc5defb51 100644 --- a/ext/node/polyfills/_fs/_fs_open.ts +++ b/ext/node/polyfills/_fs/_fs_open.ts @@ -57,8 +57,8 @@ function convertFlagAndModeToOptions( flag?: openFlags, mode?: number, ): Deno.OpenOptions | undefined { - if (!flag && !mode) return undefined; - if (!flag && mode) return { mode }; + if (flag === undefined && mode === undefined) return undefined; + if (flag === undefined && mode) return { mode }; return { ...getOpenOptions(flag), mode }; } diff --git a/ext/node/polyfills/_fs/_fs_read.ts b/ext/node/polyfills/_fs/_fs_read.ts index 6d7efbeef..66d5a4093 100644 --- a/ext/node/polyfills/_fs/_fs_read.ts +++ b/ext/node/polyfills/_fs/_fs_read.ts @@ -167,7 +167,7 @@ export function readSync( if (typeof offsetOrOpt === "number") { offset = offsetOrOpt; validateInteger(offset, "offset", 0); - } else { + } else if (offsetOrOpt !== undefined) { const opt = offsetOrOpt as readSyncOptions; offset = opt.offset ?? 0; length = opt.length ?? buffer.byteLength; |