@pithy-sh/email 0.1.2 → 0.1.4

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.
Files changed (176) hide show
  1. package/dist/analytics.d.ts +21 -0
  2. package/dist/analytics.d.ts.map +1 -0
  3. package/dist/analytics.js +15 -0
  4. package/dist/audit/actions.d.ts +47 -0
  5. package/dist/audit/actions.d.ts.map +1 -0
  6. package/dist/audit/actions.js +47 -0
  7. package/dist/bounce/classify.d.ts +40 -0
  8. package/dist/bounce/classify.d.ts.map +1 -0
  9. package/dist/bounce/classify.js +48 -0
  10. package/dist/bounce/handler.d.ts +34 -0
  11. package/dist/bounce/handler.d.ts.map +1 -0
  12. package/dist/bounce/handler.js +83 -0
  13. package/dist/capability.d.ts +160 -0
  14. package/dist/capability.d.ts.map +1 -0
  15. package/dist/capability.js +257 -0
  16. package/dist/crypto/signingKey.d.ts +38 -0
  17. package/dist/crypto/signingKey.d.ts.map +1 -0
  18. package/dist/crypto/signingKey.js +38 -0
  19. package/dist/crypto/token.d.ts +49 -0
  20. package/dist/crypto/token.d.ts.map +1 -0
  21. package/dist/crypto/token.js +101 -0
  22. package/dist/data/emailEvent.d.ts +30 -0
  23. package/dist/data/emailEvent.d.ts.map +1 -0
  24. package/dist/data/emailEvent.js +24 -0
  25. package/dist/data/emailJob.d.ts +78 -0
  26. package/dist/data/emailJob.d.ts.map +1 -0
  27. package/dist/data/emailJob.js +64 -0
  28. package/dist/data/emailSuppression.d.ts +25 -0
  29. package/dist/data/emailSuppression.d.ts.map +1 -0
  30. package/dist/data/emailSuppression.js +23 -0
  31. package/dist/data/enums.d.ts +78 -0
  32. package/dist/data/enums.d.ts.map +1 -0
  33. package/dist/data/enums.js +67 -0
  34. package/dist/data/tables.d.ts +116 -0
  35. package/dist/data/tables.d.ts.map +1 -0
  36. package/dist/data/tables.js +32 -0
  37. package/dist/error/errors.d.ts +62 -0
  38. package/dist/error/errors.d.ts.map +1 -0
  39. package/dist/error/errors.js +84 -0
  40. package/dist/http/callbacks.d.ts +23 -0
  41. package/dist/http/callbacks.d.ts.map +1 -0
  42. package/dist/http/callbacks.js +112 -0
  43. package/dist/http/guards.d.ts +101 -0
  44. package/dist/http/guards.d.ts.map +1 -0
  45. package/dist/http/guards.js +146 -0
  46. package/dist/http/responses.d.ts +297 -0
  47. package/dist/http/responses.d.ts.map +1 -0
  48. package/dist/http/responses.js +117 -0
  49. package/dist/http/routes.d.ts +73 -0
  50. package/dist/http/routes.d.ts.map +1 -0
  51. package/dist/http/routes.js +294 -0
  52. package/dist/http/schemas.d.ts +92 -0
  53. package/dist/http/schemas.d.ts.map +1 -0
  54. package/dist/http/schemas.js +93 -0
  55. package/dist/http/view.d.ts +80 -0
  56. package/dist/http/view.d.ts.map +1 -0
  57. package/dist/http/view.js +132 -0
  58. package/dist/index.d.ts +48 -0
  59. package/dist/index.d.ts.map +1 -0
  60. package/dist/index.js +13 -0
  61. package/dist/jobs/read.d.ts +193 -0
  62. package/dist/jobs/read.d.ts.map +1 -0
  63. package/dist/jobs/read.js +145 -0
  64. package/dist/jobs/retry.d.ts +101 -0
  65. package/dist/jobs/retry.d.ts.map +1 -0
  66. package/dist/jobs/retry.js +69 -0
  67. package/dist/migrations/0001_init.d.ts +34 -0
  68. package/dist/migrations/0001_init.d.ts.map +1 -0
  69. package/dist/migrations/0001_init.js +62 -0
  70. package/dist/migrations/0001_suppressions.d.ts +12 -0
  71. package/dist/migrations/0001_suppressions.d.ts.map +1 -0
  72. package/dist/migrations/0001_suppressions.js +22 -0
  73. package/dist/provision/devDelivery.d.ts +34 -0
  74. package/dist/provision/devDelivery.d.ts.map +1 -0
  75. package/dist/provision/devDelivery.js +39 -0
  76. package/dist/provision/hostCatalogs.d.ts +12 -0
  77. package/dist/provision/hostCatalogs.d.ts.map +1 -0
  78. package/dist/provision/hostCatalogs.js +80 -0
  79. package/dist/provision/provisionEmail.d.ts +145 -0
  80. package/dist/provision/provisionEmail.d.ts.map +1 -0
  81. package/dist/provision/provisionEmail.js +120 -0
  82. package/dist/provision/resolveEmailConfig.d.ts +124 -0
  83. package/dist/provision/resolveEmailConfig.d.ts.map +1 -0
  84. package/dist/provision/resolveEmailConfig.js +126 -0
  85. package/dist/provision/settingsCheck.d.ts +42 -0
  86. package/dist/provision/settingsCheck.d.ts.map +1 -0
  87. package/dist/provision/settingsCheck.js +140 -0
  88. package/dist/send/batchIdentity.d.ts +44 -0
  89. package/dist/send/batchIdentity.d.ts.map +1 -0
  90. package/dist/send/batchIdentity.js +48 -0
  91. package/dist/send/enqueue.d.ts +218 -0
  92. package/dist/send/enqueue.d.ts.map +1 -0
  93. package/dist/send/enqueue.js +155 -0
  94. package/dist/send/errorMapping.d.ts +29 -0
  95. package/dist/send/errorMapping.d.ts.map +1 -0
  96. package/dist/send/errorMapping.js +54 -0
  97. package/dist/send/events.d.ts +17 -0
  98. package/dist/send/events.d.ts.map +1 -0
  99. package/dist/send/events.js +19 -0
  100. package/dist/send/fromComposition.d.ts +49 -0
  101. package/dist/send/fromComposition.d.ts.map +1 -0
  102. package/dist/send/fromComposition.js +55 -0
  103. package/dist/send/retryPolicy.d.ts +34 -0
  104. package/dist/send/retryPolicy.d.ts.map +1 -0
  105. package/dist/send/retryPolicy.js +41 -0
  106. package/dist/send/runSend.d.ts +108 -0
  107. package/dist/send/runSend.d.ts.map +1 -0
  108. package/dist/send/runSend.js +220 -0
  109. package/dist/send/sendAt.d.ts +8 -0
  110. package/dist/send/sendAt.d.ts.map +1 -0
  111. package/dist/send/sendAt.js +64 -0
  112. package/dist/send/sender.d.ts +44 -0
  113. package/dist/send/sender.d.ts.map +1 -0
  114. package/dist/send/sender.js +2 -0
  115. package/dist/send/senderBinding.d.ts +46 -0
  116. package/dist/send/senderBinding.d.ts.map +1 -0
  117. package/dist/send/senderBinding.js +23 -0
  118. package/dist/send/suppression.d.ts +95 -0
  119. package/dist/send/suppression.d.ts.map +1 -0
  120. package/dist/send/suppression.js +134 -0
  121. package/dist/templates/engine.d.ts +111 -0
  122. package/dist/templates/engine.d.ts.map +1 -0
  123. package/dist/templates/engine.js +260 -0
  124. package/dist/templates/messages.d.ts +96 -0
  125. package/dist/templates/messages.d.ts.map +1 -0
  126. package/dist/templates/messages.es.d.ts +31 -0
  127. package/dist/templates/messages.es.d.ts.map +1 -0
  128. package/dist/templates/messages.es.js +84 -0
  129. package/dist/templates/messages.js +268 -0
  130. package/dist/templates/partials.d.ts +7 -0
  131. package/dist/templates/partials.d.ts.map +1 -0
  132. package/dist/templates/partials.js +61 -0
  133. package/dist/templates/precompiled.generated.d.ts +268 -0
  134. package/dist/templates/precompiled.generated.d.ts.map +1 -0
  135. package/dist/templates/precompiled.generated.js +4041 -0
  136. package/dist/templates/registry.d.ts +120 -0
  137. package/dist/templates/registry.d.ts.map +1 -0
  138. package/dist/templates/registry.js +325 -0
  139. package/dist/templates/samples.d.ts +9 -0
  140. package/dist/templates/samples.d.ts.map +1 -0
  141. package/dist/templates/samples.js +100 -0
  142. package/dist/templates/severity.d.ts +42 -0
  143. package/dist/templates/severity.d.ts.map +1 -0
  144. package/dist/templates/severity.js +83 -0
  145. package/dist/templates/theme.d.ts +195 -0
  146. package/dist/templates/theme.d.ts.map +1 -0
  147. package/dist/templates/theme.js +170 -0
  148. package/dist/version.generated.d.ts +7 -0
  149. package/dist/version.generated.d.ts.map +1 -0
  150. package/dist/version.generated.js +9 -0
  151. package/dist/workflows/hostApp.d.ts +35 -0
  152. package/dist/workflows/hostApp.d.ts.map +1 -0
  153. package/dist/workflows/hostApp.js +21 -0
  154. package/dist/workflows/hostEnv.d.ts +131 -0
  155. package/dist/workflows/hostEnv.d.ts.map +1 -0
  156. package/dist/workflows/hostEnv.js +149 -0
  157. package/dist/workflows/instanceLiveness.d.ts +5 -0
  158. package/dist/workflows/instanceLiveness.d.ts.map +1 -0
  159. package/dist/workflows/instanceLiveness.js +39 -0
  160. package/dist/workflows/instances.d.ts +20 -0
  161. package/dist/workflows/instances.d.ts.map +1 -0
  162. package/dist/workflows/instances.js +2 -0
  163. package/dist/workflows/params.d.ts +25 -0
  164. package/dist/workflows/params.d.ts.map +1 -0
  165. package/dist/workflows/params.js +23 -0
  166. package/dist/workflows/scheduler.d.ts +84 -0
  167. package/dist/workflows/scheduler.d.ts.map +1 -0
  168. package/dist/workflows/scheduler.js +113 -0
  169. package/dist/workflows/sendBatch.d.ts +87 -0
  170. package/dist/workflows/sendBatch.d.ts.map +1 -0
  171. package/dist/workflows/sendBatch.js +52 -0
  172. package/dist/workflows/worker.d.ts +102 -0
  173. package/dist/workflows/worker.d.ts.map +1 -0
  174. package/dist/workflows/worker.js +119 -0
  175. package/package.json +22 -11
  176. package/src/version.generated.ts +1 -1
@@ -0,0 +1,102 @@
1
+ // SPDX-FileCopyrightText: 2026 Pithy
2
+ // SPDX-License-Identifier: MIT
3
+ import { WorkflowEntrypoint, type WorkflowEvent, type WorkflowStep } from "cloudflare:workers";
4
+ import type { D1Database, ExecutionContext } from "@cloudflare/workers-types";
5
+ import type { SecretsStoreEnv } from "@pithy-sh/secrets/src/env/bindings";
6
+ import type { SendWorkflowBinding } from "../send/enqueue";
7
+ import type { EmailSender } from "../send/sender";
8
+ import type { SendWorkflowInstances } from "./instances";
9
+ import { type BatchSendReport } from "./sendBatch";
10
+ /**
11
+ * The prebuilt email worker. `pithy add email` deploys one per environment (`pithy-email-staging`,
12
+ * `pithy-email-prod`); the user authors no code for it. It hosts:
13
+ *
14
+ * - `EmailSendWorkflow` — sends a batch of jobs durably (dispatch target for immediate sends and
15
+ * the scheduler's fan-out).
16
+ * - `EmailSchedulerWorkflow` — finds due jobs and fans them out into send batches.
17
+ * - `scheduled()` — the every-minute cron that fires the scheduler Workflow.
18
+ * - `fetch()` — the loopback dispatch door, served only in `dev` (see {@link createEmailHostApp}).
19
+ *
20
+ * The bodies (`runSendBatch`, `runScheduler`, `runSend`) are tested against Miniflare; these classes are
21
+ * the thin durable shells. This module imports `cloudflare:workers`, so it runs only in the Workers
22
+ * runtime (excluded from the node meta-test).
23
+ *
24
+ * **Every entry validates the env first** (pithy-sh/pithy#410). Fourteen settings arrived here from a
25
+ * provisioning run and none of them was checked: a missing `BASE_URL` became a magic link to
26
+ * `undefined/…`, an unparseable `EMAIL_THEME` threw inside a render step, and a `SCHEDULER_BATCH_SIZE`
27
+ * of `"fifty"` became `NaN` and the scheduler claimed nothing, forever, in silence. Now
28
+ * {@link emailHostEnv} is parsed before anything reads a value, the coercions and defaults live in
29
+ * that one schema rather than at each reader, and a host that cannot work says so in one block and
30
+ * refuses.
31
+ */
32
+ /**
33
+ * The email worker's env as the runtime hands it over — bindings as objects, every var as a string.
34
+ *
35
+ * The *shape the host runs on* is {@link EmailHostEnv}, which is this parsed: numbers as numbers, the
36
+ * theme as a validated `EmailTheme`, `SCHEDULER_ENABLED` as a boolean. This type stays because it is
37
+ * what a `WorkflowEntrypoint` is generic over and what the platform actually binds.
38
+ */
39
+ export interface EmailWorkerEnv extends SecretsStoreEnv {
40
+ /** The app database the per-environment jobs/events tables live in. */
41
+ DB: D1Database;
42
+ /** The shared, durable suppression database. */
43
+ EMAIL_SUPPRESSIONS: D1Database;
44
+ /** The Cloudflare Email Service send binding. */
45
+ EMAIL: EmailSender;
46
+ /** The send Workflow (self) — the scheduler creates batches against it, and asks after them. */
47
+ EMAIL_SENDER: SendWorkflowBinding & SendWorkflowInstances;
48
+ /** The scheduler Workflow (self) — fired by the cron. */
49
+ EMAIL_SCHEDULER: {
50
+ create(): Promise<unknown>;
51
+ };
52
+ /** The resolved brand theme as a JSON string (the full `EmailTheme`), set at provision from the app config. */
53
+ EMAIL_THEME?: string;
54
+ /**
55
+ * The project's catalogs arrive as one variable per locale — `EMAIL_MESSAGES_ES` and friends — read
56
+ * through `catalogsFromEnv`. Not declared here, because the names are the project's locales.
57
+ */
58
+ [catalogVar: `EMAIL_MESSAGES_${string}`]: unknown;
59
+ BASE_URL: string;
60
+ LINK_TTL_DAYS?: string;
61
+ MAX_ATTEMPTS?: string;
62
+ SCHEDULER_ENABLED?: string;
63
+ SCHEDULER_BATCH_SIZE?: string;
64
+ SCHEDULER_MAX_JOBS?: string;
65
+ SCHEDULER_GRACE_MS?: string;
66
+ SCHEDULER_STUCK_MS?: string;
67
+ }
68
+ /**
69
+ * Sends a batch of jobs durably. Started for immediate sends and by the scheduler's fan-out.
70
+ *
71
+ * Every step runs under {@link emailWorkflowRetry}, which agrees with `errorMapping.ts` by
72
+ * construction: a rate limit and a transient provider fault re-drive the job, and a template that does
73
+ * not exist, a payload that will not render, or a job row that is gone fail it at once. See
74
+ * `send/retryPolicy.ts`.
75
+ */
76
+ export declare class EmailSendWorkflow extends WorkflowEntrypoint<EmailWorkerEnv, {
77
+ jobIds: string[];
78
+ }> {
79
+ run(event: WorkflowEvent<{
80
+ jobIds: string[];
81
+ }>, step: WorkflowStep): Promise<BatchSendReport>;
82
+ }
83
+ /** Finds due jobs and fans them out into send batches. Fired by the every-minute cron. */
84
+ export declare class EmailSchedulerWorkflow extends WorkflowEntrypoint<EmailWorkerEnv, unknown> {
85
+ run(_event: WorkflowEvent<unknown>, step: WorkflowStep): Promise<void>;
86
+ }
87
+ declare const _default: {
88
+ /** Cron entry: fire the scheduler Workflow every minute, unless disabled. */
89
+ scheduled(_controller: unknown, env: EmailWorkerEnv): Promise<void>;
90
+ /**
91
+ * The loopback dispatch door — how a sibling worker under `pithy dev` starts a send batch on this
92
+ * host's own same-script Workflow binding (pithy-sh/pithy#410). Refused in every other environment,
93
+ * where the cross-script binding is the only path in.
94
+ *
95
+ * The env is validated before the router sees the request: a host that cannot work must not accept
96
+ * a dispatch and then lose it. The refusal is `core/internal` and the block is already in the log,
97
+ * which is what its action line points the operator at.
98
+ */
99
+ fetch(request: Request, env: EmailWorkerEnv, ctx: ExecutionContext): Promise<Response>;
100
+ };
101
+ export default _default;
102
+ //# sourceMappingURL=worker.d.ts.map
@@ -0,0 +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,UAAU,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAG9E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AAK1E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAE3D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAKlD,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAEzD,OAAO,EAAE,KAAK,eAAe,EAAoC,MAAM,aAAa,CAAC;AAErF;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH;;;;;;GAMG;AACH,MAAM,WAAW,cAAe,SAAQ,eAAe;IACrD,uEAAuE;IACvE,EAAE,EAAE,UAAU,CAAC;IACf,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;AAsED;;;;;;;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,6EAA6E;IACvE,SAAS,cAAc,OAAO,OAAO,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAMzE;;;;;;;;OAQG;IACG,KAAK,UAAU,OAAO,OAAO,cAAc,OAAO,gBAAgB,GAAG,OAAO,CAAC,QAAQ,CAAC"}
@@ -0,0 +1,119 @@
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 { mintBatchId } from "../send/batchIdentity.js";
7
+ import { emailHostEnv } from "./hostEnv.js";
8
+ import { emailWorkflowRetry } from "../send/retryPolicy.js";
9
+ import { createEmailHostApp } from "./hostApp.js";
10
+ import { isLiveInstanceStatus } from "./instanceLiveness.js";
11
+ import { runScheduler } from "./scheduler.js";
12
+ import { runSendBatch } from "./sendBatch.js";
13
+ import { configureSharedSecrets } from "@pithy-sh/secrets/src/sharedSecretsStore";
14
+ import { requireHostEnv } from "@pithy-sh/core/src/workflow/hostEnv";
15
+ import { WorkflowEntrypoint } from "cloudflare:workers";
16
+ import { NonRetryableError } from "cloudflare:workflows";
17
+ import { classifiedSteps } from "@pithy-sh/core/src/workflow/faults";
18
+ //#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
+ /**
23
+ * The env, parsed — or one legible block naming every unusable setting and what fills it, then a
24
+ * refusal. Called at the top of every entry, and the block is written once per env object.
25
+ */
26
+ function hostConfig(env) {
27
+ return requireHostEnv(emailHostEnv, env);
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
+ }
51
+ /** Assemble the scheduler dependencies, dispatching each batch as a send Workflow. */
52
+ function buildSchedulerDeps(env) {
53
+ const config = hostConfig(env);
54
+ return {
55
+ db: emailDatabase(env.DB),
56
+ now: /* @__PURE__ */ new Date(),
57
+ graceMs: config.SCHEDULER_GRACE_MS,
58
+ stuckMs: config.SCHEDULER_STUCK_MS,
59
+ batchSize: config.SCHEDULER_BATCH_SIZE,
60
+ maxJobs: config.SCHEDULER_MAX_JOBS,
61
+ newBatchId: mintBatchId,
62
+ batchIsAlive: async (batchId) => {
63
+ try {
64
+ const { status } = await (await env.EMAIL_SENDER.get(batchId)).status();
65
+ return isLiveInstanceStatus(status);
66
+ } catch {
67
+ return false;
68
+ }
69
+ },
70
+ dispatch: async (batchId, jobIds) => {
71
+ await env.EMAIL_SENDER.create({
72
+ id: batchId,
73
+ params: { jobIds }
74
+ });
75
+ }
76
+ };
77
+ }
78
+ /**
79
+ * Sends a batch of jobs durably. Started for immediate sends and by the scheduler's fan-out.
80
+ *
81
+ * Every step runs under {@link emailWorkflowRetry}, which agrees with `errorMapping.ts` by
82
+ * construction: a rate limit and a transient provider fault re-drive the job, and a template that does
83
+ * not exist, a payload that will not render, or a job row that is gone fail it at once. See
84
+ * `send/retryPolicy.ts`.
85
+ */
86
+ var EmailSendWorkflow = class extends WorkflowEntrypoint {
87
+ async run(event, step) {
88
+ return await runSendBatch(await buildSendDeps(this.env), classifiedSteps(step, emailWorkflowRetry, NonRetryableError), event.payload.jobIds);
89
+ }
90
+ };
91
+ /** Finds due jobs and fans them out into send batches. Fired by the every-minute cron. */
92
+ var EmailSchedulerWorkflow = class extends WorkflowEntrypoint {
93
+ async run(_event, step) {
94
+ await classifiedSteps(step, emailWorkflowRetry, NonRetryableError).do("dispatch-due", async () => {
95
+ await runScheduler(buildSchedulerDeps(this.env));
96
+ });
97
+ }
98
+ };
99
+ var worker_default = {
100
+ /** Cron entry: fire the scheduler Workflow every minute, unless disabled. */
101
+ async scheduled(_controller, env) {
102
+ if (hostConfig(env).SCHEDULER_ENABLED) await env.EMAIL_SCHEDULER.create();
103
+ },
104
+ /**
105
+ * The loopback dispatch door — how a sibling worker under `pithy dev` starts a send batch on this
106
+ * host's own same-script Workflow binding (pithy-sh/pithy#410). Refused in every other environment,
107
+ * where the cross-script binding is the only path in.
108
+ *
109
+ * The env is validated before the router sees the request: a host that cannot work must not accept
110
+ * a dispatch and then lose it. The refusal is `core/internal` and the block is already in the log,
111
+ * which is what its action line points the operator at.
112
+ */
113
+ async fetch(request, env, ctx) {
114
+ hostConfig(env);
115
+ return await app.fetch(request, env, ctx);
116
+ }
117
+ };
118
+ //#endregion
119
+ export { EmailSchedulerWorkflow, EmailSendWorkflow, worker_default as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pithy-sh/email",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -8,20 +8,25 @@
8
8
  "directory": "packages/email"
9
9
  },
10
10
  "files": [
11
+ "dist",
11
12
  "src",
12
13
  "pithy.manifest.json",
13
- "!src/**/*.test.*"
14
+ "!src/**/*.test.*",
15
+ "!dist/**/*.test.*"
14
16
  ],
15
17
  "type": "module",
16
18
  "engines": {
17
19
  "node": ">=22"
18
20
  },
19
21
  "exports": {
20
- "./src/*": "./src/*.ts"
22
+ "./src/*": {
23
+ "types": "./dist/*.d.ts",
24
+ "default": "./dist/*.js"
25
+ }
21
26
  },
22
27
  "scripts": {
23
28
  "precompile": "bun scripts/precompile.ts",
24
- "build": "bun run precompile && tsc -p tsconfig.json --noEmit false --outDir dist",
29
+ "build": "bun run precompile && tsdown && tsc -p tsconfig.build.json && bun ../../tooling/build/src/stampDeclarations.ts",
25
30
  "typecheck": "tsc -p tsconfig.json",
26
31
  "test": "bun run precompile && vitest run",
27
32
  "test:node": "bun run precompile && vitest run --project=node",
@@ -33,13 +38,10 @@
33
38
  "dependencies": {
34
39
  "@cloudflare/workers-types": "^5.20260729.1",
35
40
  "@hono/zod-validator": "^0.9.0",
36
- "@pithy-sh/core": "^0.1.2",
37
- "@pithy-sh/secrets": "^0.1.2",
41
+ "@pithy-sh/core": "^0.1.5",
42
+ "@pithy-sh/secrets": "^0.1.4",
38
43
  "handlebars": "^4.7.9",
39
- "hono": "^4.13.2",
40
- "kysely": "^0.29.0",
41
- "postal-mime": "^3.0.0",
42
- "zod": "^4.0.0"
44
+ "postal-mime": "^3.0.0"
43
45
  },
44
46
  "devDependencies": {
45
47
  "@cloudflare/vitest-plugin": "^1.0.0",
@@ -47,9 +49,18 @@
47
49
  "@pithy-sh/tsconfig": "workspace:*",
48
50
  "@types/node": "^22.15.0",
49
51
  "@vitest/coverage-v8": "^4.1.0",
52
+ "hono": "^4.13.2",
53
+ "kysely": "^0.29.0",
50
54
  "kysely-d1": "^0.4.0",
55
+ "tsdown": "^0.23.0",
51
56
  "typescript": "^7.0.2",
52
57
  "vitest": "^4.1.0",
53
- "wrangler": "^4.115.0"
58
+ "wrangler": "^4.115.0",
59
+ "zod": "^4.4.0"
60
+ },
61
+ "peerDependencies": {
62
+ "hono": "^4.13.2",
63
+ "kysely": "^0.29.0",
64
+ "zod": "^4.4.0"
54
65
  }
55
66
  }
@@ -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.1.2";
16
+ export const PACKAGE_VERSION = "0.1.4";