@runeditrun/rer 0.2.110 → 0.2.111
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 +24 -0
- package/cli/agent-door.mjs +12 -45
- package/cli/agent-history-database.mjs +13 -6
- package/cli/bounded.mjs +3 -3
- package/cli/contract.mjs +5 -1
- package/cli/deploy-command.mjs +2 -2
- package/cli/hosted-output.mjs +30 -10
- package/cli/hosted-owner.mjs +85 -0
- package/cli/inspect.mjs +6 -2
- package/cli/lifecycle.mjs +3 -1
- package/cli/main.mjs +13 -0
- package/cli/parse.mjs +4 -4
- package/cli/size/build-facts.mjs +58 -0
- package/cli/size/build-paths.mjs +3 -0
- package/cli/size/check.mjs +16 -0
- package/cli/size/clauses.mjs +21 -0
- package/cli/size/clock.mjs +21 -0
- package/cli/size/command.mjs +64 -0
- package/cli/size/declaration.mjs +87 -0
- package/cli/size/dml-parts.mjs +108 -0
- package/cli/size/examples/happytohelp.json +68 -0
- package/cli/size/expression-depth.mjs +45 -0
- package/cli/size/expression.mjs +33 -0
- package/cli/size/fixture.mjs +139 -0
- package/cli/size/flows.mjs +81 -0
- package/cli/size/from-items.mjs +124 -0
- package/cli/size/grow.mjs +83 -0
- package/cli/size/install.mjs +136 -0
- package/cli/size/json-duplicates.mjs +45 -0
- package/cli/size/limits.mjs +9 -0
- package/cli/size/measure.mjs +9 -0
- package/cli/size/meter.mjs +167 -0
- package/cli/size/personas.mjs +138 -0
- package/cli/size/report.mjs +23 -0
- package/cli/size/schema-objects.mjs +80 -0
- package/cli/size/schema.mjs +56 -0
- package/cli/size/statements.mjs +53 -0
- package/cli/size/validate-flows.mjs +109 -0
- package/cli/size/validate-kit.mjs +44 -0
- package/cli/size/validate-shape.mjs +75 -0
- package/cli/size/verdict.mjs +131 -0
- package/cli/size/verify-step.mjs +35 -0
- package/global/CHANGELOG.md +31 -0
- package/global/README.md +24 -0
- package/modules/agent-door/conformance/audit.mjs +3 -0
- package/modules/d1-transport/limits.mjs +5 -1
- package/modules/deploy-kit/sql-tokens.mjs +5 -4
- package/modules/operator-auth/accounts/one-click.mjs +2 -0
- package/modules/operator-auth/dev/index.mjs +7 -1
- package/modules/operator-auth/dev/invited.mjs +11 -0
- package/modules/operator-auth/dev-gate.mjs +3 -2
- package/modules/operator-auth/routes.mjs +4 -0
- package/modules/release/README.md +8 -0
- package/modules/release/archive-guard.mjs +22 -0
- package/modules/release/archive-members.mjs +49 -0
- package/modules/release/export.mjs +7 -1
- package/package.json +2 -1
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// The rows-read meter of `rer size`: a Worker module that wraps every D1 binding the app reads, and the Node-side
|
|
2
|
+
// collector the wrapper reports to. It exists only inside a disposable verification install: serveHosted adds it
|
|
3
|
+
// when a caller passes `meter`, and no other caller changes.
|
|
4
|
+
//
|
|
5
|
+
// Local D1's own meta.rows_read and meta.rows_written are the numbers. D1's first() and raw() return no meta, so the
|
|
6
|
+
// wrapper runs them through all() and reshapes the result; a raw() read therefore sees each column name once.
|
|
7
|
+
|
|
8
|
+
const identifier = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
9
|
+
|
|
10
|
+
// The wrapper's runtime, as Worker source. It runs in workerd: no imports, no Node.
|
|
11
|
+
const runtime = String.raw`
|
|
12
|
+
const originals = new WeakMap();
|
|
13
|
+
const wrappers = new WeakMap();
|
|
14
|
+
const param = value => value instanceof ArrayBuffer || ArrayBuffer.isView(value) ? { $blob: value.byteLength } : value;
|
|
15
|
+
const post = (service, event) => service.fetch('https://meter.invalid/', { method: 'POST', body: JSON.stringify(event) }).then(response => response.body?.cancel(), () => {});
|
|
16
|
+
|
|
17
|
+
async function measured(service, binding, sql, params, run) {
|
|
18
|
+
let result;
|
|
19
|
+
try { result = await run(); }
|
|
20
|
+
catch (error) { await post(service, { kind: 'statement', binding, sql, params, rows_read: 0, rows_written: 0, error: String(error?.message ?? error) }); throw error; }
|
|
21
|
+
await post(service, { kind: 'statement', binding, sql, params, rows_read: result?.meta?.rows_read ?? 0, rows_written: result?.meta?.rows_written ?? 0 });
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function wrapStatement(service, binding, statement, sql, params) {
|
|
26
|
+
const run = work => measured(service, binding, sql, params, work);
|
|
27
|
+
const wrapped = {
|
|
28
|
+
bind: (...values) => wrapStatement(service, binding, statement.bind(...values), sql, values.map(param)),
|
|
29
|
+
all: () => run(() => statement.all()),
|
|
30
|
+
run: () => run(() => statement.run()),
|
|
31
|
+
first: async column => {
|
|
32
|
+
const row = (await run(() => statement.all())).results[0];
|
|
33
|
+
if (column === undefined) return row ?? null;
|
|
34
|
+
if (row && !(column in row)) throw new Error('D1_ERROR: No such column: ' + column);
|
|
35
|
+
return row ? row[column] : null;
|
|
36
|
+
},
|
|
37
|
+
raw: async options => {
|
|
38
|
+
const { results } = await run(() => statement.all());
|
|
39
|
+
const rows = results.map(row => Object.values(row));
|
|
40
|
+
return options?.columnNames ? [Object.keys(results[0] ?? {}), ...rows] : rows;
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
originals.set(wrapped, { statement, sql, params });
|
|
44
|
+
return wrapped;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function wrapDatabase(service, binding, db) {
|
|
48
|
+
const wrapped = {
|
|
49
|
+
prepare: sql => wrapStatement(service, binding, db.prepare(sql), sql, []),
|
|
50
|
+
batch: async statements => {
|
|
51
|
+
const parts = statements.map(statement => originals.get(statement) ?? { statement, sql: '[unknown statement]', params: [] });
|
|
52
|
+
let results;
|
|
53
|
+
try { results = await db.batch(parts.map(part => part.statement)); }
|
|
54
|
+
catch (error) {
|
|
55
|
+
for (const part of parts) await post(service, { kind: 'statement', binding, sql: part.sql, params: part.params, rows_read: 0, rows_written: 0, batch: true, error: String(error?.message ?? error) });
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
for (const [index, part] of parts.entries()) await post(service, { kind: 'statement', binding, sql: part.sql, params: part.params, rows_read: results[index]?.meta?.rows_read ?? 0, rows_written: results[index]?.meta?.rows_written ?? 0, batch: true });
|
|
59
|
+
return results;
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
if (typeof db.exec === 'function') wrapped.exec = async sql => {
|
|
63
|
+
try { const result = await db.exec(sql); await post(service, { kind: 'statement', binding, sql, params: [], rows_read: 0, rows_written: 0, exec: true }); return result; }
|
|
64
|
+
catch (error) { await post(service, { kind: 'statement', binding, sql, params: [], rows_read: 0, rows_written: 0, exec: true, error: String(error?.message ?? error) }); throw error; }
|
|
65
|
+
};
|
|
66
|
+
if (typeof db.dump === 'function') wrapped.dump = () => db.dump();
|
|
67
|
+
if (typeof db.withSession === 'function') wrapped.withSession = constraint => wrapDatabase(service, binding, db.withSession(constraint));
|
|
68
|
+
if (typeof db.getBookmark === 'function') wrapped.getBookmark = () => db.getBookmark();
|
|
69
|
+
return wrapped;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// One wrapper per underlying binding, so an application that caches its database handle keeps one.
|
|
73
|
+
function meteredDatabase(env, binding) {
|
|
74
|
+
const db = env[binding];
|
|
75
|
+
if (!db) return db;
|
|
76
|
+
if (!wrappers.has(db)) wrappers.set(db, wrapDatabase(env.RER_SIZE_METER, binding, db));
|
|
77
|
+
return wrappers.get(db);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function wrapEnv(env) {
|
|
81
|
+
const wrapped = { ...env };
|
|
82
|
+
for (const binding of D1_BINDINGS) if (env[binding]) wrapped[binding] = meteredDatabase(env, binding);
|
|
83
|
+
return wrapped;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// waitUntil work is announced to the collector, which closes a flow's window only once it has settled. The
|
|
87
|
+
// announcements are awaited before the handler answers, so none is still in flight when the response is read.
|
|
88
|
+
function wrapContext(env, ctx, announced) {
|
|
89
|
+
return new Proxy(ctx, { get: (target, key) => {
|
|
90
|
+
if (key === 'waitUntil') return promise => {
|
|
91
|
+
const start = post(env.RER_SIZE_METER, { kind: 'wait', delta: 1 });
|
|
92
|
+
announced.push(start);
|
|
93
|
+
target.waitUntil(Promise.resolve(promise).finally(async () => { await start; await post(env.RER_SIZE_METER, { kind: 'wait', delta: -1 }); }));
|
|
94
|
+
};
|
|
95
|
+
const value = target[key];
|
|
96
|
+
return typeof value === 'function' ? value.bind(target) : value;
|
|
97
|
+
} });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const handled = (name, handler) => async (first, env, ctx) => {
|
|
101
|
+
const announced = [];
|
|
102
|
+
try { return await handler.call(app, first, wrapEnv(env), wrapContext(env, ctx, announced)); }
|
|
103
|
+
finally { await Promise.all(announced); }
|
|
104
|
+
};
|
|
105
|
+
`;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The text of the wrapper module the Worker runs as its entry. It imports `inner` (the app, or the preparation
|
|
109
|
+
* wrapper around it), re-exports everything, wraps the D1 bindings named in `d1` in the env of `fetch`, `scheduled`
|
|
110
|
+
* and `queue`, and exports each Durable Object class in `durableObjects` as a subclass of the same name whose
|
|
111
|
+
* constructor env is wrapped.
|
|
112
|
+
*/
|
|
113
|
+
export function meterModule({ inner, d1 = [], durableObjects = [] }) {
|
|
114
|
+
const classes = durableObjects.filter(name => identifier.test(name));
|
|
115
|
+
return [
|
|
116
|
+
`import * as native from ${JSON.stringify(inner)};`,
|
|
117
|
+
`export * from ${JSON.stringify(inner)};`,
|
|
118
|
+
`const app = native.default;`,
|
|
119
|
+
`const D1_BINDINGS = ${JSON.stringify(d1)};`,
|
|
120
|
+
`if (!app || typeof app !== 'object') throw new Error('rer size meters a Worker whose default export is a handler object with fetch, scheduled and queue methods.');`,
|
|
121
|
+
runtime,
|
|
122
|
+
...classes.map(name => `export class ${name} extends native.${name} { constructor(ctx, env) { super(ctx, wrapEnv(env)); } }`),
|
|
123
|
+
`export default { ...app, ...(app.fetch ? { fetch: handled('fetch', app.fetch) } : {}), ...(app.scheduled ? { scheduled: handled('scheduled', app.scheduled) } : {}), ...(app.queue ? { queue: handled('queue', app.queue) } : {}) };`,
|
|
124
|
+
].join('\n');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The Node-side collector. `service` is the function the install's Miniflare gets as the RER_SIZE_METER service
|
|
131
|
+
* binding. `open()` starts a flow's window; `close()` waits until the window's work has finished and returns what
|
|
132
|
+
* ran in it: { statements: [{ binding, sql, params, rows_read, rows_written, error?, exec?, batch? }], unsettled }.
|
|
133
|
+
* Work outside a window (the owner's sign-in, other personas) is not recorded.
|
|
134
|
+
*/
|
|
135
|
+
export function createMeter() {
|
|
136
|
+
let current = null, pending = 0, lastEvent = 0;
|
|
137
|
+
const service = async request => {
|
|
138
|
+
const event = await request.json();
|
|
139
|
+
lastEvent = performance.now();
|
|
140
|
+
if (event.kind === 'wait') pending = Math.max(0, pending + event.delta);
|
|
141
|
+
else if (current) current.statements.push({ binding: event.binding, sql: event.sql, params: event.params ?? [], rows_read: event.rows_read ?? 0, rows_written: event.rows_written ?? 0, ...(event.error ? { error: event.error } : {}), ...(event.exec ? { exec: true } : {}), ...(event.batch ? { batch: true } : {}) });
|
|
142
|
+
return new Response(null, { status: 204 });
|
|
143
|
+
};
|
|
144
|
+
return {
|
|
145
|
+
service,
|
|
146
|
+
/** Starts a window. The returned `close` ends it once the caller has read the response, its waitUntil work has
|
|
147
|
+
* settled and no statement has been recorded for `quietMs`; after `maxMs` it ends regardless and says so. */
|
|
148
|
+
open() {
|
|
149
|
+
if (current) throw new Error('A meter window is already open.');
|
|
150
|
+
const window = current = { statements: [] };
|
|
151
|
+
return {
|
|
152
|
+
close: async ({ quietMs = 250, maxMs = 30_000 } = {}) => {
|
|
153
|
+
const readAt = performance.now();
|
|
154
|
+
let unsettled = false;
|
|
155
|
+
while (true) {
|
|
156
|
+
const now = performance.now(), quiet = now - Math.max(lastEvent, readAt) >= quietMs;
|
|
157
|
+
if (pending === 0 && quiet) break;
|
|
158
|
+
if (now - readAt >= maxMs) { unsettled = true; break; }
|
|
159
|
+
await sleep(Math.min(25, quietMs));
|
|
160
|
+
}
|
|
161
|
+
current = null;
|
|
162
|
+
return { statements: window.statements, unsettled };
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { RerError } from '../errors.mjs';
|
|
2
|
+
import { hostedRequest } from '../hosted-owner.mjs';
|
|
3
|
+
|
|
4
|
+
// The people a flow runs as. owner is the seed's verification owner, already signed in by the install; anonymous
|
|
5
|
+
// has no session; signin runs its declared requests in order against the install, keeping a cookie jar and the
|
|
6
|
+
// values it captures by JSON pointer. Every request carries the install's Origin header.
|
|
7
|
+
|
|
8
|
+
const resolveSetup = (text, variables, where) => text.replace(/\{\{\s*([A-Za-z_][A-Za-z0-9_]*)\s*\}\}/g, (_, name) => {
|
|
9
|
+
if (!variables.has(name)) throw new RerError('size_persona_failed', `${where}: {{${name}}} has no value; an earlier request of the persona must capture it.`, 1);
|
|
10
|
+
return variables.get(name);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
/** Replaces each {{persona.var}} (or {{var}} inside a persona, when `own` is given) in `text`. */
|
|
14
|
+
export function fill(text, personas, where, own) {
|
|
15
|
+
return text.replace(/\{\{\s*(?:([a-z][a-z0-9_]*)\.)?([A-Za-z_][A-Za-z0-9_]*)\s*\}\}/g, (_, persona, name) => {
|
|
16
|
+
const variables = persona === undefined ? own : personas.get(persona)?.variables;
|
|
17
|
+
if (!variables?.has(name)) throw new RerError('size_flow_failed', `${where}: {{${persona ? `${persona}.` : ''}${name}}} has no value; the persona has not captured it.`, 1);
|
|
18
|
+
return variables.get(name);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const deep = (value, change) => typeof value === 'string' ? change(value) : Array.isArray(value) ? value.map(item => deep(item, change))
|
|
23
|
+
: value !== null && typeof value === 'object' ? Object.fromEntries(Object.entries(value).map(([key, item]) => [key, deep(item, change)])) : value;
|
|
24
|
+
|
|
25
|
+
// The cookie jar: name=value pairs from Set-Cookie, a cookie that expires or has Max-Age 0 removed.
|
|
26
|
+
class Jar {
|
|
27
|
+
cookies = new Map();
|
|
28
|
+
constructor(header) { for (const part of header?.split(';') ?? []) { const [name, ...value] = part.trim().split('='); if (name) this.cookies.set(name, value.join('=')); } }
|
|
29
|
+
keep(response) {
|
|
30
|
+
for (const line of response.headers.getSetCookie()) {
|
|
31
|
+
const [pair, ...attributes] = line.split(';'), at = pair.indexOf('=');
|
|
32
|
+
if (at < 1) continue;
|
|
33
|
+
const name = pair.slice(0, at).trim(), value = pair.slice(at + 1).trim();
|
|
34
|
+
const gone = attributes.some(attribute => {
|
|
35
|
+
const [key, text = ''] = attribute.trim().split('='), lower = key.toLowerCase();
|
|
36
|
+
return lower === 'max-age' ? Number(text) <= 0 : lower === 'expires' && Date.parse(text) < Date.now();
|
|
37
|
+
});
|
|
38
|
+
if (gone || value === '') this.cookies.delete(name); else this.cookies.set(name, value);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
get header() { return [...this.cookies].map(([name, value]) => `${name}=${value}`).join('; '); }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const pointerValue = (document, pointer) => pointer.slice(1).split('/').map(part => part.replace(/~1/g, '/').replace(/~0/g, '~'))
|
|
45
|
+
.reduce((value, key) => value !== null && typeof value === 'object' ? value[key] : undefined, document);
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* One persona of an install: `send(spec, personas, where)` makes a request as the persona. `personas` maps a name
|
|
49
|
+
* to { kind, variables, send }.
|
|
50
|
+
*/
|
|
51
|
+
function persona(hosted, kind, { jar, headers = {} }) {
|
|
52
|
+
const request = hostedRequest(hosted);
|
|
53
|
+
const state = { kind, variables: new Map(), jar, headers };
|
|
54
|
+
// spec: { method, path, body?, headers? }, every text already filled in.
|
|
55
|
+
state.send = async ({ method, path, body, headers: own = {} }) => {
|
|
56
|
+
const sent = new Headers({ origin: hosted.origin, ...state.headers });
|
|
57
|
+
if (state.jar?.header) sent.set('cookie', state.jar.header);
|
|
58
|
+
for (const [name, value] of Object.entries(own)) sent.set(name, value);
|
|
59
|
+
let payload;
|
|
60
|
+
if (body !== undefined) {
|
|
61
|
+
payload = typeof body === 'string' ? body : JSON.stringify(body);
|
|
62
|
+
if (!sent.has('content-type') && typeof body !== 'string') sent.set('content-type', 'application/json');
|
|
63
|
+
}
|
|
64
|
+
const response = await request(new URL(path, hosted.origin).href, { method, headers: Object.fromEntries(sent), ...(payload === undefined || method === 'GET' ? {} : { body: payload }) });
|
|
65
|
+
state.jar?.keep(response);
|
|
66
|
+
return response;
|
|
67
|
+
};
|
|
68
|
+
return state;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Signs every declared persona in on a freshly built install and resolves a Map name -> persona. `ownerCookie` is
|
|
73
|
+
* the owner's session cookie (what the install's bootstrap produced). A persona that cannot sign in rejects with an
|
|
74
|
+
* error naming the persona, the request index and the status.
|
|
75
|
+
*/
|
|
76
|
+
export async function signPersonasIn(hosted, declared, { ownerCookie } = {}) {
|
|
77
|
+
const personas = new Map();
|
|
78
|
+
for (const [name, spec] of Object.entries(declared ?? {})) {
|
|
79
|
+
if (spec.kind === 'owner') {
|
|
80
|
+
if (ownerCookie === undefined) throw new RerError('size_persona_failed', `Persona ${name} is the owner, and this install has no owner session.`, 1);
|
|
81
|
+
personas.set(name, persona(hosted, 'owner', { jar: new Jar(ownerCookie) }));
|
|
82
|
+
} else if (spec.kind === 'anonymous') personas.set(name, persona(hosted, 'anonymous', {}));
|
|
83
|
+
else {
|
|
84
|
+
const signed = persona(hosted, 'signin', { jar: new Jar() });
|
|
85
|
+
personas.set(name, signed);
|
|
86
|
+
for (const [index, step] of spec.requests.entries()) {
|
|
87
|
+
const where = `Persona ${name} could not sign in: requests[${index}] ${step.method} ${step.path}`;
|
|
88
|
+
const own = signed.variables;
|
|
89
|
+
let response;
|
|
90
|
+
try {
|
|
91
|
+
response = await signed.send({ method: step.method, path: resolveSetup(step.path, own, where),
|
|
92
|
+
body: step.body === undefined ? undefined : deep(step.body, text => resolveSetup(text, own, where)),
|
|
93
|
+
headers: Object.fromEntries(Object.entries(step.headers ?? {}).map(([key, text]) => [key, resolveSetup(text, own, where)])) });
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (error instanceof RerError) throw error;
|
|
96
|
+
throw new RerError('size_persona_failed', `${where} failed: ${String(error?.message ?? error).slice(0, 200)}`, 1, { persona: name, request: index });
|
|
97
|
+
}
|
|
98
|
+
const text = await response.text();
|
|
99
|
+
if (response.status < 200 || response.status >= 400) throw new RerError('size_persona_failed', `${where} answered HTTP ${response.status}.`, 1, { persona: name, request: index, status: response.status });
|
|
100
|
+
if (step.capture) {
|
|
101
|
+
let json;
|
|
102
|
+
try { json = JSON.parse(text); } catch { throw new RerError('size_persona_failed', `${where} answered HTTP ${response.status} but not JSON, so its captures (${Object.keys(step.capture).join(', ')}) cannot be read.`, 1, { persona: name, request: index, status: response.status }); }
|
|
103
|
+
for (const [variable, pointer] of Object.entries(step.capture)) {
|
|
104
|
+
const value = pointerValue(json, pointer);
|
|
105
|
+
if (!['string', 'number', 'boolean'].includes(typeof value)) throw new RerError('size_persona_failed', `${where} answered HTTP ${response.status} but its response has no text or number at ${pointer} to capture as ${variable}.`, 1, { persona: name, request: index, status: response.status });
|
|
106
|
+
own.set(variable, String(value));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
// Headers sent on this persona's flows, with the captured values filled in.
|
|
111
|
+
signed.headers = Object.fromEntries(Object.entries(spec.headers ?? {}).map(([key, text]) => [key, resolveSetup(text, signed.variables, `Persona ${name} headers`)]));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return personas;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** A flow's request made as its persona: path, body and headers filled with {{persona.var}} from every persona. */
|
|
118
|
+
export function sendAs(personas, flow) {
|
|
119
|
+
const who = personas.get(flow.persona), where = `Flow ${flow.name}`, { request } = flow;
|
|
120
|
+
if (!who) throw new RerError('size_flow_failed', `${where}: persona ${flow.persona} is not signed in.`, 1);
|
|
121
|
+
const text = value => fill(value, personas, where);
|
|
122
|
+
return who.send({ method: request.method, path: text(request.path), body: request.body === undefined ? undefined : deep(request.body, text),
|
|
123
|
+
headers: Object.fromEntries(Object.entries(request.headers ?? {}).map(([key, value]) => [key, text(value)])) });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The text of a flow's path with its templates filled, for an upgrade and for the line a person reads. */
|
|
127
|
+
export function pathFor(personas, flow) {
|
|
128
|
+
return fill(flow.request.path, personas, `Flow ${flow.name}`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Headers a flow's persona sends on a WebSocket upgrade: origin, the jar's cookie and the persona's headers. */
|
|
132
|
+
export function upgradeHeaders(personas, flow, origin) {
|
|
133
|
+
const who = personas.get(flow.persona);
|
|
134
|
+
const headers = new Headers({ origin, ...who.headers });
|
|
135
|
+
if (who.jar?.header) headers.set('cookie', who.jar.header);
|
|
136
|
+
for (const [key, value] of Object.entries(flow.request.headers ?? {})) headers.set(key, fill(value, personas, `Flow ${flow.name}`));
|
|
137
|
+
return Object.fromEntries(headers);
|
|
138
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SIZE_MARGIN, d1Limits, failAt } from './limits.mjs';
|
|
2
|
+
|
|
3
|
+
const preview = sql => sql.replace(/\s+/g, ' ').trim().slice(0, 160);
|
|
4
|
+
|
|
5
|
+
/** What a person reads: each failure named with both numbers and its first 160
|
|
6
|
+
* characters, then a summary, then what to do. `records` are measureStatements
|
|
7
|
+
* results; `sqlByName` finds a statement's text. `unit` names what was checked. */
|
|
8
|
+
export function sizeReport(records, sqlOf, unit = 'statements') {
|
|
9
|
+
const failed = records.filter(record => !record.ok), passed = records.filter(record => record.ok);
|
|
10
|
+
const largest = `largest${failed.length ? ' otherwise' : ''} ${Math.max(0, ...passed.map(record => record.fromItems))} FROM items, depth ${Math.max(1, ...passed.map(record => record.depth ?? 0))}`;
|
|
11
|
+
if (!failed.length) return [`Statement size: ${records.length} ${unit} checked; ${largest}.`];
|
|
12
|
+
const lines = [`Statement size (D1 refuses at ${d1Limits.fromItems} FROM items or expression depth ${d1Limits.depth}; rer fails at ${failAt.fromItems}):`];
|
|
13
|
+
for (const record of failed) {
|
|
14
|
+
lines.push(record.error
|
|
15
|
+
? ` FAIL ${record.name}: ${record.error}`
|
|
16
|
+
: ` FAIL ${record.name}: ${record.fromItems} FROM items after expanding CTEs and views, depth ${record.depth}`);
|
|
17
|
+
lines.push(` ${preview(sqlOf(record))}`);
|
|
18
|
+
}
|
|
19
|
+
lines.push(` ${failed.length} of ${records.length} ${unit} fail${failed.length === 1 ? 's' : ''}; ${largest}.`,
|
|
20
|
+
' A CTE or view counts its FROM items again at every reference, and nested EXISTS, IN and scalar subqueries add their whole depth.',
|
|
21
|
+
` Read each CTE once, or split the statement in two. D1 changed how it enforces these limits without notice on 10 Nov 2026; the margin of ${SIZE_MARGIN} is for that.`);
|
|
22
|
+
return lines;
|
|
23
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { sqlTokens } from '../../modules/deploy-kit/sql-tokens.mjs';
|
|
2
|
+
|
|
3
|
+
// The statements a schema's views and triggers will run, each as plain SQL that
|
|
4
|
+
// compiles on its own: a view as its SELECT, a trigger as its WHEN clause (a
|
|
5
|
+
// SELECT) and each body statement, with NEW.x and OLD.x as parameters and
|
|
6
|
+
// RAISE(...) as NULL, which have the same expression heights.
|
|
7
|
+
const sym = (token, char) => token?.kind === 'symbol' && token.value === char;
|
|
8
|
+
const word = token => token?.kind === 'word' ? token.value.toUpperCase() : '';
|
|
9
|
+
|
|
10
|
+
function neutralise(sql) {
|
|
11
|
+
const t = sqlTokens(sql), replacements = [];
|
|
12
|
+
for (let i = 0; i < t.length; i++) {
|
|
13
|
+
if (['NEW', 'OLD'].includes(word(t[i])) && sym(t[i + 1], '.') && ['word', 'identifier'].includes(t[i + 2]?.kind)) {
|
|
14
|
+
replacements.push([t[i].start, t[i + 2].end, '?']);
|
|
15
|
+
i += 2;
|
|
16
|
+
} else if (word(t[i]) === 'RAISE' && sym(t[i + 1], '(')) {
|
|
17
|
+
let depth = 0, j = i + 1;
|
|
18
|
+
for (; j < t.length; j++) { if (sym(t[j], '(')) depth++; else if (sym(t[j], ')') && !--depth) break; }
|
|
19
|
+
replacements.push([t[i].start, t[Math.min(j, t.length - 1)].end, 'NULL']);
|
|
20
|
+
i = j;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
let out = '', at = 0;
|
|
24
|
+
for (const [start, end, text] of replacements) { out += sql.slice(at, start) + text; at = end; }
|
|
25
|
+
return out + sql.slice(at);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// A view's own SELECT: what follows the first top-level AS.
|
|
29
|
+
function viewSelect(sql) {
|
|
30
|
+
const t = sqlTokens(sql);
|
|
31
|
+
let depth = 0;
|
|
32
|
+
for (let i = 0; i < t.length - 1; i++) {
|
|
33
|
+
if (sym(t[i], '(')) depth++;
|
|
34
|
+
else if (sym(t[i], ')')) depth--;
|
|
35
|
+
else if (!depth && word(t[i]) === 'AS' && ['SELECT', 'WITH', 'VALUES'].includes(word(t[i + 1]))) return sql.slice(t[i + 1].start);
|
|
36
|
+
}
|
|
37
|
+
throw new Error(`cannot find the SELECT of ${sql.slice(0, 80)}`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// The WHEN clause and the statements between BEGIN and END, split at the
|
|
41
|
+
// top-level semicolons that are not inside a CASE ... END.
|
|
42
|
+
function triggerParts(sql) {
|
|
43
|
+
const t = sqlTokens(sql);
|
|
44
|
+
const begin = t.findIndex(token => word(token) === 'BEGIN');
|
|
45
|
+
let end = t.length - 1;
|
|
46
|
+
while (end > begin && sym(t[end], ';')) end--;
|
|
47
|
+
if (begin < 0 || word(t[end]) !== 'END') throw new Error(`cannot find the body of ${sql.slice(0, 80)}`);
|
|
48
|
+
const when = t.findIndex(token => word(token) === 'WHEN');
|
|
49
|
+
const statements = [];
|
|
50
|
+
let depth = 0, cases = 0, first = begin + 1;
|
|
51
|
+
const close = last => { if (last > first) statements.push(sql.slice(t[first].start, t[last - 1].end)); first = last + 1; };
|
|
52
|
+
for (let i = begin + 1; i < end; i++) {
|
|
53
|
+
if (sym(t[i], '(')) depth++;
|
|
54
|
+
else if (sym(t[i], ')')) depth--;
|
|
55
|
+
else if (word(t[i]) === 'CASE') cases++;
|
|
56
|
+
else if (word(t[i]) === 'END') cases--;
|
|
57
|
+
else if (sym(t[i], ';') && !depth && !cases) close(i);
|
|
58
|
+
}
|
|
59
|
+
close(end);
|
|
60
|
+
return { when: when >= 0 && when < begin ? sql.slice(t[when + 1].start, t[begin - 1].end) : '', statements };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** From a database holding the schema: { views: [{ name, select }], statements:
|
|
64
|
+
* [{ name, sql }] } for every view and trigger. */
|
|
65
|
+
export function schemaObjects(db) {
|
|
66
|
+
const objects = db.prepare("SELECT type, name, tbl_name, sql FROM sqlite_master WHERE type IN ('view', 'trigger') ORDER BY type, name").all();
|
|
67
|
+
const views = [], statements = [];
|
|
68
|
+
for (const object of objects) {
|
|
69
|
+
if (object.type === 'view') {
|
|
70
|
+
const select = viewSelect(object.sql);
|
|
71
|
+
views.push({ name: object.name, select });
|
|
72
|
+
statements.push({ name: `view ${object.name}`, sql: select });
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const { when, statements: body } = triggerParts(object.sql), label = `trigger ${object.name}`;
|
|
76
|
+
if (when) statements.push({ name: `${label} WHEN (on ${object.tbl_name})`, sql: `SELECT ${neutralise(when)}` });
|
|
77
|
+
body.forEach((sql, i) => statements.push({ name: `${label} #${i + 1} (on ${object.tbl_name})`, sql: neutralise(sql) }));
|
|
78
|
+
}
|
|
79
|
+
return { views, statements };
|
|
80
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import '../sqlite-notice.mjs';
|
|
2
|
+
import { readFile, readdir } from 'node:fs/promises';
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { dirname, join, resolve } from 'node:path';
|
|
5
|
+
import { readMigrationRewrites, withRewrites } from '../../modules/deploy-kit/migrations.mjs';
|
|
6
|
+
import { RerError, missing } from '../errors.mjs';
|
|
7
|
+
import { defaultBuild } from './build-paths.mjs';
|
|
8
|
+
|
|
9
|
+
// The schema a build's D1 binding gets: its migrations, in order, in a fresh
|
|
10
|
+
// in-memory SQLite. Nothing here reaches a network or an account.
|
|
11
|
+
async function migrationFiles(directory) {
|
|
12
|
+
const names = (await readdir(directory)).filter(name => name.endsWith('.sql')).sort();
|
|
13
|
+
return Promise.all(names.map(async name => ({ name, sql: await readFile(join(directory, name), 'utf8') })));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// A build's missing migrations directory is an empty database, as serveHosted reads it.
|
|
17
|
+
async function apply(directory, label, { optional = false } = {}) {
|
|
18
|
+
const { DatabaseSync } = await import('node:sqlite');
|
|
19
|
+
// Foreign keys are D1's to enforce; here only the schema objects matter. D1 (and local D1) accepts a double-quoted
|
|
20
|
+
// string literal where no such column exists, and a statement that does so must compile here too.
|
|
21
|
+
const db = new DatabaseSync(':memory:', { enableForeignKeyConstraints: false, enableDoubleQuotedStringLiterals: true });
|
|
22
|
+
let files;
|
|
23
|
+
try { files = await migrationFiles(directory); }
|
|
24
|
+
catch (error) { if (optional && error.code === 'ENOENT') files = []; else throw missing(`Cannot read the migrations of ${label} at ${directory}: ${error.code ?? error.message}.`); }
|
|
25
|
+
for (const { name, sql } of files) {
|
|
26
|
+
try { db.exec(sql); }
|
|
27
|
+
catch (error) { throw new RerError('size_schema_invalid', `Migration ${name} of ${label} does not apply in SQLite: ${error.message}`, 1); }
|
|
28
|
+
}
|
|
29
|
+
return { db, migrations: files.length };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** One schema from a directory of migrations: { db, migrations }. */
|
|
33
|
+
export const schemaFromMigrations = directory => apply(resolve(directory), resolve(directory));
|
|
34
|
+
|
|
35
|
+
/** The build's D1 databases, each with its migrations applied as serveHosted
|
|
36
|
+
* does (the build's wrangler file, dist/server/wrangler.json unless `wrangler`
|
|
37
|
+
* names another, d1_databases[].migrations_dir from that file's folder, with the
|
|
38
|
+
* seed's reviewed migration rewrites checked against the migrations'
|
|
39
|
+
* current bytes): [{ binding, db, migrations }]. An empty list is a build
|
|
40
|
+
* with no D1 binding; no build is a missing input. */
|
|
41
|
+
export async function schemasFromBuild(root, { wrangler = defaultBuild.wrangler } = {}) {
|
|
42
|
+
const file = join(root, wrangler), build = dirname(file);
|
|
43
|
+
let config;
|
|
44
|
+
try { config = JSON.parse(await readFile(file, 'utf8')); }
|
|
45
|
+
catch { throw missing(`No production build at ${wrangler}. Run rer build, or point --migrations at a directory of D1 migrations.`); }
|
|
46
|
+
const schemas = [];
|
|
47
|
+
for (const item of config.d1_databases ?? []) {
|
|
48
|
+
const directory = resolve(build, item.migrations_dir ?? 'migrations');
|
|
49
|
+
const files = await migrationFiles(directory).catch(() => []);
|
|
50
|
+
try {
|
|
51
|
+
withRewrites(files.map(({ name, sql }) => ({ name, sha256: createHash('sha256').update(sql).digest('hex') })), await readMigrationRewrites(root), { partial: true });
|
|
52
|
+
} catch (error) { throw new RerError('size_schema_invalid', `The migration rewrites of ${item.binding} do not match its migrations (${error.message}).`, 1); }
|
|
53
|
+
schemas.push({ binding: item.binding, ...await apply(directory, `database ${item.binding}`, { optional: true }) });
|
|
54
|
+
}
|
|
55
|
+
return schemas;
|
|
56
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { sqlTokens } from '../../modules/deploy-kit/sql-tokens.mjs';
|
|
3
|
+
import { invalid, missing } from '../errors.mjs';
|
|
4
|
+
import { fromItems, tokenize, viewScope } from './from-items.mjs';
|
|
5
|
+
import { depthMeter, MeasureError } from './expression-depth.mjs';
|
|
6
|
+
import { schemaObjects } from './schema-objects.mjs';
|
|
7
|
+
import { failAt } from './limits.mjs';
|
|
8
|
+
|
|
9
|
+
/** The fleet runner's statements file: { statements: [{ name, sql, params? }] }.
|
|
10
|
+
* Other keys, and params, are ignored. */
|
|
11
|
+
export async function readStatementsFile(path) {
|
|
12
|
+
let text;
|
|
13
|
+
try { text = await readFile(path, 'utf8'); } catch (error) { throw missing(`Cannot read the statements file ${path}: ${error.code ?? error.message}.`); }
|
|
14
|
+
let file;
|
|
15
|
+
try { file = JSON.parse(text); } catch { throw invalid(`${path} is not JSON. Expected {"statements":[{"name":"...","sql":"..."}]}.`); }
|
|
16
|
+
if (!Array.isArray(file?.statements) || !file.statements.every(item => typeof item?.name === 'string' && typeof item?.sql === 'string'))
|
|
17
|
+
throw invalid(`${path} must hold {"statements":[{"name":"...","sql":"..."}]}, each with a name and sql.`);
|
|
18
|
+
return file.statements.map(({ name, sql }) => ({ name, sql }));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// A trailing ; is fine; a second statement would go unmeasured.
|
|
22
|
+
function oneStatement(sql) {
|
|
23
|
+
const t = sqlTokens(sql);
|
|
24
|
+
const stop = t.findIndex((token, i) => token.kind === 'symbol' && token.value === ';' && i < t.length - 1);
|
|
25
|
+
if (stop >= 0) throw new MeasureError('it holds more than one statement; give each statement its own entry');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Measure statements against a database holding the schema. Each record is
|
|
29
|
+
* { name, fromItems, depth, ok } or, when SQLite cannot compile the statement
|
|
30
|
+
* (or it cannot be measured), { name, ok: false, error }. A statement's depth
|
|
31
|
+
* is looked up exactly when it fails (by either measure), when `exact` asks for every one, and
|
|
32
|
+
* when it is the largest passing so far; otherwise it is only known to pass. */
|
|
33
|
+
export function measureStatements(db, statements, { exact = false } = {}) {
|
|
34
|
+
const scope = viewScope(schemaObjects(db).views), meter = depthMeter(db);
|
|
35
|
+
let largest = 1;
|
|
36
|
+
return statements.map(({ name, sql }) => {
|
|
37
|
+
try {
|
|
38
|
+
oneStatement(sql);
|
|
39
|
+
try { db.prepare(sql); } catch (error) { throw new MeasureError(`SQLite cannot compile it: ${error.message}`); }
|
|
40
|
+
const tokens = tokenize(sql), parts = meter.parts(sql);
|
|
41
|
+
const items = fromItems(sql, scope, tokens);
|
|
42
|
+
let depth;
|
|
43
|
+
if (!meter.atMost(parts, failAt.depth - 1)) depth = meter.exact(parts);
|
|
44
|
+
else {
|
|
45
|
+
if (exact || items >= failAt.fromItems || !meter.atMost(parts, largest)) depth = meter.exact(parts);
|
|
46
|
+
if (items < failAt.fromItems) largest = Math.max(largest, depth ?? 1);
|
|
47
|
+
}
|
|
48
|
+
return { name, fromItems: items, depth: depth ?? null, ok: items < failAt.fromItems && (depth ?? 0) < failAt.depth };
|
|
49
|
+
} catch (error) {
|
|
50
|
+
return { name, ok: false, error: error.message };
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { count, integer, isInt, isObject, keyed, methods, name as personaName, slug, stringsIn, templateRefs, variable } from './validate-kit.mjs';
|
|
2
|
+
|
|
3
|
+
const pointer = /^(?:\/(?:[^~/]|~[01])*)+$/;
|
|
4
|
+
|
|
5
|
+
// Template problems in `texts`. `own` is the variable set a {{var}} may use (inside a persona); without it only
|
|
6
|
+
// {{persona.var}} is allowed (inside a flow) and `personas` maps each persona to its captured variables.
|
|
7
|
+
function templates(found, texts, path, { own, personas }) {
|
|
8
|
+
for (const text of texts) for (const ref of templateRefs(text)) {
|
|
9
|
+
if (ref.bad) found.add(path, `${ref.bad} is not a template; write {{var}} inside a persona or {{persona.var}} inside a flow.`);
|
|
10
|
+
else if (own) {
|
|
11
|
+
if (ref.persona) found.add(path, `{{${ref.persona}.${ref.variable}}} names a persona inside a persona's own requests; write {{${ref.variable}}}.`);
|
|
12
|
+
else if (!own.has(ref.variable)) found.add(path, `{{${ref.variable}}} is not captured by an earlier request of this persona; add capture ${ref.variable} to the request that returns it, or correct the name.`);
|
|
13
|
+
} else if (!ref.persona) found.add(path, `{{${ref.variable}}} needs its persona in a flow; write {{persona.${ref.variable}}}.`);
|
|
14
|
+
else if (!personas.has(ref.persona)) found.add(path, `{{${ref.persona}.${ref.variable}}}: persona ${ref.persona} is not declared in personas.`);
|
|
15
|
+
else if (!personas.get(ref.persona).has(ref.variable)) found.add(path, `{{${ref.persona}.${ref.variable}}}: persona ${ref.persona} captures no ${ref.variable}; add capture ${ref.variable} to one of its requests, or correct the name.`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function headers(found, value, path, scope) {
|
|
20
|
+
if (!isObject(value)) { found.add(path, 'must map header names to text, for example { "Authorization": "Bearer {{token}}" }.'); return; }
|
|
21
|
+
for (const [header, text] of Object.entries(value)) {
|
|
22
|
+
if (typeof text !== 'string') found.add([...path, header], 'must be text.');
|
|
23
|
+
else templates(found, [text], [...path, header], scope);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function request(found, value, path, scope, { setup }) {
|
|
28
|
+
const allowed = setup ? ['method', 'path', 'body', 'headers', 'capture'] : ['method', 'path', 'body', 'headers', 'websocket'];
|
|
29
|
+
if (!keyed(found, value, path, allowed, setup ? 'a setup request' : 'a request')) return;
|
|
30
|
+
if (!methods.includes(value.method)) found.add([...path, 'method'], `must be one of ${methods.join(', ')}.`);
|
|
31
|
+
if (typeof value.path !== 'string' || !value.path.startsWith('/')) found.add([...path, 'path'], 'must be a route path starting with "/".');
|
|
32
|
+
else templates(found, [value.path], [...path, 'path'], scope);
|
|
33
|
+
if (value.body !== undefined) templates(found, stringsIn(value.body), [...path, 'body'], scope);
|
|
34
|
+
if (value.headers !== undefined) headers(found, value.headers, [...path, 'headers'], scope);
|
|
35
|
+
if (!setup && value.websocket !== undefined && typeof value.websocket !== 'boolean') found.add([...path, 'websocket'], 'must be true or false.');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// personas: returns Map persona -> Set of captured variables. Each persona's own templates see earlier captures only.
|
|
39
|
+
export function validatePersonas(found, personas, path) {
|
|
40
|
+
const captured = new Map();
|
|
41
|
+
if (personas === undefined) return captured;
|
|
42
|
+
if (!isObject(personas)) { found.add(path, 'must map a persona name to { "kind": "owner" | "anonymous" | "signin" }.'); return captured; }
|
|
43
|
+
for (const [persona, spec] of Object.entries(personas)) {
|
|
44
|
+
const here = [...path, persona], variables = new Set();
|
|
45
|
+
captured.set(persona, variables);
|
|
46
|
+
if (!personaName.test(persona)) found.add(here, 'is not a persona name (lowercase letters, digits and underscore, starting with a letter).');
|
|
47
|
+
if (!isObject(spec) || !['owner', 'anonymous', 'signin'].includes(spec.kind)) { found.add([...here, 'kind'], 'must be "owner", "anonymous" or "signin".'); continue; }
|
|
48
|
+
if (spec.kind !== 'signin') { keyed(found, spec, here, ['kind'], `a ${spec.kind} persona`); continue; }
|
|
49
|
+
if (!keyed(found, spec, here, ['kind', 'requests', 'headers'], 'a signin persona')) continue;
|
|
50
|
+
if (!Array.isArray(spec.requests) || spec.requests.length < 1 || spec.requests.length > 4) found.add([...here, 'requests'], 'must list 1 to 4 sign-in requests, run in order.');
|
|
51
|
+
else spec.requests.forEach((item, index) => {
|
|
52
|
+
request(found, item, [...here, 'requests', index], { own: new Set(variables) }, { setup: true });
|
|
53
|
+
if (isObject(item) && item.capture !== undefined) {
|
|
54
|
+
if (!isObject(item.capture) || !Object.keys(item.capture).length) found.add([...here, 'requests', index, 'capture'], 'must map a variable name to a JSON pointer into the response, for example { "token": "/token" }.');
|
|
55
|
+
else for (const [key, at] of Object.entries(item.capture)) {
|
|
56
|
+
if (!variable.test(key)) found.add([...here, 'requests', index, 'capture', key], 'is not a variable name.');
|
|
57
|
+
else if (typeof at !== 'string' || !pointer.test(at)) found.add([...here, 'requests', index, 'capture', key], 'must be a JSON pointer such as "/token".');
|
|
58
|
+
else variables.add(key);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
if (spec.headers !== undefined) headers(found, spec.headers, [...here, 'headers'], { own: variables });
|
|
63
|
+
}
|
|
64
|
+
return captured;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function growth(found, value, path) {
|
|
68
|
+
if (!keyed(found, value, path, ['allowed', 'why'], 'growth')) return;
|
|
69
|
+
if (value.allowed !== true) found.add([...path, 'allowed'], 'must be true; delete growth to have the flow checked for growth.');
|
|
70
|
+
if (typeof value.why !== 'string' || !value.why.trim()) found.add([...path, 'why'], 'growth.allowed needs a why: say in one sentence why this flow may read more at the large size; it is printed on every run.');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function status(found, value, path) {
|
|
74
|
+
const codes = Array.isArray(value) ? value : [value];
|
|
75
|
+
if (!codes.length || !codes.every(code => isInt(code, 100, 599))) found.add(path, 'must be an HTTP status from 100 to 599, or a non-empty list of them.');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// flows: returns the normalized list. `facts.crons` are the crons the checkout declares; `budget.windowRows` may be unset.
|
|
79
|
+
export function validateFlows(found, flows, path, { personas, crons, windowRows }) {
|
|
80
|
+
if (!Array.isArray(flows) || flows.length < 1 || flows.length > 64) { found.add(path, 'must list 1 to 64 flows, each with a ceiling.'); return []; }
|
|
81
|
+
const names = new Set(), normalized = [];
|
|
82
|
+
flows.forEach((flow, index) => {
|
|
83
|
+
const label = typeof flow?.name === 'string' && slug.test(flow.name) ? flow.name : index, here = [...path, label];
|
|
84
|
+
const scheduled = isObject(flow) && flow.scheduled !== undefined;
|
|
85
|
+
if (!keyed(found, flow, here, ['name', 'ceiling', 'callsInRow', 'repeat', 'status', 'growth', ...(scheduled ? ['scheduled'] : ['persona', 'request'])], 'a flow')) return;
|
|
86
|
+
if (typeof flow.name !== 'string' || !slug.test(flow.name)) found.add([...here, 'name'], 'must be a short name of lowercase letters, digits and hyphens.');
|
|
87
|
+
else if (names.has(flow.name)) found.add([...here, 'name'], `${flow.name} is used by two flows; give each flow its own name.`);
|
|
88
|
+
else names.add(flow.name);
|
|
89
|
+
if (scheduled) {
|
|
90
|
+
if (typeof flow.scheduled !== 'string' || !flow.scheduled.trim()) found.add([...here, 'scheduled'], 'must be one of the cron expressions the build declares.');
|
|
91
|
+
else if (!crons.includes(flow.scheduled)) found.add([...here, 'scheduled'], `cron "${flow.scheduled}" is not one the checkout declares (${crons.length ? crons.map(cron => `"${cron}"`).join(', ') : 'it declares none'}); name a declared cron, or declare it in triggers.crons.`);
|
|
92
|
+
} else {
|
|
93
|
+
if (typeof flow.persona !== 'string' || !personas.has(flow.persona)) found.add([...here, 'persona'], `${typeof flow.persona === 'string' ? `persona ${flow.persona} is not declared` : 'is required for a request flow'}; declare it under personas${personas.size ? ` (declared: ${[...personas.keys()].join(', ')})` : ''}, or correct the name.`);
|
|
94
|
+
if (flow.request === undefined) found.add([...here, 'request'], 'is required; give method and path, or use "scheduled" for a cron flow.');
|
|
95
|
+
else request(found, flow.request, [...here, 'request'], { personas }, { setup: false });
|
|
96
|
+
}
|
|
97
|
+
const ceiling = integer(found, flow.ceiling, [...here, 'ceiling'], 1, undefined, 'it is the rows this flow may read at the large size, per call');
|
|
98
|
+
const calls = flow.callsInRow === undefined ? true : integer(found, flow.callsInRow, [...here, 'callsInRow'], 1);
|
|
99
|
+
if (ceiling && calls && windowRows) {
|
|
100
|
+
const inRow = flow.callsInRow ?? 1, total = flow.ceiling * inRow;
|
|
101
|
+
if (total > windowRows) found.add([...here, 'ceiling'], `ceiling ${count(flow.ceiling)} x callsInRow ${count(inRow)} = ${count(total)} rows, over budget.windowRows ${count(windowRows)}; lower the ceiling or the calls in a row.`);
|
|
102
|
+
}
|
|
103
|
+
if (flow.repeat !== undefined && ![1, 2, 3].includes(flow.repeat)) found.add([...here, 'repeat'], 'must be 1, 2 or 3.');
|
|
104
|
+
if (flow.status !== undefined) status(found, flow.status, [...here, 'status']);
|
|
105
|
+
if (flow.growth !== undefined) growth(found, flow.growth, [...here, 'growth']);
|
|
106
|
+
normalized.push({ ...flow, callsInRow: flow.callsInRow ?? 1, repeat: flow.repeat ?? 1, status: flow.status === undefined ? null : [].concat(flow.status), growth: flow.growth ?? null });
|
|
107
|
+
});
|
|
108
|
+
return normalized;
|
|
109
|
+
}
|