@iterant/site-runtime 3.5.0 → 3.6.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/docs/runtime-contract.md +1 -1
- package/package.json +1 -1
- package/src/lib/database.ts +119 -11
package/docs/runtime-contract.md
CHANGED
|
@@ -50,7 +50,7 @@ runtime and says so.
|
|
|
50
50
|
|
|
51
51
|
<!-- generated: available libraries -->
|
|
52
52
|
|
|
53
|
-
_Generated from package.json by scripts/generate-kit-table.mjs. Runtime 3.
|
|
53
|
+
_Generated from package.json by scripts/generate-kit-table.mjs. Runtime 3.6.0._
|
|
54
54
|
|
|
55
55
|
**Toolchain** (this package owns the version; do NOT declare these):
|
|
56
56
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iterant/site-runtime",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "The platform layer every Iterant brand site runs on: content grammar, collection schemas, SEO head and JSON-LD, layout core, Astro config preset, dev integrations and the verify gates.",
|
|
6
6
|
"scripts": {
|
package/src/lib/database.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
// Reading the brand's Database from a page
|
|
2
|
-
// DBX-D11).
|
|
1
|
+
// Reading the brand's Database from a page (epic DBX, decision DBX-D11).
|
|
3
2
|
//
|
|
4
3
|
// A published site holds no database binding and composes no SQL. It asks the
|
|
5
4
|
// platform's public read route for rows of ONE table, and the platform decides
|
|
@@ -12,10 +11,21 @@
|
|
|
12
11
|
// platform generates on every publish and puts in front of Astro's entry. It
|
|
13
12
|
// assigns them to `globalThis.__ITERANT_SITE_DB__` at module scope. In `astro
|
|
14
13
|
// dev` there is no gate, so the pair arrives as environment variables instead.
|
|
14
|
+
// A prerendered route reads while the site BUILDS, before that gate is even
|
|
15
|
+
// generated, so the publish pipeline puts the pair in `.dev.vars` beside the
|
|
16
|
+
// wrangler config and it reaches the render as environment variables too.
|
|
15
17
|
//
|
|
16
18
|
// Every failure answers with no rows and a console warning, never a throw: a
|
|
17
|
-
// listing that renders empty is a page the customer can still see, and a
|
|
18
|
-
// that fails on a database blip is not.
|
|
19
|
+
// listing that renders empty is a page the customer can still see, and a
|
|
20
|
+
// request that fails on a database blip is not.
|
|
21
|
+
//
|
|
22
|
+
// That contract inverts at BAKE time. A prerendered route reads once, during
|
|
23
|
+
// the publish build, and whatever it read is served until the next publish, so
|
|
24
|
+
// an empty answer there is not a transient blank list but a blog that stays
|
|
25
|
+
// empty for days. The publish build therefore sets `ITERANT_SITE_DB_STRICT=1`
|
|
26
|
+
// and every failure below throws instead, killing the build at its cause. The
|
|
27
|
+
// deployed runtime and the dev server never set it, so their behavior is
|
|
28
|
+
// unchanged.
|
|
19
29
|
|
|
20
30
|
const GLOBAL_KEY = "__ITERANT_SITE_DB__";
|
|
21
31
|
|
|
@@ -23,9 +33,18 @@ const URL_ENV = "ITERANT_SITE_DB_URL";
|
|
|
23
33
|
|
|
24
34
|
const TOKEN_ENV = "ITERANT_SITE_DB_TOKEN";
|
|
25
35
|
|
|
36
|
+
const STRICT_ENV = "ITERANT_SITE_DB_STRICT";
|
|
37
|
+
|
|
26
38
|
/** How long one read may take before the page renders without it. */
|
|
27
39
|
const TIMEOUT_MS = 10_000;
|
|
28
40
|
|
|
41
|
+
/** The platform's own ceiling on `limit`, and so the pager's page size. */
|
|
42
|
+
const MAX_LIMIT = 200;
|
|
43
|
+
|
|
44
|
+
/** The platform refuses an `offset` past this, so `MAX_LIMIT` rows after it is
|
|
45
|
+
* the last row {@link readDatabaseAll} can reach. */
|
|
46
|
+
const MAX_OFFSET = 100_000;
|
|
47
|
+
|
|
29
48
|
export type DatabaseFilterOp =
|
|
30
49
|
| "eq"
|
|
31
50
|
| "ne"
|
|
@@ -98,15 +117,29 @@ type SiteDatabaseHandoff = {
|
|
|
98
117
|
|
|
99
118
|
const EMPTY: DatabaseResult = { rows: [], total: 0 };
|
|
100
119
|
|
|
101
|
-
function
|
|
102
|
-
|
|
120
|
+
function environment(): Record<string, string | undefined> | undefined {
|
|
121
|
+
return (
|
|
103
122
|
globalThis as { process?: { env?: Record<string, string | undefined> } }
|
|
104
123
|
).process?.env;
|
|
105
|
-
|
|
106
|
-
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function fromEnvironment(): SiteDatabaseHandoff | null {
|
|
127
|
+
const url = environment()?.[URL_ENV];
|
|
128
|
+
const token = environment()?.[TOKEN_ENV];
|
|
107
129
|
return url && token ? { url, token } : null;
|
|
108
130
|
}
|
|
109
131
|
|
|
132
|
+
/**
|
|
133
|
+
* Whether a failed read must kill the process instead of rendering empty.
|
|
134
|
+
*
|
|
135
|
+
* Read from the environment only, never from the global handoff: the handoff is
|
|
136
|
+
* what a DEPLOYED site carries, and a deployed site is exactly where the
|
|
137
|
+
* never-throw contract still holds.
|
|
138
|
+
*/
|
|
139
|
+
function strict(): boolean {
|
|
140
|
+
return environment()?.[STRICT_ENV] === "1";
|
|
141
|
+
}
|
|
142
|
+
|
|
110
143
|
function handoff(): SiteDatabaseHandoff | null {
|
|
111
144
|
const baked = (globalThis as Record<string, unknown>)[GLOBAL_KEY];
|
|
112
145
|
if (baked && typeof baked === "object") {
|
|
@@ -118,8 +151,25 @@ function handoff(): SiteDatabaseHandoff | null {
|
|
|
118
151
|
return fromEnvironment();
|
|
119
152
|
}
|
|
120
153
|
|
|
154
|
+
function detailOf(detail: unknown): string {
|
|
155
|
+
if (detail instanceof Error) return detail.message;
|
|
156
|
+
if (typeof detail === "string") return detail;
|
|
157
|
+
return JSON.stringify(detail) ?? String(detail);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Say a read did not work, the way this environment wants it said. */
|
|
161
|
+
function report(message: string, detail?: unknown): void {
|
|
162
|
+
const line = `[site-runtime] ${message}`;
|
|
163
|
+
if (strict()) {
|
|
164
|
+
throw new Error(
|
|
165
|
+
detail === undefined ? line : `${line} ${detailOf(detail)}`,
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
console.warn(line, detail ?? "");
|
|
169
|
+
}
|
|
170
|
+
|
|
121
171
|
function warn(message: string, detail?: unknown): DatabaseResult {
|
|
122
|
-
|
|
172
|
+
report(`readDatabase: ${message}`, detail);
|
|
123
173
|
return EMPTY;
|
|
124
174
|
}
|
|
125
175
|
|
|
@@ -133,8 +183,9 @@ function warn(message: string, detail?: unknown): DatabaseResult {
|
|
|
133
183
|
* });
|
|
134
184
|
* ```
|
|
135
185
|
*
|
|
136
|
-
* Call it in a page's frontmatter (server side).
|
|
137
|
-
*
|
|
186
|
+
* Call it in a page's frontmatter (server side). On a prerendered route the read
|
|
187
|
+
* happens once, at publish time, and the site shows those rows until the next
|
|
188
|
+
* publish; on an SSR route it happens per request.
|
|
138
189
|
*/
|
|
139
190
|
export async function readDatabase(
|
|
140
191
|
table: string,
|
|
@@ -182,6 +233,63 @@ export async function readDatabase(
|
|
|
182
233
|
};
|
|
183
234
|
}
|
|
184
235
|
|
|
236
|
+
/**
|
|
237
|
+
* Every row a query matches, not just the first page of it.
|
|
238
|
+
*
|
|
239
|
+
* ```ts
|
|
240
|
+
* export async function getStaticPaths() {
|
|
241
|
+
* const { rows } = await readDatabaseAll("articles", { select: ["slug"] });
|
|
242
|
+
* return rows.map((row) => ({ params: { slug: String(row.slug) } }));
|
|
243
|
+
* }
|
|
244
|
+
* ```
|
|
245
|
+
*
|
|
246
|
+
* This is the enumeration a `getStaticPaths` needs, and the reason it exists is
|
|
247
|
+
* that a plain {@link readDatabase} answers at most {@link MAX_LIMIT} rows and
|
|
248
|
+
* says so only in `total`. A blog of 300 articles would bake its first 200 and
|
|
249
|
+
* look complete. Paging here is not an optimization; it is the difference
|
|
250
|
+
* between a whole blog and a silently truncated one.
|
|
251
|
+
*
|
|
252
|
+
* Any `limit` or `offset` on the query is ignored; `filter`, `sort` and `select`
|
|
253
|
+
* are carried on every page.
|
|
254
|
+
*/
|
|
255
|
+
export async function readDatabaseAll(
|
|
256
|
+
table: string,
|
|
257
|
+
query: DatabaseQuery = {},
|
|
258
|
+
): Promise<DatabaseResult> {
|
|
259
|
+
const { limit: _limit, offset: _offset, ...shared } = query;
|
|
260
|
+
const rows: DatabaseRow[] = [];
|
|
261
|
+
let total = 0;
|
|
262
|
+
for (let offset = 0; ; offset += MAX_LIMIT) {
|
|
263
|
+
const page = await readDatabase(table, {
|
|
264
|
+
...shared,
|
|
265
|
+
limit: MAX_LIMIT,
|
|
266
|
+
offset,
|
|
267
|
+
});
|
|
268
|
+
// No rows means the end of the table, or a page that failed and rendered
|
|
269
|
+
// empty under the never-throw contract. Its `total` is not the table's, and
|
|
270
|
+
// adopting it would answer with rows already read alongside a total of
|
|
271
|
+
// none, which is a shape no caller can act on.
|
|
272
|
+
if (page.rows.length === 0) break;
|
|
273
|
+
rows.push(...page.rows);
|
|
274
|
+
// The first page's count is the snapshot the rest of the walk is measured
|
|
275
|
+
// against. A total that is not a real number (a route answering `"many"`)
|
|
276
|
+
// would compare false forever and walk to the offset ceiling, so an
|
|
277
|
+
// unusable one falls back to what actually arrived.
|
|
278
|
+
if (offset === 0) {
|
|
279
|
+
total = Number.isFinite(page.total) ? page.total : rows.length;
|
|
280
|
+
}
|
|
281
|
+
if (rows.length >= total) break;
|
|
282
|
+
if (offset + MAX_LIMIT > MAX_OFFSET) {
|
|
283
|
+
report(
|
|
284
|
+
`readDatabaseAll: "${table}" holds ${total} rows, past the ${MAX_OFFSET + MAX_LIMIT} ` +
|
|
285
|
+
`that offset paging reaches, so ${rows.length} of them were read.`,
|
|
286
|
+
);
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return { rows, total };
|
|
291
|
+
}
|
|
292
|
+
|
|
185
293
|
/**
|
|
186
294
|
* Attach {@link DbProvenance} to a row that carries a system `_id`.
|
|
187
295
|
*
|