diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2024-03-06 18:29:10 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-06 13:59:10 +0100 |
commit | 6ba0b7952d1ca867d0e166de4d36dcd6fe489e89 (patch) | |
tree | 9791b1dca4dbba002d61123d295ffca017785e7a /ext/node/polyfills/_fs/_fs_stat.ts | |
parent | 156950828e8c25f9de346c7cd737f1d0ebc8c1fb (diff) |
fix(node): stat/statSync returns instance of fs.Stats (#22294)
Fixes https://github.com/denoland/deno/issues/22291
---------
Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com>
Diffstat (limited to 'ext/node/polyfills/_fs/_fs_stat.ts')
-rw-r--r-- | ext/node/polyfills/_fs/_fs_stat.ts | 101 |
1 files changed, 93 insertions, 8 deletions
diff --git a/ext/node/polyfills/_fs/_fs_stat.ts b/ext/node/polyfills/_fs/_fs_stat.ts index 119faecee..de9b69ba3 100644 --- a/ext/node/polyfills/_fs/_fs_stat.ts +++ b/ext/node/polyfills/_fs/_fs_stat.ts @@ -5,13 +5,16 @@ import { denoErrorToNodeError } from "ext:deno_node/internal/errors.ts"; import { promisify } from "ext:deno_node/internal/util.mjs"; +import { primordials } from "ext:core/mod.js"; + +const { ObjectCreate, ObjectAssign } = primordials; export type statOptions = { bigint: boolean; throwIfNoEntry?: boolean; }; -export type Stats = { +interface IStats { /** ID of the device containing the file. * * _Linux/Mac OS only._ */ @@ -80,9 +83,84 @@ export type Stats = { isFile: () => boolean; isSocket: () => boolean; isSymbolicLink: () => boolean; -}; +} + +class StatsBase { + constructor( + dev, + mode, + nlink, + uid, + gid, + rdev, + blksize, + ino, + size, + blocks, + ) { + this.dev = dev; + this.mode = mode; + this.nlink = nlink; + this.uid = uid; + this.gid = gid; + this.rdev = rdev; + this.blksize = blksize; + this.ino = ino; + this.size = size; + this.blocks = blocks; + } +} + +// The Date constructor performs Math.floor() to the timestamp. +// https://www.ecma-international.org/ecma-262/#sec-timeclip +// Since there may be a precision loss when the timestamp is +// converted to a floating point number, we manually round +// the timestamp here before passing it to Date(). +function dateFromMs(ms) { + return new Date(Number(ms) + 0.5); +} -export type BigIntStats = { +export class Stats extends StatsBase { + constructor( + dev, + mode, + nlink, + uid, + gid, + rdev, + blksize, + ino, + size, + blocks, + atimeMs, + mtimeMs, + ctimeMs, + birthtimeMs, + ) { + super( + dev, + mode, + nlink, + uid, + gid, + rdev, + blksize, + ino, + size, + blocks, + ); + this.atimeMs = atimeMs; + this.mtimeMs = mtimeMs; + this.ctimeMs = ctimeMs; + this.birthtimeMs = birthtimeMs; + this.atime = dateFromMs(atimeMs); + this.mtime = dateFromMs(mtimeMs); + this.ctime = dateFromMs(ctimeMs); + this.birthtime = dateFromMs(birthtimeMs); + } +} + +export interface IBigIntStats { /** ID of the device containing the file. * * _Linux/Mac OS only._ */ @@ -159,10 +237,13 @@ export type BigIntStats = { isFile: () => boolean; isSocket: () => boolean; isSymbolicLink: () => boolean; -}; +} + +export class BigIntStats {} export function convertFileInfoToStats(origin: Deno.FileInfo): Stats { - return { + const stats = ObjectCreate(Stats.prototype); + ObjectAssign(stats, { dev: origin.dev, ino: origin.ino, mode: origin.mode, @@ -189,7 +270,9 @@ export function convertFileInfoToStats(origin: Deno.FileInfo): Stats { isSocket: () => false, ctime: origin.mtime, ctimeMs: origin.mtime?.getTime() || null, - }; + }); + + return stats; } function toBigInt(number?: number | null) { @@ -200,7 +283,8 @@ function toBigInt(number?: number | null) { export function convertFileInfoToBigIntStats( origin: Deno.FileInfo, ): BigIntStats { - return { + const stats = ObjectCreate(BigIntStats.prototype); + ObjectAssign(stats, { dev: toBigInt(origin.dev), ino: toBigInt(origin.ino), mode: toBigInt(origin.mode), @@ -233,7 +317,8 @@ export function convertFileInfoToBigIntStats( ctime: origin.mtime, ctimeMs: origin.mtime ? BigInt(origin.mtime.getTime()) : null, ctimeNs: origin.mtime ? BigInt(origin.mtime.getTime()) * 1000000n : null, - }; + }); + return stats; } // shortcut for Convert File Info to Stats or BigIntStats |