@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.
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 +48 -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
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 productdevbook
4
+ Copyright (c) 2019 Material-UI SAS
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,15 @@
1
- # Temporary Holding Version
1
+ # @hamur/mail
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The first-party mail module for [Hamur](https://github.com/productdevbook/hamur), a full-stack web framework for Bun. It has no dependency.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ bun add @hamur/mail
9
+ ```
10
+
11
+ Add it to `modules` in `hamur.config.ts` and set the `mail` key.
12
+
13
+ ## Docs
14
+
15
+ [Documentation](https://docs.productdevbook.com/) and the [API reference](https://docs.productdevbook.com/api-reference/mail). MIT license.
@@ -0,0 +1,268 @@
1
+ // @bun
2
+ // packages/mail/src/devtools.ts
3
+ import { defineDevtoolsApp } from "hamur/app";
4
+
5
+ // packages/mail/src/devtools-model.ts
6
+ var MAIL_CSP = "default-src 'none'; img-src * data:; style-src 'unsafe-inline'";
7
+ function framedDocument(html) {
8
+ return `<!doctype html><html><head><meta http-equiv="Content-Security-Policy" content="${MAIL_CSP}"></head><body style="margin:0">${html}</body></html>`;
9
+ }
10
+ function parseProps(text) {
11
+ if (text.trim() === "")
12
+ return { props: {} };
13
+ let value;
14
+ try {
15
+ value = JSON.parse(text);
16
+ } catch (error) {
17
+ return { error: `Props are not JSON: ${error instanceof Error ? error.message : String(error)}` };
18
+ }
19
+ if (typeof value !== "object" || value === null || Array.isArray(value))
20
+ return { error: "Props must be a JSON object" };
21
+ return { props: value };
22
+ }
23
+ var emlFileName = (name) => `${name.replace(/[^\w.@-]+/g, "_")}.eml`;
24
+
25
+ // packages/mail/src/devtools.ts
26
+ var FOCUS = "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent";
27
+ var BUTTON = `inline-flex items-center gap-1.5 h-6 px-2 rounded-md border-0 bg-transparent text-fg-2 ring-1 ring-inset ring-line-strong font-medium text-sm whitespace-nowrap cursor-pointer hover:bg-hover hover:text-fg disabled:opacity-40 disabled:cursor-default ${FOCUS}`;
28
+ var TAB = (on) => `inline-flex items-center h-7 px-0.5 mr-3.5 border-0 border-b-2 bg-transparent text-sm cursor-pointer ${FOCUS} ${on ? "border-fg text-fg" : "border-transparent text-fg-3 hover:text-fg"}`;
29
+ var PILL = (on) => `inline-flex items-center h-6 px-2 rounded-md border-0 font-medium text-xs whitespace-nowrap cursor-pointer ${FOCUS} ${on ? "bg-active text-fg" : "bg-transparent text-fg-3 hover:bg-hover hover:text-fg"}`;
30
+ var TEXTAREA = `block w-full min-h-20 px-2 py-1.5 rounded-md border-0 bg-field ring-1 ring-inset ring-line font-mono text-xs text-fg placeholder:text-fg-4 ${FOCUS}`;
31
+ var NOTICE = "m-0 p-4 text-sm text-fg-3";
32
+ var FAIL = "text-xs text-red";
33
+ var LABEL = "text-xs font-medium text-fg-3";
34
+ var element = (tag, props = {}, ...children) => {
35
+ const node = Object.assign(document.createElement(tag), props);
36
+ node.append(...children);
37
+ return node;
38
+ };
39
+ var messageOf = (error) => error instanceof Error ? error.message : String(error);
40
+ function frame(html) {
41
+ const iframe = element("iframe", {
42
+ className: "block w-full flex-1 min-h-64 border-0 rounded-md bg-white",
43
+ title: "Message HTML",
44
+ srcdoc: framedDocument(html),
45
+ referrerPolicy: "no-referrer"
46
+ });
47
+ iframe.setAttribute("sandbox", "");
48
+ iframe.setAttribute("csp", MAIL_CSP);
49
+ return iframe;
50
+ }
51
+ var pre = (text) => element("pre", {
52
+ className: "m-0 px-3 py-2 rounded-lg bg-field font-mono text-xs text-fg-2 whitespace-pre-wrap break-words overflow-auto"
53
+ }, text);
54
+ function download(name, eml) {
55
+ const url = URL.createObjectURL(new Blob([eml], { type: "message/rfc822" }));
56
+ const link = element("a", { href: url, download: emlFileName(name) });
57
+ link.click();
58
+ setTimeout(() => URL.revokeObjectURL(url), 1000);
59
+ }
60
+ function shownTabs(current, choose, names = ["html", "text", "headers"]) {
61
+ return element("div", { className: "flex gap-1" }, ...names.map((name) => {
62
+ const button = element("button", { type: "button", className: PILL(name === current) }, name.toUpperCase());
63
+ button.dataset.shown = name;
64
+ button.addEventListener("click", () => choose(name));
65
+ return button;
66
+ }));
67
+ }
68
+ var devtools_default = defineDevtoolsApp({
69
+ async init(canvas, ctx) {
70
+ canvas.className = "flex flex-col flex-1 min-h-0";
71
+ const strip = element("div", {
72
+ className: "flex-none flex gap-0.5 px-3 pt-1 border-b border-line",
73
+ role: "tablist"
74
+ });
75
+ strip.setAttribute("aria-label", "Mail views");
76
+ const note = element("p", { className: `m-0 px-4 py-1 ${FAIL} empty:hidden` });
77
+ note.dataset.error = "";
78
+ const body = element("div", { className: "flex-1 flex min-h-0" });
79
+ canvas.append(strip, note, body);
80
+ let part = "outbox";
81
+ let messages = [];
82
+ let transport = "none";
83
+ let templates = [];
84
+ let selected;
85
+ let shown = "html";
86
+ let detail;
87
+ let template;
88
+ let propsText = "{}";
89
+ let preview;
90
+ let previewShown = "html";
91
+ let previewError = "";
92
+ let detailToken = 0;
93
+ const detailPane = element("div", { className: "flex-1 min-w-0 flex flex-col gap-2 p-3 overflow-auto" });
94
+ const listPane = element("ul", {
95
+ className: "flex-none w-72 m-0 p-1.5 list-none border-r border-line overflow-auto"
96
+ });
97
+ function paintStrip() {
98
+ strip.replaceChildren(...[
99
+ ["outbox", "Outbox"],
100
+ ["templates", "Templates"]
101
+ ].map(([name, title]) => {
102
+ const button = element("button", { type: "button", role: "tab", className: TAB(name === part) }, title);
103
+ button.dataset.part = name;
104
+ button.setAttribute("aria-selected", String(name === part));
105
+ button.addEventListener("click", () => {
106
+ part = name;
107
+ paint();
108
+ });
109
+ return button;
110
+ }));
111
+ }
112
+ function listItem(message) {
113
+ const button = element("button", {
114
+ type: "button",
115
+ className: `flex flex-col items-start gap-0.5 w-full px-2.5 py-1.5 border-0 rounded-md text-left cursor-pointer ${FOCUS} ${message.name === selected ? "bg-active" : "bg-transparent hover:bg-hover"}`
116
+ }, element("strong", { className: "block max-w-full truncate text-sm font-medium text-fg" }, message.subject || "(no subject)"), element("span", { className: "block max-w-full truncate text-xs text-fg-3" }, message.to.join(", ")), element("span", { className: "text-xs text-fg-4" }, new Date(message.date).toLocaleTimeString()));
117
+ button.addEventListener("click", () => void open(message.name));
118
+ const item = element("li", {}, button);
119
+ item.dataset.name = message.name;
120
+ return item;
121
+ }
122
+ async function open(name) {
123
+ selected = name;
124
+ const token = ++detailToken;
125
+ paintList();
126
+ try {
127
+ const found = await ctx.rpc("mail:message", { name });
128
+ if (token !== detailToken)
129
+ return;
130
+ detail = { name, ...found };
131
+ note.textContent = "";
132
+ } catch (error) {
133
+ if (token !== detailToken)
134
+ return;
135
+ detail = undefined;
136
+ note.textContent = messageOf(error);
137
+ }
138
+ paintDetail();
139
+ }
140
+ function paintList() {
141
+ listPane.replaceChildren(...messages.map(listItem));
142
+ }
143
+ function paintDetail() {
144
+ if (!detail) {
145
+ detailPane.replaceChildren(element("p", { className: NOTICE }, "Pick a message"));
146
+ return;
147
+ }
148
+ const { name, record, eml } = detail;
149
+ const save = element("button", { type: "button", className: BUTTON }, "Download .eml");
150
+ save.addEventListener("click", () => download(name, eml));
151
+ const line = (label, value) => element("div", { className: "flex gap-2 text-xs" }, element("span", { className: `flex-none w-12 ${LABEL}` }, label), element("span", { className: "min-w-0 break-words text-fg" }, value));
152
+ const content = shown === "html" ? frame(record.html) : shown === "text" ? pre(record.text || "(no text part)") : pre(Object.entries(record.headers).map(([key, value]) => `${key}: ${value}`).join(`
153
+ `) || "(no extra headers)");
154
+ detailPane.replaceChildren(element("div", { className: "flex items-start gap-2" }, element("strong", { className: "flex-1 min-w-0 text-sm font-semibold text-fg break-words" }, record.subject), save), line("From", record.from), line("To", record.to.join(", ")), ...record.cc.length ? [line("Cc", record.cc.join(", "))] : [], ...record.bcc.length ? [line("Bcc", record.bcc.join(", "))] : [], line("Date", new Date(record.date).toLocaleString()), ...record.template ? [line("Template", record.template)] : [], ...record.attachments.length ? [line("Files", record.attachments.map((file) => `${file.filename} (${file.size} B)`).join(", "))] : [], shownTabs(shown, (next) => {
155
+ shown = next;
156
+ paintDetail();
157
+ }), content);
158
+ }
159
+ function paintOutbox() {
160
+ if (!messages.length) {
161
+ body.replaceChildren(element("p", { className: NOTICE }, transport === "outbox" ? "No mail sent yet" : `The outbox is off: mail.transport is ${transport}. Set it to { type: "outbox" } to read mail here.`));
162
+ return;
163
+ }
164
+ paintList();
165
+ paintDetail();
166
+ body.replaceChildren(listPane, detailPane);
167
+ }
168
+ function paintTemplates() {
169
+ if (!templates.length) {
170
+ body.replaceChildren(element("p", { className: NOTICE }, "No template in app/mail/"));
171
+ return;
172
+ }
173
+ const choose = element("select", {
174
+ className: `h-6 px-2 rounded-md border-0 bg-field ring-1 ring-inset ring-line text-xs text-fg ${FOCUS}`
175
+ });
176
+ choose.setAttribute("aria-label", "Template");
177
+ for (const name of templates)
178
+ choose.append(element("option", { value: name, selected: name === template }, name));
179
+ choose.addEventListener("change", () => {
180
+ template = choose.value;
181
+ preview = undefined;
182
+ previewError = "";
183
+ paintPreview();
184
+ });
185
+ template ??= templates[0];
186
+ choose.value = template;
187
+ const props = element("textarea", { className: TEXTAREA, rows: 6, value: propsText, spellcheck: false });
188
+ props.setAttribute("aria-label", "Props (JSON)");
189
+ props.addEventListener("input", () => {
190
+ propsText = props.value;
191
+ });
192
+ const render = element("button", { type: "button", className: BUTTON }, "Render");
193
+ render.addEventListener("click", () => {
194
+ (async () => {
195
+ const parsed = parseProps(propsText);
196
+ if ("error" in parsed) {
197
+ previewError = parsed.error;
198
+ paintPreview();
199
+ return;
200
+ }
201
+ render.disabled = true;
202
+ try {
203
+ preview = await ctx.rpc("mail:render", { template, props: parsed.props });
204
+ previewError = "";
205
+ } catch (error) {
206
+ preview = undefined;
207
+ previewError = messageOf(error);
208
+ }
209
+ render.disabled = false;
210
+ paintPreview();
211
+ })();
212
+ });
213
+ const form = element("div", { className: "flex-none w-80 flex flex-col gap-2 p-3 border-r border-line overflow-auto" }, element("span", { className: LABEL }, "Template"), choose, element("span", { className: LABEL }, "Props (JSON)"), props, render, element("span", { className: "text-xs text-fg-3" }, "Rendering sends nothing."));
214
+ preview = undefined;
215
+ body.replaceChildren(form, previewPane);
216
+ paintPreview();
217
+ }
218
+ const previewPane = element("div", { className: "flex-1 min-w-0 flex flex-col gap-2 p-3 overflow-auto" });
219
+ function paintPreview() {
220
+ if (previewError) {
221
+ previewPane.replaceChildren(element("p", { className: `m-0 whitespace-pre-wrap ${FAIL}` }, previewError));
222
+ previewPane.firstElementChild.setAttribute("data-error", "");
223
+ return;
224
+ }
225
+ if (!preview) {
226
+ previewPane.replaceChildren(element("p", { className: NOTICE }, "Render a template to see it"));
227
+ return;
228
+ }
229
+ previewPane.replaceChildren(shownTabs(previewShown, (next) => {
230
+ previewShown = next;
231
+ paintPreview();
232
+ }, ["html", "text"]), previewShown === "text" ? pre(preview.text || "(empty)") : frame(preview.html));
233
+ }
234
+ function paint() {
235
+ paintStrip();
236
+ if (part === "outbox")
237
+ paintOutbox();
238
+ else
239
+ paintTemplates();
240
+ }
241
+ const off = ctx.on("devtools:mail", (data) => {
242
+ const pushed = data.messages;
243
+ if (!Array.isArray(pushed))
244
+ return;
245
+ messages = pushed;
246
+ if (part === "outbox") {
247
+ if (!selected && messages[0])
248
+ open(messages[0].name);
249
+ paintOutbox();
250
+ }
251
+ });
252
+ try {
253
+ const list = await ctx.rpc("mail:list");
254
+ messages = list.messages;
255
+ transport = list.transport;
256
+ templates = (await ctx.rpc("mail:templates")).templates;
257
+ if (messages[0])
258
+ await open(messages[0].name);
259
+ } catch (error) {
260
+ note.textContent = messageOf(error);
261
+ }
262
+ paint();
263
+ return off;
264
+ }
265
+ });
266
+ export {
267
+ devtools_default as default
268
+ };