Skip to content

Commit

Permalink
Merge pull request #458 from senacor/bugfix/huge_memory_consumption
Browse files Browse the repository at this point in the history
Fix huge memory consumption for logging
  • Loading branch information
ziegler-daniel authored Dec 16, 2024
2 parents 33902a6 + 5e68cf9 commit 6b69815
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 3.1.2 (13.12.2024)
* Use default limits for array length, object depth or string length for logging (reverts change from version 3.1.1) as this could cause a huge memory consumption in some cases
* Use `JSON.stringify` to log objects that have a `toJSON` method

## 3.1.1 (15.11.2024)
* Do not limit array length, object depth or string length for logging

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@senacor/azure-function-middleware",
"version": "3.1.1",
"version": "3.1.2",
"description": "Middleware for azure functions to handle authentication, authorization, error handling and logging",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
14 changes: 12 additions & 2 deletions src/util/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ describe('stringify should', () => {
expect(stringify('Test', 2, { message: 'World' })).toBe("Test 2 { message: 'World' }");
});

it('print all array items', () => {
expect(stringify(Array(250).fill(1)).match(/1/g)?.length).toBe(250);
it('do not print all items of large arrays', () => {
expect(stringify(Array(250).fill(1)).match(/1/g)?.length).toBeLessThan(250);
});

it('use toJSON function if available', () => {
const input = {
toJSON: () => ({
value: 42,
}),
};

expect(stringify(input)).toEqual('{"value":42}');
});
});
14 changes: 13 additions & 1 deletion src/util/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,17 @@ function stringifyValue(value: unknown): string {
return value;
}

return inspect(value, { depth: null, maxArrayLength: null, maxStringLength: null });
if (isJsonSerializable(value)) {
return JSON.stringify(value);
}

return inspect(value);
}

type JsonSerializable = {
toJSON: () => unknown;
};

function isJsonSerializable(value: unknown): value is JsonSerializable {
return typeof (value as JsonSerializable)?.toJSON === 'function';
}
9 changes: 4 additions & 5 deletions src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { AnySchema } from 'joi';

import { ApplicationError } from './error';
import { BeforeExecutionFunction, MiddlewareResult, PostExecutionFunction, isErrorResult } from './middleware';
import { stringify } from './util/stringify';

type ValidationOptions = Partial<{
transformErrorMessage: (message: string) => unknown;
Expand Down Expand Up @@ -39,8 +38,8 @@ export function requestValidation(schema: AnySchema, opts?: ValidationOptions):
if (validationResult && validationResult.error) {
context.error(
`The request did not match the given schema.${
printRequest ? stringify(toBeValidatedContent) : ''
}: ${stringify(validationResult)}`,
printRequest ? JSON.stringify(toBeValidatedContent) : ''
}: ${JSON.stringify(validationResult)}`,
);

if (shouldThrowOnValidationError) {
Expand Down Expand Up @@ -93,8 +92,8 @@ export function responseValidation(schema: AnySchema, opts?: ValidationOptions):
if (validationResult && validationResult.error) {
context.error(
`The response did not match the given schema.${
printResponse ? stringify(toBeValidatedContent) : ''
}: ${stringify(validationResult)}`,
printResponse ? JSON.stringify(toBeValidatedContent) : ''
}: ${JSON.stringify(validationResult)}`,
);

if (shouldThrowOnValidationError) {
Expand Down

0 comments on commit 6b69815

Please sign in to comment.