@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
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type FoliwakeAppConfig } from './config.js';
|
|
2
|
+
import { type FetchDiscoveryOptions, type FoliwakeDiscovery } from './discovery.js';
|
|
3
|
+
export { PREVIEW_TOKEN_ENV } from './config-v3.js';
|
|
4
|
+
export { DISCOVERY_PATH, URL_ENV, type FoliwakeDiscovery } from './discovery.js';
|
|
5
|
+
/** The inputs of an app's config, as plain JSON. */
|
|
6
|
+
export interface FoliwakeConfigSnapshot {
|
|
7
|
+
/** FOLIWAKE_URL, or null. */
|
|
8
|
+
url: string | null;
|
|
9
|
+
/** The site's discovery document read from `url`, or null without it. */
|
|
10
|
+
discovery: FoliwakeDiscovery | null;
|
|
11
|
+
/** The config file's name (for messages) and text, or null when there is none. */
|
|
12
|
+
file: string | null;
|
|
13
|
+
text: string | null;
|
|
14
|
+
}
|
|
15
|
+
/** Options of `readConfigSnapshot` and `loadAppConfig`. */
|
|
16
|
+
export interface ReadSnapshotOptions extends FetchDiscoveryOptions {
|
|
17
|
+
cwd?: string;
|
|
18
|
+
/** Default process.env: FOLIWAKE_URL, FOLIWAKE_REMOTE. */
|
|
19
|
+
env?: NodeJS.ProcessEnv;
|
|
20
|
+
/** The config file (default FOLIWAKE_REMOTE, else ./foliwake.remote.json). */
|
|
21
|
+
file?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Reads the inputs: FOLIWAKE_URL and its discovery document (fetched now), and the config file when there is one
|
|
25
|
+
* (a file named explicitly or by FOLIWAKE_REMOTE must exist). Throws FoliwakeConfigError.
|
|
26
|
+
*/
|
|
27
|
+
export declare function readConfigSnapshot(opts?: ReadSnapshotOptions): Promise<FoliwakeConfigSnapshot>;
|
|
28
|
+
/** The app config of a snapshot (no I/O). Throws FoliwakeConfigError naming the field. */
|
|
29
|
+
export declare function configFromSnapshot(snapshot: FoliwakeConfigSnapshot): FoliwakeAppConfig;
|
|
30
|
+
/** readConfigSnapshot, then configFromSnapshot. */
|
|
31
|
+
export declare function loadAppConfig(opts?: ReadSnapshotOptions): Promise<FoliwakeAppConfig>;
|
package/dist/resolve.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Where an app's config comes from: FOLIWAKE_URL (the site's discovery document) with an optional version 3
|
|
2
|
+
// file, or a version 2 file alone. The inputs are read once into a snapshot (plain JSON: an app framework can
|
|
3
|
+
// inline it at build time, so nothing is fetched at run time); the config is computed from the snapshot.
|
|
4
|
+
import { readFile } from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { CONFIG_ENV, CONFIG_FILE, configFilePath, parseFoliwakeConfig } from './config.js';
|
|
7
|
+
import { parseOverrides, resolveConfig } from './config-v3.js';
|
|
8
|
+
import { fetchDiscovery, URL_ENV } from './discovery.js';
|
|
9
|
+
import { FoliwakeConfigError } from './errors.js';
|
|
10
|
+
export { PREVIEW_TOKEN_ENV } from './config-v3.js';
|
|
11
|
+
export { DISCOVERY_PATH, URL_ENV } from './discovery.js';
|
|
12
|
+
async function readOptionalFile(file, required) {
|
|
13
|
+
try {
|
|
14
|
+
return await readFile(file, 'utf8');
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
const code = error.code;
|
|
18
|
+
if (code === 'ENOENT' && !required)
|
|
19
|
+
return null;
|
|
20
|
+
if (code === 'ENOENT')
|
|
21
|
+
throw new FoliwakeConfigError(`${file} does not exist.`);
|
|
22
|
+
throw new FoliwakeConfigError(`Cannot read ${file}: ${error.message}`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const NOTHING = `Set ${URL_ENV} to the site's live URL (https://…), ` + `or create ${CONFIG_FILE} (version 2) in the app's root.`;
|
|
26
|
+
/**
|
|
27
|
+
* Reads the inputs: FOLIWAKE_URL and its discovery document (fetched now), and the config file when there is one
|
|
28
|
+
* (a file named explicitly or by FOLIWAKE_REMOTE must exist). Throws FoliwakeConfigError.
|
|
29
|
+
*/
|
|
30
|
+
export async function readConfigSnapshot(opts = {}) {
|
|
31
|
+
const env = opts.env ?? process.env;
|
|
32
|
+
const url = env[URL_ENV]?.trim() || null;
|
|
33
|
+
const file = configFilePath({ cwd: opts.cwd, file: opts.file, env });
|
|
34
|
+
const named = opts.file !== undefined || env[CONFIG_ENV] !== undefined;
|
|
35
|
+
const text = await readOptionalFile(file, named);
|
|
36
|
+
if (url === null && text === null)
|
|
37
|
+
throw new FoliwakeConfigError(NOTHING);
|
|
38
|
+
const discovery = url === null ? null : await fetchDiscovery(url, opts);
|
|
39
|
+
return { url, discovery, file: text === null ? null : path.basename(file), text };
|
|
40
|
+
}
|
|
41
|
+
function versionOf(text) {
|
|
42
|
+
try {
|
|
43
|
+
const parsed = JSON.parse(text);
|
|
44
|
+
return typeof parsed === 'object' && parsed !== null ? parsed.version : undefined;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return undefined; // the parser names the problem
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function withVersion2(discovery, text, file) {
|
|
51
|
+
const config = parseFoliwakeConfig(text, { file });
|
|
52
|
+
if (config.site !== discovery.site) {
|
|
53
|
+
const text = `"site" is ${config.site}, but ${URL_ENV} serves the site ${discovery.site}.`;
|
|
54
|
+
throw new FoliwakeConfigError(`${file}: ${text} Make them agree, or drop one.`);
|
|
55
|
+
}
|
|
56
|
+
return config;
|
|
57
|
+
}
|
|
58
|
+
/** The app config of a snapshot (no I/O). Throws FoliwakeConfigError naming the field. */
|
|
59
|
+
export function configFromSnapshot(snapshot) {
|
|
60
|
+
const { discovery, text } = snapshot;
|
|
61
|
+
const file = snapshot.file ?? CONFIG_FILE;
|
|
62
|
+
if (discovery === null) {
|
|
63
|
+
if (text === null)
|
|
64
|
+
throw new FoliwakeConfigError(NOTHING);
|
|
65
|
+
return parseFoliwakeConfig(text, { file });
|
|
66
|
+
}
|
|
67
|
+
if (text === null)
|
|
68
|
+
return resolveConfig(discovery);
|
|
69
|
+
if (versionOf(text) === 2)
|
|
70
|
+
return withVersion2(discovery, text, file);
|
|
71
|
+
return resolveConfig(discovery, parseOverrides(text, file));
|
|
72
|
+
}
|
|
73
|
+
/** readConfigSnapshot, then configFromSnapshot. */
|
|
74
|
+
export async function loadAppConfig(opts = {}) {
|
|
75
|
+
return configFromSnapshot(await readConfigSnapshot(opts));
|
|
76
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const SERVER_SDK = "@foliwake/server/__FOLIWAKE_SERVER_VERSION__";
|
package/dist/version.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@foliwake/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Server toolkit for apps that show Foliwake pages: config v2, origin client, envelope read, page check and preparation, code/media proxy handlers, CSP. Web Request/Response, no framework.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
"foliwake": {
|
|
34
34
|
"plain": 2,
|
|
35
35
|
"blocksContract": 2,
|
|
36
|
-
"remoteConfig":
|
|
36
|
+
"remoteConfig": 3,
|
|
37
|
+
"discovery": 1
|
|
37
38
|
},
|
|
38
39
|
"exports": {
|
|
39
40
|
"./config": {
|
|
@@ -64,13 +65,34 @@
|
|
|
64
65
|
"types": "./dist/preload.d.ts",
|
|
65
66
|
"default": "./dist/preload.js"
|
|
66
67
|
},
|
|
68
|
+
"./hook": {
|
|
69
|
+
"types": "./dist/hook.d.ts",
|
|
70
|
+
"default": "./dist/hook.js"
|
|
71
|
+
},
|
|
72
|
+
"./handler": {
|
|
73
|
+
"types": "./dist/handler.d.ts",
|
|
74
|
+
"default": "./dist/handler.js"
|
|
75
|
+
},
|
|
76
|
+
"./hook-endpoint": {
|
|
77
|
+
"types": "./dist/hook-endpoint.d.ts",
|
|
78
|
+
"default": "./dist/hook-endpoint.js"
|
|
79
|
+
},
|
|
80
|
+
"./discovery": {
|
|
81
|
+
"types": "./dist/discovery.d.ts",
|
|
82
|
+
"default": "./dist/discovery.js"
|
|
83
|
+
},
|
|
84
|
+
"./resolve": {
|
|
85
|
+
"types": "./dist/resolve.d.ts",
|
|
86
|
+
"default": "./dist/resolve.js"
|
|
87
|
+
},
|
|
67
88
|
"./schemas/remote-2.schema.json": "./schemas/remote-2.schema.json",
|
|
89
|
+
"./schemas/remote-3.schema.json": "./schemas/remote-3.schema.json",
|
|
68
90
|
"./package.json": "./package.json"
|
|
69
91
|
},
|
|
70
92
|
"dependencies": {
|
|
71
93
|
"htmlparser2": "10.1.0",
|
|
72
94
|
"jose": "6.2.15",
|
|
73
|
-
"@foliwake/blocks-contract": "0.
|
|
95
|
+
"@foliwake/blocks-contract": "0.10.0"
|
|
74
96
|
},
|
|
75
97
|
"files": [
|
|
76
98
|
"dist/",
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://foliwake.dev/schemas/remote-3.schema.json",
|
|
4
|
+
"title": "foliwake.remote.json version 3",
|
|
5
|
+
"description": "Optional overrides of an app configured by FOLIWAKE_URL: the site's discovery document (/.well-known/foliwake.json) gives its origins and media CDN; this file holds the app's own settings and the NAMES of the variables holding credentials. No secrets. The TypeScript parser (@foliwake/server/resolve) is the authority; test/server-config-v3.test.js keeps both in step.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"additionalProperties": false,
|
|
8
|
+
"required": [
|
|
9
|
+
"version"
|
|
10
|
+
],
|
|
11
|
+
"properties": {
|
|
12
|
+
"$schema": {
|
|
13
|
+
"type": "string"
|
|
14
|
+
},
|
|
15
|
+
"$comment": {
|
|
16
|
+
"type": "string"
|
|
17
|
+
},
|
|
18
|
+
"version": {
|
|
19
|
+
"const": 3
|
|
20
|
+
},
|
|
21
|
+
"site": {
|
|
22
|
+
"type": "string",
|
|
23
|
+
"description": "Checked against the discovery document's site."
|
|
24
|
+
},
|
|
25
|
+
"live": {
|
|
26
|
+
"$ref": "#/$defs/channel"
|
|
27
|
+
},
|
|
28
|
+
"preview": {
|
|
29
|
+
"$ref": "#/$defs/channel",
|
|
30
|
+
"description": "Default auth: Basic, user preview, password from FOLIWAKE_PREVIEW_TOKEN."
|
|
31
|
+
},
|
|
32
|
+
"app": {
|
|
33
|
+
"type": "object",
|
|
34
|
+
"additionalProperties": false,
|
|
35
|
+
"properties": {
|
|
36
|
+
"pages": {
|
|
37
|
+
"description": "Page globs the app may read (query-index grammar: [a-z0-9_-] literals, * within a segment, ** as a whole segment; /x/** also matches /x).",
|
|
38
|
+
"type": "array",
|
|
39
|
+
"minItems": 1,
|
|
40
|
+
"maxItems": 20,
|
|
41
|
+
"items": {
|
|
42
|
+
"type": "string",
|
|
43
|
+
"maxLength": 200,
|
|
44
|
+
"pattern": "^/(?:(?:\\*\\*|[a-z0-9_*-]+)(?:/(?:\\*\\*|[a-z0-9_*-]+))*)?$"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"proxies": {
|
|
48
|
+
"description": "App-origin URL prefixes of the proxies. Not /, /_next, /api, /code, /media or below them; code and media differ and neither is inside the other.",
|
|
49
|
+
"type": "object",
|
|
50
|
+
"additionalProperties": false,
|
|
51
|
+
"properties": {
|
|
52
|
+
"code": {
|
|
53
|
+
"$ref": "#/$defs/prefix",
|
|
54
|
+
"default": "/foliwake/code"
|
|
55
|
+
},
|
|
56
|
+
"media": {
|
|
57
|
+
"$ref": "#/$defs/prefix",
|
|
58
|
+
"default": "/foliwake/media"
|
|
59
|
+
},
|
|
60
|
+
"pages": {
|
|
61
|
+
"type": "null"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"blocks": {
|
|
66
|
+
"type": "object",
|
|
67
|
+
"additionalProperties": false,
|
|
68
|
+
"properties": {
|
|
69
|
+
"entry": {
|
|
70
|
+
"description": "Path of the blocks module inside a code release; absent or null: bodies stay static.",
|
|
71
|
+
"type": [
|
|
72
|
+
"string",
|
|
73
|
+
"null"
|
|
74
|
+
],
|
|
75
|
+
"pattern": "^(?:scripts|components|_foliwake)(?:/[A-Za-z0-9_][A-Za-z0-9_.-]*){1,7}\\.js$"
|
|
76
|
+
},
|
|
77
|
+
"contract": {
|
|
78
|
+
"type": "string",
|
|
79
|
+
"pattern": "^foliwake-blocks/[1-9][0-9]{0,2}$",
|
|
80
|
+
"default": "foliwake-blocks/2"
|
|
81
|
+
},
|
|
82
|
+
"eventPrefix": {
|
|
83
|
+
"type": "string",
|
|
84
|
+
"pattern": "^[a-z][a-z0-9-]{0,31}$",
|
|
85
|
+
"default": "block"
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
"media": {
|
|
90
|
+
"description": "Live pictures through a transforming host (preview pictures always go through the media proxy).",
|
|
91
|
+
"type": [
|
|
92
|
+
"object",
|
|
93
|
+
"null"
|
|
94
|
+
],
|
|
95
|
+
"additionalProperties": false,
|
|
96
|
+
"required": [
|
|
97
|
+
"base"
|
|
98
|
+
],
|
|
99
|
+
"properties": {
|
|
100
|
+
"base": {
|
|
101
|
+
"type": "string",
|
|
102
|
+
"pattern": "^https://[^/?#@]+$"
|
|
103
|
+
},
|
|
104
|
+
"transform": {
|
|
105
|
+
"enum": [
|
|
106
|
+
"cloudflare-images",
|
|
107
|
+
null
|
|
108
|
+
]
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
"revalidate": {
|
|
113
|
+
"description": "Seconds before a cached envelope is stale (stale-while-revalidate; expire is 24 h).",
|
|
114
|
+
"type": "integer",
|
|
115
|
+
"minimum": 1,
|
|
116
|
+
"maximum": 86400,
|
|
117
|
+
"default": 300
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
"description": "The app's own decisions; every key is optional. Without pages: [\"/**\"] (required when live uses a service token). Without blocks: each page's code release names its blocks module (X-Foliwake-Blocks). Without media: the discovery document's media CDN, if any."
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
"$defs": {
|
|
124
|
+
"prefix": {
|
|
125
|
+
"type": "string",
|
|
126
|
+
"pattern": "^(?:/[a-z0-9-]+)+$"
|
|
127
|
+
},
|
|
128
|
+
"basicAuth": {
|
|
129
|
+
"type": "object",
|
|
130
|
+
"additionalProperties": false,
|
|
131
|
+
"required": [
|
|
132
|
+
"type",
|
|
133
|
+
"username",
|
|
134
|
+
"passwordEnv"
|
|
135
|
+
],
|
|
136
|
+
"properties": {
|
|
137
|
+
"type": {
|
|
138
|
+
"const": "basic"
|
|
139
|
+
},
|
|
140
|
+
"username": {
|
|
141
|
+
"type": "string",
|
|
142
|
+
"minLength": 1,
|
|
143
|
+
"maxLength": 128,
|
|
144
|
+
"pattern": "^[^:\\u0000-\\u001f\\u007f]+$"
|
|
145
|
+
},
|
|
146
|
+
"passwordEnv": {
|
|
147
|
+
"type": "string",
|
|
148
|
+
"pattern": "^[A-Z_][A-Z0-9_]{0,127}$",
|
|
149
|
+
"description": "The NAME of the environment variable holding the password."
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
"serviceTokenAuth": {
|
|
154
|
+
"description": "Each request carries a fresh ES256 JWT for its exact path (exp = iat + 60), signed with the private key in privateKeyEnv; the delivery checks it with the public key of kid.",
|
|
155
|
+
"type": "object",
|
|
156
|
+
"additionalProperties": false,
|
|
157
|
+
"required": [
|
|
158
|
+
"type",
|
|
159
|
+
"issuer",
|
|
160
|
+
"kid",
|
|
161
|
+
"privateKeyEnv"
|
|
162
|
+
],
|
|
163
|
+
"properties": {
|
|
164
|
+
"type": {
|
|
165
|
+
"const": "service-token"
|
|
166
|
+
},
|
|
167
|
+
"issuer": {
|
|
168
|
+
"type": "string",
|
|
169
|
+
"pattern": "^[A-Za-z0-9._:/-]{1,256}$",
|
|
170
|
+
"description": "The iss the delivery expects (its TOKEN_ISSUER)."
|
|
171
|
+
},
|
|
172
|
+
"kid": {
|
|
173
|
+
"type": "string",
|
|
174
|
+
"pattern": "^[A-Za-z0-9._-]{1,128}$",
|
|
175
|
+
"description": "The key id of the public key the delivery holds."
|
|
176
|
+
},
|
|
177
|
+
"privateKeyEnv": {
|
|
178
|
+
"type": "string",
|
|
179
|
+
"pattern": "^[A-Z_][A-Z0-9_]{0,127}$",
|
|
180
|
+
"description": "The NAME of the environment variable holding the ES256 private key as JWK JSON text."
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
"channel": {
|
|
185
|
+
"type": "object",
|
|
186
|
+
"additionalProperties": false,
|
|
187
|
+
"properties": {
|
|
188
|
+
"auth": {
|
|
189
|
+
"oneOf": [
|
|
190
|
+
{
|
|
191
|
+
"type": "null"
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
"$ref": "#/$defs/basicAuth"
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
"$ref": "#/$defs/serviceTokenAuth"
|
|
198
|
+
}
|
|
199
|
+
]
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|