@pithy-sh/email 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/capability.d.ts.map +1 -1
- package/dist/capability.js +15 -0
- package/dist/crypto/signingKey.d.ts +4 -0
- package/dist/crypto/signingKey.d.ts.map +1 -1
- package/dist/crypto/signingKey.js +5 -1
- package/dist/data/emailJob.d.ts +1 -1
- package/dist/data/tables.d.ts +1 -1
- package/dist/error/errors.d.ts +4 -3
- package/dist/error/errors.d.ts.map +1 -1
- package/dist/provision/provisionEmail.d.ts +9 -1
- package/dist/provision/provisionEmail.d.ts.map +1 -1
- package/dist/provision/provisionEmail.js +5 -1
- package/dist/provision/resolveEmailConfig.d.ts +7 -0
- package/dist/provision/resolveEmailConfig.d.ts.map +1 -1
- package/dist/provision/resolveEmailConfig.js +14 -1
- package/dist/send/enqueue.d.ts +16 -0
- package/dist/send/enqueue.d.ts.map +1 -1
- package/dist/send/enqueue.js +14 -0
- package/dist/templates/messages.d.ts.map +1 -1
- package/dist/templates/messages.es.d.ts.map +1 -1
- package/dist/templates/messages.es.js +26 -0
- package/dist/templates/messages.js +28 -2
- package/dist/templates/precompiled.generated.d.ts +86 -0
- package/dist/templates/precompiled.generated.d.ts.map +1 -1
- package/dist/templates/precompiled.generated.js +1234 -0
- package/dist/templates/registry.d.ts.map +1 -1
- package/dist/templates/registry.js +67 -0
- package/dist/templates/samples.d.ts.map +1 -1
- package/dist/templates/samples.js +15 -0
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +1 -1
- package/package.json +4 -4
- package/src/capability.ts +16 -0
- package/src/crypto/signingKey.ts +6 -0
- package/src/error/errors.ts +4 -3
- package/src/provision/provisionEmail.ts +10 -1
- package/src/provision/resolveEmailConfig.ts +12 -0
- package/src/send/enqueue.ts +28 -0
- package/src/templates/messages.es.ts +36 -0
- package/src/templates/messages.ts +36 -2
- package/src/templates/precompiled.generated.ts +417 -0
- package/src/templates/registry.ts +105 -0
- package/src/templates/samples.ts +4 -0
- package/src/version.generated.ts +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/templates/registry.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH;oFACoF;AACpF,MAAM,WAAW,QAAQ;IACvB,0FAA0F;IAC1F,IAAI,EAAE,MAAM,CAAC;IACb,8EAA8E;IAC9E,KAAK,EAAE,MAAM,CAAC;CACf;AAED,8GAA8G;AAC9G,MAAM,WAAW,aAAa;IAC5B,qEAAqE;IACrE,EAAE,EAAE,MAAM,CAAC;IACX,+GAA+G;IAC/G,QAAQ,EAAE,gBAAgB,CAAC;IAC3B;;;;;;;;;OASG;IACH,IAAI,EAAE,SAAS,CAAC;IAChB,mGAAmG;IACnG,KAAK,EAAE,YAAY,CAAC;IACpB,kFAAkF;IAClF,OAAO,EAAE,CAAC,CAAC,OAAO,CAAC;IACnB,yEAAyE;IACzE,OAAO,EAAE,MAAM,CAAC;IAChB,yFAAyF;IACzF,IAAI,EAAE,MAAM,CAAC;IACb,kFAAkF;IAClF,IAAI,EAAE,MAAM,CAAC;IACb,uEAAuE;IACvE,KAAK,EAAE,QAAQ,EAAE,CAAC;CACnB;
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/templates/registry.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH;oFACoF;AACpF,MAAM,WAAW,QAAQ;IACvB,0FAA0F;IAC1F,IAAI,EAAE,MAAM,CAAC;IACb,8EAA8E;IAC9E,KAAK,EAAE,MAAM,CAAC;CACf;AAED,8GAA8G;AAC9G,MAAM,WAAW,aAAa;IAC5B,qEAAqE;IACrE,EAAE,EAAE,MAAM,CAAC;IACX,+GAA+G;IAC/G,QAAQ,EAAE,gBAAgB,CAAC;IAC3B;;;;;;;;;OASG;IACH,IAAI,EAAE,SAAS,CAAC;IAChB,mGAAmG;IACnG,KAAK,EAAE,YAAY,CAAC;IACpB,kFAAkF;IAClF,OAAO,EAAE,CAAC,CAAC,OAAO,CAAC;IACnB,yEAAyE;IACzE,OAAO,EAAE,MAAM,CAAC;IAChB,yFAAyF;IACzF,IAAI,EAAE,MAAM,CAAC;IACb,kFAAkF;IAClF,IAAI,EAAE,MAAM,CAAC;IACb,uEAAuE;IACvE,KAAK,EAAE,QAAQ,EAAE,CAAC;CACnB;AAuMD;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,wBAAwB;;;;;;;;;;;;;;;;iBA2ClC,CAAC;AACJ;;;;;;GAMG;AACH,MAAM,MAAM,wBAAwB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,wBAAwB,CAAC,CAAC;AAmEhF,0CAA0C;AAC1C,eAAO,MAAM,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAgPnD,CAAC"}
|
|
@@ -81,6 +81,26 @@ const PasswordChangedPayload = z.object({
|
|
|
81
81
|
when: z.string().describe("A human-readable timestamp of when the credential changed."),
|
|
82
82
|
supportUrl: z.string().describe("A link to contact support if the change was not the recipient. Tracked when click tracking is on.")
|
|
83
83
|
}).describe("Inputs for the account-credential-changed security notice.");
|
|
84
|
+
/**
|
|
85
|
+
* The four account-lifecycle notices `@pithy-sh/auth` sends (#620). Every date arrives already written for
|
|
86
|
+
* the reader — `1 November 2026` — because the sender knows the recipient's locale and this template does
|
|
87
|
+
* not, which is the convention `when` set for `securityAlert`.
|
|
88
|
+
*/
|
|
89
|
+
const AccountSuspendedPayload = z.object({
|
|
90
|
+
name: z.string().optional().describe("The recipient's name, for a personal greeting. Optional."),
|
|
91
|
+
reason: z.string().optional().describe("Why the account was suspended, in the operator's words. Rendered escaped, as every payload value is. Omitted when none was given."),
|
|
92
|
+
until: z.string().optional().describe("When the suspension ends by itself, written for the reader. Omitted for one with no end.")
|
|
93
|
+
}).describe("Inputs for the notice that an account was suspended.");
|
|
94
|
+
const AccountReinstatedPayload = z.object({ name: z.string().optional().describe("The recipient's name, for a personal greeting. Optional.") }).describe("Inputs for the notice that a suspension was lifted.");
|
|
95
|
+
const DeletionScheduledPayload = z.object({
|
|
96
|
+
name: z.string().optional().describe("The recipient's name, for a personal greeting. Optional."),
|
|
97
|
+
purgeOn: z.string().describe("The date the account's data will be erased, written for the reader. Until then it can be restored."),
|
|
98
|
+
restoreUrl: z.string().optional().describe("Where the account's owner can restore it inside the window. Tracked when click tracking is on. Omitted when the project offers no self-service restore.")
|
|
99
|
+
}).describe("Inputs for the notice that an account was deleted and will be erased.");
|
|
100
|
+
const DeletionCompletePayload = z.object({
|
|
101
|
+
name: z.string().optional().describe("The recipient's name, for a personal greeting. Optional."),
|
|
102
|
+
purgedOn: z.string().describe("The date the account's data was erased, written for the reader.")
|
|
103
|
+
}).describe("Inputs for the notice that an account's data was erased — sent before the address is.");
|
|
84
104
|
/** One labeled fact in an operational notice — what makes the notice specific rather than a mood. */
|
|
85
105
|
const OperationalNoticeFact = z.object({
|
|
86
106
|
label: z.string().describe("What this fact is: `Environment`, `Last rotated`, `Version`, `Owner`."),
|
|
@@ -253,6 +273,53 @@ const templates = {
|
|
|
253
273
|
label: "password-support"
|
|
254
274
|
}]
|
|
255
275
|
},
|
|
276
|
+
accountSuspended: {
|
|
277
|
+
id: "accountSuspended",
|
|
278
|
+
category: "transactional",
|
|
279
|
+
kind: "transactional",
|
|
280
|
+
width: "narrow",
|
|
281
|
+
payload: AccountSuspendedPayload,
|
|
282
|
+
subject: `{{t "email/account_suspended.subject" app=theme.appName}}`,
|
|
283
|
+
html: layout(`${heading("{{t \"email/account_suspended.heading\"}}")}<p style="margin:0 0 8px">${greeting} {{t "email/account_suspended.body" app=theme.appName}}</p>{{#if reason}}<p style="margin:0 0 8px">{{t "email/account_suspended.reason" reason=reason}}</p>{{/if}}{{#if until}}<p style="margin:0 0 8px">{{t "email/account_suspended.until" until=until}}</p>{{/if}}<p class="t-subtle" style="margin:16px 0 0; font-size:13px; color:{{theme.light.textSubtle}}">{{t "email/account_suspended.contact"}}</p>`),
|
|
284
|
+
text: `${greeting}\n\n{{t "email/account_suspended.text_body" app=theme.appName}}{{#if reason}}\n\n{{t "email/account_suspended.reason" reason=reason}}{{/if}}{{#if until}}\n\n{{t "email/account_suspended.until" until=until}}{{/if}}\n\n{{t "email/account_suspended.text_contact"}}`,
|
|
285
|
+
links: []
|
|
286
|
+
},
|
|
287
|
+
accountReinstated: {
|
|
288
|
+
id: "accountReinstated",
|
|
289
|
+
category: "transactional",
|
|
290
|
+
kind: "transactional",
|
|
291
|
+
width: "narrow",
|
|
292
|
+
payload: AccountReinstatedPayload,
|
|
293
|
+
subject: `{{t "email/account_reinstated.subject" app=theme.appName}}`,
|
|
294
|
+
html: layout(`${heading("{{t \"email/account_reinstated.heading\"}}")}<p style="margin:0 0 8px">${greeting} {{t "email/account_reinstated.body" app=theme.appName}}</p>`),
|
|
295
|
+
text: `${greeting}\n\n{{t "email/account_reinstated.text_body" app=theme.appName}}`,
|
|
296
|
+
links: []
|
|
297
|
+
},
|
|
298
|
+
deletionScheduled: {
|
|
299
|
+
id: "deletionScheduled",
|
|
300
|
+
category: "transactional",
|
|
301
|
+
kind: "transactional",
|
|
302
|
+
width: "narrow",
|
|
303
|
+
payload: DeletionScheduledPayload,
|
|
304
|
+
subject: `{{t "email/deletion_scheduled.subject" app=theme.appName}}`,
|
|
305
|
+
html: layout(`${heading("{{t \"email/deletion_scheduled.heading\"}}")}<p style="margin:0 0 8px">${greeting} {{t "email/deletion_scheduled.body" app=theme.appName}}</p><p style="margin:0 0 8px">{{t "email/deletion_scheduled.purge" purgeOn=purgeOn}}</p>{{#if restoreUrl}}${button("restoreUrl", "{{t \"email/deletion_scheduled.cta\"}}")}{{/if}}<p class="t-subtle" style="margin:16px 0 0; font-size:13px; color:{{theme.light.textSubtle}}">{{t "email/deletion_scheduled.contact" purgeOn=purgeOn}}</p>`),
|
|
306
|
+
text: `${greeting}\n\n{{t "email/deletion_scheduled.text_body" app=theme.appName}}\n\n{{t "email/deletion_scheduled.purge" purgeOn=purgeOn}}{{#if restoreUrl}}\n\n{{t "email/deletion_scheduled.text_restore"}} {{restoreUrl}}{{/if}}\n\n{{t "email/deletion_scheduled.text_contact" purgeOn=purgeOn}}`,
|
|
307
|
+
links: [{
|
|
308
|
+
path: "restoreUrl",
|
|
309
|
+
label: "deletion-restore"
|
|
310
|
+
}]
|
|
311
|
+
},
|
|
312
|
+
deletionComplete: {
|
|
313
|
+
id: "deletionComplete",
|
|
314
|
+
category: "transactional",
|
|
315
|
+
kind: "transactional",
|
|
316
|
+
width: "narrow",
|
|
317
|
+
payload: DeletionCompletePayload,
|
|
318
|
+
subject: `{{t "email/deletion_complete.subject" app=theme.appName}}`,
|
|
319
|
+
html: layout(`${heading("{{t \"email/deletion_complete.heading\"}}")}<p style="margin:0 0 8px">${greeting} {{t "email/deletion_complete.body" app=theme.appName purgedOn=purgedOn}}</p><p class="t-subtle" style="margin:16px 0 0; font-size:13px; color:{{theme.light.textSubtle}}">{{t "email/deletion_complete.last"}}</p>`),
|
|
320
|
+
text: `${greeting}\n\n{{t "email/deletion_complete.text_body" app=theme.appName purgedOn=purgedOn}}\n\n{{t "email/deletion_complete.last"}}`,
|
|
321
|
+
links: []
|
|
322
|
+
},
|
|
256
323
|
operationalNotice: {
|
|
257
324
|
id: "operationalNotice",
|
|
258
325
|
category: "transactional",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"samples.d.ts","sourceRoot":"","sources":["../../src/templates/samples.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,
|
|
1
|
+
{"version":3,"file":"samples.d.ts","sourceRoot":"","sources":["../../src/templates/samples.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAsElD,CAAC"}
|
|
@@ -53,6 +53,21 @@ const samplePayloads = {
|
|
|
53
53
|
when: "just now",
|
|
54
54
|
supportUrl: "https://example.com/support"
|
|
55
55
|
},
|
|
56
|
+
accountSuspended: {
|
|
57
|
+
name: "Sam",
|
|
58
|
+
reason: "A payment is under review.",
|
|
59
|
+
until: "1 November 2026"
|
|
60
|
+
},
|
|
61
|
+
accountReinstated: { name: "Sam" },
|
|
62
|
+
deletionScheduled: {
|
|
63
|
+
name: "Sam",
|
|
64
|
+
purgeOn: "19 November 2026",
|
|
65
|
+
restoreUrl: "https://example.com/account/restore"
|
|
66
|
+
},
|
|
67
|
+
deletionComplete: {
|
|
68
|
+
name: "Sam",
|
|
69
|
+
purgedOn: "19 November 2026"
|
|
70
|
+
},
|
|
56
71
|
operationalNotice: {
|
|
57
72
|
severity: "warning",
|
|
58
73
|
summary: "A secret has not been rotated in 90 days",
|
|
@@ -3,5 +3,5 @@
|
|
|
3
3
|
/** This package's npm name — the join key against a release feed. */
|
|
4
4
|
export declare const PACKAGE_NAME = "@pithy-sh/email";
|
|
5
5
|
/** This package's version, stamped from its own package.json at generation time. */
|
|
6
|
-
export declare const PACKAGE_VERSION = "0.
|
|
6
|
+
export declare const PACKAGE_VERSION = "0.6.0";
|
|
7
7
|
//# sourceMappingURL=version.generated.d.ts.map
|
|
@@ -4,6 +4,6 @@
|
|
|
4
4
|
/** This package's npm name — the join key against a release feed. */
|
|
5
5
|
const PACKAGE_NAME = "@pithy-sh/email";
|
|
6
6
|
/** This package's version, stamped from its own package.json at generation time. */
|
|
7
|
-
const PACKAGE_VERSION = "0.
|
|
7
|
+
const PACKAGE_VERSION = "0.6.0";
|
|
8
8
|
//#endregion
|
|
9
9
|
export { PACKAGE_NAME, PACKAGE_VERSION };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pithy-sh/email",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"scripts": {
|
|
28
28
|
"precompile": "bun scripts/precompile.ts",
|
|
29
29
|
"build": "bun run precompile && tsdown && tsc -p tsconfig.build.json && bun ../../tooling/build/src/stampDeclarations.ts",
|
|
30
|
-
"typecheck": "
|
|
30
|
+
"typecheck": "bun check -p tsconfig.json",
|
|
31
31
|
"test": "bun run precompile && vitest run",
|
|
32
32
|
"test:node": "bun run precompile && vitest run --project=node",
|
|
33
33
|
"test:workers": "bun run precompile && vitest run --project=workers",
|
|
@@ -42,8 +42,8 @@
|
|
|
42
42
|
"postal-mime": "^3.0.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@pithy-sh/core": "^0.
|
|
46
|
-
"@pithy-sh/secrets": "^0.
|
|
45
|
+
"@pithy-sh/core": "^0.12.0",
|
|
46
|
+
"@pithy-sh/secrets": "^0.6.0",
|
|
47
47
|
"hono": "^4.13.2",
|
|
48
48
|
"kysely": "^0.29.0",
|
|
49
49
|
"zod": "^4.4.0"
|
package/src/capability.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import type { D1Database } from "@cloudflare/workers-types";
|
|
5
5
|
import { type Capability, defineCapability } from "@pithy-sh/core/src/capability/capability";
|
|
6
6
|
import { pithyCommandOf } from "@pithy-sh/core/src/cli/command";
|
|
7
|
+
import { composedUsageMeter, type MeterCharge, type UsageMeter } from "@pithy-sh/core/src/entitlement/meter";
|
|
7
8
|
import { InternalError } from "@pithy-sh/core/src/error/pithyError";
|
|
8
9
|
import type { LocaleCatalogs } from "@pithy-sh/core/src/i18n/catalog";
|
|
9
10
|
import { z } from "zod";
|
|
@@ -194,6 +195,12 @@ export function email(config: EmailConfigInput): EmailCapability {
|
|
|
194
195
|
* capability's own config, and this is the only thing that knows it.
|
|
195
196
|
*/
|
|
196
197
|
let supportedLocales: readonly string[] = [];
|
|
198
|
+
/**
|
|
199
|
+
* The usage meter a composed provider exposes — `@pithy-sh/payments`, when it is composed — found in
|
|
200
|
+
* `compose` by core's structural predicate. Undefined with no provider, which is the email capability
|
|
201
|
+
* exactly as it always was: nothing is counted and nothing is refused.
|
|
202
|
+
*/
|
|
203
|
+
let meter: UsageMeter | undefined;
|
|
197
204
|
const capability = defineCapability({
|
|
198
205
|
name: "email",
|
|
199
206
|
// The package this capability ships in and the version it ships at, both stamped by
|
|
@@ -235,6 +242,7 @@ export function email(config: EmailConfigInput): EmailCapability {
|
|
|
235
242
|
* project sends what it always sent.
|
|
236
243
|
*/
|
|
237
244
|
compose: ({ capabilities }) => {
|
|
245
|
+
meter = composedUsageMeter(capabilities);
|
|
238
246
|
for (const composed of capabilities) {
|
|
239
247
|
if (composed.name !== "i18n") continue;
|
|
240
248
|
const candidate = (composed as { layersFor?: unknown }).layersFor;
|
|
@@ -352,6 +360,12 @@ export function email(config: EmailConfigInput): EmailCapability {
|
|
|
352
360
|
}
|
|
353
361
|
return emailSuppressionDatabase(binding);
|
|
354
362
|
};
|
|
363
|
+
/** The meter charge for one env, or undefined when no provider is composed. */
|
|
364
|
+
const chargeFor = (env: EmailEnqueueEnv): ((charge: MeterCharge) => Promise<void>) | undefined => {
|
|
365
|
+
const composed = meter;
|
|
366
|
+
if (composed === undefined) return undefined;
|
|
367
|
+
return (charge) => composed.charge(env as unknown as Record<string, unknown>, charge);
|
|
368
|
+
};
|
|
355
369
|
const enqueue = (env: EmailEnqueueEnv, input: EnqueueInput): Promise<EnqueueResult> =>
|
|
356
370
|
enqueueEmail(
|
|
357
371
|
{
|
|
@@ -366,6 +380,8 @@ export function email(config: EmailConfigInput): EmailCapability {
|
|
|
366
380
|
// Read straight off the env the consumer forwarded. This one line is what makes suppression
|
|
367
381
|
// automatic: a consumer names nothing, and a hard-bounced address is never queued for a send.
|
|
368
382
|
suppressionDb: env.EMAIL_SUPPRESSIONS ? emailSuppressionDatabase(env.EMAIL_SUPPRESSIONS) : undefined,
|
|
383
|
+
// Read through the closure for the reason `layersFor` is: `compose` fills it after this is defined.
|
|
384
|
+
meter: chargeFor(env),
|
|
369
385
|
now: new Date(),
|
|
370
386
|
newId: () => crypto.randomUUID(),
|
|
371
387
|
},
|
package/src/crypto/signingKey.ts
CHANGED
|
@@ -52,6 +52,12 @@ export const emailSigningRegistry = defineSecretRegistry({
|
|
|
52
52
|
// the entry when it is absent and never replaces one — and `local` to replace.
|
|
53
53
|
origin: { kind: "minted", recipe: { kind: "random", bytes: 32, encoding: "base64url" } },
|
|
54
54
|
rotation: { kind: "local" },
|
|
55
|
+
// **A rotation keeps the previous key for as long as a link lives (#602).** A link is minted into mail
|
|
56
|
+
// nobody can recall and stays valid for `LINK_TTL_DAYS` — 90 by default — so a key retired sooner breaks
|
|
57
|
+
// an unsubscribe that was valid when it was sent. A host that raises `LINK_TTL_DAYS` wants a longer
|
|
58
|
+
// overlap than this, declared in the project's own registry. Nothing is sealed under this key at rest:
|
|
59
|
+
// it signs, and a signature expires with its link.
|
|
60
|
+
overlap: { days: 90, minimumDays: 90 },
|
|
55
61
|
},
|
|
56
62
|
});
|
|
57
63
|
|
package/src/error/errors.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
import { PithyError } from "@pithy-sh/core/src/error/pithyError";
|
|
5
5
|
import type { MessageParams } from "@pithy-sh/core/src/i18n/catalog";
|
|
6
|
+
import type { UnmarkedString } from "@pithy-sh/core/src/privacy/types";
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* `@pithy-sh/email` throw sugar. The `email/*` codes live in core's closed `KitErrorPayload` union
|
|
@@ -14,11 +15,11 @@ import type { MessageParams } from "@pithy-sh/core/src/i18n/catalog";
|
|
|
14
15
|
/** Variable parts each subclass accepts; `code`/`status` are fixed by the subclass. */
|
|
15
16
|
interface EmailErrorArgs {
|
|
16
17
|
/** Override the public, safe-to-expose message. */
|
|
17
|
-
message?:
|
|
18
|
+
message?: UnmarkedString;
|
|
18
19
|
/** A remediation hint (CLI action line). */
|
|
19
|
-
action?:
|
|
20
|
+
action?: UnmarkedString;
|
|
20
21
|
/** Internal context for logs + audit. Never serialized to clients. */
|
|
21
|
-
detail?:
|
|
22
|
+
detail?: UnmarkedString;
|
|
22
23
|
/**
|
|
23
24
|
* Values a translating client interpolates into its own wording for this code. Client-facing, so —
|
|
24
25
|
* unlike `action` and `detail` — these cross the boundary with `message`.
|
|
@@ -197,6 +197,11 @@ export interface EmailDeprovisioner {
|
|
|
197
197
|
* before it deletes the list they share (#591).
|
|
198
198
|
*/
|
|
199
199
|
hasWorker(env: ManagedEnvironment): Promise<boolean>;
|
|
200
|
+
/**
|
|
201
|
+
* Delete the env's send and schedule Workflows, by `emailWorkflowNames`. Idempotent (a Workflow already gone is
|
|
202
|
+
* a no-op). Deleting the worker leaves them registered, so they go first (#789).
|
|
203
|
+
*/
|
|
204
|
+
deleteWorkflows(env: ManagedEnvironment): Promise<void>;
|
|
200
205
|
/** Delete the env's email worker. Idempotent (a missing worker is a no-op). */
|
|
201
206
|
deleteWorker(env: ManagedEnvironment): Promise<void>;
|
|
202
207
|
/**
|
|
@@ -230,7 +235,10 @@ export interface EmailDeprovisionOptions {
|
|
|
230
235
|
* 2. With `deleteSuppression` only: refuse while any other declared environment still runs an email worker
|
|
231
236
|
* (`assertSharedLeavesLast`). The list is one per project, and a staging teardown must not take production's.
|
|
232
237
|
* 3. Count the list, and refuse unless the operator counted the same (`assertRetainedAgreed`, #588's guard).
|
|
233
|
-
* 4. Delete the environment's worker (it binds the list), then — only when asked — the list.
|
|
238
|
+
* 4. Delete the environment's Workflows (#789), then its worker (it binds the list), then — only when asked — the list.
|
|
239
|
+
*
|
|
240
|
+
* It never deletes the environment's link-signing key. `pithy secrets provision` creates that entry and binds it
|
|
241
|
+
* into the app Worker, which is still deployed, and every link already sent is signed with it (#596).
|
|
234
242
|
*
|
|
235
243
|
* Idempotent end to end: a missing worker or database is a no-op.
|
|
236
244
|
*/
|
|
@@ -251,6 +259,7 @@ export async function deprovisionEmail(
|
|
|
251
259
|
"anything was deleted",
|
|
252
260
|
);
|
|
253
261
|
}
|
|
262
|
+
await deprovisioner.deleteWorkflows(env);
|
|
254
263
|
await deprovisioner.deleteWorker(env);
|
|
255
264
|
if (options.deleteSuppression) await deprovisioner.deleteSuppressionDatabase();
|
|
256
265
|
return { env };
|
|
@@ -78,6 +78,18 @@ export const emailWorkflowRegistry: WorkflowRegistry = Object.fromEntries(
|
|
|
78
78
|
}),
|
|
79
79
|
);
|
|
80
80
|
|
|
81
|
+
/**
|
|
82
|
+
* The Workflows one environment's email worker is deployed with — `<project>-<env>-email-send` and
|
|
83
|
+
* `<project>-<env>-email-schedule`. Derived by the call {@link resolveEmailConfig} deploys with, so teardown
|
|
84
|
+
* deletes the names provision created and no others (#789). Deleting a Worker does not delete the Workflows
|
|
85
|
+
* registered against it.
|
|
86
|
+
*/
|
|
87
|
+
export function emailWorkflowNames(project: string, env: ManagedEnvironment): string[] {
|
|
88
|
+
return hostWorkflowsFor(emailWorkflowRegistry, { project, capability: EMAIL_CAPABILITY, env }).workflows.map(
|
|
89
|
+
(workflow) => workflow.name,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
81
93
|
/**
|
|
82
94
|
* The most one Cloudflare Worker variable holds. **Per variable, and that is the whole design.**
|
|
83
95
|
*
|
package/src/send/enqueue.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
|
|
4
4
|
import { normalizeAddress } from "@pithy-sh/core/src/address/address";
|
|
5
|
+
import type { MeterCharge, MeterSubject } from "@pithy-sh/core/src/entitlement/meter";
|
|
5
6
|
import { isLocale } from "@pithy-sh/core/src/i18n/locale";
|
|
6
7
|
import type { EmailJob } from "../data/emailJob";
|
|
7
8
|
import { EmailJob as EmailJobSchema } from "../data/emailJob";
|
|
@@ -121,6 +122,16 @@ export interface EnqueueInput {
|
|
|
121
122
|
inReplyTo?: string;
|
|
122
123
|
/** The `References` chain, angle-bracketed and space-separated. Built by the caller from the parent. */
|
|
123
124
|
references?: string;
|
|
125
|
+
/**
|
|
126
|
+
* Who this message is sent on behalf of, for the `emails` meter — the holder pair `@pithy-sh/payments`
|
|
127
|
+
* bills, `{ subjectType: "organization", subjectId }`.
|
|
128
|
+
*
|
|
129
|
+
* **Omit it and nothing is counted**, which is right for every message the kit sends on its own account: a
|
|
130
|
+
* magic link to somebody who has not signed up is not a tenant's usage. Name it on mail a customer's plan
|
|
131
|
+
* pays for — a campaign, a notification their workspace triggers — and, where the meter is enforced, an
|
|
132
|
+
* enqueue past the period's quota throws `payments/quota_exceeded` and writes no job.
|
|
133
|
+
*/
|
|
134
|
+
subject?: MeterSubject;
|
|
124
135
|
}
|
|
125
136
|
|
|
126
137
|
/** Dependencies enqueue needs: the database, the from identity, optional send-Workflow binding, time, ids. */
|
|
@@ -149,6 +160,11 @@ export interface EnqueueDeps {
|
|
|
149
160
|
* goes could not be consulted yet, which is strictly worse than queueing it.
|
|
150
161
|
*/
|
|
151
162
|
suppressionDb?: EmailSuppressionDatabase;
|
|
163
|
+
/**
|
|
164
|
+
* Charge one email against the `emails` meter, or refuse — the composed provider's seam, closed over this
|
|
165
|
+
* request's env. Absent when no provider is composed, and then nothing is counted.
|
|
166
|
+
*/
|
|
167
|
+
meter?: (charge: MeterCharge) => Promise<void>;
|
|
152
168
|
now: Date;
|
|
153
169
|
/** Generate a job id (a UUID in production). */
|
|
154
170
|
newId: () => string;
|
|
@@ -309,6 +325,18 @@ export async function enqueueEmail(deps: EnqueueDeps, input: EnqueueInput): Prom
|
|
|
309
325
|
*/
|
|
310
326
|
const batchId = sender ? (deps.newBatchId ?? mintBatchId)() : null;
|
|
311
327
|
|
|
328
|
+
/**
|
|
329
|
+
* Charge the `emails` meter **before the row exists**, so a refusal writes no job — and only for a message
|
|
330
|
+
* that is going to be sent. A withheld recipient costs the customer nothing, and a message the kit sends
|
|
331
|
+
* on nobody's account names no subject and is never counted.
|
|
332
|
+
*
|
|
333
|
+
* Before the insert rather than after it, and the cost is the right way round: an insert that fails after
|
|
334
|
+
* a charge over-counts by one, and over-counting briefly is recoverable where under-counting is not.
|
|
335
|
+
*/
|
|
336
|
+
if (!blocked && input.subject !== undefined && deps.meter !== undefined) {
|
|
337
|
+
await deps.meter({ subject: input.subject, meter: "emails", amount: 1, now: deps.now });
|
|
338
|
+
}
|
|
339
|
+
|
|
312
340
|
const marketing = template.category === "marketing";
|
|
313
341
|
const job: EmailJob = {
|
|
314
342
|
id: deps.newId(),
|
|
@@ -100,6 +100,42 @@ export const EMAIL_ES: MessageCatalog = {
|
|
|
100
100
|
"email/password_changed.text_body":
|
|
101
101
|
"Las credenciales de tu cuenta cambiaron el {when}. Si no fuiste tú, contacta con soporte:",
|
|
102
102
|
|
|
103
|
+
// --- The account lifecycle ---
|
|
104
|
+
"email/account_suspended.subject": "Tu cuenta de {app} está suspendida",
|
|
105
|
+
"email/account_suspended.heading": "Tu cuenta está suspendida",
|
|
106
|
+
"email/account_suspended.body":
|
|
107
|
+
"tu cuenta de {app} está suspendida. No puedes iniciar sesión hasta que se levante la suspensión.",
|
|
108
|
+
"email/account_suspended.text_body":
|
|
109
|
+
"Tu cuenta de {app} está suspendida. No puedes iniciar sesión hasta que se levante la suspensión.",
|
|
110
|
+
"email/account_suspended.reason": "El motivo indicado: {reason}",
|
|
111
|
+
"email/account_suspended.until": "Termina el {until}.",
|
|
112
|
+
"email/account_suspended.contact": "Si crees que es un error, responde a este correo.",
|
|
113
|
+
"email/account_suspended.text_contact": "Si crees que es un error, responde a este correo.",
|
|
114
|
+
|
|
115
|
+
"email/account_reinstated.subject": "Tu cuenta de {app} vuelve a estar activa",
|
|
116
|
+
"email/account_reinstated.heading": "Tu cuenta vuelve a estar activa",
|
|
117
|
+
"email/account_reinstated.body":
|
|
118
|
+
"se ha levantado la suspensión de tu cuenta de {app}. Ya puedes volver a iniciar sesión.",
|
|
119
|
+
"email/account_reinstated.text_body":
|
|
120
|
+
"Se ha levantado la suspensión de tu cuenta de {app}. Ya puedes volver a iniciar sesión.",
|
|
121
|
+
|
|
122
|
+
"email/deletion_scheduled.subject": "Tu cuenta de {app} se ha eliminado",
|
|
123
|
+
"email/deletion_scheduled.heading": "Tu cuenta se ha eliminado",
|
|
124
|
+
"email/deletion_scheduled.body": "tu cuenta de {app} se ha eliminado y ya no puedes iniciar sesión.",
|
|
125
|
+
"email/deletion_scheduled.text_body": "Tu cuenta de {app} se ha eliminado y ya no puedes iniciar sesión.",
|
|
126
|
+
"email/deletion_scheduled.purge": "Tus datos se borrarán el {purgeOn}. Hasta entonces, la cuenta se puede recuperar.",
|
|
127
|
+
"email/deletion_scheduled.cta": "Recuperar mi cuenta",
|
|
128
|
+
"email/deletion_scheduled.text_restore": "Recupérala:",
|
|
129
|
+
"email/deletion_scheduled.contact": "Si no lo pediste tú, responde a este correo antes del {purgeOn}.",
|
|
130
|
+
"email/deletion_scheduled.text_contact": "Si no lo pediste tú, responde a este correo antes del {purgeOn}.",
|
|
131
|
+
|
|
132
|
+
"email/deletion_complete.subject": "Tus datos de {app} se han borrado",
|
|
133
|
+
"email/deletion_complete.heading": "Tus datos se han borrado",
|
|
134
|
+
"email/deletion_complete.body": "tu cuenta de {app} y sus datos se borraron el {purgedOn}. No se puede deshacer.",
|
|
135
|
+
"email/deletion_complete.text_body":
|
|
136
|
+
"Tu cuenta de {app} y sus datos se borraron el {purgedOn}. No se puede deshacer.",
|
|
137
|
+
"email/deletion_complete.last": "Es el último correo que recibirá esta cuenta.",
|
|
138
|
+
|
|
103
139
|
// --- leadCapture ---
|
|
104
140
|
"email/lead_capture.subject": "Tu descarga: {asset}",
|
|
105
141
|
"email/lead_capture.heading": "Tu descarga está lista",
|
|
@@ -44,14 +44,14 @@ import { EMAIL_ES } from "./messages.es";
|
|
|
44
44
|
*
|
|
45
45
|
* ## Only the kit's own copy
|
|
46
46
|
*
|
|
47
|
-
*
|
|
47
|
+
* Eleven templates are here. The five whose words arrive as payload — `testerNudge`, `supportReply`,
|
|
48
48
|
* `operationalNotice`, `newsletter`, `marketingCampaign` — are the adopter's copy, chosen by a human
|
|
49
49
|
* for one message, and a catalog cannot translate a sentence it has never seen. Their **shell** still
|
|
50
50
|
* follows the job's locale (the severity word, the footer, the document's `lang` and `dir`), so a
|
|
51
51
|
* notice at locale `es` reads its severity in Spanish and its summary in whatever the caller wrote.
|
|
52
52
|
*/
|
|
53
53
|
|
|
54
|
-
/** The kit's English for every string the
|
|
54
|
+
/** The kit's English for every string the eleven kit-authored templates and the shared shell render. */
|
|
55
55
|
const EMAIL_EN: MessageCatalog = {
|
|
56
56
|
// --- The shared shell ---
|
|
57
57
|
// The greeting is two keys rather than one with an optional placeholder, because a language that
|
|
@@ -123,6 +123,40 @@ const EMAIL_EN: MessageCatalog = {
|
|
|
123
123
|
"email/password_changed.text_body":
|
|
124
124
|
"Your account credentials were changed on {when}. If this wasn't you, contact support:",
|
|
125
125
|
|
|
126
|
+
// --- The account lifecycle (#620) ---
|
|
127
|
+
// Each says what happened, what it means, and who to answer. The answer is a reply: the mail comes from the
|
|
128
|
+
// adopter's own address, so a reply reaches the people who can act on it.
|
|
129
|
+
"email/account_suspended.subject": "Your {app} account is suspended",
|
|
130
|
+
"email/account_suspended.heading": "Your account is suspended",
|
|
131
|
+
"email/account_suspended.body": "your {app} account is suspended. You cannot sign in until it is lifted.",
|
|
132
|
+
"email/account_suspended.text_body": "Your {app} account is suspended. You cannot sign in until it is lifted.",
|
|
133
|
+
"email/account_suspended.reason": "The reason given: {reason}",
|
|
134
|
+
"email/account_suspended.until": "It ends on {until}.",
|
|
135
|
+
"email/account_suspended.contact": "If you think this is a mistake, reply to this email.",
|
|
136
|
+
"email/account_suspended.text_contact": "If you think this is a mistake, reply to this email.",
|
|
137
|
+
|
|
138
|
+
"email/account_reinstated.subject": "Your {app} account is back",
|
|
139
|
+
"email/account_reinstated.heading": "Your account is back",
|
|
140
|
+
"email/account_reinstated.body": "the suspension on your {app} account is lifted. You can sign in again.",
|
|
141
|
+
"email/account_reinstated.text_body": "The suspension on your {app} account is lifted. You can sign in again.",
|
|
142
|
+
|
|
143
|
+
"email/deletion_scheduled.subject": "Your {app} account is deleted",
|
|
144
|
+
"email/deletion_scheduled.heading": "Your account is deleted",
|
|
145
|
+
"email/deletion_scheduled.body": "your {app} account is deleted, and you can no longer sign in.",
|
|
146
|
+
"email/deletion_scheduled.text_body": "Your {app} account is deleted, and you can no longer sign in.",
|
|
147
|
+
"email/deletion_scheduled.purge": "Your data will be erased on {purgeOn}. Until then, the account can be restored.",
|
|
148
|
+
"email/deletion_scheduled.cta": "Restore my account",
|
|
149
|
+
"email/deletion_scheduled.text_restore": "Restore it:",
|
|
150
|
+
"email/deletion_scheduled.contact": "If you did not ask for this, reply to this email before {purgeOn}.",
|
|
151
|
+
"email/deletion_scheduled.text_contact": "If you did not ask for this, reply to this email before {purgeOn}.",
|
|
152
|
+
|
|
153
|
+
"email/deletion_complete.subject": "Your {app} data is erased",
|
|
154
|
+
"email/deletion_complete.heading": "Your data is erased",
|
|
155
|
+
"email/deletion_complete.body": "your {app} account and its data were erased on {purgedOn}. This cannot be undone.",
|
|
156
|
+
"email/deletion_complete.text_body":
|
|
157
|
+
"Your {app} account and its data were erased on {purgedOn}. This cannot be undone.",
|
|
158
|
+
"email/deletion_complete.last": "This is the last email this account will receive.",
|
|
159
|
+
|
|
126
160
|
// --- leadCapture ---
|
|
127
161
|
"email/lead_capture.subject": "Your download: {asset}",
|
|
128
162
|
"email/lead_capture.heading": "Your download is ready",
|