@fabrika/email 0.9.0-dev.7 → 0.9.0-dev.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/email",
3
- "version": "0.9.0-dev.7",
3
+ "version": "0.9.0-dev.9",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
package/src/platform.ts CHANGED
@@ -7,7 +7,13 @@ import {
7
7
  type EmailSenderCapabilities,
8
8
  } from './email.js'
9
9
  import { parseTimeout, postToProvider } from './http.js'
10
- import { encodePlatformMailRequest, parsePlatformMailAnswer, PLATFORM_MAIL_CAPABILITIES, PLATFORM_MAIL_SEND_PATH } from './platform-wire.js'
10
+ import {
11
+ encodePlatformMailRequest,
12
+ installationMailKey,
13
+ parsePlatformMailAnswer,
14
+ PLATFORM_MAIL_CAPABILITIES,
15
+ PLATFORM_MAIL_SEND_PATH,
16
+ } from './platform-wire.js'
11
17
  import { isVisibleAscii, validateEmailAttachments, validateEmailBroadcast, validateEmailMessage } from './validation.js'
12
18
 
13
19
  export {
@@ -99,8 +105,48 @@ export class PlatformEmailSender implements EmailSender {
99
105
  }
100
106
  }
101
107
 
102
- /** `http(s)://host[:port]` exactly as written: no path, query, fragment or credentials. */
103
- function isHttpOrigin(value: string): boolean {
108
+ export interface InstallationMailSenderOptions {
109
+ /** Control's http(s) origin; the send path is appended to it. */
110
+ readonly url: string
111
+ /** The secret the service shares with Control, which its key derives from (`installationMailKey`). */
112
+ readonly secret: string
113
+ readonly fetch?: EmailFetch
114
+ readonly timeoutMs?: number
115
+ }
116
+
117
+ /**
118
+ * An installation service's sender to Control's relay: a `PlatformEmailSender` under `installationMailKey(secret)`,
119
+ * built at the first send because deriving the key is asynchronous. A send that cannot build it (a URL that is not an
120
+ * origin, a secret that is not usable) throws and keeps nothing, so the next send builds it again.
121
+ */
122
+ export function installationMailSender(options: InstallationMailSenderOptions): EmailSender {
123
+ let sender: Promise<PlatformEmailSender> | undefined
124
+ return {
125
+ capabilities: PLATFORM_MAIL_CAPABILITIES,
126
+ async send(message, signal) {
127
+ const pending = sender ??= installationMailKey(options.secret).then((key) =>
128
+ new PlatformEmailSender({
129
+ url: options.url,
130
+ key,
131
+ ...(options.fetch === undefined ? {} : { fetch: options.fetch }),
132
+ ...(options.timeoutMs === undefined ? {} : { timeoutMs: options.timeoutMs }),
133
+ })
134
+ )
135
+ const ready = await pending.catch((error: unknown) => {
136
+ // Only the build that failed is dropped; a send that started a newer one keeps it.
137
+ if (sender === pending) sender = undefined
138
+ throw error
139
+ })
140
+ return ready.send(message, signal)
141
+ },
142
+ }
143
+ }
144
+
145
+ /**
146
+ * `http(s)://host[:port]` exactly as written: no path, query, fragment or credentials. The one check of a relay's origin,
147
+ * for the sender, IAM's boot and IAM's Cloudflare deploy (which also requires `https:`).
148
+ */
149
+ export function isHttpOrigin(value: string): boolean {
104
150
  try {
105
151
  const url = new URL(value)
106
152
  return (url.protocol === 'http:' || url.protocol === 'https:') && url.origin === value
@@ -17,3 +17,22 @@ export declare class PlatformEmailSender implements EmailSender {
17
17
  constructor(options: PlatformEmailSenderOptions);
18
18
  send(message: EmailMessage, signal: AbortSignal): Promise<EmailDeliveryResult>;
19
19
  }
20
+ export interface InstallationMailSenderOptions {
21
+ /** Control's http(s) origin; the send path is appended to it. */
22
+ readonly url: string;
23
+ /** The secret the service shares with Control, which its key derives from (`installationMailKey`). */
24
+ readonly secret: string;
25
+ readonly fetch?: EmailFetch;
26
+ readonly timeoutMs?: number;
27
+ }
28
+ /**
29
+ * An installation service's sender to Control's relay: a `PlatformEmailSender` under `installationMailKey(secret)`,
30
+ * built at the first send because deriving the key is asynchronous. A send that cannot build it (a URL that is not an
31
+ * origin, a secret that is not usable) throws and keeps nothing, so the next send builds it again.
32
+ */
33
+ export declare function installationMailSender(options: InstallationMailSenderOptions): EmailSender;
34
+ /**
35
+ * `http(s)://host[:port]` exactly as written: no path, query, fragment or credentials. The one check of a relay's origin,
36
+ * for the sender, IAM's boot and IAM's Cloudflare deploy (which also requires `https:`).
37
+ */
38
+ export declare function isHttpOrigin(value: string): boolean;