@northtalk/ts 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 +55 -2
- package/UNICODE-LICENSE.txt +39 -0
- package/core-eU5KaYkz.d.ts +23 -0
- package/debug-D8Z_u244.d.ts +2698 -0
- package/debug.d.ts +2 -0
- package/debug.js +1 -0
- package/driver.d.ts +27 -0
- package/driver.js +74 -0
- package/encoding-DWlbPq-e.js +7792 -0
- package/engine-BPFnHT9N.d.ts +86 -0
- package/engine-CgCMdGGd.js +324 -0
- package/errors-DHwz1Vho.js +3661 -0
- package/group-B4eyJKTA.js +15958 -0
- package/index.d.ts +645 -0
- package/index.js +37 -0
- package/manifest-DKe2Je6Z.js +208 -0
- package/notify.d.ts +2 -0
- package/notify.js +1 -0
- package/objects-MeXIDDFo.d.ts +625 -0
- package/package.json +62 -4
- package/readers-DyjxJEkL.js +479 -0
- package/readers-Oh4tO2dK.d.ts +57 -0
- package/router-DHPJNvbt.js +341 -0
- package/router-DpDpAJjz.d.ts +54 -0
- package/session.d.ts +8 -0
- package/session.js +3476 -0
- package/setup.d.ts +2 -0
- package/setup.js +189 -0
- package/sha256-0jiTgVJu.js +123 -0
- package/sqlite-capability-C7-SYdD7.d.ts +87 -0
- package/sqlite-capability-CrCej3Po.js +478 -0
- package/sqlite.d.ts +75 -0
- package/sqlite.js +528 -0
- package/store-DrYjcoru.js +59 -0
- package/store-capability-CDoVafZL.js +387 -0
- package/store.d.ts +20 -0
- package/store.js +3 -0
- package/user-capability-CLkDsrCX.d.ts +40 -0
- package/user-capability-Cv0oJhhv.js +273 -0
- package/wasm.d.ts +223 -0
- package/wasm.js +1453 -0
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
import { E as listValues, M as num, V as text, a as invalidValue, c as bool, h as dec, i as ScriptError, j as nothing, k as map, l as bytes, ot as characterBoundaries, t as HostError, ut as normalizeNFC, z as requireValue } from "./errors-DHwz1Vho.js";
|
|
2
|
+
import { ft as errorMessages, r as registerStandardChecks, u as ScriptError$1, vt as defineCapability, xt as shape, yt as expectedOf } from "./encoding-DWlbPq-e.js";
|
|
3
|
+
import { c as fixed, s as costOf } from "./store-capability-CDoVafZL.js";
|
|
4
|
+
//#region src/json.ts
|
|
5
|
+
var JsonReader = class {
|
|
6
|
+
source;
|
|
7
|
+
i = 0;
|
|
8
|
+
constructor(source) {
|
|
9
|
+
this.source = source;
|
|
10
|
+
}
|
|
11
|
+
fail(at = this.i) {
|
|
12
|
+
const scalar = Array.from(this.source, (c) => /^[\uD800-\uDFFF]$/.test(c) ? "�" : c).join("");
|
|
13
|
+
const offset = characterBoundaries(scalar).filter((i) => i <= at).length;
|
|
14
|
+
throw new ScriptError("can't decode", errorMessages["can't decode"].replace("{format}", text("json").toString()).replace("{offset}", String(offset)), map([["format", text("json")], ["offset", num(offset)]]));
|
|
15
|
+
}
|
|
16
|
+
space() {
|
|
17
|
+
while (/[\t\n\r ]/.test(this.source[this.i] ?? "") && this.i < this.source.length) this.i++;
|
|
18
|
+
}
|
|
19
|
+
eat(c) {
|
|
20
|
+
if (this.source[this.i] !== c) this.fail();
|
|
21
|
+
this.i++;
|
|
22
|
+
}
|
|
23
|
+
hex4() {
|
|
24
|
+
let n = 0;
|
|
25
|
+
for (let k = 0; k < 4; k++) {
|
|
26
|
+
const c = this.source[this.i];
|
|
27
|
+
if (c === void 0 || !/[\dA-Fa-f]/.test(c)) this.fail();
|
|
28
|
+
n = n * 16 + Number.parseInt(c, 16);
|
|
29
|
+
this.i++;
|
|
30
|
+
}
|
|
31
|
+
return n;
|
|
32
|
+
}
|
|
33
|
+
string() {
|
|
34
|
+
this.eat("\"");
|
|
35
|
+
const parts = [];
|
|
36
|
+
const escapes = {
|
|
37
|
+
"\"": "\"",
|
|
38
|
+
"\\": "\\",
|
|
39
|
+
"/": "/",
|
|
40
|
+
b: "\b",
|
|
41
|
+
f: "\f",
|
|
42
|
+
n: "\n",
|
|
43
|
+
r: "\r",
|
|
44
|
+
t: " "
|
|
45
|
+
};
|
|
46
|
+
while (this.i < this.source.length) {
|
|
47
|
+
const start = this.i;
|
|
48
|
+
const cp = this.source.codePointAt(this.i);
|
|
49
|
+
const c = String.fromCodePoint(cp);
|
|
50
|
+
if (c === "\"") {
|
|
51
|
+
this.i++;
|
|
52
|
+
return normalizeNFC(parts.join(""));
|
|
53
|
+
}
|
|
54
|
+
if (cp < 32 || cp >= 55296 && cp <= 57343) this.fail();
|
|
55
|
+
this.i += c.length;
|
|
56
|
+
if (c !== "\\") {
|
|
57
|
+
parts.push(c);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (this.i === this.source.length) this.fail();
|
|
61
|
+
const escape = this.source[this.i++];
|
|
62
|
+
if (Object.hasOwn(escapes, escape)) {
|
|
63
|
+
parts.push(escapes[escape]);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (escape !== "u") this.fail(this.i - 1);
|
|
67
|
+
let u = this.hex4();
|
|
68
|
+
if (u >= 56320 && u <= 57343) this.fail(start);
|
|
69
|
+
if (u >= 55296 && u <= 56319) {
|
|
70
|
+
const lowStart = this.i;
|
|
71
|
+
if (this.source[this.i] !== "\\" || this.source[this.i + 1] !== "u") this.fail();
|
|
72
|
+
this.i += 2;
|
|
73
|
+
const low = this.hex4();
|
|
74
|
+
if (low < 56320 || low > 57343) this.fail(lowStart);
|
|
75
|
+
u = 65536 + (u - 55296) * 1024 + low - 56320;
|
|
76
|
+
}
|
|
77
|
+
parts.push(String.fromCodePoint(u));
|
|
78
|
+
}
|
|
79
|
+
return this.fail();
|
|
80
|
+
}
|
|
81
|
+
digits() {
|
|
82
|
+
const start = this.i;
|
|
83
|
+
while (this.i < this.source.length && /\d/.test(this.source[this.i])) this.i++;
|
|
84
|
+
return this.source.slice(start, this.i);
|
|
85
|
+
}
|
|
86
|
+
number() {
|
|
87
|
+
const start = this.i;
|
|
88
|
+
let sign = "";
|
|
89
|
+
if (this.source[this.i] === "-") {
|
|
90
|
+
sign = "-";
|
|
91
|
+
this.i++;
|
|
92
|
+
}
|
|
93
|
+
if (!/\d/.test(this.source[this.i] ?? "")) this.fail();
|
|
94
|
+
let whole;
|
|
95
|
+
if (this.source[this.i] === "0") {
|
|
96
|
+
whole = "0";
|
|
97
|
+
this.i++;
|
|
98
|
+
} else whole = this.digits();
|
|
99
|
+
let fraction = "";
|
|
100
|
+
if (this.source[this.i] === ".") {
|
|
101
|
+
this.i++;
|
|
102
|
+
fraction = this.digits();
|
|
103
|
+
if (!fraction) this.fail();
|
|
104
|
+
}
|
|
105
|
+
let exponent = "", negativeExponent = false;
|
|
106
|
+
if (/[Ee]/.test(this.source[this.i] ?? "")) {
|
|
107
|
+
this.i++;
|
|
108
|
+
if (/[+-]/.test(this.source[this.i] ?? "")) negativeExponent = this.source[this.i++] === "-";
|
|
109
|
+
exponent = this.digits();
|
|
110
|
+
if (!exponent) this.fail();
|
|
111
|
+
}
|
|
112
|
+
const coefficient = whole + fraction;
|
|
113
|
+
const significant = coefficient.replace(/^0+/, "");
|
|
114
|
+
exponent = exponent.replace(/^0+/, "");
|
|
115
|
+
if (exponent.length > 9) {
|
|
116
|
+
if (!significant && !negativeExponent) return num(0);
|
|
117
|
+
this.fail(start);
|
|
118
|
+
}
|
|
119
|
+
const q = (negativeExponent ? -1 : 1) * Number(exponent) - fraction.length;
|
|
120
|
+
if (!significant && q >= 0) return num(0);
|
|
121
|
+
if (significant.length > 34) this.fail(start);
|
|
122
|
+
if (q >= 0) {
|
|
123
|
+
if (significant.length + q > 34) this.fail(start);
|
|
124
|
+
return dec(sign + significant + "0".repeat(q));
|
|
125
|
+
}
|
|
126
|
+
if (-q > 6176) this.fail(start);
|
|
127
|
+
const digits = coefficient.padStart(-q + 1, "0");
|
|
128
|
+
return dec(sign + digits.slice(0, q) + "." + digits.slice(q));
|
|
129
|
+
}
|
|
130
|
+
atom() {
|
|
131
|
+
const c = this.source[this.i];
|
|
132
|
+
if (c === "\"") return text(this.string());
|
|
133
|
+
if (c === "-" || /\d/.test(c ?? "")) return this.number();
|
|
134
|
+
for (const [literal, value] of [
|
|
135
|
+
["true", bool(true)],
|
|
136
|
+
["false", bool(false)],
|
|
137
|
+
["null", nothing]
|
|
138
|
+
]) {
|
|
139
|
+
if (c !== literal[0]) continue;
|
|
140
|
+
for (const ch of literal) this.eat(ch);
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
return this.fail();
|
|
144
|
+
}
|
|
145
|
+
read() {
|
|
146
|
+
const frames = [];
|
|
147
|
+
let value;
|
|
148
|
+
const key = (frame) => {
|
|
149
|
+
this.space();
|
|
150
|
+
const start = this.i;
|
|
151
|
+
frame.key = this.string();
|
|
152
|
+
if (frame.keys.has(frame.key)) this.fail(start);
|
|
153
|
+
frame.keys.add(frame.key);
|
|
154
|
+
this.space();
|
|
155
|
+
this.eat(":");
|
|
156
|
+
};
|
|
157
|
+
for (;;) {
|
|
158
|
+
if (!value) {
|
|
159
|
+
this.space();
|
|
160
|
+
const c = this.source[this.i];
|
|
161
|
+
if (c === "[" || c === "{") {
|
|
162
|
+
this.i++;
|
|
163
|
+
this.space();
|
|
164
|
+
const frame = c === "[" ? {
|
|
165
|
+
kind: "list",
|
|
166
|
+
values: []
|
|
167
|
+
} : {
|
|
168
|
+
kind: "map",
|
|
169
|
+
pairs: [],
|
|
170
|
+
keys: /* @__PURE__ */ new Set(),
|
|
171
|
+
key: ""
|
|
172
|
+
};
|
|
173
|
+
if (this.source[this.i] === (c === "[" ? "]" : "}")) {
|
|
174
|
+
this.i++;
|
|
175
|
+
value = c === "[" ? listValues([]) : map([]);
|
|
176
|
+
} else {
|
|
177
|
+
if (frame.kind === "map") key(frame);
|
|
178
|
+
frames.push(frame);
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
} else value = this.atom();
|
|
182
|
+
}
|
|
183
|
+
const parent = frames.at(-1);
|
|
184
|
+
this.space();
|
|
185
|
+
if (!parent) {
|
|
186
|
+
if (this.i !== this.source.length) this.fail();
|
|
187
|
+
return value;
|
|
188
|
+
}
|
|
189
|
+
if (parent.kind === "list") parent.values.push(value);
|
|
190
|
+
else parent.pairs.push([parent.key, value]);
|
|
191
|
+
if (this.source[this.i] === ",") {
|
|
192
|
+
this.i++;
|
|
193
|
+
if (parent.kind === "map") key(parent);
|
|
194
|
+
value = void 0;
|
|
195
|
+
} else {
|
|
196
|
+
this.eat(parent.kind === "list" ? "]" : "}");
|
|
197
|
+
frames.pop();
|
|
198
|
+
value = parent.kind === "list" ? listValues(parent.values) : map(parent.pairs);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
/** Read exact decimal digits, NFC strings and insertion-ordered maps. */
|
|
204
|
+
const decodeJson = (source) => {
|
|
205
|
+
if (typeof source !== "string") invalidValue("JSON must be text");
|
|
206
|
+
return new JsonReader(source).read();
|
|
207
|
+
};
|
|
208
|
+
const quoteJson = (s) => {
|
|
209
|
+
const short = {
|
|
210
|
+
"\b": "b",
|
|
211
|
+
" ": "t",
|
|
212
|
+
"\n": "n",
|
|
213
|
+
"\f": "f",
|
|
214
|
+
"\r": "r"
|
|
215
|
+
};
|
|
216
|
+
return "\"" + s.replaceAll(/[\u0000-\u001f"\\]/g, (c) => c === "\"" || c === "\\" ? "\\" + c : Object.hasOwn(short, c) ? "\\" + short[c] : String.raw`\u00` + c.charCodeAt(0).toString(16).padStart(2, "0")) + "\"";
|
|
217
|
+
};
|
|
218
|
+
/** Compact JSON, with canonical numbers and chapter 7's string escapes. */
|
|
219
|
+
const encodeJson = (value) => {
|
|
220
|
+
requireValue(value);
|
|
221
|
+
const pending = [{
|
|
222
|
+
value,
|
|
223
|
+
path: void 0
|
|
224
|
+
}];
|
|
225
|
+
const output = [];
|
|
226
|
+
while (pending.length) {
|
|
227
|
+
const next = pending.pop();
|
|
228
|
+
if (typeof next === "string") {
|
|
229
|
+
output.push(next);
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
const { value: v, path } = next;
|
|
233
|
+
switch (v.kind) {
|
|
234
|
+
case "nothing":
|
|
235
|
+
output.push("null");
|
|
236
|
+
break;
|
|
237
|
+
case "boolean":
|
|
238
|
+
case "number":
|
|
239
|
+
output.push(v.toString());
|
|
240
|
+
break;
|
|
241
|
+
case "text":
|
|
242
|
+
output.push(quoteJson(v.asText()));
|
|
243
|
+
break;
|
|
244
|
+
case "list":
|
|
245
|
+
output.push("[");
|
|
246
|
+
pending.push("]");
|
|
247
|
+
for (let i = v.length; i >= 1; i--) {
|
|
248
|
+
if (i < v.length) pending.push(",");
|
|
249
|
+
pending.push({
|
|
250
|
+
value: v.index(i),
|
|
251
|
+
path: {
|
|
252
|
+
key: i,
|
|
253
|
+
parent: path
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
break;
|
|
258
|
+
case "map": {
|
|
259
|
+
output.push("{");
|
|
260
|
+
pending.push("}");
|
|
261
|
+
const entries = v.entries();
|
|
262
|
+
for (let i = entries.length - 1; i >= 0; i--) {
|
|
263
|
+
const [k, child] = entries[i];
|
|
264
|
+
if (i < entries.length - 1) pending.push(",");
|
|
265
|
+
pending.push({
|
|
266
|
+
value: child,
|
|
267
|
+
path: {
|
|
268
|
+
key: k,
|
|
269
|
+
parent: path
|
|
270
|
+
}
|
|
271
|
+
}, quoteJson(k) + ":");
|
|
272
|
+
}
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
default: {
|
|
276
|
+
const keys = [];
|
|
277
|
+
for (let p = path; p; p = p.parent) keys.push(typeof p.key === "string" ? text(p.key) : num(p.key));
|
|
278
|
+
throw new ScriptError("not encodable", errorMessages["not encodable"].replace("{kind}", text(v.kind).toString()), map([["kind", text(v.kind)], ["path", listValues(keys.reverse())]]));
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return output.join("");
|
|
283
|
+
};
|
|
284
|
+
//#endregion
|
|
285
|
+
//#region src/sqlite-capability.ts
|
|
286
|
+
const MAX_WHOLE = Number.MAX_SAFE_INTEGER;
|
|
287
|
+
const isWhole = (n) => Number.isSafeInteger(n) && n >= 0;
|
|
288
|
+
const INT64_MIN = -(2n ** 63n);
|
|
289
|
+
const INT64_MAX = 2n ** 63n - 1n;
|
|
290
|
+
const paramsShape = shape.any;
|
|
291
|
+
const maxShape = shape.optional(shape.number);
|
|
292
|
+
const sqlShape = shape.oneOf(shape.nothing, shape.number, shape.text, shape.bytes, shape.bool);
|
|
293
|
+
const SQL_KINDS = /* @__PURE__ */ new Set([
|
|
294
|
+
"nothing",
|
|
295
|
+
"number",
|
|
296
|
+
"text",
|
|
297
|
+
"bytes",
|
|
298
|
+
"boolean"
|
|
299
|
+
]);
|
|
300
|
+
const CONSTRAINTS = [
|
|
301
|
+
"unique",
|
|
302
|
+
"primary key",
|
|
303
|
+
"not null",
|
|
304
|
+
"check",
|
|
305
|
+
"foreign key",
|
|
306
|
+
"datatype",
|
|
307
|
+
"trigger",
|
|
308
|
+
"other"
|
|
309
|
+
];
|
|
310
|
+
const isText = (data, field) => data.get(field).kind === "text";
|
|
311
|
+
const errorFields = {
|
|
312
|
+
sql: (data) => isText(data, "reason"),
|
|
313
|
+
constraint: (data) => CONSTRAINTS.includes(data.get("kind").asText() ?? ""),
|
|
314
|
+
"sqlite busy": () => true,
|
|
315
|
+
"not read-only": () => true,
|
|
316
|
+
"too many rows": (data) => data.get("max").kind === "number",
|
|
317
|
+
unrepresentable: (data) => isText(data, "column")
|
|
318
|
+
};
|
|
319
|
+
const errors = (...codes) => codes.map((code) => ({ code }));
|
|
320
|
+
const coreFailure = (code, field, value) => new ScriptError(code, "", map([[field, text(value)]]));
|
|
321
|
+
const checkBinding = (binding) => {
|
|
322
|
+
if (!binding || typeof binding !== "object" || typeof binding.database !== "string" || !isWhole(binding.maxRows) || binding.tables !== void 0 && (!Array.isArray(binding.tables) || !binding.tables.every((t) => typeof t === "string"))) throw new HostError("invalid value", "A sqlite binding is {database, tables, maxRows}");
|
|
323
|
+
};
|
|
324
|
+
const rowsAllowed = (max, binding) => {
|
|
325
|
+
if (max === void 0 || max.kind === "nothing") return binding.maxRows;
|
|
326
|
+
const [whole, fraction = ""] = max.asDecimal().toString().split(".");
|
|
327
|
+
if (whole.startsWith("-") || /[1-9]/.test(fraction) || BigInt(whole) > BigInt(binding.maxRows)) throw new ScriptError$1("out of range", [["field", text("max")], ["value", max]], true);
|
|
328
|
+
return Number(whole);
|
|
329
|
+
};
|
|
330
|
+
const sqlValueOf = (v) => {
|
|
331
|
+
switch (v.kind) {
|
|
332
|
+
case "nothing": return null;
|
|
333
|
+
case "boolean": return v.asBool() ? 1n : 0n;
|
|
334
|
+
case "text": return v.asText();
|
|
335
|
+
case "bytes": return v.asBytes();
|
|
336
|
+
default: {
|
|
337
|
+
const canonical = v.asDecimal().toString();
|
|
338
|
+
if (!canonical.includes(".")) {
|
|
339
|
+
const n = BigInt(canonical);
|
|
340
|
+
if (n >= INT64_MIN && n <= INT64_MAX) return n;
|
|
341
|
+
}
|
|
342
|
+
return Number(canonical);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
const paramsOf = (params) => params.kind === "list" ? Array.from({ length: params.length }, (_, i) => sqlValueOf(params.index(i + 1))) : new Map(params.entries().map(([k, v]) => [k, sqlValueOf(v)]));
|
|
347
|
+
const checkParams = (params, named) => {
|
|
348
|
+
const wrongKind = (expected, item, path) => new ScriptError$1("wrong kind", [
|
|
349
|
+
["expected", text(expected)],
|
|
350
|
+
["got", text(item.kind)],
|
|
351
|
+
["value", item],
|
|
352
|
+
...named,
|
|
353
|
+
["argument", dec("2")],
|
|
354
|
+
...path
|
|
355
|
+
], true);
|
|
356
|
+
if (params.kind !== "list" && params.kind !== "map") throw wrongKind("list or map", params, []);
|
|
357
|
+
const items = params.kind === "list" ? Array.from({ length: params.length }, (_, i) => [i + 1, params.index(i + 1)]) : params.entries();
|
|
358
|
+
for (const [at, item] of items) if (!SQL_KINDS.has(item.kind)) throw wrongKind(expectedOf(sqlShape), item, [["path", listValues([typeof at === "number" ? dec(String(at)) : text(at)])]]);
|
|
359
|
+
};
|
|
360
|
+
const checkForm = (result, max, changes) => {
|
|
361
|
+
if (!(!!result && typeof result === "object" && Array.isArray(result.columns) && result.columns.every((c) => typeof c === "string") && Array.isArray(result.rows) && result.rows.length <= max && result.rows.every((row) => Array.isArray(row) && row.length === result.columns.length) && (!changes || isWhole(result.changes)))) throw new Error("A sqlite implementation gave a malformed result");
|
|
362
|
+
};
|
|
363
|
+
const valueOf = (v, column) => {
|
|
364
|
+
const unrepresentable = () => coreFailure("unrepresentable", "column", column);
|
|
365
|
+
if (v === null) return nothing;
|
|
366
|
+
if (typeof v === "bigint") {
|
|
367
|
+
if (v < INT64_MIN || v > INT64_MAX) throw new Error("A sqlite INTEGER must fit 64 bits");
|
|
368
|
+
return dec(v.toString());
|
|
369
|
+
}
|
|
370
|
+
if (typeof v === "number") try {
|
|
371
|
+
return num(v);
|
|
372
|
+
} catch (error) {
|
|
373
|
+
if (error instanceof HostError) throw unrepresentable();
|
|
374
|
+
throw error;
|
|
375
|
+
}
|
|
376
|
+
if (typeof v === "string") {
|
|
377
|
+
if (/[\uD800-\uDFFF]/u.test(v)) throw unrepresentable();
|
|
378
|
+
return text(v);
|
|
379
|
+
}
|
|
380
|
+
if (ArrayBuffer.isView(v) && Object.prototype.toString.call(v) === "[object Uint8Array]") return bytes(v);
|
|
381
|
+
throw new Error("A sqlite value must be NULL, INTEGER, REAL, TEXT or BLOB");
|
|
382
|
+
};
|
|
383
|
+
const rowsOf = (result) => {
|
|
384
|
+
const columns = result.columns.map((c) => c.normalize("NFC"));
|
|
385
|
+
const seen = /* @__PURE__ */ new Set();
|
|
386
|
+
for (const column of columns) {
|
|
387
|
+
if (seen.has(column)) throw coreFailure("sql", "reason", `duplicate column ${column}`);
|
|
388
|
+
seen.add(column);
|
|
389
|
+
}
|
|
390
|
+
return listValues(result.rows.map((row) => map(columns.map((column, i) => [column, valueOf(row[i], column)]))));
|
|
391
|
+
};
|
|
392
|
+
/** Optional for Hosts. perRow is whole Fuel charged per row of max. */
|
|
393
|
+
const sqliteCapability = (impl, costs, perRow) => {
|
|
394
|
+
for (const name of [
|
|
395
|
+
"coordinator",
|
|
396
|
+
"query",
|
|
397
|
+
"change",
|
|
398
|
+
"begin",
|
|
399
|
+
"commit",
|
|
400
|
+
"rollback"
|
|
401
|
+
]) if (!impl || typeof impl[name] !== "function") throw new HostError("invalid value", `sqlite needs ${name}`);
|
|
402
|
+
if (!isWhole(perRow)) throw new HostError("invalid value", "The per-row cost must be a whole number");
|
|
403
|
+
const chargeRows = (call, max) => call.charge(Math.min(max * perRow, MAX_WHOLE));
|
|
404
|
+
const statement = (run) => (call, ...args) => {
|
|
405
|
+
const max = rowsAllowed(args[2], call.binding);
|
|
406
|
+
chargeRows(call, max);
|
|
407
|
+
return run(call, args[0].asText(), paramsOf(args[1]), max);
|
|
408
|
+
};
|
|
409
|
+
const statementArgs = [
|
|
410
|
+
shape.text,
|
|
411
|
+
paramsShape,
|
|
412
|
+
maxShape
|
|
413
|
+
];
|
|
414
|
+
const scoped = (name, scope, codes) => ({
|
|
415
|
+
mode: "immediate",
|
|
416
|
+
args: [],
|
|
417
|
+
result: shape.nothing,
|
|
418
|
+
errors: errors(...codes),
|
|
419
|
+
cost: costOf(costs, name),
|
|
420
|
+
segmentBound: true,
|
|
421
|
+
scope,
|
|
422
|
+
do: (call) => {
|
|
423
|
+
impl[name](call);
|
|
424
|
+
return nothing;
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
const operations = {
|
|
428
|
+
query: {
|
|
429
|
+
mode: "immediate",
|
|
430
|
+
args: statementArgs,
|
|
431
|
+
result: shape.listOf(shape.any),
|
|
432
|
+
errors: errors("sql", "not read-only", "too many rows", "unrepresentable"),
|
|
433
|
+
cost: costOf(costs, "query"),
|
|
434
|
+
do: statement((call, sql, params, max) => {
|
|
435
|
+
const result = impl.query(call, sql, params, max);
|
|
436
|
+
checkForm(result, max, false);
|
|
437
|
+
return rowsOf(result);
|
|
438
|
+
})
|
|
439
|
+
},
|
|
440
|
+
change: {
|
|
441
|
+
mode: "immediate",
|
|
442
|
+
args: statementArgs,
|
|
443
|
+
result: shape.map({
|
|
444
|
+
changes: shape.number,
|
|
445
|
+
rows: shape.listOf(shape.any)
|
|
446
|
+
}),
|
|
447
|
+
errors: errors("sql", "constraint", "sqlite busy", "too many rows", "unrepresentable"),
|
|
448
|
+
cost: costOf(costs, "change"),
|
|
449
|
+
segmentBound: true,
|
|
450
|
+
do: statement((call, sql, params, max) => {
|
|
451
|
+
const result = impl.change(call, sql, params, max);
|
|
452
|
+
checkForm(result, max, true);
|
|
453
|
+
const rows = rowsOf(result);
|
|
454
|
+
return map([["changes", dec(String(result.changes))], ["rows", rows]]);
|
|
455
|
+
})
|
|
456
|
+
},
|
|
457
|
+
begin: scoped("begin", {
|
|
458
|
+
opens: "transaction",
|
|
459
|
+
abandon: "rollback"
|
|
460
|
+
}, ["sqlite busy"]),
|
|
461
|
+
commit: scoped("commit", { closes: "transaction" }, []),
|
|
462
|
+
rollback: scoped("rollback", { closes: "transaction" }, [])
|
|
463
|
+
};
|
|
464
|
+
const capability = defineCapability("sqlite", operations, { coordinator: (binding) => {
|
|
465
|
+
checkBinding(binding);
|
|
466
|
+
return impl.coordinator(binding.database);
|
|
467
|
+
} });
|
|
468
|
+
for (const [name, op] of capability.operations) registerStandardChecks(op, {
|
|
469
|
+
error: (code, data) => Boolean(op.errors?.some((e) => e.code === code) && errorFields[code]?.(data)),
|
|
470
|
+
...name === "query" || name === "change" ? { arguments: (args, binding, named) => {
|
|
471
|
+
rowsAllowed(args[2], binding);
|
|
472
|
+
checkParams(args[1], named);
|
|
473
|
+
} } : {}
|
|
474
|
+
});
|
|
475
|
+
return fixed(capability);
|
|
476
|
+
};
|
|
477
|
+
//#endregion
|
|
478
|
+
export { decodeJson as n, encodeJson as r, sqliteCapability as t };
|
package/sqlite.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { At as Call, qt as SegmentCoordinator } from "./debug-D8Z_u244.js";
|
|
2
|
+
import { a as SqliteImpl, i as SqliteBinding, n as SqlRows, t as SqlParams } from "./sqlite-capability-C7-SYdD7.js";
|
|
3
|
+
import { r as StoreImpl } from "./user-capability-CLkDsrCX.js";
|
|
4
|
+
import { a as Stores, r as StoreQuotas } from "./engine-BPFnHT9N.js";
|
|
5
|
+
//#region src/sqlite/database.d.ts
|
|
6
|
+
/** The table that keeps a database's Stores, which no `sqlite` Grant may use. */
|
|
7
|
+
export declare const STORE_TABLE = "store";
|
|
8
|
+
type SqliteDatabaseOptions = {
|
|
9
|
+
/** Keep Stores in the database, with these quotas, on its coordinator. */
|
|
10
|
+
readonly stores?: StoreQuotas;
|
|
11
|
+
};
|
|
12
|
+
/** The Stores a database keeps: the `store` implementation and the Host's commands. */
|
|
13
|
+
type SqliteStores = StoreImpl & Pick<Stores, 'clear' | 'entries' | 'replace' | 'router'>;
|
|
14
|
+
/** One database a Host offers through `sqlite`, and the Stores it may keep. */
|
|
15
|
+
export declare class SqliteDatabase {
|
|
16
|
+
/** Every Grant on the database, and its Stores, map to this coordinator. */
|
|
17
|
+
readonly coordinator: SegmentCoordinator;
|
|
18
|
+
/** The Stores, for `storeCapability`, when the database keeps them. */
|
|
19
|
+
readonly stores?: SqliteStores;
|
|
20
|
+
private readonly lock;
|
|
21
|
+
private readonly reader;
|
|
22
|
+
private readonly writer;
|
|
23
|
+
private readonly engine?;
|
|
24
|
+
private readonly counts;
|
|
25
|
+
private holder?;
|
|
26
|
+
private savepoints;
|
|
27
|
+
private committing?;
|
|
28
|
+
private shared;
|
|
29
|
+
private readonly groups;
|
|
30
|
+
private nextGroup;
|
|
31
|
+
constructor(path: string, options?: SqliteDatabaseOptions);
|
|
32
|
+
/**
|
|
33
|
+
* Marks the database as offered through `sqlite`, as `sqliteDatabases`
|
|
34
|
+
* does. From then on a Store write takes the write lock at its call, so a
|
|
35
|
+
* Store's commit never finds it held (ADR 0070). Until then, a Store's
|
|
36
|
+
* commit takes the lock only for the commit.
|
|
37
|
+
*/
|
|
38
|
+
shareWithSqlite(): void;
|
|
39
|
+
/** Runs SQL directly, outside every Segment and with no authorizer, as for a migration. */
|
|
40
|
+
exec(sql: string): void;
|
|
41
|
+
/** Closes the database, discarding any Segment's uncommitted changes. */
|
|
42
|
+
close(): void;
|
|
43
|
+
query(call: Call<SqliteBinding>, sql: string, params: SqlParams, max: number): SqlRows;
|
|
44
|
+
change(call: Call<SqliteBinding>, sql: string, params: SqlParams, max: number): SqlRows & {
|
|
45
|
+
readonly changes: number;
|
|
46
|
+
};
|
|
47
|
+
begin(call: Call<SqliteBinding>): void;
|
|
48
|
+
commit(call: Call<SqliteBinding>): void;
|
|
49
|
+
rollback(call: Call<SqliteBinding>): void;
|
|
50
|
+
private beginSegment;
|
|
51
|
+
private commitSegment;
|
|
52
|
+
private rollbackSegment;
|
|
53
|
+
private key;
|
|
54
|
+
private acquire;
|
|
55
|
+
private held;
|
|
56
|
+
private publish;
|
|
57
|
+
private abandon;
|
|
58
|
+
private changeCounts;
|
|
59
|
+
private statement;
|
|
60
|
+
private prepareOne;
|
|
61
|
+
private storeBackend;
|
|
62
|
+
private storeWrite;
|
|
63
|
+
private storeOperations;
|
|
64
|
+
}
|
|
65
|
+
/** Opens or creates the database at `path`, which the process holds until `close`. */
|
|
66
|
+
export declare const openSqliteDatabase: (path: string, options?: SqliteDatabaseOptions) => SqliteDatabase;
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/sqlite/index.d.ts
|
|
69
|
+
/**
|
|
70
|
+
* The implementation over the Host's databases, by the name a binding gives.
|
|
71
|
+
* Each database's Store writes then take its write lock at their call.
|
|
72
|
+
*/
|
|
73
|
+
export declare const sqliteDatabases: (databases: Readonly<Record<string, SqliteDatabase>>) => SqliteImpl;
|
|
74
|
+
//#endregion
|
|
75
|
+
export type { SqliteDatabaseOptions, SqliteStores };
|