@powersync/web 2.6.0 → 2.7.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 (70) hide show
  1. package/dist/index.react_native_web.js +667 -140
  2. package/dist/index.react_native_web.js.map +1 -1
  3. package/dist/worker/{OPFSCoopSyncVFS-CDNBPKfy.js → OPFSCoopSyncVFS-Dujy2ZwX.js} +2 -2
  4. package/dist/worker/{OPFSCoopSyncVFS-CDNBPKfy.js.map → OPFSCoopSyncVFS-Dujy2ZwX.js.map} +1 -1
  5. package/dist/worker/SimpleOpfsFileSystem-6GYD1Xui.js +2 -0
  6. package/dist/worker/SimpleOpfsFileSystem-6GYD1Xui.js.map +1 -0
  7. package/dist/worker/worker.js +1 -1
  8. package/dist/worker/worker.js.map +1 -1
  9. package/lib/db/NavigatorTriggerClaimManager.js +1 -1
  10. package/lib/db/NavigatorTriggerClaimManager.js.map +1 -1
  11. package/lib/db/PowerSyncDatabase.js +1 -1
  12. package/lib/db/PowerSyncDatabase.js.map +1 -1
  13. package/lib/db/adapters/options.d.ts +25 -2
  14. package/lib/db/adapters/options.js.map +1 -1
  15. package/lib/db/adapters/wa-sqlite/ConcurrentConnection.d.ts +56 -7
  16. package/lib/db/adapters/wa-sqlite/ConcurrentConnection.js +130 -25
  17. package/lib/db/adapters/wa-sqlite/ConcurrentConnection.js.map +1 -1
  18. package/lib/db/adapters/wa-sqlite/DatabaseClient.d.ts +13 -0
  19. package/lib/db/adapters/wa-sqlite/DatabaseClient.js.map +1 -1
  20. package/lib/db/adapters/wa-sqlite/RawSqliteConnection.d.ts +2 -1
  21. package/lib/db/adapters/wa-sqlite/RawSqliteConnection.js +12 -6
  22. package/lib/db/adapters/wa-sqlite/RawSqliteConnection.js.map +1 -1
  23. package/lib/db/adapters/wa-sqlite/SimpleOpfsFileSystem.d.ts +30 -0
  24. package/lib/db/adapters/wa-sqlite/SimpleOpfsFileSystem.js +210 -0
  25. package/lib/db/adapters/wa-sqlite/SimpleOpfsFileSystem.js.map +1 -0
  26. package/lib/db/adapters/wa-sqlite/WASQLiteOpenFactory.d.ts +1 -0
  27. package/lib/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +178 -75
  28. package/lib/db/adapters/wa-sqlite/WASQLiteOpenFactory.js.map +1 -1
  29. package/lib/db/adapters/wa-sqlite/vfs.d.ts +30 -4
  30. package/lib/db/adapters/wa-sqlite/vfs.js +53 -2
  31. package/lib/db/adapters/wa-sqlite/vfs.js.map +1 -1
  32. package/lib/db/sync/SharedWebStreamingSyncImplementation.js +1 -1
  33. package/lib/db/sync/SharedWebStreamingSyncImplementation.js.map +1 -1
  34. package/lib/db/sync/WebStreamingSyncImplementation.js +1 -1
  35. package/lib/db/sync/WebStreamingSyncImplementation.js.map +1 -1
  36. package/lib/shared/{tab_close_signal.d.ts → locks.d.ts} +13 -0
  37. package/lib/shared/locks.js +56 -0
  38. package/lib/shared/locks.js.map +1 -0
  39. package/lib/worker/db/MultiDatabaseServer.js +37 -12
  40. package/lib/worker/db/MultiDatabaseServer.js.map +1 -1
  41. package/lib/worker/sync/SharedSyncImplementation.js +1 -1
  42. package/lib/worker/sync/SharedSyncImplementation.js.map +1 -1
  43. package/lib/worker/sync/WorkerClient.js +1 -1
  44. package/lib/worker/sync/WorkerClient.js.map +1 -1
  45. package/lib/worker/worker.js +67 -1
  46. package/lib/worker/worker.js.map +1 -1
  47. package/package.json +3 -3
  48. package/src/db/NavigatorTriggerClaimManager.ts +1 -1
  49. package/src/db/PowerSyncDatabase.ts +1 -1
  50. package/src/db/adapters/options.ts +27 -2
  51. package/src/db/adapters/wa-sqlite/ConcurrentConnection.ts +148 -34
  52. package/src/db/adapters/wa-sqlite/DatabaseClient.ts +16 -0
  53. package/src/db/adapters/wa-sqlite/RawSqliteConnection.ts +16 -7
  54. package/src/db/adapters/wa-sqlite/SimpleOpfsFileSystem.ts +249 -0
  55. package/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts +236 -97
  56. package/src/db/adapters/wa-sqlite/vfs.ts +61 -7
  57. package/src/db/sync/SharedWebStreamingSyncImplementation.ts +1 -1
  58. package/src/db/sync/WebStreamingSyncImplementation.ts +1 -1
  59. package/src/shared/locks.ts +66 -0
  60. package/src/worker/db/MultiDatabaseServer.ts +46 -12
  61. package/src/worker/sync/SharedSyncImplementation.ts +1 -1
  62. package/src/worker/sync/WorkerClient.ts +1 -1
  63. package/src/worker/worker.ts +82 -3
  64. package/lib/shared/navigator.d.ts +0 -1
  65. package/lib/shared/navigator.js +0 -7
  66. package/lib/shared/navigator.js.map +0 -1
  67. package/lib/shared/tab_close_signal.js +0 -27
  68. package/lib/shared/tab_close_signal.js.map +0 -1
  69. package/src/shared/navigator.ts +0 -9
  70. package/src/shared/tab_close_signal.ts +0 -28
@@ -3,17 +3,96 @@ import * as Comlink from 'comlink';
3
3
  import { createConsoleLogger, LogLevels } from '@powersync/common';
4
4
  import { isSharedWorker, MultiDatabaseServer } from './db/MultiDatabaseServer.js';
5
5
  import { SharedSyncImplementation } from './sync/SharedSyncImplementation.js';
6
- import { OpenWorkerConnection } from '../db/adapters/wa-sqlite/DatabaseClient.js';
7
- import { SharedWorkerConnectionRequest } from './SharedWorkerConnectionRequest.js';
6
+ import type { DedicatedFeatureDetectionResult, OpenWorkerConnection } from '../db/adapters/wa-sqlite/DatabaseClient.js';
7
+ import type { SharedWorkerConnectionRequest } from './SharedWorkerConnectionRequest.js';
8
8
  import { WorkerClient } from './sync/WorkerClient.js';
9
9
 
10
+ interface NestedWorker {
11
+ remote: Comlink.Remote<OpenWorkerConnection>;
12
+ features: DedicatedFeatureDetectionResult;
13
+ }
14
+
10
15
  const server = new MultiDatabaseServer(createConsoleLogger({ prefix: 'db-worker', minLevel: LogLevels.trace }));
16
+ let nestedWorker: Promise<NestedWorker> | null = null;
11
17
 
12
18
  const exposedDatabaseFunctions: OpenWorkerConnection = {
13
19
  connect: (config) => server.handleConnection(config),
14
- connectToExisting: ({ identifier, lockName }) => server.connectToExisting(identifier, lockName)
20
+ connectToExisting: ({ identifier, lockName }) => server.connectToExisting(identifier, lockName),
21
+ sharedFeatureDetection: async () => {
22
+ let nestedWorkerSupport: DedicatedFeatureDetectionResult | null = null;
23
+ if ('Worker' in globalThis) {
24
+ try {
25
+ const worker = await (nestedWorker ??= trySpawningNestedWorker());
26
+ nestedWorkerSupport = worker.features;
27
+ } catch (e) {
28
+ // We don't support nested workers.
29
+ }
30
+ }
31
+
32
+ return {
33
+ worker: nestedWorkerSupport
34
+ };
35
+ },
36
+ dedicatedFeatureDetection: async () => {
37
+ return {
38
+ openWithMode: fileSystemSupportsOpenMode(),
39
+ opfs: await checkOpfsSupport()
40
+ };
41
+ },
42
+ spawnNested: async () => {
43
+ const { remote } = await (nestedWorker ??= trySpawningNestedWorker());
44
+
45
+ const port = await remote[Comlink.createEndpoint]();
46
+ return Comlink.transfer(port, [port]);
47
+ }
15
48
  };
16
49
 
50
+ async function checkOpfsSupport(): Promise<boolean> {
51
+ const storage = navigator.storage;
52
+ if (storage == null || !('FileSystemSyncAccessHandle' in globalThis)) return false;
53
+
54
+ try {
55
+ await storage.getDirectory();
56
+ return true;
57
+ } catch (e) {
58
+ return false;
59
+ }
60
+ }
61
+
62
+ function trySpawningNestedWorker(): Promise<NestedWorker> {
63
+ const fork = new Worker(location.href, { type: 'module' });
64
+
65
+ return new Promise((resolve, reject) => {
66
+ let done = false;
67
+ const wrapped = Comlink.wrap<OpenWorkerConnection>(fork);
68
+
69
+ wrapped.dedicatedFeatureDetection().then(
70
+ (features) => {
71
+ done = true;
72
+ resolve({ remote: wrapped, features });
73
+ },
74
+ (e) => {
75
+ done = true;
76
+ reject(e);
77
+ }
78
+ );
79
+
80
+ fork.addEventListener('error', (e) => {
81
+ if (!done) {
82
+ done = true;
83
+ reject(e);
84
+ } else {
85
+ console.error('Nested worker error', e);
86
+ }
87
+ });
88
+ });
89
+ }
90
+
91
+ function fileSystemSupportsOpenMode(): boolean {
92
+ // Credits: https://github.com/whatwg/fs/issues/34#issuecomment-1780168010
93
+ return 'FileSystemSyncAccessHandle' in globalThis && FileSystemSyncAccessHandle.prototype.hasOwnProperty('mode');
94
+ }
95
+
17
96
  if (isSharedWorker) {
18
97
  const sharedSyncImplementation = new SharedSyncImplementation();
19
98
 
@@ -1 +0,0 @@
1
- export declare const getNavigatorLocks: () => LockManager;
@@ -1,7 +0,0 @@
1
- export const getNavigatorLocks = () => {
2
- if ('locks' in navigator && navigator.locks) {
3
- return navigator.locks;
4
- }
5
- throw new Error('Navigator locks are not available in an insecure context. Use a secure context such as HTTPS or http://localhost.');
6
- };
7
- //# sourceMappingURL=navigator.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"navigator.js","sourceRoot":"","sources":["../../src/shared/navigator.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,iBAAiB,GAAG,GAAgB,EAAE;IACjD,IAAI,OAAO,IAAI,SAAS,IAAI,SAAS,CAAC,KAAK,EAAE,CAAC;QAC5C,OAAO,SAAS,CAAC,KAAK,CAAC;IACzB,CAAC;IAED,MAAM,IAAI,KAAK,CACb,mHAAmH,CACpH,CAAC;AACJ,CAAC,CAAC"}
@@ -1,27 +0,0 @@
1
- import { getNavigatorLocks } from './navigator.js';
2
- /**
3
- * Requests a random lock that will be released once the optional signal is aborted (or, if no signal is given, when the
4
- * tab is closed).
5
- *
6
- * This allows sending the name of the lock to another context (e.g. a shared worker), which will also attempt to
7
- * acquire it. Since the lock is returned when the tab is closed, this allows the shared worker to free resources
8
- * assocatiated with this tab.
9
- *
10
- * We take hold of this lock as soon-as-possible in order to cater for potentially closed tabs.
11
- */
12
- export function generateTabCloseSignal(abort) {
13
- return new Promise((resolve, reject) => {
14
- const options = { signal: abort };
15
- getNavigatorLocks()
16
- .request(`tab-close-signal-${crypto.randomUUID()}`, options, (lock) => {
17
- resolve(lock.name);
18
- return new Promise((resolve) => {
19
- if (abort) {
20
- abort.addEventListener('abort', () => resolve());
21
- }
22
- });
23
- })
24
- .catch(reject);
25
- });
26
- }
27
- //# sourceMappingURL=tab_close_signal.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tab_close_signal.js","sourceRoot":"","sources":["../../src/shared/tab_close_signal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAEnD;;;;;;;;;GASG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAAmB;IACxD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,MAAM,OAAO,GAAgB,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;QAC/C,iBAAiB,EAAE;aAChB,OAAO,CAAC,oBAAoB,MAAM,CAAC,UAAU,EAAE,EAAE,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE;YACpE,OAAO,CAAC,IAAK,CAAC,IAAI,CAAC,CAAC;YAEpB,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;gBACnC,IAAI,KAAK,EAAE,CAAC;oBACV,KAAK,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;gBACnD,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC;aACD,KAAK,CAAC,MAAM,CAAC,CAAC;IACnB,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -1,9 +0,0 @@
1
- export const getNavigatorLocks = (): LockManager => {
2
- if ('locks' in navigator && navigator.locks) {
3
- return navigator.locks;
4
- }
5
-
6
- throw new Error(
7
- 'Navigator locks are not available in an insecure context. Use a secure context such as HTTPS or http://localhost.'
8
- );
9
- };
@@ -1,28 +0,0 @@
1
- import { getNavigatorLocks } from './navigator.js';
2
-
3
- /**
4
- * Requests a random lock that will be released once the optional signal is aborted (or, if no signal is given, when the
5
- * tab is closed).
6
- *
7
- * This allows sending the name of the lock to another context (e.g. a shared worker), which will also attempt to
8
- * acquire it. Since the lock is returned when the tab is closed, this allows the shared worker to free resources
9
- * assocatiated with this tab.
10
- *
11
- * We take hold of this lock as soon-as-possible in order to cater for potentially closed tabs.
12
- */
13
- export function generateTabCloseSignal(abort?: AbortSignal): Promise<string> {
14
- return new Promise((resolve, reject) => {
15
- const options: LockOptions = { signal: abort };
16
- getNavigatorLocks()
17
- .request(`tab-close-signal-${crypto.randomUUID()}`, options, (lock) => {
18
- resolve(lock!.name);
19
-
20
- return new Promise<void>((resolve) => {
21
- if (abort) {
22
- abort.addEventListener('abort', () => resolve());
23
- }
24
- });
25
- })
26
- .catch(reject);
27
- });
28
- }