diff options
author | Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> | 2024-07-02 19:33:32 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-02 19:33:32 -0700 |
commit | dadc606419096325ef0b03b6216ee20bef442045 (patch) | |
tree | a09293a1f852d03d13a2b6eb9e4dfb3f552fb7a0 /ext/node/polyfills/_fs | |
parent | 3324d7203e8d00bb39cfae104e0aec61ba954e9b (diff) |
fix(ext/node): Add `fs.lutimes` / `fs.lutimesSync` (#23172)
Part of #18218
- Adds `fs.lutimes` and `fs.lutimesSync` to our node polyfills. To do
this I added methods to the `FileSystem` trait + ops to expose the
functionality to JS.
- Exports `fs._toUnixTimestamp`. Node exposes an internal util
`toUnixTimestamp` from the fs module to be used by unit tests (so we
need it for the unit test to pass unmodified). It's weird because it's
only supposed to be used internally but it's still publicly accessible
- Matches up error handling and timestamp handling for fs.futimes and
fs.utimes with node
- Enables the node_compat utimes test - this exercises futimes, lutimes,
and utimes.
Diffstat (limited to 'ext/node/polyfills/_fs')
-rw-r--r-- | ext/node/polyfills/_fs/_fs_futimes.ts | 16 | ||||
-rw-r--r-- | ext/node/polyfills/_fs/_fs_lutimes.ts | 85 | ||||
-rw-r--r-- | ext/node/polyfills/_fs/_fs_utimes.ts | 13 |
3 files changed, 108 insertions, 6 deletions
diff --git a/ext/node/polyfills/_fs/_fs_futimes.ts b/ext/node/polyfills/_fs/_fs_futimes.ts index cc4e35b0b..98cd1066c 100644 --- a/ext/node/polyfills/_fs/_fs_futimes.ts +++ b/ext/node/polyfills/_fs/_fs_futimes.ts @@ -5,6 +5,9 @@ import type { CallbackWithError } from "ext:deno_node/_fs/_fs_common.ts"; import { FsFile } from "ext:deno_fs/30_fs.js"; +import { validateInteger } from "ext:deno_node/internal/validators.mjs"; +import { ERR_INVALID_ARG_TYPE } from "ext:deno_node/internal/errors.ts"; +import { toUnixTimestamp } from "ext:deno_node/internal/fs/utils.mjs"; function getValidTime( time: number | string | Date, @@ -23,7 +26,7 @@ function getValidTime( ); } - return time; + return toUnixTimestamp(time); } export function futimes( @@ -35,6 +38,11 @@ export function futimes( if (!callback) { throw new Deno.errors.InvalidData("No callback function supplied"); } + if (typeof fd !== "number") { + throw new ERR_INVALID_ARG_TYPE("fd", "number", fd); + } + + validateInteger(fd, "fd", 0, 2147483647); atime = getValidTime(atime, "atime"); mtime = getValidTime(mtime, "mtime"); @@ -51,6 +59,12 @@ export function futimesSync( atime: number | string | Date, mtime: number | string | Date, ) { + if (typeof fd !== "number") { + throw new ERR_INVALID_ARG_TYPE("fd", "number", fd); + } + + validateInteger(fd, "fd", 0, 2147483647); + atime = getValidTime(atime, "atime"); mtime = getValidTime(mtime, "mtime"); diff --git a/ext/node/polyfills/_fs/_fs_lutimes.ts b/ext/node/polyfills/_fs/_fs_lutimes.ts new file mode 100644 index 000000000..2475c5714 --- /dev/null +++ b/ext/node/polyfills/_fs/_fs_lutimes.ts @@ -0,0 +1,85 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +// deno-lint-ignore-file prefer-primordials + +import type { CallbackWithError } from "ext:deno_node/_fs/_fs_common.ts"; +import { type Buffer } from "node:buffer"; +import { primordials } from "ext:core/mod.js"; +import { op_node_lutimes, op_node_lutimes_sync } from "ext:core/ops"; +import { promisify } from "ext:deno_node/internal/util.mjs"; +import { + getValidatedPath, + toUnixTimestamp, +} from "ext:deno_node/internal/fs/utils.mjs"; + +const { MathTrunc } = primordials; + +type TimeLike = number | string | Date; +type PathLike = string | Buffer | URL; + +function getValidUnixTime( + value: TimeLike, + name: string, +): [number, number] { + if (typeof value === "string") { + value = Number(value); + } + + if ( + typeof value === "number" && + (Number.isNaN(value) || !Number.isFinite(value)) + ) { + throw new Deno.errors.InvalidData( + `invalid ${name}, must not be infinity or NaN`, + ); + } + + const unixSeconds = toUnixTimestamp(value); + + const seconds = MathTrunc(unixSeconds); + const nanoseconds = MathTrunc((unixSeconds * 1e3) - (seconds * 1e3)) * 1e6; + + return [ + seconds, + nanoseconds, + ]; +} + +export function lutimes( + path: PathLike, + atime: TimeLike, + mtime: TimeLike, + callback: CallbackWithError, +): void { + if (!callback) { + throw new Error("No callback function supplied"); + } + const [atimeSecs, atimeNanos] = getValidUnixTime(atime, "atime"); + const [mtimeSecs, mtimeNanos] = getValidUnixTime(mtime, "mtime"); + + path = getValidatedPath(path).toString(); + + op_node_lutimes(path, atimeSecs, atimeNanos, mtimeSecs, mtimeNanos).then( + () => callback(null), + callback, + ); +} + +export function lutimesSync( + path: PathLike, + atime: TimeLike, + mtime: TimeLike, +): void { + const { 0: atimeSecs, 1: atimeNanos } = getValidUnixTime(atime, "atime"); + const { 0: mtimeSecs, 1: mtimeNanos } = getValidUnixTime(mtime, "mtime"); + + path = getValidatedPath(path).toString(); + + op_node_lutimes_sync(path, atimeSecs, atimeNanos, mtimeSecs, mtimeNanos); +} + +export const lutimesPromise = promisify(lutimes) as ( + path: PathLike, + atime: TimeLike, + mtime: TimeLike, +) => Promise<void>; diff --git a/ext/node/polyfills/_fs/_fs_utimes.ts b/ext/node/polyfills/_fs/_fs_utimes.ts index 1d0e5c1ff..3fff4a462 100644 --- a/ext/node/polyfills/_fs/_fs_utimes.ts +++ b/ext/node/polyfills/_fs/_fs_utimes.ts @@ -4,13 +4,16 @@ // deno-lint-ignore-file prefer-primordials import type { CallbackWithError } from "ext:deno_node/_fs/_fs_common.ts"; -import { pathFromURL } from "ext:deno_web/00_infra.js"; import { promisify } from "ext:deno_node/internal/util.mjs"; +import { + getValidatedPath, + toUnixTimestamp, +} from "ext:deno_node/internal/fs/utils.mjs"; function getValidTime( time: number | string | Date, name: string, -): number | Date { +): number { if (typeof time === "string") { time = Number(time); } @@ -24,7 +27,7 @@ function getValidTime( ); } - return time; + return toUnixTimestamp(time); } export function utimes( @@ -33,7 +36,7 @@ export function utimes( mtime: number | string | Date, callback: CallbackWithError, ) { - path = path instanceof URL ? pathFromURL(path) : path; + path = getValidatedPath(path).toString(); if (!callback) { throw new Deno.errors.InvalidData("No callback function supplied"); @@ -56,7 +59,7 @@ export function utimesSync( atime: number | string | Date, mtime: number | string | Date, ) { - path = path instanceof URL ? pathFromURL(path) : path; + path = getValidatedPath(path).toString(); atime = getValidTime(atime, "atime"); mtime = getValidTime(mtime, "mtime"); |