Skip to content

Commit 3f648cd

Browse files
committed
fs: load dates lazily
1 parent 7981e2e commit 3f648cd

File tree

1 file changed

+81
-8
lines changed

1 file changed

+81
-8
lines changed

lib/internal/fs/utils.js

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
NumberIsFinite,
1313
MathMin,
1414
MathRound,
15+
ObjectDefineProperty,
1516
ObjectIs,
1617
ObjectSetPrototypeOf,
1718
ReflectApply,
@@ -462,15 +463,51 @@ function BigIntStats(dev, mode, nlink, uid, gid, rdev, blksize,
462463
this.mtimeNs = mtimeNs;
463464
this.ctimeNs = ctimeNs;
464465
this.birthtimeNs = birthtimeNs;
465-
this.atime = dateFromMs(this.atimeMs);
466-
this.mtime = dateFromMs(this.mtimeMs);
467-
this.ctime = dateFromMs(this.ctimeMs);
468-
this.birthtime = dateFromMs(this.birthtimeMs);
469466
}
470467

471468
ObjectSetPrototypeOf(BigIntStats.prototype, StatsBase.prototype);
472469
ObjectSetPrototypeOf(BigIntStats, StatsBase);
473470

471+
ObjectDefineProperty(BigIntStats.prototype, 'atime', {
472+
__proto__: null,
473+
enumerable: true,
474+
get: function() {
475+
const value = dateFromMs(this.atimeMs);
476+
ObjectDefineProperty(this, 'atime', { __proto__: null, value });
477+
return this.atime;
478+
},
479+
});
480+
481+
ObjectDefineProperty(BigIntStats.prototype, 'mtime', {
482+
__proto__: null,
483+
enumerable: true,
484+
get: function() {
485+
const value = dateFromMs(this.mtimeMs);
486+
ObjectDefineProperty(this, 'mtime', { __proto__: null, value });
487+
return this.mtime;
488+
},
489+
});
490+
491+
ObjectDefineProperty(BigIntStats.prototype, 'ctime', {
492+
__proto__: null,
493+
enumerable: true,
494+
get: function() {
495+
const value = dateFromMs(this.ctimeMs);
496+
ObjectDefineProperty(this, 'ctime', { __proto__: null, value });
497+
return this.ctime;
498+
},
499+
});
500+
501+
ObjectDefineProperty(BigIntStats.prototype, 'birthtime', {
502+
__proto__: null,
503+
enumerable: true,
504+
get: function() {
505+
const value = dateFromMs(this.birthtimeMs);
506+
ObjectDefineProperty(this, 'birthtime', { __proto__: null, value });
507+
return this.birthtime;
508+
},
509+
});
510+
474511
BigIntStats.prototype._checkModeProperty = function(property) {
475512
if (isWindows && (property === S_IFIFO || property === S_IFBLK ||
476513
property === S_IFSOCK)) {
@@ -488,15 +525,51 @@ function Stats(dev, mode, nlink, uid, gid, rdev, blksize,
488525
this.mtimeMs = mtimeMs;
489526
this.ctimeMs = ctimeMs;
490527
this.birthtimeMs = birthtimeMs;
491-
this.atime = dateFromMs(atimeMs);
492-
this.mtime = dateFromMs(mtimeMs);
493-
this.ctime = dateFromMs(ctimeMs);
494-
this.birthtime = dateFromMs(birthtimeMs);
495528
}
496529

497530
ObjectSetPrototypeOf(Stats.prototype, StatsBase.prototype);
498531
ObjectSetPrototypeOf(Stats, StatsBase);
499532

533+
ObjectDefineProperty(Stats.prototype, 'atime', {
534+
__proto__: null,
535+
enumerable: true,
536+
get: function() {
537+
const value = dateFromMs(this.atimeMs);
538+
ObjectDefineProperty(this, 'atime', { __proto__: null, value });
539+
return this.atime;
540+
},
541+
});
542+
543+
ObjectDefineProperty(Stats.prototype, 'mtime', {
544+
__proto__: null,
545+
enumerable: true,
546+
get: function() {
547+
const value = dateFromMs(this.mtimeMs);
548+
ObjectDefineProperty(this, 'mtime', { __proto__: null, value });
549+
return this.mtime;
550+
},
551+
});
552+
553+
ObjectDefineProperty(Stats.prototype, 'ctime', {
554+
__proto__: null,
555+
enumerable: true,
556+
get: function() {
557+
const value = dateFromMs(this.ctimeMs);
558+
ObjectDefineProperty(this, 'ctime', { __proto__: null, value });
559+
return this.ctime;
560+
},
561+
});
562+
563+
ObjectDefineProperty(Stats.prototype, 'birthtime', {
564+
__proto__: null,
565+
enumerable: true,
566+
get: function() {
567+
const value = dateFromMs(this.birthtimeMs);
568+
ObjectDefineProperty(this, 'birthtime', { __proto__: null, value });
569+
return this.birthtime;
570+
},
571+
});
572+
500573
// HACK: Workaround for https://github.com/standard-things/esm/issues/821.
501574
// TODO(ronag): Remove this as soon as `esm` publishes a fixed version.
502575
Stats.prototype.isFile = StatsBase.prototype.isFile;

0 commit comments

Comments
 (0)