From ccbc92f1be52b65b53bdb3467f9186e8ea44ff75 Mon Sep 17 00:00:00 2001 From: Daniel Pihlstrom Date: Thu, 4 May 2017 00:26:31 +0200 Subject: [PATCH 1/5] lib: lazy instantiation of fs dates --- lib/fs.js | 71 +++++++++++++++++++++++++++++++++-- test/parallel/test-fs-stat.js | 16 ++++++++ 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 7469875d71cc12..bc066e52202d3a 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -196,13 +196,76 @@ function Stats( this.ino = ino; this.size = size; this.blocks = blocks; - this.atime = new Date(atim_msec + 0.5); - this.mtime = new Date(mtim_msec + 0.5); - this.ctime = new Date(ctim_msec + 0.5); - this.birthtime = new Date(birthtim_msec + 0.5); + this._atim_msec = atim_msec; + this._mtim_msec = mtim_msec; + this._ctim_msec = ctim_msec; + this._birthtim_msec = birthtim_msec; } fs.Stats = Stats; +Object.defineProperties(Stats.prototype, { + atime: { + configurable: true, + enumerable: true, + get() { + return this._atime !== undefined ? + this._atime : + (this._atime = new Date(this._atim_msec + 0.5)); + }, + set(value) { return this._atime = value; } + }, + mtime: { + configurable: true, + enumerable: true, + get() { + return this._mtime !== undefined ? + this._mtime : + (this._mtime = new Date(this._mtim_msec + 0.5)); + }, + set(value) { return this._mtime = value; } + }, + ctime: { + configurable: true, + enumerable: true, + get() { + return this._ctime !== undefined ? + this._ctime : + (this._ctime = new Date(this._ctim_msec + 0.5)); + }, + set(value) { return this._ctime = value; } + }, + birthtime: { + configurable: true, + enumerable: true, + get() { + return this._birthtime !== undefined ? + this._birthtime : + (this._birthtime = new Date(this._birthtim_msec + 0.5)); + }, + set(value) { return this._birthtime = value; } + }, +}); + +Stats.prototype.toJSON = function toJSON() { + return { + dev: this.dev, + mode: this.mode, + nlink: this.nlink, + uid: this.uid, + gid: this.gid, + rdev: this.rdev, + blksize: this.blksize, + ino: this.ino, + size: this.size, + blocks: this.blocks, + atime: this.atime, + ctime: this.ctime, + mtime: this.mtime, + birthtime: this.birthtime + }; +} + + Stats.prototype._checkModeProperty = function(property) { return ((this.mode & S_IFMT) === property); }; diff --git a/test/parallel/test-fs-stat.js b/test/parallel/test-fs-stat.js index 6ed27806b9343c..eb5677986e7038 100644 --- a/test/parallel/test-fs-stat.js +++ b/test/parallel/test-fs-stat.js @@ -98,5 +98,21 @@ fs.stat(__filename, common.mustCall(function(err, s) { console.log(`isSymbolicLink: ${JSON.stringify(s.isSymbolicLink())}`); assert.strictEqual(false, s.isSymbolicLink()); + assert.ok(s.atime instanceof Date); assert.ok(s.mtime instanceof Date); + assert.ok(s.ctime instanceof Date); + assert.ok(s.birthtime instanceof Date); +})); + +fs.stat(__filename, common.mustCall(function(err, s) { + const json = JSON.parse(JSON.stringify(s)); + const keys = [ + 'dev', 'mode', 'nlink', 'uid', + 'gid', 'rdev', 'blksize', 'ino', + 'size', 'blocks', 'atime', 'mtime', + 'ctime', 'birthtime' + ]; + keys.forEach(function(k) { + assert.ok(json[k] !== undefined && json[k] !== null); + }); })); From 71b01c6f7f6d366c3366e6bbcd815137694272a3 Mon Sep 17 00:00:00 2001 From: Daniel Pihlstrom Date: Thu, 4 May 2017 00:41:57 +0200 Subject: [PATCH 2/5] lint --- lib/fs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fs.js b/lib/fs.js index bc066e52202d3a..26a7ddc35250dd 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -263,7 +263,7 @@ Stats.prototype.toJSON = function toJSON() { mtime: this.mtime, birthtime: this.birthtime }; -} +}; Stats.prototype._checkModeProperty = function(property) { From dcbe6b21d24aaf4b652f055f88004428abe17625 Mon Sep 17 00:00:00 2001 From: Daniel Pihlstrom Date: Mon, 22 May 2017 22:50:54 +0200 Subject: [PATCH 3/5] remove blksize from test --- test/parallel/test-fs-stat.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-fs-stat.js b/test/parallel/test-fs-stat.js index eb5677986e7038..67b1246e263d40 100644 --- a/test/parallel/test-fs-stat.js +++ b/test/parallel/test-fs-stat.js @@ -108,11 +108,11 @@ fs.stat(__filename, common.mustCall(function(err, s) { const json = JSON.parse(JSON.stringify(s)); const keys = [ 'dev', 'mode', 'nlink', 'uid', - 'gid', 'rdev', 'blksize', 'ino', + 'gid', 'rdev', 'ino', 'size', 'blocks', 'atime', 'mtime', 'ctime', 'birthtime' ]; keys.forEach(function(k) { - assert.ok(json[k] !== undefined && json[k] !== null); + assert.ok(json[k] !== undefined && json[k] !== null, k + ' should not be null or undefined'); }); })); From 1eb7196a31e42ce303f4145379f5affae888674e Mon Sep 17 00:00:00 2001 From: Daniel Pihlstrom Date: Tue, 23 May 2017 00:10:43 +0200 Subject: [PATCH 4/5] lint --- test/parallel/test-fs-stat.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-fs-stat.js b/test/parallel/test-fs-stat.js index 67b1246e263d40..56f96bb22e1fbf 100644 --- a/test/parallel/test-fs-stat.js +++ b/test/parallel/test-fs-stat.js @@ -113,6 +113,9 @@ fs.stat(__filename, common.mustCall(function(err, s) { 'ctime', 'birthtime' ]; keys.forEach(function(k) { - assert.ok(json[k] !== undefined && json[k] !== null, k + ' should not be null or undefined'); + assert.ok( + json[k] !== undefined && json[k] !== null, + k + ' should not be null or undefined' + ); }); })); From 61ff1382f1c83187542368027ccd84577dfb5bd6 Mon Sep 17 00:00:00 2001 From: Daniel Pihlstrom Date: Tue, 23 May 2017 08:16:13 +0200 Subject: [PATCH 5/5] blocks as well, why not --- test/parallel/test-fs-stat.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-fs-stat.js b/test/parallel/test-fs-stat.js index 56f96bb22e1fbf..0769f79f1c502d 100644 --- a/test/parallel/test-fs-stat.js +++ b/test/parallel/test-fs-stat.js @@ -109,9 +109,12 @@ fs.stat(__filename, common.mustCall(function(err, s) { const keys = [ 'dev', 'mode', 'nlink', 'uid', 'gid', 'rdev', 'ino', - 'size', 'blocks', 'atime', 'mtime', + 'size', 'atime', 'mtime', 'ctime', 'birthtime' ]; + if (!common.isWindows) { + keys.push('blocks', 'blksize'); + } keys.forEach(function(k) { assert.ok( json[k] !== undefined && json[k] !== null,