@drupflare/spool 0.2.0 → 0.4.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/README.md +499 -43
- package/dist/cdn/client.js +1 -1
- package/dist/cdn/manifest.json +7 -7
- package/dist/cdn/spool-sw.js +1 -1
- package/dist/client/activity.d.ts +37 -0
- package/dist/client/activity.js +114 -0
- package/dist/client/entry.js +34 -1
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.js +1 -0
- package/dist/client/register.d.ts +23 -1
- package/dist/client/register.js +53 -7
- package/dist/media/embeds.d.ts +3 -2
- package/dist/media/embeds.js +16 -1
- package/dist/protocol/config.d.ts +16 -1
- package/dist/protocol/config.js +3 -1
- package/dist/protocol/dataset.d.ts +183 -0
- package/dist/protocol/dataset.js +206 -0
- package/dist/protocol/envelope.d.ts +3 -0
- package/dist/protocol/envelope.js +14 -3
- package/dist/protocol/errors.d.ts +1 -1
- package/dist/protocol/graph.d.ts +23 -4
- package/dist/protocol/graph.js +34 -3
- package/dist/protocol/hash.d.ts +2 -0
- package/dist/protocol/hash.js +8 -0
- package/dist/protocol/index.d.ts +7 -0
- package/dist/protocol/index.js +7 -0
- package/dist/protocol/inline.d.ts +14 -0
- package/dist/protocol/inline.js +43 -0
- package/dist/protocol/navigation.d.ts +81 -0
- package/dist/protocol/navigation.js +249 -0
- package/dist/protocol/normalise.d.ts +4 -3
- package/dist/protocol/normalise.js +6 -4
- package/dist/protocol/plan.d.ts +100 -0
- package/dist/protocol/plan.js +661 -0
- package/dist/protocol/provision.d.ts +52 -1
- package/dist/protocol/provision.js +53 -5
- package/dist/protocol/query.d.ts +141 -0
- package/dist/protocol/query.js +252 -0
- package/dist/protocol/route.d.ts +85 -5
- package/dist/protocol/route.js +91 -0
- package/dist/protocol/write.d.ts +146 -0
- package/dist/protocol/write.js +101 -0
- package/dist/proxy/bases.d.ts +53 -0
- package/dist/proxy/bases.js +76 -0
- package/dist/proxy/index.d.ts +1 -0
- package/dist/proxy/index.js +1 -0
- package/dist/proxy/proxy.js +8 -2
- package/dist/report/capacity.d.ts +59 -0
- package/dist/report/capacity.js +76 -0
- package/dist/report/channels.d.ts +60 -0
- package/dist/report/channels.js +65 -0
- package/dist/report/dataset.d.ts +48 -0
- package/dist/report/dataset.js +93 -0
- package/dist/report/derive.d.ts +285 -0
- package/dist/report/derive.js +676 -1
- package/dist/report/economics.d.ts +11 -1
- package/dist/report/economics.js +19 -1
- package/dist/report/index.d.ts +5 -0
- package/dist/report/index.js +5 -0
- package/dist/report/inputs.d.ts +97 -0
- package/dist/report/inputs.js +133 -0
- package/dist/report/institution.d.ts +284 -0
- package/dist/report/institution.js +225 -0
- package/dist/report/ledger.d.ts +28 -0
- package/dist/report/ledger.js +30 -0
- package/dist/report/loops.d.ts +73 -0
- package/dist/report/loops.js +101 -0
- package/dist/report/measurement.d.ts +1 -1
- package/dist/report/measurement.js +6 -1
- package/dist/report/model.d.ts +2 -0
- package/dist/report/model.js +4 -0
- package/dist/report/thirdparty.d.ts +57 -0
- package/dist/report/thirdparty.js +111 -0
- package/dist/report/traffic.d.ts +11 -2
- package/dist/report/traffic.js +13 -3
- package/dist/report/trim.d.ts +56 -0
- package/dist/report/trim.js +83 -1
- package/dist/server/dataset.d.ts +48 -0
- package/dist/server/dataset.js +308 -0
- package/dist/server/embedded.d.ts +3 -3
- package/dist/server/embedded.js +3 -3
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.js +3 -0
- package/dist/server/spool.d.ts +40 -0
- package/dist/server/spool.js +184 -23
- package/dist/server/write.d.ts +27 -0
- package/dist/server/write.js +127 -0
- package/dist/sw/analytics.d.ts +6 -0
- package/dist/sw/analytics.js +14 -0
- package/dist/sw/dataset-sync.d.ts +76 -0
- package/dist/sw/dataset-sync.js +149 -0
- package/dist/sw/dataset.d.ts +91 -0
- package/dist/sw/dataset.js +318 -0
- package/dist/sw/graph/index.d.ts +31 -1
- package/dist/sw/graph/index.js +57 -5
- package/dist/sw/graph/invalidate.d.ts +15 -3
- package/dist/sw/graph/invalidate.js +42 -4
- package/dist/sw/idb.js +78 -9
- package/dist/sw/partition.d.ts +6 -1
- package/dist/sw/partition.js +21 -2
- package/dist/sw/principal-dataset.d.ts +64 -0
- package/dist/sw/principal-dataset.js +254 -0
- package/dist/sw/render.d.ts +8 -1
- package/dist/sw/render.js +28 -1
- package/dist/sw/replica.d.ts +14 -3
- package/dist/sw/replica.js +46 -5
- package/dist/sw/router.js +2 -2
- package/dist/sw/runtime.d.ts +43 -2
- package/dist/sw/runtime.js +440 -29
- package/dist/sw/seal.d.ts +13 -0
- package/dist/sw/seal.js +13 -4
- package/dist/sw/static.d.ts +18 -0
- package/dist/sw/static.js +84 -1
- package/dist/sw/storage.d.ts +51 -1
- package/dist/sw/storage.js +33 -1
- package/dist/sw/write.d.ts +107 -0
- package/dist/sw/write.js +442 -0
- package/package.json +5 -2
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { ProtocolError } from './errors.js';
|
|
2
|
+
const SNAPSHOT_KEYS = ['v', 't', 'one', 'two'];
|
|
3
|
+
const round3 = (x) => Math.round(x * 1000) / 1000;
|
|
4
|
+
const keyOf = (ctx) => JSON.stringify(ctx);
|
|
5
|
+
function range(ok, name) {
|
|
6
|
+
if (!ok)
|
|
7
|
+
throw new RangeError(`nav option ${name} is out of range`);
|
|
8
|
+
}
|
|
9
|
+
function refuse(what) {
|
|
10
|
+
throw new ProtocolError('bad-nav', `nav snapshot ${what}`);
|
|
11
|
+
}
|
|
12
|
+
function isRecord(v) {
|
|
13
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
14
|
+
}
|
|
15
|
+
function isRoute(v) {
|
|
16
|
+
return typeof v === 'string' && v.length > 0;
|
|
17
|
+
}
|
|
18
|
+
function isTick(v, max) {
|
|
19
|
+
return typeof v === 'number' && Number.isSafeInteger(v) && v >= 0 && v <= max;
|
|
20
|
+
}
|
|
21
|
+
function entryOf(row) {
|
|
22
|
+
const next = [...row.next].map(([route, c]) => [route, c.n, c.t]);
|
|
23
|
+
return [[...row.ctx], row.t, next];
|
|
24
|
+
}
|
|
25
|
+
function dropOldest(table) {
|
|
26
|
+
let oldest = '';
|
|
27
|
+
let oldestT = Infinity;
|
|
28
|
+
for (const [key, row] of table) {
|
|
29
|
+
if (row.t < oldestT) {
|
|
30
|
+
oldest = key;
|
|
31
|
+
oldestT = row.t;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
table.delete(oldest);
|
|
35
|
+
}
|
|
36
|
+
function restore(table, entries, size, tick) {
|
|
37
|
+
for (const item of entries) {
|
|
38
|
+
if (!Array.isArray(item) || item.length !== 3)
|
|
39
|
+
refuse('has a malformed row');
|
|
40
|
+
const [ctx, t, next] = item;
|
|
41
|
+
if (!Array.isArray(ctx) || ctx.length !== size || !ctx.every(isRoute)) {
|
|
42
|
+
refuse('has a malformed context');
|
|
43
|
+
}
|
|
44
|
+
if (!isTick(t, tick) || t === 0)
|
|
45
|
+
refuse('has a row touched outside the model');
|
|
46
|
+
if (!Array.isArray(next))
|
|
47
|
+
refuse('has a malformed row');
|
|
48
|
+
const key = keyOf(ctx);
|
|
49
|
+
if (table.has(key))
|
|
50
|
+
refuse('repeats a context');
|
|
51
|
+
const row = { ctx, t, next: new Map() };
|
|
52
|
+
for (const pair of next) {
|
|
53
|
+
if (!Array.isArray(pair) || pair.length !== 3)
|
|
54
|
+
refuse('has a malformed route');
|
|
55
|
+
const [route, n, touch] = pair;
|
|
56
|
+
if (!isRoute(route) || row.next.has(route))
|
|
57
|
+
refuse('has a malformed route');
|
|
58
|
+
if (typeof n !== 'number')
|
|
59
|
+
refuse('has a count that is not a number');
|
|
60
|
+
if (n < 0)
|
|
61
|
+
refuse('has a negative count');
|
|
62
|
+
if (!isTick(touch, tick) || touch === 0)
|
|
63
|
+
refuse('has a route touched outside the model');
|
|
64
|
+
row.next.set(route, { n, t: touch });
|
|
65
|
+
}
|
|
66
|
+
table.set(key, row);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Predicts the next route pattern from the ones before it. Counts decay by half every `halfLife`
|
|
71
|
+
* observations, and a route the model has never seen (in counts or prior) is never predicted.
|
|
72
|
+
*/
|
|
73
|
+
export class NavModel {
|
|
74
|
+
#order;
|
|
75
|
+
#halfLife;
|
|
76
|
+
#alpha;
|
|
77
|
+
#backoff;
|
|
78
|
+
#prior;
|
|
79
|
+
#maxContexts;
|
|
80
|
+
#maxNext;
|
|
81
|
+
#one = new Map();
|
|
82
|
+
#two = new Map();
|
|
83
|
+
#tick = 0;
|
|
84
|
+
constructor(options = {}) {
|
|
85
|
+
const order = options.order ?? 1;
|
|
86
|
+
const halfLife = options.halfLife ?? 200;
|
|
87
|
+
const alpha = options.alpha ?? 1;
|
|
88
|
+
const backoff = options.backoff ?? 5;
|
|
89
|
+
const prior = options.prior ?? {};
|
|
90
|
+
const maxContexts = options.maxContexts ?? 64;
|
|
91
|
+
const maxNext = options.maxNext ?? 16;
|
|
92
|
+
range(order === 1 || order === 2, 'order');
|
|
93
|
+
range(halfLife > 0, 'halfLife');
|
|
94
|
+
range(alpha >= 0 && Number.isFinite(alpha), 'alpha');
|
|
95
|
+
range(backoff >= 0 && Number.isFinite(backoff), 'backoff');
|
|
96
|
+
range(Number.isSafeInteger(maxContexts) && maxContexts >= 1, 'maxContexts');
|
|
97
|
+
range(Number.isSafeInteger(maxNext) && maxNext >= 1, 'maxNext');
|
|
98
|
+
for (const row of Object.values(prior)) {
|
|
99
|
+
for (const n of Object.values(row))
|
|
100
|
+
range(n >= 0 && Number.isFinite(n), 'prior');
|
|
101
|
+
}
|
|
102
|
+
this.#order = order;
|
|
103
|
+
this.#halfLife = halfLife;
|
|
104
|
+
this.#alpha = alpha;
|
|
105
|
+
this.#backoff = backoff;
|
|
106
|
+
this.#prior = prior;
|
|
107
|
+
this.#maxContexts = maxContexts;
|
|
108
|
+
this.#maxNext = maxNext;
|
|
109
|
+
}
|
|
110
|
+
/** records that `route` came after `context`, newest route last; an empty context is a no-op */
|
|
111
|
+
observe(context, route) {
|
|
112
|
+
if (context.length === 0)
|
|
113
|
+
return;
|
|
114
|
+
this.#tick += 1;
|
|
115
|
+
this.#touch(this.#one, context.slice(-1), route);
|
|
116
|
+
if (this.#order === 2 && context.length >= 2) {
|
|
117
|
+
this.#touch(this.#two, context.slice(-2), route);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/** the likely next routes, most probable first; routes with no probability are left out */
|
|
121
|
+
predict(context) {
|
|
122
|
+
if (context.length === 0)
|
|
123
|
+
return [];
|
|
124
|
+
const last = context[context.length - 1] ?? '';
|
|
125
|
+
const one = this.#one.get(keyOf(context.slice(-1)));
|
|
126
|
+
const two = this.#order === 2 && context.length >= 2
|
|
127
|
+
? this.#two.get(keyOf(context.slice(-2)))
|
|
128
|
+
: undefined;
|
|
129
|
+
const prior = Object.hasOwn(this.#prior, last) ? this.#prior[last] : undefined;
|
|
130
|
+
const p1 = this.#distribution(one, prior);
|
|
131
|
+
// the prior's counts are evidence about the mean, so they count toward the weight
|
|
132
|
+
const priorMass = prior ? Object.values(prior).reduce((sum, n) => sum + n, 0) : 0;
|
|
133
|
+
const w1 = (one ? this.#rowWeight(one) : 0) + priorMass;
|
|
134
|
+
const p2 = this.#distribution(two);
|
|
135
|
+
const w2 = two ? this.#rowWeight(two) : 0;
|
|
136
|
+
const lambda = w2 === 0 ? 0 : w2 / (w2 + this.#backoff);
|
|
137
|
+
const weight = lambda * w2 + (1 - lambda) * w1;
|
|
138
|
+
const out = [];
|
|
139
|
+
for (const route of new Set([...p1.keys(), ...p2.keys()])) {
|
|
140
|
+
const p = lambda * (p2.get(route) ?? 0) + (1 - lambda) * (p1.get(route) ?? 0);
|
|
141
|
+
if (p > 0)
|
|
142
|
+
out.push({ route, p, weight });
|
|
143
|
+
}
|
|
144
|
+
return out.sort((a, b) => b.p - a.p || (a.route < b.route ? -1 : a.route > b.route ? 1 : 0));
|
|
145
|
+
}
|
|
146
|
+
/** the model as a snapshot that fromJSON restores */
|
|
147
|
+
toJSON() {
|
|
148
|
+
return {
|
|
149
|
+
v: 1,
|
|
150
|
+
t: this.#tick,
|
|
151
|
+
one: [...this.#one.values()].map(entryOf),
|
|
152
|
+
two: [...this.#two.values()].map(entryOf)
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/** restores a model from toJSON output; throws bad-nav on a malformed snapshot */
|
|
156
|
+
static fromJSON(snapshot, options) {
|
|
157
|
+
if (!isRecord(snapshot))
|
|
158
|
+
refuse('is not an object');
|
|
159
|
+
if (snapshot.v !== 1)
|
|
160
|
+
refuse('has an unknown version');
|
|
161
|
+
if (Object.keys(snapshot).some((key) => !SNAPSHOT_KEYS.includes(key))) {
|
|
162
|
+
refuse('has an unknown field');
|
|
163
|
+
}
|
|
164
|
+
const { t, one, two } = snapshot;
|
|
165
|
+
if (!isTick(t, Number.MAX_SAFE_INTEGER))
|
|
166
|
+
refuse('has a bad tick');
|
|
167
|
+
if (!Array.isArray(one) || !Array.isArray(two))
|
|
168
|
+
refuse('has a table that is not a list');
|
|
169
|
+
const model = new NavModel(options);
|
|
170
|
+
if (model.#order === 1 && two.length > 0)
|
|
171
|
+
refuse('has two-pattern contexts for order 1');
|
|
172
|
+
model.#tick = t;
|
|
173
|
+
restore(model.#one, one, 1, t);
|
|
174
|
+
restore(model.#two, two, 2, t);
|
|
175
|
+
return model;
|
|
176
|
+
}
|
|
177
|
+
#touch(table, ctx, route) {
|
|
178
|
+
const key = keyOf(ctx);
|
|
179
|
+
let row = table.get(key);
|
|
180
|
+
if (!row) {
|
|
181
|
+
if (table.size >= this.#maxContexts)
|
|
182
|
+
dropOldest(table);
|
|
183
|
+
row = { ctx, t: this.#tick, next: new Map() };
|
|
184
|
+
table.set(key, row);
|
|
185
|
+
}
|
|
186
|
+
row.t = this.#tick;
|
|
187
|
+
const prev = row.next.get(route);
|
|
188
|
+
row.next.set(route, { n: round3((prev ? this.#weigh(prev) : 0) + 1), t: this.#tick });
|
|
189
|
+
if (row.next.size > this.#maxNext)
|
|
190
|
+
this.#dropWeakest(row.next);
|
|
191
|
+
}
|
|
192
|
+
#dropWeakest(next) {
|
|
193
|
+
let weakest = '';
|
|
194
|
+
let weakestW = Infinity;
|
|
195
|
+
for (const [route, count] of next) {
|
|
196
|
+
const w = this.#weigh(count);
|
|
197
|
+
if (w < weakestW || (w === weakestW && route < weakest)) {
|
|
198
|
+
weakest = route;
|
|
199
|
+
weakestW = w;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
next.delete(weakest);
|
|
203
|
+
}
|
|
204
|
+
#weigh(count) {
|
|
205
|
+
return count.n * 0.5 ** ((this.#tick - count.t) / this.#halfLife);
|
|
206
|
+
}
|
|
207
|
+
#rowWeight(row) {
|
|
208
|
+
let total = 0;
|
|
209
|
+
for (const count of row.next.values())
|
|
210
|
+
total += this.#weigh(count);
|
|
211
|
+
return total;
|
|
212
|
+
}
|
|
213
|
+
#distribution(row, prior) {
|
|
214
|
+
const out = new Map();
|
|
215
|
+
const denom = (row ? this.#rowWeight(row) : 0) + this.#alpha;
|
|
216
|
+
if (denom <= 0)
|
|
217
|
+
return out;
|
|
218
|
+
const priorTotal = prior ? Object.values(prior).reduce((sum, n) => sum + n, 0) : 0;
|
|
219
|
+
const routes = new Set([...(row?.next.keys() ?? []), ...Object.keys(prior ?? {})]);
|
|
220
|
+
for (const route of routes) {
|
|
221
|
+
const count = row?.next.get(route);
|
|
222
|
+
const shared = prior && Object.hasOwn(prior, route) ? (prior[route] ?? 0) : 0;
|
|
223
|
+
const share = priorTotal > 0 ? shared / priorTotal : 0;
|
|
224
|
+
out.set(route, ((count ? this.#weigh(count) : 0) + this.#alpha * share) / denom);
|
|
225
|
+
}
|
|
226
|
+
return out;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/** a Wilson score lower bound for a proportion `p` over `weight` effective observations */
|
|
230
|
+
export function lowerBound(p, weight, z = 1.28) {
|
|
231
|
+
if (!(p >= 0 && p <= 1) || !(weight > 0) || !Number.isFinite(weight))
|
|
232
|
+
return 0;
|
|
233
|
+
const z2 = z * z;
|
|
234
|
+
const centre = p + z2 / (2 * weight);
|
|
235
|
+
const margin = z * Math.sqrt((p * (1 - p)) / weight + z2 / (4 * weight * weight));
|
|
236
|
+
const bound = (centre - margin) / (1 + z2 / weight);
|
|
237
|
+
return Math.min(p, Math.max(0, bound));
|
|
238
|
+
}
|
|
239
|
+
/** expected joules of acting on a candidate, with its probability taken at the lower bound */
|
|
240
|
+
export function expectedNet(input, z) {
|
|
241
|
+
return (lowerBound(input.p, input.weight, z) * input.avoided -
|
|
242
|
+
input.provision -
|
|
243
|
+
(input.render ?? 0) -
|
|
244
|
+
(input.maintain ?? 0) -
|
|
245
|
+
(input.model ?? 0));
|
|
246
|
+
}
|
|
247
|
+
export function shouldAct(input, z) {
|
|
248
|
+
return expectedNet(input, z) > 0;
|
|
249
|
+
}
|
|
@@ -3,9 +3,10 @@ export type NormaliseOptions = {
|
|
|
3
3
|
ignore?: readonly RegExp[];
|
|
4
4
|
};
|
|
5
5
|
/**
|
|
6
|
-
* Reduces markup to what two renders of the same page must share: comments
|
|
7
|
-
* whitespace collapsed to one space, whitespace between tags
|
|
8
|
-
* blanked (a per-render nonce, say). It does not reorder
|
|
6
|
+
* Reduces markup to what two renders of the same page must share: comments and the inline
|
|
7
|
+
* artifact element removed, runs of whitespace collapsed to one space, whitespace between tags
|
|
8
|
+
* dropped, and every `ignore` match blanked (a per-render nonce, say). It does not reorder
|
|
9
|
+
* attributes or fix tag soup.
|
|
9
10
|
*/
|
|
10
11
|
export declare function normalise(html: string, options?: NormaliseOptions): string;
|
|
11
12
|
/** the verdict of {@link equivalent}; a mismatch names where the two first differ */
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
import { stripInline } from './inline.js';
|
|
1
2
|
const IGNORED = '\u0000';
|
|
2
3
|
/**
|
|
3
|
-
* Reduces markup to what two renders of the same page must share: comments
|
|
4
|
-
* whitespace collapsed to one space, whitespace between tags
|
|
5
|
-
* blanked (a per-render nonce, say). It does not reorder
|
|
4
|
+
* Reduces markup to what two renders of the same page must share: comments and the inline
|
|
5
|
+
* artifact element removed, runs of whitespace collapsed to one space, whitespace between tags
|
|
6
|
+
* dropped, and every `ignore` match blanked (a per-render nonce, say). It does not reorder
|
|
7
|
+
* attributes or fix tag soup.
|
|
6
8
|
*/
|
|
7
9
|
export function normalise(html, options = {}) {
|
|
8
|
-
let out = html.replace(/<!--[\s\S]*?-->/g, '');
|
|
10
|
+
let out = stripInline(html).replace(/<!--[\s\S]*?-->/g, '');
|
|
9
11
|
for (const pattern of options.ignore ?? []) {
|
|
10
12
|
const flags = pattern.flags.includes('g') ? pattern.flags : `${pattern.flags}g`;
|
|
11
13
|
out = out.replace(new RegExp(pattern.source, flags), IGNORED);
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { QueryResult, Shaped } from './query.js';
|
|
2
|
+
type Plain = Record<string, unknown>;
|
|
3
|
+
export type PlanOp = 'eq' | 'in' | 'gte' | 'lte' | 'prefix';
|
|
4
|
+
/** one condition of a plan; `param` is the URL parameter that supplies its value */
|
|
5
|
+
export type PlanCondition = {
|
|
6
|
+
/** a base field, or `as.field` of a join */
|
|
7
|
+
field: string;
|
|
8
|
+
op: PlanOp;
|
|
9
|
+
param: string;
|
|
10
|
+
type: 'string' | 'number';
|
|
11
|
+
/** prefix only: ASCII-only lowercasing on both sides, as SQLite's lower() does */
|
|
12
|
+
fold?: 'ascii';
|
|
13
|
+
};
|
|
14
|
+
/** an equi-join from the base row; `on[1]` null matches the right dataset's key */
|
|
15
|
+
export type PlanJoin = {
|
|
16
|
+
dataset: string;
|
|
17
|
+
on: [string, string | null];
|
|
18
|
+
type: 'inner' | 'left';
|
|
19
|
+
/** the joined row's name in the output row */
|
|
20
|
+
as: string;
|
|
21
|
+
/** the right row's fields the plan may read */
|
|
22
|
+
fields: string[];
|
|
23
|
+
};
|
|
24
|
+
export type PlanAggregate = 'count' | {
|
|
25
|
+
count: string;
|
|
26
|
+
} | {
|
|
27
|
+
sum: string;
|
|
28
|
+
} | {
|
|
29
|
+
min: string;
|
|
30
|
+
} | {
|
|
31
|
+
max: string;
|
|
32
|
+
} | {
|
|
33
|
+
avg: string;
|
|
34
|
+
};
|
|
35
|
+
/** a read-only query over held datasets, declared on a route and written in JSON */
|
|
36
|
+
export type QueryPlan = {
|
|
37
|
+
/** base fields the plan may read; the output row carries exactly these */
|
|
38
|
+
from: string[];
|
|
39
|
+
join?: PlanJoin[];
|
|
40
|
+
/** conditions, all ANDed; a condition whose parameter is absent is skipped */
|
|
41
|
+
where?: PlanCondition[];
|
|
42
|
+
group?: {
|
|
43
|
+
by: string[];
|
|
44
|
+
aggregates: Record<string, PlanAggregate>;
|
|
45
|
+
};
|
|
46
|
+
/** fields the `sort` parameter may name; a leading minus in the URL sorts descending */
|
|
47
|
+
sort?: string[];
|
|
48
|
+
pageSize: number;
|
|
49
|
+
/** most result rows (groups or joined rows) the plan answers locally */
|
|
50
|
+
limit: number;
|
|
51
|
+
};
|
|
52
|
+
/** the held rows of one dataset in ascending key order (code point order) */
|
|
53
|
+
export type PlanTable = {
|
|
54
|
+
keys: readonly string[];
|
|
55
|
+
rows: ReadonlyMap<string, Plain>;
|
|
56
|
+
index?: TableIndex;
|
|
57
|
+
};
|
|
58
|
+
/** the base table and a way to reach each joined dataset; undefined when it is not held */
|
|
59
|
+
export type PlanTables = {
|
|
60
|
+
base: PlanTable;
|
|
61
|
+
joined(dataset: string): PlanTable | undefined;
|
|
62
|
+
};
|
|
63
|
+
/** the fewest rows a table holds before its fields are indexed; below it a scan is cheaper */
|
|
64
|
+
export declare const INDEX_MIN_ROWS = 200;
|
|
65
|
+
/** a stored value the engine compares exactly as SQLite does; anything else makes a plan not local */
|
|
66
|
+
export declare function plainValue(value: unknown): boolean;
|
|
67
|
+
/** UTF-8 byte order, which SQLite's BINARY collation uses; UTF-16 unit order differs above U+FFFF */
|
|
68
|
+
export declare function compareText(a: string, b: string): number;
|
|
69
|
+
/**
|
|
70
|
+
* Lazy per-field indexes over one table. Each index is built on first use and is only an
|
|
71
|
+
* optimisation: a plan gives the same answer with or without one.
|
|
72
|
+
*/
|
|
73
|
+
export declare class TableIndex {
|
|
74
|
+
#private;
|
|
75
|
+
constructor(keys: readonly string[], rows: ReadonlyMap<string, Plain>);
|
|
76
|
+
/** how many field indexes have been built */
|
|
77
|
+
get built(): number;
|
|
78
|
+
/** how many entries the built indexes hold, across every field */
|
|
79
|
+
get entries(): number;
|
|
80
|
+
/** whether every row's `field` holds a value {@link plainValue} accepts */
|
|
81
|
+
clean(field: string): boolean;
|
|
82
|
+
/** the keys whose `field` equals one of `values`, ascending */
|
|
83
|
+
equal(field: string, values: readonly (number | string)[]): string[];
|
|
84
|
+
/** the keys whose `field` is at least or at most `value`, within the value's type, ascending */
|
|
85
|
+
range(field: string, op: 'gte' | 'lte', value: number | string): string[];
|
|
86
|
+
/** the keys whose `field` starts with `text`, ascending */
|
|
87
|
+
prefix(field: string, fold: 'ascii' | undefined, text: string): string[];
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Answers a query string from held tables under `plan`, or returns null when the answer is not
|
|
91
|
+
* local: an unknown or repeated parameter, a value the plan refuses, a float or text in a sum, a
|
|
92
|
+
* table that is not held, a boolean or nested value in a declared field, or more result rows than
|
|
93
|
+
* `limit`. Equal to the SQL the plan describes, row for row.
|
|
94
|
+
*/
|
|
95
|
+
export declare function evaluatePlan(plan: QueryPlan, search: string, tables: PlanTables): QueryResult | null;
|
|
96
|
+
/** the page data for a plan route: the stored data with the rows under `list` and a `query` view */
|
|
97
|
+
export declare function shapePlan(list: string, plan: QueryPlan, data: unknown, search: string, tables: PlanTables | undefined): Shaped;
|
|
98
|
+
/** the validated copy of a plan; throws `bad-route` for a plan the engine could not answer exactly */
|
|
99
|
+
export declare function checkPlan(pattern: string, value: unknown): QueryPlan;
|
|
100
|
+
export {};
|