@gravito/signal 1.0.0-alpha.2 → 1.0.0-alpha.5

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.
@@ -0,0 +1,27 @@
1
+ # Orbit Mail
2
+
3
+ > Gravito 的郵件模組,支援多種傳輸與 HTML/模板/React/Vue 渲染。
4
+
5
+ ## 安裝
6
+
7
+ ```bash
8
+ bun add @gravito/signal
9
+ ```
10
+
11
+ ## 快速開始
12
+
13
+ ```typescript
14
+ import { OrbitSignal, SmtpTransport } from '@gravito/signal'
15
+
16
+ const mail = OrbitSignal.configure({
17
+ from: { name: 'My App', address: 'noreply@myapp.com' },
18
+ transport: new SmtpTransport({
19
+ host: 'smtp.mailtrap.io',
20
+ port: 2525,
21
+ auth: { user: '...', pass: '...' }
22
+ }),
23
+ devMode: process.env.NODE_ENV === 'development',
24
+ })
25
+
26
+ mail.install(core)
27
+ ```
@@ -0,0 +1,7 @@
1
+ import {
2
+ OrbitSignal
3
+ } from "./chunk-YLVDJSED.mjs";
4
+ import "./chunk-EBO3CZXG.mjs";
5
+ export {
6
+ OrbitSignal
7
+ };
@@ -0,0 +1,7 @@
1
+ import {
2
+ OrbitSignal
3
+ } from "./chunk-456QRYFW.mjs";
4
+ import "./chunk-6DZX6EAA.mjs";
5
+ export {
6
+ OrbitSignal
7
+ };
@@ -0,0 +1,7 @@
1
+ import {
2
+ OrbitSignal
3
+ } from "./chunk-NEQCQSZI.mjs";
4
+ import "./chunk-EBO3CZXG.mjs";
5
+ export {
6
+ OrbitSignal
7
+ };
@@ -0,0 +1,7 @@
1
+ import {
2
+ OrbitSignal
3
+ } from "./chunk-F6MVTUCT.mjs";
4
+ import "./chunk-EBO3CZXG.mjs";
5
+ export {
6
+ OrbitSignal
7
+ };
@@ -0,0 +1,27 @@
1
+ import "./chunk-EBO3CZXG.mjs";
2
+
3
+ // src/renderers/ReactRenderer.ts
4
+ var ReactRenderer = class {
5
+ constructor(component, props) {
6
+ this.component = component;
7
+ this.props = props;
8
+ }
9
+ async render(data) {
10
+ const { createElement } = await import("react");
11
+ const { renderToStaticMarkup } = await import("react-dom/server");
12
+ const mergedProps = { ...this.props, ...data };
13
+ const element = createElement(this.component, mergedProps);
14
+ const html = renderToStaticMarkup(element);
15
+ const fullHtml = html.startsWith("<!DOCTYPE") ? html : `<!DOCTYPE html>${html}`;
16
+ return {
17
+ html: fullHtml,
18
+ text: this.stripHtml(html)
19
+ };
20
+ }
21
+ stripHtml(html) {
22
+ return html.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, "").replace(/<script[^>]*>[\s\S]*?<\/script>/gi, "").replace(/<[^>]+>/g, "").replace(/&nbsp;/g, " ").replace(/\s+/g, " ").trim();
23
+ }
24
+ };
25
+ export {
26
+ ReactRenderer
27
+ };
@@ -0,0 +1,29 @@
1
+ import "./chunk-EBO3CZXG.mjs";
2
+
3
+ // src/renderers/VueRenderer.ts
4
+ var VueRenderer = class {
5
+ constructor(component, props) {
6
+ this.component = component;
7
+ this.props = props;
8
+ }
9
+ async render(data) {
10
+ const { createSSRApp, h } = await import("vue");
11
+ const { renderToString } = await import("./server-renderer-4W4FI7YG.mjs");
12
+ const mergedProps = { ...this.props, ...data };
13
+ const app = createSSRApp({
14
+ render: () => h(this.component, mergedProps)
15
+ });
16
+ const html = await renderToString(app);
17
+ const fullHtml = html.startsWith("<!DOCTYPE") ? html : `<!DOCTYPE html>${html}`;
18
+ return {
19
+ html: fullHtml,
20
+ text: this.stripHtml(html)
21
+ };
22
+ }
23
+ stripHtml(html) {
24
+ return html.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, "").replace(/<script[^>]*>[\s\S]*?<\/script>/gi, "").replace(/<[^>]+>/g, "").replace(/&nbsp;/g, " ").replace(/\s+/g, " ").trim();
25
+ }
26
+ };
27
+ export {
28
+ VueRenderer
29
+ };
@@ -0,0 +1,401 @@
1
+ // src/dev/DevMailbox.ts
2
+ import { randomUUID } from "crypto";
3
+ var DevMailbox = class {
4
+ entries = [];
5
+ maxEntries = 50;
6
+ add(message) {
7
+ const entry = {
8
+ id: randomUUID(),
9
+ envelope: {
10
+ from: message.from,
11
+ to: message.to,
12
+ ...message.cc ? { cc: message.cc } : {},
13
+ ...message.bcc ? { bcc: message.bcc } : {},
14
+ ...message.replyTo ? { replyTo: message.replyTo } : {},
15
+ subject: message.subject,
16
+ ...message.priority ? { priority: message.priority } : {},
17
+ ...message.attachments ? { attachments: message.attachments } : {}
18
+ },
19
+ html: message.html,
20
+ ...message.text ? { text: message.text } : {},
21
+ sentAt: /* @__PURE__ */ new Date()
22
+ };
23
+ this.entries.unshift(entry);
24
+ if (this.entries.length > this.maxEntries) {
25
+ this.entries = this.entries.slice(0, this.maxEntries);
26
+ }
27
+ return entry;
28
+ }
29
+ list() {
30
+ return this.entries;
31
+ }
32
+ get(id) {
33
+ return this.entries.find((e) => e.id === id);
34
+ }
35
+ delete(id) {
36
+ const index = this.entries.findIndex((e) => e.id === id);
37
+ if (index !== -1) {
38
+ this.entries.splice(index, 1);
39
+ return true;
40
+ }
41
+ return false;
42
+ }
43
+ clear() {
44
+ this.entries = [];
45
+ }
46
+ };
47
+
48
+ // src/dev/ui/shared.ts
49
+ var styles = `
50
+ :root {
51
+ --primary: #6366f1;
52
+ --primary-dark: #4f46e5;
53
+ --bg-dark: #0f172a;
54
+ --bg-card: #1e293b;
55
+ --text: #f1f5f9;
56
+ --text-muted: #94a3b8;
57
+ --border: #334155;
58
+ --danger: #ef4444;
59
+ }
60
+ body { background: var(--bg-dark); color: var(--text); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 20px; }
61
+ .container { max-width: 1000px; margin: 0 auto; }
62
+ .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
63
+ .title { font-size: 24px; font-weight: bold; display: flex; align-items: center; gap: 10px; }
64
+ .card { background: var(--bg-card); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
65
+ .btn { background: var(--border); color: var(--text); border: none; padding: 8px 16px; border-radius: 6px; cursor: pointer; text-decoration: none; font-size: 14px; transition: 0.2s; }
66
+ .btn:hover { background: var(--bg-card-hover); }
67
+ .btn-primary { background: var(--primary); color: white; }
68
+ .btn-primary:hover { background: var(--primary-dark); }
69
+ .btn-danger { background: var(--danger); color: white; }
70
+ .list-item { padding: 16px; border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 4px; text-decoration: none; color: inherit; transition: background 0.2s; }
71
+ .list-item:last-child { border-bottom: none; }
72
+ .list-item:hover { background: #334155; }
73
+ .meta { display: flex; justify-content: space-between; font-size: 14px; color: var(--text-muted); }
74
+ .subject { font-weight: 600; font-size: 16px; }
75
+ .from { color: #cbd5e1; }
76
+ .badge { background: #475569; padding: 2px 6px; border-radius: 4px; font-size: 12px; }
77
+ .badge-high { background: #dc2626; color: white; }
78
+ .empty { padding: 40px; text-align: center; color: var(--text-muted); }
79
+ `;
80
+ var layout = (title, content) => `
81
+ <!DOCTYPE html>
82
+ <html>
83
+ <head>
84
+ <title>${title} - Gravito Mailbox</title>
85
+ <style>${styles}</style>
86
+ </head>
87
+ <body>
88
+ <div class="container">
89
+ ${content}
90
+ </div>
91
+ </body>
92
+ </html>
93
+ `;
94
+
95
+ // src/dev/ui/mailbox.ts
96
+ function formatAddress(addr) {
97
+ return addr.name ? `${addr.name} <${addr.address}>` : addr.address;
98
+ }
99
+ function timeAgo(date) {
100
+ const seconds = Math.floor((Date.now() - date.getTime()) / 1e3);
101
+ if (seconds < 60) {
102
+ return "Just now";
103
+ }
104
+ const minutes = Math.floor(seconds / 60);
105
+ if (minutes < 60) {
106
+ return `${minutes}m ago`;
107
+ }
108
+ const hours = Math.floor(minutes / 60);
109
+ if (hours < 24) {
110
+ return `${hours}h ago`;
111
+ }
112
+ return date.toLocaleDateString();
113
+ }
114
+ function getMailboxHtml(entries, prefix) {
115
+ const list = entries.length === 0 ? '<div class="empty">No emails found in mailbox.</div>' : entries.map(
116
+ (entry) => `
117
+ <a href="${prefix}/${entry.id}" class="list-item">
118
+ <div class="meta">
119
+ <span class="from">${formatAddress(entry.envelope.from || { address: "Unknown" })}</span>
120
+ <span>${timeAgo(entry.sentAt)}</span>
121
+ </div>
122
+ <div class="subject">
123
+ ${entry.envelope.priority === "high" ? '<span class="badge badge-high">High</span> ' : ""}
124
+ ${entry.envelope.subject || "(No Subject)"}
125
+ </div>
126
+ <div class="meta" style="margin-top: 4px;">
127
+ To: ${entry.envelope.to?.map((t) => t.address).join(", ")}
128
+ </div>
129
+ </a>
130
+ `
131
+ ).join("");
132
+ const content = `
133
+ <div class="header">
134
+ <div class="title">\u{1F4EC} Gravito Mailbox</div>
135
+ ${entries.length > 0 ? `<button onclick="clearAll()" class="btn btn-danger">Clear All</button>` : ""}
136
+ </div>
137
+
138
+ <div class="card">
139
+ ${list}
140
+ </div>
141
+
142
+ <script>
143
+ async function clearAll() {
144
+ if (!confirm('Clear all emails?')) return;
145
+ await fetch('${prefix}', { method: 'DELETE' });
146
+ window.location.reload();
147
+ }
148
+ </script>
149
+ `;
150
+ return layout("Inbox", content);
151
+ }
152
+
153
+ // src/dev/ui/preview.ts
154
+ function getPreviewHtml(entry, prefix) {
155
+ const from = entry.envelope.from ? `${entry.envelope.from.name || ""} &lt;${entry.envelope.from.address}&gt;` : "Unknown";
156
+ const to = entry.envelope.to?.map((t) => t.address).join(", ") || "Unknown";
157
+ const content = `
158
+ <div class="header">
159
+ <div class="title">
160
+ <a href="${prefix}" class="btn">\u2190 Back</a>
161
+ <span style="margin-left: 10px">Email Preview</span>
162
+ </div>
163
+ <button onclick="deleteEmail('${entry.id}')" class="btn btn-danger">Delete</button>
164
+ </div>
165
+
166
+ <div class="card" style="margin-bottom: 20px; padding: 20px;">
167
+ <div style="font-size: 18px; font-weight: bold; margin-bottom: 10px;">${entry.envelope.subject || "(No Subject)"}</div>
168
+ <div class="meta" style="margin-bottom: 5px;">From: ${from}</div>
169
+ <div class="meta" style="margin-bottom: 5px;">To: ${to}</div>
170
+ <div class="meta">Date: ${entry.sentAt.toLocaleString()}</div>
171
+ </div>
172
+
173
+ <div style="margin-bottom: 10px;">
174
+ <button onclick="setView('html')" id="btn-html" class="btn btn-primary">HTML</button>
175
+ <button onclick="setView('text')" id="btn-text" class="btn">Text</button>
176
+ <button onclick="setView('raw')" id="btn-raw" class="btn">Raw JSON</button>
177
+ <a href="${prefix}/${entry.id}/html" target="_blank" class="btn">Open HTML \u2197</a>
178
+ </div>
179
+
180
+ <div class="card" style="height: 600px; display: flex;">
181
+ <iframe id="preview-frame" src="${prefix}/${entry.id}/html" style="width: 100%; height: 100%; border: none;"></iframe>
182
+ <pre id="raw-view" style="display: none; padding: 20px; overflow: auto; width: 100%;">${JSON.stringify(entry, null, 2)}</pre>
183
+ <pre id="text-view" style="display: none; padding: 20px; overflow: auto; width: 100%; white-space: pre-wrap; font-family: monospace;">${entry.text || "No text content"}</pre>
184
+ </div>
185
+
186
+ <script>
187
+ function setView(mode) {
188
+ document.getElementById('preview-frame').style.display = mode === 'html' ? 'block' : 'none';
189
+ document.getElementById('raw-view').style.display = mode === 'raw' ? 'block' : 'none';
190
+ document.getElementById('text-view').style.display = mode === 'text' ? 'block' : 'none';
191
+
192
+ document.getElementById('btn-html').className = mode === 'html' ? 'btn btn-primary' : 'btn';
193
+ document.getElementById('btn-text').className = mode === 'text' ? 'btn btn-primary' : 'btn';
194
+ document.getElementById('btn-raw').className = mode === 'raw' ? 'btn btn-primary' : 'btn';
195
+ }
196
+
197
+ async function deleteEmail(id) {
198
+ if (!confirm('Delete this email?')) return;
199
+ await fetch('${prefix}/' + id, { method: 'DELETE' });
200
+ window.location.href = '${prefix}';
201
+ }
202
+ </script>
203
+ `;
204
+ return layout(entry.envelope.subject || "Preview", content);
205
+ }
206
+
207
+ // src/dev/DevServer.ts
208
+ var DevServer = class {
209
+ constructor(mailbox, base = "/__mail") {
210
+ this.mailbox = mailbox;
211
+ this.base = base;
212
+ }
213
+ register(core) {
214
+ const router = core.router;
215
+ const prefix = this.base.replace(/\/$/, "");
216
+ router.get(prefix, (ctx) => {
217
+ const entries = this.mailbox.list();
218
+ return ctx.html(getMailboxHtml(entries, prefix));
219
+ });
220
+ router.get(`${prefix}/:id`, (ctx) => {
221
+ const id = ctx.req.param("id");
222
+ const entry = this.mailbox.get(id);
223
+ if (!entry) {
224
+ return ctx.text("Email not found", 404);
225
+ }
226
+ return ctx.html(getPreviewHtml(entry, prefix));
227
+ });
228
+ router.get(`${prefix}/:id/html`, (ctx) => {
229
+ const id = ctx.req.param("id");
230
+ const entry = this.mailbox.get(id);
231
+ if (!entry) {
232
+ return ctx.text("Not found", 404);
233
+ }
234
+ return ctx.html(entry.html);
235
+ });
236
+ router.get(`${prefix}/:id/text`, (ctx) => {
237
+ const id = ctx.req.param("id");
238
+ const entry = this.mailbox.get(id);
239
+ if (!entry) {
240
+ return ctx.text("Not found", 404);
241
+ }
242
+ return ctx.text(entry.text || "No text content", 200, {
243
+ "Content-Type": "text/plain; charset=utf-8"
244
+ });
245
+ });
246
+ router.get(`${prefix}/:id/raw`, (ctx) => {
247
+ const id = ctx.req.param("id");
248
+ const entry = this.mailbox.get(id);
249
+ if (!entry) {
250
+ return ctx.json({ error: "Not found" }, 404);
251
+ }
252
+ return ctx.json(entry);
253
+ });
254
+ router.get(`${prefix}/:id/delete`, (ctx) => {
255
+ return ctx.text("Method not allowed", 405);
256
+ });
257
+ router.delete(`${prefix}/:id`, (ctx) => {
258
+ const success = this.mailbox.delete(ctx.req.param("id"));
259
+ return ctx.json({ success });
260
+ });
261
+ router.delete(prefix, (ctx) => {
262
+ this.mailbox.clear();
263
+ return ctx.json({ success: true });
264
+ });
265
+ core.logger.info(`[OrbitSignal] Dev Mailbox available at ${prefix}`);
266
+ }
267
+ };
268
+
269
+ // src/transports/LogTransport.ts
270
+ var LogTransport = class {
271
+ async send(message) {
272
+ console.log("\n\u{1F4E7} [OrbitSignal] Email Sent (Simulated):");
273
+ console.log("\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501");
274
+ console.log(
275
+ `From: ${message.from.name ? `${message.from.name} <${message.from.address}>` : message.from.address}`
276
+ );
277
+ console.log(`To: ${message.to.map((t) => t.address).join(", ")}`);
278
+ console.log(`Subject: ${message.subject}`);
279
+ console.log("\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501");
280
+ console.log(`[Content Size]: ${message.html.length} chars (HTML)`);
281
+ console.log("\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\n");
282
+ }
283
+ };
284
+
285
+ // src/transports/MemoryTransport.ts
286
+ var MemoryTransport = class {
287
+ constructor(mailbox) {
288
+ this.mailbox = mailbox;
289
+ }
290
+ async send(message) {
291
+ this.mailbox.add(message);
292
+ }
293
+ };
294
+
295
+ // src/OrbitSignal.ts
296
+ var OrbitSignal = class _OrbitSignal {
297
+ static instance;
298
+ config;
299
+ devMailbox;
300
+ constructor(config = {}) {
301
+ this.config = config;
302
+ _OrbitSignal.instance = this;
303
+ }
304
+ /**
305
+ * Get the singleton instance of OrbitSignal
306
+ */
307
+ static getInstance() {
308
+ if (!_OrbitSignal.instance) {
309
+ throw new Error("OrbitSignal has not been initialized. Call OrbitSignal.configure() first.");
310
+ }
311
+ return _OrbitSignal.instance;
312
+ }
313
+ /**
314
+ * Configure the OrbitSignal instance
315
+ */
316
+ static configure(config) {
317
+ if (!config.transport && !config.devMode) {
318
+ console.warn("[OrbitSignal] No transport provided, falling back to LogTransport");
319
+ config.transport = new LogTransport();
320
+ }
321
+ return new _OrbitSignal(config);
322
+ }
323
+ /**
324
+ * Install the orbit into PlanetCore
325
+ */
326
+ install(core) {
327
+ core.logger.info("[OrbitSignal] Initializing Mail Service (Exposed as: mail)");
328
+ if (!this.config.transport && !this.config.devMode) {
329
+ this.config.transport = new LogTransport();
330
+ }
331
+ if (this.config.devMode) {
332
+ this.devMailbox = new DevMailbox();
333
+ this.config.transport = new MemoryTransport(this.devMailbox);
334
+ core.logger.info("[OrbitSignal] Dev Mode Enabled: Emails will be intercepted to Dev Mailbox");
335
+ const devServer = new DevServer(this.devMailbox, this.config.devUiPrefix || "/__mail");
336
+ devServer.register(core);
337
+ }
338
+ core.adapter.use("*", async (c, next) => {
339
+ c.set("mail", {
340
+ send: (mailable) => this.send(mailable),
341
+ queue: (mailable) => this.queue(mailable)
342
+ });
343
+ await next();
344
+ return void 0;
345
+ });
346
+ }
347
+ /**
348
+ * Send a mailable instance
349
+ */
350
+ async send(mailable) {
351
+ const envelope = await mailable.buildEnvelope(this.config);
352
+ if (!envelope.from) {
353
+ throw new Error('Message is missing "from" address');
354
+ }
355
+ if (!envelope.to || envelope.to.length === 0) {
356
+ throw new Error('Message is missing "to" address');
357
+ }
358
+ const content = await mailable.renderContent();
359
+ const message = {
360
+ ...envelope,
361
+ from: envelope.from,
362
+ to: envelope.to,
363
+ subject: envelope.subject || "(No Subject)",
364
+ priority: envelope.priority || "normal",
365
+ html: content.html
366
+ };
367
+ if (content.text) {
368
+ message.text = content.text;
369
+ }
370
+ if (!this.config.transport) {
371
+ throw new Error(
372
+ "[OrbitSignal] No transport configured. Did you call configure() or register the orbit?"
373
+ );
374
+ }
375
+ await this.config.transport.send(message);
376
+ }
377
+ /**
378
+ * Queue a mailable instance
379
+ *
380
+ * Push a mailable into the queue for execution.
381
+ * Requires OrbitStream to be installed and available in the context.
382
+ */
383
+ async queue(mailable) {
384
+ const queue = this.queueService;
385
+ if (queue) {
386
+ await queue.push(mailable);
387
+ } else {
388
+ console.warn(
389
+ "[OrbitSignal] Queue service not available, sending immediately. Install OrbitStream to enable queuing."
390
+ );
391
+ await this.send(mailable);
392
+ }
393
+ }
394
+ };
395
+
396
+ export {
397
+ DevMailbox,
398
+ LogTransport,
399
+ MemoryTransport,
400
+ OrbitSignal
401
+ };
@@ -0,0 +1,15 @@
1
+ var __getOwnPropNames = Object.getOwnPropertyNames;
2
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
3
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
4
+ }) : x)(function(x) {
5
+ if (typeof require !== "undefined") return require.apply(this, arguments);
6
+ throw Error('Dynamic require of "' + x + '" is not supported');
7
+ });
8
+ var __commonJS = (cb, mod) => function __require2() {
9
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
+ };
11
+
12
+ export {
13
+ __require,
14
+ __commonJS
15
+ };