UNPKG

@node-in-layers/core

Version:

The core library for the Node In Layers rapid web development framework.

114 lines 3.82 kB
import get from 'lodash/get.js'; import merge from 'lodash/merge.js'; import { LogLevelNames } from '../types.js'; import { combineCrossLayerProps } from '../libs.js'; const MAX_LOG_CHARACTERS = 50000; const defaultGetFunctionWrapLogLevel = (layerName) => { switch (layerName) { case 'features': case 'entries': return LogLevelNames.info; case 'services': return LogLevelNames.trace; default: return LogLevelNames.debug; } }; /** * Gets the cross layer props and combines it with information from the logger. * Does this in a "smart" way. * * The result of this can be dumped directly into applyData. * * @param {Logger} logger * @param {CrossLayerProps} crossLayerProps */ const combineLoggingProps = (logger, crossLayerProps) => { return combineCrossLayerProps({ logging: { ids: logger.getIds(), }, }, crossLayerProps || {}).logging; }; const isCrossLayerLoggingProps = (maybe) => { return Boolean(get(maybe, 'logging.ids')); }; // eslint-disable-next-line consistent-return const capForLogging = (input, maxSize = MAX_LOG_CHARACTERS) => { function safeStringify(obj) { // eslint-disable-next-line functional/no-try-statements try { return JSON.stringify(obj); } catch { return '[Unserializable]'; } } const inputType = Array.isArray(input) ? 'array' : typeof input === 'object' && input !== null ? 'object' : 'other'; if (inputType === 'other') { return input; } if (safeStringify(input).length <= maxSize) { return input; } if (inputType === 'array') { // Build a new truncated array functionally const build = (arr, idx) => { /* c8 ignore next line */ if (idx >= input.length) { /* c8 ignore next line */ return arr; /* c8 ignore next line */ } const nextArr = arr.concat(input[idx]); if (safeStringify([ ...nextArr, `[truncated, original length: ${input.length}]`, ]).length > maxSize) { return arr.concat(`[truncated, original length: ${input.length}]`); } return build(nextArr, idx + 1); }; return build([], 0); } if (inputType === 'object') { // Build a new truncated object functionally const keys = Object.keys(input); const build = (obj, idx) => { /* c8 ignore next line */ if (idx >= keys.length) { /* c8 ignore next line */ return obj; /* c8 ignore next line */ } const key = keys[idx]; const nextObj = merge(obj, { [key]: input[key] }); const truncated = merge(obj, { '[truncated]': `original keys: ${keys.length}`, }); if (safeStringify(truncated).length > maxSize) { return truncated; } return build(nextObj, idx + 1); }; return build({}, 0); } }; const extractCrossLayerProps = (args) => { if (args.length === 0) { return [[], undefined]; } const lastArg = args[args.length - 1]; if (isCrossLayerLoggingProps(lastArg)) { // Return all args except the last one, and the last one as CrossLayerProps return [args.slice(0, args.length - 1), lastArg]; } // Return all args, and undefined for CrossLayerProps return [args, undefined]; }; export { defaultGetFunctionWrapLogLevel, combineLoggingProps, isCrossLayerLoggingProps, capForLogging, extractCrossLayerProps, }; //# sourceMappingURL=libs.js.map