Skip to content

Commit

Permalink
feat: add guardrails to the instantiation of a Profiler (#2226)
Browse files Browse the repository at this point in the history
Authored-by: debaduttakore <[email protected]>
Reviewed-by: @wbt @maverick1872 
Merged-by: maverick1872 <[email protected]>
Ref: 2091
  • Loading branch information
debadutta98 authored Aug 9, 2023
1 parent 23cb80c commit 914b846
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
16 changes: 9 additions & 7 deletions lib/winston/profiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
*/

'use strict';

/**
* 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
Expand All @@ -21,12 +20,13 @@ module.exports = class Profiler {
* @private
*/
constructor(logger) {
if (!logger) {
throw new Error('Logger is required for profiling.');
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();
}

this.logger = logger;
this.start = Date.now();
}

/**
Expand All @@ -49,3 +49,5 @@ module.exports = class Profiler {
return this.logger.write(info);
}
};

Check warning on line 51 in lib/winston/profiler.js

View workflow job for this annotation

GitHub Actions / Tests (16)

Unnecessary semicolon

Check warning on line 51 in lib/winston/profiler.js

View workflow job for this annotation

GitHub Actions / Tests (18)

Unnecessary semicolon

Check warning on line 51 in lib/winston/profiler.js

View workflow job for this annotation

GitHub Actions / Tests (20)

Unnecessary semicolon

module.exports = Profiler;
54 changes: 40 additions & 14 deletions test/unit/winston/profiler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@
*/

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 () {
var profiler = new Profiler();
}).throws();
new Profiler();
}).throws('Logger is required for profiling');
});

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',
Expand All @@ -36,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');
})
});

0 comments on commit 914b846

Please sign in to comment.