UNPKG

logpipes

Version:

Console.log transformation pipes

110 lines 4.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.estimateArgsSizeByStringify = exports.createLogCachePipe = exports.getDefaultLogCachePipeOptions = void 0; const JsonSimplifier_1 = require("./JsonSimplifier"); function getDefaultLogCachePipeOptions() { return { /** Keeps only 1000 messages in the cache by default. */ cacheSize: 1000, /** Do not use estimation by data size by default. */ cacheSizeByStringify: -1, }; } exports.getDefaultLogCachePipeOptions = getDefaultLogCachePipeOptions; function createLogCachePipe(inputOptions = {}) { const options = Object.assign(Object.assign({}, getDefaultLogCachePipeOptions()), inputOptions); if (options.cacheSize < 0 || isNaN(options.cacheSize)) { throw new Error(`Invalid cache size: ${options.cacheSize}`); } const cache = { size: 0 }; let isInsideOnCacheSizeReachedCall = false; let currentCacheSizeByStringify = 0; function removeFirstMessageFromCache() { if (cache.first) { if (currentCacheSizeByStringify > 0) { currentCacheSizeByStringify -= estimateArgsSizeByStringify(...cache.first.value.args); } cache.first = cache.first.next; cache.size--; } } function addNewMessageToCache(newMessageNode) { if (cache.last === undefined) { cache.first = newMessageNode; } else { cache.last.next = newMessageNode; } cache.last = newMessageNode; cache.size++; if (options.cacheSizeByStringify >= 0) { currentCacheSizeByStringify += estimateArgsSizeByStringify(...newMessageNode.value.args); } } const pipe = (level, ...args) => { if (options.cacheSize === 0 || isInsideOnCacheSizeReachedCall) { return args; } const newMessageNode = { value: { level, args, timestamp: Date.now() } }; addNewMessageToCache(newMessageNode); const isCacheSizeLimitReached = cache.size > options.cacheSize; const isStringifyLimitReached = options.cacheSizeByStringify >= 0 && currentCacheSizeByStringify > options.cacheSizeByStringify; if (isCacheSizeLimitReached || isStringifyLimitReached) { if (options.onCacheSizeReached) { isInsideOnCacheSizeReachedCall = true; try { options.onCacheSizeReached(cachePipe); } finally { isInsideOnCacheSizeReachedCall = false; } } } if (isStringifyLimitReached) { while (currentCacheSizeByStringify > options.cacheSizeByStringify && cache.first !== undefined) { removeFirstMessageFromCache(); } // Do some state recovery to keep 'currentCacheSizeByStringify' within meaningful ranges. // This may happen if messages in the cache are modified by user and sizes change. currentCacheSizeByStringify = Math.max(currentCacheSizeByStringify, 0); if (cache.first === undefined) { currentCacheSizeByStringify = 0; } } else if (isCacheSizeLimitReached) { removeFirstMessageFromCache(); } return args; }; const cachePipe = pipe; cachePipe.getMessages = () => { const result = []; let node = cache.first; while (node !== undefined) { result.push(node.value); node = node.next; } return result; }; cachePipe.clearMessages = () => { cache.first = undefined; cache.last = undefined; cache.size = 0; currentCacheSizeByStringify = 0; }; cachePipe.onInstall = () => cachePipe.clearMessages(); return cachePipe; } exports.createLogCachePipe = createLogCachePipe; /** Estimates args array size using simplifyJson call. */ function estimateArgsSizeByStringify(...args) { let size = 0; for (const arg of args) { if (arg !== undefined) { size += JSON.stringify((0, JsonSimplifier_1.simplifyJson)(arg)).length; } } return size; } exports.estimateArgsSizeByStringify = estimateArgsSizeByStringify; //# sourceMappingURL=LogCachePipe.js.map