@foliwake/server 0.0.0-stage → 0.3.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/CHANGELOG.md +62 -0
- package/LICENSE +202 -0
- package/README.md +999 -2
- package/dist/config.d.ts +111 -0
- package/dist/config.js +321 -0
- package/dist/constants.d.ts +26 -0
- package/dist/constants.js +46 -0
- package/dist/csp.d.ts +25 -0
- package/dist/csp.js +78 -0
- package/dist/env-names.d.ts +2 -0
- package/dist/env-names.js +20 -0
- package/dist/errors.d.ts +16 -0
- package/dist/errors.js +16 -0
- package/dist/glob.d.ts +10 -0
- package/dist/glob.js +68 -0
- package/dist/handlers.d.ts +17 -0
- package/dist/handlers.js +118 -0
- package/dist/html-parse.d.ts +25 -0
- package/dist/html-parse.js +147 -0
- package/dist/html.d.ts +27 -0
- package/dist/html.js +148 -0
- package/dist/media-rewrite.d.ts +9 -0
- package/dist/media-rewrite.js +37 -0
- package/dist/origin.d.ts +36 -0
- package/dist/origin.js +129 -0
- package/dist/page.d.ts +43 -0
- package/dist/page.js +187 -0
- package/dist/preload.d.ts +14 -0
- package/dist/preload.js +140 -0
- package/dist/stream.d.ts +9 -0
- package/dist/stream.js +59 -0
- package/package.json +82 -3
- package/schemas/remote-2.schema.json +110 -0
package/README.md
CHANGED
|
@@ -1,3 +1,1000 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @foliwake/server
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<!-- usage -->
|
|
4
|
+
|
|
5
|
+
`@foliwake/server` is the server toolkit for any app that shows Foliwake pages, with no framework
|
|
6
|
+
dependency: Web `Request`/`Response` and plain functions. It parses the app's `foliwake.remote.json`, reads a
|
|
7
|
+
page's body from the site's live or preview origin with the credential, checks and prepares it per request,
|
|
8
|
+
serves the page's code release and pictures through your app's origin (proxies), builds a
|
|
9
|
+
Content-Security-Policy, and computes preload hints. `@foliwake/next` is built on it; use this package
|
|
10
|
+
directly with another server framework (a Vite or Remix server, Hono, Express with Web adapters, a worker).
|
|
11
|
+
|
|
12
|
+
| Entry | What |
|
|
13
|
+
| --- | --- |
|
|
14
|
+
| `@foliwake/server/config` | `loadFoliwakeConfig`, `parseFoliwakeConfig`, `clientSettings`, `pageAllowed`, `FoliwakeConfigError` |
|
|
15
|
+
| `@foliwake/server/origin` | `createOriginClient`: the only place a password is read (per request, from the variable the config names) |
|
|
16
|
+
| `@foliwake/server/page` | `readEnvelope` (primitive inputs: safe inside a cache function), `preparePage`, `FoliwakePageError` |
|
|
17
|
+
| `@foliwake/server/handlers` | `codeHandler`, `mediaHandler`: the code and media proxies |
|
|
18
|
+
| `@foliwake/server/html` | the strict body check: `checkPlainBody`, `checkBody`, `serialize`, `srcsetCandidates` |
|
|
19
|
+
| `@foliwake/server/csp` | `foliwakeCsp` |
|
|
20
|
+
| `@foliwake/server/preload` | `preloadHints`: modulepreload and stylesheet hints for a page's blocks |
|
|
21
|
+
| `@foliwake/server/schemas/remote-2.schema.json` | the JSON Schema of the config file |
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
The packages are published on npm (npmjs.com) under the `@foliwake` scope; no registry setup or token is
|
|
26
|
+
needed. Install an exact version, the same for every `@foliwake/*` package:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pnpm add @foliwake/server@0.3.0
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The config file sits in the app's root and holds no secrets, only the names of the variables with passwords or keys:
|
|
33
|
+
|
|
34
|
+
```json
|
|
35
|
+
{
|
|
36
|
+
"$schema": "./node_modules/@foliwake/server/schemas/remote-2.schema.json",
|
|
37
|
+
"version": 2,
|
|
38
|
+
"site": "mysite",
|
|
39
|
+
"live": { "url": "https://mysite.example.org" },
|
|
40
|
+
"preview": { "url": "https://preview.mysite.example.org", "auth": { "type": "basic", "username": "app", "passwordEnv": "MYSITE_PREVIEW_PASSWORD" } },
|
|
41
|
+
"app": {
|
|
42
|
+
"pages": ["/**"],
|
|
43
|
+
"proxies": { "code": "/foliwake-code", "media": "/foliwake-media", "pages": null },
|
|
44
|
+
"blocks": { "entry": "scripts/blocks.js", "contract": "foliwake-blocks/2", "eventPrefix": "demo" },
|
|
45
|
+
"revalidate": 300
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
An endpoint is open (no `auth`), behind HTTP Basic (`"type": "basic"`), or behind a delivery that checks
|
|
51
|
+
service tokens, such as the Cloudflare Worker (`packages/worker-origin`):
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
"live": {
|
|
55
|
+
"url": "https://mysite.worker.example.org",
|
|
56
|
+
"auth": { "type": "service-token", "issuer": "mysite-app", "kid": "mysite-app-2026-11", "privateKeyEnv": "MYSITE_LIVE_SIGNING_KEY" }
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
With `service-token`, every origin request carries `Authorization: Bearer <JWT>`, signed for that request
|
|
61
|
+
alone: ES256, header `kid`, claims `iss` (= `issuer`, the delivery's `TOKEN_ISSUER`), `aud: site:<site>`,
|
|
62
|
+
`iat`, `exp = iat + 60` and `path`, the exact path of the request as sent, without the query
|
|
63
|
+
(`/lessons/a.plain.html`, `/lessons/query-index.json`). A token for a page does not open its `.plain.html`.
|
|
64
|
+
The variable holds the ES256 private key as JWK JSON text; the delivery holds the public key under the same
|
|
65
|
+
`kid`. Tokens are never cached, reused or logged.
|
|
66
|
+
|
|
67
|
+
## Minimal example
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { loadFoliwakeConfig } from '@foliwake/server/config';
|
|
71
|
+
import { createOriginClient } from '@foliwake/server/origin';
|
|
72
|
+
import { preparePage, readEnvelope } from '@foliwake/server/page';
|
|
73
|
+
|
|
74
|
+
const config = await loadFoliwakeConfig(); // FOLIWAKE_REMOTE, else ./foliwake.remote.json
|
|
75
|
+
const client = createOriginClient(config, process.env);
|
|
76
|
+
|
|
77
|
+
const envelope = await readEnvelope(config, client, config.site, 'live', '/lessons/a'); // cacheable
|
|
78
|
+
const page = preparePage(config, envelope); // per request: checked, rewritten, serialisable
|
|
79
|
+
console.log(page.identity, page.blocks);
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Examples
|
|
83
|
+
|
|
84
|
+
### A page endpoint for the browser, with a cache of envelopes
|
|
85
|
+
|
|
86
|
+
Cache only the envelope (what the origin answered) and prepare per request, so your callbacks (`rewriteLink`,
|
|
87
|
+
`transform`) always run on the current code. The prepared page is what `@foliwake/client` or
|
|
88
|
+
`@foliwake/react` mount.
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { loadFoliwakeConfig } from '@foliwake/server/config';
|
|
92
|
+
import { createOriginClient } from '@foliwake/server/origin';
|
|
93
|
+
import { FoliwakePageError, preparePage, readEnvelope, type PageEnvelope } from '@foliwake/server/page';
|
|
94
|
+
import { preloadHints } from '@foliwake/server/preload';
|
|
95
|
+
|
|
96
|
+
const config = await loadFoliwakeConfig();
|
|
97
|
+
const client = createOriginClient(config, process.env);
|
|
98
|
+
const cache = new Map<string, { at: number; envelope: PageEnvelope }>();
|
|
99
|
+
|
|
100
|
+
async function liveEnvelope(path: string): Promise<PageEnvelope> {
|
|
101
|
+
const hit = cache.get(path);
|
|
102
|
+
if (hit && Date.now() - hit.at < config.app!.revalidate * 1000) return hit.envelope;
|
|
103
|
+
const envelope = await readEnvelope(config, client, config.site, 'live', path);
|
|
104
|
+
cache.set(path, { at: Date.now(), envelope });
|
|
105
|
+
return envelope;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** GET /api/page?path=/lessons/a */
|
|
109
|
+
export async function pageEndpoint(request: Request): Promise<Response> {
|
|
110
|
+
const path = new URL(request.url).searchParams.get('path') ?? '/';
|
|
111
|
+
try {
|
|
112
|
+
const page = preparePage(config, await liveEnvelope(path), { rewriteLink: (href) => `/learn${href}` });
|
|
113
|
+
page.preload = await preloadHints(config, client, page);
|
|
114
|
+
return Response.json(page, { headers: { 'Cache-Control': 'private, no-store' } });
|
|
115
|
+
} catch (error) {
|
|
116
|
+
if (error instanceof FoliwakePageError && (error.kind === 'not_found' || error.kind === 'not_allowed')) return new Response(null, { status: 404 });
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Code and media proxies
|
|
123
|
+
|
|
124
|
+
Mount the handlers under the prefixes of `app.proxies` and pass them the path segments after the prefix. They
|
|
125
|
+
serve only files of a `cd_…` code release and hashed pictures, add the credential on the server and forward
|
|
126
|
+
nothing from the browser. Live files are cached for a year (immutable); preview answers are
|
|
127
|
+
`private, no-store` and need `authorizePreview`.
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
import { loadFoliwakeConfig } from '@foliwake/server/config';
|
|
131
|
+
import { codeHandler, mediaHandler } from '@foliwake/server/handlers';
|
|
132
|
+
import { createOriginClient } from '@foliwake/server/origin';
|
|
133
|
+
|
|
134
|
+
const config = await loadFoliwakeConfig();
|
|
135
|
+
const client = createOriginClient(config, process.env);
|
|
136
|
+
const isAuthor = async (request: Request) => (request.headers.get('cookie') ?? '').includes('role=author'); // your sign-in
|
|
137
|
+
const code = codeHandler(config, client, { authorizePreview: isAuthor });
|
|
138
|
+
const media = mediaHandler(config, client, { authorizePreview: isAuthor });
|
|
139
|
+
|
|
140
|
+
/** Route requests under /foliwake-code/ and /foliwake-media/ (app.proxies) to the handlers. */
|
|
141
|
+
export async function proxies(request: Request): Promise<Response | null> {
|
|
142
|
+
const { pathname } = new URL(request.url);
|
|
143
|
+
const segments = (prefix: string) => pathname.slice(prefix.length + 1).split('/').filter(Boolean);
|
|
144
|
+
if (pathname.startsWith(`${config.app!.proxies.code}/`)) return code(request, { path: segments(config.app!.proxies.code) });
|
|
145
|
+
if (pathname.startsWith(`${config.app!.proxies.media}/`)) return media(request, { path: segments(config.app!.proxies.media) });
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### CSP and browser settings
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
import { clientSettings, loadFoliwakeConfig } from '@foliwake/server/config';
|
|
154
|
+
import { foliwakeCsp } from '@foliwake/server/csp';
|
|
155
|
+
|
|
156
|
+
const config = await loadFoliwakeConfig();
|
|
157
|
+
|
|
158
|
+
// One header value for every route: blocks load from your own origin, so no Foliwake host is named.
|
|
159
|
+
export const csp = foliwakeCsp({
|
|
160
|
+
strategy: 'self-inline',
|
|
161
|
+
mediaBase: config.app?.media?.base ?? null,
|
|
162
|
+
reportUri: '/csp-report',
|
|
163
|
+
dev: process.env.NODE_ENV === 'development',
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// The only part of the config a browser may see: site, proxy prefixes, blocks contract and event prefix.
|
|
167
|
+
export const settingsForBrowser = clientSettings(config);
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Compatibility
|
|
171
|
+
|
|
172
|
+
| | |
|
|
173
|
+
| --- | --- |
|
|
174
|
+
| Node.js | 22.22 or newer (Web `fetch`, `Request`, `Response`, streams) |
|
|
175
|
+
| Foliwake origin | sends `X-Code-Release` with `.plain.html` (plain/2); a plain/1 origin gives bodies without blocks |
|
|
176
|
+
| Config | `foliwake.remote.json` version 2 (version 1 files are refused) |
|
|
177
|
+
| Blocks contract | `foliwake-blocks/2` |
|
|
178
|
+
|
|
179
|
+
Changes per version, breaking ones marked: `CHANGELOG.md` in the package
|
|
180
|
+
(`node_modules/@foliwake/server/CHANGELOG.md`).
|
|
181
|
+
|
|
182
|
+
## Good to know
|
|
183
|
+
|
|
184
|
+
- Bodies are checked before and after your `transform`: no scripts, styles, frames, inline SVG or MathML, no
|
|
185
|
+
`on…` attributes or `javascript:` URLs; `srcset` and entities are parsed like a browser does. What is
|
|
186
|
+
returned is serialised from the checked tree. A refused body throws `FoliwakePageError` with kind `invalid`.
|
|
187
|
+
- Origin responses are size-limited while streaming (body 2 MB, code 5 MB, media 25 MB); requests time out
|
|
188
|
+
after 10 s.
|
|
189
|
+
- Credentials never appear in messages; a missing or malformed password or key variable is a
|
|
190
|
+
`FoliwakeConfigError` that names the variable, not its value. The secret is read at each request, so a
|
|
191
|
+
rotated secret works without a restart. A private key whose own `kid` differs from the config's is refused.
|
|
192
|
+
- Rotating a service-token key: add the new public key to the delivery's key set first (both `kid`s listed),
|
|
193
|
+
then switch the app's `kid` and private key, then remove the old public key.
|
|
194
|
+
- `preparePage` leaves `page.preload` empty; `preloadHints` fills it from the release's
|
|
195
|
+
`components/components.json` (only blocks that declare a `.js` or `.css` get hints).
|
|
196
|
+
|
|
197
|
+
## Guides
|
|
198
|
+
|
|
199
|
+
- [Embed Foliwake pages in a React or Next app](https://docs.foliwake.dev/guide/embed-in-react): config keys,
|
|
200
|
+
proxies, caching and troubleshooting.
|
|
201
|
+
- [Configuration and storage](https://docs.foliwake.dev/guide/configuration-storage)
|
|
202
|
+
|
|
203
|
+
License: Apache-2.0.
|
|
204
|
+
|
|
205
|
+
<!-- usagestop -->
|
|
206
|
+
|
|
207
|
+
## API
|
|
208
|
+
|
|
209
|
+
<!-- api -->
|
|
210
|
+
|
|
211
|
+
<!-- Generated by `pnpm packages:reference` from the type declarations; do not edit by hand. -->
|
|
212
|
+
|
|
213
|
+
### `@foliwake/server/config`
|
|
214
|
+
|
|
215
|
+
**Functions and components**
|
|
216
|
+
|
|
217
|
+
#### `appOf()`
|
|
218
|
+
|
|
219
|
+
The app settings; throws when the config has none.
|
|
220
|
+
|
|
221
|
+
```ts
|
|
222
|
+
function appOf(config: FoliwakeAppConfig): FoliwakeAppSettings
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
**Returns** `FoliwakeAppSettings`
|
|
226
|
+
|
|
227
|
+
#### `checkEnvName()`
|
|
228
|
+
|
|
229
|
+
Validates an environment variable NAME (never a value).
|
|
230
|
+
|
|
231
|
+
```ts
|
|
232
|
+
function checkEnvName(value: unknown, where: string, extra?: ParseOptions['envNameProblem']): string
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
**Returns** `string`
|
|
236
|
+
|
|
237
|
+
#### `checkUsername()`
|
|
238
|
+
|
|
239
|
+
Validates a Basic user name (no `:` or control characters, at most 128 characters).
|
|
240
|
+
|
|
241
|
+
```ts
|
|
242
|
+
function checkUsername(value: unknown, where: string): string
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
**Returns** `string`
|
|
246
|
+
|
|
247
|
+
#### `clientSettings()`
|
|
248
|
+
|
|
249
|
+
The explicit browser projection: nothing else of the config reaches a browser.
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
function clientSettings(config: FoliwakeAppConfig): ClientSettings
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
**Returns** `ClientSettings`
|
|
256
|
+
|
|
257
|
+
#### `configFilePath()`
|
|
258
|
+
|
|
259
|
+
Which file: an explicit `file` beats FOLIWAKE_REMOTE beats `<cwd>/foliwake.remote.json`.
|
|
260
|
+
|
|
261
|
+
```ts
|
|
262
|
+
function configFilePath(opts?: {
|
|
263
|
+
cwd?: string;
|
|
264
|
+
file?: string;
|
|
265
|
+
env?: NodeJS.ProcessEnv;
|
|
266
|
+
}): string
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
**Returns** `string`
|
|
270
|
+
|
|
271
|
+
#### `endpointOf()`
|
|
272
|
+
|
|
273
|
+
The endpoint of a channel; a missing one is an error, never replaced by the other channel.
|
|
274
|
+
|
|
275
|
+
```ts
|
|
276
|
+
function endpointOf(config: FoliwakeAppConfig, channel: ChannelName): FoliwakeEndpoint
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
**Returns** `FoliwakeEndpoint`
|
|
280
|
+
|
|
281
|
+
#### `isLoopbackName()`
|
|
282
|
+
|
|
283
|
+
True for 127.x, localhost, *.localhost and ::1.
|
|
284
|
+
|
|
285
|
+
```ts
|
|
286
|
+
function isLoopbackName(hostname: string): boolean
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
**Returns** `boolean`
|
|
290
|
+
|
|
291
|
+
#### `loadFoliwakeConfig()`
|
|
292
|
+
|
|
293
|
+
Reads and parses the config file (see configFilePath).
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
function loadFoliwakeConfig(opts?: {
|
|
297
|
+
cwd?: string;
|
|
298
|
+
file?: string;
|
|
299
|
+
env?: NodeJS.ProcessEnv;
|
|
300
|
+
} & Omit<ParseOptions, 'file'>): Promise<FoliwakeAppConfig>
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
**Returns** `Promise<FoliwakeAppConfig>`
|
|
304
|
+
|
|
305
|
+
#### `pageAllowed()`
|
|
306
|
+
|
|
307
|
+
Whether the app may read this page path (`app.pages`).
|
|
308
|
+
|
|
309
|
+
```ts
|
|
310
|
+
function pageAllowed(config: FoliwakeAppConfig, pagePath: string): boolean
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
**Returns** `boolean`
|
|
314
|
+
|
|
315
|
+
#### `parseFoliwakeConfig()`
|
|
316
|
+
|
|
317
|
+
Parses the text of a version 2 config. Throws FoliwakeConfigError with a message that names the field.
|
|
318
|
+
|
|
319
|
+
```ts
|
|
320
|
+
function parseFoliwakeConfig(text: string, opts?: ParseOptions): FoliwakeAppConfig
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
**Returns** `FoliwakeAppConfig`
|
|
324
|
+
|
|
325
|
+
#### `parseRemoteUrl()`
|
|
326
|
+
|
|
327
|
+
A site origin: https (http only for loopback), no credentials, path, query or fragment.
|
|
328
|
+
|
|
329
|
+
```ts
|
|
330
|
+
function parseRemoteUrl(value: unknown, where: string): string
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
**Returns** `string`
|
|
334
|
+
|
|
335
|
+
**Classes**
|
|
336
|
+
|
|
337
|
+
#### `FoliwakeConfigError`
|
|
338
|
+
|
|
339
|
+
A configuration problem; the message is shown as is (it never contains a secret).
|
|
340
|
+
|
|
341
|
+
```ts
|
|
342
|
+
class FoliwakeConfigError extends Error
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
| Member | Type | Description |
|
|
346
|
+
| --- | --- | --- |
|
|
347
|
+
| `name` | `string` | |
|
|
348
|
+
|
|
349
|
+
**Constants**
|
|
350
|
+
|
|
351
|
+
#### `CHANNELS`
|
|
352
|
+
|
|
353
|
+
The channels a config can name.
|
|
354
|
+
|
|
355
|
+
```ts
|
|
356
|
+
const CHANNELS: readonly ChannelName[]
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
#### `CONFIG_ENV`
|
|
360
|
+
|
|
361
|
+
The environment variable that names another config file.
|
|
362
|
+
|
|
363
|
+
```ts
|
|
364
|
+
const CONFIG_ENV = "FOLIWAKE_REMOTE"
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
#### `CONFIG_FILE`
|
|
368
|
+
|
|
369
|
+
The default config file name, looked up in the current directory.
|
|
370
|
+
|
|
371
|
+
```ts
|
|
372
|
+
const CONFIG_FILE = "foliwake.remote.json"
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
**Interfaces**
|
|
376
|
+
|
|
377
|
+
#### `BasicAuth`
|
|
378
|
+
|
|
379
|
+
HTTP Basic credentials: a user name and the NAME of the variable holding the password.
|
|
380
|
+
|
|
381
|
+
```ts
|
|
382
|
+
interface BasicAuth
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
| Member | Type | Description |
|
|
386
|
+
| --- | --- | --- |
|
|
387
|
+
| `type` | `'basic'` | |
|
|
388
|
+
| `username` | `string` | |
|
|
389
|
+
| `passwordEnv` | `string` | |
|
|
390
|
+
|
|
391
|
+
#### `FoliwakeAppConfig`
|
|
392
|
+
|
|
393
|
+
A parsed and validated `foliwake.remote.json` (version 2). It holds credential variable names, never secrets.
|
|
394
|
+
|
|
395
|
+
```ts
|
|
396
|
+
interface FoliwakeAppConfig
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
| Member | Type | Description |
|
|
400
|
+
| --- | --- | --- |
|
|
401
|
+
| `version` | `2` | |
|
|
402
|
+
| `site` | `string` | |
|
|
403
|
+
| `live` | `FoliwakeEndpoint \| null` | |
|
|
404
|
+
| `preview` | `FoliwakeEndpoint \| null` | |
|
|
405
|
+
| `app` | `FoliwakeAppSettings \| null` | Null only when parsed with `requireApp: false` (the engine CLI). |
|
|
406
|
+
| `from` | `string` | Where the config came from, for messages. |
|
|
407
|
+
|
|
408
|
+
#### `FoliwakeAppSettings`
|
|
409
|
+
|
|
410
|
+
The `app` part of the config, with defaults applied.
|
|
411
|
+
|
|
412
|
+
```ts
|
|
413
|
+
interface FoliwakeAppSettings
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
| Member | Type | Description |
|
|
417
|
+
| --- | --- | --- |
|
|
418
|
+
| `pages` | `readonly string[]` | Page globs the app may read. |
|
|
419
|
+
| `proxies` | `{ code: string; media: string; pages: null; }` | App-origin URL prefixes of the proxies. |
|
|
420
|
+
| `blocks` | `{ entry: string \| null; contract: string; major: number; eventPrefix: string; }` | Where `mountBlocks` lives in a code release; null: bodies stay static. |
|
|
421
|
+
| `media` | `{ base: string; transform: 'cloudflare-images' \| null; } \| null` | Live media through a transforming host; null: media through the proxy. |
|
|
422
|
+
| `revalidate` | `number` | Seconds before a cached envelope is stale (stale-while-revalidate). |
|
|
423
|
+
|
|
424
|
+
#### `FoliwakeEndpoint`
|
|
425
|
+
|
|
426
|
+
A channel's origin and its optional credential.
|
|
427
|
+
|
|
428
|
+
```ts
|
|
429
|
+
interface FoliwakeEndpoint
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
| Member | Type | Description |
|
|
433
|
+
| --- | --- | --- |
|
|
434
|
+
| `origin` | `string` | Origin only: scheme, host, port. |
|
|
435
|
+
| `auth` | `RemoteAuth \| null` | |
|
|
436
|
+
|
|
437
|
+
#### `ParseOptions`
|
|
438
|
+
|
|
439
|
+
Options of `parseFoliwakeConfig`.
|
|
440
|
+
|
|
441
|
+
```ts
|
|
442
|
+
interface ParseOptions
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
| Member | Type | Description |
|
|
446
|
+
| --- | --- | --- |
|
|
447
|
+
| `file?` | `string` | For messages (default foliwake.remote.json). |
|
|
448
|
+
| `requireApp?` | `boolean` | Refuse a file without `app` (default true; the CLI passes false). |
|
|
449
|
+
| `envNameProblem?` | `(name: string, what: string) => string \| null` | An additional rule for credential variable names (the engine CLI passes its own). |
|
|
450
|
+
|
|
451
|
+
#### `ServiceTokenAuth`
|
|
452
|
+
|
|
453
|
+
A service token: each request carries a fresh ES256 JWT for its exact path, signed with the private key in the variable `privateKeyEnv` (a JWK as JSON text). Delivery checks it with the public key of `kid`.
|
|
454
|
+
|
|
455
|
+
```ts
|
|
456
|
+
interface ServiceTokenAuth
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
| Member | Type | Description |
|
|
460
|
+
| --- | --- | --- |
|
|
461
|
+
| `type` | `'service-token'` | |
|
|
462
|
+
| `issuer` | `string` | The `iss` the delivery expects (its TOKEN_ISSUER). |
|
|
463
|
+
| `kid` | `string` | The key id the delivery looks the public key up by. |
|
|
464
|
+
| `privateKeyEnv` | `string` | The NAME of the variable holding the private key. |
|
|
465
|
+
|
|
466
|
+
**Types**
|
|
467
|
+
|
|
468
|
+
#### `RemoteAuth`
|
|
469
|
+
|
|
470
|
+
The credential of an endpoint.
|
|
471
|
+
|
|
472
|
+
```ts
|
|
473
|
+
type RemoteAuth = BasicAuth | ServiceTokenAuth
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
**Also exported** (documented where they are defined)
|
|
477
|
+
|
|
478
|
+
- from `@foliwake/blocks-contract/page`: `ClientSettings`
|
|
479
|
+
|
|
480
|
+
### `@foliwake/server/origin`
|
|
481
|
+
|
|
482
|
+
**Functions and components**
|
|
483
|
+
|
|
484
|
+
#### `createOriginClient()`
|
|
485
|
+
|
|
486
|
+
The client for the config's origins. The secret is read from the environment at each request (a rotated secret takes effect without a restart) and goes only into the Authorization header: `basic` sends the password of `passwordEnv`; `service-token` signs a fresh token for the request's exact path with the private key of `privateKeyEnv`.
|
|
487
|
+
|
|
488
|
+
```ts
|
|
489
|
+
function createOriginClient(config: FoliwakeAppConfig, env?: NodeJS.ProcessEnv, opts?: OriginClientOptions): OriginClient
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
| Parameter | Type | Description |
|
|
493
|
+
| --- | --- | --- |
|
|
494
|
+
| `config` | `FoliwakeAppConfig` | |
|
|
495
|
+
| `env?` | `NodeJS.ProcessEnv` | Where secrets are read (default `process.env`). |
|
|
496
|
+
| `opts?` | `OriginClientOptions` | |
|
|
497
|
+
|
|
498
|
+
**Returns** `OriginClient`
|
|
499
|
+
|
|
500
|
+
**Constants**
|
|
501
|
+
|
|
502
|
+
#### `SERVICE_TOKEN_LIFETIME_SECONDS`
|
|
503
|
+
|
|
504
|
+
Seconds a service token lives: `exp = iat + 60`. Delivery accepts at most 120, with 5 s of clock skew.
|
|
505
|
+
|
|
506
|
+
```ts
|
|
507
|
+
const SERVICE_TOKEN_LIFETIME_SECONDS = 60
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
**Interfaces**
|
|
511
|
+
|
|
512
|
+
#### `OriginClient`
|
|
513
|
+
|
|
514
|
+
Requests to the configured live and preview origins, with the credential added per request.
|
|
515
|
+
|
|
516
|
+
```ts
|
|
517
|
+
interface OriginClient
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
| Member | Type | Description |
|
|
521
|
+
| --- | --- | --- |
|
|
522
|
+
| `readonly config` | `FoliwakeAppConfig` | |
|
|
523
|
+
| `originOf()` | `(channel: ChannelName) => string` | The origin of a channel (for messages and loopback checks). |
|
|
524
|
+
| `request()` | `(channel: ChannelName, path: string, opts?: OriginRequestOptions) => Promise<Response>` | GET/HEAD `<origin><path>`; `redirect: 'manual'`, 10 s timeout unless `signal` is given. |
|
|
525
|
+
|
|
526
|
+
#### `OriginClientOptions`
|
|
527
|
+
|
|
528
|
+
Options of `createOriginClient`.
|
|
529
|
+
|
|
530
|
+
```ts
|
|
531
|
+
interface OriginClientOptions
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
| Member | Type | Description |
|
|
535
|
+
| --- | --- | --- |
|
|
536
|
+
| `fetch?` | `typeof fetch` | A fetch implementation (tests); default globalThis.fetch. |
|
|
537
|
+
| `timeoutMs?` | `number` | |
|
|
538
|
+
| `now?` | `() => number` | The clock of service tokens, in milliseconds (tests); default Date.now. |
|
|
539
|
+
|
|
540
|
+
#### `OriginRequestOptions`
|
|
541
|
+
|
|
542
|
+
Options of one origin request.
|
|
543
|
+
|
|
544
|
+
```ts
|
|
545
|
+
interface OriginRequestOptions
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
| Member | Type | Description |
|
|
549
|
+
| --- | --- | --- |
|
|
550
|
+
| `method?` | `'GET' \| 'HEAD'` | |
|
|
551
|
+
| `signal?` | `AbortSignal` | |
|
|
552
|
+
| `headers?` | `Record<string, string>` | Only these headers are sent (never a browser's). |
|
|
553
|
+
|
|
554
|
+
### `@foliwake/server/page`
|
|
555
|
+
|
|
556
|
+
**Functions and components**
|
|
557
|
+
|
|
558
|
+
#### `blocksUrlOf()`
|
|
559
|
+
|
|
560
|
+
The same-origin URL of the blocks module, or null (no entry, no code release, or `local` from a remote origin).
|
|
561
|
+
|
|
562
|
+
```ts
|
|
563
|
+
function blocksUrlOf(config: FoliwakeAppConfig, channel: ChannelName, codeRelease: CodeReleaseId | null): {
|
|
564
|
+
base: string;
|
|
565
|
+
url: string;
|
|
566
|
+
} | null
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
**Returns** `{ base: string; url: string; } \| null`
|
|
570
|
+
|
|
571
|
+
#### `plainPath()`
|
|
572
|
+
|
|
573
|
+
The origin URL path of a page's body (`/` → `/index.plain.html`).
|
|
574
|
+
|
|
575
|
+
```ts
|
|
576
|
+
function plainPath(pagePath: string): string
|
|
577
|
+
```
|
|
578
|
+
|
|
579
|
+
**Returns** `string`
|
|
580
|
+
|
|
581
|
+
#### `preparePage()`
|
|
582
|
+
|
|
583
|
+
Per request, after the cache: check → rewrite media/links → transform → check again → serialise.
|
|
584
|
+
|
|
585
|
+
```ts
|
|
586
|
+
function preparePage(config: FoliwakeAppConfig, envelope: PageEnvelope, opts?: PrepareOptions): FoliwakePageData
|
|
587
|
+
```
|
|
588
|
+
|
|
589
|
+
| Parameter | Type | Description |
|
|
590
|
+
| --- | --- | --- |
|
|
591
|
+
| `config` | `FoliwakeAppConfig` | |
|
|
592
|
+
| `envelope` | `PageEnvelope` | What readEnvelope (or a cached copy of it) returned. |
|
|
593
|
+
| `opts?` | `PrepareOptions` | |
|
|
594
|
+
|
|
595
|
+
**Returns** `FoliwakePageData`: The serialisable page for a client component (`preload` is empty; readPage of the Next package fills it). Throws FoliwakePageError('invalid') when the body or the transform's output is refused.
|
|
596
|
+
|
|
597
|
+
#### `readEnvelope()`
|
|
598
|
+
|
|
599
|
+
Reads `<path>.plain.html` from the channel's origin. Primitive inputs only, no callbacks and no signal (an internal 10 s timeout), so it can run inside a cache function. Refuses paths outside `app.pages`.
|
|
600
|
+
|
|
601
|
+
```ts
|
|
602
|
+
function readEnvelope(config: FoliwakeAppConfig, client: OriginClient, site: string, channel: ChannelName, path: string): Promise<PageEnvelope>
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
| Parameter | Type | Description |
|
|
606
|
+
| --- | --- | --- |
|
|
607
|
+
| `config` | `FoliwakeAppConfig` | |
|
|
608
|
+
| `client` | `OriginClient` | |
|
|
609
|
+
| `site` | `string` | The config's site (`config.site`); another site is refused. |
|
|
610
|
+
| `channel` | `ChannelName` | |
|
|
611
|
+
| `path` | `string` | The page path (`/`, `/lessons/a`). |
|
|
612
|
+
|
|
613
|
+
**Returns** `Promise<PageEnvelope>`: The envelope; throws FoliwakePageError (`not_found`, `not_allowed`, `origin`, `unauthorized`, …).
|
|
614
|
+
|
|
615
|
+
**Classes**
|
|
616
|
+
|
|
617
|
+
#### `FoliwakePageError`
|
|
618
|
+
|
|
619
|
+
A page that cannot be shown. `status`: the origin's HTTP status when there was one.
|
|
620
|
+
|
|
621
|
+
```ts
|
|
622
|
+
class FoliwakePageError extends Error
|
|
623
|
+
new FoliwakePageError(kind: PageErrorKind, message: string, opts?: {
|
|
624
|
+
status?: number;
|
|
625
|
+
cause?: unknown;
|
|
626
|
+
})
|
|
627
|
+
```
|
|
628
|
+
|
|
629
|
+
| Member | Type | Description |
|
|
630
|
+
| --- | --- | --- |
|
|
631
|
+
| `name` | `string` | |
|
|
632
|
+
| `readonly kind` | `PageErrorKind` | |
|
|
633
|
+
| `readonly status?` | `number` | |
|
|
634
|
+
|
|
635
|
+
**Interfaces**
|
|
636
|
+
|
|
637
|
+
#### `PrepareOptions`
|
|
638
|
+
|
|
639
|
+
Options of `preparePage`.
|
|
640
|
+
|
|
641
|
+
```ts
|
|
642
|
+
interface PrepareOptions
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
| Member | Type | Description |
|
|
646
|
+
| --- | --- | --- |
|
|
647
|
+
| `mode?` | `string` | The site's mode of the page (a token, `[a-z][a-z0-9-]`, at most 32 characters), given to the blocks as `host.mode`; absent → none. `exam` is refused: the assessment extension's exam needs answers stripped by the origin, which does not exist yet, so an exam body would carry its answers. |
|
|
648
|
+
| `rewriteLink?` | `(href: string) => string` | Root-relative links (`/x`) of `<a href>`; returns the app's URL. |
|
|
649
|
+
| `transform?` | `(html: string, envelope: PageEnvelope) => string` | The app's own change of the body (allow-list, artwork, …); its output is checked again. |
|
|
650
|
+
|
|
651
|
+
**Types**
|
|
652
|
+
|
|
653
|
+
#### `PageErrorKind`
|
|
654
|
+
|
|
655
|
+
Why a page cannot be shown (`FoliwakePageError.kind`).
|
|
656
|
+
|
|
657
|
+
```ts
|
|
658
|
+
type PageErrorKind = 'not_found' | 'not_allowed' | 'origin' | 'invalid' | 'unauthorized' | 'too_large'
|
|
659
|
+
```
|
|
660
|
+
|
|
661
|
+
**Also exported** (documented where they are defined)
|
|
662
|
+
|
|
663
|
+
- from `@foliwake/blocks-contract/import-map`: `pageImportMap`
|
|
664
|
+
- from `@foliwake/blocks-contract/page`: `FoliwakePageData`, `PageEnvelope`
|
|
665
|
+
|
|
666
|
+
### `@foliwake/server/handlers`
|
|
667
|
+
|
|
668
|
+
**Functions and components**
|
|
669
|
+
|
|
670
|
+
#### `codeHandler()`
|
|
671
|
+
|
|
672
|
+
`GET|HEAD <proxies.code>/[preview/]<cd>/<path inside the code release>`.
|
|
673
|
+
|
|
674
|
+
```ts
|
|
675
|
+
function codeHandler(config: FoliwakeAppConfig, client: OriginClient, opts: ProxyOptions): Handler
|
|
676
|
+
```
|
|
677
|
+
|
|
678
|
+
**Returns** `Handler`
|
|
679
|
+
|
|
680
|
+
#### `mediaHandler()`
|
|
681
|
+
|
|
682
|
+
`GET|HEAD <proxies.media>/[preview/]<sha256>.<ext>`: images only, one hashed segment.
|
|
683
|
+
|
|
684
|
+
```ts
|
|
685
|
+
function mediaHandler(config: FoliwakeAppConfig, client: OriginClient, opts: ProxyOptions): Handler
|
|
686
|
+
```
|
|
687
|
+
|
|
688
|
+
**Returns** `Handler`
|
|
689
|
+
|
|
690
|
+
**Interfaces**
|
|
691
|
+
|
|
692
|
+
#### `ProxyOptions`
|
|
693
|
+
|
|
694
|
+
Options of `codeHandler` and `mediaHandler`.
|
|
695
|
+
|
|
696
|
+
```ts
|
|
697
|
+
interface ProxyOptions
|
|
698
|
+
```
|
|
699
|
+
|
|
700
|
+
| Member | Type | Description |
|
|
701
|
+
| --- | --- | --- |
|
|
702
|
+
| `authorizePreview` | `AuthorizePreview` | The app's author check for `preview/…` requests, or `false` to answer them 404. Required. |
|
|
703
|
+
|
|
704
|
+
**Types**
|
|
705
|
+
|
|
706
|
+
#### `AuthorizePreview`
|
|
707
|
+
|
|
708
|
+
The app's author check for preview requests, or `false` (preview answers 404).
|
|
709
|
+
|
|
710
|
+
```ts
|
|
711
|
+
type AuthorizePreview = ((request: Request) => boolean | Promise<boolean>) | false
|
|
712
|
+
```
|
|
713
|
+
|
|
714
|
+
#### `Handler`
|
|
715
|
+
|
|
716
|
+
A proxy handler: the request and the path segments after the proxy prefix.
|
|
717
|
+
|
|
718
|
+
```ts
|
|
719
|
+
type Handler = (request: Request, params: {
|
|
720
|
+
path: string[];
|
|
721
|
+
}) => Promise<Response>
|
|
722
|
+
```
|
|
723
|
+
|
|
724
|
+
### `@foliwake/server/html`
|
|
725
|
+
|
|
726
|
+
**Functions and components**
|
|
727
|
+
|
|
728
|
+
#### `checkBody()`
|
|
729
|
+
|
|
730
|
+
Parses and checks a body; returns the checked tree. Throws FoliwakePageError('invalid').
|
|
731
|
+
|
|
732
|
+
```ts
|
|
733
|
+
function checkBody(html: string, pagePath?: string): HtmlNode[]
|
|
734
|
+
```
|
|
735
|
+
|
|
736
|
+
**Returns** `HtmlNode[]`
|
|
737
|
+
|
|
738
|
+
#### `checkPlainBody()`
|
|
739
|
+
|
|
740
|
+
Checks a body (the strict body check); throws FoliwakePageError('invalid').
|
|
741
|
+
|
|
742
|
+
```ts
|
|
743
|
+
function checkPlainBody(html: string, pagePath?: string): void
|
|
744
|
+
```
|
|
745
|
+
|
|
746
|
+
**Returns** `void`
|
|
747
|
+
|
|
748
|
+
#### `componentNames()`
|
|
749
|
+
|
|
750
|
+
Distinct `data-component` names, sorted.
|
|
751
|
+
|
|
752
|
+
```ts
|
|
753
|
+
function componentNames(nodes: readonly HtmlNode[]): string[]
|
|
754
|
+
```
|
|
755
|
+
|
|
756
|
+
**Returns** `string[]`
|
|
757
|
+
|
|
758
|
+
#### `elements()`
|
|
759
|
+
|
|
760
|
+
Every element below `nodes`, depth first.
|
|
761
|
+
|
|
762
|
+
```ts
|
|
763
|
+
function elements(nodes: readonly HtmlNode[]): Generator<HtmlElement>
|
|
764
|
+
```
|
|
765
|
+
|
|
766
|
+
**Returns** `Generator<HtmlElement>`
|
|
767
|
+
|
|
768
|
+
#### `parseStrict()`
|
|
769
|
+
|
|
770
|
+
Parses a body strictly. Comments are dropped; a doctype or processing instruction is an error.
|
|
771
|
+
|
|
772
|
+
```ts
|
|
773
|
+
function parseStrict(html: string): HtmlNode[]
|
|
774
|
+
```
|
|
775
|
+
|
|
776
|
+
**Returns** `HtmlNode[]`
|
|
777
|
+
|
|
778
|
+
#### `sanitizedHtml()`
|
|
779
|
+
|
|
780
|
+
The checked body, serialised from its tree.
|
|
781
|
+
|
|
782
|
+
```ts
|
|
783
|
+
function sanitizedHtml(nodes: readonly HtmlNode[]): string
|
|
784
|
+
```
|
|
785
|
+
|
|
786
|
+
**Returns** `string`
|
|
787
|
+
|
|
788
|
+
#### `serialize()`
|
|
789
|
+
|
|
790
|
+
Serialises the tree as HTML (the WHATWG fragment serialisation rules for the elements a body may hold).
|
|
791
|
+
|
|
792
|
+
```ts
|
|
793
|
+
function serialize(nodes: readonly HtmlNode[]): string
|
|
794
|
+
```
|
|
795
|
+
|
|
796
|
+
**Returns** `string`
|
|
797
|
+
|
|
798
|
+
#### `srcsetCandidates()`
|
|
799
|
+
|
|
800
|
+
Splits a srcset into candidates: the URL ends at whitespace; descriptors run to the next comma.
|
|
801
|
+
|
|
802
|
+
```ts
|
|
803
|
+
function srcsetCandidates(srcset: string): {
|
|
804
|
+
url: string;
|
|
805
|
+
descriptor: string;
|
|
806
|
+
}[]
|
|
807
|
+
```
|
|
808
|
+
|
|
809
|
+
**Returns** `{ url: string; descriptor: string; }[]`
|
|
810
|
+
|
|
811
|
+
#### `urlProblem()`
|
|
812
|
+
|
|
813
|
+
Why a URL value is not allowed for this element/attribute, or null. Values are entity-decoded already.
|
|
814
|
+
|
|
815
|
+
```ts
|
|
816
|
+
function urlProblem(element: string, attribute: string, value: string, pagePath?: string): string | null
|
|
817
|
+
```
|
|
818
|
+
|
|
819
|
+
**Returns** `string \| null`
|
|
820
|
+
|
|
821
|
+
**Classes**
|
|
822
|
+
|
|
823
|
+
#### `HtmlParseError`
|
|
824
|
+
|
|
825
|
+
A body that cannot be parsed strictly.
|
|
826
|
+
|
|
827
|
+
```ts
|
|
828
|
+
class HtmlParseError extends Error
|
|
829
|
+
```
|
|
830
|
+
|
|
831
|
+
| Member | Type | Description |
|
|
832
|
+
| --- | --- | --- |
|
|
833
|
+
| `name` | `string` | |
|
|
834
|
+
|
|
835
|
+
**Constants**
|
|
836
|
+
|
|
837
|
+
#### `COMPONENT_NAME`
|
|
838
|
+
|
|
839
|
+
A valid `data-component` name.
|
|
840
|
+
|
|
841
|
+
```ts
|
|
842
|
+
const COMPONENT_NAME: RegExp
|
|
843
|
+
```
|
|
844
|
+
|
|
845
|
+
#### `REFUSED_ELEMENTS`
|
|
846
|
+
|
|
847
|
+
Elements a body may not hold (incl. foreign content: no inline SVG or MathML in 0.1). `<form>` is allowed only as the engine writes it for the question block: an explicit `method="dialog"`, no `action`, `target` or `enctype`; no control may override the submission (`formmethod`, `formaction`, `formtarget`, `formenctype`) or associate itself with another form (`form`). Such a form closes a dialog at most; it submits nowhere.
|
|
848
|
+
|
|
849
|
+
```ts
|
|
850
|
+
const REFUSED_ELEMENTS: ReadonlySet<string>
|
|
851
|
+
```
|
|
852
|
+
|
|
853
|
+
**Types**
|
|
854
|
+
|
|
855
|
+
#### `HtmlElement`
|
|
856
|
+
|
|
857
|
+
An element of a parsed body.
|
|
858
|
+
|
|
859
|
+
```ts
|
|
860
|
+
type HtmlElement = {
|
|
861
|
+
type: 'tag';
|
|
862
|
+
name: string;
|
|
863
|
+
attribs: Record<string, string>;
|
|
864
|
+
children: HtmlNode[];
|
|
865
|
+
}
|
|
866
|
+
```
|
|
867
|
+
|
|
868
|
+
#### `HtmlNode`
|
|
869
|
+
|
|
870
|
+
A node of a parsed body.
|
|
871
|
+
|
|
872
|
+
```ts
|
|
873
|
+
type HtmlNode = HtmlText | HtmlElement
|
|
874
|
+
```
|
|
875
|
+
|
|
876
|
+
#### `HtmlText`
|
|
877
|
+
|
|
878
|
+
A text node of a parsed body.
|
|
879
|
+
|
|
880
|
+
```ts
|
|
881
|
+
type HtmlText = {
|
|
882
|
+
type: 'text';
|
|
883
|
+
data: string;
|
|
884
|
+
}
|
|
885
|
+
```
|
|
886
|
+
|
|
887
|
+
### `@foliwake/server/csp`
|
|
888
|
+
|
|
889
|
+
**Functions and components**
|
|
890
|
+
|
|
891
|
+
#### `foliwakeCsp()`
|
|
892
|
+
|
|
893
|
+
The policy as one header value.
|
|
894
|
+
|
|
895
|
+
```ts
|
|
896
|
+
function foliwakeCsp(opts: CspOptions): string
|
|
897
|
+
```
|
|
898
|
+
|
|
899
|
+
**Returns** `string`
|
|
900
|
+
|
|
901
|
+
**Constants**
|
|
902
|
+
|
|
903
|
+
#### `REACT_SHELL_SCRIPT`
|
|
904
|
+
|
|
905
|
+
React DOM 19.2's inline `requestAnimationFrame(function(){$RT=performance.now()});` in PPR static shells.
|
|
906
|
+
|
|
907
|
+
```ts
|
|
908
|
+
const REACT_SHELL_SCRIPT = "requestAnimationFrame(function(){$RT=performance.now()});"
|
|
909
|
+
```
|
|
910
|
+
|
|
911
|
+
#### `REACT_SHELL_SCRIPT_HASH`
|
|
912
|
+
|
|
913
|
+
The CSP hash source of `REACT_SHELL_SCRIPT` (used by the `nonce` strategy).
|
|
914
|
+
|
|
915
|
+
```ts
|
|
916
|
+
const REACT_SHELL_SCRIPT_HASH = "'sha256-7mu4H06fwDCjmnxxr/xNHyuQC6pLTHr4M2E4jXw5WZs='"
|
|
917
|
+
```
|
|
918
|
+
|
|
919
|
+
**Interfaces**
|
|
920
|
+
|
|
921
|
+
#### `CspOptions`
|
|
922
|
+
|
|
923
|
+
Options of `foliwakeCsp`.
|
|
924
|
+
|
|
925
|
+
```ts
|
|
926
|
+
interface CspOptions
|
|
927
|
+
```
|
|
928
|
+
|
|
929
|
+
| Member | Type | Description |
|
|
930
|
+
| --- | --- | --- |
|
|
931
|
+
| `strategy` | `'self-inline' \| 'nonce'` | `self-inline` (recommended; works with partial prerendering) or `nonce` (experimental, not recommended). |
|
|
932
|
+
| `nonce?` | `string` | Required with 'nonce'. |
|
|
933
|
+
| `mediaBase?` | `string \| null` | The media host whose pictures pages show: `app.media.base` (live pictures through a transforming host), or the site's own CDN when the site resizes images on one (`media.resize: "cdn"`, whose `cdnBase` is in every live `srcset`). An https address; only its origin (scheme and host) is added to `img-src`. |
|
|
934
|
+
| `dev?` | `boolean` | Development: adds 'unsafe-eval' (React's debugging) and websocket connections for HMR. |
|
|
935
|
+
| `reportUri?` | `string` | Where browsers report violations (`report-uri`). |
|
|
936
|
+
| `extra?` | `Partial<Record<string, string[]>>` | More sources per directive, e.g. { 'connect-src': ['https://api.example.org'] }. |
|
|
937
|
+
|
|
938
|
+
### `@foliwake/server/preload`
|
|
939
|
+
|
|
940
|
+
**Functions and components**
|
|
941
|
+
|
|
942
|
+
#### `clearPreloadCache()`
|
|
943
|
+
|
|
944
|
+
Forgets every remembered code file (tests).
|
|
945
|
+
|
|
946
|
+
```ts
|
|
947
|
+
function clearPreloadCache(): void
|
|
948
|
+
```
|
|
949
|
+
|
|
950
|
+
**Returns** `void`
|
|
951
|
+
|
|
952
|
+
#### `codePath()`
|
|
953
|
+
|
|
954
|
+
A path inside the code release the proxy would serve, or null.
|
|
955
|
+
|
|
956
|
+
```ts
|
|
957
|
+
function codePath(relative: string): string | null
|
|
958
|
+
```
|
|
959
|
+
|
|
960
|
+
**Returns** `string \| null`
|
|
961
|
+
|
|
962
|
+
#### `preloadHints()`
|
|
963
|
+
|
|
964
|
+
Hints for a prepared page: modulepreload for its modules, preload as=style for its block stylesheets.
|
|
965
|
+
|
|
966
|
+
```ts
|
|
967
|
+
function preloadHints(config: FoliwakeAppConfig, client: OriginClient, page: FoliwakePageData): Promise<PreloadHint[]>
|
|
968
|
+
```
|
|
969
|
+
|
|
970
|
+
**Returns** `Promise<PreloadHint[]>`
|
|
971
|
+
|
|
972
|
+
#### `resolveInRelease()`
|
|
973
|
+
|
|
974
|
+
Resolves `specifier` against the module at `from` (both inside the release), or null when it leaves it.
|
|
975
|
+
|
|
976
|
+
```ts
|
|
977
|
+
function resolveInRelease(from: string, specifier: string): string | null
|
|
978
|
+
```
|
|
979
|
+
|
|
980
|
+
**Returns** `string \| null`
|
|
981
|
+
|
|
982
|
+
#### `staticImports()`
|
|
983
|
+
|
|
984
|
+
The relative static import specifiers of an ES module's text, and the kit's (KIT_SPECIFIERS).
|
|
985
|
+
|
|
986
|
+
```ts
|
|
987
|
+
function staticImports(source: string): string[]
|
|
988
|
+
```
|
|
989
|
+
|
|
990
|
+
**Returns** `string[]`
|
|
991
|
+
|
|
992
|
+
**Also exported** (documented where they are defined)
|
|
993
|
+
|
|
994
|
+
- from `@foliwake/blocks-contract/page`: `PreloadHint`
|
|
995
|
+
|
|
996
|
+
### `@foliwake/server/schemas/remote-2.schema.json`
|
|
997
|
+
|
|
998
|
+
A file, not a module: `schemas/remote-2.schema.json` in the package.
|
|
999
|
+
|
|
1000
|
+
<!-- apistop -->
|