@powersync/service-core 1.19.2 → 1.20.0

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.
Files changed (73) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/dist/api/diagnostics.js +11 -4
  3. package/dist/api/diagnostics.js.map +1 -1
  4. package/dist/entry/commands/compact-action.js +13 -2
  5. package/dist/entry/commands/compact-action.js.map +1 -1
  6. package/dist/entry/commands/config-command.js +2 -2
  7. package/dist/entry/commands/config-command.js.map +1 -1
  8. package/dist/replication/AbstractReplicator.js +2 -5
  9. package/dist/replication/AbstractReplicator.js.map +1 -1
  10. package/dist/routes/configure-fastify.d.ts +84 -0
  11. package/dist/routes/endpoints/admin.d.ts +168 -0
  12. package/dist/routes/endpoints/admin.js +33 -20
  13. package/dist/routes/endpoints/admin.js.map +1 -1
  14. package/dist/routes/endpoints/sync-rules.js +6 -9
  15. package/dist/routes/endpoints/sync-rules.js.map +1 -1
  16. package/dist/storage/BucketStorageFactory.d.ts +43 -15
  17. package/dist/storage/BucketStorageFactory.js +70 -1
  18. package/dist/storage/BucketStorageFactory.js.map +1 -1
  19. package/dist/storage/PersistedSyncRulesContent.d.ts +28 -2
  20. package/dist/storage/PersistedSyncRulesContent.js +79 -1
  21. package/dist/storage/PersistedSyncRulesContent.js.map +1 -1
  22. package/dist/storage/StorageVersionConfig.d.ts +20 -0
  23. package/dist/storage/StorageVersionConfig.js +20 -0
  24. package/dist/storage/StorageVersionConfig.js.map +1 -0
  25. package/dist/storage/SyncRulesBucketStorage.d.ts +2 -1
  26. package/dist/storage/SyncRulesBucketStorage.js.map +1 -1
  27. package/dist/storage/storage-index.d.ts +1 -0
  28. package/dist/storage/storage-index.js +1 -0
  29. package/dist/storage/storage-index.js.map +1 -1
  30. package/dist/sync/BucketChecksumState.d.ts +6 -2
  31. package/dist/sync/BucketChecksumState.js +85 -10
  32. package/dist/sync/BucketChecksumState.js.map +1 -1
  33. package/dist/util/config/collectors/config-collector.js +13 -0
  34. package/dist/util/config/collectors/config-collector.js.map +1 -1
  35. package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.d.ts +1 -1
  36. package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.js +4 -4
  37. package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.js.map +1 -1
  38. package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.d.ts +1 -1
  39. package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.js +2 -2
  40. package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.js.map +1 -1
  41. package/dist/util/config/sync-rules/impl/inline-sync-rules-collector.d.ts +1 -1
  42. package/dist/util/config/sync-rules/impl/inline-sync-rules-collector.js +3 -3
  43. package/dist/util/config/sync-rules/impl/inline-sync-rules-collector.js.map +1 -1
  44. package/dist/util/config/types.d.ts +1 -1
  45. package/dist/util/config/types.js.map +1 -1
  46. package/dist/util/env.d.ts +1 -0
  47. package/dist/util/env.js +5 -0
  48. package/dist/util/env.js.map +1 -1
  49. package/package.json +6 -6
  50. package/src/api/diagnostics.ts +12 -4
  51. package/src/entry/commands/compact-action.ts +15 -2
  52. package/src/entry/commands/config-command.ts +3 -3
  53. package/src/replication/AbstractReplicator.ts +3 -5
  54. package/src/routes/endpoints/admin.ts +42 -25
  55. package/src/routes/endpoints/sync-rules.ts +14 -13
  56. package/src/storage/BucketStorageFactory.ts +110 -19
  57. package/src/storage/PersistedSyncRulesContent.ts +114 -4
  58. package/src/storage/StorageVersionConfig.ts +30 -0
  59. package/src/storage/SyncRulesBucketStorage.ts +2 -1
  60. package/src/storage/storage-index.ts +1 -0
  61. package/src/sync/BucketChecksumState.ts +129 -16
  62. package/src/util/config/collectors/config-collector.ts +16 -0
  63. package/src/util/config/sync-rules/impl/base64-sync-rules-collector.ts +5 -5
  64. package/src/util/config/sync-rules/impl/filesystem-sync-rules-collector.ts +3 -3
  65. package/src/util/config/sync-rules/impl/inline-sync-rules-collector.ts +4 -4
  66. package/src/util/config/types.ts +1 -2
  67. package/src/util/env.ts +5 -0
  68. package/test/src/config.test.ts +115 -0
  69. package/test/src/routes/admin.test.ts +48 -0
  70. package/test/src/routes/mocks.ts +22 -1
  71. package/test/src/routes/stream.test.ts +3 -2
  72. package/test/src/sync/BucketChecksumState.test.ts +285 -78
  73. package/tsconfig.tsbuildinfo +1 -1
@@ -1,6 +1,6 @@
1
+ import { configFile } from '@powersync/service-types';
1
2
  import { RunnerConfig, SyncRulesConfig } from '../../types.js';
2
3
  import { SyncRulesCollector } from '../sync-collector.js';
3
- import { configFile } from '@powersync/service-types';
4
4
 
5
5
  export class Base64SyncRulesCollector extends SyncRulesCollector {
6
6
  get name(): string {
@@ -8,15 +8,15 @@ export class Base64SyncRulesCollector extends SyncRulesCollector {
8
8
  }
9
9
 
10
10
  async collect(baseConfig: configFile.PowerSyncConfig, runnerConfig: RunnerConfig): Promise<SyncRulesConfig | null> {
11
- const { sync_rules_base64 } = runnerConfig;
12
- if (!sync_rules_base64) {
11
+ const { sync_config_base64 } = runnerConfig;
12
+ if (!sync_config_base64) {
13
13
  return null;
14
14
  }
15
15
 
16
16
  return {
17
17
  present: true,
18
- exit_on_error: baseConfig.sync_rules?.exit_on_error ?? true,
19
- content: Buffer.from(sync_rules_base64, 'base64').toString()
18
+ exit_on_error: baseConfig.sync_config?.exit_on_error ?? true,
19
+ content: Buffer.from(sync_config_base64, 'base64').toString()
20
20
  };
21
21
  }
22
22
  }
@@ -1,7 +1,7 @@
1
+ import { configFile } from '@powersync/service-types';
1
2
  import * as path from 'path';
2
3
  import { RunnerConfig, SyncRulesConfig } from '../../types.js';
3
4
  import { SyncRulesCollector } from '../sync-collector.js';
4
- import { configFile } from '@powersync/service-types';
5
5
 
6
6
  export class FileSystemSyncRulesCollector extends SyncRulesCollector {
7
7
  get name(): string {
@@ -9,7 +9,7 @@ export class FileSystemSyncRulesCollector extends SyncRulesCollector {
9
9
  }
10
10
 
11
11
  async collect(baseConfig: configFile.PowerSyncConfig, runnerConfig: RunnerConfig): Promise<SyncRulesConfig | null> {
12
- const sync_path = baseConfig.sync_rules?.path;
12
+ const sync_path = baseConfig.sync_config?.path;
13
13
  if (!sync_path) {
14
14
  return null;
15
15
  }
@@ -20,7 +20,7 @@ export class FileSystemSyncRulesCollector extends SyncRulesCollector {
20
20
  // Only persist the path here, and load on demand using `loadSyncRules()`.
21
21
  return {
22
22
  present: true,
23
- exit_on_error: baseConfig.sync_rules?.exit_on_error ?? true,
23
+ exit_on_error: baseConfig.sync_config?.exit_on_error ?? true,
24
24
  path: config_path ? path.resolve(path.dirname(config_path), sync_path) : sync_path
25
25
  };
26
26
  }
@@ -1,6 +1,6 @@
1
+ import { configFile } from '@powersync/service-types';
1
2
  import { SyncRulesConfig } from '../../types.js';
2
3
  import { SyncRulesCollector } from '../sync-collector.js';
3
- import { configFile } from '@powersync/service-types';
4
4
 
5
5
  export class InlineSyncRulesCollector extends SyncRulesCollector {
6
6
  get name(): string {
@@ -8,15 +8,15 @@ export class InlineSyncRulesCollector extends SyncRulesCollector {
8
8
  }
9
9
 
10
10
  async collect(baseConfig: configFile.PowerSyncConfig): Promise<SyncRulesConfig | null> {
11
- const content = baseConfig.sync_rules?.content;
11
+ const content = baseConfig?.sync_config?.content;
12
12
  if (!content) {
13
13
  return null;
14
14
  }
15
15
 
16
16
  return {
17
17
  present: true,
18
- exit_on_error: true,
19
- ...baseConfig.sync_rules
18
+ exit_on_error: baseConfig.sync_config?.exit_on_error ?? true,
19
+ ...baseConfig.sync_config
20
20
  };
21
21
  }
22
22
  }
@@ -1,6 +1,5 @@
1
1
  import { configFile } from '@powersync/service-types';
2
2
  import { CompoundKeyCollector } from '../../auth/CompoundKeyCollector.js';
3
- import { KeySpec } from '../../auth/KeySpec.js';
4
3
  import { KeyStore } from '../../auth/KeyStore.js';
5
4
 
6
5
  export enum ServiceRunner {
@@ -12,7 +11,7 @@ export enum ServiceRunner {
12
11
  export type RunnerConfig = {
13
12
  config_path?: string;
14
13
  config_base64?: string;
15
- sync_rules_base64?: string;
14
+ sync_config_base64?: string;
16
15
  };
17
16
 
18
17
  export type MigrationContext = {
package/src/util/env.ts CHANGED
@@ -12,9 +12,14 @@ export const env = utils.collectEnvironmentVariables({
12
12
  */
13
13
  POWERSYNC_CONFIG_B64: utils.type.string.optional(),
14
14
  /**
15
+ * @deprecated use POWERSYNC_SYNC_CONFIG_B64 instead.
15
16
  * Base64 encoded contents of sync rules YAML
16
17
  */
17
18
  POWERSYNC_SYNC_RULES_B64: utils.type.string.optional(),
19
+ /**
20
+ * Base64 encoded contents of sync config YAML
21
+ */
22
+ POWERSYNC_SYNC_CONFIG_B64: utils.type.string.optional(),
18
23
  /**
19
24
  * Runner to be started in this process
20
25
  */
@@ -69,6 +69,7 @@ describe('Config', () => {
69
69
 
70
70
  expect(config.api_parameters.max_buckets_per_connection).toBe(1);
71
71
  });
72
+
72
73
  it('should throw YAML validation error for invalid base64 config', {}, async () => {
73
74
  const yamlConfig = /* yaml */ `
74
75
  # PowerSync config
@@ -86,4 +87,118 @@ describe('Config', () => {
86
87
  })
87
88
  ).rejects.toThrow(/YAML Error:[\s\S]*Attempting to substitute environment variable INVALID_VAR/);
88
89
  });
90
+
91
+ it('should resolve inline sync config', async () => {
92
+ const yamlConfig = /* yaml */ `
93
+ # PowerSync config
94
+ replication:
95
+ connections: []
96
+ storage:
97
+ type: mongodb
98
+ sync_config:
99
+ content: |
100
+ config:
101
+ edition: 2
102
+ streams:
103
+ a:
104
+ query: SELECT * FROM users
105
+ `;
106
+
107
+ const collector = new CompoundConfigCollector();
108
+
109
+ const result = await collector.collectConfig({
110
+ config_base64: Buffer.from(yamlConfig, 'utf-8').toString('base64')
111
+ });
112
+
113
+ expect(result.sync_rules).toEqual({
114
+ present: true,
115
+ exit_on_error: true,
116
+ content: expect.stringContaining('edition: 2')
117
+ });
118
+ });
119
+
120
+ it('should still resolve inline sync rules', async () => {
121
+ const yamlConfig = /* yaml */ `
122
+ # PowerSync config
123
+ replication:
124
+ connections: []
125
+ storage:
126
+ type: mongodb
127
+ sync_rules:
128
+ content: |
129
+ config:
130
+ edition: 2
131
+ streams:
132
+ a:
133
+ query: SELECT * FROM users
134
+ `;
135
+
136
+ const collector = new CompoundConfigCollector();
137
+
138
+ const result = await collector.collectConfig({
139
+ config_base64: Buffer.from(yamlConfig, 'utf-8').toString('base64')
140
+ });
141
+
142
+ expect(result.sync_rules).toEqual({
143
+ present: true,
144
+ exit_on_error: true,
145
+ content: expect.stringContaining('edition: 2')
146
+ });
147
+ });
148
+
149
+ it('should resolve base64 sync config', async () => {
150
+ const yamlConfig = /* yaml */ `
151
+ # PowerSync config
152
+ replication:
153
+ connections: []
154
+ storage:
155
+ type: mongodb
156
+ `;
157
+ const yamlSyncConfig = /* yaml */ `
158
+ config:
159
+ edition: 2
160
+ streams:
161
+ a:
162
+ query: SELECT * FROM users
163
+ `;
164
+
165
+ const collector = new CompoundConfigCollector();
166
+
167
+ const result = await collector.collectConfig({
168
+ config_base64: Buffer.from(yamlConfig, 'utf-8').toString('base64'),
169
+ sync_config_base64: Buffer.from(yamlSyncConfig, 'utf-8').toString('base64')
170
+ });
171
+
172
+ expect(result.sync_rules).toEqual({
173
+ present: true,
174
+ exit_on_error: true,
175
+ content: expect.stringContaining('edition: 2')
176
+ });
177
+ });
178
+
179
+ it('should not allow both sync_config and sync_rules', async () => {
180
+ const yamlConfig = /* yaml */ `
181
+ # PowerSync config
182
+ replication:
183
+ connections: []
184
+ storage:
185
+ type: mongodb
186
+ sync_config:
187
+ content: |
188
+ config:
189
+ edition: 2
190
+ sync_rules:
191
+ content: |
192
+ config:
193
+ edition: 2
194
+ `;
195
+
196
+ const collector = new CompoundConfigCollector();
197
+
198
+ await expect(
199
+ collector.collectConfig({
200
+ config_base64: Buffer.from(yamlConfig, 'utf-8').toString('base64')
201
+ })
202
+ ).rejects.toThrow(/Both `sync_config` and `sync_rules` are present/);
203
+ });
89
204
  });
@@ -0,0 +1,48 @@
1
+ import { BasicRouterRequest, Context, JwtPayload } from '@/index.js';
2
+ import { logger } from '@powersync/lib-services-framework';
3
+ import { describe, expect, it } from 'vitest';
4
+ import { validate } from '../../../src/routes/endpoints/admin.js';
5
+ import { mockServiceContext } from './mocks.js';
6
+
7
+ describe('admin routes', () => {
8
+ describe('validate', () => {
9
+ it('reports errors with source location', async () => {
10
+ const context: Context = {
11
+ logger: logger,
12
+ service_context: mockServiceContext(null),
13
+ token_payload: new JwtPayload({
14
+ sub: '',
15
+ exp: 0,
16
+ iat: 0
17
+ })
18
+ };
19
+
20
+ const request: BasicRouterRequest = {
21
+ headers: {},
22
+ hostname: '',
23
+ protocol: 'http'
24
+ };
25
+
26
+ const response = await validate.handler({
27
+ context,
28
+ params: {
29
+ sync_rules: `
30
+ bucket_definitions:
31
+ missing_table:
32
+ data:
33
+ - SELECT * FROM missing_table
34
+ `
35
+ },
36
+ request
37
+ });
38
+
39
+ expect(response.errors).toEqual([
40
+ expect.objectContaining({
41
+ level: 'warning',
42
+ location: { start_offset: 70, end_offset: 83 },
43
+ message: 'Table public.missing_table not found'
44
+ })
45
+ ]);
46
+ });
47
+ });
48
+ });
@@ -41,8 +41,29 @@ export function mockServiceContext(storage: Partial<SyncRulesBucketStorage> | nu
41
41
  return {
42
42
  getParseSyncRulesOptions() {
43
43
  return { defaultSchema: 'public' };
44
+ },
45
+ async getSourceConfig() {
46
+ return {
47
+ tag: 'test_tag',
48
+ id: 'test_id',
49
+ type: 'test_type'
50
+ };
51
+ },
52
+ async getConnectionSchema() {
53
+ return [];
54
+ },
55
+ async getConnectionStatus() {
56
+ return {
57
+ id: 'test_id',
58
+ uri: 'http://example.org/',
59
+ connected: true,
60
+ errors: []
61
+ };
62
+ },
63
+ async getDebugTablesInfo() {
64
+ return [];
44
65
  }
45
- } as Partial<RouteAPI>;
66
+ } satisfies Partial<RouteAPI> as unknown as RouteAPI;
46
67
  },
47
68
  addStopHandler() {
48
69
  return () => {};
@@ -8,6 +8,7 @@ import winston from 'winston';
8
8
  import { syncStreamed } from '../../../src/routes/endpoints/sync-stream.js';
9
9
  import { DEFAULT_PARAM_LOGGING_FORMAT_OPTIONS, limitParamsForLogging } from '../../../src/util/param-logging.js';
10
10
  import { mockServiceContext } from './mocks.js';
11
+ import { DEFAULT_HYDRATION_STATE } from '@powersync/service-sync-rules';
11
12
 
12
13
  describe('Stream Route', () => {
13
14
  describe('compressed stream', () => {
@@ -45,7 +46,7 @@ describe('Stream Route', () => {
45
46
 
46
47
  const storage = {
47
48
  getParsedSyncRules() {
48
- return new SqlSyncRules('bucket_definitions: {}').hydrate();
49
+ return new SqlSyncRules('bucket_definitions: {}').hydrate({ hydrationState: DEFAULT_HYDRATION_STATE });
49
50
  },
50
51
  watchCheckpointChanges: async function* (options) {
51
52
  throw new Error('Simulated storage error');
@@ -83,7 +84,7 @@ describe('Stream Route', () => {
83
84
  it('logs the application metadata', async () => {
84
85
  const storage = {
85
86
  getParsedSyncRules() {
86
- return new SqlSyncRules('bucket_definitions: {}').hydrate();
87
+ return new SqlSyncRules('bucket_definitions: {}').hydrate({ hydrationState: DEFAULT_HYDRATION_STATE });
87
88
  },
88
89
  watchCheckpointChanges: async function* (options) {
89
90
  throw new Error('Simulated storage error');