Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const {
Number,
NumberIsFinite,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectIs,
ObjectSetPrototypeOf,
ReflectOwnKeys,
Expand Down Expand Up @@ -48,6 +47,7 @@ const {
once,
deprecate,
isWindows,
setOwnProperty,
} = require('internal/util');
const { toPathIfFileURL } = require('internal/url');
const {
Expand Down Expand Up @@ -449,43 +449,43 @@ const lazyDateFields = {
enumerable: true,
configurable: true,
get() {
return this.atime = dateFromMs(this.atimeMs);
return setOwnProperty(this, 'atime', dateFromMs(this.atimeMs));
},
set(value) {
ObjectDefineProperty(this, 'atime', { __proto__: null, value, writable: true });
setOwnProperty(this, 'atime', value);
},
},
mtime: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
return this.mtime = dateFromMs(this.mtimeMs);
return setOwnProperty(this, 'mtime', dateFromMs(this.mtimeMs));
},
set(value) {
ObjectDefineProperty(this, 'mtime', { __proto__: null, value, writable: true });
setOwnProperty(this, 'mtime', value);
},
},
ctime: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
return this.ctime = dateFromMs(this.ctimeMs);
return setOwnProperty(this, 'ctime', dateFromMs(this.ctimeMs));
},
set(value) {
ObjectDefineProperty(this, 'ctime', { __proto__: null, value, writable: true });
setOwnProperty(this, 'ctime', value);
},
},
birthtime: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
return this.birthtime = dateFromMs(this.birthtimeMs);
return setOwnProperty(this, 'birthtime', dateFromMs(this.birthtimeMs));
},
set(value) {
ObjectDefineProperty(this, 'birthtime', { __proto__: null, value, writable: true });
setOwnProperty(this, 'birthtime', value);
},
},
};
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,13 +752,14 @@ function filterOwnProperties(source, keys) {
* @returns {any}
*/
function setOwnProperty(obj, key, value) {
return ObjectDefineProperty(obj, key, {
ObjectDefineProperty(obj, key, {
__proto__: null,
configurable: true,
enumerable: true,
value,
writable: true,
});
return value;
}

let internalGlobal;
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-fs-stat-date.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ function closeEnough(actual, expected, margin) {
`expected ${expected} ± ${margin}, got ${actual}`);
}

// Ensure that accessed atime and mtime are enumerable
function validateEnumerability(stats) {
const keys = Object.keys(stats);
assert.ok(keys.includes('atime'));
assert.ok(keys.includes('mtime'));
}

async function runTest(atime, mtime, margin = 0) {
margin += Number.EPSILON;
try {
Expand All @@ -56,24 +63,28 @@ async function runTest(atime, mtime, margin = 0) {
closeEnough(stats.mtimeMs, mtime, margin);
closeEnough(stats.atime.getTime(), new Date(atime).getTime(), margin);
closeEnough(stats.mtime.getTime(), new Date(mtime).getTime(), margin);
validateEnumerability(stats);

const statsBigint = await fsPromises.stat(filepath, { bigint: true });
closeEnough(statsBigint.atimeMs, BigInt(atime), margin);
closeEnough(statsBigint.mtimeMs, BigInt(mtime), margin);
closeEnough(statsBigint.atime.getTime(), new Date(atime).getTime(), margin);
closeEnough(statsBigint.mtime.getTime(), new Date(mtime).getTime(), margin);
validateEnumerability(statsBigint);

const statsSync = fs.statSync(filepath);
closeEnough(statsSync.atimeMs, atime, margin);
closeEnough(statsSync.mtimeMs, mtime, margin);
closeEnough(statsSync.atime.getTime(), new Date(atime).getTime(), margin);
closeEnough(statsSync.mtime.getTime(), new Date(mtime).getTime(), margin);
validateEnumerability(statsSync);

const statsSyncBigint = fs.statSync(filepath, { bigint: true });
closeEnough(statsSyncBigint.atimeMs, BigInt(atime), margin);
closeEnough(statsSyncBigint.mtimeMs, BigInt(mtime), margin);
closeEnough(statsSyncBigint.atime.getTime(), new Date(atime).getTime(), margin);
closeEnough(statsSyncBigint.mtime.getTime(), new Date(mtime).getTime(), margin);
validateEnumerability(statsSyncBigint);
}

// Too high/low numbers produce too different results on different platforms
Expand Down
Loading