@powersync/common 0.0.0-dev-20251203144301 → 0.0.0-dev-20260112083235

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 (33) hide show
  1. package/dist/bundle.cjs +116 -82
  2. package/dist/bundle.cjs.map +1 -1
  3. package/dist/bundle.mjs +116 -83
  4. package/dist/bundle.mjs.map +1 -1
  5. package/dist/bundle.node.cjs +104 -70
  6. package/dist/bundle.node.cjs.map +1 -1
  7. package/dist/bundle.node.mjs +104 -71
  8. package/dist/bundle.node.mjs.map +1 -1
  9. package/dist/index.d.cts +25 -2
  10. package/lib/client/AbstractPowerSyncDatabase.d.ts +2 -2
  11. package/lib/client/AbstractPowerSyncDatabase.js +7 -7
  12. package/lib/client/AbstractPowerSyncDatabase.js.map +1 -1
  13. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +4 -0
  14. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +8 -0
  15. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
  16. package/lib/client/sync/stream/streaming-sync-types.d.ts +4 -0
  17. package/lib/client/sync/stream/streaming-sync-types.js.map +1 -1
  18. package/lib/db/ConnectionClosedError.d.ts +10 -0
  19. package/lib/db/ConnectionClosedError.js +21 -0
  20. package/lib/db/ConnectionClosedError.js.map +1 -0
  21. package/lib/db/crud/SyncStatus.d.ts +4 -0
  22. package/lib/db/crud/SyncStatus.js +4 -0
  23. package/lib/db/crud/SyncStatus.js.map +1 -1
  24. package/lib/index.d.ts +1 -0
  25. package/lib/index.js +1 -0
  26. package/lib/index.js.map +1 -1
  27. package/package.json +1 -1
  28. package/src/client/AbstractPowerSyncDatabase.ts +10 -10
  29. package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +18 -0
  30. package/src/client/sync/stream/streaming-sync-types.ts +5 -0
  31. package/src/db/ConnectionClosedError.ts +23 -0
  32. package/src/db/crud/SyncStatus.ts +4 -0
  33. package/src/index.ts +1 -0
@@ -38,13 +38,13 @@ import {
38
38
  type PowerSyncConnectionOptions,
39
39
  type RequiredAdditionalConnectionOptions
40
40
  } from './sync/stream/AbstractStreamingSyncImplementation.js';
41
+ import { CoreSyncStatus, coreStatusToJs } from './sync/stream/core-instruction.js';
42
+ import { SyncStream } from './sync/sync-streams.js';
41
43
  import { TriggerManager } from './triggers/TriggerManager.js';
42
44
  import { TriggerManagerImpl } from './triggers/TriggerManagerImpl.js';
43
45
  import { DEFAULT_WATCH_THROTTLE_MS, WatchCompatibleQuery } from './watched/WatchedQuery.js';
44
46
  import { OnChangeQueryProcessor } from './watched/processors/OnChangeQueryProcessor.js';
45
47
  import { WatchedQueryComparator } from './watched/processors/comparators.js';
46
- import { coreStatusToJs, CoreSyncStatus } from './sync/stream/core-instruction.js';
47
- import { SyncStream } from './sync/sync-streams.js';
48
48
 
49
49
  export interface DisconnectAndClearOptions {
50
50
  /** When set to false, data in local-only tables is preserved. */
@@ -195,7 +195,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
195
195
 
196
196
  /**
197
197
  * The connector used to connect to the PowerSync service.
198
- *
198
+ *
199
199
  * @returns The connector used to connect to the PowerSync service or null if `connect()` has not been called.
200
200
  */
201
201
  get connector() {
@@ -204,7 +204,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
204
204
 
205
205
  /**
206
206
  * The resolved connection options used to connect to the PowerSync service.
207
- *
207
+ *
208
208
  * @returns The resolved connection options used to connect to the PowerSync service or null if `connect()` has not been called.
209
209
  */
210
210
  get connectionOptions() {
@@ -416,7 +416,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
416
416
  protected async initialize() {
417
417
  await this._initialize();
418
418
  await this.bucketStorageAdapter.init();
419
- await this._loadVersion();
419
+ await this.loadVersion();
420
420
  await this.updateSchema(this.options.schema);
421
421
  await this.resolveOfflineSyncStatus();
422
422
  await this.database.execute('PRAGMA RECURSIVE_TRIGGERS=TRUE');
@@ -424,7 +424,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
424
424
  this.iterateListeners((cb) => cb.initialized?.());
425
425
  }
426
426
 
427
- private async _loadVersion() {
427
+ protected async loadVersion() {
428
428
  try {
429
429
  const { version } = await this.database.get<{ version: string }>('SELECT powersync_rs_version() as version');
430
430
  this.sdkVersion = version;
@@ -438,13 +438,13 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
438
438
  .map((n) => parseInt(n));
439
439
  } catch (e) {
440
440
  throw new Error(
441
- `Unsupported powersync extension version. Need >=0.4.5 <1.0.0, got: ${this.sdkVersion}. Details: ${e.message}`
441
+ `Unsupported powersync extension version. Need >=0.4.10 <1.0.0, got: ${this.sdkVersion}. Details: ${e.message}`
442
442
  );
443
443
  }
444
444
 
445
- // Validate >=0.4.5 <1.0.0
446
- if (versionInts[0] != 0 || versionInts[1] < 4 || (versionInts[1] == 4 && versionInts[2] < 5)) {
447
- throw new Error(`Unsupported powersync extension version. Need >=0.4.5 <1.0.0, got: ${this.sdkVersion}`);
445
+ // Validate >=0.4.10 <1.0.0
446
+ if (versionInts[0] != 0 || versionInts[1] < 4 || (versionInts[1] == 4 && versionInts[2] < 10)) {
447
+ throw new Error(`Unsupported powersync extension version. Need >=0.4.10 <1.0.0, got: ${this.sdkVersion}`);
448
448
  }
449
449
  }
450
450
 
@@ -129,6 +129,11 @@ export interface InternalConnectionOptions extends BaseConnectionOptions, Additi
129
129
 
130
130
  /** @internal */
131
131
  export interface BaseConnectionOptions {
132
+ /**
133
+ * A set of metadata to be included in service logs.
134
+ */
135
+ appMetadata?: Record<string, string>;
136
+
132
137
  /**
133
138
  * Whether to use a JavaScript implementation to handle received sync lines from the sync
134
139
  * service, or whether this work should be offloaded to the PowerSync core extension.
@@ -223,6 +228,7 @@ export const DEFAULT_STREAMING_SYNC_OPTIONS = {
223
228
  export type RequiredPowerSyncConnectionOptions = Required<BaseConnectionOptions>;
224
229
 
225
230
  export const DEFAULT_STREAM_CONNECTION_OPTIONS: RequiredPowerSyncConnectionOptions = {
231
+ appMetadata: {},
226
232
  connectionMethod: SyncStreamConnectionMethod.WEB_SOCKET,
227
233
  clientImplementation: DEFAULT_SYNC_CLIENT_IMPLEMENTATION,
228
234
  fetchStrategy: FetchStrategy.Buffered,
@@ -658,6 +664,16 @@ The next upload iteration will be delayed.`);
658
664
  ...DEFAULT_STREAM_CONNECTION_OPTIONS,
659
665
  ...(options ?? {})
660
666
  };
667
+
668
+ // Validate app metadata
669
+ const invalidMetadata = Object.entries(resolvedOptions.appMetadata).filter(
670
+ ([_, value]) => typeof value != 'string'
671
+ );
672
+ if (invalidMetadata.length > 0) {
673
+ throw new Error(
674
+ `Invalid appMetadata provided. Only string values are allowed. Invalid values: ${invalidMetadata.map(([key, value]) => `${key}: ${value}`).join(', ')}`
675
+ );
676
+ }
661
677
  const clientImplementation = resolvedOptions.clientImplementation;
662
678
  this.updateSyncStatus({ clientImplementation });
663
679
 
@@ -699,6 +715,7 @@ The next upload iteration will be delayed.`);
699
715
  include_checksum: true,
700
716
  raw_data: true,
701
717
  parameters: resolvedOptions.params,
718
+ app_metadata: resolvedOptions.appMetadata,
702
719
  client_id: clientId
703
720
  }
704
721
  };
@@ -1088,6 +1105,7 @@ The next upload iteration will be delayed.`);
1088
1105
  try {
1089
1106
  const options: any = {
1090
1107
  parameters: resolvedOptions.params,
1108
+ app_metadata: resolvedOptions.appMetadata,
1091
1109
  active_streams: this.activeStreams,
1092
1110
  include_defaults: resolvedOptions.includeDefaultStreams
1093
1111
  };
@@ -90,6 +90,11 @@ export interface StreamingSyncRequest {
90
90
  */
91
91
  parameters?: Record<string, StreamingSyncRequestParameterType>;
92
92
 
93
+ /**
94
+ * Application metadata to be included in service logs.
95
+ */
96
+ app_metadata?: Record<string, string>;
97
+
93
98
  client_id?: string;
94
99
  }
95
100
 
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Thrown when an underlying database connection is closed.
3
+ * This is particularly relevant when worker connections are marked as closed while
4
+ * operations are still in progress.
5
+ */
6
+ export class ConnectionClosedError extends Error {
7
+ static NAME = 'ConnectionClosedError';
8
+
9
+ static MATCHES(input: any) {
10
+ /**
11
+ * If there are weird package issues which cause multiple versions of classes to be present, the instanceof
12
+ * check might fail. This also performs a failsafe check.
13
+ * This might also happen if the Error is serialized and parsed over a bridging channel like a MessagePort.
14
+ */
15
+ return (
16
+ input instanceof ConnectionClosedError || (input instanceof Error && input.name == ConnectionClosedError.NAME)
17
+ );
18
+ }
19
+ constructor(message: string) {
20
+ super(message);
21
+ this.name = ConnectionClosedError.NAME;
22
+ }
23
+ }
@@ -261,6 +261,10 @@ export class SyncStatus {
261
261
  };
262
262
  }
263
263
 
264
+ /**
265
+ * Not all errors are serializable over a MessagePort. E.g. some `DomExceptions` fail to be passed across workers.
266
+ * This explicitly serializes errors in the SyncStatus.
267
+ */
264
268
  protected serializeError(error?: Error) {
265
269
  if (typeof error == 'undefined') {
266
270
  return undefined;
package/src/index.ts CHANGED
@@ -21,6 +21,7 @@ export * from './client/sync/stream/streaming-sync-types.js';
21
21
  export * from './client/sync/sync-streams.js';
22
22
 
23
23
  export * from './client/ConnectionManager.js';
24
+ export * from './db/ConnectionClosedError.js';
24
25
  export { ProgressWithOperations, SyncProgress } from './db/crud/SyncProgress.js';
25
26
  export * from './db/crud/SyncStatus.js';
26
27
  export * from './db/crud/UploadQueueStatus.js';