@jxsuite/compiler 11.0.0 → 11.1.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/dist/cli.js +278 -275
- package/dist/compiler.js +120 -117
- package/package.json +8 -8
- package/src/child-separator.ts +260 -0
- package/src/shared.ts +26 -30
- package/src/site/build-time.ts +45 -0
- package/src/site/context-injection.ts +4 -0
- package/src/site/csp.ts +102 -0
- package/src/site/headers-emitter.ts +2 -1
- package/src/site/image-transform.ts +1 -1
- package/src/site/locale-negotiation.ts +44 -14
- package/src/site/site-build.ts +105 -29
- package/src/site/site-loader.ts +2 -1
- package/src/site/social-image.ts +159 -0
- package/src/site/validate-command.ts +54 -34
- package/src/targets/compile-client.ts +20 -9
- package/src/targets/compile-element.ts +14 -6
- package/src/targets/compile-server.ts +3 -2
- package/src/targets/compile-static.ts +5 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jxsuite/compiler",
|
|
3
|
-
"version": "11.
|
|
3
|
+
"version": "11.1.0",
|
|
4
4
|
"description": "Jx static HTML compiler, island detector, and site builder",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -56,19 +56,19 @@
|
|
|
56
56
|
},
|
|
57
57
|
"dependencies": {
|
|
58
58
|
"@jxsuite/compiler-binding": "^0.3.0",
|
|
59
|
-
"@jxsuite/create": "^1.8.
|
|
60
|
-
"@jxsuite/erase": "^0.3.
|
|
61
|
-
"@jxsuite/runtime": "^9.0.
|
|
62
|
-
"@jxsuite/schema": "^8.
|
|
63
|
-
"@jxsuite/site": "^4.
|
|
59
|
+
"@jxsuite/create": "^1.8.6",
|
|
60
|
+
"@jxsuite/erase": "^0.3.5",
|
|
61
|
+
"@jxsuite/runtime": "^9.0.1",
|
|
62
|
+
"@jxsuite/schema": "^8.1.0",
|
|
63
|
+
"@jxsuite/site": "^4.1.0",
|
|
64
64
|
"@vue/reactivity": "3.5.45",
|
|
65
65
|
"esbuild": "^0.28.2",
|
|
66
66
|
"lit-html": "^3.3.3"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
69
|
"@happy-dom/global-registrator": "^21.0.4",
|
|
70
|
-
"@jxsuite/connector": "^0.8.
|
|
71
|
-
"@jxsuite/parser": "^5.2.
|
|
70
|
+
"@jxsuite/connector": "^0.8.14",
|
|
71
|
+
"@jxsuite/parser": "^5.2.1",
|
|
72
72
|
"@jxsuite/test-support": "^0.0.0",
|
|
73
73
|
"@types/mdast": "^4.0.4"
|
|
74
74
|
},
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the emitters put between an element's children (compiler.md §8.1, "Whitespace between
|
|
3
|
+
* siblings").
|
|
4
|
+
*
|
|
5
|
+
* A static emitter writes each child as a string and joins the strings. A newline and indent
|
|
6
|
+
* between two block elements is formatting: the browser drops it. Between two inline-level
|
|
7
|
+
* siblings, or beside text, the same run of whitespace is a text node the page renders, so
|
|
8
|
+
* `[link](/x/).` would build as `<a>link</a>\n.` and read "link ." (`` `code`, `` as "code ,"). The
|
|
9
|
+
* separator is therefore written only where nothing it touches can render it, and never otherwise;
|
|
10
|
+
* the text the document holds is never touched.
|
|
11
|
+
*
|
|
12
|
+
* The decision is made from the two strings being joined (the tag that ends the left one and the
|
|
13
|
+
* tag or text that begins the right one), not from the nodes that produced them, so it holds for
|
|
14
|
+
* everything that ends up between siblings: an element, a text node, a component instance expanded
|
|
15
|
+
* in place, the rows of a repeater, a slot's content.
|
|
16
|
+
*
|
|
17
|
+
* @docs framework/build
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** What stands at one edge of a rendered child. */
|
|
21
|
+
export type EdgeKind = "text" | "inline" | "block";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Elements whose default `display` is `block` (or a table or list-item box), and `<br>`, which ends
|
|
25
|
+
* the line it is on: whitespace on either side of one is at the edge of a line, where it is
|
|
26
|
+
* collapsed away. Everything else is inline-level, whatever the author's CSS later makes of it.
|
|
27
|
+
*/
|
|
28
|
+
const BLOCK_TAGS: ReadonlySet<string> = new Set([
|
|
29
|
+
"address",
|
|
30
|
+
"article",
|
|
31
|
+
"aside",
|
|
32
|
+
"blockquote",
|
|
33
|
+
"body",
|
|
34
|
+
"br",
|
|
35
|
+
"caption",
|
|
36
|
+
"center",
|
|
37
|
+
"col",
|
|
38
|
+
"colgroup",
|
|
39
|
+
"dd",
|
|
40
|
+
"details",
|
|
41
|
+
"dialog",
|
|
42
|
+
"dir",
|
|
43
|
+
"div",
|
|
44
|
+
"dl",
|
|
45
|
+
"dt",
|
|
46
|
+
"fieldset",
|
|
47
|
+
"figcaption",
|
|
48
|
+
"figure",
|
|
49
|
+
"footer",
|
|
50
|
+
"form",
|
|
51
|
+
"h1",
|
|
52
|
+
"h2",
|
|
53
|
+
"h3",
|
|
54
|
+
"h4",
|
|
55
|
+
"h5",
|
|
56
|
+
"h6",
|
|
57
|
+
"head",
|
|
58
|
+
"header",
|
|
59
|
+
"hgroup",
|
|
60
|
+
"hr",
|
|
61
|
+
"html",
|
|
62
|
+
"legend",
|
|
63
|
+
"li",
|
|
64
|
+
"listing",
|
|
65
|
+
"main",
|
|
66
|
+
"menu",
|
|
67
|
+
"nav",
|
|
68
|
+
"ol",
|
|
69
|
+
"optgroup",
|
|
70
|
+
"option",
|
|
71
|
+
"p",
|
|
72
|
+
"plaintext",
|
|
73
|
+
"pre",
|
|
74
|
+
"search",
|
|
75
|
+
"section",
|
|
76
|
+
"summary",
|
|
77
|
+
"table",
|
|
78
|
+
"tbody",
|
|
79
|
+
"td",
|
|
80
|
+
"tfoot",
|
|
81
|
+
"th",
|
|
82
|
+
"thead",
|
|
83
|
+
"tr",
|
|
84
|
+
"ul",
|
|
85
|
+
"xmp",
|
|
86
|
+
]);
|
|
87
|
+
|
|
88
|
+
/** The tag a rendered edge opens or closes. */
|
|
89
|
+
function kindOfTag(tag: string): EdgeKind {
|
|
90
|
+
return BLOCK_TAGS.has(tag.toLowerCase()) ? "block" : "inline";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** A comment at the start of a string. */
|
|
94
|
+
const LEADING_COMMENT = /^<!--[\s\S]*?-->/u;
|
|
95
|
+
/** A start tag at the start of a string. */
|
|
96
|
+
const LEADING_TAG = /^<([a-zA-Z][^\s/>]*)/u;
|
|
97
|
+
/** The end tag that closes a string. */
|
|
98
|
+
const TRAILING_CLOSE = /<\/([a-zA-Z][^\s>]*)\s*>$/u;
|
|
99
|
+
/** The start tag of a void element that ends a string (it has no end tag to find). */
|
|
100
|
+
const TRAILING_VOID = /<([a-zA-Z][^\s/<>]*)(?:\s[^<]*)?>$/u;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The kind of the node that begins `html`, looking past comments (the hydration markers a slot's
|
|
104
|
+
* content is bracketed with): `null` when nothing but comments is there.
|
|
105
|
+
*
|
|
106
|
+
* @param {string} html
|
|
107
|
+
* @returns {EdgeKind | null}
|
|
108
|
+
*/
|
|
109
|
+
function leadingKind(html: string): EdgeKind | null {
|
|
110
|
+
let rest = html;
|
|
111
|
+
for (let match = LEADING_COMMENT.exec(rest); match; match = LEADING_COMMENT.exec(rest)) {
|
|
112
|
+
rest = rest.slice(match[0].length);
|
|
113
|
+
}
|
|
114
|
+
if (rest === "") {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
const tag = LEADING_TAG.exec(rest);
|
|
118
|
+
return tag ? kindOfTag(tag[1]!) : "text";
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The kind of the node that ends `html`, looking past comments: `null` when nothing but comments is
|
|
123
|
+
* there. Text is anything that does not end in a tag, which includes a template hole.
|
|
124
|
+
*
|
|
125
|
+
* @param {string} html
|
|
126
|
+
* @returns {EdgeKind | null}
|
|
127
|
+
*/
|
|
128
|
+
function trailingKind(html: string): EdgeKind | null {
|
|
129
|
+
let rest = html;
|
|
130
|
+
while (rest.endsWith("-->")) {
|
|
131
|
+
const open = rest.lastIndexOf("<!--");
|
|
132
|
+
if (open === -1) {
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
rest = rest.slice(0, open);
|
|
136
|
+
}
|
|
137
|
+
if (rest === "") {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
const close = TRAILING_CLOSE.exec(rest) ?? TRAILING_VOID.exec(rest);
|
|
141
|
+
return close ? kindOfTag(close[1]!) : "text";
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Whether a newline between `left` and `right` is formatting only: one of the two is a block-level
|
|
146
|
+
* element, so the whitespace falls at the edge of a line, and neither is text. Two inline-level
|
|
147
|
+
* siblings never qualify (the whitespace would be a space between them), and nor does anything
|
|
148
|
+
* beside text. A string of nothing but comments never asks for one.
|
|
149
|
+
*
|
|
150
|
+
* @param {string} left - The child rendered before the separator
|
|
151
|
+
* @param {string} right - The child rendered after it
|
|
152
|
+
* @returns {boolean}
|
|
153
|
+
*/
|
|
154
|
+
export function separatorIsSafe(left: string, right: string): boolean {
|
|
155
|
+
const before = trailingKind(left);
|
|
156
|
+
const after = leadingKind(right);
|
|
157
|
+
if (before === null || after === null) {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
return before !== "text" && after !== "text" && (before === "block" || after === "block");
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** How {@link joinChildren} writes a separator. */
|
|
164
|
+
export interface JoinOptions {
|
|
165
|
+
/** Inside a `<pre>`-like subtree, where whitespace is content: the parts are concatenated. */
|
|
166
|
+
preformatted?: boolean | undefined;
|
|
167
|
+
/** Written between two siblings that may be separated (`"\n "` by default). */
|
|
168
|
+
separator?: string | undefined;
|
|
169
|
+
/**
|
|
170
|
+
* What each part starts with because its emitter indented it. It is part of the layout, never of
|
|
171
|
+
* the content: a part is classified without it, and it is dropped when no separator is written,
|
|
172
|
+
* where it would be a run of spaces the page renders.
|
|
173
|
+
*/
|
|
174
|
+
lead?: string | undefined;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Join the rendered children of one element.
|
|
179
|
+
*
|
|
180
|
+
* Where a newline is formatting only (`separatorIsSafe`) it is written, so a block-level tree reads
|
|
181
|
+
* as an indented one. Anywhere else the two children are joined with nothing between them, so the
|
|
182
|
+
* output renders exactly what the document says. A part that is empty (a slot with nothing in it, a
|
|
183
|
+
* child that renders nothing) takes no separator of its own.
|
|
184
|
+
*
|
|
185
|
+
* @param {readonly string[]} parts - The children, each as its emitter rendered it
|
|
186
|
+
* @param {JoinOptions} [options]
|
|
187
|
+
* @returns {string}
|
|
188
|
+
*/
|
|
189
|
+
export function joinChildren(parts: readonly string[], options: JoinOptions = {}): string {
|
|
190
|
+
const { lead = "", preformatted = false, separator = "\n " } = options;
|
|
191
|
+
if (preformatted) {
|
|
192
|
+
return parts.join("");
|
|
193
|
+
}
|
|
194
|
+
const present = parts.filter((part) => part !== "" && part !== lead);
|
|
195
|
+
let out = "";
|
|
196
|
+
for (const [index, part] of present.entries()) {
|
|
197
|
+
if (index === 0) {
|
|
198
|
+
out = part;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
const previous = present[index - 1]!;
|
|
202
|
+
const body = stripLead(part, lead);
|
|
203
|
+
out += separatorIsSafe(previous, body) ? `${separator}${part}` : body;
|
|
204
|
+
}
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** How {@link padChildren} writes the edges of an element's content. */
|
|
209
|
+
export interface PadOptions {
|
|
210
|
+
/** Written before the first child, if what it touches is not text or inline-level. */
|
|
211
|
+
open: string;
|
|
212
|
+
/** Written after the last child, on the same condition. */
|
|
213
|
+
close: string;
|
|
214
|
+
/** What each part starts with because its emitter indented it (see {@link JoinOptions}). */
|
|
215
|
+
lead?: string | undefined;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Put a newline and indent between an element's start tag and its content, and between its content
|
|
220
|
+
* and its end tag, where the child at that edge is block-level. Inside an inline parent (`<a>`,
|
|
221
|
+
* `<em>`) text is flush against both tags, because a space there is a space the page shows. With no
|
|
222
|
+
* `open` written, the first child loses the indent its emitter gave it.
|
|
223
|
+
*
|
|
224
|
+
* @param {string} joined - The content, as {@link joinChildren} returned it
|
|
225
|
+
* @param {PadOptions} options
|
|
226
|
+
* @returns {string}
|
|
227
|
+
*/
|
|
228
|
+
export function padChildren(joined: string, options: PadOptions): string {
|
|
229
|
+
const { close, lead = "", open } = options;
|
|
230
|
+
if (joined === "") {
|
|
231
|
+
return joined;
|
|
232
|
+
}
|
|
233
|
+
const first = leadingKind(stripLead(joined, lead));
|
|
234
|
+
const last = trailingKind(joined);
|
|
235
|
+
const head = first === "block" ? open : "";
|
|
236
|
+
const tail = last === "block" ? close : "";
|
|
237
|
+
return `${head}${head === "" ? stripLead(joined, lead) : joined}${tail}`;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** How {@link blockChildren} lays out an element's whole content. */
|
|
241
|
+
export interface BlockOptions extends JoinOptions, PadOptions {}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The content of an element laid out one child per line: {@link joinChildren} then
|
|
245
|
+
* {@link padChildren}.
|
|
246
|
+
*
|
|
247
|
+
* @param {readonly string[]} parts
|
|
248
|
+
* @param {BlockOptions} options
|
|
249
|
+
* @returns {string}
|
|
250
|
+
*/
|
|
251
|
+
export function blockChildren(parts: readonly string[], options: BlockOptions): string {
|
|
252
|
+
const { close, open, ...join } = options;
|
|
253
|
+
const joined = joinChildren(parts, join);
|
|
254
|
+
return join.preformatted ? joined : padChildren(joined, { close, lead: join.lead, open });
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** A part without the indent its emitter gave it. */
|
|
258
|
+
function stripLead(part: string, lead: string): string {
|
|
259
|
+
return part.startsWith(lead) ? part.slice(lead.length) : part;
|
|
260
|
+
}
|
package/src/shared.ts
CHANGED
|
@@ -69,6 +69,7 @@ import {
|
|
|
69
69
|
maskCode,
|
|
70
70
|
} from "@jxsuite/schema/guards";
|
|
71
71
|
import type { TagNameSlot } from "@jxsuite/schema/guards";
|
|
72
|
+
import { joinChildren } from "./child-separator.ts";
|
|
72
73
|
import { resolveShadowMode, styleScopePrefix } from "./shadow.ts";
|
|
73
74
|
import type { ShadowMode } from "./shadow.ts";
|
|
74
75
|
import {
|
|
@@ -1873,18 +1874,6 @@ export function contentPropertyText(
|
|
|
1873
1874
|
*/
|
|
1874
1875
|
export const PREFORMATTED_TAGS = new Set(["pre", "textarea", "listing", "plaintext", "xmp"]);
|
|
1875
1876
|
|
|
1876
|
-
/**
|
|
1877
|
-
* The separator to place between an element's compiled children: none inside a preformatted
|
|
1878
|
-
* subtree, a newline plus `indent` elsewhere.
|
|
1879
|
-
*
|
|
1880
|
-
* @param {boolean | undefined} preformatted
|
|
1881
|
-
* @param {string} [indent]
|
|
1882
|
-
* @returns {string}
|
|
1883
|
-
*/
|
|
1884
|
-
export function childSeparator(preformatted: boolean | undefined, indent = " "): string {
|
|
1885
|
-
return preformatted ? "" : `\n${indent}`;
|
|
1886
|
-
}
|
|
1887
|
-
|
|
1888
1877
|
/**
|
|
1889
1878
|
* A {@link CompileContext} as a static emitter hands it to a node, before that node's own scope is
|
|
1890
1879
|
* known: the scope in effect may still be none.
|
|
@@ -1934,12 +1923,13 @@ export function buildInner(
|
|
|
1934
1923
|
}
|
|
1935
1924
|
if (Array.isArray(source.children)) {
|
|
1936
1925
|
const rawChildren = raw?.children;
|
|
1937
|
-
return
|
|
1938
|
-
.map((c, i) => {
|
|
1926
|
+
return joinChildren(
|
|
1927
|
+
source.children.map((c, i) => {
|
|
1939
1928
|
const childRaw = (Array.isArray(rawChildren) ? rawChildren[i] : undefined) ?? c;
|
|
1940
1929
|
return childCompiler(c, childRaw, context);
|
|
1941
|
-
})
|
|
1942
|
-
|
|
1930
|
+
}),
|
|
1931
|
+
{ preformatted: context.preformatted },
|
|
1932
|
+
);
|
|
1943
1933
|
}
|
|
1944
1934
|
return "";
|
|
1945
1935
|
}
|
|
@@ -3449,7 +3439,7 @@ export function createSlotFill(
|
|
|
3449
3439
|
}
|
|
3450
3440
|
const group = groups.get(name);
|
|
3451
3441
|
if (group) {
|
|
3452
|
-
group.html = group.html
|
|
3442
|
+
group.html = joinChildren([group.html, html], { separator: "\n" });
|
|
3453
3443
|
group.content ||= content;
|
|
3454
3444
|
} else {
|
|
3455
3445
|
groups.set(name, { content, html });
|
|
@@ -3713,11 +3703,12 @@ function renderInner(
|
|
|
3713
3703
|
return renderMappedRows(node.children, scope, slots, context, preformatted);
|
|
3714
3704
|
}
|
|
3715
3705
|
if (Array.isArray(node.children)) {
|
|
3716
|
-
return
|
|
3717
|
-
.map((c: JxElement | JxMutableNode | string) =>
|
|
3706
|
+
return joinChildren(
|
|
3707
|
+
node.children.map((c: JxElement | JxMutableNode | string) =>
|
|
3718
3708
|
renderStaticNode(c, scope, slots, context, preformatted),
|
|
3719
|
-
)
|
|
3720
|
-
|
|
3709
|
+
),
|
|
3710
|
+
{ preformatted, separator: "\n" },
|
|
3711
|
+
);
|
|
3721
3712
|
}
|
|
3722
3713
|
return "";
|
|
3723
3714
|
}
|
|
@@ -3763,15 +3754,16 @@ export function renderMappedRows(
|
|
|
3763
3754
|
if (rows === null) {
|
|
3764
3755
|
return "";
|
|
3765
3756
|
}
|
|
3766
|
-
return
|
|
3767
|
-
.map((item, index) => {
|
|
3757
|
+
return joinChildren(
|
|
3758
|
+
rows.map((item, index) => {
|
|
3768
3759
|
const rowScope = emptyScope(parent);
|
|
3769
3760
|
rowScope.$map = { index, item };
|
|
3770
3761
|
rowScope["$map/item"] = item;
|
|
3771
3762
|
rowScope["$map/index"] = index;
|
|
3772
3763
|
return renderStaticNode(template, rowScope, slots, context, preformatted);
|
|
3773
|
-
})
|
|
3774
|
-
|
|
3764
|
+
}),
|
|
3765
|
+
{ preformatted, separator: "\n" },
|
|
3766
|
+
);
|
|
3775
3767
|
}
|
|
3776
3768
|
|
|
3777
3769
|
/**
|
|
@@ -3861,9 +3853,10 @@ export function renderStaticNode(
|
|
|
3861
3853
|
return escapeHtml(String(node));
|
|
3862
3854
|
}
|
|
3863
3855
|
if (Array.isArray(node)) {
|
|
3864
|
-
return
|
|
3865
|
-
.map((c): string => renderStaticNode(c, scope, slots, context, preformatted))
|
|
3866
|
-
|
|
3856
|
+
return joinChildren(
|
|
3857
|
+
node.map((c): string => renderStaticNode(c, scope, slots, context, preformatted)),
|
|
3858
|
+
{ preformatted, separator: "\n" },
|
|
3859
|
+
);
|
|
3867
3860
|
}
|
|
3868
3861
|
if (!isNonNullObject(node)) {
|
|
3869
3862
|
return "";
|
|
@@ -3918,7 +3911,7 @@ export function renderStaticNode(
|
|
|
3918
3911
|
return out;
|
|
3919
3912
|
}
|
|
3920
3913
|
const fallback = renderInner(node, scope, slots, context, preformatted);
|
|
3921
|
-
return out
|
|
3914
|
+
return joinChildren([out, fallback], { preformatted, separator: "\n" });
|
|
3922
3915
|
}
|
|
3923
3916
|
|
|
3924
3917
|
// A registered component: expanded exactly as the page walk expands one, not emitted bare.
|
|
@@ -4031,7 +4024,10 @@ function renderComponentInstance(
|
|
|
4031
4024
|
if (shadow) {
|
|
4032
4025
|
// The light children stay siblings of the template, where a real `<slot>` distributes them.
|
|
4033
4026
|
const light = Array.isArray(children)
|
|
4034
|
-
?
|
|
4027
|
+
? joinChildren(
|
|
4028
|
+
children.map((c: StaticChild) => renderChild(c)),
|
|
4029
|
+
{ separator: "\n" },
|
|
4030
|
+
)
|
|
4035
4031
|
: "";
|
|
4036
4032
|
innerHTML =
|
|
4037
4033
|
`<template shadowrootmode="${shadow}">` +
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The build's own timestamp, for the few generated files that have no date of their own to report.
|
|
3
|
+
*
|
|
4
|
+
* Follows the reproducible-builds convention: when `SOURCE_DATE_EPOCH` is set the build time is
|
|
5
|
+
* that instant and the clock is never read, so rebuilding one commit writes the same bytes.
|
|
6
|
+
* Otherwise it is the clock at the moment the build asks. The one exception is a
|
|
7
|
+
* scheduled-publishing rule's `$now`, which is deliberately the real clock (site-architecture.md
|
|
8
|
+
* §7.6).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** `SOURCE_DATE_EPOCH` is a decimal count of seconds since the Unix epoch, and nothing else. */
|
|
12
|
+
const EPOCH_SECONDS = /^\d+$/;
|
|
13
|
+
|
|
14
|
+
/** A build instant as RFC 3339 at second precision in UTC, the shape a feed's `<updated>` takes. */
|
|
15
|
+
export function formatBuildTime(instant: Date): string {
|
|
16
|
+
return instant.toISOString().replace(/\.\d{3}Z$/, "Z");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The build's timestamp.
|
|
21
|
+
*
|
|
22
|
+
* @param {Readonly<Record<string, string | undefined>>} env The environment to read
|
|
23
|
+
* `SOURCE_DATE_EPOCH` from.
|
|
24
|
+
* @param {() => Date} clock Read only when `SOURCE_DATE_EPOCH` is unset.
|
|
25
|
+
* @returns {string} RFC 3339, UTC, second precision.
|
|
26
|
+
* @throws {RangeError} When `SOURCE_DATE_EPOCH` is set but is not a non-negative integer number of
|
|
27
|
+
* seconds. The convention says to fail rather than guess, because a value silently replaced by
|
|
28
|
+
* the clock would make a build that was asked to be reproducible quietly not be.
|
|
29
|
+
*/
|
|
30
|
+
export function resolveBuildTime(
|
|
31
|
+
env: Readonly<Record<string, string | undefined>>,
|
|
32
|
+
clock: () => Date = () => new Date(),
|
|
33
|
+
): string {
|
|
34
|
+
const raw = env.SOURCE_DATE_EPOCH;
|
|
35
|
+
if (raw === undefined || raw === "") {
|
|
36
|
+
return formatBuildTime(clock());
|
|
37
|
+
}
|
|
38
|
+
const instant = new Date(Number(raw) * 1000);
|
|
39
|
+
if (!EPOCH_SECONDS.test(raw) || Number.isNaN(instant.getTime())) {
|
|
40
|
+
throw new RangeError(
|
|
41
|
+
`SOURCE_DATE_EPOCH must be a non-negative integer number of seconds, got "${raw}"`,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
return formatBuildTime(instant);
|
|
45
|
+
}
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
import { dirname, relative, resolve } from "node:path";
|
|
13
13
|
import { injectContext as injectContextWith } from "@jxsuite/site/context";
|
|
14
14
|
import type { ImportRebaser, SiteRoute, TranslationMember } from "@jxsuite/site/context";
|
|
15
|
+
import type { PageFacts } from "@jxsuite/site/layout";
|
|
15
16
|
import type { ResolvedI18n } from "@jxsuite/schema/locale";
|
|
16
17
|
import type { JxDocument, ProjectConfig } from "@jxsuite/schema/types";
|
|
17
18
|
|
|
@@ -34,6 +35,7 @@ export function nodeImportRebaser(projectRoot: string): ImportRebaser {
|
|
|
34
35
|
* @param {string | null} [projectRoot] - Absolute path to the project root (for import rebasing)
|
|
35
36
|
* @param {ResolvedI18n | null} [i18n] - Validated locale config, when the project declares one
|
|
36
37
|
* @param {readonly TranslationMember[]} [translations] - This route's translation set
|
|
38
|
+
* @param {PageFacts} [page] - What the page says about itself, read before its layout wrapped it
|
|
37
39
|
* @returns {JxDocument} The mutated document
|
|
38
40
|
*/
|
|
39
41
|
export function injectContext(
|
|
@@ -43,6 +45,7 @@ export function injectContext(
|
|
|
43
45
|
projectRoot: string | null = null,
|
|
44
46
|
i18n: ResolvedI18n | null = null,
|
|
45
47
|
translations: readonly TranslationMember[] = [],
|
|
48
|
+
page?: PageFacts,
|
|
46
49
|
): JxDocument {
|
|
47
50
|
return injectContextWith(
|
|
48
51
|
doc,
|
|
@@ -51,5 +54,6 @@ export function injectContext(
|
|
|
51
54
|
projectRoot === null ? null : nodeImportRebaser(projectRoot),
|
|
52
55
|
i18n,
|
|
53
56
|
translations,
|
|
57
|
+
page,
|
|
54
58
|
);
|
|
55
59
|
}
|
package/src/site/csp.ts
CHANGED
|
@@ -198,6 +198,103 @@ export function normalizeCspConfig(security: HeadersConfig["security"]): CspConf
|
|
|
198
198
|
return csp;
|
|
199
199
|
}
|
|
200
200
|
|
|
201
|
+
/**
|
|
202
|
+
* The directive a browser consults when a fetch directive is absent, nearest first. Only fetch
|
|
203
|
+
* directives fall back at all: `frame-ancestors`, `base-uri`, `form-action` and the reporting
|
|
204
|
+
* directives do not, and giving them `default-src`'s sources would invent a policy the author never
|
|
205
|
+
* wrote.
|
|
206
|
+
*/
|
|
207
|
+
const FALLBACKS = new Map<string, string[]>([
|
|
208
|
+
["child-src", ["default-src"]],
|
|
209
|
+
["connect-src", ["default-src"]],
|
|
210
|
+
["font-src", ["default-src"]],
|
|
211
|
+
["frame-src", ["child-src", "default-src"]],
|
|
212
|
+
["img-src", ["default-src"]],
|
|
213
|
+
["manifest-src", ["default-src"]],
|
|
214
|
+
["media-src", ["default-src"]],
|
|
215
|
+
["object-src", ["default-src"]],
|
|
216
|
+
["prefetch-src", ["default-src"]],
|
|
217
|
+
["script-src", ["default-src"]],
|
|
218
|
+
["script-src-attr", ["script-src", "default-src"]],
|
|
219
|
+
["script-src-elem", ["script-src", "default-src"]],
|
|
220
|
+
["style-src", ["default-src"]],
|
|
221
|
+
["style-src-attr", ["style-src", "default-src"]],
|
|
222
|
+
["style-src-elem", ["style-src", "default-src"]],
|
|
223
|
+
["worker-src", ["child-src", "script-src", "default-src"]],
|
|
224
|
+
]);
|
|
225
|
+
|
|
226
|
+
/** A `sources` entry as source expressions: a string may carry several, space separated. */
|
|
227
|
+
function sourceTokens(value: string | string[]): string[] {
|
|
228
|
+
return (Array.isArray(value) ? value : [value])
|
|
229
|
+
.flatMap((entry) => entry.split(/\s+/))
|
|
230
|
+
.filter(Boolean);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* What is wrong with `config.sources`, as build errors. A source containing `;` or `,` would end
|
|
235
|
+
* the directive early and smuggle in another, so it is refused rather than emitted. A directive
|
|
236
|
+
* named in both `sources` and `directives` is refused too: the replacement is wholesale, so the
|
|
237
|
+
* sources would be silently dropped or silently kept, and either reads as the other.
|
|
238
|
+
*/
|
|
239
|
+
export function cspConfigErrors(config: CspConfig): string[] {
|
|
240
|
+
const errors: string[] = [];
|
|
241
|
+
const replaced = new Set(Object.keys(config.directives ?? {}).map((name) => name.toLowerCase()));
|
|
242
|
+
for (const [name, value] of Object.entries(config.sources ?? {})) {
|
|
243
|
+
const path = `build.headers.security.csp.sources["${name}"]`;
|
|
244
|
+
if (!/^[A-Za-z][A-Za-z0-9-]*$/.test(name)) {
|
|
245
|
+
errors.push(`${path}: not a CSP directive name.`);
|
|
246
|
+
}
|
|
247
|
+
if (replaced.has(name.toLowerCase())) {
|
|
248
|
+
errors.push(
|
|
249
|
+
`${path}: csp.directives also sets "${name}" and replaces it wholesale, so put the ` +
|
|
250
|
+
"sources in that string, or remove one of the two.",
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
for (const token of sourceTokens(value)) {
|
|
254
|
+
if (/[;,]/.test(token)) {
|
|
255
|
+
errors.push(`${path}: "${token}" contains ";" or "," and would split the directive.`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return errors;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Append `config.sources` to the computed directives, in place.
|
|
264
|
+
*
|
|
265
|
+
* A source already in the directive is skipped, comparing exactly because a hash is case-sensitive.
|
|
266
|
+
* `'none'` is dropped when anything is added: next to another source a browser ignores it, so
|
|
267
|
+
* keeping it would only make the policy read as stricter than it is.
|
|
268
|
+
*/
|
|
269
|
+
function appendSources(directives: Record<string, string | false>, config: CspConfig): void {
|
|
270
|
+
const replaced = new Set(Object.keys(config.directives ?? {}).map((name) => name.toLowerCase()));
|
|
271
|
+
for (const [rawName, value] of Object.entries(config.sources ?? {})) {
|
|
272
|
+
const name = rawName.toLowerCase();
|
|
273
|
+
const added = sourceTokens(value);
|
|
274
|
+
if (replaced.has(name) || added.length === 0) {
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
let base = directives[name];
|
|
278
|
+
if (base === undefined || base === false) {
|
|
279
|
+
base = "";
|
|
280
|
+
for (const fallback of FALLBACKS.get(name) ?? []) {
|
|
281
|
+
const inherited = directives[fallback];
|
|
282
|
+
if (inherited !== undefined && inherited !== false) {
|
|
283
|
+
base = inherited;
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
const merged = base.split(/\s+/).filter(Boolean);
|
|
289
|
+
for (const token of added) {
|
|
290
|
+
if (!merged.includes(token)) {
|
|
291
|
+
merged.push(token);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
directives[name] = merged.filter((token) => token !== "'none'").join(" ");
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
201
298
|
/**
|
|
202
299
|
* Build the policy. Returns the headers to add, which is more than one when reporting is on:
|
|
203
300
|
* `report-to` names an endpoint group that `Reporting-Endpoints` has to define.
|
|
@@ -245,6 +342,11 @@ export function buildCspHeaders(
|
|
|
245
342
|
for (const [name, value] of Object.entries<string | false>(config.directives ?? {})) {
|
|
246
343
|
directives[name] = value;
|
|
247
344
|
}
|
|
345
|
+
/*
|
|
346
|
+
* After the overrides, so a fallback reads the `default-src` the author actually set. A directive
|
|
347
|
+
* in `directives` is skipped: `cspConfigErrors` already refused the pair.
|
|
348
|
+
*/
|
|
349
|
+
appendSources(directives, config);
|
|
248
350
|
|
|
249
351
|
const policy = Object.entries(directives)
|
|
250
352
|
.filter(([, value]) => value !== false && value !== "")
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
import { withBase } from "@jxsuite/schema/asset-paths";
|
|
15
15
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
16
16
|
import { join } from "node:path";
|
|
17
|
-
import { buildCspHeaders, normalizeCspConfig } from "./csp.ts";
|
|
17
|
+
import { buildCspHeaders, cspConfigErrors, normalizeCspConfig } from "./csp.ts";
|
|
18
18
|
import type { CspSources } from "./csp.ts";
|
|
19
19
|
import type { HeadersConfig, ProjectConfig } from "@jxsuite/schema/types";
|
|
20
20
|
|
|
@@ -125,6 +125,7 @@ export function buildHeaderRules(
|
|
|
125
125
|
"policy would block every one of them.",
|
|
126
126
|
);
|
|
127
127
|
} else {
|
|
128
|
+
errors.push(...cspConfigErrors(cspConfig));
|
|
128
129
|
Object.assign(wide, buildCspHeaders(cspSources, cspConfig, security.frameOptions));
|
|
129
130
|
}
|
|
130
131
|
}
|
|
@@ -252,7 +252,7 @@ function isAllowedRemote(src: string, config: ImageConfig) {
|
|
|
252
252
|
* @param {readonly AssetMount[]} [mounts]
|
|
253
253
|
* @returns {string}
|
|
254
254
|
*/
|
|
255
|
-
function resolveImagePath(src: string, projectRoot: string, mounts?: readonly AssetMount[]) {
|
|
255
|
+
export function resolveImagePath(src: string, projectRoot: string, mounts?: readonly AssetMount[]) {
|
|
256
256
|
const mounted = mounts?.length ? resolveAssetUrl(mounts, src) : null;
|
|
257
257
|
if (mounted) {
|
|
258
258
|
return mounted;
|