@vireocodedev/sqlite 0.2.0 → 0.2.1
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/dist/concurrency/createSqliteOperationCoordinator.d.ts +24 -0
- package/dist/core/sqliteCrud.d.ts +4 -0
- package/dist/core/sqliteEntityFactory.d.ts +134 -0
- package/dist/core/sqliteRequestHandlers.d.ts +7 -0
- package/dist/core/sqliteRuntime.d.ts +7 -0
- package/dist/core/sqliteTypes.d.ts +12 -0
- package/dist/core/sqliteWorkerProtocol.d.ts +18 -0
- package/dist/core/sqliteWorkerRuntime.d.ts +19 -0
- package/dist/devtools/createSqlConsoleClient.d.ts +20 -0
- package/dist/hydration/createHydrationContributorRegistry.d.ts +11 -0
- package/dist/hydration/createHydrationController.d.ts +60 -0
- package/dist/hydration/createHydrationEntityClient.d.ts +16 -0
- package/dist/hydration/createHydrationGate.d.ts +22 -0
- package/dist/hydration/createHydrationRequestQueue.d.ts +22 -0
- package/dist/hydration/createHydrationStatus.d.ts +26 -0
- package/dist/hydration/createLocalReflectionQueue.d.ts +11 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +1508 -0
- package/dist/index.js.map +1 -0
- package/dist/lifecycle/createDatabaseOwnerStore.d.ts +11 -0
- package/dist/lifecycle/createOfflineDataLifecycle.d.ts +26 -0
- package/dist/lifecycle/createOpfsDatabaseFiles.d.ts +15 -0
- package/dist/offline/hydration/hydratePagedSnapshot.d.ts +14 -0
- package/dist/offline/index.d.ts +7 -0
- package/dist/offline/index.js +99 -0
- package/dist/offline/index.js.map +1 -0
- package/dist/offline/mutations/offlineMutations.d.ts +20 -0
- package/dist/offline/queue/queueingHeaders.d.ts +4 -0
- package/dist/offline/queue/queueingPolicy.d.ts +2 -0
- package/dist/offline/queue/queueingTypes.d.ts +18 -0
- package/dist/offline/status/offlineStatusRuntime.d.ts +2 -0
- package/dist/offline/status/offlineStatusTypes.d.ts +23 -0
- package/dist/offline-queue/createOfflineQueueCapture.d.ts +16 -0
- package/dist/offline-queue/createOfflineQueueClient.d.ts +23 -0
- package/dist/offline-queue/createOfflineQueueStatus.d.ts +18 -0
- package/dist/offline-queue/offlineQueueStateSqlite.d.ts +30 -0
- package/dist/offline-queue/replayOfflineSyncBatch.d.ts +29 -0
- package/dist/queueingHeaders-B7TBifLV.js +20 -0
- package/dist/queueingHeaders-B7TBifLV.js.map +1 -0
- package/dist/runtime/contracts.d.ts +54 -0
- package/dist/runtime/createManagedSqliteRuntime.d.ts +23 -0
- package/dist/runtime/createSqliteEntityClient.d.ts +32 -0
- package/dist/runtime/createSqliteTransport.d.ts +17 -0
- package/dist/runtime/hydrationEntityStateSqlite.d.ts +20 -0
- package/dist/runtime/sqlConsoleSqlite.d.ts +20 -0
- package/dist/sqliteWorkerConfig.d.ts +3 -0
- package/package.json +1 -1
package/dist/index.js
ADDED
|
@@ -0,0 +1,1508 @@
|
|
|
1
|
+
import { i as e, r as t } from "./queueingHeaders-B7TBifLV.js";
|
|
2
|
+
import n from "@sqlite.org/sqlite-wasm";
|
|
3
|
+
//#region src/core/sqliteCrud.ts
|
|
4
|
+
function r(e, t) {
|
|
5
|
+
e.exec("BEGIN;");
|
|
6
|
+
try {
|
|
7
|
+
t(), e.exec("COMMIT;");
|
|
8
|
+
} catch (t) {
|
|
9
|
+
throw e.exec("ROLLBACK;"), t;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function i(e, t, n) {
|
|
13
|
+
let r = e.prepare(t), i = [];
|
|
14
|
+
try {
|
|
15
|
+
for (; r.step();) i.push(n(r.get([])));
|
|
16
|
+
} finally {
|
|
17
|
+
r.finalize();
|
|
18
|
+
}
|
|
19
|
+
return i;
|
|
20
|
+
}
|
|
21
|
+
function a(e, t, n, i) {
|
|
22
|
+
i.length !== 0 && r(e, () => {
|
|
23
|
+
for (let r of i) {
|
|
24
|
+
let i = e.prepare(`DELETE FROM ${t} WHERE ${n} = ?;`);
|
|
25
|
+
try {
|
|
26
|
+
i.bind([r]), i.step();
|
|
27
|
+
} finally {
|
|
28
|
+
i.finalize();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/core/sqliteRequestHandlers.ts
|
|
35
|
+
function o(e) {
|
|
36
|
+
return e;
|
|
37
|
+
}
|
|
38
|
+
function s(...e) {
|
|
39
|
+
let t = {};
|
|
40
|
+
for (let n of e) for (let [e, r] of Object.entries(n)) {
|
|
41
|
+
if (e in t) throw Error(`SQLite request handler operation "${e}" is registered more than once.`);
|
|
42
|
+
t[e] = r;
|
|
43
|
+
}
|
|
44
|
+
return t;
|
|
45
|
+
}
|
|
46
|
+
async function c(e, t, n) {
|
|
47
|
+
let r = n[t.type];
|
|
48
|
+
if (r) return await r(e, t);
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/core/sqliteEntityFactory.ts
|
|
52
|
+
function l(e, t) {
|
|
53
|
+
return t.map((t) => e[t]).filter((e) => e != null).map((e) => String(e).trim()).filter((e) => e.length > 0).join(" ").trim();
|
|
54
|
+
}
|
|
55
|
+
function u(e, t) {
|
|
56
|
+
if (e.trim().length === 0) throw Error(`${t} must be a non-empty string.`);
|
|
57
|
+
}
|
|
58
|
+
function d(e) {
|
|
59
|
+
let t = Object.entries(e.fields);
|
|
60
|
+
u(e.entityNameSingular, "entityNameSingular"), u(e.entityNamePlural, "entityNamePlural"), u(e.tableName, "tableName"), u(e.requestKeys.replace, "requestKeys.replace"), u(e.requestKeys.upsert, "requestKeys.upsert"), u(e.requestKeys.delete, "requestKeys.delete");
|
|
61
|
+
for (let [e, n] of t) u(n.column, `fields.${e}.column`);
|
|
62
|
+
let n = t.filter(([, e]) => e.id === !0);
|
|
63
|
+
if (n.length !== 1) throw Error(`Sqlite entity spec for ${e.entityNameSingular} must define exactly one field with id: true.`);
|
|
64
|
+
let s = n[0][1].column, c = e.entityNamePlural, d = {
|
|
65
|
+
replace: `replace${c}`,
|
|
66
|
+
upsert: `upsert${e.entityNameSingular}`,
|
|
67
|
+
list: `list${c}`,
|
|
68
|
+
delete: `delete${c}`
|
|
69
|
+
}, f = e.requestKeys, p = e.softDelete?.enabled ?? !0, m = e.softDelete?.columnName ?? "deleted", h = "keywords", g = t.map(([, e]) => e.column), _ = t.map(([e]) => e);
|
|
70
|
+
if (new Set(g).size !== g.length) throw Error(`Sqlite entity spec for ${e.entityNameSingular} maps more than one field to the same column.`);
|
|
71
|
+
let v = [h, ...p ? [m] : []], y = v.find((e) => g.includes(e));
|
|
72
|
+
if (y) throw Error(`Sqlite entity spec for ${e.entityNameSingular} maps field column "${y}", which is managed by the entity runtime.`);
|
|
73
|
+
if (new Set(v).size !== v.length) throw Error(`Sqlite entity spec for ${e.entityNameSingular} configures duplicate generated columns.`);
|
|
74
|
+
let b = [...g, h];
|
|
75
|
+
p && b.push(m);
|
|
76
|
+
let x = e.keywordFields ?? _;
|
|
77
|
+
function S(e, t, n) {
|
|
78
|
+
let r = n[e];
|
|
79
|
+
return t.toDb ? t.toDb(r) : r;
|
|
80
|
+
}
|
|
81
|
+
function C(e) {
|
|
82
|
+
let n = t.map(([t, n]) => S(t, n, e));
|
|
83
|
+
return n.push(l(e, x)), p && n.push(0), n;
|
|
84
|
+
}
|
|
85
|
+
let w = b.map(() => "?").join(", "), T = b.filter((e) => e !== s);
|
|
86
|
+
function E(t, n) {
|
|
87
|
+
r(t, () => {
|
|
88
|
+
t.exec(`DELETE FROM ${e.tableName};`);
|
|
89
|
+
for (let r of n) {
|
|
90
|
+
let n = t.prepare(`
|
|
91
|
+
INSERT INTO ${e.tableName} (${b.join(", ")})
|
|
92
|
+
VALUES (${w});
|
|
93
|
+
`);
|
|
94
|
+
try {
|
|
95
|
+
n.bind(C(r)), n.step();
|
|
96
|
+
} finally {
|
|
97
|
+
n.finalize();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
function D(t, n) {
|
|
103
|
+
let r = t.prepare(`
|
|
104
|
+
INSERT INTO ${e.tableName} (${b.join(", ")})
|
|
105
|
+
VALUES (${w})
|
|
106
|
+
ON CONFLICT(${s}) DO UPDATE SET
|
|
107
|
+
${T.map((e) => `${e} = excluded.${e}`).join(", ")};
|
|
108
|
+
`);
|
|
109
|
+
try {
|
|
110
|
+
r.bind(C(n)), r.step();
|
|
111
|
+
} finally {
|
|
112
|
+
r.finalize();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function O(n) {
|
|
116
|
+
let r = p ? `WHERE ${m} = 0` : "", a = e.orderBy ?? s;
|
|
117
|
+
return i(n, `
|
|
118
|
+
SELECT ${g.join(", ")}
|
|
119
|
+
FROM ${e.tableName}
|
|
120
|
+
${r}
|
|
121
|
+
ORDER BY ${a} ASC;
|
|
122
|
+
`, (e) => {
|
|
123
|
+
let n = {};
|
|
124
|
+
for (let r = 0; r < t.length; r += 1) {
|
|
125
|
+
let [i, a] = t[r], o = e[r];
|
|
126
|
+
n[i] = a.fromDb(o);
|
|
127
|
+
}
|
|
128
|
+
return n;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
function k(t, n) {
|
|
132
|
+
a(t, e.tableName, s, n);
|
|
133
|
+
}
|
|
134
|
+
let A = o({
|
|
135
|
+
[d.replace]: (e, t) => (E(e, t[f.replace]), null),
|
|
136
|
+
[d.upsert]: (e, t) => (D(e, t[f.upsert]), null),
|
|
137
|
+
[d.list]: (e) => O(e),
|
|
138
|
+
[d.delete]: (e, t) => (k(e, t[f.delete]), null)
|
|
139
|
+
});
|
|
140
|
+
return {
|
|
141
|
+
__sqliteEntityBundle: !0,
|
|
142
|
+
tableName: e.tableName,
|
|
143
|
+
entityNameSingular: e.entityNameSingular,
|
|
144
|
+
entityNamePlural: e.entityNamePlural,
|
|
145
|
+
operationNames: d,
|
|
146
|
+
requestKeys: f,
|
|
147
|
+
fields: e.fields,
|
|
148
|
+
hydration: e.hydration,
|
|
149
|
+
replaceRows: E,
|
|
150
|
+
upsertRow: D,
|
|
151
|
+
listRows: O,
|
|
152
|
+
deleteRows: k,
|
|
153
|
+
sse: e.sse,
|
|
154
|
+
requestHandlers: A,
|
|
155
|
+
operationMap: void 0
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
//#endregion
|
|
159
|
+
//#region src/concurrency/createSqliteOperationCoordinator.ts
|
|
160
|
+
function f(e) {
|
|
161
|
+
return typeof DOMException > "u" ? Error(e) : new DOMException(e, "AbortError");
|
|
162
|
+
}
|
|
163
|
+
function p(e) {
|
|
164
|
+
let t = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new WeakSet(), r = Promise.resolve(), i = null, a = null, o = async (e) => {
|
|
165
|
+
let t = r.catch(() => void 0).then(e);
|
|
166
|
+
return r = t.then(() => void 0, () => void 0), await t;
|
|
167
|
+
}, s = async (t, r) => {
|
|
168
|
+
if (r != null && n.has(r)) return await t(r);
|
|
169
|
+
let i = async () => {
|
|
170
|
+
let e = {};
|
|
171
|
+
n.add(e);
|
|
172
|
+
try {
|
|
173
|
+
return await t(e);
|
|
174
|
+
} finally {
|
|
175
|
+
n.delete(e);
|
|
176
|
+
}
|
|
177
|
+
}, a = e.getLockManager?.() ?? null;
|
|
178
|
+
return a ? await a.request(e.lockName, { mode: "exclusive" }, i) : await o(i);
|
|
179
|
+
}, c = async (e, n) => {
|
|
180
|
+
let r = (t.get(e) ?? Promise.resolve()).catch(() => void 0).then(n), i = r.then(() => void 0, () => void 0);
|
|
181
|
+
t.set(e, i);
|
|
182
|
+
try {
|
|
183
|
+
return await r;
|
|
184
|
+
} finally {
|
|
185
|
+
t.get(e) === i && t.delete(e);
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
return {
|
|
189
|
+
runDatabaseExclusive: s,
|
|
190
|
+
runEntityExclusive: async (e, t, n) => await s((n) => c(e, () => t(n)), n),
|
|
191
|
+
runHydrationExclusive: async (e) => {
|
|
192
|
+
if (i) return await i;
|
|
193
|
+
a = new AbortController();
|
|
194
|
+
let t = a;
|
|
195
|
+
return i = s((n) => (t.signal.throwIfAborted(), e(t.signal, n))).finally(() => {
|
|
196
|
+
i = null, a === t && (a = null);
|
|
197
|
+
}), await i;
|
|
198
|
+
},
|
|
199
|
+
abortHydration(t = "SQLite hydration was cancelled.") {
|
|
200
|
+
a?.abort((e.createAbortReason ?? f)(t));
|
|
201
|
+
},
|
|
202
|
+
async awaitHydrationIdle() {
|
|
203
|
+
await i?.catch(() => void 0);
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
//#endregion
|
|
208
|
+
//#region src/devtools/createSqlConsoleClient.ts
|
|
209
|
+
function m() {
|
|
210
|
+
return typeof performance < "u" && typeof performance.now == "function" ? performance.now() : Date.now();
|
|
211
|
+
}
|
|
212
|
+
function h(e) {
|
|
213
|
+
return typeof e != "object" || !e ? JSON.stringify(e) ?? String(e) : Array.isArray(e) ? `[${e.map((e) => h(e)).join(",")}]` : `{${Object.entries(e).sort(([e], [t]) => e.localeCompare(t)).map(([e, t]) => `${JSON.stringify(e)}:${h(t)}`).join(",")}}`;
|
|
214
|
+
}
|
|
215
|
+
function g(e) {
|
|
216
|
+
let t = /* @__PURE__ */ new Map(), n = e.now ?? m, r = e.requestTypes ?? {
|
|
217
|
+
executeScript: "executeSqlScript",
|
|
218
|
+
executePagedQuery: "executePagedQuery"
|
|
219
|
+
}, i = e.createUnavailableError ?? ((e) => /* @__PURE__ */ Error(`SQLite ${e === "script" ? "SQL console" : "paged query execution"} is unavailable.`));
|
|
220
|
+
return {
|
|
221
|
+
async executeScript(t) {
|
|
222
|
+
if (e.runtime.shouldUseInMemoryFallback()) throw i("script");
|
|
223
|
+
return await e.transport.sendWorkerRequest({
|
|
224
|
+
type: r.executeScript,
|
|
225
|
+
script: t
|
|
226
|
+
});
|
|
227
|
+
},
|
|
228
|
+
async executePagedQuery(a) {
|
|
229
|
+
if (e.runtime.shouldUseInMemoryFallback()) throw i("paged-query");
|
|
230
|
+
let o = h(a), s = t.get(o);
|
|
231
|
+
if (s) return e.log?.("executePagedQuery dedupe hit"), await s;
|
|
232
|
+
let c = e.transport.sendWorkerRequest({
|
|
233
|
+
type: r.executePagedQuery,
|
|
234
|
+
...a,
|
|
235
|
+
clientSentAtMs: n()
|
|
236
|
+
});
|
|
237
|
+
t.set(o, c);
|
|
238
|
+
try {
|
|
239
|
+
return await c;
|
|
240
|
+
} finally {
|
|
241
|
+
t.delete(o);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
//#endregion
|
|
247
|
+
//#region src/hydration/createHydrationContributorRegistry.ts
|
|
248
|
+
function _() {
|
|
249
|
+
let e = /* @__PURE__ */ new Map();
|
|
250
|
+
return {
|
|
251
|
+
register(t) {
|
|
252
|
+
for (let n of t) e.set(n.key, n);
|
|
253
|
+
},
|
|
254
|
+
list: () => [...e.values()].sort((e, t) => e.key.localeCompare(t.key))
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
//#endregion
|
|
258
|
+
//#region src/hydration/createHydrationController.ts
|
|
259
|
+
function v() {
|
|
260
|
+
return typeof performance > "u" ? Date.now() : performance.now();
|
|
261
|
+
}
|
|
262
|
+
function y(e, t) {
|
|
263
|
+
return typeof DOMException > "u" ? Error(e) : new DOMException(e, t);
|
|
264
|
+
}
|
|
265
|
+
function b(e) {
|
|
266
|
+
let t = e.elapsedNow ?? v, n = e.timestampNow ?? Date.now, r = e.schedule ?? setTimeout, i = e.cancelScheduled ?? clearTimeout, a = e.isCancellationError ?? ((e) => typeof DOMException < "u" && e instanceof DOMException && e.name === "AbortError"), o = null, s = 0, c = null, l = !1, u = () => {
|
|
267
|
+
c != null && i(c), c = null;
|
|
268
|
+
}, d = (t) => {
|
|
269
|
+
if (c != null || l) return;
|
|
270
|
+
let n = ++s, i = Math.min(e.retryBaseMs * 2 ** Math.min(n - 1, e.retryExponentLimit ?? 6), e.retryMaxMs);
|
|
271
|
+
e.debug?.(t, {
|
|
272
|
+
retryNumber: n,
|
|
273
|
+
retryDelayMs: i
|
|
274
|
+
}), c = r(() => {
|
|
275
|
+
c = null, l || e.events.resetGate();
|
|
276
|
+
}, i);
|
|
277
|
+
}, f = async (e, t, n, a) => {
|
|
278
|
+
let o = new AbortController(), s = !1, c = () => o.abort(t.reason);
|
|
279
|
+
t.addEventListener("abort", c, { once: !0 });
|
|
280
|
+
let l = r(() => {
|
|
281
|
+
s = !0, o.abort(y(a, "TimeoutError"));
|
|
282
|
+
}, n);
|
|
283
|
+
try {
|
|
284
|
+
let t = await e(o.signal);
|
|
285
|
+
return o.signal.throwIfAborted(), t;
|
|
286
|
+
} catch (e) {
|
|
287
|
+
throw s ? Error(a) : e;
|
|
288
|
+
} finally {
|
|
289
|
+
i(l), t.removeEventListener("abort", c);
|
|
290
|
+
}
|
|
291
|
+
}, p = async (r) => {
|
|
292
|
+
let i = e.getContributors();
|
|
293
|
+
e.events.registerEntities(i.map((e) => e.key));
|
|
294
|
+
let o = null;
|
|
295
|
+
try {
|
|
296
|
+
await e.runHydrationExclusive(async (a, c) => {
|
|
297
|
+
let l = e.requests.consume();
|
|
298
|
+
o = l;
|
|
299
|
+
let p = t();
|
|
300
|
+
e.events.markRunning(!0), e.events.startGate(), e.events.setErrorMessage(null), e.debug?.("Hydration started", {
|
|
301
|
+
entityCount: i.length,
|
|
302
|
+
heartbeatEnabled: r.heartbeatEnabled ?? !1,
|
|
303
|
+
heartbeatConnected: r.heartbeatConnected ?? !1
|
|
304
|
+
});
|
|
305
|
+
try {
|
|
306
|
+
let r = i.map((e) => e.key).sort((e, t) => e.localeCompare(t)), o = t(), p = await e.entityStateClient.list();
|
|
307
|
+
e.debug?.("Loaded local hydration metadata", {
|
|
308
|
+
elapsedMs: Math.round(t() - o),
|
|
309
|
+
entityCount: p.length
|
|
310
|
+
});
|
|
311
|
+
let m = new Map(p.map((e) => [e.entityKey, e])), h = /* @__PURE__ */ new Map(), g = /* @__PURE__ */ new Set(), _ = !0, v = !0, y = !0, b = t();
|
|
312
|
+
try {
|
|
313
|
+
let n = await f((t) => e.fetchRemoteVersions(r, t), a, e.remoteVersionsTimeoutMs, "Fetching hydration versions timed out.");
|
|
314
|
+
for (let e of n.versions) h.set(e.entity.toLowerCase(), Number(e.revision ?? 0));
|
|
315
|
+
e.debug?.("Fetched remote entity revisions", {
|
|
316
|
+
elapsedMs: Math.round(t() - b),
|
|
317
|
+
entityCount: n.versions.length
|
|
318
|
+
});
|
|
319
|
+
} catch (n) {
|
|
320
|
+
if (a.aborted) throw n;
|
|
321
|
+
y = !1, v = !1, e.warn?.("Could not fetch remote entity revisions; every entity will be hydrated", {
|
|
322
|
+
elapsedMs: Math.round(t() - b),
|
|
323
|
+
error: n
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
for (let r of i) {
|
|
327
|
+
a.throwIfAborted();
|
|
328
|
+
let i = t();
|
|
329
|
+
e.events.markEntityRunning(r.key);
|
|
330
|
+
let o = m.get(r.key), s = h.get(r.key.toLowerCase()) ?? 0, u = y && h.has(r.key.toLowerCase()), d = u && o?.isStale === !0 && o.lastError == null && o.appliedRevision === s;
|
|
331
|
+
if (!(l.forcedKeys.has(r.key) || !u || o == null || o.isStale && !d || o.appliedRevision !== s)) {
|
|
332
|
+
d && await e.entityStateClient.upsert({
|
|
333
|
+
...o,
|
|
334
|
+
isStale: !1,
|
|
335
|
+
lastError: null
|
|
336
|
+
}), e.events.markEntitySuccess(r.key, o.lastRowCount ?? 0), e.debug?.("Entity is current; skipped hydration", {
|
|
337
|
+
entity: r.key,
|
|
338
|
+
elapsedMs: Math.round(t() - i),
|
|
339
|
+
revision: s,
|
|
340
|
+
rowCount: o.lastRowCount ?? 0
|
|
341
|
+
});
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
e.debug?.("Hydrating entity", {
|
|
345
|
+
entity: r.key,
|
|
346
|
+
reason: l.forcedKeys.has(r.key) ? "manual-request" : u ? o == null ? "no-local-state" : o.isStale ? "local-state-stale" : "revision-mismatch" : "remote-revision-unavailable",
|
|
347
|
+
localRevision: o?.appliedRevision ?? null,
|
|
348
|
+
remoteRevision: u ? s : null
|
|
349
|
+
}), await e.runEntityExclusive(r.key, async () => {
|
|
350
|
+
try {
|
|
351
|
+
let c = e.entityTimeoutMs(r.key), l = await f((e) => r.hydrate(e), a, c, `Hydration timed out after ${c}ms for entity "${r.key}".`);
|
|
352
|
+
a.throwIfAborted();
|
|
353
|
+
let d = (await e.entityStateClient.list()).find((e) => e.entityKey === r.key);
|
|
354
|
+
a.throwIfAborted();
|
|
355
|
+
let p = u ? Math.max(s, d?.appliedRevision ?? 0) : d?.appliedRevision ?? o?.appliedRevision ?? 0;
|
|
356
|
+
await e.entityStateClient.upsert({
|
|
357
|
+
entityKey: r.key,
|
|
358
|
+
appliedRevision: p,
|
|
359
|
+
isStale: !u,
|
|
360
|
+
lastHydratedAt: n(),
|
|
361
|
+
lastRowCount: l.rowCount,
|
|
362
|
+
lastError: null
|
|
363
|
+
}), e.events.markEntitySuccess(r.key, l.rowCount), e.debug?.("Entity hydration completed", {
|
|
364
|
+
entity: r.key,
|
|
365
|
+
elapsedMs: Math.round(t() - i),
|
|
366
|
+
rowCount: l.rowCount
|
|
367
|
+
});
|
|
368
|
+
} catch (n) {
|
|
369
|
+
if (a.aborted) throw n;
|
|
370
|
+
_ = !1, g.add(r.key);
|
|
371
|
+
let s = n instanceof Error ? n.message : "Unknown hydration error.";
|
|
372
|
+
e.events.setErrorMessage(s), await e.entityStateClient.upsert({
|
|
373
|
+
entityKey: r.key,
|
|
374
|
+
appliedRevision: o?.appliedRevision ?? 0,
|
|
375
|
+
isStale: !0,
|
|
376
|
+
lastHydratedAt: o?.lastHydratedAt ?? null,
|
|
377
|
+
lastRowCount: o?.lastRowCount ?? null,
|
|
378
|
+
lastError: s
|
|
379
|
+
}), e.events.markEntityError(r.key, s), e.error?.("Entity hydration failed", {
|
|
380
|
+
entity: r.key,
|
|
381
|
+
elapsedMs: Math.round(t() - i),
|
|
382
|
+
error: n
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
}, c);
|
|
386
|
+
}
|
|
387
|
+
_ && v ? (s = 0, u(), e.events.markDataReady()) : d("Hydration retry scheduled"), e.requests.settle(l, g);
|
|
388
|
+
} finally {
|
|
389
|
+
e.debug?.("Hydration finished", { elapsedMs: Math.round(t() - p) }), e.events.markRunning(!1), e.requests.hasPending() ? e.events.resetGate() : e.events.finishGate();
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
} catch (t) {
|
|
393
|
+
if (o && e.requests.reject(o, t), a(t)) {
|
|
394
|
+
e.debug?.("Hydration cancelled", { reason: t instanceof Error ? t.message : "Request cancelled" });
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
let n = t instanceof Error ? t.message : "Unknown SQLite storage error.";
|
|
398
|
+
e.events.setErrorMessage(n);
|
|
399
|
+
for (let t of i) e.events.markEntityError(t.key, n);
|
|
400
|
+
e.error?.("Hydration failed unexpectedly", { error: t }), d("Hydration retry scheduled after an unexpected SQLite failure");
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
return {
|
|
404
|
+
async run(e = {}) {
|
|
405
|
+
if (l) throw Error("Hydration controller has been disposed.");
|
|
406
|
+
return o ??= p(e).finally(() => {
|
|
407
|
+
o = null;
|
|
408
|
+
}), await o;
|
|
409
|
+
},
|
|
410
|
+
requestRetry() {
|
|
411
|
+
u(), e.events.setErrorMessage(null), e.events.resetGate();
|
|
412
|
+
},
|
|
413
|
+
reset(t) {
|
|
414
|
+
e.requests.rejectPending(t), u(), s = 0, e.events.setErrorMessage(null), e.events.resetGate();
|
|
415
|
+
},
|
|
416
|
+
cancelScheduledRetry: u,
|
|
417
|
+
dispose() {
|
|
418
|
+
l = !0, u();
|
|
419
|
+
},
|
|
420
|
+
isRunning: () => o != null
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
//#endregion
|
|
424
|
+
//#region src/hydration/createHydrationEntityClient.ts
|
|
425
|
+
function x(e) {
|
|
426
|
+
let t = /* @__PURE__ */ new Map();
|
|
427
|
+
return {
|
|
428
|
+
async list() {
|
|
429
|
+
return e.runtime.shouldUseInMemoryFallback() ? [...t.values()].sort((e, t) => e.entityKey.localeCompare(t.entityKey)) : await e.transport.sendWorkerRequest({ type: "listHydrationEntityStates" });
|
|
430
|
+
},
|
|
431
|
+
async upsert(n) {
|
|
432
|
+
if (e.runtime.shouldUseInMemoryFallback()) {
|
|
433
|
+
t.set(n.entityKey, n);
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
await e.transport.sendWorkerRequest({
|
|
437
|
+
type: "upsertHydrationEntityState",
|
|
438
|
+
state: n
|
|
439
|
+
});
|
|
440
|
+
},
|
|
441
|
+
dispose: e.runtime.registerInMemoryStore(t)
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
//#endregion
|
|
445
|
+
//#region src/hydration/createHydrationGate.ts
|
|
446
|
+
var S = {
|
|
447
|
+
phase: "idle",
|
|
448
|
+
startedAt: null,
|
|
449
|
+
dataReady: !1,
|
|
450
|
+
errorMessage: null,
|
|
451
|
+
retryRequestTick: 0
|
|
452
|
+
};
|
|
453
|
+
function C(e = {}) {
|
|
454
|
+
let t = e.now ?? Date.now, n = /* @__PURE__ */ new Set(), r = S, i = (e) => {
|
|
455
|
+
if (e.phase !== r.phase || e.startedAt !== r.startedAt || e.dataReady !== r.dataReady || e.errorMessage !== r.errorMessage || e.retryRequestTick !== r.retryRequestTick) {
|
|
456
|
+
r = e;
|
|
457
|
+
for (let e of n) e(r);
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
return {
|
|
461
|
+
getSnapshot: () => r,
|
|
462
|
+
subscribe(e) {
|
|
463
|
+
return n.add(e), () => n.delete(e);
|
|
464
|
+
},
|
|
465
|
+
setErrorMessage(e) {
|
|
466
|
+
i({
|
|
467
|
+
...r,
|
|
468
|
+
errorMessage: e
|
|
469
|
+
});
|
|
470
|
+
},
|
|
471
|
+
requestRetry() {
|
|
472
|
+
i({
|
|
473
|
+
...r,
|
|
474
|
+
retryRequestTick: r.retryRequestTick + 1
|
|
475
|
+
});
|
|
476
|
+
},
|
|
477
|
+
reset() {
|
|
478
|
+
i({
|
|
479
|
+
...r,
|
|
480
|
+
phase: "idle",
|
|
481
|
+
startedAt: null,
|
|
482
|
+
dataReady: !1
|
|
483
|
+
});
|
|
484
|
+
},
|
|
485
|
+
start() {
|
|
486
|
+
i({
|
|
487
|
+
...r,
|
|
488
|
+
phase: "running",
|
|
489
|
+
startedAt: r.startedAt ?? t()
|
|
490
|
+
});
|
|
491
|
+
},
|
|
492
|
+
finish() {
|
|
493
|
+
i({
|
|
494
|
+
...r,
|
|
495
|
+
phase: "finished"
|
|
496
|
+
});
|
|
497
|
+
},
|
|
498
|
+
markDataReady() {
|
|
499
|
+
i({
|
|
500
|
+
...r,
|
|
501
|
+
dataReady: !0
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
//#endregion
|
|
507
|
+
//#region src/hydration/createHydrationRequestQueue.ts
|
|
508
|
+
function w(e = {}) {
|
|
509
|
+
let t = [];
|
|
510
|
+
return {
|
|
511
|
+
request(n) {
|
|
512
|
+
let r = new Set(n.map((e) => e.trim()).filter(Boolean));
|
|
513
|
+
if (r.size === 0) return Promise.resolve();
|
|
514
|
+
let i = new Promise((e, n) => {
|
|
515
|
+
t.push({
|
|
516
|
+
keys: r,
|
|
517
|
+
resolve: e,
|
|
518
|
+
reject: n
|
|
519
|
+
});
|
|
520
|
+
});
|
|
521
|
+
return e.onRequestQueued?.(), i;
|
|
522
|
+
},
|
|
523
|
+
consume() {
|
|
524
|
+
let e = t.splice(0);
|
|
525
|
+
return {
|
|
526
|
+
requests: e,
|
|
527
|
+
forcedKeys: new Set(e.flatMap((e) => [...e.keys]))
|
|
528
|
+
};
|
|
529
|
+
},
|
|
530
|
+
hasPending: () => t.length > 0,
|
|
531
|
+
rejectPending(e) {
|
|
532
|
+
let n = Error(e);
|
|
533
|
+
for (let e of t.splice(0)) e.reject(n);
|
|
534
|
+
},
|
|
535
|
+
settle(e, t) {
|
|
536
|
+
for (let n of e.requests) {
|
|
537
|
+
let e = [...n.keys].filter((e) => t.has(e));
|
|
538
|
+
e.length === 0 ? n.resolve() : n.reject(/* @__PURE__ */ Error(`Hydration failed for: ${e.join(", ")}.`));
|
|
539
|
+
}
|
|
540
|
+
},
|
|
541
|
+
reject(t, n) {
|
|
542
|
+
let r = n instanceof Error ? n : e.createUnexpectedError?.() ?? /* @__PURE__ */ Error("Hydration failed unexpectedly.");
|
|
543
|
+
for (let e of t.requests) e.reject(r);
|
|
544
|
+
}
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
//#endregion
|
|
548
|
+
//#region src/hydration/createHydrationStatus.ts
|
|
549
|
+
function T(e) {
|
|
550
|
+
return {
|
|
551
|
+
key: e,
|
|
552
|
+
state: "idle",
|
|
553
|
+
lastAttemptAt: null,
|
|
554
|
+
lastSuccessAt: null,
|
|
555
|
+
rowCount: null,
|
|
556
|
+
errorMessage: null
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
function E(e = {}) {
|
|
560
|
+
let t = e.now ?? Date.now, n = /* @__PURE__ */ new Set(), r = {}, i = !1, a = () => ({
|
|
561
|
+
running: i,
|
|
562
|
+
entities: Object.values(r).sort((e, t) => e.key.localeCompare(t.key))
|
|
563
|
+
}), o = () => {
|
|
564
|
+
let e = a();
|
|
565
|
+
for (let t of n) t(e);
|
|
566
|
+
}, s = (e, t) => {
|
|
567
|
+
r = {
|
|
568
|
+
...r,
|
|
569
|
+
[e]: {
|
|
570
|
+
...r[e] ?? T(e),
|
|
571
|
+
...t
|
|
572
|
+
}
|
|
573
|
+
}, o();
|
|
574
|
+
};
|
|
575
|
+
return {
|
|
576
|
+
getSnapshot: a,
|
|
577
|
+
subscribe(e) {
|
|
578
|
+
return n.add(e), () => n.delete(e);
|
|
579
|
+
},
|
|
580
|
+
registerEntities(e) {
|
|
581
|
+
let t = !1, n = r;
|
|
582
|
+
for (let r of e) n[r] || (t = !0, n = {
|
|
583
|
+
...n,
|
|
584
|
+
[r]: T(r)
|
|
585
|
+
});
|
|
586
|
+
t && (r = n, o());
|
|
587
|
+
},
|
|
588
|
+
markRunning(e) {
|
|
589
|
+
i !== e && (i = e, o());
|
|
590
|
+
},
|
|
591
|
+
markEntityRunning(e) {
|
|
592
|
+
s(e, {
|
|
593
|
+
state: "running",
|
|
594
|
+
lastAttemptAt: t(),
|
|
595
|
+
errorMessage: null
|
|
596
|
+
});
|
|
597
|
+
},
|
|
598
|
+
markEntitySuccess(e, n) {
|
|
599
|
+
s(e, {
|
|
600
|
+
state: "success",
|
|
601
|
+
lastSuccessAt: t(),
|
|
602
|
+
rowCount: n,
|
|
603
|
+
errorMessage: null
|
|
604
|
+
});
|
|
605
|
+
},
|
|
606
|
+
markEntityError(e, t) {
|
|
607
|
+
s(e, {
|
|
608
|
+
state: "error",
|
|
609
|
+
errorMessage: t
|
|
610
|
+
});
|
|
611
|
+
},
|
|
612
|
+
reset() {
|
|
613
|
+
!i && Object.keys(r).length === 0 || (i = !1, r = {}, o());
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
//#endregion
|
|
618
|
+
//#region src/hydration/createLocalReflectionQueue.ts
|
|
619
|
+
function D(e) {
|
|
620
|
+
let t = /* @__PURE__ */ new Set(), n = () => e.onPendingCountChange?.(t.size);
|
|
621
|
+
return {
|
|
622
|
+
enqueue(r, i) {
|
|
623
|
+
let a = e.runEntityExclusive(r, i);
|
|
624
|
+
t.add(a), n(), a.catch((t) => e.onError?.(r, t)).finally(() => {
|
|
625
|
+
t.delete(a), n();
|
|
626
|
+
});
|
|
627
|
+
},
|
|
628
|
+
async awaitIdle() {
|
|
629
|
+
for (; t.size > 0;) await Promise.allSettled([...t]);
|
|
630
|
+
},
|
|
631
|
+
getPendingCount: () => t.size
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
//#endregion
|
|
635
|
+
//#region src/lifecycle/createDatabaseOwnerStore.ts
|
|
636
|
+
function O({ key: e, getStorage: t }) {
|
|
637
|
+
return {
|
|
638
|
+
read() {
|
|
639
|
+
let n = t().getItem(e);
|
|
640
|
+
return n != null && n.length > 0 ? n : null;
|
|
641
|
+
},
|
|
642
|
+
persist(n) {
|
|
643
|
+
t().setItem(e, n);
|
|
644
|
+
},
|
|
645
|
+
clear() {
|
|
646
|
+
t().removeItem(e);
|
|
647
|
+
}
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
//#endregion
|
|
651
|
+
//#region src/lifecycle/createOfflineDataLifecycle.ts
|
|
652
|
+
function k(e) {
|
|
653
|
+
let t = null, n = async (t) => {
|
|
654
|
+
t.onPhase?.("hydration"), e.abortHydration(e.hydrationAbortReason ?? "SQLite hydration was cancelled before purging local data."), await e.awaitHydrationIdle(), await e.awaitLocalReflectionIdle(), t.onPhase?.("queue"), await e.awaitQueueFlushIdle(), e.resetHydrationGate(), e.resetHydrationStatus(), e.resetQueueStatus(), e.resetRuntime(), e.clearInMemoryStores();
|
|
655
|
+
}, r = async (e = {}) => {
|
|
656
|
+
await n(e), e.onPhase?.("storage");
|
|
657
|
+
}, i = async (t = {}) => {
|
|
658
|
+
let r = e.readOwner(), i = r == null ? void 0 : e.resolveDatabaseFileName(r, r);
|
|
659
|
+
await n(t), t.onPhase?.("storage"), await e.deleteDatabaseFiles(i);
|
|
660
|
+
}, a = async (t) => {
|
|
661
|
+
let n = e.readOwner();
|
|
662
|
+
return n === t ? (e.resolveDatabaseFileName(t, n), !1) : (n != null && await r(), e.persistOwner(t), e.resolveDatabaseFileName(t, n), n != null);
|
|
663
|
+
};
|
|
664
|
+
return {
|
|
665
|
+
release: r,
|
|
666
|
+
purge: i,
|
|
667
|
+
async ensureOwnedBy(e) {
|
|
668
|
+
return t ??= a(e).finally(() => {
|
|
669
|
+
t = null;
|
|
670
|
+
}), await t;
|
|
671
|
+
}
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
//#endregion
|
|
675
|
+
//#region src/lifecycle/createOpfsDatabaseFiles.ts
|
|
676
|
+
var A = /* @__PURE__ */ new Set([
|
|
677
|
+
"NoModificationAllowedError",
|
|
678
|
+
"InvalidStateError",
|
|
679
|
+
"InvalidModificationError"
|
|
680
|
+
]);
|
|
681
|
+
function ee(e) {
|
|
682
|
+
return Array.from(new TextEncoder().encode(e), (e) => e.toString(16).padStart(2, "0")).join("");
|
|
683
|
+
}
|
|
684
|
+
function te(e, t) {
|
|
685
|
+
return e instanceof Error && e.name === t;
|
|
686
|
+
}
|
|
687
|
+
async function ne(e, t) {
|
|
688
|
+
let n = e;
|
|
689
|
+
if (typeof n.keys != "function") return [t];
|
|
690
|
+
let r = [];
|
|
691
|
+
for await (let e of n.keys()) (e === t || e.startsWith(`${t}-`)) && r.push(e);
|
|
692
|
+
return r;
|
|
693
|
+
}
|
|
694
|
+
async function re(e, t) {
|
|
695
|
+
try {
|
|
696
|
+
await e.removeEntry(t, { recursive: !0 });
|
|
697
|
+
} catch (e) {
|
|
698
|
+
if (!te(e, "NotFoundError")) throw e;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
function ie(e) {
|
|
702
|
+
let t = e.lockRetryDelayMs ?? 50, n = e.wait ?? ((e) => new Promise((t) => setTimeout(t, e)));
|
|
703
|
+
return {
|
|
704
|
+
getFileName(t, n) {
|
|
705
|
+
let r = ee(t), i = `${e.mappingKeyPrefix}${r}`, a = e.getStorage(), o = a.getItem(i);
|
|
706
|
+
if (o != null) return o;
|
|
707
|
+
let s = n === t ? e.legacyFileName : e.createScopedFileName(r);
|
|
708
|
+
return a.setItem(i, s), s;
|
|
709
|
+
},
|
|
710
|
+
async deleteFiles(r = e.legacyFileName) {
|
|
711
|
+
let i = e.getDirectory();
|
|
712
|
+
if (!i) return;
|
|
713
|
+
let a = await i;
|
|
714
|
+
for (let e of await ne(a, r)) try {
|
|
715
|
+
await re(a, e);
|
|
716
|
+
} catch (r) {
|
|
717
|
+
if (!(r instanceof Error) || !A.has(r.name)) throw r;
|
|
718
|
+
await n(t), await re(a, e);
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
//#endregion
|
|
724
|
+
//#region src/offline-queue/createOfflineQueueCapture.ts
|
|
725
|
+
function ae(n) {
|
|
726
|
+
return async (r) => {
|
|
727
|
+
let i = {
|
|
728
|
+
commandId: n.createCommandId(),
|
|
729
|
+
method: (r.method ?? "get").toUpperCase(),
|
|
730
|
+
url: n.resolveUrl(r),
|
|
731
|
+
body: r.body ?? null,
|
|
732
|
+
headers: t(e(r.headers)),
|
|
733
|
+
createdAt: n.now()
|
|
734
|
+
};
|
|
735
|
+
return await n.enqueue(i), n.markEnqueued(), await n.refreshStatus(), i;
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
//#endregion
|
|
739
|
+
//#region src/offline-queue/offlineQueueStateSqlite.ts
|
|
740
|
+
var j = "offline_sync_commands", M = "PENDING", N = "PERMANENTLY_FAILED";
|
|
741
|
+
function P(e, t, n) {
|
|
742
|
+
try {
|
|
743
|
+
return JSON.parse(e);
|
|
744
|
+
} catch {
|
|
745
|
+
throw Error(`Offline command "${t}" contains invalid JSON in ${n}.`);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
function oe(e, t) {
|
|
749
|
+
let n = P(e, t, "headers_json");
|
|
750
|
+
if (typeof n != "object" || !n || Array.isArray(n) || Object.values(n).some((e) => typeof e != "string")) throw Error(`Offline command "${t}" contains invalid headers_json.`);
|
|
751
|
+
return n;
|
|
752
|
+
}
|
|
753
|
+
function F(e, t) {
|
|
754
|
+
if (!Number.isInteger(e) || e <= 0) throw Error(`${t} must be a positive integer.`);
|
|
755
|
+
}
|
|
756
|
+
function I(e) {
|
|
757
|
+
return String(e ?? "PENDING") === "PERMANENTLY_FAILED" ? N : M;
|
|
758
|
+
}
|
|
759
|
+
function L(e, t) {
|
|
760
|
+
let n = e.prepare(`
|
|
761
|
+
INSERT OR REPLACE INTO ${j}
|
|
762
|
+
(command_id, method, url, body_json, headers_json, created_at)
|
|
763
|
+
VALUES (?, ?, ?, ?, ?, ?);
|
|
764
|
+
`);
|
|
765
|
+
try {
|
|
766
|
+
n.bind([
|
|
767
|
+
t.commandId,
|
|
768
|
+
t.method,
|
|
769
|
+
t.url,
|
|
770
|
+
JSON.stringify(t.body ?? null),
|
|
771
|
+
JSON.stringify(t.headers),
|
|
772
|
+
t.createdAt
|
|
773
|
+
]), n.step();
|
|
774
|
+
} finally {
|
|
775
|
+
n.finalize();
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
function R(e, t) {
|
|
779
|
+
F(t, "batchSize");
|
|
780
|
+
let n = e.prepare(`
|
|
781
|
+
SELECT command_id, method, url, body_json, headers_json, created_at, status, retry_count, last_error
|
|
782
|
+
FROM ${j}
|
|
783
|
+
WHERE status = '${M}'
|
|
784
|
+
ORDER BY created_at ASC
|
|
785
|
+
LIMIT ?;
|
|
786
|
+
`), r = [];
|
|
787
|
+
try {
|
|
788
|
+
for (n.bind([t]); n.step();) {
|
|
789
|
+
let e = n.get([]), t = String(e[0] ?? "");
|
|
790
|
+
r.push({
|
|
791
|
+
commandId: t,
|
|
792
|
+
method: String(e[1] ?? ""),
|
|
793
|
+
url: String(e[2] ?? ""),
|
|
794
|
+
body: P(String(e[3] ?? "null"), t, "body_json"),
|
|
795
|
+
headers: oe(String(e[4] ?? "{}"), t),
|
|
796
|
+
createdAt: Number(e[5] ?? 0),
|
|
797
|
+
status: I(e[6]),
|
|
798
|
+
retryCount: Number(e[7] ?? 0),
|
|
799
|
+
lastError: e[8] == null ? null : String(e[8])
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
} finally {
|
|
803
|
+
n.finalize();
|
|
804
|
+
}
|
|
805
|
+
return r;
|
|
806
|
+
}
|
|
807
|
+
function z(e, t) {
|
|
808
|
+
t.length !== 0 && r(e, () => {
|
|
809
|
+
for (let n of t) {
|
|
810
|
+
let t = e.prepare(`DELETE FROM ${j} WHERE command_id = ?;`);
|
|
811
|
+
try {
|
|
812
|
+
t.bind([n]), t.step();
|
|
813
|
+
} finally {
|
|
814
|
+
t.finalize();
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
});
|
|
818
|
+
}
|
|
819
|
+
function B(e) {
|
|
820
|
+
let t = e.prepare(`SELECT COUNT(1) AS count FROM ${j};`);
|
|
821
|
+
try {
|
|
822
|
+
return t.step() ? Number(t.get([])[0] ?? 0) : 0;
|
|
823
|
+
} finally {
|
|
824
|
+
t.finalize();
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
function V(e) {
|
|
828
|
+
let t = e.prepare(`
|
|
829
|
+
SELECT status, COUNT(1) AS count
|
|
830
|
+
FROM ${j}
|
|
831
|
+
GROUP BY status;
|
|
832
|
+
`), n = {
|
|
833
|
+
pending: 0,
|
|
834
|
+
permanentlyFailed: 0
|
|
835
|
+
};
|
|
836
|
+
try {
|
|
837
|
+
for (; t.step();) {
|
|
838
|
+
let e = t.get([]), r = Number(e[1] ?? 0);
|
|
839
|
+
I(e[0]) === "PERMANENTLY_FAILED" ? n.permanentlyFailed = r : n.pending = r;
|
|
840
|
+
}
|
|
841
|
+
} finally {
|
|
842
|
+
t.finalize();
|
|
843
|
+
}
|
|
844
|
+
return n;
|
|
845
|
+
}
|
|
846
|
+
function H(e, t, n, i) {
|
|
847
|
+
t.length !== 0 && (F(i, "maxRetryCount"), r(e, () => {
|
|
848
|
+
for (let r of t) {
|
|
849
|
+
let t = e.prepare(`
|
|
850
|
+
UPDATE ${j}
|
|
851
|
+
SET retry_count = retry_count + 1,
|
|
852
|
+
last_error = ?,
|
|
853
|
+
status = CASE
|
|
854
|
+
WHEN retry_count + 1 >= ? THEN '${N}'
|
|
855
|
+
ELSE status
|
|
856
|
+
END
|
|
857
|
+
WHERE command_id = ?;
|
|
858
|
+
`);
|
|
859
|
+
try {
|
|
860
|
+
t.bind([
|
|
861
|
+
n,
|
|
862
|
+
i,
|
|
863
|
+
r
|
|
864
|
+
]), t.step();
|
|
865
|
+
} finally {
|
|
866
|
+
t.finalize();
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
}));
|
|
870
|
+
}
|
|
871
|
+
function U(e, t, n) {
|
|
872
|
+
t.length !== 0 && r(e, () => {
|
|
873
|
+
for (let r of t) {
|
|
874
|
+
let t = e.prepare(`
|
|
875
|
+
UPDATE ${j}
|
|
876
|
+
SET status = '${N}', last_error = ?
|
|
877
|
+
WHERE command_id = ?;
|
|
878
|
+
`);
|
|
879
|
+
try {
|
|
880
|
+
t.bind([n, r]), t.step();
|
|
881
|
+
} finally {
|
|
882
|
+
t.finalize();
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
var se = o({
|
|
888
|
+
enqueue: (e, t) => (L(e, t.command), null),
|
|
889
|
+
getPendingBatch: (e, t) => R(e, t.batchSize),
|
|
890
|
+
deleteCommands: (e, t) => (z(e, t.commandIds), null),
|
|
891
|
+
getQueueSize: (e) => B(e),
|
|
892
|
+
getStatusCounts: (e) => V(e),
|
|
893
|
+
markCommandsRetryable: (e, t) => {
|
|
894
|
+
let n = t;
|
|
895
|
+
return H(e, n.commandIds, n.lastError, n.maxRetryCount), null;
|
|
896
|
+
},
|
|
897
|
+
markCommandsPermanentlyFailed: (e, t) => {
|
|
898
|
+
let n = t;
|
|
899
|
+
return U(e, n.commandIds, n.lastError), null;
|
|
900
|
+
}
|
|
901
|
+
});
|
|
902
|
+
//#endregion
|
|
903
|
+
//#region src/offline-queue/createOfflineQueueClient.ts
|
|
904
|
+
function ce(e) {
|
|
905
|
+
let t = /* @__PURE__ */ new Map();
|
|
906
|
+
return {
|
|
907
|
+
async enqueue(n) {
|
|
908
|
+
if (e.runtime.shouldUseInMemoryFallback()) {
|
|
909
|
+
t.set(n.commandId, {
|
|
910
|
+
...n,
|
|
911
|
+
status: M,
|
|
912
|
+
retryCount: 0,
|
|
913
|
+
lastError: null
|
|
914
|
+
});
|
|
915
|
+
return;
|
|
916
|
+
}
|
|
917
|
+
await e.transport.sendWorkerRequest({
|
|
918
|
+
type: "enqueue",
|
|
919
|
+
command: n
|
|
920
|
+
});
|
|
921
|
+
},
|
|
922
|
+
async getBatch(n) {
|
|
923
|
+
if (!Number.isInteger(n) || n <= 0) throw Error("batchSize must be a positive integer.");
|
|
924
|
+
return e.runtime.shouldUseInMemoryFallback() ? [...t.values()].filter((e) => e.status === M).sort((e, t) => e.createdAt - t.createdAt).slice(0, n) : await e.transport.sendWorkerRequest({
|
|
925
|
+
type: "getPendingBatch",
|
|
926
|
+
batchSize: n
|
|
927
|
+
});
|
|
928
|
+
},
|
|
929
|
+
async delete(n) {
|
|
930
|
+
if (e.runtime.shouldUseInMemoryFallback()) {
|
|
931
|
+
for (let e of n) t.delete(e);
|
|
932
|
+
return;
|
|
933
|
+
}
|
|
934
|
+
await e.transport.sendWorkerRequest({
|
|
935
|
+
type: "deleteCommands",
|
|
936
|
+
commandIds: n
|
|
937
|
+
});
|
|
938
|
+
},
|
|
939
|
+
async markRetryable(n, r, i) {
|
|
940
|
+
if (n.length !== 0) {
|
|
941
|
+
if (!Number.isInteger(i) || i <= 0) throw Error("maxRetryCount must be a positive integer.");
|
|
942
|
+
if (e.runtime.shouldUseInMemoryFallback()) {
|
|
943
|
+
for (let e of n) {
|
|
944
|
+
let n = t.get(e);
|
|
945
|
+
if (!n) continue;
|
|
946
|
+
let a = n.retryCount + 1;
|
|
947
|
+
t.set(e, {
|
|
948
|
+
...n,
|
|
949
|
+
retryCount: a,
|
|
950
|
+
lastError: r,
|
|
951
|
+
status: a >= i ? N : n.status
|
|
952
|
+
});
|
|
953
|
+
}
|
|
954
|
+
return;
|
|
955
|
+
}
|
|
956
|
+
await e.transport.sendWorkerRequest({
|
|
957
|
+
type: "markCommandsRetryable",
|
|
958
|
+
commandIds: n,
|
|
959
|
+
lastError: r,
|
|
960
|
+
maxRetryCount: i
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
},
|
|
964
|
+
async markPermanentlyFailed(n, r) {
|
|
965
|
+
if (n.length !== 0) {
|
|
966
|
+
if (e.runtime.shouldUseInMemoryFallback()) {
|
|
967
|
+
for (let e of n) {
|
|
968
|
+
let n = t.get(e);
|
|
969
|
+
n && t.set(e, {
|
|
970
|
+
...n,
|
|
971
|
+
lastError: r,
|
|
972
|
+
status: N
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
return;
|
|
976
|
+
}
|
|
977
|
+
await e.transport.sendWorkerRequest({
|
|
978
|
+
type: "markCommandsPermanentlyFailed",
|
|
979
|
+
commandIds: n,
|
|
980
|
+
lastError: r
|
|
981
|
+
});
|
|
982
|
+
}
|
|
983
|
+
},
|
|
984
|
+
async getSize() {
|
|
985
|
+
return e.runtime.shouldUseInMemoryFallback() ? t.size : await e.transport.sendWorkerRequest({ type: "getQueueSize" });
|
|
986
|
+
},
|
|
987
|
+
async getStatusCounts() {
|
|
988
|
+
if (!e.runtime.shouldUseInMemoryFallback()) return await e.transport.sendWorkerRequest({ type: "getStatusCounts" });
|
|
989
|
+
let n = 0, r = 0;
|
|
990
|
+
for (let e of t.values()) e.status === "PERMANENTLY_FAILED" ? r += 1 : n += 1;
|
|
991
|
+
return {
|
|
992
|
+
pending: n,
|
|
993
|
+
permanentlyFailed: r
|
|
994
|
+
};
|
|
995
|
+
},
|
|
996
|
+
dispose: e.runtime.registerInMemoryStore(t)
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
//#endregion
|
|
1000
|
+
//#region src/offline-queue/createOfflineQueueStatus.ts
|
|
1001
|
+
var W = {
|
|
1002
|
+
pending: 0,
|
|
1003
|
+
permanentlyFailed: 0
|
|
1004
|
+
};
|
|
1005
|
+
function le(e) {
|
|
1006
|
+
let t = /* @__PURE__ */ new Set(), n = {
|
|
1007
|
+
...W,
|
|
1008
|
+
enqueueTick: 0
|
|
1009
|
+
}, r = (e) => {
|
|
1010
|
+
if (e.pending !== n.pending || e.permanentlyFailed !== n.permanentlyFailed || e.enqueueTick !== n.enqueueTick) {
|
|
1011
|
+
n = e;
|
|
1012
|
+
for (let e of t) e(n);
|
|
1013
|
+
}
|
|
1014
|
+
};
|
|
1015
|
+
return {
|
|
1016
|
+
getSnapshot: () => n,
|
|
1017
|
+
subscribe(e) {
|
|
1018
|
+
return t.add(e), () => t.delete(e);
|
|
1019
|
+
},
|
|
1020
|
+
markEnqueued() {
|
|
1021
|
+
r({
|
|
1022
|
+
...n,
|
|
1023
|
+
enqueueTick: n.enqueueTick + 1
|
|
1024
|
+
});
|
|
1025
|
+
},
|
|
1026
|
+
async refresh() {
|
|
1027
|
+
try {
|
|
1028
|
+
let t = await e.readStatusCounts();
|
|
1029
|
+
r({
|
|
1030
|
+
...t,
|
|
1031
|
+
enqueueTick: n.enqueueTick
|
|
1032
|
+
});
|
|
1033
|
+
} catch (t) {
|
|
1034
|
+
e.onReadError?.(t);
|
|
1035
|
+
}
|
|
1036
|
+
},
|
|
1037
|
+
resetCounts() {
|
|
1038
|
+
r({
|
|
1039
|
+
...W,
|
|
1040
|
+
enqueueTick: n.enqueueTick
|
|
1041
|
+
});
|
|
1042
|
+
}
|
|
1043
|
+
};
|
|
1044
|
+
}
|
|
1045
|
+
//#endregion
|
|
1046
|
+
//#region src/offline-queue/replayOfflineSyncBatch.ts
|
|
1047
|
+
var ue = /* @__PURE__ */ new Set(["REJECTED", "RETRY_LIMIT_EXCEEDED"]);
|
|
1048
|
+
function de(e) {
|
|
1049
|
+
return !e.success && e.reason != null && ue.has(e.reason);
|
|
1050
|
+
}
|
|
1051
|
+
function fe(e) {
|
|
1052
|
+
return {
|
|
1053
|
+
commandId: e.commandId,
|
|
1054
|
+
method: e.method,
|
|
1055
|
+
url: e.url,
|
|
1056
|
+
body: e.body,
|
|
1057
|
+
headers: e.headers
|
|
1058
|
+
};
|
|
1059
|
+
}
|
|
1060
|
+
async function pe(e) {
|
|
1061
|
+
let { batchSize: t, maxAttempts: n, dependencies: r } = e;
|
|
1062
|
+
if (!Number.isInteger(t) || t <= 0) throw Error("batchSize must be a positive integer.");
|
|
1063
|
+
if (!Number.isInteger(n) || n <= 0) throw Error("maxAttempts must be a positive integer.");
|
|
1064
|
+
let i = await r.getBatch(t);
|
|
1065
|
+
if (i.length === 0) return 0;
|
|
1066
|
+
let a = await r.sendBatch(i.map(fe)), o = new Map(a.results.map((e) => [e.commandId, e])), s = [], c = [], l = [], u = null, d = null;
|
|
1067
|
+
for (let e of i) {
|
|
1068
|
+
let t = o.get(e.commandId);
|
|
1069
|
+
t ? t.success ? s.push(e.commandId) : de(t) ? (c.push(e.commandId), u = t.error ?? u) : (l.push(e.commandId), d = t.error ?? d) : l.push(e.commandId);
|
|
1070
|
+
}
|
|
1071
|
+
let f = new Set(s);
|
|
1072
|
+
return await r.cleanupSuccessfulCommands(i.filter((e) => f.has(e.commandId))), s.length > 0 && await r.deleteSuccessful(s), await r.markPermanentlyFailed(c, u), await r.markRetryable(l, d, n), await r.refreshStatus(), s.length;
|
|
1073
|
+
}
|
|
1074
|
+
//#endregion
|
|
1075
|
+
//#region src/runtime/createManagedSqliteRuntime.ts
|
|
1076
|
+
function me() {
|
|
1077
|
+
return typeof performance < "u" && typeof performance.now == "function" ? performance.now() : Date.now();
|
|
1078
|
+
}
|
|
1079
|
+
function he(e) {
|
|
1080
|
+
let t = e.bootLogPrefix ?? "[SQLITE-BOOT]", n = e.now ?? me, r = /* @__PURE__ */ new Set(), i = /* @__PURE__ */ new Map(), a = null, o = 1, s = !1, c = null, l = 0, u = !1, d = (n, ...r) => {
|
|
1081
|
+
e.debug && console.debug(`${t} ${n}`, ...r);
|
|
1082
|
+
}, f = () => {
|
|
1083
|
+
if (u) throw Error("SQLite runtime has been disposed.");
|
|
1084
|
+
}, p = (e) => {
|
|
1085
|
+
for (let t of i.values()) t.reject(e);
|
|
1086
|
+
i.clear();
|
|
1087
|
+
}, m = (e) => {
|
|
1088
|
+
a?.terminate(), a = null, s = !1, c = null, l += 1, p(e);
|
|
1089
|
+
}, h = () => (f(), a || (a = e.workerFactory(), a.addEventListener("message", (e) => {
|
|
1090
|
+
let t = e.data, n = i.get(t.id);
|
|
1091
|
+
n && (i.delete(t.id), t.ok ? n.resolve(t.result) : n.reject(Error(t.error)));
|
|
1092
|
+
}), a.addEventListener("error", (e) => {
|
|
1093
|
+
let t = e.error instanceof Error ? e.error : Error(e.message);
|
|
1094
|
+
m(t);
|
|
1095
|
+
}), a.addEventListener("messageerror", () => {
|
|
1096
|
+
m(/* @__PURE__ */ Error("SQLite worker returned an unreadable message."));
|
|
1097
|
+
}), a)), g = async (e) => {
|
|
1098
|
+
let t = o++, n = h();
|
|
1099
|
+
return await new Promise((r, a) => {
|
|
1100
|
+
i.set(t, {
|
|
1101
|
+
resolve: (e) => r(e),
|
|
1102
|
+
reject: a
|
|
1103
|
+
});
|
|
1104
|
+
try {
|
|
1105
|
+
n.postMessage({
|
|
1106
|
+
id: t,
|
|
1107
|
+
...e
|
|
1108
|
+
});
|
|
1109
|
+
} catch (e) {
|
|
1110
|
+
i.delete(t), a(e);
|
|
1111
|
+
}
|
|
1112
|
+
});
|
|
1113
|
+
}, _ = async () => {
|
|
1114
|
+
if (f(), s) return;
|
|
1115
|
+
if (c) return await c;
|
|
1116
|
+
let e = n(), t = ++l;
|
|
1117
|
+
d("client init request -> worker");
|
|
1118
|
+
let r = (async () => {
|
|
1119
|
+
try {
|
|
1120
|
+
await g({ type: "init" }), s = !0, d(`client init completed in ${Math.round(n() - e)}ms`);
|
|
1121
|
+
} catch (t) {
|
|
1122
|
+
throw d(`client init failed after ${Math.round(n() - e)}ms`, t), t;
|
|
1123
|
+
} finally {
|
|
1124
|
+
l === t && (c = null);
|
|
1125
|
+
}
|
|
1126
|
+
})();
|
|
1127
|
+
c = r, await r;
|
|
1128
|
+
};
|
|
1129
|
+
return {
|
|
1130
|
+
registerInMemoryStore(e) {
|
|
1131
|
+
return r.add(e), () => r.delete(e);
|
|
1132
|
+
},
|
|
1133
|
+
clearInMemoryStores() {
|
|
1134
|
+
for (let e of r) e.clear();
|
|
1135
|
+
},
|
|
1136
|
+
shouldUseInMemoryFallback: () => e.shouldUseInMemoryFallback?.() ?? !1,
|
|
1137
|
+
send: g,
|
|
1138
|
+
ensureInitialized: _,
|
|
1139
|
+
warmup: _,
|
|
1140
|
+
reset() {
|
|
1141
|
+
f(), m(/* @__PURE__ */ Error("SQLite runtime was reset."));
|
|
1142
|
+
},
|
|
1143
|
+
dispose() {
|
|
1144
|
+
u || (u = !0, m(/* @__PURE__ */ Error("SQLite runtime was disposed.")), r.clear());
|
|
1145
|
+
}
|
|
1146
|
+
};
|
|
1147
|
+
}
|
|
1148
|
+
//#endregion
|
|
1149
|
+
//#region src/runtime/createSqliteEntityClient.ts
|
|
1150
|
+
function G(e, t, n) {
|
|
1151
|
+
return {
|
|
1152
|
+
type: e,
|
|
1153
|
+
[t]: n
|
|
1154
|
+
};
|
|
1155
|
+
}
|
|
1156
|
+
function ge(e) {
|
|
1157
|
+
let t = /* @__PURE__ */ new Map();
|
|
1158
|
+
return e.runtime.registerInMemoryStore(t), {
|
|
1159
|
+
async replace(n) {
|
|
1160
|
+
if (e.runtime.shouldUseInMemoryFallback()) {
|
|
1161
|
+
t.clear();
|
|
1162
|
+
for (let r of n) t.set(e.getKey(r), r);
|
|
1163
|
+
return;
|
|
1164
|
+
}
|
|
1165
|
+
await e.transport.sendWorkerRequest(G(e.requests.replace.type, e.requests.replace.payloadKey, n));
|
|
1166
|
+
},
|
|
1167
|
+
async upsert(n) {
|
|
1168
|
+
if (e.runtime.shouldUseInMemoryFallback()) {
|
|
1169
|
+
t.set(e.getKey(n), n);
|
|
1170
|
+
return;
|
|
1171
|
+
}
|
|
1172
|
+
await e.transport.sendWorkerRequest(G(e.requests.upsert.type, e.requests.upsert.payloadKey, n));
|
|
1173
|
+
},
|
|
1174
|
+
async list() {
|
|
1175
|
+
return e.runtime.shouldUseInMemoryFallback() ? [...t.values()].sort(e.compare) : await e.transport.sendWorkerRequest({ type: e.requests.list.type });
|
|
1176
|
+
},
|
|
1177
|
+
async delete(n) {
|
|
1178
|
+
if (e.runtime.shouldUseInMemoryFallback()) {
|
|
1179
|
+
for (let e of n) t.delete(e);
|
|
1180
|
+
return;
|
|
1181
|
+
}
|
|
1182
|
+
await e.transport.sendWorkerRequest(G(e.requests.delete.type, e.requests.delete.payloadKey, n));
|
|
1183
|
+
}
|
|
1184
|
+
};
|
|
1185
|
+
}
|
|
1186
|
+
//#endregion
|
|
1187
|
+
//#region src/runtime/createSqliteTransport.ts
|
|
1188
|
+
function _e({ runtime: e, createUnavailableError: t }) {
|
|
1189
|
+
let n = t ?? ((e) => /* @__PURE__ */ Error(`SQLite request '${e}' is unavailable in this runtime.`));
|
|
1190
|
+
return {
|
|
1191
|
+
async sendRequest(t, r) {
|
|
1192
|
+
if (e.shouldUseInMemoryFallback()) throw n(t);
|
|
1193
|
+
return await e.ensureInitialized(), await e.send({
|
|
1194
|
+
type: t,
|
|
1195
|
+
...r ?? {}
|
|
1196
|
+
});
|
|
1197
|
+
},
|
|
1198
|
+
async sendWorkerRequest(t) {
|
|
1199
|
+
return await e.ensureInitialized(), await e.send(t);
|
|
1200
|
+
}
|
|
1201
|
+
};
|
|
1202
|
+
}
|
|
1203
|
+
//#endregion
|
|
1204
|
+
//#region src/core/sqliteRuntime.ts
|
|
1205
|
+
function K() {
|
|
1206
|
+
return typeof performance < "u" && typeof performance.now == "function" ? performance.now() : Date.now();
|
|
1207
|
+
}
|
|
1208
|
+
function q(e) {
|
|
1209
|
+
return e instanceof Error ? e.message : "Unknown worker error";
|
|
1210
|
+
}
|
|
1211
|
+
function ve(e) {
|
|
1212
|
+
let t = typeof navigator < "u", n = t && navigator.storage != null;
|
|
1213
|
+
return {
|
|
1214
|
+
crossOriginIsolated: globalThis.crossOriginIsolated === !0,
|
|
1215
|
+
hasSharedArrayBuffer: typeof SharedArrayBuffer < "u",
|
|
1216
|
+
hasNavigator: t,
|
|
1217
|
+
hasNavigatorStorage: n,
|
|
1218
|
+
hasStorageGetDirectory: n && typeof navigator.storage.getDirectory == "function",
|
|
1219
|
+
hasOpfsDbCtor: typeof e.oo1.OpfsDb == "function",
|
|
1220
|
+
hasSqliteOpfsNamespace: e.opfs != null
|
|
1221
|
+
};
|
|
1222
|
+
}
|
|
1223
|
+
function J(e) {
|
|
1224
|
+
let t = ve(e);
|
|
1225
|
+
return Object.entries(t).map(([e, t]) => `${e}=${String(t)}`).join(", ");
|
|
1226
|
+
}
|
|
1227
|
+
function ye(e, t) {
|
|
1228
|
+
if (typeof e.oo1.OpfsDb != "function") throw Error(`[offline-sqlite] OPFS database constructor is unavailable. Diagnostics: ${J(e)}.`);
|
|
1229
|
+
try {
|
|
1230
|
+
return new e.oo1.OpfsDb(t);
|
|
1231
|
+
} catch (t) {
|
|
1232
|
+
throw Error(`[offline-sqlite] Failed to open OPFS SQLite database: ${q(t)}. Diagnostics: ${J(e)}.`);
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
function be(e, t) {
|
|
1236
|
+
let n = e.prepare(t);
|
|
1237
|
+
try {
|
|
1238
|
+
if (!n.step()) return 0;
|
|
1239
|
+
let e = n.get([]);
|
|
1240
|
+
return Number(e[0] ?? 0);
|
|
1241
|
+
} finally {
|
|
1242
|
+
n.finalize();
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
function Y(e) {
|
|
1246
|
+
return be(e, "PRAGMA user_version;");
|
|
1247
|
+
}
|
|
1248
|
+
function xe(e, t) {
|
|
1249
|
+
e.exec(`PRAGMA user_version = ${t};`);
|
|
1250
|
+
}
|
|
1251
|
+
function Se(e, t) {
|
|
1252
|
+
let n = K();
|
|
1253
|
+
e.exec("BEGIN;");
|
|
1254
|
+
try {
|
|
1255
|
+
let r = Y(e);
|
|
1256
|
+
`${r}${t.length}`;
|
|
1257
|
+
for (let n = r; n < t.length; n += 1) {
|
|
1258
|
+
let r = K();
|
|
1259
|
+
t[n](e), xe(e, n + 1), `${n + 1}${Math.round(K() - r)}`;
|
|
1260
|
+
}
|
|
1261
|
+
if (Y(e) > t.length) throw Error("Database schema version is newer than supported client version.");
|
|
1262
|
+
e.exec("COMMIT;"), `${Math.round(K() - n)}`;
|
|
1263
|
+
} catch (t) {
|
|
1264
|
+
throw e.exec("ROLLBACK;"), `${Math.round(K() - n)}`, t;
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
async function Ce(e, t) {
|
|
1268
|
+
let r = K(), i = K(), a = await n();
|
|
1269
|
+
`${Math.round(K() - i)}`;
|
|
1270
|
+
let o = K(), s = ye(a, e);
|
|
1271
|
+
`${Math.round(K() - o)}`;
|
|
1272
|
+
let c = K();
|
|
1273
|
+
return Se(s, t), `${Math.round(K() - c)}`, `${Math.round(K() - r)}`, s;
|
|
1274
|
+
}
|
|
1275
|
+
//#endregion
|
|
1276
|
+
//#region src/core/sqliteWorkerRuntime.ts
|
|
1277
|
+
function X() {
|
|
1278
|
+
return typeof performance < "u" && typeof performance.now == "function" ? performance.now() : Date.now();
|
|
1279
|
+
}
|
|
1280
|
+
function we(e) {
|
|
1281
|
+
let t = e.bootLogPrefix ?? "[SQLITE-BOOT]", n = null, r = null;
|
|
1282
|
+
function i(n, ...r) {
|
|
1283
|
+
e.debug && console.debug(`${t} ${n}`, ...r);
|
|
1284
|
+
}
|
|
1285
|
+
function a(e, t) {
|
|
1286
|
+
e.postMessage(t);
|
|
1287
|
+
}
|
|
1288
|
+
function o() {
|
|
1289
|
+
if (n == null) throw Error("SQLite database is not initialized.");
|
|
1290
|
+
return n;
|
|
1291
|
+
}
|
|
1292
|
+
async function s() {
|
|
1293
|
+
if (n != null) return;
|
|
1294
|
+
if (r != null) {
|
|
1295
|
+
i("worker init wait for in-flight initialization"), await r;
|
|
1296
|
+
return;
|
|
1297
|
+
}
|
|
1298
|
+
let t = X();
|
|
1299
|
+
i("worker init start"), r = (async () => {
|
|
1300
|
+
try {
|
|
1301
|
+
n = await Ce(e.dbFile, e.migrations), i(`worker init done in ${Math.round(X() - t)}ms`);
|
|
1302
|
+
} finally {
|
|
1303
|
+
r = null;
|
|
1304
|
+
}
|
|
1305
|
+
})(), await r;
|
|
1306
|
+
}
|
|
1307
|
+
async function l(t) {
|
|
1308
|
+
return await c(o(), t, e.requestHandlers);
|
|
1309
|
+
}
|
|
1310
|
+
async function u(e, t) {
|
|
1311
|
+
try {
|
|
1312
|
+
if (await s(), t.type === "init") {
|
|
1313
|
+
a(e, {
|
|
1314
|
+
id: t.id,
|
|
1315
|
+
ok: !0,
|
|
1316
|
+
result: null
|
|
1317
|
+
});
|
|
1318
|
+
return;
|
|
1319
|
+
}
|
|
1320
|
+
let n = await l(t);
|
|
1321
|
+
if (n !== void 0) {
|
|
1322
|
+
a(e, {
|
|
1323
|
+
id: t.id,
|
|
1324
|
+
ok: !0,
|
|
1325
|
+
result: n
|
|
1326
|
+
});
|
|
1327
|
+
return;
|
|
1328
|
+
}
|
|
1329
|
+
throw Error(`Unsupported worker request: ${JSON.stringify(t)}`);
|
|
1330
|
+
} catch (n) {
|
|
1331
|
+
a(e, {
|
|
1332
|
+
id: t.id,
|
|
1333
|
+
ok: !1,
|
|
1334
|
+
error: q(n)
|
|
1335
|
+
});
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
function d(e) {
|
|
1339
|
+
e.addEventListener("message", async (t) => {
|
|
1340
|
+
await u(e, t.data);
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
return {
|
|
1344
|
+
attach: d,
|
|
1345
|
+
handleRequest: u
|
|
1346
|
+
};
|
|
1347
|
+
}
|
|
1348
|
+
//#endregion
|
|
1349
|
+
//#region src/sqliteWorkerConfig.ts
|
|
1350
|
+
function Te(e) {
|
|
1351
|
+
if (e.dbFile.trim().length === 0) throw Error("SQLite worker configuration requires a non-empty dbFile.");
|
|
1352
|
+
return {
|
|
1353
|
+
dbFile: e.dbFile,
|
|
1354
|
+
migrations: e.migrations,
|
|
1355
|
+
debug: e.debug,
|
|
1356
|
+
requestHandlers: s(...e.entityBundles.map((e) => e.requestHandlers), ...e.extraRequestHandlers ?? [])
|
|
1357
|
+
};
|
|
1358
|
+
}
|
|
1359
|
+
//#endregion
|
|
1360
|
+
//#region src/runtime/hydrationEntityStateSqlite.ts
|
|
1361
|
+
var Z = "hydration_entity_state";
|
|
1362
|
+
function Ee(e) {
|
|
1363
|
+
let t = e.prepare(`
|
|
1364
|
+
SELECT entity_key, applied_revision, is_stale, last_hydrated_at, last_row_count, last_error
|
|
1365
|
+
FROM ${Z}
|
|
1366
|
+
ORDER BY entity_key ASC;
|
|
1367
|
+
`), n = [];
|
|
1368
|
+
try {
|
|
1369
|
+
for (; t.step();) {
|
|
1370
|
+
let e = t.get([]);
|
|
1371
|
+
n.push({
|
|
1372
|
+
entityKey: String(e[0] ?? ""),
|
|
1373
|
+
appliedRevision: Number(e[1] ?? 0),
|
|
1374
|
+
isStale: Number(e[2] ?? 0) === 1,
|
|
1375
|
+
lastHydratedAt: e[3] == null ? null : Number(e[3]),
|
|
1376
|
+
lastRowCount: e[4] == null ? null : Number(e[4]),
|
|
1377
|
+
lastError: e[5] == null ? null : String(e[5])
|
|
1378
|
+
});
|
|
1379
|
+
}
|
|
1380
|
+
} finally {
|
|
1381
|
+
t.finalize();
|
|
1382
|
+
}
|
|
1383
|
+
return n;
|
|
1384
|
+
}
|
|
1385
|
+
function De(e, t) {
|
|
1386
|
+
let n = e.prepare(`
|
|
1387
|
+
INSERT INTO ${Z}
|
|
1388
|
+
(entity_key, applied_revision, is_stale, last_hydrated_at, last_row_count, last_error)
|
|
1389
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
1390
|
+
ON CONFLICT(entity_key) DO UPDATE SET
|
|
1391
|
+
applied_revision = excluded.applied_revision,
|
|
1392
|
+
is_stale = excluded.is_stale,
|
|
1393
|
+
last_hydrated_at = excluded.last_hydrated_at,
|
|
1394
|
+
last_row_count = excluded.last_row_count,
|
|
1395
|
+
last_error = excluded.last_error;
|
|
1396
|
+
`);
|
|
1397
|
+
try {
|
|
1398
|
+
n.bind([
|
|
1399
|
+
t.entityKey,
|
|
1400
|
+
t.appliedRevision,
|
|
1401
|
+
+!!t.isStale,
|
|
1402
|
+
t.lastHydratedAt,
|
|
1403
|
+
t.lastRowCount,
|
|
1404
|
+
t.lastError
|
|
1405
|
+
]), n.step();
|
|
1406
|
+
} finally {
|
|
1407
|
+
n.finalize();
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
var Oe = o({
|
|
1411
|
+
listHydrationEntityStates: (e) => Ee(e),
|
|
1412
|
+
upsertHydrationEntityState: (e, t) => (De(e, t.state), null)
|
|
1413
|
+
});
|
|
1414
|
+
//#endregion
|
|
1415
|
+
//#region src/runtime/sqlConsoleSqlite.ts
|
|
1416
|
+
function Q() {
|
|
1417
|
+
return typeof performance < "u" && typeof performance.now == "function" ? performance.now() : Date.now();
|
|
1418
|
+
}
|
|
1419
|
+
function ke(e, t) {
|
|
1420
|
+
let n = e.prepare(t);
|
|
1421
|
+
try {
|
|
1422
|
+
if (!n.step()) return 0;
|
|
1423
|
+
let e = n.get([]);
|
|
1424
|
+
return Number(e[0] ?? 0);
|
|
1425
|
+
} finally {
|
|
1426
|
+
n.finalize();
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
function Ae(e) {
|
|
1430
|
+
return e.split(";").map((e) => e.trim()).filter((e) => e.length > 0);
|
|
1431
|
+
}
|
|
1432
|
+
function je(e, t) {
|
|
1433
|
+
let n = Ae(t);
|
|
1434
|
+
if (n.length === 0) return { statements: [] };
|
|
1435
|
+
let r = [];
|
|
1436
|
+
for (let t of n) {
|
|
1437
|
+
let n = e.prepare(t);
|
|
1438
|
+
try {
|
|
1439
|
+
let i = n.getColumnNames?.([]) ?? [], a = [];
|
|
1440
|
+
for (; n.step();) a.push(n.get([]));
|
|
1441
|
+
let o = ke(e, "SELECT changes();");
|
|
1442
|
+
r.push({
|
|
1443
|
+
statement: t,
|
|
1444
|
+
columns: i,
|
|
1445
|
+
rows: a,
|
|
1446
|
+
rowsAffected: o
|
|
1447
|
+
});
|
|
1448
|
+
} finally {
|
|
1449
|
+
n.finalize();
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
return { statements: r };
|
|
1453
|
+
}
|
|
1454
|
+
function $(e, t) {
|
|
1455
|
+
let n = Q(), r = Number.isFinite(t.clientSentAtMs) && t.clientSentAtMs != null ? Math.max(0, Math.round(n - t.clientSentAtMs)) : void 0, i = [];
|
|
1456
|
+
t.limit != null && t.limit > 0 && (i.push(`LIMIT ${t.limit}`), t.offset != null && t.offset > 0 && i.push(`OFFSET ${t.offset}`));
|
|
1457
|
+
let a = `
|
|
1458
|
+
SELECT ${t.selectSql}
|
|
1459
|
+
FROM ${t.fromSql}
|
|
1460
|
+
WHERE ${t.whereSql}
|
|
1461
|
+
ORDER BY ${t.orderBySql}
|
|
1462
|
+
${i.join(" ")};
|
|
1463
|
+
`, o = e.prepare(a), s = [], c = [];
|
|
1464
|
+
try {
|
|
1465
|
+
for (s = o.getColumnNames?.([]) ?? []; o.step();) c.push(o.get([]));
|
|
1466
|
+
} finally {
|
|
1467
|
+
o.finalize();
|
|
1468
|
+
}
|
|
1469
|
+
if (t.includeTotalCount === !1) return {
|
|
1470
|
+
columns: s,
|
|
1471
|
+
rows: c,
|
|
1472
|
+
totalElements: null,
|
|
1473
|
+
queueWaitMs: r,
|
|
1474
|
+
workerExecMs: Math.round(Q() - n)
|
|
1475
|
+
};
|
|
1476
|
+
let l = `
|
|
1477
|
+
SELECT COUNT(*) AS total_elements
|
|
1478
|
+
FROM ${t.fromSql}
|
|
1479
|
+
WHERE ${t.whereSql};
|
|
1480
|
+
`, u = e.prepare(l);
|
|
1481
|
+
try {
|
|
1482
|
+
if (!u.step()) return {
|
|
1483
|
+
columns: s,
|
|
1484
|
+
rows: c,
|
|
1485
|
+
totalElements: 0,
|
|
1486
|
+
queueWaitMs: r,
|
|
1487
|
+
workerExecMs: Math.round(Q() - n)
|
|
1488
|
+
};
|
|
1489
|
+
let e = u.get([]);
|
|
1490
|
+
return {
|
|
1491
|
+
columns: s,
|
|
1492
|
+
rows: c,
|
|
1493
|
+
totalElements: Number(e[0] ?? 0),
|
|
1494
|
+
queueWaitMs: r,
|
|
1495
|
+
workerExecMs: Math.round(Q() - n)
|
|
1496
|
+
};
|
|
1497
|
+
} finally {
|
|
1498
|
+
u.finalize();
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
var Me = o({
|
|
1502
|
+
executeSqlScript: (e, t) => je(e, t.script),
|
|
1503
|
+
executePagedQuery: (e, t) => $(e, t)
|
|
1504
|
+
});
|
|
1505
|
+
//#endregion
|
|
1506
|
+
export { Oe as HYDRATION_ENTITY_STATE_SQLITE_REQUEST_HANDLERS, M as OFFLINE_QUEUE_PENDING, N as OFFLINE_QUEUE_PERMANENTLY_FAILED, se as OFFLINE_QUEUE_STATE_SQLITE_REQUEST_HANDLERS, Me as SQL_CONSOLE_SQLITE_REQUEST_HANDLERS, O as createDatabaseOwnerStore, _ as createHydrationContributorRegistry, b as createHydrationController, x as createHydrationEntityClient, C as createHydrationGate, w as createHydrationRequestQueue, E as createHydrationStatus, D as createLocalReflectionQueue, he as createManagedSqliteRuntime, k as createOfflineDataLifecycle, ae as createOfflineQueueCapture, ce as createOfflineQueueClient, le as createOfflineQueueStatus, ie as createOpfsDatabaseFiles, g as createSqlConsoleClient, d as createSqliteEntityBundle, ge as createSqliteEntityClient, p as createSqliteOperationCoordinator, o as createSqliteRequestHandlers, _e as createSqliteTransport, we as createSqliteWorkerRuntime, Te as createSqliteWorkerRuntimeConfig, z as deleteOfflineCommands, a as deleteSqliteRowsByKey, c as dispatchSqliteRequest, L as enqueueOfflineCommand, $ as executePagedQuery, je as executeSqlScript, B as getOfflineQueueSize, V as getOfflineQueueStatusCounts, R as getPendingOfflineCommands, Ee as listHydrationEntityStates, i as listSqliteRows, U as markOfflineCommandsPermanentlyFailed, H as markOfflineCommandsRetryable, s as mergeSqliteRequestHandlers, pe as replayOfflineSyncBatch, r as runSqliteTransaction, De as upsertHydrationEntityState };
|
|
1507
|
+
|
|
1508
|
+
//# sourceMappingURL=index.js.map
|