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
package/dist/index.js
ADDED
|
@@ -0,0 +1,2766 @@
|
|
|
1
|
+
// src/events.ts
|
|
2
|
+
class PlateauEvent {
|
|
3
|
+
type;
|
|
4
|
+
bubbles;
|
|
5
|
+
cancelable;
|
|
6
|
+
isTrusted = true;
|
|
7
|
+
target = null;
|
|
8
|
+
currentTarget = null;
|
|
9
|
+
eventPhase = 0;
|
|
10
|
+
defaultPrevented = false;
|
|
11
|
+
stopped = false;
|
|
12
|
+
stoppedNow = false;
|
|
13
|
+
constructor(type, init = {}) {
|
|
14
|
+
this.type = type;
|
|
15
|
+
this.bubbles = !!init.bubbles;
|
|
16
|
+
this.cancelable = !!init.cancelable;
|
|
17
|
+
}
|
|
18
|
+
preventDefault() {
|
|
19
|
+
if (this.cancelable)
|
|
20
|
+
this.defaultPrevented = true;
|
|
21
|
+
}
|
|
22
|
+
stopPropagation() {
|
|
23
|
+
this.stopped = true;
|
|
24
|
+
}
|
|
25
|
+
stopImmediatePropagation() {
|
|
26
|
+
this.stopped = true;
|
|
27
|
+
this.stoppedNow = true;
|
|
28
|
+
}
|
|
29
|
+
get timeStamp() {
|
|
30
|
+
return performance.now();
|
|
31
|
+
}
|
|
32
|
+
get cancelBubble() {
|
|
33
|
+
return this.stopped;
|
|
34
|
+
}
|
|
35
|
+
get returnValue() {
|
|
36
|
+
return !this.defaultPrevented;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class PlateauVersionChangeEvent extends PlateauEvent {
|
|
41
|
+
oldVersion;
|
|
42
|
+
newVersion;
|
|
43
|
+
constructor(type, init = {}) {
|
|
44
|
+
super(type);
|
|
45
|
+
this.oldVersion = init.oldVersion ?? 0;
|
|
46
|
+
this.newVersion = init.newVersion ?? null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class PlateauTarget {
|
|
51
|
+
entries = null;
|
|
52
|
+
slots = null;
|
|
53
|
+
parentTarget() {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
addEventListener(type, fn, opts) {
|
|
57
|
+
if (!fn)
|
|
58
|
+
return;
|
|
59
|
+
const capture = typeof opts === "boolean" ? opts : !!opts?.capture;
|
|
60
|
+
const once = typeof opts === "object" && !!opts?.once;
|
|
61
|
+
this.entries ??= [];
|
|
62
|
+
if (this.entries.some((e) => !e.removed && e.type === type && e.fn === fn && e.capture === capture))
|
|
63
|
+
return;
|
|
64
|
+
this.entries.push({ type, capture, once, fn });
|
|
65
|
+
}
|
|
66
|
+
removeEventListener(type, fn, opts) {
|
|
67
|
+
const capture = typeof opts === "boolean" ? opts : !!opts?.capture;
|
|
68
|
+
if (!this.entries)
|
|
69
|
+
return;
|
|
70
|
+
const i = this.entries.findIndex((e) => !e.removed && e.type === type && e.fn === fn && e.capture === capture);
|
|
71
|
+
if (i >= 0) {
|
|
72
|
+
this.entries[i].removed = true;
|
|
73
|
+
this.entries.splice(i, 1);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
getHandler(type) {
|
|
77
|
+
return this.slots?.[type] ?? null;
|
|
78
|
+
}
|
|
79
|
+
setHandler(type, fn) {
|
|
80
|
+
const f = typeof fn === "function" ? fn : null;
|
|
81
|
+
this.entries ??= [];
|
|
82
|
+
this.slots ??= {};
|
|
83
|
+
if (f) {
|
|
84
|
+
let has = false;
|
|
85
|
+
for (const e of this.entries)
|
|
86
|
+
if (e.slot === type)
|
|
87
|
+
has = true;
|
|
88
|
+
if (!has)
|
|
89
|
+
this.entries.push({ type, capture: false, once: false, fn: null, slot: type });
|
|
90
|
+
} else {
|
|
91
|
+
const i = this.entries.findIndex((e) => e.slot === type);
|
|
92
|
+
if (i >= 0) {
|
|
93
|
+
this.entries[i].removed = true;
|
|
94
|
+
this.entries.splice(i, 1);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
this.slots[type] = f;
|
|
98
|
+
}
|
|
99
|
+
hasListeners() {
|
|
100
|
+
return this.entries !== null && this.entries.length > 0;
|
|
101
|
+
}
|
|
102
|
+
listensOnPath(type, bubbles) {
|
|
103
|
+
const entries = this.entries;
|
|
104
|
+
if (entries === null)
|
|
105
|
+
return false;
|
|
106
|
+
for (const e of entries)
|
|
107
|
+
if (e.type === type && (bubbles || e.capture))
|
|
108
|
+
return true;
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
dispatchEvent(e) {
|
|
112
|
+
dispatch(this, e);
|
|
113
|
+
return !e.defaultPrevented;
|
|
114
|
+
}
|
|
115
|
+
invoke(e, phase, out) {
|
|
116
|
+
const entries = this.entries;
|
|
117
|
+
if (entries === null || entries.length === 0)
|
|
118
|
+
return;
|
|
119
|
+
let only = null;
|
|
120
|
+
let matches = 0;
|
|
121
|
+
for (const x of entries) {
|
|
122
|
+
if (x.type === e.type) {
|
|
123
|
+
matches++;
|
|
124
|
+
only = x;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (matches === 0)
|
|
128
|
+
return;
|
|
129
|
+
if (matches !== 1)
|
|
130
|
+
only = null;
|
|
131
|
+
if (only !== null && only.slot !== undefined && phase !== "capture") {
|
|
132
|
+
const fn = this.slots[only.slot];
|
|
133
|
+
if (fn) {
|
|
134
|
+
try {
|
|
135
|
+
fn.call(this, e);
|
|
136
|
+
} catch (err) {
|
|
137
|
+
out.threw = true;
|
|
138
|
+
report(err);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const list = entries.filter((x) => x.type === e.type && (phase === "target" || x.capture === (phase === "capture")));
|
|
144
|
+
for (const x of list) {
|
|
145
|
+
if (x.removed)
|
|
146
|
+
continue;
|
|
147
|
+
if (x.once)
|
|
148
|
+
this.removeEventListener(x.type, x.fn, x.capture);
|
|
149
|
+
const fn = x.slot ? this.slots[x.slot] : x.fn;
|
|
150
|
+
if (!fn)
|
|
151
|
+
continue;
|
|
152
|
+
try {
|
|
153
|
+
if (typeof fn === "function")
|
|
154
|
+
fn.call(this, e);
|
|
155
|
+
else
|
|
156
|
+
fn.handleEvent(e);
|
|
157
|
+
} catch (err) {
|
|
158
|
+
out.threw = true;
|
|
159
|
+
report(err);
|
|
160
|
+
}
|
|
161
|
+
if (e.stoppedNow)
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function report(err) {
|
|
167
|
+
const g = globalThis;
|
|
168
|
+
if (g.reportError)
|
|
169
|
+
g.reportError(err);
|
|
170
|
+
else
|
|
171
|
+
setTimeout(() => {
|
|
172
|
+
throw err;
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
function dispatch(target, e) {
|
|
176
|
+
const p1 = target.parentTarget();
|
|
177
|
+
const p2 = p1 === null ? null : p1.parentTarget();
|
|
178
|
+
if ((p2 === null || p2.parentTarget() === null) && (p1 === null || !p1.listensOnPath(e.type, e.bubbles)) && (p2 === null || !p2.listensOnPath(e.type, e.bubbles))) {
|
|
179
|
+
const out = { threw: false };
|
|
180
|
+
e.target = target;
|
|
181
|
+
e.eventPhase = 2;
|
|
182
|
+
e.currentTarget = target;
|
|
183
|
+
target.invoke(e, "target", out);
|
|
184
|
+
e.eventPhase = 0;
|
|
185
|
+
e.currentTarget = null;
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
188
|
+
const path = [];
|
|
189
|
+
for (let t = target.parentTarget();t; t = t.parentTarget())
|
|
190
|
+
path.push(t);
|
|
191
|
+
const out = { threw: false };
|
|
192
|
+
e.target = target;
|
|
193
|
+
e.eventPhase = 1;
|
|
194
|
+
for (let i = path.length - 1;i >= 0 && !e.stopped; i--) {
|
|
195
|
+
e.currentTarget = path[i];
|
|
196
|
+
path[i].invoke(e, "capture", out);
|
|
197
|
+
}
|
|
198
|
+
if (!e.stopped) {
|
|
199
|
+
e.eventPhase = 2;
|
|
200
|
+
e.currentTarget = target;
|
|
201
|
+
target.invoke(e, "target", out);
|
|
202
|
+
}
|
|
203
|
+
if (e.bubbles) {
|
|
204
|
+
e.eventPhase = 3;
|
|
205
|
+
for (let i = 0;i < path.length && !e.stopped; i++) {
|
|
206
|
+
e.currentTarget = path[i];
|
|
207
|
+
path[i].invoke(e, "bubble", out);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
e.eventPhase = 0;
|
|
211
|
+
e.currentTarget = null;
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
214
|
+
function handlers(cls, types) {
|
|
215
|
+
for (const type of types) {
|
|
216
|
+
Object.defineProperty(cls.prototype, `on${type}`, {
|
|
217
|
+
get() {
|
|
218
|
+
return this.getHandler(type);
|
|
219
|
+
},
|
|
220
|
+
set(fn) {
|
|
221
|
+
this.setHandler(type, fn);
|
|
222
|
+
},
|
|
223
|
+
configurable: true,
|
|
224
|
+
enumerable: true
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// src/key.ts
|
|
230
|
+
var NUMBER = 16;
|
|
231
|
+
var DATE = 32;
|
|
232
|
+
var STRING = 48;
|
|
233
|
+
var BINARY = 64;
|
|
234
|
+
var ARRAY = 80;
|
|
235
|
+
var f64 = new Float64Array(1);
|
|
236
|
+
var u32 = new Uint32Array(f64.buffer);
|
|
237
|
+
var LITTLE = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
238
|
+
var HI = LITTLE ? 1 : 0;
|
|
239
|
+
var LO = LITTLE ? 0 : 1;
|
|
240
|
+
function encodeNumber(tag, n) {
|
|
241
|
+
f64[0] = n === 0 ? 0 : n;
|
|
242
|
+
let hi = u32[HI];
|
|
243
|
+
let lo = u32[LO];
|
|
244
|
+
if (hi >>> 31) {
|
|
245
|
+
hi = ~hi >>> 0;
|
|
246
|
+
lo = ~lo >>> 0;
|
|
247
|
+
} else {
|
|
248
|
+
hi = (hi | 2147483648) >>> 0;
|
|
249
|
+
}
|
|
250
|
+
return String.fromCharCode(tag, hi >>> 16, hi & 65535, lo >>> 16, lo & 65535);
|
|
251
|
+
}
|
|
252
|
+
function decodeNumber(s, at) {
|
|
253
|
+
let hi = (s.charCodeAt(at) << 16 | s.charCodeAt(at + 1)) >>> 0;
|
|
254
|
+
let lo = (s.charCodeAt(at + 2) << 16 | s.charCodeAt(at + 3)) >>> 0;
|
|
255
|
+
if (hi >>> 31) {
|
|
256
|
+
hi = (hi & 2147483647) >>> 0;
|
|
257
|
+
} else {
|
|
258
|
+
hi = ~hi >>> 0;
|
|
259
|
+
lo = ~lo >>> 0;
|
|
260
|
+
}
|
|
261
|
+
u32[HI] = hi;
|
|
262
|
+
u32[LO] = lo;
|
|
263
|
+
return f64[0];
|
|
264
|
+
}
|
|
265
|
+
function isBinary(v) {
|
|
266
|
+
return v instanceof ArrayBuffer || ArrayBuffer.isView(v) || Object.prototype.toString.call(v) === "[object ArrayBuffer]";
|
|
267
|
+
}
|
|
268
|
+
function bytesOf(v) {
|
|
269
|
+
return ArrayBuffer.isView(v) ? new Uint8Array(v.buffer, v.byteOffset, v.byteLength) : new Uint8Array(v);
|
|
270
|
+
}
|
|
271
|
+
function isDate(v) {
|
|
272
|
+
return v instanceof Date || Object.prototype.toString.call(v) === "[object Date]";
|
|
273
|
+
}
|
|
274
|
+
function encodeKey(v) {
|
|
275
|
+
return enc(v, null);
|
|
276
|
+
}
|
|
277
|
+
function enc(v, seen) {
|
|
278
|
+
switch (typeof v) {
|
|
279
|
+
case "number":
|
|
280
|
+
return Number.isNaN(v) ? undefined : encodeNumber(NUMBER, v);
|
|
281
|
+
case "string": {
|
|
282
|
+
let out = String.fromCharCode(STRING);
|
|
283
|
+
let from = 0;
|
|
284
|
+
for (let i = 0;i < v.length; i++) {
|
|
285
|
+
const c = v.charCodeAt(i);
|
|
286
|
+
if (c < 2) {
|
|
287
|
+
out += v.slice(from, i) + String.fromCharCode(1, c + 1);
|
|
288
|
+
from = i + 1;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return out + v.slice(from) + "\x00";
|
|
292
|
+
}
|
|
293
|
+
case "object": {
|
|
294
|
+
if (v === null)
|
|
295
|
+
return;
|
|
296
|
+
if (isDate(v)) {
|
|
297
|
+
const t = v.getTime();
|
|
298
|
+
return Number.isNaN(t) ? undefined : encodeNumber(DATE, t);
|
|
299
|
+
}
|
|
300
|
+
if (isBinary(v)) {
|
|
301
|
+
const b = bytesOf(v);
|
|
302
|
+
const units = new Array(b.length);
|
|
303
|
+
for (let i = 0;i < b.length; i++)
|
|
304
|
+
units[i] = b[i] + 2;
|
|
305
|
+
let out = String.fromCharCode(BINARY);
|
|
306
|
+
for (let i = 0;i < units.length; i += 8192)
|
|
307
|
+
out += String.fromCharCode(...units.slice(i, i + 8192));
|
|
308
|
+
return out + "\x00";
|
|
309
|
+
}
|
|
310
|
+
if (Array.isArray(v)) {
|
|
311
|
+
const stack = seen ?? [];
|
|
312
|
+
if (stack.includes(v))
|
|
313
|
+
return;
|
|
314
|
+
stack.push(v);
|
|
315
|
+
let out = String.fromCharCode(ARRAY);
|
|
316
|
+
for (let i = 0;i < v.length; i++) {
|
|
317
|
+
if (!Object.prototype.hasOwnProperty.call(v, i))
|
|
318
|
+
return;
|
|
319
|
+
const e = enc(v[i], stack);
|
|
320
|
+
if (e === undefined)
|
|
321
|
+
return;
|
|
322
|
+
out += e;
|
|
323
|
+
}
|
|
324
|
+
stack.pop();
|
|
325
|
+
return out + "\x00";
|
|
326
|
+
}
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
default:
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
function decodeKeyAt(s, at) {
|
|
334
|
+
const tag = s.charCodeAt(at);
|
|
335
|
+
switch (tag) {
|
|
336
|
+
case NUMBER:
|
|
337
|
+
return [decodeNumber(s, at + 1), at + 5];
|
|
338
|
+
case DATE:
|
|
339
|
+
return [new Date(decodeNumber(s, at + 1)), at + 5];
|
|
340
|
+
case STRING: {
|
|
341
|
+
let out = "";
|
|
342
|
+
let i = at + 1;
|
|
343
|
+
let from = i;
|
|
344
|
+
for (;; ) {
|
|
345
|
+
const c = s.charCodeAt(i);
|
|
346
|
+
if (c === 0)
|
|
347
|
+
return [out + s.slice(from, i), i + 1];
|
|
348
|
+
if (c === 1) {
|
|
349
|
+
out += s.slice(from, i) + String.fromCharCode(s.charCodeAt(i + 1) - 1);
|
|
350
|
+
i += 2;
|
|
351
|
+
from = i;
|
|
352
|
+
} else
|
|
353
|
+
i++;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
case BINARY: {
|
|
357
|
+
const end = s.indexOf("\x00", at + 1);
|
|
358
|
+
const b = new Uint8Array(end - at - 1);
|
|
359
|
+
for (let i = 0;i < b.length; i++)
|
|
360
|
+
b[i] = s.charCodeAt(at + 1 + i) - 2;
|
|
361
|
+
return [b.buffer, end + 1];
|
|
362
|
+
}
|
|
363
|
+
case ARRAY: {
|
|
364
|
+
const out = [];
|
|
365
|
+
let i = at + 1;
|
|
366
|
+
while (s.charCodeAt(i) !== 0) {
|
|
367
|
+
const [k, next] = decodeKeyAt(s, i);
|
|
368
|
+
out.push(k);
|
|
369
|
+
i = next;
|
|
370
|
+
}
|
|
371
|
+
return [out, i + 1];
|
|
372
|
+
}
|
|
373
|
+
default:
|
|
374
|
+
throw new Error(`plateau: corrupt key encoding (tag ${tag})`);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
function decodeKey(s) {
|
|
378
|
+
return decodeKeyAt(s, 0)[0];
|
|
379
|
+
}
|
|
380
|
+
function dataError(message) {
|
|
381
|
+
return new DOMException(message, "DataError");
|
|
382
|
+
}
|
|
383
|
+
function requireKey(v, what = "The parameter") {
|
|
384
|
+
const e = encodeKey(v);
|
|
385
|
+
if (e === undefined)
|
|
386
|
+
throw dataError(`${what} is not a valid key.`);
|
|
387
|
+
return e;
|
|
388
|
+
}
|
|
389
|
+
function cmp(a, b) {
|
|
390
|
+
const x = requireKey(a, "The first parameter");
|
|
391
|
+
const y = requireKey(b, "The second parameter");
|
|
392
|
+
return x < y ? -1 : x > y ? 1 : 0;
|
|
393
|
+
}
|
|
394
|
+
var IDENT = /^[$_\p{ID_Start}][$\p{ID_Continue}]*$/u;
|
|
395
|
+
function validPathString(p) {
|
|
396
|
+
return p === "" || p.split(".").every((part) => IDENT.test(part));
|
|
397
|
+
}
|
|
398
|
+
function validKeyPath(p) {
|
|
399
|
+
if (typeof p === "string")
|
|
400
|
+
return validPathString(p);
|
|
401
|
+
if (Array.isArray(p))
|
|
402
|
+
return p.length > 0 && p.every((x) => typeof x === "string" && validPathString(x));
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
function normalizeKeyPath(p) {
|
|
406
|
+
if (p === null || p === undefined)
|
|
407
|
+
return p;
|
|
408
|
+
if (Array.isArray(p))
|
|
409
|
+
return p.map((x) => String(x));
|
|
410
|
+
if (typeof p === "object" && typeof p[Symbol.iterator] === "function") {
|
|
411
|
+
return Array.from(p, (x) => String(x));
|
|
412
|
+
}
|
|
413
|
+
return String(p);
|
|
414
|
+
}
|
|
415
|
+
var FAIL = Symbol("no value at key path");
|
|
416
|
+
function evalPath(value, path) {
|
|
417
|
+
if (path === "")
|
|
418
|
+
return value;
|
|
419
|
+
let v = value;
|
|
420
|
+
for (const id of path.split(".")) {
|
|
421
|
+
if (typeof v === "string" && id === "length") {
|
|
422
|
+
v = v.length;
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
if (v === null || typeof v !== "object")
|
|
426
|
+
return FAIL;
|
|
427
|
+
if (Array.isArray(v) && id === "length") {
|
|
428
|
+
v = v.length;
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
if (!Object.prototype.hasOwnProperty.call(v, id))
|
|
432
|
+
return FAIL;
|
|
433
|
+
v = v[id];
|
|
434
|
+
}
|
|
435
|
+
return v;
|
|
436
|
+
}
|
|
437
|
+
function extractKey(value, path) {
|
|
438
|
+
if (Array.isArray(path)) {
|
|
439
|
+
const parts = [];
|
|
440
|
+
for (const p of path) {
|
|
441
|
+
const v = evalPath(value, p);
|
|
442
|
+
if (v === FAIL)
|
|
443
|
+
return "invalid";
|
|
444
|
+
parts.push(v);
|
|
445
|
+
}
|
|
446
|
+
const key = encodeKey(parts);
|
|
447
|
+
return key === undefined ? "invalid" : { key };
|
|
448
|
+
}
|
|
449
|
+
const v = evalPath(value, path);
|
|
450
|
+
if (v === FAIL)
|
|
451
|
+
return "missing";
|
|
452
|
+
const key = encodeKey(v);
|
|
453
|
+
return key === undefined ? "invalid" : { key };
|
|
454
|
+
}
|
|
455
|
+
function canInjectKey(value, path) {
|
|
456
|
+
const ids = path.split(".");
|
|
457
|
+
let v = value;
|
|
458
|
+
for (let i = 0;i < ids.length - 1; i++) {
|
|
459
|
+
if (v === null || typeof v !== "object")
|
|
460
|
+
return false;
|
|
461
|
+
if (!Object.prototype.hasOwnProperty.call(v, ids[i]))
|
|
462
|
+
return true;
|
|
463
|
+
v = v[ids[i]];
|
|
464
|
+
}
|
|
465
|
+
return v !== null && typeof v === "object";
|
|
466
|
+
}
|
|
467
|
+
class PlateauKeyRange {
|
|
468
|
+
lo;
|
|
469
|
+
hi;
|
|
470
|
+
lowerOpen;
|
|
471
|
+
upperOpen;
|
|
472
|
+
constructor(lo, hi, lowerOpen, upperOpen) {
|
|
473
|
+
this.lo = lo;
|
|
474
|
+
this.hi = hi;
|
|
475
|
+
this.lowerOpen = lowerOpen;
|
|
476
|
+
this.upperOpen = upperOpen;
|
|
477
|
+
}
|
|
478
|
+
get lower() {
|
|
479
|
+
return this.lo === undefined ? undefined : decodeKey(this.lo);
|
|
480
|
+
}
|
|
481
|
+
get upper() {
|
|
482
|
+
return this.hi === undefined ? undefined : decodeKey(this.hi);
|
|
483
|
+
}
|
|
484
|
+
static only(value) {
|
|
485
|
+
const k = requireKey(value);
|
|
486
|
+
return new PlateauKeyRange(k, k, false, false);
|
|
487
|
+
}
|
|
488
|
+
static lowerBound(lower, open = false) {
|
|
489
|
+
return new PlateauKeyRange(requireKey(lower), undefined, !!open, true);
|
|
490
|
+
}
|
|
491
|
+
static upperBound(upper, open = false) {
|
|
492
|
+
return new PlateauKeyRange(undefined, requireKey(upper), true, !!open);
|
|
493
|
+
}
|
|
494
|
+
static bound(lower, upper, lowerOpen = false, upperOpen = false) {
|
|
495
|
+
const lo = requireKey(lower);
|
|
496
|
+
const hi = requireKey(upper);
|
|
497
|
+
if (lo > hi || lo === hi && (lowerOpen || upperOpen))
|
|
498
|
+
throw dataError("The lower key is greater than the upper key.");
|
|
499
|
+
return new PlateauKeyRange(lo, hi, !!lowerOpen, !!upperOpen);
|
|
500
|
+
}
|
|
501
|
+
includes(key) {
|
|
502
|
+
return inRange(this.wire(), requireKey(key));
|
|
503
|
+
}
|
|
504
|
+
wire() {
|
|
505
|
+
return { lo: this.lo, hi: this.hi, loOpen: this.lowerOpen, hiOpen: this.upperOpen };
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
function inRange(r, k) {
|
|
509
|
+
if (r.lo !== undefined && (r.loOpen ? k <= r.lo : k < r.lo))
|
|
510
|
+
return false;
|
|
511
|
+
if (r.hi !== undefined && (r.hiOpen ? k >= r.hi : k > r.hi))
|
|
512
|
+
return false;
|
|
513
|
+
return true;
|
|
514
|
+
}
|
|
515
|
+
function toRange(q, nullOk) {
|
|
516
|
+
if (q === undefined || q === null) {
|
|
517
|
+
if (nullOk)
|
|
518
|
+
return null;
|
|
519
|
+
throw dataError("No key or key range specified.");
|
|
520
|
+
}
|
|
521
|
+
if (q instanceof PlateauKeyRange)
|
|
522
|
+
return q.wire();
|
|
523
|
+
if (typeof IDBKeyRange !== "undefined" && q instanceof IDBKeyRange) {
|
|
524
|
+
return {
|
|
525
|
+
lo: q.lower === undefined ? undefined : requireKey(q.lower),
|
|
526
|
+
hi: q.upper === undefined ? undefined : requireKey(q.upper),
|
|
527
|
+
loOpen: q.lowerOpen,
|
|
528
|
+
hiOpen: q.upperOpen
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
const k = requireKey(q);
|
|
532
|
+
return { lo: k, hi: k, loOpen: false, hiOpen: false };
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// src/scheduler.ts
|
|
536
|
+
class Scheduler {
|
|
537
|
+
live = [];
|
|
538
|
+
add(tx) {
|
|
539
|
+
this.live.push(tx);
|
|
540
|
+
this.pump();
|
|
541
|
+
}
|
|
542
|
+
done(tx) {
|
|
543
|
+
const i = this.live.indexOf(tx);
|
|
544
|
+
if (i >= 0)
|
|
545
|
+
this.live.splice(i, 1);
|
|
546
|
+
this.pump();
|
|
547
|
+
}
|
|
548
|
+
pump() {
|
|
549
|
+
for (let i = 0;i < this.live.length; i++) {
|
|
550
|
+
const tx = this.live[i];
|
|
551
|
+
if (tx._started || tx._state === "finished")
|
|
552
|
+
continue;
|
|
553
|
+
let ok = true;
|
|
554
|
+
for (let j = 0;j < i && ok; j++) {
|
|
555
|
+
const t = this.live[j];
|
|
556
|
+
if (t._state === "finished")
|
|
557
|
+
continue;
|
|
558
|
+
const both = t.mode === "readonly" && tx.mode === "readonly";
|
|
559
|
+
if (!both && t._scope.some((s) => tx._scope.includes(s)))
|
|
560
|
+
ok = false;
|
|
561
|
+
}
|
|
562
|
+
if (ok)
|
|
563
|
+
tx._start();
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
var schedulers = new Map;
|
|
568
|
+
function scheduler(name) {
|
|
569
|
+
let s = schedulers.get(name);
|
|
570
|
+
if (!s)
|
|
571
|
+
schedulers.set(name, s = new Scheduler);
|
|
572
|
+
return s;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// src/runtime.ts
|
|
576
|
+
var WASM_URL = new URL("./plateau_engine_bg.wasm", import.meta.url).href;
|
|
577
|
+
var LEADER = "plateau/leader";
|
|
578
|
+
var ALIVE = "plateau/alive/";
|
|
579
|
+
var HUB = "plateau/hub";
|
|
580
|
+
var TAB = "plateau/tab/";
|
|
581
|
+
var versionChangeHook = () => null;
|
|
582
|
+
function onVersionChange(hook) {
|
|
583
|
+
versionChangeHook = hook;
|
|
584
|
+
}
|
|
585
|
+
var changeHook = () => {};
|
|
586
|
+
function onChange(hook) {
|
|
587
|
+
changeHook = hook;
|
|
588
|
+
}
|
|
589
|
+
var forever = () => new Promise(() => {});
|
|
590
|
+
function count(m, key, by) {
|
|
591
|
+
const n = (m.get(key) ?? 0) + by;
|
|
592
|
+
if (n > 0)
|
|
593
|
+
m.set(key, n);
|
|
594
|
+
else
|
|
595
|
+
m.delete(key);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
class OpenQueue {
|
|
599
|
+
busy = false;
|
|
600
|
+
waiting = [];
|
|
601
|
+
add(job, first) {
|
|
602
|
+
if (first)
|
|
603
|
+
this.waiting.unshift(job);
|
|
604
|
+
else
|
|
605
|
+
this.waiting.push(job);
|
|
606
|
+
this.pump();
|
|
607
|
+
}
|
|
608
|
+
pump() {
|
|
609
|
+
if (this.busy)
|
|
610
|
+
return;
|
|
611
|
+
const job = this.waiting.shift();
|
|
612
|
+
if (!job)
|
|
613
|
+
return;
|
|
614
|
+
this.busy = true;
|
|
615
|
+
job().catch((e) => console.error(e)).finally(() => {
|
|
616
|
+
this.busy = false;
|
|
617
|
+
this.pump();
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
class RemoteTx {
|
|
623
|
+
rt;
|
|
624
|
+
f;
|
|
625
|
+
remote;
|
|
626
|
+
id;
|
|
627
|
+
db;
|
|
628
|
+
mode;
|
|
629
|
+
_scope;
|
|
630
|
+
_started = false;
|
|
631
|
+
_state = "running";
|
|
632
|
+
waiting = [];
|
|
633
|
+
constructor(rt, f, remote, id, db, begin) {
|
|
634
|
+
this.rt = rt;
|
|
635
|
+
this.f = f;
|
|
636
|
+
this.remote = remote;
|
|
637
|
+
this.id = id;
|
|
638
|
+
this.db = db;
|
|
639
|
+
this.mode = begin.mode;
|
|
640
|
+
this._scope = [...begin.stores];
|
|
641
|
+
}
|
|
642
|
+
receive(m) {
|
|
643
|
+
const local = { ...m, tx: this.id };
|
|
644
|
+
if (this._started)
|
|
645
|
+
this.rt.worker.postMessage(local);
|
|
646
|
+
else
|
|
647
|
+
this.waiting.push(local);
|
|
648
|
+
}
|
|
649
|
+
_start() {
|
|
650
|
+
this._started = true;
|
|
651
|
+
for (const m of this.waiting)
|
|
652
|
+
this.rt.worker.postMessage(m);
|
|
653
|
+
this.waiting = [];
|
|
654
|
+
}
|
|
655
|
+
_onReply(r) {
|
|
656
|
+
if (!this.f.gone)
|
|
657
|
+
this.f.port.postMessage({ ...r, tx: this.remote });
|
|
658
|
+
if (r.end && (r.end.ok || r.end.name !== "Deferred"))
|
|
659
|
+
this.finish();
|
|
660
|
+
}
|
|
661
|
+
finish() {
|
|
662
|
+
if (this._state === "finished")
|
|
663
|
+
return;
|
|
664
|
+
this._state = "finished";
|
|
665
|
+
this.rt.txs.delete(this.id);
|
|
666
|
+
this.f.txs.delete(this.remote);
|
|
667
|
+
scheduler(this.db).done(this);
|
|
668
|
+
}
|
|
669
|
+
kill() {
|
|
670
|
+
if (this._started && this.mode !== "readonly" && this._state !== "finished") {
|
|
671
|
+
this.rt.worker.postMessage({ t: "tx", tx: this.id, db: this.db, ops: [], end: "abort" });
|
|
672
|
+
}
|
|
673
|
+
this.finish();
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
class Runtime {
|
|
678
|
+
worker = null;
|
|
679
|
+
role = "pending";
|
|
680
|
+
txs = new Map;
|
|
681
|
+
nextTx = 1;
|
|
682
|
+
tab;
|
|
683
|
+
leading;
|
|
684
|
+
becameLeader;
|
|
685
|
+
calls = new Map;
|
|
686
|
+
nextCall = 1;
|
|
687
|
+
held = [];
|
|
688
|
+
begun = new Set;
|
|
689
|
+
lost = new Set;
|
|
690
|
+
opened = new Map;
|
|
691
|
+
leaderTab = null;
|
|
692
|
+
hub = null;
|
|
693
|
+
port = null;
|
|
694
|
+
slots = new Map;
|
|
695
|
+
nextSlot = 1;
|
|
696
|
+
vcs = new Map;
|
|
697
|
+
nextVc = 1;
|
|
698
|
+
seen = new Map;
|
|
699
|
+
followers = new Map;
|
|
700
|
+
relayed = new Map;
|
|
701
|
+
queues = new Map;
|
|
702
|
+
slotHolds = new Map;
|
|
703
|
+
fanouts = new Map;
|
|
704
|
+
constructor(o = {}) {
|
|
705
|
+
this.leading = new Promise((resolve) => this.becameLeader = resolve);
|
|
706
|
+
this.tab = typeof crypto.randomUUID === "function" ? crypto.randomUUID() : `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
707
|
+
this.startWorker(o);
|
|
708
|
+
const locks = typeof navigator !== "undefined" ? navigator.locks : undefined;
|
|
709
|
+
if (!locks || typeof BroadcastChannel === "undefined") {
|
|
710
|
+
this.lead();
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
locks.request(ALIVE + this.tab, () => {
|
|
714
|
+
this.hub = new BroadcastChannel(HUB);
|
|
715
|
+
this.hub.onmessage = (e) => this.onHub(e.data);
|
|
716
|
+
locks.request(LEADER, { ifAvailable: true }, (lock) => {
|
|
717
|
+
if (lock) {
|
|
718
|
+
this.lead();
|
|
719
|
+
return forever();
|
|
720
|
+
}
|
|
721
|
+
this.hub.postMessage({ t: "who" });
|
|
722
|
+
locks.request(LEADER, () => {
|
|
723
|
+
this.lead();
|
|
724
|
+
return forever();
|
|
725
|
+
});
|
|
726
|
+
return;
|
|
727
|
+
});
|
|
728
|
+
return forever();
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
call(m) {
|
|
732
|
+
const id = this.nextCall++;
|
|
733
|
+
return new Promise((resolve, reject) => {
|
|
734
|
+
this.calls.set(id, { m, resolve, reject, sent: false });
|
|
735
|
+
if (m.t === "close")
|
|
736
|
+
count(this.opened, m.db, -1);
|
|
737
|
+
this.send({ ...m, id });
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
post(m, transfer = []) {
|
|
741
|
+
if (this.lost.size && this.lost.has(m.tx))
|
|
742
|
+
return;
|
|
743
|
+
this.send(m, transfer);
|
|
744
|
+
}
|
|
745
|
+
finished(tx) {
|
|
746
|
+
this.txs.delete(tx);
|
|
747
|
+
this.lost.delete(tx);
|
|
748
|
+
if (this.begun.delete(tx) && this.role === "follower")
|
|
749
|
+
this.port.postMessage({ t: "done", tx });
|
|
750
|
+
}
|
|
751
|
+
queue(db, job) {
|
|
752
|
+
const run = (done) => queueMicrotask(() => void job().catch((e) => console.error(e)).finally(done));
|
|
753
|
+
if (this.role === "leader") {
|
|
754
|
+
this.openQueue(db).add(() => new Promise(run), false);
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
const id = this.nextSlot++;
|
|
758
|
+
const s = {
|
|
759
|
+
db,
|
|
760
|
+
held: false,
|
|
761
|
+
go: () => {
|
|
762
|
+
if (s.held)
|
|
763
|
+
return;
|
|
764
|
+
s.held = true;
|
|
765
|
+
run(() => {
|
|
766
|
+
this.slots.delete(id);
|
|
767
|
+
this.toLeader({ t: "slotEnd", id });
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
this.slots.set(id, s);
|
|
772
|
+
this.toLeader({ t: "slot", id, db, held: false });
|
|
773
|
+
}
|
|
774
|
+
changed(db, stores) {
|
|
775
|
+
this.hub?.postMessage({ t: "changed", db, stores });
|
|
776
|
+
}
|
|
777
|
+
versionChange(db, oldVersion, newVersion) {
|
|
778
|
+
if (this.role === "leader" && ![...this.followers.values()].some((f) => f.conns.has(db)))
|
|
779
|
+
return null;
|
|
780
|
+
const id = this.nextVc++;
|
|
781
|
+
let ack;
|
|
782
|
+
let closed;
|
|
783
|
+
const out = { ack: new Promise((r) => ack = r), closed: new Promise((r) => closed = r) };
|
|
784
|
+
this.vcs.set(id, { db, old: oldVersion, new: newVersion, ack, closed });
|
|
785
|
+
this.toLeader({ t: "vc", id, db, old: oldVersion, new: newVersion });
|
|
786
|
+
return out;
|
|
787
|
+
}
|
|
788
|
+
startWorker(o) {
|
|
789
|
+
const worker = o.worker ?? new Worker(new URL("./host.worker.js", import.meta.url), { type: "module" });
|
|
790
|
+
if (!o.worker) {
|
|
791
|
+
const compiled = WebAssembly.compileStreaming(fetch(WASM_URL)).catch(async () => WebAssembly.compile(await (await fetch(WASM_URL)).arrayBuffer()));
|
|
792
|
+
compiled.then((module) => worker.postMessage({ t: "init", module }));
|
|
793
|
+
}
|
|
794
|
+
worker.addEventListener("message", (e) => {
|
|
795
|
+
const m = e.data;
|
|
796
|
+
if (m.t === "call") {
|
|
797
|
+
const r = this.relayed.get(m.id);
|
|
798
|
+
if (r) {
|
|
799
|
+
this.relayed.delete(m.id);
|
|
800
|
+
r(m);
|
|
801
|
+
} else
|
|
802
|
+
this.settle(m);
|
|
803
|
+
} else if (m.t === "tx")
|
|
804
|
+
this.txs.get(m.tx)?._onReply(m);
|
|
805
|
+
});
|
|
806
|
+
this.worker = worker;
|
|
807
|
+
}
|
|
808
|
+
send(m, transfer = []) {
|
|
809
|
+
if (this.role === "leader") {
|
|
810
|
+
this.worker.postMessage(m, transfer);
|
|
811
|
+
} else if (this.role === "follower") {
|
|
812
|
+
if (m.t === "tx") {
|
|
813
|
+
if (m.begin)
|
|
814
|
+
this.begun.add(m.tx);
|
|
815
|
+
} else {
|
|
816
|
+
const c = this.calls.get(m.id);
|
|
817
|
+
if (c)
|
|
818
|
+
c.sent = true;
|
|
819
|
+
}
|
|
820
|
+
this.port.postMessage(m);
|
|
821
|
+
} else
|
|
822
|
+
this.held.push([m, transfer]);
|
|
823
|
+
}
|
|
824
|
+
toLeader(m) {
|
|
825
|
+
if (this.role === "leader")
|
|
826
|
+
this.onLeader(this.tab, m);
|
|
827
|
+
else if (this.role === "follower")
|
|
828
|
+
this.port.postMessage(m);
|
|
829
|
+
}
|
|
830
|
+
settle(r) {
|
|
831
|
+
const c = this.calls.get(r.id);
|
|
832
|
+
if (!c)
|
|
833
|
+
return;
|
|
834
|
+
this.calls.delete(r.id);
|
|
835
|
+
if (r.ok) {
|
|
836
|
+
if (c.m.t === "open")
|
|
837
|
+
count(this.opened, c.m.db, 1);
|
|
838
|
+
c.resolve(r.value);
|
|
839
|
+
} else
|
|
840
|
+
c.reject(new DOMException(r.message, r.name));
|
|
841
|
+
}
|
|
842
|
+
watch(tab, gone) {
|
|
843
|
+
navigator.locks.request(ALIVE + tab, { mode: "shared" }, () => gone());
|
|
844
|
+
}
|
|
845
|
+
onHub(m) {
|
|
846
|
+
if (m.t === "changed")
|
|
847
|
+
changeHook(m.db, m.stores);
|
|
848
|
+
else if (m.t === "who") {
|
|
849
|
+
if (this.role === "leader")
|
|
850
|
+
this.hub.postMessage({ t: "leader", tab: this.tab });
|
|
851
|
+
} else if (m.t === "leader") {
|
|
852
|
+
if (m.tab !== this.tab && this.role !== "leader" && m.tab !== this.leaderTab)
|
|
853
|
+
this.join(m.tab);
|
|
854
|
+
} else if (m.leader === this.tab && this.role === "leader" && !this.followers.has(m.tab)) {
|
|
855
|
+
this.serve(m.tab);
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
join(leader) {
|
|
859
|
+
if (this.leaderTab !== null)
|
|
860
|
+
this.leaderLost();
|
|
861
|
+
this.leaderTab = leader;
|
|
862
|
+
this.worker?.terminate();
|
|
863
|
+
this.worker = null;
|
|
864
|
+
this.port = new BroadcastChannel(TAB + this.tab);
|
|
865
|
+
this.port.onmessage = (e) => this.receive(e.data);
|
|
866
|
+
this.watch(leader, () => {
|
|
867
|
+
if (this.leaderTab === leader)
|
|
868
|
+
this.leaderLost();
|
|
869
|
+
});
|
|
870
|
+
this.hub.postMessage({ t: "hello", tab: this.tab, leader });
|
|
871
|
+
}
|
|
872
|
+
lead() {
|
|
873
|
+
if (this.leaderTab !== null)
|
|
874
|
+
this.leaderLost();
|
|
875
|
+
this.leaderTab = this.tab;
|
|
876
|
+
this.role = "leader";
|
|
877
|
+
this.becameLeader();
|
|
878
|
+
if (!this.worker)
|
|
879
|
+
this.startWorker({});
|
|
880
|
+
this.hub?.postMessage({ t: "leader", tab: this.tab });
|
|
881
|
+
this.flush();
|
|
882
|
+
}
|
|
883
|
+
leaderLost() {
|
|
884
|
+
this.leaderTab = null;
|
|
885
|
+
this.role = "pending";
|
|
886
|
+
this.port?.close();
|
|
887
|
+
this.port = null;
|
|
888
|
+
const begun = [...this.begun];
|
|
889
|
+
this.begun.clear();
|
|
890
|
+
for (const tx of begun) {
|
|
891
|
+
const t = this.txs.get(tx);
|
|
892
|
+
if (!t)
|
|
893
|
+
continue;
|
|
894
|
+
this.lost.add(tx);
|
|
895
|
+
t._onReply({ t: "tx", tx, res: [], errs: [], end: { ok: false, name: "UnknownError", message: "plateau: the tab that ran the database closed during the transaction" } });
|
|
896
|
+
}
|
|
897
|
+
const resend = [];
|
|
898
|
+
for (const [id, c] of this.calls) {
|
|
899
|
+
if (!c.sent)
|
|
900
|
+
continue;
|
|
901
|
+
c.sent = false;
|
|
902
|
+
if (c.m.t === "close") {
|
|
903
|
+
this.calls.delete(id);
|
|
904
|
+
c.resolve(null);
|
|
905
|
+
} else
|
|
906
|
+
resend.push([{ ...c.m, id }, []]);
|
|
907
|
+
}
|
|
908
|
+
this.held = [...resend, ...this.held];
|
|
909
|
+
}
|
|
910
|
+
flush() {
|
|
911
|
+
for (const [db, n] of this.opened)
|
|
912
|
+
for (let i = 0;i < n; i++)
|
|
913
|
+
this.send({ t: "open", db, id: this.nextCall++ });
|
|
914
|
+
for (const [id, s] of this.slots)
|
|
915
|
+
this.toLeader({ t: "slot", id, db: s.db, held: s.held });
|
|
916
|
+
for (const [id, v] of this.vcs)
|
|
917
|
+
this.toLeader({ t: "vc", id, db: v.db, old: v.old, new: v.new });
|
|
918
|
+
const held = this.held;
|
|
919
|
+
this.held = [];
|
|
920
|
+
for (const [m, transfer] of held)
|
|
921
|
+
this.send(m, transfer);
|
|
922
|
+
}
|
|
923
|
+
receive(m) {
|
|
924
|
+
switch (m.t) {
|
|
925
|
+
case "call":
|
|
926
|
+
this.settle(m);
|
|
927
|
+
return;
|
|
928
|
+
case "tx":
|
|
929
|
+
if (m.end && (m.end.ok || m.end.name !== "Deferred"))
|
|
930
|
+
this.begun.delete(m.tx);
|
|
931
|
+
this.txs.get(m.tx)?._onReply(m);
|
|
932
|
+
return;
|
|
933
|
+
case "welcome":
|
|
934
|
+
if (m.leader === this.leaderTab && this.role === "pending") {
|
|
935
|
+
this.role = "follower";
|
|
936
|
+
this.flush();
|
|
937
|
+
}
|
|
938
|
+
return;
|
|
939
|
+
case "slotGo":
|
|
940
|
+
this.slots.get(m.id)?.go();
|
|
941
|
+
return;
|
|
942
|
+
case "vc": {
|
|
943
|
+
const w = this.answer(m.key, m.db, m.old, m.new);
|
|
944
|
+
const id = m.id;
|
|
945
|
+
Promise.resolve(w?.ack ?? { blocking: false, version: 0 }).then((a) => this.toLeader({ t: "vcAck", id, ...a }));
|
|
946
|
+
(w?.closed ?? Promise.resolve()).then(() => this.toLeader({ t: "vcClosed", id }));
|
|
947
|
+
return;
|
|
948
|
+
}
|
|
949
|
+
case "vcAck":
|
|
950
|
+
this.vcs.get(m.id)?.ack({ blocking: m.blocking, version: m.version });
|
|
951
|
+
return;
|
|
952
|
+
case "vcClosed": {
|
|
953
|
+
const v = this.vcs.get(m.id);
|
|
954
|
+
this.vcs.delete(m.id);
|
|
955
|
+
v?.closed();
|
|
956
|
+
return;
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
answer(key, db, oldVersion, newVersion) {
|
|
961
|
+
let w = this.seen.get(key) ?? null;
|
|
962
|
+
if (!w) {
|
|
963
|
+
w = versionChangeHook(db, oldVersion, newVersion);
|
|
964
|
+
if (w) {
|
|
965
|
+
this.seen.set(key, w);
|
|
966
|
+
w.closed.then(() => this.seen.delete(key));
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
return w;
|
|
970
|
+
}
|
|
971
|
+
reply(tab, m) {
|
|
972
|
+
if (tab === this.tab)
|
|
973
|
+
this.receive(m);
|
|
974
|
+
else {
|
|
975
|
+
const f = this.followers.get(tab);
|
|
976
|
+
if (f && !f.gone)
|
|
977
|
+
f.port.postMessage(m);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
serve(tab) {
|
|
981
|
+
const f = { tab, port: new BroadcastChannel(TAB + tab), conns: new Map, txs: new Map, gone: false };
|
|
982
|
+
f.port.onmessage = (e) => this.onLeader(tab, e.data);
|
|
983
|
+
this.followers.set(tab, f);
|
|
984
|
+
this.watch(tab, () => this.followerGone(tab));
|
|
985
|
+
f.port.postMessage({ t: "welcome", leader: this.tab });
|
|
986
|
+
}
|
|
987
|
+
onLeader(from, m) {
|
|
988
|
+
switch (m.t) {
|
|
989
|
+
case "tx":
|
|
990
|
+
return this.relayTx(from, m);
|
|
991
|
+
case "done":
|
|
992
|
+
this.followers.get(from)?.txs.get(m.tx)?.finish();
|
|
993
|
+
return;
|
|
994
|
+
case "slot":
|
|
995
|
+
return this.grantSlot(from, m.id, m.db, m.held);
|
|
996
|
+
case "slotEnd": {
|
|
997
|
+
const key = `${from}:${m.id}`;
|
|
998
|
+
const release = this.slotHolds.get(key);
|
|
999
|
+
this.slotHolds.delete(key);
|
|
1000
|
+
release?.();
|
|
1001
|
+
return;
|
|
1002
|
+
}
|
|
1003
|
+
case "vc":
|
|
1004
|
+
return this.fanOut(from, m.id, m.db, m.old, m.new);
|
|
1005
|
+
case "vcAck":
|
|
1006
|
+
this.fanouts.get(m.id)?.ack({ blocking: m.blocking, version: m.version });
|
|
1007
|
+
return;
|
|
1008
|
+
case "vcClosed": {
|
|
1009
|
+
const w = this.fanouts.get(m.id);
|
|
1010
|
+
this.fanouts.delete(m.id);
|
|
1011
|
+
w?.closed();
|
|
1012
|
+
return;
|
|
1013
|
+
}
|
|
1014
|
+
default:
|
|
1015
|
+
return this.relayCall(from, m);
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
relayCall(from, m) {
|
|
1019
|
+
const f = this.followers.get(from);
|
|
1020
|
+
if (!f)
|
|
1021
|
+
return;
|
|
1022
|
+
const id = this.nextCall++;
|
|
1023
|
+
if (m.t === "close")
|
|
1024
|
+
count(f.conns, m.db, -1);
|
|
1025
|
+
this.relayed.set(id, (r) => {
|
|
1026
|
+
if (r.ok && m.t === "open") {
|
|
1027
|
+
if (f.gone)
|
|
1028
|
+
this.worker.postMessage({ t: "close", db: m.db, id: this.nextCall++ });
|
|
1029
|
+
else
|
|
1030
|
+
count(f.conns, m.db, 1);
|
|
1031
|
+
}
|
|
1032
|
+
if (!f.gone)
|
|
1033
|
+
f.port.postMessage({ ...r, id: m.id });
|
|
1034
|
+
});
|
|
1035
|
+
this.worker.postMessage({ ...m, id });
|
|
1036
|
+
}
|
|
1037
|
+
relayTx(from, m) {
|
|
1038
|
+
const f = this.followers.get(from);
|
|
1039
|
+
if (!f)
|
|
1040
|
+
return;
|
|
1041
|
+
let p = f.txs.get(m.tx);
|
|
1042
|
+
if (!p) {
|
|
1043
|
+
if (!m.begin)
|
|
1044
|
+
return;
|
|
1045
|
+
p = new RemoteTx(this, f, m.tx, this.nextTx++, m.db, m.begin);
|
|
1046
|
+
f.txs.set(m.tx, p);
|
|
1047
|
+
this.txs.set(p.id, p);
|
|
1048
|
+
scheduler(m.db).add(p);
|
|
1049
|
+
}
|
|
1050
|
+
p.receive(m);
|
|
1051
|
+
}
|
|
1052
|
+
openQueue(db) {
|
|
1053
|
+
let q = this.queues.get(db);
|
|
1054
|
+
if (!q)
|
|
1055
|
+
this.queues.set(db, q = new OpenQueue);
|
|
1056
|
+
return q;
|
|
1057
|
+
}
|
|
1058
|
+
grantSlot(from, id, db, held) {
|
|
1059
|
+
this.openQueue(db).add(() => new Promise((release) => {
|
|
1060
|
+
if (from !== this.tab && !this.followers.has(from))
|
|
1061
|
+
return release();
|
|
1062
|
+
this.slotHolds.set(`${from}:${id}`, release);
|
|
1063
|
+
if (!held)
|
|
1064
|
+
this.reply(from, { t: "slotGo", id });
|
|
1065
|
+
}), held);
|
|
1066
|
+
}
|
|
1067
|
+
fanOut(from, id, db, oldVersion, newVersion) {
|
|
1068
|
+
const key = `${from}:${id}`;
|
|
1069
|
+
const waits = [];
|
|
1070
|
+
if (from !== this.tab) {
|
|
1071
|
+
const own = this.answer(key, db, oldVersion, newVersion);
|
|
1072
|
+
if (own)
|
|
1073
|
+
waits.push(own);
|
|
1074
|
+
}
|
|
1075
|
+
for (const [tab, f] of this.followers) {
|
|
1076
|
+
if (tab === from || !f.conns.has(db))
|
|
1077
|
+
continue;
|
|
1078
|
+
const fid = `${this.tab}:${this.nextVc++}`;
|
|
1079
|
+
let ack;
|
|
1080
|
+
let closed;
|
|
1081
|
+
waits.push({ ack: new Promise((r) => ack = r), closed: new Promise((r) => closed = r) });
|
|
1082
|
+
this.fanouts.set(fid, { tab, ack, closed });
|
|
1083
|
+
f.port.postMessage({ t: "vc", id: fid, key, db, old: oldVersion, new: newVersion });
|
|
1084
|
+
}
|
|
1085
|
+
Promise.all(waits.map((w) => w.ack)).then((acks) => this.reply(from, { t: "vcAck", id, blocking: acks.some((a) => a.blocking), version: Math.max(0, ...acks.map((a) => a.version)) }));
|
|
1086
|
+
Promise.all(waits.map((w) => w.closed)).then(() => this.reply(from, { t: "vcClosed", id }));
|
|
1087
|
+
}
|
|
1088
|
+
followerGone(tab) {
|
|
1089
|
+
const f = this.followers.get(tab);
|
|
1090
|
+
if (!f)
|
|
1091
|
+
return;
|
|
1092
|
+
f.gone = true;
|
|
1093
|
+
this.followers.delete(tab);
|
|
1094
|
+
f.port.close();
|
|
1095
|
+
for (const p of [...f.txs.values()])
|
|
1096
|
+
p.kill();
|
|
1097
|
+
for (const [db, n] of f.conns)
|
|
1098
|
+
for (let i = 0;i < n; i++)
|
|
1099
|
+
this.worker.postMessage({ t: "close", db, id: this.nextCall++ });
|
|
1100
|
+
for (const [key, release] of this.slotHolds) {
|
|
1101
|
+
if (!key.startsWith(`${tab}:`))
|
|
1102
|
+
continue;
|
|
1103
|
+
this.slotHolds.delete(key);
|
|
1104
|
+
release();
|
|
1105
|
+
}
|
|
1106
|
+
for (const [id, w] of this.fanouts) {
|
|
1107
|
+
if (w.tab !== tab)
|
|
1108
|
+
continue;
|
|
1109
|
+
this.fanouts.delete(id);
|
|
1110
|
+
w.ack({ blocking: false, version: 0 });
|
|
1111
|
+
w.closed();
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
var current = null;
|
|
1116
|
+
function runtime(o) {
|
|
1117
|
+
current ??= new Runtime(o);
|
|
1118
|
+
return current;
|
|
1119
|
+
}
|
|
1120
|
+
function startRuntime(o) {
|
|
1121
|
+
return runtime(o);
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
// src/value.ts
|
|
1125
|
+
var TAGS = new Set(["$u", "$n", "$bi", "$d", "$re", "$bo", "$no", "$so", "$ab", "$ta", "$sa", "$m", "$set", "$r"]);
|
|
1126
|
+
var VIEWS = {
|
|
1127
|
+
Int8Array,
|
|
1128
|
+
Uint8Array,
|
|
1129
|
+
Uint8ClampedArray,
|
|
1130
|
+
Int16Array,
|
|
1131
|
+
Uint16Array,
|
|
1132
|
+
Int32Array,
|
|
1133
|
+
Uint32Array,
|
|
1134
|
+
Float32Array,
|
|
1135
|
+
Float64Array,
|
|
1136
|
+
BigInt64Array,
|
|
1137
|
+
BigUint64Array,
|
|
1138
|
+
DataView
|
|
1139
|
+
};
|
|
1140
|
+
function cloneError(what) {
|
|
1141
|
+
return new DOMException(`${what} could not be cloned.`, "DataCloneError");
|
|
1142
|
+
}
|
|
1143
|
+
function toBase64(b) {
|
|
1144
|
+
let s = "";
|
|
1145
|
+
for (let i = 0;i < b.length; i += 8192)
|
|
1146
|
+
s += String.fromCharCode(...b.subarray(i, i + 8192));
|
|
1147
|
+
return btoa(s);
|
|
1148
|
+
}
|
|
1149
|
+
function fromBase64(s) {
|
|
1150
|
+
const bin = atob(s);
|
|
1151
|
+
const b = new Uint8Array(bin.length);
|
|
1152
|
+
for (let i = 0;i < bin.length; i++)
|
|
1153
|
+
b[i] = bin.charCodeAt(i);
|
|
1154
|
+
return b.buffer;
|
|
1155
|
+
}
|
|
1156
|
+
function num(n) {
|
|
1157
|
+
if (Number.isFinite(n) && !Object.is(n, -0))
|
|
1158
|
+
return n;
|
|
1159
|
+
return { $n: Object.is(n, -0) ? "-0" : String(n) };
|
|
1160
|
+
}
|
|
1161
|
+
function unnum(v) {
|
|
1162
|
+
if (typeof v === "number")
|
|
1163
|
+
return v;
|
|
1164
|
+
const s = v.$n;
|
|
1165
|
+
return s === "-0" ? -0 : Number(s);
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
class Encoder {
|
|
1169
|
+
tagged = false;
|
|
1170
|
+
ids = new Map;
|
|
1171
|
+
tag(o) {
|
|
1172
|
+
this.tagged = true;
|
|
1173
|
+
return o;
|
|
1174
|
+
}
|
|
1175
|
+
walk(v) {
|
|
1176
|
+
switch (typeof v) {
|
|
1177
|
+
case "string":
|
|
1178
|
+
case "boolean":
|
|
1179
|
+
return v;
|
|
1180
|
+
case "number": {
|
|
1181
|
+
const out = num(v);
|
|
1182
|
+
if (out !== v)
|
|
1183
|
+
this.tagged = true;
|
|
1184
|
+
return out;
|
|
1185
|
+
}
|
|
1186
|
+
case "undefined":
|
|
1187
|
+
return this.tag({ $u: 0 });
|
|
1188
|
+
case "bigint":
|
|
1189
|
+
return this.tag({ $bi: v.toString() });
|
|
1190
|
+
case "object":
|
|
1191
|
+
break;
|
|
1192
|
+
default:
|
|
1193
|
+
throw cloneError(typeof v === "function" ? "A function" : `A ${typeof v}`);
|
|
1194
|
+
}
|
|
1195
|
+
if (v === null)
|
|
1196
|
+
return null;
|
|
1197
|
+
const seen = this.ids.get(v);
|
|
1198
|
+
if (seen !== undefined)
|
|
1199
|
+
return this.tag({ $r: seen });
|
|
1200
|
+
this.ids.set(v, this.ids.size);
|
|
1201
|
+
const kind = Object.prototype.toString.call(v).slice(8, -1);
|
|
1202
|
+
if (Array.isArray(v)) {
|
|
1203
|
+
const n = v.length;
|
|
1204
|
+
let dense = true;
|
|
1205
|
+
for (let i = 0;i < n; i++) {
|
|
1206
|
+
if (!Object.prototype.hasOwnProperty.call(v, i)) {
|
|
1207
|
+
dense = false;
|
|
1208
|
+
break;
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
if (dense) {
|
|
1212
|
+
const out = new Array(n);
|
|
1213
|
+
for (let i = 0;i < n; i++)
|
|
1214
|
+
out[i] = this.walk(v[i]);
|
|
1215
|
+
return out;
|
|
1216
|
+
}
|
|
1217
|
+
const items = [];
|
|
1218
|
+
for (const k of Object.keys(v)) {
|
|
1219
|
+
const i = Number(k);
|
|
1220
|
+
if (Number.isInteger(i) && i >= 0 && String(i) === k)
|
|
1221
|
+
items.push([i, this.walk(v[i])]);
|
|
1222
|
+
}
|
|
1223
|
+
return this.tag({ $sa: [n, items] });
|
|
1224
|
+
}
|
|
1225
|
+
switch (kind) {
|
|
1226
|
+
case "Object":
|
|
1227
|
+
break;
|
|
1228
|
+
case "Date":
|
|
1229
|
+
return this.tag({ $d: num(v.getTime()) });
|
|
1230
|
+
case "RegExp":
|
|
1231
|
+
return this.tag({ $re: [v.source, v.flags] });
|
|
1232
|
+
case "Boolean":
|
|
1233
|
+
return this.tag({ $bo: v.valueOf() });
|
|
1234
|
+
case "Number":
|
|
1235
|
+
return this.tag({ $no: num(v.valueOf()) });
|
|
1236
|
+
case "String":
|
|
1237
|
+
return this.tag({ $so: v.valueOf() });
|
|
1238
|
+
case "ArrayBuffer":
|
|
1239
|
+
return this.tag({ $ab: toBase64(new Uint8Array(v)) });
|
|
1240
|
+
case "Map": {
|
|
1241
|
+
const items = [];
|
|
1242
|
+
for (const [k, x] of v)
|
|
1243
|
+
items.push([this.walk(k), this.walk(x)]);
|
|
1244
|
+
return this.tag({ $m: items });
|
|
1245
|
+
}
|
|
1246
|
+
case "Set": {
|
|
1247
|
+
const items = [];
|
|
1248
|
+
for (const x of v)
|
|
1249
|
+
items.push(this.walk(x));
|
|
1250
|
+
return this.tag({ $set: items });
|
|
1251
|
+
}
|
|
1252
|
+
default:
|
|
1253
|
+
if (ArrayBuffer.isView(v) && kind in VIEWS) {
|
|
1254
|
+
return this.tag({ $ta: [kind, toBase64(new Uint8Array(v.buffer, v.byteOffset, v.byteLength))] });
|
|
1255
|
+
}
|
|
1256
|
+
throw cloneError(`An object of type ${kind}`);
|
|
1257
|
+
}
|
|
1258
|
+
const out = {};
|
|
1259
|
+
for (const k of Object.keys(v)) {
|
|
1260
|
+
const x = this.walk(v[k]);
|
|
1261
|
+
if (k.charCodeAt(0) === 36) {
|
|
1262
|
+
this.tagged = true;
|
|
1263
|
+
out["$" + k] = x;
|
|
1264
|
+
} else
|
|
1265
|
+
out[k] = x;
|
|
1266
|
+
}
|
|
1267
|
+
return out;
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
function encodeValue(v) {
|
|
1271
|
+
const e = new Encoder;
|
|
1272
|
+
const tree = e.walk(v);
|
|
1273
|
+
return (e.tagged ? "T" : "J") + JSON.stringify(tree);
|
|
1274
|
+
}
|
|
1275
|
+
function decodeValue(s) {
|
|
1276
|
+
const tree = JSON.parse(s.slice(1));
|
|
1277
|
+
return s.charCodeAt(0) === 74 ? tree : new Decoder().walk(tree);
|
|
1278
|
+
}
|
|
1279
|
+
class Decoder {
|
|
1280
|
+
objects = [];
|
|
1281
|
+
walk(t) {
|
|
1282
|
+
if (t === null || typeof t !== "object")
|
|
1283
|
+
return t;
|
|
1284
|
+
if (Array.isArray(t)) {
|
|
1285
|
+
const out = new Array(t.length);
|
|
1286
|
+
this.objects.push(out);
|
|
1287
|
+
for (let i = 0;i < t.length; i++)
|
|
1288
|
+
out[i] = this.walk(t[i]);
|
|
1289
|
+
return out;
|
|
1290
|
+
}
|
|
1291
|
+
const keys = Object.keys(t);
|
|
1292
|
+
const o = t;
|
|
1293
|
+
if (keys.length === 1 && TAGS.has(keys[0])) {
|
|
1294
|
+
const x = o[keys[0]];
|
|
1295
|
+
switch (keys[0]) {
|
|
1296
|
+
case "$u":
|
|
1297
|
+
return;
|
|
1298
|
+
case "$n":
|
|
1299
|
+
return unnum(t);
|
|
1300
|
+
case "$bi":
|
|
1301
|
+
return BigInt(x);
|
|
1302
|
+
case "$r":
|
|
1303
|
+
return this.objects[x];
|
|
1304
|
+
}
|
|
1305
|
+
let out;
|
|
1306
|
+
const at = this.objects.length;
|
|
1307
|
+
this.objects.push(null);
|
|
1308
|
+
switch (keys[0]) {
|
|
1309
|
+
case "$d":
|
|
1310
|
+
out = new Date(unnum(x));
|
|
1311
|
+
break;
|
|
1312
|
+
case "$re":
|
|
1313
|
+
out = new RegExp(x[0], x[1]);
|
|
1314
|
+
break;
|
|
1315
|
+
case "$bo":
|
|
1316
|
+
out = new Boolean(x);
|
|
1317
|
+
break;
|
|
1318
|
+
case "$no":
|
|
1319
|
+
out = new Number(unnum(x));
|
|
1320
|
+
break;
|
|
1321
|
+
case "$so":
|
|
1322
|
+
out = new String(x);
|
|
1323
|
+
break;
|
|
1324
|
+
case "$ab":
|
|
1325
|
+
out = fromBase64(x);
|
|
1326
|
+
break;
|
|
1327
|
+
case "$ta": {
|
|
1328
|
+
const [kind, b64] = x;
|
|
1329
|
+
out = new VIEWS[kind](fromBase64(b64));
|
|
1330
|
+
break;
|
|
1331
|
+
}
|
|
1332
|
+
case "$sa": {
|
|
1333
|
+
const [n, items] = x;
|
|
1334
|
+
const arr = new Array(n);
|
|
1335
|
+
this.objects[at] = arr;
|
|
1336
|
+
for (const [i, v] of items)
|
|
1337
|
+
arr[i] = this.walk(v);
|
|
1338
|
+
return arr;
|
|
1339
|
+
}
|
|
1340
|
+
case "$m": {
|
|
1341
|
+
const m = new Map;
|
|
1342
|
+
this.objects[at] = m;
|
|
1343
|
+
for (const [k, v] of x) {
|
|
1344
|
+
const kk = this.walk(k);
|
|
1345
|
+
m.set(kk, this.walk(v));
|
|
1346
|
+
}
|
|
1347
|
+
return m;
|
|
1348
|
+
}
|
|
1349
|
+
case "$set": {
|
|
1350
|
+
const s = new Set;
|
|
1351
|
+
this.objects[at] = s;
|
|
1352
|
+
for (const v of x)
|
|
1353
|
+
s.add(this.walk(v));
|
|
1354
|
+
return s;
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
this.objects[at] = out;
|
|
1358
|
+
return out;
|
|
1359
|
+
}
|
|
1360
|
+
const out = {};
|
|
1361
|
+
this.objects.push(out);
|
|
1362
|
+
for (const k of keys) {
|
|
1363
|
+
const v = this.walk(o[k]);
|
|
1364
|
+
if (k.charCodeAt(0) === 36)
|
|
1365
|
+
out[k.slice(1)] = v;
|
|
1366
|
+
else
|
|
1367
|
+
out[k] = v;
|
|
1368
|
+
}
|
|
1369
|
+
return out;
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
// src/facade.ts
|
|
1374
|
+
var err = (name, message) => new DOMException(message, name);
|
|
1375
|
+
var afterTask = (() => {
|
|
1376
|
+
let queue = [];
|
|
1377
|
+
let ch = null;
|
|
1378
|
+
let armed = false;
|
|
1379
|
+
let timerFirst = null;
|
|
1380
|
+
const run = (byTimer) => {
|
|
1381
|
+
if (!armed)
|
|
1382
|
+
return;
|
|
1383
|
+
timerFirst ??= byTimer;
|
|
1384
|
+
armed = false;
|
|
1385
|
+
const q = queue;
|
|
1386
|
+
queue = [];
|
|
1387
|
+
for (const f of q)
|
|
1388
|
+
f();
|
|
1389
|
+
};
|
|
1390
|
+
return (fn) => {
|
|
1391
|
+
if (!ch) {
|
|
1392
|
+
ch = new MessageChannel;
|
|
1393
|
+
ch.port1.onmessage = () => run(false);
|
|
1394
|
+
}
|
|
1395
|
+
queue.push(fn);
|
|
1396
|
+
if (!armed) {
|
|
1397
|
+
armed = true;
|
|
1398
|
+
ch.port2.postMessage(0);
|
|
1399
|
+
if (timerFirst !== false)
|
|
1400
|
+
setTimeout(() => run(true), 0);
|
|
1401
|
+
}
|
|
1402
|
+
};
|
|
1403
|
+
})();
|
|
1404
|
+
function stringList(names) {
|
|
1405
|
+
const a = [...names].sort();
|
|
1406
|
+
a.contains = (s) => a.includes(String(s));
|
|
1407
|
+
a.item = (i) => a[i] ?? null;
|
|
1408
|
+
return a;
|
|
1409
|
+
}
|
|
1410
|
+
var PREFETCH = 32;
|
|
1411
|
+
|
|
1412
|
+
class PlateauRequest extends PlateauTarget {
|
|
1413
|
+
_result = undefined;
|
|
1414
|
+
_error = null;
|
|
1415
|
+
_done = false;
|
|
1416
|
+
kind;
|
|
1417
|
+
cursor = null;
|
|
1418
|
+
source;
|
|
1419
|
+
transaction;
|
|
1420
|
+
plan;
|
|
1421
|
+
known = undefined;
|
|
1422
|
+
constructor(source, transaction, kind) {
|
|
1423
|
+
super();
|
|
1424
|
+
this.source = source;
|
|
1425
|
+
this.transaction = transaction;
|
|
1426
|
+
this.kind = kind;
|
|
1427
|
+
}
|
|
1428
|
+
get readyState() {
|
|
1429
|
+
return this._done ? "done" : "pending";
|
|
1430
|
+
}
|
|
1431
|
+
get result() {
|
|
1432
|
+
if (!this._done)
|
|
1433
|
+
throw err("InvalidStateError", "The request has not finished.");
|
|
1434
|
+
return this._result;
|
|
1435
|
+
}
|
|
1436
|
+
get error() {
|
|
1437
|
+
if (!this._done)
|
|
1438
|
+
throw err("InvalidStateError", "The request has not finished.");
|
|
1439
|
+
return this._error;
|
|
1440
|
+
}
|
|
1441
|
+
parentTarget() {
|
|
1442
|
+
return this.transaction;
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
handlers(PlateauRequest, ["success", "error"]);
|
|
1446
|
+
|
|
1447
|
+
class PlateauOpenDBRequest extends PlateauRequest {
|
|
1448
|
+
constructor() {
|
|
1449
|
+
super(null, null, "value");
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
handlers(PlateauOpenDBRequest, ["blocked", "upgradeneeded"]);
|
|
1453
|
+
function promise(r) {
|
|
1454
|
+
return new Promise((resolve, reject) => {
|
|
1455
|
+
r.addEventListener("success", () => resolve(r.result));
|
|
1456
|
+
r.addEventListener("error", () => reject(r.error));
|
|
1457
|
+
});
|
|
1458
|
+
}
|
|
1459
|
+
var connections = new Map;
|
|
1460
|
+
function enqueue(name, job) {
|
|
1461
|
+
runtime().queue(name, job);
|
|
1462
|
+
}
|
|
1463
|
+
function toVersion(v) {
|
|
1464
|
+
if (v === undefined)
|
|
1465
|
+
return;
|
|
1466
|
+
const n = Number(v);
|
|
1467
|
+
if (!Number.isFinite(n) || n < 1 || n > Number.MAX_SAFE_INTEGER)
|
|
1468
|
+
throw new TypeError("The version must be a positive integer.");
|
|
1469
|
+
return Math.floor(n);
|
|
1470
|
+
}
|
|
1471
|
+
function versionChangeHere(name, self, oldVersion, newVersion) {
|
|
1472
|
+
const others = [...connections.get(name) ?? []].filter((c) => c !== self && !c._closePending);
|
|
1473
|
+
for (const c of others) {
|
|
1474
|
+
if (c._closePending)
|
|
1475
|
+
continue;
|
|
1476
|
+
dispatch(c, new PlateauVersionChangeEvent("versionchange", { oldVersion: oldVersion ?? c._version, newVersion }));
|
|
1477
|
+
}
|
|
1478
|
+
const open = others.filter((c) => !c._closed);
|
|
1479
|
+
if (!open.length)
|
|
1480
|
+
return null;
|
|
1481
|
+
return {
|
|
1482
|
+
ack: { blocking: open.some((c) => !c._closePending), version: open[0]._version },
|
|
1483
|
+
closed: Promise.all(open.map((c) => c._closedPromise)).then(() => {})
|
|
1484
|
+
};
|
|
1485
|
+
}
|
|
1486
|
+
onVersionChange((name, oldVersion, newVersion) => versionChangeHere(name, null, oldVersion, newVersion));
|
|
1487
|
+
async function closeOthers(name, self, oldVersion, newVersion, req) {
|
|
1488
|
+
const here = versionChangeHere(name, self, oldVersion, newVersion);
|
|
1489
|
+
const elsewhere = runtime().versionChange(name, oldVersion, newVersion);
|
|
1490
|
+
let ack = here?.ack;
|
|
1491
|
+
if (!ack?.blocking && elsewhere) {
|
|
1492
|
+
const remote = await elsewhere.ack;
|
|
1493
|
+
ack = { blocking: remote.blocking, version: Math.max(ack?.version ?? 0, remote.version) };
|
|
1494
|
+
}
|
|
1495
|
+
if (ack?.blocking)
|
|
1496
|
+
dispatch(req, new PlateauVersionChangeEvent("blocked", { oldVersion: oldVersion ?? ack.version, newVersion }));
|
|
1497
|
+
await Promise.all([here?.closed, elsewhere?.closed]);
|
|
1498
|
+
}
|
|
1499
|
+
function changed(name, stores) {
|
|
1500
|
+
for (const c of connections.get(name) ?? []) {
|
|
1501
|
+
if (c._closePending)
|
|
1502
|
+
continue;
|
|
1503
|
+
for (const o of c._observers) {
|
|
1504
|
+
const hit = stores.filter((s) => o.stores.has(s));
|
|
1505
|
+
if (!hit.length)
|
|
1506
|
+
continue;
|
|
1507
|
+
try {
|
|
1508
|
+
o.callback({ stores: hit });
|
|
1509
|
+
} catch (e) {
|
|
1510
|
+
reportError(e);
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
onChange(changed);
|
|
1516
|
+
function finishRequest(req, result, error) {
|
|
1517
|
+
req._done = true;
|
|
1518
|
+
req._result = result;
|
|
1519
|
+
req._error = error;
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
class PlateauFactory {
|
|
1523
|
+
open(name, version) {
|
|
1524
|
+
const n = String(name);
|
|
1525
|
+
const v = toVersion(version);
|
|
1526
|
+
const req = new PlateauOpenDBRequest;
|
|
1527
|
+
enqueue(n, () => openDatabase(req, n, v));
|
|
1528
|
+
return req;
|
|
1529
|
+
}
|
|
1530
|
+
deleteDatabase(name) {
|
|
1531
|
+
const n = String(name);
|
|
1532
|
+
const req = new PlateauOpenDBRequest;
|
|
1533
|
+
enqueue(n, async () => {
|
|
1534
|
+
await new Promise((r) => afterTask(r));
|
|
1535
|
+
await closeOthers(n, null, null, null, req);
|
|
1536
|
+
let old = 0;
|
|
1537
|
+
try {
|
|
1538
|
+
old = await runtime().call({ t: "delete", db: n });
|
|
1539
|
+
} catch (e) {
|
|
1540
|
+
finishRequest(req, undefined, e);
|
|
1541
|
+
dispatch(req, new PlateauEvent("error", { bubbles: true, cancelable: true }));
|
|
1542
|
+
return;
|
|
1543
|
+
}
|
|
1544
|
+
finishRequest(req, undefined, null);
|
|
1545
|
+
dispatch(req, new PlateauVersionChangeEvent("success", { oldVersion: old, newVersion: null }));
|
|
1546
|
+
});
|
|
1547
|
+
return req;
|
|
1548
|
+
}
|
|
1549
|
+
cmp(a, b) {
|
|
1550
|
+
return cmp(a, b);
|
|
1551
|
+
}
|
|
1552
|
+
async databases() {
|
|
1553
|
+
return [];
|
|
1554
|
+
}
|
|
1555
|
+
whenLeader() {
|
|
1556
|
+
return runtime().leading;
|
|
1557
|
+
}
|
|
1558
|
+
async setMemoryBudget(bytes) {
|
|
1559
|
+
if (bytes !== null && !(bytes > 0))
|
|
1560
|
+
throw new TypeError("plateau: the memory budget must be a positive number of bytes, or null.");
|
|
1561
|
+
await runtime().call({ t: "budget", bytes });
|
|
1562
|
+
}
|
|
1563
|
+
memoryStats(name) {
|
|
1564
|
+
return runtime().call(name === undefined ? { t: "stats" } : { t: "stats", db: String(name) });
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
async function openDatabase(req, name, version) {
|
|
1568
|
+
await new Promise((r) => afterTask(r));
|
|
1569
|
+
const rt = runtime();
|
|
1570
|
+
let info;
|
|
1571
|
+
try {
|
|
1572
|
+
info = await rt.call({ t: "open", db: name });
|
|
1573
|
+
} catch (e) {
|
|
1574
|
+
finishRequest(req, undefined, e);
|
|
1575
|
+
dispatch(req, new PlateauEvent("error", { bubbles: true, cancelable: true }));
|
|
1576
|
+
return;
|
|
1577
|
+
}
|
|
1578
|
+
const cur = info.version;
|
|
1579
|
+
const want = version ?? Math.max(cur, 1);
|
|
1580
|
+
if (want < cur) {
|
|
1581
|
+
await rt.call({ t: "close", db: name });
|
|
1582
|
+
finishRequest(req, undefined, err("VersionError", `The requested version (${want}) is less than the existing version (${cur}).`));
|
|
1583
|
+
dispatch(req, new PlateauEvent("error", { bubbles: true, cancelable: true }));
|
|
1584
|
+
return;
|
|
1585
|
+
}
|
|
1586
|
+
const db = new PlateauDatabase(name, cur, info.nextId, info.stores);
|
|
1587
|
+
let set = connections.get(name);
|
|
1588
|
+
if (!set)
|
|
1589
|
+
connections.set(name, set = new Set);
|
|
1590
|
+
if (want > cur) {
|
|
1591
|
+
await closeOthers(name, db, cur, want, req);
|
|
1592
|
+
set.add(db);
|
|
1593
|
+
db._version = want;
|
|
1594
|
+
const tx = new PlateauTransaction(db, [...db._stores.keys()], "versionchange", want);
|
|
1595
|
+
db._upgrade = tx;
|
|
1596
|
+
req.transaction = tx;
|
|
1597
|
+
finishRequest(req, db, null);
|
|
1598
|
+
tx._state = "active";
|
|
1599
|
+
tx._settleSoon();
|
|
1600
|
+
const out = dispatch(req, new PlateauVersionChangeEvent("upgradeneeded", { oldVersion: cur, newVersion: want }));
|
|
1601
|
+
if (out.threw)
|
|
1602
|
+
tx._abort(err("AbortError", "An exception was thrown in the upgradeneeded handler."));
|
|
1603
|
+
await tx._finishedPromise();
|
|
1604
|
+
db._upgrade = null;
|
|
1605
|
+
req.transaction = null;
|
|
1606
|
+
if (tx._aborted || db._closePending) {
|
|
1607
|
+
db._version = cur;
|
|
1608
|
+
if (!db._closePending)
|
|
1609
|
+
db._closeNow();
|
|
1610
|
+
finishRequest(req, undefined, err("AbortError", "The upgrade transaction was aborted."));
|
|
1611
|
+
dispatch(req, new PlateauEvent("error", { bubbles: true, cancelable: true }));
|
|
1612
|
+
return;
|
|
1613
|
+
}
|
|
1614
|
+
} else
|
|
1615
|
+
set.add(db);
|
|
1616
|
+
finishRequest(req, db, null);
|
|
1617
|
+
dispatch(req, new PlateauEvent("success"));
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
class PlateauDatabase extends PlateauTarget {
|
|
1621
|
+
name;
|
|
1622
|
+
_version;
|
|
1623
|
+
_stores = new Map;
|
|
1624
|
+
_nextId;
|
|
1625
|
+
_upgrade = null;
|
|
1626
|
+
_txs = new Set;
|
|
1627
|
+
_closePending = false;
|
|
1628
|
+
_observers = new Set;
|
|
1629
|
+
_closed = false;
|
|
1630
|
+
_closedPromise;
|
|
1631
|
+
markClosed = () => {};
|
|
1632
|
+
constructor(name, version, nextId, stores) {
|
|
1633
|
+
super();
|
|
1634
|
+
this.name = name;
|
|
1635
|
+
this._version = version;
|
|
1636
|
+
this._nextId = nextId;
|
|
1637
|
+
for (const s of stores)
|
|
1638
|
+
this._stores.set(s.name, s);
|
|
1639
|
+
this._closedPromise = new Promise((r) => this.markClosed = r);
|
|
1640
|
+
}
|
|
1641
|
+
get version() {
|
|
1642
|
+
return this._version;
|
|
1643
|
+
}
|
|
1644
|
+
get objectStoreNames() {
|
|
1645
|
+
return stringList(this._stores.keys());
|
|
1646
|
+
}
|
|
1647
|
+
upgradeTx() {
|
|
1648
|
+
const tx = this._upgrade;
|
|
1649
|
+
if (!tx || tx._state === "finished")
|
|
1650
|
+
throw err("InvalidStateError", "Not in a version change transaction.");
|
|
1651
|
+
if (tx._state !== "active")
|
|
1652
|
+
throw err("TransactionInactiveError", "The version change transaction is not active.");
|
|
1653
|
+
return tx;
|
|
1654
|
+
}
|
|
1655
|
+
createObjectStore(name, options = {}) {
|
|
1656
|
+
const tx = this.upgradeTx();
|
|
1657
|
+
const n = String(name);
|
|
1658
|
+
const keyPath = normalizeKeyPath(options?.keyPath ?? null);
|
|
1659
|
+
if (keyPath !== null && !validKeyPath(keyPath))
|
|
1660
|
+
throw err("SyntaxError", "The keyPath is not a valid key path.");
|
|
1661
|
+
if (this._stores.has(n))
|
|
1662
|
+
throw err("ConstraintError", `An object store named ${n} already exists.`);
|
|
1663
|
+
const autoIncrement = !!options?.autoIncrement;
|
|
1664
|
+
if (autoIncrement && (keyPath === "" || Array.isArray(keyPath)))
|
|
1665
|
+
throw err("InvalidAccessError", "autoIncrement needs a non-empty, non-array key path.");
|
|
1666
|
+
const id = this._nextId++;
|
|
1667
|
+
const def = { id, file: `s${id}`, name: n, keyPath, autoIncrement, indexes: [] };
|
|
1668
|
+
if (options?.columns !== undefined)
|
|
1669
|
+
def.columns = columnDecls(options.columns, keyPath);
|
|
1670
|
+
this._stores.set(n, def);
|
|
1671
|
+
tx._scope.push(n);
|
|
1672
|
+
tx._internal({ o: "mkStore", def: structuredClone(def) });
|
|
1673
|
+
return tx.objectStore(n);
|
|
1674
|
+
}
|
|
1675
|
+
deleteObjectStore(name) {
|
|
1676
|
+
const tx = this.upgradeTx();
|
|
1677
|
+
const n = String(name);
|
|
1678
|
+
const def = this._stores.get(n);
|
|
1679
|
+
if (!def)
|
|
1680
|
+
throw err("NotFoundError", `No object store named ${n}.`);
|
|
1681
|
+
this._stores.delete(n);
|
|
1682
|
+
tx._scope = tx._scope.filter((s) => s !== n);
|
|
1683
|
+
tx._handles.delete(n);
|
|
1684
|
+
tx._internal({ o: "rmStore", s: n });
|
|
1685
|
+
}
|
|
1686
|
+
transaction(names, mode = "readonly", options) {
|
|
1687
|
+
if (this._closePending)
|
|
1688
|
+
throw err("InvalidStateError", "The connection is closed.");
|
|
1689
|
+
if (this._upgrade && this._upgrade._state !== "finished")
|
|
1690
|
+
throw err("InvalidStateError", "A version change transaction is running.");
|
|
1691
|
+
const list = typeof names === "string" ? [names] : [...names].map(String);
|
|
1692
|
+
const scope = [...new Set(list)];
|
|
1693
|
+
for (const s of scope)
|
|
1694
|
+
if (!this._stores.has(s))
|
|
1695
|
+
throw err("NotFoundError", `No object store named ${s}.`);
|
|
1696
|
+
if (mode !== "readonly" && mode !== "readwrite")
|
|
1697
|
+
throw new TypeError(`The mode ${String(mode)} is not valid.`);
|
|
1698
|
+
if (!scope.length)
|
|
1699
|
+
throw err("InvalidAccessError", "The scope is empty.");
|
|
1700
|
+
const tx = new PlateauTransaction(this, scope, mode);
|
|
1701
|
+
tx.durability = options?.durability ?? "default";
|
|
1702
|
+
tx._settleSoon();
|
|
1703
|
+
return tx;
|
|
1704
|
+
}
|
|
1705
|
+
close() {
|
|
1706
|
+
if (this._closePending)
|
|
1707
|
+
return;
|
|
1708
|
+
this._closePending = true;
|
|
1709
|
+
this._maybeClosed();
|
|
1710
|
+
}
|
|
1711
|
+
observe(stores, callback) {
|
|
1712
|
+
if (this._closePending)
|
|
1713
|
+
throw err("InvalidStateError", "The connection is closed.");
|
|
1714
|
+
if (typeof callback !== "function")
|
|
1715
|
+
throw new TypeError("plateau: observe needs a callback.");
|
|
1716
|
+
const o = { stores: new Set(typeof stores === "string" ? [stores] : [...stores].map(String)), callback };
|
|
1717
|
+
this._observers.add(o);
|
|
1718
|
+
return () => {
|
|
1719
|
+
this._observers.delete(o);
|
|
1720
|
+
};
|
|
1721
|
+
}
|
|
1722
|
+
async compact() {
|
|
1723
|
+
if (this._closePending)
|
|
1724
|
+
throw err("InvalidStateError", "The connection is closed.");
|
|
1725
|
+
while (this._txs.size)
|
|
1726
|
+
await Promise.all([...this._txs].map((tx) => tx._finishedPromise().catch(() => {})));
|
|
1727
|
+
if (this._closePending)
|
|
1728
|
+
throw err("InvalidStateError", "The connection is closed.");
|
|
1729
|
+
await runtime().call({ t: "compact", db: this.name });
|
|
1730
|
+
}
|
|
1731
|
+
_closeNow() {
|
|
1732
|
+
this._closePending = true;
|
|
1733
|
+
this._maybeClosed();
|
|
1734
|
+
}
|
|
1735
|
+
_maybeClosed() {
|
|
1736
|
+
if (!this._closePending || this._closed || this._txs.size)
|
|
1737
|
+
return;
|
|
1738
|
+
this._closed = true;
|
|
1739
|
+
connections.get(this.name)?.delete(this);
|
|
1740
|
+
runtime().call({ t: "close", db: this.name }).finally(() => this.markClosed());
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
handlers(PlateauDatabase, ["abort", "close", "error", "versionchange"]);
|
|
1744
|
+
var KINDS = ["u32", "u32_multi", "f64", "str"];
|
|
1745
|
+
function columnDecls(spec, keyPath) {
|
|
1746
|
+
const entries = Array.isArray(spec) ? spec.map((c) => [String(c.name), c]) : Object.entries(spec);
|
|
1747
|
+
const out = entries.map(([name, c]) => {
|
|
1748
|
+
const d = typeof c === "string" ? { kind: c } : c;
|
|
1749
|
+
if (!KINDS.includes(d.kind))
|
|
1750
|
+
throw new TypeError(`plateau: column ${name} has an unknown kind ${String(d.kind)}.`);
|
|
1751
|
+
if (name.startsWith("_") || name.includes("."))
|
|
1752
|
+
throw new TypeError(`plateau: column ${name} must be a plain field name not starting with _.`);
|
|
1753
|
+
const decl = { name, kind: d.kind };
|
|
1754
|
+
if (d.index)
|
|
1755
|
+
decl.index = d.index;
|
|
1756
|
+
if (d.group)
|
|
1757
|
+
decl.group = d.group;
|
|
1758
|
+
return decl;
|
|
1759
|
+
});
|
|
1760
|
+
const pk = out.find((c) => c.name === keyPath);
|
|
1761
|
+
if (typeof keyPath !== "string" || !pk || pk.kind !== "f64")
|
|
1762
|
+
throw new TypeError("plateau: a store with columns needs a key path naming one of them, of kind f64.");
|
|
1763
|
+
return out;
|
|
1764
|
+
}
|
|
1765
|
+
|
|
1766
|
+
class PlateauTransaction extends PlateauTarget {
|
|
1767
|
+
db;
|
|
1768
|
+
mode;
|
|
1769
|
+
durability = "default";
|
|
1770
|
+
_scope;
|
|
1771
|
+
_state = "active";
|
|
1772
|
+
_started = false;
|
|
1773
|
+
_aborted = false;
|
|
1774
|
+
_error = null;
|
|
1775
|
+
_handles = new Map;
|
|
1776
|
+
finished = null;
|
|
1777
|
+
markFinished = null;
|
|
1778
|
+
isFinished = false;
|
|
1779
|
+
id;
|
|
1780
|
+
version;
|
|
1781
|
+
began = false;
|
|
1782
|
+
unsent = [];
|
|
1783
|
+
unsentReqs = [];
|
|
1784
|
+
inflight = [];
|
|
1785
|
+
head = 0;
|
|
1786
|
+
flushQueued = false;
|
|
1787
|
+
settleQueued = false;
|
|
1788
|
+
commitSent = false;
|
|
1789
|
+
beforeUpgrade = null;
|
|
1790
|
+
nextIdBefore = 0;
|
|
1791
|
+
written = null;
|
|
1792
|
+
constructor(db, scope, mode, version) {
|
|
1793
|
+
super();
|
|
1794
|
+
this.db = db;
|
|
1795
|
+
this.mode = mode;
|
|
1796
|
+
this._scope = [...scope].sort();
|
|
1797
|
+
this.version = version;
|
|
1798
|
+
const rt = runtime();
|
|
1799
|
+
this.id = rt.nextTx++;
|
|
1800
|
+
rt.txs.set(this.id, this);
|
|
1801
|
+
db._txs.add(this);
|
|
1802
|
+
if (mode === "versionchange") {
|
|
1803
|
+
this.beforeUpgrade = new Map([...db._stores].map(([k, v]) => [k, structuredClone(v)]));
|
|
1804
|
+
this.nextIdBefore = db._nextId;
|
|
1805
|
+
}
|
|
1806
|
+
scheduler(db.name).add(this);
|
|
1807
|
+
}
|
|
1808
|
+
get objectStoreNames() {
|
|
1809
|
+
return stringList(this._scope);
|
|
1810
|
+
}
|
|
1811
|
+
get error() {
|
|
1812
|
+
return this._error;
|
|
1813
|
+
}
|
|
1814
|
+
get done() {
|
|
1815
|
+
return this._finishedPromise().then(() => {
|
|
1816
|
+
if (this._aborted)
|
|
1817
|
+
throw this._error ?? err("AbortError", "The transaction was aborted.");
|
|
1818
|
+
});
|
|
1819
|
+
}
|
|
1820
|
+
parentTarget() {
|
|
1821
|
+
return this.db;
|
|
1822
|
+
}
|
|
1823
|
+
_finishedPromise() {
|
|
1824
|
+
if (this.isFinished)
|
|
1825
|
+
return Promise.resolve();
|
|
1826
|
+
this.finished ??= new Promise((r) => this.markFinished = r);
|
|
1827
|
+
return this.finished;
|
|
1828
|
+
}
|
|
1829
|
+
objectStore(name) {
|
|
1830
|
+
if (this._state === "finished")
|
|
1831
|
+
throw err("InvalidStateError", "The transaction has finished.");
|
|
1832
|
+
const n = String(name);
|
|
1833
|
+
if (!this._scope.includes(n) || !this.db._stores.has(n))
|
|
1834
|
+
throw err("NotFoundError", `No object store named ${n} in this transaction.`);
|
|
1835
|
+
let h = this._handles.get(n);
|
|
1836
|
+
if (!h || h._def !== this.db._stores.get(n)) {
|
|
1837
|
+
h = new PlateauObjectStore(this, this.db._stores.get(n));
|
|
1838
|
+
this._handles.set(n, h);
|
|
1839
|
+
}
|
|
1840
|
+
return h;
|
|
1841
|
+
}
|
|
1842
|
+
abort() {
|
|
1843
|
+
if (this._state === "finished" || this._state === "committing")
|
|
1844
|
+
throw err("InvalidStateError", "The transaction has finished or is committing.");
|
|
1845
|
+
this._abort(null);
|
|
1846
|
+
}
|
|
1847
|
+
commit() {
|
|
1848
|
+
if (this._state !== "active")
|
|
1849
|
+
throw err("InvalidStateError", "The transaction is not active.");
|
|
1850
|
+
this._state = "inactive";
|
|
1851
|
+
this.settle(true);
|
|
1852
|
+
}
|
|
1853
|
+
_check(write) {
|
|
1854
|
+
if (this._state !== "active")
|
|
1855
|
+
throw err("TransactionInactiveError", "The transaction is not active.");
|
|
1856
|
+
if (write && this.mode === "readonly")
|
|
1857
|
+
throw err("ReadOnlyError", "The transaction is read-only.");
|
|
1858
|
+
}
|
|
1859
|
+
_issue(op, req) {
|
|
1860
|
+
this.unsent.push(op);
|
|
1861
|
+
this.queued(req);
|
|
1862
|
+
}
|
|
1863
|
+
_issueGet(s, key, req) {
|
|
1864
|
+
const last = this.unsent[this.unsent.length - 1];
|
|
1865
|
+
if (last !== undefined && last.o === "get" && last.s === s)
|
|
1866
|
+
last.ks.push(key);
|
|
1867
|
+
else
|
|
1868
|
+
this.unsent.push({ o: "get", s, ks: [key] });
|
|
1869
|
+
this.queued(req);
|
|
1870
|
+
}
|
|
1871
|
+
_issuePut(s, row, req) {
|
|
1872
|
+
const last = this.unsent[this.unsent.length - 1];
|
|
1873
|
+
if (last !== undefined && last.o === "put" && last.s === s)
|
|
1874
|
+
last.rows.push(row);
|
|
1875
|
+
else
|
|
1876
|
+
this.unsent.push({ o: "put", s, rows: [row] });
|
|
1877
|
+
this.queued(req);
|
|
1878
|
+
}
|
|
1879
|
+
_issueTyped(def, t, rest, add, gen, req) {
|
|
1880
|
+
let last = this.unsent[this.unsent.length - 1];
|
|
1881
|
+
if (last === undefined || last.o !== "tput" || last.s !== def.name) {
|
|
1882
|
+
last = { o: "tput", s: def.name, n: 0, cols: def.columns.map((c) => c.kind === "u32_multi" ? [[0], []] : []), v: null, add: [], gen: [] };
|
|
1883
|
+
this.unsent.push(last);
|
|
1884
|
+
}
|
|
1885
|
+
const cols = def.columns;
|
|
1886
|
+
for (let i = 0;i < cols.length; i++) {
|
|
1887
|
+
const b = last.cols[i];
|
|
1888
|
+
if (cols[i].kind === "u32_multi") {
|
|
1889
|
+
const [offsets, values] = b;
|
|
1890
|
+
for (const x of t[i])
|
|
1891
|
+
values.push(x);
|
|
1892
|
+
offsets.push(values.length);
|
|
1893
|
+
} else
|
|
1894
|
+
b.push(t[i]);
|
|
1895
|
+
}
|
|
1896
|
+
if (rest !== "" && last.v === null)
|
|
1897
|
+
last.v = new Array(last.n).fill("");
|
|
1898
|
+
if (last.v !== null)
|
|
1899
|
+
last.v.push(rest);
|
|
1900
|
+
if (add)
|
|
1901
|
+
last.add.push(last.n);
|
|
1902
|
+
if (gen)
|
|
1903
|
+
last.gen.push(last.n);
|
|
1904
|
+
last.n++;
|
|
1905
|
+
this.queued(req);
|
|
1906
|
+
}
|
|
1907
|
+
_issueDeleteNumber(s, key, req) {
|
|
1908
|
+
const last = this.unsent[this.unsent.length - 1];
|
|
1909
|
+
if (last !== undefined && last.o === "delk" && last.s === s && last.nums !== undefined)
|
|
1910
|
+
last.nums.push(key);
|
|
1911
|
+
else
|
|
1912
|
+
this.unsent.push({ o: "delk", s, ks: [], nums: [key] });
|
|
1913
|
+
this.queued(req);
|
|
1914
|
+
}
|
|
1915
|
+
_issueDelete(s, key, req) {
|
|
1916
|
+
const last = this.unsent[this.unsent.length - 1];
|
|
1917
|
+
if (last !== undefined && last.o === "delk" && last.s === s && last.nums === undefined)
|
|
1918
|
+
last.ks.push(key);
|
|
1919
|
+
else
|
|
1920
|
+
this.unsent.push({ o: "delk", s, ks: [key] });
|
|
1921
|
+
this.queued(req);
|
|
1922
|
+
}
|
|
1923
|
+
queued(req) {
|
|
1924
|
+
this.unsentReqs.push(req);
|
|
1925
|
+
if (!this.flushQueued) {
|
|
1926
|
+
this.flushQueued = true;
|
|
1927
|
+
queueMicrotask(() => this.flush());
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
local = new Set;
|
|
1931
|
+
_local(req, hit) {
|
|
1932
|
+
this.local.add(req);
|
|
1933
|
+
queueMicrotask(() => {
|
|
1934
|
+
this.local.delete(req);
|
|
1935
|
+
if (this._state === "finished")
|
|
1936
|
+
return;
|
|
1937
|
+
this._state = "active";
|
|
1938
|
+
this._settleSoon();
|
|
1939
|
+
this.deliver(req, hit, false);
|
|
1940
|
+
});
|
|
1941
|
+
}
|
|
1942
|
+
_internal(op) {
|
|
1943
|
+
this._issue(op, null);
|
|
1944
|
+
}
|
|
1945
|
+
_start() {
|
|
1946
|
+
this._started = true;
|
|
1947
|
+
this.flush();
|
|
1948
|
+
this.settle(false);
|
|
1949
|
+
}
|
|
1950
|
+
_settleSoon() {
|
|
1951
|
+
if (this.settleQueued)
|
|
1952
|
+
return;
|
|
1953
|
+
this.settleQueued = true;
|
|
1954
|
+
afterTask(() => {
|
|
1955
|
+
this.settleQueued = false;
|
|
1956
|
+
if (this._state === "active")
|
|
1957
|
+
this._state = "inactive";
|
|
1958
|
+
this.settle(false);
|
|
1959
|
+
});
|
|
1960
|
+
}
|
|
1961
|
+
message(ops, end) {
|
|
1962
|
+
const m = { t: "tx", tx: this.id, db: this.db.name, ops };
|
|
1963
|
+
if (!this.began) {
|
|
1964
|
+
this.began = true;
|
|
1965
|
+
m.begin = { mode: this.mode, stores: this._scope, version: this.version };
|
|
1966
|
+
}
|
|
1967
|
+
if (end)
|
|
1968
|
+
m.end = end;
|
|
1969
|
+
return m;
|
|
1970
|
+
}
|
|
1971
|
+
flush() {
|
|
1972
|
+
this.flushQueued = false;
|
|
1973
|
+
if (!this._started || this._state === "finished" || !this.unsent.length)
|
|
1974
|
+
return;
|
|
1975
|
+
const ops = this.unsent;
|
|
1976
|
+
this.unsent = [];
|
|
1977
|
+
const transfer = [];
|
|
1978
|
+
for (let i = 0;i < ops.length; i++) {
|
|
1979
|
+
const o = ops[i].o;
|
|
1980
|
+
if (o === "put" || o === "tput" || o === "del" || o === "delk" || o === "clear")
|
|
1981
|
+
(this.written ??= new Set).add(ops[i].s);
|
|
1982
|
+
if (o === "tput")
|
|
1983
|
+
ops[i] = wireTyped(ops[i], this.db._stores.get(ops[i].s), transfer);
|
|
1984
|
+
}
|
|
1985
|
+
if (this.head === this.inflight.length) {
|
|
1986
|
+
this.inflight = this.unsentReqs;
|
|
1987
|
+
this.head = 0;
|
|
1988
|
+
} else
|
|
1989
|
+
for (const r of this.unsentReqs)
|
|
1990
|
+
this.inflight.push(r);
|
|
1991
|
+
this.unsentReqs = [];
|
|
1992
|
+
runtime().post(this.message(ops), transfer);
|
|
1993
|
+
}
|
|
1994
|
+
settle(explicit) {
|
|
1995
|
+
if (this._state !== "inactive" || !this._started || this.commitSent)
|
|
1996
|
+
return;
|
|
1997
|
+
if (this.unsent.length)
|
|
1998
|
+
this.flush();
|
|
1999
|
+
if (this.local.size)
|
|
2000
|
+
return;
|
|
2001
|
+
if (this.head === this.inflight.length) {
|
|
2002
|
+
this.commitSent = true;
|
|
2003
|
+
this._state = "committing";
|
|
2004
|
+
if (this.mode === "readonly") {
|
|
2005
|
+
if (explicit)
|
|
2006
|
+
afterTask(() => this.complete());
|
|
2007
|
+
else
|
|
2008
|
+
this.complete();
|
|
2009
|
+
} else
|
|
2010
|
+
runtime().post(this.message([], "commit"));
|
|
2011
|
+
return;
|
|
2012
|
+
}
|
|
2013
|
+
if (this.mode === "readonly")
|
|
2014
|
+
return;
|
|
2015
|
+
if (explicit || this.quiet()) {
|
|
2016
|
+
this.commitSent = true;
|
|
2017
|
+
this._state = "committing";
|
|
2018
|
+
runtime().post(this.message([], explicit ? "commit" : "commitIfClean"));
|
|
2019
|
+
}
|
|
2020
|
+
}
|
|
2021
|
+
quiet() {
|
|
2022
|
+
for (let i = this.head;i < this.inflight.length; i++) {
|
|
2023
|
+
const r = this.inflight[i];
|
|
2024
|
+
if (r !== null && r.hasListeners())
|
|
2025
|
+
return false;
|
|
2026
|
+
}
|
|
2027
|
+
return true;
|
|
2028
|
+
}
|
|
2029
|
+
_onReply(r) {
|
|
2030
|
+
if (this._state === "finished")
|
|
2031
|
+
return;
|
|
2032
|
+
const errs = new Map;
|
|
2033
|
+
for (const [i, name, message] of r.errs)
|
|
2034
|
+
errs.set(i, [name, message]);
|
|
2035
|
+
if (r.res.length) {
|
|
2036
|
+
const prev = this._state;
|
|
2037
|
+
this._state = "active";
|
|
2038
|
+
this._settleSoon();
|
|
2039
|
+
const quietPath = !this.listensOnPath("success", false) && !this.db.listensOnPath("success", false);
|
|
2040
|
+
for (let i = 0;i < r.res.length; i++) {
|
|
2041
|
+
const req = this.inflight[this.head++];
|
|
2042
|
+
if (this._state === "finished")
|
|
2043
|
+
break;
|
|
2044
|
+
const e = errs.get(i);
|
|
2045
|
+
if (req === null || req === undefined) {
|
|
2046
|
+
if (e)
|
|
2047
|
+
this._abort(err(e[0], e[1]));
|
|
2048
|
+
continue;
|
|
2049
|
+
}
|
|
2050
|
+
if (e)
|
|
2051
|
+
this.deliverError(req, err(e[0], e[1]));
|
|
2052
|
+
else
|
|
2053
|
+
this.deliver(req, r.res[i], quietPath);
|
|
2054
|
+
}
|
|
2055
|
+
if (this._state === "active" && prev === "committing")
|
|
2056
|
+
this._state = "committing";
|
|
2057
|
+
}
|
|
2058
|
+
if (r.end) {
|
|
2059
|
+
if (this._state === "finished")
|
|
2060
|
+
return;
|
|
2061
|
+
if (r.end.ok)
|
|
2062
|
+
this.complete();
|
|
2063
|
+
else if (r.end.name === "Deferred") {
|
|
2064
|
+
this.commitSent = false;
|
|
2065
|
+
if (this._state === "committing")
|
|
2066
|
+
this._state = "inactive";
|
|
2067
|
+
this._settleSoon();
|
|
2068
|
+
} else
|
|
2069
|
+
this._abort(err(r.end.name, r.end.message));
|
|
2070
|
+
}
|
|
2071
|
+
}
|
|
2072
|
+
deliver(req, v, quietPath) {
|
|
2073
|
+
let result;
|
|
2074
|
+
switch (req.kind) {
|
|
2075
|
+
case "key":
|
|
2076
|
+
result = v === undefined ? req.known : decodeKey(v);
|
|
2077
|
+
break;
|
|
2078
|
+
case "keys":
|
|
2079
|
+
result = v.map(decodeKey);
|
|
2080
|
+
break;
|
|
2081
|
+
case "cursor":
|
|
2082
|
+
result = req.cursor._land(v);
|
|
2083
|
+
break;
|
|
2084
|
+
case "records":
|
|
2085
|
+
result = v.map((x) => ({ key: decodeKey(x.k), primaryKey: decodeKey(x.p), value: x.v }));
|
|
2086
|
+
break;
|
|
2087
|
+
case "query":
|
|
2088
|
+
req.plan = v.plan;
|
|
2089
|
+
result = v.value;
|
|
2090
|
+
break;
|
|
2091
|
+
default:
|
|
2092
|
+
result = v;
|
|
2093
|
+
}
|
|
2094
|
+
finishRequest(req, result, null);
|
|
2095
|
+
if (quietPath && !req.hasListeners())
|
|
2096
|
+
return;
|
|
2097
|
+
const out = dispatch(req, new PlateauEvent("success"));
|
|
2098
|
+
if (out.threw)
|
|
2099
|
+
this._abort(err("AbortError", "An exception was thrown in a success handler."));
|
|
2100
|
+
}
|
|
2101
|
+
deliverError(req, e) {
|
|
2102
|
+
if (req.cursor)
|
|
2103
|
+
req.cursor._land(null);
|
|
2104
|
+
finishRequest(req, undefined, e);
|
|
2105
|
+
const ev = new PlateauEvent("error", { bubbles: true, cancelable: true });
|
|
2106
|
+
const out = dispatch(req, ev);
|
|
2107
|
+
if (this._state === "finished")
|
|
2108
|
+
return;
|
|
2109
|
+
if (out.threw)
|
|
2110
|
+
this._abort(err("AbortError", "An exception was thrown in an error handler."));
|
|
2111
|
+
else if (!ev.defaultPrevented)
|
|
2112
|
+
this._abort(e);
|
|
2113
|
+
}
|
|
2114
|
+
complete() {
|
|
2115
|
+
if (this._state === "finished")
|
|
2116
|
+
return;
|
|
2117
|
+
this._state = "finished";
|
|
2118
|
+
runtime().finished(this.id);
|
|
2119
|
+
dispatch(this, new PlateauEvent("complete"));
|
|
2120
|
+
this.finish();
|
|
2121
|
+
if (this.written && this.mode === "readwrite") {
|
|
2122
|
+
const stores = [...this.written];
|
|
2123
|
+
runtime().changed(this.db.name, stores);
|
|
2124
|
+
changed(this.db.name, stores);
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
2127
|
+
finish() {
|
|
2128
|
+
this.db._txs.delete(this);
|
|
2129
|
+
scheduler(this.db.name).done(this);
|
|
2130
|
+
this.isFinished = true;
|
|
2131
|
+
this.markFinished?.();
|
|
2132
|
+
this.db._maybeClosed();
|
|
2133
|
+
}
|
|
2134
|
+
_abort(error) {
|
|
2135
|
+
if (this._state === "finished")
|
|
2136
|
+
return;
|
|
2137
|
+
this._state = "finished";
|
|
2138
|
+
this._aborted = true;
|
|
2139
|
+
this._error = error;
|
|
2140
|
+
if (this.beforeUpgrade) {
|
|
2141
|
+
this.db._stores = this.beforeUpgrade;
|
|
2142
|
+
this.db._nextId = this.nextIdBefore;
|
|
2143
|
+
}
|
|
2144
|
+
if (this.began && this.mode !== "readonly")
|
|
2145
|
+
runtime().post(this.message([], "abort"));
|
|
2146
|
+
runtime().finished(this.id);
|
|
2147
|
+
const reqs = [...this.inflight.slice(this.head), ...this.unsentReqs, ...this.local].filter((r) => r !== null);
|
|
2148
|
+
this.local.clear();
|
|
2149
|
+
this.inflight = [];
|
|
2150
|
+
this.head = 0;
|
|
2151
|
+
this.unsent = [];
|
|
2152
|
+
this.unsentReqs = [];
|
|
2153
|
+
afterTask(() => {
|
|
2154
|
+
for (const req of reqs) {
|
|
2155
|
+
if (req.cursor)
|
|
2156
|
+
req.cursor._land(null);
|
|
2157
|
+
finishRequest(req, undefined, err("AbortError", "The transaction was aborted."));
|
|
2158
|
+
dispatch(req, new PlateauEvent("error", { bubbles: true, cancelable: true }));
|
|
2159
|
+
}
|
|
2160
|
+
dispatch(this, new PlateauEvent("abort", { bubbles: true }));
|
|
2161
|
+
this.finish();
|
|
2162
|
+
});
|
|
2163
|
+
}
|
|
2164
|
+
}
|
|
2165
|
+
handlers(PlateauTransaction, ["abort", "complete", "error"]);
|
|
2166
|
+
function direction(d) {
|
|
2167
|
+
const s = d === undefined ? "next" : String(d);
|
|
2168
|
+
if (s !== "next" && s !== "nextunique" && s !== "prev" && s !== "prevunique")
|
|
2169
|
+
throw new TypeError(`The direction ${s} is not valid.`);
|
|
2170
|
+
return s;
|
|
2171
|
+
}
|
|
2172
|
+
function count2(n) {
|
|
2173
|
+
if (n === undefined)
|
|
2174
|
+
return 0;
|
|
2175
|
+
const x = Number(n);
|
|
2176
|
+
if (!Number.isFinite(x) || x < 0 || x > 4294967295)
|
|
2177
|
+
throw new TypeError("The count is not valid.");
|
|
2178
|
+
return Math.floor(x);
|
|
2179
|
+
}
|
|
2180
|
+
function pointKey(q) {
|
|
2181
|
+
if (q instanceof PlateauKeyRange || typeof IDBKeyRange !== "undefined" && q instanceof IDBKeyRange)
|
|
2182
|
+
return;
|
|
2183
|
+
return requireKey(q);
|
|
2184
|
+
}
|
|
2185
|
+
|
|
2186
|
+
class PlateauObjectStore {
|
|
2187
|
+
_tx;
|
|
2188
|
+
_def;
|
|
2189
|
+
indexHandles = new Map;
|
|
2190
|
+
keyPathValue;
|
|
2191
|
+
constructor(tx, def) {
|
|
2192
|
+
this._tx = tx;
|
|
2193
|
+
this._def = def;
|
|
2194
|
+
this.keyPathValue = Array.isArray(def.keyPath) ? [...def.keyPath] : def.keyPath;
|
|
2195
|
+
}
|
|
2196
|
+
get name() {
|
|
2197
|
+
return this._def.name;
|
|
2198
|
+
}
|
|
2199
|
+
get keyPath() {
|
|
2200
|
+
return this.keyPathValue;
|
|
2201
|
+
}
|
|
2202
|
+
get autoIncrement() {
|
|
2203
|
+
return this._def.autoIncrement;
|
|
2204
|
+
}
|
|
2205
|
+
get indexNames() {
|
|
2206
|
+
return stringList(this._def.indexes.map((i) => i.name));
|
|
2207
|
+
}
|
|
2208
|
+
get transaction() {
|
|
2209
|
+
return this._tx;
|
|
2210
|
+
}
|
|
2211
|
+
_live() {
|
|
2212
|
+
if (this._tx.db._stores.get(this._def.name) !== this._def)
|
|
2213
|
+
throw err("InvalidStateError", "The object store has been deleted.");
|
|
2214
|
+
}
|
|
2215
|
+
_ready(write) {
|
|
2216
|
+
this._live();
|
|
2217
|
+
this._tx._check(write);
|
|
2218
|
+
}
|
|
2219
|
+
put(value, key) {
|
|
2220
|
+
return this.store(value, key, false, arguments.length > 1);
|
|
2221
|
+
}
|
|
2222
|
+
add(value, key) {
|
|
2223
|
+
return this.store(value, key, true, arguments.length > 1);
|
|
2224
|
+
}
|
|
2225
|
+
store(value, key, add, keyGiven, source) {
|
|
2226
|
+
this._ready(true);
|
|
2227
|
+
const def = this._def;
|
|
2228
|
+
const inline = def.keyPath !== null;
|
|
2229
|
+
keyGiven = keyGiven && key !== undefined;
|
|
2230
|
+
if (inline && keyGiven)
|
|
2231
|
+
throw dataError("The object store uses in-line keys and the key parameter was provided.");
|
|
2232
|
+
if (!inline && !def.autoIncrement && !keyGiven)
|
|
2233
|
+
throw dataError("The object store uses out-of-line keys and has no key generator and the key parameter was not provided.");
|
|
2234
|
+
let k;
|
|
2235
|
+
if (keyGiven)
|
|
2236
|
+
k = requireKey(key);
|
|
2237
|
+
if (def.columns) {
|
|
2238
|
+
const [t, rest, pk] = splitTyped(def, value);
|
|
2239
|
+
if (pk === undefined && !def.autoIncrement)
|
|
2240
|
+
throw dataError("The record has no number at the key path.");
|
|
2241
|
+
const req = new PlateauRequest(source ?? this, this._tx, "key");
|
|
2242
|
+
if (pk !== undefined)
|
|
2243
|
+
req.known = pk;
|
|
2244
|
+
this._tx._issueTyped(def, t, rest, add, pk === undefined, req);
|
|
2245
|
+
return req;
|
|
2246
|
+
}
|
|
2247
|
+
const row = { v: "", add };
|
|
2248
|
+
{
|
|
2249
|
+
row.v = encodeValue(value);
|
|
2250
|
+
if (inline) {
|
|
2251
|
+
const r = extractKey(value, def.keyPath);
|
|
2252
|
+
if (r === "invalid")
|
|
2253
|
+
throw dataError("Evaluating the object store's key path yielded a value that is not a valid key.");
|
|
2254
|
+
if (r === "missing") {
|
|
2255
|
+
if (!def.autoIncrement)
|
|
2256
|
+
throw dataError("Evaluating the object store's key path did not yield a value.");
|
|
2257
|
+
if (!canInjectKey(value, def.keyPath))
|
|
2258
|
+
throw dataError("A generated key could not be inserted into the value.");
|
|
2259
|
+
} else
|
|
2260
|
+
k = r.key;
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
2263
|
+
if (k !== undefined)
|
|
2264
|
+
row.k = k;
|
|
2265
|
+
const req = new PlateauRequest(source ?? this, this._tx, "key");
|
|
2266
|
+
this._tx._issuePut(def.name, row, req);
|
|
2267
|
+
return req;
|
|
2268
|
+
}
|
|
2269
|
+
delete(query) {
|
|
2270
|
+
this._ready(true);
|
|
2271
|
+
if (this._def.columns && typeof query === "number" && !Number.isNaN(query)) {
|
|
2272
|
+
const req = new PlateauRequest(this, this._tx, "none");
|
|
2273
|
+
this._tx._issueDeleteNumber(this._def.name, query, req);
|
|
2274
|
+
return req;
|
|
2275
|
+
}
|
|
2276
|
+
const k = query === undefined || query === null ? undefined : pointKey(query);
|
|
2277
|
+
if (k !== undefined) {
|
|
2278
|
+
const req = new PlateauRequest(this, this._tx, "none");
|
|
2279
|
+
this._tx._issueDelete(this._def.name, k, req);
|
|
2280
|
+
return req;
|
|
2281
|
+
}
|
|
2282
|
+
const r = toRange(query, false);
|
|
2283
|
+
return this.request({ o: "del", s: this._def.name, r }, "none");
|
|
2284
|
+
}
|
|
2285
|
+
clear() {
|
|
2286
|
+
this._ready(true);
|
|
2287
|
+
return this.request({ o: "clear", s: this._def.name }, "none");
|
|
2288
|
+
}
|
|
2289
|
+
get(query) {
|
|
2290
|
+
this._ready(false);
|
|
2291
|
+
const k = pointKey(query === undefined || query === null ? requireRange(query) : query);
|
|
2292
|
+
if (k !== undefined) {
|
|
2293
|
+
const req = new PlateauRequest(this, this._tx, "value");
|
|
2294
|
+
this._tx._issueGet(this._def.name, k, req);
|
|
2295
|
+
return req;
|
|
2296
|
+
}
|
|
2297
|
+
return this.request({ o: "first", s: this._def.name, r: toRange(query, false), key: false }, "value");
|
|
2298
|
+
}
|
|
2299
|
+
getKey(query) {
|
|
2300
|
+
this._ready(false);
|
|
2301
|
+
return this.request({ o: "first", s: this._def.name, r: toRange(query, false), key: true }, "key");
|
|
2302
|
+
}
|
|
2303
|
+
getAll(query, n) {
|
|
2304
|
+
this._ready(false);
|
|
2305
|
+
return this.request({ o: "all", s: this._def.name, r: toRange(query, true), n: count2(n), key: false }, "values");
|
|
2306
|
+
}
|
|
2307
|
+
getAllKeys(query, n) {
|
|
2308
|
+
this._ready(false);
|
|
2309
|
+
return this.request({ o: "all", s: this._def.name, r: toRange(query, true), n: count2(n), key: true }, "keys");
|
|
2310
|
+
}
|
|
2311
|
+
count(query) {
|
|
2312
|
+
this._ready(false);
|
|
2313
|
+
return this.request({ o: "count", s: this._def.name, r: toRange(query, true) }, "count");
|
|
2314
|
+
}
|
|
2315
|
+
getAllRecords(o = {}) {
|
|
2316
|
+
this._ready(false);
|
|
2317
|
+
return this.request({ o: "recs", s: this._def.name, r: toRange(o?.query, true), n: count2(o?.count), d: direction(o?.direction) }, "records");
|
|
2318
|
+
}
|
|
2319
|
+
openCursor(query, dir) {
|
|
2320
|
+
return this.cursor(query, dir, true);
|
|
2321
|
+
}
|
|
2322
|
+
openKeyCursor(query, dir) {
|
|
2323
|
+
return this.cursor(query, dir, false);
|
|
2324
|
+
}
|
|
2325
|
+
query(q) {
|
|
2326
|
+
this._ready(false);
|
|
2327
|
+
return this.request({ o: "query", s: this._def.name, q: JSON.stringify(q) }, "query");
|
|
2328
|
+
}
|
|
2329
|
+
cursor(query, dir, values) {
|
|
2330
|
+
this._ready(false);
|
|
2331
|
+
const d = direction(dir);
|
|
2332
|
+
const r = toRange(query, true);
|
|
2333
|
+
const req = new PlateauRequest(this, this._tx, "cursor");
|
|
2334
|
+
req.cursor = values ? new PlateauCursorWithValue(req, this, this, undefined, r, d, true) : new PlateauCursor(req, this, this, undefined, r, d, false);
|
|
2335
|
+
req.cursor._step(undefined, 1);
|
|
2336
|
+
return req;
|
|
2337
|
+
}
|
|
2338
|
+
request(op, kind) {
|
|
2339
|
+
const req = new PlateauRequest(this, this._tx, kind);
|
|
2340
|
+
this._tx._issue(op, req);
|
|
2341
|
+
return req;
|
|
2342
|
+
}
|
|
2343
|
+
index(name) {
|
|
2344
|
+
if (this._tx._state === "finished")
|
|
2345
|
+
throw err("InvalidStateError", "The transaction has finished.");
|
|
2346
|
+
this._live();
|
|
2347
|
+
const n = String(name);
|
|
2348
|
+
const def = this._def.indexes.find((i) => i.name === n);
|
|
2349
|
+
if (!def)
|
|
2350
|
+
throw err("NotFoundError", `No index named ${n}.`);
|
|
2351
|
+
let h = this.indexHandles.get(n);
|
|
2352
|
+
if (!h || h._def !== def) {
|
|
2353
|
+
h = new PlateauIndex(this, def);
|
|
2354
|
+
this.indexHandles.set(n, h);
|
|
2355
|
+
}
|
|
2356
|
+
return h;
|
|
2357
|
+
}
|
|
2358
|
+
createIndex(name, keyPath, options = {}) {
|
|
2359
|
+
const tx = this._tx;
|
|
2360
|
+
if (tx.mode !== "versionchange")
|
|
2361
|
+
throw err("InvalidStateError", "Not in a version change transaction.");
|
|
2362
|
+
this._live();
|
|
2363
|
+
if (tx._state !== "active")
|
|
2364
|
+
throw err("TransactionInactiveError", "The version change transaction is not active.");
|
|
2365
|
+
const n = String(name);
|
|
2366
|
+
if (this._def.indexes.some((i) => i.name === n))
|
|
2367
|
+
throw err("ConstraintError", `An index named ${n} already exists.`);
|
|
2368
|
+
const kp = normalizeKeyPath(keyPath);
|
|
2369
|
+
if (!validKeyPath(kp))
|
|
2370
|
+
throw err("SyntaxError", "The keyPath is not a valid key path.");
|
|
2371
|
+
const multiEntry = !!options?.multiEntry;
|
|
2372
|
+
if (multiEntry && Array.isArray(kp))
|
|
2373
|
+
throw err("InvalidAccessError", "A multiEntry index cannot have an array key path.");
|
|
2374
|
+
const def = { id: tx.db._nextId++, name: n, keyPath: kp, unique: !!options?.unique, multiEntry };
|
|
2375
|
+
this._def.indexes.push(def);
|
|
2376
|
+
tx._internal({ o: "mkIndex", s: this._def.name, def: structuredClone(def) });
|
|
2377
|
+
return this.index(n);
|
|
2378
|
+
}
|
|
2379
|
+
deleteIndex(name) {
|
|
2380
|
+
const tx = this._tx;
|
|
2381
|
+
if (tx.mode !== "versionchange")
|
|
2382
|
+
throw err("InvalidStateError", "Not in a version change transaction.");
|
|
2383
|
+
this._live();
|
|
2384
|
+
if (tx._state !== "active")
|
|
2385
|
+
throw err("TransactionInactiveError", "The version change transaction is not active.");
|
|
2386
|
+
const n = String(name);
|
|
2387
|
+
const i = this._def.indexes.findIndex((x) => x.name === n);
|
|
2388
|
+
if (i < 0)
|
|
2389
|
+
throw err("NotFoundError", `No index named ${n}.`);
|
|
2390
|
+
this._def.indexes.splice(i, 1);
|
|
2391
|
+
tx._internal({ o: "rmIndex", s: this._def.name, i: n });
|
|
2392
|
+
}
|
|
2393
|
+
}
|
|
2394
|
+
function requireRange(q) {
|
|
2395
|
+
throw dataError("No key or key range specified.");
|
|
2396
|
+
}
|
|
2397
|
+
var layouts = new WeakMap;
|
|
2398
|
+
function layoutOf(def) {
|
|
2399
|
+
let l = layouts.get(def);
|
|
2400
|
+
if (!l) {
|
|
2401
|
+
const cols = def.columns;
|
|
2402
|
+
l = { names: cols.map((c) => c.name), kinds: cols.map((c) => c.kind), pkAt: cols.findIndex((c) => c.name === def.keyPath), declared: new Set(cols.map((c) => c.name)) };
|
|
2403
|
+
layouts.set(def, l);
|
|
2404
|
+
}
|
|
2405
|
+
return l;
|
|
2406
|
+
}
|
|
2407
|
+
function splitTyped(def, value) {
|
|
2408
|
+
const proto = value !== null && typeof value === "object" ? Object.getPrototypeOf(value) : undefined;
|
|
2409
|
+
if (proto !== Object.prototype && proto !== null)
|
|
2410
|
+
throw dataError("plateau: a store with columns takes plain objects.");
|
|
2411
|
+
const v = value;
|
|
2412
|
+
const { names, kinds, pkAt, declared } = layoutOf(def);
|
|
2413
|
+
const n = names.length;
|
|
2414
|
+
const t = new Array(n);
|
|
2415
|
+
let pk;
|
|
2416
|
+
for (let i = 0;i < n; i++) {
|
|
2417
|
+
const x = v[names[i]];
|
|
2418
|
+
if (i === pkAt && x === undefined && def.autoIncrement && !Object.prototype.hasOwnProperty.call(v, names[i])) {
|
|
2419
|
+
t[i] = 0;
|
|
2420
|
+
continue;
|
|
2421
|
+
}
|
|
2422
|
+
if (!fits(kinds[i], x) || !Object.prototype.hasOwnProperty.call(v, names[i])) {
|
|
2423
|
+
throw dataError(`plateau: field ${names[i]} must hold a ${kinds[i]} value (declared column).`);
|
|
2424
|
+
}
|
|
2425
|
+
t[i] = kinds[i] === "u32_multi" ? x.slice() : x;
|
|
2426
|
+
if (i === pkAt)
|
|
2427
|
+
pk = x;
|
|
2428
|
+
}
|
|
2429
|
+
const keys = Object.keys(v);
|
|
2430
|
+
let rest = "";
|
|
2431
|
+
if (keys.length !== n) {
|
|
2432
|
+
const r = {};
|
|
2433
|
+
for (const k of keys)
|
|
2434
|
+
if (!declared.has(k))
|
|
2435
|
+
r[k] = v[k];
|
|
2436
|
+
rest = encodeValue(r);
|
|
2437
|
+
}
|
|
2438
|
+
return [t, rest, pk];
|
|
2439
|
+
}
|
|
2440
|
+
function wireTyped(p, def, transfer) {
|
|
2441
|
+
const cols = def.columns.map((c, i) => {
|
|
2442
|
+
const b = p.cols[i];
|
|
2443
|
+
switch (c.kind) {
|
|
2444
|
+
case "f64": {
|
|
2445
|
+
const a = Float64Array.from(b);
|
|
2446
|
+
transfer.push(a.buffer);
|
|
2447
|
+
return a;
|
|
2448
|
+
}
|
|
2449
|
+
case "u32": {
|
|
2450
|
+
const a = Uint32Array.from(b);
|
|
2451
|
+
transfer.push(a.buffer);
|
|
2452
|
+
return a;
|
|
2453
|
+
}
|
|
2454
|
+
case "u32_multi": {
|
|
2455
|
+
const [o, v] = b;
|
|
2456
|
+
const pair = [Uint32Array.from(o), Uint32Array.from(v)];
|
|
2457
|
+
transfer.push(pair[0].buffer, pair[1].buffer);
|
|
2458
|
+
return pair;
|
|
2459
|
+
}
|
|
2460
|
+
default:
|
|
2461
|
+
return b;
|
|
2462
|
+
}
|
|
2463
|
+
});
|
|
2464
|
+
return { o: "tput", s: p.s, n: p.n, cols, v: p.v, add: p.add, gen: p.gen };
|
|
2465
|
+
}
|
|
2466
|
+
function fits(kind, x) {
|
|
2467
|
+
switch (kind) {
|
|
2468
|
+
case "f64":
|
|
2469
|
+
return typeof x === "number" && !Number.isNaN(x);
|
|
2470
|
+
case "u32":
|
|
2471
|
+
return typeof x === "number" && Number.isInteger(x) && x >= 0 && x <= 4294967295;
|
|
2472
|
+
case "str":
|
|
2473
|
+
return typeof x === "string";
|
|
2474
|
+
case "u32_multi": {
|
|
2475
|
+
if (!Array.isArray(x))
|
|
2476
|
+
return false;
|
|
2477
|
+
for (let i = 0;i < x.length; i++) {
|
|
2478
|
+
const y = x[i];
|
|
2479
|
+
if (typeof y !== "number" || !Number.isInteger(y) || y < 0 || y > 4294967295)
|
|
2480
|
+
return false;
|
|
2481
|
+
}
|
|
2482
|
+
return true;
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
}
|
|
2486
|
+
|
|
2487
|
+
class PlateauIndex {
|
|
2488
|
+
objectStore;
|
|
2489
|
+
_def;
|
|
2490
|
+
keyPathValue;
|
|
2491
|
+
constructor(store, def) {
|
|
2492
|
+
this.objectStore = store;
|
|
2493
|
+
this._def = def;
|
|
2494
|
+
this.keyPathValue = Array.isArray(def.keyPath) ? [...def.keyPath] : def.keyPath;
|
|
2495
|
+
}
|
|
2496
|
+
get name() {
|
|
2497
|
+
return this._def.name;
|
|
2498
|
+
}
|
|
2499
|
+
get keyPath() {
|
|
2500
|
+
return this.keyPathValue;
|
|
2501
|
+
}
|
|
2502
|
+
get unique() {
|
|
2503
|
+
return this._def.unique;
|
|
2504
|
+
}
|
|
2505
|
+
get multiEntry() {
|
|
2506
|
+
return this._def.multiEntry;
|
|
2507
|
+
}
|
|
2508
|
+
_live() {
|
|
2509
|
+
this.objectStore._live();
|
|
2510
|
+
if (!this.objectStore._def.indexes.includes(this._def))
|
|
2511
|
+
throw err("InvalidStateError", "The index has been deleted.");
|
|
2512
|
+
}
|
|
2513
|
+
_ready() {
|
|
2514
|
+
this._live();
|
|
2515
|
+
this.objectStore._tx._check(false);
|
|
2516
|
+
}
|
|
2517
|
+
request(op, kind) {
|
|
2518
|
+
const req = new PlateauRequest(this, this.objectStore._tx, kind);
|
|
2519
|
+
this.objectStore._tx._issue(op, req);
|
|
2520
|
+
return req;
|
|
2521
|
+
}
|
|
2522
|
+
get s() {
|
|
2523
|
+
return this.objectStore._def.name;
|
|
2524
|
+
}
|
|
2525
|
+
get(query) {
|
|
2526
|
+
this._ready();
|
|
2527
|
+
return this.request({ o: "first", s: this.s, i: this._def.name, r: toRange(query, false), key: false }, "value");
|
|
2528
|
+
}
|
|
2529
|
+
getKey(query) {
|
|
2530
|
+
this._ready();
|
|
2531
|
+
return this.request({ o: "first", s: this.s, i: this._def.name, r: toRange(query, false), key: true }, "key");
|
|
2532
|
+
}
|
|
2533
|
+
getAll(query, n) {
|
|
2534
|
+
this._ready();
|
|
2535
|
+
return this.request({ o: "all", s: this.s, i: this._def.name, r: toRange(query, true), n: count2(n), key: false }, "values");
|
|
2536
|
+
}
|
|
2537
|
+
getAllKeys(query, n) {
|
|
2538
|
+
this._ready();
|
|
2539
|
+
return this.request({ o: "all", s: this.s, i: this._def.name, r: toRange(query, true), n: count2(n), key: true }, "keys");
|
|
2540
|
+
}
|
|
2541
|
+
count(query) {
|
|
2542
|
+
this._ready();
|
|
2543
|
+
return this.request({ o: "count", s: this.s, i: this._def.name, r: toRange(query, true) }, "count");
|
|
2544
|
+
}
|
|
2545
|
+
getAllRecords(o = {}) {
|
|
2546
|
+
this._ready();
|
|
2547
|
+
return this.request({ o: "recs", s: this.s, i: this._def.name, r: toRange(o?.query, true), n: count2(o?.count), d: direction(o?.direction) }, "records");
|
|
2548
|
+
}
|
|
2549
|
+
openCursor(query, dir) {
|
|
2550
|
+
return this.cursor(query, dir, true);
|
|
2551
|
+
}
|
|
2552
|
+
openKeyCursor(query, dir) {
|
|
2553
|
+
return this.cursor(query, dir, false);
|
|
2554
|
+
}
|
|
2555
|
+
cursor(query, dir, values) {
|
|
2556
|
+
this._ready();
|
|
2557
|
+
const d = direction(dir);
|
|
2558
|
+
const r = toRange(query, true);
|
|
2559
|
+
const req = new PlateauRequest(this, this.objectStore._tx, "cursor");
|
|
2560
|
+
req.cursor = values ? new PlateauCursorWithValue(req, this, this.objectStore, this._def.name, r, d, true) : new PlateauCursor(req, this, this.objectStore, this._def.name, r, d, false);
|
|
2561
|
+
req.cursor._step(undefined, 1);
|
|
2562
|
+
return req;
|
|
2563
|
+
}
|
|
2564
|
+
}
|
|
2565
|
+
|
|
2566
|
+
class PlateauCursor {
|
|
2567
|
+
request;
|
|
2568
|
+
source;
|
|
2569
|
+
direction;
|
|
2570
|
+
store;
|
|
2571
|
+
index;
|
|
2572
|
+
range;
|
|
2573
|
+
withValues;
|
|
2574
|
+
at;
|
|
2575
|
+
gotValue = false;
|
|
2576
|
+
keyValue;
|
|
2577
|
+
pkValue;
|
|
2578
|
+
_value;
|
|
2579
|
+
ahead = [];
|
|
2580
|
+
aheadEnd = false;
|
|
2581
|
+
constructor(req, source, store, index, range, dir, values) {
|
|
2582
|
+
this.request = req;
|
|
2583
|
+
this.source = source;
|
|
2584
|
+
this.store = store;
|
|
2585
|
+
this.index = index;
|
|
2586
|
+
this.range = range;
|
|
2587
|
+
this.direction = dir;
|
|
2588
|
+
this.withValues = values;
|
|
2589
|
+
}
|
|
2590
|
+
get key() {
|
|
2591
|
+
return this.keyValue;
|
|
2592
|
+
}
|
|
2593
|
+
get primaryKey() {
|
|
2594
|
+
return this.pkValue;
|
|
2595
|
+
}
|
|
2596
|
+
_step(to, n) {
|
|
2597
|
+
const tx = this.store._tx;
|
|
2598
|
+
const ahead = tx.mode === "readonly" && to === undefined && n === 1 && (this.direction === "next" || this.direction === "prev");
|
|
2599
|
+
if (ahead && (this.ahead.length || this.aheadEnd)) {
|
|
2600
|
+
this.request._done = false;
|
|
2601
|
+
tx._local(this.request, this.ahead.shift() ?? null);
|
|
2602
|
+
return;
|
|
2603
|
+
}
|
|
2604
|
+
this.ahead = [];
|
|
2605
|
+
this.aheadEnd = false;
|
|
2606
|
+
const op = { o: "cur", s: this.store._def.name, r: this.range, d: this.direction, n, v: this.withValues };
|
|
2607
|
+
if (tx.mode === "readonly" && (this.direction === "next" || this.direction === "prev"))
|
|
2608
|
+
op.pre = PREFETCH;
|
|
2609
|
+
if (this.index !== undefined)
|
|
2610
|
+
op.i = this.index;
|
|
2611
|
+
if (this.at)
|
|
2612
|
+
op.at = this.at;
|
|
2613
|
+
if (to)
|
|
2614
|
+
op.to = to;
|
|
2615
|
+
this.request._done = false;
|
|
2616
|
+
this.store._tx._issue(op, this.request);
|
|
2617
|
+
}
|
|
2618
|
+
_land(hit) {
|
|
2619
|
+
if (hit?.more !== undefined) {
|
|
2620
|
+
this.ahead = hit.more;
|
|
2621
|
+
this.aheadEnd = this.ahead.length < PREFETCH;
|
|
2622
|
+
delete hit.more;
|
|
2623
|
+
}
|
|
2624
|
+
if (!hit) {
|
|
2625
|
+
this.ahead = [];
|
|
2626
|
+
this.aheadEnd = false;
|
|
2627
|
+
this.at = undefined;
|
|
2628
|
+
this.keyValue = this.pkValue = this._value = undefined;
|
|
2629
|
+
return null;
|
|
2630
|
+
}
|
|
2631
|
+
this.at = [hit.k, hit.p];
|
|
2632
|
+
this.keyValue = decodeKey(hit.k);
|
|
2633
|
+
this.pkValue = hit.k === hit.p ? this.keyValue : decodeKey(hit.p);
|
|
2634
|
+
this._value = hit.v;
|
|
2635
|
+
this.gotValue = true;
|
|
2636
|
+
return this;
|
|
2637
|
+
}
|
|
2638
|
+
ready() {
|
|
2639
|
+
this.store._tx._check(false);
|
|
2640
|
+
if (this.source instanceof PlateauIndex)
|
|
2641
|
+
this.source._live();
|
|
2642
|
+
else
|
|
2643
|
+
this.store._live();
|
|
2644
|
+
if (!this.gotValue)
|
|
2645
|
+
throw err("InvalidStateError", "The cursor is being iterated or has iterated past its end.");
|
|
2646
|
+
}
|
|
2647
|
+
advance(count) {
|
|
2648
|
+
const n = Number(count);
|
|
2649
|
+
if (!Number.isFinite(n) || n < 1 || n > 4294967295)
|
|
2650
|
+
throw new TypeError("The count must be a positive integer.");
|
|
2651
|
+
this.ready();
|
|
2652
|
+
this.gotValue = false;
|
|
2653
|
+
this._step(undefined, Math.floor(n));
|
|
2654
|
+
}
|
|
2655
|
+
continue(key) {
|
|
2656
|
+
this.ready();
|
|
2657
|
+
let to;
|
|
2658
|
+
if (key !== undefined) {
|
|
2659
|
+
const k = requireKey(key);
|
|
2660
|
+
const cur = this.at[0];
|
|
2661
|
+
const forward = this.direction === "next" || this.direction === "nextunique";
|
|
2662
|
+
if (forward ? k <= cur : k >= cur)
|
|
2663
|
+
throw dataError("The key is not past the cursor's position in its direction.");
|
|
2664
|
+
to = [k];
|
|
2665
|
+
}
|
|
2666
|
+
this.gotValue = false;
|
|
2667
|
+
this._step(to, 1);
|
|
2668
|
+
}
|
|
2669
|
+
continuePrimaryKey(key, primaryKey) {
|
|
2670
|
+
this.store._tx._check(false);
|
|
2671
|
+
if (this.source instanceof PlateauIndex)
|
|
2672
|
+
this.source._live();
|
|
2673
|
+
else
|
|
2674
|
+
this.store._live();
|
|
2675
|
+
if (!(this.source instanceof PlateauIndex))
|
|
2676
|
+
throw err("InvalidAccessError", "continuePrimaryKey needs an index cursor.");
|
|
2677
|
+
if (this.direction !== "next" && this.direction !== "prev")
|
|
2678
|
+
throw err("InvalidAccessError", "continuePrimaryKey needs a next or prev cursor.");
|
|
2679
|
+
if (!this.gotValue)
|
|
2680
|
+
throw err("InvalidStateError", "The cursor is being iterated or has iterated past its end.");
|
|
2681
|
+
const k = requireKey(key);
|
|
2682
|
+
const p = requireKey(primaryKey);
|
|
2683
|
+
const [ck, cp] = this.at;
|
|
2684
|
+
const forward = this.direction === "next";
|
|
2685
|
+
const c = k < ck ? -1 : k > ck ? 1 : 0;
|
|
2686
|
+
if (forward ? c < 0 || c === 0 && p <= cp : c > 0 || c === 0 && p >= cp)
|
|
2687
|
+
throw dataError("The key is not past the cursor's position in its direction.");
|
|
2688
|
+
this.gotValue = false;
|
|
2689
|
+
this._step([k, p], 1);
|
|
2690
|
+
}
|
|
2691
|
+
update(value) {
|
|
2692
|
+
const tx = this.store._tx;
|
|
2693
|
+
tx._check(false);
|
|
2694
|
+
if (this.source instanceof PlateauIndex)
|
|
2695
|
+
this.source._live();
|
|
2696
|
+
else
|
|
2697
|
+
this.store._live();
|
|
2698
|
+
if (tx.mode === "readonly")
|
|
2699
|
+
throw err("ReadOnlyError", "The transaction is read-only.");
|
|
2700
|
+
if (!this.gotValue)
|
|
2701
|
+
throw err("InvalidStateError", "The cursor is being iterated or has iterated past its end.");
|
|
2702
|
+
if (!this.withValues)
|
|
2703
|
+
throw err("InvalidStateError", "A key cursor has no value.");
|
|
2704
|
+
const def = this.store._def;
|
|
2705
|
+
if (def.keyPath !== null) {
|
|
2706
|
+
encodeValue(value);
|
|
2707
|
+
const r = def.columns ? { key: encodeKey(value?.[def.keyPath]) } : extractKey(value, def.keyPath);
|
|
2708
|
+
if (typeof r !== "object" || r.key !== this.at[1])
|
|
2709
|
+
throw dataError("The value's key does not match the cursor's primary key.");
|
|
2710
|
+
return this.store.store(value, undefined, false, false, this);
|
|
2711
|
+
}
|
|
2712
|
+
return this.store.store(value, this.pkValue, false, true, this);
|
|
2713
|
+
}
|
|
2714
|
+
delete() {
|
|
2715
|
+
const tx = this.store._tx;
|
|
2716
|
+
tx._check(false);
|
|
2717
|
+
if (this.source instanceof PlateauIndex)
|
|
2718
|
+
this.source._live();
|
|
2719
|
+
else
|
|
2720
|
+
this.store._live();
|
|
2721
|
+
if (tx.mode === "readonly")
|
|
2722
|
+
throw err("ReadOnlyError", "The transaction is read-only.");
|
|
2723
|
+
if (!this.gotValue)
|
|
2724
|
+
throw err("InvalidStateError", "The cursor is being iterated or has iterated past its end.");
|
|
2725
|
+
if (!this.withValues)
|
|
2726
|
+
throw err("InvalidStateError", "A key cursor cannot delete.");
|
|
2727
|
+
const p = this.at[1];
|
|
2728
|
+
const req = new PlateauRequest(this, tx, "none");
|
|
2729
|
+
tx._issueDelete(this.store._def.name, p, req);
|
|
2730
|
+
return req;
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
|
|
2734
|
+
class PlateauCursorWithValue extends PlateauCursor {
|
|
2735
|
+
}
|
|
2736
|
+
Object.defineProperty(PlateauCursorWithValue.prototype, "value", {
|
|
2737
|
+
get() {
|
|
2738
|
+
return this._value;
|
|
2739
|
+
},
|
|
2740
|
+
enumerable: true,
|
|
2741
|
+
configurable: true
|
|
2742
|
+
});
|
|
2743
|
+
var plateau = new PlateauFactory;
|
|
2744
|
+
export {
|
|
2745
|
+
PlateauCursor,
|
|
2746
|
+
PlateauCursorWithValue,
|
|
2747
|
+
PlateauDatabase,
|
|
2748
|
+
PlateauEvent,
|
|
2749
|
+
PlateauFactory,
|
|
2750
|
+
PlateauIndex,
|
|
2751
|
+
PlateauKeyRange,
|
|
2752
|
+
PlateauObjectStore,
|
|
2753
|
+
PlateauOpenDBRequest,
|
|
2754
|
+
PlateauRequest,
|
|
2755
|
+
PlateauTransaction,
|
|
2756
|
+
PlateauVersionChangeEvent,
|
|
2757
|
+
cmp,
|
|
2758
|
+
decodeKey,
|
|
2759
|
+
decodeValue,
|
|
2760
|
+
encodeKey,
|
|
2761
|
+
encodeValue,
|
|
2762
|
+
plateau,
|
|
2763
|
+
promise,
|
|
2764
|
+
runtime,
|
|
2765
|
+
startRuntime
|
|
2766
|
+
};
|