Skip to content

Commit a16d5b4

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

File tree

1 file changed

+74
-8
lines changed

1 file changed

+74
-8
lines changed

lib/internal/fs/utils.js

Lines changed: 74 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,47 @@ 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+
enumerable: true,
473+
get: function() {
474+
const value = dateFromMs(this.atimeMs);
475+
ObjectDefineProperty(this, 'atime', { __proto__: null, value });
476+
return this.atime;
477+
}
478+
});
479+
480+
ObjectDefineProperty(BigIntStats.prototype, 'mtime', {
481+
enumerable: true,
482+
get: function() {
483+
const value = dateFromMs(this.mtimeMs);
484+
ObjectDefineProperty(this, 'mtime', { __proto__: null, value });
485+
return this.mtime;
486+
}
487+
});
488+
489+
ObjectDefineProperty(BigIntStats.prototype, 'ctime', {
490+
enumerable: true,
491+
get: function() {
492+
const value = dateFromMs(this.ctimeMs);
493+
ObjectDefineProperty(this, 'ctime', { __proto__: null, value });
494+
return this.ctime;
495+
}
496+
});
497+
498+
ObjectDefineProperty(BigIntStats.prototype, 'birthtime', {
499+
enumerable: true,
500+
get: function() {
501+
const value = dateFromMs(this.birthtimeMs);
502+
ObjectDefineProperty(this, 'birthtime', { __proto__: null, value });
503+
return this.birthtime;
504+
}
505+
});
506+
474507
BigIntStats.prototype._checkModeProperty = function(property) {
475508
if (isWindows && (property === S_IFIFO || property === S_IFBLK ||
476509
property === S_IFSOCK)) {
@@ -488,15 +521,48 @@ function Stats(dev, mode, nlink, uid, gid, rdev, blksize,
488521
this.mtimeMs = mtimeMs;
489522
this.ctimeMs = ctimeMs;
490523
this.birthtimeMs = birthtimeMs;
491-
this.atime = dateFromMs(atimeMs);
492-
this.mtime = dateFromMs(mtimeMs);
493-
this.ctime = dateFromMs(ctimeMs);
494-
this.birthtime = dateFromMs(birthtimeMs);
495524
}
496525

497526
ObjectSetPrototypeOf(Stats.prototype, StatsBase.prototype);
498527
ObjectSetPrototypeOf(Stats, StatsBase);
499528

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

0 commit comments

Comments
 (0)