@tallyui/storage-sqlite 2.0.0 → 3.0.0-next.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.
- package/README.md +62 -9
- package/dist/chunk-3OYD5CSS.js +32 -0
- package/dist/index.d.ts +3 -5
- package/dist/index.js +3 -0
- package/dist/web/index.d.ts +32 -4
- package/dist/web/index.js +22 -2
- package/dist/web/worker.js +22 -6
- package/package.json +8 -5
- package/src/handle-types.test-d.ts +17 -0
- package/src/rx-storage-sqlite.ts +8 -0
- package/src/types.ts +4 -2
- package/src/web/errors.ts +32 -6
- package/src/web/index.ts +10 -1
- package/src/web/is-storage-worker-start-error.ts +20 -0
- package/src/web/worker.ts +34 -10
- package/dist/chunk-CNKWKIZV.js +0 -18
package/README.md
CHANGED
|
@@ -10,6 +10,33 @@ worker on the web (ADR-061 in `docs/DECISIONS.md`).
|
|
|
10
10
|
optional peer dependencies: a native app installs only the first, a web app
|
|
11
11
|
only the second.
|
|
12
12
|
|
|
13
|
+
## Upgrading to 3.0.0
|
|
14
|
+
|
|
15
|
+
3.0.0 moves to RxDB and RxDB Premium 17.5.0 (with `@tallyui/pos` and
|
|
16
|
+
`@tallyui/database` 3.0.0). See ADR-069 in `docs/DECISIONS.md`.
|
|
17
|
+
|
|
18
|
+
- **Storage is one-way.** Once a till has opened 3.0.0, an older build shows
|
|
19
|
+
no orders and sends none of the pending ones until the till is upgraded
|
|
20
|
+
again. Nothing is deleted. Never roll an app back across 3.0.0, and never
|
|
21
|
+
re-ring the sales it hides.
|
|
22
|
+
- A web app ships the 17.5.0 storage worker with the 17.5.0 main thread.
|
|
23
|
+
- Pin `rxdb` and `rxdb-premium` to exactly `17.5.0`.
|
|
24
|
+
- RxDB 17 defaults `toggleOnDocumentVisible` to true: a replication no longer
|
|
25
|
+
keeps a hidden tab awake, so a browser may throttle its pull (see the
|
|
26
|
+
changeset for the detail).
|
|
27
|
+
|
|
28
|
+
## Supported handles
|
|
29
|
+
|
|
30
|
+
- Native: expo-sqlite 16's `SQLiteDatabase` from `openDatabaseSync`, checked at
|
|
31
|
+
compile time by `typecheck/expo-handle.ts`. The peer range allows 15, which
|
|
32
|
+
isn't checked.
|
|
33
|
+
- Node: a `node:sqlite` `DatabaseSync` wrapped like `src/node-sqlite.test-helper.ts`.
|
|
34
|
+
- Web: `getRxStorageSQLiteWasm` takes `workerInput`; the worker opens its own
|
|
35
|
+
SQLite-wasm `opfs-sahpool` handle, so callers do not supply a database handle.
|
|
36
|
+
|
|
37
|
+
Any handle with `execSync`, `getAllSync`, and `runSync` matching the minimal
|
|
38
|
+
`SQLiteDatabase` interface works with `getRxStorageSQLite`.
|
|
39
|
+
|
|
13
40
|
## Web (SQLite-wasm)
|
|
14
41
|
|
|
15
42
|
`getRxStorageSQLiteWasm` (from `@tallyui/storage-sqlite/web`) runs SQLite-wasm
|
|
@@ -87,22 +114,48 @@ the function instead.
|
|
|
87
114
|
|
|
88
115
|
### Recognising a failed start
|
|
89
116
|
|
|
90
|
-
If the worker's start-up fails
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
117
|
+
If the worker's start-up fails, every storage call rejects instead of
|
|
118
|
+
hanging. Each error carries its cause's name and message in its own message,
|
|
119
|
+
since the cause itself doesn't cross the worker channel. Three failures have
|
|
120
|
+
their own predicate, and their own sentence for the cashier:
|
|
121
|
+
|
|
122
|
+
- **Storage unavailable: `isStorageUnavailableError`** (a
|
|
123
|
+
`StorageUnavailableError`). The browser gives the page no usable OPFS:
|
|
124
|
+
`navigator.storage.getDirectory()` fails, or sync access handles are
|
|
125
|
+
missing. This is what happens in a **private window**: Safari Private
|
|
126
|
+
Browsing (WebKit's ephemeral storage, #293) refuses OPFS. No other tab is
|
|
127
|
+
involved, and neither closing tabs nor reloading helps.
|
|
128
|
+
- **Held by another tab: `isStorageHeldError`** (a `StorageWorkerStartError`
|
|
129
|
+
whose message says another tab holds the database). OPFS is reachable, but
|
|
130
|
+
another tab holds the opfs-sahpool database (WebKit's `InvalidStateError`,
|
|
131
|
+
Chromium's `NoModificationAllowedError`).
|
|
132
|
+
- **Stale worker: `isRxdbRemoteVersionMismatch`** from `@tallyui/core`
|
|
133
|
+
(RxDB's RM1, #280). After an upgrade the browser may still have the old
|
|
134
|
+
worker cached, and RxDB refuses it; a reload loads the matching worker.
|
|
135
|
+
|
|
136
|
+
Any other start failure is a generic `StorageWorkerStartError` ("SQLite
|
|
137
|
+
worker start failed", with its cause). `isStorageWorkerStartError` is true
|
|
138
|
+
for any failed start except storage unavailable; use the three predicates
|
|
139
|
+
above to choose the wording, in this order:
|
|
95
140
|
|
|
96
141
|
```ts
|
|
97
|
-
import {
|
|
142
|
+
import { isRxdbRemoteVersionMismatch } from '@tallyui/core';
|
|
143
|
+
import { isStorageHeldError, isStorageUnavailableError } from '@tallyui/storage-sqlite/web';
|
|
98
144
|
|
|
99
145
|
try {
|
|
100
146
|
await db.addCollections({ /* ... */ });
|
|
101
147
|
} catch (error) {
|
|
102
|
-
if (
|
|
103
|
-
showMessage(
|
|
148
|
+
if (isStorageUnavailableError(error)) {
|
|
149
|
+
showMessage(
|
|
150
|
+
"This till can't save sales in a private window.",
|
|
151
|
+
'Open it in a normal Safari window (or another browser) and sign in again. Nothing has been lost: no sale was taken here.'
|
|
152
|
+
);
|
|
153
|
+
} else if (isStorageHeldError(error)) {
|
|
154
|
+
showMessage('This till is already open in another tab. Close the other tab, then reload this one.');
|
|
155
|
+
} else if (isRxdbRemoteVersionMismatch(error)) {
|
|
156
|
+
showMessage('This till needs a quick reload to finish updating. Reload the page.');
|
|
104
157
|
} else {
|
|
105
|
-
throw error;
|
|
158
|
+
throw error; // or the app's own generic failure message
|
|
106
159
|
}
|
|
107
160
|
}
|
|
108
161
|
```
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// src/web/errors.ts
|
|
2
|
+
var StorageWorkerStartError = class extends Error {
|
|
3
|
+
constructor(message, options) {
|
|
4
|
+
super(message, options);
|
|
5
|
+
this.name = "StorageWorkerStartError";
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
var StorageUnavailableError = class extends Error {
|
|
9
|
+
constructor(message, options) {
|
|
10
|
+
super(message, options);
|
|
11
|
+
this.name = "StorageUnavailableError";
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var STORAGE_HELD_MARKER = "another tab holds the database";
|
|
15
|
+
function isStorageHeldError(error) {
|
|
16
|
+
const text = typeof error === "string" ? error : error?.message;
|
|
17
|
+
return typeof text === "string" && text.includes("StorageWorkerStartError") && text.includes(STORAGE_HELD_MARKER);
|
|
18
|
+
}
|
|
19
|
+
function isStorageUnavailableError(error) {
|
|
20
|
+
if (typeof error === "string") return error.includes("StorageUnavailableError");
|
|
21
|
+
if (!error || typeof error !== "object") return false;
|
|
22
|
+
const { name, message } = error;
|
|
23
|
+
return name === "StorageUnavailableError" || typeof message === "string" && message.includes("StorageUnavailableError");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
StorageWorkerStartError,
|
|
28
|
+
StorageUnavailableError,
|
|
29
|
+
STORAGE_HELD_MARKER,
|
|
30
|
+
isStorageHeldError,
|
|
31
|
+
isStorageUnavailableError
|
|
32
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { RxStorage } from 'rxdb';
|
|
2
|
+
import { SQLResultRow } from 'rxdb/plugins/storage-sqlite';
|
|
2
3
|
|
|
3
4
|
interface SQLiteDatabase {
|
|
4
5
|
execSync(source: string): void;
|
|
5
|
-
getAllSync
|
|
6
|
-
runSync(source: string, params
|
|
7
|
-
changes: number;
|
|
8
|
-
lastInsertRowId: number;
|
|
9
|
-
};
|
|
6
|
+
getAllSync(source: string, params: (string | number)[]): SQLResultRow[];
|
|
7
|
+
runSync(source: string, params: (string | number)[]): unknown;
|
|
10
8
|
}
|
|
11
9
|
interface SQLiteStorageSettings {
|
|
12
10
|
database: SQLiteDatabase;
|
package/dist/index.js
CHANGED
|
@@ -3,8 +3,11 @@ import {
|
|
|
3
3
|
} from "./chunk-LBITF2LV.js";
|
|
4
4
|
|
|
5
5
|
// src/rx-storage-sqlite.ts
|
|
6
|
+
import { setPremiumFlag } from "rxdb-premium/plugins/shared";
|
|
6
7
|
import { getRxStorageSQLite as getPremiumRxStorageSQLite } from "rxdb-premium/plugins/storage-sqlite";
|
|
8
|
+
setPremiumFlag();
|
|
7
9
|
function getRxStorageSQLite(database) {
|
|
10
|
+
setPremiumFlag();
|
|
8
11
|
let databaseName;
|
|
9
12
|
const sqliteBasics = {
|
|
10
13
|
debugId: "tallyui-sqlite-sync",
|
package/dist/web/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { RxStorage } from 'rxdb';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Thrown by the web SQLite worker's storage (ADR-061) when start-up failed
|
|
5
|
-
* another tab
|
|
6
|
-
*
|
|
4
|
+
* Thrown by the web SQLite worker's storage (ADR-061) when start-up failed
|
|
5
|
+
* with OPFS reachable: another tab holds the opfs-sahpool database, or an
|
|
6
|
+
* unexpected failure. Keeps the original failure as `cause`, and its name
|
|
7
|
+
* and message in this error's message.
|
|
7
8
|
*
|
|
8
9
|
* RxDB's remote-storage channel re-throws a rejected `createStorageInstance`
|
|
9
10
|
* on the main thread as a plain `Error` whose message embeds the original
|
|
@@ -17,6 +18,33 @@ declare class StorageWorkerStartError extends Error {
|
|
|
17
18
|
cause?: unknown;
|
|
18
19
|
});
|
|
19
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Thrown by the worker's storage when the browser gives it no usable OPFS, as in a Safari private window (#293):
|
|
23
|
+
* `getDirectory()` fails or sync access handles are missing. Closing tabs or reloading can't help, so its message,
|
|
24
|
+
* which carries its name like `StorageWorkerStartError`'s, never contains `StorageWorkerStartError`.
|
|
25
|
+
*/
|
|
26
|
+
declare class StorageUnavailableError extends Error {
|
|
27
|
+
constructor(message: string, options?: {
|
|
28
|
+
cause?: unknown;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* True for the held `StorageWorkerStartError` (another tab holds the database), its `{ name, message }` copy or its
|
|
33
|
+
* message; false for any other start failure, a stale worker (RM1) or storage unavailable.
|
|
34
|
+
*/
|
|
35
|
+
declare function isStorageHeldError(error: unknown): boolean;
|
|
36
|
+
/** True for a `StorageUnavailableError`, its `{ name, message }` copy or its message (see above). */
|
|
37
|
+
declare function isStorageUnavailableError(error: unknown): boolean;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* True for a `StorageWorkerStartError`, its `{ name, message }` copy or its
|
|
41
|
+
* message (see ./errors), and for RxDB's RM1: a stale worker is a failed
|
|
42
|
+
* start too, and reloading loads the matching worker. False for a
|
|
43
|
+
* `StorageUnavailableError`, whose message never names this error.
|
|
44
|
+
* It means "any failed start": to choose wording, an app maps the three
|
|
45
|
+
* specific predicates instead: `isStorageUnavailableError`,
|
|
46
|
+
* `isStorageHeldError`, and `isRxdbRemoteVersionMismatch` (@tallyui/core).
|
|
47
|
+
*/
|
|
20
48
|
declare function isStorageWorkerStartError(error: unknown): boolean;
|
|
21
49
|
|
|
22
50
|
/** Marks a storage as this package's web engine (ADR-061), for job 2b to recognise. */
|
|
@@ -40,4 +68,4 @@ declare function getRxStorageSQLiteWasm(options: {
|
|
|
40
68
|
workerName?: string;
|
|
41
69
|
}): RxStorageSQLiteWasm;
|
|
42
70
|
|
|
43
|
-
export { type RxStorageSQLiteWasm, SQLITE_SAHPOOL_ENGINE, StorageWorkerStartError, getRxStorageSQLiteWasm, isStorageWorkerStartError };
|
|
71
|
+
export { type RxStorageSQLiteWasm, SQLITE_SAHPOOL_ENGINE, StorageUnavailableError, StorageWorkerStartError, getRxStorageSQLiteWasm, isStorageHeldError, isStorageUnavailableError, isStorageWorkerStartError };
|
package/dist/web/index.js
CHANGED
|
@@ -1,13 +1,30 @@
|
|
|
1
1
|
import {
|
|
2
|
+
StorageUnavailableError,
|
|
2
3
|
StorageWorkerStartError,
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
isStorageHeldError,
|
|
5
|
+
isStorageUnavailableError
|
|
6
|
+
} from "../chunk-3OYD5CSS.js";
|
|
5
7
|
|
|
6
8
|
// src/web/index.ts
|
|
7
9
|
import { MESSAGE_CHANNEL_CACHE_BY_IDENTIFIER, OPEN_REMOTE_MESSAGE_CHANNELS } from "rxdb/plugins/storage-remote";
|
|
10
|
+
import { setPremiumFlag } from "rxdb-premium/plugins/shared";
|
|
8
11
|
import { getRxStorageWorker } from "rxdb-premium/plugins/storage-worker";
|
|
12
|
+
|
|
13
|
+
// src/web/is-storage-worker-start-error.ts
|
|
14
|
+
import { isRxdbRemoteVersionMismatch } from "@tallyui/core";
|
|
15
|
+
function isStorageWorkerStartError(error) {
|
|
16
|
+
if (isRxdbRemoteVersionMismatch(error)) return true;
|
|
17
|
+
if (typeof error === "string") return error.includes("StorageWorkerStartError");
|
|
18
|
+
if (!error || typeof error !== "object") return false;
|
|
19
|
+
const { name, message } = error;
|
|
20
|
+
return name === "StorageWorkerStartError" || typeof message === "string" && message.includes("StorageWorkerStartError");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/web/index.ts
|
|
24
|
+
setPremiumFlag();
|
|
9
25
|
var SQLITE_SAHPOOL_ENGINE = "sqlite-sahpool";
|
|
10
26
|
function getRxStorageSQLiteWasm(options) {
|
|
27
|
+
setPremiumFlag();
|
|
11
28
|
const { workerInput } = options;
|
|
12
29
|
const workerOptions = { type: "module", name: options.workerName ?? "tallyui-storage" };
|
|
13
30
|
let worker;
|
|
@@ -36,7 +53,10 @@ function getRxStorageSQLiteWasm(options) {
|
|
|
36
53
|
}
|
|
37
54
|
export {
|
|
38
55
|
SQLITE_SAHPOOL_ENGINE,
|
|
56
|
+
StorageUnavailableError,
|
|
39
57
|
StorageWorkerStartError,
|
|
40
58
|
getRxStorageSQLiteWasm,
|
|
59
|
+
isStorageHeldError,
|
|
60
|
+
isStorageUnavailableError,
|
|
41
61
|
isStorageWorkerStartError
|
|
42
62
|
};
|
package/dist/web/worker.js
CHANGED
|
@@ -2,11 +2,14 @@ import {
|
|
|
2
2
|
sqliteBoolParams
|
|
3
3
|
} from "../chunk-LBITF2LV.js";
|
|
4
4
|
import {
|
|
5
|
+
STORAGE_HELD_MARKER,
|
|
6
|
+
StorageUnavailableError,
|
|
5
7
|
StorageWorkerStartError
|
|
6
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-3OYD5CSS.js";
|
|
7
9
|
|
|
8
10
|
// src/web/worker.ts
|
|
9
11
|
import sqlite3InitModule from "@sqlite.org/sqlite-wasm";
|
|
12
|
+
import { setPremiumFlag } from "rxdb-premium/plugins/shared";
|
|
10
13
|
import { getRxStorageSQLite } from "rxdb-premium/plugins/storage-sqlite";
|
|
11
14
|
import { exposeWorkerRxStorage } from "rxdb-premium/plugins/storage-worker";
|
|
12
15
|
|
|
@@ -57,10 +60,23 @@ function getSQLiteBasicsOpfsSahPool({ openDb }) {
|
|
|
57
60
|
}
|
|
58
61
|
|
|
59
62
|
// src/web/worker.ts
|
|
63
|
+
var HELD_ERROR_NAMES = ["InvalidStateError", "NoModificationAllowedError"];
|
|
64
|
+
var describeCause = (cause) => cause instanceof Error ? `${cause.name}: ${cause.message}` : String(cause);
|
|
65
|
+
async function requireOpfs() {
|
|
66
|
+
try {
|
|
67
|
+
const handle = globalThis.FileSystemFileHandle?.prototype ?? {};
|
|
68
|
+
if (!("createSyncAccessHandle" in handle)) throw new TypeError("FileSystemFileHandle.createSyncAccessHandle is missing");
|
|
69
|
+
await navigator.storage.getDirectory();
|
|
70
|
+
} catch (cause) {
|
|
71
|
+
const why = `this browser gives the page no OPFS storage (a private window?): ${describeCause(cause)}`;
|
|
72
|
+
throw new StorageUnavailableError(`StorageUnavailableError: ${why}`, { cause });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
setPremiumFlag();
|
|
60
76
|
var { name, rxdbVersion } = getRxStorageSQLite({
|
|
61
77
|
sqliteBasics: getSQLiteBasicsOpfsSahPool({ openDb: () => Promise.reject(new Error("unused: throwaway instance")) })
|
|
62
78
|
});
|
|
63
|
-
var ready = sqlite3InitModule().then((sqlite3) => sqlite3.installOpfsSAHPoolVfs({ name: "tallyui", initialCapacity: 64 })).then(
|
|
79
|
+
var ready = Promise.all([sqlite3InitModule(), requireOpfs()]).then(([sqlite3]) => sqlite3.installOpfsSAHPoolVfs({ name: "tallyui", initialCapacity: 64 })).then(
|
|
64
80
|
(pool) => getRxStorageSQLite({
|
|
65
81
|
sqliteBasics: getSQLiteBasicsOpfsSahPool({
|
|
66
82
|
// The OO1 `exec()` here is the real, overloaded sqlite-wasm signature;
|
|
@@ -79,10 +95,10 @@ var storage = {
|
|
|
79
95
|
try {
|
|
80
96
|
realStorage = await ready;
|
|
81
97
|
} catch (cause) {
|
|
82
|
-
throw
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
);
|
|
98
|
+
if (cause instanceof StorageUnavailableError) throw cause;
|
|
99
|
+
const held = HELD_ERROR_NAMES.includes(cause?.name ?? "");
|
|
100
|
+
const what = held ? `${STORAGE_HELD_MARKER} (opfs-sahpool)` : "SQLite worker start failed";
|
|
101
|
+
throw new StorageWorkerStartError(`StorageWorkerStartError: ${what}: ${describeCause(cause)}`, { cause });
|
|
86
102
|
}
|
|
87
103
|
return realStorage.createStorageInstance(params);
|
|
88
104
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tallyui/storage-sqlite",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-next.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "RxDB Premium SQLite storage for a synchronous SQLite handle (expo-sqlite)",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -40,18 +40,21 @@
|
|
|
40
40
|
"directory": "packages/storage-sqlite"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"rxdb": "
|
|
43
|
+
"rxdb": "17.5.0",
|
|
44
44
|
"rxjs": "7.8.2"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@sqlite.org/sqlite-wasm": "3.53.4-build1",
|
|
48
|
+
"@tallyui/core": "^3.0.0-next.0",
|
|
48
49
|
"esbuild": "0.27.3",
|
|
49
|
-
"
|
|
50
|
+
"expo-sqlite": "16.0.10",
|
|
51
|
+
"rxdb-premium": "17.5.0"
|
|
50
52
|
},
|
|
51
53
|
"peerDependencies": {
|
|
52
54
|
"@sqlite.org/sqlite-wasm": "^3.53.4-build1",
|
|
55
|
+
"@tallyui/core": "^3.0.0-next.0",
|
|
53
56
|
"expo-sqlite": ">=15.0.0",
|
|
54
|
-
"rxdb-premium": "
|
|
57
|
+
"rxdb-premium": "17.5.0"
|
|
55
58
|
},
|
|
56
59
|
"peerDependenciesMeta": {
|
|
57
60
|
"@sqlite.org/sqlite-wasm": {
|
|
@@ -63,7 +66,7 @@
|
|
|
63
66
|
},
|
|
64
67
|
"scripts": {
|
|
65
68
|
"build": "tsup",
|
|
66
|
-
"typecheck": "tsc --noEmit",
|
|
69
|
+
"typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.expo-handle.json",
|
|
67
70
|
"test": "vitest run"
|
|
68
71
|
}
|
|
69
72
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, it } from 'vitest';
|
|
2
|
+
import type { openNodeSQLite } from './node-sqlite.test-helper';
|
|
3
|
+
import { getRxStorageSQLite } from './rx-storage-sqlite';
|
|
4
|
+
|
|
5
|
+
declare const nodeDatabase: ReturnType<typeof openNodeSQLite>['database'];
|
|
6
|
+
const missingRunSync = { execSync: () => {}, getAllSync: () => [] };
|
|
7
|
+
|
|
8
|
+
describe('SQLite handle types', () => {
|
|
9
|
+
it('accepts the wrapped node:sqlite handle', () => {
|
|
10
|
+
getRxStorageSQLite(nodeDatabase);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('requires runSync', () => {
|
|
14
|
+
// @ts-expect-error A handle without runSync cannot execute writes.
|
|
15
|
+
getRxStorageSQLite(missingRunSync);
|
|
16
|
+
});
|
|
17
|
+
});
|
package/src/rx-storage-sqlite.ts
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
import type { RxStorage } from 'rxdb';
|
|
2
2
|
import type { SQLiteBasics } from 'rxdb/plugins/storage-sqlite';
|
|
3
|
+
import { setPremiumFlag } from 'rxdb-premium/plugins/shared';
|
|
3
4
|
import { getRxStorageSQLite as getPremiumRxStorageSQLite } from 'rxdb-premium/plugins/storage-sqlite';
|
|
4
5
|
import { sqliteBoolParams } from './params';
|
|
5
6
|
import type { SQLiteDatabase } from './types';
|
|
6
7
|
|
|
8
|
+
// Also at import, before anything else can make RxDB cache the flag's first check as false.
|
|
9
|
+
setPremiumFlag();
|
|
10
|
+
|
|
7
11
|
/**
|
|
8
12
|
* Wraps a synchronous SQLite handle with RxDB Premium SQLite storage.
|
|
9
13
|
* Apps install the RxDB Premium peer dependency under their own RxDB Premium licence.
|
|
10
14
|
* One SQLite handle serves exactly one RxDB database.
|
|
11
15
|
*/
|
|
12
16
|
export function getRxStorageSQLite(database: SQLiteDatabase): RxStorage<any, any> {
|
|
17
|
+
// RxDB 17 caps a process at 13 open collections unless the premium flag is
|
|
18
|
+
// set; premium storages do not set it themselves. RxDB caches the first
|
|
19
|
+
// flag check, so this must run before any collection is created.
|
|
20
|
+
setPremiumFlag();
|
|
13
21
|
let databaseName: string | undefined;
|
|
14
22
|
const sqliteBasics: SQLiteBasics<SQLiteDatabase> = {
|
|
15
23
|
debugId: 'tallyui-sqlite-sync',
|
package/src/types.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import type { SQLResultRow } from 'rxdb/plugins/storage-sqlite';
|
|
2
|
+
|
|
1
3
|
export interface SQLiteDatabase {
|
|
2
4
|
execSync(source: string): void;
|
|
3
|
-
getAllSync
|
|
4
|
-
runSync(source: string, params
|
|
5
|
+
getAllSync(source: string, params: (string | number)[]): SQLResultRow[];
|
|
6
|
+
runSync(source: string, params: (string | number)[]): unknown;
|
|
5
7
|
}
|
|
6
8
|
|
|
7
9
|
export interface SQLiteStorageSettings {
|
package/src/web/errors.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Thrown by the web SQLite worker's storage (ADR-061) when start-up failed
|
|
3
|
-
* another tab
|
|
4
|
-
*
|
|
2
|
+
* Thrown by the web SQLite worker's storage (ADR-061) when start-up failed
|
|
3
|
+
* with OPFS reachable: another tab holds the opfs-sahpool database, or an
|
|
4
|
+
* unexpected failure. Keeps the original failure as `cause`, and its name
|
|
5
|
+
* and message in this error's message.
|
|
5
6
|
*
|
|
6
7
|
* RxDB's remote-storage channel re-throws a rejected `createStorageInstance`
|
|
7
8
|
* on the main thread as a plain `Error` whose message embeds the original
|
|
@@ -17,9 +18,34 @@ export class StorageWorkerStartError extends Error {
|
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Thrown by the worker's storage when the browser gives it no usable OPFS, as in a Safari private window (#293):
|
|
23
|
+
* `getDirectory()` fails or sync access handles are missing. Closing tabs or reloading can't help, so its message,
|
|
24
|
+
* which carries its name like `StorageWorkerStartError`'s, never contains `StorageWorkerStartError`.
|
|
25
|
+
*/
|
|
26
|
+
export class StorageUnavailableError extends Error {
|
|
27
|
+
constructor(message: string, options?: { cause?: unknown }) {
|
|
28
|
+
super(message, options);
|
|
29
|
+
this.name = 'StorageUnavailableError';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The fixed text of the held `StorageWorkerStartError`'s message: the worker writes it, `isStorageHeldError` reads it. */
|
|
34
|
+
export const STORAGE_HELD_MARKER = 'another tab holds the database';
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* True for the held `StorageWorkerStartError` (another tab holds the database), its `{ name, message }` copy or its
|
|
38
|
+
* message; false for any other start failure, a stale worker (RM1) or storage unavailable.
|
|
39
|
+
*/
|
|
40
|
+
export function isStorageHeldError(error: unknown): boolean {
|
|
41
|
+
const text = typeof error === 'string' ? error : (error as { message?: unknown } | null | undefined)?.message;
|
|
42
|
+
return typeof text === 'string' && text.includes('StorageWorkerStartError') && text.includes(STORAGE_HELD_MARKER);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** True for a `StorageUnavailableError`, its `{ name, message }` copy or its message (see above). */
|
|
46
|
+
export function isStorageUnavailableError(error: unknown): boolean {
|
|
47
|
+
if (typeof error === 'string') return error.includes('StorageUnavailableError');
|
|
22
48
|
if (!error || typeof error !== 'object') return false;
|
|
23
49
|
const { name, message } = error as { name?: unknown; message?: unknown };
|
|
24
|
-
return name === '
|
|
50
|
+
return name === 'StorageUnavailableError' || (typeof message === 'string' && message.includes('StorageUnavailableError'));
|
|
25
51
|
}
|
package/src/web/index.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import type { RxStorage } from 'rxdb';
|
|
2
2
|
import { MESSAGE_CHANNEL_CACHE_BY_IDENTIFIER, OPEN_REMOTE_MESSAGE_CHANNELS } from 'rxdb/plugins/storage-remote';
|
|
3
|
+
import { setPremiumFlag } from 'rxdb-premium/plugins/shared';
|
|
3
4
|
import { getRxStorageWorker } from 'rxdb-premium/plugins/storage-worker';
|
|
4
5
|
|
|
5
|
-
export { StorageWorkerStartError,
|
|
6
|
+
export { StorageWorkerStartError, StorageUnavailableError, isStorageUnavailableError, isStorageHeldError } from './errors';
|
|
7
|
+
export { isStorageWorkerStartError } from './is-storage-worker-start-error';
|
|
8
|
+
|
|
9
|
+
// Also at import, before anything else can make RxDB cache the flag's first check as false.
|
|
10
|
+
setPremiumFlag();
|
|
6
11
|
|
|
7
12
|
/** Marks a storage as this package's web engine (ADR-061), for job 2b to recognise. */
|
|
8
13
|
export const SQLITE_SAHPOOL_ENGINE = 'sqlite-sahpool';
|
|
@@ -26,6 +31,10 @@ export function getRxStorageSQLiteWasm(options: {
|
|
|
26
31
|
workerInput: string | URL | (() => Worker);
|
|
27
32
|
workerName?: string;
|
|
28
33
|
}): RxStorageSQLiteWasm {
|
|
34
|
+
// RxDB 17 caps a process at 13 open collections unless the premium flag is
|
|
35
|
+
// set; premium storages do not set it themselves. RxDB caches the first
|
|
36
|
+
// flag check, so this must run before any collection is created.
|
|
37
|
+
setPremiumFlag();
|
|
29
38
|
const { workerInput } = options;
|
|
30
39
|
const workerOptions = { type: 'module' as const, name: options.workerName ?? 'tallyui-storage' };
|
|
31
40
|
// Premium creates the worker inside its own closure, out of reach, so this
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Kept apart from `StorageWorkerStartError` (./errors), which the worker imports: @tallyui/core's
|
|
2
|
+
// main entry carries React context, and it must stay out of the worker bundle.
|
|
3
|
+
import { isRxdbRemoteVersionMismatch } from '@tallyui/core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* True for a `StorageWorkerStartError`, its `{ name, message }` copy or its
|
|
7
|
+
* message (see ./errors), and for RxDB's RM1: a stale worker is a failed
|
|
8
|
+
* start too, and reloading loads the matching worker. False for a
|
|
9
|
+
* `StorageUnavailableError`, whose message never names this error.
|
|
10
|
+
* It means "any failed start": to choose wording, an app maps the three
|
|
11
|
+
* specific predicates instead: `isStorageUnavailableError`,
|
|
12
|
+
* `isStorageHeldError`, and `isRxdbRemoteVersionMismatch` (@tallyui/core).
|
|
13
|
+
*/
|
|
14
|
+
export function isStorageWorkerStartError(error: unknown): boolean {
|
|
15
|
+
if (isRxdbRemoteVersionMismatch(error)) return true;
|
|
16
|
+
if (typeof error === 'string') return error.includes('StorageWorkerStartError');
|
|
17
|
+
if (!error || typeof error !== 'object') return false;
|
|
18
|
+
const { name, message } = error as { name?: unknown; message?: unknown };
|
|
19
|
+
return name === 'StorageWorkerStartError' || (typeof message === 'string' && message.includes('StorageWorkerStartError'));
|
|
20
|
+
}
|
package/src/web/worker.ts
CHANGED
|
@@ -1,9 +1,33 @@
|
|
|
1
1
|
import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
|
|
2
2
|
import type { RxStorage } from 'rxdb';
|
|
3
|
+
import { setPremiumFlag } from 'rxdb-premium/plugins/shared';
|
|
3
4
|
import { getRxStorageSQLite } from 'rxdb-premium/plugins/storage-sqlite';
|
|
4
5
|
import { exposeWorkerRxStorage } from 'rxdb-premium/plugins/storage-worker';
|
|
5
6
|
import { getSQLiteBasicsOpfsSahPool, type Oo1Db } from './sqlite-basics-sahpool';
|
|
6
|
-
import { StorageWorkerStartError } from './errors';
|
|
7
|
+
import { STORAGE_HELD_MARKER, StorageUnavailableError, StorageWorkerStartError } from './errors';
|
|
8
|
+
|
|
9
|
+
// What a pool install rejects with while another tab holds the pool (#293): WebKit's, then Chromium's.
|
|
10
|
+
const HELD_ERROR_NAMES = ['InvalidStateError', 'NoModificationAllowedError'];
|
|
11
|
+
|
|
12
|
+
// Only the message crosses RxDB's worker channel, so each start error carries its cause's name and message.
|
|
13
|
+
const describeCause = (cause: unknown) => (cause instanceof Error ? `${cause.name}: ${cause.message}` : String(cause));
|
|
14
|
+
|
|
15
|
+
// A private window (WebKit's ephemeral storage, #293) rejects getDirectory(), and a browser without sync access
|
|
16
|
+
// handles can't host the pool: the storage is unavailable, whatever other tabs do.
|
|
17
|
+
async function requireOpfs(): Promise<void> {
|
|
18
|
+
try {
|
|
19
|
+
const handle = globalThis.FileSystemFileHandle?.prototype ?? {};
|
|
20
|
+
if (!('createSyncAccessHandle' in handle)) throw new TypeError('FileSystemFileHandle.createSyncAccessHandle is missing');
|
|
21
|
+
await navigator.storage.getDirectory();
|
|
22
|
+
} catch (cause) {
|
|
23
|
+
const why = `this browser gives the page no OPFS storage (a private window?): ${describeCause(cause)}`;
|
|
24
|
+
throw new StorageUnavailableError(`StorageUnavailableError: ${why}`, { cause });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// The worker is its own JS context with its own RxDB globals, so it sets
|
|
29
|
+
// RxDB 17's premium flag itself (13-collection cap otherwise).
|
|
30
|
+
setPremiumFlag();
|
|
7
31
|
|
|
8
32
|
// This must run inside a dedicated worker (ADR-061): `createSyncAccessHandle`,
|
|
9
33
|
// which opfs-sahpool needs, exists only there, not on the main thread or in
|
|
@@ -24,8 +48,8 @@ const { name, rxdbVersion } = getRxStorageSQLite({
|
|
|
24
48
|
sqliteBasics: getSQLiteBasicsOpfsSahPool({ openDb: () => Promise.reject(new Error('unused: throwaway instance')) }),
|
|
25
49
|
});
|
|
26
50
|
|
|
27
|
-
const ready: Promise<RxStorage<any, any>> = sqlite3InitModule()
|
|
28
|
-
.then((sqlite3) => sqlite3.installOpfsSAHPoolVfs({ name: 'tallyui', initialCapacity: 64 }))
|
|
51
|
+
const ready: Promise<RxStorage<any, any>> = Promise.all([sqlite3InitModule(), requireOpfs()])
|
|
52
|
+
.then(([sqlite3]) => sqlite3.installOpfsSAHPoolVfs({ name: 'tallyui', initialCapacity: 64 }))
|
|
29
53
|
.then((pool) =>
|
|
30
54
|
getRxStorageSQLite({
|
|
31
55
|
sqliteBasics: getSQLiteBasicsOpfsSahPool({
|
|
@@ -35,7 +59,7 @@ const ready: Promise<RxStorage<any, any>> = sqlite3InitModule()
|
|
|
35
59
|
}),
|
|
36
60
|
})
|
|
37
61
|
);
|
|
38
|
-
// A pool install failure before any createStorageInstance call would otherwise be an unhandled rejection; createStorageInstance below still surfaces it as
|
|
62
|
+
// A pool install failure before any createStorageInstance call would otherwise be an unhandled rejection; createStorageInstance below still surfaces it as a start error.
|
|
39
63
|
ready.catch(() => {});
|
|
40
64
|
|
|
41
65
|
const storage: RxStorage<any, any> = {
|
|
@@ -46,12 +70,12 @@ const storage: RxStorage<any, any> = {
|
|
|
46
70
|
try {
|
|
47
71
|
realStorage = await ready;
|
|
48
72
|
} catch (cause) {
|
|
49
|
-
//
|
|
50
|
-
//
|
|
51
|
-
throw
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
);
|
|
73
|
+
// Start-up failed: fail every call instead of hanging it forever. With OPFS reachable, a refused
|
|
74
|
+
// pool install means another tab holds it; any other failure keeps its cause for diagnosis.
|
|
75
|
+
if (cause instanceof StorageUnavailableError) throw cause;
|
|
76
|
+
const held = HELD_ERROR_NAMES.includes((cause as Error | undefined)?.name ?? '');
|
|
77
|
+
const what = held ? `${STORAGE_HELD_MARKER} (opfs-sahpool)` : 'SQLite worker start failed';
|
|
78
|
+
throw new StorageWorkerStartError(`StorageWorkerStartError: ${what}: ${describeCause(cause)}`, { cause });
|
|
55
79
|
}
|
|
56
80
|
return realStorage.createStorageInstance(params);
|
|
57
81
|
},
|
package/dist/chunk-CNKWKIZV.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
// src/web/errors.ts
|
|
2
|
-
var StorageWorkerStartError = class extends Error {
|
|
3
|
-
constructor(message, options) {
|
|
4
|
-
super(message, options);
|
|
5
|
-
this.name = "StorageWorkerStartError";
|
|
6
|
-
}
|
|
7
|
-
};
|
|
8
|
-
function isStorageWorkerStartError(error) {
|
|
9
|
-
if (typeof error === "string") return error.includes("StorageWorkerStartError");
|
|
10
|
-
if (!error || typeof error !== "object") return false;
|
|
11
|
-
const { name, message } = error;
|
|
12
|
-
return name === "StorageWorkerStartError" || typeof message === "string" && message.includes("StorageWorkerStartError");
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export {
|
|
16
|
-
StorageWorkerStartError,
|
|
17
|
-
isStorageWorkerStartError
|
|
18
|
-
};
|