@pithy-sh/email 0.4.0 → 0.5.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.
@@ -1 +1 @@
1
- {"version":3,"file":"capability.d.ts","sourceRoot":"","sources":["../src/capability.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,KAAK,UAAU,EAAoB,MAAM,0CAA0C,CAAC;AAG7F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACtE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EACL,KAAK,wBAAwB,EAM9B,MAAM,eAAe,CAAC;AAMvB,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAG1D,OAAO,EAAE,KAAK,YAAY,EAAE,KAAK,aAAa,EAAgB,MAAM,gBAAgB,CAAC;AACrF,OAAO,EAAE,KAAK,cAAc,EAAsB,MAAM,sBAAsB,CAAC;AAE/E,OAAO,EAAe,KAAK,UAAU,EAAgB,MAAM,mBAAmB,CAAC;AAI/E;;;;;;;;;;GAUG;AACH,MAAM,WAAW,eAAgB,SAAQ,cAAc;IACrD,EAAE,EAAE,UAAU,CAAC;IACf;;;;;;;OAOG;IACH,kBAAkB,CAAC,EAAE,UAAU,CAAC;CACjC;AAED;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,MAAM,CAAC;AAEzC,kGAAkG;AAClG,eAAO,MAAM,kCAAkC,MAAM,CAAC;AAEtD;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAiC8B,CAAC;AACvD,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,WAAW,CAAC,CAAC;AACvD,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,WAAW,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;;;;;;;;OAWG;IACH,YAAY,EAAE,MAAM,cAAc,CAAC;CACpC;AAED;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,MAAM,EAAE,gBAAgB,GAAG,eAAe,CAoO/D;AAED,kFAAkF;AAClF,wBAAgB,iBAAiB,CAAC,UAAU,EAAE,UAAU,GAAG,UAAU,IAAI,eAAe,CAEvF"}
@@ -20,6 +20,7 @@ import { EmailScheduleParams, EmailSendParams } from "./workflows/params.js";
20
20
  import { z } from "zod";
21
21
  import { defineCapability } from "@pithy-sh/core/src/capability/capability";
22
22
  import { pithyCommandOf } from "@pithy-sh/core/src/cli/command";
23
+ import { composedUsageMeter } from "@pithy-sh/core/src/entitlement/meter";
23
24
  import { InternalError } from "@pithy-sh/core/src/error/pithyError";
24
25
  //#region src/capability.ts
25
26
  /**
@@ -80,6 +81,12 @@ function email(config) {
80
81
  * capability's own config, and this is the only thing that knows it.
81
82
  */
82
83
  let supportedLocales = [];
84
+ /**
85
+ * The usage meter a composed provider exposes — `@pithy-sh/payments`, when it is composed — found in
86
+ * `compose` by core's structural predicate. Undefined with no provider, which is the email capability
87
+ * exactly as it always was: nothing is counted and nothing is refused.
88
+ */
89
+ let meter;
83
90
  const capability = defineCapability({
84
91
  name: "email",
85
92
  version: PACKAGE_VERSION,
@@ -114,6 +121,7 @@ function email(config) {
114
121
  * project sends what it always sent.
115
122
  */
116
123
  compose: ({ capabilities }) => {
124
+ meter = composedUsageMeter(capabilities);
117
125
  for (const composed of capabilities) {
118
126
  if (composed.name !== "i18n") continue;
119
127
  const candidate = composed.layersFor;
@@ -230,6 +238,12 @@ function email(config) {
230
238
  });
231
239
  return emailSuppressionDatabase(binding);
232
240
  };
241
+ /** The meter charge for one env, or undefined when no provider is composed. */
242
+ const chargeFor = (env) => {
243
+ const composed = meter;
244
+ if (composed === void 0) return void 0;
245
+ return (charge) => composed.charge(env, charge);
246
+ };
233
247
  const enqueue = (env, input) => enqueueEmail({
234
248
  db: emailDatabase(env.DB),
235
249
  fromAddress: resolved.fromAddress,
@@ -238,6 +252,7 @@ function email(config) {
238
252
  layersFor: (locale) => layersFor(locale),
239
253
  sender: emailSenderBinding(env),
240
254
  suppressionDb: env.EMAIL_SUPPRESSIONS ? emailSuppressionDatabase(env.EMAIL_SUPPRESSIONS) : void 0,
255
+ meter: chargeFor(env),
241
256
  now: /* @__PURE__ */ new Date(),
242
257
  newId: () => crypto.randomUUID()
243
258
  }, input);
@@ -33,6 +33,10 @@ export declare const emailSigningRegistry: {
33
33
  readonly rotation: {
34
34
  readonly kind: "local";
35
35
  };
36
+ readonly overlap: {
37
+ readonly days: 90;
38
+ readonly minimumDays: 90;
39
+ };
36
40
  };
37
41
  };
38
42
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"signingKey.d.ts","sourceRoot":"","sources":["../../src/crypto/signingKey.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AAE1E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AAG1E;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,2BAA2B,CAAC;AAE/D;;;;GAIG;AACH,eAAO,MAAM,8BAA8B,0BAA4C,CAAC;AAExF,gHAAgH;AAChH,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;CA+B/B,CAAC;AAEH;;;;GAIG;AACH,wBAAsB,kBAAkB,CACtC,GAAG,EAAE,eAAe,GACnB,OAAO,CAAC,eAAe,CAAC,CAAC,OAAO,oBAAoB,CAAC,CAAC,OAAO,sBAAsB,CAAC,CAAC,CAAC,CAGxF"}
1
+ {"version":3,"file":"signingKey.d.ts","sourceRoot":"","sources":["../../src/crypto/signingKey.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AAE1E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AAG1E;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,2BAA2B,CAAC;AAE/D;;;;GAIG;AACH,eAAO,MAAM,8BAA8B,0BAA4C,CAAC;AAExF,gHAAgH;AAChH,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;CAqC/B,CAAC;AAEH;;;;GAIG;AACH,wBAAsB,kBAAkB,CACtC,GAAG,EAAE,eAAe,GACnB,OAAO,CAAC,eAAe,CAAC,CAAC,OAAO,oBAAoB,CAAC,CAAC,OAAO,sBAAsB,CAAC,CAAC,CAAC,CAGxF"}
@@ -31,7 +31,11 @@ const emailSigningRegistry = defineSecretRegistry({ [EMAIL_LINK_SIGNING_KEY]: {
31
31
  encoding: "base64url"
32
32
  }
33
33
  },
34
- rotation: { kind: "local" }
34
+ rotation: { kind: "local" },
35
+ overlap: {
36
+ days: 90,
37
+ minimumDays: 90
38
+ }
35
39
  } });
36
40
  /**
37
41
  * Resolve the current signing key plus every still-valid prior version. The current version is the
@@ -153,6 +153,11 @@ export interface EmailDeprovisioner {
153
153
  * before it deletes the list they share (#591).
154
154
  */
155
155
  hasWorker(env: ManagedEnvironment): Promise<boolean>;
156
+ /**
157
+ * Delete the env's send and schedule Workflows, by `emailWorkflowNames`. Idempotent (a Workflow already gone is
158
+ * a no-op). Deleting the worker leaves them registered, so they go first (#789).
159
+ */
160
+ deleteWorkflows(env: ManagedEnvironment): Promise<void>;
156
161
  /** Delete the env's email worker. Idempotent (a missing worker is a no-op). */
157
162
  deleteWorker(env: ManagedEnvironment): Promise<void>;
158
163
  /**
@@ -184,7 +189,10 @@ export interface EmailDeprovisionOptions {
184
189
  * 2. With `deleteSuppression` only: refuse while any other declared environment still runs an email worker
185
190
  * (`assertSharedLeavesLast`). The list is one per project, and a staging teardown must not take production's.
186
191
  * 3. Count the list, and refuse unless the operator counted the same (`assertRetainedAgreed`, #588's guard).
187
- * 4. Delete the environment's worker (it binds the list), then — only when asked — the list.
192
+ * 4. Delete the environment's Workflows (#789), then its worker (it binds the list), then — only when asked — the list.
193
+ *
194
+ * It never deletes the environment's link-signing key. `pithy secrets provision` creates that entry and binds it
195
+ * into the app Worker, which is still deployed, and every link already sent is signed with it (#596).
188
196
  *
189
197
  * Idempotent end to end: a missing worker or database is a no-op.
190
198
  */
@@ -1 +1 @@
1
- {"version":3,"file":"provisionEmail.d.ts","sourceRoot":"","sources":["../../src/provision/provisionEmail.ts"],"names":[],"mappings":"AAGA,OAAO,EAAwB,KAAK,YAAY,EAAE,MAAM,wCAAwC,CAAC;AACjG,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AAKlF,OAAO,EAEL,KAAK,iBAAiB,EAEtB,KAAK,kBAAkB,EAExB,MAAM,6BAA6B,CAAC;AAErC;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,4GAA4G;AAC5G,eAAO,MAAM,gBAAgB,UAAU,CAAC;AAExC,kGAAkG;AAClG,eAAO,MAAM,0BAA0B,uBAAuB,CAAC;AAW/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAE/D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAE7D;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,kBAAkB,GAAG,MAAM,CAEhF;AAED,uEAAuE;AACvE,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3B;;;OAGG;IACH,yBAAyB,IAAI,OAAO,CAAC;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;IAC/E,8GAA8G;IAC9G,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACtD,sHAAsH;IACtH,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AAM,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,gBAAgB,CACpC,aAAa,EAAE,kBAAkB,EACjC,MAAM,EAAE,iBAAiB,EACzB,OAAO,GAAE,uBAA4B,GACpC,OAAO,CAAC;IAAE,GAAG,EAAE,kBAAkB,CAAA;CAAE,CAAC,CAiBtC"}
@@ -146,7 +146,10 @@ async function provisionEmail(provisioner, environments) {
146
146
  * 2. With `deleteSuppression` only: refuse while any other declared environment still runs an email worker
147
147
  * (`assertSharedLeavesLast`). The list is one per project, and a staging teardown must not take production's.
148
148
  * 3. Count the list, and refuse unless the operator counted the same (`assertRetainedAgreed`, #588's guard).
149
- * 4. Delete the environment's worker (it binds the list), then — only when asked — the list.
149
+ * 4. Delete the environment's Workflows (#789), then its worker (it binds the list), then — only when asked — the list.
150
+ *
151
+ * It never deletes the environment's link-signing key. `pithy secrets provision` creates that entry and binds it
152
+ * into the app Worker, which is still deployed, and every link already sent is signed with it (#596).
150
153
  *
151
154
  * Idempotent end to end: a missing worker or database is a no-op.
152
155
  */
@@ -159,6 +162,7 @@ async function deprovisionEmail(deprovisioner, target, options = {}) {
159
162
  }]);
160
163
  assertRetainedAgreed(await deprovisioner.countSuppressionRetained(), options.destroyRetained, "anything was deleted");
161
164
  }
165
+ await deprovisioner.deleteWorkflows(env);
162
166
  await deprovisioner.deleteWorker(env);
163
167
  if (options.deleteSuppression) await deprovisioner.deleteSuppressionDatabase();
164
168
  return { env };
@@ -29,6 +29,13 @@ export interface EmailWorkerWranglerTemplate extends WorkflowHostTemplate {
29
29
  }
30
30
  /** The registry the host resolver derives its `workflows` array from, and the host's app dispatches on. */
31
31
  export declare const emailWorkflowRegistry: WorkflowRegistry;
32
+ /**
33
+ * The Workflows one environment's email worker is deployed with — `<project>-<env>-email-send` and
34
+ * `<project>-<env>-email-schedule`. Derived by the call {@link resolveEmailConfig} deploys with, so teardown
35
+ * deletes the names provision created and no others (#789). Deleting a Worker does not delete the Workflows
36
+ * registered against it.
37
+ */
38
+ export declare function emailWorkflowNames(project: string, env: ManagedEnvironment): string[];
32
39
  /**
33
40
  * The most one Cloudflare Worker variable holds. **Per variable, and that is the whole design.**
34
41
  *
@@ -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;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;;;;;;;;;;;;;;;;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C,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"}
@@ -34,6 +34,19 @@ const emailWorkflowRegistry = Object.fromEntries(Object.entries({
34
34
  }];
35
35
  }));
36
36
  /**
37
+ * The Workflows one environment's email worker is deployed with — `<project>-<env>-email-send` and
38
+ * `<project>-<env>-email-schedule`. Derived by the call {@link resolveEmailConfig} deploys with, so teardown
39
+ * deletes the names provision created and no others (#789). Deleting a Worker does not delete the Workflows
40
+ * registered against it.
41
+ */
42
+ function emailWorkflowNames(project, env) {
43
+ return hostWorkflowsFor(emailWorkflowRegistry, {
44
+ project,
45
+ capability: EMAIL_CAPABILITY,
46
+ env
47
+ }).workflows.map((workflow) => workflow.name);
48
+ }
49
+ /**
37
50
  * The most one Cloudflare Worker variable holds. **Per variable, and that is the whole design.**
38
51
  *
39
52
  * Cloudflare's documented limit is 5 KB per variable, on Free and Paid alike, and a Worker carries 64
@@ -131,4 +144,4 @@ function resolveEmailConfig(template, params) {
131
144
  });
132
145
  }
133
146
  //#endregion
134
- export { MAX_WORKER_VAR_BYTES, emailMessagesVars, emailWorkflowRegistry, resolveEmailConfig };
147
+ export { MAX_WORKER_VAR_BYTES, emailMessagesVars, emailWorkflowNames, emailWorkflowRegistry, resolveEmailConfig };
@@ -1,5 +1,6 @@
1
1
  // SPDX-FileCopyrightText: 2026 Pithy
2
2
  // SPDX-License-Identifier: MIT
3
+ import type { MeterCharge, MeterSubject } from "@pithy-sh/core/src/entitlement/meter";
3
4
  import type { EmailJobStatus, SendMode, SuppressionReason } from "../data/enums";
4
5
  import type { EmailDatabase, EmailSuppressionDatabase } from "../data/tables";
5
6
  import { type EmailMessageLayers } from "../templates/messages";
@@ -112,6 +113,16 @@ export interface EnqueueInput {
112
113
  inReplyTo?: string;
113
114
  /** The `References` chain, angle-bracketed and space-separated. Built by the caller from the parent. */
114
115
  references?: string;
116
+ /**
117
+ * Who this message is sent on behalf of, for the `emails` meter — the holder pair `@pithy-sh/payments`
118
+ * bills, `{ subjectType: "organization", subjectId }`.
119
+ *
120
+ * **Omit it and nothing is counted**, which is right for every message the kit sends on its own account: a
121
+ * magic link to somebody who has not signed up is not a tenant's usage. Name it on mail a customer's plan
122
+ * pays for — a campaign, a notification their workspace triggers — and, where the meter is enforced, an
123
+ * enqueue past the period's quota throws `payments/quota_exceeded` and writes no job.
124
+ */
125
+ subject?: MeterSubject;
115
126
  }
116
127
  /** Dependencies enqueue needs: the database, the from identity, optional send-Workflow binding, time, ids. */
117
128
  export interface EnqueueDeps {
@@ -139,6 +150,11 @@ export interface EnqueueDeps {
139
150
  * goes could not be consulted yet, which is strictly worse than queueing it.
140
151
  */
141
152
  suppressionDb?: EmailSuppressionDatabase;
153
+ /**
154
+ * Charge one email against the `emails` meter, or refuse — the composed provider's seam, closed over this
155
+ * request's env. Absent when no provider is composed, and then nothing is counted.
156
+ */
157
+ meter?: (charge: MeterCharge) => Promise<void>;
142
158
  now: Date;
143
159
  /** Generate a job id (a UUID in production). */
144
160
  newId: () => string;
@@ -1 +1 @@
1
- {"version":3,"file":"enqueue.d.ts","sourceRoot":"","sources":["../../src/send/enqueue.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,cAA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G;IACH,OAAO,CAAC,EAAE,YAAY,CAAC;CACxB;AAED,8GAA8G;AAC9G,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,aAAa,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,UAAU,CAAC;IAClB;;;;;OAKG;IACH,SAAS,CAAC,EAAE,kBAAkB,CAAC;IAC/B,mHAAmH;IACnH,MAAM,CAAC,EAAE,mBAAmB,CAAC;IAC7B;;;;;;;;;OASG;IACH,aAAa,CAAC,EAAE,wBAAwB,CAAC;IACzC;;;OAGG;IACH,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/C,GAAG,EAAE,IAAI,CAAC;IACV,gDAAgD;IAChD,KAAK,EAAE,MAAM,MAAM,CAAC;IACpB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,MAAM,CAAC;CAC3B;AAED,sEAAsE;AACtE,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd;;;;;;;;;OASG;IACH,MAAM,EAAE,cAAc,CAAC;IACvB;;;;;;;OAOG;IACH,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;IACtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAuCG;IACH,OAAO,EAAE,MAAM,CAAC;CACjB;AAgBD,oGAAoG;AACpG,wBAAsB,YAAY,CAAC,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC,CAgKjG"}
@@ -89,6 +89,20 @@ async function enqueueEmail(deps, input) {
89
89
  * exist.
90
90
  */
91
91
  const batchId = sender ? (deps.newBatchId ?? mintBatchId)() : null;
92
+ /**
93
+ * Charge the `emails` meter **before the row exists**, so a refusal writes no job — and only for a message
94
+ * that is going to be sent. A withheld recipient costs the customer nothing, and a message the kit sends
95
+ * on nobody's account names no subject and is never counted.
96
+ *
97
+ * Before the insert rather than after it, and the cost is the right way round: an insert that fails after
98
+ * a charge over-counts by one, and over-counting briefly is recoverable where under-counting is not.
99
+ */
100
+ if (!blocked && input.subject !== void 0 && deps.meter !== void 0) await deps.meter({
101
+ subject: input.subject,
102
+ meter: "emails",
103
+ amount: 1,
104
+ now: deps.now
105
+ });
92
106
  const marketing = template.category === "marketing";
93
107
  const job = {
94
108
  id: deps.newId(),
@@ -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.4.0";
6
+ export declare const PACKAGE_VERSION = "0.5.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.4.0";
7
+ const PACKAGE_VERSION = "0.5.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.4.0",
3
+ "version": "0.5.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": "tsc -p tsconfig.json",
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.10.0",
46
- "@pithy-sh/secrets": "^0.4.0",
45
+ "@pithy-sh/core": "^0.11.0",
46
+ "@pithy-sh/secrets": "^0.5.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
  },
@@ -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
 
@@ -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
  *
@@ -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(),
@@ -13,4 +13,4 @@
13
13
  export const PACKAGE_NAME = "@pithy-sh/email";
14
14
 
15
15
  /** This package's version, stamped from its own package.json at generation time. */
16
- export const PACKAGE_VERSION = "0.4.0";
16
+ export const PACKAGE_VERSION = "0.5.0";