@dokebi/template-inertia 0.4.19 → 0.4.21
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/AI.md +59 -1
- package/README.md +4 -0
- package/dist/errors.d.ts +2 -2
- package/dist/errors.js +5 -1
- package/dist/index.d.ts +19 -0
- package/dist/index.js +107 -11
- package/dist/options.d.ts +9 -0
- package/dist/options.js +53 -0
- package/dist/pages.d.ts +33 -0
- package/dist/pages.js +82 -0
- package/dist/responder.d.ts +6 -0
- package/dist/responder.js +18 -8
- package/dist/template-build.d.ts +43 -0
- package/dist/template-build.js +365 -0
- package/dist/template-bundle.d.ts +102 -0
- package/dist/template-bundle.js +344 -0
- package/dist/template-runtime.d.ts +145 -0
- package/dist/template-runtime.js +723 -0
- package/dist/template-shared.d.ts +99 -0
- package/dist/template-shared.js +231 -0
- package/dist/template.d.ts +3 -0
- package/dist/template.js +5 -0
- package/dist/vite-plugin.d.ts +20 -2
- package/dist/vite-plugin.js +27 -0
- package/dist/vue-typescript.d.ts +9 -0
- package/dist/vue-typescript.js +68 -0
- package/options.schema.json +79 -1
- package/package.json +6 -2
- package/templates/shared.ts +4 -0
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
// D-0291: the template bundle — a `views/<template>/` folder built apart from the app (Layout.vue,
|
|
2
|
+
// Pages/**, its components, styles), loaded by the app while it runs. This module is the format
|
|
3
|
+
// (manifest `dokebi-template.json`, format 1) and its check; it reads files only, so the build
|
|
4
|
+
// (`dokebi template build`), `dokebi template check` and the app's loader (stage 2) share it.
|
|
5
|
+
//
|
|
6
|
+
// <bundle>/dokebi-template.json the manifest below
|
|
7
|
+
// <bundle>/dokebi-template.sig the signature, in a form the app chooses (read, never judged here)
|
|
8
|
+
// <bundle>/browser/… one ES module entry per page, its chunks and CSS (hashed names)
|
|
9
|
+
// <bundle>/ssr/… the same pages for server rendering
|
|
10
|
+
//
|
|
11
|
+
// The shared modules (vue, @inertiajs/vue3, the app's SDK, …) are not in the bundle: its code
|
|
12
|
+
// imports them by name, and the app's own build provides them (template-shared.ts), so a page of
|
|
13
|
+
// the bundle and the app share one instance of each.
|
|
14
|
+
import { createHash } from 'node:crypto';
|
|
15
|
+
import { lstat, readdir, readFile } from 'node:fs/promises';
|
|
16
|
+
import { join, relative, sep } from 'node:path';
|
|
17
|
+
/** The manifest file of a bundle. */
|
|
18
|
+
export const TEMPLATE_MANIFEST = 'dokebi-template.json';
|
|
19
|
+
/** The signature file of a bundle (optional; its form is the app's). */
|
|
20
|
+
export const TEMPLATE_SIGNATURE = 'dokebi-template.sig';
|
|
21
|
+
/** The manifest format this version writes and reads. */
|
|
22
|
+
export const TEMPLATE_FORMAT = 1;
|
|
23
|
+
/** A template name: the folder name of `views/<template>/` and `domains.<host>.template`. */
|
|
24
|
+
export const TEMPLATE_NAME = /^[a-z][a-z0-9-]{0,31}$/;
|
|
25
|
+
/** A template version (the template repository's): 1 to 64 of letters, digits, `.`, `+`, `-`. */
|
|
26
|
+
export const TEMPLATE_VERSION = /^[0-9A-Za-z][0-9A-Za-z.+-]{0,63}$/;
|
|
27
|
+
/** A page of a bundle: `Layout`, or `Pages/<path>` without the `.vue`. */
|
|
28
|
+
export const TEMPLATE_PAGE = /^(?:Layout|Pages(?:\/[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}){1,16})$/;
|
|
29
|
+
/** A file of a bundle: under browser/ or ssr/, plain path segments only. */
|
|
30
|
+
const FILE = /^(?:browser|ssr)(?:\/[A-Za-z0-9_@+][A-Za-z0-9_.@+-]{0,199}){1,32}$/;
|
|
31
|
+
/** A shared module specifier: a package name with an optional subpath. */
|
|
32
|
+
export const SHARED_SPECIFIER = /^(?:@[a-z0-9][a-z0-9._-]{0,213}\/)?[a-z0-9][a-z0-9._-]{0,213}(?:\/[A-Za-z0-9_.@+-]{1,200}){0,16}$/;
|
|
33
|
+
/** A page's `meta` (JSON) may take this many bytes. */
|
|
34
|
+
export const PAGE_META_MAX_BYTES = 4096;
|
|
35
|
+
/** Default limits of one bundle. */
|
|
36
|
+
export const TEMPLATE_MAX_BYTES = 50 * 1024 * 1024;
|
|
37
|
+
export const TEMPLATE_MAX_FILES = 4096;
|
|
38
|
+
/**
|
|
39
|
+
* The shared modules every bundle may import and every app build provides (when it opts in with
|
|
40
|
+
* `dokebiInertia({ templates })`). `vue/server-renderer` is provided by the server build only.
|
|
41
|
+
*/
|
|
42
|
+
export const SHARED_DEFAULTS = Object.freeze([
|
|
43
|
+
'vue',
|
|
44
|
+
'vue/server-renderer',
|
|
45
|
+
'@inertiajs/vue3',
|
|
46
|
+
'@dokebi/template-inertia/i18n',
|
|
47
|
+
'@dokebi/template-inertia/pages',
|
|
48
|
+
]);
|
|
49
|
+
/** The defaults the server build alone provides. */
|
|
50
|
+
export const SERVER_ONLY_SHARED = Object.freeze(['vue/server-renderer']);
|
|
51
|
+
function plain(value) {
|
|
52
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
53
|
+
}
|
|
54
|
+
/** The JSON `meta` value of a page is plain data (objects, arrays, strings, finite numbers…). */
|
|
55
|
+
export function jsonValue(value, depth = 0) {
|
|
56
|
+
if (depth > 16)
|
|
57
|
+
return false;
|
|
58
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean')
|
|
59
|
+
return true;
|
|
60
|
+
if (typeof value === 'number')
|
|
61
|
+
return Number.isFinite(value);
|
|
62
|
+
if (Array.isArray(value))
|
|
63
|
+
return value.every((item) => jsonValue(item, depth + 1));
|
|
64
|
+
if (!plain(value) || Object.getPrototypeOf(value) !== Object.prototype)
|
|
65
|
+
return false;
|
|
66
|
+
return Object.values(value).every((item) => jsonValue(item, depth + 1));
|
|
67
|
+
}
|
|
68
|
+
/** The problem of a manifest's shape, or undefined (paths, names, page references). */
|
|
69
|
+
export function manifestProblem(value) {
|
|
70
|
+
if (!plain(value))
|
|
71
|
+
return 'the manifest is not a JSON object';
|
|
72
|
+
if (value.format !== TEMPLATE_FORMAT)
|
|
73
|
+
return `format must be ${TEMPLATE_FORMAT}`;
|
|
74
|
+
if (typeof value.name !== 'string' || !TEMPLATE_NAME.test(value.name))
|
|
75
|
+
return 'name must be a template name (^[a-z][a-z0-9-]{0,31}$)';
|
|
76
|
+
if (typeof value.version !== 'string' || !TEMPLATE_VERSION.test(value.version))
|
|
77
|
+
return 'version must be 1 to 64 letters, digits, ".", "+" or "-"';
|
|
78
|
+
if (typeof value.builtWith !== 'string' || !TEMPLATE_VERSION.test(value.builtWith))
|
|
79
|
+
return 'builtWith must be the template-inertia version';
|
|
80
|
+
if (!plain(value.shared))
|
|
81
|
+
return 'shared must be an object of module → version';
|
|
82
|
+
for (const [name, version] of Object.entries(value.shared))
|
|
83
|
+
if (!SHARED_SPECIFIER.test(name) || (version !== null && typeof version !== 'string'))
|
|
84
|
+
return `shared ${name} must be a module name with a version string or null`;
|
|
85
|
+
if (!plain(value.files))
|
|
86
|
+
return 'files must be an object of path → { bytes, sha256 }';
|
|
87
|
+
for (const [file, entry] of Object.entries(value.files)) {
|
|
88
|
+
if (!FILE.test(file) || file.split('/').some((part) => part === '..' || part === '.'))
|
|
89
|
+
return `file ${file} is not a path under browser/ or ssr/`;
|
|
90
|
+
if (!plain(entry) ||
|
|
91
|
+
!Number.isSafeInteger(entry.bytes) ||
|
|
92
|
+
entry.bytes < 0 ||
|
|
93
|
+
typeof entry.sha256 !== 'string' ||
|
|
94
|
+
!/^[0-9a-f]{64}$/.test(entry.sha256))
|
|
95
|
+
return `file ${file} needs bytes (a whole number) and sha256 (64 hex)`;
|
|
96
|
+
}
|
|
97
|
+
if (!plain(value.pages) || Object.keys(value.pages).length === 0)
|
|
98
|
+
return 'pages must name at least one page';
|
|
99
|
+
const files = value.files;
|
|
100
|
+
for (const [page, entry] of Object.entries(value.pages)) {
|
|
101
|
+
if (!TEMPLATE_PAGE.test(page))
|
|
102
|
+
return `page ${page} is not Layout or Pages/<path>`;
|
|
103
|
+
if (!plain(entry))
|
|
104
|
+
return `page ${page} is not an object`;
|
|
105
|
+
for (const key of ['browser', 'ssr'])
|
|
106
|
+
if (typeof entry[key] !== 'string' || !Object.hasOwn(files, entry[key]))
|
|
107
|
+
return `page ${page}: ${key} must be a file of files`;
|
|
108
|
+
for (const key of ['css', 'preload']) {
|
|
109
|
+
const list = entry[key];
|
|
110
|
+
if (!Array.isArray(list) ||
|
|
111
|
+
list.some((file) => typeof file !== 'string' || !Object.hasOwn(files, file)))
|
|
112
|
+
return `page ${page}: ${key} must be a list of files of files`;
|
|
113
|
+
}
|
|
114
|
+
if (entry.meta !== undefined) {
|
|
115
|
+
if (!jsonValue(entry.meta))
|
|
116
|
+
return `page ${page}: meta must be JSON data`;
|
|
117
|
+
if (Buffer.byteLength(JSON.stringify(entry.meta)) > PAGE_META_MAX_BYTES)
|
|
118
|
+
return `page ${page}: meta is larger than ${PAGE_META_MAX_BYTES} bytes`;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
/** Every file under `dir` (relative, `/`-separated), links and other non-files reported. */
|
|
124
|
+
async function walk(dir, root, found, links, max) {
|
|
125
|
+
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
126
|
+
if (found.length + links.length > max)
|
|
127
|
+
return;
|
|
128
|
+
const path = join(dir, entry.name);
|
|
129
|
+
const name = relative(root, path).split(sep).join('/');
|
|
130
|
+
if (entry.isSymbolicLink() || (!entry.isFile() && !entry.isDirectory()))
|
|
131
|
+
links.push(name);
|
|
132
|
+
else if (entry.isDirectory())
|
|
133
|
+
await walk(path, root, found, links, max);
|
|
134
|
+
else
|
|
135
|
+
found.push(name);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/** sha256 (hex) of bytes. */
|
|
139
|
+
export function sha256(bytes) {
|
|
140
|
+
return createHash('sha256').update(bytes).digest('hex');
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Checks one bundle folder: the manifest's shape, every file's size and sha256, no file the
|
|
144
|
+
* manifest does not list (but the manifest and the signature), no link, the limits. Reads only;
|
|
145
|
+
* never throws for what the folder holds (a missing folder is TEMPLATE_MANIFEST_MISSING).
|
|
146
|
+
*/
|
|
147
|
+
export async function checkTemplateBundle(dir, limits = {}) {
|
|
148
|
+
const maxBytes = limits.maxBytes ?? TEMPLATE_MAX_BYTES;
|
|
149
|
+
const maxFiles = limits.maxFiles ?? TEMPLATE_MAX_FILES;
|
|
150
|
+
const problems = [];
|
|
151
|
+
let text;
|
|
152
|
+
try {
|
|
153
|
+
const info = await lstat(join(dir, TEMPLATE_MANIFEST));
|
|
154
|
+
if (!info.isFile())
|
|
155
|
+
return {
|
|
156
|
+
bytes: 0,
|
|
157
|
+
problems: [
|
|
158
|
+
{
|
|
159
|
+
code: 'TEMPLATE_FILE_LINK',
|
|
160
|
+
file: TEMPLATE_MANIFEST,
|
|
161
|
+
message: `${TEMPLATE_MANIFEST} is not a plain file`,
|
|
162
|
+
},
|
|
163
|
+
],
|
|
164
|
+
};
|
|
165
|
+
if (info.size > 4 * 1024 * 1024)
|
|
166
|
+
return {
|
|
167
|
+
bytes: 0,
|
|
168
|
+
problems: [
|
|
169
|
+
{
|
|
170
|
+
code: 'TEMPLATE_TOO_LARGE',
|
|
171
|
+
file: TEMPLATE_MANIFEST,
|
|
172
|
+
message: `${TEMPLATE_MANIFEST} is larger than 4 MiB`,
|
|
173
|
+
},
|
|
174
|
+
],
|
|
175
|
+
};
|
|
176
|
+
text = await readFile(join(dir, TEMPLATE_MANIFEST));
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
return {
|
|
180
|
+
bytes: 0,
|
|
181
|
+
problems: [
|
|
182
|
+
{
|
|
183
|
+
code: 'TEMPLATE_MANIFEST_MISSING',
|
|
184
|
+
file: TEMPLATE_MANIFEST,
|
|
185
|
+
message: `${TEMPLATE_MANIFEST} could not be read: ${error.message}`,
|
|
186
|
+
},
|
|
187
|
+
],
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
let parsed;
|
|
191
|
+
try {
|
|
192
|
+
parsed = JSON.parse(text.toString('utf8'));
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
return {
|
|
196
|
+
bytes: 0,
|
|
197
|
+
problems: [
|
|
198
|
+
{
|
|
199
|
+
code: 'TEMPLATE_MANIFEST_INVALID',
|
|
200
|
+
file: TEMPLATE_MANIFEST,
|
|
201
|
+
message: `${TEMPLATE_MANIFEST} is not JSON: ${error.message}`,
|
|
202
|
+
},
|
|
203
|
+
],
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
if (plain(parsed) && parsed.format !== TEMPLATE_FORMAT)
|
|
207
|
+
return {
|
|
208
|
+
bytes: 0,
|
|
209
|
+
problems: [
|
|
210
|
+
{
|
|
211
|
+
code: 'TEMPLATE_FORMAT_UNSUPPORTED',
|
|
212
|
+
file: TEMPLATE_MANIFEST,
|
|
213
|
+
message: `format ${String(parsed.format)} is not ${TEMPLATE_FORMAT}: build the bundle with this template-inertia, or upgrade the app`,
|
|
214
|
+
},
|
|
215
|
+
],
|
|
216
|
+
};
|
|
217
|
+
const shape = manifestProblem(parsed);
|
|
218
|
+
if (shape !== undefined)
|
|
219
|
+
return {
|
|
220
|
+
bytes: 0,
|
|
221
|
+
problems: [{ code: 'TEMPLATE_MANIFEST_INVALID', file: TEMPLATE_MANIFEST, message: shape }],
|
|
222
|
+
};
|
|
223
|
+
const manifest = parsed;
|
|
224
|
+
const buildId = sha256(text).slice(0, 12);
|
|
225
|
+
const listed = new Map(Object.entries(manifest.files));
|
|
226
|
+
if (listed.size > maxFiles)
|
|
227
|
+
problems.push({
|
|
228
|
+
code: 'TEMPLATE_TOO_LARGE',
|
|
229
|
+
message: `the bundle lists ${listed.size} files, more than ${maxFiles}`,
|
|
230
|
+
});
|
|
231
|
+
const declared = [...listed.values()].reduce((sum, entry) => sum + entry.bytes, 0);
|
|
232
|
+
if (declared > maxBytes)
|
|
233
|
+
problems.push({
|
|
234
|
+
code: 'TEMPLATE_TOO_LARGE',
|
|
235
|
+
message: `the bundle's files take ${declared} bytes, more than ${maxBytes}`,
|
|
236
|
+
});
|
|
237
|
+
if (problems.length)
|
|
238
|
+
return { manifest, buildId, bytes: 0, problems };
|
|
239
|
+
const found = [];
|
|
240
|
+
const links = [];
|
|
241
|
+
for (const top of ['browser', 'ssr']) {
|
|
242
|
+
try {
|
|
243
|
+
const info = await lstat(join(dir, top));
|
|
244
|
+
if (!info.isDirectory()) {
|
|
245
|
+
links.push(top);
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
// A bundle without that folder: its listed files are reported missing below.
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
await walk(join(dir, top), dir, found, links, maxFiles);
|
|
254
|
+
}
|
|
255
|
+
for (const entry of await readdir(dir, { withFileTypes: true }))
|
|
256
|
+
if (entry.name !== TEMPLATE_MANIFEST &&
|
|
257
|
+
entry.name !== TEMPLATE_SIGNATURE &&
|
|
258
|
+
entry.name !== 'browser' &&
|
|
259
|
+
entry.name !== 'ssr')
|
|
260
|
+
problems.push({
|
|
261
|
+
code: 'TEMPLATE_FILE_EXTRA',
|
|
262
|
+
file: entry.name,
|
|
263
|
+
message: `${entry.name} is not part of a bundle (only ${TEMPLATE_MANIFEST}, ${TEMPLATE_SIGNATURE}, browser/ and ssr/)`,
|
|
264
|
+
});
|
|
265
|
+
for (const link of links)
|
|
266
|
+
problems.push({
|
|
267
|
+
code: 'TEMPLATE_FILE_LINK',
|
|
268
|
+
file: link,
|
|
269
|
+
message: `${link} is a link or a special file; a bundle holds plain files only`,
|
|
270
|
+
});
|
|
271
|
+
let bytes = 0;
|
|
272
|
+
for (const file of found) {
|
|
273
|
+
const entry = listed.get(file);
|
|
274
|
+
if (entry === undefined) {
|
|
275
|
+
problems.push({
|
|
276
|
+
code: 'TEMPLATE_FILE_EXTRA',
|
|
277
|
+
file,
|
|
278
|
+
message: `${file} is not in the manifest's files`,
|
|
279
|
+
});
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
const content = await readFile(join(dir, file));
|
|
283
|
+
bytes += content.length;
|
|
284
|
+
if (bytes > maxBytes) {
|
|
285
|
+
problems.push({
|
|
286
|
+
code: 'TEMPLATE_TOO_LARGE',
|
|
287
|
+
message: `the bundle's files take more than ${maxBytes} bytes`,
|
|
288
|
+
});
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
if (content.length !== entry.bytes || sha256(content) !== entry.sha256)
|
|
292
|
+
problems.push({
|
|
293
|
+
code: 'TEMPLATE_FILE_CHANGED',
|
|
294
|
+
file,
|
|
295
|
+
message: `${file} is not the file the manifest names (size or sha256 differs)`,
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
const present = new Set(found);
|
|
299
|
+
for (const file of listed.keys())
|
|
300
|
+
if (!present.has(file))
|
|
301
|
+
problems.push({
|
|
302
|
+
code: 'TEMPLATE_FILE_MISSING',
|
|
303
|
+
file,
|
|
304
|
+
message: `${file} is in the manifest but not in the folder`,
|
|
305
|
+
});
|
|
306
|
+
return { manifest, buildId, bytes, problems };
|
|
307
|
+
}
|
|
308
|
+
/** The major part of a version for compatibility: `3` of 3.5.1, `0.4` of 0.4.19. */
|
|
309
|
+
export function compatibleLine(version) {
|
|
310
|
+
const match = /^v?(\d+)\.(\d+)(?:\.\d+)?/.exec(version);
|
|
311
|
+
if (!match)
|
|
312
|
+
return undefined;
|
|
313
|
+
return match[1] === '0' ? `0.${match[2]}` : match[1];
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* The problems of a bundle's shared modules against what an app build provides: a module the app
|
|
317
|
+
* does not provide, or one whose package is another major (0.x: minor) than the bundle was built
|
|
318
|
+
* with. A version unknown on either side (a local alias) is not compared.
|
|
319
|
+
*/
|
|
320
|
+
export function sharedProblems(bundle, provided) {
|
|
321
|
+
const problems = [];
|
|
322
|
+
for (const [module, built] of Object.entries(bundle)) {
|
|
323
|
+
const own = provided[module];
|
|
324
|
+
if (own === undefined) {
|
|
325
|
+
problems.push({
|
|
326
|
+
code: 'TEMPLATE_SHARED_MISSING',
|
|
327
|
+
module,
|
|
328
|
+
message: `the bundle imports ${module}, which the app build does not provide (add it to dokebiInertia({ templates: { shared } }) and build the app again)`,
|
|
329
|
+
});
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
if (built === null || own.version === null)
|
|
333
|
+
continue;
|
|
334
|
+
const want = compatibleLine(built);
|
|
335
|
+
const have = compatibleLine(own.version);
|
|
336
|
+
if (want !== undefined && have !== undefined && want !== have)
|
|
337
|
+
problems.push({
|
|
338
|
+
code: 'TEMPLATE_SHARED_VERSION',
|
|
339
|
+
module,
|
|
340
|
+
message: `the bundle was built with ${module} ${built}, the app provides ${own.version} (another major version)`,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
return problems;
|
|
344
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { Logger } from '@dokebi/core';
|
|
2
|
+
import type { FastifyReply, FastifyRequest } from 'fastify';
|
|
3
|
+
import { type TemplateManifest } from './template-bundle.js';
|
|
4
|
+
/** The URL prefix of bundle files (one route for every app, template and generation). */
|
|
5
|
+
export declare const TEMPLATE_FILES_PREFIX = "/_dokebi/templates/";
|
|
6
|
+
/** The registry `templatePages()` reads during server rendering (no imports there). */
|
|
7
|
+
export declare const TEMPLATE_REGISTRY: unique symbol;
|
|
8
|
+
/** `templates.<app>.<template>` of the options. */
|
|
9
|
+
export interface TemplateSource {
|
|
10
|
+
readonly path: string;
|
|
11
|
+
}
|
|
12
|
+
/** The template part of the options. */
|
|
13
|
+
export interface TemplateSettings {
|
|
14
|
+
readonly templates: Readonly<Record<string, Readonly<Record<string, TemplateSource>>>>;
|
|
15
|
+
readonly checkMs: number;
|
|
16
|
+
readonly maxBytes: number;
|
|
17
|
+
readonly keep: number;
|
|
18
|
+
readonly verifyTimeoutMs: number;
|
|
19
|
+
readonly cacheDir: string;
|
|
20
|
+
}
|
|
21
|
+
/** What the app's `verifyTemplate` receives. */
|
|
22
|
+
export interface TemplateVerifyInput {
|
|
23
|
+
readonly app: string;
|
|
24
|
+
readonly template: string;
|
|
25
|
+
/** The configured bundle folder (the mount). */
|
|
26
|
+
readonly path: string;
|
|
27
|
+
readonly buildId: string;
|
|
28
|
+
readonly manifest: TemplateManifest;
|
|
29
|
+
/** `dokebi-template.sig` as it was read with the manifest, or undefined. */
|
|
30
|
+
readonly signature: Buffer | undefined;
|
|
31
|
+
/** A file of the checked copy (`dokebi-template.json`, `browser/…`, `ssr/…`). */
|
|
32
|
+
read(file: string): Promise<Buffer>;
|
|
33
|
+
}
|
|
34
|
+
/** What it answers. */
|
|
35
|
+
export type TemplateVerifyResult = {
|
|
36
|
+
readonly accept: true;
|
|
37
|
+
readonly rejectPages?: readonly {
|
|
38
|
+
readonly page: string;
|
|
39
|
+
readonly reason: string;
|
|
40
|
+
}[];
|
|
41
|
+
} | {
|
|
42
|
+
readonly accept: false;
|
|
43
|
+
readonly reason: string;
|
|
44
|
+
};
|
|
45
|
+
export type VerifyTemplate = (bundle: TemplateVerifyInput) => TemplateVerifyResult | Promise<TemplateVerifyResult>;
|
|
46
|
+
/** The app build a template's bundle joins (from the app's Inertia scope). */
|
|
47
|
+
export interface TemplateAppBuild {
|
|
48
|
+
/** `vite.base` of the app (the URL prefix of its built files). */
|
|
49
|
+
readonly base: string;
|
|
50
|
+
/** `vite.manifest`: dokebi-shared.json is beside it. */
|
|
51
|
+
readonly manifest: string;
|
|
52
|
+
/** `ssr.bundle`, when the app renders on the server. */
|
|
53
|
+
readonly ssrBundle?: string;
|
|
54
|
+
}
|
|
55
|
+
interface PageUrls {
|
|
56
|
+
readonly js: string;
|
|
57
|
+
readonly css: readonly string[];
|
|
58
|
+
readonly preload: readonly string[];
|
|
59
|
+
}
|
|
60
|
+
/** One loaded bundle of one app's template. */
|
|
61
|
+
export declare class TemplateGeneration {
|
|
62
|
+
#private;
|
|
63
|
+
readonly app: string;
|
|
64
|
+
readonly template: string;
|
|
65
|
+
readonly buildId: string;
|
|
66
|
+
readonly manifest: TemplateManifest;
|
|
67
|
+
/** The checked copy in the cache. */
|
|
68
|
+
readonly dir: string;
|
|
69
|
+
/** Accepted pages → their URLs. */
|
|
70
|
+
readonly pages: ReadonlyMap<string, PageUrls>;
|
|
71
|
+
/** Accepted pages → their server modules (empty without server rendering). */
|
|
72
|
+
readonly server: ReadonlyMap<string, Readonly<Record<string, unknown>>>;
|
|
73
|
+
readonly importMap: Readonly<Record<string, string>>;
|
|
74
|
+
/** Pages the app's check refused. */
|
|
75
|
+
readonly rejected: readonly string[];
|
|
76
|
+
readonly setAside: Set<string>;
|
|
77
|
+
constructor(app: string, template: string, buildId: string, manifest: TemplateManifest,
|
|
78
|
+
/** The checked copy in the cache. */
|
|
79
|
+
dir: string,
|
|
80
|
+
/** Accepted pages → their URLs. */
|
|
81
|
+
pages: ReadonlyMap<string, PageUrls>,
|
|
82
|
+
/** Accepted pages → their server modules (empty without server rendering). */
|
|
83
|
+
server: ReadonlyMap<string, Readonly<Record<string, unknown>>>, importMap: Readonly<Record<string, string>>,
|
|
84
|
+
/** Pages the app's check refused. */
|
|
85
|
+
rejected: readonly string[]);
|
|
86
|
+
/** The page table of the first HTML (JSON, without the pages set aside). */
|
|
87
|
+
table(): string;
|
|
88
|
+
/** Leaves `page` to the app's own page for the rest of this generation. */
|
|
89
|
+
putAside(page: string): void;
|
|
90
|
+
/** The server module of `page`, unless it is set aside. */
|
|
91
|
+
serverPage(page: string): Readonly<Record<string, unknown>> | undefined;
|
|
92
|
+
}
|
|
93
|
+
/** The part of a template the responder uses for one request. */
|
|
94
|
+
export interface TemplateUse {
|
|
95
|
+
/** The asset version of this request's pages. */
|
|
96
|
+
readonly version: string;
|
|
97
|
+
readonly generation: TemplateGeneration;
|
|
98
|
+
/** The head markup: import map, page table, the first page's stylesheets and preloads. */
|
|
99
|
+
head(component: string, nonce: string): string;
|
|
100
|
+
/**
|
|
101
|
+
* Runs a server render with this generation; when it fails after taking bundle pages, those
|
|
102
|
+
* pages are set aside and it runs once more with the app's own.
|
|
103
|
+
*/
|
|
104
|
+
render<T>(run: () => Promise<T>, component: string): Promise<T>;
|
|
105
|
+
}
|
|
106
|
+
export interface TemplateBundlesOptions {
|
|
107
|
+
readonly logger: Logger;
|
|
108
|
+
readonly settings: TemplateSettings;
|
|
109
|
+
readonly verify: VerifyTemplate | undefined;
|
|
110
|
+
/** `logRepeatWindowMs`: a failure of the same cause is said once per window. */
|
|
111
|
+
readonly repeat: (key: string) => number | false | undefined;
|
|
112
|
+
readonly production?: boolean;
|
|
113
|
+
}
|
|
114
|
+
/** Every app's template bundles. */
|
|
115
|
+
export declare class TemplateBundles {
|
|
116
|
+
#private;
|
|
117
|
+
constructor(options: TemplateBundlesOptions);
|
|
118
|
+
/** The configured template names of an app. */
|
|
119
|
+
templatesOf(app: string): readonly string[];
|
|
120
|
+
/** An app's build, from its Inertia scope (the first scope of an app wins). */
|
|
121
|
+
registerApp(app: string, build: TemplateAppBuild): void;
|
|
122
|
+
/** The apps of `templates` that are not apps (`names`, undefined: no app routing). */
|
|
123
|
+
static unknownApps(settings: TemplateSettings, names: readonly string[] | undefined): string[];
|
|
124
|
+
/** At start: every configured bundle, and the timer. Never throws. */
|
|
125
|
+
start(): Promise<void>;
|
|
126
|
+
/** A reload: the new settings for the next look; the bundles are compared now. */
|
|
127
|
+
update(settings: TemplateSettings): void;
|
|
128
|
+
stop(): void;
|
|
129
|
+
/** The current generation of an app's template, or undefined. */
|
|
130
|
+
current(app: string, template: string | undefined): TemplateGeneration | undefined;
|
|
131
|
+
/** The use of one request: undefined when its template has no bundle. */
|
|
132
|
+
use(app: string, template: string | undefined, baseVersion: string): TemplateUse | undefined;
|
|
133
|
+
/** `GET /_dokebi/templates/<app>/<template>/<buildId>/browser/…` of a kept generation. */
|
|
134
|
+
serve(path: string, reply: FastifyReply): Promise<FastifyReply>;
|
|
135
|
+
/** For tests and describe: the loaded generations. */
|
|
136
|
+
generations(): ReadonlyArray<{
|
|
137
|
+
app: string;
|
|
138
|
+
template: string;
|
|
139
|
+
buildId: string;
|
|
140
|
+
kept: string[];
|
|
141
|
+
}>;
|
|
142
|
+
}
|
|
143
|
+
/** Reads a request's template through web (`templateOf`), for `use`. */
|
|
144
|
+
export type TemplateOf = (request: FastifyRequest) => string | undefined;
|
|
145
|
+
export {};
|