Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"scripts": {
"build": "tsup",
"typecheck": "tsc --noEmit",
"test": "node --import tsx --test test/*.test.js test/adapters/*.test.js"
"test": "node --import tsx --test test/*.test.js test/**/*.test.js"
},
"repository": {
"type": "git",
Expand Down
43 changes: 43 additions & 0 deletions src/core/formatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
export interface FormatPayloadOptions {
maxDepth?: number; // Maximum depth before collapsing inner objects (default: 4)
maxArrayItems?: number; // Maximum array items to show before truncating (default: 10)
}

/**
* Safely format and truncate deep JSON structures to prevent terminal spam.
*/
export function formatPayload(data: any, options: FormatPayloadOptions = {}, currentDepth: number = 1): any {
const maxDepth = options.maxDepth != null ? options.maxDepth : 4;
const maxArrayItems = options.maxArrayItems != null ? options.maxArrayItems : 10;

if (data === null || data === undefined) {
return data;
}

if (typeof data !== 'object') {
return data;
}

if (currentDepth > maxDepth) {
if (Array.isArray(data)) {
return `[Array(${data.length})]`;
}
return '[Object]';
}

if (Array.isArray(data)) {
if (data.length > maxArrayItems) {
const truncatedSlice = data
.slice(0, maxArrayItems)
.map((item) => formatPayload(item, options, currentDepth + 1));
return [...truncatedSlice, `... ${data.length - maxArrayItems} more items` as any];
}
return data.map((item) => formatPayload(item, options, currentDepth + 1));
}

const result: Record<string, any> = {};
for (const key of Object.keys(data)) {
result[key] = formatPayload(data[key], options, currentDepth + 1);
}
return result;
}
52 changes: 52 additions & 0 deletions src/core/stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { IncomingMessage, ServerResponse } from 'node:http';

export interface StreamCaptureOptions {
maxBodySize?: number; // Maximum bytes to capture before dropping further chunks (default: 1024)
}

export interface CapturedStreamResult {
body: string;
truncated: boolean;
sizeBytes: number;
}

/**
* Utility to safely collect raw stream body chunks up to maxBodySize limit.
*/
export function createStreamCapturer(maxBodySize: number = 1024) {
let chunks: Buffer[] = [];
let currentSize = 0;
let truncated = false;

return {
onData(chunk: any) {
if (truncated) return;
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
currentSize += buf.length;

if (currentSize > maxBodySize) {
const allowedBytes = maxBodySize - (currentSize - buf.length);
if (allowedBytes > 0) {
chunks.push(buf.subarray(0, allowedBytes));
}
truncated = true;
} else {
chunks.push(buf);
}
},
getResult(): CapturedStreamResult {
const buffer = Buffer.concat(chunks);
let bodyText = '';
try {
bodyText = buffer.toString('utf-8');
} catch {
bodyText = '[Binary Data]';
}
return {
body: bodyText,
truncated,
sizeBytes: currentSize,
};
},
};
}
5 changes: 5 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import store from './store.ts';
import { getSystemMetrics } from './system.ts';
import { redactHeaders, generateCurl } from './utils.ts';
import type { AlertOptions } from './store.ts';
import { createStreamCapturer } from './core/stream.ts';
import { formatPayload } from './core/formatter.ts';

export interface ExpressLensOptions {
logAnalytics?: boolean;
logInterval?: number;
colorize?: boolean;
ignoreRoutes?: (string | RegExp)[];
slowThresholdMs?: number;
maxBodySize?: number;
maxDepth?: number;
maxArrayItems?: number;
redactHeaders?: string[];
alerts?: AlertOptions;
}
Expand Down
22 changes: 22 additions & 0 deletions test/core/formatter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import test from 'node:test';
import assert from 'node:assert';
import { formatPayload } from '../../src/core/formatter.ts';

test('Payload Formatter Suite', async (t) => {
await t.test('collapses objects exceeding maxDepth', () => {
const deepObj = { level1: { level2: { level3: { level4: { level5: 'hidden' } } } } };
const formatted = formatPayload(deepObj, { maxDepth: 3 });

assert.deepStrictEqual(formatted, {
level1: { level2: { level3: '[Object]' } }
});
});

await t.test('truncates array items exceeding maxArrayItems', () => {
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const formatted = formatPayload(arr, { maxArrayItems: 5 });

assert.strictEqual(formatted.length, 6);
assert.strictEqual(formatted[5], '... 7 more items');
});
});
26 changes: 26 additions & 0 deletions test/core/stream.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import test from 'node:test';
import assert from 'node:assert';
import { createStreamCapturer } from '../../src/core/stream.ts';

test('Stream Capturer Suite', async (t) => {
await t.test('captures small chunks within limit', () => {
const capturer = createStreamCapturer(1024);
capturer.onData('Hello ');
capturer.onData('World!');

const result = capturer.getResult();
assert.strictEqual(result.body, 'Hello World!');
assert.strictEqual(result.truncated, false);
assert.strictEqual(result.sizeBytes, 12);
});

await t.test('truncates payload exceeding maxBodySize', () => {
const capturer = createStreamCapturer(10);
capturer.onData('1234567890EXTRA_DATA');

const result = capturer.getResult();
assert.strictEqual(result.body, '1234567890');
assert.strictEqual(result.truncated, true);
assert.strictEqual(result.sizeBytes, 20);
});
});
Loading