@pithy-sh/email 0.3.7 → 0.3.9
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/provision/resolveEmailConfig.d.ts +9 -0
- package/dist/provision/resolveEmailConfig.d.ts.map +1 -1
- package/dist/provision/resolveEmailConfig.js +11 -6
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +1 -1
- package/dist/workflows/sendDeps.d.ts +62 -0
- package/dist/workflows/sendDeps.d.ts.map +1 -0
- package/dist/workflows/sendDeps.js +47 -0
- package/dist/workflows/worker.d.ts +2 -66
- package/dist/workflows/worker.d.ts.map +1 -1
- package/dist/workflows/worker.js +24 -36
- package/package.json +3 -3
- package/src/provision/resolveEmailConfig.ts +33 -6
- package/src/version.generated.ts +1 -1
- package/src/workflows/sendDeps.ts +94 -0
- package/src/workflows/worker.ts +5 -85
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
import type { LocaleCatalogs } from "@pithy-sh/core/src/i18n/catalog";
|
|
4
|
+
import type { FeatureIdentity } from "@pithy-sh/core/src/naming/feature";
|
|
4
5
|
import type { HostD1Binding, HostSecretsStoreBinding, HostSendEmailBinding, HostWorkflowBinding, WorkflowHostTemplate } from "@pithy-sh/core/src/workflow/host";
|
|
5
6
|
import type { WorkflowRegistry } from "@pithy-sh/core/src/workflow/spec";
|
|
6
7
|
import type { ManagedEnvironment } from "@pithy-sh/secrets/src/scope";
|
|
@@ -74,6 +75,14 @@ export interface EmailConfigParams {
|
|
|
74
75
|
* governs.
|
|
75
76
|
*/
|
|
76
77
|
env: ManagedEnvironment | "dev";
|
|
78
|
+
/**
|
|
79
|
+
* **The feature this host serves, when it serves one (#643).** A feature gets its own email host, as a
|
|
80
|
+
* declared environment does, and everything the host is called or reads takes the feature's names: the
|
|
81
|
+
* Worker and its Workflows (`<project>-f<issue>-<slug>-email[-<job>]`), the feature's own master key and
|
|
82
|
+
* link-signing key entries, and the feature's own suppression database. `env` is then `feature`, which is
|
|
83
|
+
* what the host's `ENVIRONMENT` var says.
|
|
84
|
+
*/
|
|
85
|
+
feature?: FeatureIdentity;
|
|
77
86
|
/** The app database id for this environment — where jobs/events live. */
|
|
78
87
|
appDatabaseId: string;
|
|
79
88
|
/** The shared suppression database id (same in every environment). */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolveEmailConfig.d.ts","sourceRoot":"","sources":["../../src/provision/resolveEmailConfig.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"resolveEmailConfig.d.ts","sourceRoot":"","sources":["../../src/provision/resolveEmailConfig.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACtE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AAEzE,OAAO,KAAK,EACV,aAAa,EACb,uBAAuB,EACvB,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACrB,MAAM,kCAAkC,CAAC;AAG1C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AAGzE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAC;AAGtE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAErD,OAAO,EAAE,KAAK,eAAe,EAAuB,MAAM,eAAe,CAAC;AAG1E;;;;;;GAMG;AACH,MAAM,WAAW,2BAA4B,SAAQ,oBAAoB;IACvE,kBAAkB,EAAE,MAAM,CAAC;IAC3B,mBAAmB,EAAE,MAAM,EAAE,CAAC;IAC9B,WAAW,EAAE,OAAO,CAAC;IACrB,YAAY,EAAE,aAAa,EAAE,CAAC;IAC9B,UAAU,EAAE,oBAAoB,EAAE,CAAC;IACnC,qBAAqB,EAAE,uBAAuB,EAAE,CAAC;IACjD,SAAS,EAAE,mBAAmB,EAAE,CAAC;IACjC,QAAQ,EAAE;QAAE,KAAK,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;IAC9B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC9B;AA4BD,2GAA2G;AAC3G,eAAO,MAAM,qBAAqB,EAAE,gBAKnC,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,oBAAoB,QAAW,CAAC;AAE7C;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,cAAc,GAAG,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAiB9F;AAED,4FAA4F;AAC5F,MAAM,WAAW,iBAAiB;IAChC;;;;;OAKG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;;;OAIG;IACH,GAAG,EAAE,kBAAkB,GAAG,KAAK,CAAC;IAChC;;;;;;OAMG;IACH,OAAO,CAAC,EAAE,eAAe,CAAC;IAC1B,yEAAyE;IACzE,aAAa,EAAE,MAAM,CAAC;IACtB,sEAAsE;IACtE,qBAAqB,EAAE,MAAM,CAAC;IAC9B,+GAA+G;IAC/G,iBAAiB,EAAE,MAAM,CAAC;IAC1B,+FAA+F;IAC/F,OAAO,EAAE,MAAM,CAAC;IAChB,mGAAmG;IACnG,OAAO,EAAE,MAAM,CAAC;IAChB,iFAAiF;IACjF,KAAK,EAAE,UAAU,CAAC;IAClB;;;;;;;OAOG;IACH,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B;;;OAGG;IACH,WAAW,CAAC,EAAE,eAAe,CAAC;CAK/B;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,2BAA2B,EACrC,MAAM,EAAE,iBAAiB,GACxB,2BAA2B,CAwD7B"}
|
|
@@ -3,12 +3,13 @@
|
|
|
3
3
|
import { EMAIL_LINK_SIGNING_KEY, EMAIL_LINK_SIGNING_KEY_BINDING, emailSigningRegistry } from "../crypto/signingKey.js";
|
|
4
4
|
import { emailCatalogVarName } from "../templates/messages.js";
|
|
5
5
|
import { emailRemoteBindings } from "./devDelivery.js";
|
|
6
|
-
import { EMAIL_CAPABILITY, suppressionDatabaseName } from "./provisionEmail.js";
|
|
6
|
+
import { EMAIL_CAPABILITY, EMAIL_SUPPRESSIONS_BINDING, suppressionDatabaseName } from "./provisionEmail.js";
|
|
7
7
|
import { EmailScheduleParams, EmailSendParams } from "../workflows/params.js";
|
|
8
8
|
import { ValidationError } from "@pithy-sh/core/src/error/pithyError";
|
|
9
|
-
import { environmentScope } from "@pithy-sh/core/src/naming/provisionScope";
|
|
9
|
+
import { environmentScope, featureScope } from "@pithy-sh/core/src/naming/provisionScope";
|
|
10
10
|
import { hostWorkflowsFor, resolveWorkflowHost } from "@pithy-sh/core/src/workflow/host";
|
|
11
11
|
import { workflowKey } from "@pithy-sh/core/src/workflow/naming";
|
|
12
|
+
import { MASTER_KEY_BINDING } from "@pithy-sh/secrets/src/env/masterKeyBinding";
|
|
12
13
|
import { masterKeySecretName } from "@pithy-sh/secrets/src/provision/provisionSecrets";
|
|
13
14
|
/** The registry the host resolver derives its `workflows` array from, and the host's app dispatches on. */
|
|
14
15
|
const emailWorkflowRegistry = Object.fromEntries(Object.entries({
|
|
@@ -99,19 +100,22 @@ function emailMessagesVars(messages) {
|
|
|
99
100
|
*/
|
|
100
101
|
function resolveEmailConfig(template, params) {
|
|
101
102
|
const { project, env, appDatabaseId, suppressionDatabaseId, secretsDatabaseId, storeId, baseUrl, theme } = params;
|
|
103
|
+
const feature = params.feature;
|
|
104
|
+
const scope = feature ? featureScope(feature) : environmentScope(project, env);
|
|
102
105
|
return resolveWorkflowHost(template, {
|
|
103
106
|
project,
|
|
104
107
|
capability: EMAIL_CAPABILITY,
|
|
105
108
|
env,
|
|
109
|
+
...feature ? { feature } : {},
|
|
106
110
|
databaseIds: {
|
|
107
111
|
DB: appDatabaseId,
|
|
108
112
|
EMAIL_SUPPRESSIONS: suppressionDatabaseId,
|
|
109
113
|
SECRETS: secretsDatabaseId
|
|
110
114
|
},
|
|
111
|
-
databaseNames: { EMAIL_SUPPRESSIONS: suppressionDatabaseName(project) },
|
|
115
|
+
databaseNames: { EMAIL_SUPPRESSIONS: feature ? scope.resource(EMAIL_SUPPRESSIONS_BINDING, "d1", { scope: "global" }) : suppressionDatabaseName(project) },
|
|
112
116
|
secretsStoreId: storeId,
|
|
113
|
-
masterKeySecretName: masterKeySecretName(project, env),
|
|
114
|
-
secretNames: { [EMAIL_LINK_SIGNING_KEY_BINDING]:
|
|
117
|
+
masterKeySecretName: feature ? scope.secretEntry(MASTER_KEY_BINDING, "environment") : masterKeySecretName(project, env),
|
|
118
|
+
secretNames: { [EMAIL_LINK_SIGNING_KEY_BINDING]: scope.secretEntry(EMAIL_LINK_SIGNING_KEY, emailSigningRegistry[EMAIL_LINK_SIGNING_KEY].scope) },
|
|
115
119
|
vars: {
|
|
116
120
|
EMAIL_THEME: JSON.stringify(theme),
|
|
117
121
|
BASE_URL: baseUrl,
|
|
@@ -121,7 +125,8 @@ function resolveEmailConfig(template, params) {
|
|
|
121
125
|
workflows: hostWorkflowsFor(emailWorkflowRegistry, {
|
|
122
126
|
project,
|
|
123
127
|
capability: EMAIL_CAPABILITY,
|
|
124
|
-
env
|
|
128
|
+
env,
|
|
129
|
+
...feature ? { feature } : {}
|
|
125
130
|
}).workflows
|
|
126
131
|
});
|
|
127
132
|
}
|
|
@@ -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.3.
|
|
6
|
+
export declare const PACKAGE_VERSION = "0.3.9";
|
|
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.3.
|
|
7
|
+
const PACKAGE_VERSION = "0.3.9";
|
|
8
8
|
//#endregion
|
|
9
9
|
export { PACKAGE_NAME, PACKAGE_VERSION };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import type { D1Database } from "@cloudflare/workers-types";
|
|
4
|
+
import type { SecretBinding, SecretsStoreEnv } from "@pithy-sh/secrets/src/env/bindings";
|
|
5
|
+
import { EMAIL_LINK_SIGNING_KEY_BINDING } from "../crypto/signingKey";
|
|
6
|
+
import type { SendWorkflowBinding } from "../send/enqueue";
|
|
7
|
+
import type { EmailSender } from "../send/sender";
|
|
8
|
+
import { type EmailHostEnv } from "./hostEnv";
|
|
9
|
+
import type { SendWorkflowInstances } from "./instances";
|
|
10
|
+
import type { SendBatchDeps } from "./sendBatch";
|
|
11
|
+
/**
|
|
12
|
+
* The email worker's env as the runtime hands it over — bindings as objects, every var as a string.
|
|
13
|
+
*
|
|
14
|
+
* The *shape the host runs on* is {@link EmailHostEnv}, which is this parsed: numbers as numbers, the
|
|
15
|
+
* theme as a validated `EmailTheme`, `SCHEDULER_ENABLED` as a boolean. This type stays because it is
|
|
16
|
+
* what a `WorkflowEntrypoint` is generic over and what the platform actually binds.
|
|
17
|
+
*/
|
|
18
|
+
export interface EmailWorkerEnv extends SecretsStoreEnv {
|
|
19
|
+
/** The app database the per-environment jobs/events tables live in. */
|
|
20
|
+
DB: D1Database;
|
|
21
|
+
/** The link-signing key: this environment's Secrets Store entry, or its `.dev.vars` string locally (#596). */
|
|
22
|
+
[EMAIL_LINK_SIGNING_KEY_BINDING]: SecretBinding | string;
|
|
23
|
+
/** The shared, durable suppression database. */
|
|
24
|
+
EMAIL_SUPPRESSIONS: D1Database;
|
|
25
|
+
/** The Cloudflare Email Service send binding. */
|
|
26
|
+
EMAIL: EmailSender;
|
|
27
|
+
/** The send Workflow (self) — the scheduler creates batches against it, and asks after them. */
|
|
28
|
+
EMAIL_SENDER: SendWorkflowBinding & SendWorkflowInstances;
|
|
29
|
+
/** The scheduler Workflow (self) — fired by the cron. */
|
|
30
|
+
EMAIL_SCHEDULER: {
|
|
31
|
+
create(): Promise<unknown>;
|
|
32
|
+
};
|
|
33
|
+
/** The resolved brand theme as a JSON string (the full `EmailTheme`), set at provision from the app config. */
|
|
34
|
+
EMAIL_THEME?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The project's catalogs arrive as one variable per locale — `EMAIL_MESSAGES_ES` and friends — read
|
|
37
|
+
* through `catalogsFromEnv`. Not declared here, because the names are the project's locales.
|
|
38
|
+
*/
|
|
39
|
+
[catalogVar: `EMAIL_MESSAGES_${string}`]: unknown;
|
|
40
|
+
BASE_URL: string;
|
|
41
|
+
LINK_TTL_DAYS?: string;
|
|
42
|
+
MAX_ATTEMPTS?: string;
|
|
43
|
+
SCHEDULER_ENABLED?: string;
|
|
44
|
+
SCHEDULER_BATCH_SIZE?: string;
|
|
45
|
+
SCHEDULER_MAX_JOBS?: string;
|
|
46
|
+
SCHEDULER_GRACE_MS?: string;
|
|
47
|
+
SCHEDULER_STUCK_MS?: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The env, parsed — or one legible block naming every unusable setting and what fills it, then a
|
|
51
|
+
* refusal. Called at the top of every entry, and the block is written once per env object.
|
|
52
|
+
*/
|
|
53
|
+
export declare function hostConfig(env: EmailWorkerEnv): EmailHostEnv;
|
|
54
|
+
/**
|
|
55
|
+
* Assemble the send dependencies, resolving the current signing key from the secrets store.
|
|
56
|
+
*
|
|
57
|
+
* Here rather than in `worker.ts`, which imports `cloudflare:workers` and so loads in workerd alone: this is the
|
|
58
|
+
* host's whole reading of its env for a send, and a test that drives a send the way the deployed host would —
|
|
59
|
+
* the feature sign-in end to end, #643 — has to read it through this function, not a copy of it.
|
|
60
|
+
*/
|
|
61
|
+
export declare function buildSendDeps(env: EmailWorkerEnv): Promise<SendBatchDeps>;
|
|
62
|
+
//# sourceMappingURL=sendDeps.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sendDeps.d.ts","sourceRoot":"","sources":["../../src/workflows/sendDeps.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAE5D,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AAEzF,OAAO,EAAE,8BAA8B,EAA4C,MAAM,sBAAsB,CAAC;AAEhH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAC3D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAElD,OAAO,EAAE,KAAK,YAAY,EAAgB,MAAM,WAAW,CAAC;AAC5D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEjD;;;;;;GAMG;AACH,MAAM,WAAW,cAAe,SAAQ,eAAe;IACrD,uEAAuE;IACvE,EAAE,EAAE,UAAU,CAAC;IACf,8GAA8G;IAC9G,CAAC,8BAA8B,CAAC,EAAE,aAAa,GAAG,MAAM,CAAC;IACzD,gDAAgD;IAChD,kBAAkB,EAAE,UAAU,CAAC;IAC/B,iDAAiD;IACjD,KAAK,EAAE,WAAW,CAAC;IACnB,gGAAgG;IAChG,YAAY,EAAE,mBAAmB,GAAG,qBAAqB,CAAC;IAC1D,yDAAyD;IACzD,eAAe,EAAE;QAAE,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,CAAA;KAAE,CAAC;IAChD,+GAA+G;IAC/G,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,CAAC,UAAU,EAAE,kBAAkB,MAAM,EAAE,GAAG,OAAO,CAAC;IAClD,QAAQ,EAAE,MAAM,CAAC;IAEjB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC7B;AAOD;;;GAGG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,cAAc,GAAG,YAAY,CAE5D;AAED;;;;;;GAMG;AACH,wBAAsB,aAAa,CAAC,GAAG,EAAE,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC,CAmB/E"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { emailDatabase, emailSuppressionDatabase } from "../data/tables.js";
|
|
4
|
+
import { emailSigningRegistry, resolveSigningKeys } from "../crypto/signingKey.js";
|
|
5
|
+
import { catalogLayers, catalogsFromEnv } from "../templates/messages.js";
|
|
6
|
+
import { emailHostEnv } from "./hostEnv.js";
|
|
7
|
+
import { configureSharedSecrets } from "@pithy-sh/secrets/src/sharedSecretsStore";
|
|
8
|
+
import { requireHostEnv } from "@pithy-sh/core/src/workflow/hostEnv";
|
|
9
|
+
//#region src/workflows/sendDeps.ts
|
|
10
|
+
configureSharedSecrets({ registry: emailSigningRegistry });
|
|
11
|
+
/**
|
|
12
|
+
* The env, parsed — or one legible block naming every unusable setting and what fills it, then a
|
|
13
|
+
* refusal. Called at the top of every entry, and the block is written once per env object.
|
|
14
|
+
*/
|
|
15
|
+
function hostConfig(env) {
|
|
16
|
+
return requireHostEnv(emailHostEnv, env);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Assemble the send dependencies, resolving the current signing key from the secrets store.
|
|
20
|
+
*
|
|
21
|
+
* Here rather than in `worker.ts`, which imports `cloudflare:workers` and so loads in workerd alone: this is the
|
|
22
|
+
* host's whole reading of its env for a send, and a test that drives a send the way the deployed host would —
|
|
23
|
+
* the feature sign-in end to end, #643 — has to read it through this function, not a copy of it.
|
|
24
|
+
*/
|
|
25
|
+
async function buildSendDeps(env) {
|
|
26
|
+
const config = hostConfig(env);
|
|
27
|
+
const keys = await resolveSigningKeys(env);
|
|
28
|
+
const key = keys.versions[keys.currentVersion];
|
|
29
|
+
return {
|
|
30
|
+
db: emailDatabase(env.DB),
|
|
31
|
+
suppressionDb: emailSuppressionDatabase(env.EMAIL_SUPPRESSIONS),
|
|
32
|
+
sender: env.EMAIL,
|
|
33
|
+
theme: config.EMAIL_THEME,
|
|
34
|
+
layersFor: catalogLayers(catalogsFromEnv(env)),
|
|
35
|
+
baseUrl: config.BASE_URL,
|
|
36
|
+
signing: key ? {
|
|
37
|
+
key,
|
|
38
|
+
kid: keys.currentVersion
|
|
39
|
+
} : void 0,
|
|
40
|
+
linkTtlDays: config.LINK_TTL_DAYS,
|
|
41
|
+
maxAttempts: config.MAX_ATTEMPTS,
|
|
42
|
+
environment: config.ENVIRONMENT,
|
|
43
|
+
heartbeatAt: () => /* @__PURE__ */ new Date()
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
export { buildSendDeps, hostConfig };
|
|
@@ -1,73 +1,9 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
import { WorkflowEntrypoint, type WorkflowEvent, type WorkflowStep } from "cloudflare:workers";
|
|
4
|
-
import type {
|
|
5
|
-
import type { SecretBinding, SecretsStoreEnv } from "@pithy-sh/secrets/src/env/bindings";
|
|
6
|
-
import { EMAIL_LINK_SIGNING_KEY_BINDING } from "../crypto/signingKey";
|
|
7
|
-
import type { SendWorkflowBinding } from "../send/enqueue";
|
|
8
|
-
import type { EmailSender } from "../send/sender";
|
|
9
|
-
import type { SendWorkflowInstances } from "./instances";
|
|
4
|
+
import type { ExecutionContext } from "@cloudflare/workers-types";
|
|
10
5
|
import { type BatchSendReport } from "./sendBatch";
|
|
11
|
-
|
|
12
|
-
* The prebuilt email worker. `pithy add email` deploys one per environment (`pithy-email-staging`,
|
|
13
|
-
* `pithy-email-prod`); the user authors no code for it. It hosts:
|
|
14
|
-
*
|
|
15
|
-
* - `EmailSendWorkflow` — sends a batch of jobs durably (dispatch target for immediate sends and
|
|
16
|
-
* the scheduler's fan-out).
|
|
17
|
-
* - `EmailSchedulerWorkflow` — finds due jobs and fans them out into send batches.
|
|
18
|
-
* - `scheduled()` — the every-minute cron that fires the scheduler Workflow when a row is due.
|
|
19
|
-
* - `fetch()` — the loopback dispatch door, served only in `dev` (see {@link createEmailHostApp}).
|
|
20
|
-
*
|
|
21
|
-
* The bodies (`runSendBatch`, `runScheduler`, `runSend`) are tested against Miniflare; these classes are
|
|
22
|
-
* the thin durable shells. This module imports `cloudflare:workers`, so it runs only in the Workers
|
|
23
|
-
* runtime (excluded from the node meta-test).
|
|
24
|
-
*
|
|
25
|
-
* **Every entry validates the env first** (pithy-sh/pithy#410). Fourteen settings arrived here from a
|
|
26
|
-
* provisioning run and none of them was checked: a missing `BASE_URL` became a magic link to
|
|
27
|
-
* `undefined/…`, an unparseable `EMAIL_THEME` threw inside a render step, and a `SCHEDULER_BATCH_SIZE`
|
|
28
|
-
* of `"fifty"` became `NaN` and the scheduler claimed nothing, forever, in silence. Now
|
|
29
|
-
* {@link emailHostEnv} is parsed before anything reads a value, the coercions and defaults live in
|
|
30
|
-
* that one schema rather than at each reader, and a host that cannot work says so in one block and
|
|
31
|
-
* refuses.
|
|
32
|
-
*/
|
|
33
|
-
/**
|
|
34
|
-
* The email worker's env as the runtime hands it over — bindings as objects, every var as a string.
|
|
35
|
-
*
|
|
36
|
-
* The *shape the host runs on* is {@link EmailHostEnv}, which is this parsed: numbers as numbers, the
|
|
37
|
-
* theme as a validated `EmailTheme`, `SCHEDULER_ENABLED` as a boolean. This type stays because it is
|
|
38
|
-
* what a `WorkflowEntrypoint` is generic over and what the platform actually binds.
|
|
39
|
-
*/
|
|
40
|
-
export interface EmailWorkerEnv extends SecretsStoreEnv {
|
|
41
|
-
/** The app database the per-environment jobs/events tables live in. */
|
|
42
|
-
DB: D1Database;
|
|
43
|
-
/** The link-signing key: this environment's Secrets Store entry, or its `.dev.vars` string locally (#596). */
|
|
44
|
-
[EMAIL_LINK_SIGNING_KEY_BINDING]: SecretBinding | string;
|
|
45
|
-
/** The shared, durable suppression database. */
|
|
46
|
-
EMAIL_SUPPRESSIONS: D1Database;
|
|
47
|
-
/** The Cloudflare Email Service send binding. */
|
|
48
|
-
EMAIL: EmailSender;
|
|
49
|
-
/** The send Workflow (self) — the scheduler creates batches against it, and asks after them. */
|
|
50
|
-
EMAIL_SENDER: SendWorkflowBinding & SendWorkflowInstances;
|
|
51
|
-
/** The scheduler Workflow (self) — fired by the cron. */
|
|
52
|
-
EMAIL_SCHEDULER: {
|
|
53
|
-
create(): Promise<unknown>;
|
|
54
|
-
};
|
|
55
|
-
/** The resolved brand theme as a JSON string (the full `EmailTheme`), set at provision from the app config. */
|
|
56
|
-
EMAIL_THEME?: string;
|
|
57
|
-
/**
|
|
58
|
-
* The project's catalogs arrive as one variable per locale — `EMAIL_MESSAGES_ES` and friends — read
|
|
59
|
-
* through `catalogsFromEnv`. Not declared here, because the names are the project's locales.
|
|
60
|
-
*/
|
|
61
|
-
[catalogVar: `EMAIL_MESSAGES_${string}`]: unknown;
|
|
62
|
-
BASE_URL: string;
|
|
63
|
-
LINK_TTL_DAYS?: string;
|
|
64
|
-
MAX_ATTEMPTS?: string;
|
|
65
|
-
SCHEDULER_ENABLED?: string;
|
|
66
|
-
SCHEDULER_BATCH_SIZE?: string;
|
|
67
|
-
SCHEDULER_MAX_JOBS?: string;
|
|
68
|
-
SCHEDULER_GRACE_MS?: string;
|
|
69
|
-
SCHEDULER_STUCK_MS?: string;
|
|
70
|
-
}
|
|
6
|
+
import { type EmailWorkerEnv } from "./sendDeps";
|
|
71
7
|
/**
|
|
72
8
|
* Sends a batch of jobs durably. Started for immediate sends and by the scheduler's fan-out.
|
|
73
9
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/workflows/worker.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,kBAAkB,EAAE,KAAK,aAAa,EAAE,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAE/F,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/workflows/worker.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,kBAAkB,EAAE,KAAK,aAAa,EAAE,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAE/F,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AASlE,OAAO,EAAE,KAAK,eAAe,EAAgB,MAAM,aAAa,CAAC;AACjE,OAAO,EAAiB,KAAK,cAAc,EAAc,MAAM,YAAY,CAAC;AA+D5E;;;;;;;GAOG;AACH,qBAAa,iBAAkB,SAAQ,kBAAkB,CAAC,cAAc,EAAE;IAAE,MAAM,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;IAI9E,GAAG,CAAC,KAAK,EAAE,aAAa,CAAC;QAAE,MAAM,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,EAAE,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,eAAe,CAAC,CAM3G;CACF;AAED,0FAA0F;AAC1F,qBAAa,sBAAuB,SAAQ,kBAAkB,CAAC,cAAc,EAAE,OAAO,CAAC;IACtE,GAAG,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAIpF;CACF;;IAGC;;;;;;;;;;;;;;;;;;;;;OAqBG;IACG,SAAS,cAAc,OAAO,OAAO,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAOzE;;;;;;;;OAQG;IACG,KAAK,UAAU,OAAO,OAAO,cAAc,OAAO,gBAAgB,GAAG,OAAO,CAAC,QAAQ,CAAC"}
|
package/dist/workflows/worker.js
CHANGED
|
@@ -1,53 +1,41 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
|
-
import { emailDatabase
|
|
4
|
-
import { emailSigningRegistry, resolveSigningKeys } from "../crypto/signingKey.js";
|
|
5
|
-
import { catalogLayers, catalogsFromEnv } from "../templates/messages.js";
|
|
3
|
+
import { emailDatabase } from "../data/tables.js";
|
|
6
4
|
import { mintBatchId } from "../send/batchIdentity.js";
|
|
7
|
-
import { emailHostEnv } from "./hostEnv.js";
|
|
8
5
|
import { emailWorkflowRetry } from "../send/retryPolicy.js";
|
|
9
6
|
import { createEmailHostApp } from "./hostApp.js";
|
|
10
7
|
import { isLiveInstanceStatus } from "./instanceLiveness.js";
|
|
11
8
|
import { runScheduler, schedulerHasWork } from "./scheduler.js";
|
|
12
9
|
import { runSendBatch } from "./sendBatch.js";
|
|
13
|
-
import {
|
|
14
|
-
import { requireHostEnv } from "@pithy-sh/core/src/workflow/hostEnv";
|
|
10
|
+
import { buildSendDeps, hostConfig } from "./sendDeps.js";
|
|
15
11
|
import { WorkflowEntrypoint } from "cloudflare:workers";
|
|
16
12
|
import { NonRetryableError } from "cloudflare:workflows";
|
|
17
13
|
import { classifiedSteps } from "@pithy-sh/core/src/workflow/faults";
|
|
18
14
|
//#region src/workflows/worker.ts
|
|
19
|
-
configureSharedSecrets({ registry: emailSigningRegistry });
|
|
20
|
-
/** The dispatch door. Built once per isolate; the environment gate reads its answer per request. */
|
|
21
|
-
const app = createEmailHostApp();
|
|
22
15
|
/**
|
|
23
|
-
* The
|
|
24
|
-
*
|
|
16
|
+
* The prebuilt email worker. `pithy add email` deploys one per environment (`pithy-email-staging`,
|
|
17
|
+
* `pithy-email-prod`); the user authors no code for it. It hosts:
|
|
18
|
+
*
|
|
19
|
+
* - `EmailSendWorkflow` — sends a batch of jobs durably (dispatch target for immediate sends and
|
|
20
|
+
* the scheduler's fan-out).
|
|
21
|
+
* - `EmailSchedulerWorkflow` — finds due jobs and fans them out into send batches.
|
|
22
|
+
* - `scheduled()` — the every-minute cron that fires the scheduler Workflow when a row is due.
|
|
23
|
+
* - `fetch()` — the loopback dispatch door, served only in `dev` (see {@link createEmailHostApp}).
|
|
24
|
+
*
|
|
25
|
+
* The bodies (`runSendBatch`, `runScheduler`, `runSend`) are tested against Miniflare; these classes are
|
|
26
|
+
* the thin durable shells. This module imports `cloudflare:workers`, so it runs only in the Workers
|
|
27
|
+
* runtime (excluded from the node meta-test).
|
|
28
|
+
*
|
|
29
|
+
* **Every entry validates the env first** (pithy-sh/pithy#410). Fourteen settings arrived here from a
|
|
30
|
+
* provisioning run and none of them was checked: a missing `BASE_URL` became a magic link to
|
|
31
|
+
* `undefined/…`, an unparseable `EMAIL_THEME` threw inside a render step, and a `SCHEDULER_BATCH_SIZE`
|
|
32
|
+
* of `"fifty"` became `NaN` and the scheduler claimed nothing, forever, in silence. Now
|
|
33
|
+
* {@link emailHostEnv} is parsed before anything reads a value, the coercions and defaults live in
|
|
34
|
+
* that one schema rather than at each reader, and a host that cannot work says so in one block and
|
|
35
|
+
* refuses.
|
|
25
36
|
*/
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
/** Assemble the send dependencies, resolving the current signing key from the secrets store. */
|
|
30
|
-
async function buildSendDeps(env) {
|
|
31
|
-
const config = hostConfig(env);
|
|
32
|
-
const keys = await resolveSigningKeys(env);
|
|
33
|
-
const key = keys.versions[keys.currentVersion];
|
|
34
|
-
return {
|
|
35
|
-
db: emailDatabase(env.DB),
|
|
36
|
-
suppressionDb: emailSuppressionDatabase(env.EMAIL_SUPPRESSIONS),
|
|
37
|
-
sender: env.EMAIL,
|
|
38
|
-
theme: config.EMAIL_THEME,
|
|
39
|
-
layersFor: catalogLayers(catalogsFromEnv(env)),
|
|
40
|
-
baseUrl: config.BASE_URL,
|
|
41
|
-
signing: key ? {
|
|
42
|
-
key,
|
|
43
|
-
kid: keys.currentVersion
|
|
44
|
-
} : void 0,
|
|
45
|
-
linkTtlDays: config.LINK_TTL_DAYS,
|
|
46
|
-
maxAttempts: config.MAX_ATTEMPTS,
|
|
47
|
-
environment: config.ENVIRONMENT,
|
|
48
|
-
heartbeatAt: () => /* @__PURE__ */ new Date()
|
|
49
|
-
};
|
|
50
|
-
}
|
|
37
|
+
/** The dispatch door. Built once per isolate; the environment gate reads its answer per request. */
|
|
38
|
+
const app = createEmailHostApp();
|
|
51
39
|
/**
|
|
52
40
|
* Assemble the scheduler dependencies, dispatching each batch as a send Workflow.
|
|
53
41
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pithy-sh/email",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.9",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -42,8 +42,8 @@
|
|
|
42
42
|
"postal-mime": "^3.0.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@pithy-sh/core": "^0.7.
|
|
46
|
-
"@pithy-sh/secrets": "^0.
|
|
45
|
+
"@pithy-sh/core": "^0.7.4",
|
|
46
|
+
"@pithy-sh/secrets": "^0.3.0",
|
|
47
47
|
"hono": "^4.13.2",
|
|
48
48
|
"kysely": "^0.29.0",
|
|
49
49
|
"zod": "^4.4.0"
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
|
|
4
4
|
import { ValidationError } from "@pithy-sh/core/src/error/pithyError";
|
|
5
5
|
import type { LocaleCatalogs } from "@pithy-sh/core/src/i18n/catalog";
|
|
6
|
-
import {
|
|
6
|
+
import type { FeatureIdentity } from "@pithy-sh/core/src/naming/feature";
|
|
7
|
+
import { environmentScope, featureScope, type SecretNameScope } from "@pithy-sh/core/src/naming/provisionScope";
|
|
7
8
|
import type {
|
|
8
9
|
HostD1Binding,
|
|
9
10
|
HostSecretsStoreBinding,
|
|
@@ -14,6 +15,7 @@ import type {
|
|
|
14
15
|
import { hostWorkflowsFor, resolveWorkflowHost } from "@pithy-sh/core/src/workflow/host";
|
|
15
16
|
import { workflowKey } from "@pithy-sh/core/src/workflow/naming";
|
|
16
17
|
import type { WorkflowRegistry } from "@pithy-sh/core/src/workflow/spec";
|
|
18
|
+
import { MASTER_KEY_BINDING } from "@pithy-sh/secrets/src/env/masterKeyBinding";
|
|
17
19
|
import { masterKeySecretName } from "@pithy-sh/secrets/src/provision/provisionSecrets";
|
|
18
20
|
import type { ManagedEnvironment } from "@pithy-sh/secrets/src/scope";
|
|
19
21
|
import { EMAIL_LINK_SIGNING_KEY, EMAIL_LINK_SIGNING_KEY_BINDING, emailSigningRegistry } from "../crypto/signingKey";
|
|
@@ -21,7 +23,7 @@ import { emailCatalogVarName } from "../templates/messages";
|
|
|
21
23
|
import type { EmailTheme } from "../templates/theme";
|
|
22
24
|
import { EmailScheduleParams, EmailSendParams } from "../workflows/params";
|
|
23
25
|
import { type DevMailDelivery, emailRemoteBindings } from "./devDelivery";
|
|
24
|
-
import { EMAIL_CAPABILITY, suppressionDatabaseName } from "./provisionEmail";
|
|
26
|
+
import { EMAIL_CAPABILITY, EMAIL_SUPPRESSIONS_BINDING, suppressionDatabaseName } from "./provisionEmail";
|
|
25
27
|
|
|
26
28
|
/**
|
|
27
29
|
* The email worker's `wrangler.jsonc` template shape. Email invented the prebuilt-host convention;
|
|
@@ -141,6 +143,14 @@ export interface EmailConfigParams {
|
|
|
141
143
|
* governs.
|
|
142
144
|
*/
|
|
143
145
|
env: ManagedEnvironment | "dev";
|
|
146
|
+
/**
|
|
147
|
+
* **The feature this host serves, when it serves one (#643).** A feature gets its own email host, as a
|
|
148
|
+
* declared environment does, and everything the host is called or reads takes the feature's names: the
|
|
149
|
+
* Worker and its Workflows (`<project>-f<issue>-<slug>-email[-<job>]`), the feature's own master key and
|
|
150
|
+
* link-signing key entries, and the feature's own suppression database. `env` is then `feature`, which is
|
|
151
|
+
* what the host's `ENVIRONMENT` var says.
|
|
152
|
+
*/
|
|
153
|
+
feature?: FeatureIdentity;
|
|
144
154
|
/** The app database id for this environment — where jobs/events live. */
|
|
145
155
|
appDatabaseId: string;
|
|
146
156
|
/** The shared suppression database id (same in every environment). */
|
|
@@ -197,25 +207,37 @@ export function resolveEmailConfig(
|
|
|
197
207
|
params: EmailConfigParams,
|
|
198
208
|
): EmailWorkerWranglerTemplate {
|
|
199
209
|
const { project, env, appDatabaseId, suppressionDatabaseId, secretsDatabaseId, storeId, baseUrl, theme } = params;
|
|
210
|
+
const feature = params.feature;
|
|
211
|
+
// One scope for every name below, so a feature's host can never bind a declared environment's key or list.
|
|
212
|
+
const scope = feature ? featureScope(feature) : environmentScope(project, env);
|
|
200
213
|
const resolved = resolveWorkflowHost(template, {
|
|
201
214
|
project,
|
|
202
215
|
capability: EMAIL_CAPABILITY,
|
|
203
216
|
env,
|
|
217
|
+
...(feature ? { feature } : {}),
|
|
204
218
|
databaseIds: {
|
|
205
219
|
DB: appDatabaseId,
|
|
206
220
|
EMAIL_SUPPRESSIONS: suppressionDatabaseId,
|
|
207
221
|
SECRETS: secretsDatabaseId,
|
|
208
222
|
},
|
|
209
|
-
|
|
223
|
+
// A feature migrates and owns its own suppression list, so it is named for the feature (`featureScope`
|
|
224
|
+
// does not honor `global`); a declared environment shares the project's one list.
|
|
225
|
+
databaseNames: {
|
|
226
|
+
EMAIL_SUPPRESSIONS: feature
|
|
227
|
+
? scope.resource(EMAIL_SUPPRESSIONS_BINDING, "d1", { scope: "global" })
|
|
228
|
+
: suppressionDatabaseName(project),
|
|
229
|
+
},
|
|
210
230
|
secretsStoreId: storeId,
|
|
211
231
|
// The master key entry is project- and env-scoped, matching what the secrets manager wrote.
|
|
212
|
-
masterKeySecretName:
|
|
232
|
+
masterKeySecretName: feature
|
|
233
|
+
? scope.secretEntry(MASTER_KEY_BINDING, "environment")
|
|
234
|
+
: masterKeySecretName(project, env),
|
|
213
235
|
// The link-signing key, at the entry `pithy secrets provision` created and bound the app Worker to —
|
|
214
236
|
// composed by the same `environmentScope(...).secretEntry` call, from the declaration's own scope, so
|
|
215
237
|
// the Worker that signs a link and the Worker that verifies it cannot be handed two different keys.
|
|
216
238
|
// Keyed by the binding the template declares; the entry keeps the registry key's name (#603).
|
|
217
239
|
secretNames: {
|
|
218
|
-
[EMAIL_LINK_SIGNING_KEY_BINDING]:
|
|
240
|
+
[EMAIL_LINK_SIGNING_KEY_BINDING]: scope.secretEntry(
|
|
219
241
|
EMAIL_LINK_SIGNING_KEY,
|
|
220
242
|
emailSigningRegistry[EMAIL_LINK_SIGNING_KEY].scope as SecretNameScope,
|
|
221
243
|
),
|
|
@@ -229,7 +251,12 @@ export function resolveEmailConfig(
|
|
|
229
251
|
remoteBindings: emailRemoteBindings(env, params.devDelivery ?? "remote"),
|
|
230
252
|
// Both Workflows, derived from the registry. A Workflow name is account-scoped, so the deployed
|
|
231
253
|
// name has to carry the project — the template's `pithy-email-send` cannot be suffixed into one.
|
|
232
|
-
workflows: hostWorkflowsFor(emailWorkflowRegistry, {
|
|
254
|
+
workflows: hostWorkflowsFor(emailWorkflowRegistry, {
|
|
255
|
+
project,
|
|
256
|
+
capability: EMAIL_CAPABILITY,
|
|
257
|
+
env,
|
|
258
|
+
...(feature ? { feature } : {}),
|
|
259
|
+
}).workflows,
|
|
233
260
|
});
|
|
234
261
|
// The resolver fills fields; it never drops one. So every field this template narrows to required
|
|
235
262
|
// survives — knowledge the generic return type cannot express, restored here.
|
package/src/version.generated.ts
CHANGED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import type { D1Database } from "@cloudflare/workers-types";
|
|
5
|
+
import { requireHostEnv } from "@pithy-sh/core/src/workflow/hostEnv";
|
|
6
|
+
import type { SecretBinding, SecretsStoreEnv } from "@pithy-sh/secrets/src/env/bindings";
|
|
7
|
+
import { configureSharedSecrets } from "@pithy-sh/secrets/src/sharedSecretsStore";
|
|
8
|
+
import { EMAIL_LINK_SIGNING_KEY_BINDING, emailSigningRegistry, resolveSigningKeys } from "../crypto/signingKey";
|
|
9
|
+
import { emailDatabase, emailSuppressionDatabase } from "../data/tables";
|
|
10
|
+
import type { SendWorkflowBinding } from "../send/enqueue";
|
|
11
|
+
import type { EmailSender } from "../send/sender";
|
|
12
|
+
import { catalogLayers, catalogsFromEnv } from "../templates/messages";
|
|
13
|
+
import { type EmailHostEnv, emailHostEnv } from "./hostEnv";
|
|
14
|
+
import type { SendWorkflowInstances } from "./instances";
|
|
15
|
+
import type { SendBatchDeps } from "./sendBatch";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The email worker's env as the runtime hands it over — bindings as objects, every var as a string.
|
|
19
|
+
*
|
|
20
|
+
* The *shape the host runs on* is {@link EmailHostEnv}, which is this parsed: numbers as numbers, the
|
|
21
|
+
* theme as a validated `EmailTheme`, `SCHEDULER_ENABLED` as a boolean. This type stays because it is
|
|
22
|
+
* what a `WorkflowEntrypoint` is generic over and what the platform actually binds.
|
|
23
|
+
*/
|
|
24
|
+
export interface EmailWorkerEnv extends SecretsStoreEnv {
|
|
25
|
+
/** The app database the per-environment jobs/events tables live in. */
|
|
26
|
+
DB: D1Database;
|
|
27
|
+
/** The link-signing key: this environment's Secrets Store entry, or its `.dev.vars` string locally (#596). */
|
|
28
|
+
[EMAIL_LINK_SIGNING_KEY_BINDING]: SecretBinding | string;
|
|
29
|
+
/** The shared, durable suppression database. */
|
|
30
|
+
EMAIL_SUPPRESSIONS: D1Database;
|
|
31
|
+
/** The Cloudflare Email Service send binding. */
|
|
32
|
+
EMAIL: EmailSender;
|
|
33
|
+
/** The send Workflow (self) — the scheduler creates batches against it, and asks after them. */
|
|
34
|
+
EMAIL_SENDER: SendWorkflowBinding & SendWorkflowInstances;
|
|
35
|
+
/** The scheduler Workflow (self) — fired by the cron. */
|
|
36
|
+
EMAIL_SCHEDULER: { create(): Promise<unknown> };
|
|
37
|
+
/** The resolved brand theme as a JSON string (the full `EmailTheme`), set at provision from the app config. */
|
|
38
|
+
EMAIL_THEME?: string;
|
|
39
|
+
/**
|
|
40
|
+
* The project's catalogs arrive as one variable per locale — `EMAIL_MESSAGES_ES` and friends — read
|
|
41
|
+
* through `catalogsFromEnv`. Not declared here, because the names are the project's locales.
|
|
42
|
+
*/
|
|
43
|
+
[catalogVar: `EMAIL_MESSAGES_${string}`]: unknown;
|
|
44
|
+
BASE_URL: string;
|
|
45
|
+
// ENVIRONMENT is inherited from SecretsStoreEnv (a `ManagedEnvironment`); never redeclare it as a plain string.
|
|
46
|
+
LINK_TTL_DAYS?: string;
|
|
47
|
+
MAX_ATTEMPTS?: string;
|
|
48
|
+
SCHEDULER_ENABLED?: string;
|
|
49
|
+
SCHEDULER_BATCH_SIZE?: string;
|
|
50
|
+
SCHEDULER_MAX_JOBS?: string;
|
|
51
|
+
SCHEDULER_GRACE_MS?: string;
|
|
52
|
+
SCHEDULER_STUCK_MS?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// This is a standalone worker, not assembled by `createBackend`, so the secrets capability's `compose`
|
|
56
|
+
// hook never runs here. Configure the shared per-invocation accessor directly from email's own slice so
|
|
57
|
+
// `resolveSigningKeys` reads the signing key through the one cached path.
|
|
58
|
+
configureSharedSecrets({ registry: emailSigningRegistry });
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The env, parsed — or one legible block naming every unusable setting and what fills it, then a
|
|
62
|
+
* refusal. Called at the top of every entry, and the block is written once per env object.
|
|
63
|
+
*/
|
|
64
|
+
export function hostConfig(env: EmailWorkerEnv): EmailHostEnv {
|
|
65
|
+
return requireHostEnv(emailHostEnv, env);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Assemble the send dependencies, resolving the current signing key from the secrets store.
|
|
70
|
+
*
|
|
71
|
+
* Here rather than in `worker.ts`, which imports `cloudflare:workers` and so loads in workerd alone: this is the
|
|
72
|
+
* host's whole reading of its env for a send, and a test that drives a send the way the deployed host would —
|
|
73
|
+
* the feature sign-in end to end, #643 — has to read it through this function, not a copy of it.
|
|
74
|
+
*/
|
|
75
|
+
export async function buildSendDeps(env: EmailWorkerEnv): Promise<SendBatchDeps> {
|
|
76
|
+
const config = hostConfig(env);
|
|
77
|
+
const keys = await resolveSigningKeys(env);
|
|
78
|
+
const key = keys.versions[keys.currentVersion];
|
|
79
|
+
return {
|
|
80
|
+
db: emailDatabase(env.DB),
|
|
81
|
+
suppressionDb: emailSuppressionDatabase(env.EMAIL_SUPPRESSIONS),
|
|
82
|
+
sender: env.EMAIL,
|
|
83
|
+
theme: config.EMAIL_THEME,
|
|
84
|
+
// The catalogs, as a seam over the one JSON var. A body renders in the job's own locale from here;
|
|
85
|
+
// with the var absent this resolves to the kit's English, unchanged from before #441.
|
|
86
|
+
layersFor: catalogLayers(catalogsFromEnv(env as unknown as Record<string, unknown>)),
|
|
87
|
+
baseUrl: config.BASE_URL,
|
|
88
|
+
signing: key ? { key, kid: keys.currentVersion } : undefined,
|
|
89
|
+
linkTtlDays: config.LINK_TTL_DAYS,
|
|
90
|
+
maxAttempts: config.MAX_ATTEMPTS,
|
|
91
|
+
environment: config.ENVIRONMENT,
|
|
92
|
+
heartbeatAt: () => new Date(),
|
|
93
|
+
};
|
|
94
|
+
}
|
package/src/workflows/worker.ts
CHANGED
|
@@ -3,24 +3,17 @@
|
|
|
3
3
|
|
|
4
4
|
import { WorkflowEntrypoint, type WorkflowEvent, type WorkflowStep } from "cloudflare:workers";
|
|
5
5
|
import { NonRetryableError } from "cloudflare:workflows";
|
|
6
|
-
import type {
|
|
6
|
+
import type { ExecutionContext } from "@cloudflare/workers-types";
|
|
7
7
|
import { classifiedSteps } from "@pithy-sh/core/src/workflow/faults";
|
|
8
|
-
import {
|
|
9
|
-
import type { SecretBinding, SecretsStoreEnv } from "@pithy-sh/secrets/src/env/bindings";
|
|
10
|
-
import { configureSharedSecrets } from "@pithy-sh/secrets/src/sharedSecretsStore";
|
|
11
|
-
import { EMAIL_LINK_SIGNING_KEY_BINDING, emailSigningRegistry, resolveSigningKeys } from "../crypto/signingKey";
|
|
12
|
-
import { emailDatabase, emailSuppressionDatabase } from "../data/tables";
|
|
8
|
+
import { emailDatabase } from "../data/tables";
|
|
13
9
|
import { mintBatchId } from "../send/batchIdentity";
|
|
14
|
-
import type { SendWorkflowBinding } from "../send/enqueue";
|
|
15
10
|
import { emailWorkflowRetry } from "../send/retryPolicy";
|
|
16
|
-
import type { EmailSender } from "../send/sender";
|
|
17
|
-
import { catalogLayers, catalogsFromEnv } from "../templates/messages";
|
|
18
11
|
import { createEmailHostApp } from "./hostApp";
|
|
19
|
-
import {
|
|
12
|
+
import type { EmailHostEnv } from "./hostEnv";
|
|
20
13
|
import { isLiveInstanceStatus } from "./instanceLiveness";
|
|
21
|
-
import type { SendWorkflowInstances } from "./instances";
|
|
22
14
|
import { runScheduler, type SchedulerDeps, schedulerHasWork } from "./scheduler";
|
|
23
|
-
import { type BatchSendReport, runSendBatch
|
|
15
|
+
import { type BatchSendReport, runSendBatch } from "./sendBatch";
|
|
16
|
+
import { buildSendDeps, type EmailWorkerEnv, hostConfig } from "./sendDeps";
|
|
24
17
|
|
|
25
18
|
/**
|
|
26
19
|
* The prebuilt email worker. `pithy add email` deploys one per environment (`pithy-email-staging`,
|
|
@@ -45,82 +38,9 @@ import { type BatchSendReport, runSendBatch, type SendBatchDeps } from "./sendBa
|
|
|
45
38
|
* refuses.
|
|
46
39
|
*/
|
|
47
40
|
|
|
48
|
-
/**
|
|
49
|
-
* The email worker's env as the runtime hands it over — bindings as objects, every var as a string.
|
|
50
|
-
*
|
|
51
|
-
* The *shape the host runs on* is {@link EmailHostEnv}, which is this parsed: numbers as numbers, the
|
|
52
|
-
* theme as a validated `EmailTheme`, `SCHEDULER_ENABLED` as a boolean. This type stays because it is
|
|
53
|
-
* what a `WorkflowEntrypoint` is generic over and what the platform actually binds.
|
|
54
|
-
*/
|
|
55
|
-
export interface EmailWorkerEnv extends SecretsStoreEnv {
|
|
56
|
-
/** The app database the per-environment jobs/events tables live in. */
|
|
57
|
-
DB: D1Database;
|
|
58
|
-
/** The link-signing key: this environment's Secrets Store entry, or its `.dev.vars` string locally (#596). */
|
|
59
|
-
[EMAIL_LINK_SIGNING_KEY_BINDING]: SecretBinding | string;
|
|
60
|
-
/** The shared, durable suppression database. */
|
|
61
|
-
EMAIL_SUPPRESSIONS: D1Database;
|
|
62
|
-
/** The Cloudflare Email Service send binding. */
|
|
63
|
-
EMAIL: EmailSender;
|
|
64
|
-
/** The send Workflow (self) — the scheduler creates batches against it, and asks after them. */
|
|
65
|
-
EMAIL_SENDER: SendWorkflowBinding & SendWorkflowInstances;
|
|
66
|
-
/** The scheduler Workflow (self) — fired by the cron. */
|
|
67
|
-
EMAIL_SCHEDULER: { create(): Promise<unknown> };
|
|
68
|
-
/** The resolved brand theme as a JSON string (the full `EmailTheme`), set at provision from the app config. */
|
|
69
|
-
EMAIL_THEME?: string;
|
|
70
|
-
/**
|
|
71
|
-
* The project's catalogs arrive as one variable per locale — `EMAIL_MESSAGES_ES` and friends — read
|
|
72
|
-
* through `catalogsFromEnv`. Not declared here, because the names are the project's locales.
|
|
73
|
-
*/
|
|
74
|
-
[catalogVar: `EMAIL_MESSAGES_${string}`]: unknown;
|
|
75
|
-
BASE_URL: string;
|
|
76
|
-
// ENVIRONMENT is inherited from SecretsStoreEnv (a `ManagedEnvironment`); never redeclare it as a plain string.
|
|
77
|
-
LINK_TTL_DAYS?: string;
|
|
78
|
-
MAX_ATTEMPTS?: string;
|
|
79
|
-
SCHEDULER_ENABLED?: string;
|
|
80
|
-
SCHEDULER_BATCH_SIZE?: string;
|
|
81
|
-
SCHEDULER_MAX_JOBS?: string;
|
|
82
|
-
SCHEDULER_GRACE_MS?: string;
|
|
83
|
-
SCHEDULER_STUCK_MS?: string;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// This is a standalone worker, not assembled by `createBackend`, so the secrets capability's `compose`
|
|
87
|
-
// hook never runs here. Configure the shared per-invocation accessor directly from email's own slice so
|
|
88
|
-
// `resolveSigningKeys` reads the signing key through the one cached path.
|
|
89
|
-
configureSharedSecrets({ registry: emailSigningRegistry });
|
|
90
|
-
|
|
91
41
|
/** The dispatch door. Built once per isolate; the environment gate reads its answer per request. */
|
|
92
42
|
const app = createEmailHostApp();
|
|
93
43
|
|
|
94
|
-
/**
|
|
95
|
-
* The env, parsed — or one legible block naming every unusable setting and what fills it, then a
|
|
96
|
-
* refusal. Called at the top of every entry, and the block is written once per env object.
|
|
97
|
-
*/
|
|
98
|
-
function hostConfig(env: EmailWorkerEnv): EmailHostEnv {
|
|
99
|
-
return requireHostEnv(emailHostEnv, env);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/** Assemble the send dependencies, resolving the current signing key from the secrets store. */
|
|
103
|
-
async function buildSendDeps(env: EmailWorkerEnv): Promise<SendBatchDeps> {
|
|
104
|
-
const config = hostConfig(env);
|
|
105
|
-
const keys = await resolveSigningKeys(env);
|
|
106
|
-
const key = keys.versions[keys.currentVersion];
|
|
107
|
-
return {
|
|
108
|
-
db: emailDatabase(env.DB),
|
|
109
|
-
suppressionDb: emailSuppressionDatabase(env.EMAIL_SUPPRESSIONS),
|
|
110
|
-
sender: env.EMAIL,
|
|
111
|
-
theme: config.EMAIL_THEME,
|
|
112
|
-
// The catalogs, as a seam over the one JSON var. A body renders in the job's own locale from here;
|
|
113
|
-
// with the var absent this resolves to the kit's English, unchanged from before #441.
|
|
114
|
-
layersFor: catalogLayers(catalogsFromEnv(env as unknown as Record<string, unknown>)),
|
|
115
|
-
baseUrl: config.BASE_URL,
|
|
116
|
-
signing: key ? { key, kid: keys.currentVersion } : undefined,
|
|
117
|
-
linkTtlDays: config.LINK_TTL_DAYS,
|
|
118
|
-
maxAttempts: config.MAX_ATTEMPTS,
|
|
119
|
-
environment: config.ENVIRONMENT,
|
|
120
|
-
heartbeatAt: () => new Date(),
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
|
|
124
44
|
/**
|
|
125
45
|
* Assemble the scheduler dependencies, dispatching each batch as a send Workflow.
|
|
126
46
|
*
|