-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
3324d72
commit dadc606
Showing
17 changed files
with
511 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>; |
Oops, something went wrong.