mirror of
https://github.com/denoland/deno.git
synced 2024-12-30 11:16:38 -05:00
dadc606419
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.
67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
// TODO(petamoriken): enable prefer-primordials for node polyfills
|
|
// deno-lint-ignore-file prefer-primordials
|
|
|
|
import type { CallbackWithError } from "ext:deno_node/_fs/_fs_common.ts";
|
|
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 {
|
|
if (typeof time === "string") {
|
|
time = Number(time);
|
|
}
|
|
|
|
if (
|
|
typeof time === "number" &&
|
|
(Number.isNaN(time) || !Number.isFinite(time))
|
|
) {
|
|
throw new Deno.errors.InvalidData(
|
|
`invalid ${name}, must not be infinity or NaN`,
|
|
);
|
|
}
|
|
|
|
return toUnixTimestamp(time);
|
|
}
|
|
|
|
export function utimes(
|
|
path: string | URL,
|
|
atime: number | string | Date,
|
|
mtime: number | string | Date,
|
|
callback: CallbackWithError,
|
|
) {
|
|
path = getValidatedPath(path).toString();
|
|
|
|
if (!callback) {
|
|
throw new Deno.errors.InvalidData("No callback function supplied");
|
|
}
|
|
|
|
atime = getValidTime(atime, "atime");
|
|
mtime = getValidTime(mtime, "mtime");
|
|
|
|
Deno.utime(path, atime, mtime).then(() => callback(null), callback);
|
|
}
|
|
|
|
export const utimesPromise = promisify(utimes) as (
|
|
path: string | URL,
|
|
atime: number | string | Date,
|
|
mtime: number | string | Date,
|
|
) => Promise<void>;
|
|
|
|
export function utimesSync(
|
|
path: string | URL,
|
|
atime: number | string | Date,
|
|
mtime: number | string | Date,
|
|
) {
|
|
path = getValidatedPath(path).toString();
|
|
atime = getValidTime(atime, "atime");
|
|
mtime = getValidTime(mtime, "mtime");
|
|
|
|
Deno.utimeSync(path, atime, mtime);
|
|
}
|