plateaudb 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +180 -2
- package/dist/events.d.ts +70 -0
- package/dist/facade.d.ts +322 -0
- package/dist/host.worker.js +2327 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +2766 -0
- package/dist/key.d.ts +63 -0
- package/dist/plateau_engine_bg.wasm +0 -0
- package/dist/protocol.d.ts +241 -0
- package/dist/runtime.d.ts +114 -0
- package/dist/scheduler.d.ts +15 -0
- package/dist/value.d.ts +7 -0
- package/package.json +55 -4
|
@@ -0,0 +1,2327 @@
|
|
|
1
|
+
// ../../crates/engine/pkg/plateau_engine.js
|
|
2
|
+
class Db {
|
|
3
|
+
static __wrap(ptr) {
|
|
4
|
+
const obj = Object.create(Db.prototype);
|
|
5
|
+
obj.__wbg_ptr = ptr;
|
|
6
|
+
DbFinalization.register(obj, obj.__wbg_ptr, obj);
|
|
7
|
+
return obj;
|
|
8
|
+
}
|
|
9
|
+
__destroy_into_raw() {
|
|
10
|
+
const ptr = this.__wbg_ptr;
|
|
11
|
+
this.__wbg_ptr = 0;
|
|
12
|
+
DbFinalization.unregister(this);
|
|
13
|
+
return ptr;
|
|
14
|
+
}
|
|
15
|
+
free() {
|
|
16
|
+
const ptr = this.__destroy_into_raw();
|
|
17
|
+
wasm.__wbg_db_free(ptr, 0);
|
|
18
|
+
}
|
|
19
|
+
beginBatch(rows) {
|
|
20
|
+
wasm.db_beginBatch(this.__wbg_ptr, rows);
|
|
21
|
+
}
|
|
22
|
+
commitBatch() {
|
|
23
|
+
const ret = wasm.db_commitBatch(this.__wbg_ptr);
|
|
24
|
+
if (ret[1]) {
|
|
25
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
compact() {
|
|
29
|
+
const ret = wasm.db_compact(this.__wbg_ptr);
|
|
30
|
+
if (ret[1]) {
|
|
31
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
static create(storage, schema_json) {
|
|
35
|
+
const ptr0 = passStringToWasm0(schema_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
36
|
+
const len0 = WASM_VECTOR_LEN;
|
|
37
|
+
const ret = wasm.db_create(storage, ptr0, len0);
|
|
38
|
+
if (ret[2]) {
|
|
39
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
40
|
+
}
|
|
41
|
+
return Db.__wrap(ret[0]);
|
|
42
|
+
}
|
|
43
|
+
delete(keys) {
|
|
44
|
+
const ptr0 = passArrayF64ToWasm0(keys, wasm.__wbindgen_malloc);
|
|
45
|
+
const len0 = WASM_VECTOR_LEN;
|
|
46
|
+
const ret = wasm.db_delete(this.__wbg_ptr, ptr0, len0);
|
|
47
|
+
if (ret[2]) {
|
|
48
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
49
|
+
}
|
|
50
|
+
return ret[0];
|
|
51
|
+
}
|
|
52
|
+
details() {
|
|
53
|
+
let deferred1_0;
|
|
54
|
+
let deferred1_1;
|
|
55
|
+
try {
|
|
56
|
+
const ret = wasm.db_details(this.__wbg_ptr);
|
|
57
|
+
deferred1_0 = ret[0];
|
|
58
|
+
deferred1_1 = ret[1];
|
|
59
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
60
|
+
} finally {
|
|
61
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
getMany(keys) {
|
|
65
|
+
let deferred3_0;
|
|
66
|
+
let deferred3_1;
|
|
67
|
+
try {
|
|
68
|
+
const ptr0 = passArrayF64ToWasm0(keys, wasm.__wbindgen_malloc);
|
|
69
|
+
const len0 = WASM_VECTOR_LEN;
|
|
70
|
+
const ret = wasm.db_getMany(this.__wbg_ptr, ptr0, len0);
|
|
71
|
+
var ptr2 = ret[0];
|
|
72
|
+
var len2 = ret[1];
|
|
73
|
+
if (ret[3]) {
|
|
74
|
+
ptr2 = 0;
|
|
75
|
+
len2 = 0;
|
|
76
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
77
|
+
}
|
|
78
|
+
deferred3_0 = ptr2;
|
|
79
|
+
deferred3_1 = len2;
|
|
80
|
+
return getStringFromWasm0(ptr2, len2);
|
|
81
|
+
} finally {
|
|
82
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
isEmpty() {
|
|
86
|
+
const ret = wasm.db_isEmpty(this.__wbg_ptr);
|
|
87
|
+
return ret !== 0;
|
|
88
|
+
}
|
|
89
|
+
keyCount(column, lo, hi) {
|
|
90
|
+
let ptr0, len0, ptr1, len1, ptr2, len2;
|
|
91
|
+
try {
|
|
92
|
+
ptr0 = passStringToWasm0(column, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
93
|
+
len0 = WASM_VECTOR_LEN;
|
|
94
|
+
ptr1 = passStringToWasm0(lo, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
95
|
+
len1 = WASM_VECTOR_LEN;
|
|
96
|
+
ptr2 = isLikeNone(hi) ? 0 : passStringToWasm0(hi, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
97
|
+
len2 = WASM_VECTOR_LEN;
|
|
98
|
+
} catch (e) {
|
|
99
|
+
if (ptr2) {
|
|
100
|
+
wasm.__wbindgen_free(ptr2, len2 * 1, 1);
|
|
101
|
+
}
|
|
102
|
+
if (ptr1) {
|
|
103
|
+
wasm.__wbindgen_free(ptr1, len1 * 1, 1);
|
|
104
|
+
}
|
|
105
|
+
if (ptr0) {
|
|
106
|
+
wasm.__wbindgen_free(ptr0, len0 * 1, 1);
|
|
107
|
+
}
|
|
108
|
+
throw e;
|
|
109
|
+
}
|
|
110
|
+
const ret = wasm.db_keyCount(this.__wbg_ptr, ptr0, len0, ptr1, len1, ptr2, len2);
|
|
111
|
+
if (ret[2]) {
|
|
112
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
113
|
+
}
|
|
114
|
+
return ret[0];
|
|
115
|
+
}
|
|
116
|
+
keyScan(column, lo, hi, desc, limit) {
|
|
117
|
+
let deferred7_0;
|
|
118
|
+
let deferred7_1;
|
|
119
|
+
try {
|
|
120
|
+
const val4 = +desc;
|
|
121
|
+
const val5 = +limit;
|
|
122
|
+
let ptr0, len0, ptr1, len1, ptr2, len2;
|
|
123
|
+
try {
|
|
124
|
+
ptr0 = passStringToWasm0(column, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
125
|
+
len0 = WASM_VECTOR_LEN;
|
|
126
|
+
ptr1 = passStringToWasm0(lo, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
127
|
+
len1 = WASM_VECTOR_LEN;
|
|
128
|
+
ptr2 = isLikeNone(hi) ? 0 : passStringToWasm0(hi, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
129
|
+
len2 = WASM_VECTOR_LEN;
|
|
130
|
+
} catch (e) {
|
|
131
|
+
if (ptr2) {
|
|
132
|
+
wasm.__wbindgen_free(ptr2, len2 * 1, 1);
|
|
133
|
+
}
|
|
134
|
+
if (ptr1) {
|
|
135
|
+
wasm.__wbindgen_free(ptr1, len1 * 1, 1);
|
|
136
|
+
}
|
|
137
|
+
if (ptr0) {
|
|
138
|
+
wasm.__wbindgen_free(ptr0, len0 * 1, 1);
|
|
139
|
+
}
|
|
140
|
+
throw e;
|
|
141
|
+
}
|
|
142
|
+
const ret = wasm.db_keyScan(this.__wbg_ptr, ptr0, len0, ptr1, len1, ptr2, len2, val4, val5);
|
|
143
|
+
var ptr6 = ret[0];
|
|
144
|
+
var len6 = ret[1];
|
|
145
|
+
if (ret[3]) {
|
|
146
|
+
ptr6 = 0;
|
|
147
|
+
len6 = 0;
|
|
148
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
149
|
+
}
|
|
150
|
+
deferred7_0 = ptr6;
|
|
151
|
+
deferred7_1 = len6;
|
|
152
|
+
return getStringFromWasm0(ptr6, len6);
|
|
153
|
+
} finally {
|
|
154
|
+
wasm.__wbindgen_free(deferred7_0, deferred7_1, 1);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
len() {
|
|
158
|
+
const ret = wasm.db_len(this.__wbg_ptr);
|
|
159
|
+
return ret;
|
|
160
|
+
}
|
|
161
|
+
meta() {
|
|
162
|
+
let deferred1_0;
|
|
163
|
+
let deferred1_1;
|
|
164
|
+
try {
|
|
165
|
+
const ret = wasm.db_meta(this.__wbg_ptr);
|
|
166
|
+
deferred1_0 = ret[0];
|
|
167
|
+
deferred1_1 = ret[1];
|
|
168
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
169
|
+
} finally {
|
|
170
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
static openAt(storage, max_seq) {
|
|
174
|
+
const ret = wasm.db_openAt(storage, max_seq);
|
|
175
|
+
if (ret[2]) {
|
|
176
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
177
|
+
}
|
|
178
|
+
return Db.__wrap(ret[0]);
|
|
179
|
+
}
|
|
180
|
+
static open(storage) {
|
|
181
|
+
const ret = wasm.db_open(storage);
|
|
182
|
+
if (ret[2]) {
|
|
183
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
184
|
+
}
|
|
185
|
+
return Db.__wrap(ret[0]);
|
|
186
|
+
}
|
|
187
|
+
pkScan(lo, lo_open, hi, hi_open, desc, limit) {
|
|
188
|
+
const ret = wasm.db_pkScan(this.__wbg_ptr, lo, lo_open, hi, hi_open, desc, limit);
|
|
189
|
+
if (ret[3]) {
|
|
190
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
191
|
+
}
|
|
192
|
+
var v1 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
|
|
193
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
|
|
194
|
+
return v1;
|
|
195
|
+
}
|
|
196
|
+
putBatch() {
|
|
197
|
+
const ret = wasm.db_putBatch(this.__wbg_ptr);
|
|
198
|
+
if (ret[1]) {
|
|
199
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
query(query_json) {
|
|
203
|
+
let deferred3_0;
|
|
204
|
+
let deferred3_1;
|
|
205
|
+
try {
|
|
206
|
+
const ptr0 = passStringToWasm0(query_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
207
|
+
const len0 = WASM_VECTOR_LEN;
|
|
208
|
+
const ret = wasm.db_query(this.__wbg_ptr, ptr0, len0);
|
|
209
|
+
var ptr2 = ret[0];
|
|
210
|
+
var len2 = ret[1];
|
|
211
|
+
if (ret[3]) {
|
|
212
|
+
ptr2 = 0;
|
|
213
|
+
len2 = 0;
|
|
214
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
215
|
+
}
|
|
216
|
+
deferred3_0 = ptr2;
|
|
217
|
+
deferred3_1 = len2;
|
|
218
|
+
return getStringFromWasm0(ptr2, len2);
|
|
219
|
+
} finally {
|
|
220
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
releasePrevious() {
|
|
224
|
+
const ret = wasm.db_releasePrevious(this.__wbg_ptr);
|
|
225
|
+
if (ret[1]) {
|
|
226
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
rollback() {
|
|
230
|
+
wasm.db_rollback(this.__wbg_ptr);
|
|
231
|
+
}
|
|
232
|
+
save() {
|
|
233
|
+
const ret = wasm.db_save(this.__wbg_ptr);
|
|
234
|
+
if (ret[1]) {
|
|
235
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
seq() {
|
|
239
|
+
const ret = wasm.db_seq(this.__wbg_ptr);
|
|
240
|
+
return ret;
|
|
241
|
+
}
|
|
242
|
+
setF64(column, values) {
|
|
243
|
+
let ptr0, len0, ptr1, len1;
|
|
244
|
+
try {
|
|
245
|
+
ptr0 = passStringToWasm0(column, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
246
|
+
len0 = WASM_VECTOR_LEN;
|
|
247
|
+
ptr1 = passArrayF64ToWasm0(values, wasm.__wbindgen_malloc);
|
|
248
|
+
len1 = WASM_VECTOR_LEN;
|
|
249
|
+
} catch (e) {
|
|
250
|
+
if (ptr1) {
|
|
251
|
+
wasm.__wbindgen_free(ptr1, len1 * 8, 8);
|
|
252
|
+
}
|
|
253
|
+
if (ptr0) {
|
|
254
|
+
wasm.__wbindgen_free(ptr0, len0 * 1, 1);
|
|
255
|
+
}
|
|
256
|
+
throw e;
|
|
257
|
+
}
|
|
258
|
+
const ret = wasm.db_setF64(this.__wbg_ptr, ptr0, len0, ptr1, len1);
|
|
259
|
+
if (ret[1]) {
|
|
260
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
setKeepPrevious(keep) {
|
|
264
|
+
wasm.db_setKeepPrevious(this.__wbg_ptr, keep);
|
|
265
|
+
}
|
|
266
|
+
setMeta(meta) {
|
|
267
|
+
const ptr0 = passStringToWasm0(meta, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
268
|
+
const len0 = WASM_VECTOR_LEN;
|
|
269
|
+
wasm.db_setMeta(this.__wbg_ptr, ptr0, len0);
|
|
270
|
+
}
|
|
271
|
+
setMulti(column, offsets, values) {
|
|
272
|
+
let ptr0, len0, ptr1, len1, ptr2, len2;
|
|
273
|
+
try {
|
|
274
|
+
ptr0 = passStringToWasm0(column, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
275
|
+
len0 = WASM_VECTOR_LEN;
|
|
276
|
+
ptr1 = passArray32ToWasm0(offsets, wasm.__wbindgen_malloc);
|
|
277
|
+
len1 = WASM_VECTOR_LEN;
|
|
278
|
+
ptr2 = passArray32ToWasm0(values, wasm.__wbindgen_malloc);
|
|
279
|
+
len2 = WASM_VECTOR_LEN;
|
|
280
|
+
} catch (e) {
|
|
281
|
+
if (ptr2) {
|
|
282
|
+
wasm.__wbindgen_free(ptr2, len2 * 4, 4);
|
|
283
|
+
}
|
|
284
|
+
if (ptr1) {
|
|
285
|
+
wasm.__wbindgen_free(ptr1, len1 * 4, 4);
|
|
286
|
+
}
|
|
287
|
+
if (ptr0) {
|
|
288
|
+
wasm.__wbindgen_free(ptr0, len0 * 1, 1);
|
|
289
|
+
}
|
|
290
|
+
throw e;
|
|
291
|
+
}
|
|
292
|
+
const ret = wasm.db_setMulti(this.__wbg_ptr, ptr0, len0, ptr1, len1, ptr2, len2);
|
|
293
|
+
if (ret[1]) {
|
|
294
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
setStr(column, offsets, bytes) {
|
|
298
|
+
let ptr0, len0, ptr1, len1, ptr2, len2;
|
|
299
|
+
try {
|
|
300
|
+
ptr0 = passStringToWasm0(column, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
301
|
+
len0 = WASM_VECTOR_LEN;
|
|
302
|
+
ptr1 = passArray32ToWasm0(offsets, wasm.__wbindgen_malloc);
|
|
303
|
+
len1 = WASM_VECTOR_LEN;
|
|
304
|
+
ptr2 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
|
|
305
|
+
len2 = WASM_VECTOR_LEN;
|
|
306
|
+
} catch (e) {
|
|
307
|
+
if (ptr2) {
|
|
308
|
+
wasm.__wbindgen_free(ptr2, len2 * 1, 1);
|
|
309
|
+
}
|
|
310
|
+
if (ptr1) {
|
|
311
|
+
wasm.__wbindgen_free(ptr1, len1 * 4, 4);
|
|
312
|
+
}
|
|
313
|
+
if (ptr0) {
|
|
314
|
+
wasm.__wbindgen_free(ptr0, len0 * 1, 1);
|
|
315
|
+
}
|
|
316
|
+
throw e;
|
|
317
|
+
}
|
|
318
|
+
const ret = wasm.db_setStr(this.__wbg_ptr, ptr0, len0, ptr1, len1, ptr2, len2);
|
|
319
|
+
if (ret[1]) {
|
|
320
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
setU32(column, values) {
|
|
324
|
+
let ptr0, len0, ptr1, len1;
|
|
325
|
+
try {
|
|
326
|
+
ptr0 = passStringToWasm0(column, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
327
|
+
len0 = WASM_VECTOR_LEN;
|
|
328
|
+
ptr1 = passArray32ToWasm0(values, wasm.__wbindgen_malloc);
|
|
329
|
+
len1 = WASM_VECTOR_LEN;
|
|
330
|
+
} catch (e) {
|
|
331
|
+
if (ptr1) {
|
|
332
|
+
wasm.__wbindgen_free(ptr1, len1 * 4, 4);
|
|
333
|
+
}
|
|
334
|
+
if (ptr0) {
|
|
335
|
+
wasm.__wbindgen_free(ptr0, len0 * 1, 1);
|
|
336
|
+
}
|
|
337
|
+
throw e;
|
|
338
|
+
}
|
|
339
|
+
const ret = wasm.db_setU32(this.__wbg_ptr, ptr0, len0, ptr1, len1);
|
|
340
|
+
if (ret[1]) {
|
|
341
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
usedBytes() {
|
|
345
|
+
const ret = wasm.db_usedBytes(this.__wbg_ptr);
|
|
346
|
+
return ret;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
if (Symbol.dispose)
|
|
350
|
+
Db.prototype[Symbol.dispose] = Db.prototype.free;
|
|
351
|
+
function cachedBytes() {
|
|
352
|
+
const ret = wasm.cachedBytes();
|
|
353
|
+
return ret;
|
|
354
|
+
}
|
|
355
|
+
function memoryBudget() {
|
|
356
|
+
const ret = wasm.memoryBudget();
|
|
357
|
+
return ret;
|
|
358
|
+
}
|
|
359
|
+
function setMemoryBudget(bytes) {
|
|
360
|
+
wasm.setMemoryBudget(bytes);
|
|
361
|
+
}
|
|
362
|
+
function __wbg_get_imports() {
|
|
363
|
+
const import0 = {
|
|
364
|
+
__proto__: null,
|
|
365
|
+
__wbg_Error_208446807d0d1e30: function(arg0, arg1) {
|
|
366
|
+
const ret = Error(getStringFromWasm0(arg0, arg1));
|
|
367
|
+
return ret;
|
|
368
|
+
},
|
|
369
|
+
__wbg___wbindgen_debug_string_f249a2a76caa9a55: function(arg0, arg1) {
|
|
370
|
+
const ret = debugString(arg1);
|
|
371
|
+
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
372
|
+
const len1 = WASM_VECTOR_LEN;
|
|
373
|
+
getDataViewMemory0().setInt32((arg0 >>> 0) + 4 * 1, len1, true);
|
|
374
|
+
getDataViewMemory0().setInt32((arg0 >>> 0) + 4 * 0, ptr1, true);
|
|
375
|
+
},
|
|
376
|
+
__wbg___wbindgen_string_get_7ce8eee7fd327545: function(arg0, arg1) {
|
|
377
|
+
const obj = arg1;
|
|
378
|
+
const ret = typeof obj === "string" ? obj : undefined;
|
|
379
|
+
var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
380
|
+
var len1 = WASM_VECTOR_LEN;
|
|
381
|
+
getDataViewMemory0().setInt32((arg0 >>> 0) + 4 * 1, len1, true);
|
|
382
|
+
getDataViewMemory0().setInt32((arg0 >>> 0) + 4 * 0, ptr1, true);
|
|
383
|
+
},
|
|
384
|
+
__wbg___wbindgen_throw_4c88d8bf264fd984: function(arg0, arg1) {
|
|
385
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
386
|
+
},
|
|
387
|
+
__wbg_flush_5d2593f23069d033: function() {
|
|
388
|
+
return handleError(function(arg0) {
|
|
389
|
+
arg0.flush();
|
|
390
|
+
}, arguments);
|
|
391
|
+
},
|
|
392
|
+
__wbg_read_6802dc6603dd9bc9: function() {
|
|
393
|
+
return handleError(function(arg0, arg1, arg2, arg3) {
|
|
394
|
+
const ret = arg0.read(arg1 >>> 0, arg2 >>> 0, arg3);
|
|
395
|
+
return ret;
|
|
396
|
+
}, arguments);
|
|
397
|
+
},
|
|
398
|
+
__wbg_size_e861f5064852a940: function() {
|
|
399
|
+
return handleError(function(arg0) {
|
|
400
|
+
const ret = arg0.size();
|
|
401
|
+
return ret;
|
|
402
|
+
}, arguments);
|
|
403
|
+
},
|
|
404
|
+
__wbg_truncate_e5c329ab52374576: function() {
|
|
405
|
+
return handleError(function(arg0, arg1) {
|
|
406
|
+
arg0.truncate(arg1);
|
|
407
|
+
}, arguments);
|
|
408
|
+
},
|
|
409
|
+
__wbg_write_e1c6df03851aaf79: function() {
|
|
410
|
+
return handleError(function(arg0, arg1, arg2, arg3) {
|
|
411
|
+
const ret = arg0.write(arg1 >>> 0, arg2 >>> 0, arg3);
|
|
412
|
+
return ret;
|
|
413
|
+
}, arguments);
|
|
414
|
+
},
|
|
415
|
+
__wbindgen_init_externref_table: function() {
|
|
416
|
+
const table = wasm.__wbindgen_externrefs;
|
|
417
|
+
table.set(0, undefined);
|
|
418
|
+
table.set(1024, undefined);
|
|
419
|
+
table.set(1025, null);
|
|
420
|
+
table.set(1026, true);
|
|
421
|
+
table.set(1027, false);
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
return {
|
|
425
|
+
__proto__: null,
|
|
426
|
+
"./plateau_engine_bg.js": import0
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
var DbFinalization = typeof FinalizationRegistry === "undefined" ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry((ptr) => wasm.__wbg_db_free(ptr, 1));
|
|
430
|
+
function addToExternrefTable0(obj) {
|
|
431
|
+
const idx = wasm.__externref_table_alloc();
|
|
432
|
+
wasm.__wbindgen_externrefs.set(idx, obj);
|
|
433
|
+
return idx;
|
|
434
|
+
}
|
|
435
|
+
function debugString(val) {
|
|
436
|
+
switch (typeof val) {
|
|
437
|
+
case "number":
|
|
438
|
+
case "boolean":
|
|
439
|
+
case "undefined":
|
|
440
|
+
return `${val}`;
|
|
441
|
+
case "string":
|
|
442
|
+
return JSON.stringify(val);
|
|
443
|
+
case "bigint":
|
|
444
|
+
return `${val}n`;
|
|
445
|
+
case "symbol":
|
|
446
|
+
return val.description !== undefined ? `Symbol(${val.description})` : "Symbol";
|
|
447
|
+
case "function":
|
|
448
|
+
return typeof val.name === "string" && val.name !== "" ? `Function(${val.name})` : "Function";
|
|
449
|
+
case "object": {
|
|
450
|
+
if (val === null) {
|
|
451
|
+
return "null";
|
|
452
|
+
}
|
|
453
|
+
if (Array.isArray(val)) {
|
|
454
|
+
return "[" + Array.from(val, debugString).join(", ") + "]";
|
|
455
|
+
}
|
|
456
|
+
if (val instanceof Error) {
|
|
457
|
+
return `${val.name}: ${val.message}
|
|
458
|
+
${val.stack}`;
|
|
459
|
+
}
|
|
460
|
+
const toStringOutput = toString.call(val);
|
|
461
|
+
const className = /\[object ([^\]]+)\]/.exec(toStringOutput)?.[1];
|
|
462
|
+
if (className === undefined) {
|
|
463
|
+
return toStringOutput;
|
|
464
|
+
}
|
|
465
|
+
if (className === "Object") {
|
|
466
|
+
try {
|
|
467
|
+
return "Object(" + JSON.stringify(val) + ")";
|
|
468
|
+
} catch (_) {
|
|
469
|
+
return "Object";
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return className;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
function getArrayF64FromWasm0(ptr, len) {
|
|
477
|
+
ptr = ptr >>> 0;
|
|
478
|
+
return getFloat64ArrayMemory0().subarray(ptr / 8, ptr / 8 + len);
|
|
479
|
+
}
|
|
480
|
+
var cachedDataViewMemory0 = null;
|
|
481
|
+
function getDataViewMemory0() {
|
|
482
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer) {
|
|
483
|
+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
484
|
+
}
|
|
485
|
+
return cachedDataViewMemory0;
|
|
486
|
+
}
|
|
487
|
+
var cachedFloat64ArrayMemory0 = null;
|
|
488
|
+
function getFloat64ArrayMemory0() {
|
|
489
|
+
if (cachedFloat64ArrayMemory0 === null || cachedFloat64ArrayMemory0.byteLength === 0) {
|
|
490
|
+
cachedFloat64ArrayMemory0 = new Float64Array(wasm.memory.buffer);
|
|
491
|
+
}
|
|
492
|
+
return cachedFloat64ArrayMemory0;
|
|
493
|
+
}
|
|
494
|
+
function getStringFromWasm0(ptr, len) {
|
|
495
|
+
ptr = ptr >>> 0;
|
|
496
|
+
return decodeText(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
497
|
+
}
|
|
498
|
+
var cachedUint32ArrayMemory0 = null;
|
|
499
|
+
function getUint32ArrayMemory0() {
|
|
500
|
+
if (cachedUint32ArrayMemory0 === null || cachedUint32ArrayMemory0.byteLength === 0) {
|
|
501
|
+
cachedUint32ArrayMemory0 = new Uint32Array(wasm.memory.buffer);
|
|
502
|
+
}
|
|
503
|
+
return cachedUint32ArrayMemory0;
|
|
504
|
+
}
|
|
505
|
+
var cachedUint8ArrayMemory0 = null;
|
|
506
|
+
function getUint8ArrayMemory0() {
|
|
507
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
508
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
509
|
+
}
|
|
510
|
+
return cachedUint8ArrayMemory0;
|
|
511
|
+
}
|
|
512
|
+
function handleError(f, args) {
|
|
513
|
+
try {
|
|
514
|
+
return f.apply(this, args);
|
|
515
|
+
} catch (e) {
|
|
516
|
+
const idx = addToExternrefTable0(e);
|
|
517
|
+
wasm.__wbindgen_exn_store(idx);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
function isLikeNone(x) {
|
|
521
|
+
return x === undefined || x === null;
|
|
522
|
+
}
|
|
523
|
+
function passArray32ToWasm0(arg, malloc) {
|
|
524
|
+
const ptr = malloc(arg.length * 4, 4) >>> 0;
|
|
525
|
+
getUint32ArrayMemory0().set(arg, ptr / 4);
|
|
526
|
+
WASM_VECTOR_LEN = arg.length;
|
|
527
|
+
return ptr;
|
|
528
|
+
}
|
|
529
|
+
function passArray8ToWasm0(arg, malloc) {
|
|
530
|
+
const ptr = malloc(arg.length * 1, 1) >>> 0;
|
|
531
|
+
getUint8ArrayMemory0().set(arg, ptr / 1);
|
|
532
|
+
WASM_VECTOR_LEN = arg.length;
|
|
533
|
+
return ptr;
|
|
534
|
+
}
|
|
535
|
+
function passArrayF64ToWasm0(arg, malloc) {
|
|
536
|
+
const ptr = malloc(arg.length * 8, 8) >>> 0;
|
|
537
|
+
getFloat64ArrayMemory0().set(arg, ptr / 8);
|
|
538
|
+
WASM_VECTOR_LEN = arg.length;
|
|
539
|
+
return ptr;
|
|
540
|
+
}
|
|
541
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
542
|
+
if (realloc === undefined) {
|
|
543
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
544
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
545
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
546
|
+
WASM_VECTOR_LEN = buf.length;
|
|
547
|
+
return ptr;
|
|
548
|
+
}
|
|
549
|
+
let len = arg.length;
|
|
550
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
551
|
+
const mem = getUint8ArrayMemory0();
|
|
552
|
+
let offset = 0;
|
|
553
|
+
for (;offset < len; offset++) {
|
|
554
|
+
const code = arg.charCodeAt(offset);
|
|
555
|
+
if (code > 127)
|
|
556
|
+
break;
|
|
557
|
+
mem[ptr + offset] = code;
|
|
558
|
+
}
|
|
559
|
+
if (offset !== len) {
|
|
560
|
+
if (offset !== 0) {
|
|
561
|
+
arg = arg.slice(offset);
|
|
562
|
+
}
|
|
563
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
564
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
565
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
566
|
+
offset += ret.written;
|
|
567
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
568
|
+
}
|
|
569
|
+
WASM_VECTOR_LEN = offset;
|
|
570
|
+
return ptr;
|
|
571
|
+
}
|
|
572
|
+
function takeFromExternrefTable0(idx) {
|
|
573
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
574
|
+
wasm.__externref_table_dealloc(idx);
|
|
575
|
+
return value;
|
|
576
|
+
}
|
|
577
|
+
var cachedTextDecoder = new TextDecoder("utf-8", { ignoreBOM: true });
|
|
578
|
+
cachedTextDecoder.decode();
|
|
579
|
+
var MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
580
|
+
var numBytesDecoded = 0;
|
|
581
|
+
function decodeText(bytes) {
|
|
582
|
+
numBytesDecoded += bytes.length;
|
|
583
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
584
|
+
cachedTextDecoder = new TextDecoder("utf-8", { ignoreBOM: true });
|
|
585
|
+
cachedTextDecoder.decode();
|
|
586
|
+
numBytesDecoded = bytes.length;
|
|
587
|
+
}
|
|
588
|
+
return cachedTextDecoder.decode(bytes);
|
|
589
|
+
}
|
|
590
|
+
var cachedTextEncoder = new TextEncoder;
|
|
591
|
+
if (!("encodeInto" in cachedTextEncoder)) {
|
|
592
|
+
cachedTextEncoder.encodeInto = function(arg, view) {
|
|
593
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
594
|
+
view.set(buf);
|
|
595
|
+
return {
|
|
596
|
+
read: arg.length,
|
|
597
|
+
written: buf.length
|
|
598
|
+
};
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
var WASM_VECTOR_LEN = 0;
|
|
602
|
+
var wasmModule;
|
|
603
|
+
var wasmInstance;
|
|
604
|
+
var wasm;
|
|
605
|
+
function __wbg_finalize_init(instance, module) {
|
|
606
|
+
wasmInstance = instance;
|
|
607
|
+
wasm = instance.exports;
|
|
608
|
+
wasmModule = module;
|
|
609
|
+
cachedDataViewMemory0 = null;
|
|
610
|
+
cachedFloat64ArrayMemory0 = null;
|
|
611
|
+
cachedUint32ArrayMemory0 = null;
|
|
612
|
+
cachedUint8ArrayMemory0 = null;
|
|
613
|
+
wasm.__wbindgen_start();
|
|
614
|
+
return wasm;
|
|
615
|
+
}
|
|
616
|
+
function initSync(module) {
|
|
617
|
+
if (wasm !== undefined)
|
|
618
|
+
return wasm;
|
|
619
|
+
if (module !== undefined) {
|
|
620
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
621
|
+
({ module } = module);
|
|
622
|
+
} else {
|
|
623
|
+
console.warn("using deprecated parameters for `initSync()`; pass a single object instead");
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
const imports = __wbg_get_imports();
|
|
627
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
628
|
+
module = new WebAssembly.Module(module);
|
|
629
|
+
}
|
|
630
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
631
|
+
return __wbg_finalize_init(instance, module);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
// src/key.ts
|
|
635
|
+
var NUMBER = 16;
|
|
636
|
+
var DATE = 32;
|
|
637
|
+
var STRING = 48;
|
|
638
|
+
var BINARY = 64;
|
|
639
|
+
var ARRAY = 80;
|
|
640
|
+
var f64 = new Float64Array(1);
|
|
641
|
+
var u32 = new Uint32Array(f64.buffer);
|
|
642
|
+
var LITTLE = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
643
|
+
var HI = LITTLE ? 1 : 0;
|
|
644
|
+
var LO = LITTLE ? 0 : 1;
|
|
645
|
+
function encodeNumber(tag, n) {
|
|
646
|
+
f64[0] = n === 0 ? 0 : n;
|
|
647
|
+
let hi = u32[HI];
|
|
648
|
+
let lo = u32[LO];
|
|
649
|
+
if (hi >>> 31) {
|
|
650
|
+
hi = ~hi >>> 0;
|
|
651
|
+
lo = ~lo >>> 0;
|
|
652
|
+
} else {
|
|
653
|
+
hi = (hi | 2147483648) >>> 0;
|
|
654
|
+
}
|
|
655
|
+
return String.fromCharCode(tag, hi >>> 16, hi & 65535, lo >>> 16, lo & 65535);
|
|
656
|
+
}
|
|
657
|
+
function decodeNumber(s, at) {
|
|
658
|
+
let hi = (s.charCodeAt(at) << 16 | s.charCodeAt(at + 1)) >>> 0;
|
|
659
|
+
let lo = (s.charCodeAt(at + 2) << 16 | s.charCodeAt(at + 3)) >>> 0;
|
|
660
|
+
if (hi >>> 31) {
|
|
661
|
+
hi = (hi & 2147483647) >>> 0;
|
|
662
|
+
} else {
|
|
663
|
+
hi = ~hi >>> 0;
|
|
664
|
+
lo = ~lo >>> 0;
|
|
665
|
+
}
|
|
666
|
+
u32[HI] = hi;
|
|
667
|
+
u32[LO] = lo;
|
|
668
|
+
return f64[0];
|
|
669
|
+
}
|
|
670
|
+
function isBinary(v) {
|
|
671
|
+
return v instanceof ArrayBuffer || ArrayBuffer.isView(v) || Object.prototype.toString.call(v) === "[object ArrayBuffer]";
|
|
672
|
+
}
|
|
673
|
+
function bytesOf(v) {
|
|
674
|
+
return ArrayBuffer.isView(v) ? new Uint8Array(v.buffer, v.byteOffset, v.byteLength) : new Uint8Array(v);
|
|
675
|
+
}
|
|
676
|
+
function isDate(v) {
|
|
677
|
+
return v instanceof Date || Object.prototype.toString.call(v) === "[object Date]";
|
|
678
|
+
}
|
|
679
|
+
function encodeKey(v) {
|
|
680
|
+
return enc(v, null);
|
|
681
|
+
}
|
|
682
|
+
function enc(v, seen) {
|
|
683
|
+
switch (typeof v) {
|
|
684
|
+
case "number":
|
|
685
|
+
return Number.isNaN(v) ? undefined : encodeNumber(NUMBER, v);
|
|
686
|
+
case "string": {
|
|
687
|
+
let out = String.fromCharCode(STRING);
|
|
688
|
+
let from = 0;
|
|
689
|
+
for (let i = 0;i < v.length; i++) {
|
|
690
|
+
const c = v.charCodeAt(i);
|
|
691
|
+
if (c < 2) {
|
|
692
|
+
out += v.slice(from, i) + String.fromCharCode(1, c + 1);
|
|
693
|
+
from = i + 1;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
return out + v.slice(from) + "\x00";
|
|
697
|
+
}
|
|
698
|
+
case "object": {
|
|
699
|
+
if (v === null)
|
|
700
|
+
return;
|
|
701
|
+
if (isDate(v)) {
|
|
702
|
+
const t = v.getTime();
|
|
703
|
+
return Number.isNaN(t) ? undefined : encodeNumber(DATE, t);
|
|
704
|
+
}
|
|
705
|
+
if (isBinary(v)) {
|
|
706
|
+
const b = bytesOf(v);
|
|
707
|
+
const units = new Array(b.length);
|
|
708
|
+
for (let i = 0;i < b.length; i++)
|
|
709
|
+
units[i] = b[i] + 2;
|
|
710
|
+
let out = String.fromCharCode(BINARY);
|
|
711
|
+
for (let i = 0;i < units.length; i += 8192)
|
|
712
|
+
out += String.fromCharCode(...units.slice(i, i + 8192));
|
|
713
|
+
return out + "\x00";
|
|
714
|
+
}
|
|
715
|
+
if (Array.isArray(v)) {
|
|
716
|
+
const stack = seen ?? [];
|
|
717
|
+
if (stack.includes(v))
|
|
718
|
+
return;
|
|
719
|
+
stack.push(v);
|
|
720
|
+
let out = String.fromCharCode(ARRAY);
|
|
721
|
+
for (let i = 0;i < v.length; i++) {
|
|
722
|
+
if (!Object.prototype.hasOwnProperty.call(v, i))
|
|
723
|
+
return;
|
|
724
|
+
const e = enc(v[i], stack);
|
|
725
|
+
if (e === undefined)
|
|
726
|
+
return;
|
|
727
|
+
out += e;
|
|
728
|
+
}
|
|
729
|
+
stack.pop();
|
|
730
|
+
return out + "\x00";
|
|
731
|
+
}
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
default:
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
function skipKey(s, at) {
|
|
739
|
+
const tag = s.charCodeAt(at);
|
|
740
|
+
switch (tag) {
|
|
741
|
+
case NUMBER:
|
|
742
|
+
case DATE:
|
|
743
|
+
return at + 5;
|
|
744
|
+
case STRING:
|
|
745
|
+
case BINARY: {
|
|
746
|
+
let i = at + 1;
|
|
747
|
+
for (;; ) {
|
|
748
|
+
const c = s.charCodeAt(i);
|
|
749
|
+
if (c === 0)
|
|
750
|
+
return i + 1;
|
|
751
|
+
i += tag === STRING && c === 1 ? 2 : 1;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
case ARRAY: {
|
|
755
|
+
let i = at + 1;
|
|
756
|
+
while (s.charCodeAt(i) !== 0)
|
|
757
|
+
i = skipKey(s, i);
|
|
758
|
+
return i + 1;
|
|
759
|
+
}
|
|
760
|
+
default:
|
|
761
|
+
throw new Error(`plateau: corrupt key encoding (tag ${tag})`);
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
function decodeKeyAt(s, at) {
|
|
765
|
+
const tag = s.charCodeAt(at);
|
|
766
|
+
switch (tag) {
|
|
767
|
+
case NUMBER:
|
|
768
|
+
return [decodeNumber(s, at + 1), at + 5];
|
|
769
|
+
case DATE:
|
|
770
|
+
return [new Date(decodeNumber(s, at + 1)), at + 5];
|
|
771
|
+
case STRING: {
|
|
772
|
+
let out = "";
|
|
773
|
+
let i = at + 1;
|
|
774
|
+
let from = i;
|
|
775
|
+
for (;; ) {
|
|
776
|
+
const c = s.charCodeAt(i);
|
|
777
|
+
if (c === 0)
|
|
778
|
+
return [out + s.slice(from, i), i + 1];
|
|
779
|
+
if (c === 1) {
|
|
780
|
+
out += s.slice(from, i) + String.fromCharCode(s.charCodeAt(i + 1) - 1);
|
|
781
|
+
i += 2;
|
|
782
|
+
from = i;
|
|
783
|
+
} else
|
|
784
|
+
i++;
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
case BINARY: {
|
|
788
|
+
const end = s.indexOf("\x00", at + 1);
|
|
789
|
+
const b = new Uint8Array(end - at - 1);
|
|
790
|
+
for (let i = 0;i < b.length; i++)
|
|
791
|
+
b[i] = s.charCodeAt(at + 1 + i) - 2;
|
|
792
|
+
return [b.buffer, end + 1];
|
|
793
|
+
}
|
|
794
|
+
case ARRAY: {
|
|
795
|
+
const out = [];
|
|
796
|
+
let i = at + 1;
|
|
797
|
+
while (s.charCodeAt(i) !== 0) {
|
|
798
|
+
const [k, next] = decodeKeyAt(s, i);
|
|
799
|
+
out.push(k);
|
|
800
|
+
i = next;
|
|
801
|
+
}
|
|
802
|
+
return [out, i + 1];
|
|
803
|
+
}
|
|
804
|
+
default:
|
|
805
|
+
throw new Error(`plateau: corrupt key encoding (tag ${tag})`);
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
function decodeKey(s) {
|
|
809
|
+
return decodeKeyAt(s, 0)[0];
|
|
810
|
+
}
|
|
811
|
+
function encodedNumber(s) {
|
|
812
|
+
return s.charCodeAt(0) === NUMBER && s.length === 5 ? decodeNumber(s, 1) : undefined;
|
|
813
|
+
}
|
|
814
|
+
function encodeNumberKey(n) {
|
|
815
|
+
return encodeNumber(NUMBER, n);
|
|
816
|
+
}
|
|
817
|
+
var FAIL = Symbol("no value at key path");
|
|
818
|
+
function evalPath(value, path) {
|
|
819
|
+
if (path === "")
|
|
820
|
+
return value;
|
|
821
|
+
let v = value;
|
|
822
|
+
for (const id of path.split(".")) {
|
|
823
|
+
if (typeof v === "string" && id === "length") {
|
|
824
|
+
v = v.length;
|
|
825
|
+
continue;
|
|
826
|
+
}
|
|
827
|
+
if (v === null || typeof v !== "object")
|
|
828
|
+
return FAIL;
|
|
829
|
+
if (Array.isArray(v) && id === "length") {
|
|
830
|
+
v = v.length;
|
|
831
|
+
continue;
|
|
832
|
+
}
|
|
833
|
+
if (!Object.prototype.hasOwnProperty.call(v, id))
|
|
834
|
+
return FAIL;
|
|
835
|
+
v = v[id];
|
|
836
|
+
}
|
|
837
|
+
return v;
|
|
838
|
+
}
|
|
839
|
+
function extractKey(value, path) {
|
|
840
|
+
if (Array.isArray(path)) {
|
|
841
|
+
const parts = [];
|
|
842
|
+
for (const p of path) {
|
|
843
|
+
const v = evalPath(value, p);
|
|
844
|
+
if (v === FAIL)
|
|
845
|
+
return "invalid";
|
|
846
|
+
parts.push(v);
|
|
847
|
+
}
|
|
848
|
+
const key = encodeKey(parts);
|
|
849
|
+
return key === undefined ? "invalid" : { key };
|
|
850
|
+
}
|
|
851
|
+
const v = evalPath(value, path);
|
|
852
|
+
if (v === FAIL)
|
|
853
|
+
return "missing";
|
|
854
|
+
const key = encodeKey(v);
|
|
855
|
+
return key === undefined ? "invalid" : { key };
|
|
856
|
+
}
|
|
857
|
+
function indexKeys(value, path, multiEntry) {
|
|
858
|
+
if (multiEntry && typeof path === "string") {
|
|
859
|
+
const v = evalPath(value, path);
|
|
860
|
+
if (v === FAIL)
|
|
861
|
+
return [];
|
|
862
|
+
if (Array.isArray(v)) {
|
|
863
|
+
const out = new Set;
|
|
864
|
+
for (let i = 0;i < v.length; i++) {
|
|
865
|
+
if (!Object.prototype.hasOwnProperty.call(v, i))
|
|
866
|
+
continue;
|
|
867
|
+
const k = encodeKey(v[i]);
|
|
868
|
+
if (k !== undefined)
|
|
869
|
+
out.add(k);
|
|
870
|
+
}
|
|
871
|
+
return [...out];
|
|
872
|
+
}
|
|
873
|
+
const k = encodeKey(v);
|
|
874
|
+
return k === undefined ? [] : [k];
|
|
875
|
+
}
|
|
876
|
+
const r = extractKey(value, path);
|
|
877
|
+
return typeof r === "object" ? [r.key] : [];
|
|
878
|
+
}
|
|
879
|
+
function injectKey(value, path, key) {
|
|
880
|
+
const ids = path.split(".");
|
|
881
|
+
let v = value;
|
|
882
|
+
for (let i = 0;i < ids.length - 1; i++) {
|
|
883
|
+
if (!Object.prototype.hasOwnProperty.call(v, ids[i]))
|
|
884
|
+
v[ids[i]] = {};
|
|
885
|
+
v = v[ids[i]];
|
|
886
|
+
}
|
|
887
|
+
v[ids[ids.length - 1]] = key;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// src/value.ts
|
|
891
|
+
var TAGS = new Set(["$u", "$n", "$bi", "$d", "$re", "$bo", "$no", "$so", "$ab", "$ta", "$sa", "$m", "$set", "$r"]);
|
|
892
|
+
var VIEWS = {
|
|
893
|
+
Int8Array,
|
|
894
|
+
Uint8Array,
|
|
895
|
+
Uint8ClampedArray,
|
|
896
|
+
Int16Array,
|
|
897
|
+
Uint16Array,
|
|
898
|
+
Int32Array,
|
|
899
|
+
Uint32Array,
|
|
900
|
+
Float32Array,
|
|
901
|
+
Float64Array,
|
|
902
|
+
BigInt64Array,
|
|
903
|
+
BigUint64Array,
|
|
904
|
+
DataView
|
|
905
|
+
};
|
|
906
|
+
function cloneError(what) {
|
|
907
|
+
return new DOMException(`${what} could not be cloned.`, "DataCloneError");
|
|
908
|
+
}
|
|
909
|
+
function toBase64(b) {
|
|
910
|
+
let s = "";
|
|
911
|
+
for (let i = 0;i < b.length; i += 8192)
|
|
912
|
+
s += String.fromCharCode(...b.subarray(i, i + 8192));
|
|
913
|
+
return btoa(s);
|
|
914
|
+
}
|
|
915
|
+
function fromBase64(s) {
|
|
916
|
+
const bin = atob(s);
|
|
917
|
+
const b = new Uint8Array(bin.length);
|
|
918
|
+
for (let i = 0;i < bin.length; i++)
|
|
919
|
+
b[i] = bin.charCodeAt(i);
|
|
920
|
+
return b.buffer;
|
|
921
|
+
}
|
|
922
|
+
function num(n) {
|
|
923
|
+
if (Number.isFinite(n) && !Object.is(n, -0))
|
|
924
|
+
return n;
|
|
925
|
+
return { $n: Object.is(n, -0) ? "-0" : String(n) };
|
|
926
|
+
}
|
|
927
|
+
function unnum(v) {
|
|
928
|
+
if (typeof v === "number")
|
|
929
|
+
return v;
|
|
930
|
+
const s = v.$n;
|
|
931
|
+
return s === "-0" ? -0 : Number(s);
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
class Encoder {
|
|
935
|
+
tagged = false;
|
|
936
|
+
ids = new Map;
|
|
937
|
+
tag(o) {
|
|
938
|
+
this.tagged = true;
|
|
939
|
+
return o;
|
|
940
|
+
}
|
|
941
|
+
walk(v) {
|
|
942
|
+
switch (typeof v) {
|
|
943
|
+
case "string":
|
|
944
|
+
case "boolean":
|
|
945
|
+
return v;
|
|
946
|
+
case "number": {
|
|
947
|
+
const out = num(v);
|
|
948
|
+
if (out !== v)
|
|
949
|
+
this.tagged = true;
|
|
950
|
+
return out;
|
|
951
|
+
}
|
|
952
|
+
case "undefined":
|
|
953
|
+
return this.tag({ $u: 0 });
|
|
954
|
+
case "bigint":
|
|
955
|
+
return this.tag({ $bi: v.toString() });
|
|
956
|
+
case "object":
|
|
957
|
+
break;
|
|
958
|
+
default:
|
|
959
|
+
throw cloneError(typeof v === "function" ? "A function" : `A ${typeof v}`);
|
|
960
|
+
}
|
|
961
|
+
if (v === null)
|
|
962
|
+
return null;
|
|
963
|
+
const seen = this.ids.get(v);
|
|
964
|
+
if (seen !== undefined)
|
|
965
|
+
return this.tag({ $r: seen });
|
|
966
|
+
this.ids.set(v, this.ids.size);
|
|
967
|
+
const kind = Object.prototype.toString.call(v).slice(8, -1);
|
|
968
|
+
if (Array.isArray(v)) {
|
|
969
|
+
const n = v.length;
|
|
970
|
+
let dense = true;
|
|
971
|
+
for (let i = 0;i < n; i++) {
|
|
972
|
+
if (!Object.prototype.hasOwnProperty.call(v, i)) {
|
|
973
|
+
dense = false;
|
|
974
|
+
break;
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
if (dense) {
|
|
978
|
+
const out = new Array(n);
|
|
979
|
+
for (let i = 0;i < n; i++)
|
|
980
|
+
out[i] = this.walk(v[i]);
|
|
981
|
+
return out;
|
|
982
|
+
}
|
|
983
|
+
const items = [];
|
|
984
|
+
for (const k of Object.keys(v)) {
|
|
985
|
+
const i = Number(k);
|
|
986
|
+
if (Number.isInteger(i) && i >= 0 && String(i) === k)
|
|
987
|
+
items.push([i, this.walk(v[i])]);
|
|
988
|
+
}
|
|
989
|
+
return this.tag({ $sa: [n, items] });
|
|
990
|
+
}
|
|
991
|
+
switch (kind) {
|
|
992
|
+
case "Object":
|
|
993
|
+
break;
|
|
994
|
+
case "Date":
|
|
995
|
+
return this.tag({ $d: num(v.getTime()) });
|
|
996
|
+
case "RegExp":
|
|
997
|
+
return this.tag({ $re: [v.source, v.flags] });
|
|
998
|
+
case "Boolean":
|
|
999
|
+
return this.tag({ $bo: v.valueOf() });
|
|
1000
|
+
case "Number":
|
|
1001
|
+
return this.tag({ $no: num(v.valueOf()) });
|
|
1002
|
+
case "String":
|
|
1003
|
+
return this.tag({ $so: v.valueOf() });
|
|
1004
|
+
case "ArrayBuffer":
|
|
1005
|
+
return this.tag({ $ab: toBase64(new Uint8Array(v)) });
|
|
1006
|
+
case "Map": {
|
|
1007
|
+
const items = [];
|
|
1008
|
+
for (const [k, x] of v)
|
|
1009
|
+
items.push([this.walk(k), this.walk(x)]);
|
|
1010
|
+
return this.tag({ $m: items });
|
|
1011
|
+
}
|
|
1012
|
+
case "Set": {
|
|
1013
|
+
const items = [];
|
|
1014
|
+
for (const x of v)
|
|
1015
|
+
items.push(this.walk(x));
|
|
1016
|
+
return this.tag({ $set: items });
|
|
1017
|
+
}
|
|
1018
|
+
default:
|
|
1019
|
+
if (ArrayBuffer.isView(v) && kind in VIEWS) {
|
|
1020
|
+
return this.tag({ $ta: [kind, toBase64(new Uint8Array(v.buffer, v.byteOffset, v.byteLength))] });
|
|
1021
|
+
}
|
|
1022
|
+
throw cloneError(`An object of type ${kind}`);
|
|
1023
|
+
}
|
|
1024
|
+
const out = {};
|
|
1025
|
+
for (const k of Object.keys(v)) {
|
|
1026
|
+
const x = this.walk(v[k]);
|
|
1027
|
+
if (k.charCodeAt(0) === 36) {
|
|
1028
|
+
this.tagged = true;
|
|
1029
|
+
out["$" + k] = x;
|
|
1030
|
+
} else
|
|
1031
|
+
out[k] = x;
|
|
1032
|
+
}
|
|
1033
|
+
return out;
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
function encodeValue(v) {
|
|
1037
|
+
const e = new Encoder;
|
|
1038
|
+
const tree = e.walk(v);
|
|
1039
|
+
return (e.tagged ? "T" : "J") + JSON.stringify(tree);
|
|
1040
|
+
}
|
|
1041
|
+
function decodeValue(s) {
|
|
1042
|
+
const tree = JSON.parse(s.slice(1));
|
|
1043
|
+
return s.charCodeAt(0) === 74 ? tree : new Decoder().walk(tree);
|
|
1044
|
+
}
|
|
1045
|
+
class Decoder {
|
|
1046
|
+
objects = [];
|
|
1047
|
+
walk(t) {
|
|
1048
|
+
if (t === null || typeof t !== "object")
|
|
1049
|
+
return t;
|
|
1050
|
+
if (Array.isArray(t)) {
|
|
1051
|
+
const out = new Array(t.length);
|
|
1052
|
+
this.objects.push(out);
|
|
1053
|
+
for (let i = 0;i < t.length; i++)
|
|
1054
|
+
out[i] = this.walk(t[i]);
|
|
1055
|
+
return out;
|
|
1056
|
+
}
|
|
1057
|
+
const keys = Object.keys(t);
|
|
1058
|
+
const o = t;
|
|
1059
|
+
if (keys.length === 1 && TAGS.has(keys[0])) {
|
|
1060
|
+
const x = o[keys[0]];
|
|
1061
|
+
switch (keys[0]) {
|
|
1062
|
+
case "$u":
|
|
1063
|
+
return;
|
|
1064
|
+
case "$n":
|
|
1065
|
+
return unnum(t);
|
|
1066
|
+
case "$bi":
|
|
1067
|
+
return BigInt(x);
|
|
1068
|
+
case "$r":
|
|
1069
|
+
return this.objects[x];
|
|
1070
|
+
}
|
|
1071
|
+
let out;
|
|
1072
|
+
const at = this.objects.length;
|
|
1073
|
+
this.objects.push(null);
|
|
1074
|
+
switch (keys[0]) {
|
|
1075
|
+
case "$d":
|
|
1076
|
+
out = new Date(unnum(x));
|
|
1077
|
+
break;
|
|
1078
|
+
case "$re":
|
|
1079
|
+
out = new RegExp(x[0], x[1]);
|
|
1080
|
+
break;
|
|
1081
|
+
case "$bo":
|
|
1082
|
+
out = new Boolean(x);
|
|
1083
|
+
break;
|
|
1084
|
+
case "$no":
|
|
1085
|
+
out = new Number(unnum(x));
|
|
1086
|
+
break;
|
|
1087
|
+
case "$so":
|
|
1088
|
+
out = new String(x);
|
|
1089
|
+
break;
|
|
1090
|
+
case "$ab":
|
|
1091
|
+
out = fromBase64(x);
|
|
1092
|
+
break;
|
|
1093
|
+
case "$ta": {
|
|
1094
|
+
const [kind, b64] = x;
|
|
1095
|
+
out = new VIEWS[kind](fromBase64(b64));
|
|
1096
|
+
break;
|
|
1097
|
+
}
|
|
1098
|
+
case "$sa": {
|
|
1099
|
+
const [n, items] = x;
|
|
1100
|
+
const arr = new Array(n);
|
|
1101
|
+
this.objects[at] = arr;
|
|
1102
|
+
for (const [i, v] of items)
|
|
1103
|
+
arr[i] = this.walk(v);
|
|
1104
|
+
return arr;
|
|
1105
|
+
}
|
|
1106
|
+
case "$m": {
|
|
1107
|
+
const m = new Map;
|
|
1108
|
+
this.objects[at] = m;
|
|
1109
|
+
for (const [k, v] of x) {
|
|
1110
|
+
const kk = this.walk(k);
|
|
1111
|
+
m.set(kk, this.walk(v));
|
|
1112
|
+
}
|
|
1113
|
+
return m;
|
|
1114
|
+
}
|
|
1115
|
+
case "$set": {
|
|
1116
|
+
const s = new Set;
|
|
1117
|
+
this.objects[at] = s;
|
|
1118
|
+
for (const v of x)
|
|
1119
|
+
s.add(this.walk(v));
|
|
1120
|
+
return s;
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
this.objects[at] = out;
|
|
1124
|
+
return out;
|
|
1125
|
+
}
|
|
1126
|
+
const out = {};
|
|
1127
|
+
this.objects.push(out);
|
|
1128
|
+
for (const k of keys) {
|
|
1129
|
+
const v = this.walk(o[k]);
|
|
1130
|
+
if (k.charCodeAt(0) === 36)
|
|
1131
|
+
out[k.slice(1)] = v;
|
|
1132
|
+
else
|
|
1133
|
+
out[k] = v;
|
|
1134
|
+
}
|
|
1135
|
+
return out;
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
// src/host.worker.ts
|
|
1140
|
+
var memory = null;
|
|
1141
|
+
var markReady = () => {};
|
|
1142
|
+
var wasmReady = new Promise((resolve) => markReady = resolve);
|
|
1143
|
+
var encoder = new TextEncoder;
|
|
1144
|
+
|
|
1145
|
+
class IdbError extends Error {
|
|
1146
|
+
name;
|
|
1147
|
+
constructor(name, message) {
|
|
1148
|
+
super(message);
|
|
1149
|
+
this.name = name;
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
function storageOf(access) {
|
|
1153
|
+
return {
|
|
1154
|
+
read: (ptr, len, at) => access.read(new Uint8Array(memory.buffer, ptr, len), { at }),
|
|
1155
|
+
write: (ptr, len, at) => access.write(new Uint8Array(memory.buffer, ptr, len), { at }),
|
|
1156
|
+
size: () => access.getSize(),
|
|
1157
|
+
truncate: (size) => access.truncate(size),
|
|
1158
|
+
flush: () => access.flush()
|
|
1159
|
+
};
|
|
1160
|
+
}
|
|
1161
|
+
var CATALOG_SCHEMA = JSON.stringify({ primaryKey: "k", columns: [{ name: "k", kind: "f64" }] });
|
|
1162
|
+
var MAX_KEY = 2 ** 53;
|
|
1163
|
+
var CLOSE_DELAY_MS = 2000;
|
|
1164
|
+
function defaultBudget() {
|
|
1165
|
+
const mem = navigator.deviceMemory;
|
|
1166
|
+
return mem !== undefined && mem <= 4 ? 24 << 20 : 64 << 20;
|
|
1167
|
+
}
|
|
1168
|
+
async function lockFile(dir, name) {
|
|
1169
|
+
const file = await fileHandle(dir, name);
|
|
1170
|
+
for (let wait = 1;; wait *= 2) {
|
|
1171
|
+
try {
|
|
1172
|
+
return await file.createSyncAccessHandle();
|
|
1173
|
+
} catch (e) {
|
|
1174
|
+
const name = e?.name;
|
|
1175
|
+
if (wait > 1024 || name !== "NoModificationAllowedError" && name !== "InvalidStateError")
|
|
1176
|
+
throw e;
|
|
1177
|
+
await new Promise((r) => setTimeout(r, wait));
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
function dirName(db) {
|
|
1182
|
+
return "db-" + encodeURIComponent(db);
|
|
1183
|
+
}
|
|
1184
|
+
var root = null;
|
|
1185
|
+
var dirs = new Map;
|
|
1186
|
+
var files = new Map;
|
|
1187
|
+
function rootDir() {
|
|
1188
|
+
root ??= navigator.storage.getDirectory().then((r) => r.getDirectoryHandle("plateau", { create: true }));
|
|
1189
|
+
return root;
|
|
1190
|
+
}
|
|
1191
|
+
function dbDir(db) {
|
|
1192
|
+
let d = dirs.get(db);
|
|
1193
|
+
if (!d) {
|
|
1194
|
+
d = rootDir().then((r) => r.getDirectoryHandle(dirName(db), { create: true }));
|
|
1195
|
+
dirs.set(db, d);
|
|
1196
|
+
}
|
|
1197
|
+
return d;
|
|
1198
|
+
}
|
|
1199
|
+
function fileHandle(dir, name) {
|
|
1200
|
+
let m = files.get(dir);
|
|
1201
|
+
if (!m)
|
|
1202
|
+
files.set(dir, m = new Map);
|
|
1203
|
+
let f = m.get(name);
|
|
1204
|
+
if (!f) {
|
|
1205
|
+
f = dir.getFileHandle(name, { create: true });
|
|
1206
|
+
m.set(name, f);
|
|
1207
|
+
}
|
|
1208
|
+
return f;
|
|
1209
|
+
}
|
|
1210
|
+
function removeFile(dir, name) {
|
|
1211
|
+
files.get(dir)?.delete(name);
|
|
1212
|
+
return dir.removeEntry(name).catch(() => {});
|
|
1213
|
+
}
|
|
1214
|
+
var DIG = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
|
|
1215
|
+
var DIGV = new Uint8Array(128);
|
|
1216
|
+
for (let i = 0;i < DIG.length; i++)
|
|
1217
|
+
DIGV[DIG.charCodeAt(i)] = i;
|
|
1218
|
+
function digits(s) {
|
|
1219
|
+
let o = "";
|
|
1220
|
+
for (let i = 0;i < s.length; i++) {
|
|
1221
|
+
const u = s.charCodeAt(i);
|
|
1222
|
+
o += DIG[u / 1681 | 0] + DIG[(u / 41 | 0) % 41] + DIG[u % 41];
|
|
1223
|
+
}
|
|
1224
|
+
return o;
|
|
1225
|
+
}
|
|
1226
|
+
function undigits(d) {
|
|
1227
|
+
const units = new Array(d.length / 3);
|
|
1228
|
+
for (let i = 0, j = 0;i < d.length; i += 3, j++) {
|
|
1229
|
+
units[j] = DIGV[d.charCodeAt(i)] * 1681 + DIGV[d.charCodeAt(i + 1)] * 41 + DIGV[d.charCodeAt(i + 2)];
|
|
1230
|
+
}
|
|
1231
|
+
let s = "";
|
|
1232
|
+
for (let i = 0;i < units.length; i += 8192)
|
|
1233
|
+
s += String.fromCharCode(...units.slice(i, i + 8192));
|
|
1234
|
+
return s;
|
|
1235
|
+
}
|
|
1236
|
+
var loDigits = (b) => b === null ? "" : digits(b.s) + (b.m === "ge" ? "" : b.m === "gt" ? " " : "~");
|
|
1237
|
+
var hiDigits = (b) => b === null ? undefined : digits(b.s) + (b.m === "lt" ? "" : b.m === "le" ? " " : "~");
|
|
1238
|
+
var rangeLo = (r) => r && r.lo !== undefined ? { s: r.lo, m: r.loOpen ? "after" : "ge" } : null;
|
|
1239
|
+
var rangeHi = (r) => r && r.hi !== undefined ? { s: r.hi, m: r.hiOpen ? "lt" : "through" } : null;
|
|
1240
|
+
|
|
1241
|
+
class StoreHost {
|
|
1242
|
+
def;
|
|
1243
|
+
owner;
|
|
1244
|
+
db = null;
|
|
1245
|
+
access = null;
|
|
1246
|
+
meta = { gen: 1, rid: 1 };
|
|
1247
|
+
metaDirty = false;
|
|
1248
|
+
written = false;
|
|
1249
|
+
created = false;
|
|
1250
|
+
pending = { keys: new Set, byPk: new Map, unique: new Map, rows: [] };
|
|
1251
|
+
replaced = null;
|
|
1252
|
+
constructor(def, owner) {
|
|
1253
|
+
this.def = def;
|
|
1254
|
+
this.owner = owner;
|
|
1255
|
+
}
|
|
1256
|
+
get typed() {
|
|
1257
|
+
return !!this.def.columns;
|
|
1258
|
+
}
|
|
1259
|
+
get engine() {
|
|
1260
|
+
if (!this.db)
|
|
1261
|
+
throw new IdbError("UnknownError", `plateau: store ${this.def.name} is not open`);
|
|
1262
|
+
return this.db;
|
|
1263
|
+
}
|
|
1264
|
+
schema() {
|
|
1265
|
+
const ix = this.def.indexes.map((i) => ({ name: `_i${i.id}`, kind: "str", index: "keys" }));
|
|
1266
|
+
if (!this.def.columns) {
|
|
1267
|
+
return JSON.stringify({
|
|
1268
|
+
primaryKey: "_rid",
|
|
1269
|
+
columns: [{ name: "_rid", kind: "f64" }, { name: "_k", kind: "str", index: "keys" }, { name: "_v", kind: "str" }, ...ix]
|
|
1270
|
+
});
|
|
1271
|
+
}
|
|
1272
|
+
return JSON.stringify({
|
|
1273
|
+
primaryKey: this.def.keyPath,
|
|
1274
|
+
columns: [...this.def.columns.map((c) => ({ name: c.name, kind: c.kind, index: c.index ?? "none", group: c.group })), { name: "_v", kind: "str", omit_empty: true }, ...ix]
|
|
1275
|
+
});
|
|
1276
|
+
}
|
|
1277
|
+
async open(create) {
|
|
1278
|
+
if (this.db)
|
|
1279
|
+
return;
|
|
1280
|
+
await wasmReady;
|
|
1281
|
+
this.access = await lockFile(this.owner.dir, this.def.file);
|
|
1282
|
+
const storage = storageOf(this.access);
|
|
1283
|
+
this.db = create || this.access.getSize() === 0 ? Db.create(storage, this.schema()) : Db.open(storage);
|
|
1284
|
+
this.loadMeta();
|
|
1285
|
+
}
|
|
1286
|
+
loadMeta() {
|
|
1287
|
+
const m = this.db.meta();
|
|
1288
|
+
this.meta = m ? JSON.parse(m) : { gen: 1, rid: 1 };
|
|
1289
|
+
this.metaDirty = false;
|
|
1290
|
+
}
|
|
1291
|
+
close() {
|
|
1292
|
+
this.db?.free();
|
|
1293
|
+
this.db = null;
|
|
1294
|
+
this.access?.close();
|
|
1295
|
+
this.access = null;
|
|
1296
|
+
}
|
|
1297
|
+
clearPending() {
|
|
1298
|
+
this.pending = { keys: new Set, byPk: new Map, unique: new Map, rows: [] };
|
|
1299
|
+
}
|
|
1300
|
+
flush() {
|
|
1301
|
+
const p = this.pending;
|
|
1302
|
+
if (!p.rows.length)
|
|
1303
|
+
return;
|
|
1304
|
+
const db = this.engine;
|
|
1305
|
+
const n = p.rows.length;
|
|
1306
|
+
db.beginBatch(n);
|
|
1307
|
+
if (!this.def.columns) {
|
|
1308
|
+
db.setF64("_rid", Float64Array.from(p.rows, (r) => r.rid));
|
|
1309
|
+
setStrings(db, "_k", p.rows.map((r) => r.k));
|
|
1310
|
+
} else {
|
|
1311
|
+
this.def.columns.forEach((c, ci) => setColumn(db, c, p.rows.map((r) => r.t[ci])));
|
|
1312
|
+
}
|
|
1313
|
+
setStrings(db, "_v", p.rows.map((r) => r.v));
|
|
1314
|
+
this.def.indexes.forEach((ix, i) => setStrings(db, `_i${ix.id}`, p.rows.map((r) => r.ix[i])));
|
|
1315
|
+
db.putBatch();
|
|
1316
|
+
this.clearPending();
|
|
1317
|
+
}
|
|
1318
|
+
fetch(rids) {
|
|
1319
|
+
this.flush();
|
|
1320
|
+
if (!rids.length)
|
|
1321
|
+
return [];
|
|
1322
|
+
const rows = JSON.parse(this.engine.getMany(Float64Array.from(rids)));
|
|
1323
|
+
return rows.map((r) => r ? this.toValue(r) : undefined);
|
|
1324
|
+
}
|
|
1325
|
+
toValue(row) {
|
|
1326
|
+
const v = row._v;
|
|
1327
|
+
if (!this.typed)
|
|
1328
|
+
return decodeValue(v);
|
|
1329
|
+
if (v === undefined)
|
|
1330
|
+
return row;
|
|
1331
|
+
delete row._v;
|
|
1332
|
+
return Object.assign(row, decodeValue(v));
|
|
1333
|
+
}
|
|
1334
|
+
index(name) {
|
|
1335
|
+
const ix = this.def.indexes.find((x) => x.name === name);
|
|
1336
|
+
if (!ix)
|
|
1337
|
+
throw new IdbError("NotFoundError", `No index named ${name}.`);
|
|
1338
|
+
return ix;
|
|
1339
|
+
}
|
|
1340
|
+
scan(ix, lo, hi, desc, limit) {
|
|
1341
|
+
this.flush();
|
|
1342
|
+
if (ix === null && this.typed)
|
|
1343
|
+
return this.scanNumbers(lo, hi, desc, limit);
|
|
1344
|
+
const col = ix === null ? "_k" : `_i${ix.id}`;
|
|
1345
|
+
const text = this.engine.keyScan(col, loDigits(lo), hiDigits(hi), desc, Math.min(limit, 4294967295));
|
|
1346
|
+
if (!text)
|
|
1347
|
+
return [];
|
|
1348
|
+
return text.split(`
|
|
1349
|
+
`).map((line) => {
|
|
1350
|
+
const tab = line.indexOf("\t");
|
|
1351
|
+
const e = undigits(line.slice(0, tab));
|
|
1352
|
+
const rid = Number(line.slice(tab + 1));
|
|
1353
|
+
if (ix === null)
|
|
1354
|
+
return { k: e, p: e, rid };
|
|
1355
|
+
const kl = skipKey(e, 0);
|
|
1356
|
+
return { k: e.slice(0, kl), p: e.slice(kl), rid };
|
|
1357
|
+
});
|
|
1358
|
+
}
|
|
1359
|
+
scanNumbers(lo, hi, desc, limit) {
|
|
1360
|
+
let l = -Infinity;
|
|
1361
|
+
let lOpen = false;
|
|
1362
|
+
let h = Infinity;
|
|
1363
|
+
let hOpen = false;
|
|
1364
|
+
if (lo) {
|
|
1365
|
+
const n = encodedNumber(lo.s);
|
|
1366
|
+
if (n === undefined)
|
|
1367
|
+
return [];
|
|
1368
|
+
l = n;
|
|
1369
|
+
lOpen = lo.m !== "ge";
|
|
1370
|
+
}
|
|
1371
|
+
if (hi) {
|
|
1372
|
+
const n = encodedNumber(hi.s);
|
|
1373
|
+
if (n !== undefined) {
|
|
1374
|
+
h = n;
|
|
1375
|
+
hOpen = hi.m === "lt";
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
const keys = this.engine.pkScan(l, lOpen, h, hOpen, desc, Math.min(limit, 4294967295));
|
|
1379
|
+
return Array.from(keys, (n) => {
|
|
1380
|
+
const k = encodeNumberKey(n);
|
|
1381
|
+
return { k, p: k, rid: n };
|
|
1382
|
+
});
|
|
1383
|
+
}
|
|
1384
|
+
count(ix, r) {
|
|
1385
|
+
this.flush();
|
|
1386
|
+
if (ix === null && this.typed)
|
|
1387
|
+
return r === null ? this.engine.len() : this.scanNumbers(rangeLo(r), rangeHi(r), false, Infinity).length;
|
|
1388
|
+
return this.engine.keyCount(ix === null ? "_k" : `_i${ix.id}`, loDigits(rangeLo(r)), hiDigits(rangeHi(r)));
|
|
1389
|
+
}
|
|
1390
|
+
ridOf(pk) {
|
|
1391
|
+
if (this.typed) {
|
|
1392
|
+
const n = encodedNumber(pk);
|
|
1393
|
+
if (n === undefined)
|
|
1394
|
+
return;
|
|
1395
|
+
if (this.pending.keys.has(n))
|
|
1396
|
+
return n;
|
|
1397
|
+
return this.engine.pkScan(n, false, n, false, false, 1).length ? n : undefined;
|
|
1398
|
+
}
|
|
1399
|
+
const r = this.pending.byPk.get(pk);
|
|
1400
|
+
if (r !== undefined)
|
|
1401
|
+
return r;
|
|
1402
|
+
const d = digits(pk);
|
|
1403
|
+
const text = this.engine.keyScan("_k", d, d + " ", false, 1);
|
|
1404
|
+
return text ? Number(text.slice(text.indexOf("\t") + 1)) : undefined;
|
|
1405
|
+
}
|
|
1406
|
+
generate() {
|
|
1407
|
+
if (this.meta.gen > MAX_KEY)
|
|
1408
|
+
throw new IdbError("ConstraintError", "The key generator has reached its maximum.");
|
|
1409
|
+
this.metaDirty = true;
|
|
1410
|
+
return this.meta.gen++;
|
|
1411
|
+
}
|
|
1412
|
+
bump(n) {
|
|
1413
|
+
if (n >= this.meta.gen) {
|
|
1414
|
+
this.meta.gen = Math.floor(Math.min(n, MAX_KEY)) + 1;
|
|
1415
|
+
this.metaDirty = true;
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
indexKeysOf(value, pk) {
|
|
1419
|
+
return this.def.indexes.map((ix) => {
|
|
1420
|
+
const keys = indexKeys(value, ix.keyPath, ix.multiEntry);
|
|
1421
|
+
if (ix.unique) {
|
|
1422
|
+
const mine = this.pending.unique.get(ix.id);
|
|
1423
|
+
for (const ik of keys) {
|
|
1424
|
+
const other = mine?.get(ik);
|
|
1425
|
+
if (other !== undefined && other !== pk)
|
|
1426
|
+
throw new IdbError("ConstraintError", `Unique index ${ix.name} already holds this key.`);
|
|
1427
|
+
const d = digits(ik);
|
|
1428
|
+
const text = this.engine.keyScan(`_i${ix.id}`, d, d + "~", false, 2);
|
|
1429
|
+
for (const line of text ? text.split(`
|
|
1430
|
+
`) : []) {
|
|
1431
|
+
const e = undigits(line.slice(0, line.indexOf("\t")));
|
|
1432
|
+
if (e.slice(skipKey(e, 0)) !== pk)
|
|
1433
|
+
throw new IdbError("ConstraintError", `Unique index ${ix.name} already holds this key.`);
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
return keys;
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
put(row) {
|
|
1441
|
+
const def = this.def;
|
|
1442
|
+
let k = row.k;
|
|
1443
|
+
let generated = false;
|
|
1444
|
+
if (k === undefined) {
|
|
1445
|
+
k = encodeNumberKey(this.generate());
|
|
1446
|
+
generated = true;
|
|
1447
|
+
} else if (def.autoIncrement) {
|
|
1448
|
+
const n = encodedNumber(k);
|
|
1449
|
+
if (n !== undefined)
|
|
1450
|
+
this.bump(n);
|
|
1451
|
+
}
|
|
1452
|
+
let v = row.v;
|
|
1453
|
+
let t = row.t;
|
|
1454
|
+
let value;
|
|
1455
|
+
if (generated && typeof def.keyPath === "string") {
|
|
1456
|
+
if (this.typed) {
|
|
1457
|
+
t = t.slice();
|
|
1458
|
+
t[def.columns.findIndex((c) => c.name === def.keyPath)] = encodedNumber(k);
|
|
1459
|
+
} else {
|
|
1460
|
+
value = decodeValue(v);
|
|
1461
|
+
injectKey(value, def.keyPath, decodeKey(k));
|
|
1462
|
+
v = encodeValue(value);
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
const old = this.ridOf(k);
|
|
1466
|
+
if (row.add && old !== undefined)
|
|
1467
|
+
throw new IdbError("ConstraintError", "A record with this key already exists.");
|
|
1468
|
+
let ix = [];
|
|
1469
|
+
if (def.indexes.length) {
|
|
1470
|
+
if (value === undefined)
|
|
1471
|
+
value = this.typed ? this.typedValue(t, v) : decodeValue(v);
|
|
1472
|
+
ix = this.indexKeysOf(value, k);
|
|
1473
|
+
}
|
|
1474
|
+
let rid;
|
|
1475
|
+
if (this.typed)
|
|
1476
|
+
rid = encodedNumber(k);
|
|
1477
|
+
else if (old !== undefined)
|
|
1478
|
+
rid = old;
|
|
1479
|
+
else {
|
|
1480
|
+
rid = this.meta.rid++;
|
|
1481
|
+
this.metaDirty = true;
|
|
1482
|
+
}
|
|
1483
|
+
if (this.pending.keys.has(rid))
|
|
1484
|
+
this.flush();
|
|
1485
|
+
const p = this.pending;
|
|
1486
|
+
p.keys.add(rid);
|
|
1487
|
+
if (!this.typed)
|
|
1488
|
+
p.byPk.set(k, rid);
|
|
1489
|
+
def.indexes.forEach((x, i) => {
|
|
1490
|
+
if (!x.unique)
|
|
1491
|
+
return;
|
|
1492
|
+
let m = p.unique.get(x.id);
|
|
1493
|
+
if (!m)
|
|
1494
|
+
p.unique.set(x.id, m = new Map);
|
|
1495
|
+
for (const ik of ix[i])
|
|
1496
|
+
m.set(ik, k);
|
|
1497
|
+
});
|
|
1498
|
+
const dk = this.typed ? "" : digits(k);
|
|
1499
|
+
p.rows.push({ rid, k: dk, v, t, ix: ix.map((keys) => keys.map((ik) => digits(ik) + (dk || digits(k))).join(" ")) });
|
|
1500
|
+
return k;
|
|
1501
|
+
}
|
|
1502
|
+
putColumns(op, res, fail) {
|
|
1503
|
+
const def = this.def;
|
|
1504
|
+
const cols = def.columns;
|
|
1505
|
+
const pkAt = cols.findIndex((c) => c.name === def.keyPath);
|
|
1506
|
+
const keys = op.cols[pkAt];
|
|
1507
|
+
let fast = def.indexes.length === 0 && op.add.length === 0 && op.gen.length === 0;
|
|
1508
|
+
if (fast) {
|
|
1509
|
+
const seen = new Set;
|
|
1510
|
+
for (let i = 0;i < op.n && fast; i++) {
|
|
1511
|
+
const k = keys[i];
|
|
1512
|
+
if (seen.has(k) || this.pending.keys.has(k))
|
|
1513
|
+
fast = false;
|
|
1514
|
+
seen.add(k);
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
if (fast) {
|
|
1518
|
+
if (def.autoIncrement)
|
|
1519
|
+
for (let i = 0;i < op.n; i++)
|
|
1520
|
+
this.bump(keys[i]);
|
|
1521
|
+
this.flush();
|
|
1522
|
+
const db = this.engine;
|
|
1523
|
+
db.beginBatch(op.n);
|
|
1524
|
+
cols.forEach((c, ci) => setWire(db, c, op.cols[ci]));
|
|
1525
|
+
if (op.v)
|
|
1526
|
+
setStrings(db, "_v", op.v);
|
|
1527
|
+
else
|
|
1528
|
+
db.setStr("_v", new Uint32Array(op.n + 1), new Uint8Array(0));
|
|
1529
|
+
db.putBatch();
|
|
1530
|
+
for (let i = 0;i < op.n; i++)
|
|
1531
|
+
res.push(undefined);
|
|
1532
|
+
return;
|
|
1533
|
+
}
|
|
1534
|
+
const adds = new Set(op.add);
|
|
1535
|
+
const gens = new Set(op.gen);
|
|
1536
|
+
for (let i = 0;i < op.n; i++) {
|
|
1537
|
+
const t = cols.map((c, ci) => wireAt(c, op.cols[ci], i));
|
|
1538
|
+
const row = { v: op.v ? op.v[i] : "", t, add: adds.has(i) };
|
|
1539
|
+
if (!gens.has(i))
|
|
1540
|
+
row.k = encodeNumberKey(keys[i]);
|
|
1541
|
+
try {
|
|
1542
|
+
const k = this.put(row);
|
|
1543
|
+
res.push(gens.has(i) ? k : undefined);
|
|
1544
|
+
} catch (e) {
|
|
1545
|
+
fail(e, res.length);
|
|
1546
|
+
res.push(undefined);
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
typedValue(t, rest) {
|
|
1551
|
+
const row = {};
|
|
1552
|
+
this.def.columns.forEach((c, i) => row[c.name] = t[i]);
|
|
1553
|
+
return rest === "" ? row : Object.assign(row, decodeValue(rest));
|
|
1554
|
+
}
|
|
1555
|
+
deleteKeys(ks) {
|
|
1556
|
+
this.flush();
|
|
1557
|
+
if (this.typed) {
|
|
1558
|
+
const nums = ks.map((k) => encodedNumber(k)).filter((n) => n !== undefined);
|
|
1559
|
+
if (nums.length)
|
|
1560
|
+
this.engine.delete(Float64Array.from(nums));
|
|
1561
|
+
return;
|
|
1562
|
+
}
|
|
1563
|
+
const rids = [];
|
|
1564
|
+
for (const k of ks) {
|
|
1565
|
+
const r = this.ridOf(k);
|
|
1566
|
+
if (r !== undefined)
|
|
1567
|
+
rids.push(r);
|
|
1568
|
+
}
|
|
1569
|
+
if (rids.length)
|
|
1570
|
+
this.engine.delete(Float64Array.from(rids));
|
|
1571
|
+
}
|
|
1572
|
+
deleteRange(r) {
|
|
1573
|
+
const rids = this.scan(null, rangeLo(r), rangeHi(r), false, Infinity).map((e) => e.rid);
|
|
1574
|
+
if (rids.length)
|
|
1575
|
+
this.engine.delete(Float64Array.from(rids));
|
|
1576
|
+
}
|
|
1577
|
+
values(ents) {
|
|
1578
|
+
return this.fetch(ents.map((e) => e.rid));
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
var ASCII = /^[\x00-\x7f]*$/;
|
|
1582
|
+
function setStrings(db, name, strs) {
|
|
1583
|
+
const n = strs.length;
|
|
1584
|
+
const offsets = new Uint32Array(n + 1);
|
|
1585
|
+
const joined = strs.join("");
|
|
1586
|
+
if (ASCII.test(joined)) {
|
|
1587
|
+
for (let i = 0;i < n; i++)
|
|
1588
|
+
offsets[i + 1] = offsets[i] + strs[i].length;
|
|
1589
|
+
db.setStr(name, offsets, encoder.encode(joined));
|
|
1590
|
+
return;
|
|
1591
|
+
}
|
|
1592
|
+
let size = 0;
|
|
1593
|
+
for (const s of strs)
|
|
1594
|
+
size += s.length * 3;
|
|
1595
|
+
const bytes = new Uint8Array(size);
|
|
1596
|
+
let at = 0;
|
|
1597
|
+
strs.forEach((s, i) => {
|
|
1598
|
+
at += encoder.encodeInto(s, bytes.subarray(at)).written;
|
|
1599
|
+
offsets[i + 1] = at;
|
|
1600
|
+
});
|
|
1601
|
+
db.setStr(name, offsets, bytes.subarray(0, at));
|
|
1602
|
+
}
|
|
1603
|
+
function setWire(db, c, w) {
|
|
1604
|
+
switch (c.kind) {
|
|
1605
|
+
case "f64":
|
|
1606
|
+
db.setF64(c.name, w);
|
|
1607
|
+
break;
|
|
1608
|
+
case "u32":
|
|
1609
|
+
db.setU32(c.name, w);
|
|
1610
|
+
break;
|
|
1611
|
+
case "str":
|
|
1612
|
+
setStrings(db, c.name, w);
|
|
1613
|
+
break;
|
|
1614
|
+
case "u32_multi": {
|
|
1615
|
+
const [offsets, values] = w;
|
|
1616
|
+
db.setMulti(c.name, offsets, values);
|
|
1617
|
+
break;
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
function wireAt(c, w, i) {
|
|
1622
|
+
if (c.kind === "u32_multi") {
|
|
1623
|
+
const [offsets, values] = w;
|
|
1624
|
+
return Array.from(values.subarray(offsets[i], offsets[i + 1]));
|
|
1625
|
+
}
|
|
1626
|
+
return w[i];
|
|
1627
|
+
}
|
|
1628
|
+
function setColumn(db, c, vals) {
|
|
1629
|
+
switch (c.kind) {
|
|
1630
|
+
case "f64":
|
|
1631
|
+
db.setF64(c.name, Float64Array.from(vals));
|
|
1632
|
+
break;
|
|
1633
|
+
case "u32":
|
|
1634
|
+
db.setU32(c.name, Uint32Array.from(vals));
|
|
1635
|
+
break;
|
|
1636
|
+
case "str":
|
|
1637
|
+
setStrings(db, c.name, vals);
|
|
1638
|
+
break;
|
|
1639
|
+
case "u32_multi": {
|
|
1640
|
+
const offsets = new Uint32Array(vals.length + 1);
|
|
1641
|
+
let total = 0;
|
|
1642
|
+
for (const v of vals)
|
|
1643
|
+
total += v.length;
|
|
1644
|
+
const values = new Uint32Array(total);
|
|
1645
|
+
let at = 0;
|
|
1646
|
+
vals.forEach((v, i) => {
|
|
1647
|
+
for (const x of v)
|
|
1648
|
+
values[at++] = x;
|
|
1649
|
+
offsets[i + 1] = at;
|
|
1650
|
+
});
|
|
1651
|
+
db.setMulti(c.name, offsets, values);
|
|
1652
|
+
break;
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
class DatabaseHost {
|
|
1658
|
+
name;
|
|
1659
|
+
dir = null;
|
|
1660
|
+
catalogAccess = null;
|
|
1661
|
+
catalogDb = null;
|
|
1662
|
+
cat = { version: 0, nextId: 1, stores: [] };
|
|
1663
|
+
stores = new Map;
|
|
1664
|
+
conns = 0;
|
|
1665
|
+
upgrade = null;
|
|
1666
|
+
txStores = new Map;
|
|
1667
|
+
closeTimer = null;
|
|
1668
|
+
failed = new Set;
|
|
1669
|
+
constructor(name) {
|
|
1670
|
+
this.name = name;
|
|
1671
|
+
}
|
|
1672
|
+
async open() {
|
|
1673
|
+
if (this.dir)
|
|
1674
|
+
return;
|
|
1675
|
+
await wasmReady;
|
|
1676
|
+
this.dir = await dbDir(this.name);
|
|
1677
|
+
this.catalogAccess = await lockFile(this.dir, "catalog");
|
|
1678
|
+
if (this.catalogAccess.getSize() > 0) {
|
|
1679
|
+
this.catalogDb = Db.open(storageOf(this.catalogAccess));
|
|
1680
|
+
const meta = JSON.parse(this.catalogDb.meta());
|
|
1681
|
+
if (meta.intent)
|
|
1682
|
+
await this.recover(meta.intent);
|
|
1683
|
+
this.cat = { version: meta.version, nextId: meta.nextId, stores: meta.stores };
|
|
1684
|
+
}
|
|
1685
|
+
for (const def of this.cat.stores)
|
|
1686
|
+
this.stores.set(def.name, new StoreHost(def, this));
|
|
1687
|
+
for (const s of this.stores.values())
|
|
1688
|
+
await s.open(false);
|
|
1689
|
+
}
|
|
1690
|
+
async recover(intent) {
|
|
1691
|
+
for (const [file, seq] of Object.entries(intent)) {
|
|
1692
|
+
const access = await lockFile(this.dir, file);
|
|
1693
|
+
if (access.getSize() > 0) {
|
|
1694
|
+
const db = Db.openAt(storageOf(access), seq);
|
|
1695
|
+
db.save();
|
|
1696
|
+
db.free();
|
|
1697
|
+
}
|
|
1698
|
+
access.close();
|
|
1699
|
+
}
|
|
1700
|
+
const meta = JSON.parse(this.catalogDb.meta());
|
|
1701
|
+
meta.intent = null;
|
|
1702
|
+
this.catalogDb.setMeta(JSON.stringify(meta));
|
|
1703
|
+
this.catalogDb.save();
|
|
1704
|
+
}
|
|
1705
|
+
writeCatalog(cat, intent) {
|
|
1706
|
+
if (!this.catalogDb)
|
|
1707
|
+
this.catalogDb = Db.create(storageOf(this.catalogAccess), CATALOG_SCHEMA);
|
|
1708
|
+
this.catalogDb.setMeta(JSON.stringify({ ...cat, intent }));
|
|
1709
|
+
this.catalogDb.save();
|
|
1710
|
+
}
|
|
1711
|
+
close() {
|
|
1712
|
+
for (const s of this.stores.values())
|
|
1713
|
+
s.close();
|
|
1714
|
+
this.catalogDb?.free();
|
|
1715
|
+
this.catalogDb = null;
|
|
1716
|
+
this.catalogAccess?.close();
|
|
1717
|
+
this.catalogAccess = null;
|
|
1718
|
+
this.dir = null;
|
|
1719
|
+
this.stores.clear();
|
|
1720
|
+
}
|
|
1721
|
+
store(name) {
|
|
1722
|
+
const s = this.stores.get(name);
|
|
1723
|
+
if (!s)
|
|
1724
|
+
throw new IdbError("NotFoundError", `No object store named ${name}.`);
|
|
1725
|
+
return s;
|
|
1726
|
+
}
|
|
1727
|
+
beginIsAsync(b) {
|
|
1728
|
+
if (b.mode === "versionchange")
|
|
1729
|
+
return [...this.stores.values()].some((s) => !s.db);
|
|
1730
|
+
return b.stores.some((n) => this.stores.get(n)?.db === null);
|
|
1731
|
+
}
|
|
1732
|
+
async begin(tx, b) {
|
|
1733
|
+
if (b.mode === "versionchange") {
|
|
1734
|
+
this.upgrade = { before: structuredClone(this.cat), deleted: [] };
|
|
1735
|
+
this.cat = structuredClone(this.cat);
|
|
1736
|
+
this.cat.version = b.version;
|
|
1737
|
+
for (const def of this.cat.stores)
|
|
1738
|
+
this.stores.get(def.name).def = def;
|
|
1739
|
+
for (const s of this.stores.values())
|
|
1740
|
+
await s.open(false);
|
|
1741
|
+
} else {
|
|
1742
|
+
for (const name of b.stores) {
|
|
1743
|
+
const s = this.store(name);
|
|
1744
|
+
if (!s.db)
|
|
1745
|
+
await s.open(false);
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
if (b.mode !== "readonly")
|
|
1749
|
+
this.txStores.set(tx, new Set);
|
|
1750
|
+
}
|
|
1751
|
+
touched(tx, s) {
|
|
1752
|
+
s.written = true;
|
|
1753
|
+
this.txStores.get(tx)?.add(s);
|
|
1754
|
+
return s;
|
|
1755
|
+
}
|
|
1756
|
+
run(tx, op, res, errs) {
|
|
1757
|
+
const at = res.length;
|
|
1758
|
+
const fail = (e, i) => {
|
|
1759
|
+
const err = e instanceof IdbError ? e : new IdbError("UnknownError", e instanceof Error ? e.message : String(e));
|
|
1760
|
+
if (!(e instanceof IdbError))
|
|
1761
|
+
console.error(e);
|
|
1762
|
+
errs.push([i, err.name, err.message]);
|
|
1763
|
+
};
|
|
1764
|
+
switch (op.o) {
|
|
1765
|
+
case "get": {
|
|
1766
|
+
const s = this.store(op.s);
|
|
1767
|
+
try {
|
|
1768
|
+
s.flush();
|
|
1769
|
+
const rids = op.ks.map((k) => s.typed ? encodedNumber(k) : s.ridOf(k));
|
|
1770
|
+
const vals = s.fetch(rids.filter((r) => r !== undefined));
|
|
1771
|
+
let j = 0;
|
|
1772
|
+
for (const r of rids)
|
|
1773
|
+
res.push(r === undefined ? undefined : vals[j++]);
|
|
1774
|
+
} catch (e) {
|
|
1775
|
+
for (let i = 0;i < op.ks.length; i++) {
|
|
1776
|
+
fail(e, at + i);
|
|
1777
|
+
res.push(undefined);
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
return;
|
|
1781
|
+
}
|
|
1782
|
+
case "delk": {
|
|
1783
|
+
const n = op.nums ? op.nums.length : op.ks.length;
|
|
1784
|
+
try {
|
|
1785
|
+
const s = this.touched(tx, this.store(op.s));
|
|
1786
|
+
if (op.nums) {
|
|
1787
|
+
s.flush();
|
|
1788
|
+
s.engine.delete(Float64Array.from(op.nums));
|
|
1789
|
+
} else
|
|
1790
|
+
s.deleteKeys(op.ks);
|
|
1791
|
+
} catch (e) {
|
|
1792
|
+
for (let i = 0;i < n; i++)
|
|
1793
|
+
fail(e, at + i);
|
|
1794
|
+
}
|
|
1795
|
+
for (let i = 0;i < n; i++)
|
|
1796
|
+
res.push(undefined);
|
|
1797
|
+
return;
|
|
1798
|
+
}
|
|
1799
|
+
case "tput": {
|
|
1800
|
+
this.touched(tx, this.store(op.s)).putColumns(op, res, fail);
|
|
1801
|
+
return;
|
|
1802
|
+
}
|
|
1803
|
+
case "put": {
|
|
1804
|
+
const s = this.touched(tx, this.store(op.s));
|
|
1805
|
+
for (const row of op.rows) {
|
|
1806
|
+
try {
|
|
1807
|
+
res.push(s.put(row));
|
|
1808
|
+
} catch (e) {
|
|
1809
|
+
fail(e, res.length);
|
|
1810
|
+
res.push(undefined);
|
|
1811
|
+
}
|
|
1812
|
+
}
|
|
1813
|
+
return;
|
|
1814
|
+
}
|
|
1815
|
+
}
|
|
1816
|
+
try {
|
|
1817
|
+
res.push(this.one(tx, op));
|
|
1818
|
+
} catch (e) {
|
|
1819
|
+
fail(e, at);
|
|
1820
|
+
res.push(undefined);
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
one(tx, op) {
|
|
1824
|
+
switch (op.o) {
|
|
1825
|
+
case "first": {
|
|
1826
|
+
const s = this.store(op.s);
|
|
1827
|
+
const ix = op.i === undefined ? null : s.index(op.i);
|
|
1828
|
+
const [e] = s.scan(ix, rangeLo(op.r), rangeHi(op.r), false, 1);
|
|
1829
|
+
if (!e)
|
|
1830
|
+
return;
|
|
1831
|
+
return op.key ? e.p : s.values([e])[0];
|
|
1832
|
+
}
|
|
1833
|
+
case "all": {
|
|
1834
|
+
const s = this.store(op.s);
|
|
1835
|
+
const ix = op.i === undefined ? null : s.index(op.i);
|
|
1836
|
+
const ents = s.scan(ix, rangeLo(op.r), rangeHi(op.r), false, op.n > 0 ? op.n : Infinity);
|
|
1837
|
+
return op.key ? ents.map((e) => e.p) : s.values(ents);
|
|
1838
|
+
}
|
|
1839
|
+
case "recs": {
|
|
1840
|
+
const s = this.store(op.s);
|
|
1841
|
+
const ix = op.i === undefined ? null : s.index(op.i);
|
|
1842
|
+
const desc = op.d === "prev" || op.d === "prevunique";
|
|
1843
|
+
let ents = s.scan(ix, rangeLo(op.r), rangeHi(op.r), desc, op.d.endsWith("unique") ? Infinity : op.n > 0 ? op.n : Infinity);
|
|
1844
|
+
if (op.d.endsWith("unique")) {
|
|
1845
|
+
const firsts = new Map;
|
|
1846
|
+
for (const e of ents)
|
|
1847
|
+
if (desc || !firsts.has(e.k))
|
|
1848
|
+
firsts.set(e.k, e);
|
|
1849
|
+
ents = [...firsts.values()];
|
|
1850
|
+
if (op.n > 0)
|
|
1851
|
+
ents = ents.slice(0, op.n);
|
|
1852
|
+
}
|
|
1853
|
+
const vals = s.values(ents);
|
|
1854
|
+
return ents.map((e, i) => ({ k: e.k, p: e.p, v: vals[i] }));
|
|
1855
|
+
}
|
|
1856
|
+
case "count": {
|
|
1857
|
+
const s = this.store(op.s);
|
|
1858
|
+
return s.count(op.i === undefined ? null : s.index(op.i), op.r);
|
|
1859
|
+
}
|
|
1860
|
+
case "del":
|
|
1861
|
+
this.touched(tx, this.store(op.s)).deleteRange(op.r);
|
|
1862
|
+
return;
|
|
1863
|
+
case "clear":
|
|
1864
|
+
this.touched(tx, this.store(op.s)).deleteRange(null);
|
|
1865
|
+
return;
|
|
1866
|
+
case "cur":
|
|
1867
|
+
return this.cursor(op);
|
|
1868
|
+
case "query": {
|
|
1869
|
+
const s = this.store(op.s);
|
|
1870
|
+
if (!s.typed)
|
|
1871
|
+
throw new IdbError("NotSupportedError", "plateau: query() needs a store with declared columns.");
|
|
1872
|
+
s.flush();
|
|
1873
|
+
const out = JSON.parse(s.engine.query(op.q));
|
|
1874
|
+
return { plan: out.plan, value: out.rows ? out.rows.map((r) => s.toValue(r)) : out.count };
|
|
1875
|
+
}
|
|
1876
|
+
case "rmStore": {
|
|
1877
|
+
const s = this.store(op.s);
|
|
1878
|
+
this.stores.delete(op.s);
|
|
1879
|
+
this.cat.stores = this.cat.stores.filter((d) => d !== s.def);
|
|
1880
|
+
if (!s.created)
|
|
1881
|
+
this.upgrade.deleted.push(s);
|
|
1882
|
+
else
|
|
1883
|
+
s.close();
|
|
1884
|
+
return;
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
throw new IdbError("UnknownError", `plateau: unknown op ${op.o}`);
|
|
1888
|
+
}
|
|
1889
|
+
async createStore(tx, def) {
|
|
1890
|
+
const s = new StoreHost(def, this);
|
|
1891
|
+
s.created = true;
|
|
1892
|
+
this.cat.stores.push(def);
|
|
1893
|
+
this.cat.nextId = Math.max(this.cat.nextId, def.id + 1);
|
|
1894
|
+
this.stores.set(def.name, s);
|
|
1895
|
+
await s.open(true);
|
|
1896
|
+
this.touched(tx, s);
|
|
1897
|
+
}
|
|
1898
|
+
async changeIndex(tx, op) {
|
|
1899
|
+
const s = this.store(op.s);
|
|
1900
|
+
const before = s.def.indexes;
|
|
1901
|
+
if (op.o === "mkIndex") {
|
|
1902
|
+
s.def.indexes = [...before, op.def];
|
|
1903
|
+
this.cat.nextId = Math.max(this.cat.nextId, op.def.id + 1);
|
|
1904
|
+
} else {
|
|
1905
|
+
s.index(op.i);
|
|
1906
|
+
s.def.indexes = before.filter((x) => x.name !== op.i);
|
|
1907
|
+
}
|
|
1908
|
+
s.flush();
|
|
1909
|
+
const old = { db: s.engine, access: s.access, file: s.def.file };
|
|
1910
|
+
const m = /^(.*?)(?:\.(\d+))?$/.exec(old.file);
|
|
1911
|
+
const file = `${m[1]}.${Number(m[2] ?? 0) + 1}`;
|
|
1912
|
+
const ents = s.scan(null, null, null, false, Infinity);
|
|
1913
|
+
const values = s.fetch(ents.map((e) => e.rid));
|
|
1914
|
+
const meta = s.meta;
|
|
1915
|
+
s.db = null;
|
|
1916
|
+
s.access = null;
|
|
1917
|
+
s.def.file = file;
|
|
1918
|
+
try {
|
|
1919
|
+
await s.open(true);
|
|
1920
|
+
s.meta = meta;
|
|
1921
|
+
s.metaDirty = true;
|
|
1922
|
+
for (let i = 0;i < ents.length; i++) {
|
|
1923
|
+
const v = values[i];
|
|
1924
|
+
if (s.typed) {
|
|
1925
|
+
const cols = s.def.columns;
|
|
1926
|
+
const t = cols.map((c) => v[c.name]);
|
|
1927
|
+
const rest = {};
|
|
1928
|
+
for (const key of Object.keys(v))
|
|
1929
|
+
if (!cols.some((c) => c.name === key))
|
|
1930
|
+
rest[key] = v[key];
|
|
1931
|
+
s.put({ k: ents[i].p, v: Object.keys(rest).length ? encodeValue(rest) : "", t, add: false });
|
|
1932
|
+
} else {
|
|
1933
|
+
const rid = ents[i].rid;
|
|
1934
|
+
const ix = s.indexKeysOf(v, ents[i].p);
|
|
1935
|
+
const dk = digits(ents[i].p);
|
|
1936
|
+
if (s.pending.keys.has(rid))
|
|
1937
|
+
s.flush();
|
|
1938
|
+
s.pending.keys.add(rid);
|
|
1939
|
+
s.pending.byPk.set(ents[i].p, rid);
|
|
1940
|
+
s.def.indexes.forEach((x, j) => {
|
|
1941
|
+
if (!x.unique)
|
|
1942
|
+
return;
|
|
1943
|
+
let mm = s.pending.unique.get(x.id);
|
|
1944
|
+
if (!mm)
|
|
1945
|
+
s.pending.unique.set(x.id, mm = new Map);
|
|
1946
|
+
for (const ik of ix[j])
|
|
1947
|
+
mm.set(ik, ents[i].p);
|
|
1948
|
+
});
|
|
1949
|
+
s.pending.rows.push({ rid, k: dk, v: encodeValue(v), ix: ix.map((keys) => keys.map((ik) => digits(ik) + dk).join(" ")) });
|
|
1950
|
+
}
|
|
1951
|
+
if (s.pending.rows.length >= 1e4)
|
|
1952
|
+
s.flush();
|
|
1953
|
+
}
|
|
1954
|
+
s.flush();
|
|
1955
|
+
} catch (e) {
|
|
1956
|
+
s.close();
|
|
1957
|
+
removeFile(this.dir, file);
|
|
1958
|
+
s.db = old.db;
|
|
1959
|
+
s.access = old.access;
|
|
1960
|
+
s.def.file = old.file;
|
|
1961
|
+
s.def.indexes = before;
|
|
1962
|
+
throw e;
|
|
1963
|
+
}
|
|
1964
|
+
if (!s.replaced && !s.created)
|
|
1965
|
+
s.replaced = old;
|
|
1966
|
+
else {
|
|
1967
|
+
old.db.free();
|
|
1968
|
+
old.access.close();
|
|
1969
|
+
removeFile(this.dir, old.file);
|
|
1970
|
+
}
|
|
1971
|
+
this.touched(tx, s);
|
|
1972
|
+
}
|
|
1973
|
+
cursor(op) {
|
|
1974
|
+
const s = this.store(op.s);
|
|
1975
|
+
const ix = op.i === undefined ? null : s.index(op.i);
|
|
1976
|
+
const lo0 = rangeLo(op.r);
|
|
1977
|
+
const hi0 = rangeHi(op.r);
|
|
1978
|
+
let at = op.at ? { k: op.at[0], p: op.at[1], rid: 0 } : null;
|
|
1979
|
+
const entry = (e) => ix === null ? e.p : e.k + e.p;
|
|
1980
|
+
let hit = null;
|
|
1981
|
+
for (let step = 0;step < op.n; step++) {
|
|
1982
|
+
const to = step === 0 ? op.to : undefined;
|
|
1983
|
+
let found;
|
|
1984
|
+
switch (op.d) {
|
|
1985
|
+
case "next": {
|
|
1986
|
+
const lo = to ? { s: to[1] === undefined ? to[0] : to[0] + to[1], m: "ge" } : at ? { s: entry(at), m: "gt" } : lo0;
|
|
1987
|
+
found = s.scan(ix, lo, hi0, false, 1)[0];
|
|
1988
|
+
break;
|
|
1989
|
+
}
|
|
1990
|
+
case "nextunique": {
|
|
1991
|
+
const lo = to ? { s: to[0], m: "ge" } : at ? { s: at.k, m: "after" } : lo0;
|
|
1992
|
+
found = s.scan(ix, lo, hi0, false, 1)[0];
|
|
1993
|
+
break;
|
|
1994
|
+
}
|
|
1995
|
+
case "prev": {
|
|
1996
|
+
const hi = to ? to[1] === undefined ? { s: to[0], m: "through" } : { s: to[0] + to[1], m: "le" } : at ? { s: entry(at), m: "lt" } : hi0;
|
|
1997
|
+
found = s.scan(ix, lo0, hi, true, 1)[0];
|
|
1998
|
+
break;
|
|
1999
|
+
}
|
|
2000
|
+
case "prevunique": {
|
|
2001
|
+
const hi = to ? { s: to[0], m: "through" } : at ? { s: at.k, m: "lt" } : hi0;
|
|
2002
|
+
const last = s.scan(ix, lo0, hi, true, 1)[0];
|
|
2003
|
+
found = last && s.scan(ix, { s: last.k, m: "ge" }, hi, false, 1)[0];
|
|
2004
|
+
break;
|
|
2005
|
+
}
|
|
2006
|
+
}
|
|
2007
|
+
if (!found)
|
|
2008
|
+
return null;
|
|
2009
|
+
at = hit = found;
|
|
2010
|
+
}
|
|
2011
|
+
const out = { k: hit.k, p: hit.p };
|
|
2012
|
+
if (op.v)
|
|
2013
|
+
out.v = s.values([hit])[0];
|
|
2014
|
+
if (op.pre && (op.d === "next" || op.d === "prev")) {
|
|
2015
|
+
const more = op.d === "next" ? s.scan(ix, { s: entry(hit), m: "gt" }, hi0, false, op.pre) : s.scan(ix, lo0, { s: entry(hit), m: "lt" }, true, op.pre);
|
|
2016
|
+
const vals = op.v ? s.values(more) : [];
|
|
2017
|
+
out.more = more.map((e, i) => op.v ? { k: e.k, p: e.p, v: vals[i] } : { k: e.k, p: e.p });
|
|
2018
|
+
}
|
|
2019
|
+
return out;
|
|
2020
|
+
}
|
|
2021
|
+
commit(tx) {
|
|
2022
|
+
const written = [...this.txStores.get(tx) ?? []].filter((s) => this.stores.get(s.def.name) === s);
|
|
2023
|
+
this.txStores.delete(tx);
|
|
2024
|
+
const up = this.upgrade;
|
|
2025
|
+
const existing = written.filter((s) => !s.created && !s.replaced);
|
|
2026
|
+
const group = up ? existing.length > 0 : written.length > 1;
|
|
2027
|
+
if (group) {
|
|
2028
|
+
const intent = {};
|
|
2029
|
+
for (const s of existing)
|
|
2030
|
+
intent[s.def.file] = s.engine.seq();
|
|
2031
|
+
this.writeCatalog(up ? up.before : this.cat, intent);
|
|
2032
|
+
for (const s of existing)
|
|
2033
|
+
s.engine.setKeepPrevious(true);
|
|
2034
|
+
}
|
|
2035
|
+
try {
|
|
2036
|
+
for (const s of written) {
|
|
2037
|
+
s.flush();
|
|
2038
|
+
if (s.metaDirty || s.created || s.replaced)
|
|
2039
|
+
s.engine.setMeta(JSON.stringify(s.meta));
|
|
2040
|
+
s.engine.save();
|
|
2041
|
+
s.metaDirty = false;
|
|
2042
|
+
s.written = false;
|
|
2043
|
+
}
|
|
2044
|
+
if (up || group)
|
|
2045
|
+
this.writeCatalog(this.cat, null);
|
|
2046
|
+
} finally {
|
|
2047
|
+
if (group)
|
|
2048
|
+
for (const s of existing)
|
|
2049
|
+
s.engine.setKeepPrevious(false);
|
|
2050
|
+
}
|
|
2051
|
+
if (group)
|
|
2052
|
+
for (const s of existing)
|
|
2053
|
+
s.engine.releasePrevious();
|
|
2054
|
+
for (const s of written) {
|
|
2055
|
+
s.created = false;
|
|
2056
|
+
if (s.replaced) {
|
|
2057
|
+
s.replaced.db.free();
|
|
2058
|
+
s.replaced.access.close();
|
|
2059
|
+
removeFile(this.dir, s.replaced.file);
|
|
2060
|
+
s.replaced = null;
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
if (up) {
|
|
2064
|
+
this.upgrade = null;
|
|
2065
|
+
for (const s of up.deleted) {
|
|
2066
|
+
s.close();
|
|
2067
|
+
removeFile(this.dir, s.def.file);
|
|
2068
|
+
}
|
|
2069
|
+
}
|
|
2070
|
+
}
|
|
2071
|
+
abort(tx) {
|
|
2072
|
+
const written = this.txStores.get(tx) ?? new Set;
|
|
2073
|
+
this.txStores.delete(tx);
|
|
2074
|
+
const up = this.upgrade;
|
|
2075
|
+
if (up) {
|
|
2076
|
+
for (const s of [...this.stores.values(), ...up.deleted]) {
|
|
2077
|
+
if (s.created) {
|
|
2078
|
+
s.close();
|
|
2079
|
+
removeFile(this.dir, s.def.file);
|
|
2080
|
+
} else if (s.replaced) {
|
|
2081
|
+
s.close();
|
|
2082
|
+
removeFile(this.dir, s.def.file);
|
|
2083
|
+
s.db = s.replaced.db;
|
|
2084
|
+
s.access = s.replaced.access;
|
|
2085
|
+
s.replaced = null;
|
|
2086
|
+
}
|
|
2087
|
+
}
|
|
2088
|
+
for (const s of up.deleted)
|
|
2089
|
+
if (!s.created)
|
|
2090
|
+
written.add(s);
|
|
2091
|
+
this.cat = up.before;
|
|
2092
|
+
this.upgrade = null;
|
|
2093
|
+
const hosts = new Map;
|
|
2094
|
+
for (const s of [...this.stores.values(), ...up.deleted]) {
|
|
2095
|
+
const def = this.cat.stores.find((d) => d.id === s.def.id);
|
|
2096
|
+
if (def && s.db) {
|
|
2097
|
+
s.def = def;
|
|
2098
|
+
hosts.set(def.name, s);
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
2101
|
+
this.stores = hosts;
|
|
2102
|
+
}
|
|
2103
|
+
for (const s of written) {
|
|
2104
|
+
if (!s.db)
|
|
2105
|
+
continue;
|
|
2106
|
+
s.clearPending();
|
|
2107
|
+
s.engine.rollback();
|
|
2108
|
+
s.loadMeta();
|
|
2109
|
+
s.written = false;
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
}
|
|
2113
|
+
var hosts = new Map;
|
|
2114
|
+
async function host(name) {
|
|
2115
|
+
let h = hosts.get(name);
|
|
2116
|
+
if (!h) {
|
|
2117
|
+
h = new DatabaseHost(name);
|
|
2118
|
+
hosts.set(name, h);
|
|
2119
|
+
}
|
|
2120
|
+
await h.open();
|
|
2121
|
+
return h;
|
|
2122
|
+
}
|
|
2123
|
+
async function call(m) {
|
|
2124
|
+
switch (m.t) {
|
|
2125
|
+
case "open": {
|
|
2126
|
+
const h = await host(m.db);
|
|
2127
|
+
if (h.closeTimer !== null)
|
|
2128
|
+
clearTimeout(h.closeTimer);
|
|
2129
|
+
h.closeTimer = null;
|
|
2130
|
+
h.conns++;
|
|
2131
|
+
return { version: h.cat.version, nextId: h.cat.nextId, stores: h.cat.stores };
|
|
2132
|
+
}
|
|
2133
|
+
case "close": {
|
|
2134
|
+
const h = hosts.get(m.db);
|
|
2135
|
+
if (h && --h.conns <= 0) {
|
|
2136
|
+
h.closeTimer = setTimeout(() => {
|
|
2137
|
+
h.closeTimer = null;
|
|
2138
|
+
if (h.conns > 0 || hosts.get(m.db) !== h)
|
|
2139
|
+
return;
|
|
2140
|
+
h.close();
|
|
2141
|
+
hosts.delete(m.db);
|
|
2142
|
+
}, CLOSE_DELAY_MS);
|
|
2143
|
+
}
|
|
2144
|
+
return null;
|
|
2145
|
+
}
|
|
2146
|
+
case "budget":
|
|
2147
|
+
await wasmReady;
|
|
2148
|
+
setMemoryBudget(m.bytes ?? Infinity);
|
|
2149
|
+
return memoryBudget();
|
|
2150
|
+
case "stats": {
|
|
2151
|
+
await wasmReady;
|
|
2152
|
+
const out = { wasmMemoryBytes: memory?.buffer.byteLength ?? 0, budgetBytes: memoryBudget(), cachedBytes: cachedBytes() };
|
|
2153
|
+
const h = m.db === undefined ? undefined : hosts.get(m.db);
|
|
2154
|
+
if (h?.dir) {
|
|
2155
|
+
const files = {};
|
|
2156
|
+
let used = 0;
|
|
2157
|
+
if (h.catalogAccess)
|
|
2158
|
+
files.catalog = h.catalogAccess.getSize();
|
|
2159
|
+
used += h.catalogDb?.usedBytes() ?? files.catalog ?? 0;
|
|
2160
|
+
for (const s of h.stores.values()) {
|
|
2161
|
+
if (!s.access || !s.db)
|
|
2162
|
+
continue;
|
|
2163
|
+
files[s.def.file] = s.access.getSize();
|
|
2164
|
+
used += s.db.usedBytes();
|
|
2165
|
+
}
|
|
2166
|
+
out.files = files;
|
|
2167
|
+
out.fileBytes = Object.values(files).reduce((a, b) => a + b, 0);
|
|
2168
|
+
out.usedBytes = used;
|
|
2169
|
+
}
|
|
2170
|
+
return out;
|
|
2171
|
+
}
|
|
2172
|
+
case "compact": {
|
|
2173
|
+
const h = hosts.get(m.db);
|
|
2174
|
+
if (!h?.dir)
|
|
2175
|
+
throw new IdbError("InvalidStateError", `plateau: database ${m.db} is not open`);
|
|
2176
|
+
if (h.txStores.size || h.upgrade)
|
|
2177
|
+
throw new IdbError("InvalidStateError", "plateau: a readwrite transaction is running");
|
|
2178
|
+
for (const s of h.stores.values())
|
|
2179
|
+
if (s.db)
|
|
2180
|
+
s.engine.compact();
|
|
2181
|
+
return null;
|
|
2182
|
+
}
|
|
2183
|
+
case "delete": {
|
|
2184
|
+
let version = 0;
|
|
2185
|
+
try {
|
|
2186
|
+
const h = await host(m.db);
|
|
2187
|
+
if (h.closeTimer !== null)
|
|
2188
|
+
clearTimeout(h.closeTimer);
|
|
2189
|
+
version = h.cat.version;
|
|
2190
|
+
} catch (e) {
|
|
2191
|
+
console.warn(`plateau: deleting ${m.db}, which does not open: ${e instanceof Error ? e.message : String(e)}`);
|
|
2192
|
+
}
|
|
2193
|
+
hosts.get(m.db)?.close();
|
|
2194
|
+
hosts.delete(m.db);
|
|
2195
|
+
const dir = await dbDir(m.db);
|
|
2196
|
+
files.delete(dir);
|
|
2197
|
+
dirs.delete(m.db);
|
|
2198
|
+
await (await rootDir()).removeEntry(dirName(m.db), { recursive: true }).catch(() => {});
|
|
2199
|
+
return version;
|
|
2200
|
+
}
|
|
2201
|
+
}
|
|
2202
|
+
}
|
|
2203
|
+
function txIsAsync(m) {
|
|
2204
|
+
const h = hosts.get(m.db);
|
|
2205
|
+
if (!h)
|
|
2206
|
+
return false;
|
|
2207
|
+
return m.begin !== undefined && (m.begin.mode === "versionchange" || h.beginIsAsync(m.begin)) || m.ops.some((op) => op.o === "mkStore" || op.o === "mkIndex" || op.o === "rmIndex");
|
|
2208
|
+
}
|
|
2209
|
+
async function txAsync(m) {
|
|
2210
|
+
const h = hosts.get(m.db);
|
|
2211
|
+
if (!h)
|
|
2212
|
+
throw new IdbError("InvalidStateError", `plateau: database ${m.db} is not open`);
|
|
2213
|
+
if (m.begin)
|
|
2214
|
+
await h.begin(m.tx, m.begin);
|
|
2215
|
+
const res = [];
|
|
2216
|
+
const errs = [];
|
|
2217
|
+
for (const op of m.ops) {
|
|
2218
|
+
if (op.o !== "mkStore" && op.o !== "mkIndex" && op.o !== "rmIndex")
|
|
2219
|
+
h.run(m.tx, op, res, errs);
|
|
2220
|
+
else {
|
|
2221
|
+
try {
|
|
2222
|
+
if (op.o === "mkStore")
|
|
2223
|
+
await h.createStore(m.tx, op.def);
|
|
2224
|
+
else
|
|
2225
|
+
await h.changeIndex(m.tx, op);
|
|
2226
|
+
} catch (e) {
|
|
2227
|
+
const x = e instanceof IdbError ? e : new IdbError("UnknownError", e instanceof Error ? e.message : String(e));
|
|
2228
|
+
errs.push([res.length, x.name, x.message]);
|
|
2229
|
+
}
|
|
2230
|
+
res.push(undefined);
|
|
2231
|
+
}
|
|
2232
|
+
}
|
|
2233
|
+
return finishTx(h, m, res, errs);
|
|
2234
|
+
}
|
|
2235
|
+
function txSync(m) {
|
|
2236
|
+
const h = hosts.get(m.db);
|
|
2237
|
+
if (!h)
|
|
2238
|
+
throw new IdbError("InvalidStateError", `plateau: database ${m.db} is not open`);
|
|
2239
|
+
if (m.begin && m.begin.mode !== "readonly")
|
|
2240
|
+
h.txStores.set(m.tx, new Set);
|
|
2241
|
+
const res = [];
|
|
2242
|
+
const errs = [];
|
|
2243
|
+
for (const op of m.ops)
|
|
2244
|
+
h.run(m.tx, op, res, errs);
|
|
2245
|
+
return finishTx(h, m, res, errs);
|
|
2246
|
+
}
|
|
2247
|
+
function finishTx(h, m, res, errs) {
|
|
2248
|
+
if (errs.length && h.txStores.has(m.tx))
|
|
2249
|
+
h.failed.add(m.tx);
|
|
2250
|
+
const reply = { t: "tx", tx: m.tx, res, errs };
|
|
2251
|
+
if (m.end === "commitIfClean" && h.failed.has(m.tx)) {
|
|
2252
|
+
reply.end = { ok: false, name: "Deferred", message: "a request failed" };
|
|
2253
|
+
return reply;
|
|
2254
|
+
}
|
|
2255
|
+
if (m.end)
|
|
2256
|
+
h.failed.delete(m.tx);
|
|
2257
|
+
if (m.end === "abort") {
|
|
2258
|
+
h.abort(m.tx);
|
|
2259
|
+
reply.end = { ok: true };
|
|
2260
|
+
} else if (m.end) {
|
|
2261
|
+
try {
|
|
2262
|
+
h.commit(m.tx);
|
|
2263
|
+
reply.end = { ok: true };
|
|
2264
|
+
} catch (e) {
|
|
2265
|
+
h.abort(m.tx);
|
|
2266
|
+
const err = e instanceof IdbError ? e : new IdbError("UnknownError", e instanceof Error ? e.message : String(e));
|
|
2267
|
+
reply.end = { ok: false, name: err.name, message: err.message };
|
|
2268
|
+
}
|
|
2269
|
+
}
|
|
2270
|
+
return reply;
|
|
2271
|
+
}
|
|
2272
|
+
function failure(err) {
|
|
2273
|
+
if (err instanceof IdbError)
|
|
2274
|
+
return err;
|
|
2275
|
+
console.error(err);
|
|
2276
|
+
return new IdbError("UnknownError", err instanceof Error ? err.message : String(err));
|
|
2277
|
+
}
|
|
2278
|
+
function txFailed(m, err) {
|
|
2279
|
+
const x = failure(err);
|
|
2280
|
+
return { t: "tx", tx: m.tx, res: [], errs: [], end: { ok: false, name: x.name, message: x.message } };
|
|
2281
|
+
}
|
|
2282
|
+
var post = (m) => self.postMessage(m);
|
|
2283
|
+
function handle(m) {
|
|
2284
|
+
if (m.t === "tx") {
|
|
2285
|
+
if (!txIsAsync(m) && memory) {
|
|
2286
|
+
let reply;
|
|
2287
|
+
try {
|
|
2288
|
+
reply = txSync(m);
|
|
2289
|
+
} catch (err) {
|
|
2290
|
+
reply = txFailed(m, err);
|
|
2291
|
+
}
|
|
2292
|
+
post(reply);
|
|
2293
|
+
return;
|
|
2294
|
+
}
|
|
2295
|
+
return txAsync(m).then(post, (err) => post(txFailed(m, err)));
|
|
2296
|
+
}
|
|
2297
|
+
return call(m).then((value) => post({ t: "call", id: m.id, ok: true, value }), (err) => {
|
|
2298
|
+
const x = failure(err);
|
|
2299
|
+
post({ t: "call", id: m.id, ok: false, name: x.name, message: x.message });
|
|
2300
|
+
});
|
|
2301
|
+
}
|
|
2302
|
+
var busy = false;
|
|
2303
|
+
var queue = [];
|
|
2304
|
+
function drain() {
|
|
2305
|
+
while (!busy && queue.length) {
|
|
2306
|
+
const r = handle(queue.shift());
|
|
2307
|
+
if (r) {
|
|
2308
|
+
busy = true;
|
|
2309
|
+
r.finally(() => {
|
|
2310
|
+
busy = false;
|
|
2311
|
+
drain();
|
|
2312
|
+
});
|
|
2313
|
+
}
|
|
2314
|
+
}
|
|
2315
|
+
}
|
|
2316
|
+
self.onmessage = (e) => {
|
|
2317
|
+
const m = e.data;
|
|
2318
|
+
if (m.t === "init") {
|
|
2319
|
+
memory = initSync({ module: m.module }).memory;
|
|
2320
|
+
setMemoryBudget(defaultBudget());
|
|
2321
|
+
markReady();
|
|
2322
|
+
post({ t: "init" });
|
|
2323
|
+
return;
|
|
2324
|
+
}
|
|
2325
|
+
queue.push(m);
|
|
2326
|
+
drain();
|
|
2327
|
+
};
|