From a16d5b457976a6399297076d1da45c4e871e6567 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sat, 25 Nov 2023 14:47:04 -0500 Subject: [PATCH] fs: load dates lazily --- lib/internal/fs/utils.js | 82 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 8 deletions(-) diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 9f5ad9bf5f6c0e..22d9d7ec77b586 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -12,6 +12,7 @@ const { NumberIsFinite, MathMin, MathRound, + ObjectDefineProperty, ObjectIs, ObjectSetPrototypeOf, ReflectApply, @@ -462,15 +463,47 @@ function BigIntStats(dev, mode, nlink, uid, gid, rdev, blksize, this.mtimeNs = mtimeNs; this.ctimeNs = ctimeNs; this.birthtimeNs = birthtimeNs; - this.atime = dateFromMs(this.atimeMs); - this.mtime = dateFromMs(this.mtimeMs); - this.ctime = dateFromMs(this.ctimeMs); - this.birthtime = dateFromMs(this.birthtimeMs); } ObjectSetPrototypeOf(BigIntStats.prototype, StatsBase.prototype); ObjectSetPrototypeOf(BigIntStats, StatsBase); +ObjectDefineProperty(BigIntStats.prototype, 'atime', { + enumerable: true, + get: function() { + const value = dateFromMs(this.atimeMs); + ObjectDefineProperty(this, 'atime', { __proto__: null, value }); + return this.atime; + } +}); + +ObjectDefineProperty(BigIntStats.prototype, 'mtime', { + enumerable: true, + get: function() { + const value = dateFromMs(this.mtimeMs); + ObjectDefineProperty(this, 'mtime', { __proto__: null, value }); + return this.mtime; + } +}); + +ObjectDefineProperty(BigIntStats.prototype, 'ctime', { + enumerable: true, + get: function() { + const value = dateFromMs(this.ctimeMs); + ObjectDefineProperty(this, 'ctime', { __proto__: null, value }); + return this.ctime; + } +}); + +ObjectDefineProperty(BigIntStats.prototype, 'birthtime', { + enumerable: true, + get: function() { + const value = dateFromMs(this.birthtimeMs); + ObjectDefineProperty(this, 'birthtime', { __proto__: null, value }); + return this.birthtime; + } +}); + BigIntStats.prototype._checkModeProperty = function(property) { if (isWindows && (property === S_IFIFO || property === S_IFBLK || property === S_IFSOCK)) { @@ -488,15 +521,48 @@ function Stats(dev, mode, nlink, uid, gid, rdev, blksize, 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); } ObjectSetPrototypeOf(Stats.prototype, StatsBase.prototype); ObjectSetPrototypeOf(Stats, StatsBase); +ObjectDefineProperty(Stats.prototype, 'atime', { + enumerable: true, + get: function() { + const value = dateFromMs(this.atimeMs); + ObjectDefineProperty(this, 'atime', { __proto__: null, value }); + return this.atime; + } +}); + +ObjectDefineProperty(Stats.prototype, 'mtime', { + enumerable: true, + get: function() { + console.log('getter called mtime') + const value = dateFromMs(this.mtimeMs); + ObjectDefineProperty(this, 'mtime', { __proto__: null, value }); + return this.mtime; + } +}); + +ObjectDefineProperty(Stats.prototype, 'ctime', { + enumerable: true, + get: function() { + const value = dateFromMs(this.ctimeMs); + ObjectDefineProperty(this, 'ctime', { __proto__: null, value }); + return this.ctime; + } +}); + +ObjectDefineProperty(Stats.prototype, 'birthtime', { + enumerable: true, + get: function() { + const value = dateFromMs(this.birthtimeMs); + ObjectDefineProperty(this, 'birthtime', { __proto__: null, value }); + return this.birthtime; + } +}); + // HACK: Workaround for https://github.com/standard-things/esm/issues/821. // TODO(ronag): Remove this as soon as `esm` publishes a fixed version. Stats.prototype.isFile = StatsBase.prototype.isFile;