@hamur/mail 0.0.0-stage → 0.0.1

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 (90) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +14 -2
  3. package/dist/src/devtools.js +268 -0
  4. package/dist/src/index.js +573 -0
  5. package/dist/src/server/index.js +916 -0
  6. package/dist/src/test.js +39 -0
  7. package/dist/types/src/address.d.ts +14 -0
  8. package/dist/types/src/build/devtools.d.ts +20 -0
  9. package/dist/types/src/build/outbox.d.ts +35 -0
  10. package/dist/types/src/build/setup.d.ts +22 -0
  11. package/dist/types/src/build/templates.d.ts +12 -0
  12. package/dist/types/src/devtools-model.d.ts +12 -0
  13. package/dist/types/src/devtools.d.ts +2 -0
  14. package/dist/types/src/html.d.ts +10 -0
  15. package/dist/types/src/index.d.ts +24 -0
  16. package/dist/types/src/mime.d.ts +32 -0
  17. package/dist/types/src/module.d.ts +3 -0
  18. package/dist/types/src/options.d.ts +12 -0
  19. package/dist/types/src/server/deliver.d.ts +15 -0
  20. package/dist/types/src/server/devtools-routes.d.ts +1 -0
  21. package/dist/types/src/server/door-dev.d.ts +3 -0
  22. package/dist/types/src/server/door.d.ts +2 -0
  23. package/dist/types/src/server/hooks.d.ts +5 -0
  24. package/dist/types/src/server/index.d.ts +6 -0
  25. package/dist/types/src/server/message.d.ts +3 -0
  26. package/dist/types/src/server/plugin.d.ts +1 -0
  27. package/dist/types/src/server/preview.d.ts +8 -0
  28. package/dist/types/src/server/queue.d.ts +19 -0
  29. package/dist/types/src/server/runtime.d.ts +5 -0
  30. package/dist/types/src/server/send.d.ts +54 -0
  31. package/dist/types/src/server/task.d.ts +4 -0
  32. package/dist/types/src/server/template.d.ts +8 -0
  33. package/dist/types/src/smtp/client.d.ts +28 -0
  34. package/dist/types/src/smtp/dkim.d.ts +8 -0
  35. package/dist/types/src/smtp/sockets.d.ts +42 -0
  36. package/dist/types/src/test.d.ts +27 -0
  37. package/dist/types/src/transport.d.ts +28 -0
  38. package/dist/types/src/transports/cloudflare.d.ts +17 -0
  39. package/dist/types/src/transports/http.d.ts +26 -0
  40. package/dist/types/src/transports/mailgun.d.ts +9 -0
  41. package/dist/types/src/transports/outbox.d.ts +9 -0
  42. package/dist/types/src/transports/postmark.d.ts +8 -0
  43. package/dist/types/src/transports/resend.d.ts +7 -0
  44. package/dist/types/src/transports/sendgrid.d.ts +7 -0
  45. package/dist/types/src/transports/ses.d.ts +9 -0
  46. package/dist/types/src/transports/smtp.d.ts +3 -0
  47. package/dist/types/src/types.d.ts +124 -0
  48. package/package.json +36 -4
  49. package/src/address.ts +86 -0
  50. package/src/build/devtools.ts +81 -0
  51. package/src/build/outbox.ts +69 -0
  52. package/src/build/setup.ts +226 -0
  53. package/src/build/templates.ts +113 -0
  54. package/src/devtools-model.ts +24 -0
  55. package/src/devtools.ts +351 -0
  56. package/src/env.d.ts +36 -0
  57. package/src/html.ts +274 -0
  58. package/src/index.ts +25 -0
  59. package/src/mime.ts +220 -0
  60. package/src/module.ts +11 -0
  61. package/src/options.ts +157 -0
  62. package/src/server/deliver.ts +54 -0
  63. package/src/server/devtools-routes.ts +32 -0
  64. package/src/server/door-dev.ts +6 -0
  65. package/src/server/door.ts +4 -0
  66. package/src/server/hooks.ts +13 -0
  67. package/src/server/index.ts +28 -0
  68. package/src/server/message.ts +36 -0
  69. package/src/server/plugin.ts +4 -0
  70. package/src/server/preview.ts +41 -0
  71. package/src/server/queue.ts +58 -0
  72. package/src/server/runtime.ts +45 -0
  73. package/src/server/send.ts +308 -0
  74. package/src/server/task.ts +13 -0
  75. package/src/server/template.ts +37 -0
  76. package/src/smtp/client.ts +305 -0
  77. package/src/smtp/dkim.ts +72 -0
  78. package/src/smtp/sockets.ts +296 -0
  79. package/src/test.ts +70 -0
  80. package/src/transport.ts +35 -0
  81. package/src/transports/cloudflare.ts +68 -0
  82. package/src/transports/http.ts +75 -0
  83. package/src/transports/mailgun.ts +31 -0
  84. package/src/transports/outbox.ts +64 -0
  85. package/src/transports/postmark.ts +59 -0
  86. package/src/transports/resend.ts +46 -0
  87. package/src/transports/sendgrid.ts +64 -0
  88. package/src/transports/ses.ts +53 -0
  89. package/src/transports/smtp.ts +37 -0
  90. package/src/types.ts +104 -0
@@ -0,0 +1,59 @@
1
+ import { address, base64, origin, post, recipients, secret, type HttpDeps } from "./http.ts"
2
+ import type { MailSender, OpenTransport } from "../transport.ts"
3
+
4
+ export function createPostmark(
5
+ config: { serverToken: unknown; messageStream?: string },
6
+ deps: HttpDeps = {},
7
+ ): MailSender {
8
+ return {
9
+ async send(mail) {
10
+ const { message } = mail
11
+ const token = secret(config.serverToken, "serverToken")
12
+ const body = {
13
+ From: address(message.from),
14
+ To: message.to.map(address).join(", "),
15
+ ...(message.cc.length ? { Cc: message.cc.map(address).join(", ") } : {}),
16
+ ...(message.bcc.length ? { Bcc: message.bcc.map(address).join(", ") } : {}),
17
+ ...(message.replyTo ? { ReplyTo: address(message.replyTo) } : {}),
18
+ Subject: message.subject,
19
+ HtmlBody: message.html,
20
+ ...(message.text === undefined ? {} : { TextBody: message.text }),
21
+ ...(Object.keys(message.headers).length
22
+ ? { Headers: Object.entries(message.headers).map(([Name, Value]) => ({ Name, Value })) }
23
+ : {}),
24
+ ...(message.attachments.length
25
+ ? {
26
+ Attachments: message.attachments.map((attachment) => ({
27
+ Name: attachment.filename,
28
+ Content: base64(attachment.content),
29
+ ContentType: attachment.contentType,
30
+ ...(attachment.cid === undefined ? {} : { ContentID: `cid:${attachment.cid}` }),
31
+ })),
32
+ }
33
+ : {}),
34
+ ...(message.tags.length ? { Tag: message.tags[0] } : {}),
35
+ ...(config.messageStream === undefined ? {} : { MessageStream: config.messageStream }),
36
+ }
37
+ await post(
38
+ `${origin(deps, "https://api.postmarkapp.com")}/email`,
39
+ {
40
+ headers: {
41
+ "x-postmark-server-token": token,
42
+ accept: "application/json",
43
+ "content-type": "application/json",
44
+ },
45
+ body: JSON.stringify(body),
46
+ },
47
+ [token, ...recipients(mail)],
48
+ deps,
49
+ )
50
+ },
51
+ }
52
+ }
53
+
54
+ const open: OpenTransport = (config) => {
55
+ if (config.type !== "postmark") throw new Error(`the postmark transport cannot open a ${config.type} transport`)
56
+ return createPostmark(config)
57
+ }
58
+
59
+ export default open
@@ -0,0 +1,46 @@
1
+ import { address, base64, origin, post, recipients, secret, type HttpDeps } from "./http.ts"
2
+ import type { MailSender, OpenTransport } from "../transport.ts"
3
+
4
+ export function createResend(config: { apiKey: unknown }, deps: HttpDeps = {}): MailSender {
5
+ return {
6
+ async send(mail) {
7
+ const { message } = mail
8
+ const key = secret(config.apiKey, "apiKey")
9
+ const body = {
10
+ from: address(message.from),
11
+ to: message.to.map(address),
12
+ ...(message.cc.length ? { cc: message.cc.map(address) } : {}),
13
+ ...(message.bcc.length ? { bcc: message.bcc.map(address) } : {}),
14
+ ...(message.replyTo ? { reply_to: address(message.replyTo) } : {}),
15
+ subject: message.subject,
16
+ html: message.html,
17
+ ...(message.text === undefined ? {} : { text: message.text }),
18
+ ...(Object.keys(message.headers).length ? { headers: message.headers } : {}),
19
+ ...(message.attachments.length
20
+ ? {
21
+ attachments: message.attachments.map((attachment) => ({
22
+ filename: attachment.filename,
23
+ content: base64(attachment.content),
24
+ content_type: attachment.contentType,
25
+ ...(attachment.cid === undefined ? {} : { content_id: attachment.cid }),
26
+ })),
27
+ }
28
+ : {}),
29
+ ...(message.tags.length ? { tags: message.tags.map((tag) => ({ name: tag, value: tag })) } : {}),
30
+ }
31
+ await post(
32
+ `${origin(deps, "https://api.resend.com")}/emails`,
33
+ { headers: { authorization: `Bearer ${key}`, "content-type": "application/json" }, body: JSON.stringify(body) },
34
+ [key, ...recipients(mail)],
35
+ deps,
36
+ )
37
+ },
38
+ }
39
+ }
40
+
41
+ const open: OpenTransport = (config) => {
42
+ if (config.type !== "resend") throw new Error(`the resend transport cannot open a ${config.type} transport`)
43
+ return createResend(config)
44
+ }
45
+
46
+ export default open
@@ -0,0 +1,64 @@
1
+ import { base64, origin, post, recipients, secret, type HttpDeps } from "./http.ts"
2
+ import type { MailSender, OpenTransport } from "../transport.ts"
3
+ import type { ParsedAddress } from "../types.ts"
4
+
5
+ const person = (parsed: ParsedAddress) => ({
6
+ email: parsed.address,
7
+ ...(parsed.name ? { name: parsed.name } : {}),
8
+ })
9
+
10
+ export function createSendgrid(config: { apiKey: unknown }, deps: HttpDeps = {}): MailSender {
11
+ return {
12
+ async send(mail) {
13
+ const { message } = mail
14
+ const key = secret(config.apiKey, "apiKey")
15
+ const seen = new Set<string>()
16
+ const once = (list: ParsedAddress[]) =>
17
+ list.filter((item) => {
18
+ const lower = item.address.toLowerCase()
19
+ if (seen.has(lower)) return false
20
+ seen.add(lower)
21
+ return true
22
+ })
23
+ const to = once(message.to).map(person)
24
+ const cc = once(message.cc).map(person)
25
+ const bcc = once(message.bcc).map(person)
26
+ const body = {
27
+ personalizations: [{ to, ...(cc.length ? { cc } : {}), ...(bcc.length ? { bcc } : {}) }],
28
+ from: person(message.from),
29
+ ...(message.replyTo ? { reply_to: person(message.replyTo) } : {}),
30
+ subject: message.subject,
31
+ content: [
32
+ ...(message.text ? [{ type: "text/plain", value: message.text }] : []),
33
+ { type: "text/html", value: message.html },
34
+ ],
35
+ ...(Object.keys(message.headers).length ? { headers: message.headers } : {}),
36
+ ...(message.attachments.length
37
+ ? {
38
+ attachments: message.attachments.map((attachment) => ({
39
+ content: base64(attachment.content),
40
+ filename: attachment.filename,
41
+ type: attachment.contentType,
42
+ disposition: attachment.cid === undefined ? "attachment" : "inline",
43
+ ...(attachment.cid === undefined ? {} : { content_id: attachment.cid }),
44
+ })),
45
+ }
46
+ : {}),
47
+ ...(message.tags.length ? { categories: message.tags } : {}),
48
+ }
49
+ await post(
50
+ `${origin(deps, "https://api.sendgrid.com")}/v3/mail/send`,
51
+ { headers: { authorization: `Bearer ${key}`, "content-type": "application/json" }, body: JSON.stringify(body) },
52
+ [key, ...recipients(mail)],
53
+ deps,
54
+ )
55
+ },
56
+ }
57
+ }
58
+
59
+ const open: OpenTransport = (config) => {
60
+ if (config.type !== "sendgrid") throw new Error(`the sendgrid transport cannot open a ${config.type} transport`)
61
+ return createSendgrid(config)
62
+ }
63
+
64
+ export default open
@@ -0,0 +1,53 @@
1
+ import { sha256Hex, signV4 } from "hamur/server/internal"
2
+ import { base64Text, origin, post, recipients, secret, type HttpDeps } from "./http.ts"
3
+ import type { MailSender, OpenTransport } from "../transport.ts"
4
+
5
+ const encoder = new TextEncoder()
6
+
7
+ export function createSes(
8
+ config: { region: string; accessKeyId: unknown; secretAccessKey: unknown },
9
+ deps: HttpDeps = {},
10
+ ): MailSender {
11
+ return {
12
+ async send(mail) {
13
+ const { message } = mail
14
+ const accessKeyId = secret(config.accessKeyId, "accessKeyId")
15
+ const secretAccessKey = secret(config.secretAccessKey, "secretAccessKey")
16
+ const list = (items: Array<{ address: string }>) => items.map((item) => item.address)
17
+ const body = JSON.stringify({
18
+ FromEmailAddress: mail.envelope.from,
19
+ Destination: {
20
+ ToAddresses: list(message.to),
21
+ ...(message.cc.length ? { CcAddresses: list(message.cc) } : {}),
22
+ ...(message.bcc.length ? { BccAddresses: list(message.bcc) } : {}),
23
+ },
24
+ Content: { Raw: { Data: base64Text(mail.raw) } },
25
+ ...(message.tags.length ? { EmailTags: message.tags.map((tag) => ({ Name: tag, Value: tag })) } : {}),
26
+ })
27
+ const url = new URL(`${origin(deps, `https://email.${config.region}.amazonaws.com`)}/v2/email/outbound-emails`)
28
+ const headers = new Headers({ "content-type": "application/json" })
29
+ headers.set(
30
+ "authorization",
31
+ await signV4({
32
+ method: "POST",
33
+ url,
34
+ headers,
35
+ payloadHash: await sha256Hex(encoder.encode(body)),
36
+ region: config.region,
37
+ service: "ses",
38
+ accessKeyId,
39
+ secretAccessKey,
40
+ date: new Date(),
41
+ }),
42
+ )
43
+ await post(url.href, { headers, body }, [accessKeyId, secretAccessKey, ...recipients(mail)], deps)
44
+ },
45
+ }
46
+ }
47
+
48
+ const open: OpenTransport = (config) => {
49
+ if (config.type !== "ses") throw new Error(`the ses transport cannot open a ${config.type} transport`)
50
+ return createSes(config)
51
+ }
52
+
53
+ export default open
@@ -0,0 +1,37 @@
1
+ import { readSecret } from "hamur/server/internal"
2
+ import { createSmtp } from "../smtp/client.ts"
3
+ import { signDkim } from "../smtp/dkim.ts"
4
+ import { platformConnect } from "../smtp/sockets.ts"
5
+ import type { OpenTransport } from "../transport.ts"
6
+
7
+ const open: OpenTransport = (config, { reuse }) => {
8
+ if (config.type !== "smtp") throw new Error(`the smtp transport cannot open a ${config.type} transport`)
9
+ const { user, password, dkim } = config
10
+ const port = config.port ?? (config.secure === "tls" ? 465 : 587)
11
+ const sender = createSmtp(
12
+ {
13
+ host: config.host.replace(/^\[|\]$/g, ""),
14
+ port,
15
+ secure: config.secure ?? (port === 465 ? "tls" : "starttls"),
16
+ reuse,
17
+ ...(user === undefined
18
+ ? {}
19
+ : {
20
+ credentials: () => ({
21
+ user: readSecret(user, "mail.transport.user"),
22
+ password: readSecret(password, "mail.transport.password"),
23
+ }),
24
+ }),
25
+ },
26
+ platformConnect,
27
+ )
28
+ if (!dkim) return sender
29
+ return {
30
+ async send(mail) {
31
+ const privateKey = readSecret(dkim.privateKey, "mail.dkim.privateKey")
32
+ await sender.send({ ...mail, raw: await signDkim(mail.raw, { ...dkim, privateKey }) })
33
+ },
34
+ }
35
+ }
36
+
37
+ export default open
package/src/types.ts ADDED
@@ -0,0 +1,104 @@
1
+ import type { EnvRef } from "hamur"
2
+ import type { MailTemplates } from "./index.ts"
3
+
4
+ export type { EnvRef }
5
+
6
+ /** `"Ada <ada@x.dev>"`, `"ada@x.dev"` or the object. */
7
+ export type MailAddress = string | { name?: string; address: string }
8
+
9
+ export type MailTransport =
10
+ | {
11
+ type: "smtp"
12
+ host: string
13
+ port?: number
14
+ secure?: "tls" | "starttls" | false
15
+ user?: string | EnvRef
16
+ password?: EnvRef
17
+ }
18
+ | { type: "resend"; apiKey: EnvRef }
19
+ | { type: "postmark"; serverToken: EnvRef; messageStream?: string }
20
+ | { type: "sendgrid"; apiKey: EnvRef }
21
+ | { type: "mailgun"; apiKey: EnvRef; domain: string; region?: "us" | "eu" }
22
+ | { type: "ses"; region: string; accessKeyId: EnvRef; secretAccessKey: EnvRef }
23
+ | { type: "cloudflare"; binding: string }
24
+ | { type: "outbox"; dir?: string }
25
+
26
+ /** `selector` names the DNS record `<selector>._domainkey.<domain>` that holds the public key. */
27
+ export interface MailDkim {
28
+ domain: string
29
+ selector: string
30
+ /** A PKCS #8 RSA private key in PEM. */
31
+ privateKey: EnvRef
32
+ }
33
+
34
+ export interface MailConfig {
35
+ from: MailAddress
36
+ replyTo?: MailAddress
37
+ transport?: MailTransport
38
+ /** Default false: `sendMail` sends in the call. */
39
+ queue?: boolean
40
+ /** Signs each message the smtp transport sends. */
41
+ dkim?: MailDkim
42
+ }
43
+
44
+ export type MailTemplateName = keyof MailTemplates & string
45
+
46
+ export interface MailAttachment {
47
+ filename: string
48
+ content: Uint8Array | Blob | string
49
+ contentType?: string
50
+ cid?: string
51
+ }
52
+
53
+ export interface MailMessage<T extends MailTemplateName = MailTemplateName> {
54
+ to: MailAddress | MailAddress[]
55
+ cc?: MailAddress | MailAddress[]
56
+ bcc?: MailAddress | MailAddress[]
57
+ /** Default `mail.from`. */
58
+ from?: MailAddress
59
+ replyTo?: MailAddress
60
+ subject: string
61
+ template?: T
62
+ props?: MailTemplates[T]
63
+ /** A body without a template. */
64
+ html?: string
65
+ text?: string
66
+ attachments?: MailAttachment[]
67
+ headers?: Record<string, string>
68
+ /** Passed to HTTP providers that take tags; ignored by smtp, cloudflare and outbox. */
69
+ tags?: string[]
70
+ }
71
+
72
+ export interface MailResult {
73
+ /** The Message-ID without `<>`. */
74
+ id: string
75
+ queued: boolean
76
+ }
77
+
78
+ /** One address as the module checked it: the domain in its IDNA form. */
79
+ export interface ParsedAddress {
80
+ name?: string
81
+ address: string
82
+ }
83
+
84
+ /** The message `mail:beforeSend` receives and may change, `from` filled from `mail.from`, every address parsed. */
85
+ export interface RenderedMail {
86
+ from: ParsedAddress
87
+ to: ParsedAddress[]
88
+ cc: ParsedAddress[]
89
+ bcc: ParsedAddress[]
90
+ replyTo?: ParsedAddress
91
+ subject: string
92
+ template?: string
93
+ html: string
94
+ text?: string
95
+ attachments: Array<{ filename: string; content: Uint8Array; contentType: string; cid?: string }>
96
+ headers: Record<string, string>
97
+ tags: string[]
98
+ }
99
+
100
+ /** `mail.transport` as the build resolved it: the outbox `dir` absolute, `mail.dkim` on smtp. */
101
+ export type ResolvedMailTransport =
102
+ | Exclude<MailTransport, { type: "outbox" | "smtp" }>
103
+ | (Extract<MailTransport, { type: "smtp" }> & { dkim?: MailDkim })
104
+ | { type: "outbox"; dir: string }