Skip to content

Commit

Permalink
Simplify createOmitPropertiesSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
72636c committed Oct 6, 2023
1 parent ad3e48d commit 22fa653
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 80 deletions.
44 changes: 22 additions & 22 deletions src/serializers/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type pino from 'pino';
import { err, errWithCause } from 'pino-std-serializers';

import { omitProperties } from './omitProperties';
import { createOmitPropertiesSerializer } from './omitPropertiesSerializer';
import type { SerializerFn } from './types';

export const defaultOmitHeaderNames = [
'x-envoy-attempt-count',
Expand Down Expand Up @@ -45,19 +45,17 @@ const isObject = (value: unknown): boolean => {
return value != null && (type === 'object' || type === 'function');
};

const createReqSerializer = (opts: SerializerOptions) => (request: Request) =>
isObject(request)
? {
method: request.method,
url: request.url,
headers: omitProperties(
request.headers,
opts.omitHeaderNames ?? defaultOmitHeaderNames,
),
remoteAddress: request?.socket?.remoteAddress,
remotePort: request?.socket?.remotePort,
}
: request;
const createReqSerializer =
(serializeHeaders: SerializerFn) => (request: Request) =>
isObject(request)
? {
method: request.method,
url: request.url,
headers: serializeHeaders(request.headers),
remoteAddress: request?.socket?.remoteAddress,
remotePort: request?.socket?.remotePort,
}
: request;

const res = (response: Response) =>
isObject(response)
Expand All @@ -70,15 +68,17 @@ const res = (response: Response) =>
export const createSerializers = (
opts: SerializerOptions & Pick<pino.LoggerOptions, 'serializers'>,
) => {
const omitHeaderNamesSerializer = createOmitPropertiesSerializer('headers', {
omitPropertyNames: opts.omitHeaderNames ?? defaultOmitHeaderNames,
});
return {
const serializeHeaders = createOmitPropertiesSerializer(
opts.omitHeaderNames ?? defaultOmitHeaderNames,
);

const serializers = {
err,
errWithCause,
req: createReqSerializer(opts),
req: createReqSerializer(serializeHeaders),
res,
...omitHeaderNamesSerializer,
...opts.serializers,
};
headers: serializeHeaders,
} satisfies pino.LoggerOptions['serializers'];

return serializers;
};
35 changes: 6 additions & 29 deletions src/serializers/omitPropertiesSerializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,6 @@ import { createOmitPropertiesSerializer } from './omitPropertiesSerializer';

const omitPropertyNamesBase = ['remove-prop', 'remove.prop'];

it.each`
scenario | topLevelPropertyName
${'undefined'} | ${undefined}
${'null'} | ${null}
`(
'returns empty object when topLevelPropertyName is $scenario',
({ topLevelPropertyName }) => {
const serializer = createOmitPropertiesSerializer(topLevelPropertyName, {
omitPropertyNames: [...omitPropertyNamesBase],
});

expect(serializer).toStrictEqual({});
},
);

it.each`
scenario | omitPropertyNames
${'undefined'} | ${undefined}
Expand All @@ -32,20 +17,16 @@ it.each`
`(
'returns empty object when omitPropertyNames is $scenario',
({ omitPropertyNames }) => {
const serializer = createOmitPropertiesSerializer('main', {
omitPropertyNames,
});
const serializer = createOmitPropertiesSerializer(omitPropertyNames);

expect(serializer).toStrictEqual({});
expect(serializer({})).toStrictEqual({});
},
);

it('returns object with named property containing function', () => {
const serializer = createOmitPropertiesSerializer('main', {
omitPropertyNames: [...omitPropertyNamesBase],
});
const serializer = createOmitPropertiesSerializer(omitPropertyNamesBase);

expect(serializer).toStrictEqual({ main: expect.any(Function) });
expect(serializer).toStrictEqual(expect.any(Function));
});

const sink = () =>
Expand All @@ -71,9 +52,7 @@ function once(emitter: Transform, name: string) {
}

it('omits properties from logged object', async () => {
const serializer = createOmitPropertiesSerializer('main', {
omitPropertyNames: [...omitPropertyNamesBase],
});
const serializer = createOmitPropertiesSerializer(omitPropertyNamesBase);

const input = {
main: {
Expand Down Expand Up @@ -112,9 +91,7 @@ it.each`
`(
'does nothing when top-level property is $scenario',
async ({ value, expected }) => {
const serializer = createOmitPropertiesSerializer('main', {
omitPropertyNames: ['keepProp'],
});
const serializer = createOmitPropertiesSerializer(['keepProp']);

const input = {
main: value,
Expand Down
39 changes: 10 additions & 29 deletions src/serializers/omitPropertiesSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
import { omitProperties } from './omitProperties';
import type { SerializerFn } from './types';

export interface OmitPropertiesSerializerOptions {
export const createOmitPropertiesSerializer = (
/**
* A list of properties that should not be logged.
*/
omitPropertyNames: string[];
}

type OmitHeaderNamesFn = (record: unknown) => unknown;
type OmitHeaderNamesSerializer = Record<string, OmitHeaderNamesFn>;
properties: string[],
): SerializerFn => {
const uniquePropertySet = new Set(properties);

/** Creates a serializer that operates on the logged object's top-level property named `topLevelPropertyName`
* and omits the properties listed in `options.omitPropertyNames`.
* @param topLevelPropertyName - The name of the root property on the logged object that to omit properties from.
* @param options - Options for the serializer.
*/
export const createOmitPropertiesSerializer = (
topLevelPropertyName: string,
options: OmitPropertiesSerializerOptions,
): OmitHeaderNamesSerializer => {
const propertyNames = [...new Set(options.omitPropertyNames ?? [])].filter(
(propertyName) => typeof propertyName === 'string',
);

if (
!topLevelPropertyName ||
typeof topLevelPropertyName !== 'string' ||
topLevelPropertyName.length === 0 ||
propertyNames.length === 0
) {
return {};
if (uniquePropertySet.size === 0) {
return (input) => input;
}

return {
[topLevelPropertyName]: (record) => omitProperties(record, propertyNames),
};
const uniqueProperties = Array.from(uniquePropertySet);

return (input) => omitProperties(input, uniqueProperties);
};
1 change: 1 addition & 0 deletions src/serializers/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type SerializerFn = (input: unknown) => unknown;

0 comments on commit 22fa653

Please sign in to comment.