@moyodb/sdk 0.0.0-alpha.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +10 -2
  3. package/dist/assets/worker-R2dy1Ju5.js +4 -0
  4. package/dist/assets/worker-R2dy1Ju5.js.map +1 -0
  5. package/dist/change-events.d.ts.map +1 -1
  6. package/dist/codec.d.ts.map +1 -1
  7. package/dist/compression.d.ts.map +1 -1
  8. package/dist/engine/README.md +20 -4
  9. package/dist/engine/moyodb_engine.d.ts +115 -54
  10. package/dist/engine/moyodb_engine.js +885 -391
  11. package/dist/engine/moyodb_engine_bg.wasm +0 -0
  12. package/dist/engine/moyodb_engine_bg.wasm.d.ts +55 -47
  13. package/dist/engine/package.json +3 -3
  14. package/dist/engine/snippets/moyodb-engine-8ebe6af0cc2e81b7/js/opfs_shim.js +513 -0
  15. package/dist/engine/snippets/moyodb-engine-cff0a0e2441a7a8f/js/opfs_shim.js +192 -81
  16. package/dist/errors.d.ts +8 -0
  17. package/dist/errors.d.ts.map +1 -1
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/indexing.d.ts.map +1 -1
  20. package/dist/moyodb-sdk.js +1853 -1985
  21. package/dist/moyodb-sdk.js.map +1 -1
  22. package/dist/registry.d.ts +2 -1
  23. package/dist/registry.d.ts.map +1 -1
  24. package/dist/subscriptions.d.ts.map +1 -1
  25. package/dist/types.d.ts +29 -1
  26. package/dist/types.d.ts.map +1 -1
  27. package/dist/worker-api.d.ts +51 -37
  28. package/dist/worker-api.d.ts.map +1 -1
  29. package/dist/worker-client.d.ts +5 -2
  30. package/dist/worker-client.d.ts.map +1 -1
  31. package/dist/worker-protocol.d.ts +14 -3
  32. package/dist/worker-protocol.d.ts.map +1 -1
  33. package/dist/worker-server.d.ts.map +1 -1
  34. package/package.json +74 -72
  35. package/dist/assets/worker-CaOzbBz1.js +0 -2351
  36. package/dist/assets/worker-CaOzbBz1.js.map +0 -1
  37. package/dist/moyodb-sdk.umd.cjs +0 -2
  38. package/dist/moyodb-sdk.umd.cjs.map +0 -1
@@ -1,1988 +1,1856 @@
1
- function v(t) {
2
- return t !== null && typeof t == "object";
3
- }
4
- function L(t, e) {
5
- return t < e ? -1 : t > e ? 1 : 0;
6
- }
7
- function j(t, e, r) {
8
- return new Promise((n) => {
9
- const o = setTimeout(() => n(r), e);
10
- t.then(
11
- (s) => {
12
- clearTimeout(o), n(s);
13
- },
14
- () => {
15
- clearTimeout(o), n(r);
16
- }
17
- );
18
- });
19
- }
20
- const K = 1, Be = "moyodb:worker-protocol:ready", qe = "moyodb:worker-protocol:request", Le = "moyodb:worker-protocol:response", Ce = [
21
- "open",
22
- "close",
23
- "destroy",
24
- "deleteDB",
25
- "begin",
26
- "commit",
27
- "rollback",
28
- "createStore",
29
- "dropStore",
30
- "clearStore",
31
- "get",
32
- "getMany",
33
- "has",
34
- "put",
35
- "putMany",
36
- "delete",
37
- "deleteMany",
38
- "applyBatch",
39
- "scan",
40
- "getByIndex",
41
- "scanByIndex",
42
- "getIndexes",
43
- "reconcileIndexes",
44
- "listStores",
45
- "getVersion",
46
- "changesSince",
47
- "setSchemaVersion",
48
- "exportSnapshot",
49
- "importSnapshot",
50
- "reset",
51
- "compact",
52
- "rebuild",
53
- "stats",
54
- "storageInfo",
55
- "requestPersistence",
56
- "setFailpoint"
57
- ], de = "moyodb:packed-binary-list:v1", Ne = "moyodb:packed-nullable-binary-list:v1", Ue = "moyodb:packed-scan-rows:v1", De = "moyodb:packed-batch-ops:v1", We = 0, ze = 1, T = 4294967295, $e = [];
58
- new Set(Ce);
59
- function Fe(t) {
60
- return v(t) && t.type === Be && t.version === K;
61
- }
62
- function Ve(t) {
63
- return v(t) && t.type === Le && t.version === K && typeof t.id == "number" && Number.isSafeInteger(t.id) && typeof t.ok == "boolean";
64
- }
65
- function je(t) {
66
- const e = new Error(t.message);
67
- return e.name = t.name || t.code || "Error", t.stack && (e.stack = t.stack), t.code && Object.defineProperty(e, "code", {
68
- value: t.code,
69
- enumerable: !0,
70
- configurable: !0
71
- }), e;
72
- }
73
- function y(t, e, r = t) {
74
- const n = new Error(e);
75
- return n.name = t, Object.defineProperty(n, "code", {
76
- value: r,
77
- enumerable: !0,
78
- configurable: !0
79
- }), n;
80
- }
81
- function Ke(t, e) {
82
- if (t === "importSnapshot") {
83
- const r = e[0];
84
- if (r instanceof Uint8Array) {
85
- const n = tt(r);
86
- return {
87
- args: [n],
88
- transfer: [n.buffer]
89
- };
90
- }
91
- }
92
- if (t === "getMany" || t === "deleteMany") {
93
- const [r, n, o] = e;
94
- if (Array.isArray(o) && o.length > 0) {
95
- const s = Je(o);
96
- return {
97
- args: [r, n, s],
98
- transfer: [s.bytes.buffer]
99
- };
100
- }
101
- }
102
- if (t === "putMany") {
103
- const [r, n, o, s] = e;
104
- if (Array.isArray(o) && o.length > 0) {
105
- const i = Ye(o);
106
- return {
107
- args: [r, n, i, s],
108
- transfer: [i.bytes.buffer]
109
- };
110
- }
111
- }
112
- if (t === "applyBatch") {
113
- const [r, n, o] = e;
114
- if (Array.isArray(o) && o.length > 0) {
115
- const s = Qe(o);
116
- return {
117
- args: [r, n, s],
118
- transfer: [s.bytes.buffer]
119
- };
120
- }
121
- }
122
- return { args: e, transfer: $e };
123
- }
124
- function He(t, e) {
125
- return t === "getMany" && Ze(e) ? Ge(e) : (t === "scan" || t === "scanByIndex") && et(e) ? Xe(e) : e;
126
- }
127
- function Ye(t) {
128
- const e = t.length, r = I(e * 2, "packed putMany item count"), n = R(r, 4, "packed putMany metadata");
129
- let o = 0;
130
- for (let c = 0; c < e; c += 1) {
131
- const h = t[c], g = h[0], E = h[1];
132
- if (!(g instanceof Uint8Array) || !(E instanceof Uint8Array))
133
- throw y("WorkerProtocolError", "putMany entry key/value is not a Uint8Array");
134
- const B = I(g.byteLength, "putMany key length"), q = I(E.byteLength, "putMany value length");
135
- if (o += B + q, o > T)
136
- throw y("WorkerProtocolError", "packed putMany payload exceeds 32-bit payload limit");
137
- }
138
- const s = A(4, n, "packed putMany header"), i = A(s, o, "packed putMany total"), d = new Uint8Array(i), u = new DataView(d.buffer);
139
- u.setUint32(0, r, !0);
140
- let l = 4, a = s;
141
- for (let c = 0; c < e; c += 1) {
142
- const h = t[c], g = h[0], E = h[1], B = g.byteLength, q = E.byteLength;
143
- u.setUint32(l, B, !0), l += 4, u.setUint32(l, q, !0), l += 4, d.set(g, a), a += B, d.set(E, a), a += q;
144
- }
145
- return {
146
- __moyodbPacked: de,
147
- bytes: d
148
- };
149
- }
150
- function Je(t) {
151
- const e = t.length, r = R(e, 4, "packed binary-list metadata");
152
- let n = 0;
153
- for (let a = 0; a < e; a += 1) {
154
- const c = t[a];
155
- if (!(c instanceof Uint8Array))
156
- throw y("WorkerProtocolError", "binary-list payload item is not a Uint8Array");
157
- const h = I(c.byteLength, "binary-list item length");
158
- if (n += h, n > T)
159
- throw y("WorkerProtocolError", "packed binary-list payload exceeds 32-bit payload limit");
160
- }
161
- const o = A(4, r, "packed binary-list header"), s = A(o, n, "packed binary-list total"), i = new Uint8Array(s), d = new DataView(i.buffer);
162
- d.setUint32(0, e, !0);
163
- let u = 4, l = o;
164
- for (let a = 0; a < e; a += 1) {
165
- const c = t[a], h = c.byteLength;
166
- d.setUint32(u, h, !0), u += 4, i.set(c, l), l += h;
167
- }
168
- return {
169
- __moyodbPacked: de,
170
- bytes: i
171
- };
172
- }
173
- function Ge(t) {
174
- const e = t.bytes;
175
- if (!(e instanceof Uint8Array) || e.byteLength < 4)
176
- throw y("WorkerProtocolError", "invalid packed nullable binary-list payload");
177
- const r = new DataView(e.buffer, e.byteOffset, e.byteLength), n = r.getUint32(0, !0), o = R(n, 5, "packed nullable binary-list metadata"), s = A(4, o, "packed nullable binary-list header");
178
- if (s > e.byteLength)
179
- throw y("WorkerProtocolError", "packed nullable binary-list metadata exceeds payload length");
180
- const i = new Array(n);
181
- let d = 4, u = s;
182
- for (let l = 0; l < n; l += 1) {
183
- const a = r.getUint8(d), c = r.getUint32(d + 1, !0);
184
- if (d += 5, a === 0) {
185
- if (c !== 0)
186
- throw y(
187
- "WorkerProtocolError",
188
- "packed nullable binary-list null item has a payload length"
189
- );
190
- i[l] = null;
191
- continue;
192
- }
193
- if (a !== 1)
194
- throw y(
195
- "WorkerProtocolError",
196
- `packed nullable binary-list item has invalid presence byte: ${a}`
197
- );
198
- const h = u + c;
199
- if (h > T || h > e.byteLength)
200
- throw y("WorkerProtocolError", "packed nullable binary-list item exceeds payload length");
201
- i[l] = e.subarray(u, h), u = h;
202
- }
203
- if (u !== e.byteLength)
204
- throw y("WorkerProtocolError", "packed nullable binary-list payload has trailing bytes");
205
- return i;
206
- }
207
- function Xe(t) {
208
- const e = t.bytes;
209
- if (!(e instanceof Uint8Array) || e.byteLength < 4)
210
- throw y("WorkerProtocolError", "invalid packed scan row payload");
211
- const r = new DataView(e.buffer, e.byteOffset, e.byteLength), n = r.getUint32(0, !0), o = R(n, 8, "packed scan row metadata"), s = A(4, o, "packed scan row header");
212
- if (s > e.byteLength)
213
- throw y("WorkerProtocolError", "packed scan row metadata exceeds payload length");
214
- const i = new Array(n);
215
- let d = 4, u = s;
216
- for (let l = 0; l < n; l += 1) {
217
- const a = r.getUint32(d, !0), c = r.getUint32(d + 4, !0);
218
- d += 8;
219
- const h = u + a;
220
- if (h > T || h > e.byteLength)
221
- throw y("WorkerProtocolError", "packed scan row key exceeds payload length");
222
- const g = h + c;
223
- if (g > T || g > e.byteLength)
224
- throw y("WorkerProtocolError", "packed scan row value exceeds payload length");
225
- i[l] = {
226
- key: e.subarray(u, h),
227
- value: e.subarray(h, g)
228
- }, u = g;
229
- }
230
- if (u !== e.byteLength)
231
- throw y("WorkerProtocolError", "packed scan row payload has trailing bytes");
232
- return i;
233
- }
234
- function Qe(t) {
235
- const e = t.length, r = R(e, 9, "packed batch metadata");
236
- let n = 0;
237
- for (let a = 0; a < e; a += 1) {
238
- const c = t[a];
239
- if (c.kind !== "put" && c.kind !== "delete")
240
- throw y(
241
- "WorkerProtocolError",
242
- `unknown batch operation kind: ${String(c.kind)}`
243
- );
244
- if (!(c.key instanceof Uint8Array))
245
- throw y("WorkerProtocolError", "batch operation key is not a Uint8Array");
246
- const h = I(c.key.byteLength, "batch operation key length");
247
- if (n += h, n > T)
248
- throw y("WorkerProtocolError", "packed batch payload exceeds 32-bit payload limit");
249
- if (c.kind === "put") {
250
- if (!(c.value instanceof Uint8Array))
251
- throw y("WorkerProtocolError", "batch put value is not a Uint8Array");
252
- const g = I(c.value.byteLength, "batch put value length");
253
- if (n += g, n > T)
254
- throw y("WorkerProtocolError", "packed batch payload exceeds 32-bit payload limit");
255
- }
256
- }
257
- const o = A(4, r, "packed batch header"), s = A(o, n, "packed batch total"), i = new Uint8Array(s), d = new DataView(i.buffer);
258
- d.setUint32(0, e, !0);
259
- let u = 4, l = o;
260
- for (let a = 0; a < e; a += 1) {
261
- const c = t[a], h = c.key, g = h.byteLength, E = c.kind === "put" ? c.value.byteLength : 0;
262
- d.setUint8(u, c.kind === "put" ? ze : We), d.setUint32(u + 1, g, !0), d.setUint32(u + 5, E, !0), u += 9, i.set(h, l), l += g, c.kind === "put" && (i.set(c.value, l), l += E);
263
- }
264
- return {
265
- __moyodbPacked: De,
266
- bytes: i
267
- };
268
- }
269
- function Ze(t) {
270
- return v(t) && t.__moyodbPacked === Ne && t.bytes instanceof Uint8Array;
271
- }
272
- function et(t) {
273
- return v(t) && t.__moyodbPacked === Ue && t.bytes instanceof Uint8Array;
274
- }
275
- function R(t, e, r) {
276
- if (!Number.isSafeInteger(t) || t < 0)
277
- throw y("WorkerProtocolError", `${r} count is invalid: ${t}`);
278
- return I(t * e, r);
279
- }
280
- function A(t, e, r) {
281
- const n = t + e;
282
- return I(n, r);
283
- }
284
- function I(t, e) {
285
- if (!Number.isSafeInteger(t) || t < 0 || t > T)
286
- throw y("WorkerProtocolError", `${e} exceeds 32-bit payload limit`);
287
- return t;
288
- }
289
- function tt(t) {
290
- const e = new Uint8Array(t.byteLength);
291
- return e.set(t), e;
292
- }
293
- function W(t) {
294
- t.timeout && clearTimeout(t.timeout);
295
- }
296
- class rt {
297
- constructor(e, r = {}) {
298
- this.worker = e, this.requestTimeoutMs = r.requestTimeoutMs ?? 0, this.ready = new Promise((n, o) => {
299
- this.readyResolve = n, this.readyReject = o;
300
- }), this.worker.addEventListener("message", this.handleMessage), this.worker.addEventListener("messageerror", this.handleMessageError), this.worker.addEventListener("error", this.handleError);
301
- }
302
- worker;
303
- nextRequestId = 1;
304
- pending = /* @__PURE__ */ new Map();
305
- closed = !1;
306
- readyResolved = !1;
307
- readyResolve = null;
308
- readyReject = null;
309
- ready;
310
- requestTimeoutMs;
311
- fatalHandler = null;
312
- closeReason = null;
313
- setFatalHandler(e) {
314
- this.fatalHandler = e;
315
- }
316
- whenReady() {
317
- return this.ready;
318
- }
319
- dispose(e = y("WorkerTerminatedError", "worker transport was closed")) {
320
- this.disposeInternal(e, !1);
321
- }
322
- open(e) {
323
- return this.request("open", [e]);
324
- }
325
- close() {
326
- return this.request("close", []);
327
- }
328
- destroy() {
329
- return this.request("destroy", []);
330
- }
331
- deleteDB(e) {
332
- return this.request("deleteDB", [e]);
333
- }
334
- begin(e) {
335
- return this.request("begin", [e]);
336
- }
337
- commit(e) {
338
- return this.request("commit", [e]);
339
- }
340
- rollback(e) {
341
- return this.request("rollback", [e]);
342
- }
343
- createStore(e, r, n) {
344
- return this.request("createStore", [e, r, n]);
345
- }
346
- dropStore(e, r) {
347
- return this.request("dropStore", [e, r]);
348
- }
349
- clearStore(e, r) {
350
- return this.request("clearStore", [e, r]);
351
- }
352
- get(e, r, n) {
353
- return this.request("get", [e, r, n]);
354
- }
355
- getMany(e, r, n) {
356
- return this.request("getMany", [e, r, n]);
357
- }
358
- has(e, r, n) {
359
- return this.request("has", [e, r, n]);
360
- }
361
- put(e, r, n, o, s) {
362
- return this.request("put", [e, r, n, o, s]);
363
- }
364
- putMany(e, r, n, o) {
365
- return this.request("putMany", [e, r, n, o]);
366
- }
367
- delete(e, r, n) {
368
- return this.request("delete", [e, r, n]);
369
- }
370
- deleteMany(e, r, n) {
371
- return this.request("deleteMany", [e, r, n]);
372
- }
373
- applyBatch(e, r, n) {
374
- return this.request("applyBatch", [e, r, n]);
375
- }
376
- scan(e, r, n) {
377
- return this.request("scan", [e, r, n]);
378
- }
379
- getByIndex(e, r, n, o) {
380
- return this.request("getByIndex", [e, r, n, o]);
381
- }
382
- scanByIndex(e, r, n, o) {
383
- return this.request("scanByIndex", [e, r, n, o]);
384
- }
385
- getIndexes() {
386
- return this.request("getIndexes", []);
387
- }
388
- reconcileIndexes(e, r) {
389
- return this.request("reconcileIndexes", [e, r]);
390
- }
391
- listStores() {
392
- return this.request("listStores", []);
393
- }
394
- getVersion() {
395
- return this.request("getVersion", []);
396
- }
397
- changesSince(e, r) {
398
- return this.request("changesSince", [e, r]);
399
- }
400
- setSchemaVersion(e, r) {
401
- return this.request("setSchemaVersion", [e, r]);
402
- }
403
- exportSnapshot(e) {
404
- return this.request("exportSnapshot", [e]);
405
- }
406
- importSnapshot(e) {
407
- return this.request("importSnapshot", [e]);
408
- }
409
- reset() {
410
- return this.request("reset", []);
411
- }
412
- compact() {
413
- return this.request("compact", []);
414
- }
415
- rebuild() {
416
- return this.request("rebuild", []);
417
- }
418
- stats() {
419
- return this.request("stats", []);
420
- }
421
- storageInfo() {
422
- return this.request("storageInfo", []);
423
- }
424
- requestPersistence() {
425
- return this.request("requestPersistence", []);
426
- }
427
- setFailpoint(e) {
428
- return this.request("setFailpoint", [e]);
429
- }
430
- async request(e, r) {
431
- this.ensureOpen(), await this.ready, this.ensureOpen();
432
- const n = this.nextRequestId;
433
- this.nextRequestId += 1;
434
- const o = Ke(e, r), s = {
435
- type: qe,
436
- version: K,
437
- id: n,
438
- command: e,
439
- args: o.args
440
- };
441
- return await new Promise((i, d) => {
442
- const u = this.requestTimeoutMs > 0 ? setTimeout(() => {
443
- this.pending.delete(n), d(
444
- y(
445
- "WorkerRequestTimeoutError",
446
- `worker command ${e} timed out after ${this.requestTimeoutMs}ms`
447
- )
448
- );
449
- }, this.requestTimeoutMs) : null;
450
- this.pending.set(n, {
451
- command: e,
452
- resolve: (l) => i(l),
453
- reject: d,
454
- timeout: u
455
- });
456
- try {
457
- o.transfer.length > 0 ? this.worker.postMessage(s, o.transfer) : this.worker.postMessage(s);
458
- } catch (l) {
459
- this.rejectPending(n, l instanceof Error ? l : new Error(String(l)));
460
- }
461
- });
462
- }
463
- ensureOpen() {
464
- if (this.closed)
465
- throw this.closeReason ?? y("WorkerTerminatedError", "worker transport is closed");
466
- }
467
- handleMessage = (e) => {
468
- const r = e.data;
469
- if (Fe(r)) {
470
- this.readyResolved = !0, this.readyResolve?.(), this.readyResolve = null, this.readyReject = null;
471
- return;
472
- }
473
- if (!Ve(r))
474
- return;
475
- const n = this.pending.get(r.id);
476
- if (n) {
477
- if (this.pending.delete(r.id), W(n), r.ok) {
478
- n.resolve(He(n.command, r.result));
479
- return;
480
- }
481
- n.reject(je(r.error));
482
- }
483
- };
484
- handleMessageError = () => {
485
- this.disposeInternal(
486
- y("WorkerMessageError", "worker message could not be deserialized"),
487
- !0
488
- );
489
- };
490
- handleError = (e) => {
491
- const r = e.error instanceof Error ? e.error : y("WorkerError", e.message || "worker runtime failed");
492
- this.disposeInternal(r, !0);
493
- };
494
- rejectPending(e, r) {
495
- const n = this.pending.get(e);
496
- n && (this.pending.delete(e), W(n), n.reject(r));
497
- }
498
- disposeInternal(e, r) {
499
- if (!this.closed) {
500
- this.closed = !0, this.closeReason = e, this.worker.removeEventListener("message", this.handleMessage), this.worker.removeEventListener("messageerror", this.handleMessageError), this.worker.removeEventListener("error", this.handleError), this.readyResolved || this.readyReject?.(e), this.readyResolve = null, this.readyReject = null;
501
- for (const n of this.pending.values())
502
- W(n), n.reject(e);
503
- this.pending.clear(), r && this.fatalHandler?.(e);
504
- }
505
- }
506
- }
507
- class p extends Error {
508
- constructor(e, r) {
509
- super(r), this.name = e, Object.setPrototypeOf(this, new.target.prototype);
510
- }
511
- }
512
- class O extends p {
513
- constructor(e) {
514
- super("UnsupportedPlatformError", e);
515
- }
516
- }
517
- class he extends p {
518
- constructor(e) {
519
- super("DatabaseBusyError", e);
520
- }
521
- }
522
- class nt extends p {
523
- constructor(e) {
524
- super("CorruptionError", e);
525
- }
526
- }
527
- class ot extends p {
528
- constructor(e) {
529
- super("StoreExistsError", e);
530
- }
531
- }
532
- class st extends p {
533
- constructor(e) {
534
- super("StoreNotFoundError", e);
535
- }
536
- }
537
- class it extends p {
538
- constructor(e) {
539
- super("IndexNotFoundError", e);
540
- }
541
- }
542
- class fe extends p {
543
- constructor(e) {
544
- super("ConstraintError", e);
545
- }
546
- }
547
- class at extends fe {
548
- constructor(e) {
549
- super(e), this.name = "UniqueIndexConstraintError";
550
- }
551
- }
552
- class ct extends p {
553
- constructor(e = "write transaction already open") {
554
- super("WriteTransactionAlreadyOpenError", e);
555
- }
556
- }
557
- class ut extends p {
558
- constructor(e = "readonly transaction cannot perform this operation") {
559
- super("ReadonlyTransactionError", e);
560
- }
561
- }
562
- class z extends p {
563
- constructor(e = "transaction is already closed") {
564
- super("TransactionClosedError", e);
565
- }
566
- }
567
- class H extends p {
568
- constructor(e = "database handle is closed") {
569
- super("DatabaseClosedError", e);
570
- }
571
- }
572
- class lt extends p {
573
- constructor(e) {
574
- super("ValueTooLargeError", e);
575
- }
576
- }
577
- class dt extends p {
578
- constructor(e) {
579
- super("KeyTooLargeError", e);
580
- }
581
- }
582
- class ht extends p {
583
- constructor(e) {
584
- super("StoreNameTooLongError", e);
585
- }
586
- }
587
- class ye extends p {
588
- constructor(e) {
589
- super("ReservedStoreNameError", e);
590
- }
591
- }
592
- class ft extends p {
593
- constructor(e) {
594
- super("InvalidRangeError", e);
595
- }
596
- }
597
- class yt extends p {
598
- constructor(e) {
599
- super("StorageError", e);
600
- }
601
- }
602
- class _ extends p {
603
- constructor(e) {
604
- super("SerializationError", e);
605
- }
606
- }
607
- class pt extends p {
608
- constructor(e) {
609
- super("InjectedFailureError", e);
610
- }
611
- }
612
- class bt extends p {
613
- constructor(e) {
614
- super("ChangeFeedCompactedError", e);
615
- }
616
- }
617
- class wt extends p {
618
- constructor(e) {
619
- super("InternalError", e);
620
- }
621
- }
622
- class b extends p {
623
- constructor(e) {
624
- super("InvalidOpenOptionsError", e);
625
- }
626
- }
627
- class C extends p {
628
- constructor(e) {
629
- super("VersionError", e);
630
- }
631
- }
632
- const gt = {
633
- UnsupportedPlatformError: O,
634
- DatabaseBusyError: he,
635
- CorruptionError: nt,
636
- StoreExistsError: ot,
637
- StoreNotFoundError: st,
638
- IndexNotFoundError: it,
639
- ConstraintError: fe,
640
- UniqueIndexConstraintError: at,
641
- WriteTransactionAlreadyOpenError: ct,
642
- ReadonlyTransactionError: ut,
643
- TransactionClosedError: z,
644
- DatabaseClosedError: H,
645
- ValueTooLargeError: lt,
646
- KeyTooLargeError: dt,
647
- StoreNameTooLongError: ht,
648
- ReservedStoreNameError: ye,
649
- InvalidRangeError: ft,
650
- StorageError: yt,
651
- SerializationError: _,
652
- InjectedFailureError: pt,
653
- ChangeFeedCompactedError: bt,
654
- InternalError: wt,
655
- InvalidOpenOptionsError: b,
656
- VersionError: C
657
- };
658
- function Z(t, e) {
659
- const r = gt[t];
660
- if (r)
661
- return new r(e);
662
- const n = new Error(e);
663
- return n.name = t, n;
664
- }
665
- function x(t) {
666
- if (t instanceof p)
667
- return t;
668
- if (t instanceof Error)
669
- return t.name === "Error" ? t : Z(t.name, t.message);
670
- const e = t, r = e?.code ?? e?.name ?? "Error", n = e?.message ?? String(t);
671
- return Z(r, n);
672
- }
673
- const ee = /* @__PURE__ */ new Set([
674
- "after_wal_flush",
675
- "after_main_flush",
676
- "before_superblock_flush"
677
- ]), mt = "moyodb:persistence-bridge:init", kt = "moyodb:persistence-bridge:request", xt = "moyodb:persistence-bridge:response", k = /* @__PURE__ */ new Map();
678
- function Et(t) {
679
- if (t === null || typeof t != "object" || Array.isArray(t))
680
- throw new b("open options must be an object");
681
- }
682
- function te(t, e, r) {
683
- if (t === void 0)
684
- return r;
685
- if (typeof t != "boolean")
686
- throw new b(`${e} must be a boolean`);
687
- return t;
688
- }
689
- function pe(t, e, r) {
690
- if (t === void 0)
691
- return r;
692
- if (typeof t != "number" || !Number.isSafeInteger(t) || t < 0)
693
- throw new b(`${e} must be a non-negative safe integer`);
694
- return t;
695
- }
696
- function St(t) {
697
- return Math.max(1, pe(t, "cachePages", 256));
698
- }
699
- function It(t) {
700
- if (t == null)
701
- return null;
702
- if (typeof t != "string" || !ee.has(t))
703
- throw new b(`debugFailpoint must be one of: ${Array.from(ee).join(", ")}`);
704
- return t;
705
- }
706
- function Pt(t = {}) {
707
- return Et(t), {
708
- createIfMissing: te(t.createIfMissing, "createIfMissing", !0),
709
- ownerWaitMs: pe(t.ownerWaitMs, "ownerWaitMs", 0),
710
- requestPersistence: te(t.requestPersistence, "requestPersistence", !0),
711
- cachePages: St(t.cachePages),
712
- debugFailpoint: It(t.debugFailpoint)
713
- };
714
- }
715
- function Tt(t, e, r) {
716
- if (e.cachePages !== r.cachePages)
717
- throw new b(
718
- `database ${t} is already open in this tab with cachePages=${e.cachePages}; requested cachePages=${r.cachePages}`
719
- );
720
- }
721
- async function At() {
722
- const t = globalThis.navigator?.storage;
723
- return typeof t?.persisted != "function" ? !1 : await j(t.persisted(), 1e3, !1);
724
- }
725
- async function Ot() {
726
- const t = globalThis.navigator?.storage;
727
- return typeof t?.persist != "function" ? !1 : await j(t.persist(), 1e3, !1);
728
- }
729
- async function vt(t, e) {
730
- const r = e.op === "persist" ? await Ot() : await At(), n = {
731
- type: xt,
732
- id: e.id,
733
- granted: r
734
- };
735
- t.postMessage(n);
736
- }
737
- function Mt(t) {
738
- const e = new MessageChannel(), r = e.port1;
739
- return r.addEventListener("message", (n) => {
740
- const o = n.data;
741
- !o || o.type !== kt || vt(r, o);
742
- }), r.start(), t.postMessage({ type: mt }, [e.port2]), {
743
- close() {
744
- r.close();
745
- }
746
- };
747
- }
748
- function be() {
749
- const t = new Worker(new URL(
750
- /* @vite-ignore */
751
- "/assets/worker-CaOzbBz1.js",
752
- import.meta.url
753
- ), { type: "module" }), e = new rt(t), r = Mt(t);
754
- return { worker: t, proxy: e, persistenceBridge: r };
755
- }
756
- function Rt(t, e, r, n, o) {
757
- const s = {
758
- dbName: t,
759
- refs: 1,
760
- worker: e,
761
- proxy: r,
762
- persistenceBridge: n,
763
- options: o,
764
- invalidated: !1,
765
- txInvalidationListeners: /* @__PURE__ */ new Set(),
766
- handleInvalidationListeners: /* @__PURE__ */ new Set()
767
- };
768
- return r.setFatalHandler(() => {
769
- k.get(t) !== s || s.invalidated || (k.delete(t), s.refs = 0, Y(s), s.persistenceBridge.close(), s.worker.terminate());
770
- }), s;
771
- }
772
- function we(t) {
773
- for (const e of Array.from(t))
774
- e();
775
- }
776
- function _t(t, e) {
777
- return t.txInvalidationListeners.add(e), () => {
778
- t.txInvalidationListeners.delete(e);
779
- };
780
- }
781
- function Bt(t, e) {
782
- return t.handleInvalidationListeners.add(e), () => {
783
- t.handleInvalidationListeners.delete(e);
784
- };
785
- }
786
- function N(t) {
787
- we(t.txInvalidationListeners);
788
- }
789
- function Y(t) {
790
- t.invalidated || (t.invalidated = !0, N(t), we(t.handleInvalidationListeners));
791
- }
792
- function qt(t) {
793
- const e = k.get(t);
794
- return e ? (k.delete(t), Y(e), e.refs = 0, e.persistenceBridge.close(), e.proxy.dispose(new Error("worker was terminated by unsafeDebugCrashWorker")), e.worker.terminate(), !0) : !1;
795
- }
796
- async function Lt(t, e = {}) {
797
- const r = Pt(e), n = k.get(t);
798
- if (n)
799
- if (n.invalidated)
800
- k.delete(t);
801
- else {
802
- if (Tt(t, n.options, r), r.debugFailpoint !== null)
803
- try {
804
- await n.proxy.setFailpoint(r.debugFailpoint);
805
- } catch (u) {
806
- throw x(u);
807
- }
808
- return n.refs += 1, n;
809
- }
810
- const { worker: o, proxy: s, persistenceBridge: i } = be(), d = Rt(t, o, s, i, r);
811
- try {
812
- return await s.open({
813
- dbName: t,
814
- options: r
815
- }), k.set(t, d), d;
816
- } catch (u) {
817
- throw i.close(), s.dispose(new Error("worker open failed")), o.terminate(), x(u);
818
- }
819
- }
820
- async function Ct(t) {
821
- if (t.invalidated)
822
- return;
823
- const e = k.get(t.dbName);
824
- if (e && (e.refs -= 1, !(e.refs > 0))) {
825
- k.delete(t.dbName);
826
- try {
827
- await e.proxy.close();
828
- } finally {
829
- e.persistenceBridge.close(), e.proxy.dispose(new Error("worker was released")), e.worker.terminate();
830
- }
831
- }
832
- }
833
- async function ge(t) {
834
- const e = k.get(t.dbName) ?? t;
835
- k.delete(t.dbName), Y(e), e.refs = 0;
836
- try {
837
- await e.proxy.destroy();
838
- } catch (r) {
839
- throw x(r);
840
- } finally {
841
- e.persistenceBridge.close(), e.proxy.dispose(new Error("worker was released")), e.worker.terminate();
842
- }
843
- }
844
- async function Nt(t) {
845
- const e = k.get(t);
846
- if (e) {
847
- await ge(e);
848
- return;
849
- }
850
- const { worker: r, proxy: n, persistenceBridge: o } = be();
851
- try {
852
- await n.deleteDB(t);
853
- } catch (s) {
854
- throw x(s);
855
- } finally {
856
- o.close(), n.dispose(new Error("temporary worker was terminated")), r.terminate();
857
- }
858
- }
859
- const Ut = new TextEncoder(), Dt = new TextDecoder(), Wt = 16, zt = 32, $t = 33, Ft = 48, Vt = 64, jt = 80, M = 0, me = 255, $ = 0, Kt = 0xffffffffffffffffn;
860
- function P(t) {
861
- return Ut.encode(t);
862
- }
863
- function Ht(t) {
864
- return Dt.decode(t);
865
- }
866
- function Mr(t) {
867
- return P(JSON.stringify(t));
868
- }
869
- function Rr(t) {
870
- return JSON.parse(Ht(t));
871
- }
872
- function _r(t) {
873
- const e = typeof t == "bigint" ? t : BigInt(t);
874
- if (e < 0n)
875
- throw new RangeError("u64Key only accepts non-negative values");
876
- const r = new ArrayBuffer(8);
877
- return new DataView(r).setBigUint64(0, e, !1), new Uint8Array(r);
878
- }
879
- function Br(t) {
880
- return Array.isArray(t) ? ke(...t) : J(t);
881
- }
882
- function ke(...t) {
883
- return Ee(t.map((e) => J(e)));
1
+ //#region src/internal.ts
2
+ function e(e) {
3
+ return typeof e == "object" && !!e;
4
+ }
5
+ function t(e, t) {
6
+ return e < t ? -1 : +(e > t);
7
+ }
8
+ function n(e, t, n) {
9
+ return new Promise((r) => {
10
+ let i = setTimeout(() => r(n), t);
11
+ e.then((e) => {
12
+ clearTimeout(i), r(e);
13
+ }, () => {
14
+ clearTimeout(i), r(n);
15
+ });
16
+ });
17
+ }
18
+ var r = /* @__PURE__ */ "open.close.destroy.deleteDB.begin.commit.rollback.autocommit.createStore.dropStore.clearStore.get.getMany.has.put.putMany.delete.deleteMany.applyBatch.scan.getByIndex.scanByIndex.scanByIndexPage.getIndexes.reconcileIndexes.listStores.getVersion.changesSince.setSchemaVersion.exportSnapshot.importSnapshot.reset.compact.rebuild.stats.storageInfo.requestPersistence.setFailpoint".split("."), i = /* @__PURE__ */ new Set([
19
+ "get",
20
+ "getMany",
21
+ "has",
22
+ "put",
23
+ "putMany",
24
+ "delete",
25
+ "deleteMany",
26
+ "applyBatch",
27
+ "scan",
28
+ "getByIndex",
29
+ "scanByIndex",
30
+ "createStore",
31
+ "dropStore",
32
+ "clearStore"
33
+ ]);
34
+ function a(e) {
35
+ return typeof e == "string" && i.has(e);
36
+ }
37
+ var o = "moyodb:packed-binary-list:v1", s = "moyodb:packed-nullable-binary-list:v1", c = "moyodb:packed-scan-rows:v1", l = "moyodb:packed-batch-ops:v1", u = "moyodb:packed-optional-values:v1", d = 0, ee = 1, f = 4294967295, te = [];
38
+ new Set(r);
39
+ function ne(t) {
40
+ return e(t) && t.type === "moyodb:worker-protocol:ready" && t.version === 1;
884
41
  }
885
42
  function re(t) {
886
- return J(t);
887
- }
888
- function S(t) {
889
- return Ee(t);
890
- }
891
- function Yt(t) {
892
- const e = [], r = [];
893
- for (let n = 0; n < t.length; n += 1) {
894
- const o = t[n];
895
- if (o !== M) {
896
- r.push(o);
897
- continue;
898
- }
899
- const s = t[n + 1];
900
- if (s === me) {
901
- r.push(M), n += 1;
902
- continue;
903
- }
904
- if (s === $) {
905
- e.push(Uint8Array.from(r)), r.length = 0, n += 1;
906
- continue;
907
- }
908
- throw new TypeError("invalid compound key encoding");
909
- }
910
- if (r.length !== 0)
911
- throw new TypeError("compound key terminated unexpectedly");
912
- return e;
913
- }
914
- function F(t) {
915
- const e = t.slice();
916
- for (let r = e.length - 1; r >= 0; r -= 1)
917
- if (e[r] !== 255)
918
- return e[r] += 1, e.subarray(0, r + 1);
919
- return null;
920
- }
921
- function xe(t) {
922
- const e = F(t);
923
- return e ? { gte: t, lt: e } : { gte: t };
924
- }
925
- function qr(...t) {
926
- return xe(ke(...t));
927
- }
928
- function J(t) {
929
- if (t === null)
930
- return Uint8Array.of(Wt);
931
- if (typeof t == "boolean")
932
- return Uint8Array.of(t ? $t : zt);
933
- if (typeof t == "number")
934
- return Jt(t);
935
- if (typeof t == "string") {
936
- const e = P(t), r = new Uint8Array(1 + e.length);
937
- return r[0] = Vt, r.set(e, 1), r;
938
- }
939
- if (t instanceof Uint8Array) {
940
- const e = new Uint8Array(1 + t.length);
941
- return e[0] = jt, e.set(t, 1), e;
942
- }
943
- throw new TypeError("compound key parts must be string, number, boolean, null, or Uint8Array");
944
- }
945
- function Ee(t) {
946
- let e = 0;
947
- for (const o of t) {
948
- for (const s of o)
949
- e += s === M ? 2 : 1;
950
- e += 2;
951
- }
952
- const r = new Uint8Array(e);
953
- let n = 0;
954
- for (const o of t) {
955
- for (const s of o)
956
- s === M ? (r[n] = M, r[n + 1] = me, n += 2) : (r[n] = s, n += 1);
957
- r[n] = $, r[n + 1] = $, n += 2;
958
- }
959
- return r;
960
- }
961
- function Jt(t) {
962
- if (!Number.isFinite(t))
963
- throw new TypeError("compound key number parts must be finite");
964
- const e = Object.is(t, -0) ? 0 : t, r = new ArrayBuffer(9), n = new DataView(r);
965
- n.setUint8(0, Ft), n.setFloat64(1, e, !1);
966
- let o = n.getBigUint64(1, !1);
967
- return o = (o & 1n << 63n) !== 0n ? ~o & Kt : o ^ 1n << 63n, n.setBigUint64(1, o, !1), new Uint8Array(r);
968
- }
969
- const Gt = new TextEncoder(), Se = new TextDecoder("utf-8", { fatal: !0 }), U = /* @__PURE__ */ Symbol("moyodb.indexing.missing"), Xt = 0xcbf29ce484222325n, Qt = 0x100000001b3n, Zt = 0xffffffffffffffffn, er = "__browserdb:", ne = "__browserdb:index:", Lr = "__browserdb:indexes";
970
- function tr(t) {
971
- return t.startsWith(er);
972
- }
973
- function w(t) {
974
- if (tr(t))
975
- throw new ye(`reserved store name: ${t}`);
976
- }
977
- function G(t) {
978
- if (t === void 0)
979
- return [];
980
- if (!Array.isArray(t))
981
- throw new b("indexes must be an array");
982
- const e = [], r = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Map();
983
- for (const o of t) {
984
- if (o === null || typeof o != "object" || Array.isArray(o))
985
- throw new b("each index definition must be an object");
986
- const { store: s, name: i, keyPath: d, unique: u } = o;
987
- if (typeof s != "string" || s.length === 0)
988
- throw new b("index store must be a non-empty string");
989
- if (typeof i != "string" || i.length === 0)
990
- throw new b("index name must be a non-empty string");
991
- w(s);
992
- const l = nr(d);
993
- if (u !== void 0 && typeof u != "boolean")
994
- throw new b("index unique must be a boolean");
995
- const a = {
996
- store: s,
997
- name: i,
998
- keyPath: l.paths,
999
- compound: l.compound,
1000
- unique: u ?? !1,
1001
- internalStore: or(s, i)
1002
- }, c = Ie(a.store, a.name);
1003
- if (r.has(c))
1004
- throw new b(`duplicate index definition ${a.store}.${a.name}`);
1005
- r.add(c);
1006
- const h = n.get(a.internalStore);
1007
- if (h && h !== c)
1008
- throw new b(
1009
- `index definitions ${h} and ${c} map to the same internal store ${a.internalStore}`
1010
- );
1011
- n.set(a.internalStore, c), e.push(a);
1012
- }
1013
- return e.sort(rr), e;
1014
- }
1015
- function rr(t, e) {
1016
- const r = L(t.store, e.store);
1017
- if (r !== 0)
1018
- return r;
1019
- const n = L(t.name, e.name);
1020
- if (n !== 0)
1021
- return n;
1022
- const o = Number(t.unique) - Number(e.unique);
1023
- return o !== 0 ? o : L(ie(t), ie(e));
1024
- }
1025
- function Ie(t, e) {
1026
- return `${t}\0${e}`;
1027
- }
1028
- function Cr(t, e, r) {
1029
- return t.find((n) => n.store === e && n.name === r) ?? null;
1030
- }
1031
- function Nr(t, e) {
1032
- return t.filter((r) => r.store === e);
1033
- }
1034
- function Ur(t, e) {
1035
- return S([P(t), P(e)]);
1036
- }
1037
- function Dr(t) {
1038
- return P(JSON.stringify(Te(t)));
1039
- }
1040
- function Wr(t) {
1041
- let e;
1042
- try {
1043
- e = JSON.parse(Se.decode(t));
1044
- } catch (n) {
1045
- throw new _(`invalid persisted index metadata: ${String(n)}`);
1046
- }
1047
- const [r] = G([e]);
1048
- return r;
1049
- }
1050
- function zr(t, e) {
1051
- return S([t, e]);
1052
- }
1053
- function $r(t) {
1054
- const e = Yt(t);
1055
- if (e.length !== 2)
1056
- throw new _("invalid index entry key encoding");
1057
- return {
1058
- logicalKey: e[0],
1059
- primaryKey: e[1]
1060
- };
1061
- }
1062
- function Fr(t) {
1063
- return xe(S([t]));
1064
- }
1065
- function Vr(t = {}) {
1066
- cr(t);
1067
- const e = {};
1068
- if (t.reverse !== void 0 && (e.reverse = t.reverse), t.limit !== void 0 && (e.limit = t.limit), t.gte && (e.gte = S([t.gte])), t.gt) {
1069
- const r = F(S([t.gt]));
1070
- r && (e.gte = r);
1071
- }
1072
- if (t.lt && (e.lt = S([t.lt])), t.lte) {
1073
- const r = F(S([t.lte]));
1074
- r && (e.lt = r);
1075
- }
1076
- return e;
1077
- }
1078
- function jr(t, e) {
1079
- const r = ir(e);
1080
- if (t.compound) {
1081
- const o = [];
1082
- for (const s of t.keyPath) {
1083
- const i = ce(r, s);
1084
- if (i === U)
1085
- return null;
1086
- o.push(ue(i));
1087
- }
1088
- return S(o);
1089
- }
1090
- const n = ce(r, t.keyPath[0]);
1091
- return n === U ? null : ue(n);
1092
- }
1093
- function oe(t) {
1094
- return JSON.stringify(Pe(t));
1095
- }
1096
- function Kr(t) {
1097
- return t.map((e) => ({
1098
- store: e.store,
1099
- name: e.name,
1100
- keyPath: [...e.keyPath],
1101
- compound: e.compound,
1102
- unique: e.unique,
1103
- internalStore: e.internalStore
1104
- }));
1105
- }
1106
- function Pe(t) {
1107
- return t.map(Te);
1108
- }
1109
- function Te(t) {
1110
- return {
1111
- store: t.store,
1112
- name: t.name,
1113
- keyPath: t.compound ? [...t.keyPath] : t.keyPath[0],
1114
- unique: t.unique
1115
- };
1116
- }
1117
- function nr(t) {
1118
- if (typeof t == "string")
1119
- return se(t), { paths: [t], compound: !1 };
1120
- if (!Array.isArray(t) || t.length === 0 || t.some((e) => typeof e != "string"))
1121
- throw new b("index keyPath must be a non-empty string or non-empty string[]");
1122
- for (const e of t)
1123
- se(e);
1124
- return { paths: [...t], compound: !0 };
1125
- }
1126
- function se(t) {
1127
- if (t.length === 0)
1128
- throw new b("index keyPath strings must be non-empty");
1129
- if (t.startsWith(".") || t.endsWith(".") || t.includes(".."))
1130
- throw new b(`invalid index keyPath: ${t}`);
1131
- }
1132
- function ie(t) {
1133
- return t.compound ? `[${t.keyPath.join(",")}]` : t.keyPath[0];
1134
- }
1135
- function or(t, e) {
1136
- const r = ae(P(t)), n = ae(P(e)), o = `${ne}${r}:${n}`;
1137
- if (Gt.encode(o).length <= 255)
1138
- return o;
1139
- const s = P(Ie(t, e));
1140
- return `${ne}h:${sr(s)}`;
1141
- }
1142
- function ae(t) {
1143
- let e = "";
1144
- for (const r of t)
1145
- e += String.fromCharCode(r);
1146
- return btoa(e).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/u, "");
1147
- }
1148
- function sr(t) {
1149
- let e = Xt;
1150
- for (const r of t)
1151
- e ^= BigInt(r), e = e * Qt & Zt;
1152
- return e.toString(16).padStart(16, "0");
1153
- }
1154
- function ir(t) {
1155
- try {
1156
- return JSON.parse(Se.decode(t));
1157
- } catch (e) {
1158
- throw new _(`indexed values must be valid UTF-8 JSON: ${String(e)}`);
1159
- }
1160
- }
1161
- function ce(t, e) {
1162
- let r = t;
1163
- for (const n of e.split(".")) {
1164
- if (r == null)
1165
- return U;
1166
- const o = Object(r);
1167
- if (!(n in o))
1168
- return U;
1169
- r = Reflect.get(o, n);
1170
- }
1171
- return r;
1172
- }
1173
- function ue(t) {
1174
- if (t instanceof Uint8Array || t === null || typeof t == "string" || typeof t == "number" || typeof t == "boolean")
1175
- return re(t);
1176
- throw new _(
1177
- `indexed keyPath values must resolve to string, number, boolean, null, or Uint8Array; got ${ar(t)}`
1178
- );
1179
- }
1180
- function ar(t) {
1181
- return Array.isArray(t) ? "array" : t === null ? "null" : typeof t;
1182
- }
1183
- function cr(t) {
1184
- if (t.gt !== void 0 && t.gte !== void 0)
1185
- throw new TypeError("index range cannot specify both gt and gte");
1186
- if (t.lt !== void 0 && t.lte !== void 0)
1187
- throw new TypeError("index range cannot specify both lt and lte");
1188
- }
1189
- function ur(t) {
1190
- return v(t) && t.key instanceof Uint8Array && (t.kind === "put" || t.kind === "delete");
1191
- }
1192
- function lr(t) {
1193
- return v(t) && typeof t.store == "string" && Array.isArray(t.changes) && t.changes.every(ur);
1194
- }
1195
- function dr(t) {
1196
- return v(t) && t.type === "commit_applied" && typeof t.dbName == "string" && typeof t.txid == "number" && Array.isArray(t.stores) && t.stores.every(lr);
1197
- }
1198
- function le(t) {
1199
- return t.map((e) => ({
1200
- key: e.key.slice(),
1201
- kind: e.kind
1202
- }));
1203
- }
1204
- function hr(t, e) {
1205
- if (e.byteLength > t.byteLength)
1206
- return !1;
1207
- for (let r = 0; r < e.byteLength; r += 1)
1208
- if (t[r] !== e[r])
1209
- return !1;
1210
- return !0;
1211
- }
1212
- function fr(t, e, r) {
1213
- if (typeof t == "function") {
1214
- if (e !== void 0 || r !== void 0)
1215
- throw new TypeError("subscribe(callback) accepts exactly one callback argument");
1216
- return {
1217
- store: null,
1218
- keyPrefix: null,
1219
- callback: t
1220
- };
1221
- }
1222
- if (typeof t != "string")
1223
- throw new TypeError("subscribe() storeName must be a string");
1224
- if (w(t), typeof e == "function") {
1225
- if (r !== void 0)
1226
- throw new TypeError("subscribe(storeName, callback) accepts exactly two arguments");
1227
- return {
1228
- store: t,
1229
- keyPrefix: null,
1230
- callback: e
1231
- };
1232
- }
1233
- if (e instanceof Uint8Array && typeof r == "function")
1234
- return {
1235
- store: t,
1236
- keyPrefix: e.slice(),
1237
- callback: r
1238
- };
1239
- throw new TypeError(
1240
- "subscribe() expects one of: (callback), (storeName, callback), (storeName, keyPrefix, callback)"
1241
- );
1242
- }
1243
- class yr {
1244
- #e;
1245
- #t = null;
1246
- #n = /* @__PURE__ */ new Set();
1247
- #s = !1;
1248
- constructor(e) {
1249
- this.#e = e;
1250
- }
1251
- subscribe(e, r, n) {
1252
- if (this.#s)
1253
- throw new H();
1254
- const o = fr(e, r, n), s = {
1255
- active: !0,
1256
- store: o.store,
1257
- keyPrefix: o.keyPrefix,
1258
- callback: o.callback
1259
- };
1260
- return this.#r(), this.#n.add(s), () => {
1261
- s.active && (s.active = !1, this.#n.delete(s), this.#n.size === 0 && this.#i());
1262
- };
1263
- }
1264
- close() {
1265
- this.#s || (this.#s = !0, this.#n.clear(), this.#i());
1266
- }
1267
- #r() {
1268
- if (this.#t !== null || this.#s)
1269
- return;
1270
- const e = new BroadcastChannel(`db:${this.#e}:events`);
1271
- e.onmessage = (r) => {
1272
- this.#c(r.data);
1273
- }, this.#t = e;
1274
- }
1275
- #i() {
1276
- this.#t?.close(), this.#t = null;
1277
- }
1278
- #c(e) {
1279
- if (!dr(e) || e.dbName !== this.#e || this.#n.size === 0)
1280
- return;
1281
- const r = Array.from(this.#n);
1282
- for (const n of e.stores)
1283
- for (const o of r) {
1284
- if (!o.active || o.store !== null && o.store !== n.store)
1285
- continue;
1286
- let s;
1287
- if (o.keyPrefix === null)
1288
- s = le(n.changes);
1289
- else if (s = le(
1290
- n.changes.filter((i) => hr(i.key, o.keyPrefix))
1291
- ), s.length === 0)
1292
- continue;
1293
- try {
1294
- o.callback(n.store, s, e.txid);
1295
- } catch (i) {
1296
- queueMicrotask(() => {
1297
- throw i;
1298
- });
1299
- }
1300
- }
1301
- }
1302
- }
1303
- async function f(t) {
1304
- try {
1305
- return await t();
1306
- } catch (e) {
1307
- throw x(e);
1308
- }
1309
- }
1310
- class pr {
1311
- mode;
1312
- #e;
1313
- #t;
1314
- #n = !1;
1315
- #s;
1316
- constructor(e, r, n, o) {
1317
- this.#e = e, this.#t = r, this.mode = n, this.#s = o;
1318
- }
1319
- async get(e, r) {
1320
- return this.#r(), w(e), f(() => this.#e.proxy.get(this.#t, e, r));
1321
- }
1322
- async getMany(e, r) {
1323
- return this.#r(), w(e), f(() => this.#e.proxy.getMany(this.#t, e, r));
1324
- }
1325
- async has(e, r) {
1326
- return this.#r(), w(e), f(() => this.#e.proxy.has(this.#t, e, r));
1327
- }
1328
- async put(e, r, n, o = {}) {
1329
- return this.#r(), w(e), f(() => this.#e.proxy.put(this.#t, e, r, n, V(o)));
1330
- }
1331
- async putMany(e, r, n = {}) {
1332
- return this.#r(), w(e), f(() => this.#e.proxy.putMany(this.#t, e, r, V(n)));
1333
- }
1334
- async delete(e, r) {
1335
- return this.#r(), w(e), f(() => this.#e.proxy.delete(this.#t, e, r));
1336
- }
1337
- async deleteMany(e, r) {
1338
- return this.#r(), w(e), f(() => this.#e.proxy.deleteMany(this.#t, e, r));
1339
- }
1340
- async applyBatch(e, r) {
1341
- return this.#r(), w(e), f(() => this.#e.proxy.applyBatch(this.#t, e, r));
1342
- }
1343
- async scan(e, r = {}) {
1344
- return this.#r(), w(e), f(() => this.#e.proxy.scan(this.#t, e, r));
1345
- }
1346
- async getByIndex(e, r, n) {
1347
- return this.#r(), w(e), f(() => this.#e.proxy.getByIndex(this.#t, e, r, n));
1348
- }
1349
- async *scanByIndex(e, r, n = {}) {
1350
- this.#r(), w(e);
1351
- const o = await f(() => this.#e.proxy.scanByIndex(this.#t, e, r, n));
1352
- for (const s of o)
1353
- yield [s.key, s.value];
1354
- }
1355
- async createStore(e, r = {}) {
1356
- return this.#r(), w(e), f(() => this.#e.proxy.createStore(this.#t, e, Re(r)));
1357
- }
1358
- async dropStore(e) {
1359
- return this.#r(), w(e), f(() => this.#e.proxy.dropStore(this.#t, e));
1360
- }
1361
- async clearStore(e) {
1362
- return this.#r(), w(e), f(() => this.#e.proxy.clearStore(this.#t, e));
1363
- }
1364
- async commit() {
1365
- this.#r();
1366
- try {
1367
- await f(() => this.#e.proxy.commit(this.#t));
1368
- } finally {
1369
- this.#i();
1370
- }
1371
- }
1372
- async rollback() {
1373
- this.#r();
1374
- try {
1375
- await f(() => this.#e.proxy.rollback(this.#t));
1376
- } finally {
1377
- this.#i();
1378
- }
1379
- }
1380
- async rollbackIfOpen() {
1381
- if (!this.#n) {
1382
- if (this.#e.invalidated) {
1383
- this.#i();
1384
- return;
1385
- }
1386
- try {
1387
- await this.#e.proxy.rollback(this.#t);
1388
- } catch (e) {
1389
- const r = x(e);
1390
- if (!(r instanceof z))
1391
- throw r;
1392
- } finally {
1393
- this.#i();
1394
- }
1395
- }
1396
- }
1397
- forceClose() {
1398
- this.#i();
1399
- }
1400
- internalId() {
1401
- return this.#t;
1402
- }
1403
- #r() {
1404
- if (this.#n || this.#e.invalidated)
1405
- throw new z();
1406
- }
1407
- #i() {
1408
- this.#n || (this.#n = !0, this.#s());
1409
- }
1410
- }
1411
- class br {
1412
- #e;
1413
- #t = !1;
1414
- #n = /* @__PURE__ */ new Set();
1415
- #s;
1416
- #r = null;
1417
- #i = null;
1418
- constructor(e) {
1419
- this.#e = e, this.#s = new yr(e.dbName), this.#r = _t(e, () => {
1420
- this.#l();
1421
- }), this.#i = Bt(e, () => {
1422
- this.#d();
1423
- });
1424
- }
1425
- async begin(e = "readonly") {
1426
- return this.#c(e);
1427
- }
1428
- async runSchemaMigration(e, r, n, o) {
1429
- this.#o();
1430
- const s = await this.#c("readwrite"), i = new wr(), d = new gr(s, i), u = new mr(this, d, i);
1431
- try {
1432
- await this.#e.proxy.reconcileIndexes(s.internalId(), o), await n({
1433
- db: u,
1434
- oldVersion: e,
1435
- newVersion: r,
1436
- transaction: d
1437
- }), await this.#e.proxy.setSchemaVersion(s.internalId(), r), await s.commit();
1438
- } catch (l) {
1439
- try {
1440
- await s.rollbackIfOpen();
1441
- } catch {
1442
- }
1443
- throw l;
1444
- }
1445
- }
1446
- async listStores() {
1447
- return this.#o(), f(() => this.#e.proxy.listStores());
1448
- }
1449
- async getVersion() {
1450
- return this.#o(), f(() => this.#e.proxy.getVersion());
1451
- }
1452
- async changesSince(e, r = {}) {
1453
- return this.#o(), f(
1454
- () => this.#e.proxy.changesSince(Sr(e), Ir(r))
1455
- );
1456
- }
1457
- async #c(e) {
1458
- return this.#o(), f(async () => {
1459
- const r = await this.#e.proxy.begin(e);
1460
- let n;
1461
- return n = new pr(this.#e, r, e, () => {
1462
- this.#n.delete(n);
1463
- }), this.#n.add(n), n;
1464
- });
1465
- }
1466
- async createStore(e, r = {}) {
1467
- await this.#a("readwrite", async (n) => {
1468
- await n.createStore(e, Re(r));
1469
- });
1470
- }
1471
- async dropStore(e) {
1472
- await this.#a("readwrite", async (r) => {
1473
- await r.dropStore(e);
1474
- });
1475
- }
1476
- async clearStore(e) {
1477
- await this.#a("readwrite", async (r) => {
1478
- await r.clearStore(e);
1479
- });
1480
- }
1481
- async get(e, r) {
1482
- return this.#a("readonly", (n) => n.get(e, r));
1483
- }
1484
- async getMany(e, r) {
1485
- return this.#a("readonly", (n) => n.getMany(e, r));
1486
- }
1487
- async has(e, r) {
1488
- return this.#a("readonly", (n) => n.has(e, r));
1489
- }
1490
- async put(e, r, n, o = {}) {
1491
- await this.#a("readwrite", async (s) => {
1492
- await s.put(e, r, n, V(o));
1493
- });
1494
- }
1495
- async delete(e, r) {
1496
- return this.#a("readwrite", (n) => n.delete(e, r));
1497
- }
1498
- async scan(e, r = {}) {
1499
- return this.#a("readonly", (n) => n.scan(e, r));
1500
- }
1501
- async exportSnapshot(e = {}) {
1502
- return this.#o(), f(async () => {
1503
- const r = await this.#e.proxy.exportSnapshot(Er(e));
1504
- return r instanceof Uint8Array ? r : new Uint8Array(r);
1505
- });
1506
- }
1507
- async importSnapshot(e) {
1508
- return this.#o(), f(() => {
1509
- const r = e instanceof Uint8Array ? e : new Uint8Array(e);
1510
- return this.#e.proxy.importSnapshot(r);
1511
- });
1512
- }
1513
- async reset() {
1514
- return this.#o(), N(this.#e), f(() => this.#e.proxy.reset());
1515
- }
1516
- async compact() {
1517
- return this.#o(), N(this.#e), f(() => this.#e.proxy.compact());
1518
- }
1519
- async rebuild() {
1520
- return this.#o(), N(this.#e), f(() => this.#e.proxy.rebuild());
1521
- }
1522
- async stats() {
1523
- return this.#o(), f(() => this.#e.proxy.stats());
1524
- }
1525
- async storageInfo() {
1526
- return this.#o(), f(() => this.#e.proxy.storageInfo());
1527
- }
1528
- async requestPersistence() {
1529
- return this.#o(), f(() => this.#e.proxy.requestPersistence());
1530
- }
1531
- async destroy() {
1532
- return this.#o(), f(() => ge(this.#e));
1533
- }
1534
- async setFailpoint(e) {
1535
- return this.#o(), f(() => this.#e.proxy.setFailpoint(e));
1536
- }
1537
- subscribe(e, r, n) {
1538
- if (this.#o(), typeof e == "function")
1539
- return this.#s.subscribe(e);
1540
- if (typeof r == "function")
1541
- return this.#s.subscribe(e, r);
1542
- if (r instanceof Uint8Array && typeof n == "function")
1543
- return this.#s.subscribe(e, r, n);
1544
- throw new TypeError(
1545
- "subscribe() expects one of: (callback), (storeName, callback), (storeName, keyPrefix, callback)"
1546
- );
1547
- }
1548
- watch(e, r, n) {
1549
- if (typeof e == "function")
1550
- return this.subscribe(e);
1551
- if (typeof r == "function")
1552
- return this.subscribe(e, r);
1553
- if (r instanceof Uint8Array && typeof n == "function")
1554
- return this.subscribe(e, r, n);
1555
- throw new TypeError(
1556
- "watch() expects one of: (callback), (storeName, callback), (storeName, keyPrefix, callback)"
1557
- );
1558
- }
1559
- async close() {
1560
- if (this.#t)
1561
- return;
1562
- this.#t = !0, this.#u(), this.#s.close();
1563
- let e = null;
1564
- const r = Array.from(this.#n);
1565
- if (r.length > 0) {
1566
- const n = await Promise.allSettled(r.map((o) => o.rollbackIfOpen()));
1567
- for (const o of n)
1568
- o.status === "rejected" && e === null && (e = x(o.reason));
1569
- }
1570
- try {
1571
- await Ct(this.#e);
1572
- } catch (n) {
1573
- throw x(n);
1574
- }
1575
- if (e)
1576
- throw e;
1577
- }
1578
- async #a(e, r) {
1579
- const n = await this.begin(e);
1580
- try {
1581
- const o = await r(n);
1582
- return e === "readwrite" ? await n.commit() : await n.rollback(), o;
1583
- } catch (o) {
1584
- try {
1585
- await n.rollback();
1586
- } catch {
1587
- }
1588
- throw o;
1589
- }
1590
- }
1591
- #u() {
1592
- this.#r?.(), this.#r = null, this.#i?.(), this.#i = null;
1593
- }
1594
- #l() {
1595
- for (const e of Array.from(this.#n))
1596
- e.forceClose();
1597
- }
1598
- #d() {
1599
- this.#t || (this.#t = !0, this.#u(), this.#l(), this.#s.close());
1600
- }
1601
- #o() {
1602
- if (this.#t || this.#e.invalidated)
1603
- throw new H();
1604
- }
1605
- }
1606
- class wr {
1607
- #e = /* @__PURE__ */ new Set();
1608
- #t = /* @__PURE__ */ new Set();
1609
- recordCreate(e) {
1610
- this.#t.delete(e), this.#e.add(e);
1611
- }
1612
- recordDrop(e) {
1613
- this.#e.delete(e) || this.#t.add(e);
1614
- }
1615
- apply(e) {
1616
- const r = new Set(e);
1617
- for (const n of this.#e)
1618
- r.add(n);
1619
- for (const n of this.#t)
1620
- r.delete(n);
1621
- return Array.from(r).sort(L);
1622
- }
1623
- }
1624
- class gr {
1625
- #e;
1626
- #t;
1627
- constructor(e, r) {
1628
- this.#e = e, this.#t = r;
1629
- }
1630
- get mode() {
1631
- return this.#e.mode;
1632
- }
1633
- async get(e, r) {
1634
- return this.#e.get(e, r);
1635
- }
1636
- async getMany(e, r) {
1637
- return this.#e.getMany(e, r);
1638
- }
1639
- async has(e, r) {
1640
- return this.#e.has(e, r);
1641
- }
1642
- async put(e, r, n, o = {}) {
1643
- await this.#e.put(e, r, n, o);
1644
- }
1645
- async putMany(e, r, n = {}) {
1646
- await this.#e.putMany(e, r, n);
1647
- }
1648
- async delete(e, r) {
1649
- return this.#e.delete(e, r);
1650
- }
1651
- async deleteMany(e, r) {
1652
- await this.#e.deleteMany(e, r);
1653
- }
1654
- async applyBatch(e, r) {
1655
- await this.#e.applyBatch(e, r);
1656
- }
1657
- async scan(e, r = {}) {
1658
- return this.#e.scan(e, r);
1659
- }
1660
- async getByIndex(e, r, n) {
1661
- return this.#e.getByIndex(e, r, n);
1662
- }
1663
- scanByIndex(e, r, n = {}) {
1664
- return this.#e.scanByIndex(e, r, n);
1665
- }
1666
- async createStore(e, r = {}) {
1667
- await this.#e.createStore(e, r), this.#t.recordCreate(e);
1668
- }
1669
- async dropStore(e) {
1670
- await this.#e.dropStore(e), this.#t.recordDrop(e);
1671
- }
1672
- async clearStore(e) {
1673
- await this.#e.clearStore(e);
1674
- }
1675
- async commit() {
1676
- throw m("transaction.commit");
1677
- }
1678
- async rollback() {
1679
- throw m("transaction.rollback");
1680
- }
1681
- }
1682
- class mr {
1683
- #e;
1684
- #t;
1685
- #n;
1686
- constructor(e, r, n) {
1687
- this.#e = e, this.#t = r, this.#n = n;
1688
- }
1689
- async begin(e = "readonly") {
1690
- throw m("db.begin");
1691
- }
1692
- async createStore(e, r = {}) {
1693
- await this.#t.createStore(e, r);
1694
- }
1695
- async dropStore(e) {
1696
- await this.#t.dropStore(e);
1697
- }
1698
- async clearStore(e) {
1699
- await this.#t.clearStore(e);
1700
- }
1701
- async listStores() {
1702
- return this.#n.apply(await this.#e.listStores());
1703
- }
1704
- async getVersion() {
1705
- return this.#e.getVersion();
1706
- }
1707
- async changesSince(e, r = {}) {
1708
- throw m("db.changesSince");
1709
- }
1710
- async get(e, r) {
1711
- return this.#t.get(e, r);
1712
- }
1713
- async getMany(e, r) {
1714
- return this.#t.getMany(e, r);
1715
- }
1716
- async has(e, r) {
1717
- return this.#t.has(e, r);
1718
- }
1719
- async put(e, r, n, o = {}) {
1720
- await this.#t.put(e, r, n, o);
1721
- }
1722
- async delete(e, r) {
1723
- return this.#t.delete(e, r);
1724
- }
1725
- async scan(e, r = {}) {
1726
- return this.#t.scan(e, r);
1727
- }
1728
- async exportSnapshot(e = {}) {
1729
- throw m("db.exportSnapshot");
1730
- }
1731
- async importSnapshot(e) {
1732
- throw m("db.importSnapshot");
1733
- }
1734
- async reset() {
1735
- throw m("db.reset");
1736
- }
1737
- async compact() {
1738
- throw m("db.compact");
1739
- }
1740
- async rebuild() {
1741
- throw m("db.rebuild");
1742
- }
1743
- async stats() {
1744
- return this.#e.stats();
1745
- }
1746
- async storageInfo() {
1747
- return this.#e.storageInfo();
1748
- }
1749
- async requestPersistence() {
1750
- return this.#e.requestPersistence();
1751
- }
1752
- async close() {
1753
- throw m("db.close");
1754
- }
1755
- async destroy() {
1756
- throw m("db.destroy");
1757
- }
1758
- async setFailpoint(e) {
1759
- throw m("db.setFailpoint");
1760
- }
1761
- subscribe(e, r, n) {
1762
- throw m("db.subscribe");
1763
- }
1764
- watch(e, r, n) {
1765
- throw m("db.watch");
1766
- }
1767
- }
1768
- function m(t) {
1769
- const e = new Error(
1770
- `${t}() is not available inside openDB() migrations; use the provided migration transaction and wait for openDB() to resolve`
1771
- );
1772
- return e.name = "InvalidStateError", e;
1773
- }
1774
- function X(t, e) {
1775
- if (typeof t != "string" || t.length === 0)
1776
- throw new TypeError(`${e}() database name must be a non-empty string`);
1777
- return t;
1778
- }
1779
- const Ae = (t) => new TypeError(t), Oe = (t) => new b(t);
1780
- function D(t, e, r = Ae) {
1781
- if (t === void 0)
1782
- return {};
1783
- if (t === null || typeof t != "object" || Array.isArray(t))
1784
- throw r(e);
1785
- return t;
1786
- }
1787
- function ve(t, e, r = Ae) {
1788
- if (typeof t != "number" || !Number.isSafeInteger(t) || t < 0)
1789
- throw r(e);
1790
- return t;
1791
- }
1792
- function Q(t, e, r) {
1793
- return t === void 0 ? void 0 : ve(t, e, r);
1794
- }
1795
- function kr(t) {
1796
- return D(t, "open options must be an object", Oe);
1797
- }
1798
- function xr(t) {
1799
- return Q(
1800
- t,
1801
- "version must be a non-negative safe integer",
1802
- Oe
1803
- );
1804
- }
1805
- function V(t) {
1806
- const { ttl: e } = D(t, "put options must be an object"), r = Q(e, "ttl must be a non-negative safe integer");
1807
- return r === void 0 ? {} : { ttl: r };
1808
- }
1809
- function Me(t, e) {
1810
- const { compression: r } = D(t, e);
1811
- if (r === void 0)
1812
- return {};
1813
- if (r !== !1 && r !== "gzip" && r !== "deflate")
1814
- throw new TypeError('compression must be "gzip", "deflate", or false');
1815
- return { compression: r };
1816
- }
1817
- function Re(t) {
1818
- return Me(t, "createStore options must be an object");
1819
- }
1820
- function Er(t) {
1821
- return Me(t, "exportSnapshot options must be an object");
1822
- }
1823
- function Sr(t) {
1824
- return ve(t, "txid must be a non-negative safe integer");
1825
- }
1826
- function Ir(t) {
1827
- const { stores: e, limit: r } = D(t, "change feed options must be an object"), n = {};
1828
- if (e !== void 0) {
1829
- if (!Array.isArray(e) || e.some((s) => typeof s != "string"))
1830
- throw new TypeError("stores must be an array of strings");
1831
- for (const s of e)
1832
- w(s);
1833
- n.stores = [...e];
1834
- }
1835
- const o = Q(r, "limit must be a non-negative safe integer");
1836
- return o !== void 0 && (n.limit = o), n;
1837
- }
1838
- function Pr(t) {
1839
- if (t !== void 0) {
1840
- if (typeof t != "function")
1841
- throw new b("migrate must be a function");
1842
- return t;
1843
- }
1844
- }
1845
- function Tr(t) {
1846
- if (t !== void 0)
1847
- return G(t);
1848
- }
1849
- function Ar(t, e) {
1850
- return oe(t) === oe(e);
1851
- }
1852
- async function Or(t) {
1853
- return G(await t.proxy.getIndexes());
1854
- }
1855
- function _e() {
1856
- if (!globalThis.isSecureContext)
1857
- throw new O("moyodb requires a secure context (HTTPS)");
1858
- const t = globalThis.navigator;
1859
- if (!t?.storage)
1860
- throw new O("navigator.storage is unavailable");
1861
- if (typeof t.storage.getDirectory != "function")
1862
- throw new O("navigator.storage.getDirectory is unavailable");
1863
- if (typeof globalThis.Worker != "function")
1864
- throw new O("Worker is unavailable");
1865
- if (typeof globalThis.BroadcastChannel > "u")
1866
- throw new O("BroadcastChannel is unavailable");
1867
- if (!t.locks)
1868
- throw new O("navigator.locks is unavailable");
1869
- }
1870
- async function vr(t) {
1871
- const e = globalThis.navigator;
1872
- (t.requestPersistence ?? !0) && typeof e?.storage?.persist == "function" && await j(e.storage.persist(), 1e3, !1);
1873
- }
1874
- async function Hr(t, e = {}) {
1875
- const r = X(t, "openDB"), n = kr(e), o = xr(n.version), s = Pr(n.migrate), i = Tr(n.indexes);
1876
- if (o === void 0 && s !== void 0)
1877
- throw new b("migrate requires version");
1878
- if (o === void 0 && i !== void 0)
1879
- throw new b("indexes requires version");
1880
- _e(), await vr(n);
1881
- const d = await Lt(r, n), u = new br(d);
1882
- let l = !1;
1883
- try {
1884
- if (o === void 0)
1885
- return l = !0, u;
1886
- const [a, c] = await Promise.all([u.getVersion(), Or(d)]);
1887
- if (o < a)
1888
- throw new C(
1889
- `cannot open database ${r} at schema version ${o}; current schema version is ${a}`
1890
- );
1891
- if (o === a) {
1892
- if (i !== void 0 && !Ar(i, c))
1893
- throw new C(
1894
- `database ${r} is already at schema version ${a}, but the requested index catalog does not match the committed schema`
1895
- );
1896
- return l = !0, u;
1897
- }
1898
- if (d.refs > 1)
1899
- throw new he(
1900
- `database ${r} is already open in this tab; close existing handles before migrating from version ${a} to ${o}`
1901
- );
1902
- if (!s)
1903
- throw new C(
1904
- `database ${r} requires migration from version ${a} to ${o}, but no migrate hook was provided`
1905
- );
1906
- const h = i ?? c;
1907
- return await u.runSchemaMigration(a, o, s, Pe(h)), l = !0, u;
1908
- } catch (a) {
1909
- if (!l)
1910
- try {
1911
- await u.close();
1912
- } catch {
1913
- }
1914
- throw x(a);
1915
- }
1916
- }
1917
- async function Yr(t) {
1918
- const e = X(t, "deleteDB");
1919
- _e(), await Nt(e);
1920
- }
1921
- function Jr(t) {
1922
- const e = X(t, "unsafeDebugCrashWorker");
1923
- return qt(e);
1924
- }
1925
- export {
1926
- bt as ChangeFeedCompactedError,
1927
- fe as ConstraintError,
1928
- nt as CorruptionError,
1929
- he as DatabaseBusyError,
1930
- H as DatabaseClosedError,
1931
- Lr as INDEX_METADATA_STORE,
1932
- it as IndexNotFoundError,
1933
- pt as InjectedFailureError,
1934
- wt as InternalError,
1935
- b as InvalidOpenOptionsError,
1936
- ft as InvalidRangeError,
1937
- dt as KeyTooLargeError,
1938
- p as MoyoDbError,
1939
- ut as ReadonlyTransactionError,
1940
- ye as ReservedStoreNameError,
1941
- _ as SerializationError,
1942
- yt as StorageError,
1943
- ot as StoreExistsError,
1944
- ht as StoreNameTooLongError,
1945
- st as StoreNotFoundError,
1946
- z as TransactionClosedError,
1947
- at as UniqueIndexConstraintError,
1948
- O as UnsupportedPlatformError,
1949
- lt as ValueTooLargeError,
1950
- C as VersionError,
1951
- ct as WriteTransactionAlreadyOpenError,
1952
- w as assertPublicStoreName,
1953
- Kr as cloneNormalizedIndexDefinitions,
1954
- rr as compareNormalizedIndexDefinitions,
1955
- ke as compoundKey,
1956
- qr as compoundKeyRange,
1957
- $r as decodeIndexEntryKey,
1958
- Wr as decodeIndexMetadataValue,
1959
- Yr as deleteDB,
1960
- S as encodeCompoundKeyParts,
1961
- zr as encodeIndexEntryKey,
1962
- Ur as encodeIndexMetadataKey,
1963
- Dr as encodeIndexMetadataValue,
1964
- re as encodeIndexScalar,
1965
- jr as extractLogicalIndexKey,
1966
- Cr as findIndexDefinition,
1967
- Ie as indexDefinitionIdentity,
1968
- Br as indexKey,
1969
- Fr as indexKeyExactRange,
1970
- Vr as indexRangeToPhysicalRange,
1971
- Nr as indexesForStore,
1972
- tr as isInternalStoreName,
1973
- Rr as jsonDecode,
1974
- Mr as jsonEncode,
1975
- x as normalizeError,
1976
- G as normalizeIndexDefinitions,
1977
- Hr as openDB,
1978
- xe as prefixRange,
1979
- F as prefixSuccessor,
1980
- oe as serializeNormalizedIndexDefinitions,
1981
- Yt as splitCompoundKey,
1982
- Pe as toPublicIndexDefinitions,
1983
- _r as u64Key,
1984
- Jr as unsafeDebugCrashWorker,
1985
- Ht as utf8Decode,
1986
- P as utf8Encode
43
+ return e(t) && t.type === "moyodb:worker-protocol:response" && t.version === 1 && typeof t.id == "number" && Number.isSafeInteger(t.id) && typeof t.ok == "boolean";
44
+ }
45
+ function ie(e) {
46
+ let t = Error(e.message);
47
+ return t.name = e.name || e.code || "Error", e.stack && (t.stack = e.stack), e.code && Object.defineProperty(t, "code", {
48
+ value: e.code,
49
+ enumerable: !0,
50
+ configurable: !0
51
+ }), t;
52
+ }
53
+ function p(e, t, n = e) {
54
+ let r = Error(t);
55
+ return r.name = e, Object.defineProperty(r, "code", {
56
+ value: n,
57
+ enumerable: !0,
58
+ configurable: !0
59
+ }), r;
60
+ }
61
+ function ae(e, t) {
62
+ if (e === "autocommit") {
63
+ let [e, n, r] = t;
64
+ if (a(n) && Array.isArray(r)) {
65
+ let t = ae(n, [0, ...r]);
66
+ return {
67
+ args: [
68
+ e,
69
+ n,
70
+ t.args.slice(1)
71
+ ],
72
+ transfer: t.transfer
73
+ };
74
+ }
75
+ }
76
+ if (e === "importSnapshot") {
77
+ let e = t[0];
78
+ if (e instanceof Uint8Array) {
79
+ let t = ge(e);
80
+ return {
81
+ args: [t],
82
+ transfer: [t.buffer]
83
+ };
84
+ }
85
+ }
86
+ if (e === "getMany" || e === "deleteMany") {
87
+ let [e, n, r] = t;
88
+ if (Array.isArray(r) && r.length > 0) {
89
+ let t = le(r);
90
+ return {
91
+ args: [
92
+ e,
93
+ n,
94
+ t
95
+ ],
96
+ transfer: [t.bytes.buffer]
97
+ };
98
+ }
99
+ }
100
+ if (e === "putMany") {
101
+ let [e, n, r, i] = t;
102
+ if (Array.isArray(r) && r.length > 0) {
103
+ let t = ce(r);
104
+ return {
105
+ args: [
106
+ e,
107
+ n,
108
+ t,
109
+ i
110
+ ],
111
+ transfer: [t.bytes.buffer]
112
+ };
113
+ }
114
+ }
115
+ if (e === "applyBatch") {
116
+ let [e, n, r] = t;
117
+ if (Array.isArray(r) && r.length > 0) {
118
+ let t = fe(r);
119
+ return {
120
+ args: [
121
+ e,
122
+ n,
123
+ t
124
+ ],
125
+ transfer: [t.bytes.buffer]
126
+ };
127
+ }
128
+ }
129
+ return {
130
+ args: t,
131
+ transfer: te
132
+ };
133
+ }
134
+ function oe(e) {
135
+ if (!(e instanceof Uint8Array) || e.byteLength < 4) throw p("WorkerProtocolError", "invalid packed optional-values payload");
136
+ let t = new DataView(e.buffer, e.byteOffset, e.byteLength), n = t.getUint32(0, !0), r = h(4, m(n, 4, "packed optional-values metadata"), "packed optional-values header");
137
+ if (r > e.byteLength) throw p("WorkerProtocolError", "packed optional-values metadata exceeds payload length");
138
+ let i = Array(n), a = r;
139
+ for (let r = 0; r < n; r += 1) {
140
+ let n = t.getUint32(4 + r * 4, !0);
141
+ if (n === f) {
142
+ i[r] = null;
143
+ continue;
144
+ }
145
+ let o = a + n;
146
+ if (o > e.byteLength) throw p("WorkerProtocolError", "packed optional-values item exceeds payload length");
147
+ i[r] = e.subarray(a, o), a = o;
148
+ }
149
+ if (a !== e.byteLength) throw p("WorkerProtocolError", "packed optional-values payload has trailing bytes");
150
+ return i;
151
+ }
152
+ function se(t, n) {
153
+ return t === "getMany" && pe(n) ? ue(n) : t === "getMany" && he(n) ? oe(n.bytes) : t === "scanByIndexPage" && e(n) ? {
154
+ rows: me(n.rows) ? de(n.rows) : n.rows,
155
+ cursor: n.cursor instanceof Uint8Array ? n.cursor : null
156
+ } : (t === "scan" || t === "scanByIndex") && me(n) ? de(n) : n;
157
+ }
158
+ function ce(e) {
159
+ let t = e.length, n = g(t * 2, "packed putMany item count"), r = m(n, 4, "packed putMany metadata"), i = 0;
160
+ for (let n = 0; n < t; n += 1) {
161
+ let t = e[n], r = t[0], a = t[1];
162
+ if (!(r instanceof Uint8Array) || !(a instanceof Uint8Array)) throw p("WorkerProtocolError", "putMany entry key/value is not a Uint8Array");
163
+ let o = g(r.byteLength, "putMany key length"), s = g(a.byteLength, "putMany value length");
164
+ if (i += o + s, i > f) throw p("WorkerProtocolError", "packed putMany payload exceeds 32-bit payload limit");
165
+ }
166
+ let a = h(4, r, "packed putMany header"), s = h(a, i, "packed putMany total"), c = new Uint8Array(s), l = new DataView(c.buffer);
167
+ l.setUint32(0, n, !0);
168
+ let u = 4, d = a;
169
+ for (let n = 0; n < t; n += 1) {
170
+ let t = e[n], r = t[0], i = t[1], a = r.byteLength, o = i.byteLength;
171
+ l.setUint32(u, a, !0), u += 4, l.setUint32(u, o, !0), u += 4, c.set(r, d), d += a, c.set(i, d), d += o;
172
+ }
173
+ return {
174
+ __moyodbPacked: o,
175
+ bytes: c
176
+ };
177
+ }
178
+ function le(e) {
179
+ let t = e.length, n = m(t, 4, "packed binary-list metadata"), r = 0;
180
+ for (let n = 0; n < t; n += 1) {
181
+ let t = e[n];
182
+ if (!(t instanceof Uint8Array)) throw p("WorkerProtocolError", "binary-list payload item is not a Uint8Array");
183
+ let i = g(t.byteLength, "binary-list item length");
184
+ if (r += i, r > f) throw p("WorkerProtocolError", "packed binary-list payload exceeds 32-bit payload limit");
185
+ }
186
+ let i = h(4, n, "packed binary-list header"), a = h(i, r, "packed binary-list total"), s = new Uint8Array(a), c = new DataView(s.buffer);
187
+ c.setUint32(0, t, !0);
188
+ let l = 4, u = i;
189
+ for (let n = 0; n < t; n += 1) {
190
+ let t = e[n], r = t.byteLength;
191
+ c.setUint32(l, r, !0), l += 4, s.set(t, u), u += r;
192
+ }
193
+ return {
194
+ __moyodbPacked: o,
195
+ bytes: s
196
+ };
197
+ }
198
+ function ue(e) {
199
+ let t = e.bytes;
200
+ if (!(t instanceof Uint8Array) || t.byteLength < 4) throw p("WorkerProtocolError", "invalid packed nullable binary-list payload");
201
+ let n = new DataView(t.buffer, t.byteOffset, t.byteLength), r = n.getUint32(0, !0), i = h(4, m(r, 5, "packed nullable binary-list metadata"), "packed nullable binary-list header");
202
+ if (i > t.byteLength) throw p("WorkerProtocolError", "packed nullable binary-list metadata exceeds payload length");
203
+ let a = Array(r), o = 4, s = i;
204
+ for (let e = 0; e < r; e += 1) {
205
+ let r = n.getUint8(o), i = n.getUint32(o + 1, !0);
206
+ if (o += 5, r === 0) {
207
+ if (i !== 0) throw p("WorkerProtocolError", "packed nullable binary-list null item has a payload length");
208
+ a[e] = null;
209
+ continue;
210
+ }
211
+ if (r !== 1) throw p("WorkerProtocolError", `packed nullable binary-list item has invalid presence byte: ${r}`);
212
+ let c = s + i;
213
+ if (c > f || c > t.byteLength) throw p("WorkerProtocolError", "packed nullable binary-list item exceeds payload length");
214
+ a[e] = t.subarray(s, c), s = c;
215
+ }
216
+ if (s !== t.byteLength) throw p("WorkerProtocolError", "packed nullable binary-list payload has trailing bytes");
217
+ return a;
218
+ }
219
+ function de(e) {
220
+ let t = e.bytes;
221
+ if (!(t instanceof Uint8Array) || t.byteLength < 4) throw p("WorkerProtocolError", "invalid packed scan row payload");
222
+ let n = new DataView(t.buffer, t.byteOffset, t.byteLength), r = n.getUint32(0, !0), i = h(4, m(r, 8, "packed scan row metadata"), "packed scan row header");
223
+ if (i > t.byteLength) throw p("WorkerProtocolError", "packed scan row metadata exceeds payload length");
224
+ let a = Array(r), o = 4, s = i;
225
+ for (let e = 0; e < r; e += 1) {
226
+ let r = n.getUint32(o, !0), i = n.getUint32(o + 4, !0);
227
+ o += 8;
228
+ let c = s + r;
229
+ if (c > f || c > t.byteLength) throw p("WorkerProtocolError", "packed scan row key exceeds payload length");
230
+ let l = c + i;
231
+ if (l > f || l > t.byteLength) throw p("WorkerProtocolError", "packed scan row value exceeds payload length");
232
+ a[e] = {
233
+ key: t.subarray(s, c),
234
+ value: t.subarray(c, l)
235
+ }, s = l;
236
+ }
237
+ if (s !== t.byteLength) throw p("WorkerProtocolError", "packed scan row payload has trailing bytes");
238
+ return a;
239
+ }
240
+ function fe(e) {
241
+ let t = e.length, n = m(t, 9, "packed batch metadata"), r = 0;
242
+ for (let n = 0; n < t; n += 1) {
243
+ let t = e[n];
244
+ if (!(t.key instanceof Uint8Array)) throw p("WorkerProtocolError", "batch operation key is not a Uint8Array");
245
+ let i = g(t.key.byteLength, "batch operation key length");
246
+ if (r += i, r > f) throw p("WorkerProtocolError", "packed batch payload exceeds 32-bit payload limit");
247
+ if (t.kind === "put") {
248
+ if (!(t.value instanceof Uint8Array)) throw p("WorkerProtocolError", "batch put value is not a Uint8Array");
249
+ let e = g(t.value.byteLength, "batch put value length");
250
+ if (r += e, r > f) throw p("WorkerProtocolError", "packed batch payload exceeds 32-bit payload limit");
251
+ }
252
+ }
253
+ let i = h(4, n, "packed batch header"), a = h(i, r, "packed batch total"), o = new Uint8Array(a), s = new DataView(o.buffer);
254
+ s.setUint32(0, t, !0);
255
+ let c = 4, u = i;
256
+ for (let n = 0; n < t; n += 1) {
257
+ let t = e[n], r = t.key, i = r.byteLength, a = t.kind === "put" ? t.value.byteLength : 0;
258
+ s.setUint8(c, t.kind === "put" ? ee : d), s.setUint32(c + 1, i, !0), s.setUint32(c + 5, a, !0), c += 9, o.set(r, u), u += i, t.kind === "put" && (o.set(t.value, u), u += a);
259
+ }
260
+ return {
261
+ __moyodbPacked: l,
262
+ bytes: o
263
+ };
264
+ }
265
+ function pe(t) {
266
+ return e(t) && t.__moyodbPacked === s && t.bytes instanceof Uint8Array;
267
+ }
268
+ function me(t) {
269
+ return e(t) && t.__moyodbPacked === c && t.bytes instanceof Uint8Array;
270
+ }
271
+ function he(t) {
272
+ return e(t) && t.__moyodbPacked === u && t.bytes instanceof Uint8Array;
273
+ }
274
+ function m(e, t, n) {
275
+ if (!Number.isSafeInteger(e) || e < 0) throw p("WorkerProtocolError", `${n} count is invalid: ${e}`);
276
+ return g(e * t, n);
277
+ }
278
+ function h(e, t, n) {
279
+ return g(e + t, n);
280
+ }
281
+ function g(e, t) {
282
+ if (!Number.isSafeInteger(e) || e < 0 || e > f) throw p("WorkerProtocolError", `${t} exceeds 32-bit payload limit`);
283
+ return e;
284
+ }
285
+ function ge(e) {
286
+ let t = new Uint8Array(e.byteLength);
287
+ return t.set(e), t;
288
+ }
289
+ //#endregion
290
+ //#region src/worker-client.ts
291
+ function _(e) {
292
+ e.timeout && clearTimeout(e.timeout);
293
+ }
294
+ var _e = class {
295
+ worker;
296
+ nextRequestId = 1;
297
+ pending = /* @__PURE__ */ new Map();
298
+ closed = !1;
299
+ readyResolved = !1;
300
+ readyResolve = null;
301
+ readyReject = null;
302
+ ready;
303
+ requestTimeoutMs;
304
+ fatalHandler = null;
305
+ closeReason = null;
306
+ constructor(e, t = {}) {
307
+ this.worker = e, this.requestTimeoutMs = t.requestTimeoutMs ?? 0, this.ready = new Promise((e, t) => {
308
+ this.readyResolve = e, this.readyReject = t;
309
+ }), this.worker.addEventListener("message", this.handleMessage), this.worker.addEventListener("messageerror", this.handleMessageError), this.worker.addEventListener("error", this.handleError);
310
+ }
311
+ setFatalHandler(e) {
312
+ this.fatalHandler = e;
313
+ }
314
+ whenReady() {
315
+ return this.ready;
316
+ }
317
+ dispose(e = p("WorkerTerminatedError", "worker transport was closed")) {
318
+ this.disposeInternal(e, !1);
319
+ }
320
+ open(e) {
321
+ return this.request("open", [e]);
322
+ }
323
+ close() {
324
+ return this.request("close", []);
325
+ }
326
+ destroy() {
327
+ return this.request("destroy", []);
328
+ }
329
+ deleteDB(e) {
330
+ return this.request("deleteDB", [e]);
331
+ }
332
+ begin(e) {
333
+ return this.request("begin", [e]);
334
+ }
335
+ commit(e) {
336
+ return this.request("commit", [e]);
337
+ }
338
+ rollback(e) {
339
+ return this.request("rollback", [e]);
340
+ }
341
+ autocommit(e, t, n) {
342
+ return this.send("autocommit", [
343
+ e,
344
+ t,
345
+ n
346
+ ], t);
347
+ }
348
+ createStore(e, t, n) {
349
+ return this.request("createStore", [
350
+ e,
351
+ t,
352
+ n
353
+ ]);
354
+ }
355
+ dropStore(e, t) {
356
+ return this.request("dropStore", [e, t]);
357
+ }
358
+ clearStore(e, t) {
359
+ return this.request("clearStore", [e, t]);
360
+ }
361
+ get(e, t, n) {
362
+ return this.request("get", [
363
+ e,
364
+ t,
365
+ n
366
+ ]);
367
+ }
368
+ getMany(e, t, n) {
369
+ return this.request("getMany", [
370
+ e,
371
+ t,
372
+ n
373
+ ]);
374
+ }
375
+ has(e, t, n) {
376
+ return this.request("has", [
377
+ e,
378
+ t,
379
+ n
380
+ ]);
381
+ }
382
+ put(e, t, n, r, i) {
383
+ return this.request("put", [
384
+ e,
385
+ t,
386
+ n,
387
+ r,
388
+ i
389
+ ]);
390
+ }
391
+ putMany(e, t, n, r) {
392
+ return this.request("putMany", [
393
+ e,
394
+ t,
395
+ n,
396
+ r
397
+ ]);
398
+ }
399
+ delete(e, t, n) {
400
+ return this.request("delete", [
401
+ e,
402
+ t,
403
+ n
404
+ ]);
405
+ }
406
+ deleteMany(e, t, n) {
407
+ return this.request("deleteMany", [
408
+ e,
409
+ t,
410
+ n
411
+ ]);
412
+ }
413
+ applyBatch(e, t, n) {
414
+ return this.request("applyBatch", [
415
+ e,
416
+ t,
417
+ n
418
+ ]);
419
+ }
420
+ scan(e, t, n) {
421
+ return this.request("scan", [
422
+ e,
423
+ t,
424
+ n
425
+ ]);
426
+ }
427
+ getByIndex(e, t, n, r) {
428
+ return this.request("getByIndex", [
429
+ e,
430
+ t,
431
+ n,
432
+ r
433
+ ]);
434
+ }
435
+ scanByIndex(e, t, n, r) {
436
+ return this.request("scanByIndex", [
437
+ e,
438
+ t,
439
+ n,
440
+ r
441
+ ]);
442
+ }
443
+ scanByIndexPage(e, t, n, r, i, a) {
444
+ return this.request("scanByIndexPage", [
445
+ e,
446
+ t,
447
+ n,
448
+ r,
449
+ i,
450
+ a
451
+ ]);
452
+ }
453
+ getIndexes() {
454
+ return this.request("getIndexes", []);
455
+ }
456
+ reconcileIndexes(e, t) {
457
+ return this.request("reconcileIndexes", [e, t]);
458
+ }
459
+ listStores() {
460
+ return this.request("listStores", []);
461
+ }
462
+ getVersion() {
463
+ return this.request("getVersion", []);
464
+ }
465
+ changesSince(e, t) {
466
+ return this.request("changesSince", [e, t]);
467
+ }
468
+ setSchemaVersion(e, t) {
469
+ return this.request("setSchemaVersion", [e, t]);
470
+ }
471
+ exportSnapshot(e) {
472
+ return this.request("exportSnapshot", [e]);
473
+ }
474
+ importSnapshot(e) {
475
+ return this.request("importSnapshot", [e]);
476
+ }
477
+ reset() {
478
+ return this.request("reset", []);
479
+ }
480
+ compact() {
481
+ return this.request("compact", []);
482
+ }
483
+ rebuild() {
484
+ return this.request("rebuild", []);
485
+ }
486
+ stats() {
487
+ return this.request("stats", []);
488
+ }
489
+ storageInfo() {
490
+ return this.request("storageInfo", []);
491
+ }
492
+ requestPersistence() {
493
+ return this.request("requestPersistence", []);
494
+ }
495
+ setFailpoint(e) {
496
+ return this.request("setFailpoint", [e]);
497
+ }
498
+ async request(e, t) {
499
+ return await this.send(e, t, e);
500
+ }
501
+ async send(e, t, n) {
502
+ this.ensureOpen(), await this.ready, this.ensureOpen();
503
+ let r = this.nextRequestId;
504
+ this.nextRequestId += 1;
505
+ let i = ae(e, t), a = {
506
+ type: "moyodb:worker-protocol:request",
507
+ version: 1,
508
+ id: r,
509
+ command: e,
510
+ args: i.args
511
+ };
512
+ return await new Promise((t, o) => {
513
+ let s = this.requestTimeoutMs > 0 ? setTimeout(() => {
514
+ this.pending.delete(r), o(p("WorkerRequestTimeoutError", `worker command ${e} timed out after ${this.requestTimeoutMs}ms`));
515
+ }, this.requestTimeoutMs) : null;
516
+ this.pending.set(r, {
517
+ command: n,
518
+ resolve: (e) => t(e),
519
+ reject: o,
520
+ timeout: s
521
+ });
522
+ try {
523
+ i.transfer.length > 0 ? this.worker.postMessage(a, i.transfer) : this.worker.postMessage(a);
524
+ } catch (e) {
525
+ this.rejectPending(r, e instanceof Error ? e : Error(String(e)));
526
+ }
527
+ });
528
+ }
529
+ ensureOpen() {
530
+ if (this.closed) throw this.closeReason ?? p("WorkerTerminatedError", "worker transport is closed");
531
+ }
532
+ handleMessage = (e) => {
533
+ let t = e.data;
534
+ if (ne(t)) {
535
+ this.readyResolved = !0, this.readyResolve?.(), this.readyResolve = null, this.readyReject = null;
536
+ return;
537
+ }
538
+ if (!re(t)) return;
539
+ let n = this.pending.get(t.id);
540
+ if (n) {
541
+ if (this.pending.delete(t.id), _(n), t.ok) {
542
+ n.resolve(se(n.command, t.result));
543
+ return;
544
+ }
545
+ n.reject(ie(t.error));
546
+ }
547
+ };
548
+ handleMessageError = () => {
549
+ this.disposeInternal(p("WorkerMessageError", "worker message could not be deserialized"), !0);
550
+ };
551
+ handleError = (e) => {
552
+ let t = e.error instanceof Error ? e.error : p("WorkerError", e.message || "worker runtime failed");
553
+ this.disposeInternal(t, !0);
554
+ };
555
+ rejectPending(e, t) {
556
+ let n = this.pending.get(e);
557
+ n && (this.pending.delete(e), _(n), n.reject(t));
558
+ }
559
+ disposeInternal(e, t) {
560
+ if (!this.closed) {
561
+ this.closed = !0, this.closeReason = e, this.worker.removeEventListener("message", this.handleMessage), this.worker.removeEventListener("messageerror", this.handleMessageError), this.worker.removeEventListener("error", this.handleError), this.readyResolved || this.readyReject?.(e), this.readyResolve = null, this.readyReject = null;
562
+ for (let t of this.pending.values()) _(t), t.reject(e);
563
+ this.pending.clear(), t && this.fatalHandler?.(e);
564
+ }
565
+ }
566
+ }, v = class extends Error {
567
+ constructor(e, t) {
568
+ super(t), this.name = e, Object.setPrototypeOf(this, new.target.prototype);
569
+ }
570
+ }, y = class extends v {
571
+ constructor(e) {
572
+ super("UnsupportedPlatformError", e);
573
+ }
574
+ }, b = class extends v {
575
+ constructor(e) {
576
+ super("DatabaseBusyError", e);
577
+ }
578
+ }, ve = class extends v {
579
+ constructor(e) {
580
+ super("CorruptionError", e);
581
+ }
582
+ }, ye = class extends v {
583
+ constructor(e) {
584
+ super("StoreExistsError", e);
585
+ }
586
+ }, be = class extends v {
587
+ constructor(e) {
588
+ super("StoreNotFoundError", e);
589
+ }
590
+ }, xe = class extends v {
591
+ constructor(e) {
592
+ super("IndexNotFoundError", e);
593
+ }
594
+ }, x = class extends v {
595
+ constructor(e) {
596
+ super("ConstraintError", e);
597
+ }
598
+ }, Se = class extends x {
599
+ constructor(e) {
600
+ super(e), this.name = "UniqueIndexConstraintError";
601
+ }
602
+ }, Ce = class extends v {
603
+ constructor(e = "write transaction already open") {
604
+ super("WriteTransactionAlreadyOpenError", e);
605
+ }
606
+ }, we = class extends v {
607
+ constructor(e = "readonly transaction cannot perform this operation") {
608
+ super("ReadonlyTransactionError", e);
609
+ }
610
+ }, S = class extends v {
611
+ constructor(e = "transaction is already closed") {
612
+ super("TransactionClosedError", e);
613
+ }
614
+ }, C = class extends v {
615
+ constructor(e = "database handle is closed") {
616
+ super("DatabaseClosedError", e);
617
+ }
618
+ }, Te = class extends v {
619
+ constructor(e) {
620
+ super("ValueTooLargeError", e);
621
+ }
622
+ }, Ee = class extends v {
623
+ constructor(e) {
624
+ super("KeyTooLargeError", e);
625
+ }
626
+ }, De = class extends v {
627
+ constructor(e) {
628
+ super("StoreNameTooLongError", e);
629
+ }
630
+ }, w = class extends v {
631
+ constructor(e) {
632
+ super("ReservedStoreNameError", e);
633
+ }
634
+ }, Oe = class extends v {
635
+ constructor(e) {
636
+ super("InvalidRangeError", e);
637
+ }
638
+ }, ke = class extends v {
639
+ constructor(e) {
640
+ super("StorageError", e);
641
+ }
642
+ }, T = class extends v {
643
+ constructor(e) {
644
+ super("SerializationError", e);
645
+ }
646
+ }, Ae = class extends v {
647
+ constructor(e) {
648
+ super("InjectedFailureError", e);
649
+ }
650
+ }, je = class extends v {
651
+ constructor(e) {
652
+ super("ChangeFeedCompactedError", e);
653
+ }
654
+ }, Me = class extends v {
655
+ constructor(e) {
656
+ super("RecoveryRequiredError", e);
657
+ }
658
+ }, Ne = class extends v {
659
+ constructor(e) {
660
+ super("InternalError", e);
661
+ }
662
+ }, E = class extends v {
663
+ constructor(e) {
664
+ super("InvalidOpenOptionsError", e);
665
+ }
666
+ }, D = class extends v {
667
+ constructor(e) {
668
+ super("VersionError", e);
669
+ }
670
+ }, Pe = {
671
+ UnsupportedPlatformError: y,
672
+ DatabaseBusyError: b,
673
+ CorruptionError: ve,
674
+ StoreExistsError: ye,
675
+ StoreNotFoundError: be,
676
+ IndexNotFoundError: xe,
677
+ ConstraintError: x,
678
+ UniqueIndexConstraintError: Se,
679
+ WriteTransactionAlreadyOpenError: Ce,
680
+ ReadonlyTransactionError: we,
681
+ TransactionClosedError: S,
682
+ DatabaseClosedError: C,
683
+ ValueTooLargeError: Te,
684
+ KeyTooLargeError: Ee,
685
+ StoreNameTooLongError: De,
686
+ ReservedStoreNameError: w,
687
+ InvalidRangeError: Oe,
688
+ StorageError: ke,
689
+ SerializationError: T,
690
+ InjectedFailureError: Ae,
691
+ ChangeFeedCompactedError: je,
692
+ RecoveryRequiredError: Me,
693
+ InternalError: Ne,
694
+ InvalidOpenOptionsError: E,
695
+ VersionError: D
696
+ };
697
+ function Fe(e, t) {
698
+ if (Object.prototype.hasOwnProperty.call(Pe, e)) {
699
+ let n = Pe[e];
700
+ return new n(t);
701
+ }
702
+ let n = Error(t);
703
+ return n.name = e, n;
704
+ }
705
+ function O(e) {
706
+ if (e instanceof v) return e;
707
+ if (e instanceof Error) return e.name === "Error" ? e : Fe(e.name, e.message);
708
+ let t = e;
709
+ return Fe(t?.code ?? t?.name ?? "Error", t?.message ?? String(e));
710
+ }
711
+ //#endregion
712
+ //#region src/registry.ts
713
+ var Ie = /* @__PURE__ */ new Set([
714
+ "after_wal_flush",
715
+ "after_main_flush",
716
+ "before_superblock_flush"
717
+ ]), Le = "moyodb:persistence-bridge:init", Re = "moyodb:persistence-bridge:request", ze = "moyodb:persistence-bridge:response";
718
+ function Be(t) {
719
+ return e(t) && t.type === Re && typeof t.id == "number" && (t.op === "persist" || t.op === "persisted");
720
+ }
721
+ var Ve = 1e5, k = /* @__PURE__ */ new Map();
722
+ function He(e) {
723
+ if (typeof e != "object" || !e || Array.isArray(e)) throw new E("open options must be an object");
724
+ }
725
+ function A(e, t, n) {
726
+ if (e === void 0) return n;
727
+ if (typeof e != "boolean") throw new E(`${t} must be a boolean`);
728
+ return e;
729
+ }
730
+ function j(e, t, n) {
731
+ if (e === void 0) return n;
732
+ if (typeof e != "number" || !Number.isSafeInteger(e) || e < 0) throw new E(`${t} must be a non-negative safe integer`);
733
+ return e;
734
+ }
735
+ function Ue(e) {
736
+ return Math.max(1, j(e, "cachePages", 256));
737
+ }
738
+ function We(e) {
739
+ if (e == null) return null;
740
+ if (typeof e != "string" || !Ie.has(e)) throw new E(`debugFailpoint must be one of: ${Array.from(Ie).join(", ")}`);
741
+ return e;
742
+ }
743
+ function Ge(e) {
744
+ if (e === void 0) return null;
745
+ if (typeof e != "object" || !e || Array.isArray(e)) throw new E("changeFeed must be an object");
746
+ let t = e, n = A(t.enabled, "changeFeed.enabled", !0), r = Ve;
747
+ if (t.retainTxids === null) r = null;
748
+ else if (t.retainTxids !== void 0 && (r = j(t.retainTxids, "changeFeed.retainTxids", 0), r === 0)) throw new E("changeFeed.retainTxids must be positive; disable the feed instead");
749
+ return {
750
+ enabled: n,
751
+ retainTxids: r
752
+ };
753
+ }
754
+ function Ke(e = {}) {
755
+ return He(e), {
756
+ createIfMissing: A(e.createIfMissing, "createIfMissing", !0),
757
+ ownerWaitMs: j(e.ownerWaitMs, "ownerWaitMs", 0),
758
+ requestPersistence: A(e.requestPersistence, "requestPersistence", !0),
759
+ cachePages: Ue(e.cachePages),
760
+ debugFailpoint: We(e.debugFailpoint),
761
+ changeFeed: Ge(e.changeFeed)
762
+ };
763
+ }
764
+ function qe(e, t) {
765
+ return e?.enabled === t?.enabled && e?.retainTxids === t?.retainTxids;
766
+ }
767
+ function Je(e, t, n) {
768
+ if (t.cachePages !== n.cachePages) throw new E(`database ${e} is already open in this tab with cachePages=${t.cachePages}; requested cachePages=${n.cachePages}`);
769
+ if (n.changeFeed !== null && !qe(t.changeFeed, n.changeFeed)) throw new E(`database ${e} is already open in this tab with a different changeFeed policy`);
770
+ }
771
+ async function Ye() {
772
+ let e = globalThis.navigator.storage;
773
+ return typeof e.persisted == "function" && await n(e.persisted(), 1e3, !1);
774
+ }
775
+ async function Xe() {
776
+ let e = globalThis.navigator.storage;
777
+ return typeof e.persist == "function" && await n(e.persist(), 1e3, !1);
778
+ }
779
+ async function Ze(e, t) {
780
+ let n = t.op === "persist" ? await Xe() : await Ye(), r = {
781
+ type: ze,
782
+ id: t.id,
783
+ granted: n
784
+ };
785
+ e.postMessage(r);
786
+ }
787
+ function Qe(e) {
788
+ let t = new MessageChannel(), n = t.port1;
789
+ return n.addEventListener("message", (e) => {
790
+ let t = e.data;
791
+ Be(t) && Ze(n, t);
792
+ }), n.start(), e.postMessage({ type: Le }, [t.port2]), { close() {
793
+ n.close();
794
+ } };
795
+ }
796
+ function $e() {
797
+ let e = new Worker(new URL(
798
+ /* @vite-ignore */
799
+ "/assets/worker-R2dy1Ju5.js",
800
+ "" + import.meta.url
801
+ ), { type: "module" });
802
+ return {
803
+ worker: e,
804
+ proxy: new _e(e),
805
+ persistenceBridge: Qe(e)
806
+ };
807
+ }
808
+ function et(e, t, n, r, i) {
809
+ let a = {
810
+ dbName: e,
811
+ refs: 1,
812
+ worker: t,
813
+ proxy: n,
814
+ persistenceBridge: r,
815
+ options: i,
816
+ invalidated: !1,
817
+ txInvalidationListeners: /* @__PURE__ */ new Set(),
818
+ handleInvalidationListeners: /* @__PURE__ */ new Set()
819
+ };
820
+ return n.setFatalHandler(() => {
821
+ k.get(e) !== a || a.invalidated || (k.delete(e), a.refs = 0, N(a), a.persistenceBridge.close(), a.worker.terminate());
822
+ }), a;
823
+ }
824
+ function tt(e) {
825
+ for (let t of Array.from(e)) t();
826
+ }
827
+ function nt(e, t) {
828
+ return e.txInvalidationListeners.add(t), () => {
829
+ e.txInvalidationListeners.delete(t);
830
+ };
831
+ }
832
+ function rt(e, t) {
833
+ return e.handleInvalidationListeners.add(t), () => {
834
+ e.handleInvalidationListeners.delete(t);
835
+ };
836
+ }
837
+ function M(e) {
838
+ tt(e.txInvalidationListeners);
839
+ }
840
+ function N(e) {
841
+ e.invalidated || (e.invalidated = !0, M(e), tt(e.handleInvalidationListeners));
842
+ }
843
+ function it(e) {
844
+ let t = k.get(e);
845
+ return t ? (k.delete(e), N(t), t.refs = 0, t.persistenceBridge.close(), t.proxy.dispose(/* @__PURE__ */ Error("worker was terminated by unsafeDebugCrashWorker")), t.worker.terminate(), !0) : !1;
846
+ }
847
+ async function at(e, t = {}) {
848
+ let n = Ke(t), r = k.get(e);
849
+ if (r) {
850
+ if (r.invalidated) k.delete(e);
851
+ else {
852
+ if (Je(e, r.options, n), n.debugFailpoint !== null) try {
853
+ await r.proxy.setFailpoint(n.debugFailpoint);
854
+ } catch (e) {
855
+ throw O(e);
856
+ }
857
+ return r.refs += 1, r;
858
+ }
859
+ }
860
+ let { worker: i, proxy: a, persistenceBridge: o } = $e(), s = et(e, i, a, o, n);
861
+ try {
862
+ return await a.open({
863
+ dbName: e,
864
+ options: n
865
+ }), k.set(e, s), s;
866
+ } catch (e) {
867
+ throw o.close(), a.dispose(/* @__PURE__ */ Error("worker open failed")), i.terminate(), O(e);
868
+ }
869
+ }
870
+ async function ot(e) {
871
+ if (e.invalidated) return;
872
+ let t = k.get(e.dbName);
873
+ if (t && (--t.refs, !(t.refs > 0))) {
874
+ k.delete(e.dbName);
875
+ try {
876
+ await t.proxy.close();
877
+ } finally {
878
+ t.persistenceBridge.close(), t.proxy.dispose(/* @__PURE__ */ Error("worker was released")), t.worker.terminate();
879
+ }
880
+ }
881
+ }
882
+ async function st(e) {
883
+ let t = k.get(e.dbName) ?? e;
884
+ k.delete(e.dbName), N(t), t.refs = 0;
885
+ try {
886
+ await t.proxy.destroy();
887
+ } catch (e) {
888
+ throw O(e);
889
+ } finally {
890
+ t.persistenceBridge.close(), t.proxy.dispose(/* @__PURE__ */ Error("worker was released")), t.worker.terminate();
891
+ }
892
+ }
893
+ async function ct(e) {
894
+ let t = k.get(e);
895
+ if (t) {
896
+ await st(t);
897
+ return;
898
+ }
899
+ let { worker: n, proxy: r, persistenceBridge: i } = $e();
900
+ try {
901
+ await r.deleteDB(e);
902
+ } catch (e) {
903
+ throw O(e);
904
+ } finally {
905
+ i.close(), r.dispose(/* @__PURE__ */ Error("temporary worker was terminated")), n.terminate();
906
+ }
907
+ }
908
+ //#endregion
909
+ //#region src/codec.ts
910
+ var lt = new TextEncoder(), ut = new TextDecoder(), dt = 16, ft = 32, pt = 33, mt = 48, ht = 64, gt = 80, P = 0, _t = 255, F = 0, vt = 18446744073709551615n;
911
+ function I(e) {
912
+ return lt.encode(e);
913
+ }
914
+ function yt(e) {
915
+ return ut.decode(e);
916
+ }
917
+ function bt(e) {
918
+ return I(JSON.stringify(e));
919
+ }
920
+ function xt(e) {
921
+ return JSON.parse(yt(e));
922
+ }
923
+ function St(e) {
924
+ let t = typeof e == "bigint" ? e : BigInt(e);
925
+ if (t < 0n) throw RangeError("u64Key only accepts non-negative values");
926
+ let n = /* @__PURE__ */ new ArrayBuffer(8);
927
+ return new DataView(n).setBigUint64(0, t, !1), new Uint8Array(n);
928
+ }
929
+ function Ct(e) {
930
+ return Array.isArray(e);
931
+ }
932
+ function wt(e) {
933
+ return Ct(e) ? L(...e) : V(e);
934
+ }
935
+ function L(...e) {
936
+ return Ot(e.map((e) => V(e)));
937
+ }
938
+ function Tt(e) {
939
+ return V(e);
940
+ }
941
+ function R(e) {
942
+ return Ot(e);
943
+ }
944
+ function Et(e) {
945
+ let t = [], n = [];
946
+ for (let r = 0; r < e.length; r += 1) {
947
+ let i = e[r];
948
+ if (i !== P) {
949
+ n.push(i);
950
+ continue;
951
+ }
952
+ let a = e[r + 1];
953
+ if (a === _t) {
954
+ n.push(P), r += 1;
955
+ continue;
956
+ }
957
+ if (a === F) {
958
+ t.push(Uint8Array.from(n)), n.length = 0, r += 1;
959
+ continue;
960
+ }
961
+ throw TypeError("invalid compound key encoding");
962
+ }
963
+ if (n.length !== 0) throw TypeError("compound key terminated unexpectedly");
964
+ return t;
965
+ }
966
+ function z(e) {
967
+ let t = e.slice();
968
+ for (let e = t.length - 1; e >= 0; --e) if (t[e] !== 255) return t[e] += 1, t.subarray(0, e + 1);
969
+ return null;
970
+ }
971
+ function B(e) {
972
+ let t = z(e);
973
+ return t ? {
974
+ gte: e,
975
+ lt: t
976
+ } : { gte: e };
977
+ }
978
+ function Dt(...e) {
979
+ return B(L(...e));
980
+ }
981
+ function V(e) {
982
+ if (e === null) return Uint8Array.of(dt);
983
+ if (typeof e == "boolean") return Uint8Array.of(e ? pt : ft);
984
+ if (typeof e == "number") return kt(e);
985
+ if (typeof e == "string") {
986
+ let t = I(e), n = new Uint8Array(1 + t.length);
987
+ return n[0] = ht, n.set(t, 1), n;
988
+ }
989
+ if (e instanceof Uint8Array) {
990
+ let t = new Uint8Array(1 + e.length);
991
+ return t[0] = gt, t.set(e, 1), t;
992
+ }
993
+ throw TypeError("compound key parts must be string, number, boolean, null, or Uint8Array");
994
+ }
995
+ function Ot(e) {
996
+ let t = 0;
997
+ for (let n of e) {
998
+ for (let e of n) t += e === P ? 2 : 1;
999
+ t += 2;
1000
+ }
1001
+ let n = new Uint8Array(t), r = 0;
1002
+ for (let t of e) {
1003
+ for (let e of t) e === P ? (n[r] = P, n[r + 1] = _t, r += 2) : (n[r] = e, r += 1);
1004
+ n[r] = F, n[r + 1] = F, r += 2;
1005
+ }
1006
+ return n;
1007
+ }
1008
+ function kt(e) {
1009
+ if (!Number.isFinite(e)) throw TypeError("compound key number parts must be finite");
1010
+ let t = Object.is(e, -0) ? 0 : e, n = /* @__PURE__ */ new ArrayBuffer(9), r = new DataView(n);
1011
+ r.setUint8(0, mt), r.setFloat64(1, t, !1);
1012
+ let i = r.getBigUint64(1, !1);
1013
+ return i = (i & 1n << 63n) == 0n ? i ^ 1n << 63n : ~i & vt, r.setBigUint64(1, i, !1), new Uint8Array(n);
1014
+ }
1015
+ //#endregion
1016
+ //#region src/indexing.ts
1017
+ var At = new TextEncoder(), jt = new TextDecoder("utf-8", { fatal: !0 }), H = Symbol("moyodb.indexing.missing"), Mt = 14695981039346656037n, Nt = 1099511628211n, Pt = 18446744073709551615n, Ft = "__browserdb:", It = "__browserdb:index:", Lt = "__browserdb:indexes";
1018
+ function Rt(e) {
1019
+ return e.startsWith(Ft);
1020
+ }
1021
+ function U(e) {
1022
+ if (Rt(e)) throw new w(`reserved store name: ${e}`);
1023
+ }
1024
+ function W(e) {
1025
+ if (e === void 0) return [];
1026
+ if (!Array.isArray(e)) throw new E("indexes must be an array");
1027
+ let t = [], n = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Map();
1028
+ for (let i of e) {
1029
+ if (typeof i != "object" || !i || Array.isArray(i)) throw new E("each index definition must be an object");
1030
+ let { store: e, name: a, keyPath: o, unique: s } = i;
1031
+ if (typeof e != "string" || e.length === 0) throw new E("index store must be a non-empty string");
1032
+ if (typeof a != "string" || a.length === 0) throw new E("index name must be a non-empty string");
1033
+ U(e);
1034
+ let c = Qt(o);
1035
+ if (s !== void 0 && typeof s != "boolean") throw new E("index unique must be a boolean");
1036
+ let l = {
1037
+ store: e,
1038
+ name: a,
1039
+ keyPath: c.paths,
1040
+ compound: c.compound,
1041
+ unique: s ?? !1,
1042
+ internalStore: tn(e, a)
1043
+ }, u = G(l.store, l.name);
1044
+ if (n.has(u)) throw new E(`duplicate index definition ${l.store}.${l.name}`);
1045
+ n.add(u);
1046
+ let d = r.get(l.internalStore);
1047
+ if (d && d !== u) throw new E(`index definitions ${d} and ${u} map to the same internal store ${l.internalStore}`);
1048
+ r.set(l.internalStore, u), t.push(l);
1049
+ }
1050
+ return t.sort(zt), t;
1051
+ }
1052
+ function zt(e, n) {
1053
+ let r = t(e.store, n.store);
1054
+ if (r !== 0) return r;
1055
+ let i = t(e.name, n.name);
1056
+ if (i !== 0) return i;
1057
+ let a = Number(e.unique) - Number(n.unique);
1058
+ return a === 0 ? t(en(e), en(n)) : a;
1059
+ }
1060
+ function G(e, t) {
1061
+ return `${e}\u0000${t}`;
1062
+ }
1063
+ function Bt(e, t, n) {
1064
+ return e.find((e) => e.store === t && e.name === n) ?? null;
1065
+ }
1066
+ function Vt(e, t) {
1067
+ return e.filter((e) => e.store === t);
1068
+ }
1069
+ function Ht(e, t) {
1070
+ return R([I(e), I(t)]);
1071
+ }
1072
+ function Ut(e) {
1073
+ return I(JSON.stringify(Zt(e)));
1074
+ }
1075
+ function Wt(e) {
1076
+ let t;
1077
+ try {
1078
+ t = JSON.parse(jt.decode(e));
1079
+ } catch (e) {
1080
+ throw new T(`invalid persisted index metadata: ${String(e)}`);
1081
+ }
1082
+ let [n] = W([t]);
1083
+ return n;
1084
+ }
1085
+ function Gt(e, t) {
1086
+ return R([e, t]);
1087
+ }
1088
+ function Kt(e) {
1089
+ let t = Et(e);
1090
+ if (t.length !== 2) throw new T("invalid index entry key encoding");
1091
+ return {
1092
+ logicalKey: t[0],
1093
+ primaryKey: t[1]
1094
+ };
1095
+ }
1096
+ function qt(e) {
1097
+ return B(R([e]));
1098
+ }
1099
+ function Jt(e = {}) {
1100
+ ln(e);
1101
+ let t = {};
1102
+ if (e.reverse !== void 0 && (t.reverse = e.reverse), e.limit !== void 0 && (t.limit = e.limit), e.gte && (t.gte = R([e.gte])), e.gt) {
1103
+ let n = z(R([e.gt]));
1104
+ n && (t.gte = n);
1105
+ }
1106
+ if (e.lt && (t.lt = R([e.lt])), e.lte) {
1107
+ let n = z(R([e.lte]));
1108
+ n && (t.lt = n);
1109
+ }
1110
+ return t;
1111
+ }
1112
+ function Yt(e, t) {
1113
+ let n = an(t);
1114
+ if (e.compound) {
1115
+ let t = [];
1116
+ for (let r of e.keyPath) {
1117
+ let e = on(n, r);
1118
+ if (e === H) return null;
1119
+ t.push(sn(e));
1120
+ }
1121
+ return R(t);
1122
+ }
1123
+ let r = on(n, e.keyPath[0]);
1124
+ return r === H ? null : sn(r);
1125
+ }
1126
+ function K(e) {
1127
+ return JSON.stringify(q(e));
1128
+ }
1129
+ function Xt(e) {
1130
+ return e.map((e) => ({
1131
+ store: e.store,
1132
+ name: e.name,
1133
+ keyPath: [...e.keyPath],
1134
+ compound: e.compound,
1135
+ unique: e.unique,
1136
+ internalStore: e.internalStore
1137
+ }));
1138
+ }
1139
+ function q(e) {
1140
+ return e.map(Zt);
1141
+ }
1142
+ function Zt(e) {
1143
+ return {
1144
+ store: e.store,
1145
+ name: e.name,
1146
+ keyPath: e.compound ? [...e.keyPath] : e.keyPath[0],
1147
+ unique: e.unique
1148
+ };
1149
+ }
1150
+ function Qt(e) {
1151
+ if (typeof e == "string") return $t(e), {
1152
+ paths: [e],
1153
+ compound: !1
1154
+ };
1155
+ if (!Array.isArray(e) || e.length === 0 || !e.every((e) => typeof e == "string")) throw new E("index keyPath must be a non-empty string or non-empty string[]");
1156
+ for (let t of e) $t(t);
1157
+ return {
1158
+ paths: [...e],
1159
+ compound: !0
1160
+ };
1161
+ }
1162
+ function $t(e) {
1163
+ if (e.length === 0) throw new E("index keyPath strings must be non-empty");
1164
+ if (e.startsWith(".") || e.endsWith(".") || e.includes("..")) throw new E(`invalid index keyPath: ${e}`);
1165
+ }
1166
+ function en(e) {
1167
+ return e.compound ? `[${e.keyPath.join(",")}]` : e.keyPath[0];
1168
+ }
1169
+ function tn(e, t) {
1170
+ let n = `${It}${nn(I(e))}:${nn(I(t))}`;
1171
+ return At.encode(n).length <= 255 ? n : `${It}h:${rn(I(G(e, t)))}`;
1172
+ }
1173
+ function nn(e) {
1174
+ let t = "";
1175
+ for (let n of e) t += String.fromCharCode(n);
1176
+ return btoa(t).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/u, "");
1177
+ }
1178
+ function rn(e) {
1179
+ let t = Mt;
1180
+ for (let n of e) t ^= BigInt(n), t = t * Nt & Pt;
1181
+ return t.toString(16).padStart(16, "0");
1182
+ }
1183
+ function an(e) {
1184
+ try {
1185
+ return JSON.parse(jt.decode(e));
1186
+ } catch (e) {
1187
+ throw new T(`indexed values must be valid UTF-8 JSON: ${String(e)}`);
1188
+ }
1189
+ }
1190
+ function on(e, t) {
1191
+ let n = e;
1192
+ for (let e of t.split(".")) {
1193
+ if (n == null) return H;
1194
+ let t = Object(n);
1195
+ if (!(e in t)) return H;
1196
+ n = Reflect.get(t, e);
1197
+ }
1198
+ return n;
1199
+ }
1200
+ function sn(e) {
1201
+ if (e instanceof Uint8Array || e === null || typeof e == "string" || typeof e == "number" || typeof e == "boolean") return Tt(e);
1202
+ throw new T(`indexed keyPath values must resolve to string, number, boolean, null, or Uint8Array; got ${cn(e)}`);
1203
+ }
1204
+ function cn(e) {
1205
+ return Array.isArray(e) ? "array" : e === null ? "null" : typeof e;
1206
+ }
1207
+ function ln(e) {
1208
+ if (e.gt !== void 0 && e.gte !== void 0) throw TypeError("index range cannot specify both gt and gte");
1209
+ if (e.lt !== void 0 && e.lte !== void 0) throw TypeError("index range cannot specify both lt and lte");
1210
+ }
1211
+ //#endregion
1212
+ //#region src/change-events.ts
1213
+ function un(t) {
1214
+ return !e(t) || !(t.key instanceof Uint8Array) ? !1 : t.kind === "put" || t.kind === "delete" || (t.kind === "clear" || t.kind === "drop") && t.key.byteLength === 0;
1215
+ }
1216
+ function dn(t) {
1217
+ return e(t) && typeof t.store == "string" && Array.isArray(t.changes) && t.changes.every(un);
1218
+ }
1219
+ function fn(t) {
1220
+ return e(t) && t.type === "commit_applied" && typeof t.dbName == "string" && typeof t.txid == "number" && Array.isArray(t.stores) && t.stores.every(dn);
1221
+ }
1222
+ //#endregion
1223
+ //#region src/subscriptions.ts
1224
+ function pn(e) {
1225
+ return e.map((e) => ({
1226
+ key: e.key.slice(),
1227
+ kind: e.kind
1228
+ }));
1229
+ }
1230
+ function mn(e, t) {
1231
+ if (t.byteLength > e.byteLength) return !1;
1232
+ for (let n = 0; n < t.byteLength; n += 1) if (e[n] !== t[n]) return !1;
1233
+ return !0;
1234
+ }
1235
+ function hn(e, t, n) {
1236
+ if (typeof e == "function") {
1237
+ if (t !== void 0 || n !== void 0) throw TypeError("subscribe(callback) accepts exactly one callback argument");
1238
+ return {
1239
+ store: null,
1240
+ keyPrefix: null,
1241
+ callback: e
1242
+ };
1243
+ }
1244
+ if (typeof e != "string") throw TypeError("subscribe() storeName must be a string");
1245
+ if (U(e), typeof t == "function") {
1246
+ if (n !== void 0) throw TypeError("subscribe(storeName, callback) accepts exactly two arguments");
1247
+ return {
1248
+ store: e,
1249
+ keyPrefix: null,
1250
+ callback: t
1251
+ };
1252
+ }
1253
+ if (t instanceof Uint8Array && typeof n == "function") return {
1254
+ store: e,
1255
+ keyPrefix: t.slice(),
1256
+ callback: n
1257
+ };
1258
+ throw TypeError("subscribe() expects one of: (callback), (storeName, callback), (storeName, keyPrefix, callback)");
1259
+ }
1260
+ var gn = class {
1261
+ #e;
1262
+ #t = null;
1263
+ #n = /* @__PURE__ */ new Set();
1264
+ #r = !1;
1265
+ constructor(e) {
1266
+ this.#e = e;
1267
+ }
1268
+ subscribe(e, t, n) {
1269
+ if (this.#r) throw new C();
1270
+ let r = hn(e, t, n), i = {
1271
+ active: !0,
1272
+ store: r.store,
1273
+ keyPrefix: r.keyPrefix,
1274
+ callback: r.callback
1275
+ };
1276
+ return this.#i(), this.#n.add(i), () => {
1277
+ i.active && (i.active = !1, this.#n.delete(i), this.#n.size === 0 && this.#a());
1278
+ };
1279
+ }
1280
+ close() {
1281
+ this.#r || (this.#r = !0, this.#n.clear(), this.#a());
1282
+ }
1283
+ #i() {
1284
+ if (this.#t !== null || this.#r) return;
1285
+ let e = new BroadcastChannel(`db:${this.#e}:events`);
1286
+ e.onmessage = (e) => {
1287
+ this.#o(e.data);
1288
+ }, this.#t = e;
1289
+ }
1290
+ #a() {
1291
+ this.#t?.close(), this.#t = null;
1292
+ }
1293
+ #o(e) {
1294
+ if (!fn(e) || e.dbName !== this.#e || this.#n.size === 0) return;
1295
+ let t = Array.from(this.#n);
1296
+ for (let n of e.stores) for (let r of t) {
1297
+ if (!r.active || r.store !== null && r.store !== n.store) continue;
1298
+ let t, i = r.keyPrefix;
1299
+ if (i === null) t = pn(n.changes);
1300
+ else if (t = pn(n.changes.filter((e) => e.kind === "clear" || e.kind === "drop" || mn(e.key, i))), t.length === 0) continue;
1301
+ try {
1302
+ r.callback(n.store, t, e.txid);
1303
+ } catch (e) {
1304
+ queueMicrotask(() => {
1305
+ throw e;
1306
+ });
1307
+ }
1308
+ }
1309
+ }
1310
+ }, _n = 256;
1311
+ async function J(e) {
1312
+ try {
1313
+ return await e();
1314
+ } catch (e) {
1315
+ throw O(e);
1316
+ }
1317
+ }
1318
+ var vn = class {
1319
+ mode;
1320
+ #e;
1321
+ #t;
1322
+ #n = !1;
1323
+ #r;
1324
+ constructor(e, t, n, r) {
1325
+ this.#e = e, this.#t = t, this.mode = n, this.#r = r;
1326
+ }
1327
+ async get(e, t) {
1328
+ return this.#i(), U(e), J(() => this.#e.proxy.get(this.#t, e, t));
1329
+ }
1330
+ async getMany(e, t) {
1331
+ return this.#i(), U(e), J(() => this.#e.proxy.getMany(this.#t, e, t));
1332
+ }
1333
+ async has(e, t) {
1334
+ return this.#i(), U(e), J(() => this.#e.proxy.has(this.#t, e, t));
1335
+ }
1336
+ async put(e, t, n, r = {}) {
1337
+ return this.#i(), U(e), J(() => this.#e.proxy.put(this.#t, e, t, n, $(r)));
1338
+ }
1339
+ async putMany(e, t, n = {}) {
1340
+ return this.#i(), U(e), J(() => this.#e.proxy.putMany(this.#t, e, t, $(n)));
1341
+ }
1342
+ async delete(e, t) {
1343
+ return this.#i(), U(e), J(() => this.#e.proxy.delete(this.#t, e, t));
1344
+ }
1345
+ async deleteMany(e, t) {
1346
+ return this.#i(), U(e), J(() => this.#e.proxy.deleteMany(this.#t, e, t));
1347
+ }
1348
+ async applyBatch(e, t) {
1349
+ return this.#i(), U(e), J(() => this.#e.proxy.applyBatch(this.#t, e, t));
1350
+ }
1351
+ async scan(e, t = {}) {
1352
+ return this.#i(), U(e), J(() => this.#e.proxy.scan(this.#t, e, t));
1353
+ }
1354
+ async getByIndex(e, t, n) {
1355
+ return this.#i(), U(e), J(() => this.#e.proxy.getByIndex(this.#t, e, t, n));
1356
+ }
1357
+ async *scanByIndex(e, t, n = {}) {
1358
+ this.#i(), U(e);
1359
+ let r = n.limit ?? Infinity, i = null;
1360
+ for (; r > 0;) {
1361
+ this.#i();
1362
+ let a = Math.min(r, _n), o = i, s = await J(() => this.#e.proxy.scanByIndexPage(this.#t, e, t, n, o, a));
1363
+ for (let e of s.rows) yield [e.key, e.value];
1364
+ if (r -= s.rows.length, s.cursor === null) return;
1365
+ i = s.cursor;
1366
+ }
1367
+ }
1368
+ async createStore(e, t = {}) {
1369
+ return this.#i(), U(e), J(() => this.#e.proxy.createStore(this.#t, e, kn(t)));
1370
+ }
1371
+ async dropStore(e) {
1372
+ return this.#i(), U(e), J(() => this.#e.proxy.dropStore(this.#t, e));
1373
+ }
1374
+ async clearStore(e) {
1375
+ return this.#i(), U(e), J(() => this.#e.proxy.clearStore(this.#t, e));
1376
+ }
1377
+ async commit() {
1378
+ this.#i();
1379
+ try {
1380
+ await J(() => this.#e.proxy.commit(this.#t));
1381
+ } finally {
1382
+ this.#a();
1383
+ }
1384
+ }
1385
+ async rollback() {
1386
+ this.#i();
1387
+ try {
1388
+ await J(() => this.#e.proxy.rollback(this.#t));
1389
+ } finally {
1390
+ this.#a();
1391
+ }
1392
+ }
1393
+ async rollbackIfOpen() {
1394
+ if (!this.#n) {
1395
+ if (this.#e.invalidated) {
1396
+ this.#a();
1397
+ return;
1398
+ }
1399
+ try {
1400
+ await this.#e.proxy.rollback(this.#t);
1401
+ } catch (e) {
1402
+ let t = O(e);
1403
+ if (!(t instanceof S)) throw t;
1404
+ } finally {
1405
+ this.#a();
1406
+ }
1407
+ }
1408
+ }
1409
+ forceClose() {
1410
+ this.#a();
1411
+ }
1412
+ internalId() {
1413
+ return this.#t;
1414
+ }
1415
+ #i() {
1416
+ if (this.#n || this.#e.invalidated) throw new S();
1417
+ }
1418
+ #a() {
1419
+ this.#n || (this.#n = !0, this.#r());
1420
+ }
1421
+ }, yn = class {
1422
+ #e;
1423
+ #t = !1;
1424
+ #n = /* @__PURE__ */ new Set();
1425
+ #r;
1426
+ #i = null;
1427
+ #a = null;
1428
+ constructor(e) {
1429
+ this.#e = e, this.#r = new gn(e.dbName), this.#i = nt(e, () => {
1430
+ this.#l();
1431
+ }), this.#a = rt(e, () => {
1432
+ this.#u();
1433
+ });
1434
+ }
1435
+ async begin(e = "readonly") {
1436
+ return this.#o(e);
1437
+ }
1438
+ async runSchemaMigration(e, t, n, r) {
1439
+ this.#d();
1440
+ let i = await this.#o("readwrite"), a = new bn(), o = new xn(i, a), s = new Sn(this, o, a);
1441
+ try {
1442
+ await this.#e.proxy.reconcileIndexes(i.internalId(), r), await n({
1443
+ db: s,
1444
+ oldVersion: e,
1445
+ newVersion: t,
1446
+ transaction: o
1447
+ }), await this.#e.proxy.setSchemaVersion(i.internalId(), t), await i.commit();
1448
+ } catch (e) {
1449
+ try {
1450
+ await i.rollbackIfOpen();
1451
+ } catch {}
1452
+ throw e;
1453
+ }
1454
+ }
1455
+ async listStores() {
1456
+ return this.#d(), J(() => this.#e.proxy.listStores());
1457
+ }
1458
+ async getVersion() {
1459
+ return this.#d(), J(() => this.#e.proxy.getVersion());
1460
+ }
1461
+ async changesSince(e, t = {}) {
1462
+ return this.#d(), J(() => this.#e.proxy.changesSince(jn(e), Mn(t)));
1463
+ }
1464
+ async #o(e) {
1465
+ return this.#d(), J(async () => {
1466
+ let t = await this.#e.proxy.begin(e), n = new vn(this.#e, t, e, () => {
1467
+ this.#n.delete(n);
1468
+ });
1469
+ return this.#n.add(n), n;
1470
+ });
1471
+ }
1472
+ async createStore(e, t = {}) {
1473
+ U(e), await this.#s("readwrite", "createStore", [e, kn(t)]);
1474
+ }
1475
+ async dropStore(e) {
1476
+ U(e), await this.#s("readwrite", "dropStore", [e]);
1477
+ }
1478
+ async clearStore(e) {
1479
+ U(e), await this.#s("readwrite", "clearStore", [e]);
1480
+ }
1481
+ async get(e, t) {
1482
+ return U(e), this.#s("readonly", "get", [e, t]);
1483
+ }
1484
+ async getMany(e, t) {
1485
+ return U(e), this.#s("readonly", "getMany", [e, t]);
1486
+ }
1487
+ async has(e, t) {
1488
+ return U(e), this.#s("readonly", "has", [e, t]);
1489
+ }
1490
+ async put(e, t, n, r = {}) {
1491
+ U(e), await this.#s("readwrite", "put", [
1492
+ e,
1493
+ t,
1494
+ n,
1495
+ $(r)
1496
+ ]);
1497
+ }
1498
+ async delete(e, t) {
1499
+ return U(e), this.#s("readwrite", "delete", [e, t]);
1500
+ }
1501
+ async scan(e, t = {}) {
1502
+ return U(e), this.#s("readonly", "scan", [e, t]);
1503
+ }
1504
+ async #s(e, t, n) {
1505
+ return this.#d(), J(() => this.#e.proxy.autocommit(e, t, n));
1506
+ }
1507
+ async exportSnapshot(e = {}) {
1508
+ return this.#d(), J(async () => {
1509
+ let t = await this.#e.proxy.exportSnapshot(An(e));
1510
+ return t instanceof Uint8Array ? t : new Uint8Array(t);
1511
+ });
1512
+ }
1513
+ async importSnapshot(e) {
1514
+ return this.#d(), J(() => {
1515
+ let t = e instanceof Uint8Array ? e : new Uint8Array(e);
1516
+ return this.#e.proxy.importSnapshot(t);
1517
+ });
1518
+ }
1519
+ async reset() {
1520
+ return this.#d(), M(this.#e), J(() => this.#e.proxy.reset());
1521
+ }
1522
+ async compact() {
1523
+ return this.#d(), M(this.#e), J(() => this.#e.proxy.compact());
1524
+ }
1525
+ async rebuild() {
1526
+ return this.#d(), M(this.#e), J(() => this.#e.proxy.rebuild());
1527
+ }
1528
+ async stats() {
1529
+ return this.#d(), J(() => this.#e.proxy.stats());
1530
+ }
1531
+ async storageInfo() {
1532
+ return this.#d(), J(() => this.#e.proxy.storageInfo());
1533
+ }
1534
+ async requestPersistence() {
1535
+ return this.#d(), J(() => this.#e.proxy.requestPersistence());
1536
+ }
1537
+ async destroy() {
1538
+ return this.#d(), J(() => st(this.#e));
1539
+ }
1540
+ async setFailpoint(e) {
1541
+ return this.#d(), J(() => this.#e.proxy.setFailpoint(e));
1542
+ }
1543
+ subscribe(e, t, n) {
1544
+ if (this.#d(), typeof e == "function") return this.#r.subscribe(e);
1545
+ if (typeof t == "function") return this.#r.subscribe(e, t);
1546
+ if (t instanceof Uint8Array && typeof n == "function") return this.#r.subscribe(e, t, n);
1547
+ throw TypeError("subscribe() expects one of: (callback), (storeName, callback), (storeName, keyPrefix, callback)");
1548
+ }
1549
+ watch(e, t, n) {
1550
+ if (typeof e == "function") return this.subscribe(e);
1551
+ if (typeof t == "function") return this.subscribe(e, t);
1552
+ if (t instanceof Uint8Array && typeof n == "function") return this.subscribe(e, t, n);
1553
+ throw TypeError("watch() expects one of: (callback), (storeName, callback), (storeName, keyPrefix, callback)");
1554
+ }
1555
+ async close() {
1556
+ if (this.#t) return;
1557
+ this.#t = !0, this.#c(), this.#r.close();
1558
+ let e = null, t = Array.from(this.#n);
1559
+ if (t.length > 0) {
1560
+ let n = await Promise.allSettled(t.map((e) => e.rollbackIfOpen()));
1561
+ for (let t of n) t.status === "rejected" && e === null && (e = O(t.reason));
1562
+ }
1563
+ try {
1564
+ await ot(this.#e);
1565
+ } catch (e) {
1566
+ throw O(e);
1567
+ }
1568
+ if (e) throw e;
1569
+ }
1570
+ #c() {
1571
+ this.#i?.(), this.#i = null, this.#a?.(), this.#a = null;
1572
+ }
1573
+ #l() {
1574
+ for (let e of Array.from(this.#n)) e.forceClose();
1575
+ }
1576
+ #u() {
1577
+ this.#t || (this.#t = !0, this.#c(), this.#l(), this.#r.close());
1578
+ }
1579
+ #d() {
1580
+ if (this.#t || this.#e.invalidated) throw new C();
1581
+ }
1582
+ }, bn = class {
1583
+ #e = /* @__PURE__ */ new Set();
1584
+ #t = /* @__PURE__ */ new Set();
1585
+ recordCreate(e) {
1586
+ this.#t.delete(e), this.#e.add(e);
1587
+ }
1588
+ recordDrop(e) {
1589
+ this.#e.delete(e) || this.#t.add(e);
1590
+ }
1591
+ apply(e) {
1592
+ let n = new Set(e);
1593
+ for (let e of this.#e) n.add(e);
1594
+ for (let e of this.#t) n.delete(e);
1595
+ return Array.from(n).sort(t);
1596
+ }
1597
+ }, xn = class {
1598
+ #e;
1599
+ #t;
1600
+ constructor(e, t) {
1601
+ this.#e = e, this.#t = t;
1602
+ }
1603
+ get mode() {
1604
+ return this.#e.mode;
1605
+ }
1606
+ async get(e, t) {
1607
+ return this.#e.get(e, t);
1608
+ }
1609
+ async getMany(e, t) {
1610
+ return this.#e.getMany(e, t);
1611
+ }
1612
+ async has(e, t) {
1613
+ return this.#e.has(e, t);
1614
+ }
1615
+ async put(e, t, n, r = {}) {
1616
+ await this.#e.put(e, t, n, r);
1617
+ }
1618
+ async putMany(e, t, n = {}) {
1619
+ await this.#e.putMany(e, t, n);
1620
+ }
1621
+ async delete(e, t) {
1622
+ return this.#e.delete(e, t);
1623
+ }
1624
+ async deleteMany(e, t) {
1625
+ await this.#e.deleteMany(e, t);
1626
+ }
1627
+ async applyBatch(e, t) {
1628
+ await this.#e.applyBatch(e, t);
1629
+ }
1630
+ async scan(e, t = {}) {
1631
+ return this.#e.scan(e, t);
1632
+ }
1633
+ async getByIndex(e, t, n) {
1634
+ return this.#e.getByIndex(e, t, n);
1635
+ }
1636
+ scanByIndex(e, t, n = {}) {
1637
+ return this.#e.scanByIndex(e, t, n);
1638
+ }
1639
+ async createStore(e, t = {}) {
1640
+ await this.#e.createStore(e, t), this.#t.recordCreate(e);
1641
+ }
1642
+ async dropStore(e) {
1643
+ await this.#e.dropStore(e), this.#t.recordDrop(e);
1644
+ }
1645
+ async clearStore(e) {
1646
+ await this.#e.clearStore(e);
1647
+ }
1648
+ commit() {
1649
+ return Promise.reject(Y("transaction.commit"));
1650
+ }
1651
+ rollback() {
1652
+ return Promise.reject(Y("transaction.rollback"));
1653
+ }
1654
+ }, Sn = class {
1655
+ #e;
1656
+ #t;
1657
+ #n;
1658
+ constructor(e, t, n) {
1659
+ this.#e = e, this.#t = t, this.#n = n;
1660
+ }
1661
+ begin(e = "readonly") {
1662
+ return Promise.reject(Y("db.begin"));
1663
+ }
1664
+ async createStore(e, t = {}) {
1665
+ await this.#t.createStore(e, t);
1666
+ }
1667
+ async dropStore(e) {
1668
+ await this.#t.dropStore(e);
1669
+ }
1670
+ async clearStore(e) {
1671
+ await this.#t.clearStore(e);
1672
+ }
1673
+ async listStores() {
1674
+ return this.#n.apply(await this.#e.listStores());
1675
+ }
1676
+ async getVersion() {
1677
+ return this.#e.getVersion();
1678
+ }
1679
+ changesSince(e, t = {}) {
1680
+ return Promise.reject(Y("db.changesSince"));
1681
+ }
1682
+ async get(e, t) {
1683
+ return this.#t.get(e, t);
1684
+ }
1685
+ async getMany(e, t) {
1686
+ return this.#t.getMany(e, t);
1687
+ }
1688
+ async has(e, t) {
1689
+ return this.#t.has(e, t);
1690
+ }
1691
+ async put(e, t, n, r = {}) {
1692
+ await this.#t.put(e, t, n, r);
1693
+ }
1694
+ async delete(e, t) {
1695
+ return this.#t.delete(e, t);
1696
+ }
1697
+ async scan(e, t = {}) {
1698
+ return this.#t.scan(e, t);
1699
+ }
1700
+ exportSnapshot(e = {}) {
1701
+ return Promise.reject(Y("db.exportSnapshot"));
1702
+ }
1703
+ importSnapshot(e) {
1704
+ return Promise.reject(Y("db.importSnapshot"));
1705
+ }
1706
+ reset() {
1707
+ return Promise.reject(Y("db.reset"));
1708
+ }
1709
+ compact() {
1710
+ return Promise.reject(Y("db.compact"));
1711
+ }
1712
+ rebuild() {
1713
+ return Promise.reject(Y("db.rebuild"));
1714
+ }
1715
+ async stats() {
1716
+ return this.#e.stats();
1717
+ }
1718
+ async storageInfo() {
1719
+ return this.#e.storageInfo();
1720
+ }
1721
+ async requestPersistence() {
1722
+ return this.#e.requestPersistence();
1723
+ }
1724
+ close() {
1725
+ return Promise.reject(Y("db.close"));
1726
+ }
1727
+ destroy() {
1728
+ return Promise.reject(Y("db.destroy"));
1729
+ }
1730
+ setFailpoint(e) {
1731
+ return Promise.reject(Y("db.setFailpoint"));
1732
+ }
1733
+ subscribe(e, t, n) {
1734
+ throw Y("db.subscribe");
1735
+ }
1736
+ watch(e, t, n) {
1737
+ throw Y("db.watch");
1738
+ }
1987
1739
  };
1988
- //# sourceMappingURL=moyodb-sdk.js.map
1740
+ function Y(e) {
1741
+ let t = /* @__PURE__ */ Error(`${e}() is not available inside openDB() migrations; use the provided migration transaction and wait for openDB() to resolve`);
1742
+ return t.name = "InvalidStateError", t;
1743
+ }
1744
+ function X(e, t) {
1745
+ if (typeof e != "string" || e.length === 0) throw TypeError(`${t}() database name must be a non-empty string`);
1746
+ return e;
1747
+ }
1748
+ var Cn = (e) => TypeError(e), wn = (e) => new E(e);
1749
+ function Z(e, t, n = Cn) {
1750
+ if (e === void 0) return {};
1751
+ if (typeof e != "object" || !e || Array.isArray(e)) throw n(t);
1752
+ return e;
1753
+ }
1754
+ function Tn(e, t, n = Cn) {
1755
+ if (typeof e != "number" || !Number.isSafeInteger(e) || e < 0) throw n(t);
1756
+ return e;
1757
+ }
1758
+ function Q(e, t, n) {
1759
+ return e === void 0 ? void 0 : Tn(e, t, n);
1760
+ }
1761
+ function En(e) {
1762
+ return Z(e, "open options must be an object", wn);
1763
+ }
1764
+ function Dn(e) {
1765
+ return Q(e, "version must be a non-negative safe integer", wn);
1766
+ }
1767
+ function $(e) {
1768
+ let { ttl: t } = Z(e, "put options must be an object"), n = Q(t, "ttl must be a non-negative safe integer");
1769
+ return n === void 0 ? {} : { ttl: n };
1770
+ }
1771
+ function On(e, t) {
1772
+ let { compression: n } = Z(e, t);
1773
+ if (n === void 0) return {};
1774
+ if (n !== !1 && n !== "gzip" && n !== "deflate") throw TypeError("compression must be \"gzip\", \"deflate\", or false");
1775
+ return { compression: n };
1776
+ }
1777
+ function kn(e) {
1778
+ return On(e, "createStore options must be an object");
1779
+ }
1780
+ function An(e) {
1781
+ return On(e, "exportSnapshot options must be an object");
1782
+ }
1783
+ function jn(e) {
1784
+ return Tn(e, "txid must be a non-negative safe integer");
1785
+ }
1786
+ function Mn(e) {
1787
+ let { stores: t, limit: n } = Z(e, "change feed options must be an object"), r = {};
1788
+ if (t !== void 0) {
1789
+ if (!Array.isArray(t) || !t.every((e) => typeof e == "string")) throw TypeError("stores must be an array of strings");
1790
+ for (let e of t) U(e);
1791
+ r.stores = [...t];
1792
+ }
1793
+ let i = Q(n, "limit must be a non-negative safe integer");
1794
+ return i !== void 0 && (r.limit = i), r;
1795
+ }
1796
+ function Nn(e) {
1797
+ if (e !== void 0) {
1798
+ if (typeof e != "function") throw new E("migrate must be a function");
1799
+ return e;
1800
+ }
1801
+ }
1802
+ function Pn(e) {
1803
+ if (e !== void 0) return W(e);
1804
+ }
1805
+ function Fn(e, t) {
1806
+ return K(e) === K(t);
1807
+ }
1808
+ async function In(e) {
1809
+ return W(await e.proxy.getIndexes());
1810
+ }
1811
+ function Ln() {
1812
+ if (!globalThis.isSecureContext) throw new y("moyodb requires a secure context (HTTPS)");
1813
+ if (typeof globalThis.navigator.storage.getDirectory != "function") throw new y("navigator.storage.getDirectory is unavailable");
1814
+ if (typeof globalThis.Worker != "function") throw new y("Worker is unavailable");
1815
+ if (globalThis.BroadcastChannel === void 0) throw new y("BroadcastChannel is unavailable");
1816
+ }
1817
+ async function Rn(e) {
1818
+ let t = globalThis.navigator;
1819
+ (e.requestPersistence ?? !0) && typeof t.storage.persist == "function" && await n(t.storage.persist(), 1e3, !1);
1820
+ }
1821
+ async function zn(e, t = {}) {
1822
+ let n = X(e, "openDB"), r = En(t), i = Dn(r.version), a = Nn(r.migrate), o = Pn(r.indexes);
1823
+ if (i === void 0 && a !== void 0) throw new E("migrate requires version");
1824
+ if (i === void 0 && o !== void 0) throw new E("indexes requires version");
1825
+ Ln(), await Rn(r);
1826
+ let s = await at(n, r), c = new yn(s), l = !1;
1827
+ try {
1828
+ if (i === void 0) return l = !0, c;
1829
+ let [e, t] = await Promise.all([c.getVersion(), In(s)]);
1830
+ if (i < e) throw new D(`cannot open database ${n} at schema version ${i}; current schema version is ${e}`);
1831
+ if (i === e) {
1832
+ if (o !== void 0 && !Fn(o, t)) throw new D(`database ${n} is already at schema version ${e}, but the requested index catalog does not match the committed schema`);
1833
+ return l = !0, c;
1834
+ }
1835
+ if (s.refs > 1) throw new b(`database ${n} is already open in this tab; close existing handles before migrating from version ${e} to ${i}`);
1836
+ if (!a) throw new D(`database ${n} requires migration from version ${e} to ${i}, but no migrate hook was provided`);
1837
+ let r = o ?? t;
1838
+ return await c.runSchemaMigration(e, i, a, q(r)), l = !0, c;
1839
+ } catch (e) {
1840
+ if (!l) try {
1841
+ await c.close();
1842
+ } catch {}
1843
+ throw O(e);
1844
+ }
1845
+ }
1846
+ async function Bn(e) {
1847
+ let t = X(e, "deleteDB");
1848
+ Ln(), await ct(t);
1849
+ }
1850
+ function Vn(e) {
1851
+ return it(X(e, "unsafeDebugCrashWorker"));
1852
+ }
1853
+ //#endregion
1854
+ export { je as ChangeFeedCompactedError, x as ConstraintError, ve as CorruptionError, b as DatabaseBusyError, C as DatabaseClosedError, Lt as INDEX_METADATA_STORE, xe as IndexNotFoundError, Ae as InjectedFailureError, Ne as InternalError, E as InvalidOpenOptionsError, Oe as InvalidRangeError, Ee as KeyTooLargeError, v as MoyoDbError, we as ReadonlyTransactionError, Me as RecoveryRequiredError, w as ReservedStoreNameError, T as SerializationError, ke as StorageError, ye as StoreExistsError, De as StoreNameTooLongError, be as StoreNotFoundError, S as TransactionClosedError, Se as UniqueIndexConstraintError, y as UnsupportedPlatformError, Te as ValueTooLargeError, D as VersionError, Ce as WriteTransactionAlreadyOpenError, U as assertPublicStoreName, Xt as cloneNormalizedIndexDefinitions, zt as compareNormalizedIndexDefinitions, L as compoundKey, Dt as compoundKeyRange, Kt as decodeIndexEntryKey, Wt as decodeIndexMetadataValue, Bn as deleteDB, R as encodeCompoundKeyParts, Gt as encodeIndexEntryKey, Ht as encodeIndexMetadataKey, Ut as encodeIndexMetadataValue, Tt as encodeIndexScalar, Yt as extractLogicalIndexKey, Bt as findIndexDefinition, G as indexDefinitionIdentity, wt as indexKey, qt as indexKeyExactRange, Jt as indexRangeToPhysicalRange, Vt as indexesForStore, Rt as isInternalStoreName, xt as jsonDecode, bt as jsonEncode, O as normalizeError, W as normalizeIndexDefinitions, zn as openDB, B as prefixRange, z as prefixSuccessor, K as serializeNormalizedIndexDefinitions, Et as splitCompoundKey, q as toPublicIndexDefinitions, St as u64Key, Vn as unsafeDebugCrashWorker, yt as utf8Decode, I as utf8Encode };
1855
+
1856
+ //# sourceMappingURL=moyodb-sdk.js.map