From 757fc13f5ab1a636da17c32b65dd0305c6c410a3 Mon Sep 17 00:00:00 2001 From: Debadutta Panda Date: Sat, 29 Oct 2022 11:50:46 +0530 Subject: [PATCH 1/7] fix #2091 --- lib/winston/profiler.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/winston/profiler.js b/lib/winston/profiler.js index edcc5a65a..c9dae7ca6 100644 --- a/lib/winston/profiler.js +++ b/lib/winston/profiler.js @@ -6,7 +6,6 @@ */ 'use strict'; - /** * TODO: add class description. * @type {Profiler} @@ -21,7 +20,7 @@ module.exports = class Profiler { * @private */ constructor(logger) { - if (!logger) { + if (!logger && logger?.constructor?.name !== 'Logger') { throw new Error('Logger is required for profiling.'); } From 84b374b65fc446573b4342678468aee3b95e10a9 Mon Sep 17 00:00:00 2001 From: Debadutta Panda Date: Sat, 29 Oct 2022 11:59:35 +0530 Subject: [PATCH 2/7] Add guardrails for Profiler --- lib/winston/profiler.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/winston/profiler.js b/lib/winston/profiler.js index c9dae7ca6..271c4c509 100644 --- a/lib/winston/profiler.js +++ b/lib/winston/profiler.js @@ -20,12 +20,16 @@ module.exports = class Profiler { * @private */ constructor(logger) { - if (!logger && logger?.constructor?.name !== 'Logger') { + if (!logger) { + throw new Error('Logger is required for profiling.'); + } + + if (logger?.constructor?.name === 'Logger'){ + this.logger = logger; + this.start = Date.now(); + } else { throw new Error('Logger is required for profiling.'); } - - this.logger = logger; - this.start = Date.now(); } /** From 23ac781acd549730b430821c6ac252e7b9950fb9 Mon Sep 17 00:00:00 2001 From: Debadutta Panda Date: Sat, 29 Oct 2022 13:33:16 +0530 Subject: [PATCH 3/7] change profiler testcase and force profiler to avoid object that is not a instance of logger --- lib/winston/profiler.js | 4 ++-- test/unit/winston/profiler.test.js | 25 ++++++++++++------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/winston/profiler.js b/lib/winston/profiler.js index 271c4c509..d379f3088 100644 --- a/lib/winston/profiler.js +++ b/lib/winston/profiler.js @@ -23,8 +23,8 @@ module.exports = class Profiler { if (!logger) { throw new Error('Logger is required for profiling.'); } - - if (logger?.constructor?.name === 'Logger'){ + const name = logger?.constructor?.name; + if (name === 'Logger' || name === 'DerivedLogger'){ this.logger = logger; this.start = Date.now(); } else { diff --git a/test/unit/winston/profiler.test.js b/test/unit/winston/profiler.test.js index b0f86e95b..6b4dbd75a 100644 --- a/test/unit/winston/profiler.test.js +++ b/test/unit/winston/profiler.test.js @@ -7,27 +7,26 @@ */ const assume = require('assume'); +const Logger = require('../../../lib/winston/logger'); const Profiler = require('../../../lib/winston/profiler'); - describe('Profiler', function () { it('new Profiler()', function () { assume(function () { - var profiler = new Profiler(); + new Profiler(); }).throws(); }); it('.done({ info })', function (done) { - var profiler = new Profiler({ - write: function (info) { - assume(info).is.an('object'); - assume(info.something).equals('ok'); - assume(info.level).equals('info'); - assume(info.durationMs).is.a('number'); - assume(info.message).equals('testing1'); - done(); - } - }); - + const logger = new Logger(); + logger.write = function (info) { + assume(info).is.an('object'); + assume(info.something).equals('ok'); + assume(info.level).equals('info'); + assume(info.durationMs).is.a('number'); + assume(info.message).equals('testing1'); + done(); + }; + var profiler = new Profiler(logger); setTimeout(function () { profiler.done({ something: 'ok', From fae7aa876e9a0c658692b0f590a6a32244e17878 Mon Sep 17 00:00:00 2001 From: Debadutta Panda Date: Sat, 29 Oct 2022 17:37:51 +0530 Subject: [PATCH 4/7] update profiler --- lib/winston/profiler.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/winston/profiler.js b/lib/winston/profiler.js index d379f3088..6533f6e9c 100644 --- a/lib/winston/profiler.js +++ b/lib/winston/profiler.js @@ -6,6 +6,7 @@ */ 'use strict'; +const Logger = require('./logger'); /** * TODO: add class description. * @type {Profiler} @@ -23,8 +24,8 @@ module.exports = class Profiler { if (!logger) { throw new Error('Logger is required for profiling.'); } - const name = logger?.constructor?.name; - if (name === 'Logger' || name === 'DerivedLogger'){ + + if (logger instanceof Logger.constructor){ this.logger = logger; this.start = Date.now(); } else { From 2ff0628749f76fd3362a04e5c95e184afb571168 Mon Sep 17 00:00:00 2001 From: Debadutta Panda Date: Mon, 31 Oct 2022 21:17:11 +0530 Subject: [PATCH 5/7] fix lint --- lib/winston/profiler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/winston/profiler.js b/lib/winston/profiler.js index 6533f6e9c..2a86c2e46 100644 --- a/lib/winston/profiler.js +++ b/lib/winston/profiler.js @@ -25,7 +25,7 @@ module.exports = class Profiler { throw new Error('Logger is required for profiling.'); } - if (logger instanceof Logger.constructor){ + if (logger instanceof Logger.constructor) { this.logger = logger; this.start = Date.now(); } else { From f60d4a5b8b0964757c88f5789276d7cce46d99b8 Mon Sep 17 00:00:00 2001 From: Debadutta Panda Date: Sat, 24 Jun 2023 22:42:05 +0530 Subject: [PATCH 6/7] add additional testcase --- lib/winston/profiler.js | 16 +++++++--------- test/unit/winston/profiler.test.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/lib/winston/profiler.js b/lib/winston/profiler.js index 2a86c2e46..4511194ac 100644 --- a/lib/winston/profiler.js +++ b/lib/winston/profiler.js @@ -6,13 +6,12 @@ */ 'use strict'; -const Logger = require('./logger'); /** * TODO: add class description. * @type {Profiler} * @private */ -module.exports = class Profiler { +class Profiler { /** * Constructor function for the Profiler instance used by * `Logger.prototype.startTimer`. When done is called the timer will finish @@ -21,15 +20,12 @@ module.exports = class Profiler { * @private */ constructor(logger) { - if (!logger) { - throw new Error('Logger is required for profiling.'); - } - - if (logger instanceof Logger.constructor) { + const Logger = require('./logger'); + if (typeof logger !== 'object' || Array.isArray(logger) || !(logger instanceof Logger)) { + throw new Error('Logger is required for profiling'); + } else { this.logger = logger; this.start = Date.now(); - } else { - throw new Error('Logger is required for profiling.'); } } @@ -53,3 +49,5 @@ module.exports = class Profiler { return this.logger.write(info); } }; + +module.exports = Profiler; \ No newline at end of file diff --git a/test/unit/winston/profiler.test.js b/test/unit/winston/profiler.test.js index 6b4dbd75a..d4d993c05 100644 --- a/test/unit/winston/profiler.test.js +++ b/test/unit/winston/profiler.test.js @@ -9,11 +9,12 @@ const assume = require('assume'); const Logger = require('../../../lib/winston/logger'); const Profiler = require('../../../lib/winston/profiler'); +const { PassThrough } = require('stream'); describe('Profiler', function () { it('new Profiler()', function () { assume(function () { new Profiler(); - }).throws(); + }).throws('Logger is required for profiling'); }); it('.done({ info })', function (done) { @@ -35,4 +36,30 @@ describe('Profiler', function () { }); }, 200); }); + + it('non logger object', function(){ + assume(function() { + new Profiler(new Error('Unknown error')); + }).throws('Logger is required for profiling'); + + assume(function () { + new Profiler({a:'b'}); + }).throws('Logger is required for profiling'); + + assume(function(){ + new Profiler([1,2,3,4]); + }).throws('Logger is required for profiling'); + + assume(function () { + new Profiler(new PassThrough()); + }).throws('Logger is required for profiling'); + + assume(function () { + new Profiler(2); + }).throws('Logger is required for profiling'); + + assume(function () { + new Profiler('1'); + }).throws('Logger is required for profiling'); + }) }); From 6ab14197de81304c407eeccba726538c8d671c5c Mon Sep 17 00:00:00 2001 From: debaduttakore <139857436+debaduttakore@users.noreply.github.com> Date: Sat, 5 Aug 2023 21:43:45 +0530 Subject: [PATCH 7/7] add a new line on profiler file --- lib/winston/profiler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/winston/profiler.js b/lib/winston/profiler.js index 4511194ac..d632fcee5 100644 --- a/lib/winston/profiler.js +++ b/lib/winston/profiler.js @@ -50,4 +50,4 @@ class Profiler { } }; -module.exports = Profiler; \ No newline at end of file +module.exports = Profiler;