Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guardrails to Profiler class #2226

Merged
merged 10 commits into from
Aug 9, 2023
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);
}
};

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');
})
});