@anthusai/papyrus 2.0.0-next.4 → 2.0.0-next.40

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.
Files changed (118) hide show
  1. package/amplify/data/resource.js +22 -3
  2. package/amplify/data/resource.ts +24 -3
  3. package/amplify/data/schema.js +2029 -1967
  4. package/amplify/data/schema.ts +72 -2
  5. package/amplify/functions/assignment-action/data-access.js +88 -0
  6. package/amplify/functions/assignment-action/data-access.ts +87 -0
  7. package/amplify/functions/content-actions/handler.py +33 -0
  8. package/amplify/functions/content-actions/requirements.txt +1 -0
  9. package/amplify/functions/content-actions/resource.js +1 -1
  10. package/amplify/functions/content-actions/resource.ts +1 -1
  11. package/amplify/functions/editorial-checks/handler.py +14 -0
  12. package/amplify/functions/editorial-checks/requirements.txt +1 -0
  13. package/amplify/functions/editorial-checks/resource.js +43 -0
  14. package/amplify/functions/editorial-checks/resource.ts +60 -0
  15. package/amplify/functions/editorial-checks/schema.js +121 -0
  16. package/amplify/functions/editorial-checks/schema.ts +123 -0
  17. package/amplify/functions/editorial-checks/wire.js +53 -0
  18. package/amplify/functions/editorial-checks/wire.ts +61 -0
  19. package/amplify/functions/monitoring-view/handler.py +11 -0
  20. package/amplify/functions/monitoring-view/resource.js +24 -0
  21. package/amplify/functions/monitoring-view/resource.ts +34 -0
  22. package/amplify/functions/monitoring-view/schema.js +16 -0
  23. package/amplify/functions/monitoring-view/schema.ts +18 -0
  24. package/amplify/functions/monitoring-view/wire.js +37 -0
  25. package/amplify/functions/monitoring-view/wire.ts +47 -0
  26. package/amplify/functions/public-forms/core.js +247 -0
  27. package/amplify/functions/public-forms/core.ts +310 -0
  28. package/amplify/functions/public-forms/handler.ts +41 -0
  29. package/amplify/functions/public-forms/origins.js +35 -0
  30. package/amplify/functions/public-forms/origins.ts +43 -0
  31. package/amplify/functions/public-forms/resource.js +9 -0
  32. package/amplify/functions/public-forms/resource.ts +10 -0
  33. package/amplify/functions/public-forms/wire.js +25 -0
  34. package/amplify/functions/public-forms/wire.ts +32 -0
  35. package/amplify/functions/shared/python-bundle.js +20 -14
  36. package/amplify/functions/shared/python-bundle.ts +25 -18
  37. package/amplify/publication/redirect-and-index.js +105 -0
  38. package/amplify/publication/redirect-rules.js +73 -0
  39. package/amplify/publication/redirect-rules.ts +93 -0
  40. package/amplify/publication/s3-projection.js +7 -1
  41. package/amplify/publication/s3-projection.ts +9 -1
  42. package/amplify/site-backend-config.ts +17 -0
  43. package/amplify/site-backend.js +36 -1
  44. package/amplify/site-backend.ts +44 -1
  45. package/app/api/newsroom/curation/context/route.ts +9 -0
  46. package/app/api/newsroom/curation/priming/route.ts +9 -0
  47. package/app/api/newsroom/curation/publication/route.ts +9 -0
  48. package/bin/papyrus-infra.mjs +20 -1
  49. package/components/editorial-checks-admin-panel.tsx +362 -0
  50. package/components/editorial-finding-card.tsx +162 -0
  51. package/components/editorial-findings-panel.tsx +91 -0
  52. package/components/editorial-preview-popover.tsx +103 -0
  53. package/components/markus-body-editor.tsx +213 -0
  54. package/components/markus-ir-preview.tsx +147 -6
  55. package/components/monitoring-overview-view.tsx +241 -0
  56. package/components/monitoring-panel.tsx +40 -0
  57. package/components/newsroom-article-editor.tsx +136 -11
  58. package/components/newsroom-board-view.tsx +6 -7
  59. package/components/newsroom-curation-bootstrap.tsx +45 -0
  60. package/components/newsroom-ops-shell.tsx +7 -3
  61. package/components/newsroom-organizer.css +74 -61
  62. package/components/newsroom-references-view.tsx +14 -14
  63. package/components/newsroom-theme-fonts/broadsheet-green.css +10 -0
  64. package/components/newsroom-theme-fonts/broadsheet-green.tsx +6 -0
  65. package/components/newsroom-theme-fonts/newspaper-gothic.css +9 -0
  66. package/components/newsroom-theme-fonts/newspaper-gothic.tsx +6 -0
  67. package/components/newsroom-theme-fonts/night-desk.css +10 -0
  68. package/components/newsroom-theme-fonts/night-desk.tsx +6 -0
  69. package/components/newsroom-theme-fonts/refined-newsprint.css +11 -0
  70. package/components/newsroom-theme-fonts/refined-newsprint.tsx +6 -0
  71. package/components/newsroom-theme.css +39 -0
  72. package/components/newsroom-theme.tsx +33 -0
  73. package/components/publication-cyclotron-panel.tsx +15 -10
  74. package/components/reference-add-entry.tsx +75 -0
  75. package/components/reference-curation-controls.tsx +31 -22
  76. package/components/reference-cyclotron-priming.tsx +4 -4
  77. package/components/reference-cyclotron-suggestion.tsx +1 -1
  78. package/components/reference-priming.tsx +14 -10
  79. package/components/reference-section-suggestion.tsx +1 -1
  80. package/components/topic-steering-workspace.tsx +27 -2
  81. package/components/use-editorial-checks.ts +133 -0
  82. package/infra/amplify-app-shell.js +76 -16
  83. package/infra/amplify-app-shell.ts +86 -18
  84. package/infra/index.js +1 -0
  85. package/infra/secure-string-parameter-handler.js +41 -0
  86. package/infra/secure-string-parameter-handler.ts +41 -0
  87. package/infra/secure-string-parameter.js +79 -0
  88. package/infra/secure-string-parameter.ts +91 -0
  89. package/infra/site-config.js +75 -6
  90. package/infra/site-config.ts +106 -6
  91. package/lib/category-repository.ts +11 -9
  92. package/lib/curation-routes.ts +345 -0
  93. package/lib/editorial-checks-backend.ts +16 -0
  94. package/lib/editorial-checks-client.ts +212 -0
  95. package/lib/editorial-checks-demo.ts +147 -0
  96. package/lib/editorial-checks-settings.ts +27 -0
  97. package/lib/editorial-checks.ts +171 -0
  98. package/lib/editorial-preview.ts +228 -0
  99. package/lib/monitoring-client.ts +29 -0
  100. package/lib/monitoring-overview-fixture.ts +51 -0
  101. package/lib/monitoring-overview.ts +145 -0
  102. package/lib/newsroom-board.ts +27 -2
  103. package/lib/prospectus-session.ts +14 -14
  104. package/lib/prospectus-sign-in-routes.ts +52 -11
  105. package/lib/relevance-status.ts +2 -2
  106. package/lib/site-brand-tenant.ts +11 -6
  107. package/lib/site-brand.ts +23 -3
  108. package/lib/site-origin.ts +67 -0
  109. package/middleware.ts +14 -3
  110. package/package.json +48 -4
  111. package/routes.manifest.json +34 -1
  112. package/components/newsroom-fonts/playfair-display-700.ttf +0 -0
  113. package/components/newsroom-fonts/playfair-display-900.ttf +0 -0
  114. package/components/newsroom-fonts/playfair-display-OFL.txt +0 -93
  115. package/components/newsroom-fonts/plus-jakarta-sans-400.ttf +0 -0
  116. package/components/newsroom-fonts/plus-jakarta-sans-600.ttf +0 -0
  117. package/components/newsroom-fonts/plus-jakarta-sans-OFL.txt +0 -93
  118. package/components/newsroom-newsprint-board.css +0 -7
@@ -0,0 +1,9 @@
1
+ import { defineFunction } from "@aws-amplify/backend";
2
+ /** The public form receiving endpoint. Wired (table, Function URL, origins, output) by `wire.ts`. */
3
+ export const publicForms = defineFunction({
4
+ name: "papyrus-public-forms",
5
+ entry: "./handler.ts",
6
+ timeoutSeconds: 10,
7
+ memoryMB: 256,
8
+ resourceGroupName: "data",
9
+ });
@@ -0,0 +1,10 @@
1
+ import { defineFunction } from "@aws-amplify/backend";
2
+
3
+ /** The public form receiving endpoint. Wired (table, Function URL, origins, output) by `wire.ts`. */
4
+ export const publicForms = defineFunction({
5
+ name: "papyrus-public-forms",
6
+ entry: "./handler.ts",
7
+ timeoutSeconds: 10,
8
+ memoryMB: 256,
9
+ resourceGroupName: "data",
10
+ });
@@ -0,0 +1,25 @@
1
+ import { RemovalPolicy, Stack } from "aws-cdk-lib";
2
+ import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
3
+ import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
4
+ import { FunctionUrlAuthType } from "aws-cdk-lib/aws-lambda";
5
+ export const PUBLIC_FORMS_OUTPUT_KEY = "publicForms";
6
+ /**
7
+ * Wires the endpoint: the rate-limit counter table (TTL), the Function URL and the function's settings, and returns
8
+ * the URL for the `custom.publicForms.endpoint` output. Everything lives in the function's own stack.
9
+ */
10
+ export function wirePublicForms(backend, wiring) {
11
+ const lambda = backend.publicForms.resources.lambda;
12
+ const stack = Stack.of(lambda);
13
+ const table = new dynamodb.Table(stack, "PublicFormsLimits", {
14
+ partitionKey: { name: "pk", type: dynamodb.AttributeType.STRING },
15
+ billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
16
+ timeToLiveAttribute: "expiresAt",
17
+ removalPolicy: RemovalPolicy.DESTROY,
18
+ });
19
+ lambda.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: ["dynamodb:UpdateItem"], resources: [table.tableArn] }));
20
+ backend.publicForms.addEnvironment("PAPYRUS_PUBLIC_FORMS_TABLE", table.tableName);
21
+ backend.publicForms.addEnvironment("PAPYRUS_PUBLIC_FORMS_ORIGINS", wiring.allowedOrigins.join(","));
22
+ backend.publicForms.addEnvironment("PAPYRUS_PUBLIC_FORMS_HONEYPOTS", (wiring.honeypotFields ?? []).join(","));
23
+ // CORS is answered by the handler (it must echo exactly one allowed origin), so the URL carries no CORS block.
24
+ return lambda.addFunctionUrl({ authType: FunctionUrlAuthType.NONE }).url;
25
+ }
@@ -0,0 +1,32 @@
1
+ import { RemovalPolicy, Stack } from "aws-cdk-lib";
2
+ import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
3
+ import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
4
+ import { type Function as LambdaFunction, FunctionUrlAuthType } from "aws-cdk-lib/aws-lambda";
5
+
6
+ export const PUBLIC_FORMS_OUTPUT_KEY = "publicForms";
7
+
8
+ export type PublicFormsWiring = {
9
+ allowedOrigins: string[];
10
+ honeypotFields?: string[];
11
+ };
12
+
13
+ /**
14
+ * Wires the endpoint: the rate-limit counter table (TTL), the Function URL and the function's settings, and returns
15
+ * the URL for the `custom.publicForms.endpoint` output. Everything lives in the function's own stack.
16
+ */
17
+ export function wirePublicForms(backend: { publicForms: { resources: { lambda: unknown }; addEnvironment: (name: string, value: string) => void } }, wiring: PublicFormsWiring): string {
18
+ const lambda = backend.publicForms.resources.lambda as LambdaFunction;
19
+ const stack = Stack.of(lambda);
20
+ const table = new dynamodb.Table(stack, "PublicFormsLimits", {
21
+ partitionKey: { name: "pk", type: dynamodb.AttributeType.STRING },
22
+ billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
23
+ timeToLiveAttribute: "expiresAt",
24
+ removalPolicy: RemovalPolicy.DESTROY,
25
+ });
26
+ lambda.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: ["dynamodb:UpdateItem"], resources: [table.tableArn] }));
27
+ backend.publicForms.addEnvironment("PAPYRUS_PUBLIC_FORMS_TABLE", table.tableName);
28
+ backend.publicForms.addEnvironment("PAPYRUS_PUBLIC_FORMS_ORIGINS", wiring.allowedOrigins.join(","));
29
+ backend.publicForms.addEnvironment("PAPYRUS_PUBLIC_FORMS_HONEYPOTS", (wiring.honeypotFields ?? []).join(","));
30
+ // CORS is answered by the handler (it must echo exactly one allowed origin), so the URL carries no CORS block.
31
+ return lambda.addFunctionUrl({ authType: FunctionUrlAuthType.NONE }).url;
32
+ }
@@ -78,23 +78,29 @@ export function siteFilesContentHash(siteFiles) {
78
78
  function pipSpec() {
79
79
  return (process.env.PAPYRUS_PIP_SPEC ?? "").trim() || `papyrus-newsroom==${toPep440(papyrusVersion())}`;
80
80
  }
81
+ /** What the first pip install gets: `papyrus-newsroom` with its dependencies, plus the function's own requirements. Empty means skip it. */
82
+ export function firstPipRequirements(options) {
83
+ return [...(options.noDeps ? [] : [pipSpec()]), ...(options.requirements ?? [])];
84
+ }
81
85
  function bundleFromPip(outputDir, options) {
82
86
  const extra = (process.env.PAPYRUS_PIP_EXTRA_ARGS ?? "").trim().split(/\s+/).filter(Boolean);
83
87
  const python = (process.env.PAPYRUS_PIP_PYTHON ?? "python3").trim();
84
- execFileSync(python, [
85
- "-m", "pip", "install",
86
- "--target", outputDir,
87
- "--no-cache-dir",
88
- "--platform", "manylinux2014_aarch64",
89
- "--implementation", "cp",
90
- "--python-version", "3.12",
91
- "--only-binary=:all:",
92
- // Older pip (e.g. a Python 3.9 build image) checks Requires-Python against the host interpreter, not --python-version.
93
- "--ignore-requires-python",
94
- ...extra,
95
- ...(options.noDeps ? [] : [pipSpec()]),
96
- ...(options.requirements ?? []),
97
- ], { stdio: "inherit" });
88
+ const requirements = firstPipRequirements(options);
89
+ if (requirements.length > 0) {
90
+ execFileSync(python, [
91
+ "-m", "pip", "install",
92
+ "--target", outputDir,
93
+ "--no-cache-dir",
94
+ "--platform", "manylinux2014_aarch64",
95
+ "--implementation", "cp",
96
+ "--python-version", "3.12",
97
+ "--only-binary=:all:",
98
+ // Older pip (e.g. a Python 3.9 build image) checks Requires-Python against the host interpreter, not --python-version.
99
+ "--ignore-requires-python",
100
+ ...extra,
101
+ ...requirements,
102
+ ], { stdio: "inherit" });
103
+ }
98
104
  if (options.noDeps) {
99
105
  execFileSync(python, [
100
106
  "-m", "pip", "install",
@@ -102,27 +102,34 @@ function pipSpec(): string {
102
102
  return (process.env.PAPYRUS_PIP_SPEC ?? "").trim() || `papyrus-newsroom==${toPep440(papyrusVersion())}`;
103
103
  }
104
104
 
105
+ /** What the first pip install gets: `papyrus-newsroom` with its dependencies, plus the function's own requirements. Empty means skip it. */
106
+ export function firstPipRequirements(options: Pick<PythonBundleOptions, "noDeps" | "requirements">): string[] {
107
+ return [...(options.noDeps ? [] : [pipSpec()]), ...(options.requirements ?? [])];
108
+ }
109
+
105
110
  function bundleFromPip(outputDir: string, options: PythonBundleOptions): void {
106
111
  const extra = (process.env.PAPYRUS_PIP_EXTRA_ARGS ?? "").trim().split(/\s+/).filter(Boolean);
107
112
  const python = (process.env.PAPYRUS_PIP_PYTHON ?? "python3").trim();
108
- execFileSync(
109
- python,
110
- [
111
- "-m", "pip", "install",
112
- "--target", outputDir,
113
- "--no-cache-dir",
114
- "--platform", "manylinux2014_aarch64",
115
- "--implementation", "cp",
116
- "--python-version", "3.12",
117
- "--only-binary=:all:",
118
- // Older pip (e.g. a Python 3.9 build image) checks Requires-Python against the host interpreter, not --python-version.
119
- "--ignore-requires-python",
120
- ...extra,
121
- ...(options.noDeps ? [] : [pipSpec()]),
122
- ...(options.requirements ?? []),
123
- ],
124
- { stdio: "inherit" },
125
- );
113
+ const requirements = firstPipRequirements(options);
114
+ if (requirements.length > 0) {
115
+ execFileSync(
116
+ python,
117
+ [
118
+ "-m", "pip", "install",
119
+ "--target", outputDir,
120
+ "--no-cache-dir",
121
+ "--platform", "manylinux2014_aarch64",
122
+ "--implementation", "cp",
123
+ "--python-version", "3.12",
124
+ "--only-binary=:all:",
125
+ // Older pip (e.g. a Python 3.9 build image) checks Requires-Python against the host interpreter, not --python-version.
126
+ "--ignore-requires-python",
127
+ ...extra,
128
+ ...requirements,
129
+ ],
130
+ { stdio: "inherit" },
131
+ );
132
+ }
126
133
  if (options.noDeps) {
127
134
  execFileSync(
128
135
  python,
@@ -12,6 +12,9 @@ async function lookUpRedirectTarget(uri) {
12
12
  }
13
13
 
14
14
  function rewriteToIndexDocument(uri) {
15
+ if (uri === "/404") {
16
+ return "/404.html";
17
+ }
15
18
  if (uri.endsWith("/")) {
16
19
  return uri + "index.html";
17
20
  }
@@ -22,6 +25,88 @@ function rewriteToIndexDocument(uri) {
22
25
  return uri;
23
26
  }
24
27
 
28
+ // Pattern redirects: data from the site's projection.json, spliced between the markers at deploy
29
+ // time by redirect-rules.ts. Each rule is {p: regex, t: target with $1..$9, s: status, x: excluded paths}.
30
+ const patternRules = /*PAPYRUS_PATTERN_RULES*/[]/*END_PAPYRUS_PATTERN_RULES*/;
31
+
32
+ function lookUpPatternRedirect(uri) {
33
+ for (let index = 0; index < patternRules.length; index += 1) {
34
+ const rule = patternRules[index];
35
+ if (rule.x.indexOf(uri) !== -1) {
36
+ continue;
37
+ }
38
+ const match = new RegExp(rule.p).exec(uri);
39
+ if (match !== null) {
40
+ return {
41
+ status: rule.s,
42
+ target: rule.t.replace(/\$([0-9])/g, function (whole, group) {
43
+ return match[Number(group)] || "";
44
+ }),
45
+ };
46
+ }
47
+ }
48
+ return null;
49
+ }
50
+
51
+ // Content negotiation for agents: when the site publishes Markdown twins (standard front end, agents.markdown), a request
52
+ // whose Accept header prefers text/markdown over text/html is rewritten to the .md object before the cache lookup. The
53
+ // flag is spliced in by redirect-rules.ts at deploy time.
54
+ const negotiateMarkdown = /*PAPYRUS_NEGOTIATE_MARKDOWN*/false/*END_PAPYRUS_NEGOTIATE_MARKDOWN*/;
55
+
56
+ function qualityOf(accept, mediaType) {
57
+ let quality = 0;
58
+ const parts = accept.toLowerCase().split(",");
59
+ for (let index = 0; index < parts.length; index += 1) {
60
+ const fields = parts[index].split(";");
61
+ if (fields[0].trim() !== mediaType) {
62
+ continue;
63
+ }
64
+ quality = 1;
65
+ for (let field = 1; field < fields.length; field += 1) {
66
+ const pair = fields[field].trim();
67
+ if (pair.indexOf("q=") === 0) {
68
+ quality = Number(pair.substring(2));
69
+ if (isNaN(quality)) {
70
+ quality = 0;
71
+ }
72
+ }
73
+ }
74
+ }
75
+ return quality;
76
+ }
77
+
78
+ function prefersMarkdown(request) {
79
+ const header = request.headers && request.headers.accept;
80
+ if (!header || !header.value) {
81
+ return false;
82
+ }
83
+ const markdown = qualityOf(header.value, "text/markdown");
84
+ return markdown > 0 && markdown > qualityOf(header.value, "text/html");
85
+ }
86
+
87
+ function markdownTwin(uri) {
88
+ if (uri === "/404.html" || uri.length < 6 || uri.substring(uri.length - 5) !== ".html") {
89
+ return null;
90
+ }
91
+ return uri.substring(0, uri.length - 5) + ".md";
92
+ }
93
+
94
+ // Directory-style sites keep a page's twin at /x/index.md, so a bare /x.md is rewritten there. The edge cannot test for
95
+ // existence, hence the rule: a last segment <name>.md with a name other than index. A path prefix (a base path) is just
96
+ // part of <path>. The flag is spliced in by redirect-rules.ts at deploy time from urls.style.
97
+ const directoryTwins = /*PAPYRUS_DIRECTORY_TWINS*/false/*END_PAPYRUS_DIRECTORY_TWINS*/;
98
+
99
+ function directoryTwin(uri) {
100
+ if (uri.length < 5 || uri.substring(uri.length - 3) !== ".md") {
101
+ return null;
102
+ }
103
+ const lastSegment = uri.substring(uri.lastIndexOf("/") + 1);
104
+ if (lastSegment === "index.md" || lastSegment === ".md") {
105
+ return null;
106
+ }
107
+ return uri.substring(0, uri.length - 3) + "/index.md";
108
+ }
109
+
25
110
  async function handler(event) {
26
111
  const request = event.request;
27
112
  const redirectTarget = await lookUpRedirectTarget(request.uri);
@@ -32,6 +117,26 @@ async function handler(event) {
32
117
  headers: { location: { value: redirectTarget } },
33
118
  };
34
119
  }
120
+ const patternRedirect = lookUpPatternRedirect(request.uri);
121
+ if (patternRedirect !== null) {
122
+ return {
123
+ statusCode: patternRedirect.status,
124
+ statusDescription: "Moved",
125
+ headers: { location: { value: patternRedirect.target } },
126
+ };
127
+ }
35
128
  request.uri = rewriteToIndexDocument(request.uri);
129
+ if (directoryTwins) {
130
+ const bareTwin = directoryTwin(request.uri);
131
+ if (bareTwin !== null) {
132
+ request.uri = bareTwin;
133
+ }
134
+ }
135
+ if (negotiateMarkdown && prefersMarkdown(request)) {
136
+ const twin = markdownTwin(request.uri);
137
+ if (twin !== null) {
138
+ request.uri = twin;
139
+ }
140
+ }
36
141
  return request;
37
142
  }
@@ -0,0 +1,73 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { resolve } from "node:path";
3
+ /** Mirrors DIRECTORY_HTML_PATTERN and DIRECTORY_SLASH_PATTERN in standard/config.py. */
4
+ const DIRECTORY_HTML_PATTERN = "^/(?!(?:.*/)?index\\.html$)(.+)\\.html$";
5
+ const DIRECTORY_SLASH_PATTERN = "^/((?:[^/]+/)*[^/.]+)$";
6
+ const START_MARKER = "/*PAPYRUS_PATTERN_RULES*/";
7
+ const END_MARKER = "/*END_PAPYRUS_PATTERN_RULES*/";
8
+ /**
9
+ * The pattern rules a site declares in projection.json: its `redirects.patterns`, plus, for
10
+ * `urls.style: "directory"`, `/x.html` to `/x/`. Mirrors `StandardSettings.edge_rules` in Python.
11
+ */
12
+ export function edgeRulesFromSettings(settings) {
13
+ const rules = (settings.redirects?.patterns ?? []).map((rule) => ({
14
+ p: rule.pattern,
15
+ t: rule.to,
16
+ s: rule.status ?? 301,
17
+ x: rule.except ?? [],
18
+ }));
19
+ if (settings.urls?.style === "directory") {
20
+ // /x.html to /x/ (a directory index file such as /x/index.html is served as it is), then /x to /x/.
21
+ rules.push({ p: DIRECTORY_HTML_PATTERN, t: "/$1/", s: 301, x: ["/404.html"] });
22
+ rules.push({ p: DIRECTORY_SLASH_PATTERN, t: "/$1/", s: 301, x: ["/404"] });
23
+ }
24
+ return rules;
25
+ }
26
+ /** A standard-front-end site publishes Markdown twins unless it turns `agents.markdown` off. */
27
+ export function negotiatesMarkdown(settings) {
28
+ return settings.site !== undefined && settings.agents?.markdown !== false;
29
+ }
30
+ /** A directory-style site keeps a page's Markdown twin at `/x/index.md`; the edge then serves a bare `/x.md` from there. */
31
+ export function usesDirectoryTwins(settings) {
32
+ return negotiatesMarkdown(settings) && settings.urls?.style === "directory";
33
+ }
34
+ export function readEdgeConfig(staticDir, baseDirectory = process.cwd()) {
35
+ if (staticDir === undefined)
36
+ return { rules: [], negotiateMarkdown: false, directoryTwins: false };
37
+ const settingsPath = resolve(baseDirectory, staticDir, "projection.json");
38
+ if (!existsSync(settingsPath))
39
+ return { rules: [], negotiateMarkdown: false, directoryTwins: false };
40
+ const settings = JSON.parse(readFileSync(settingsPath, "utf8"));
41
+ return { rules: edgeRulesFromSettings(settings), negotiateMarkdown: negotiatesMarkdown(settings), directoryTwins: usesDirectoryTwins(settings) };
42
+ }
43
+ export function readEdgeRules(staticDir, baseDirectory = process.cwd()) {
44
+ return readEdgeConfig(staticDir, baseDirectory).rules;
45
+ }
46
+ const NEGOTIATE_START = "/*PAPYRUS_NEGOTIATE_MARKDOWN*/";
47
+ const NEGOTIATE_END = "/*END_PAPYRUS_NEGOTIATE_MARKDOWN*/";
48
+ const TWINS_START = "/*PAPYRUS_DIRECTORY_TWINS*/";
49
+ const TWINS_END = "/*END_PAPYRUS_DIRECTORY_TWINS*/";
50
+ function spliceFlag(source, start, end, label) {
51
+ const from = source.indexOf(start);
52
+ const to = source.indexOf(end);
53
+ if (from === -1 || to === -1 || to < from) {
54
+ throw new Error(`redirect function source has no ${label} markers`);
55
+ }
56
+ return source.slice(0, from + start.length) + "true" + source.slice(to);
57
+ }
58
+ /** The CloudFront Function source with the site's pattern rules and Markdown negotiation flag spliced in. */
59
+ export function compileRedirectFunctionSource(source, rules, options = {}) {
60
+ const start = source.indexOf(START_MARKER);
61
+ const end = source.indexOf(END_MARKER);
62
+ if (start === -1 || end === -1 || end < start) {
63
+ throw new Error("redirect function source has no pattern-rule markers");
64
+ }
65
+ let compiled = rules.length === 0 ? source : source.slice(0, start + START_MARKER.length) + JSON.stringify(rules) + source.slice(end);
66
+ if (options.negotiateMarkdown === true) {
67
+ compiled = spliceFlag(compiled, NEGOTIATE_START, NEGOTIATE_END, "markdown negotiation");
68
+ }
69
+ if (options.directoryTwins === true) {
70
+ compiled = spliceFlag(compiled, TWINS_START, TWINS_END, "directory twin");
71
+ }
72
+ return compiled;
73
+ }
@@ -0,0 +1,93 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { resolve } from "node:path";
3
+
4
+ /** One pattern redirect as the edge function reads it: regex, target with $1..$9, status, excluded paths. */
5
+ export type EdgeRedirectRule = { p: string; t: string; s: number; x: string[] };
6
+
7
+ /** Mirrors DIRECTORY_HTML_PATTERN and DIRECTORY_SLASH_PATTERN in standard/config.py. */
8
+ const DIRECTORY_HTML_PATTERN = "^/(?!(?:.*/)?index\\.html$)(.+)\\.html$";
9
+ const DIRECTORY_SLASH_PATTERN = "^/((?:[^/]+/)*[^/.]+)$";
10
+
11
+ const START_MARKER = "/*PAPYRUS_PATTERN_RULES*/";
12
+ const END_MARKER = "/*END_PAPYRUS_PATTERN_RULES*/";
13
+
14
+ type ProjectionRedirects = { patterns?: { pattern: string; to: string; status?: number; except?: string[] }[] };
15
+ type ProjectionSettings = { site?: unknown; urls?: { style?: string }; redirects?: ProjectionRedirects; agents?: { markdown?: boolean } };
16
+
17
+ /**
18
+ * The pattern rules a site declares in projection.json: its `redirects.patterns`, plus, for
19
+ * `urls.style: "directory"`, `/x.html` to `/x/`. Mirrors `StandardSettings.edge_rules` in Python.
20
+ */
21
+ export function edgeRulesFromSettings(settings: ProjectionSettings): EdgeRedirectRule[] {
22
+ const rules = (settings.redirects?.patterns ?? []).map((rule) => ({
23
+ p: rule.pattern,
24
+ t: rule.to,
25
+ s: rule.status ?? 301,
26
+ x: rule.except ?? [],
27
+ }));
28
+ if (settings.urls?.style === "directory") {
29
+ // /x.html to /x/ (a directory index file such as /x/index.html is served as it is), then /x to /x/.
30
+ rules.push({ p: DIRECTORY_HTML_PATTERN, t: "/$1/", s: 301, x: ["/404.html"] });
31
+ rules.push({ p: DIRECTORY_SLASH_PATTERN, t: "/$1/", s: 301, x: ["/404"] });
32
+ }
33
+ return rules;
34
+ }
35
+
36
+ /** A standard-front-end site publishes Markdown twins unless it turns `agents.markdown` off. */
37
+ export function negotiatesMarkdown(settings: ProjectionSettings): boolean {
38
+ return settings.site !== undefined && settings.agents?.markdown !== false;
39
+ }
40
+
41
+ /** A directory-style site keeps a page's Markdown twin at `/x/index.md`; the edge then serves a bare `/x.md` from there. */
42
+ export function usesDirectoryTwins(settings: ProjectionSettings): boolean {
43
+ return negotiatesMarkdown(settings) && settings.urls?.style === "directory";
44
+ }
45
+
46
+ export type EdgeConfig = { rules: EdgeRedirectRule[]; negotiateMarkdown: boolean; directoryTwins: boolean };
47
+
48
+ export function readEdgeConfig(staticDir: string | undefined, baseDirectory: string = process.cwd()): EdgeConfig {
49
+ if (staticDir === undefined) return { rules: [], negotiateMarkdown: false, directoryTwins: false };
50
+ const settingsPath = resolve(baseDirectory, staticDir, "projection.json");
51
+ if (!existsSync(settingsPath)) return { rules: [], negotiateMarkdown: false, directoryTwins: false };
52
+ const settings = JSON.parse(readFileSync(settingsPath, "utf8")) as ProjectionSettings;
53
+ return { rules: edgeRulesFromSettings(settings), negotiateMarkdown: negotiatesMarkdown(settings), directoryTwins: usesDirectoryTwins(settings) };
54
+ }
55
+
56
+ export function readEdgeRules(staticDir: string | undefined, baseDirectory: string = process.cwd()): EdgeRedirectRule[] {
57
+ return readEdgeConfig(staticDir, baseDirectory).rules;
58
+ }
59
+
60
+ const NEGOTIATE_START = "/*PAPYRUS_NEGOTIATE_MARKDOWN*/";
61
+ const NEGOTIATE_END = "/*END_PAPYRUS_NEGOTIATE_MARKDOWN*/";
62
+ const TWINS_START = "/*PAPYRUS_DIRECTORY_TWINS*/";
63
+ const TWINS_END = "/*END_PAPYRUS_DIRECTORY_TWINS*/";
64
+
65
+ function spliceFlag(source: string, start: string, end: string, label: string): string {
66
+ const from = source.indexOf(start);
67
+ const to = source.indexOf(end);
68
+ if (from === -1 || to === -1 || to < from) {
69
+ throw new Error(`redirect function source has no ${label} markers`);
70
+ }
71
+ return source.slice(0, from + start.length) + "true" + source.slice(to);
72
+ }
73
+
74
+ /** The CloudFront Function source with the site's pattern rules and Markdown negotiation flag spliced in. */
75
+ export function compileRedirectFunctionSource(
76
+ source: string,
77
+ rules: EdgeRedirectRule[],
78
+ options: { negotiateMarkdown?: boolean; directoryTwins?: boolean } = {},
79
+ ): string {
80
+ const start = source.indexOf(START_MARKER);
81
+ const end = source.indexOf(END_MARKER);
82
+ if (start === -1 || end === -1 || end < start) {
83
+ throw new Error("redirect function source has no pattern-rule markers");
84
+ }
85
+ let compiled = rules.length === 0 ? source : source.slice(0, start + START_MARKER.length) + JSON.stringify(rules) + source.slice(end);
86
+ if (options.negotiateMarkdown === true) {
87
+ compiled = spliceFlag(compiled, NEGOTIATE_START, NEGOTIATE_END, "markdown negotiation");
88
+ }
89
+ if (options.directoryTwins === true) {
90
+ compiled = spliceFlag(compiled, TWINS_START, TWINS_END, "directory twin");
91
+ }
92
+ return compiled;
93
+ }
@@ -7,11 +7,13 @@ import { SqsDestination } from "aws-cdk-lib/aws-lambda-destinations";
7
7
  import * as route53 from "aws-cdk-lib/aws-route53";
8
8
  import * as s3 from "aws-cdk-lib/aws-s3";
9
9
  import * as sqs from "aws-cdk-lib/aws-sqs";
10
+ import { readFileSync } from "node:fs";
10
11
  import { dirname, resolve } from "node:path";
11
12
  import { fileURLToPath } from "node:url";
12
13
  import { assertValidPublicationConfig } from "../site-backend-config.js";
13
14
  import { addRenditionsFunction } from "./renditions-function.js";
14
15
  import { RENDITIONS_FUNCTION_NAME_ENV } from "./renditions-settings.js";
16
+ import { compileRedirectFunctionSource, readEdgeConfig } from "./redirect-rules.js";
15
17
  import { addInvalidationPathAlarm } from "./invalidation-alarm.js";
16
18
  import { distributionIdFromArn, s3ProjectionEnvironment, s3ProjectionPolicyStatements } from "./s3-projection-settings.js";
17
19
  const redirectFunctionSourcePath = resolve(dirname(fileURLToPath(import.meta.url)), "redirect-and-index.js");
@@ -68,9 +70,13 @@ export function addS3ProjectionPublication(scope, props, media) {
68
70
  assertValidPublicationConfig({ kind: "s3-projection", ...props });
69
71
  const bucket = createSiteBucket(scope, props.retainBucketOnDelete !== false);
70
72
  const keyValueStore = new cloudfront.KeyValueStore(scope, "RedirectsKeyValueStore");
73
+ const edgeConfig = readEdgeConfig(props.staticDir);
71
74
  const redirectFunction = new cloudfront.Function(scope, "RedirectAndIndexFunction", {
72
75
  runtime: cloudfront.FunctionRuntime.JS_2_0,
73
- code: cloudfront.FunctionCode.fromFile({ filePath: redirectFunctionSourcePath }),
76
+ code: cloudfront.FunctionCode.fromInline(compileRedirectFunctionSource(readFileSync(redirectFunctionSourcePath, "utf8"), edgeConfig.rules, {
77
+ negotiateMarkdown: edgeConfig.negotiateMarkdown,
78
+ directoryTwins: edgeConfig.directoryTwins,
79
+ })),
74
80
  keyValueStore,
75
81
  });
76
82
  const deadLetterQueue = new sqs.Queue(scope, "ProjectionDeadLetterQueue", {
@@ -10,11 +10,13 @@ import * as route53 from "aws-cdk-lib/aws-route53";
10
10
  import * as s3 from "aws-cdk-lib/aws-s3";
11
11
  import * as sqs from "aws-cdk-lib/aws-sqs";
12
12
  import type { Construct } from "constructs";
13
+ import { readFileSync } from "node:fs";
13
14
  import { dirname, resolve } from "node:path";
14
15
  import { fileURLToPath } from "node:url";
15
16
  import { assertValidPublicationConfig, type PapyrusPublicationConfig } from "../site-backend-config";
16
17
  import { addRenditionsFunction, type RenditionsFunction } from "./renditions-function";
17
18
  import { RENDITIONS_FUNCTION_NAME_ENV, type RenditionsPlan } from "./renditions-settings";
19
+ import { compileRedirectFunctionSource, readEdgeConfig } from "./redirect-rules";
18
20
  import { addInvalidationPathAlarm } from "./invalidation-alarm";
19
21
  import { distributionIdFromArn, s3ProjectionEnvironment, s3ProjectionPolicyStatements } from "./s3-projection-settings";
20
22
 
@@ -110,9 +112,15 @@ export function addS3ProjectionPublication(
110
112
  const bucket = createSiteBucket(scope, props.retainBucketOnDelete !== false);
111
113
 
112
114
  const keyValueStore = new cloudfront.KeyValueStore(scope, "RedirectsKeyValueStore");
115
+ const edgeConfig = readEdgeConfig(props.staticDir);
113
116
  const redirectFunction = new cloudfront.Function(scope, "RedirectAndIndexFunction", {
114
117
  runtime: cloudfront.FunctionRuntime.JS_2_0,
115
- code: cloudfront.FunctionCode.fromFile({ filePath: redirectFunctionSourcePath }),
118
+ code: cloudfront.FunctionCode.fromInline(
119
+ compileRedirectFunctionSource(readFileSync(redirectFunctionSourcePath, "utf8"), edgeConfig.rules, {
120
+ negotiateMarkdown: edgeConfig.negotiateMarkdown,
121
+ directoryTwins: edgeConfig.directoryTwins,
122
+ }),
123
+ ),
116
124
  keyValueStore,
117
125
  });
118
126
  const deadLetterQueue = new sqs.Queue(scope, "ProjectionDeadLetterQueue", {
@@ -145,6 +145,13 @@ export type PapyrusSiteBackendConfig = {
145
145
  * DependsOn ordering; no resource changes. Default 12; 0 turns it off.
146
146
  */
147
147
  resolverWaveSize?: number;
148
+ /**
149
+ * Optional model groups to leave out of the data API (see `OPTIONAL_MODEL_GROUPS`; today `editions`).
150
+ * CloudFormation allows 2,500 resources per stack operation, nested stacks included, and a fresh create of the full
151
+ * model set exceeds it. Deploy a new site once with a group excluded, then remove the exclusion and deploy again:
152
+ * the second deploy is an ordinary update that adds the models.
153
+ */
154
+ excludeModelGroups?: string[];
148
155
  /** Static reader app (Markus-static sites) that a publish rebuilds through IAM `amplify:StartJob`. */
149
156
  reader?: { amplifyAppId: string; branchName?: string };
150
157
  /** Lets the newsroom start the `staging` branch build of this CMS app. */
@@ -178,6 +185,16 @@ export type PapyrusSiteBackendConfig = {
178
185
  * Unset: no plugin, and the synthesized backend is exactly what it was before plugins existed.
179
186
  */
180
187
  plugins?: PluginRequest[];
188
+ /**
189
+ * The public form receiving endpoint every site gets (docs/public-forms.md). It accepts posts only from the site's
190
+ * own public hosts (distribution aliases, reader domain, revalidate URL, sign-in redirect URLs) plus these.
191
+ */
192
+ publicForms?: {
193
+ /** Extra https origins (or http://localhost) allowed to post, for example `https://www.example.org`. */
194
+ allowedOrigins?: string[];
195
+ /** Extra honeypot field names besides `website`. */
196
+ honeypotFields?: string[];
197
+ };
181
198
  features?: {
182
199
  consoleResponder?: boolean;
183
200
  inboundEmail?: boolean;