@anthusai/papyrus 2.0.0-next.11 → 2.0.0-next.110
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/amplify/auth/resource.js +16 -1
- package/amplify/auth/resource.ts +19 -1
- package/amplify/data/function-handler.js +20 -0
- package/amplify/data/function-handler.ts +40 -0
- package/amplify/data/resource.js +152 -62
- package/amplify/data/resource.ts +158 -62
- package/amplify/data/schema.js +2101 -1987
- package/amplify/data/schema.ts +197 -70
- package/amplify/functions/assignment-action/data-access.js +2 -0
- package/amplify/functions/assignment-action/data-access.ts +2 -0
- package/amplify/functions/assignment-action/handler.ts +1 -1
- package/amplify/functions/automation-view/handler.py +17 -0
- package/amplify/functions/automation-view/resource.js +24 -0
- package/amplify/functions/automation-view/resource.ts +34 -0
- package/amplify/functions/automation-view/schema.js +26 -0
- package/amplify/functions/automation-view/schema.ts +28 -0
- package/amplify/functions/automation-view/wire.js +56 -0
- package/amplify/functions/automation-view/wire.ts +76 -0
- package/amplify/functions/content-actions/handler.py +64 -3
- package/amplify/functions/content-actions/requirements.txt +1 -0
- package/amplify/functions/content-actions/resource.js +1 -1
- package/amplify/functions/content-actions/resource.ts +1 -1
- package/amplify/functions/editorial-checks/handler.py +2 -2
- package/amplify/functions/editorial-checks/requirements.txt +1 -1
- package/amplify/functions/editorial-checks/resource.js +3 -3
- package/amplify/functions/editorial-checks/resource.ts +3 -3
- package/amplify/functions/editorial-checks/schema.js +38 -8
- package/amplify/functions/editorial-checks/schema.ts +38 -8
- package/amplify/functions/editorial-checks/wire.js +4 -0
- package/amplify/functions/editorial-checks/wire.ts +4 -0
- package/amplify/functions/monitoring-view/handler.py +11 -0
- package/amplify/functions/monitoring-view/resource.js +24 -0
- package/amplify/functions/monitoring-view/resource.ts +34 -0
- package/amplify/functions/monitoring-view/schema.js +29 -0
- package/amplify/functions/monitoring-view/schema.ts +31 -0
- package/amplify/functions/monitoring-view/wire.js +64 -0
- package/amplify/functions/monitoring-view/wire.ts +81 -0
- package/amplify/functions/public-forms/core.js +247 -0
- package/amplify/functions/public-forms/core.ts +310 -0
- package/amplify/functions/public-forms/handler.ts +41 -0
- package/amplify/functions/public-forms/origins.js +35 -0
- package/amplify/functions/public-forms/origins.ts +43 -0
- package/amplify/functions/public-forms/resource.js +9 -0
- package/amplify/functions/public-forms/resource.ts +10 -0
- package/amplify/functions/public-forms/wire.js +25 -0
- package/amplify/functions/public-forms/wire.ts +32 -0
- package/amplify/functions/shared/editorial-context.ts +63 -4
- package/amplify/functions/shared/python-bundle.js +20 -14
- package/amplify/functions/shared/python-bundle.ts +25 -18
- package/amplify/plugins/newsletter/config.js +245 -0
- package/amplify/plugins/newsletter/config.ts +305 -0
- package/amplify/plugins/newsletter/function/handler.py +6 -0
- package/amplify/plugins/newsletter/function/requirements.txt +5 -0
- package/amplify/plugins/newsletter/ops/handler.py +12 -0
- package/amplify/plugins/newsletter/ops/resource.js +27 -0
- package/amplify/plugins/newsletter/ops/resource.ts +37 -0
- package/amplify/plugins/newsletter/papyrus.plugin.js +263 -0
- package/amplify/plugins/newsletter/papyrus.plugin.ts +311 -0
- package/amplify/plugins/newsletter/schema.js +57 -0
- package/amplify/plugins/newsletter/schema.ts +62 -0
- package/amplify/plugins/newsletter/secure-string-parameter.js +107 -0
- package/amplify/plugins/newsletter/secure-string-parameter.ts +120 -0
- package/amplify/plugins/newsletter/sending.js +140 -0
- package/amplify/plugins/newsletter/sending.ts +186 -0
- package/amplify/plugins/reader-steering/config.js +182 -0
- package/amplify/plugins/reader-steering/config.ts +290 -0
- package/amplify/plugins/reader-steering/function/ai_moderation.py +246 -0
- package/amplify/plugins/reader-steering/function/handler.py +1589 -0
- package/amplify/plugins/reader-steering/function/moderation-prompt.md +27 -0
- package/amplify/plugins/reader-steering/function/moderation.py +316 -0
- package/amplify/plugins/reader-steering/function/proposals.py +487 -0
- package/amplify/plugins/reader-steering/function/resource.js +27 -0
- package/amplify/plugins/reader-steering/function/resource.ts +35 -0
- package/amplify/plugins/reader-steering/papyrus.plugin.js +187 -0
- package/amplify/plugins/reader-steering/papyrus.plugin.ts +213 -0
- package/amplify/plugins/reader-steering/schema.js +252 -0
- package/amplify/plugins/reader-steering/schema.ts +254 -0
- package/amplify/plugins/registry.js +4 -0
- package/amplify/plugins/registry.ts +4 -0
- package/amplify/plugins/signup/schema.js +2 -1
- package/amplify/plugins/signup/schema.ts +2 -1
- package/amplify/plugins/types.ts +5 -0
- package/amplify/plugins/wire.js +1 -0
- package/amplify/plugins/wire.ts +1 -0
- package/amplify/publication/projection-lease.js +30 -0
- package/amplify/publication/projection-lease.ts +39 -0
- package/amplify/publication/redirect-and-index.js +105 -0
- package/amplify/publication/redirect-rules.js +73 -0
- package/amplify/publication/redirect-rules.ts +93 -0
- package/amplify/publication/s3-projection.js +7 -1
- package/amplify/publication/s3-projection.ts +9 -1
- package/amplify/site-backend-config.js +19 -0
- package/amplify/site-backend-config.ts +45 -1
- package/amplify/site-backend-names.js +4 -2
- package/amplify/site-backend-names.ts +4 -2
- package/amplify/site-backend.js +42 -4
- package/amplify/site-backend.ts +45 -4
- package/app/.well-known/papyrus-cli.json/route.ts +9 -0
- package/app/api/auth/prospectus/sign-in/route.ts +4 -0
- package/app/api/newsletter/confirm/route.ts +13 -0
- package/app/api/newsletter/preferences/route.ts +13 -0
- package/app/api/newsletter/state/route.ts +9 -0
- package/app/api/newsletter/subscribe/route.ts +9 -0
- package/app/api/newsletter/unsubscribe/route.ts +13 -0
- package/app/api/newsroom/chat/[...path]/route.ts +17 -0
- package/app/api/newsroom/curation/context/route.ts +9 -0
- package/app/api/newsroom/curation/priming/route.ts +9 -0
- package/app/api/newsroom/curation/publication/route.ts +9 -0
- package/app/api/reader/comment/delete/route.ts +9 -0
- package/app/api/reader/comment/edit/route.ts +9 -0
- package/app/api/reader/comment/route.ts +9 -0
- package/app/api/reader/flag/route.ts +9 -0
- package/app/api/reader/handle/route.ts +9 -0
- package/app/api/reader/me/route.ts +9 -0
- package/app/api/reader/rating/route.ts +9 -0
- package/app/api/reader/report/route.ts +9 -0
- package/app/api/reader/retired/route.ts +9 -0
- package/app/api/reader/submit/route.ts +9 -0
- package/app/api/reader/vote/route.ts +9 -0
- package/app/layout.tsx +5 -1
- package/bin/papyrus-infra.mjs +20 -1
- package/components/automation-panel.tsx +49 -0
- package/components/automation-run-now.tsx +97 -0
- package/components/automation-view.tsx +195 -0
- package/components/editorial-checks-admin-panel.tsx +32 -17
- package/components/moderation-panel.tsx +248 -0
- package/components/monitoring-overview-view.tsx +422 -0
- package/components/monitoring-panel.tsx +60 -0
- package/components/news-desk-client-provider.tsx +18 -18
- package/components/news-desk-taxonomy-client.ts +28 -8
- package/components/newsletter-panel.tsx +474 -0
- package/components/newsletter-sections.tsx +163 -0
- package/components/newsroom-access-gate.tsx +16 -2
- package/components/newsroom-article-editor.tsx +9 -7
- package/components/newsroom-articles-view.tsx +6 -6
- package/components/newsroom-assignment-detail.tsx +6 -6
- package/components/newsroom-board-view.tsx +8 -3
- package/components/newsroom-curation-bootstrap.tsx +45 -0
- package/components/newsroom-ops-overview.tsx +26 -96
- package/components/newsroom-ops-shell.tsx +2 -2
- package/components/newsroom-organizer.css +33 -0
- package/components/newsroom-overview-skeleton.tsx +25 -0
- package/components/newsroom-references-view.tsx +192 -49
- package/components/notification-center.tsx +281 -0
- package/components/papyrus-console-conversation.css +14 -0
- package/components/papyrus-console-conversation.tsx +55 -0
- package/components/papyrus-console-shell.tsx +62 -420
- package/components/papyrus-private-chat-panel.tsx +134 -0
- package/components/publication-cyclotron-panel.tsx +15 -10
- package/components/reader-auth-state.ts +1 -1
- package/components/reference-add-entry.tsx +75 -0
- package/components/reference-curation-controls.tsx +31 -22
- package/components/reference-cyclotron-priming.tsx +58 -19
- package/components/reference-cyclotron-suggestion.tsx +10 -6
- package/components/reference-priming.tsx +14 -10
- package/components/reference-relevance-review.tsx +3 -3
- package/components/reference-section-suggestion.tsx +1 -1
- package/components/topic-steering-workspace.tsx +230 -144
- package/infra/amplify-app-shell.js +96 -16
- package/infra/amplify-app-shell.ts +108 -18
- package/infra/cloud-email-config.js +32 -0
- package/infra/cloud-email-config.ts +39 -0
- package/infra/cloud-email.js +57 -0
- package/infra/cloud-email.test.js +69 -0
- package/infra/cloud-email.test.ts +66 -0
- package/infra/cloud-email.ts +60 -0
- package/infra/index.js +1 -0
- package/infra/secure-string-parameter-handler.js +41 -0
- package/infra/secure-string-parameter-handler.ts +41 -0
- package/infra/secure-string-parameter.js +79 -0
- package/infra/secure-string-parameter.ts +91 -0
- package/infra/site-config.js +92 -6
- package/infra/site-config.ts +138 -6
- package/lib/automation-client.ts +54 -0
- package/lib/automation-fixture.ts +93 -0
- package/lib/automation-overview.ts +252 -0
- package/lib/category-repository.ts +14 -12
- package/lib/cli-login-discovery.ts +78 -0
- package/lib/console-web-context.ts +1 -1
- package/lib/curation-routes.ts +345 -0
- package/lib/cyclotron-suggestions.ts +4 -4
- package/lib/editorial-checks-client.ts +18 -0
- package/lib/editorial-checks-demo.ts +1 -1
- package/lib/editorial-checks.ts +8 -4
- package/lib/editorial-preview.ts +5 -5
- package/lib/monitoring-client.ts +83 -0
- package/lib/monitoring-overview-fixture.ts +51 -0
- package/lib/monitoring-overview.ts +191 -0
- package/lib/news-desk-session.ts +12 -21
- package/lib/newsletter-api.ts +418 -0
- package/lib/newsletter-client.ts +46 -0
- package/lib/newsletter-fixture.ts +184 -0
- package/lib/newsletter-model.ts +202 -0
- package/lib/newsroom-demo-dashboard.ts +1 -1
- package/lib/newsroom-nav.ts +1 -1
- package/lib/newsroom-organizer-nav.ts +2 -1
- package/lib/papyrus-chat-admission.ts +65 -0
- package/lib/papyrus-hosted-chat.ts +217 -0
- package/lib/papyrus-private-chat-client.ts +81 -0
- package/lib/prospectus-session.ts +30 -14
- package/lib/prospectus-sign-in-routes.ts +106 -31
- package/lib/prospectus-site-identity.ts +46 -6
- package/lib/reader-api.ts +145 -0
- package/lib/reader-moderation.ts +220 -0
- package/lib/reader-retirement-route.ts +89 -0
- package/lib/reader-subject.md +29 -0
- package/lib/reader-subject.ts +53 -0
- package/lib/reference-review-details.ts +71 -0
- package/lib/reference-review-request.ts +14 -0
- package/lib/relevance-decisions.ts +42 -10
- package/lib/relevance-status.ts +2 -2
- package/lib/review-client.ts +120 -0
- package/lib/review-fixture.ts +45 -0
- package/lib/review-requests.ts +246 -0
- package/lib/semantic-graph.ts +2 -0
- package/lib/shared-chat-admission.ts +182 -0
- package/lib/site-brand-tenant.ts +11 -6
- package/lib/site-brand.ts +6 -3
- package/lib/site-origin-value.ts +50 -0
- package/lib/site-origin.ts +8 -0
- package/lib/use-reference-review-data.ts +44 -0
- package/middleware.ts +14 -3
- package/package.json +67 -3
- package/routes.manifest.json +238 -2
- package/src/sync.mjs +11 -2
- package/src/with-papyrus.d.mts +1 -0
- package/src/with-papyrus.mjs +17 -1
|
@@ -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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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,
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
export const NEWSLETTER_SITE_ID = /^[a-z0-9][a-z0-9-]{0,39}$/;
|
|
2
|
+
export const NEWSLETTER_LIST_ID = /^[a-z][a-z0-9-]{0,39}$/;
|
|
3
|
+
export const COMPUTE_ROLE_ARN = /^arn:aws:iam::[0-9]{12}:role\/[A-Za-z0-9+=,.@_/-]{1,512}$/;
|
|
4
|
+
const HOSTNAME = /^(?=.{1,253}$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+$/;
|
|
5
|
+
const LOCAL_PART = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*$/;
|
|
6
|
+
const SES_NAME = /^[A-Za-z0-9_-]{1,64}$/;
|
|
7
|
+
const ISSUE_TYPE = /^[a-z][a-z0-9_]{0,59}$/;
|
|
8
|
+
/** One path segment: it is the Item section of the archive and the `urls.sections` key of the site's projection.json. */
|
|
9
|
+
const ARCHIVE_PATH = /^\/[a-z0-9][a-z0-9_-]*\/$/;
|
|
10
|
+
const SCHEDULER_CRON = /^cron\(\S+ \S+ \S+ \S+ \S+ \S+\)$/;
|
|
11
|
+
const UNSAFE_CSS = /[<>{};"\\`]|url\(|expression\(|@import/i;
|
|
12
|
+
const CONTROL_CHARACTERS = /[\u0000-\u001f\u007f]/;
|
|
13
|
+
export const ITEM_URL_STYLES = ["html", "directory", "clean"];
|
|
14
|
+
const THEME_STYLE_KEYS = [
|
|
15
|
+
"surface", "muted", "fontFamily", "headerBand", "headerText", "buttonFill", "buttonText", "buttonBorder", "footerBand",
|
|
16
|
+
"footerText", "labelFont",
|
|
17
|
+
];
|
|
18
|
+
const THEME_KEYS = [
|
|
19
|
+
"accent", "text", "background", ...THEME_STYLE_KEYS, "logoUrl", "wordmarkImageUrl", "wordmarkAlt", "wordmarkWidth", "wordmarkHeight",
|
|
20
|
+
"footerWordmarkWidth",
|
|
21
|
+
];
|
|
22
|
+
const KNOWN_KEYS = new Set([
|
|
23
|
+
"name", "siteId", "siteOrigin", "basePath", "computeRoleArn", "sendingDomain", "fromAddress", "fromName", "instantAlerts", "theme", "lists",
|
|
24
|
+
"configurationSet", "eventTopicName", "itemUrlStyle",
|
|
25
|
+
]);
|
|
26
|
+
const LIST_KEYS = {
|
|
27
|
+
roundup: ["id", "name", "description", "kind", "schedule", "issueType", "archivePath"],
|
|
28
|
+
alert: ["id", "name", "description", "kind", "rule", "approval"],
|
|
29
|
+
};
|
|
30
|
+
function fail(message) {
|
|
31
|
+
throw new Error(`newsletter plugin ${message}`);
|
|
32
|
+
}
|
|
33
|
+
function record(raw, label, known) {
|
|
34
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw))
|
|
35
|
+
fail(`${label} must be an object`);
|
|
36
|
+
const unknown = Object.keys(raw).filter((key) => !known.includes(key));
|
|
37
|
+
if (unknown.length > 0)
|
|
38
|
+
fail(`${label} has unknown settings: ${unknown.join(", ")}`);
|
|
39
|
+
return raw;
|
|
40
|
+
}
|
|
41
|
+
function text(raw, label, maximum, options = {}) {
|
|
42
|
+
const value = raw === undefined ? options.fallback : raw;
|
|
43
|
+
if (value === undefined)
|
|
44
|
+
fail(`${label} is required`);
|
|
45
|
+
if (typeof value !== "string" || CONTROL_CHARACTERS.test(value) || value.trim() !== value) {
|
|
46
|
+
fail(`${label} must be text on one line with no leading or trailing spaces`);
|
|
47
|
+
}
|
|
48
|
+
if (value.length === 0 && !options.optional)
|
|
49
|
+
fail(`${label} is required`);
|
|
50
|
+
if (value.length > maximum)
|
|
51
|
+
fail(`${label} must be at most ${maximum} characters`);
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
function matching(raw, label, pattern, description, fallback) {
|
|
55
|
+
const value = raw === undefined ? fallback : raw;
|
|
56
|
+
if (typeof value !== "string" || !pattern.test(value))
|
|
57
|
+
fail(`${label} must be ${description}`);
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
function timeZone(raw, label) {
|
|
61
|
+
const value = text(raw, label, 64);
|
|
62
|
+
try {
|
|
63
|
+
new Intl.DateTimeFormat("en-US", { timeZone: value });
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
fail(`${label} must be an IANA time zone such as America/New_York`);
|
|
67
|
+
}
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
function origin(raw) {
|
|
71
|
+
const value = text(raw, "siteOrigin", 200);
|
|
72
|
+
let parsed;
|
|
73
|
+
try {
|
|
74
|
+
parsed = new URL(value);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
parsed = undefined;
|
|
78
|
+
}
|
|
79
|
+
if (!parsed || parsed.protocol !== "https:" || parsed.origin !== value)
|
|
80
|
+
fail("siteOrigin must be an https origin such as https://example.com with no path");
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
/** `urls.basePath` as projection.json writes it ("/posts" or "posts"): lowercase path segments, no dots, at most 3 deep. */
|
|
84
|
+
const BASE_PATH = /^[a-z0-9][a-z0-9_-]*(\/[a-z0-9][a-z0-9_-]*){0,2}$/;
|
|
85
|
+
function basePath(raw) {
|
|
86
|
+
if (raw === undefined)
|
|
87
|
+
return "";
|
|
88
|
+
const value = typeof raw === "string" ? raw.replace(/^\/+|\/+$/g, "") : null;
|
|
89
|
+
if (value === null || (value !== "" && !BASE_PATH.test(value)))
|
|
90
|
+
fail('basePath must be the site\'s urls.basePath, a lowercase path such as "/posts"');
|
|
91
|
+
return value;
|
|
92
|
+
}
|
|
93
|
+
function stringList(raw, label) {
|
|
94
|
+
if (raw === undefined)
|
|
95
|
+
return undefined;
|
|
96
|
+
if (!Array.isArray(raw) || raw.length === 0 || raw.length > 50)
|
|
97
|
+
fail(`${label} must be a list of 1 to 50 names`);
|
|
98
|
+
return [...new Set(raw.map((entry, index) => text(entry, `${label}[${index}]`, 80)))];
|
|
99
|
+
}
|
|
100
|
+
function itemUrlStyle(raw) {
|
|
101
|
+
if (raw === undefined)
|
|
102
|
+
return "html";
|
|
103
|
+
if (typeof raw !== "string" || !ITEM_URL_STYLES.includes(raw))
|
|
104
|
+
fail(`itemUrlStyle must be one of ${ITEM_URL_STYLES.join(", ")}`);
|
|
105
|
+
return raw;
|
|
106
|
+
}
|
|
107
|
+
function resolveTheme(raw) {
|
|
108
|
+
const theme = record(raw, "theme", THEME_KEYS);
|
|
109
|
+
const resolved = {
|
|
110
|
+
accent: text(theme.accent, "theme.accent", 80),
|
|
111
|
+
text: text(theme.text, "theme.text", 80),
|
|
112
|
+
background: text(theme.background, "theme.background", 80),
|
|
113
|
+
};
|
|
114
|
+
for (const key of THEME_STYLE_KEYS) {
|
|
115
|
+
if (theme[key] !== undefined)
|
|
116
|
+
resolved[key] = text(theme[key], `theme.${key}`, 200);
|
|
117
|
+
}
|
|
118
|
+
for (const [key, value] of Object.entries(resolved)) {
|
|
119
|
+
if (typeof value === "string" && UNSAFE_CSS.test(value))
|
|
120
|
+
fail(`theme.${key} contains characters that are not allowed in a style value`);
|
|
121
|
+
}
|
|
122
|
+
if (theme.logoUrl !== undefined) {
|
|
123
|
+
const logoUrl = text(theme.logoUrl, "theme.logoUrl", 500);
|
|
124
|
+
if (!/^https:\/\//.test(logoUrl))
|
|
125
|
+
fail("theme.logoUrl must be an https URL");
|
|
126
|
+
resolved.logoUrl = logoUrl;
|
|
127
|
+
}
|
|
128
|
+
if (theme.wordmarkImageUrl !== undefined) {
|
|
129
|
+
const url = text(theme.wordmarkImageUrl, "theme.wordmarkImageUrl", 500);
|
|
130
|
+
if (!/^https:\/\/[^\s"'<>\\`]+$/.test(url))
|
|
131
|
+
fail("theme.wordmarkImageUrl must be an absolute https URL");
|
|
132
|
+
resolved.wordmarkImageUrl = url;
|
|
133
|
+
for (const key of ["wordmarkWidth", "wordmarkHeight"]) {
|
|
134
|
+
const value = theme[key];
|
|
135
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > 2000) {
|
|
136
|
+
fail(`theme.${key} is required with theme.wordmarkImageUrl and must be a whole number of pixels from 1 to 2000`);
|
|
137
|
+
}
|
|
138
|
+
resolved[key] = value;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else if (theme.wordmarkWidth !== undefined || theme.wordmarkHeight !== undefined) {
|
|
142
|
+
fail("theme.wordmarkWidth and theme.wordmarkHeight need theme.wordmarkImageUrl");
|
|
143
|
+
}
|
|
144
|
+
if (theme.footerWordmarkWidth !== undefined) {
|
|
145
|
+
if (theme.wordmarkImageUrl === undefined)
|
|
146
|
+
fail("theme.footerWordmarkWidth needs theme.wordmarkImageUrl");
|
|
147
|
+
const width = theme.footerWordmarkWidth;
|
|
148
|
+
if (typeof width !== "number" || !Number.isInteger(width) || width < 1 || width > 400) {
|
|
149
|
+
fail("theme.footerWordmarkWidth must be a whole number of pixels from 1 to 400");
|
|
150
|
+
}
|
|
151
|
+
resolved.footerWordmarkWidth = width;
|
|
152
|
+
}
|
|
153
|
+
if (theme.wordmarkAlt !== undefined) {
|
|
154
|
+
const alt = text(theme.wordmarkAlt, "theme.wordmarkAlt", 200);
|
|
155
|
+
if (/[\u0000-\u001f\u007f]/.test(alt))
|
|
156
|
+
fail("theme.wordmarkAlt must not contain control characters");
|
|
157
|
+
resolved.wordmarkAlt = alt;
|
|
158
|
+
}
|
|
159
|
+
return resolved;
|
|
160
|
+
}
|
|
161
|
+
function resolveRule(raw, label) {
|
|
162
|
+
if (raw === undefined)
|
|
163
|
+
return {};
|
|
164
|
+
const rule = record(raw, label, ["sections", "tags", "types"]);
|
|
165
|
+
return {
|
|
166
|
+
...(rule.sections === undefined ? {} : { sections: stringList(rule.sections, `${label}.sections`) }),
|
|
167
|
+
...(rule.tags === undefined ? {} : { tags: stringList(rule.tags, `${label}.tags`) }),
|
|
168
|
+
...(rule.types === undefined ? {} : { types: stringList(rule.types, `${label}.types`) }),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function resolveList(raw, index, instantAlerts) {
|
|
172
|
+
const label = `lists[${index}]`;
|
|
173
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw))
|
|
174
|
+
fail(`${label} must be an object`);
|
|
175
|
+
const kind = raw.kind;
|
|
176
|
+
if (kind !== "roundup" && kind !== "alert")
|
|
177
|
+
fail(`${label}.kind must be "roundup" or "alert"`);
|
|
178
|
+
const list = record(raw, label, LIST_KEYS[kind]);
|
|
179
|
+
const base = {
|
|
180
|
+
id: matching(list.id, `${label}.id`, NEWSLETTER_LIST_ID, "lowercase letters, digits and hyphens, starting with a letter (at most 40 characters)"),
|
|
181
|
+
name: text(list.name, `${label}.name`, 80),
|
|
182
|
+
description: text(list.description, `${label}.description`, 300, { optional: true, fallback: "" }),
|
|
183
|
+
};
|
|
184
|
+
if (kind === "roundup") {
|
|
185
|
+
const schedule = record(list.schedule, `${label}.schedule`, ["draft", "timezone"]);
|
|
186
|
+
return {
|
|
187
|
+
...base,
|
|
188
|
+
kind,
|
|
189
|
+
schedule: {
|
|
190
|
+
draft: matching(schedule.draft, `${label}.schedule.draft`, SCHEDULER_CRON, 'an EventBridge Scheduler cron such as "cron(0 13 ? * FRI *)"'),
|
|
191
|
+
timezone: timeZone(schedule.timezone, `${label}.schedule.timezone`),
|
|
192
|
+
},
|
|
193
|
+
issueType: matching(list.issueType, `${label}.issueType`, ISSUE_TYPE, "a lowercase item type name", "newsletter_issue"),
|
|
194
|
+
archivePath: matching(list.archivePath, `${label}.archivePath`, ARCHIVE_PATH, 'a path that starts and ends with "/", such as /roundup/'),
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
const approval = list.approval === undefined ? "admin" : list.approval;
|
|
198
|
+
if (approval !== "admin" && approval !== "none")
|
|
199
|
+
fail(`${label}.approval must be "admin" or "none"`);
|
|
200
|
+
if (approval === "none" && !instantAlerts) {
|
|
201
|
+
fail(`${label}.approval "none" sends alerts without an admin approving them: set instantAlerts: true on the plugin first`);
|
|
202
|
+
}
|
|
203
|
+
return { ...base, kind, rule: resolveRule(list.rule, `${label}.rule`), approval };
|
|
204
|
+
}
|
|
205
|
+
export function resolveNewsletterConfig(request) {
|
|
206
|
+
const raw = record(typeof request === "string" ? { name: request } : request, "settings", [...KNOWN_KEYS]);
|
|
207
|
+
const siteId = matching(raw.siteId, "siteId", NEWSLETTER_SITE_ID, "a lowercase site id (letters, digits, hyphens), the SiteId of the sending stack");
|
|
208
|
+
const computeRoleArn = raw.computeRoleArn;
|
|
209
|
+
if (typeof computeRoleArn !== "string" || !COMPUTE_ROLE_ARN.test(computeRoleArn)) {
|
|
210
|
+
fail("computeRoleArn must be the IAM role ARN of the CMS app's compute role (the CmsComputeRoleArn output)");
|
|
211
|
+
}
|
|
212
|
+
const sendingDomain = matching(raw.sendingDomain, "sendingDomain", HOSTNAME, "a lowercase domain name such as news.example.com");
|
|
213
|
+
const fromAddress = text(raw.fromAddress, "fromAddress", 254);
|
|
214
|
+
const [local, domain, ...extra] = fromAddress.split("@");
|
|
215
|
+
if (extra.length > 0 || !local || local.length > 64 || !LOCAL_PART.test(local) || domain !== sendingDomain) {
|
|
216
|
+
fail(`fromAddress must be an address on ${sendingDomain} (lowercase, such as newsletter@${sendingDomain})`);
|
|
217
|
+
}
|
|
218
|
+
if (raw.instantAlerts !== undefined && typeof raw.instantAlerts !== "boolean")
|
|
219
|
+
fail("instantAlerts must be a boolean");
|
|
220
|
+
const instantAlerts = raw.instantAlerts === true;
|
|
221
|
+
if (!Array.isArray(raw.lists) || raw.lists.length === 0 || raw.lists.length > 20)
|
|
222
|
+
fail("lists must be a list of 1 to 20 lists");
|
|
223
|
+
const lists = raw.lists.map((entry, index) => resolveList(entry, index, instantAlerts));
|
|
224
|
+
const duplicates = lists.map((list) => list.id).filter((id, index, ids) => ids.indexOf(id) !== index);
|
|
225
|
+
if (duplicates.length > 0)
|
|
226
|
+
fail(`lists has duplicate ids: ${[...new Set(duplicates)].join(", ")}`);
|
|
227
|
+
const roundups = lists.filter((list) => list.kind === "roundup");
|
|
228
|
+
if (new Set(roundups.map((list) => list.archivePath)).size !== roundups.length)
|
|
229
|
+
fail("roundup lists need different archivePath values");
|
|
230
|
+
return {
|
|
231
|
+
siteId,
|
|
232
|
+
siteOrigin: origin(raw.siteOrigin),
|
|
233
|
+
basePath: basePath(raw.basePath),
|
|
234
|
+
computeRoleArn,
|
|
235
|
+
sendingDomain,
|
|
236
|
+
fromAddress,
|
|
237
|
+
fromName: text(raw.fromName, "fromName", 80),
|
|
238
|
+
instantAlerts,
|
|
239
|
+
theme: resolveTheme(raw.theme),
|
|
240
|
+
lists,
|
|
241
|
+
itemUrlStyle: itemUrlStyle(raw.itemUrlStyle),
|
|
242
|
+
configurationSet: matching(raw.configurationSet, "configurationSet", SES_NAME, "an SES configuration set name", `papyrus-${siteId}`),
|
|
243
|
+
eventTopicName: matching(raw.eventTopicName, "eventTopicName", SES_NAME, "an SNS topic name", `papyrus-newsletter-events-${siteId}`),
|
|
244
|
+
};
|
|
245
|
+
}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import type { PluginRequest } from "../types";
|
|
2
|
+
|
|
3
|
+
export const NEWSLETTER_SITE_ID = /^[a-z0-9][a-z0-9-]{0,39}$/;
|
|
4
|
+
export const NEWSLETTER_LIST_ID = /^[a-z][a-z0-9-]{0,39}$/;
|
|
5
|
+
export const COMPUTE_ROLE_ARN = /^arn:aws:iam::[0-9]{12}:role\/[A-Za-z0-9+=,.@_/-]{1,512}$/;
|
|
6
|
+
const HOSTNAME = /^(?=.{1,253}$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+$/;
|
|
7
|
+
const LOCAL_PART = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*$/;
|
|
8
|
+
const SES_NAME = /^[A-Za-z0-9_-]{1,64}$/;
|
|
9
|
+
const ISSUE_TYPE = /^[a-z][a-z0-9_]{0,59}$/;
|
|
10
|
+
/** One path segment: it is the Item section of the archive and the `urls.sections` key of the site's projection.json. */
|
|
11
|
+
const ARCHIVE_PATH = /^\/[a-z0-9][a-z0-9_-]*\/$/;
|
|
12
|
+
const SCHEDULER_CRON = /^cron\(\S+ \S+ \S+ \S+ \S+ \S+\)$/;
|
|
13
|
+
const UNSAFE_CSS = /[<>{};"\\`]|url\(|expression\(|@import/i;
|
|
14
|
+
const CONTROL_CHARACTERS = /[\u0000-\u001f\u007f]/;
|
|
15
|
+
|
|
16
|
+
export const ITEM_URL_STYLES = ["html", "directory", "clean"] as const;
|
|
17
|
+
export type ItemUrlStyle = (typeof ITEM_URL_STYLES)[number];
|
|
18
|
+
|
|
19
|
+
export type NewsletterTheme = {
|
|
20
|
+
accent: string;
|
|
21
|
+
text: string;
|
|
22
|
+
background: string;
|
|
23
|
+
surface?: string;
|
|
24
|
+
muted?: string;
|
|
25
|
+
fontFamily?: string;
|
|
26
|
+
logoUrl?: string;
|
|
27
|
+
/** Branding, all optional: a plain theme keeps the plain layout. Colours are CSS values; the border is 1.5px when set. */
|
|
28
|
+
headerBand?: string;
|
|
29
|
+
headerText?: string;
|
|
30
|
+
buttonFill?: string;
|
|
31
|
+
buttonText?: string;
|
|
32
|
+
buttonBorder?: string;
|
|
33
|
+
footerBand?: string;
|
|
34
|
+
footerText?: string;
|
|
35
|
+
/** An absolute https image, drawn with alt text and explicit pixel size (web fonts do not load in email). */
|
|
36
|
+
wordmarkImageUrl?: string;
|
|
37
|
+
wordmarkAlt?: string;
|
|
38
|
+
wordmarkWidth?: number;
|
|
39
|
+
wordmarkHeight?: number;
|
|
40
|
+
/** Draws the footer's wordmark at this width in pixels (1 to 400), smaller than the header's; the height keeps the aspect ratio. */
|
|
41
|
+
footerWordmarkWidth?: number;
|
|
42
|
+
labelFont?: string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const THEME_STYLE_KEYS = [
|
|
46
|
+
"surface", "muted", "fontFamily", "headerBand", "headerText", "buttonFill", "buttonText", "buttonBorder", "footerBand",
|
|
47
|
+
"footerText", "labelFont",
|
|
48
|
+
] as const;
|
|
49
|
+
const THEME_KEYS = [
|
|
50
|
+
"accent", "text", "background", ...THEME_STYLE_KEYS, "logoUrl", "wordmarkImageUrl", "wordmarkAlt", "wordmarkWidth", "wordmarkHeight",
|
|
51
|
+
"footerWordmarkWidth",
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
export type NewsletterRule = { sections?: string[]; tags?: string[]; types?: string[] };
|
|
55
|
+
|
|
56
|
+
type ListBase = { id: string; name: string; description: string };
|
|
57
|
+
export type RoundupList = ListBase & {
|
|
58
|
+
kind: "roundup";
|
|
59
|
+
/** EventBridge Scheduler cron for assembling the draft, and the IANA time zone it runs in. */
|
|
60
|
+
schedule: { draft: string; timezone: string };
|
|
61
|
+
/** The CMS item type of an issue. */
|
|
62
|
+
issueType: string;
|
|
63
|
+
/** Where the sent issue is archived, such as `/roundup/`: one path segment, also the section the issue Items are filed under. */
|
|
64
|
+
archivePath: string;
|
|
65
|
+
};
|
|
66
|
+
export type AlertList = ListBase & {
|
|
67
|
+
kind: "alert";
|
|
68
|
+
/** Which published items raise an alert. Absent or empty: every published item (an admin still approves it). */
|
|
69
|
+
rule: NewsletterRule;
|
|
70
|
+
approval: "admin" | "none";
|
|
71
|
+
};
|
|
72
|
+
export type NewsletterList = RoundupList | AlertList;
|
|
73
|
+
|
|
74
|
+
export type NewsletterConfig = {
|
|
75
|
+
/** The site id: names the key parameter, the configuration set and the event topic. Must match the sending stack's SiteId. */
|
|
76
|
+
siteId: string;
|
|
77
|
+
/** The site's public https origin. The plugin's pages are at `<siteOrigin><basePath>/newsletter/...`. */
|
|
78
|
+
siteOrigin: string;
|
|
79
|
+
/** The site's `urls.basePath` from projection.json, as a path without slashes at either end ("" for a site at the root, "posts" for dataparade.io/posts). */
|
|
80
|
+
basePath: string;
|
|
81
|
+
/** The IAM role of the CMS app's server runtime (the `CmsComputeRoleArn` output of the app-shell stack): the only caller of the function. */
|
|
82
|
+
computeRoleArn: string;
|
|
83
|
+
sendingDomain: string;
|
|
84
|
+
fromAddress: string;
|
|
85
|
+
fromName: string;
|
|
86
|
+
/** A site must set this to true before any list may send without approval. */
|
|
87
|
+
instantAlerts: boolean;
|
|
88
|
+
theme: NewsletterTheme;
|
|
89
|
+
lists: NewsletterList[];
|
|
90
|
+
/** How the site addresses its item pages (`urls.style` in projection.json): roundup drafts link to published items with it. */
|
|
91
|
+
itemUrlStyle: ItemUrlStyle;
|
|
92
|
+
/** `papyrus-<siteId>` unless overridden. */
|
|
93
|
+
configurationSet: string;
|
|
94
|
+
/** `papyrus-newsletter-events-<siteId>` unless overridden. */
|
|
95
|
+
eventTopicName: string;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const KNOWN_KEYS = new Set([
|
|
99
|
+
"name", "siteId", "siteOrigin", "basePath", "computeRoleArn", "sendingDomain", "fromAddress", "fromName", "instantAlerts", "theme", "lists",
|
|
100
|
+
"configurationSet", "eventTopicName", "itemUrlStyle",
|
|
101
|
+
]);
|
|
102
|
+
const LIST_KEYS = {
|
|
103
|
+
roundup: ["id", "name", "description", "kind", "schedule", "issueType", "archivePath"],
|
|
104
|
+
alert: ["id", "name", "description", "kind", "rule", "approval"],
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
function fail(message: string): never {
|
|
108
|
+
throw new Error(`newsletter plugin ${message}`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function record(raw: unknown, label: string, known: string[]): Record<string, unknown> {
|
|
112
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) fail(`${label} must be an object`);
|
|
113
|
+
const unknown = Object.keys(raw).filter((key) => !known.includes(key));
|
|
114
|
+
if (unknown.length > 0) fail(`${label} has unknown settings: ${unknown.join(", ")}`);
|
|
115
|
+
return raw as Record<string, unknown>;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function text(raw: unknown, label: string, maximum: number, options: { optional?: boolean; fallback?: string } = {}): string {
|
|
119
|
+
const value = raw === undefined ? options.fallback : raw;
|
|
120
|
+
if (value === undefined) fail(`${label} is required`);
|
|
121
|
+
if (typeof value !== "string" || CONTROL_CHARACTERS.test(value) || value.trim() !== value) {
|
|
122
|
+
fail(`${label} must be text on one line with no leading or trailing spaces`);
|
|
123
|
+
}
|
|
124
|
+
if (value.length === 0 && !options.optional) fail(`${label} is required`);
|
|
125
|
+
if (value.length > maximum) fail(`${label} must be at most ${maximum} characters`);
|
|
126
|
+
return value;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function matching(raw: unknown, label: string, pattern: RegExp, description: string, fallback?: string): string {
|
|
130
|
+
const value = raw === undefined ? fallback : raw;
|
|
131
|
+
if (typeof value !== "string" || !pattern.test(value)) fail(`${label} must be ${description}`);
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function timeZone(raw: unknown, label: string): string {
|
|
136
|
+
const value = text(raw, label, 64);
|
|
137
|
+
try {
|
|
138
|
+
new Intl.DateTimeFormat("en-US", { timeZone: value });
|
|
139
|
+
} catch {
|
|
140
|
+
fail(`${label} must be an IANA time zone such as America/New_York`);
|
|
141
|
+
}
|
|
142
|
+
return value;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function origin(raw: unknown): string {
|
|
146
|
+
const value = text(raw, "siteOrigin", 200);
|
|
147
|
+
let parsed: URL | undefined;
|
|
148
|
+
try {
|
|
149
|
+
parsed = new URL(value);
|
|
150
|
+
} catch {
|
|
151
|
+
parsed = undefined;
|
|
152
|
+
}
|
|
153
|
+
if (!parsed || parsed.protocol !== "https:" || parsed.origin !== value) fail("siteOrigin must be an https origin such as https://example.com with no path");
|
|
154
|
+
return value;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** `urls.basePath` as projection.json writes it ("/posts" or "posts"): lowercase path segments, no dots, at most 3 deep. */
|
|
158
|
+
const BASE_PATH = /^[a-z0-9][a-z0-9_-]*(\/[a-z0-9][a-z0-9_-]*){0,2}$/;
|
|
159
|
+
|
|
160
|
+
function basePath(raw: unknown): string {
|
|
161
|
+
if (raw === undefined) return "";
|
|
162
|
+
const value = typeof raw === "string" ? raw.replace(/^\/+|\/+$/g, "") : null;
|
|
163
|
+
if (value === null || (value !== "" && !BASE_PATH.test(value))) fail('basePath must be the site\'s urls.basePath, a lowercase path such as "/posts"');
|
|
164
|
+
return value;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function stringList(raw: unknown, label: string): string[] | undefined {
|
|
168
|
+
if (raw === undefined) return undefined;
|
|
169
|
+
if (!Array.isArray(raw) || raw.length === 0 || raw.length > 50) fail(`${label} must be a list of 1 to 50 names`);
|
|
170
|
+
return [...new Set(raw.map((entry, index) => text(entry, `${label}[${index}]`, 80)))];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function itemUrlStyle(raw: unknown): ItemUrlStyle {
|
|
174
|
+
if (raw === undefined) return "html";
|
|
175
|
+
if (typeof raw !== "string" || !(ITEM_URL_STYLES as readonly string[]).includes(raw)) fail(`itemUrlStyle must be one of ${ITEM_URL_STYLES.join(", ")}`);
|
|
176
|
+
return raw as ItemUrlStyle;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function resolveTheme(raw: unknown): NewsletterTheme {
|
|
180
|
+
const theme = record(raw, "theme", THEME_KEYS);
|
|
181
|
+
const resolved: NewsletterTheme = {
|
|
182
|
+
accent: text(theme.accent, "theme.accent", 80),
|
|
183
|
+
text: text(theme.text, "theme.text", 80),
|
|
184
|
+
background: text(theme.background, "theme.background", 80),
|
|
185
|
+
};
|
|
186
|
+
for (const key of THEME_STYLE_KEYS) {
|
|
187
|
+
if (theme[key] !== undefined) resolved[key] = text(theme[key], `theme.${key}`, 200);
|
|
188
|
+
}
|
|
189
|
+
for (const [key, value] of Object.entries(resolved)) {
|
|
190
|
+
if (typeof value === "string" && UNSAFE_CSS.test(value)) fail(`theme.${key} contains characters that are not allowed in a style value`);
|
|
191
|
+
}
|
|
192
|
+
if (theme.logoUrl !== undefined) {
|
|
193
|
+
const logoUrl = text(theme.logoUrl, "theme.logoUrl", 500);
|
|
194
|
+
if (!/^https:\/\//.test(logoUrl)) fail("theme.logoUrl must be an https URL");
|
|
195
|
+
resolved.logoUrl = logoUrl;
|
|
196
|
+
}
|
|
197
|
+
if (theme.wordmarkImageUrl !== undefined) {
|
|
198
|
+
const url = text(theme.wordmarkImageUrl, "theme.wordmarkImageUrl", 500);
|
|
199
|
+
if (!/^https:\/\/[^\s"'<>\\`]+$/.test(url)) fail("theme.wordmarkImageUrl must be an absolute https URL");
|
|
200
|
+
resolved.wordmarkImageUrl = url;
|
|
201
|
+
for (const key of ["wordmarkWidth", "wordmarkHeight"] as const) {
|
|
202
|
+
const value = theme[key];
|
|
203
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > 2000) {
|
|
204
|
+
fail(`theme.${key} is required with theme.wordmarkImageUrl and must be a whole number of pixels from 1 to 2000`);
|
|
205
|
+
}
|
|
206
|
+
resolved[key] = value;
|
|
207
|
+
}
|
|
208
|
+
} else if (theme.wordmarkWidth !== undefined || theme.wordmarkHeight !== undefined) {
|
|
209
|
+
fail("theme.wordmarkWidth and theme.wordmarkHeight need theme.wordmarkImageUrl");
|
|
210
|
+
}
|
|
211
|
+
if (theme.footerWordmarkWidth !== undefined) {
|
|
212
|
+
if (theme.wordmarkImageUrl === undefined) fail("theme.footerWordmarkWidth needs theme.wordmarkImageUrl");
|
|
213
|
+
const width = theme.footerWordmarkWidth;
|
|
214
|
+
if (typeof width !== "number" || !Number.isInteger(width) || width < 1 || width > 400) {
|
|
215
|
+
fail("theme.footerWordmarkWidth must be a whole number of pixels from 1 to 400");
|
|
216
|
+
}
|
|
217
|
+
resolved.footerWordmarkWidth = width;
|
|
218
|
+
}
|
|
219
|
+
if (theme.wordmarkAlt !== undefined) {
|
|
220
|
+
const alt = text(theme.wordmarkAlt, "theme.wordmarkAlt", 200);
|
|
221
|
+
if (/[\u0000-\u001f\u007f]/.test(alt)) fail("theme.wordmarkAlt must not contain control characters");
|
|
222
|
+
resolved.wordmarkAlt = alt;
|
|
223
|
+
}
|
|
224
|
+
return resolved;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function resolveRule(raw: unknown, label: string): NewsletterRule {
|
|
228
|
+
if (raw === undefined) return {};
|
|
229
|
+
const rule = record(raw, label, ["sections", "tags", "types"]);
|
|
230
|
+
return {
|
|
231
|
+
...(rule.sections === undefined ? {} : { sections: stringList(rule.sections, `${label}.sections`) }),
|
|
232
|
+
...(rule.tags === undefined ? {} : { tags: stringList(rule.tags, `${label}.tags`) }),
|
|
233
|
+
...(rule.types === undefined ? {} : { types: stringList(rule.types, `${label}.types`) }),
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function resolveList(raw: unknown, index: number, instantAlerts: boolean): NewsletterList {
|
|
238
|
+
const label = `lists[${index}]`;
|
|
239
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) fail(`${label} must be an object`);
|
|
240
|
+
const kind = (raw as Record<string, unknown>).kind;
|
|
241
|
+
if (kind !== "roundup" && kind !== "alert") fail(`${label}.kind must be "roundup" or "alert"`);
|
|
242
|
+
const list = record(raw, label, LIST_KEYS[kind]);
|
|
243
|
+
const base = {
|
|
244
|
+
id: matching(list.id, `${label}.id`, NEWSLETTER_LIST_ID, "lowercase letters, digits and hyphens, starting with a letter (at most 40 characters)"),
|
|
245
|
+
name: text(list.name, `${label}.name`, 80),
|
|
246
|
+
description: text(list.description, `${label}.description`, 300, { optional: true, fallback: "" }),
|
|
247
|
+
};
|
|
248
|
+
if (kind === "roundup") {
|
|
249
|
+
const schedule = record(list.schedule, `${label}.schedule`, ["draft", "timezone"]);
|
|
250
|
+
return {
|
|
251
|
+
...base,
|
|
252
|
+
kind,
|
|
253
|
+
schedule: {
|
|
254
|
+
draft: matching(schedule.draft, `${label}.schedule.draft`, SCHEDULER_CRON, 'an EventBridge Scheduler cron such as "cron(0 13 ? * FRI *)"'),
|
|
255
|
+
timezone: timeZone(schedule.timezone, `${label}.schedule.timezone`),
|
|
256
|
+
},
|
|
257
|
+
issueType: matching(list.issueType, `${label}.issueType`, ISSUE_TYPE, "a lowercase item type name", "newsletter_issue"),
|
|
258
|
+
archivePath: matching(list.archivePath, `${label}.archivePath`, ARCHIVE_PATH, 'a path that starts and ends with "/", such as /roundup/'),
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
const approval = list.approval === undefined ? "admin" : list.approval;
|
|
262
|
+
if (approval !== "admin" && approval !== "none") fail(`${label}.approval must be "admin" or "none"`);
|
|
263
|
+
if (approval === "none" && !instantAlerts) {
|
|
264
|
+
fail(`${label}.approval "none" sends alerts without an admin approving them: set instantAlerts: true on the plugin first`);
|
|
265
|
+
}
|
|
266
|
+
return { ...base, kind, rule: resolveRule(list.rule, `${label}.rule`), approval };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function resolveNewsletterConfig(request: PluginRequest): NewsletterConfig {
|
|
270
|
+
const raw = record(typeof request === "string" ? { name: request } : request, "settings", [...KNOWN_KEYS]);
|
|
271
|
+
const siteId = matching(raw.siteId, "siteId", NEWSLETTER_SITE_ID, "a lowercase site id (letters, digits, hyphens), the SiteId of the sending stack");
|
|
272
|
+
const computeRoleArn = raw.computeRoleArn;
|
|
273
|
+
if (typeof computeRoleArn !== "string" || !COMPUTE_ROLE_ARN.test(computeRoleArn)) {
|
|
274
|
+
fail("computeRoleArn must be the IAM role ARN of the CMS app's compute role (the CmsComputeRoleArn output)");
|
|
275
|
+
}
|
|
276
|
+
const sendingDomain = matching(raw.sendingDomain, "sendingDomain", HOSTNAME, "a lowercase domain name such as news.example.com");
|
|
277
|
+
const fromAddress = text(raw.fromAddress, "fromAddress", 254);
|
|
278
|
+
const [local, domain, ...extra] = fromAddress.split("@");
|
|
279
|
+
if (extra.length > 0 || !local || local.length > 64 || !LOCAL_PART.test(local) || domain !== sendingDomain) {
|
|
280
|
+
fail(`fromAddress must be an address on ${sendingDomain} (lowercase, such as newsletter@${sendingDomain})`);
|
|
281
|
+
}
|
|
282
|
+
if (raw.instantAlerts !== undefined && typeof raw.instantAlerts !== "boolean") fail("instantAlerts must be a boolean");
|
|
283
|
+
const instantAlerts = raw.instantAlerts === true;
|
|
284
|
+
if (!Array.isArray(raw.lists) || raw.lists.length === 0 || raw.lists.length > 20) fail("lists must be a list of 1 to 20 lists");
|
|
285
|
+
const lists = raw.lists.map((entry, index) => resolveList(entry, index, instantAlerts));
|
|
286
|
+
const duplicates = lists.map((list) => list.id).filter((id, index, ids) => ids.indexOf(id) !== index);
|
|
287
|
+
if (duplicates.length > 0) fail(`lists has duplicate ids: ${[...new Set(duplicates)].join(", ")}`);
|
|
288
|
+
const roundups = lists.filter((list) => list.kind === "roundup");
|
|
289
|
+
if (new Set(roundups.map((list) => (list as RoundupList).archivePath)).size !== roundups.length) fail("roundup lists need different archivePath values");
|
|
290
|
+
return {
|
|
291
|
+
siteId,
|
|
292
|
+
siteOrigin: origin(raw.siteOrigin),
|
|
293
|
+
basePath: basePath(raw.basePath),
|
|
294
|
+
computeRoleArn,
|
|
295
|
+
sendingDomain,
|
|
296
|
+
fromAddress,
|
|
297
|
+
fromName: text(raw.fromName, "fromName", 80),
|
|
298
|
+
instantAlerts,
|
|
299
|
+
theme: resolveTheme(raw.theme),
|
|
300
|
+
lists,
|
|
301
|
+
itemUrlStyle: itemUrlStyle(raw.itemUrlStyle),
|
|
302
|
+
configurationSet: matching(raw.configurationSet, "configurationSet", SES_NAME, "an SES configuration set name", `papyrus-${siteId}`),
|
|
303
|
+
eventTopicName: matching(raw.eventTopicName, "eventTopicName", SES_NAME, "an SNS topic name", `papyrus-newsletter-events-${siteId}`),
|
|
304
|
+
};
|
|
305
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"""Bundle entry kept for the shared Python bundler, which copies ``handler.py`` to the asset root.
|
|
2
|
+
|
|
3
|
+
The Lambda's handler is ``papyrus_content.newsletter.handler.handler`` (src/papyrus_content/newsletter/handler.py).
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from papyrus_content.newsletter.handler import handler # noqa: F401
|