netjes 0.0.3 → 0.0.5
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 +10 -8
- package/cli/commands.mjs +32 -6
- package/cli/info.mjs +268 -31
- package/cli/routes.mjs +35 -8
- package/dist/actions.d.ts +3 -2
- package/dist/actions.d.ts.map +1 -1
- package/dist/actions.js +5 -4
- package/dist/actions.js.map +1 -1
- package/dist/document.d.ts +3 -1
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +4 -3
- package/dist/document.js.map +1 -1
- package/dist/href.d.ts +7 -0
- package/dist/href.d.ts.map +1 -1
- package/dist/href.js +16 -5
- package/dist/href.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/lang.d.ts +10 -0
- package/dist/lang.d.ts.map +1 -0
- package/dist/lang.js +26 -0
- package/dist/lang.js.map +1 -0
- package/dist/link.d.ts +15 -3
- package/dist/link.d.ts.map +1 -1
- package/dist/link.js +15 -9
- package/dist/link.js.map +1 -1
- package/dist/mounted.d.ts +51 -0
- package/dist/mounted.d.ts.map +1 -0
- package/dist/mounted.js +110 -0
- package/dist/mounted.js.map +1 -0
- package/dist/navigation-guard.d.ts +59 -0
- package/dist/navigation-guard.d.ts.map +1 -0
- package/dist/navigation-guard.js +116 -0
- package/dist/navigation-guard.js.map +1 -0
- package/dist/payload.d.ts +28 -2
- package/dist/payload.d.ts.map +1 -1
- package/dist/payload.js +53 -1
- package/dist/payload.js.map +1 -1
- package/dist/render-document.d.ts +18 -4
- package/dist/render-document.d.ts.map +1 -1
- package/dist/render-document.js +79 -16
- package/dist/render-document.js.map +1 -1
- package/dist/root.d.ts.map +1 -1
- package/dist/root.js +2 -1
- package/dist/root.js.map +1 -1
- package/dist/router.d.ts +101 -20
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +590 -68
- package/dist/router.js.map +1 -1
- package/dist/schema.d.ts +2 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +5 -0
- package/dist/schema.js.map +1 -1
- package/dist/testing.d.ts +23 -0
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +63 -21
- package/dist/testing.js.map +1 -1
- package/package.json +5 -1
- package/sidecar/server.mjs +117 -24
- package/skills/netjes/SKILL.md +6 -5
- package/skills/netjes/references/data-and-actions.md +10 -5
- package/skills/netjes/references/deployment.md +1 -1
- package/skills/netjes/references/routing.md +21 -6
- package/skills/netjes/references/testing.md +6 -3
- package/src/actions.tsx +5 -4
- package/src/document.tsx +11 -4
- package/src/href.ts +16 -5
- package/src/index.ts +27 -1
- package/src/lang.ts +28 -0
- package/src/link.tsx +37 -9
- package/src/mounted.tsx +160 -0
- package/src/navigation-guard.ts +136 -0
- package/src/payload.ts +69 -3
- package/src/render-document.tsx +117 -20
- package/src/root.tsx +3 -1
- package/src/router.tsx +764 -104
- package/src/schema.ts +11 -0
- package/src/testing-vitest.ts +1 -1
- package/src/testing.tsx +74 -15
- package/src/virtual.d.ts +6 -1
- package/template/Dockerfile +1 -1
- package/template/README.md +1 -1
- package/template/_AGENTS.md +7 -4
- package/template/docker-compose.override.yml +15 -3
- package/template/generated/netjes.ts +1 -1
- package/vite/packages.mjs +345 -0
- package/vite/plugin.d.ts +7 -0
- package/vite/plugin.mjs +208 -30
package/src/render-document.tsx
CHANGED
|
@@ -32,7 +32,17 @@ export type RenderDocumentOptions = {
|
|
|
32
32
|
lang?: string;
|
|
33
33
|
/** What `usePublicConfig()` reads, as the `Netjes:PublicConfig` section. */
|
|
34
34
|
publicConfig?: Record<string, unknown>;
|
|
35
|
-
/**
|
|
35
|
+
/**
|
|
36
|
+
* The page packages the app mounts, by npm name, each at its prefix as `MapPagePackage` takes it:
|
|
37
|
+
* `{ '@acme/help-center': '/(marketing)/help' }`. Their files in the pages map go into the app's folders under the
|
|
38
|
+
* prefix, so an app page in a package's folder renders inside the package's layouts, and the payload's `mounts` gives
|
|
39
|
+
* each one's URL for the page's `params`. A package that `netjes({ packages })` does not list has its pages in `app`.
|
|
40
|
+
*/
|
|
41
|
+
mounts?: Record<string, string>;
|
|
42
|
+
/**
|
|
43
|
+
* The page files, keyed `/app/...` as `import.meta.glob` keys them, and a page package's `/<npm name>/<its pages
|
|
44
|
+
* folder>/...`. Default: the app's and the listed packages', from the `netjes()` plugin.
|
|
45
|
+
*/
|
|
36
46
|
modules?: Record<string, () => Promise<unknown>>;
|
|
37
47
|
/** Stops the render, which then rejects with the signal's reason: for a page that waits on a Promise that never settles. */
|
|
38
48
|
signal?: AbortSignal;
|
|
@@ -65,6 +75,65 @@ function list(items: readonly string[]): string {
|
|
|
65
75
|
return items.length < 2 ? (items[0] ?? '') : `${items.slice(0, -1).join(', ')} and ${items.at(-1) ?? ''}`;
|
|
66
76
|
}
|
|
67
77
|
|
|
78
|
+
/** A page package mounted at a prefix: the prefix's folders below the app directory (`''` for `/`), and its pages folder. */
|
|
79
|
+
type Mount = { name: string; folder: string; pages: string };
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* A file of the pages map the server routes: its id, its path in the app directory (a package's under its mount), and
|
|
83
|
+
* its source, `app` or the package's name.
|
|
84
|
+
*/
|
|
85
|
+
type RoutedFile = { id: string; path: string; source: string };
|
|
86
|
+
|
|
87
|
+
const PREFIX_SEGMENT = /^(?:[\p{L}\p{N}._~-]+|\[[A-Za-z_][A-Za-z0-9_]*\]|\([\p{L}\p{N}_-]+\))$/u;
|
|
88
|
+
|
|
89
|
+
/** The mounts of `options.mounts`, with their pages folders from the plugin's list; throws for a prefix MapPagePackage refuses. */
|
|
90
|
+
function mountsOf(mounts: Readonly<Record<string, string>> | undefined, packages: readonly { name: string; pages: string }[]): Mount[] {
|
|
91
|
+
return Object.entries(mounts ?? {}).map(([name, prefix]) => {
|
|
92
|
+
const folders = prefix === '/' ? [] : prefix.split('/').slice(1);
|
|
93
|
+
const refused = (folder: string) => folder === '.' || folder === '..' || isPrivate(folder) || !PREFIX_SEGMENT.test(folder);
|
|
94
|
+
// A parameter used twice, in any case, as MapPagePackage refuses it.
|
|
95
|
+
const parameters = folders.filter((folder) => folder.startsWith('[')).map((folder) => folder.toLowerCase());
|
|
96
|
+
if (!prefix.startsWith('/') || folders.some(refused) || new Set(parameters).size < parameters.length) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
`netjes: renderDocument cannot mount '${name}' at '${prefix}': write the prefix as MapPagePackage takes it, ` +
|
|
99
|
+
"such as '/help', '/(marketing)/help' or '/[tenant]/help'.",
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return { name, folder: folders.join('/'), pages: packages.find((p) => p.name === name)?.pages ?? 'app' };
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The files the server routes: the app's where they are, and a mounted package's under its prefix's folders. */
|
|
107
|
+
function routedFiles(files: readonly string[], appDir: string, mounts: readonly Mount[]): RoutedFile[] {
|
|
108
|
+
return files.flatMap((id): RoutedFile[] => {
|
|
109
|
+
if (id.startsWith(`${appDir}/`)) return [{ id, path: id, source: 'app' }];
|
|
110
|
+
const mount = mounts.find((m) => id.startsWith(`${m.name}/${m.pages}/`));
|
|
111
|
+
if (!mount) return [];
|
|
112
|
+
const below = id.slice(mount.name.length + mount.pages.length + 2);
|
|
113
|
+
return [{ id, path: [appDir, mount.folder, below].filter(Boolean).join('/'), source: mount.name }];
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Each mount's URL for these route values, as the server sends it: the literal segments as written and each value
|
|
119
|
+
* encoded as one segment, or null when a value is missing, a list, empty, `.` or `..`.
|
|
120
|
+
*/
|
|
121
|
+
function mountUrls(mounts: readonly Mount[], params: Params): Record<string, string | null> {
|
|
122
|
+
// As the server encodes a value (Uri.EscapeDataString), which encodeURIComponent leaves these five unencoded.
|
|
123
|
+
const encode = (value: string) => encodeURIComponent(value).replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
124
|
+
return Object.fromEntries(
|
|
125
|
+
mounts.map((mount) => {
|
|
126
|
+
const segments: string[] = [];
|
|
127
|
+
for (const folder of mount.folder.split('/').filter((f) => f && !isGroup(f))) {
|
|
128
|
+
const value = folder.startsWith('[') ? params[folder.slice(1, -1)] : folder;
|
|
129
|
+
if (typeof value !== 'string' || value === '' || value === '.' || value === '..') return [mount.name, null];
|
|
130
|
+
segments.push(folder.startsWith('[') ? encode(value) : value);
|
|
131
|
+
}
|
|
132
|
+
return [mount.name, `/${segments.join('/')}`];
|
|
133
|
+
}),
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
68
137
|
/** A file in the app directory: the folders below the directory and the file's name without its extension; null for any other file. */
|
|
69
138
|
function parse(file: string, appDir: string): { folders: string[]; name: string } | null {
|
|
70
139
|
if (!file.startsWith(`${appDir}/`) || !PAGE_EXTENSION.test(file)) return null;
|
|
@@ -75,17 +144,17 @@ function parse(file: string, appDir: string): { folders: string[]; name: string
|
|
|
75
144
|
|
|
76
145
|
type AppFiles = {
|
|
77
146
|
/** Each folder's convention files by name, by its path below the app directory (`''` for the directory itself). */
|
|
78
|
-
folders: Map<string, Map<string,
|
|
147
|
+
folders: Map<string, Map<string, RoutedFile>>;
|
|
79
148
|
/** The folders with parallel route slots, each with the first of its slots. */
|
|
80
149
|
slots: Map<string, string>;
|
|
81
150
|
};
|
|
82
151
|
|
|
83
152
|
/** The convention files the server routes: none in private folders, and the slots that pages and defaults make. */
|
|
84
|
-
function appFiles(files: readonly
|
|
85
|
-
const folders = new Map<string, Map<string,
|
|
153
|
+
function appFiles(files: readonly RoutedFile[], appDir: string): AppFiles {
|
|
154
|
+
const folders = new Map<string, Map<string, RoutedFile>>();
|
|
86
155
|
const slots = new Map<string, string>();
|
|
87
|
-
for (const file of [...files].sort(ordinal)) {
|
|
88
|
-
const parsed = parse(file, appDir);
|
|
156
|
+
for (const file of [...files].sort((a, b) => ordinal(a.id, b.id))) {
|
|
157
|
+
const parsed = parse(file.path, appDir);
|
|
89
158
|
if (!parsed || parsed.folders.some(isPrivate)) continue;
|
|
90
159
|
const slot = parsed.folders.findIndex(isSlot);
|
|
91
160
|
if (slot >= 0) {
|
|
@@ -98,7 +167,12 @@ function appFiles(files: readonly string[], appDir: string): AppFiles {
|
|
|
98
167
|
const folder = parsed.folders.join('/');
|
|
99
168
|
let conventions = folders.get(folder);
|
|
100
169
|
if (!conventions) folders.set(folder, (conventions = new Map()));
|
|
101
|
-
|
|
170
|
+
const other = conventions.get(parsed.name);
|
|
171
|
+
// As the server, which refuses two sources' segment files in one folder.
|
|
172
|
+
if (other && other.source !== file.source && parsed.name !== 'page') {
|
|
173
|
+
throw new Error(`netjes: renderDocument has two ${parsed.name} files for ${[appDir, folder].filter(Boolean).join('/')}: ${other.id} and ${file.id}.`);
|
|
174
|
+
}
|
|
175
|
+
if (!other) conventions.set(parsed.name, file);
|
|
102
176
|
}
|
|
103
177
|
return { folders, slots };
|
|
104
178
|
}
|
|
@@ -141,7 +215,7 @@ function segmentsFor(page: string, app: AppFiles, folders: readonly string[], ap
|
|
|
141
215
|
if (!conventions || !SEGMENT_FILES.some((name) => conventions.has(name))) continue;
|
|
142
216
|
const segment: SegmentRef = { id: folder ? `${appDir}/${folder}` : appDir };
|
|
143
217
|
for (const name of ['layout', 'template', 'loading', 'error'] as const) {
|
|
144
|
-
const file = conventions.get(name);
|
|
218
|
+
const file = conventions.get(name)?.id;
|
|
145
219
|
if (file) segment[name] = file;
|
|
146
220
|
}
|
|
147
221
|
if (segment.layout) {
|
|
@@ -154,44 +228,66 @@ function segmentsFor(page: string, app: AppFiles, folders: readonly string[], ap
|
|
|
154
228
|
}
|
|
155
229
|
|
|
156
230
|
/** Every file renderDocument could render from these: the pages and the boundary files, in ordinal order. */
|
|
157
|
-
function renderable(files: readonly
|
|
231
|
+
function renderable(files: readonly RoutedFile[], appDir: string): string[] {
|
|
158
232
|
return files
|
|
159
233
|
.filter((file) => {
|
|
160
|
-
const parsed = parse(file, appDir);
|
|
234
|
+
const parsed = parse(file.path, appDir);
|
|
161
235
|
if (!parsed || parsed.folders.some((folder) => isPrivate(folder) || isSlot(folder))) return false;
|
|
162
|
-
return parsed.name === 'page' || (BOUNDARIES.has(parsed.name) && (parsed.name !== 'global-error' || parsed
|
|
236
|
+
return parsed.name === 'page' || (BOUNDARIES.has(parsed.name) && (parsed.name !== 'global-error' || isRootGlobalError(file, parsed)));
|
|
163
237
|
})
|
|
238
|
+
.map(({ id }) => id)
|
|
164
239
|
.sort(ordinal);
|
|
165
240
|
}
|
|
166
241
|
|
|
242
|
+
/** The global error page is the app's own, in the app directory itself; the server refuses one in a package. */
|
|
243
|
+
const isRootGlobalError = (file: RoutedFile, parsed: { folders: string[] }) => file.source === 'app' && parsed.folders.length === 0;
|
|
244
|
+
|
|
167
245
|
/**
|
|
168
246
|
* The payload the server sends with the document of `page`, a file of the pages map named without its leading slash,
|
|
169
247
|
* for the values in `options`: the route, kind, status and segments by the server's rules, and a build with no
|
|
170
|
-
* stylesheets or scripts.
|
|
171
|
-
*
|
|
248
|
+
* stylesheets or scripts. The files of the page packages `options.mounts` names go under their prefixes, with their
|
|
249
|
+
* pages folders from `packages`. Throws for a file it cannot render: one not in `files`, a layout, a page in a private
|
|
250
|
+
* folder or under a parallel route slot, or a file of a package it does not mount.
|
|
172
251
|
*/
|
|
173
|
-
export function documentPayload(
|
|
252
|
+
export function documentPayload(
|
|
253
|
+
page: string,
|
|
254
|
+
options: DocumentOptions,
|
|
255
|
+
files: readonly string[],
|
|
256
|
+
appDir: string,
|
|
257
|
+
packages: readonly { name: string; pages: string }[] = [],
|
|
258
|
+
): InitialPayload {
|
|
174
259
|
const paths = files.map((file) => file.replace(/^\//, ''));
|
|
260
|
+
const mounts = mountsOf(options.mounts, packages);
|
|
261
|
+
const routed = routedFiles(paths, appDir, mounts);
|
|
175
262
|
if (!paths.includes(page)) {
|
|
176
263
|
const hint = page.startsWith('/') && paths.includes(page.slice(1))
|
|
177
264
|
? `; name the file without the leading slash: ${page.slice(1)}.`
|
|
178
|
-
: `. The files it can render are ${list(renderable(
|
|
265
|
+
: `. The files it can render are ${list(renderable(routed, appDir))}.`;
|
|
179
266
|
throw new Error(`netjes: renderDocument has no ${page} in the pages map${hint}`);
|
|
180
267
|
}
|
|
181
|
-
const
|
|
182
|
-
|
|
268
|
+
const file = routed.find((routedFile) => routedFile.id === page);
|
|
269
|
+
const parsed = file && parse(file.path, appDir);
|
|
270
|
+
if (!parsed) {
|
|
271
|
+
const unmounted = packages.find((p) => page.startsWith(`${p.name}/`))?.name;
|
|
272
|
+
throw new Error(
|
|
273
|
+
unmounted
|
|
274
|
+
? `netjes: renderDocument cannot render ${page}, a file of the page package '${unmounted}', which it does not mount: ` +
|
|
275
|
+
`pass its prefix as MapPagePackage takes it, as in mounts: { '${unmounted}': '/help' }.`
|
|
276
|
+
: `netjes: renderDocument cannot render ${page}, which is not in the app's directory, ${appDir}, or in a mounted page package's pages.`,
|
|
277
|
+
);
|
|
278
|
+
}
|
|
183
279
|
if (parsed.folders.some(isPrivate)) throw new Error(`netjes: renderDocument cannot render ${page}: a private folder is not routed.`);
|
|
184
280
|
const slot = parsed.folders.findIndex(isSlot);
|
|
185
281
|
if (slot >= 0) throw slotError(page, 'is in', [appDir, ...parsed.folders.slice(0, slot + 1)].join('/'));
|
|
186
282
|
const boundary = BOUNDARIES.get(parsed.name);
|
|
187
|
-
if ((parsed.name !== 'page' && !boundary) || (parsed.name === 'global-error' && parsed
|
|
283
|
+
if ((parsed.name !== 'page' && !boundary) || (parsed.name === 'global-error' && !isRootGlobalError(file, parsed))) {
|
|
188
284
|
throw new Error(
|
|
189
285
|
`netjes: renderDocument renders a page or a not-found, error, forbidden, unauthorized or global-error file, not ${page}.`,
|
|
190
286
|
);
|
|
191
287
|
}
|
|
192
288
|
|
|
193
289
|
// The global error page renders without any layout, the others inside the segments down to their own folder.
|
|
194
|
-
const segments = parsed.name === 'global-error' ? [] : segmentsFor(page, appFiles(
|
|
290
|
+
const segments = parsed.name === 'global-error' ? [] : segmentsFor(page, appFiles(routed, appDir), parsed.folders, appDir, options);
|
|
195
291
|
for (const file of Object.keys(options.layouts ?? {})) {
|
|
196
292
|
if (!segments.some((segment) => segment.layout === file)) {
|
|
197
293
|
const layouts = segments.flatMap((segment) => (segment.layout ? [segment.layout] : []));
|
|
@@ -216,6 +312,7 @@ export function documentPayload(page: string, options: DocumentOptions, files: r
|
|
|
216
312
|
status: options.status ?? boundary?.status ?? 200,
|
|
217
313
|
url: options.url ?? '/',
|
|
218
314
|
version: 'test',
|
|
315
|
+
...(mounts.length > 0 ? { mounts: mountUrls(mounts, options.params ?? {}) } : {}),
|
|
219
316
|
document: {
|
|
220
317
|
lang: options.lang ?? 'en',
|
|
221
318
|
assets: { stylesheets: [], modulePreloads: [], scripts: [] },
|
|
@@ -294,7 +391,7 @@ export async function renderDocument(page: string, options: RenderDocumentOption
|
|
|
294
391
|
signal?.throwIfAborted();
|
|
295
392
|
const app = await import('virtual:netjes/pages');
|
|
296
393
|
const pages = modules ?? app.pages;
|
|
297
|
-
const payload = documentPayload(page, sent, Object.keys(pages), app.appDir);
|
|
394
|
+
const payload = documentPayload(page, sent, Object.keys(pages), app.appDir, app.packages);
|
|
298
395
|
// Loaded here, so that component tests, which import netjes/testing too, never load react-dom/server.
|
|
299
396
|
const { createServerRenderer } = await import('./server-render.tsx');
|
|
300
397
|
const stream = await createServerRenderer(pages).renderToStream(htmlSafeJson(payload), {
|
package/src/root.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import type { ReactNode } from 'react';
|
|
|
2
2
|
import { QueryShell } from 'virtual:netjes/query';
|
|
3
3
|
import { Document } from './document.tsx';
|
|
4
4
|
import { ImageConfigProvider } from './image.tsx';
|
|
5
|
+
import { documentLang } from './lang.ts';
|
|
5
6
|
import { NonceProvider } from './nonce.tsx';
|
|
6
7
|
import { PublicConfigProvider } from './public-config.tsx';
|
|
7
8
|
import type { ModuleLoader, RouteModules } from './modules.ts';
|
|
@@ -24,13 +25,14 @@ function RootProviders({ info, children }: { info: DocumentInfo; children: React
|
|
|
24
25
|
/** The same tree on the server and in the browser, so hydration matches. */
|
|
25
26
|
export function Root({ payload, modules, loader, dataJson }: RootProps) {
|
|
26
27
|
return (
|
|
27
|
-
<Document info={payload.document} dataJson={dataJson}>
|
|
28
|
+
<Document info={payload.document} lang={documentLang(payload, payload.document.lang)} dataJson={dataJson}>
|
|
28
29
|
<RootProviders info={payload.document}>
|
|
29
30
|
<RouterProvider
|
|
30
31
|
initial={payload}
|
|
31
32
|
initialModules={modules}
|
|
32
33
|
loader={loader}
|
|
33
34
|
errorPage={payload.document.errorPage}
|
|
35
|
+
documentLang={payload.document.lang}
|
|
34
36
|
shell={QueryShell}
|
|
35
37
|
/>
|
|
36
38
|
</RootProviders>
|