Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!_.isObject(opts))
throw new BadOptionsError(opts, text.optionsNotObj(typeof opts));
if (_.isUndefined(opts.token))
throw new BadOptionsError(opts, text.noToken());
if (!_.isString(opts.token) || !tokenPattern.test(opts.token))
throw new BadOptionsError(opts, text.invalidToken(opts.token));
// Log method aliases
this[$levels] = levelUtil.normalize(opts);
for (const lvlName of this.levels) {
if (lvlName in this)
throw new BadOptionsError(opts, text.levelConflict(lvlName));
Object.defineProperty(this, lvlName, {
enumerable: true,
writable: false,
value() {
this.log.apply(this, [lvlName, ...arguments]);
}
});
}
const bufferSizeConfig = opts.bufferSize || defaults.bufferSize;
this.ringBuffer = new RingBuffer(bufferSizeConfig);
// Other permanent options
constructor(opts) {
super({
objectMode: true,
highWaterMark: 0
});
// Sanity checks
if (_.isUndefined(opts))
throw new BadOptionsError(opts, text.noOptions());
if (!_.isObject(opts))
throw new BadOptionsError(opts, text.optionsNotObj(typeof opts));
if (_.isUndefined(opts.token))
throw new BadOptionsError(opts, text.noToken());
if (!_.isString(opts.token) || !tokenPattern.test(opts.token))
throw new BadOptionsError(opts, text.invalidToken(opts.token));
// Log method aliases
this[$levels] = levelUtil.normalize(opts);
for (const lvlName of this.levels) {
if (lvlName in this)
throw new BadOptionsError(opts, text.levelConflict(lvlName));
Object.defineProperty(this, lvlName, {
constructor(opts) {
super({
objectMode: true,
highWaterMark: 0
});
// Sanity checks
if (_.isUndefined(opts))
throw new BadOptionsError(opts, text.noOptions());
if (!_.isObject(opts))
throw new BadOptionsError(opts, text.optionsNotObj(typeof opts));
if (_.isUndefined(opts.token))
throw new BadOptionsError(opts, text.noToken());
if (!_.isString(opts.token) || !tokenPattern.test(opts.token))
throw new BadOptionsError(opts, text.invalidToken(opts.token));
// Log method aliases
this[$levels] = levelUtil.normalize(opts);
for (const lvlName of this.levels) {
if (lvlName in this)
constructor(opts) {
super({
objectMode: true,
highWaterMark: 0
});
// Sanity checks
if (_.isUndefined(opts))
throw new BadOptionsError(opts, text.noOptions());
if (!_.isObject(opts))
throw new BadOptionsError(opts, text.optionsNotObj(typeof opts));
if (_.isUndefined(opts.token))
throw new BadOptionsError(opts, text.noToken());
if (!_.isString(opts.token) || !tokenPattern.test(opts.token))
throw new BadOptionsError(opts, text.invalidToken(opts.token));
// Log method aliases
this[$levels] = levelUtil.normalize(opts);
for (const lvlName of this.levels) {
if (lvlName in this)
throw new BadOptionsError(opts, text.levelConflict(lvlName));
Object.defineProperty(this, lvlName, {
enumerable: true,
writable: false,
value() {
export const normalize = (opts) => {
let custom = opts.levels;
if (!_.isUndefined(custom) && !_.isNull(custom) && !_.isObject(custom))
throw new BadOptionsError(opts, text.levelsNotObj(typeof custom));
if (!custom)
return defaults.levels.slice();
custom = _.isArray(custom) ? normArr(custom, opts) : normObj(custom, opts);
const levels = defaults.levels.map((lvl, i) => custom[i] || lvl);
const duplicates =
_(levels).countBy().pick(count => count > 1).keys().value();
if (duplicates.length)
throw new BadOptionsError(opts, text.duplicateLevels(duplicates));
return levels;
};
const normArr = (arr, opts) => {
if (arr.length > 8)
throw new BadOptionsError(opts, text.tooManyLevels(arr.length));
return arr.map(val => {
if (val && _.isString(val)) return val;
if (_.isNumber(val) && isFinite(val)) return val.toString();
if (_.isNull(val) || _.isUndefined(val)) return;
throw new BadOptionsError(opts, text.levelNotString(val));
});
};
const normObj = (obj, opts) => {
const lvlNums = _.values(obj);
for (const num of lvlNums) {
if (!isNumberValid(num))
throw new BadOptionsError(opts, text.invalidLevelNum(num));
}
const duplicates =
_(obj).countBy().pick(lvl => lvl > 1).keys().value();
if (duplicates.length)
throw new BadOptionsError(opts, text.duplicateLevelNums(duplicates));
return _.reduce(obj, (arr, i, name) => {
arr[i] = name;
return arr;
}, []);
};
const normObj = (obj, opts) => {
const lvlNums = _.values(obj);
for (const num of lvlNums) {
if (!isNumberValid(num))
throw new BadOptionsError(opts, text.invalidLevelNum(num));
}
const duplicates =
_(obj).countBy().pick(lvl => lvl > 1).keys().value();
if (duplicates.length)
throw new BadOptionsError(opts, text.duplicateLevelNums(duplicates));
return _.reduce(obj, (arr, i, name) => {
arr[i] = name;
return arr;
}, []);
};
return arr.map(val => {
if (val && _.isString(val)) return val;
if (_.isNumber(val) && isFinite(val)) return val.toString();
if (_.isNull(val) || _.isUndefined(val)) return;
throw new BadOptionsError(opts, text.levelNotString(val));
});
};
if (!_.isUndefined(custom) && !_.isNull(custom) && !_.isObject(custom))
throw new BadOptionsError(opts, text.levelsNotObj(typeof custom));
if (!custom)
return defaults.levels.slice();
custom = _.isArray(custom) ? normArr(custom, opts) : normObj(custom, opts);
const levels = defaults.levels.map((lvl, i) => custom[i] || lvl);
const duplicates =
_(levels).countBy().pick(count => count > 1).keys().value();
if (duplicates.length)
throw new BadOptionsError(opts, text.duplicateLevels(duplicates));
return levels;
};