Skip to content

Commit d51cd46

Browse files
Code cleanup
1 parent 5556870 commit d51cd46

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

packages/service-core/src/routes/endpoints/socket-route.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { SocketRouteGenerator } from '../router-socket.js';
66
import { SyncRoutes } from './sync-stream.js';
77

88
import { APIMetric, event_types } from '@powersync/service-types';
9-
import { formatParamsForLogging } from '../../util/param-logging.js';
9+
import { limitParamsForLogging } from '../../util/param-logging.js';
1010

1111
export const syncStreamReactive: SocketRouteGenerator = (router) =>
1212
router.reactiveStream<util.StreamingSyncRequest, any>(SyncRoutes.STREAM, {
@@ -99,10 +99,10 @@ export const syncStreamReactive: SocketRouteGenerator = (router) =>
9999
tracker.setCompressed(connection.tracker.encoding);
100100
}
101101

102-
const formattedAppMetadata = params.app_metadata ? formatParamsForLogging(params.app_metadata) : undefined;
102+
const formattedAppMetadata = params.app_metadata ? limitParamsForLogging(params.app_metadata) : undefined;
103103
logger.info('Sync stream started', {
104104
app_metadata: formattedAppMetadata,
105-
client_params: params.parameters ? formatParamsForLogging(params.parameters) : undefined
105+
client_params: params.parameters ? limitParamsForLogging(params.parameters) : undefined
106106
});
107107

108108
try {

packages/service-core/src/routes/endpoints/sync-stream.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { APIMetric, event_types } from '@powersync/service-types';
99
import { authUser } from '../auth.js';
1010
import { routeDefinition } from '../router.js';
1111

12-
import { formatParamsForLogging } from '../../util/param-logging.js';
12+
import { limitParamsForLogging } from '../../util/param-logging.js';
1313
import { maybeCompressResponseStream } from '../compression.js';
1414

1515
export enum SyncRoutes {
@@ -78,12 +78,12 @@ export const syncStreamed = routeDefinition({
7878
const tracker = new sync.RequestTracker(metricsEngine);
7979

8080
const formattedAppMetadata = payload.params.app_metadata
81-
? formatParamsForLogging(payload.params.app_metadata)
81+
? limitParamsForLogging(payload.params.app_metadata)
8282
: undefined;
8383

8484
logger.info('Sync stream started', {
8585
app_metadata: formattedAppMetadata,
86-
client_params: payload.params.parameters ? formatParamsForLogging(payload.params.parameters) : undefined
86+
client_params: payload.params.parameters ? limitParamsForLogging(payload.params.parameters) : undefined
8787
});
8888

8989
try {

packages/service-core/src/util/param-logging.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
1+
/**
2+
* Options for {@link limitParamsForLogging}.
3+
*/
14
export type ParamLoggingFormatOptions = {
25
maxKeyCount: number;
36
maxStringLength: number;
47
};
58

9+
/**
10+
* Default options for {@link limitParamsForLogging}.
11+
*/
612
export const DEFAULT_PARAM_LOGGING_FORMAT_OPTIONS: ParamLoggingFormatOptions = {
713
maxKeyCount: 10,
814
maxStringLength: 50
915
};
1016

11-
export function formatParamsForLogging(params: Record<string, any>, options: Partial<ParamLoggingFormatOptions> = {}) {
17+
/**
18+
* Formats potentially arbitrary parameters for logging.
19+
* This limits the number of keys and strings to a maximum length.
20+
* A warning key-value is added if the number of keys exceeds the maximum.
21+
* String values exceeding the maximum length are truncated.
22+
* Non-String values are stringified, the maximum length is then applied.
23+
* @param params - The parameters to format.
24+
* @param options - The options to use.
25+
* @default DEFAULT_PARAM_LOGGING_FORMAT_OPTIONS
26+
* @returns The formatted parameters.
27+
*/
28+
export function limitParamsForLogging(
29+
params: Record<string, any>,
30+
options: Partial<ParamLoggingFormatOptions> = DEFAULT_PARAM_LOGGING_FORMAT_OPTIONS
31+
) {
1232
const {
1333
maxStringLength = DEFAULT_PARAM_LOGGING_FORMAT_OPTIONS.maxStringLength,
1434
maxKeyCount = DEFAULT_PARAM_LOGGING_FORMAT_OPTIONS.maxKeyCount
@@ -31,7 +51,7 @@ export function formatParamsForLogging(params: Record<string, any>, options: Par
3151
return [];
3252
}
3353

34-
if (typeof value === 'string') {
54+
if (typeof value == 'string') {
3555
return [key, trimString(value)];
3656
}
3757
return [key, trimString(JSON.stringify(value))];

packages/service-core/test/src/routes/stream.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { pipeline } from 'stream/promises';
66
import { describe, expect, it } from 'vitest';
77
import winston from 'winston';
88
import { syncStreamed } from '../../../src/routes/endpoints/sync-stream.js';
9-
import { DEFAULT_PARAM_LOGGING_FORMAT_OPTIONS, formatParamsForLogging } from '../../../src/util/param-logging.js';
9+
import { DEFAULT_PARAM_LOGGING_FORMAT_OPTIONS, limitParamsForLogging } from '../../../src/util/param-logging.js';
1010
import { mockServiceContext } from './mocks.js';
1111

1212
describe('Stream Route', () => {
@@ -149,7 +149,7 @@ describe('Stream Route', () => {
149149

150150
// Verify that app_metadata from defaultMeta is present in the log
151151
expect(syncStartedLog?.app_metadata).toBeDefined();
152-
expect(syncStartedLog?.app_metadata).toEqual(formatParamsForLogging(inputMeta));
152+
expect(syncStartedLog?.app_metadata).toEqual(limitParamsForLogging(inputMeta));
153153
// Should trim long metadata
154154
expect(syncStartedLog?.app_metadata.long_meta.length).toEqual(
155155
DEFAULT_PARAM_LOGGING_FORMAT_OPTIONS.maxStringLength

0 commit comments

Comments
 (0)