@moyodb/sdk 0.0.0-alpha.3 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +10 -2
- package/dist/LICENSE +202 -0
- package/dist/assets/worker-KDc8Khl_.js +4 -0
- package/dist/assets/worker-KDc8Khl_.js.map +1 -0
- package/dist/change-events.d.ts.map +1 -1
- package/dist/codec.d.ts.map +1 -1
- package/dist/compression.d.ts.map +1 -1
- package/dist/engine/README.md +26 -9
- package/dist/engine/moyodb_engine.d.ts +125 -54
- package/dist/engine/moyodb_engine.js +935 -391
- package/dist/engine/moyodb_engine_bg.wasm +0 -0
- package/dist/engine/moyodb_engine_bg.wasm.d.ts +57 -47
- package/dist/engine/package.json +3 -3
- package/dist/engine/snippets/moyodb-engine-8ebe6af0cc2e81b7/js/opfs_shim.js +602 -0
- package/dist/engine/snippets/moyodb-engine-cff0a0e2441a7a8f/js/opfs_shim.js +192 -81
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/indexing.d.ts +8 -0
- package/dist/indexing.d.ts.map +1 -1
- package/dist/moyodb-sdk.js +2034 -1985
- package/dist/moyodb-sdk.js.map +1 -1
- package/dist/registry.d.ts +3 -1
- package/dist/registry.d.ts.map +1 -1
- package/dist/subscriptions.d.ts.map +1 -1
- package/dist/types.d.ts +29 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/worker-api.d.ts +50 -37
- package/dist/worker-api.d.ts.map +1 -1
- package/dist/worker-client.d.ts +13 -2
- package/dist/worker-client.d.ts.map +1 -1
- package/dist/worker-protocol.d.ts +42 -3
- package/dist/worker-protocol.d.ts.map +1 -1
- package/dist/worker-server.d.ts.map +1 -1
- package/package.json +13 -9
- package/dist/assets/worker-CaOzbBz1.js +0 -2351
- package/dist/assets/worker-CaOzbBz1.js.map +0 -1
- package/dist/moyodb-sdk.umd.cjs +0 -2
- package/dist/moyodb-sdk.umd.cjs.map +0 -1
|
@@ -1,2351 +0,0 @@
|
|
|
1
|
-
function k(n) {
|
|
2
|
-
return n !== null && typeof n == "object";
|
|
3
|
-
}
|
|
4
|
-
function Q(n, e) {
|
|
5
|
-
return n < e ? -1 : n > e ? 1 : 0;
|
|
6
|
-
}
|
|
7
|
-
const J = 1, ve = "moyodb:worker-protocol:ready", Be = "moyodb:worker-protocol:request", le = "moyodb:worker-protocol:response", at = [
|
|
8
|
-
"open",
|
|
9
|
-
"close",
|
|
10
|
-
"destroy",
|
|
11
|
-
"deleteDB",
|
|
12
|
-
"begin",
|
|
13
|
-
"commit",
|
|
14
|
-
"rollback",
|
|
15
|
-
"createStore",
|
|
16
|
-
"dropStore",
|
|
17
|
-
"clearStore",
|
|
18
|
-
"get",
|
|
19
|
-
"getMany",
|
|
20
|
-
"has",
|
|
21
|
-
"put",
|
|
22
|
-
"putMany",
|
|
23
|
-
"delete",
|
|
24
|
-
"deleteMany",
|
|
25
|
-
"applyBatch",
|
|
26
|
-
"scan",
|
|
27
|
-
"getByIndex",
|
|
28
|
-
"scanByIndex",
|
|
29
|
-
"getIndexes",
|
|
30
|
-
"reconcileIndexes",
|
|
31
|
-
"listStores",
|
|
32
|
-
"getVersion",
|
|
33
|
-
"changesSince",
|
|
34
|
-
"setSchemaVersion",
|
|
35
|
-
"exportSnapshot",
|
|
36
|
-
"importSnapshot",
|
|
37
|
-
"reset",
|
|
38
|
-
"compact",
|
|
39
|
-
"rebuild",
|
|
40
|
-
"stats",
|
|
41
|
-
"storageInfo",
|
|
42
|
-
"requestPersistence",
|
|
43
|
-
"setFailpoint"
|
|
44
|
-
], ct = "moyodb:packed-binary-list:v1", lt = "moyodb:packed-nullable-binary-list:v1", ht = "moyodb:packed-scan-rows:v1", ut = "moyodb:packed-batch-ops:v1", Ne = 0, De = 1, x = 4294967295, Ue = 16 * 1024 * 1024, K = [], dt = new Set(at);
|
|
45
|
-
function ft(n) {
|
|
46
|
-
return typeof n == "string" && dt.has(n);
|
|
47
|
-
}
|
|
48
|
-
function yt(n) {
|
|
49
|
-
return k(n) && n.type === Be && n.version === J && typeof n.id == "number" && Number.isSafeInteger(n.id) && typeof n.command == "string" && Array.isArray(n.args);
|
|
50
|
-
}
|
|
51
|
-
function pt(n) {
|
|
52
|
-
return k(n) && (n.type === Be || n.type === le || n.type === ve);
|
|
53
|
-
}
|
|
54
|
-
function gt(n) {
|
|
55
|
-
if (k(n)) {
|
|
56
|
-
const e = $(n.code);
|
|
57
|
-
return {
|
|
58
|
-
name: $(n.name) ?? e ?? "Error",
|
|
59
|
-
code: e,
|
|
60
|
-
message: $(n.message) ?? String(n),
|
|
61
|
-
stack: $(n.stack)
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
return {
|
|
65
|
-
name: "Error",
|
|
66
|
-
message: String(n)
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
function f(n, e, t = n) {
|
|
70
|
-
const r = new Error(e);
|
|
71
|
-
return r.name = n, Object.defineProperty(r, "code", {
|
|
72
|
-
value: t,
|
|
73
|
-
enumerable: !0,
|
|
74
|
-
configurable: !0
|
|
75
|
-
}), r;
|
|
76
|
-
}
|
|
77
|
-
function mt(n, e) {
|
|
78
|
-
if (n === "getMany" || n === "deleteMany") {
|
|
79
|
-
const [t, r, o] = e;
|
|
80
|
-
if (ie(o))
|
|
81
|
-
return [t, r, Ke(o)];
|
|
82
|
-
}
|
|
83
|
-
if (n === "putMany") {
|
|
84
|
-
const [t, r, o, i] = e;
|
|
85
|
-
if (ie(o))
|
|
86
|
-
return [t, r, We(o), i];
|
|
87
|
-
}
|
|
88
|
-
if (n === "applyBatch") {
|
|
89
|
-
const [t, r, o] = e;
|
|
90
|
-
if (Fe(o))
|
|
91
|
-
return [t, r, $e(o)];
|
|
92
|
-
}
|
|
93
|
-
return e;
|
|
94
|
-
}
|
|
95
|
-
function pe(n) {
|
|
96
|
-
return ie(n) ? n.bytes : null;
|
|
97
|
-
}
|
|
98
|
-
function wt(n) {
|
|
99
|
-
return Fe(n) ? n.bytes : null;
|
|
100
|
-
}
|
|
101
|
-
function bt(n) {
|
|
102
|
-
return Ke({ bytes: n });
|
|
103
|
-
}
|
|
104
|
-
function Et(n) {
|
|
105
|
-
return We({ bytes: n });
|
|
106
|
-
}
|
|
107
|
-
function St(n) {
|
|
108
|
-
return Ot({ bytes: n });
|
|
109
|
-
}
|
|
110
|
-
function kt(n) {
|
|
111
|
-
return $e({ bytes: n });
|
|
112
|
-
}
|
|
113
|
-
function xt(n) {
|
|
114
|
-
return Rt({ bytes: n });
|
|
115
|
-
}
|
|
116
|
-
function _t(n, e) {
|
|
117
|
-
if (n === "getMany") {
|
|
118
|
-
const t = e;
|
|
119
|
-
if (Array.isArray(t) && t.length > 0) {
|
|
120
|
-
const r = Tt(t);
|
|
121
|
-
if (r)
|
|
122
|
-
return {
|
|
123
|
-
result: r,
|
|
124
|
-
transfer: [r.bytes.buffer]
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
return {
|
|
128
|
-
result: e,
|
|
129
|
-
transfer: ge(e)
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
if (n === "scan" || n === "scanByIndex") {
|
|
133
|
-
const t = e;
|
|
134
|
-
if (Array.isArray(t) && t.length > 0) {
|
|
135
|
-
const r = Lt(t);
|
|
136
|
-
if (r)
|
|
137
|
-
return {
|
|
138
|
-
result: r,
|
|
139
|
-
transfer: [r.bytes.buffer]
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
return {
|
|
143
|
-
result: e,
|
|
144
|
-
transfer: ge(e)
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
return n === "get" || n === "getByIndex" || n === "exportSnapshot" ? {
|
|
148
|
-
result: e,
|
|
149
|
-
transfer: Ct(e)
|
|
150
|
-
} : n === "changesSince" ? {
|
|
151
|
-
result: e,
|
|
152
|
-
transfer: Pt(e)
|
|
153
|
-
} : {
|
|
154
|
-
result: e,
|
|
155
|
-
transfer: []
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
function ge(n) {
|
|
159
|
-
const e = G(n);
|
|
160
|
-
if (e !== null)
|
|
161
|
-
return [e];
|
|
162
|
-
if (n == null || typeof n != "object")
|
|
163
|
-
return K;
|
|
164
|
-
const t = /* @__PURE__ */ new Set();
|
|
165
|
-
return oe(n, t, /* @__PURE__ */ new WeakSet()), t.size === 0 ? K : Array.from(t);
|
|
166
|
-
}
|
|
167
|
-
function Ct(n) {
|
|
168
|
-
const e = G(n);
|
|
169
|
-
return e === null ? K : [e];
|
|
170
|
-
}
|
|
171
|
-
function G(n) {
|
|
172
|
-
if (n instanceof ArrayBuffer)
|
|
173
|
-
return n;
|
|
174
|
-
if (ArrayBuffer.isView(n)) {
|
|
175
|
-
const e = n.buffer;
|
|
176
|
-
if (e instanceof ArrayBuffer && n.byteOffset === 0 && n.byteLength === e.byteLength)
|
|
177
|
-
return e;
|
|
178
|
-
}
|
|
179
|
-
return null;
|
|
180
|
-
}
|
|
181
|
-
function Pt(n) {
|
|
182
|
-
if (!k(n) || !Array.isArray(n.changes) || n.changes.length === 0)
|
|
183
|
-
return K;
|
|
184
|
-
let e = null;
|
|
185
|
-
for (const t of n.changes) {
|
|
186
|
-
if (!k(t))
|
|
187
|
-
continue;
|
|
188
|
-
const r = G(t.key);
|
|
189
|
-
r !== null && (e ??= /* @__PURE__ */ new Set(), e.add(r));
|
|
190
|
-
const o = G(t.value);
|
|
191
|
-
o !== null && (e ??= /* @__PURE__ */ new Set(), e.add(o));
|
|
192
|
-
}
|
|
193
|
-
return e === null || e.size === 0 ? K : Array.from(e);
|
|
194
|
-
}
|
|
195
|
-
function oe(n, e, t) {
|
|
196
|
-
if (n == null)
|
|
197
|
-
return;
|
|
198
|
-
if (n instanceof ArrayBuffer) {
|
|
199
|
-
e.add(n);
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
if (ArrayBuffer.isView(n)) {
|
|
203
|
-
const i = n.buffer;
|
|
204
|
-
i instanceof ArrayBuffer && n.byteOffset === 0 && n.byteLength === i.byteLength && e.add(i);
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
if (typeof n != "object")
|
|
208
|
-
return;
|
|
209
|
-
const r = n;
|
|
210
|
-
if (t.has(r))
|
|
211
|
-
return;
|
|
212
|
-
if (t.add(r), Array.isArray(n)) {
|
|
213
|
-
for (const i of n)
|
|
214
|
-
oe(i, e, t);
|
|
215
|
-
return;
|
|
216
|
-
}
|
|
217
|
-
const o = n;
|
|
218
|
-
for (const i in o)
|
|
219
|
-
Object.prototype.hasOwnProperty.call(o, i) && oe(o[i], e, t);
|
|
220
|
-
}
|
|
221
|
-
function Ke(n) {
|
|
222
|
-
const e = n.bytes;
|
|
223
|
-
if (!(e instanceof Uint8Array) || e.byteLength < 4)
|
|
224
|
-
throw f("WorkerProtocolError", "invalid packed binary-list payload");
|
|
225
|
-
const t = new DataView(e.buffer, e.byteOffset, e.byteLength), r = t.getUint32(0, !0), o = M(r, 4, "packed binary-list metadata"), i = T(4, o, "packed binary-list header");
|
|
226
|
-
if (i > e.byteLength)
|
|
227
|
-
throw f("WorkerProtocolError", "packed binary-list metadata exceeds payload length");
|
|
228
|
-
const s = new Array(r);
|
|
229
|
-
let a = 4, c = i;
|
|
230
|
-
for (let l = 0; l < r; l += 1) {
|
|
231
|
-
const h = t.getUint32(a, !0);
|
|
232
|
-
a += 4;
|
|
233
|
-
const d = c + h;
|
|
234
|
-
if (d > x || d > e.byteLength)
|
|
235
|
-
throw f("WorkerProtocolError", "packed binary-list item exceeds payload length");
|
|
236
|
-
s[l] = e.subarray(c, d), c = d;
|
|
237
|
-
}
|
|
238
|
-
if (c !== e.byteLength)
|
|
239
|
-
throw f("WorkerProtocolError", "packed binary-list payload has trailing bytes");
|
|
240
|
-
return s;
|
|
241
|
-
}
|
|
242
|
-
function We(n) {
|
|
243
|
-
const e = n.bytes;
|
|
244
|
-
if (!(e instanceof Uint8Array) || e.byteLength < 4)
|
|
245
|
-
throw f("WorkerProtocolError", "invalid packed putMany payload");
|
|
246
|
-
const t = new DataView(e.buffer, e.byteOffset, e.byteLength), r = t.getUint32(0, !0);
|
|
247
|
-
if (r % 2 !== 0)
|
|
248
|
-
throw f("WorkerProtocolError", "packed putMany payload has an odd item count");
|
|
249
|
-
const o = M(r, 4, "packed putMany metadata"), i = T(4, o, "packed putMany header");
|
|
250
|
-
if (i > e.byteLength)
|
|
251
|
-
throw f("WorkerProtocolError", "packed putMany metadata exceeds payload length");
|
|
252
|
-
const s = r / 2, a = new Array(s);
|
|
253
|
-
let c = 4, l = i;
|
|
254
|
-
for (let h = 0; h < s; h += 1) {
|
|
255
|
-
const d = t.getUint32(c, !0), g = t.getUint32(c + 4, !0);
|
|
256
|
-
c += 8;
|
|
257
|
-
const y = l + d;
|
|
258
|
-
if (y > x || y > e.byteLength)
|
|
259
|
-
throw f("WorkerProtocolError", "packed putMany key exceeds payload length");
|
|
260
|
-
const u = y + g;
|
|
261
|
-
if (u > x || u > e.byteLength)
|
|
262
|
-
throw f("WorkerProtocolError", "packed putMany value exceeds payload length");
|
|
263
|
-
a[h] = [e.subarray(l, y), e.subarray(y, u)], l = u;
|
|
264
|
-
}
|
|
265
|
-
if (l !== e.byteLength)
|
|
266
|
-
throw f("WorkerProtocolError", "packed putMany payload has trailing bytes");
|
|
267
|
-
return a;
|
|
268
|
-
}
|
|
269
|
-
function Ot(n) {
|
|
270
|
-
const e = n.bytes;
|
|
271
|
-
if (!(e instanceof Uint8Array) || e.byteLength < 4)
|
|
272
|
-
throw f("WorkerProtocolError", "invalid packed putMany payload");
|
|
273
|
-
const t = new DataView(e.buffer, e.byteOffset, e.byteLength), r = t.getUint32(0, !0);
|
|
274
|
-
if (r % 2 !== 0)
|
|
275
|
-
throw f("WorkerProtocolError", "packed putMany payload has an odd item count");
|
|
276
|
-
const o = M(r, 4, "packed putMany metadata"), i = T(4, o, "packed putMany header");
|
|
277
|
-
if (i > e.byteLength)
|
|
278
|
-
throw f("WorkerProtocolError", "packed putMany metadata exceeds payload length");
|
|
279
|
-
const s = r / 2, a = new Array(s);
|
|
280
|
-
let c = 4, l = i;
|
|
281
|
-
for (let h = 0; h < s; h += 1) {
|
|
282
|
-
const d = t.getUint32(c, !0), g = t.getUint32(c + 4, !0);
|
|
283
|
-
c += 8;
|
|
284
|
-
const y = l + d;
|
|
285
|
-
if (y > x || y > e.byteLength)
|
|
286
|
-
throw f("WorkerProtocolError", "packed putMany key exceeds payload length");
|
|
287
|
-
const u = y + g;
|
|
288
|
-
if (u > x || u > e.byteLength)
|
|
289
|
-
throw f("WorkerProtocolError", "packed putMany value exceeds payload length");
|
|
290
|
-
a[h] = e.subarray(l, y), l = u;
|
|
291
|
-
}
|
|
292
|
-
if (l !== e.byteLength)
|
|
293
|
-
throw f("WorkerProtocolError", "packed putMany payload has trailing bytes");
|
|
294
|
-
return a;
|
|
295
|
-
}
|
|
296
|
-
function Tt(n) {
|
|
297
|
-
const e = n.length, t = M(e, 5, "packed nullable binary-list metadata");
|
|
298
|
-
let r = 0;
|
|
299
|
-
for (let h = 0; h < e; h += 1) {
|
|
300
|
-
const d = n[h];
|
|
301
|
-
if (d === null)
|
|
302
|
-
continue;
|
|
303
|
-
if (!(d instanceof Uint8Array))
|
|
304
|
-
throw f(
|
|
305
|
-
"WorkerProtocolError",
|
|
306
|
-
"nullable binary-list payload item is not a Uint8Array or null"
|
|
307
|
-
);
|
|
308
|
-
const g = W(d.byteLength, "nullable binary-list item length");
|
|
309
|
-
if (r += g, r > x)
|
|
310
|
-
throw f(
|
|
311
|
-
"WorkerProtocolError",
|
|
312
|
-
"packed nullable binary-list payload exceeds 32-bit payload limit"
|
|
313
|
-
);
|
|
314
|
-
}
|
|
315
|
-
const o = T(4, t, "packed nullable binary-list header"), i = T(o, r, "packed nullable binary-list total");
|
|
316
|
-
if (i > Ue)
|
|
317
|
-
return null;
|
|
318
|
-
const s = new Uint8Array(i), a = new DataView(s.buffer);
|
|
319
|
-
a.setUint32(0, e, !0);
|
|
320
|
-
let c = 4, l = o;
|
|
321
|
-
for (let h = 0; h < e; h += 1) {
|
|
322
|
-
const d = n[h];
|
|
323
|
-
if (d === null) {
|
|
324
|
-
a.setUint8(c, 0), a.setUint32(c + 1, 0, !0), c += 5;
|
|
325
|
-
continue;
|
|
326
|
-
}
|
|
327
|
-
const g = d.byteLength;
|
|
328
|
-
a.setUint8(c, 1), a.setUint32(c + 1, g, !0), c += 5, s.set(d, l), l += g;
|
|
329
|
-
}
|
|
330
|
-
return {
|
|
331
|
-
__moyodbPacked: lt,
|
|
332
|
-
bytes: s
|
|
333
|
-
};
|
|
334
|
-
}
|
|
335
|
-
function Lt(n) {
|
|
336
|
-
const e = n.length, t = M(e, 8, "packed scan row metadata");
|
|
337
|
-
let r = 0;
|
|
338
|
-
for (let h = 0; h < e; h += 1) {
|
|
339
|
-
const d = n[h];
|
|
340
|
-
if (!k(d) || !(d.key instanceof Uint8Array) || !(d.value instanceof Uint8Array))
|
|
341
|
-
throw f("WorkerProtocolError", "scan row payload key/value is not a Uint8Array");
|
|
342
|
-
const g = W(d.key.byteLength, "scan row key length"), y = W(d.value.byteLength, "scan row value length");
|
|
343
|
-
if (r += g + y, r > x)
|
|
344
|
-
throw f("WorkerProtocolError", "packed scan row payload exceeds 32-bit payload limit");
|
|
345
|
-
}
|
|
346
|
-
const o = T(4, t, "packed scan row header"), i = T(o, r, "packed scan row total");
|
|
347
|
-
if (i > Ue)
|
|
348
|
-
return null;
|
|
349
|
-
const s = new Uint8Array(i), a = new DataView(s.buffer);
|
|
350
|
-
a.setUint32(0, e, !0);
|
|
351
|
-
let c = 4, l = o;
|
|
352
|
-
for (let h = 0; h < e; h += 1) {
|
|
353
|
-
const d = n[h], g = d.key, y = d.value, u = g.byteLength, S = y.byteLength;
|
|
354
|
-
a.setUint32(c, u, !0), a.setUint32(c + 4, S, !0), c += 8, s.set(g, l), l += u, s.set(y, l), l += S;
|
|
355
|
-
}
|
|
356
|
-
return {
|
|
357
|
-
__moyodbPacked: ht,
|
|
358
|
-
bytes: s
|
|
359
|
-
};
|
|
360
|
-
}
|
|
361
|
-
function $e(n) {
|
|
362
|
-
const e = n.bytes;
|
|
363
|
-
if (!(e instanceof Uint8Array) || e.byteLength < 4)
|
|
364
|
-
throw f("WorkerProtocolError", "invalid packed batch payload");
|
|
365
|
-
const t = new DataView(e.buffer, e.byteOffset, e.byteLength), r = t.getUint32(0, !0), o = M(r, 9, "packed batch metadata"), i = T(4, o, "packed batch header");
|
|
366
|
-
if (i > e.byteLength)
|
|
367
|
-
throw f("WorkerProtocolError", "packed batch metadata exceeds payload length");
|
|
368
|
-
const s = new Array(r);
|
|
369
|
-
let a = 4, c = i;
|
|
370
|
-
for (let l = 0; l < r; l += 1) {
|
|
371
|
-
const h = t.getUint8(a), d = t.getUint32(a + 1, !0), g = t.getUint32(a + 5, !0);
|
|
372
|
-
a += 9;
|
|
373
|
-
const y = c + d;
|
|
374
|
-
if (y > x || y > e.byteLength)
|
|
375
|
-
throw f("WorkerProtocolError", "packed batch key exceeds payload length");
|
|
376
|
-
const u = e.subarray(c, y);
|
|
377
|
-
if (c = y, h === Ne) {
|
|
378
|
-
if (g !== 0)
|
|
379
|
-
throw f("WorkerProtocolError", "packed delete operation has a value payload");
|
|
380
|
-
s[l] = { kind: "delete", key: u };
|
|
381
|
-
continue;
|
|
382
|
-
}
|
|
383
|
-
if (h !== De)
|
|
384
|
-
throw f("WorkerProtocolError", `packed batch operation has invalid kind byte: ${h}`);
|
|
385
|
-
const S = c + g;
|
|
386
|
-
if (S > x || S > e.byteLength)
|
|
387
|
-
throw f("WorkerProtocolError", "packed batch value exceeds payload length");
|
|
388
|
-
s[l] = { kind: "put", key: u, value: e.subarray(c, S) }, c = S;
|
|
389
|
-
}
|
|
390
|
-
if (c !== e.byteLength)
|
|
391
|
-
throw f("WorkerProtocolError", "packed batch payload has trailing bytes");
|
|
392
|
-
return s;
|
|
393
|
-
}
|
|
394
|
-
function Rt(n) {
|
|
395
|
-
const e = n.bytes;
|
|
396
|
-
if (!(e instanceof Uint8Array) || e.byteLength < 4)
|
|
397
|
-
throw f("WorkerProtocolError", "invalid packed batch payload");
|
|
398
|
-
const t = new DataView(e.buffer, e.byteOffset, e.byteLength), r = t.getUint32(0, !0), o = M(r, 9, "packed batch metadata"), i = T(4, o, "packed batch header");
|
|
399
|
-
if (i > e.byteLength)
|
|
400
|
-
throw f("WorkerProtocolError", "packed batch metadata exceeds payload length");
|
|
401
|
-
const s = new Array(r);
|
|
402
|
-
let a = 4, c = i;
|
|
403
|
-
for (let l = 0; l < r; l += 1) {
|
|
404
|
-
const h = t.getUint8(a), d = t.getUint32(a + 1, !0), g = t.getUint32(a + 5, !0);
|
|
405
|
-
a += 9;
|
|
406
|
-
const y = c + d;
|
|
407
|
-
if (y > x || y > e.byteLength)
|
|
408
|
-
throw f("WorkerProtocolError", "packed batch key exceeds payload length");
|
|
409
|
-
const u = e.subarray(c, y);
|
|
410
|
-
if (c = y, h === Ne) {
|
|
411
|
-
if (g !== 0)
|
|
412
|
-
throw f("WorkerProtocolError", "packed delete operation has a value payload");
|
|
413
|
-
s[l] = { kind: "delete", key: u };
|
|
414
|
-
continue;
|
|
415
|
-
}
|
|
416
|
-
if (h !== De)
|
|
417
|
-
throw f("WorkerProtocolError", `packed batch operation has invalid kind byte: ${h}`);
|
|
418
|
-
const S = c + g;
|
|
419
|
-
if (S > x || S > e.byteLength)
|
|
420
|
-
throw f("WorkerProtocolError", "packed batch value exceeds payload length");
|
|
421
|
-
s[l] = { kind: "put", key: u }, c = S;
|
|
422
|
-
}
|
|
423
|
-
if (c !== e.byteLength)
|
|
424
|
-
throw f("WorkerProtocolError", "packed batch payload has trailing bytes");
|
|
425
|
-
return s;
|
|
426
|
-
}
|
|
427
|
-
function ie(n) {
|
|
428
|
-
return k(n) && n.__moyodbPacked === ct && n.bytes instanceof Uint8Array;
|
|
429
|
-
}
|
|
430
|
-
function Fe(n) {
|
|
431
|
-
return k(n) && n.__moyodbPacked === ut && n.bytes instanceof Uint8Array;
|
|
432
|
-
}
|
|
433
|
-
function M(n, e, t) {
|
|
434
|
-
if (!Number.isSafeInteger(n) || n < 0)
|
|
435
|
-
throw f("WorkerProtocolError", `${t} count is invalid: ${n}`);
|
|
436
|
-
return W(n * e, t);
|
|
437
|
-
}
|
|
438
|
-
function T(n, e, t) {
|
|
439
|
-
const r = n + e;
|
|
440
|
-
return W(r, t);
|
|
441
|
-
}
|
|
442
|
-
function W(n, e) {
|
|
443
|
-
if (!Number.isSafeInteger(n) || n < 0 || n > x)
|
|
444
|
-
throw f("WorkerProtocolError", `${e} exceeds 32-bit payload limit`);
|
|
445
|
-
return n;
|
|
446
|
-
}
|
|
447
|
-
function $(n) {
|
|
448
|
-
return typeof n == "string" ? n : void 0;
|
|
449
|
-
}
|
|
450
|
-
function At(n, e = self) {
|
|
451
|
-
const t = (r) => {
|
|
452
|
-
Mt(e, n, r.data);
|
|
453
|
-
};
|
|
454
|
-
return e.addEventListener("message", t), e.postMessage({
|
|
455
|
-
type: ve,
|
|
456
|
-
version: J
|
|
457
|
-
}), {
|
|
458
|
-
close() {
|
|
459
|
-
e.removeEventListener("message", t);
|
|
460
|
-
}
|
|
461
|
-
};
|
|
462
|
-
}
|
|
463
|
-
async function Mt(n, e, t) {
|
|
464
|
-
if (!pt(t))
|
|
465
|
-
return;
|
|
466
|
-
const r = typeof t.id == "number" && Number.isSafeInteger(t.id) ? t.id : 0;
|
|
467
|
-
if (!yt(t)) {
|
|
468
|
-
B(
|
|
469
|
-
n,
|
|
470
|
-
F(r, f("WorkerProtocolError", "invalid worker protocol request"))
|
|
471
|
-
);
|
|
472
|
-
return;
|
|
473
|
-
}
|
|
474
|
-
if (!ft(t.command)) {
|
|
475
|
-
B(
|
|
476
|
-
n,
|
|
477
|
-
F(
|
|
478
|
-
t.id,
|
|
479
|
-
f("WorkerProtocolError", `unsupported worker command: ${String(t.command)}`)
|
|
480
|
-
)
|
|
481
|
-
);
|
|
482
|
-
return;
|
|
483
|
-
}
|
|
484
|
-
const o = e[t.command];
|
|
485
|
-
if (typeof o != "function") {
|
|
486
|
-
B(
|
|
487
|
-
n,
|
|
488
|
-
F(
|
|
489
|
-
t.id,
|
|
490
|
-
f("WorkerProtocolError", `worker command is not implemented: ${t.command}`)
|
|
491
|
-
)
|
|
492
|
-
);
|
|
493
|
-
return;
|
|
494
|
-
}
|
|
495
|
-
try {
|
|
496
|
-
const i = It(e, t.command, t.args), s = i === null ? await o.apply(
|
|
497
|
-
e,
|
|
498
|
-
mt(t.command, t.args)
|
|
499
|
-
) : await i, a = _t(t.command, s);
|
|
500
|
-
B(n, vt(t.id, a.result), a.transfer);
|
|
501
|
-
} catch (i) {
|
|
502
|
-
B(n, F(t.id, i));
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
function It(n, e, t) {
|
|
506
|
-
const r = n;
|
|
507
|
-
if (e === "getMany" || e === "deleteMany") {
|
|
508
|
-
const [o, i, s] = t;
|
|
509
|
-
if (typeof o != "number" || typeof i != "string")
|
|
510
|
-
return null;
|
|
511
|
-
const a = pe(s);
|
|
512
|
-
return a === null ? null : e === "getMany" && typeof r.getManyPacked == "function" ? r.getManyPacked(o, i, a) : e === "deleteMany" && typeof r.deleteManyPacked == "function" ? r.deleteManyPacked(o, i, a) : null;
|
|
513
|
-
}
|
|
514
|
-
if (e === "putMany") {
|
|
515
|
-
const [o, i, s, a] = t;
|
|
516
|
-
if (typeof o != "number" || typeof i != "string" || typeof r.putManyPacked != "function")
|
|
517
|
-
return null;
|
|
518
|
-
const c = pe(s);
|
|
519
|
-
return c === null ? null : r.putManyPacked(o, i, c, a);
|
|
520
|
-
}
|
|
521
|
-
if (e === "applyBatch") {
|
|
522
|
-
const [o, i, s] = t;
|
|
523
|
-
if (typeof o != "number" || typeof i != "string" || typeof r.applyBatchPacked != "function")
|
|
524
|
-
return null;
|
|
525
|
-
const a = wt(s);
|
|
526
|
-
return a === null ? null : r.applyBatchPacked(o, i, a);
|
|
527
|
-
}
|
|
528
|
-
return null;
|
|
529
|
-
}
|
|
530
|
-
function vt(n, e) {
|
|
531
|
-
return {
|
|
532
|
-
type: le,
|
|
533
|
-
version: J,
|
|
534
|
-
id: n,
|
|
535
|
-
ok: !0,
|
|
536
|
-
result: e
|
|
537
|
-
};
|
|
538
|
-
}
|
|
539
|
-
function F(n, e) {
|
|
540
|
-
return {
|
|
541
|
-
type: le,
|
|
542
|
-
version: J,
|
|
543
|
-
id: n,
|
|
544
|
-
ok: !1,
|
|
545
|
-
error: gt(e)
|
|
546
|
-
};
|
|
547
|
-
}
|
|
548
|
-
function B(n, e, t = []) {
|
|
549
|
-
if (e.ok && t.length > 0)
|
|
550
|
-
try {
|
|
551
|
-
n.postMessage(e, t);
|
|
552
|
-
return;
|
|
553
|
-
} catch {
|
|
554
|
-
}
|
|
555
|
-
n.postMessage(e);
|
|
556
|
-
}
|
|
557
|
-
const Bt = new TextEncoder();
|
|
558
|
-
new TextDecoder();
|
|
559
|
-
const Nt = 16, Dt = 32, Ut = 33, Kt = 48, Wt = 64, $t = 80, U = 0, Ve = 255, se = 0, Ft = 0xffffffffffffffffn;
|
|
560
|
-
function C(n) {
|
|
561
|
-
return Bt.encode(n);
|
|
562
|
-
}
|
|
563
|
-
function me(n) {
|
|
564
|
-
return qt(n);
|
|
565
|
-
}
|
|
566
|
-
function L(n) {
|
|
567
|
-
return zt(n);
|
|
568
|
-
}
|
|
569
|
-
function Vt(n) {
|
|
570
|
-
const e = [], t = [];
|
|
571
|
-
for (let r = 0; r < n.length; r += 1) {
|
|
572
|
-
const o = n[r];
|
|
573
|
-
if (o !== U) {
|
|
574
|
-
t.push(o);
|
|
575
|
-
continue;
|
|
576
|
-
}
|
|
577
|
-
const i = n[r + 1];
|
|
578
|
-
if (i === Ve) {
|
|
579
|
-
t.push(U), r += 1;
|
|
580
|
-
continue;
|
|
581
|
-
}
|
|
582
|
-
if (i === se) {
|
|
583
|
-
e.push(Uint8Array.from(t)), t.length = 0, r += 1;
|
|
584
|
-
continue;
|
|
585
|
-
}
|
|
586
|
-
throw new TypeError("invalid compound key encoding");
|
|
587
|
-
}
|
|
588
|
-
if (t.length !== 0)
|
|
589
|
-
throw new TypeError("compound key terminated unexpectedly");
|
|
590
|
-
return e;
|
|
591
|
-
}
|
|
592
|
-
function ae(n) {
|
|
593
|
-
const e = n.slice();
|
|
594
|
-
for (let t = e.length - 1; t >= 0; t -= 1)
|
|
595
|
-
if (e[t] !== 255)
|
|
596
|
-
return e[t] += 1, e.subarray(0, t + 1);
|
|
597
|
-
return null;
|
|
598
|
-
}
|
|
599
|
-
function qe(n) {
|
|
600
|
-
const e = ae(n);
|
|
601
|
-
return e ? { gte: n, lt: e } : { gte: n };
|
|
602
|
-
}
|
|
603
|
-
function qt(n) {
|
|
604
|
-
if (n === null)
|
|
605
|
-
return Uint8Array.of(Nt);
|
|
606
|
-
if (typeof n == "boolean")
|
|
607
|
-
return Uint8Array.of(n ? Ut : Dt);
|
|
608
|
-
if (typeof n == "number")
|
|
609
|
-
return Ht(n);
|
|
610
|
-
if (typeof n == "string") {
|
|
611
|
-
const e = C(n), t = new Uint8Array(1 + e.length);
|
|
612
|
-
return t[0] = Wt, t.set(e, 1), t;
|
|
613
|
-
}
|
|
614
|
-
if (n instanceof Uint8Array) {
|
|
615
|
-
const e = new Uint8Array(1 + n.length);
|
|
616
|
-
return e[0] = $t, e.set(n, 1), e;
|
|
617
|
-
}
|
|
618
|
-
throw new TypeError("compound key parts must be string, number, boolean, null, or Uint8Array");
|
|
619
|
-
}
|
|
620
|
-
function zt(n) {
|
|
621
|
-
let e = 0;
|
|
622
|
-
for (const o of n) {
|
|
623
|
-
for (const i of o)
|
|
624
|
-
e += i === U ? 2 : 1;
|
|
625
|
-
e += 2;
|
|
626
|
-
}
|
|
627
|
-
const t = new Uint8Array(e);
|
|
628
|
-
let r = 0;
|
|
629
|
-
for (const o of n) {
|
|
630
|
-
for (const i of o)
|
|
631
|
-
i === U ? (t[r] = U, t[r + 1] = Ve, r += 2) : (t[r] = i, r += 1);
|
|
632
|
-
t[r] = se, t[r + 1] = se, r += 2;
|
|
633
|
-
}
|
|
634
|
-
return t;
|
|
635
|
-
}
|
|
636
|
-
function Ht(n) {
|
|
637
|
-
if (!Number.isFinite(n))
|
|
638
|
-
throw new TypeError("compound key number parts must be finite");
|
|
639
|
-
const e = Object.is(n, -0) ? 0 : n, t = new ArrayBuffer(9), r = new DataView(t);
|
|
640
|
-
r.setUint8(0, Kt), r.setFloat64(1, e, !1);
|
|
641
|
-
let o = r.getBigUint64(1, !1);
|
|
642
|
-
return o = (o & 1n << 63n) !== 0n ? ~o & Ft : o ^ 1n << 63n, r.setBigUint64(1, o, !1), new Uint8Array(t);
|
|
643
|
-
}
|
|
644
|
-
const z = C("BDBZVAL1"), ze = C("BDBZSNP1"), he = 1, A = 18, Y = 0, He = 1, Ge = 2, Ye = 2, Gt = 3 << Ye, Yt = 1024, we = 8 * 1024 * 1024, be = 256 * 1024 * 1024, jt = (() => {
|
|
645
|
-
const n = new Uint32Array(256);
|
|
646
|
-
for (let e = 0; e < 256; e += 1) {
|
|
647
|
-
let t = e;
|
|
648
|
-
for (let r = 0; r < 8; r += 1)
|
|
649
|
-
(t & 1) !== 0 ? t = t >>> 1 ^ 3988292384 : t >>>= 1;
|
|
650
|
-
n[e] = t >>> 0;
|
|
651
|
-
}
|
|
652
|
-
return n;
|
|
653
|
-
})();
|
|
654
|
-
function E(n, e) {
|
|
655
|
-
const t = new Error(e);
|
|
656
|
-
return t.name = n, t;
|
|
657
|
-
}
|
|
658
|
-
function je(n) {
|
|
659
|
-
switch (n) {
|
|
660
|
-
case !1:
|
|
661
|
-
return Y;
|
|
662
|
-
case "gzip":
|
|
663
|
-
return He;
|
|
664
|
-
case "deflate":
|
|
665
|
-
return Ge;
|
|
666
|
-
default:
|
|
667
|
-
throw E("InternalError", `unsupported compression kind: ${String(n)}`);
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
function Xe(n) {
|
|
671
|
-
switch (n) {
|
|
672
|
-
case Y:
|
|
673
|
-
return !1;
|
|
674
|
-
case He:
|
|
675
|
-
return "gzip";
|
|
676
|
-
case Ge:
|
|
677
|
-
return "deflate";
|
|
678
|
-
default:
|
|
679
|
-
throw E("CorruptionError", `invalid compression tag: ${n}`);
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
function ue() {
|
|
683
|
-
return E(
|
|
684
|
-
"UnsupportedPlatformError",
|
|
685
|
-
"CompressionStream and DecompressionStream are required for moyodb compression"
|
|
686
|
-
);
|
|
687
|
-
}
|
|
688
|
-
function Je() {
|
|
689
|
-
if (typeof CompressionStream != "function" || typeof DecompressionStream != "function")
|
|
690
|
-
throw ue();
|
|
691
|
-
}
|
|
692
|
-
function de(n) {
|
|
693
|
-
let e = 4294967295;
|
|
694
|
-
for (const t of n)
|
|
695
|
-
e = e >>> 8 ^ jt[(e ^ t) & 255];
|
|
696
|
-
return (e ^ 4294967295) >>> 0;
|
|
697
|
-
}
|
|
698
|
-
function H(n, e, t, r) {
|
|
699
|
-
const o = new Uint8Array(A + r.byteLength);
|
|
700
|
-
o.set(n, 0), o[8] = he, o[9] = e;
|
|
701
|
-
const i = new DataView(o.buffer, o.byteOffset, A);
|
|
702
|
-
return i.setUint32(10, t, !0), i.setUint32(14, de(r), !0), o.set(r, A), o;
|
|
703
|
-
}
|
|
704
|
-
function Ze(n) {
|
|
705
|
-
const e = new Uint8Array(n.byteLength);
|
|
706
|
-
return e.set(n), e.buffer;
|
|
707
|
-
}
|
|
708
|
-
function Qe(n, e) {
|
|
709
|
-
if (e.byteLength < A)
|
|
710
|
-
return null;
|
|
711
|
-
for (let r = 0; r < n.byteLength; r += 1)
|
|
712
|
-
if (e[r] !== n[r])
|
|
713
|
-
return null;
|
|
714
|
-
const t = new DataView(e.buffer, e.byteOffset, A);
|
|
715
|
-
return {
|
|
716
|
-
version: e[8],
|
|
717
|
-
kindTag: e[9],
|
|
718
|
-
rawLength: t.getUint32(10, !0),
|
|
719
|
-
payloadChecksum: t.getUint32(14, !0)
|
|
720
|
-
};
|
|
721
|
-
}
|
|
722
|
-
async function et(n, e) {
|
|
723
|
-
Je();
|
|
724
|
-
let t;
|
|
725
|
-
try {
|
|
726
|
-
t = new Blob([Ze(n)]).stream().pipeThrough(new CompressionStream(e));
|
|
727
|
-
} catch {
|
|
728
|
-
throw ue();
|
|
729
|
-
}
|
|
730
|
-
try {
|
|
731
|
-
return new Uint8Array(await new Response(t).arrayBuffer());
|
|
732
|
-
} catch (r) {
|
|
733
|
-
throw E("InternalError", `failed to compress bytes with ${e}: ${String(r)}`);
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
async function tt(n, e, t, r) {
|
|
737
|
-
Je();
|
|
738
|
-
let o;
|
|
739
|
-
try {
|
|
740
|
-
o = new Blob([Ze(n)]).stream().pipeThrough(new DecompressionStream(e));
|
|
741
|
-
} catch {
|
|
742
|
-
throw ue();
|
|
743
|
-
}
|
|
744
|
-
try {
|
|
745
|
-
return await Xt(o, t, r);
|
|
746
|
-
} catch (i) {
|
|
747
|
-
throw E("CorruptionError", `failed to decompress ${t}: ${String(i)}`);
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
|
-
async function Xt(n, e, t) {
|
|
751
|
-
const r = n.getReader(), o = [];
|
|
752
|
-
let i = 0;
|
|
753
|
-
try {
|
|
754
|
-
for (; ; ) {
|
|
755
|
-
const { value: c, done: l } = await r.read();
|
|
756
|
-
if (l)
|
|
757
|
-
break;
|
|
758
|
-
if (c !== void 0) {
|
|
759
|
-
if (i += c.byteLength, t !== void 0 && i > t)
|
|
760
|
-
throw await r.cancel(`decompressed ${e} exceeded ${t} bytes`), E("CorruptionError", `decompressed ${e} exceeds ${t} byte limit`);
|
|
761
|
-
o.push(c);
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
|
-
} finally {
|
|
765
|
-
r.releaseLock();
|
|
766
|
-
}
|
|
767
|
-
const s = new Uint8Array(i);
|
|
768
|
-
let a = 0;
|
|
769
|
-
for (const c of o)
|
|
770
|
-
s.set(c, a), a += c.byteLength;
|
|
771
|
-
return s;
|
|
772
|
-
}
|
|
773
|
-
function Jt(n) {
|
|
774
|
-
const e = (n & Gt) >>> Ye;
|
|
775
|
-
switch (e) {
|
|
776
|
-
case 0:
|
|
777
|
-
return !1;
|
|
778
|
-
case 1:
|
|
779
|
-
return "gzip";
|
|
780
|
-
case 2:
|
|
781
|
-
return "deflate";
|
|
782
|
-
default:
|
|
783
|
-
throw E("CorruptionError", `invalid store compression flags: ${e}`);
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
async function ee(n, e) {
|
|
787
|
-
if (e === !1)
|
|
788
|
-
return n;
|
|
789
|
-
if (n.byteLength < Yt)
|
|
790
|
-
return H(z, Y, n.byteLength, n);
|
|
791
|
-
const t = await et(n, e);
|
|
792
|
-
return t.byteLength >= n.byteLength ? H(z, Y, n.byteLength, n) : H(z, je(e), n.byteLength, t);
|
|
793
|
-
}
|
|
794
|
-
async function Ee(n, e) {
|
|
795
|
-
const t = Qe(z, n);
|
|
796
|
-
if (!t)
|
|
797
|
-
return n;
|
|
798
|
-
if (t.version !== he) {
|
|
799
|
-
if (e.strict)
|
|
800
|
-
throw E("CorruptionError", `unsupported value record version: ${t.version}`);
|
|
801
|
-
return n;
|
|
802
|
-
}
|
|
803
|
-
let r;
|
|
804
|
-
try {
|
|
805
|
-
r = Xe(t.kindTag);
|
|
806
|
-
} catch (s) {
|
|
807
|
-
if (e.strict)
|
|
808
|
-
throw s;
|
|
809
|
-
return n;
|
|
810
|
-
}
|
|
811
|
-
const o = n.subarray(A);
|
|
812
|
-
if (de(o) !== t.payloadChecksum) {
|
|
813
|
-
if (e.strict)
|
|
814
|
-
throw E("CorruptionError", "value record checksum mismatch");
|
|
815
|
-
return n;
|
|
816
|
-
}
|
|
817
|
-
if (r === !1) {
|
|
818
|
-
if (o.byteLength !== t.rawLength) {
|
|
819
|
-
if (e.strict)
|
|
820
|
-
throw E(
|
|
821
|
-
"CorruptionError",
|
|
822
|
-
`value record length mismatch: expected ${t.rawLength} bytes, got ${o.byteLength}`
|
|
823
|
-
);
|
|
824
|
-
return n;
|
|
825
|
-
}
|
|
826
|
-
return o.slice();
|
|
827
|
-
}
|
|
828
|
-
if (t.rawLength > we)
|
|
829
|
-
throw E(
|
|
830
|
-
"CorruptionError",
|
|
831
|
-
`compressed value advertises ${t.rawLength} decompressed bytes, exceeding the ${we} byte limit`
|
|
832
|
-
);
|
|
833
|
-
const i = await tt(o, r, `value record (${r})`, t.rawLength);
|
|
834
|
-
if (i.byteLength !== t.rawLength)
|
|
835
|
-
throw E(
|
|
836
|
-
"CorruptionError",
|
|
837
|
-
`decompressed value length mismatch: expected ${t.rawLength} bytes, got ${i.byteLength}`
|
|
838
|
-
);
|
|
839
|
-
return i;
|
|
840
|
-
}
|
|
841
|
-
async function Zt(n, e) {
|
|
842
|
-
if (e === !1)
|
|
843
|
-
return n;
|
|
844
|
-
const t = await et(n, e);
|
|
845
|
-
return H(ze, je(e), n.byteLength, t);
|
|
846
|
-
}
|
|
847
|
-
async function Qt(n) {
|
|
848
|
-
const e = Qe(ze, n);
|
|
849
|
-
if (!e)
|
|
850
|
-
return n;
|
|
851
|
-
if (e.version !== he)
|
|
852
|
-
throw E("CorruptionError", `unsupported snapshot compression version: ${e.version}`);
|
|
853
|
-
const t = Xe(e.kindTag);
|
|
854
|
-
if (t === !1)
|
|
855
|
-
throw E("CorruptionError", 'snapshot compression envelope cannot store kind "none"');
|
|
856
|
-
const r = n.subarray(A);
|
|
857
|
-
if (de(r) !== e.payloadChecksum)
|
|
858
|
-
throw E("CorruptionError", "snapshot compression checksum mismatch");
|
|
859
|
-
if (e.rawLength > be)
|
|
860
|
-
throw E(
|
|
861
|
-
"CorruptionError",
|
|
862
|
-
`compressed snapshot advertises ${e.rawLength} decompressed bytes, exceeding the ${be} byte limit`
|
|
863
|
-
);
|
|
864
|
-
const o = await tt(
|
|
865
|
-
r,
|
|
866
|
-
t,
|
|
867
|
-
`snapshot export (${t})`,
|
|
868
|
-
e.rawLength
|
|
869
|
-
);
|
|
870
|
-
if (o.byteLength !== e.rawLength)
|
|
871
|
-
throw E(
|
|
872
|
-
"CorruptionError",
|
|
873
|
-
`decompressed snapshot length mismatch: expected ${e.rawLength} bytes, got ${o.byteLength}`
|
|
874
|
-
);
|
|
875
|
-
return o;
|
|
876
|
-
}
|
|
877
|
-
class fe extends Error {
|
|
878
|
-
constructor(e, t) {
|
|
879
|
-
super(t), this.name = e, Object.setPrototypeOf(this, new.target.prototype);
|
|
880
|
-
}
|
|
881
|
-
}
|
|
882
|
-
class en extends fe {
|
|
883
|
-
constructor(e) {
|
|
884
|
-
super("ReservedStoreNameError", e);
|
|
885
|
-
}
|
|
886
|
-
}
|
|
887
|
-
class Z extends fe {
|
|
888
|
-
constructor(e) {
|
|
889
|
-
super("SerializationError", e);
|
|
890
|
-
}
|
|
891
|
-
}
|
|
892
|
-
class P extends fe {
|
|
893
|
-
constructor(e) {
|
|
894
|
-
super("InvalidOpenOptionsError", e);
|
|
895
|
-
}
|
|
896
|
-
}
|
|
897
|
-
const tn = new TextEncoder(), nt = new TextDecoder("utf-8", { fatal: !0 }), j = /* @__PURE__ */ Symbol("moyodb.indexing.missing"), nn = 0xcbf29ce484222325n, rn = 0x100000001b3n, on = 0xffffffffffffffffn, sn = "__browserdb:", Se = "__browserdb:index:", N = "__browserdb:indexes";
|
|
898
|
-
function w(n) {
|
|
899
|
-
return n.startsWith(sn);
|
|
900
|
-
}
|
|
901
|
-
function an(n) {
|
|
902
|
-
if (w(n))
|
|
903
|
-
throw new en(`reserved store name: ${n}`);
|
|
904
|
-
}
|
|
905
|
-
function ye(n) {
|
|
906
|
-
if (n === void 0)
|
|
907
|
-
return [];
|
|
908
|
-
if (!Array.isArray(n))
|
|
909
|
-
throw new P("indexes must be an array");
|
|
910
|
-
const e = [], t = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Map();
|
|
911
|
-
for (const o of n) {
|
|
912
|
-
if (o === null || typeof o != "object" || Array.isArray(o))
|
|
913
|
-
throw new P("each index definition must be an object");
|
|
914
|
-
const { store: i, name: s, keyPath: a, unique: c } = o;
|
|
915
|
-
if (typeof i != "string" || i.length === 0)
|
|
916
|
-
throw new P("index store must be a non-empty string");
|
|
917
|
-
if (typeof s != "string" || s.length === 0)
|
|
918
|
-
throw new P("index name must be a non-empty string");
|
|
919
|
-
an(i);
|
|
920
|
-
const l = gn(a);
|
|
921
|
-
if (c !== void 0 && typeof c != "boolean")
|
|
922
|
-
throw new P("index unique must be a boolean");
|
|
923
|
-
const h = {
|
|
924
|
-
store: i,
|
|
925
|
-
name: s,
|
|
926
|
-
keyPath: l.paths,
|
|
927
|
-
compound: l.compound,
|
|
928
|
-
unique: c ?? !1,
|
|
929
|
-
internalStore: mn(i, s)
|
|
930
|
-
}, d = ot(h.store, h.name);
|
|
931
|
-
if (t.has(d))
|
|
932
|
-
throw new P(`duplicate index definition ${h.store}.${h.name}`);
|
|
933
|
-
t.add(d);
|
|
934
|
-
const g = r.get(h.internalStore);
|
|
935
|
-
if (g && g !== d)
|
|
936
|
-
throw new P(
|
|
937
|
-
`index definitions ${g} and ${d} map to the same internal store ${h.internalStore}`
|
|
938
|
-
);
|
|
939
|
-
r.set(h.internalStore, d), e.push(h);
|
|
940
|
-
}
|
|
941
|
-
return e.sort(rt), e;
|
|
942
|
-
}
|
|
943
|
-
function rt(n, e) {
|
|
944
|
-
const t = Q(n.store, e.store);
|
|
945
|
-
if (t !== 0)
|
|
946
|
-
return t;
|
|
947
|
-
const r = Q(n.name, e.name);
|
|
948
|
-
if (r !== 0)
|
|
949
|
-
return r;
|
|
950
|
-
const o = Number(n.unique) - Number(e.unique);
|
|
951
|
-
return o !== 0 ? o : Q(_e(n), _e(e));
|
|
952
|
-
}
|
|
953
|
-
function ot(n, e) {
|
|
954
|
-
return `${n}\0${e}`;
|
|
955
|
-
}
|
|
956
|
-
function cn(n, e, t) {
|
|
957
|
-
return n.find((r) => r.store === e && r.name === t) ?? null;
|
|
958
|
-
}
|
|
959
|
-
function ln(n, e) {
|
|
960
|
-
return n.filter((t) => t.store === e);
|
|
961
|
-
}
|
|
962
|
-
function hn(n, e) {
|
|
963
|
-
return L([C(n), C(e)]);
|
|
964
|
-
}
|
|
965
|
-
function un(n) {
|
|
966
|
-
return C(JSON.stringify(it(n)));
|
|
967
|
-
}
|
|
968
|
-
function dn(n) {
|
|
969
|
-
let e;
|
|
970
|
-
try {
|
|
971
|
-
e = JSON.parse(nt.decode(n));
|
|
972
|
-
} catch (r) {
|
|
973
|
-
throw new Z(`invalid persisted index metadata: ${String(r)}`);
|
|
974
|
-
}
|
|
975
|
-
const [t] = ye([e]);
|
|
976
|
-
return t;
|
|
977
|
-
}
|
|
978
|
-
function fn(n, e) {
|
|
979
|
-
return L([n, e]);
|
|
980
|
-
}
|
|
981
|
-
function ke(n) {
|
|
982
|
-
const e = Vt(n);
|
|
983
|
-
if (e.length !== 2)
|
|
984
|
-
throw new Z("invalid index entry key encoding");
|
|
985
|
-
return {
|
|
986
|
-
logicalKey: e[0],
|
|
987
|
-
primaryKey: e[1]
|
|
988
|
-
};
|
|
989
|
-
}
|
|
990
|
-
function yn(n) {
|
|
991
|
-
return qe(L([n]));
|
|
992
|
-
}
|
|
993
|
-
function pn(n = {}) {
|
|
994
|
-
Sn(n);
|
|
995
|
-
const e = {};
|
|
996
|
-
if (n.reverse !== void 0 && (e.reverse = n.reverse), n.limit !== void 0 && (e.limit = n.limit), n.gte && (e.gte = L([n.gte])), n.gt) {
|
|
997
|
-
const t = ae(L([n.gt]));
|
|
998
|
-
t && (e.gte = t);
|
|
999
|
-
}
|
|
1000
|
-
if (n.lt && (e.lt = L([n.lt])), n.lte) {
|
|
1001
|
-
const t = ae(L([n.lte]));
|
|
1002
|
-
t && (e.lt = t);
|
|
1003
|
-
}
|
|
1004
|
-
return e;
|
|
1005
|
-
}
|
|
1006
|
-
function I(n, e) {
|
|
1007
|
-
const t = bn(e);
|
|
1008
|
-
if (n.compound) {
|
|
1009
|
-
const o = [];
|
|
1010
|
-
for (const i of n.keyPath) {
|
|
1011
|
-
const s = Pe(t, i);
|
|
1012
|
-
if (s === j)
|
|
1013
|
-
return null;
|
|
1014
|
-
o.push(Oe(s));
|
|
1015
|
-
}
|
|
1016
|
-
return L(o);
|
|
1017
|
-
}
|
|
1018
|
-
const r = Pe(t, n.keyPath[0]);
|
|
1019
|
-
return r === j ? null : Oe(r);
|
|
1020
|
-
}
|
|
1021
|
-
function O(n) {
|
|
1022
|
-
return n.map((e) => ({
|
|
1023
|
-
store: e.store,
|
|
1024
|
-
name: e.name,
|
|
1025
|
-
keyPath: [...e.keyPath],
|
|
1026
|
-
compound: e.compound,
|
|
1027
|
-
unique: e.unique,
|
|
1028
|
-
internalStore: e.internalStore
|
|
1029
|
-
}));
|
|
1030
|
-
}
|
|
1031
|
-
function ce(n) {
|
|
1032
|
-
return n.map(it);
|
|
1033
|
-
}
|
|
1034
|
-
function it(n) {
|
|
1035
|
-
return {
|
|
1036
|
-
store: n.store,
|
|
1037
|
-
name: n.name,
|
|
1038
|
-
keyPath: n.compound ? [...n.keyPath] : n.keyPath[0],
|
|
1039
|
-
unique: n.unique
|
|
1040
|
-
};
|
|
1041
|
-
}
|
|
1042
|
-
function gn(n) {
|
|
1043
|
-
if (typeof n == "string")
|
|
1044
|
-
return xe(n), { paths: [n], compound: !1 };
|
|
1045
|
-
if (!Array.isArray(n) || n.length === 0 || n.some((e) => typeof e != "string"))
|
|
1046
|
-
throw new P("index keyPath must be a non-empty string or non-empty string[]");
|
|
1047
|
-
for (const e of n)
|
|
1048
|
-
xe(e);
|
|
1049
|
-
return { paths: [...n], compound: !0 };
|
|
1050
|
-
}
|
|
1051
|
-
function xe(n) {
|
|
1052
|
-
if (n.length === 0)
|
|
1053
|
-
throw new P("index keyPath strings must be non-empty");
|
|
1054
|
-
if (n.startsWith(".") || n.endsWith(".") || n.includes(".."))
|
|
1055
|
-
throw new P(`invalid index keyPath: ${n}`);
|
|
1056
|
-
}
|
|
1057
|
-
function _e(n) {
|
|
1058
|
-
return n.compound ? `[${n.keyPath.join(",")}]` : n.keyPath[0];
|
|
1059
|
-
}
|
|
1060
|
-
function mn(n, e) {
|
|
1061
|
-
const t = Ce(C(n)), r = Ce(C(e)), o = `${Se}${t}:${r}`;
|
|
1062
|
-
if (tn.encode(o).length <= 255)
|
|
1063
|
-
return o;
|
|
1064
|
-
const i = C(ot(n, e));
|
|
1065
|
-
return `${Se}h:${wn(i)}`;
|
|
1066
|
-
}
|
|
1067
|
-
function Ce(n) {
|
|
1068
|
-
let e = "";
|
|
1069
|
-
for (const t of n)
|
|
1070
|
-
e += String.fromCharCode(t);
|
|
1071
|
-
return btoa(e).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/u, "");
|
|
1072
|
-
}
|
|
1073
|
-
function wn(n) {
|
|
1074
|
-
let e = nn;
|
|
1075
|
-
for (const t of n)
|
|
1076
|
-
e ^= BigInt(t), e = e * rn & on;
|
|
1077
|
-
return e.toString(16).padStart(16, "0");
|
|
1078
|
-
}
|
|
1079
|
-
function bn(n) {
|
|
1080
|
-
try {
|
|
1081
|
-
return JSON.parse(nt.decode(n));
|
|
1082
|
-
} catch (e) {
|
|
1083
|
-
throw new Z(`indexed values must be valid UTF-8 JSON: ${String(e)}`);
|
|
1084
|
-
}
|
|
1085
|
-
}
|
|
1086
|
-
function Pe(n, e) {
|
|
1087
|
-
let t = n;
|
|
1088
|
-
for (const r of e.split(".")) {
|
|
1089
|
-
if (t == null)
|
|
1090
|
-
return j;
|
|
1091
|
-
const o = Object(t);
|
|
1092
|
-
if (!(r in o))
|
|
1093
|
-
return j;
|
|
1094
|
-
t = Reflect.get(o, r);
|
|
1095
|
-
}
|
|
1096
|
-
return t;
|
|
1097
|
-
}
|
|
1098
|
-
function Oe(n) {
|
|
1099
|
-
if (n instanceof Uint8Array || n === null || typeof n == "string" || typeof n == "number" || typeof n == "boolean")
|
|
1100
|
-
return me(n);
|
|
1101
|
-
throw new Z(
|
|
1102
|
-
`indexed keyPath values must resolve to string, number, boolean, null, or Uint8Array; got ${En(n)}`
|
|
1103
|
-
);
|
|
1104
|
-
}
|
|
1105
|
-
function En(n) {
|
|
1106
|
-
return Array.isArray(n) ? "array" : n === null ? "null" : typeof n;
|
|
1107
|
-
}
|
|
1108
|
-
function Sn(n) {
|
|
1109
|
-
if (n.gt !== void 0 && n.gte !== void 0)
|
|
1110
|
-
throw new TypeError("index range cannot specify both gt and gte");
|
|
1111
|
-
if (n.lt !== void 0 && n.lte !== void 0)
|
|
1112
|
-
throw new TypeError("index range cannot specify both lt and lte");
|
|
1113
|
-
}
|
|
1114
|
-
const kn = Function("moduleUrl", "return import(moduleUrl)");
|
|
1115
|
-
function p(n) {
|
|
1116
|
-
return BigInt(n);
|
|
1117
|
-
}
|
|
1118
|
-
function b(n) {
|
|
1119
|
-
return typeof n == "bigint" ? Number(n) : n;
|
|
1120
|
-
}
|
|
1121
|
-
function te(n) {
|
|
1122
|
-
return n.ttl === void 0 ? n : { ...n, ttl: p(n.ttl) };
|
|
1123
|
-
}
|
|
1124
|
-
function xn(n) {
|
|
1125
|
-
const e = {};
|
|
1126
|
-
return n.gt !== void 0 && (e.gt = n.gt), n.gte !== void 0 && (e.gte = n.gte), n.lt !== void 0 && (e.lt = n.lt), n.lte !== void 0 && (e.lte = n.lte), n.reverse !== void 0 && (e.reverse = n.reverse), n.limit !== void 0 && (e.limit = n.limit), e;
|
|
1127
|
-
}
|
|
1128
|
-
const V = {}, Te = new Uint8Array(0), _n = 1024, Cn = Array.from({ length: 256 }, (n, e) => e.toString(16).padStart(2, "0"));
|
|
1129
|
-
function ne(n, e, t) {
|
|
1130
|
-
const r = n.get(e);
|
|
1131
|
-
if (r !== void 0)
|
|
1132
|
-
return r;
|
|
1133
|
-
const o = t();
|
|
1134
|
-
return n.set(e, o), o;
|
|
1135
|
-
}
|
|
1136
|
-
const Pn = "moyodb:persistence-bridge:init", On = "moyodb:persistence-bridge:request", Tn = "moyodb:persistence-bridge:response";
|
|
1137
|
-
class Ln {
|
|
1138
|
-
port = null;
|
|
1139
|
-
nextRequestId = 1;
|
|
1140
|
-
pending = /* @__PURE__ */ new Map();
|
|
1141
|
-
readyResolve = null;
|
|
1142
|
-
ready;
|
|
1143
|
-
constructor() {
|
|
1144
|
-
this.ready = new Promise((e) => {
|
|
1145
|
-
this.readyResolve = e;
|
|
1146
|
-
}), self.addEventListener("message", this.handleInitMessage);
|
|
1147
|
-
}
|
|
1148
|
-
async persisted() {
|
|
1149
|
-
return this.request("persisted");
|
|
1150
|
-
}
|
|
1151
|
-
async persist() {
|
|
1152
|
-
return this.request("persist");
|
|
1153
|
-
}
|
|
1154
|
-
close() {
|
|
1155
|
-
self.removeEventListener("message", this.handleInitMessage), this.readyResolve && (this.readyResolve(null), this.readyResolve = null), this.port && (this.port.onmessage = null, this.port.close(), this.port = null);
|
|
1156
|
-
for (const e of this.pending.values())
|
|
1157
|
-
e.resolve(!1);
|
|
1158
|
-
this.pending.clear();
|
|
1159
|
-
}
|
|
1160
|
-
async request(e) {
|
|
1161
|
-
const t = await this.ready;
|
|
1162
|
-
return !t || this.port !== t ? !1 : new Promise((r) => {
|
|
1163
|
-
const o = this.nextRequestId;
|
|
1164
|
-
this.nextRequestId += 1, this.pending.set(o, { resolve: r });
|
|
1165
|
-
const i = {
|
|
1166
|
-
type: On,
|
|
1167
|
-
id: o,
|
|
1168
|
-
op: e
|
|
1169
|
-
};
|
|
1170
|
-
try {
|
|
1171
|
-
t.postMessage(i);
|
|
1172
|
-
} catch {
|
|
1173
|
-
this.pending.delete(o), r(!1);
|
|
1174
|
-
}
|
|
1175
|
-
});
|
|
1176
|
-
}
|
|
1177
|
-
handleInitMessage = (e) => {
|
|
1178
|
-
if (e.data?.type !== Pn)
|
|
1179
|
-
return;
|
|
1180
|
-
const r = e.ports[0];
|
|
1181
|
-
if (r) {
|
|
1182
|
-
if (this.port) {
|
|
1183
|
-
r.close();
|
|
1184
|
-
return;
|
|
1185
|
-
}
|
|
1186
|
-
this.port = r, this.port.onmessage = this.handleResponse, this.port.start(), self.removeEventListener("message", this.handleInitMessage), this.readyResolve?.(r), this.readyResolve = null;
|
|
1187
|
-
}
|
|
1188
|
-
};
|
|
1189
|
-
handleResponse = (e) => {
|
|
1190
|
-
const t = e.data;
|
|
1191
|
-
if (!t || t.type !== Tn)
|
|
1192
|
-
return;
|
|
1193
|
-
const r = this.pending.get(t.id);
|
|
1194
|
-
r && (this.pending.delete(t.id), r.resolve(!!t.granted));
|
|
1195
|
-
};
|
|
1196
|
-
}
|
|
1197
|
-
async function Rn() {
|
|
1198
|
-
const n = navigator.storage;
|
|
1199
|
-
if (typeof n?.estimate != "function")
|
|
1200
|
-
return { usage: 0, quota: 0 };
|
|
1201
|
-
const e = await n.estimate();
|
|
1202
|
-
return {
|
|
1203
|
-
usage: e.usage ?? 0,
|
|
1204
|
-
quota: e.quota ?? 0
|
|
1205
|
-
};
|
|
1206
|
-
}
|
|
1207
|
-
function Le(n) {
|
|
1208
|
-
let e = "";
|
|
1209
|
-
for (let t = 0; t < n.byteLength; t += 1)
|
|
1210
|
-
e += Cn[n[t]];
|
|
1211
|
-
return e;
|
|
1212
|
-
}
|
|
1213
|
-
function R(n, e) {
|
|
1214
|
-
if (n.byteLength !== e.byteLength)
|
|
1215
|
-
return !1;
|
|
1216
|
-
for (let t = 0; t < n.byteLength; t += 1)
|
|
1217
|
-
if (n[t] !== e[t])
|
|
1218
|
-
return !1;
|
|
1219
|
-
return !0;
|
|
1220
|
-
}
|
|
1221
|
-
function _(n) {
|
|
1222
|
-
return n instanceof Uint8Array ? n.byteOffset === 0 && n.byteLength === n.buffer.byteLength ? n : n.slice() : new Uint8Array(n);
|
|
1223
|
-
}
|
|
1224
|
-
function An(n) {
|
|
1225
|
-
if (!k(n))
|
|
1226
|
-
return null;
|
|
1227
|
-
const e = n.name ?? n.code;
|
|
1228
|
-
return typeof e == "string" ? e : null;
|
|
1229
|
-
}
|
|
1230
|
-
function re(n, e) {
|
|
1231
|
-
return An(n) === e;
|
|
1232
|
-
}
|
|
1233
|
-
const X = C("BDBIDX1"), Mn = new TextDecoder("utf-8", { fatal: !0 });
|
|
1234
|
-
function In(n, e) {
|
|
1235
|
-
if (e.length === 0)
|
|
1236
|
-
return n;
|
|
1237
|
-
const t = C(JSON.stringify(ce(e))), r = new ArrayBuffer(4);
|
|
1238
|
-
new DataView(r).setUint32(0, t.byteLength, !0);
|
|
1239
|
-
const o = new Uint8Array(n.byteLength + t.byteLength + 4 + X.byteLength);
|
|
1240
|
-
let i = 0;
|
|
1241
|
-
return o.set(n, i), i += n.byteLength, o.set(t, i), i += t.byteLength, o.set(new Uint8Array(r), i), i += 4, o.set(X, i), o;
|
|
1242
|
-
}
|
|
1243
|
-
function vn(n) {
|
|
1244
|
-
const e = X.byteLength;
|
|
1245
|
-
if (n.byteLength < e + 4)
|
|
1246
|
-
return { snapshot: n, indexes: [] };
|
|
1247
|
-
const t = n.byteLength - e;
|
|
1248
|
-
if (!R(n.subarray(t), X))
|
|
1249
|
-
return { snapshot: n, indexes: [] };
|
|
1250
|
-
const r = t - 4;
|
|
1251
|
-
if (r < 0)
|
|
1252
|
-
return { snapshot: n, indexes: [] };
|
|
1253
|
-
const o = new DataView(n.buffer, n.byteOffset + r, 4).getUint32(0, !0), i = r - o;
|
|
1254
|
-
if (i < 0)
|
|
1255
|
-
throw m("CorruptionError", "invalid snapshot index trailer length");
|
|
1256
|
-
let s;
|
|
1257
|
-
try {
|
|
1258
|
-
s = JSON.parse(Mn.decode(n.subarray(i, r)));
|
|
1259
|
-
} catch (a) {
|
|
1260
|
-
throw m("CorruptionError", `invalid snapshot index trailer: ${String(a)}`);
|
|
1261
|
-
}
|
|
1262
|
-
return {
|
|
1263
|
-
snapshot: n.subarray(0, i),
|
|
1264
|
-
indexes: ye(s)
|
|
1265
|
-
};
|
|
1266
|
-
}
|
|
1267
|
-
function Bn(n) {
|
|
1268
|
-
return new BroadcastChannel(`db:${n}:events`);
|
|
1269
|
-
}
|
|
1270
|
-
function D(n, e) {
|
|
1271
|
-
const t = Bn(n);
|
|
1272
|
-
try {
|
|
1273
|
-
t.postMessage({ type: e, dbName: n, txid: null });
|
|
1274
|
-
} finally {
|
|
1275
|
-
t.close();
|
|
1276
|
-
}
|
|
1277
|
-
}
|
|
1278
|
-
class Re {
|
|
1279
|
-
releaseResolver = null;
|
|
1280
|
-
requestPromise = null;
|
|
1281
|
-
held = !1;
|
|
1282
|
-
async acquire(e, t) {
|
|
1283
|
-
if (!navigator.locks)
|
|
1284
|
-
throw m("UnsupportedPlatformError", "navigator.locks is unavailable");
|
|
1285
|
-
const r = new Promise((l) => {
|
|
1286
|
-
this.releaseResolver = l;
|
|
1287
|
-
}), o = `db:${e}:owner`;
|
|
1288
|
-
if (t === 0) {
|
|
1289
|
-
let l = null;
|
|
1290
|
-
const h = new Promise((y) => {
|
|
1291
|
-
l = y;
|
|
1292
|
-
}), d = navigator.locks.request(
|
|
1293
|
-
o,
|
|
1294
|
-
{ mode: "exclusive", ifAvailable: !0 },
|
|
1295
|
-
async (y) => y ? (this.held = !0, l?.(!0), await r, !0) : (l?.(!1), !1)
|
|
1296
|
-
);
|
|
1297
|
-
if (!await h)
|
|
1298
|
-
throw m("DatabaseBusyError", `database ${e} is already owned by another tab`);
|
|
1299
|
-
this.requestPromise = d;
|
|
1300
|
-
return;
|
|
1301
|
-
}
|
|
1302
|
-
const i = new AbortController(), s = setTimeout(() => i.abort(), t);
|
|
1303
|
-
let a = null;
|
|
1304
|
-
const c = new Promise((l) => {
|
|
1305
|
-
a = l;
|
|
1306
|
-
});
|
|
1307
|
-
this.requestPromise = navigator.locks.request(
|
|
1308
|
-
o,
|
|
1309
|
-
{ mode: "exclusive", signal: i.signal },
|
|
1310
|
-
async (l) => (clearTimeout(s), this.held = !0, a?.(), await r, !0)
|
|
1311
|
-
);
|
|
1312
|
-
try {
|
|
1313
|
-
await Promise.race([c, this.requestPromise]);
|
|
1314
|
-
} catch {
|
|
1315
|
-
if (!this.held)
|
|
1316
|
-
throw m("DatabaseBusyError", `timed out waiting for ownership of ${e}`);
|
|
1317
|
-
}
|
|
1318
|
-
}
|
|
1319
|
-
async release() {
|
|
1320
|
-
if (this.held) {
|
|
1321
|
-
this.releaseResolver?.();
|
|
1322
|
-
try {
|
|
1323
|
-
await this.requestPromise;
|
|
1324
|
-
} finally {
|
|
1325
|
-
this.releaseResolver = null, this.requestPromise = null, this.held = !1;
|
|
1326
|
-
}
|
|
1327
|
-
}
|
|
1328
|
-
}
|
|
1329
|
-
}
|
|
1330
|
-
class Nn {
|
|
1331
|
-
dbName = null;
|
|
1332
|
-
engine = null;
|
|
1333
|
-
events = null;
|
|
1334
|
-
lease = null;
|
|
1335
|
-
wasmReady = null;
|
|
1336
|
-
persistenceBridge = new Ln();
|
|
1337
|
-
txChanges = /* @__PURE__ */ new Map();
|
|
1338
|
-
txModes = /* @__PURE__ */ new Map();
|
|
1339
|
-
txIndexSchemas = /* @__PURE__ */ new Map();
|
|
1340
|
-
txStoreCompression = /* @__PURE__ */ new Map();
|
|
1341
|
-
committedIndexes = null;
|
|
1342
|
-
committedStoreCompression = null;
|
|
1343
|
-
openOptions = null;
|
|
1344
|
-
maintenanceOperation = null;
|
|
1345
|
-
async open(e) {
|
|
1346
|
-
if (this.engine)
|
|
1347
|
-
return;
|
|
1348
|
-
Me(), await Ae(), this.dbName = e.dbName, this.events = new BroadcastChannel(`db:${e.dbName}:events`), this.lease = new Re();
|
|
1349
|
-
let t = null;
|
|
1350
|
-
try {
|
|
1351
|
-
await this.lease.acquire(e.dbName, e.options.ownerWaitMs);
|
|
1352
|
-
const r = await this.loadWasm();
|
|
1353
|
-
t = new r.WasmEngine(), await t.open(e.dbName, {
|
|
1354
|
-
create_if_missing: e.options.createIfMissing,
|
|
1355
|
-
cache_pages: e.options.cachePages
|
|
1356
|
-
}), e.options.debugFailpoint && t.set_failpoint(e.options.debugFailpoint), this.engine = t, this.openOptions = e.options, this.clearRuntimeCaches(), this.maintenanceOperation = null, this.events.postMessage({ type: "owner_changed", dbName: e.dbName, txid: null });
|
|
1357
|
-
} catch (r) {
|
|
1358
|
-
throw await this.cleanupAfterFailedOpen(t), v(r);
|
|
1359
|
-
}
|
|
1360
|
-
}
|
|
1361
|
-
async close() {
|
|
1362
|
-
this.ensureNotInMaintenance();
|
|
1363
|
-
const e = this.dbName;
|
|
1364
|
-
this.clearRuntimeCaches();
|
|
1365
|
-
try {
|
|
1366
|
-
this.engine?.close();
|
|
1367
|
-
} catch (t) {
|
|
1368
|
-
throw v(t);
|
|
1369
|
-
} finally {
|
|
1370
|
-
this.engine = null, e && this.events && this.events.postMessage({ type: "db_closed", dbName: e, txid: null }), this.events?.close(), this.events = null, e && this.lease && await this.lease.release(), e && D(e, "owner_changed"), this.disposePersistenceBridge(), this.lease = null, this.dbName = null, this.openOptions = null, this.maintenanceOperation = null;
|
|
1371
|
-
}
|
|
1372
|
-
}
|
|
1373
|
-
async destroy() {
|
|
1374
|
-
this.ensureNotInMaintenance();
|
|
1375
|
-
const e = this.dbName;
|
|
1376
|
-
if (!e)
|
|
1377
|
-
return;
|
|
1378
|
-
let t = null;
|
|
1379
|
-
try {
|
|
1380
|
-
this.rollbackAllTransactions(), this.engine?.close(), await (await this.loadWasm()).deleteDB(e), D(e, "db_deleted");
|
|
1381
|
-
} catch (r) {
|
|
1382
|
-
t = r;
|
|
1383
|
-
} finally {
|
|
1384
|
-
if (this.engine = null, this.clearRuntimeCaches(), this.events?.close(), this.events = null, this.lease)
|
|
1385
|
-
try {
|
|
1386
|
-
await this.lease.release();
|
|
1387
|
-
} catch (r) {
|
|
1388
|
-
t === null && (t = r);
|
|
1389
|
-
}
|
|
1390
|
-
D(e, "owner_changed"), this.disposePersistenceBridge(), this.lease = null, this.dbName = null, this.openOptions = null, this.maintenanceOperation = null;
|
|
1391
|
-
}
|
|
1392
|
-
if (t !== null)
|
|
1393
|
-
throw v(t);
|
|
1394
|
-
}
|
|
1395
|
-
async deleteDB(e) {
|
|
1396
|
-
Me(), await Ae();
|
|
1397
|
-
const t = new Re();
|
|
1398
|
-
let r = null, o = !1;
|
|
1399
|
-
try {
|
|
1400
|
-
await t.acquire(e, 0), o = !0, await (await this.loadWasm()).deleteDB(e), D(e, "db_deleted");
|
|
1401
|
-
} catch (i) {
|
|
1402
|
-
r = i;
|
|
1403
|
-
} finally {
|
|
1404
|
-
try {
|
|
1405
|
-
await t.release();
|
|
1406
|
-
} catch (i) {
|
|
1407
|
-
r === null && (r = i);
|
|
1408
|
-
}
|
|
1409
|
-
o && D(e, "owner_changed");
|
|
1410
|
-
}
|
|
1411
|
-
if (r !== null)
|
|
1412
|
-
throw v(r);
|
|
1413
|
-
}
|
|
1414
|
-
async begin(e) {
|
|
1415
|
-
const t = b(this.withEngine((r) => r.begin_tx(e)));
|
|
1416
|
-
return this.txChanges.set(t, /* @__PURE__ */ new Map()), this.txModes.set(t, e), this.txStoreCompression.set(t, new Map(this.loadCommittedStoreCompression())), t;
|
|
1417
|
-
}
|
|
1418
|
-
async commit(e) {
|
|
1419
|
-
const t = this.txChanges.get(e) ?? /* @__PURE__ */ new Map(), r = this.txIndexSchemas.get(e), o = this.txStoreCompression.get(e), i = this.txModes.get(e);
|
|
1420
|
-
let s;
|
|
1421
|
-
try {
|
|
1422
|
-
s = b(this.withEngine((a) => a.commit_tx(p(e))));
|
|
1423
|
-
} catch (a) {
|
|
1424
|
-
throw this.cleanupTxState(e), a;
|
|
1425
|
-
}
|
|
1426
|
-
if (r && (this.committedIndexes = O(r)), i === "readwrite" && o && (this.committedStoreCompression = new Map(o)), this.cleanupTxState(e), this.dbName && this.events) {
|
|
1427
|
-
const a = {
|
|
1428
|
-
type: "commit_applied",
|
|
1429
|
-
dbName: this.dbName,
|
|
1430
|
-
txid: s,
|
|
1431
|
-
stores: this.finalizeTrackedChanges(t)
|
|
1432
|
-
};
|
|
1433
|
-
this.events.postMessage(a);
|
|
1434
|
-
}
|
|
1435
|
-
return s;
|
|
1436
|
-
}
|
|
1437
|
-
async rollback(e) {
|
|
1438
|
-
try {
|
|
1439
|
-
this.withEngine((t) => t.rollback_tx(p(e)));
|
|
1440
|
-
} finally {
|
|
1441
|
-
this.cleanupTxState(e);
|
|
1442
|
-
}
|
|
1443
|
-
}
|
|
1444
|
-
async createStore(e, t, r = {}) {
|
|
1445
|
-
const o = r.compression ?? !1;
|
|
1446
|
-
this.withEngine((i) => i.create_store(p(e), t, { compression: o })), this.recordStoreCompressionCreate(e, t, o), this.ensureInternalStoresForStore(e, t), this.markStoreTouched(e, t);
|
|
1447
|
-
}
|
|
1448
|
-
async dropStore(e, t) {
|
|
1449
|
-
const r = this.loadStoreKeys(e, t), o = this.indexesForStoreInTx(e, t);
|
|
1450
|
-
this.withEngine((i) => i.drop_store(p(e), t)), this.recordStoreCompressionDrop(e, t);
|
|
1451
|
-
for (const i of o)
|
|
1452
|
-
this.dropRawStoreIfExists(e, i.internalStore);
|
|
1453
|
-
this.recordDeletedKeys(e, t, r);
|
|
1454
|
-
}
|
|
1455
|
-
async clearStore(e, t) {
|
|
1456
|
-
const r = this.loadStoreKeys(e, t), o = this.indexesForStoreInTx(e, t);
|
|
1457
|
-
this.withEngine((i) => i.clear_store(p(e), t));
|
|
1458
|
-
for (const i of o)
|
|
1459
|
-
this.clearRawStoreIfExists(e, i.internalStore);
|
|
1460
|
-
this.recordDeletedKeys(e, t, r);
|
|
1461
|
-
}
|
|
1462
|
-
async get(e, t, r) {
|
|
1463
|
-
return this.readStoreValue(e, t, r);
|
|
1464
|
-
}
|
|
1465
|
-
async getMany(e, t, r) {
|
|
1466
|
-
const o = this.withEngine((i) => i.get_many(p(e), t, r));
|
|
1467
|
-
return w(t) || this.storeCompressionForTx(e, t) === !1 ? o.map((i) => i === null ? null : _(i)) : Promise.all(
|
|
1468
|
-
o.map(
|
|
1469
|
-
(i) => i === null ? null : this.decodeStoreValue(e, t, _(i))
|
|
1470
|
-
)
|
|
1471
|
-
);
|
|
1472
|
-
}
|
|
1473
|
-
async getManyPacked(e, t, r) {
|
|
1474
|
-
const o = this.withEngine((i) => i.get_many_packed(p(e), t, r));
|
|
1475
|
-
return w(t) || this.storeCompressionForTx(e, t) === !1 ? o.map((i) => i === null ? null : _(i)) : Promise.all(
|
|
1476
|
-
o.map(
|
|
1477
|
-
(i) => i === null ? null : this.decodeStoreValue(e, t, _(i))
|
|
1478
|
-
)
|
|
1479
|
-
);
|
|
1480
|
-
}
|
|
1481
|
-
async has(e, t, r) {
|
|
1482
|
-
return this.withEngine((o) => o.has(p(e), t, r));
|
|
1483
|
-
}
|
|
1484
|
-
async put(e, t, r, o, i = {}) {
|
|
1485
|
-
const s = this.indexesForStoreInTx(e, t), a = this.getOrCreateTrackedStore(e, t), c = Le(r), l = a.keys.get(c), h = await this.readStoreValue(e, t, r), d = l?.baselineExists ?? h !== null, g = s.map((u) => {
|
|
1486
|
-
const S = h === null ? null : I(u, h), st = I(u, o);
|
|
1487
|
-
return {
|
|
1488
|
-
def: u,
|
|
1489
|
-
oldLogicalKey: S,
|
|
1490
|
-
newLogicalKey: st,
|
|
1491
|
-
newPhysicalKey: null
|
|
1492
|
-
};
|
|
1493
|
-
});
|
|
1494
|
-
this.ensureInternalStoresForDefinitions(e, s);
|
|
1495
|
-
for (const u of g)
|
|
1496
|
-
u.newLogicalKey !== null && (u.newPhysicalKey = this.buildIndexEntryKey(u.def, u.newLogicalKey, r), !(u.oldLogicalKey !== null && R(u.oldLogicalKey, u.newLogicalKey)) && await this.assertUniqueIndexAvailability(e, u.def, u.newLogicalKey, r));
|
|
1497
|
-
const y = await this.encodeStoreValueForWrite(e, t, o);
|
|
1498
|
-
this.withEngine((u) => u.put(p(e), t, r, y, te(i)));
|
|
1499
|
-
for (const u of g)
|
|
1500
|
-
u.oldLogicalKey !== null && (u.newLogicalKey !== null && R(u.oldLogicalKey, u.newLogicalKey) || this.deleteIndexEntryIfPresent(e, u.def, u.oldLogicalKey, r));
|
|
1501
|
-
for (const u of g)
|
|
1502
|
-
u.newLogicalKey !== null && (u.oldLogicalKey !== null && R(u.oldLogicalKey, u.newLogicalKey) || this.putIndexEntry(e, u.def, u.newPhysicalKey, Te));
|
|
1503
|
-
this.recordPutChange(e, t, r, d);
|
|
1504
|
-
}
|
|
1505
|
-
async putMany(e, t, r, o = {}) {
|
|
1506
|
-
if (r.length === 0)
|
|
1507
|
-
return;
|
|
1508
|
-
if (this.indexesForStoreInTx(e, t).length === 0) {
|
|
1509
|
-
const s = await this.encodeEntriesForWrite(e, t, r);
|
|
1510
|
-
let a;
|
|
1511
|
-
try {
|
|
1512
|
-
a = this.withEngine(
|
|
1513
|
-
(c) => c.put_many(p(e), t, s, te(o))
|
|
1514
|
-
);
|
|
1515
|
-
} catch (c) {
|
|
1516
|
-
const l = q(c);
|
|
1517
|
-
throw this.recordPutChanges(e, t, r, l), c;
|
|
1518
|
-
}
|
|
1519
|
-
if (a.length !== r.length)
|
|
1520
|
-
throw m(
|
|
1521
|
-
"InternalError",
|
|
1522
|
-
`put_many outcome count mismatch: ${a.length} != ${r.length}`
|
|
1523
|
-
);
|
|
1524
|
-
this.recordPutChanges(e, t, r, a);
|
|
1525
|
-
return;
|
|
1526
|
-
}
|
|
1527
|
-
for (const [s, a] of r)
|
|
1528
|
-
await this.put(e, t, s, a, o);
|
|
1529
|
-
}
|
|
1530
|
-
async putManyPacked(e, t, r, o = {}) {
|
|
1531
|
-
if (this.indexesForStoreInTx(e, t).length !== 0 || this.storeCompressionForTx(e, t) !== !1) {
|
|
1532
|
-
await this.putMany(e, t, Et(r), o);
|
|
1533
|
-
return;
|
|
1534
|
-
}
|
|
1535
|
-
const s = St(r);
|
|
1536
|
-
if (s.length === 0)
|
|
1537
|
-
return;
|
|
1538
|
-
let a;
|
|
1539
|
-
try {
|
|
1540
|
-
a = this.withEngine(
|
|
1541
|
-
(c) => c.put_many_packed(p(e), t, r, te(o))
|
|
1542
|
-
);
|
|
1543
|
-
} catch (c) {
|
|
1544
|
-
const l = q(c);
|
|
1545
|
-
throw this.recordPutKeyChanges(e, t, s, l), c;
|
|
1546
|
-
}
|
|
1547
|
-
if (a.length !== s.length)
|
|
1548
|
-
throw m(
|
|
1549
|
-
"InternalError",
|
|
1550
|
-
`put_many_packed outcome count mismatch: ${a.length} != ${s.length}`
|
|
1551
|
-
);
|
|
1552
|
-
this.recordPutKeyChanges(e, t, s, a);
|
|
1553
|
-
}
|
|
1554
|
-
async delete(e, t, r) {
|
|
1555
|
-
const o = await this.readStoreValue(e, t, r), s = (o === null ? [] : this.indexesForStoreInTx(e, t)).map((c) => ({ def: c, logicalKey: I(c, o) }));
|
|
1556
|
-
if (!this.withEngine((c) => c.delete(p(e), t, r)))
|
|
1557
|
-
return this.recordDeleteChange(e, t, r, !1), !1;
|
|
1558
|
-
for (const c of s)
|
|
1559
|
-
c.logicalKey !== null && this.deleteIndexEntryIfPresent(e, c.def, c.logicalKey, r);
|
|
1560
|
-
return this.recordDeleteChange(e, t, r, !0), !0;
|
|
1561
|
-
}
|
|
1562
|
-
async deleteMany(e, t, r) {
|
|
1563
|
-
if (r.length === 0)
|
|
1564
|
-
return;
|
|
1565
|
-
if (this.indexesForStoreInTx(e, t).length === 0) {
|
|
1566
|
-
let i;
|
|
1567
|
-
try {
|
|
1568
|
-
i = this.withEngine((s) => s.delete_many(p(e), t, r));
|
|
1569
|
-
} catch (s) {
|
|
1570
|
-
const a = q(s);
|
|
1571
|
-
for (let c = 0; c < a.length && c < r.length; c += 1)
|
|
1572
|
-
this.recordDeleteChange(e, t, r[c], a[c]);
|
|
1573
|
-
throw s;
|
|
1574
|
-
}
|
|
1575
|
-
if (i.length !== r.length)
|
|
1576
|
-
throw m(
|
|
1577
|
-
"InternalError",
|
|
1578
|
-
`delete_many outcome count mismatch: ${i.length} != ${r.length}`
|
|
1579
|
-
);
|
|
1580
|
-
for (let s = 0; s < r.length; s += 1)
|
|
1581
|
-
this.recordDeleteChange(e, t, r[s], i[s]);
|
|
1582
|
-
return;
|
|
1583
|
-
}
|
|
1584
|
-
for (const i of r)
|
|
1585
|
-
await this.delete(e, t, i);
|
|
1586
|
-
}
|
|
1587
|
-
async deleteManyPacked(e, t, r) {
|
|
1588
|
-
const o = this.indexesForStoreInTx(e, t), i = bt(r);
|
|
1589
|
-
if (i.length === 0)
|
|
1590
|
-
return;
|
|
1591
|
-
if (o.length !== 0) {
|
|
1592
|
-
await this.deleteMany(e, t, i);
|
|
1593
|
-
return;
|
|
1594
|
-
}
|
|
1595
|
-
let s;
|
|
1596
|
-
try {
|
|
1597
|
-
s = this.withEngine(
|
|
1598
|
-
(a) => a.delete_many_packed(p(e), t, r)
|
|
1599
|
-
);
|
|
1600
|
-
} catch (a) {
|
|
1601
|
-
const c = q(a);
|
|
1602
|
-
for (let l = 0; l < c.length && l < i.length; l += 1)
|
|
1603
|
-
this.recordDeleteChange(e, t, i[l], c[l]);
|
|
1604
|
-
throw a;
|
|
1605
|
-
}
|
|
1606
|
-
if (s.length !== i.length)
|
|
1607
|
-
throw m(
|
|
1608
|
-
"InternalError",
|
|
1609
|
-
`delete_many_packed outcome count mismatch: ${s.length} != ${i.length}`
|
|
1610
|
-
);
|
|
1611
|
-
for (let a = 0; a < i.length; a += 1)
|
|
1612
|
-
this.recordDeleteChange(e, t, i[a], s[a]);
|
|
1613
|
-
}
|
|
1614
|
-
async applyBatch(e, t, r) {
|
|
1615
|
-
if (r.length === 0)
|
|
1616
|
-
return;
|
|
1617
|
-
if (this.indexesForStoreInTx(e, t).length === 0) {
|
|
1618
|
-
const i = await this.encodeBatchOpsForWrite(e, t, r);
|
|
1619
|
-
let s;
|
|
1620
|
-
try {
|
|
1621
|
-
s = this.withEngine(
|
|
1622
|
-
(a) => a.apply_batch(p(e), t, i)
|
|
1623
|
-
);
|
|
1624
|
-
} catch (a) {
|
|
1625
|
-
throw this.recordBatchOutcomes(e, t, r, Ie(a)), a;
|
|
1626
|
-
}
|
|
1627
|
-
if (s.length !== r.length)
|
|
1628
|
-
throw m(
|
|
1629
|
-
"InternalError",
|
|
1630
|
-
`apply_batch outcome count mismatch: ${s.length} != ${r.length}`
|
|
1631
|
-
);
|
|
1632
|
-
this.recordBatchOutcomes(e, t, r, s);
|
|
1633
|
-
return;
|
|
1634
|
-
}
|
|
1635
|
-
for (const i of r)
|
|
1636
|
-
i.kind === "put" ? await this.put(e, t, i.key, i.value) : await this.delete(e, t, i.key);
|
|
1637
|
-
}
|
|
1638
|
-
async applyBatchPacked(e, t, r) {
|
|
1639
|
-
if (this.indexesForStoreInTx(e, t).length !== 0 || this.storeCompressionForTx(e, t) !== !1) {
|
|
1640
|
-
const a = kt(r);
|
|
1641
|
-
if (a.length === 0)
|
|
1642
|
-
return;
|
|
1643
|
-
await this.applyBatch(e, t, a);
|
|
1644
|
-
return;
|
|
1645
|
-
}
|
|
1646
|
-
const i = xt(r);
|
|
1647
|
-
if (i.length === 0)
|
|
1648
|
-
return;
|
|
1649
|
-
let s;
|
|
1650
|
-
try {
|
|
1651
|
-
s = this.withEngine(
|
|
1652
|
-
(a) => a.apply_batch_packed(p(e), t, r)
|
|
1653
|
-
);
|
|
1654
|
-
} catch (a) {
|
|
1655
|
-
throw this.recordBatchKeyOutcomes(e, t, i, Ie(a)), a;
|
|
1656
|
-
}
|
|
1657
|
-
if (s.length !== i.length)
|
|
1658
|
-
throw m(
|
|
1659
|
-
"InternalError",
|
|
1660
|
-
`apply_batch_packed outcome count mismatch: ${s.length} != ${i.length}`
|
|
1661
|
-
);
|
|
1662
|
-
this.recordBatchKeyOutcomes(e, t, i, s);
|
|
1663
|
-
}
|
|
1664
|
-
async scan(e, t, r) {
|
|
1665
|
-
return this.readStoreScan(e, t, r);
|
|
1666
|
-
}
|
|
1667
|
-
async getByIndex(e, t, r, o) {
|
|
1668
|
-
const i = this.resolveIndexDefinition(e, t, r), s = await this.loadVisibleIndexRows(e, i, qe(o));
|
|
1669
|
-
return s.length === 0 ? null : s[0].value;
|
|
1670
|
-
}
|
|
1671
|
-
async scanByIndex(e, t, r, o = {}) {
|
|
1672
|
-
const i = this.resolveIndexDefinition(e, t, r);
|
|
1673
|
-
return this.loadVisibleIndexRows(e, i, o);
|
|
1674
|
-
}
|
|
1675
|
-
async getIndexes() {
|
|
1676
|
-
return ce(this.loadCommittedIndexSchema());
|
|
1677
|
-
}
|
|
1678
|
-
async reconcileIndexes(e, t) {
|
|
1679
|
-
const r = ye(t);
|
|
1680
|
-
this.ensureRawStore(e, N);
|
|
1681
|
-
const o = this.readIndexSchemaInTx(e), i = new Set(r.map((s) => s.internalStore));
|
|
1682
|
-
for (const s of o)
|
|
1683
|
-
i.has(s.internalStore) || this.dropRawStoreIfExists(e, s.internalStore);
|
|
1684
|
-
this.clearRawStoreIfExists(e, N);
|
|
1685
|
-
for (const s of r) {
|
|
1686
|
-
if (this.ensureRawStore(e, s.internalStore), this.clearRawStoreIfExists(e, s.internalStore), this.rawStoreExistsInTx(e, s.store)) {
|
|
1687
|
-
const a = await this.readStoreScan(e, s.store, V);
|
|
1688
|
-
for (const c of a) {
|
|
1689
|
-
const l = I(s, c.value);
|
|
1690
|
-
l !== null && (await this.assertUniqueIndexAvailability(e, s, l, c.key), this.putIndexEntry(e, s, this.buildIndexEntryKey(s, l, c.key), Te));
|
|
1691
|
-
}
|
|
1692
|
-
}
|
|
1693
|
-
this.putRawValue(
|
|
1694
|
-
e,
|
|
1695
|
-
N,
|
|
1696
|
-
hn(s.store, s.name),
|
|
1697
|
-
un(s)
|
|
1698
|
-
);
|
|
1699
|
-
}
|
|
1700
|
-
this.txIndexSchemas.set(e, O(r));
|
|
1701
|
-
}
|
|
1702
|
-
async listStores() {
|
|
1703
|
-
return this.currentStoreNames();
|
|
1704
|
-
}
|
|
1705
|
-
async getVersion() {
|
|
1706
|
-
return b(this.withEngine((e) => e.get_schema_version()));
|
|
1707
|
-
}
|
|
1708
|
-
async changesSince(e, t = {}) {
|
|
1709
|
-
const r = t.stores?.filter((c) => !w(c)), o = {
|
|
1710
|
-
...t,
|
|
1711
|
-
stores: r ? [...r] : void 0,
|
|
1712
|
-
limit: r ? t.limit : void 0
|
|
1713
|
-
}, i = this.withEngine(
|
|
1714
|
-
(c) => c.changes_since(p(e), o)
|
|
1715
|
-
), s = r ? new Set(r) : null, a = [];
|
|
1716
|
-
for (const c of i.changes) {
|
|
1717
|
-
if (w(c.store) || s && !s.has(c.store))
|
|
1718
|
-
continue;
|
|
1719
|
-
const l = _(c.key);
|
|
1720
|
-
let h;
|
|
1721
|
-
c.value !== void 0 && (h = await this.decodeStoreValueForFeed(c.store, _(c.value))), a.push({
|
|
1722
|
-
txId: b(c.txId),
|
|
1723
|
-
store: c.store,
|
|
1724
|
-
key: l,
|
|
1725
|
-
kind: c.kind,
|
|
1726
|
-
value: h
|
|
1727
|
-
});
|
|
1728
|
-
}
|
|
1729
|
-
return {
|
|
1730
|
-
latestTxId: b(i.latestTxId),
|
|
1731
|
-
changes: t.limit === void 0 ? a : a.slice(0, t.limit)
|
|
1732
|
-
};
|
|
1733
|
-
}
|
|
1734
|
-
async setSchemaVersion(e, t) {
|
|
1735
|
-
this.withEngine((r) => r.set_schema_version(p(e), p(t)));
|
|
1736
|
-
}
|
|
1737
|
-
async exportSnapshot(e = {}) {
|
|
1738
|
-
const t = O(this.loadCommittedIndexSchema()), r = this.withEngine((i) => i.export_snapshot()), o = In(_(r), t);
|
|
1739
|
-
return await Zt(o, e.compression ?? !1);
|
|
1740
|
-
}
|
|
1741
|
-
async importSnapshot(e) {
|
|
1742
|
-
this.rollbackAllTransactions();
|
|
1743
|
-
const t = await Qt(e), r = vn(t);
|
|
1744
|
-
this.withEngine((o) => o.import_snapshot(r.snapshot)), this.committedIndexes = [], this.committedStoreCompression = null, await this.applyCommittedIndexSchema(r.indexes);
|
|
1745
|
-
}
|
|
1746
|
-
async reset() {
|
|
1747
|
-
const e = this.dbName;
|
|
1748
|
-
if (!e)
|
|
1749
|
-
throw m("InternalError", "engine is not open");
|
|
1750
|
-
const t = O(this.loadCommittedIndexSchema());
|
|
1751
|
-
this.rollbackAllTransactions();
|
|
1752
|
-
const r = this.captureResetChanges(), o = b(this.withEngine((i) => i.reset()));
|
|
1753
|
-
if (this.committedIndexes = [], await this.applyCommittedIndexSchema(t), this.events) {
|
|
1754
|
-
const i = {
|
|
1755
|
-
type: "commit_applied",
|
|
1756
|
-
dbName: e,
|
|
1757
|
-
txid: o,
|
|
1758
|
-
stores: r
|
|
1759
|
-
};
|
|
1760
|
-
this.events.postMessage(i);
|
|
1761
|
-
}
|
|
1762
|
-
}
|
|
1763
|
-
async compact() {
|
|
1764
|
-
return this.runCompactionOperation("compact");
|
|
1765
|
-
}
|
|
1766
|
-
async rebuild() {
|
|
1767
|
-
return this.runCompactionOperation("rebuild");
|
|
1768
|
-
}
|
|
1769
|
-
async stats() {
|
|
1770
|
-
const e = this.withEngine((t) => t.stats());
|
|
1771
|
-
return {
|
|
1772
|
-
...e,
|
|
1773
|
-
db_id: b(e.db_id),
|
|
1774
|
-
catalog_root_page_id: b(e.catalog_root_page_id),
|
|
1775
|
-
next_page_id: b(e.next_page_id),
|
|
1776
|
-
last_committed_txid: b(e.last_committed_txid),
|
|
1777
|
-
last_replayed_wal_offset: b(e.last_replayed_wal_offset),
|
|
1778
|
-
manifest_len: b(e.manifest_len),
|
|
1779
|
-
main_len: b(e.main_len),
|
|
1780
|
-
wal_len: b(e.wal_len),
|
|
1781
|
-
store_count: this.currentStoreNames().length
|
|
1782
|
-
};
|
|
1783
|
-
}
|
|
1784
|
-
async storageInfo() {
|
|
1785
|
-
const [e, t, r] = await Promise.all([
|
|
1786
|
-
this.dbDirectorySize(),
|
|
1787
|
-
Rn(),
|
|
1788
|
-
this.persistenceBridge.persisted()
|
|
1789
|
-
]);
|
|
1790
|
-
return {
|
|
1791
|
-
dbSize: e,
|
|
1792
|
-
originUsage: t.usage,
|
|
1793
|
-
originQuota: t.quota,
|
|
1794
|
-
persisted: r
|
|
1795
|
-
};
|
|
1796
|
-
}
|
|
1797
|
-
async requestPersistence() {
|
|
1798
|
-
return this.persistenceBridge.persist();
|
|
1799
|
-
}
|
|
1800
|
-
async setFailpoint(e) {
|
|
1801
|
-
this.withEngine((t) => t.set_failpoint(e));
|
|
1802
|
-
}
|
|
1803
|
-
requireDbName() {
|
|
1804
|
-
if (!this.dbName)
|
|
1805
|
-
throw m("InternalError", "engine is not open");
|
|
1806
|
-
return this.dbName;
|
|
1807
|
-
}
|
|
1808
|
-
ensureNotInMaintenance() {
|
|
1809
|
-
if (!this.maintenanceOperation)
|
|
1810
|
-
return;
|
|
1811
|
-
const e = this.dbName ?? "database";
|
|
1812
|
-
throw m("DatabaseBusyError", `database ${e} is busy: ${this.maintenanceOperation} in progress`);
|
|
1813
|
-
}
|
|
1814
|
-
currentCachePages() {
|
|
1815
|
-
return this.openOptions?.cachePages ?? 256;
|
|
1816
|
-
}
|
|
1817
|
-
async dbDirectorySize() {
|
|
1818
|
-
return (await this.loadWasm()).dbDirectorySize(this.requireDbName());
|
|
1819
|
-
}
|
|
1820
|
-
async applyCommittedIndexSchema(e) {
|
|
1821
|
-
const t = O(e);
|
|
1822
|
-
if (t.length === 0) {
|
|
1823
|
-
this.committedIndexes = [];
|
|
1824
|
-
return;
|
|
1825
|
-
}
|
|
1826
|
-
const r = b(
|
|
1827
|
-
this.withEngine((o) => o.begin_tx("readwrite"), { allowDuringMaintenance: !0 })
|
|
1828
|
-
);
|
|
1829
|
-
this.txChanges.set(r, /* @__PURE__ */ new Map()), this.txModes.set(r, "readwrite"), this.txStoreCompression.set(r, new Map(this.loadCommittedStoreCompression()));
|
|
1830
|
-
try {
|
|
1831
|
-
await this.reconcileIndexes(r, ce(t)), this.withEngine((o) => o.commit_tx(p(r)), { allowDuringMaintenance: !0 }), this.committedIndexes = O(t);
|
|
1832
|
-
} catch (o) {
|
|
1833
|
-
try {
|
|
1834
|
-
this.withEngine((i) => i.rollback_tx(p(r)), { allowDuringMaintenance: !0 });
|
|
1835
|
-
} catch {
|
|
1836
|
-
}
|
|
1837
|
-
throw o;
|
|
1838
|
-
} finally {
|
|
1839
|
-
this.cleanupTxState(r);
|
|
1840
|
-
}
|
|
1841
|
-
}
|
|
1842
|
-
async runCompactionOperation(e) {
|
|
1843
|
-
const t = this.requireDbName();
|
|
1844
|
-
if (this.maintenanceOperation)
|
|
1845
|
-
throw m(
|
|
1846
|
-
"DatabaseBusyError",
|
|
1847
|
-
`database ${t} is busy: ${this.maintenanceOperation} in progress`
|
|
1848
|
-
);
|
|
1849
|
-
this.maintenanceOperation = e;
|
|
1850
|
-
const r = performance.now(), o = await this.loadWasm();
|
|
1851
|
-
let i = null, s = null, a = !1;
|
|
1852
|
-
try {
|
|
1853
|
-
const c = await o.dbDirectorySize(t), l = O(this.loadCommittedIndexSchema()), h = _(
|
|
1854
|
-
this.withEngine((y) => y.export_snapshot(), { allowDuringMaintenance: !0 })
|
|
1855
|
-
);
|
|
1856
|
-
if (this.rollbackAllTransactions({ allowDuringMaintenance: !0 }), s = (await o.prepareRebuildTarget(t)).generationName, i = new o.WasmEngine(), await i.openGeneration(t, s, {
|
|
1857
|
-
create_if_missing: !0,
|
|
1858
|
-
cache_pages: this.currentCachePages()
|
|
1859
|
-
}), i.import_snapshot(h), l.length > 0) {
|
|
1860
|
-
const y = this.engine, u = this.maintenanceOperation;
|
|
1861
|
-
this.engine = i, this.maintenanceOperation = null;
|
|
1862
|
-
try {
|
|
1863
|
-
this.committedIndexes = [], await this.applyCommittedIndexSchema(l);
|
|
1864
|
-
} finally {
|
|
1865
|
-
this.engine = y, this.maintenanceOperation = u;
|
|
1866
|
-
}
|
|
1867
|
-
}
|
|
1868
|
-
await o.swapActiveGeneration(t, s), a = !0;
|
|
1869
|
-
const d = this.engine;
|
|
1870
|
-
this.engine = i, i = null, this.committedIndexes = l, this.committedStoreCompression = null;
|
|
1871
|
-
try {
|
|
1872
|
-
d?.close();
|
|
1873
|
-
} catch {
|
|
1874
|
-
}
|
|
1875
|
-
try {
|
|
1876
|
-
await o.cleanupInactiveEntries(t);
|
|
1877
|
-
} catch {
|
|
1878
|
-
}
|
|
1879
|
-
const g = await o.dbDirectorySize(t);
|
|
1880
|
-
return {
|
|
1881
|
-
sizeBefore: c,
|
|
1882
|
-
sizeAfter: g,
|
|
1883
|
-
reclaimedBytes: Math.max(0, c - g),
|
|
1884
|
-
durationMs: performance.now() - r
|
|
1885
|
-
};
|
|
1886
|
-
} catch (c) {
|
|
1887
|
-
if (!a) {
|
|
1888
|
-
try {
|
|
1889
|
-
i?.close();
|
|
1890
|
-
} catch {
|
|
1891
|
-
}
|
|
1892
|
-
try {
|
|
1893
|
-
await o.cleanupInactiveEntries(t);
|
|
1894
|
-
} catch {
|
|
1895
|
-
}
|
|
1896
|
-
}
|
|
1897
|
-
throw v(c);
|
|
1898
|
-
} finally {
|
|
1899
|
-
this.maintenanceOperation = null;
|
|
1900
|
-
}
|
|
1901
|
-
}
|
|
1902
|
-
cleanupTxState(e) {
|
|
1903
|
-
this.txChanges.delete(e), this.txModes.delete(e), this.txIndexSchemas.delete(e), this.txStoreCompression.delete(e);
|
|
1904
|
-
}
|
|
1905
|
-
getOrCreateTrackedTxn(e) {
|
|
1906
|
-
return ne(this.txChanges, e, () => /* @__PURE__ */ new Map());
|
|
1907
|
-
}
|
|
1908
|
-
getOrCreateTrackedStore(e, t) {
|
|
1909
|
-
return ne(this.getOrCreateTrackedTxn(e), t, () => ({
|
|
1910
|
-
touched: !1,
|
|
1911
|
-
keys: /* @__PURE__ */ new Map()
|
|
1912
|
-
}));
|
|
1913
|
-
}
|
|
1914
|
-
recordPutChange(e, t, r, o) {
|
|
1915
|
-
this.recordKeyChange(e, t, r, o, "put");
|
|
1916
|
-
}
|
|
1917
|
-
recordPutChanges(e, t, r, o) {
|
|
1918
|
-
if (w(t))
|
|
1919
|
-
return;
|
|
1920
|
-
const i = this.getOrCreateTrackedStore(e, t);
|
|
1921
|
-
i.touched = !0;
|
|
1922
|
-
const s = Math.min(r.length, o.length);
|
|
1923
|
-
for (let a = 0; a < s; a += 1)
|
|
1924
|
-
this.setTrackedKeyChange(i, r[a][0], o[a], "put");
|
|
1925
|
-
}
|
|
1926
|
-
recordPutKeyChanges(e, t, r, o) {
|
|
1927
|
-
if (w(t))
|
|
1928
|
-
return;
|
|
1929
|
-
const i = this.getOrCreateTrackedStore(e, t);
|
|
1930
|
-
i.touched = !0;
|
|
1931
|
-
const s = Math.min(r.length, o.length);
|
|
1932
|
-
for (let a = 0; a < s; a += 1)
|
|
1933
|
-
this.setTrackedKeyChange(i, r[a], o[a], "put");
|
|
1934
|
-
}
|
|
1935
|
-
recordDeleteChange(e, t, r, o) {
|
|
1936
|
-
o && this.recordKeyChange(e, t, r, !0, "delete");
|
|
1937
|
-
}
|
|
1938
|
-
recordKeyChange(e, t, r, o, i) {
|
|
1939
|
-
if (w(t))
|
|
1940
|
-
return;
|
|
1941
|
-
const s = this.getOrCreateTrackedStore(e, t);
|
|
1942
|
-
s.touched = !0, this.setTrackedKeyChange(s, r, o, i);
|
|
1943
|
-
}
|
|
1944
|
-
setTrackedKeyChange(e, t, r, o) {
|
|
1945
|
-
const i = Le(t), s = e.keys.get(i);
|
|
1946
|
-
e.keys.set(i, {
|
|
1947
|
-
key: s?.key ?? t.slice(),
|
|
1948
|
-
baselineExists: s?.baselineExists ?? r,
|
|
1949
|
-
finalKind: o
|
|
1950
|
-
});
|
|
1951
|
-
}
|
|
1952
|
-
markStoreTouched(e, t) {
|
|
1953
|
-
w(t) || (this.getOrCreateTrackedStore(e, t).touched = !0);
|
|
1954
|
-
}
|
|
1955
|
-
loadStoreKeys(e, t) {
|
|
1956
|
-
return this.readRawScan(e, t, V).map((o) => o.key.slice());
|
|
1957
|
-
}
|
|
1958
|
-
recordDeletedKeys(e, t, r) {
|
|
1959
|
-
if (w(t))
|
|
1960
|
-
return;
|
|
1961
|
-
const o = this.getOrCreateTrackedStore(e, t);
|
|
1962
|
-
o.touched = !0;
|
|
1963
|
-
for (const i of r)
|
|
1964
|
-
this.setTrackedKeyChange(o, i, !0, "delete");
|
|
1965
|
-
}
|
|
1966
|
-
recordBatchOutcomes(e, t, r, o) {
|
|
1967
|
-
for (let i = 0; i < o.length && i < r.length; i += 1) {
|
|
1968
|
-
const s = r[i], a = o[i];
|
|
1969
|
-
s.kind === "put" && a.kind === "put" ? this.recordPutChange(e, t, s.key, a.baselineExists) : s.kind === "delete" && a.kind === "delete" && this.recordDeleteChange(e, t, s.key, a.deleted);
|
|
1970
|
-
}
|
|
1971
|
-
}
|
|
1972
|
-
recordBatchKeyOutcomes(e, t, r, o) {
|
|
1973
|
-
for (let i = 0; i < o.length && i < r.length; i += 1) {
|
|
1974
|
-
const s = r[i], a = o[i];
|
|
1975
|
-
s.kind === "put" && a.kind === "put" ? this.recordPutChange(e, t, s.key, a.baselineExists) : s.kind === "delete" && a.kind === "delete" && this.recordDeleteChange(e, t, s.key, a.deleted);
|
|
1976
|
-
}
|
|
1977
|
-
}
|
|
1978
|
-
finalizeTrackedChanges(e) {
|
|
1979
|
-
const t = [];
|
|
1980
|
-
for (const [r, o] of e) {
|
|
1981
|
-
if (!o.touched || w(r))
|
|
1982
|
-
continue;
|
|
1983
|
-
const i = [];
|
|
1984
|
-
for (const s of o.keys.values())
|
|
1985
|
-
s.finalKind === "delete" && !s.baselineExists || i.push({
|
|
1986
|
-
key: s.key,
|
|
1987
|
-
kind: s.finalKind
|
|
1988
|
-
});
|
|
1989
|
-
t.push({ store: r, changes: i });
|
|
1990
|
-
}
|
|
1991
|
-
return t;
|
|
1992
|
-
}
|
|
1993
|
-
rollbackAllTransactions(e = {}) {
|
|
1994
|
-
if (!this.engine || this.txChanges.size === 0) {
|
|
1995
|
-
this.clearTxCaches();
|
|
1996
|
-
return;
|
|
1997
|
-
}
|
|
1998
|
-
const t = Array.from(this.txChanges.keys());
|
|
1999
|
-
for (const r of t)
|
|
2000
|
-
try {
|
|
2001
|
-
this.withEngine((o) => o.rollback_tx(p(r)), e);
|
|
2002
|
-
} finally {
|
|
2003
|
-
this.cleanupTxState(r);
|
|
2004
|
-
}
|
|
2005
|
-
}
|
|
2006
|
-
currentStoreNames() {
|
|
2007
|
-
return this.allRawStoreNames().filter((e) => !w(e));
|
|
2008
|
-
}
|
|
2009
|
-
allRawStoreNames() {
|
|
2010
|
-
return this.withEngine((e) => e.list_stores());
|
|
2011
|
-
}
|
|
2012
|
-
loadCommittedStoreCompression() {
|
|
2013
|
-
if (this.committedStoreCompression)
|
|
2014
|
-
return this.committedStoreCompression;
|
|
2015
|
-
const e = this.withEngine((r) => r.list_store_configs()), t = /* @__PURE__ */ new Map();
|
|
2016
|
-
for (const r of e) {
|
|
2017
|
-
if (typeof r?.name != "string")
|
|
2018
|
-
throw m("CorruptionError", "invalid store config row: missing name");
|
|
2019
|
-
const o = typeof r.flags == "bigint" ? Number(r.flags) : r.flags;
|
|
2020
|
-
if (typeof o != "number" || !Number.isSafeInteger(o) || o < 0)
|
|
2021
|
-
throw m("CorruptionError", `invalid store flags for ${r.name}`);
|
|
2022
|
-
t.set(r.name, Jt(o));
|
|
2023
|
-
}
|
|
2024
|
-
return this.committedStoreCompression = t, t;
|
|
2025
|
-
}
|
|
2026
|
-
ensureTxStoreCompression(e) {
|
|
2027
|
-
return ne(this.txStoreCompression, e, () => /* @__PURE__ */ new Map());
|
|
2028
|
-
}
|
|
2029
|
-
recordStoreCompressionCreate(e, t, r) {
|
|
2030
|
-
w(t) || this.ensureTxStoreCompression(e).set(t, r);
|
|
2031
|
-
}
|
|
2032
|
-
recordStoreCompressionDrop(e, t) {
|
|
2033
|
-
w(t) || this.ensureTxStoreCompression(e).delete(t);
|
|
2034
|
-
}
|
|
2035
|
-
storeCompressionForTx(e, t) {
|
|
2036
|
-
if (w(t))
|
|
2037
|
-
return !1;
|
|
2038
|
-
const r = this.txStoreCompression.get(e);
|
|
2039
|
-
return r ? r.get(t) ?? !1 : this.loadCommittedStoreCompression().get(t) ?? !1;
|
|
2040
|
-
}
|
|
2041
|
-
async encodeEntriesForWrite(e, t, r) {
|
|
2042
|
-
if (w(t))
|
|
2043
|
-
return r;
|
|
2044
|
-
const o = this.storeCompressionForTx(e, t);
|
|
2045
|
-
if (o === !1)
|
|
2046
|
-
return r;
|
|
2047
|
-
const i = new Array(r.length);
|
|
2048
|
-
for (let s = 0; s < r.length; s += 1) {
|
|
2049
|
-
const [a, c] = r[s];
|
|
2050
|
-
i[s] = [a, await ee(c, o)];
|
|
2051
|
-
}
|
|
2052
|
-
return i;
|
|
2053
|
-
}
|
|
2054
|
-
async encodeBatchOpsForWrite(e, t, r) {
|
|
2055
|
-
if (w(t))
|
|
2056
|
-
return r;
|
|
2057
|
-
const o = this.storeCompressionForTx(e, t);
|
|
2058
|
-
if (o === !1)
|
|
2059
|
-
return r;
|
|
2060
|
-
const i = new Array(r.length);
|
|
2061
|
-
for (let s = 0; s < r.length; s += 1) {
|
|
2062
|
-
const a = r[s];
|
|
2063
|
-
i[s] = a.kind === "put" ? { kind: "put", key: a.key, value: await ee(a.value, o) } : a;
|
|
2064
|
-
}
|
|
2065
|
-
return i;
|
|
2066
|
-
}
|
|
2067
|
-
async encodeStoreValueForWrite(e, t, r) {
|
|
2068
|
-
return w(t) ? r : ee(r, this.storeCompressionForTx(e, t));
|
|
2069
|
-
}
|
|
2070
|
-
async decodeStoreValue(e, t, r) {
|
|
2071
|
-
return w(t) || this.storeCompressionForTx(e, t) === !1 ? r : Ee(r, { strict: !0 });
|
|
2072
|
-
}
|
|
2073
|
-
async decodeStoreValueForFeed(e, t) {
|
|
2074
|
-
if (w(e))
|
|
2075
|
-
return t;
|
|
2076
|
-
const r = this.loadCommittedStoreCompression().get(e) ?? !1;
|
|
2077
|
-
return Ee(t, { strict: r !== !1 });
|
|
2078
|
-
}
|
|
2079
|
-
async readStoreValue(e, t, r) {
|
|
2080
|
-
const o = this.readRawValue(e, t, r);
|
|
2081
|
-
return o === null ? null : this.decodeStoreValue(e, t, o);
|
|
2082
|
-
}
|
|
2083
|
-
async readStoreScan(e, t, r) {
|
|
2084
|
-
const o = this.readRawScan(e, t, r);
|
|
2085
|
-
return w(t) || this.storeCompressionForTx(e, t) === !1 ? o : await Promise.all(
|
|
2086
|
-
o.map(async (i) => ({
|
|
2087
|
-
key: i.key,
|
|
2088
|
-
value: await this.decodeStoreValue(e, t, i.value)
|
|
2089
|
-
}))
|
|
2090
|
-
);
|
|
2091
|
-
}
|
|
2092
|
-
captureResetChanges() {
|
|
2093
|
-
const e = this.currentStoreNames();
|
|
2094
|
-
if (e.length === 0)
|
|
2095
|
-
return [];
|
|
2096
|
-
const t = b(this.withEngine((r) => r.begin_tx("readonly")));
|
|
2097
|
-
try {
|
|
2098
|
-
return e.map((r) => {
|
|
2099
|
-
const o = this.readRawScan(t, r, V);
|
|
2100
|
-
return {
|
|
2101
|
-
store: r,
|
|
2102
|
-
changes: o.map((i) => ({
|
|
2103
|
-
key: i.key.slice(),
|
|
2104
|
-
kind: "delete"
|
|
2105
|
-
}))
|
|
2106
|
-
};
|
|
2107
|
-
});
|
|
2108
|
-
} finally {
|
|
2109
|
-
this.withEngine((r) => r.rollback_tx(p(t)));
|
|
2110
|
-
}
|
|
2111
|
-
}
|
|
2112
|
-
indexesForStoreInTx(e, t) {
|
|
2113
|
-
return ln(this.indexSchemaForTx(e), t);
|
|
2114
|
-
}
|
|
2115
|
-
resolveIndexDefinition(e, t, r) {
|
|
2116
|
-
if (!this.rawStoreExistsInTx(e, t))
|
|
2117
|
-
throw m("StoreNotFoundError", `store not found: ${t}`);
|
|
2118
|
-
const o = cn(this.indexSchemaForTx(e), t, r);
|
|
2119
|
-
if (!o)
|
|
2120
|
-
throw m("IndexNotFoundError", `index not found: ${t}.${r}`);
|
|
2121
|
-
return o;
|
|
2122
|
-
}
|
|
2123
|
-
indexSchemaForTx(e) {
|
|
2124
|
-
const t = this.txIndexSchemas.get(e);
|
|
2125
|
-
if (t)
|
|
2126
|
-
return t;
|
|
2127
|
-
if (this.committedIndexes)
|
|
2128
|
-
return this.committedIndexes;
|
|
2129
|
-
const r = this.readIndexSchemaInTx(e);
|
|
2130
|
-
return this.committedIndexes = O(r), this.committedIndexes;
|
|
2131
|
-
}
|
|
2132
|
-
loadCommittedIndexSchema() {
|
|
2133
|
-
if (this.committedIndexes)
|
|
2134
|
-
return this.committedIndexes;
|
|
2135
|
-
const e = b(this.withEngine((t) => t.begin_tx("readonly")));
|
|
2136
|
-
try {
|
|
2137
|
-
const t = this.readIndexSchemaInTx(e);
|
|
2138
|
-
return this.committedIndexes = O(t), this.committedIndexes;
|
|
2139
|
-
} finally {
|
|
2140
|
-
this.withEngine((t) => t.rollback_tx(p(e)));
|
|
2141
|
-
}
|
|
2142
|
-
}
|
|
2143
|
-
readIndexSchemaInTx(e) {
|
|
2144
|
-
if (!this.rawStoreExistsInTx(e, N))
|
|
2145
|
-
return [];
|
|
2146
|
-
const r = this.readRawScan(e, N, V).map((o) => dn(o.value));
|
|
2147
|
-
return r.sort(rt), r;
|
|
2148
|
-
}
|
|
2149
|
-
rawStoreExistsInTx(e, t) {
|
|
2150
|
-
try {
|
|
2151
|
-
return this.readRawScan(e, t, { limit: 0 }), !0;
|
|
2152
|
-
} catch (r) {
|
|
2153
|
-
if (re(r, "StoreNotFoundError"))
|
|
2154
|
-
return !1;
|
|
2155
|
-
throw r;
|
|
2156
|
-
}
|
|
2157
|
-
}
|
|
2158
|
-
ensureRawStore(e, t) {
|
|
2159
|
-
this.ignoreNamedError("StoreExistsError", () => {
|
|
2160
|
-
this.withEngine((r) => r.create_store(p(e), t));
|
|
2161
|
-
});
|
|
2162
|
-
}
|
|
2163
|
-
clearRawStoreIfExists(e, t) {
|
|
2164
|
-
this.ignoreNamedError("StoreNotFoundError", () => {
|
|
2165
|
-
this.withEngine((r) => r.clear_store(p(e), t));
|
|
2166
|
-
});
|
|
2167
|
-
}
|
|
2168
|
-
dropRawStoreIfExists(e, t) {
|
|
2169
|
-
this.ignoreNamedError("StoreNotFoundError", () => {
|
|
2170
|
-
this.withEngine((r) => r.drop_store(p(e), t));
|
|
2171
|
-
});
|
|
2172
|
-
}
|
|
2173
|
-
ensureInternalStoresForDefinitions(e, t) {
|
|
2174
|
-
for (const r of t)
|
|
2175
|
-
this.ensureRawStore(e, r.internalStore);
|
|
2176
|
-
}
|
|
2177
|
-
ensureInternalStoresForStore(e, t) {
|
|
2178
|
-
this.ensureInternalStoresForDefinitions(e, this.indexesForStoreInTx(e, t));
|
|
2179
|
-
}
|
|
2180
|
-
readRawValue(e, t, r) {
|
|
2181
|
-
const o = this.withEngine((i) => i.get(p(e), t, r));
|
|
2182
|
-
return o === null ? null : _(o);
|
|
2183
|
-
}
|
|
2184
|
-
readRawScan(e, t, r) {
|
|
2185
|
-
return this.withEngine((i) => i.scan(p(e), t, xn(r))).map((i) => ({
|
|
2186
|
-
key: _(i.key),
|
|
2187
|
-
value: _(i.value)
|
|
2188
|
-
}));
|
|
2189
|
-
}
|
|
2190
|
-
putRawValue(e, t, r, o) {
|
|
2191
|
-
this.withEngine((i) => i.put(p(e), t, r, o));
|
|
2192
|
-
}
|
|
2193
|
-
putIndexEntry(e, t, r, o) {
|
|
2194
|
-
this.putRawValue(e, t.internalStore, r, o);
|
|
2195
|
-
}
|
|
2196
|
-
deleteIndexEntryIfPresent(e, t, r, o) {
|
|
2197
|
-
const i = this.buildIndexEntryKey(t, r, o);
|
|
2198
|
-
this.ignoreNamedError("StoreNotFoundError", () => {
|
|
2199
|
-
this.withEngine((s) => s.delete(p(e), t.internalStore, i));
|
|
2200
|
-
});
|
|
2201
|
-
}
|
|
2202
|
-
buildIndexEntryKey(e, t, r) {
|
|
2203
|
-
const o = fn(t, r);
|
|
2204
|
-
if (o.byteLength > _n)
|
|
2205
|
-
throw m("KeyTooLargeError", `index key too large for ${e.store}.${e.name}`);
|
|
2206
|
-
return o;
|
|
2207
|
-
}
|
|
2208
|
-
async assertUniqueIndexAvailability(e, t, r, o) {
|
|
2209
|
-
if (!t.unique)
|
|
2210
|
-
return;
|
|
2211
|
-
const i = this.readRawScan(e, t.internalStore, yn(r));
|
|
2212
|
-
for (const s of i) {
|
|
2213
|
-
const a = ke(s.key);
|
|
2214
|
-
if (R(a.primaryKey, o))
|
|
2215
|
-
continue;
|
|
2216
|
-
const c = await this.readStoreValue(e, t.store, a.primaryKey);
|
|
2217
|
-
if (c === null) {
|
|
2218
|
-
this.cleanupStaleIndexRow(e, t, s.key);
|
|
2219
|
-
continue;
|
|
2220
|
-
}
|
|
2221
|
-
const l = I(t, c);
|
|
2222
|
-
if (l === null || !R(l, r)) {
|
|
2223
|
-
this.cleanupStaleIndexRow(e, t, s.key);
|
|
2224
|
-
continue;
|
|
2225
|
-
}
|
|
2226
|
-
throw m(
|
|
2227
|
-
"UniqueIndexConstraintError",
|
|
2228
|
-
`unique index constraint violation on ${t.store}.${t.name}`
|
|
2229
|
-
);
|
|
2230
|
-
}
|
|
2231
|
-
}
|
|
2232
|
-
async loadVisibleIndexRows(e, t, r) {
|
|
2233
|
-
const { limit: o, ...i } = r;
|
|
2234
|
-
let s;
|
|
2235
|
-
try {
|
|
2236
|
-
s = this.readRawScan(e, t.internalStore, pn(i));
|
|
2237
|
-
} catch (c) {
|
|
2238
|
-
if (re(c, "StoreNotFoundError"))
|
|
2239
|
-
return [];
|
|
2240
|
-
throw c;
|
|
2241
|
-
}
|
|
2242
|
-
const a = [];
|
|
2243
|
-
for (const c of s) {
|
|
2244
|
-
const l = await this.resolveVisibleIndexedRow(e, t, c.key);
|
|
2245
|
-
if (l && (a.push(l), o !== void 0 && a.length >= o))
|
|
2246
|
-
break;
|
|
2247
|
-
}
|
|
2248
|
-
return a;
|
|
2249
|
-
}
|
|
2250
|
-
async resolveVisibleIndexedRow(e, t, r) {
|
|
2251
|
-
const o = ke(r), i = await this.readStoreValue(e, t.store, o.primaryKey);
|
|
2252
|
-
if (i === null)
|
|
2253
|
-
return this.cleanupStaleIndexRow(e, t, r), null;
|
|
2254
|
-
const s = I(t, i);
|
|
2255
|
-
return s === null || !R(s, o.logicalKey) ? (this.cleanupStaleIndexRow(e, t, r), null) : {
|
|
2256
|
-
key: o.primaryKey.slice(),
|
|
2257
|
-
value: i
|
|
2258
|
-
};
|
|
2259
|
-
}
|
|
2260
|
-
cleanupStaleIndexRow(e, t, r) {
|
|
2261
|
-
if (this.txModes.get(e) === "readwrite")
|
|
2262
|
-
try {
|
|
2263
|
-
this.withEngine((o) => o.delete(p(e), t.internalStore, r));
|
|
2264
|
-
} catch {
|
|
2265
|
-
}
|
|
2266
|
-
}
|
|
2267
|
-
async loadWasm() {
|
|
2268
|
-
return this.wasmReady || (this.wasmReady = (async () => {
|
|
2269
|
-
const e = "/engine/moyodb_engine.js", t = "/engine/moyodb_engine_bg.wasm", r = await kn(e);
|
|
2270
|
-
return await r.default({ module_or_path: t }), r;
|
|
2271
|
-
})()), this.wasmReady;
|
|
2272
|
-
}
|
|
2273
|
-
withEngine(e, t = {}) {
|
|
2274
|
-
if (this.maintenanceOperation && !t.allowDuringMaintenance && this.ensureNotInMaintenance(), !this.engine)
|
|
2275
|
-
throw m("InternalError", "engine is not open");
|
|
2276
|
-
try {
|
|
2277
|
-
return e(this.engine);
|
|
2278
|
-
} catch (r) {
|
|
2279
|
-
throw v(r);
|
|
2280
|
-
}
|
|
2281
|
-
}
|
|
2282
|
-
ignoreNamedError(e, t) {
|
|
2283
|
-
try {
|
|
2284
|
-
t();
|
|
2285
|
-
} catch (r) {
|
|
2286
|
-
if (re(r, e))
|
|
2287
|
-
return;
|
|
2288
|
-
throw r;
|
|
2289
|
-
}
|
|
2290
|
-
}
|
|
2291
|
-
clearTxCaches() {
|
|
2292
|
-
this.txChanges.clear(), this.txModes.clear(), this.txIndexSchemas.clear(), this.txStoreCompression.clear();
|
|
2293
|
-
}
|
|
2294
|
-
clearRuntimeCaches() {
|
|
2295
|
-
this.clearTxCaches(), this.committedIndexes = null, this.committedStoreCompression = null;
|
|
2296
|
-
}
|
|
2297
|
-
async cleanupAfterFailedOpen(e) {
|
|
2298
|
-
try {
|
|
2299
|
-
e?.close();
|
|
2300
|
-
} catch {
|
|
2301
|
-
}
|
|
2302
|
-
if (this.engine = null, this.clearRuntimeCaches(), this.events?.close(), this.events = null, this.lease)
|
|
2303
|
-
try {
|
|
2304
|
-
await this.lease.release();
|
|
2305
|
-
} catch {
|
|
2306
|
-
}
|
|
2307
|
-
this.lease = null, this.disposePersistenceBridge(), this.dbName = null, this.openOptions = null, this.maintenanceOperation = null;
|
|
2308
|
-
}
|
|
2309
|
-
disposePersistenceBridge() {
|
|
2310
|
-
this.persistenceBridge.close();
|
|
2311
|
-
}
|
|
2312
|
-
}
|
|
2313
|
-
async function Ae() {
|
|
2314
|
-
if (!navigator?.storage)
|
|
2315
|
-
throw m("UnsupportedPlatformError", "navigator.storage is unavailable");
|
|
2316
|
-
if (typeof navigator.storage.getDirectory != "function")
|
|
2317
|
-
throw m("UnsupportedPlatformError", "navigator.storage.getDirectory is unavailable");
|
|
2318
|
-
if (typeof BroadcastChannel > "u")
|
|
2319
|
-
throw m("UnsupportedPlatformError", "BroadcastChannel is unavailable");
|
|
2320
|
-
if (!navigator.locks)
|
|
2321
|
-
throw m("UnsupportedPlatformError", "navigator.locks is unavailable");
|
|
2322
|
-
const e = await (await navigator.storage.getDirectory()).getDirectoryHandle("__moyodb_capability__", { create: !0 }), t = await e.getFileHandle("probe.bin", { create: !0 });
|
|
2323
|
-
if (typeof t.createSyncAccessHandle != "function")
|
|
2324
|
-
throw m("UnsupportedPlatformError", "createSyncAccessHandle is unavailable");
|
|
2325
|
-
(await t.createSyncAccessHandle()).close();
|
|
2326
|
-
try {
|
|
2327
|
-
await e.removeEntry("probe.bin");
|
|
2328
|
-
} catch {
|
|
2329
|
-
}
|
|
2330
|
-
}
|
|
2331
|
-
function Me() {
|
|
2332
|
-
if (!self.isSecureContext)
|
|
2333
|
-
throw m("UnsupportedPlatformError", "moyodb requires a secure context (HTTPS)");
|
|
2334
|
-
}
|
|
2335
|
-
function m(n, e) {
|
|
2336
|
-
return { code: n, name: n, message: e };
|
|
2337
|
-
}
|
|
2338
|
-
function q(n) {
|
|
2339
|
-
return !k(n) || !Array.isArray(n.partial) ? [] : n.partial.filter((e) => typeof e == "boolean");
|
|
2340
|
-
}
|
|
2341
|
-
function Ie(n) {
|
|
2342
|
-
return !k(n) || !Array.isArray(n.partial) ? [] : n.partial.filter((e) => !k(e) || typeof e.kind != "string" ? !1 : e.kind === "put" && typeof e.baselineExists == "boolean" || e.kind === "delete" && typeof e.deleted == "boolean");
|
|
2343
|
-
}
|
|
2344
|
-
function v(n) {
|
|
2345
|
-
if (k(n) && "code" in n && "message" in n)
|
|
2346
|
-
return n;
|
|
2347
|
-
const e = n;
|
|
2348
|
-
return m(e?.name ?? "Error", e?.message ?? String(n));
|
|
2349
|
-
}
|
|
2350
|
-
At(new Nn());
|
|
2351
|
-
//# sourceMappingURL=worker-CaOzbBz1.js.map
|