1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00
denoland-deno/ext/node/polyfills/_fs/_fs_futimes.ts
Nathan Whitaker dadc606419
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.
2024-07-02 19:33:32 -07:00

73 lines
2 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 { 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,
name: string,
): number | Date {
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 futimes(
fd: number,
atime: number | string | Date,
mtime: number | string | Date,
callback: CallbackWithError,
) {
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");
// TODO(@littledivy): Treat `fd` as real file descriptor.
new FsFile(fd, Symbol.for("Deno.internal.FsFile")).utime(atime, mtime).then(
() => callback(null),
callback,
);
}
export function futimesSync(
fd: number,
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");
// TODO(@littledivy): Treat `fd` as real file descriptor.
new FsFile(fd, Symbol.for("Deno.internal.FsFile")).utimeSync(atime, mtime);
}