Skip to content

Commit

Permalink
Fix huge memory consumption for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ziegler-daniel committed Dec 13, 2024
1 parent 33902a6 commit feda978
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 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

## 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
4 changes: 2 additions & 2 deletions src/util/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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);
});
});
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 inspect(value.toJSON());
}

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 feda978

Please sign in to comment.