@northtalk/ts 0.0.0-stage → 0.2.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 +59 -2
- package/UNICODE-LICENSE.txt +39 -0
- package/base64-DORMd3Hv.js +6621 -0
- package/binary-value-CEmh60GC.d.ts +15 -0
- package/capabilities-CH0FkRMq.js +1301 -0
- package/checker-Dc1jl2WH.js +6657 -0
- package/core-DptnvQT9.d.ts +14 -0
- package/debug-CQBxKnJB.d.ts +3065 -0
- package/debug.d.ts +3 -0
- package/debug.js +2 -0
- package/driver.d.ts +41 -0
- package/driver.js +152 -0
- package/encoding-nlrpj-Pb.js +99 -0
- package/engine-BGaJ9dAk.js +326 -0
- package/engine-vbYInVYF.d.ts +86 -0
- package/errors-LYJZ_c8h.js +3735 -0
- package/group-B742gg4T.js +10170 -0
- package/http-capability-CSTkw1Yt.js +728 -0
- package/http.d.ts +59 -0
- package/http.js +465 -0
- package/index.d.ts +651 -0
- package/index.js +44 -0
- package/manifest-BscyH_rW.d.ts +70 -0
- package/manifest-DjsxDSuB.js +129 -0
- package/message-layer.d.ts +12 -0
- package/message-layer.js +4 -0
- package/notify.d.ts +3 -0
- package/notify.js +2 -0
- package/objects-DJ3QtCCs.d.ts +640 -0
- package/objects-Db7IB5vQ.js +91 -0
- package/operation-promises-EgZXG5N_.js +23 -0
- package/package.json +70 -4
- package/readers-BFPuosPk.d.ts +25 -0
- package/readers-CjJHKHvH.js +482 -0
- package/request-pump-binary-DdY3X-yl.js +2246 -0
- package/router--71qvxtY.js +343 -0
- package/router-Zhrw5l__.d.ts +55 -0
- package/session.d.ts +9 -0
- package/session.js +3510 -0
- package/setup.d.ts +3 -0
- package/setup.js +193 -0
- package/sha256-B_kyYw3Z.js +124 -0
- package/sqlite.d.ts +74 -0
- package/sqlite.js +529 -0
- package/store-capability-OZp4hXmj.js +390 -0
- package/store-hJrRULMx.js +60 -0
- package/store.d.ts +21 -0
- package/store.js +4 -0
- package/user-capability-BK4Asg7D.js +276 -0
- package/wasm.d.ts +243 -0
- package/wasm.js +1737 -0
package/debug.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
|
|
2
|
+
import { $ as isReadable, a as DebugSnapshot, i as DebugPause, n as DebugFrame, o as DebugSource, r as DebugInstruction, s as RepeatedEffect, st as sourceForm, t as DebugController } from "./debug-CQBxKnJB.js";
|
|
3
|
+
export { type DebugController, type DebugFrame, type DebugInstruction, type DebugPause, type DebugSnapshot, type DebugSource, type RepeatedEffect, isReadable, sourceForm };
|
package/debug.js
ADDED
package/driver.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" />
|
|
2
|
+
import { Jt as Group, b as PumpResult, m as GroupOptions, y as PumpOptions } from "./debug-CQBxKnJB.js";
|
|
3
|
+
//#region src/driver.d.ts
|
|
4
|
+
export type AutoDriveOptions = {
|
|
5
|
+
/** Epoch microseconds. The default reads `Date.now()`; readings never go backwards. */
|
|
6
|
+
clock?: () => bigint;
|
|
7
|
+
/** A Pump that threw, such as a `clock backwards` HostError. The driver keeps going. */
|
|
8
|
+
onError?: (error: unknown) => void;
|
|
9
|
+
/** Each Pump's result, as soon as it returns. It may make worker calls, such as Reload after a `rewound` Pump. */
|
|
10
|
+
onPump?: (result: PumpResult) => void;
|
|
11
|
+
/** Passed to every Pump. A Fuel Slice yields the event loop between Pumps. */
|
|
12
|
+
pump?: PumpOptions;
|
|
13
|
+
/**
|
|
14
|
+
* Calls `fire` once the clock reaches `at`, and returns a cancel function.
|
|
15
|
+
* The default uses `setTimeout`; a Host with its own clock supplies its own.
|
|
16
|
+
*/
|
|
17
|
+
setDeadline?: (at: bigint, fire: () => void) => () => void;
|
|
18
|
+
};
|
|
19
|
+
export type Drive = {
|
|
20
|
+
/** Closes with the default reason, "Host shutdown". */
|
|
21
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
22
|
+
/** Stops all Scripts, drains, awaits Host run Promises, then cancels scheduling. */
|
|
23
|
+
close(reason?: string): Promise<void>;
|
|
24
|
+
/** Resolves once no Pump is scheduled or running. A Group waiting only on a deadline counts as settled. */
|
|
25
|
+
idle(): Promise<void>;
|
|
26
|
+
/** The Group's onReady: schedules a Pump, once, on a later macrotask. */
|
|
27
|
+
ready(): void;
|
|
28
|
+
/** Cancels scheduling without stopping Scripts. Does not interrupt an active close. */
|
|
29
|
+
stop(): void;
|
|
30
|
+
};
|
|
31
|
+
/** Group and driver options. The factory owns the Group's readiness callback. */
|
|
32
|
+
export type DrivenGroupOptions = Omit<GroupOptions, 'onReady'> & AutoDriveOptions;
|
|
33
|
+
/** Creates a Group on `core` ready to drive queued Inputs from the Host's event loop. */
|
|
34
|
+
export declare const newDrivenGroup: <G extends Group>(core: {
|
|
35
|
+
newGroup(o: GroupOptions): G;
|
|
36
|
+
}, o: DrivenGroupOptions) => {
|
|
37
|
+
drive: Drive;
|
|
38
|
+
group: G;
|
|
39
|
+
};
|
|
40
|
+
export declare const autoDrive: (group: Group, o?: AutoDriveOptions) => Drive;
|
|
41
|
+
//#endregion
|
package/driver.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
|
|
2
|
+
import { Xt as nowMicros } from "./errors-LYJZ_c8h.js";
|
|
3
|
+
import { n as waitForOperations } from "./operation-promises-EgZXG5N_.js";
|
|
4
|
+
//#region src/driver.ts
|
|
5
|
+
/** Creates a Group on `core` ready to drive queued Inputs from the Host's event loop. */
|
|
6
|
+
const newDrivenGroup = (core, o) => {
|
|
7
|
+
const { name, seed, trace, ...options } = o;
|
|
8
|
+
const group = core.newGroup({
|
|
9
|
+
name,
|
|
10
|
+
seed,
|
|
11
|
+
trace,
|
|
12
|
+
onReady: () => drive.ready()
|
|
13
|
+
});
|
|
14
|
+
const drive = autoDrive(group, options);
|
|
15
|
+
return {
|
|
16
|
+
group,
|
|
17
|
+
drive
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
const MAX_DELAY_MS = 2 ** 31 - 1;
|
|
21
|
+
const asyncDispose = Symbol.asyncDispose ?? Symbol.for("Symbol.asyncDispose");
|
|
22
|
+
const timeoutDeadline = (read) => (at, fire) => {
|
|
23
|
+
let handle;
|
|
24
|
+
const arm = () => {
|
|
25
|
+
const ms = Number((at - read() + 999n) / 1000n);
|
|
26
|
+
handle = setTimeout(ms > MAX_DELAY_MS ? arm : fire, Math.min(Math.max(ms, 0), MAX_DELAY_MS));
|
|
27
|
+
};
|
|
28
|
+
arm();
|
|
29
|
+
return () => clearTimeout(handle);
|
|
30
|
+
};
|
|
31
|
+
const autoDrive = (group, o = {}) => {
|
|
32
|
+
const read = o.clock ?? nowMicros;
|
|
33
|
+
const setDeadline = o.setDeadline ?? timeoutDeadline(read);
|
|
34
|
+
let last = 0n;
|
|
35
|
+
let scheduled;
|
|
36
|
+
let cancelDeadline;
|
|
37
|
+
let stopped = false;
|
|
38
|
+
let pumping = false;
|
|
39
|
+
let closing = false;
|
|
40
|
+
let closePromise;
|
|
41
|
+
let finishClose;
|
|
42
|
+
let waiters = [];
|
|
43
|
+
const settle = () => {
|
|
44
|
+
if (scheduled === void 0 && !pumping) {
|
|
45
|
+
const done = waiters;
|
|
46
|
+
waiters = [];
|
|
47
|
+
for (const resolve of done) resolve();
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const clearDeadline = () => {
|
|
51
|
+
const cancel = cancelDeadline;
|
|
52
|
+
cancelDeadline = void 0;
|
|
53
|
+
cancel?.();
|
|
54
|
+
};
|
|
55
|
+
const pump = () => {
|
|
56
|
+
scheduled = void 0;
|
|
57
|
+
if (stopped) {
|
|
58
|
+
settle();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
pumping = true;
|
|
62
|
+
let again = false;
|
|
63
|
+
try {
|
|
64
|
+
clearDeadline();
|
|
65
|
+
const reading = read();
|
|
66
|
+
const now = reading > last ? reading : last;
|
|
67
|
+
last = now;
|
|
68
|
+
const result = group.pump(now, o.pump);
|
|
69
|
+
o.onPump?.(result);
|
|
70
|
+
again = result.state === "sliced" || result.state === "rewound";
|
|
71
|
+
if (!again && result.nextDeadline !== void 0 && !stopped) cancelDeadline = setDeadline(result.nextDeadline, () => {
|
|
72
|
+
cancelDeadline = void 0;
|
|
73
|
+
ready();
|
|
74
|
+
});
|
|
75
|
+
} catch (error) {
|
|
76
|
+
let failure = error;
|
|
77
|
+
try {
|
|
78
|
+
o.onError?.(error);
|
|
79
|
+
} catch (callbackError) {
|
|
80
|
+
if (!closing) throw callbackError;
|
|
81
|
+
failure = callbackError;
|
|
82
|
+
}
|
|
83
|
+
if (closing) finishClose?.({ error: failure });
|
|
84
|
+
} finally {
|
|
85
|
+
pumping = false;
|
|
86
|
+
if (again) ready();
|
|
87
|
+
settle();
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
const ready = () => {
|
|
91
|
+
if (!stopped && scheduled === void 0) scheduled = setTimeout(pump, 0);
|
|
92
|
+
};
|
|
93
|
+
const idle = () => scheduled === void 0 && !pumping ? Promise.resolve() : new Promise((resolve) => waiters.push(resolve));
|
|
94
|
+
const stop = () => {
|
|
95
|
+
stopped = true;
|
|
96
|
+
if (scheduled !== void 0) {
|
|
97
|
+
clearTimeout(scheduled);
|
|
98
|
+
scheduled = void 0;
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
clearDeadline();
|
|
102
|
+
} finally {
|
|
103
|
+
settle();
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
const close = (reason = "Host shutdown") => {
|
|
107
|
+
if (closePromise) return closePromise;
|
|
108
|
+
closing = true;
|
|
109
|
+
stopped = false;
|
|
110
|
+
closePromise = new Promise((resolve, reject) => {
|
|
111
|
+
finishClose = (failure) => {
|
|
112
|
+
if (!closing) return;
|
|
113
|
+
closing = false;
|
|
114
|
+
try {
|
|
115
|
+
stop();
|
|
116
|
+
} catch (error) {
|
|
117
|
+
reject(error);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (failure) reject(failure.error);
|
|
121
|
+
else resolve();
|
|
122
|
+
};
|
|
123
|
+
});
|
|
124
|
+
const drain = async () => {
|
|
125
|
+
try {
|
|
126
|
+
for (const name of group.scriptNames()) group.script(name).stop(reason);
|
|
127
|
+
ready();
|
|
128
|
+
await idle();
|
|
129
|
+
if (!closing) return;
|
|
130
|
+
await waitForOperations(group);
|
|
131
|
+
if (!closing) return;
|
|
132
|
+
await idle();
|
|
133
|
+
finishClose?.();
|
|
134
|
+
} catch (error) {
|
|
135
|
+
finishClose?.({ error });
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
drain();
|
|
139
|
+
return closePromise;
|
|
140
|
+
};
|
|
141
|
+
return {
|
|
142
|
+
close,
|
|
143
|
+
idle,
|
|
144
|
+
ready,
|
|
145
|
+
stop: () => {
|
|
146
|
+
if (!closing) stop();
|
|
147
|
+
},
|
|
148
|
+
[asyncDispose]: () => close()
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
//#endregion
|
|
152
|
+
export { autoDrive, newDrivenGroup };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
|
|
2
|
+
import { H as requireValue, o as invalidValue } from "./errors-LYJZ_c8h.js";
|
|
3
|
+
import { n as toBase64 } from "./base64-DORMd3Hv.js";
|
|
4
|
+
//#region src/standard-capability-checks.ts
|
|
5
|
+
const checks = /* @__PURE__ */ new WeakMap();
|
|
6
|
+
const registerStandardChecks = (op, rules) => {
|
|
7
|
+
checks.set(op, rules);
|
|
8
|
+
};
|
|
9
|
+
const standardChecks = (op) => checks.get(op);
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/encoding.ts
|
|
12
|
+
/** The spec's JSON string form: every U+0000..U+001F uses lowercase \u00xx. */
|
|
13
|
+
const quoteJSON = (s) => "\"" + s.replaceAll(/[\u0000-\u001f"\\]/g, (ch) => ch === "\"" ? String.raw`\"` : ch === "\\" ? "\\\\" : String.raw`\u${ch.charCodeAt(0).toString(16).padStart(4, "0")}`) + "\"";
|
|
14
|
+
const encodeValue = (value, adapter) => {
|
|
15
|
+
requireValue(value);
|
|
16
|
+
const pending = [value];
|
|
17
|
+
const output = [];
|
|
18
|
+
while (pending.length) {
|
|
19
|
+
const next = pending.pop();
|
|
20
|
+
if (typeof next === "string") {
|
|
21
|
+
output.push(next);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
switch (next.kind) {
|
|
25
|
+
case "nothing":
|
|
26
|
+
output.push("null");
|
|
27
|
+
break;
|
|
28
|
+
case "boolean":
|
|
29
|
+
output.push(next.asBool() ? "true" : "false");
|
|
30
|
+
break;
|
|
31
|
+
case "text":
|
|
32
|
+
output.push(quoteJSON(next.asText()));
|
|
33
|
+
break;
|
|
34
|
+
case "number": {
|
|
35
|
+
const canonical = next.asDecimal().toString();
|
|
36
|
+
output.push(!canonical.includes(".") && BigInt(canonical) > -(2n ** 53n) && BigInt(canonical) < 2n ** 53n ? canonical : `{"$dec":${quoteJSON(canonical)}}`);
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
case "instant":
|
|
40
|
+
output.push(`{"$instant":${quoteJSON(next.toString())}}`);
|
|
41
|
+
break;
|
|
42
|
+
case "civil date":
|
|
43
|
+
output.push(`{"$date":${quoteJSON(next.toString())}}`);
|
|
44
|
+
break;
|
|
45
|
+
case "object": {
|
|
46
|
+
const o = next.asObjectRef();
|
|
47
|
+
output.push(`{"$object":[${quoteJSON(o.kind)},${quoteJSON(o.id)}]}`);
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case "bytes":
|
|
51
|
+
output.push(`{"$bytes":${quoteJSON(toBase64(next.bytesView()))}}`);
|
|
52
|
+
break;
|
|
53
|
+
case "quantity": {
|
|
54
|
+
const q = next.asQuantity();
|
|
55
|
+
output.push(`{"$quantity":[${quoteJSON(q.number.toString())},${quoteJSON(q.unit)}]}`);
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
case "range": {
|
|
59
|
+
const { from, to } = next.asRange();
|
|
60
|
+
output.push("{\"$range\":[");
|
|
61
|
+
pending.push("]}", to, ",", from);
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
case "list":
|
|
65
|
+
output.push("[");
|
|
66
|
+
pending.push("]");
|
|
67
|
+
for (let i = next.length; i >= 1; i--) {
|
|
68
|
+
if (i < next.length) pending.push(",");
|
|
69
|
+
pending.push(next.index(i));
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
case "map": {
|
|
73
|
+
const entries = next.entries();
|
|
74
|
+
const tagged = entries.some(([k]) => k.startsWith("$"));
|
|
75
|
+
output.push(tagged ? "{\"$map\":[" : "{");
|
|
76
|
+
pending.push(tagged ? "]}" : "}");
|
|
77
|
+
for (let i = entries.length - 1; i >= 0; i--) {
|
|
78
|
+
const [k, v] = entries[i];
|
|
79
|
+
if (i < entries.length - 1) pending.push(",");
|
|
80
|
+
if (tagged) pending.push("]", v, `[${quoteJSON(k)},`);
|
|
81
|
+
else pending.push(v, `${quoteJSON(k)}:`);
|
|
82
|
+
}
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
case "function":
|
|
86
|
+
case "pattern":
|
|
87
|
+
if (adapter) {
|
|
88
|
+
output.push(adapter(next));
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
invalidValue(`The Value Encoding of a ${next.kind} isn't implemented yet`);
|
|
92
|
+
break;
|
|
93
|
+
default: invalidValue(`The Value Encoding of a ${next.kind} isn't implemented yet`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return output.join("");
|
|
97
|
+
};
|
|
98
|
+
//#endregion
|
|
99
|
+
export { standardChecks as i, quoteJSON as n, registerStandardChecks as r, encodeValue as t };
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
|
|
2
|
+
import { K as text, P as nothing, _ as dec, k as listValues, o as invalidValue } from "./errors-LYJZ_c8h.js";
|
|
3
|
+
import { Y as sizeOf, st as compareText } from "./base64-DORMd3Hv.js";
|
|
4
|
+
import { i as decodeValueMembers } from "./readers-CjJHKHvH.js";
|
|
5
|
+
import { n as quoteJSON, t as encodeValue } from "./encoding-nlrpj-Pb.js";
|
|
6
|
+
import { n as hostScriptError, t as add } from "./store-capability-OZp4hXmj.js";
|
|
7
|
+
import { n as createRouter, t as changed } from "./router--71qvxtY.js";
|
|
8
|
+
//#region src/store/engine.ts
|
|
9
|
+
/** The Session Store's quotas (chapter 12, The Session Store). */
|
|
10
|
+
const sessionQuotas = Object.freeze({
|
|
11
|
+
size: 1048576,
|
|
12
|
+
keys: 1e3,
|
|
13
|
+
value: 65536
|
|
14
|
+
});
|
|
15
|
+
/** The Session Store's notification limits (Store Notifications, The Session Host). */
|
|
16
|
+
const sessionWatchLimits = Object.freeze({
|
|
17
|
+
watches: 100,
|
|
18
|
+
watchSize: 65536,
|
|
19
|
+
pending: 1e3,
|
|
20
|
+
pendingSize: 1048576
|
|
21
|
+
});
|
|
22
|
+
/** A backend's commit that may have changed some of its Stores. */
|
|
23
|
+
var StoreStateUnknownError = class extends Error {
|
|
24
|
+
name = "StoreStateUnknownError";
|
|
25
|
+
};
|
|
26
|
+
const absent = (v) => v === void 0 || v.kind === "nothing";
|
|
27
|
+
const entrySize = (key, value) => sizeOf(text(key)) + sizeOf(value);
|
|
28
|
+
const amount = (v) => v.kind === "number" || v.kind === "quantity";
|
|
29
|
+
/** A Host Object anywhere in a value: its Object Kind. */
|
|
30
|
+
const objectKind = (value) => {
|
|
31
|
+
const work = [value];
|
|
32
|
+
while (work.length) {
|
|
33
|
+
const v = work.pop();
|
|
34
|
+
if (v.kind === "object") return v.asObjectRef().kind;
|
|
35
|
+
if (v.kind === "list") for (let i = 1; i <= v.length; i++) work.push(v.index(i));
|
|
36
|
+
else if (v.kind === "map") work.push(...v.entries().map(([, item]) => item));
|
|
37
|
+
else if (v.kind === "range") {
|
|
38
|
+
const { from, to } = v.asRange();
|
|
39
|
+
work.push(from, to);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
/** Why a Host command on a Store's contents was refused. */
|
|
44
|
+
var StoreContentsError = class extends Error {
|
|
45
|
+
name = "StoreContentsError";
|
|
46
|
+
};
|
|
47
|
+
/** A Store's contents as one JSON object, in key order, with no white space. */
|
|
48
|
+
const encodeContents = (entries) => `{${entries.map(([key, value]) => `${quoteJSON(key)}:${encodeValue(value)}`).join(",")}}`;
|
|
49
|
+
/** Contents written by `encodeContents`. Throws on anything else. */
|
|
50
|
+
const decodeContents = (source) => decodeValueMembers(source);
|
|
51
|
+
/** Every Store a Host keeps through one backend, as one `StoreImpl`. */
|
|
52
|
+
var Stores = class {
|
|
53
|
+
backend;
|
|
54
|
+
quotas;
|
|
55
|
+
router;
|
|
56
|
+
revisions = /* @__PURE__ */ new Map();
|
|
57
|
+
contents = /* @__PURE__ */ new Map();
|
|
58
|
+
segments = /* @__PURE__ */ new Map();
|
|
59
|
+
groups = /* @__PURE__ */ new WeakMap();
|
|
60
|
+
nextGroup = 0;
|
|
61
|
+
constructor(backend, quotas, watchLimits = sessionWatchLimits, admitted) {
|
|
62
|
+
this.backend = backend;
|
|
63
|
+
this.quotas = quotas;
|
|
64
|
+
this.router = createRouter({
|
|
65
|
+
admitted,
|
|
66
|
+
limits: () => watchLimits,
|
|
67
|
+
revision: (store) => (this.revisions.get(store) ?? 0n).toString()
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
watch(call, name, prefix, message) {
|
|
71
|
+
this.router.watch(call, name, prefix, message);
|
|
72
|
+
}
|
|
73
|
+
unwatch(call, name) {
|
|
74
|
+
this.router.unwatch(call, name);
|
|
75
|
+
}
|
|
76
|
+
release(context) {
|
|
77
|
+
this.router.release(context);
|
|
78
|
+
}
|
|
79
|
+
begin(context) {
|
|
80
|
+
const segment = /* @__PURE__ */ new Map();
|
|
81
|
+
if (typeof context.binding === "string") {
|
|
82
|
+
this.committed(context.binding);
|
|
83
|
+
segment.set(context.binding, /* @__PURE__ */ new Map());
|
|
84
|
+
}
|
|
85
|
+
this.segments.set(this.segmentKey(context), segment);
|
|
86
|
+
return { status: "ok" };
|
|
87
|
+
}
|
|
88
|
+
commit(context) {
|
|
89
|
+
const segment = this.segments.get(this.segmentKey(context));
|
|
90
|
+
this.segments.delete(this.segmentKey(context));
|
|
91
|
+
const changes = /* @__PURE__ */ new Map();
|
|
92
|
+
try {
|
|
93
|
+
for (const [store, keys] of segment ?? []) {
|
|
94
|
+
const contents = this.committed(store);
|
|
95
|
+
const values = /* @__PURE__ */ new Map();
|
|
96
|
+
for (const [key, pending] of keys) values.set(key, applied(pending.set ?? contents.entries.get(key), pending.deltas) ?? nothing);
|
|
97
|
+
if (values.size) changes.set(store, values);
|
|
98
|
+
}
|
|
99
|
+
if (changes.size) this.backend.save(changes);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (error instanceof StoreStateUnknownError) {
|
|
102
|
+
for (const store of changes.keys()) this.contents.delete(store);
|
|
103
|
+
return {
|
|
104
|
+
status: "unknown",
|
|
105
|
+
detail: error.message
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
status: "failed",
|
|
110
|
+
detail: error.message
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
for (const [store, values] of [...changes].sort(([a], [b]) => compareText(a, b))) this.router.commit(store, () => {
|
|
114
|
+
const contents = this.committed(store);
|
|
115
|
+
const keys = [];
|
|
116
|
+
for (const [key, value] of values) {
|
|
117
|
+
if (changed(contents.entries.get(key), value)) keys.push(key);
|
|
118
|
+
this.put(contents, key, absent(value) ? void 0 : value);
|
|
119
|
+
}
|
|
120
|
+
this.bump(store, keys);
|
|
121
|
+
return keys;
|
|
122
|
+
});
|
|
123
|
+
return { status: "ok" };
|
|
124
|
+
}
|
|
125
|
+
rollback(context) {
|
|
126
|
+
this.segments.delete(this.segmentKey(context));
|
|
127
|
+
return { status: "ok" };
|
|
128
|
+
}
|
|
129
|
+
get(call, key, fallback) {
|
|
130
|
+
return this.seen(call, key) ?? fallback ?? nothing;
|
|
131
|
+
}
|
|
132
|
+
keys(call, prefix = "") {
|
|
133
|
+
const store = storeName(call.binding);
|
|
134
|
+
const own = this.own(call, store);
|
|
135
|
+
const keys = new Set(this.committed(store).entries.keys());
|
|
136
|
+
for (const key of own?.keys() ?? []) keys.add(key);
|
|
137
|
+
return listValues([...keys].filter((key) => key.startsWith(prefix) && this.seen(call, key)).sort(compareText).map((key) => text(key)));
|
|
138
|
+
}
|
|
139
|
+
set(call, key, value) {
|
|
140
|
+
this.storable(value);
|
|
141
|
+
this.write(call, key, value);
|
|
142
|
+
}
|
|
143
|
+
delete(call, key) {
|
|
144
|
+
this.write(call, key, nothing);
|
|
145
|
+
}
|
|
146
|
+
swap(call, key, expected, replacement) {
|
|
147
|
+
this.storable(replacement);
|
|
148
|
+
const store = storeName(call.binding);
|
|
149
|
+
this.unreserved(call, store, key, true);
|
|
150
|
+
if (!(this.seen(call, key) ?? nothing).equals(expected)) return false;
|
|
151
|
+
this.write(call, key, replacement);
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
increment(call, key, by) {
|
|
155
|
+
const delta = absent(by) ? ONE : by;
|
|
156
|
+
const store = storeName(call.binding);
|
|
157
|
+
this.unreserved(call, store, key, false);
|
|
158
|
+
const seen = this.seen(call, key);
|
|
159
|
+
if (seen && !amount(seen)) throw hostScriptError("wrong kind", [
|
|
160
|
+
["expected", text("number")],
|
|
161
|
+
["got", text(seen.kind)],
|
|
162
|
+
["value", seen]
|
|
163
|
+
]);
|
|
164
|
+
const result = seen ? add(seen, delta) : delta;
|
|
165
|
+
const own = this.own(call, store)?.get(key);
|
|
166
|
+
if (own?.set === void 0) {
|
|
167
|
+
let projected = this.committed(store).entries.get(key);
|
|
168
|
+
for (const keys of this.others(call, store)) projected = applied(projected, keys.get(key)?.deltas ?? []);
|
|
169
|
+
applied(projected, [...own?.deltas ?? [], delta]);
|
|
170
|
+
}
|
|
171
|
+
const next = {
|
|
172
|
+
...own,
|
|
173
|
+
deltas: [...own?.deltas ?? [], delta]
|
|
174
|
+
};
|
|
175
|
+
this.grows(call, store, key, next, seen ? void 0 : delta);
|
|
176
|
+
this.segment(call, store).set(key, next);
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
/** A Store's committed contents, in key order. */
|
|
180
|
+
entries(store) {
|
|
181
|
+
return [...this.committed(store).entries].sort(([a], [b]) => compareText(a, b));
|
|
182
|
+
}
|
|
183
|
+
/** Replaces a Store's contents, between Pumps. */
|
|
184
|
+
replace(store, entries) {
|
|
185
|
+
if ([...this.segments.values()].some((s) => s.has(store))) throw new StoreContentsError("A Segment is writing to the Store");
|
|
186
|
+
const next = {
|
|
187
|
+
entries: /* @__PURE__ */ new Map(),
|
|
188
|
+
size: 0
|
|
189
|
+
};
|
|
190
|
+
for (const [key, value] of entries) {
|
|
191
|
+
if (key === "" || next.entries.has(key)) throw new StoreContentsError("Store keys must be distinct, nonempty text");
|
|
192
|
+
if (objectKind(value) !== void 0 || value.kind === "function") throw new StoreContentsError("A Store keeps only data values");
|
|
193
|
+
if (sizeOf(value) > this.quotas.value) throw new StoreContentsError("A value is past the Store’s value limit");
|
|
194
|
+
if (!absent(value)) this.put(next, key, value);
|
|
195
|
+
}
|
|
196
|
+
if (next.entries.size > this.quotas.keys || next.size > this.quotas.size) throw new StoreContentsError("The contents are past the Store’s quotas");
|
|
197
|
+
this.router.commit(store, () => {
|
|
198
|
+
const changes = this.changesTo(store, next);
|
|
199
|
+
const keys = [...changes].filter(([key, value]) => changed(this.committed(store).entries.get(key), value)).map(([key]) => key);
|
|
200
|
+
this.backend.save(/* @__PURE__ */ new Map([[store, changes]]));
|
|
201
|
+
this.contents.set(store, next);
|
|
202
|
+
this.bump(store, keys);
|
|
203
|
+
return keys;
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/** Empties a Store, between Pumps. */
|
|
207
|
+
clear(store) {
|
|
208
|
+
this.replace(store, []);
|
|
209
|
+
}
|
|
210
|
+
bump(store, keys) {
|
|
211
|
+
if (keys.length) this.revisions.set(store, (this.revisions.get(store) ?? 0n) + 1n);
|
|
212
|
+
}
|
|
213
|
+
committed(store) {
|
|
214
|
+
let contents = this.contents.get(store);
|
|
215
|
+
if (!contents) {
|
|
216
|
+
contents = {
|
|
217
|
+
entries: /* @__PURE__ */ new Map(),
|
|
218
|
+
size: 0
|
|
219
|
+
};
|
|
220
|
+
for (const [key, value] of this.backend.load(store)) this.put(contents, key, value);
|
|
221
|
+
this.contents.set(store, contents);
|
|
222
|
+
}
|
|
223
|
+
return contents;
|
|
224
|
+
}
|
|
225
|
+
put(contents, key, value) {
|
|
226
|
+
const old = contents.entries.get(key);
|
|
227
|
+
if (old) {
|
|
228
|
+
contents.size -= entrySize(key, old);
|
|
229
|
+
contents.entries.delete(key);
|
|
230
|
+
}
|
|
231
|
+
if (value) {
|
|
232
|
+
contents.size += entrySize(key, value);
|
|
233
|
+
contents.entries.set(key, value);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
changesTo(store, next) {
|
|
237
|
+
const changes = /* @__PURE__ */ new Map();
|
|
238
|
+
for (const key of this.committed(store).entries.keys()) if (!next.entries.has(key)) changes.set(key, nothing);
|
|
239
|
+
for (const [key, value] of next.entries) changes.set(key, value);
|
|
240
|
+
return changes;
|
|
241
|
+
}
|
|
242
|
+
segmentKey(at) {
|
|
243
|
+
let group = this.groups.get(at.group);
|
|
244
|
+
if (group === void 0) {
|
|
245
|
+
group = this.nextGroup++;
|
|
246
|
+
this.groups.set(at.group, group);
|
|
247
|
+
}
|
|
248
|
+
return `${group} ${at.segmentId}`;
|
|
249
|
+
}
|
|
250
|
+
own(call, store) {
|
|
251
|
+
return this.segments.get(this.segmentKey(call))?.get(store);
|
|
252
|
+
}
|
|
253
|
+
others(call, store) {
|
|
254
|
+
const mine = this.segmentKey(call);
|
|
255
|
+
return [...this.segments].filter(([id, segment]) => id !== mine && segment.has(store)).map(([, segment]) => segment.get(store));
|
|
256
|
+
}
|
|
257
|
+
segment(call, store) {
|
|
258
|
+
const segment = this.segments.get(this.segmentKey(call));
|
|
259
|
+
if (!segment) throw new Error("A Store write outside its Segment");
|
|
260
|
+
let keys = segment.get(store);
|
|
261
|
+
if (!keys) {
|
|
262
|
+
this.committed(store);
|
|
263
|
+
keys = /* @__PURE__ */ new Map();
|
|
264
|
+
segment.set(store, keys);
|
|
265
|
+
}
|
|
266
|
+
return keys;
|
|
267
|
+
}
|
|
268
|
+
seen(call, key) {
|
|
269
|
+
const store = storeName(call.binding);
|
|
270
|
+
const committed = this.committed(store).entries.get(key);
|
|
271
|
+
const pending = this.own(call, store)?.get(key);
|
|
272
|
+
return pending ? applied(pending.set ?? committed, pending.deltas) : committed;
|
|
273
|
+
}
|
|
274
|
+
storable(value) {
|
|
275
|
+
const kind = objectKind(value);
|
|
276
|
+
if (kind !== void 0) throw hostScriptError("can't store", [["kind", text(kind)]]);
|
|
277
|
+
}
|
|
278
|
+
unreserved(call, store, key, replacing) {
|
|
279
|
+
for (const keys of this.others(call, store)) {
|
|
280
|
+
const pending = keys.get(key);
|
|
281
|
+
if (pending && (replacing || pending.set !== void 0)) throw hostScriptError("store busy", [["key", text(key)]]);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
write(call, key, value) {
|
|
285
|
+
const store = storeName(call.binding);
|
|
286
|
+
this.unreserved(call, store, key, true);
|
|
287
|
+
const next = {
|
|
288
|
+
set: value,
|
|
289
|
+
deltas: []
|
|
290
|
+
};
|
|
291
|
+
this.grows(call, store, key, next, absent(value) ? void 0 : value);
|
|
292
|
+
this.segment(call, store).set(key, next);
|
|
293
|
+
}
|
|
294
|
+
grows(call, store, key, next, written) {
|
|
295
|
+
if (written && sizeOf(written) > this.quotas.value) throw hostScriptError("store full", [["limit", text("value")]]);
|
|
296
|
+
const contents = this.committed(store);
|
|
297
|
+
const mine = new Map(this.own(call, store));
|
|
298
|
+
mine.set(key, next);
|
|
299
|
+
let keys = contents.entries.size;
|
|
300
|
+
let size = contents.size;
|
|
301
|
+
for (const pending of [...this.others(call, store), mine]) {
|
|
302
|
+
let grownKeys = 0;
|
|
303
|
+
let grownSize = 0;
|
|
304
|
+
for (const [k, p] of pending) {
|
|
305
|
+
const old = contents.entries.get(k);
|
|
306
|
+
const base = p.set === void 0 ? old : p.set;
|
|
307
|
+
const value = absent(base) ? p.deltas[0] : base;
|
|
308
|
+
grownKeys += (value ? 1 : 0) - (old ? 1 : 0);
|
|
309
|
+
grownSize += (value ? entrySize(k, value) : 0) - (old ? entrySize(k, old) : 0);
|
|
310
|
+
}
|
|
311
|
+
keys += Math.max(0, grownKeys);
|
|
312
|
+
size += Math.max(0, grownSize);
|
|
313
|
+
}
|
|
314
|
+
if (keys > this.quotas.keys) throw hostScriptError("store full", [["limit", text("keys")]]);
|
|
315
|
+
if (size > this.quotas.size) throw hostScriptError("store full", [["limit", text("size")]]);
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
const ONE = dec("1");
|
|
319
|
+
const applied = (value, deltas) => {
|
|
320
|
+
let current = absent(value) ? void 0 : value;
|
|
321
|
+
for (const delta of deltas) current = current === void 0 ? delta : add(current, delta);
|
|
322
|
+
return current;
|
|
323
|
+
};
|
|
324
|
+
const storeName = (binding) => typeof binding === "string" ? binding : invalidValue("A Store’s binding is its name, as text");
|
|
325
|
+
//#endregion
|
|
326
|
+
export { encodeContents as a, decodeContents as i, StoreStateUnknownError as n, sessionQuotas as o, Stores as r, sessionWatchLimits as s, StoreContentsError as t };
|