@hamur/mail 0.0.0-stage → 0.0.2
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/LICENSE +22 -0
- package/README.md +14 -2
- package/dist/src/devtools.js +268 -0
- package/dist/src/index.js +573 -0
- package/dist/src/server/index.js +916 -0
- package/dist/src/test.js +39 -0
- package/dist/types/src/address.d.ts +14 -0
- package/dist/types/src/build/devtools.d.ts +20 -0
- package/dist/types/src/build/outbox.d.ts +35 -0
- package/dist/types/src/build/setup.d.ts +22 -0
- package/dist/types/src/build/templates.d.ts +12 -0
- package/dist/types/src/devtools-model.d.ts +12 -0
- package/dist/types/src/devtools.d.ts +2 -0
- package/dist/types/src/html.d.ts +10 -0
- package/dist/types/src/index.d.ts +24 -0
- package/dist/types/src/mime.d.ts +32 -0
- package/dist/types/src/module.d.ts +3 -0
- package/dist/types/src/options.d.ts +12 -0
- package/dist/types/src/server/deliver.d.ts +15 -0
- package/dist/types/src/server/devtools-routes.d.ts +1 -0
- package/dist/types/src/server/door-dev.d.ts +3 -0
- package/dist/types/src/server/door.d.ts +2 -0
- package/dist/types/src/server/hooks.d.ts +5 -0
- package/dist/types/src/server/index.d.ts +6 -0
- package/dist/types/src/server/message.d.ts +3 -0
- package/dist/types/src/server/plugin.d.ts +1 -0
- package/dist/types/src/server/preview.d.ts +8 -0
- package/dist/types/src/server/queue.d.ts +19 -0
- package/dist/types/src/server/runtime.d.ts +5 -0
- package/dist/types/src/server/send.d.ts +54 -0
- package/dist/types/src/server/task.d.ts +4 -0
- package/dist/types/src/server/template.d.ts +8 -0
- package/dist/types/src/smtp/client.d.ts +28 -0
- package/dist/types/src/smtp/dkim.d.ts +8 -0
- package/dist/types/src/smtp/sockets.d.ts +42 -0
- package/dist/types/src/test.d.ts +27 -0
- package/dist/types/src/transport.d.ts +28 -0
- package/dist/types/src/transports/cloudflare.d.ts +17 -0
- package/dist/types/src/transports/http.d.ts +26 -0
- package/dist/types/src/transports/mailgun.d.ts +9 -0
- package/dist/types/src/transports/outbox.d.ts +9 -0
- package/dist/types/src/transports/postmark.d.ts +8 -0
- package/dist/types/src/transports/resend.d.ts +7 -0
- package/dist/types/src/transports/sendgrid.d.ts +7 -0
- package/dist/types/src/transports/ses.d.ts +9 -0
- package/dist/types/src/transports/smtp.d.ts +3 -0
- package/dist/types/src/types.d.ts +124 -0
- package/package.json +48 -4
- package/src/address.ts +86 -0
- package/src/build/devtools.ts +81 -0
- package/src/build/outbox.ts +69 -0
- package/src/build/setup.ts +226 -0
- package/src/build/templates.ts +113 -0
- package/src/devtools-model.ts +24 -0
- package/src/devtools.ts +351 -0
- package/src/env.d.ts +36 -0
- package/src/html.ts +274 -0
- package/src/index.ts +25 -0
- package/src/mime.ts +220 -0
- package/src/module.ts +11 -0
- package/src/options.ts +157 -0
- package/src/server/deliver.ts +54 -0
- package/src/server/devtools-routes.ts +32 -0
- package/src/server/door-dev.ts +6 -0
- package/src/server/door.ts +4 -0
- package/src/server/hooks.ts +13 -0
- package/src/server/index.ts +28 -0
- package/src/server/message.ts +36 -0
- package/src/server/plugin.ts +4 -0
- package/src/server/preview.ts +41 -0
- package/src/server/queue.ts +58 -0
- package/src/server/runtime.ts +45 -0
- package/src/server/send.ts +308 -0
- package/src/server/task.ts +13 -0
- package/src/server/template.ts +37 -0
- package/src/smtp/client.ts +305 -0
- package/src/smtp/dkim.ts +72 -0
- package/src/smtp/sockets.ts +296 -0
- package/src/test.ts +70 -0
- package/src/transport.ts +35 -0
- package/src/transports/cloudflare.ts +68 -0
- package/src/transports/http.ts +75 -0
- package/src/transports/mailgun.ts +31 -0
- package/src/transports/outbox.ts +64 -0
- package/src/transports/postmark.ts +59 -0
- package/src/transports/resend.ts +46 -0
- package/src/transports/sendgrid.ts +64 -0
- package/src/transports/ses.ts +53 -0
- package/src/transports/smtp.ts +37 -0
- package/src/types.ts +104 -0
package/dist/src/test.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/mail/src/test.ts
|
|
3
|
+
import { readdir, readFile, rm } from "fs/promises";
|
|
4
|
+
import { dirname, join, resolve } from "path";
|
|
5
|
+
import { useTestContext } from "hamur/test";
|
|
6
|
+
import { useTestLoader } from "hamur/test/internal";
|
|
7
|
+
async function outboxDir(options) {
|
|
8
|
+
if (options?.dir !== undefined)
|
|
9
|
+
return resolve(options.dir);
|
|
10
|
+
try {
|
|
11
|
+
return join(dirname(useTestContext().outputDir), ".hamur", "mail");
|
|
12
|
+
} catch {}
|
|
13
|
+
try {
|
|
14
|
+
return join((await useTestLoader()).hamur.options.buildDir, "mail");
|
|
15
|
+
} catch (error) {
|
|
16
|
+
throw new Error("@hamur/mail/test: no outbox to read; call setup() from hamur/test in this file or run the tests with the hamur/test/preload", { cause: error });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
async function names(dir) {
|
|
20
|
+
try {
|
|
21
|
+
return await readdir(dir);
|
|
22
|
+
} catch (error) {
|
|
23
|
+
if (error.code === "ENOENT")
|
|
24
|
+
return [];
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
async function sentMail(options) {
|
|
29
|
+
const dir = await outboxDir(options);
|
|
30
|
+
return Promise.all((await names(dir)).filter((name) => name.endsWith(".json")).sort().map(async (name) => JSON.parse(await readFile(join(dir, name), "utf8"))));
|
|
31
|
+
}
|
|
32
|
+
async function clearSentMail(options) {
|
|
33
|
+
const dir = await outboxDir(options);
|
|
34
|
+
await Promise.all((await names(dir)).filter((name) => /\.(?:eml|json)$/.test(name)).map((name) => rm(join(dir, name), { force: true })));
|
|
35
|
+
}
|
|
36
|
+
export {
|
|
37
|
+
clearSentMail,
|
|
38
|
+
sentMail
|
|
39
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ParsedAddress } from "./types.ts";
|
|
2
|
+
/** A domain in its IDNA form, as `URL` writes a host name; undefined when it is not one. */
|
|
3
|
+
export declare function asciiDomain(domain: string): string | undefined;
|
|
4
|
+
/** `"Ada <ada@x.dev>"`, `"ada@x.dev"` or `{ name?, address }`; undefined for anything else, a CR or LF included. */
|
|
5
|
+
export declare function parseAddress(value: unknown): ParsedAddress | undefined;
|
|
6
|
+
/** An address list as given: one address or an array of them. */
|
|
7
|
+
export declare function addressList(value: unknown): unknown[];
|
|
8
|
+
/** True when the local part needs SMTPUTF8. */
|
|
9
|
+
export declare function needsSmtpUtf8(address: ParsedAddress): boolean;
|
|
10
|
+
/** RFC 5322 phrase for a display name: atoms as they are, other ASCII quoted, the rest left to the encoder. */
|
|
11
|
+
export declare function quotedName(name: string): string;
|
|
12
|
+
export declare function isAscii(text: string): boolean;
|
|
13
|
+
/** `Ada <ada@x.dev>` for logs and the outbox record; no encoding. */
|
|
14
|
+
export declare function shownAddress(address: ParsedAddress): string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type HamurContext } from "hamur/kit";
|
|
2
|
+
import type { ResolvedMailTransport } from "../types.ts";
|
|
3
|
+
import { type MailSummary } from "./outbox.ts";
|
|
4
|
+
declare module "hamur" {
|
|
5
|
+
interface HamurDevEvents {
|
|
6
|
+
/** The outbox changed; `messages` is the whole list, newest first. */
|
|
7
|
+
"devtools:mail": {
|
|
8
|
+
messages: MailSummary[];
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export interface MailListAnswer {
|
|
13
|
+
transport: string;
|
|
14
|
+
messages: MailSummary[];
|
|
15
|
+
}
|
|
16
|
+
/** The `mail` tab, its RPCs and the `devtools:mail` push; `hamur dev` only has the RPCs and the push. */
|
|
17
|
+
export declare function setupMailDevtools(hamur: HamurContext, options: {
|
|
18
|
+
transport: ResolvedMailTransport | undefined;
|
|
19
|
+
templates: () => string[];
|
|
20
|
+
}): void;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** The `.json` the outbox writes next to each `.eml`. */
|
|
2
|
+
export interface OutboxRecord {
|
|
3
|
+
id: string;
|
|
4
|
+
date: string;
|
|
5
|
+
from: string;
|
|
6
|
+
to: string[];
|
|
7
|
+
cc: string[];
|
|
8
|
+
bcc: string[];
|
|
9
|
+
subject: string;
|
|
10
|
+
template?: string;
|
|
11
|
+
html: string;
|
|
12
|
+
text: string;
|
|
13
|
+
headers: Record<string, string>;
|
|
14
|
+
attachments: Array<{
|
|
15
|
+
filename: string;
|
|
16
|
+
contentType: string;
|
|
17
|
+
size: number;
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
/** What the `mail` tab's list and the `devtools:mail` push carry of one message; `name` is the file stem. */
|
|
21
|
+
export interface MailSummary {
|
|
22
|
+
name: string;
|
|
23
|
+
date: string;
|
|
24
|
+
from: string;
|
|
25
|
+
to: string[];
|
|
26
|
+
subject: string;
|
|
27
|
+
template?: string;
|
|
28
|
+
}
|
|
29
|
+
/** The outbox newest first. */
|
|
30
|
+
export declare function listOutbox(dir: string): Promise<MailSummary[]>;
|
|
31
|
+
/** One message; `name` must be a stem the directory lists, so nothing outside the outbox is read. */
|
|
32
|
+
export declare function readOutbox(dir: string, name: string): Promise<{
|
|
33
|
+
record: OutboxRecord;
|
|
34
|
+
eml: string;
|
|
35
|
+
} | undefined>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type HamurContext } from "hamur/kit";
|
|
2
|
+
import type { ResolvedMailOptions } from "../options.ts";
|
|
3
|
+
import type { MailDkim, ResolvedMailTransport } from "../types.ts";
|
|
4
|
+
/** The `send_email[].name` entries of the app's wrangler config, with `cloudflare.env`'s block over the top level as the preset writes it. */
|
|
5
|
+
export declare function declaredSendEmailBindings(rootDir: string, env: string | undefined): Promise<string[]>;
|
|
6
|
+
export interface TransportRun {
|
|
7
|
+
dev: boolean;
|
|
8
|
+
/** `NODE_ENV` is `test`, as for the config's `$test` block: `hamur/test` and `bun test`. */
|
|
9
|
+
test: boolean;
|
|
10
|
+
/** The build is made by `hamur/test`'s `setup` or the test preload (`HAMUR_TEST_BUILD`): no configured transport is used, so a test never sends. */
|
|
11
|
+
setup?: boolean;
|
|
12
|
+
rootDir: string;
|
|
13
|
+
buildDir: string;
|
|
14
|
+
}
|
|
15
|
+
/** The configured transport, `outbox` under `hamur dev` and tests when none is set, the outbox `dir` made absolute. */
|
|
16
|
+
export declare function chooseTransport(configured: ResolvedMailOptions["transport"], run: TransportRun): ResolvedMailTransport | undefined;
|
|
17
|
+
/** The build errors and warning of a build that is neither `hamur dev` nor a test. */
|
|
18
|
+
export declare function checkBuild(transport: ResolvedMailTransport | undefined, preset: string, siteUrl: string | undefined, sendEmailBindings?: readonly string[]): string | undefined;
|
|
19
|
+
/** The warning of a build that sets `mail.dkim` and does not send through smtp. */
|
|
20
|
+
export declare function dkimWarning(dkim: MailDkim | undefined, transport: ResolvedMailTransport | undefined): string | undefined;
|
|
21
|
+
/** The transport per run, the build checks, the import guard, the server door and `#hamur/mail/config`. */
|
|
22
|
+
export declare function setupMail(options: ResolvedMailOptions, hamur: HamurContext): void;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface MailTemplateFile {
|
|
2
|
+
name: string;
|
|
3
|
+
file: string;
|
|
4
|
+
}
|
|
5
|
+
/** The templates of the app's `mail/` folder, then each layer's in order; the first of a name wins. A script without a default export is a helper. */
|
|
6
|
+
export declare function scanTemplates(srcDirs: string[], extensions: string[]): MailTemplateFile[];
|
|
7
|
+
/** The warning of each template with a `<style>` block. */
|
|
8
|
+
export declare function styleWarnings(rootDir: string, templates: MailTemplateFile[]): string[];
|
|
9
|
+
/** `#hamur/mail/templates`: per name, a lazy import of the server build, and the adapter's `renderComponent` when there is a template. */
|
|
10
|
+
export declare function templatesModule(templates: MailTemplateFile[], serverEntry: () => string): string;
|
|
11
|
+
/** `.hamur/types/mail.d.ts`, each template's props in `MailTemplates`. */
|
|
12
|
+
export declare function templateTypes(templates: MailTemplateFile[], typesDir: string, library: string): string;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** What a mail's document may load in the `mail` tab's frame. */
|
|
2
|
+
export declare const MAIL_CSP = "default-src 'none'; img-src * data:; style-src 'unsafe-inline'";
|
|
3
|
+
/** The mail's document inside one that starts with the policy, so no markup in the mail comes before it. */
|
|
4
|
+
export declare function framedDocument(html: string): string;
|
|
5
|
+
/** The props box's text as an object, or why it is not one. */
|
|
6
|
+
export declare function parseProps(text: string): {
|
|
7
|
+
props: Record<string, unknown>;
|
|
8
|
+
} | {
|
|
9
|
+
error: string;
|
|
10
|
+
};
|
|
11
|
+
/** The file name a download of the outbox message `name` gets. */
|
|
12
|
+
export declare const emlFileName: (name: string) => string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface MailSite {
|
|
2
|
+
/** `site.url`. */
|
|
3
|
+
origin: string;
|
|
4
|
+
/** `app.baseURL`. */
|
|
5
|
+
baseURL: string;
|
|
6
|
+
}
|
|
7
|
+
/** What a mail keeps of a server render: no script, hydration marker or preload, and app paths in `href` and `src` made absolute. */
|
|
8
|
+
export declare function mailHtml(html: string, site: MailSite): string;
|
|
9
|
+
/** The text part of a mail that sets no `text`. */
|
|
10
|
+
export declare function htmlToText(html: string): string;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { HamurEvent } from "hamur/server";
|
|
2
|
+
import type { MailConfig, MailMessage, MailResult } from "./types.ts";
|
|
3
|
+
export { default } from "./module.ts";
|
|
4
|
+
export type { MailAddress, MailConfig, MailMessage, MailResult } from "./types.ts";
|
|
5
|
+
/** Filled in by typegen from `app/mail/`: each template's name and its props. */
|
|
6
|
+
export interface MailTemplates {
|
|
7
|
+
}
|
|
8
|
+
declare module "hamur" {
|
|
9
|
+
interface HamurModuleOptions {
|
|
10
|
+
mail?: MailConfig;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
declare module "hamur/server" {
|
|
14
|
+
interface ServerHooks {
|
|
15
|
+
/** Before the MIME message is written, with the rendered message; a hook may change it (headers, `bcc`), and the checks run again. */
|
|
16
|
+
"mail:beforeSend": (message: MailMessage, ctx: {
|
|
17
|
+
event?: HamurEvent;
|
|
18
|
+
}) => void | Promise<void>;
|
|
19
|
+
/** After the transport took the message. */
|
|
20
|
+
"mail:sent": (result: MailResult, message: MailMessage) => void | Promise<void>;
|
|
21
|
+
/** The transport failed; `error` is what `sendMail` rejects with. To report it, not to send again: a queue may do so too. */
|
|
22
|
+
"mail:error": (error: Error, message: MailMessage) => void | Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ParsedAddress } from "./types.ts";
|
|
2
|
+
export interface MimeAttachment {
|
|
3
|
+
filename: string;
|
|
4
|
+
content: Uint8Array;
|
|
5
|
+
contentType: string;
|
|
6
|
+
cid?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface MimeInput {
|
|
9
|
+
from: ParsedAddress;
|
|
10
|
+
to: ParsedAddress[];
|
|
11
|
+
cc?: ParsedAddress[];
|
|
12
|
+
replyTo?: ParsedAddress;
|
|
13
|
+
subject: string;
|
|
14
|
+
date: Date;
|
|
15
|
+
/** Without `<>`. */
|
|
16
|
+
messageId: string;
|
|
17
|
+
html?: string;
|
|
18
|
+
text?: string;
|
|
19
|
+
attachments?: MimeAttachment[];
|
|
20
|
+
/** Headers of the app, checked by `sendMail`. */
|
|
21
|
+
headers?: Record<string, string>;
|
|
22
|
+
}
|
|
23
|
+
/** Base64 in lines of 76 characters. */
|
|
24
|
+
export declare function base64Lines(bytes: Uint8Array): string;
|
|
25
|
+
/** RFC 2045 §6.7: UTF-8 quoted-printable, CRLF hard breaks, soft breaks keeping each line within 76 characters. */
|
|
26
|
+
export declare function quotedPrintable(text: string): string;
|
|
27
|
+
/** RFC 2047 `B` encoded words of at most 45 bytes each, never splitting a character. */
|
|
28
|
+
export declare function encodedWords(text: string): string[];
|
|
29
|
+
/** RFC 5322 §3.3: `Thu, 03 Dec 2026 10:00:00 +0000`. */
|
|
30
|
+
export declare function mailDate(date: Date): string;
|
|
31
|
+
/** `multipart/alternative` of the text and the HTML, inside `multipart/related` for `cid` attachments, inside `multipart/mixed` for the others. */
|
|
32
|
+
export declare function writeMime(input: MimeInput): string;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { MailDkim, MailTransport, ParsedAddress } from "./types.ts";
|
|
2
|
+
export declare const TRANSPORTS: readonly ["smtp", "resend", "postmark", "sendgrid", "mailgun", "ses", "cloudflare", "outbox"];
|
|
3
|
+
export interface ResolvedMailOptions {
|
|
4
|
+
from: ParsedAddress;
|
|
5
|
+
replyTo?: ParsedAddress;
|
|
6
|
+
/** The configured transport; undefined leaves it to `hamur dev` and tests. */
|
|
7
|
+
transport?: MailTransport;
|
|
8
|
+
queue: boolean;
|
|
9
|
+
dkim?: MailDkim;
|
|
10
|
+
}
|
|
11
|
+
/** The checked options, or the load error of the first value that is wrong. */
|
|
12
|
+
export declare function resolveOptions(input?: unknown): ResolvedMailOptions;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Logger } from "hamur/server";
|
|
2
|
+
import type { MailMessage } from "../types.ts";
|
|
3
|
+
export interface MailDelivery {
|
|
4
|
+
/** The same on every attempt: the left side of the Message-ID. */
|
|
5
|
+
id: string;
|
|
6
|
+
/** The site URL links are absolute on; default the app's. */
|
|
7
|
+
site?: string;
|
|
8
|
+
/** Whether the caller is a queued run: what `mail:sent` reports. */
|
|
9
|
+
queued: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A message sent in the call by the rule of the queue, for a delivery of another package. It rejects only when no
|
|
13
|
+
* recipient can have the message; a failure after which one may have it is what it resolves to. No error holds an address.
|
|
14
|
+
*/
|
|
15
|
+
export declare function deliverMail(message: MailMessage, { id, site, queued }: MailDelivery, log: Pick<Logger, "error">): Promise<Error | undefined>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const devtoolsMailRender: import("hamur/server").Handler<unknown, Response, unknown, Response, {}>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Hooks } from "hamur/kit";
|
|
2
|
+
import type { ServerHooks } from "hamur/server";
|
|
3
|
+
/** The door's plugin hands over the server's hooks at start, before any request. */
|
|
4
|
+
export declare function setServerHooks(hooks: Hooks<ServerHooks>): void;
|
|
5
|
+
export declare function serverHooks(): Hooks<ServerHooks> | undefined;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { MailMessage, MailResult, MailTemplateName } from "../types.ts";
|
|
2
|
+
/** Checks the message, runs `mail:beforeSend`, writes the MIME message and hands it to `mail.transport` or the queue. */
|
|
3
|
+
export declare function sendMail<T extends MailTemplateName>(message: MailMessage<T>, options?: {
|
|
4
|
+
queue?: boolean;
|
|
5
|
+
delay?: number;
|
|
6
|
+
}): Promise<MailResult>;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { SendMailDeps } from "./send.ts";
|
|
2
|
+
/** What checking, rendering and sending a message takes in this app; `site` replaces the site URL of the request it is called in. */
|
|
3
|
+
export declare const messageDeps: (site?: string) => Omit<SendMailDeps, "queue" | "enqueue">;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const mailPlugin: import("hamur/server").ServerPlugin;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface PreviewDeps {
|
|
2
|
+
/** The names of the `app/mail/` templates. */
|
|
3
|
+
templates: readonly string[];
|
|
4
|
+
/** The HTML document of a template, as `sendMail` renders it. */
|
|
5
|
+
render(template: string, props: Record<string, unknown>, request: Request): Promise<string>;
|
|
6
|
+
}
|
|
7
|
+
/** `POST api/mail/render` with `{ template, props? }`: the template's document and its text part; nothing is sent. */
|
|
8
|
+
export declare function answerPreview(request: Request, deps: PreviewDeps): Promise<Response>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Logger } from "hamur/server";
|
|
2
|
+
import { type OutgoingMail } from "../transport.ts";
|
|
3
|
+
import type { RenderedMail } from "../types.ts";
|
|
4
|
+
import { type TransmitDeps } from "./send.ts";
|
|
5
|
+
/** The payload of `mail:send`: the message as rendered. The run writes its MIME text, which would hold each body a second time. */
|
|
6
|
+
export interface QueuedMail {
|
|
7
|
+
id: string;
|
|
8
|
+
date: Date;
|
|
9
|
+
message: RenderedMail;
|
|
10
|
+
}
|
|
11
|
+
export declare function packMail({ id, date, message }: OutgoingMail): QueuedMail;
|
|
12
|
+
export declare function unpackMail({ id, date, message }: QueuedMail): OutgoingMail;
|
|
13
|
+
/**
|
|
14
|
+
* One attempt. It rejects only when no recipient can have the message; after an `UnconfirmedError` it resolves to the
|
|
15
|
+
* failure, and a hook that throws is logged. `queued` is what `mail:sent` reports.
|
|
16
|
+
*/
|
|
17
|
+
export declare function sendOnce(deps: TransmitDeps, mail: OutgoingMail, log: Pick<Logger, "error">, queued: boolean): Promise<Error | undefined>;
|
|
18
|
+
/** The run of `mail:send`: it rejects, so that the queue runs it again, when `sendOnce` does. */
|
|
19
|
+
export declare function sendQueued(deps: TransmitDeps, mail: OutgoingMail, log: Pick<Logger, "error">): Promise<void>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type HamurEvent } from "hamur/server";
|
|
2
|
+
import type { TransmitDeps } from "./send.ts";
|
|
3
|
+
export declare function siteOrigin(event: HamurEvent | undefined): string | undefined;
|
|
4
|
+
/** What a send in the call and the task `mail:send` share. */
|
|
5
|
+
export declare const transmitDeps: TransmitDeps;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Hooks } from "hamur/kit";
|
|
2
|
+
import type { HamurEvent, ServerHooks } from "hamur/server";
|
|
3
|
+
import type { MailSender, OutgoingMail } from "../transport.ts";
|
|
4
|
+
import type { MailMessage, MailResult, ParsedAddress, RenderedMail } from "../types.ts";
|
|
5
|
+
type Span = <T>(name: string, fn: () => Promise<T>, options: {
|
|
6
|
+
kind: "client";
|
|
7
|
+
attributes: Record<string, string>;
|
|
8
|
+
}) => Promise<T>;
|
|
9
|
+
/** What handing one message to the transport takes. */
|
|
10
|
+
export interface TransmitDeps {
|
|
11
|
+
/** `mail.transport.type` of the run, for messages and the span. */
|
|
12
|
+
transport: string;
|
|
13
|
+
open: () => Promise<MailSender>;
|
|
14
|
+
hooks: () => Hooks<ServerHooks> | undefined;
|
|
15
|
+
span: Span;
|
|
16
|
+
/** Called after each send; the server logs the dev line from it. */
|
|
17
|
+
sent?: (info: {
|
|
18
|
+
label: string;
|
|
19
|
+
to: ParsedAddress[];
|
|
20
|
+
event: HamurEvent | undefined;
|
|
21
|
+
}) => void;
|
|
22
|
+
}
|
|
23
|
+
export interface SendMailDeps extends TransmitDeps {
|
|
24
|
+
from: ParsedAddress;
|
|
25
|
+
replyTo?: ParsedAddress;
|
|
26
|
+
/** The names of the `app/mail/` templates. */
|
|
27
|
+
templates: readonly string[];
|
|
28
|
+
/** The HTML document of a template. */
|
|
29
|
+
render: (template: string, props: Record<string, unknown>, event: HamurEvent | undefined) => Promise<string>;
|
|
30
|
+
event: () => HamurEvent | undefined;
|
|
31
|
+
/** The host of `site.url`, the right side of each Message-ID. */
|
|
32
|
+
host: (event: HamurEvent | undefined) => string;
|
|
33
|
+
/** The left side of the Message-ID; default a random UUID. */
|
|
34
|
+
id?: string;
|
|
35
|
+
/** `mail.queue`. */
|
|
36
|
+
queue: boolean;
|
|
37
|
+
/** Hands the message to the task `mail:send`; resolves when the queue took it. */
|
|
38
|
+
enqueue: (mail: OutgoingMail, delay: number | undefined) => Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
/** `message` with its MIME text and envelope. */
|
|
41
|
+
export declare function outgoingMail(id: string, date: Date, message: RenderedMail): OutgoingMail;
|
|
42
|
+
/**
|
|
43
|
+
* Hands `mail` to the transport in the span `mail.send`. A rejection is safe for a span, which records the message and stack of
|
|
44
|
+
* what it sees thrown: it names the template and the transport, and the transport's text, which may hold an address, is its `cause`.
|
|
45
|
+
*/
|
|
46
|
+
export declare function transmit(deps: TransmitDeps, mail: OutgoingMail): Promise<void>;
|
|
47
|
+
/** The failure an app sees for a rejection of `transmit`: with the transport's text. */
|
|
48
|
+
export declare function mailFailure(recorded: unknown): Error;
|
|
49
|
+
/** `sendMail` over a transport, the hooks and the span. */
|
|
50
|
+
export declare function createSendMail(deps: SendMailDeps): (message: MailMessage, options?: {
|
|
51
|
+
queue?: boolean;
|
|
52
|
+
delay?: number;
|
|
53
|
+
}) => Promise<MailResult>;
|
|
54
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type AdapterServerEntry } from "hamur/adapter";
|
|
2
|
+
import { type MailSite } from "../html.ts";
|
|
3
|
+
export interface TemplateSite extends MailSite {
|
|
4
|
+
/** The document's language: `i18n.defaultLocale`, else `en`. */
|
|
5
|
+
lang: string;
|
|
6
|
+
}
|
|
7
|
+
/** `component` rendered by the adapter as a root of its own, outside any request, in the mail's document. */
|
|
8
|
+
export declare function renderTemplate(renderComponent: NonNullable<AdapterServerEntry["renderComponent"]>, component: unknown, props: Record<string, unknown>, site: TemplateSite): Promise<string>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type MailSender } from "../transport.ts";
|
|
2
|
+
import type { SmtpConnect } from "./sockets.ts";
|
|
3
|
+
export interface SmtpOptions {
|
|
4
|
+
host: string;
|
|
5
|
+
port: number;
|
|
6
|
+
secure: "tls" | "starttls" | false;
|
|
7
|
+
/** Read at each connection; undefined sends no AUTH. */
|
|
8
|
+
credentials?: () => {
|
|
9
|
+
user: string;
|
|
10
|
+
password: string;
|
|
11
|
+
};
|
|
12
|
+
/** Keeps one connection open between sends; false opens and closes one per send. */
|
|
13
|
+
reuse: boolean;
|
|
14
|
+
}
|
|
15
|
+
/** In milliseconds. */
|
|
16
|
+
export interface SmtpTimes {
|
|
17
|
+
connect: number;
|
|
18
|
+
command: number;
|
|
19
|
+
idle: number;
|
|
20
|
+
}
|
|
21
|
+
/** The failure after the message was sent in full: no refusal, so sending again may deliver it twice. */
|
|
22
|
+
export declare const UNCONFIRMED = "the server did not confirm the message and may have accepted it";
|
|
23
|
+
/** The message as the server reads it: every line end a CRLF, the last line's included. */
|
|
24
|
+
export declare function messageText(raw: string): string;
|
|
25
|
+
/** RFC 5321 §4.5.2, and the line that ends the message. */
|
|
26
|
+
export declare function dataOf(raw: string): string;
|
|
27
|
+
/** One SMTP transaction per message; with `reuse` one idle connection is kept, and a send that finds none opens its own. */
|
|
28
|
+
export declare function createSmtp(options: SmtpOptions, connect: SmtpConnect, times?: SmtpTimes): MailSender;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface DkimOptions {
|
|
2
|
+
domain: string;
|
|
3
|
+
selector: string;
|
|
4
|
+
/** PKCS #8 PEM; its line breaks may be written `\n`, as in an env value of one line. */
|
|
5
|
+
privateKey: string;
|
|
6
|
+
}
|
|
7
|
+
/** The message with its `DKIM-Signature` header (RFC 6376) first, every line end a CRLF as the SMTP client sends it. */
|
|
8
|
+
export declare function signDkim(raw: string, options: DkimOptions): Promise<string>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** One SMTP connection of a runtime, plain or over TLS. */
|
|
2
|
+
export interface SmtpSocket {
|
|
3
|
+
/** The next bytes; undefined once the server ended the connection. */
|
|
4
|
+
read(): Promise<Uint8Array | undefined>;
|
|
5
|
+
write(bytes: Uint8Array<ArrayBuffer>): Promise<void>;
|
|
6
|
+
/** The TLS socket over this connection, its certificate checked; bytes this socket still holds never reach it. */
|
|
7
|
+
startTls(): Promise<SmtpSocket>;
|
|
8
|
+
/** Tears the connection down at once, whatever the server does. */
|
|
9
|
+
close(): void;
|
|
10
|
+
/** Closes after the bytes written so far have left. */
|
|
11
|
+
end(): void;
|
|
12
|
+
/** Whether bytes nobody asked for wait here, or the connection ended; absent where no connection is reused. */
|
|
13
|
+
stale?(): boolean;
|
|
14
|
+
/** Whether the connection keeps the process running; absent where no connection outlives a send. */
|
|
15
|
+
hold?(held: boolean): void;
|
|
16
|
+
}
|
|
17
|
+
export type SmtpConnect = (target: {
|
|
18
|
+
host: string;
|
|
19
|
+
port: number;
|
|
20
|
+
}, signal: AbortSignal) => Promise<SmtpSocket>;
|
|
21
|
+
export declare const nodeConnect: SmtpConnect;
|
|
22
|
+
export declare const bunConnect: SmtpConnect;
|
|
23
|
+
interface WorkersSocket {
|
|
24
|
+
readable: ReadableStream<Uint8Array>;
|
|
25
|
+
writable: WritableStream<Uint8Array>;
|
|
26
|
+
opened: Promise<unknown>;
|
|
27
|
+
closed: Promise<void>;
|
|
28
|
+
close(): Promise<void>;
|
|
29
|
+
startTls(): WorkersSocket;
|
|
30
|
+
}
|
|
31
|
+
/** `connect` of `cloudflare:sockets`. */
|
|
32
|
+
export type WorkersConnect = (address: {
|
|
33
|
+
hostname: string;
|
|
34
|
+
port: number;
|
|
35
|
+
}, options: {
|
|
36
|
+
secureTransport: "starttls";
|
|
37
|
+
allowHalfOpen: boolean;
|
|
38
|
+
}) => WorkersSocket;
|
|
39
|
+
export declare function workersConnect(connect: WorkersConnect): SmtpConnect;
|
|
40
|
+
/** `Bun.connect` on bun, `cloudflare:sockets` where it can be imported (Workers), else `node:net`. */
|
|
41
|
+
export declare const platformConnect: SmtpConnect;
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** One `.json` of the outbox. */
|
|
2
|
+
export interface MailRecord {
|
|
3
|
+
id: string;
|
|
4
|
+
date: string;
|
|
5
|
+
from: string;
|
|
6
|
+
to: string[];
|
|
7
|
+
cc: string[];
|
|
8
|
+
bcc: string[];
|
|
9
|
+
subject: string;
|
|
10
|
+
template?: string;
|
|
11
|
+
html: string;
|
|
12
|
+
text: string;
|
|
13
|
+
headers: Record<string, string>;
|
|
14
|
+
attachments: Array<{
|
|
15
|
+
filename: string;
|
|
16
|
+
contentType: string;
|
|
17
|
+
size: number;
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
export interface SentMailOptions {
|
|
21
|
+
/** The outbox directory; default the one of the test app or of the test preload. */
|
|
22
|
+
dir?: string;
|
|
23
|
+
}
|
|
24
|
+
/** The messages in the outbox, oldest first. */
|
|
25
|
+
export declare function sentMail(options?: SentMailOptions): Promise<MailRecord[]>;
|
|
26
|
+
/** Removes every message of the outbox. */
|
|
27
|
+
export declare function clearSentMail(options?: SentMailOptions): Promise<void>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { RenderedMail, ResolvedMailTransport } from "./types.ts";
|
|
2
|
+
/** One message as a transport receives it: the MIME text, the SMTP envelope and the message it was written from. */
|
|
3
|
+
export interface OutgoingMail {
|
|
4
|
+
/** The Message-ID without `<>`. */
|
|
5
|
+
id: string;
|
|
6
|
+
date: Date;
|
|
7
|
+
raw: string;
|
|
8
|
+
envelope: {
|
|
9
|
+
from: string;
|
|
10
|
+
to: string[];
|
|
11
|
+
};
|
|
12
|
+
message: RenderedMail;
|
|
13
|
+
}
|
|
14
|
+
export interface MailSender {
|
|
15
|
+
send(mail: OutgoingMail): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
/** What the run tells a transport: `reuse` is false where a connection must not outlive a send (the serverless presets). */
|
|
18
|
+
export interface TransportRuntime {
|
|
19
|
+
reuse: boolean;
|
|
20
|
+
}
|
|
21
|
+
export type OpenTransport = (config: ResolvedMailTransport, runtime: TransportRuntime) => MailSender;
|
|
22
|
+
/** A failure that does not rule out that a recipient has the message: sending it again may deliver it twice. */
|
|
23
|
+
export declare class UnconfirmedError extends Error {
|
|
24
|
+
}
|
|
25
|
+
/** A transport that sends one message per recipient failed after some were served: sending it again reaches those twice. */
|
|
26
|
+
export declare class PartialDeliveryError extends UnconfirmedError {
|
|
27
|
+
constructor(served: number, total: number, text: string, cause: unknown);
|
|
28
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type MailSender, type OpenTransport } from "../transport.ts";
|
|
2
|
+
/** `EmailMessage` of `cloudflare:email`. */
|
|
3
|
+
type EmailMessageConstructor = new (from: string, to: string, raw: string) => unknown;
|
|
4
|
+
/** What a test changes: where the Worker's `env` and `EmailMessage` come from. */
|
|
5
|
+
export interface CloudflareDeps {
|
|
6
|
+
env?: () => Promise<Record<string, unknown>>;
|
|
7
|
+
emailMessage?: () => Promise<EmailMessageConstructor>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* `env[binding].send(new EmailMessage(from, to, raw))`, one per envelope recipient, in order, stopping at the first
|
|
11
|
+
* refusal. A refusal after some recipients were served fails with their count, since sending the message again reaches them twice.
|
|
12
|
+
*/
|
|
13
|
+
export declare function createCloudflare(config: {
|
|
14
|
+
binding: string;
|
|
15
|
+
}, deps?: CloudflareDeps): MailSender;
|
|
16
|
+
declare const open: OpenTransport;
|
|
17
|
+
export default open;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type OutgoingMail } from "../transport.ts";
|
|
2
|
+
import type { ParsedAddress } from "../types.ts";
|
|
3
|
+
/** What a test changes: `endpoint` replaces the provider's origin. */
|
|
4
|
+
export interface HttpDeps {
|
|
5
|
+
fetch?: typeof fetch;
|
|
6
|
+
endpoint?: string;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare const TIMEOUT = 30000;
|
|
10
|
+
export declare const secret: (value: unknown, option: string) => string;
|
|
11
|
+
export declare const address: (parsed: ParsedAddress) => string;
|
|
12
|
+
export declare function base64(bytes: Uint8Array): string;
|
|
13
|
+
export declare const base64Text: (text: string) => string;
|
|
14
|
+
/** The provider's text without the secrets and addresses it repeats, controls as spaces, at most 500 characters. */
|
|
15
|
+
export declare function shown(text: string, hidden: string[]): string;
|
|
16
|
+
export declare const recipients: (mail: OutgoingMail) => string[];
|
|
17
|
+
/**
|
|
18
|
+
* One POST. A rejection of `fetch` (timeout, reset, no answer) may come after the provider took the message, so it is the
|
|
19
|
+
* `UNCONFIRMED` failure, unless no connection was made; a non-2xx answer is a refusal with its status and the start of its body.
|
|
20
|
+
* A 5xx other than 503 may come from a gateway or a server that failed after the provider took the message.
|
|
21
|
+
*/
|
|
22
|
+
export declare function post(url: string, init: {
|
|
23
|
+
headers: HeadersInit;
|
|
24
|
+
body: BodyInit;
|
|
25
|
+
}, hidden: string[], deps: HttpDeps): Promise<void>;
|
|
26
|
+
export declare const origin: (deps: HttpDeps, provider: string) => string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type HttpDeps } from "./http.ts";
|
|
2
|
+
import type { MailSender, OpenTransport } from "../transport.ts";
|
|
3
|
+
export declare function createMailgun(config: {
|
|
4
|
+
apiKey: unknown;
|
|
5
|
+
domain: string;
|
|
6
|
+
region?: "us" | "eu";
|
|
7
|
+
}, deps?: HttpDeps): MailSender;
|
|
8
|
+
declare const open: OpenTransport;
|
|
9
|
+
export default open;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { MailSender, OpenTransport, OutgoingMail } from "../transport.ts";
|
|
2
|
+
/** The newest pairs the outbox keeps. */
|
|
3
|
+
export declare const KEEP = 200;
|
|
4
|
+
/** What the outbox writes next to each `.eml`, read by the `mail` tab and `sentMail`. */
|
|
5
|
+
export declare function outboxRecord(mail: OutgoingMail): Record<string, unknown>;
|
|
6
|
+
/** Each message as `<dir>/<ISO time>-<id>.eml` and `.json`, the newest `keep` pairs kept. */
|
|
7
|
+
export declare function createOutbox(dir: string, keep?: number): MailSender;
|
|
8
|
+
declare const open: OpenTransport;
|
|
9
|
+
export default open;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type HttpDeps } from "./http.ts";
|
|
2
|
+
import type { MailSender, OpenTransport } from "../transport.ts";
|
|
3
|
+
export declare function createPostmark(config: {
|
|
4
|
+
serverToken: unknown;
|
|
5
|
+
messageStream?: string;
|
|
6
|
+
}, deps?: HttpDeps): MailSender;
|
|
7
|
+
declare const open: OpenTransport;
|
|
8
|
+
export default open;
|