@foliwake/server 0.8.0 → 0.10.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 +60 -0
- package/README.md +589 -7
- package/dist/blocks-header.d.ts +5 -0
- package/dist/blocks-header.js +47 -0
- package/dist/config-checks.d.ts +9 -0
- package/dist/config-checks.js +53 -0
- package/dist/config-parts.d.ts +50 -0
- package/dist/config-parts.js +190 -0
- package/dist/config-v3.d.ts +22 -0
- package/dist/config-v3.js +104 -0
- package/dist/config.d.ts +11 -11
- package/dist/config.js +7 -222
- package/dist/discovery.d.ts +48 -0
- package/dist/discovery.js +150 -0
- package/dist/handler.d.ts +46 -0
- package/dist/handler.js +153 -0
- package/dist/hook-endpoint.d.ts +35 -0
- package/dist/hook-endpoint.js +143 -0
- package/dist/hook.d.ts +91 -0
- package/dist/hook.js +188 -0
- package/dist/page-title.d.ts +3 -0
- package/dist/page-title.js +16 -0
- package/dist/page.d.ts +12 -2
- package/dist/page.js +32 -12
- package/dist/preload.d.ts +1 -1
- package/dist/preload.js +4 -4
- package/dist/resolve.d.ts +31 -0
- package/dist/resolve.js +76 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +3 -0
- package/package.json +25 -3
- package/schemas/remote-3.schema.json +204 -0
package/README.md
CHANGED
|
@@ -18,7 +18,13 @@ directly with another server framework (a Vite or Remix server, Hono, Express wi
|
|
|
18
18
|
| `@foliwake/server/html` | the strict body check: `checkPlainBody`, `checkBody`, `serialize`, `srcsetCandidates` |
|
|
19
19
|
| `@foliwake/server/csp` | `foliwakeCsp` |
|
|
20
20
|
| `@foliwake/server/preload` | `preloadHints`: modulepreload and stylesheet hints for a page's blocks |
|
|
21
|
-
| `@foliwake/server/
|
|
21
|
+
| `@foliwake/server/hook` | `verifyFoliwakeSignature`, `parseFoliwakeHook`: the signed hook a Foliwake server sends after a change |
|
|
22
|
+
| `@foliwake/server/handler` | `createFoliwakeHandler`: every Foliwake route of an app (code, media, hook, health) as one `(request) => Promise<Response>` |
|
|
23
|
+
| `@foliwake/server/hook-endpoint` | `createHookEndpoint`: the hook endpoint without a framework (verify, parse, expire, answer) |
|
|
24
|
+
| `@foliwake/server/discovery` | `fetchDiscovery`, `parseDiscovery`: the site's `/.well-known/foliwake.json` |
|
|
25
|
+
| `@foliwake/server/resolve` | `loadAppConfig`, `readConfigSnapshot`, `configFromSnapshot`: `FOLIWAKE_URL` + discovery + the optional version 3 file, or a version 2 file |
|
|
26
|
+
| `@foliwake/server/schemas/remote-2.schema.json` | the JSON Schema of the config file (version 2) |
|
|
27
|
+
| `@foliwake/server/schemas/remote-3.schema.json` | the JSON Schema of the optional version 3 file (overrides) |
|
|
22
28
|
|
|
23
29
|
## Install
|
|
24
30
|
|
|
@@ -37,7 +43,7 @@ The config file sits in the app's root and holds no secrets, only the names of t
|
|
|
37
43
|
"version": 2,
|
|
38
44
|
"site": "mysite",
|
|
39
45
|
"live": { "url": "https://mysite.example.org" },
|
|
40
|
-
"preview": { "url": "https://preview.mysite.example.org", "auth": { "type": "basic", "username": "
|
|
46
|
+
"preview": { "url": "https://preview.mysite.example.org", "auth": { "type": "basic", "username": "preview", "passwordEnv": "MYSITE_PREVIEW_TOKEN" } },
|
|
41
47
|
"app": {
|
|
42
48
|
"pages": ["/**"],
|
|
43
49
|
"proxies": { "code": "/foliwake-code", "media": "/foliwake-media", "pages": null },
|
|
@@ -119,6 +125,36 @@ export async function pageEndpoint(request: Request): Promise<Response> {
|
|
|
119
125
|
}
|
|
120
126
|
```
|
|
121
127
|
|
|
128
|
+
### Refresh the cache when the site changes
|
|
129
|
+
|
|
130
|
+
A Foliwake server whose site lists your app as a hook target POSTs `delivery.settled` once a change is live
|
|
131
|
+
and the caches in front of the origin serve it. The body names the live `generation`, and either the changed
|
|
132
|
+
paths (`pages`, at most 100: pages and other files such as an index) or `all: true`. Check the signature on
|
|
133
|
+
the raw body, then drop what you cached:
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
import { parseFoliwakeHook, verifyFoliwakeSignature } from '@foliwake/server/hook';
|
|
137
|
+
|
|
138
|
+
const cached = new Map<string, unknown>();
|
|
139
|
+
|
|
140
|
+
/** POST /api/webhooks/foliwake */
|
|
141
|
+
export async function hookEndpoint(request: Request): Promise<Response> {
|
|
142
|
+
const body = new Uint8Array(await request.arrayBuffer());
|
|
143
|
+
const signature = request.headers.get('foliwake-signature');
|
|
144
|
+
const secrets = [process.env.FOLIWAKE_HOOK_SECRET, process.env.FOLIWAKE_HOOK_SECRET_PREVIOUS];
|
|
145
|
+
if (!verifyFoliwakeSignature(body, signature, secrets).ok) return new Response(null, { status: 401 });
|
|
146
|
+
const payload = parseFoliwakeHook(body);
|
|
147
|
+
if (payload.event === 'ping') return Response.json({ ok: true, site: payload.site });
|
|
148
|
+
if (payload.all) cached.clear();
|
|
149
|
+
else for (const path of payload.pages ?? []) cached.delete(path);
|
|
150
|
+
return Response.json({ ok: true });
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
The signature is `t=<unix seconds>,v1=<hex HMAC-SHA256(secret, "<t>.<raw body>")>`, accepted within 300 s of
|
|
155
|
+
the app's clock. Answer 2xx only once the change is handled: anything else is sent again. Limit the body to
|
|
156
|
+
64 KiB (`HOOK_BODY_LIMIT`) and check that `payload.site` is yours.
|
|
157
|
+
|
|
122
158
|
### Code and media proxies
|
|
123
159
|
|
|
124
160
|
Mount the handlers under the prefixes of `app.proxies` and pass them the path segments after the prefix. They
|
|
@@ -189,6 +225,8 @@ Changes per version, breaking ones marked: `CHANGELOG.md` in the package
|
|
|
189
225
|
- Credentials never appear in messages; a missing or malformed password or key variable is a
|
|
190
226
|
`FoliwakeConfigError` that names the variable, not its value. The secret is read at each request, so a
|
|
191
227
|
rotated secret works without a restart. A private key whose own `kid` differs from the config's is refused.
|
|
228
|
+
- Rotating the hook secret: give the app the new secret as the previous one too (both accepted), switch the
|
|
229
|
+
server to it, then drop the old one.
|
|
192
230
|
- Rotating a service-token key: add the new public key to the delivery's key set first (both `kid`s listed),
|
|
193
231
|
then switch the app's `kid` and private key, then remove the old public key.
|
|
194
232
|
- `preparePage` leaves `page.preload` empty; `preloadHints` fills it from the release's
|
|
@@ -391,7 +429,7 @@ interface BasicAuth
|
|
|
391
429
|
|
|
392
430
|
#### `FoliwakeAppConfig`
|
|
393
431
|
|
|
394
|
-
A
|
|
432
|
+
A validated app config: a `foliwake.remote.json` version 2, or (version 3) the site's discovery document merged with the optional version 3 file. It holds credential variable names, never secrets.
|
|
395
433
|
|
|
396
434
|
```ts
|
|
397
435
|
interface FoliwakeAppConfig
|
|
@@ -399,7 +437,7 @@ interface FoliwakeAppConfig
|
|
|
399
437
|
|
|
400
438
|
| Member | Type | Description |
|
|
401
439
|
| --- | --- | --- |
|
|
402
|
-
| `version` | `2` | |
|
|
440
|
+
| `version` | `2 \| 3` | |
|
|
403
441
|
| `site` | `string` | |
|
|
404
442
|
| `live` | `FoliwakeEndpoint \| null` | |
|
|
405
443
|
| `preview` | `FoliwakeEndpoint \| null` | |
|
|
@@ -418,7 +456,7 @@ interface FoliwakeAppSettings
|
|
|
418
456
|
| --- | --- | --- |
|
|
419
457
|
| `pages` | `readonly string[]` | Page globs the app may read. |
|
|
420
458
|
| `proxies` | `{ code: string; media: string; pages: null; }` | App-origin URL prefixes of the proxies. |
|
|
421
|
-
| `blocks` | `{ entry: string \| null; contract: string; major: number; eventPrefix: string; }` | Where `mountBlocks` lives in a code release; null: bodies stay static. |
|
|
459
|
+
| `blocks` | `{ entry: string \| null; contract: string; major: number; eventPrefix: string; fromRelease: boolean; }` | Where `mountBlocks` lives in a code release; null: bodies stay static. `fromRelease`: the config names no blocks (version 3), so each page's code release names them (`X-Foliwake-Blocks`); false for version 2. |
|
|
422
460
|
| `media` | `{ base: string; transform: 'cloudflare-images' \| null; } \| null` | Live media through a transforming host; null: media through the proxy. |
|
|
423
461
|
| `revalidate` | `number` | Seconds before a cached envelope is stale (stale-while-revalidate). |
|
|
424
462
|
|
|
@@ -558,17 +596,38 @@ interface OriginRequestOptions
|
|
|
558
596
|
|
|
559
597
|
#### `blocksUrlOf()`
|
|
560
598
|
|
|
561
|
-
The same-origin URL of the blocks module, or null
|
|
599
|
+
The same-origin URL of the blocks module, or null without an entry.
|
|
562
600
|
|
|
563
601
|
```ts
|
|
564
|
-
function blocksUrlOf(config: FoliwakeAppConfig, channel: ChannelName, codeRelease: CodeReleaseId): {
|
|
602
|
+
function blocksUrlOf(config: FoliwakeAppConfig, channel: ChannelName, codeRelease: CodeReleaseId, entry?: string | null): {
|
|
565
603
|
base: string;
|
|
566
604
|
url: string;
|
|
567
605
|
} | null
|
|
568
606
|
```
|
|
569
607
|
|
|
608
|
+
| Parameter | Type | Description |
|
|
609
|
+
| --- | --- | --- |
|
|
610
|
+
| `config` | `FoliwakeAppConfig` | |
|
|
611
|
+
| `channel` | `ChannelName` | |
|
|
612
|
+
| `codeRelease` | `CodeReleaseId` | |
|
|
613
|
+
| `entry?` | `string \| null` | The module inside the code release; default the config's `app.blocks.entry`. |
|
|
614
|
+
|
|
570
615
|
**Returns** `{ base: string; url: string; } \| null`
|
|
571
616
|
|
|
617
|
+
#### `pageBlocksOf()`
|
|
618
|
+
|
|
619
|
+
The blocks a page mounts: the config's, or (version 3 without `app.blocks`) the ones its code release names.
|
|
620
|
+
|
|
621
|
+
```ts
|
|
622
|
+
function pageBlocksOf(config: FoliwakeAppConfig, envelope: PageEnvelope): {
|
|
623
|
+
entry: string | null;
|
|
624
|
+
major: number;
|
|
625
|
+
eventPrefix: string;
|
|
626
|
+
}
|
|
627
|
+
```
|
|
628
|
+
|
|
629
|
+
**Returns** `{ entry: string \| null; major: number; eventPrefix: string; }`
|
|
630
|
+
|
|
572
631
|
#### `plainPath()`
|
|
573
632
|
|
|
574
633
|
The origin URL path of a page's body (`/` → `/index.plain.html`).
|
|
@@ -994,8 +1053,531 @@ function staticImports(source: string): string[]
|
|
|
994
1053
|
|
|
995
1054
|
- from `@foliwake/blocks-contract/page`: `PreloadHint`
|
|
996
1055
|
|
|
1056
|
+
### `@foliwake/server/hook`
|
|
1057
|
+
|
|
1058
|
+
**Functions and components**
|
|
1059
|
+
|
|
1060
|
+
#### `isFoliwakePagePath()`
|
|
1061
|
+
|
|
1062
|
+
Whether a path of `pages` is a page (`/lessons/a`), not another file such as `/lessons/query-index.json`.
|
|
1063
|
+
|
|
1064
|
+
```ts
|
|
1065
|
+
function isFoliwakePagePath(path: string): boolean
|
|
1066
|
+
```
|
|
1067
|
+
|
|
1068
|
+
**Returns** `boolean`
|
|
1069
|
+
|
|
1070
|
+
#### `parseFoliwakeHook()`
|
|
1071
|
+
|
|
1072
|
+
Parses a hook body (after its signature was checked). Unknown fields are ignored; an unknown operation kind is accepted, so a newer server's operations still refresh the app.
|
|
1073
|
+
|
|
1074
|
+
```ts
|
|
1075
|
+
function parseFoliwakeHook(body: Body): FoliwakeHookPayload
|
|
1076
|
+
```
|
|
1077
|
+
|
|
1078
|
+
| Parameter | Type | Description |
|
|
1079
|
+
| --- | --- | --- |
|
|
1080
|
+
| `body` | `Body` | The raw body. |
|
|
1081
|
+
|
|
1082
|
+
**Returns** `FoliwakeHookPayload`: The payload; throws FoliwakeHookError when it is not a version 1 `delivery.settled` or `ping`.
|
|
1083
|
+
|
|
1084
|
+
#### `signFoliwakeHook()`
|
|
1085
|
+
|
|
1086
|
+
The `Foliwake-Signature` value for a body: what a Foliwake server sends; useful in an app's tests.
|
|
1087
|
+
|
|
1088
|
+
```ts
|
|
1089
|
+
function signFoliwakeHook(body: Body, secret: string, t?: number): string
|
|
1090
|
+
```
|
|
1091
|
+
|
|
1092
|
+
| Parameter | Type | Description |
|
|
1093
|
+
| --- | --- | --- |
|
|
1094
|
+
| `body` | `Body` | The raw body, byte for byte as sent. |
|
|
1095
|
+
| `secret` | `string` | The shared secret. |
|
|
1096
|
+
| `t?` | `number` | The signature time in unix seconds (default: now). |
|
|
1097
|
+
|
|
1098
|
+
**Returns** `string`
|
|
1099
|
+
|
|
1100
|
+
#### `verifyFoliwakeSignature()`
|
|
1101
|
+
|
|
1102
|
+
Checks `Foliwake-Signature` against the raw body. Every secret is tried (the current one and, while it is rotated, the previous one); the comparison is constant-time.
|
|
1103
|
+
|
|
1104
|
+
```ts
|
|
1105
|
+
function verifyFoliwakeSignature(body: Body, header: string | null, secrets: readonly (string | null | undefined)[], options?: VerifySignatureOptions): SignatureCheck
|
|
1106
|
+
```
|
|
1107
|
+
|
|
1108
|
+
| Parameter | Type | Description |
|
|
1109
|
+
| --- | --- | --- |
|
|
1110
|
+
| `body` | `Body` | The raw request body, byte for byte as received. |
|
|
1111
|
+
| `header` | `string \| null` | The `Foliwake-Signature` header, or null. |
|
|
1112
|
+
| `secrets` | `readonly (string \| null \| undefined)[]` | The accepted secrets; empty or blank ones are ignored. |
|
|
1113
|
+
| `options?` | `VerifySignatureOptions` | `now` (ms) and `toleranceS` (default 300). |
|
|
1114
|
+
|
|
1115
|
+
**Returns** `SignatureCheck`: `{ ok: true }`, or `{ ok: false, reason }`: `malformed`, `expired`, `mismatch` or `no_secret`.
|
|
1116
|
+
|
|
1117
|
+
**Classes**
|
|
1118
|
+
|
|
1119
|
+
#### `FoliwakeHookError`
|
|
1120
|
+
|
|
1121
|
+
A hook body that is not a valid payload.
|
|
1122
|
+
|
|
1123
|
+
```ts
|
|
1124
|
+
class FoliwakeHookError extends Error
|
|
1125
|
+
```
|
|
1126
|
+
|
|
1127
|
+
| Member | Type | Description |
|
|
1128
|
+
| --- | --- | --- |
|
|
1129
|
+
| `name` | `string` | |
|
|
1130
|
+
|
|
1131
|
+
**Constants**
|
|
1132
|
+
|
|
1133
|
+
#### `HOOK_BODY_LIMIT`
|
|
1134
|
+
|
|
1135
|
+
The largest hook body an app accepts, in bytes.
|
|
1136
|
+
|
|
1137
|
+
```ts
|
|
1138
|
+
const HOOK_BODY_LIMIT: number
|
|
1139
|
+
```
|
|
1140
|
+
|
|
1141
|
+
#### `HOOK_PAGES_LIMIT`
|
|
1142
|
+
|
|
1143
|
+
The most `pages` a delivery lists; a larger change is sent as `all: true`.
|
|
1144
|
+
|
|
1145
|
+
```ts
|
|
1146
|
+
const HOOK_PAGES_LIMIT = 100
|
|
1147
|
+
```
|
|
1148
|
+
|
|
1149
|
+
#### `HOOK_TOLERANCE_SECONDS`
|
|
1150
|
+
|
|
1151
|
+
How far the signature time may be from the app's clock by default, in seconds.
|
|
1152
|
+
|
|
1153
|
+
```ts
|
|
1154
|
+
const HOOK_TOLERANCE_SECONDS = 300
|
|
1155
|
+
```
|
|
1156
|
+
|
|
1157
|
+
**Interfaces**
|
|
1158
|
+
|
|
1159
|
+
#### `FoliwakeDeliveryPayload`
|
|
1160
|
+
|
|
1161
|
+
`delivery.settled`: the live site is at `generation`, and the caches in front of the origin serve it.
|
|
1162
|
+
|
|
1163
|
+
```ts
|
|
1164
|
+
interface FoliwakeDeliveryPayload
|
|
1165
|
+
```
|
|
1166
|
+
|
|
1167
|
+
| Member | Type | Description |
|
|
1168
|
+
| --- | --- | --- |
|
|
1169
|
+
| `version` | `1` | |
|
|
1170
|
+
| `event` | `'delivery.settled'` | |
|
|
1171
|
+
| `site` | `string` | |
|
|
1172
|
+
| `generation` | `number` | The live generation; it only grows, except that `init` starts a new history. |
|
|
1173
|
+
| `release` | `string \| null` | The site release (`sr_…`), or null. |
|
|
1174
|
+
| `codeRelease` | `string \| null` | The code release (`cd_…`), or null. |
|
|
1175
|
+
| `operation` | `{ id: string; kind: FoliwakeOperationKind \| (string & {}); }` | |
|
|
1176
|
+
| `pages?` | `string[]` | The changed paths (pages, indexes); present only when `all` is false. A hint, at most 100. |
|
|
1177
|
+
| `all` | `boolean` | Every live page may have changed. |
|
|
1178
|
+
| `verified` | `boolean` | Whether the server read the new generation back through its own cache before sending. |
|
|
1179
|
+
| `sentAt` | `string` | |
|
|
1180
|
+
|
|
1181
|
+
#### `FoliwakePingPayload`
|
|
1182
|
+
|
|
1183
|
+
`ping`: the server checks the endpoint and its secret; the app changes nothing.
|
|
1184
|
+
|
|
1185
|
+
```ts
|
|
1186
|
+
interface FoliwakePingPayload
|
|
1187
|
+
```
|
|
1188
|
+
|
|
1189
|
+
| Member | Type | Description |
|
|
1190
|
+
| --- | --- | --- |
|
|
1191
|
+
| `version` | `1` | |
|
|
1192
|
+
| `event` | `'ping'` | |
|
|
1193
|
+
| `site` | `string` | |
|
|
1194
|
+
| `sentAt` | `string` | |
|
|
1195
|
+
|
|
1196
|
+
#### `VerifySignatureOptions`
|
|
1197
|
+
|
|
1198
|
+
Options of `verifyFoliwakeSignature`.
|
|
1199
|
+
|
|
1200
|
+
```ts
|
|
1201
|
+
interface VerifySignatureOptions
|
|
1202
|
+
```
|
|
1203
|
+
|
|
1204
|
+
| Member | Type | Description |
|
|
1205
|
+
| --- | --- | --- |
|
|
1206
|
+
| `now?` | `number` | The app's clock, in milliseconds since the epoch (default `Date.now()`). |
|
|
1207
|
+
| `toleranceS?` | `number` | The allowed distance between the signature time and `now`, in seconds (default 300). |
|
|
1208
|
+
|
|
1209
|
+
**Types**
|
|
1210
|
+
|
|
1211
|
+
#### `FoliwakeHookPayload`
|
|
1212
|
+
|
|
1213
|
+
Any hook body.
|
|
1214
|
+
|
|
1215
|
+
```ts
|
|
1216
|
+
type FoliwakeHookPayload = FoliwakeDeliveryPayload | FoliwakePingPayload
|
|
1217
|
+
```
|
|
1218
|
+
|
|
1219
|
+
#### `FoliwakeOperationKind`
|
|
1220
|
+
|
|
1221
|
+
The operations a delivery reports; a newer server may send others, which an app treats like any change.
|
|
1222
|
+
|
|
1223
|
+
```ts
|
|
1224
|
+
type FoliwakeOperationKind = 'content_publish' | 'unpublish' | 'rollback_full' | 'code_release' | 'rollback_code' | 'site_migration' | 'recovery_purge' | 'engine_update' | 'init'
|
|
1225
|
+
```
|
|
1226
|
+
|
|
1227
|
+
#### `SignatureCheck`
|
|
1228
|
+
|
|
1229
|
+
The result of `verifyFoliwakeSignature`.
|
|
1230
|
+
|
|
1231
|
+
```ts
|
|
1232
|
+
type SignatureCheck = {
|
|
1233
|
+
ok: true;
|
|
1234
|
+
} | {
|
|
1235
|
+
ok: false;
|
|
1236
|
+
reason: SignatureFailure;
|
|
1237
|
+
}
|
|
1238
|
+
```
|
|
1239
|
+
|
|
1240
|
+
#### `SignatureFailure`
|
|
1241
|
+
|
|
1242
|
+
Why a signature was refused.
|
|
1243
|
+
|
|
1244
|
+
```ts
|
|
1245
|
+
type SignatureFailure = 'malformed' | 'expired' | 'mismatch' | 'no_secret'
|
|
1246
|
+
```
|
|
1247
|
+
|
|
1248
|
+
### `@foliwake/server/handler`
|
|
1249
|
+
|
|
1250
|
+
**Functions and components**
|
|
1251
|
+
|
|
1252
|
+
#### `createFoliwakeHandler()`
|
|
1253
|
+
|
|
1254
|
+
The Foliwake routes of an app as one `(request) => Promise<Response>`. The config is read on the first request (FOLIWAKE_URL and its discovery document, unless `config` is given) and kept; a failed read is tried again on the next request and answers 503 meanwhile.
|
|
1255
|
+
|
|
1256
|
+
```ts
|
|
1257
|
+
function createFoliwakeHandler(opts?: FoliwakeHandlerOptions): FoliwakeFetchHandler
|
|
1258
|
+
```
|
|
1259
|
+
|
|
1260
|
+
| Parameter | Type | Description |
|
|
1261
|
+
| --- | --- | --- |
|
|
1262
|
+
| `opts?` | `FoliwakeHandlerOptions` | `config`, `base`, `authorizePreview` (default false), `hook`, `env`, `fetch`, `log`. |
|
|
1263
|
+
|
|
1264
|
+
**Returns** `FoliwakeFetchHandler`
|
|
1265
|
+
|
|
1266
|
+
**Constants**
|
|
1267
|
+
|
|
1268
|
+
#### `HANDLER_BASE`
|
|
1269
|
+
|
|
1270
|
+
Where the handler is mounted by default: `/foliwake/hook`, `/foliwake/health`.
|
|
1271
|
+
|
|
1272
|
+
```ts
|
|
1273
|
+
const HANDLER_BASE = "/foliwake"
|
|
1274
|
+
```
|
|
1275
|
+
|
|
1276
|
+
#### `HOOK_SECRET_ENV`
|
|
1277
|
+
|
|
1278
|
+
The variable the hook's shared secret is read from by default.
|
|
1279
|
+
|
|
1280
|
+
```ts
|
|
1281
|
+
const HOOK_SECRET_ENV = "FOLIWAKE_HOOK_SECRET"
|
|
1282
|
+
```
|
|
1283
|
+
|
|
1284
|
+
**Interfaces**
|
|
1285
|
+
|
|
1286
|
+
#### `FoliwakeHandlerOptions`
|
|
1287
|
+
|
|
1288
|
+
Options of `createFoliwakeHandler`.
|
|
1289
|
+
|
|
1290
|
+
```ts
|
|
1291
|
+
interface FoliwakeHandlerOptions
|
|
1292
|
+
```
|
|
1293
|
+
|
|
1294
|
+
| Member | Type | Description |
|
|
1295
|
+
| --- | --- | --- |
|
|
1296
|
+
| `config?` | `FoliwakeAppConfig \| (() => FoliwakeAppConfig \| Promise<FoliwakeAppConfig>)` | The app config, or how to get it; default loadAppConfig() (FOLIWAKE_URL + the optional file), once. |
|
|
1297
|
+
| `base?` | `string` | The app path of the hook and health routes; default `/foliwake`. |
|
|
1298
|
+
| `authorizePreview?` | `AuthorizePreview` | The app's author check for preview files; default `false` (preview answers 404). |
|
|
1299
|
+
| `hook?` | `false \| FoliwakeHookOptions` | The hook endpoint's options; `false` answers 404 on `<base>/hook`. |
|
|
1300
|
+
| `env?` | `NodeJS.ProcessEnv` | Where secrets and FOLIWAKE_URL are read; default process.env. |
|
|
1301
|
+
| `fetch?` | `typeof fetch` | A fetch implementation (tests); default globalThis.fetch. |
|
|
1302
|
+
| `log?` | `Pick<Console, 'warn' \| 'error'>` | |
|
|
1303
|
+
|
|
1304
|
+
#### `FoliwakeHookOptions`
|
|
1305
|
+
|
|
1306
|
+
The hook endpoint of the handler.
|
|
1307
|
+
|
|
1308
|
+
```ts
|
|
1309
|
+
interface FoliwakeHookOptions
|
|
1310
|
+
```
|
|
1311
|
+
|
|
1312
|
+
| Member | Type | Description |
|
|
1313
|
+
| --- | --- | --- |
|
|
1314
|
+
| `secretEnv?` | `string` | The variable holding the shared secret; default FOLIWAKE_HOOK_SECRET. |
|
|
1315
|
+
| `previousSecretEnv?` | `string` | While the secret is rotated: the variable holding the previous one, also accepted. |
|
|
1316
|
+
| `onDelivery?` | `(payload: FoliwakeDeliveryPayload) => void \| Promise<void>` | Called for every delivery (expire your own caches here); a throw answers 500 and the server retries. |
|
|
1317
|
+
| `toleranceS?` | `number` | The allowed clock distance of the signature, in seconds; default 300. |
|
|
1318
|
+
| `endpoint?` | `(request: Request) => Promise<Response>` | Replaces the endpoint (a framework adapter's own, such as Next's tag expiry). |
|
|
1319
|
+
|
|
1320
|
+
**Types**
|
|
1321
|
+
|
|
1322
|
+
#### `FoliwakeFetchHandler`
|
|
1323
|
+
|
|
1324
|
+
A request handler on the Web Fetch API.
|
|
1325
|
+
|
|
1326
|
+
```ts
|
|
1327
|
+
type FoliwakeFetchHandler = (request: Request) => Promise<Response>
|
|
1328
|
+
```
|
|
1329
|
+
|
|
1330
|
+
### `@foliwake/server/hook-endpoint`
|
|
1331
|
+
|
|
1332
|
+
**Functions and components**
|
|
1333
|
+
|
|
1334
|
+
#### `createHookEndpoint()`
|
|
1335
|
+
|
|
1336
|
+
The hook endpoint as a function of a Request: 413 over 64 KiB, 401 on a bad or expired signature, 400 on a body that is not a payload or names another site, 200 otherwise.
|
|
1337
|
+
|
|
1338
|
+
```ts
|
|
1339
|
+
function createHookEndpoint(deps: HookEndpointDeps): (request: Request) => Promise<Response>
|
|
1340
|
+
```
|
|
1341
|
+
|
|
1342
|
+
**Returns** `(request: Request) => Promise<Response>`
|
|
1343
|
+
|
|
1344
|
+
#### `deliveryTags()`
|
|
1345
|
+
|
|
1346
|
+
The tags a delivery expires: the site's live tag on `all`, else one tag per listed path.
|
|
1347
|
+
|
|
1348
|
+
```ts
|
|
1349
|
+
function deliveryTags(payload: FoliwakeDeliveryPayload, liveTag: (path?: string) => string): string[]
|
|
1350
|
+
```
|
|
1351
|
+
|
|
1352
|
+
**Returns** `string[]`
|
|
1353
|
+
|
|
1354
|
+
#### `warmPaths()`
|
|
1355
|
+
|
|
1356
|
+
The pages read again after a delivery: none on `all`, page paths only, at most WARM_LIMIT.
|
|
1357
|
+
|
|
1358
|
+
```ts
|
|
1359
|
+
function warmPaths(payload: FoliwakeDeliveryPayload): string[]
|
|
1360
|
+
```
|
|
1361
|
+
|
|
1362
|
+
**Returns** `string[]`
|
|
1363
|
+
|
|
1364
|
+
**Constants**
|
|
1365
|
+
|
|
1366
|
+
#### `WARM_LIMIT`
|
|
1367
|
+
|
|
1368
|
+
At most this many pages are read again right after a delivery (`warm`); never on `all`.
|
|
1369
|
+
|
|
1370
|
+
```ts
|
|
1371
|
+
const WARM_LIMIT = 20
|
|
1372
|
+
```
|
|
1373
|
+
|
|
1374
|
+
**Interfaces**
|
|
1375
|
+
|
|
1376
|
+
#### `HookEndpointDeps`
|
|
1377
|
+
|
|
1378
|
+
What the endpoint needs from the framework and from the app.
|
|
1379
|
+
|
|
1380
|
+
```ts
|
|
1381
|
+
interface HookEndpointDeps
|
|
1382
|
+
```
|
|
1383
|
+
|
|
1384
|
+
| Member | Type | Description |
|
|
1385
|
+
| --- | --- | --- |
|
|
1386
|
+
| `site` | `string` | The configured site. |
|
|
1387
|
+
| `sdk` | `string` | What a `ping` answers as `sdk`: the package and its version (`@foliwake/next/0.9.0`). |
|
|
1388
|
+
| `secrets()` | `() => (string \| undefined)[]` | The accepted secrets, read per request (current, then previous). |
|
|
1389
|
+
| `liveTag()` | `(path?: string) => string` | The live tag of the site (no path) or of one path. |
|
|
1390
|
+
| `expire()` | `(tag: string) => void` | Expires one tag (revalidateTag with the chosen profile). |
|
|
1391
|
+
| `warm()` | `(path: string) => Promise<unknown>` | Reads one page into the cache again. |
|
|
1392
|
+
| `schedule()` | `(task: () => Promise<void>) => void` | Runs work after the response is sent. |
|
|
1393
|
+
| `warmPages` | `boolean` | Whether pages are read again after a delivery. |
|
|
1394
|
+
| `onDelivery?` | `(payload: FoliwakeDeliveryPayload) => void \| Promise<void>` | |
|
|
1395
|
+
| `toleranceS?` | `number` | |
|
|
1396
|
+
| `now?` | `() => number` | |
|
|
1397
|
+
| `log` | `Pick<Console, 'warn' \| 'error'>` | |
|
|
1398
|
+
|
|
1399
|
+
### `@foliwake/server/discovery`
|
|
1400
|
+
|
|
1401
|
+
**Functions and components**
|
|
1402
|
+
|
|
1403
|
+
#### `fetchDiscovery()`
|
|
1404
|
+
|
|
1405
|
+
Reads and validates the discovery document of the site at `siteUrl` (FOLIWAKE_URL: https, or http for loopback hosts; origin only). 5 s per attempt, one retry, no redirects, at most 16 KiB of JSON.
|
|
1406
|
+
|
|
1407
|
+
```ts
|
|
1408
|
+
function fetchDiscovery(siteUrl: string, opts?: FetchDiscoveryOptions): Promise<FoliwakeDiscovery>
|
|
1409
|
+
```
|
|
1410
|
+
|
|
1411
|
+
**Returns** `Promise<FoliwakeDiscovery>`
|
|
1412
|
+
|
|
1413
|
+
#### `parseDiscovery()`
|
|
1414
|
+
|
|
1415
|
+
Validates a discovery document read from `origin` (the scheme, host and port of FOLIWAKE_URL). Throws FoliwakeConfigError naming the field.
|
|
1416
|
+
|
|
1417
|
+
```ts
|
|
1418
|
+
function parseDiscovery(value: unknown, origin: string): FoliwakeDiscovery
|
|
1419
|
+
```
|
|
1420
|
+
|
|
1421
|
+
**Returns** `FoliwakeDiscovery`
|
|
1422
|
+
|
|
1423
|
+
**Constants**
|
|
1424
|
+
|
|
1425
|
+
#### `DISCOVERY_LIMIT`
|
|
1426
|
+
|
|
1427
|
+
The largest discovery document read, in bytes.
|
|
1428
|
+
|
|
1429
|
+
```ts
|
|
1430
|
+
const DISCOVERY_LIMIT: number
|
|
1431
|
+
```
|
|
1432
|
+
|
|
1433
|
+
#### `DISCOVERY_PATH`
|
|
1434
|
+
|
|
1435
|
+
Where a site serves its discovery document, on each of its hosts.
|
|
1436
|
+
|
|
1437
|
+
```ts
|
|
1438
|
+
const DISCOVERY_PATH = "/.well-known/foliwake.json"
|
|
1439
|
+
```
|
|
1440
|
+
|
|
1441
|
+
#### `DISCOVERY_VERSION`
|
|
1442
|
+
|
|
1443
|
+
The major of the discovery format this package reads.
|
|
1444
|
+
|
|
1445
|
+
```ts
|
|
1446
|
+
const DISCOVERY_VERSION = 1
|
|
1447
|
+
```
|
|
1448
|
+
|
|
1449
|
+
#### `PLAIN_CONTRACT`
|
|
1450
|
+
|
|
1451
|
+
The plain HTML contract this package reads (`X-Code-Release` on `.plain.html`).
|
|
1452
|
+
|
|
1453
|
+
```ts
|
|
1454
|
+
const PLAIN_CONTRACT = 2
|
|
1455
|
+
```
|
|
1456
|
+
|
|
1457
|
+
#### `URL_ENV`
|
|
1458
|
+
|
|
1459
|
+
The environment variable with the site's live origin.
|
|
1460
|
+
|
|
1461
|
+
```ts
|
|
1462
|
+
const URL_ENV = "FOLIWAKE_URL"
|
|
1463
|
+
```
|
|
1464
|
+
|
|
1465
|
+
**Interfaces**
|
|
1466
|
+
|
|
1467
|
+
#### `FetchDiscoveryOptions`
|
|
1468
|
+
|
|
1469
|
+
Options of `fetchDiscovery`.
|
|
1470
|
+
|
|
1471
|
+
```ts
|
|
1472
|
+
interface FetchDiscoveryOptions
|
|
1473
|
+
```
|
|
1474
|
+
|
|
1475
|
+
| Member | Type | Description |
|
|
1476
|
+
| --- | --- | --- |
|
|
1477
|
+
| `fetch?` | `typeof fetch` | A fetch implementation (tests); default globalThis.fetch. |
|
|
1478
|
+
| `timeoutMs?` | `number` | Per attempt; default 5000. |
|
|
1479
|
+
|
|
1480
|
+
#### `FoliwakeDiscovery`
|
|
1481
|
+
|
|
1482
|
+
A validated discovery document. Unknown keys are ignored, so a newer engine may add some.
|
|
1483
|
+
|
|
1484
|
+
```ts
|
|
1485
|
+
interface FoliwakeDiscovery
|
|
1486
|
+
```
|
|
1487
|
+
|
|
1488
|
+
| Member | Type | Description |
|
|
1489
|
+
| --- | --- | --- |
|
|
1490
|
+
| `site` | `string` | |
|
|
1491
|
+
| `live` | `{ url: string; }` | |
|
|
1492
|
+
| `preview` | `{ url: string; } \| null` | Null when the site has no preview host. Its Basic user is always `preview`. |
|
|
1493
|
+
| `media` | `{ cdn: string; } \| null` | The CDN that resizes live images (its origin), or null. |
|
|
1494
|
+
| `contracts` | `{ plain: number; html: number \| null; blocks: number \| null; hook: number \| null; }` | |
|
|
1495
|
+
|
|
1496
|
+
### `@foliwake/server/resolve`
|
|
1497
|
+
|
|
1498
|
+
**Functions and components**
|
|
1499
|
+
|
|
1500
|
+
#### `configFromSnapshot()`
|
|
1501
|
+
|
|
1502
|
+
The app config of a snapshot (no I/O). Throws FoliwakeConfigError naming the field.
|
|
1503
|
+
|
|
1504
|
+
```ts
|
|
1505
|
+
function configFromSnapshot(snapshot: FoliwakeConfigSnapshot): FoliwakeAppConfig
|
|
1506
|
+
```
|
|
1507
|
+
|
|
1508
|
+
**Returns** `FoliwakeAppConfig`
|
|
1509
|
+
|
|
1510
|
+
#### `loadAppConfig()`
|
|
1511
|
+
|
|
1512
|
+
readConfigSnapshot, then configFromSnapshot.
|
|
1513
|
+
|
|
1514
|
+
```ts
|
|
1515
|
+
function loadAppConfig(opts?: ReadSnapshotOptions): Promise<FoliwakeAppConfig>
|
|
1516
|
+
```
|
|
1517
|
+
|
|
1518
|
+
**Returns** `Promise<FoliwakeAppConfig>`
|
|
1519
|
+
|
|
1520
|
+
#### `readConfigSnapshot()`
|
|
1521
|
+
|
|
1522
|
+
Reads the inputs: FOLIWAKE_URL and its discovery document (fetched now), and the config file when there is one (a file named explicitly or by FOLIWAKE_REMOTE must exist). Throws FoliwakeConfigError.
|
|
1523
|
+
|
|
1524
|
+
```ts
|
|
1525
|
+
function readConfigSnapshot(opts?: ReadSnapshotOptions): Promise<FoliwakeConfigSnapshot>
|
|
1526
|
+
```
|
|
1527
|
+
|
|
1528
|
+
**Returns** `Promise<FoliwakeConfigSnapshot>`
|
|
1529
|
+
|
|
1530
|
+
**Constants**
|
|
1531
|
+
|
|
1532
|
+
#### `PREVIEW_TOKEN_ENV`
|
|
1533
|
+
|
|
1534
|
+
The variable the default preview credential reads: Basic `preview:<site preview token>`.
|
|
1535
|
+
|
|
1536
|
+
```ts
|
|
1537
|
+
const PREVIEW_TOKEN_ENV = "FOLIWAKE_PREVIEW_TOKEN"
|
|
1538
|
+
```
|
|
1539
|
+
|
|
1540
|
+
**Interfaces**
|
|
1541
|
+
|
|
1542
|
+
#### `FoliwakeConfigSnapshot`
|
|
1543
|
+
|
|
1544
|
+
The inputs of an app's config, as plain JSON.
|
|
1545
|
+
|
|
1546
|
+
```ts
|
|
1547
|
+
interface FoliwakeConfigSnapshot
|
|
1548
|
+
```
|
|
1549
|
+
|
|
1550
|
+
| Member | Type | Description |
|
|
1551
|
+
| --- | --- | --- |
|
|
1552
|
+
| `url` | `string \| null` | FOLIWAKE_URL, or null. |
|
|
1553
|
+
| `discovery` | `FoliwakeDiscovery \| null` | The site's discovery document read from `url`, or null without it. |
|
|
1554
|
+
| `file` | `string \| null` | The config file's name (for messages) and text, or null when there is none. |
|
|
1555
|
+
| `text` | `string \| null` | |
|
|
1556
|
+
|
|
1557
|
+
#### `ReadSnapshotOptions`
|
|
1558
|
+
|
|
1559
|
+
Options of `readConfigSnapshot` and `loadAppConfig`.
|
|
1560
|
+
|
|
1561
|
+
```ts
|
|
1562
|
+
interface ReadSnapshotOptions extends FetchDiscoveryOptions
|
|
1563
|
+
```
|
|
1564
|
+
|
|
1565
|
+
| Member | Type | Description |
|
|
1566
|
+
| --- | --- | --- |
|
|
1567
|
+
| `cwd?` | `string` | |
|
|
1568
|
+
| `env?` | `NodeJS.ProcessEnv` | Default process.env: FOLIWAKE_URL, FOLIWAKE_REMOTE. |
|
|
1569
|
+
| `file?` | `string` | The config file (default FOLIWAKE_REMOTE, else ./foliwake.remote.json). |
|
|
1570
|
+
|
|
1571
|
+
**Also exported** (documented where they are defined)
|
|
1572
|
+
|
|
1573
|
+
- from `@foliwake/server/discovery`: `DISCOVERY_PATH`, `FoliwakeDiscovery`, `URL_ENV`
|
|
1574
|
+
|
|
997
1575
|
### `@foliwake/server/schemas/remote-2.schema.json`
|
|
998
1576
|
|
|
999
1577
|
A file, not a module: `schemas/remote-2.schema.json` in the package.
|
|
1000
1578
|
|
|
1579
|
+
### `@foliwake/server/schemas/remote-3.schema.json`
|
|
1580
|
+
|
|
1581
|
+
A file, not a module: `schemas/remote-3.schema.json` in the package.
|
|
1582
|
+
|
|
1001
1583
|
<!-- apistop -->
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ReleaseBlocks } from '@foliwake/blocks-contract/page';
|
|
2
|
+
/** The response header that names the blocks module of a page's code release. */
|
|
3
|
+
export declare const BLOCKS_HEADER = "x-foliwake-blocks";
|
|
4
|
+
/** The parsed header; null when the answer has none (the release names no blocks module). */
|
|
5
|
+
export declare function parseBlocksHeader(value: string | null): ReleaseBlocks | null;
|