@cosmicdrift/kumiko-samples 0.349.0 → 0.350.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosmicdrift/kumiko-samples",
3
- "version": "0.349.0",
3
+ "version": "0.350.0",
4
4
  "description": "Source trees of the Kumiko sample recipes, sample apps and bundled features in repo layout, for tooling such as few-shot corpus builds.",
5
5
  "license": "BUSL-1.1",
6
6
  "author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosmicdrift/kumiko-bundled-features",
3
- "version": "0.349.0",
3
+ "version": "0.350.0",
4
4
  "description": "Built-in features — tenant, user, auth, delivery. The stuff you'd rewrite anyway, already typed.",
5
5
  "license": "BUSL-1.1",
6
6
  "author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
@@ -1,4 +1,10 @@
1
1
  [
2
+ {
3
+ "version": "0.350.0",
4
+ "type": "improvement",
5
+ "title": "Mails get a plain-text part (EmailMessage.text, NotificationRenderer.renderText, simple renderer and GDPR mails fill it)",
6
+ "detail": "Email text part\n`EmailMessage` gets an optional `text`. With it the SMTP transport sends `multipart/alternative` with a plain-text part. `NotificationRenderer` gets an optional `renderText`, which the email channel sends next to the HTML through the queued render and send jobs. `createSimpleRenderer` implements it from the template data (header, sections, footer, branding footer; buttons as `label: url`). The GDPR default mails return and send `text` as well. The PII guard checks and redacts `text` like the body."
7
+ },
2
8
  {
3
9
  "version": "0.348.1",
4
10
  "type": "fix",
@@ -106,13 +106,15 @@ export function createEmailChannel(options: EmailChannelOptions): DeliveryChanne
106
106
  title: message.title,
107
107
  body: message.body,
108
108
  };
109
- const html = await renderer.render({
109
+ const rendererInput = {
110
110
  template: message.notificationType,
111
111
  variables,
112
112
  locale: message.locale,
113
- });
113
+ };
114
+ const html = await renderer.render(rendererInput);
115
+ const text = renderer.renderText ? await renderer.renderText(rendererInput) : undefined;
114
116
  const subject = (variables["subject"] as string) ?? message.title; // @cast-boundary dynamic-key
115
- return { html, subject };
117
+ return { html, subject, ...(text !== undefined && { text }) };
116
118
  }
117
119
 
118
120
  return {
@@ -132,9 +134,17 @@ export function createEmailChannel(options: EmailChannelOptions): DeliveryChanne
132
134
  },
133
135
 
134
136
  async send(address, message, _ctx, rendered) {
135
- const { html, subject } = rendered ?? (await renderMessage(message));
137
+ const { html, subject, text } = rendered ?? (await renderMessage(message));
136
138
  const envelope = emailEnvelopeFrom(message.data);
137
- await transport.send(guardEmailMessage({ to: address, subject, html, ...envelope }));
139
+ await transport.send(
140
+ guardEmailMessage({
141
+ to: address,
142
+ subject,
143
+ html,
144
+ ...(text !== undefined && { text }),
145
+ ...envelope,
146
+ }),
147
+ );
138
148
  return { status: "sent", address };
139
149
  },
140
150
  };
@@ -36,7 +36,9 @@ export function guardEmailMessage(message: EmailMessage): EmailMessage {
36
36
  }
37
37
  }
38
38
  const leaking =
39
- message.subject.includes(CIPHERTEXT_MARKER) || message.html.includes(CIPHERTEXT_MARKER);
39
+ message.subject.includes(CIPHERTEXT_MARKER) ||
40
+ message.html.includes(CIPHERTEXT_MARKER) ||
41
+ message.text?.includes(CIPHERTEXT_MARKER) === true;
40
42
  if (!leaking) return message;
41
43
  const detail =
42
44
  "[channel-email] mail subject/body contains a PII ciphertext " +
@@ -48,6 +50,9 @@ export function guardEmailMessage(message: EmailMessage): EmailMessage {
48
50
  ...message,
49
51
  subject: message.subject.replace(CIPHERTEXT_RE, "[pii-redacted]"),
50
52
  html: message.html.replace(CIPHERTEXT_RE, "[pii-redacted]"),
53
+ ...(message.text !== undefined && {
54
+ text: message.text.replace(CIPHERTEXT_RE, "[pii-redacted]"),
55
+ }),
51
56
  };
52
57
  }
53
58
 
@@ -92,6 +92,7 @@ export function createSmtpTransport(options: SmtpTransportOptions): EmailTranspo
92
92
  to: message.to,
93
93
  subject: message.subject,
94
94
  html: message.html,
95
+ ...(message.text !== undefined && { text: message.text }),
95
96
  ...(message.replyTo && { replyTo: message.replyTo }),
96
97
  ...(message.headers && { headers: message.headers }),
97
98
  });
@@ -3,6 +3,9 @@ export type EmailMessage = {
3
3
  readonly to: string;
4
4
  readonly subject: string;
5
5
  readonly html: string;
6
+ // Plain-text alternative to `html`. Set → the mail goes out as
7
+ // multipart/alternative, and clients without HTML show this part.
8
+ readonly text?: string;
6
9
  // Per-message From override. Absent → the transport's configured From (the
7
10
  // app-wide default). Set when one send must originate from a specific
8
11
  // mailbox — a reply from the address the original mail reached, not noreply@.
@@ -47,7 +47,9 @@ const renderJobPayloadSchema = z.object({
47
47
  });
48
48
 
49
49
  const sendJobPayloadSchema = renderJobPayloadSchema.extend({
50
- rendered: z.object({ html: z.string(), subject: z.string() }).optional(),
50
+ rendered: z
51
+ .object({ html: z.string(), subject: z.string(), text: z.string().optional() })
52
+ .optional(),
51
53
  });
52
54
 
53
55
  type RenderJobPayload = z.infer<typeof renderJobPayloadSchema>;
@@ -48,6 +48,7 @@ export type ChannelResult = {
48
48
  export type RenderedMessage = {
49
49
  readonly html: string;
50
50
  readonly subject: string;
51
+ readonly text?: string;
51
52
  };
52
53
 
53
54
  // `mode` decides how the delivery-service dispatches a channel:
@@ -83,6 +84,8 @@ export type RendererInput = {
83
84
  export type NotificationRenderer = {
84
85
  readonly name: string;
85
86
  render(input: RendererInput): Promise<string>;
87
+ /** Plain-text version of the same mail; the email channel sends it as the text part of a multipart/alternative mail. */
88
+ renderText?(input: RendererInput): Promise<string>;
86
89
  };
87
90
 
88
91
  // --- Delivery Log Entry ---
@@ -117,21 +117,35 @@ function renderBrandingHeader(
117
117
  return `<div style="margin:0 0 24px;padding:0 0 16px;border-bottom:3px solid ${escapeHtmlAttr(primaryColor)}">${logo}</div>`;
118
118
  }
119
119
 
120
- function renderBrandingFooter(
121
- branding: MailBranding | undefined,
120
+ type ResolvedBrandingFooter = {
121
+ readonly footerText: string | undefined;
122
+ readonly links: readonly { readonly label: string; readonly url: string }[];
123
+ };
124
+
125
+ function resolveBrandingFooter(
126
+ branding: MailBranding,
122
127
  locale: string | undefined,
123
- ): string {
124
- if (!branding) return "";
128
+ ): ResolvedBrandingFooter {
125
129
  const pick = (value: LocalizedText | undefined): string | undefined =>
126
130
  resolveLocalized(value, locale, branding.defaultLocale);
127
131
  const links = (branding.footerLinks ?? []).flatMap((link) => {
128
132
  const url = pick(link.url);
129
133
  if (url === undefined || !isHttpUrl(url)) return [];
130
- return [
131
- `<a href="${escapeHtmlAttr(url)}" style="color:#999">${escapeHtml(pick(link.label) ?? "")}</a>`,
132
- ];
134
+ return [{ label: pick(link.label) ?? "", url }];
133
135
  });
134
- const footerText = pick(branding.footerText);
136
+ return { footerText: pick(branding.footerText), links };
137
+ }
138
+
139
+ function renderBrandingFooter(
140
+ branding: MailBranding | undefined,
141
+ locale: string | undefined,
142
+ ): string {
143
+ if (!branding) return "";
144
+ const { footerText, links: resolvedLinks } = resolveBrandingFooter(branding, locale);
145
+ const links = resolvedLinks.map(
146
+ (link) =>
147
+ `<a href="${escapeHtmlAttr(link.url)}" style="color:#999">${escapeHtml(link.label)}</a>`,
148
+ );
135
149
  const footerPartsHtml = [footerText ? escapeHtml(footerText) : "", links.join(" · ")].filter(
136
150
  (part) => part !== "",
137
151
  );
@@ -140,6 +154,18 @@ function renderBrandingFooter(
140
154
  return `<p style="margin:16px 0 0;color:#999;font-size:12px">${footerHtml}</p>`;
141
155
  }
142
156
 
157
+ function brandingFooterLines(
158
+ branding: MailBranding | undefined,
159
+ locale: string | undefined,
160
+ ): readonly string[] {
161
+ if (!branding) return [];
162
+ const { footerText, links } = resolveBrandingFooter(branding, locale);
163
+ return [
164
+ ...(footerText ? [footerText] : []),
165
+ ...links.map((link) => (link.label ? `${link.label}: ${link.url}` : link.url)),
166
+ ];
167
+ }
168
+
143
169
  type Section =
144
170
  | { readonly text: string }
145
171
  | { readonly heading: string }
@@ -156,6 +182,29 @@ type EmailTemplateData = {
156
182
  readonly body?: string;
157
183
  };
158
184
 
185
+ function templateContent(variables: Readonly<Record<string, unknown>>): {
186
+ readonly header: string | undefined;
187
+ readonly sections: readonly Section[] | undefined;
188
+ readonly footer: string | undefined;
189
+ } {
190
+ const data = variables as EmailTemplateData; // @cast-boundary render-helper
191
+ // Without structured fields, title + body become the header and a single text section.
192
+ return {
193
+ header: data.header ?? data.title,
194
+ sections: data.sections ?? (data.body ? [{ text: data.body }] : undefined),
195
+ footer: data.footer,
196
+ };
197
+ }
198
+
199
+ // Markdown stays as written: its source is already readable plain text.
200
+ function sectionText(section: Section): string {
201
+ if ("text" in section) return section.text;
202
+ if ("heading" in section) return section.heading;
203
+ if ("markdown" in section) return section.markdown;
204
+ if ("button" in section) return `${section.button.label}: ${section.button.url}`;
205
+ return "";
206
+ }
207
+
159
208
  function renderSection(section: Section, primaryColor: string): string {
160
209
  if ("text" in section) {
161
210
  return `<p style="margin:0 0 16px;color:#333;font-size:14px;line-height:1.5">${escapeHtml(section.text)}</p>`;
@@ -183,11 +232,7 @@ export function createSimpleRenderer(branding?: MailBranding): NotificationRende
183
232
  name: "simple",
184
233
 
185
234
  async render(input) {
186
- const data = input.variables as EmailTemplateData; // @cast-boundary render-helper
187
-
188
- // Fallback: if no structured fields, use title + body as header + single text section
189
- const header = data.header ?? data.title;
190
- const sections = data.sections ?? (data.body ? [{ text: data.body }] : undefined);
235
+ const { header, sections, footer } = templateContent(input.variables);
191
236
 
192
237
  const parts: string[] = [];
193
238
  parts.push('<!DOCTYPE html><html><body style="margin:0;padding:0;font-family:sans-serif">');
@@ -206,9 +251,9 @@ export function createSimpleRenderer(branding?: MailBranding): NotificationRende
206
251
  }
207
252
  }
208
253
 
209
- if (data.footer) {
254
+ if (footer) {
210
255
  parts.push(
211
- `<p style="margin:24px 0 0;color:#999;font-size:12px;border-top:1px solid #eee;padding-top:16px">${escapeHtml(data.footer)}</p>`,
256
+ `<p style="margin:24px 0 0;color:#999;font-size:12px;border-top:1px solid #eee;padding-top:16px">${escapeHtml(footer)}</p>`,
212
257
  );
213
258
  }
214
259
 
@@ -216,6 +261,14 @@ export function createSimpleRenderer(branding?: MailBranding): NotificationRende
216
261
  parts.push("</div></body></html>");
217
262
  return parts.join("");
218
263
  },
264
+
265
+ async renderText(input) {
266
+ const { header, sections, footer } = templateContent(input.variables);
267
+ const brandingFooter = brandingFooterLines(branding, input.locale).join("\n");
268
+ return [header, ...(sections ?? []).map(sectionText), footer, brandingFooter]
269
+ .filter((block): block is string => block !== undefined && block !== "")
270
+ .join("\n\n");
271
+ },
219
272
  };
220
273
  }
221
274
 
@@ -44,6 +44,7 @@ registerMailTranslations("en", GDPR_MAIL_EN);
44
44
  export type RenderedEmail = {
45
45
  readonly subject: string;
46
46
  readonly html: string;
47
+ readonly text: string;
47
48
  };
48
49
 
49
50
  export type RenderExportReadyEmailArgs = {
@@ -89,7 +90,13 @@ export function renderExportReadyEmail(args: RenderExportReadyEmailArgs): Render
89
90
  <p style="margin: 0 0 24px;">${renderButton({ url: args.downloadUrl, label: t(locale, "gdpr.mail.exportReady.button") })}</p>
90
91
  <p style="margin: 0 0 8px; font-size: 13px; color: #555;">${escapeHtml(t(locale, "gdpr.mail.exportReady.expiry", { when: formatTimestamp(args.expiresAt) }))}</p>
91
92
  ${renderFallbackUrl({ url: args.downloadUrl, label: t(locale, "gdpr.mail.fallbackUrl") })}`;
92
- return { subject, html: renderShell({ title: subject, bodyHtml: wrapCell(body), locale }) };
93
+ const text = plainTextBody([
94
+ t(locale, "gdpr.mail.greeting"),
95
+ t(locale, "gdpr.mail.exportReady.intro", { app }),
96
+ `${t(locale, "gdpr.mail.exportReady.button")}: ${args.downloadUrl}`,
97
+ t(locale, "gdpr.mail.exportReady.expiry", { when: formatTimestamp(args.expiresAt) }),
98
+ ]);
99
+ return { subject, html: renderShell({ title: subject, bodyHtml: wrapCell(body), locale }), text };
93
100
  }
94
101
 
95
102
  export function renderExportFailedEmail(args: RenderExportFailedEmailArgs): RenderedEmail {
@@ -99,7 +106,11 @@ export function renderExportFailedEmail(args: RenderExportFailedEmailArgs): Rend
99
106
  const body = `
100
107
  <p style="margin: 0 0 16px; font-size: 16px;">${escapeHtml(t(locale, "gdpr.mail.greeting"))}</p>
101
108
  <p style="margin: 0; font-size: 14px; line-height: 1.5;">${escapeHtml(t(locale, "gdpr.mail.exportFailed.intro", { app }))}</p>`;
102
- return { subject, html: renderShell({ title: subject, bodyHtml: wrapCell(body), locale }) };
109
+ const text = plainTextBody([
110
+ t(locale, "gdpr.mail.greeting"),
111
+ t(locale, "gdpr.mail.exportFailed.intro", { app }),
112
+ ]);
113
+ return { subject, html: renderShell({ title: subject, bodyHtml: wrapCell(body), locale }), text };
103
114
  }
104
115
 
105
116
  export function renderDeletionRequestedEmail(
@@ -113,7 +124,12 @@ export function renderDeletionRequestedEmail(
113
124
  <p style="margin: 0 0 16px; font-size: 16px;">${escapeHtml(t(locale, "gdpr.mail.greeting"))}</p>
114
125
  <p style="margin: 0 0 16px; font-size: 14px; line-height: 1.5;">${escapeHtml(t(locale, "gdpr.mail.deletionRequested.intro", { app, when }))}</p>
115
126
  <p style="margin: 0; font-size: 13px; color: #555;">${escapeHtml(t(locale, "gdpr.mail.deletionRequested.cancel"))}</p>`;
116
- return { subject, html: renderShell({ title: subject, bodyHtml: wrapCell(body), locale }) };
127
+ const text = plainTextBody([
128
+ t(locale, "gdpr.mail.greeting"),
129
+ t(locale, "gdpr.mail.deletionRequested.intro", { app, when }),
130
+ t(locale, "gdpr.mail.deletionRequested.cancel"),
131
+ ]);
132
+ return { subject, html: renderShell({ title: subject, bodyHtml: wrapCell(body), locale }), text };
117
133
  }
118
134
 
119
135
  export function renderDeletionExecutedEmail(args: RenderDeletionExecutedEmailArgs): RenderedEmail {
@@ -124,7 +140,15 @@ export function renderDeletionExecutedEmail(args: RenderDeletionExecutedEmailArg
124
140
  const body = `
125
141
  <p style="margin: 0 0 16px; font-size: 16px;">${escapeHtml(t(locale, "gdpr.mail.greeting"))}</p>
126
142
  <p style="margin: 0; font-size: 14px; line-height: 1.5;">${escapeHtml(t(locale, "gdpr.mail.deletionExecuted.intro", { app, when }))}</p>`;
127
- return { subject, html: renderShell({ title: subject, bodyHtml: wrapCell(body), locale }) };
143
+ const text = plainTextBody([
144
+ t(locale, "gdpr.mail.greeting"),
145
+ t(locale, "gdpr.mail.deletionExecuted.intro", { app, when }),
146
+ ]);
147
+ return { subject, html: renderShell({ title: subject, bodyHtml: wrapCell(body), locale }), text };
148
+ }
149
+
150
+ function plainTextBody(paragraphs: readonly string[]): string {
151
+ return paragraphs.join("\n\n");
128
152
  }
129
153
 
130
154
  function wrapCell(bodyHtml: string): string {
@@ -57,13 +57,13 @@ export function makeDefaultExportReadyEmail(
57
57
  ): SendExportReadyEmailFn {
58
58
  return async (args) => {
59
59
  const transport = await resolveTransport(args.tenantId);
60
- const { subject, html } = renderExportReadyEmail({
60
+ const { subject, html, text } = renderExportReadyEmail({
61
61
  downloadUrl: args.downloadUrl,
62
62
  expiresAt: args.expiresAt,
63
63
  locale: localeFor(args.userLocale, defaults),
64
64
  appName: defaults.appName,
65
65
  });
66
- await transport.send({ to: args.userEmail, subject, html });
66
+ await transport.send({ to: args.userEmail, subject, html, text });
67
67
  };
68
68
  }
69
69
 
@@ -73,11 +73,11 @@ export function makeDefaultExportFailedEmail(
73
73
  ): SendExportFailedEmailFn {
74
74
  return async (args) => {
75
75
  const transport = await resolveTransport(args.tenantId);
76
- const { subject, html } = renderExportFailedEmail({
76
+ const { subject, html, text } = renderExportFailedEmail({
77
77
  locale: localeFor(args.userLocale, defaults),
78
78
  appName: defaults.appName,
79
79
  });
80
- await transport.send({ to: args.userEmail, subject, html });
80
+ await transport.send({ to: args.userEmail, subject, html, text });
81
81
  };
82
82
  }
83
83
 
@@ -87,12 +87,12 @@ export function makeDefaultDeletionRequestedEmail(
87
87
  ): SendDeletionRequestedEmailFn {
88
88
  return async (args) => {
89
89
  const transport = await resolveTransport(args.tenantId);
90
- const { subject, html } = renderDeletionRequestedEmail({
90
+ const { subject, html, text } = renderDeletionRequestedEmail({
91
91
  gracePeriodEnd: args.gracePeriodEnd,
92
92
  locale: localeFor(args.userLocale, defaults),
93
93
  appName: defaults.appName,
94
94
  });
95
- await transport.send({ to: args.userEmail, subject, html });
95
+ await transport.send({ to: args.userEmail, subject, html, text });
96
96
  };
97
97
  }
98
98
 
@@ -110,11 +110,11 @@ export function makeDefaultDeletionExecutedEmail(
110
110
  return;
111
111
  }
112
112
  const transport = await resolveTransport(tenantId);
113
- const { subject, html } = renderDeletionExecutedEmail({
113
+ const { subject, html, text } = renderDeletionExecutedEmail({
114
114
  executedAt: args.executedAt,
115
115
  locale: localeFor(args.userLocale, defaults),
116
116
  appName: defaults.appName,
117
117
  });
118
- await transport.send({ to: args.userEmail, subject, html });
118
+ await transport.send({ to: args.userEmail, subject, html, text });
119
119
  };
120
120
  }