logs-interceptor
Version:
High-performance, production-ready log interceptor for Node.js applications with Loki integration. Built with Clean Architecture principles. Supports Node.js, Browser, and Node-RED.
80 lines • 2.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.safeStringify = void 0;
/**
* Utility: safeStringify
* Safely stringify any value, handling circular references and non-serializable objects
*/
function safeStringify(value, maxDepth = 10) {
const seen = new WeakSet();
let depth = 0;
try {
return JSON.stringify(value, function (key, val) {
// Check depth
if (depth > maxDepth) {
return '[Max Depth Reached]';
}
if (val === null || val === undefined) {
return val;
}
if (typeof val === 'object') {
depth++;
if (seen.has(val)) {
return '[Circular Reference]';
}
seen.add(val);
// Handle special objects
if (val instanceof Buffer) {
return `[Buffer: ${val.length} bytes]`;
}
if (val instanceof Promise) {
return '[Promise]';
}
if (val instanceof WeakMap || val instanceof WeakSet) {
return `[${val.constructor.name}]`;
}
}
if (typeof val === 'function') {
return `[Function: ${val.name || 'anonymous'}]`;
}
if (typeof val === 'symbol') {
return `[Symbol: ${val.toString()}]`;
}
if (typeof val === 'bigint') {
return val.toString() + 'n';
}
if (val instanceof Error) {
return {
name: val.name,
message: val.message,
stack: val.stack,
code: val.code,
};
}
if (val instanceof Date) {
return val.toISOString();
}
if (val instanceof RegExp) {
return val.toString();
}
if (val instanceof Map) {
return {
type: 'Map',
entries: Array.from(val.entries()),
};
}
if (val instanceof Set) {
return {
type: 'Set',
values: Array.from(val.values()),
};
}
return val;
}, 2);
}
catch (error) {
return `[Unserializable: ${error instanceof Error ? error.message : 'Unknown error'}]`;
}
}
exports.safeStringify = safeStringify;
//# sourceMappingURL=safeStringify.js.map