@popcorn245/cornfield 0.21.0 → 0.23.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/core/config.js +5 -0
- package/core/theme.js +337 -0
- package/core/validate.js +37 -0
- package/explorer/explorer.css +499 -637
- package/explorer/house.css +20 -26
- package/explorer/index.html +10 -2
- package/explorer/plugins.js +2 -0
- package/explorer/theme-boot.js +22 -2
- package/explorer/theme-model.js +19 -0
- package/explorer/theme-names.js +61 -0
- package/explorer/tokens.css +271 -0
- package/package.json +1 -1
- package/server/server.js +98 -4
package/server/server.js
CHANGED
|
@@ -16,6 +16,9 @@
|
|
|
16
16
|
* GET /cornfield/house.json the repo's house file, when `sources.house` is the built-in house source;
|
|
17
17
|
* Home Assistant's floors, areas and devices, when it is `home-assistant`
|
|
18
18
|
* GET /repo/<path>.js a browser module the config names (and the files in its folder)
|
|
19
|
+
* GET /cornfield/theme.css, /cornfield/theme/<name>.css
|
|
20
|
+
* the stylesheets the config's `theme` names (css, names): those files only, as text/css
|
|
21
|
+
* GET /cornfield/theme/assets/<path> an image or font beside the theme files (png, svg, webp, gif, avif, woff2, woff; 1 MB each)
|
|
19
22
|
* GET /api/forge/… issues, pull requests, their files and checks, release notes: read from the forge (server/forge-routes.js)
|
|
20
23
|
* POST /api/forge/pulls/<n>/merge|rerun land a green pull request, re-run a failed check (server/forge-actions.js)
|
|
21
24
|
* POST /api/forge/issues/<n>/spec, /pulls/<n>/build the pipeline's steps (server/pipeline.js, server/forge-actions.js)
|
|
@@ -50,6 +53,7 @@ import { createHash, randomBytes } from 'node:crypto';
|
|
|
50
53
|
import { fileURLToPath } from 'node:url';
|
|
51
54
|
import { browserSources, BUILTIN_SOURCES, parseDuration, specPollMs, triggersOf, DEFAULTS, hookWants, ideaLabel, runnerQuietAfterMs } from '../core/config.js';
|
|
52
55
|
import { CLOUD_FILE } from '../core/cloud.js';
|
|
56
|
+
import { MAX_THEME_ASSET_BYTES, MAX_THEME_BYTES, THEME_ASSET_PREFIX, THEME_NAME, THEME_SVG_CSP, clientTheme, cssProblems, fontOrigins, fontUrl, themeAssetFile, themeAssetPath } from '../core/theme.js';
|
|
53
57
|
import { decodePath, inside, notFound, sendFile } from './files.js';
|
|
54
58
|
import { serveProposals } from './proposals.js';
|
|
55
59
|
import { serveForge } from './forge-routes.js';
|
|
@@ -90,14 +94,96 @@ function importMapOf(config) {
|
|
|
90
94
|
* The Content-Security-Policy of every page: everything from this origin, nothing from anywhere else
|
|
91
95
|
* (the explorer runs offline from any CDN). An import map is inline, so it is allowed by its hash.
|
|
92
96
|
*/
|
|
93
|
-
function cspOf(importMap) {
|
|
97
|
+
function cspOf(importMap, theme) {
|
|
98
|
+
const { style, font } = fontOrigins(theme);
|
|
94
99
|
const hash = importMap ? ` 'sha256-${createHash('sha256').update(importMap).digest('base64')}'` : '';
|
|
95
100
|
return [
|
|
96
|
-
"default-src 'self'", `script-src 'self'${hash}`,
|
|
97
|
-
"connect-src 'self'",
|
|
101
|
+
"default-src 'self'", `script-src 'self'${hash}`, `style-src 'self' 'unsafe-inline'${style.map((o) => ` ${o}`).join('')}`, "img-src 'self' data: blob:",
|
|
102
|
+
"connect-src 'self'", `font-src 'self'${font.map((o) => ` ${o}`).join('')}`, "object-src 'none'", "base-uri 'none'", "frame-ancestors 'none'", "form-action 'self'",
|
|
98
103
|
].join('; ');
|
|
99
104
|
}
|
|
100
105
|
|
|
106
|
+
/** The config's theme names that may reach the page: plain ones only (core/theme.js THEME_NAME), and the default when it is one of them */
|
|
107
|
+
function themeNamesOf(theme) {
|
|
108
|
+
const names = Object.keys(theme.names || {}).filter((n) => THEME_NAME.test(n));
|
|
109
|
+
return { names, fallback: theme.default && names.includes(theme.default) ? theme.default : null };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** `<html>` with the named themes on it, for theme-boot.js to read before the first paint */
|
|
113
|
+
function themeHtmlTag(theme) {
|
|
114
|
+
const { names, fallback } = themeNamesOf(theme);
|
|
115
|
+
return names.length ? `<html lang="en" data-theme-names="${names.join(' ')}"${fallback ? ` data-theme-default="${fallback}"` : ''}>` : '<html lang="en">';
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The links that go after cornfield's own stylesheets, in this order: the allow-listed font stylesheets, the repo's theme.css, and the
|
|
120
|
+
* default named theme. explorer/index.html includes theme-boot.js once, after these, so it points that link at the viewer's choice
|
|
121
|
+
* before the first paint. Always links, never inlined CSS.
|
|
122
|
+
*/
|
|
123
|
+
function themeHead(theme) {
|
|
124
|
+
const out = [];
|
|
125
|
+
for (const f of theme.fonts || []) {
|
|
126
|
+
const u = fontUrl(f);
|
|
127
|
+
if (u) out.push(`<link rel="stylesheet" href="${escapeHtml(u.href)}" referrerpolicy="no-referrer" />`);
|
|
128
|
+
}
|
|
129
|
+
if (theme.css) out.push('<link rel="stylesheet" href="/cornfield/theme.css" data-cf-theme="css" />');
|
|
130
|
+
const { names, fallback } = themeNamesOf(theme);
|
|
131
|
+
if (names.length) out.push(`<link rel="stylesheet" id="cf-theme-name-css"${fallback ? ` href="/cornfield/theme/${fallback}.css"` : ''} data-cf-theme="name" />`);
|
|
132
|
+
return out.join('\n ');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The theme files the config names, by the path they are served at: `Map<'/cornfield/theme.css' | '/cornfield/theme/<name>.css', rel>` */
|
|
136
|
+
function themeFilesOf(config) {
|
|
137
|
+
const t = config.theme || {};
|
|
138
|
+
const out = new Map();
|
|
139
|
+
if (t.css) out.set('/cornfield/theme.css', t.css);
|
|
140
|
+
for (const [n, rel] of Object.entries(t.names || {})) if (THEME_NAME.test(n)) out.set(`/cornfield/theme/${n}.css`, rel);
|
|
141
|
+
return out;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Serves one theme file: text/css and nosniff, only a file the config names, only a plain file whose real path is still inside the repo
|
|
146
|
+
* (a link out is refused), only up to MAX_THEME_BYTES, and only if it passes the same CSS checks as `cornfield validate`.
|
|
147
|
+
*/
|
|
148
|
+
function sendTheme(req, res, config, rel, reqPath) {
|
|
149
|
+
try {
|
|
150
|
+
const file = inside(config.root, rel);
|
|
151
|
+
const real = file && fs.realpathSync(file);
|
|
152
|
+
const base = fs.realpathSync(config.root);
|
|
153
|
+
if (!real || !real.startsWith(base + path.sep)) return notFound(res, reqPath);
|
|
154
|
+
const stats = fs.statSync(real);
|
|
155
|
+
if (!stats.isFile() || stats.size > MAX_THEME_BYTES) return notFound(res, reqPath);
|
|
156
|
+
const text = fs.readFileSync(real, 'utf8');
|
|
157
|
+
if (cssProblems(text, { fonts: config.theme.fonts }).length) return notFound(res, reqPath);
|
|
158
|
+
res.writeHead(200, { 'Content-Type': 'text/css; charset=utf-8', 'Cache-Control': 'no-cache', 'X-Content-Type-Options': 'nosniff' });
|
|
159
|
+
return res.end(req.method === 'HEAD' ? undefined : text);
|
|
160
|
+
} catch {
|
|
161
|
+
return notFound(res, reqPath);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Serves one image or font from beside the theme (docs/config.md#theme): only a THEME_ASSET_TYPES file under the theme folder
|
|
167
|
+
* (core/theme.js `themeAssetPath`, `themeAssetFile`: plain names, no dot segments, no link out, at most 1 MB), never a listing,
|
|
168
|
+
* with nosniff; an SVG gets a policy of its own, so opened by itself it runs and loads nothing.
|
|
169
|
+
*/
|
|
170
|
+
function sendThemeAsset(req, res, config, reqPath) {
|
|
171
|
+
try {
|
|
172
|
+
const rel = themeAssetPath(reqPath.slice(THEME_ASSET_PREFIX.length));
|
|
173
|
+
const found = rel && themeAssetFile(config.root, config.theme || {}, rel);
|
|
174
|
+
if (!found || 'problem' in found) return notFound(res, reqPath);
|
|
175
|
+
const body = fs.readFileSync(found.file);
|
|
176
|
+
if (body.length > MAX_THEME_ASSET_BYTES) return notFound(res, reqPath); // it grew since it was looked at
|
|
177
|
+
res.writeHead(200, {
|
|
178
|
+
'Content-Type': found.type, 'Content-Length': body.length, 'Cache-Control': 'no-cache', 'X-Content-Type-Options': 'nosniff',
|
|
179
|
+
...(found.type === 'image/svg+xml' ? { 'Content-Security-Policy': THEME_SVG_CSP } : {}),
|
|
180
|
+
});
|
|
181
|
+
return res.end(req.method === 'HEAD' ? undefined : body);
|
|
182
|
+
} catch {
|
|
183
|
+
return notFound(res, reqPath);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
101
187
|
/** The browser modules the config names from the repo: each one's folder may be served (its .js files only) */
|
|
102
188
|
function repoModuleDirs(config) {
|
|
103
189
|
const dirs = [];
|
|
@@ -120,6 +206,8 @@ export function clientConfig(config, signIn = false) {
|
|
|
120
206
|
version: PKG.version,
|
|
121
207
|
title: config.title,
|
|
122
208
|
links: config.links || [],
|
|
209
|
+
// The theme's names and default, never a file's path (docs/config.md#theme)
|
|
210
|
+
theme: clientTheme(config.theme),
|
|
123
211
|
forge: config.forge ? { type: config.forge.type, url: config.forge.url || (config.forge.type === 'github' ? 'https://github.com' : null), repo: config.forge.repo, mergeMethod: config.forge.mergeMethod || 'merge', ...(config.approveLands ? { approveLands: true } : {}) } : null,
|
|
124
212
|
// Threads (docs/threads.md): the idea label, or false when `threads: false` turns the Threads tab off
|
|
125
213
|
threads: config.threads === false ? false : { label: ideaLabel(config) },
|
|
@@ -167,7 +255,9 @@ export function createCornfieldServer(config, opts = {}) {
|
|
|
167
255
|
if (config.approveLands && !auth.enabled) throw new Error('The config says proposals.approveLands, but sign-in is not set up. An approval is a signed-in maintainer\'s, and it lands as them, so without sign-in nothing would ever land: set up sign-in (docs/sign-in.md), or remove approveLands.');
|
|
168
256
|
const root = config.root;
|
|
169
257
|
const importMap = importMapOf(config);
|
|
170
|
-
const
|
|
258
|
+
const theme = config.theme || {};
|
|
259
|
+
const csp = cspOf(importMap, theme);
|
|
260
|
+
const themeFile = themeFilesOf(config);
|
|
171
261
|
const repoModules = repoModuleDirs(config);
|
|
172
262
|
const publicHost = config.publicUrl ? new URL(config.publicUrl).hostname : null;
|
|
173
263
|
const where = { root, proposals: config.proposals, worktrees: config.worktrees, worktreeMaxAgeDays: config.worktreeMaxAgeDays, topology: config.topology, dataDir: config.dataDir, publicHost };
|
|
@@ -220,6 +310,8 @@ export function createCornfieldServer(config, opts = {}) {
|
|
|
220
310
|
const page = () => {
|
|
221
311
|
const html = fs.readFileSync(path.join(EXPLORER_DIR, 'index.html'), 'utf8')
|
|
222
312
|
.replace('<!--@importmap-->', importMap ? `<script type="importmap">${importMap}</script>` : '')
|
|
313
|
+
.replace('<html lang="en">', () => themeHtmlTag(theme))
|
|
314
|
+
.replace('<!--@theme-->', () => themeHead(theme)) // a function, so a `$` in a font URL is not a replacement pattern
|
|
223
315
|
.replace(/<title>[^<]*<\/title>/, `<title>${escapeHtml(config.title)} · cornfield</title>`);
|
|
224
316
|
return html;
|
|
225
317
|
};
|
|
@@ -313,6 +405,8 @@ export function createCornfieldServer(config, opts = {}) {
|
|
|
313
405
|
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-cache' });
|
|
314
406
|
return res.end(req.method === 'HEAD' ? undefined : page());
|
|
315
407
|
}
|
|
408
|
+
if (themeFile.has(reqPath)) return sendTheme(req, res, config, themeFile.get(reqPath), reqPath);
|
|
409
|
+
if (reqPath.startsWith(THEME_ASSET_PREFIX)) return sendThemeAsset(req, res, config, reqPath);
|
|
316
410
|
if (reqPath === '/cornfield/config.json') return sendJson(res, 200, clientConfig(config, signedIn));
|
|
317
411
|
if (reqPath === '/topology.json') {
|
|
318
412
|
const file = inside(root, config.topology);
|