@powersync/service-core 0.0.0-dev-20251128124236 → 0.0.0-dev-20251202092152

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
- import * as t from 'ts-codec';
2
- import { BucketPriority, SqliteJsonRow } from '@powersync/service-sync-rules';
3
1
  import { JsonContainer } from '@powersync/service-jsonbig';
2
+ import { BucketPriority, SqliteJsonRow } from '@powersync/service-sync-rules';
3
+ import * as t from 'ts-codec';
4
4
 
5
5
  export const BucketRequest = t.object({
6
6
  name: t.string,
@@ -81,6 +81,11 @@ export const StreamingSyncRequest = t.object({
81
81
  */
82
82
  parameters: t.record(t.any).optional(),
83
83
 
84
+ /**
85
+ * Application metadata to be used in logging.
86
+ */
87
+ app_metadata: t.record(t.string).optional(),
88
+
84
89
  /**
85
90
  * Unique client id.
86
91
  */
@@ -1,10 +1,12 @@
1
1
  import { BasicRouterRequest, Context, SyncRulesBucketStorage } from '@/index.js';
2
- import { logger, RouterResponse, ServiceError } from '@powersync/lib-services-framework';
2
+ import { RouterResponse, ServiceError, logger } from '@powersync/lib-services-framework';
3
3
  import { SqlSyncRules } from '@powersync/service-sync-rules';
4
4
  import { Readable, Writable } from 'stream';
5
5
  import { pipeline } from 'stream/promises';
6
6
  import { describe, expect, it } from 'vitest';
7
+ import winston from 'winston';
7
8
  import { syncStreamed } from '../../../src/routes/endpoints/sync-stream.js';
9
+ import { DEFAULT_PARAM_LOGGING_FORMAT_OPTIONS, limitParamsForLogging } from '../../../src/util/param-logging.js';
8
10
  import { mockServiceContext } from './mocks.js';
9
11
 
10
12
  describe('Stream Route', () => {
@@ -77,6 +79,97 @@ describe('Stream Route', () => {
77
79
  const r = await drainWithTimeout(stream).catch((error) => error);
78
80
  expect(r.message).toContain('Simulated storage error');
79
81
  });
82
+
83
+ it('logs the application metadata', async () => {
84
+ const storage = {
85
+ getParsedSyncRules() {
86
+ return new SqlSyncRules('bucket_definitions: {}');
87
+ },
88
+ watchCheckpointChanges: async function* (options) {
89
+ throw new Error('Simulated storage error');
90
+ }
91
+ } as Partial<SyncRulesBucketStorage>;
92
+ const serviceContext = mockServiceContext(storage);
93
+
94
+ // Create a custom format to capture log info objects (which include defaultMeta)
95
+ const capturedLogs: any[] = [];
96
+ const captureFormat = winston.format((info) => {
97
+ // Capture the info object which includes defaultMeta merged in
98
+ capturedLogs.push({ ...info });
99
+ return info;
100
+ });
101
+
102
+ // Create a test logger with the capture format
103
+ const testLogger = winston.createLogger({
104
+ format: winston.format.combine(captureFormat(), winston.format.json()),
105
+ transports: [new winston.transports.Console()]
106
+ });
107
+
108
+ const context: Context = {
109
+ logger: testLogger,
110
+ service_context: serviceContext,
111
+ token_payload: {
112
+ exp: new Date().getTime() / 1000 + 10000,
113
+ iat: new Date().getTime() / 1000 - 10000,
114
+ sub: 'test-user'
115
+ }
116
+ };
117
+
118
+ const request: BasicRouterRequest = {
119
+ headers: {
120
+ 'accept-encoding': 'gzip'
121
+ },
122
+ hostname: '',
123
+ protocol: 'http'
124
+ };
125
+
126
+ const inputMeta = {
127
+ test: 'test',
128
+ long_meta: 'a'.repeat(1000)
129
+ };
130
+
131
+ const response = await (syncStreamed.handler({
132
+ context,
133
+ params: {
134
+ app_metadata: inputMeta,
135
+ parameters: {
136
+ user_name: 'bob',
137
+ nested_object: {
138
+ nested_key: 'b'.repeat(1000)
139
+ }
140
+ }
141
+ },
142
+ request
143
+ }) as Promise<RouterResponse>);
144
+ expect(response.status).toEqual(200);
145
+ const stream = response.data as Readable;
146
+ const r = await drainWithTimeout(stream).catch((error) => error);
147
+ expect(r.message).toContain('Simulated storage error');
148
+
149
+ // Find the "Sync stream started" log entry
150
+ const syncStartedLog = capturedLogs.find((log) => log.message === 'Sync stream started');
151
+ expect(syncStartedLog).toBeDefined();
152
+
153
+ // Verify that app_metadata from defaultMeta is present in the log
154
+ expect(syncStartedLog?.app_metadata).toBeDefined();
155
+ expect(syncStartedLog?.app_metadata).toEqual(limitParamsForLogging(inputMeta));
156
+ // Should trim long metadata
157
+ expect(syncStartedLog?.app_metadata.long_meta.length).toEqual(
158
+ DEFAULT_PARAM_LOGGING_FORMAT_OPTIONS.maxStringLength
159
+ );
160
+
161
+ // Verify the explicit log parameters
162
+ expect(syncStartedLog?.client_params).toEqual(
163
+ expect.objectContaining({
164
+ user_name: 'bob'
165
+ })
166
+ );
167
+
168
+ expect(typeof syncStartedLog?.client_params.nested_object).toEqual('string');
169
+ expect(syncStartedLog?.client_params.nested_object.length).toEqual(
170
+ DEFAULT_PARAM_LOGGING_FORMAT_OPTIONS.maxStringLength
171
+ );
172
+ });
80
173
  });
81
174
  });
82
175