From 205cabac09d29176d4c741170faa62e6d7742e16 Mon Sep 17 00:00:00 2001 From: Corey Daley Date: Wed, 30 Oct 2024 22:00:31 -0400 Subject: [PATCH] fix(): Normalize options.maxSize and throw errors where appropriate The documentation does not make it very clear the difference between passing an integer and a string for maxSize, there also seems to be quite a bit of confusion around whether or not to use `k` or `kb` but there is no reason that `k`, `kb` and `kilobytes` can't all be used interchangeably (as an example). This pull request starts off by renaming the `getMaxSize` function to something more appropriate `normalizeMaxSize`, it also updates the `size` option that is provided to `getStream` to determine more quickly if `maxSize` is undefined, and if so go ahead and just send `null`. Next it normalizes the `maxSize` option and determines if it can return more quickly based upon the type of the data that is provided. Such that if an integer is provided, do a small check and bit of math and return it, if a string is provided, check to see if it was sent in an already correctly formatted quantifier and return it, if a longer quantifier was used, go ahead and parse it and assign the correct quantifier for passing it to the file-stream-rotator package. It also now throws errors in the appropriate places if the provided option can not be parsed instead of just silently passing null to the file-stream-rotator package and confusing users. --- daily-rotate-file.js | 50 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/daily-rotate-file.js b/daily-rotate-file.js index 0d31341..68ef35e 100644 --- a/daily-rotate-file.js +++ b/daily-rotate-file.js @@ -35,17 +35,47 @@ const DailyRotateFile = function(options) { }); } - function getMaxSize(size) { - if (size && typeof size === "string") { - if (size.toLowerCase().match(/^((?:0\.)?\d+)([kmg])$/)) { - return size; + function normalizeMaxSize(size) { + // If size is already an integer of bytes + if (Number.isInteger(size)) { + // Then make sure it is not zero (0) and return it with the correct formatting + return size === 0 ? "1k" : size / 1024 + "k" + } + // If size has any invalid characters get rid of them + size = size.replace(/[^0-9a-z]/g, ""); + // If size is a string + if (typeof size === "string") { + // Separate the integer and the string parts + let integerPart = parseInt(size); + let stringPart = size.replace(/[0-9]/g, ""); + + // If the string part has a length of one (1) and matches the regexp + if (stringPart.length === 1 && stringPart.match(/[kmg]/)) { + // Combine the integer part and the string part and return it + return integerPart + stringPart; + } + + // If the string part has a long quantifier (> 1) parse it and assign the short quantifier + switch(true) { + case /kilobytes|kilobits|kb/.test(stringPart): + stringPart = "k"; + break; + case /megabytes|megabits|mb/.test(stringPart): + stringPart = "m"; + break; + case /gigabytes|gigabits|gb/.test(stringPart): + stringPart = "g"; + break; + default: + // If none of the long quantifiers match throw an error and let the user know + throw new Error("Unable to determine size quantifier from string: " + stringPart); } - } else if (size && Number.isInteger(size)) { - const sizeK = Math.round(size / 1024); - return sizeK === 0 ? "1k" : sizeK + "k"; + + // If everything went well we can combine the integer part and the string part and return it + return integerPart + stringPart; } - - return null; + // If we get here, something is wrong and we should let the user know + throw new Error("Unable to parse option maxSize, invalid format: " + size); } function isValidFileName(filename) { @@ -83,7 +113,7 @@ const DailyRotateFile = function(options) { frequency: options.frequency ? options.frequency : "custom", date_format: options.datePattern ? options.datePattern : "YYYY-MM-DD", verbose: false, - size: getMaxSize(options.maxSize), + size: options.maxSize ? normalizeMaxSize(options.maxSize) : null, max_logs: options.maxFiles, end_stream: true, audit_file: options.auditFile