@c9up/rover 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/BaseMail.d.ts +30 -1
- package/dist/BaseMail.d.ts.map +1 -1
- package/dist/BaseMail.js +30 -0
- package/dist/BaseMail.js.map +1 -1
- package/dist/Mail.d.ts +32 -1
- package/dist/Mail.d.ts.map +1 -1
- package/dist/Mail.js +35 -1
- package/dist/Mail.js.map +1 -1
- package/dist/MessageBuilder.d.ts +197 -7
- package/dist/MessageBuilder.d.ts.map +1 -1
- package/dist/MessageBuilder.js +402 -8
- package/dist/MessageBuilder.js.map +1 -1
- package/dist/RoverProvider.d.ts +1 -1
- package/dist/RoverProvider.d.ts.map +1 -1
- package/dist/RoverProvider.js +6 -6
- package/dist/RoverProvider.js.map +1 -1
- package/dist/config.d.ts +38 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +34 -0
- package/dist/config.js.map +1 -1
- package/dist/format.d.ts +10 -0
- package/dist/format.d.ts.map +1 -1
- package/dist/format.js +28 -1
- package/dist/format.js.map +1 -1
- package/dist/index.d.ts +18 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -1
- package/dist/index.js.map +1 -1
- package/dist/testing/FakeMail.d.ts +33 -0
- package/dist/testing/FakeMail.d.ts.map +1 -1
- package/dist/testing/FakeMail.js +28 -0
- package/dist/testing/FakeMail.js.map +1 -1
- package/dist/transports/BrevoTransport.d.ts.map +1 -1
- package/dist/transports/BrevoTransport.js +5 -4
- package/dist/transports/BrevoTransport.js.map +1 -1
- package/dist/transports/MailgunTransport.d.ts.map +1 -1
- package/dist/transports/MailgunTransport.js +3 -2
- package/dist/transports/MailgunTransport.js.map +1 -1
- package/dist/transports/ResendTransport.d.ts.map +1 -1
- package/dist/transports/ResendTransport.js +5 -4
- package/dist/transports/ResendTransport.js.map +1 -1
- package/dist/transports/SendGridTransport.d.ts.map +1 -1
- package/dist/transports/SendGridTransport.js +3 -2
- package/dist/transports/SendGridTransport.js.map +1 -1
- package/dist/transports/SesTransport.d.ts.map +1 -1
- package/dist/transports/SesTransport.js +7 -5
- package/dist/transports/SesTransport.js.map +1 -1
- package/dist/transports/SparkPostTransport.d.ts.map +1 -1
- package/dist/transports/SparkPostTransport.js +5 -4
- package/dist/transports/SparkPostTransport.js.map +1 -1
- package/dist/transports/fetchError.d.ts +10 -0
- package/dist/transports/fetchError.d.ts.map +1 -1
- package/dist/transports/fetchError.js +30 -0
- package/dist/transports/fetchError.js.map +1 -1
- package/index.darwin-arm64.node +0 -0
- package/index.darwin-x64.node +0 -0
- package/index.linux-arm64-gnu.node +0 -0
- package/index.linux-x64-gnu.node +0 -0
- package/index.win32-x64-msvc.node +0 -0
- package/package.json +6 -1
- package/src/BaseMail.ts +40 -1
- package/src/Mail.ts +55 -2
- package/src/MessageBuilder.ts +561 -11
- package/src/RoverProvider.ts +10 -7
- package/src/config.ts +46 -0
- package/src/format.ts +33 -1
- package/src/index.ts +28 -2
- package/src/testing/FakeMail.ts +46 -0
- package/src/transports/BrevoTransport.ts +5 -4
- package/src/transports/MailgunTransport.ts +3 -2
- package/src/transports/ResendTransport.ts +5 -4
- package/src/transports/SendGridTransport.ts +3 -2
- package/src/transports/SesTransport.ts +7 -5
- package/src/transports/SparkPostTransport.ts +15 -10
- package/src/transports/fetchError.ts +39 -0
package/src/MessageBuilder.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { basename } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
1
4
|
import { formatAddress } from "./format.js";
|
|
5
|
+
import { RoverError } from "./RoverError.js";
|
|
2
6
|
import { renderFile as renderTemplateFile } from "./templating/SimpleTemplate.js";
|
|
3
7
|
|
|
4
8
|
export interface MailMessage {
|
|
@@ -20,14 +24,83 @@ export interface MailMessage {
|
|
|
20
24
|
inReplyTo?: string;
|
|
21
25
|
/** `References` header — the thread's message ids. */
|
|
22
26
|
references?: string[];
|
|
27
|
+
/** SMTP envelope, when it differs from the visible From/To headers. */
|
|
28
|
+
envelope?: MailEnvelope;
|
|
29
|
+
/** Body transfer encoding (nodemailer `encoding`). SMTP only. */
|
|
30
|
+
encoding?: string;
|
|
31
|
+
/** RFC 2369 `List-*` headers, keyed WITHOUT the `List-` prefix. */
|
|
32
|
+
list?: Record<string, ListHeader | ListHeader[] | ListHeader[][]>;
|
|
33
|
+
/** A calendar invitation carried as `text/calendar` (nodemailer `icalEvent`). */
|
|
34
|
+
icalEvent?: CalendarEvent;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* One `List-*` header value: a bare URL, or a URL with a human comment.
|
|
39
|
+
*
|
|
40
|
+
* `comment` is REQUIRED in the object form, as it is in nodemailer and
|
|
41
|
+
* AdonisJS — a URL without a comment is the bare string form.
|
|
42
|
+
*/
|
|
43
|
+
export type ListHeader = string | { url: string; comment: string };
|
|
44
|
+
|
|
45
|
+
/** How the receiving client should treat the invitation (RFC 5546). */
|
|
46
|
+
export type CalendarEventMethod =
|
|
47
|
+
| "PUBLISH"
|
|
48
|
+
| "REQUEST"
|
|
49
|
+
| "REPLY"
|
|
50
|
+
| "ADD"
|
|
51
|
+
| "CANCEL"
|
|
52
|
+
| "REFRESH"
|
|
53
|
+
| "COUNTER"
|
|
54
|
+
| "DECLINECOUNTER";
|
|
55
|
+
|
|
56
|
+
/** Options shared by the three `icalEvent*` forms (AdonisJS `CalendarEventOptions`). */
|
|
57
|
+
export interface CalendarEventOptions {
|
|
58
|
+
method?: CalendarEventMethod;
|
|
59
|
+
filename?: string;
|
|
60
|
+
encoding?: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A calendar invitation. Exactly one source: inline `content`, a `path` read at
|
|
65
|
+
* {@link MessageBuilder.build} time, or an `href` the provider fetches.
|
|
66
|
+
*/
|
|
67
|
+
export interface CalendarEvent extends CalendarEventOptions {
|
|
68
|
+
content?: string;
|
|
69
|
+
path?: string;
|
|
70
|
+
href?: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The addresses the mail SERVERS use, as distinct from the visible headers. */
|
|
74
|
+
export interface MailEnvelope {
|
|
75
|
+
from?: string;
|
|
76
|
+
to?: string | string[];
|
|
77
|
+
cc?: string | string[];
|
|
78
|
+
bcc?: string | string[];
|
|
23
79
|
}
|
|
24
80
|
|
|
25
81
|
export interface MailAttachment {
|
|
26
82
|
filename: string;
|
|
27
83
|
content: Buffer | string;
|
|
28
84
|
contentType?: string;
|
|
29
|
-
/** Content-ID for inline (CID) embedding — set via `embedData()`. */
|
|
85
|
+
/** Content-ID for inline (CID) embedding — set via `embed()` / `embedData()`. */
|
|
30
86
|
cid?: string;
|
|
87
|
+
/** Source path, kept so `hasAttachment(file)` can answer by path. */
|
|
88
|
+
path?: string;
|
|
89
|
+
/** `Content-Disposition`, when it is not the default for the form used. */
|
|
90
|
+
contentDisposition?: "attachment" | "inline";
|
|
91
|
+
/** `Content-Transfer-Encoding` for this part. */
|
|
92
|
+
encoding?: string;
|
|
93
|
+
/** Extra part headers. */
|
|
94
|
+
headers?: Record<string, string | string[]>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** What the `attach*` / `embed*` methods accept (AdonisJS `AttachmentOptions`). */
|
|
98
|
+
export interface AttachmentOptions {
|
|
99
|
+
filename?: string;
|
|
100
|
+
contentType?: string;
|
|
101
|
+
contentDisposition?: "attachment" | "inline";
|
|
102
|
+
encoding?: string;
|
|
103
|
+
headers?: Record<string, string | string[]>;
|
|
31
104
|
}
|
|
32
105
|
|
|
33
106
|
/**
|
|
@@ -41,6 +114,100 @@ export interface RecipientObject {
|
|
|
41
114
|
|
|
42
115
|
export type Recipient = string | RecipientObject;
|
|
43
116
|
|
|
117
|
+
/** Whether `list` holds `address`, or anything at all when it is omitted. */
|
|
118
|
+
function contains(list: readonly string[], address?: string): boolean {
|
|
119
|
+
if (address === undefined) return list.length > 0;
|
|
120
|
+
// Addresses are stored formatted (`"Name" <a@b.c>`), so an assertion on the
|
|
121
|
+
// bare address has to match inside the display form too.
|
|
122
|
+
return list.some(
|
|
123
|
+
(entry) => entry === address || entry.includes(`<${address}>`),
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The parts a transport with no calendar field must send: the declared
|
|
129
|
+
* attachments, plus the invitation rendered as a `text/calendar` part.
|
|
130
|
+
*
|
|
131
|
+
* Only nodemailer has a native `icalEvent`; the provider HTTP APIs carry an
|
|
132
|
+
* invitation the way every mail client reads it anyway — as an attachment with
|
|
133
|
+
* the right media type and `method` parameter.
|
|
134
|
+
*/
|
|
135
|
+
export function attachmentsFor(message: MailMessage): MailAttachment[] {
|
|
136
|
+
const ical = message.icalEvent;
|
|
137
|
+
if (ical === undefined) return message.attachments;
|
|
138
|
+
if (ical.content === undefined) {
|
|
139
|
+
// `icalEventFromUrl` leaves only an href, which nodemailer fetches for
|
|
140
|
+
// SMTP. An HTTP provider takes the bytes, and silently dropping the
|
|
141
|
+
// invitation would be worse than saying so.
|
|
142
|
+
throw new RoverError(
|
|
143
|
+
"ICAL_HREF_UNSUPPORTED",
|
|
144
|
+
"icalEventFromUrl() is only supported by the SMTP transport, which fetches the URL itself.",
|
|
145
|
+
{
|
|
146
|
+
hint: "Fetch the ICS yourself and pass it to icalEvent(contents), or use icalEventFromFile().",
|
|
147
|
+
},
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
const method = ical.method ?? "PUBLISH";
|
|
151
|
+
return [
|
|
152
|
+
...message.attachments,
|
|
153
|
+
{
|
|
154
|
+
filename: ical.filename ?? "invite.ics",
|
|
155
|
+
content: ical.content,
|
|
156
|
+
contentType: `text/calendar; charset=utf-8; method=${method}`,
|
|
157
|
+
encoding: ical.encoding,
|
|
158
|
+
},
|
|
159
|
+
];
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Read a file declared by `attach()` / `embed()` / `icalEventFromFile()`. */
|
|
163
|
+
async function readAttachment(path: string, label: string): Promise<Buffer> {
|
|
164
|
+
try {
|
|
165
|
+
return await readFile(path);
|
|
166
|
+
} catch (err) {
|
|
167
|
+
throw new RoverError(
|
|
168
|
+
"ATTACHMENT_UNREADABLE",
|
|
169
|
+
`Could not read ${label} from "${path}": ${err instanceof Error ? err.message : String(err)}`,
|
|
170
|
+
{
|
|
171
|
+
hint: "Give an absolute path, or attach the bytes with attachData() / embedData().",
|
|
172
|
+
},
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Every URL inside a `List-*` value, whatever nesting form it was written in. */
|
|
178
|
+
function listUrls(value: ListHeader | ListHeader[] | ListHeader[][]): string[] {
|
|
179
|
+
if (typeof value === "string") return [value];
|
|
180
|
+
if (Array.isArray(value)) return value.flatMap((entry) => listUrls(entry));
|
|
181
|
+
return [value.url];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** `unsubscribe` → `Unsubscribe`, `unsubscribe-post` → `Unsubscribe-Post`. */
|
|
185
|
+
function titleCaseKey(key: string): string {
|
|
186
|
+
return key
|
|
187
|
+
.split("-")
|
|
188
|
+
.map((part) => (part ? part[0].toUpperCase() + part.slice(1) : part))
|
|
189
|
+
.join("-");
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Render one `List-*` value the way RFC 2369 writes it: `<url> (comment)`. */
|
|
193
|
+
function renderListHeader(
|
|
194
|
+
value: ListHeader | ListHeader[] | ListHeader[][],
|
|
195
|
+
): string {
|
|
196
|
+
if (typeof value === "string") return `<${value}>`;
|
|
197
|
+
if (Array.isArray(value)) {
|
|
198
|
+
return value.map((entry) => renderListHeader(entry)).join(", ");
|
|
199
|
+
}
|
|
200
|
+
return value.comment ? `<${value.url}> (${value.comment})` : `<${value.url}>`;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function expect(passed: boolean, expectation: string, actual: unknown): void {
|
|
204
|
+
if (passed) return;
|
|
205
|
+
throw new RoverError(
|
|
206
|
+
"ASSERTION_FAILED",
|
|
207
|
+
`Expected the message ${expectation}, got ${JSON.stringify(actual)}`,
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
44
211
|
export class MessageBuilder {
|
|
45
212
|
#msg: MailMessage = {
|
|
46
213
|
from: "",
|
|
@@ -52,6 +219,196 @@ export class MessageBuilder {
|
|
|
52
219
|
headers: {},
|
|
53
220
|
};
|
|
54
221
|
#pendingView: { path: string; data: Record<string, unknown> } | null = null;
|
|
222
|
+
#pendingTextView: { path: string; data: Record<string, unknown> } | null =
|
|
223
|
+
null;
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Render a template as the PLAIN-TEXT body (AdonisJS `textView`).
|
|
227
|
+
*
|
|
228
|
+
* The counterpart of `htmlView`. A message with only an HTML part scores
|
|
229
|
+
* worse with spam filters and is unreadable in a text-only client, which is
|
|
230
|
+
* why upstream offers both.
|
|
231
|
+
*/
|
|
232
|
+
textView(path: string, data: Record<string, unknown> = {}): this {
|
|
233
|
+
this.#pendingTextView = { path, data };
|
|
234
|
+
return this;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Override the SMTP envelope — who the message is really from and to, as
|
|
239
|
+
* far as the mail servers are concerned (AdonisJS `envelope`).
|
|
240
|
+
*
|
|
241
|
+
* Distinct from the `From`/`To` HEADERS: a bounce goes to the envelope
|
|
242
|
+
* sender, which is how VERP and mailing lists route failures away from the
|
|
243
|
+
* visible author.
|
|
244
|
+
*/
|
|
245
|
+
envelope(envelope: MailEnvelope): this {
|
|
246
|
+
this.#msg.envelope = envelope;
|
|
247
|
+
return this;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* The message as built so far — what the `has*` / `assert*` helpers read.
|
|
252
|
+
*
|
|
253
|
+
* Exposed because a test asserts against a mail it never sent, and the
|
|
254
|
+
* alternative is rebuilding the message just to look at it.
|
|
255
|
+
*/
|
|
256
|
+
toObject(): Readonly<MailMessage> {
|
|
257
|
+
return this.#msg;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
toJSON(): Readonly<MailMessage> {
|
|
261
|
+
return this.toObject();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// ── Inspection ────────────────────────────────────────────────────────
|
|
265
|
+
// `has*` answers, `assert*` throws. Both exist because a test reads better
|
|
266
|
+
// as an assertion and a conditional reads better as a question.
|
|
267
|
+
|
|
268
|
+
hasTo(address?: string): boolean {
|
|
269
|
+
return contains(this.#msg.to, address);
|
|
270
|
+
}
|
|
271
|
+
hasCc(address?: string): boolean {
|
|
272
|
+
return contains(this.#msg.cc, address);
|
|
273
|
+
}
|
|
274
|
+
hasBcc(address?: string): boolean {
|
|
275
|
+
return contains(this.#msg.bcc, address);
|
|
276
|
+
}
|
|
277
|
+
hasFrom(address?: string): boolean {
|
|
278
|
+
return contains(this.#msg.from ? [this.#msg.from] : [], address);
|
|
279
|
+
}
|
|
280
|
+
hasReplyTo(address?: string): boolean {
|
|
281
|
+
return contains(this.#msg.replyTo ? [this.#msg.replyTo] : [], address);
|
|
282
|
+
}
|
|
283
|
+
hasSubject(subject?: string): boolean {
|
|
284
|
+
if (subject === undefined) return this.#msg.subject !== "";
|
|
285
|
+
return this.#msg.subject === subject;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Whether the message carries an attachment — any at all, one with this
|
|
289
|
+
* filename or source path, or one a predicate accepts (AdonisJS
|
|
290
|
+
* `hasAttachment`, whose overloads are the same three).
|
|
291
|
+
*/
|
|
292
|
+
hasAttachment(
|
|
293
|
+
match?: string | URL | ((attachment: MailAttachment) => boolean),
|
|
294
|
+
): boolean {
|
|
295
|
+
if (match === undefined) return this.#msg.attachments.length > 0;
|
|
296
|
+
if (typeof match === "function") return this.#msg.attachments.some(match);
|
|
297
|
+
const needle = match instanceof URL ? fileURLToPath(match) : match;
|
|
298
|
+
return this.#msg.attachments.some(
|
|
299
|
+
(a) => a.filename === needle || a.path === needle,
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Whether `address` is a recipient in ANY field (AdonisJS `hasRecipient`).
|
|
305
|
+
* Without one, whether the message has a recipient at all.
|
|
306
|
+
*/
|
|
307
|
+
hasRecipient(address?: string): boolean {
|
|
308
|
+
return this.hasTo(address) || this.hasCc(address) || this.hasBcc(address);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Whether the given text appears in the HTML body or the plain-text one
|
|
313
|
+
* (AdonisJS `hasContent`). The field-specific assertions are
|
|
314
|
+
* {@link assertHtmlIncludes} and {@link assertTextIncludes}.
|
|
315
|
+
*/
|
|
316
|
+
hasContent(needle: string): boolean {
|
|
317
|
+
return (
|
|
318
|
+
(this.#msg.html?.includes(needle) ?? false) ||
|
|
319
|
+
(this.#msg.text?.includes(needle) ?? false)
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** Whether a `List-<key>` header was defined. */
|
|
324
|
+
hasListHeader(key: string, url?: string): boolean {
|
|
325
|
+
const value = this.#msg.list?.[key];
|
|
326
|
+
if (value === undefined) return false;
|
|
327
|
+
if (url === undefined) return true;
|
|
328
|
+
return listUrls(value).includes(url);
|
|
329
|
+
}
|
|
330
|
+
hasHeader(name: string, value?: string): boolean {
|
|
331
|
+
const found = this.#msg.headers[name];
|
|
332
|
+
if (found === undefined) return false;
|
|
333
|
+
if (value === undefined) return true;
|
|
334
|
+
return Array.isArray(found) ? found.includes(value) : found === value;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
assertTo(address: string): void {
|
|
338
|
+
expect(this.hasTo(address), `to include "${address}"`, this.#msg.to);
|
|
339
|
+
}
|
|
340
|
+
assertFrom(address: string): void {
|
|
341
|
+
expect(this.hasFrom(address), `from to be "${address}"`, this.#msg.from);
|
|
342
|
+
}
|
|
343
|
+
assertCc(address: string): void {
|
|
344
|
+
expect(this.hasCc(address), `cc to include "${address}"`, this.#msg.cc);
|
|
345
|
+
}
|
|
346
|
+
assertBcc(address: string): void {
|
|
347
|
+
expect(this.hasBcc(address), `bcc to include "${address}"`, this.#msg.bcc);
|
|
348
|
+
}
|
|
349
|
+
assertReplyTo(address: string): void {
|
|
350
|
+
expect(
|
|
351
|
+
this.hasReplyTo(address),
|
|
352
|
+
`replyTo to be "${address}"`,
|
|
353
|
+
this.#msg.replyTo,
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
assertSubject(subject: string): void {
|
|
357
|
+
expect(
|
|
358
|
+
this.hasSubject(subject),
|
|
359
|
+
`subject to be "${subject}"`,
|
|
360
|
+
this.#msg.subject,
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
assertAttachment(
|
|
364
|
+
match: string | URL | ((attachment: MailAttachment) => boolean),
|
|
365
|
+
): void {
|
|
366
|
+
expect(
|
|
367
|
+
this.hasAttachment(match),
|
|
368
|
+
typeof match === "function"
|
|
369
|
+
? "an attachment matching the predicate"
|
|
370
|
+
: `an attachment named "${String(match)}"`,
|
|
371
|
+
this.#msg.attachments.map((a) => a.path ?? a.filename),
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** `address` is a recipient in some field (AdonisJS `assertRecipient`). */
|
|
376
|
+
assertRecipient(address: string): void {
|
|
377
|
+
expect(this.hasRecipient(address), `to reach "${address}"`, {
|
|
378
|
+
to: this.#msg.to,
|
|
379
|
+
cc: this.#msg.cc,
|
|
380
|
+
bcc: this.#msg.bcc,
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** The text appears in the HTML or the plain-text body (AdonisJS `assertContent`). */
|
|
385
|
+
assertContent(needle: string): void {
|
|
386
|
+
expect(this.hasContent(needle), `to contain "${needle}"`, {
|
|
387
|
+
html: this.#msg.html,
|
|
388
|
+
text: this.#msg.text,
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
assertHeader(name: string, value?: string): void {
|
|
392
|
+
expect(
|
|
393
|
+
this.hasHeader(name, value),
|
|
394
|
+
value === undefined ? `a "${name}" header` : `${name}: ${value}`,
|
|
395
|
+
this.#msg.headers[name],
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
assertHtmlIncludes(substring: string): void {
|
|
399
|
+
expect(
|
|
400
|
+
(this.#msg.html ?? "").includes(substring),
|
|
401
|
+
`html to include "${substring}"`,
|
|
402
|
+
this.#msg.html,
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
assertTextIncludes(substring: string): void {
|
|
406
|
+
expect(
|
|
407
|
+
(this.#msg.text ?? "").includes(substring),
|
|
408
|
+
`text to include "${substring}"`,
|
|
409
|
+
this.#msg.text,
|
|
410
|
+
);
|
|
411
|
+
}
|
|
55
412
|
|
|
56
413
|
from(address: string, name?: string): this {
|
|
57
414
|
this.#msg.from = formatAddress(address, name);
|
|
@@ -121,23 +478,85 @@ export class MessageBuilder {
|
|
|
121
478
|
return this;
|
|
122
479
|
}
|
|
123
480
|
|
|
124
|
-
|
|
125
|
-
|
|
481
|
+
/**
|
|
482
|
+
* Attach a FILE by path or `file://` URL (AdonisJS `attach`). The bytes are
|
|
483
|
+
* read at {@link build} time, so the fluent chain stays synchronous.
|
|
484
|
+
*
|
|
485
|
+
* The filename defaults to the file's own basename. For bytes you already
|
|
486
|
+
* hold, use {@link attachData}.
|
|
487
|
+
*/
|
|
488
|
+
attach(file: string | URL, options?: AttachmentOptions): this {
|
|
489
|
+
const path = file instanceof URL ? fileURLToPath(file) : file;
|
|
490
|
+
this.#msg.attachments.push({
|
|
491
|
+
filename: options?.filename ?? basename(path),
|
|
492
|
+
// Filled in by `build()`; an unread attachment must never ship as an
|
|
493
|
+
// empty part, so `build()` failing to read is an error, not a warning.
|
|
494
|
+
content: "",
|
|
495
|
+
path,
|
|
496
|
+
contentType: options?.contentType,
|
|
497
|
+
contentDisposition: options?.contentDisposition,
|
|
498
|
+
encoding: options?.encoding,
|
|
499
|
+
headers: options?.headers,
|
|
500
|
+
});
|
|
501
|
+
return this;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Attach bytes you already hold (AdonisJS `attachData`). `filename` is
|
|
506
|
+
* required — there is no path to take it from.
|
|
507
|
+
*/
|
|
508
|
+
attachData(
|
|
126
509
|
content: Buffer | string,
|
|
127
|
-
|
|
510
|
+
options: AttachmentOptions & { filename: string },
|
|
128
511
|
): this {
|
|
129
|
-
this.#msg.attachments.push({
|
|
512
|
+
this.#msg.attachments.push({
|
|
513
|
+
filename: options.filename,
|
|
514
|
+
content,
|
|
515
|
+
contentType: options.contentType,
|
|
516
|
+
contentDisposition: options.contentDisposition,
|
|
517
|
+
encoding: options.encoding,
|
|
518
|
+
headers: options.headers,
|
|
519
|
+
});
|
|
520
|
+
return this;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Embed a FILE inline, referenced by `cid:<cid>` in the HTML body (AdonisJS
|
|
525
|
+
* `embed`). Read at {@link build} time, like {@link attach}.
|
|
526
|
+
*/
|
|
527
|
+
embed(file: string | URL, cid: string, options?: AttachmentOptions): this {
|
|
528
|
+
const path = file instanceof URL ? fileURLToPath(file) : file;
|
|
529
|
+
this.#msg.attachments.push({
|
|
530
|
+
filename: options?.filename ?? basename(path),
|
|
531
|
+
content: "",
|
|
532
|
+
path,
|
|
533
|
+
cid,
|
|
534
|
+
contentType: options?.contentType,
|
|
535
|
+
contentDisposition: options?.contentDisposition ?? "inline",
|
|
536
|
+
encoding: options?.encoding,
|
|
537
|
+
headers: options?.headers,
|
|
538
|
+
});
|
|
130
539
|
return this;
|
|
131
540
|
}
|
|
132
541
|
|
|
133
542
|
/**
|
|
134
|
-
* Embed
|
|
135
|
-
*
|
|
136
|
-
* path-based `embed(file, cid)` form from `@adonisjs/mail` is a deliberate
|
|
137
|
-
* divergence — pass the bytes directly instead.
|
|
543
|
+
* Embed bytes you already hold, referenced by `cid:<cid>` in the HTML body
|
|
544
|
+
* (AdonisJS `embedData`).
|
|
138
545
|
*/
|
|
139
|
-
embedData(
|
|
140
|
-
|
|
546
|
+
embedData(
|
|
547
|
+
content: Buffer | string,
|
|
548
|
+
cid: string,
|
|
549
|
+
options?: AttachmentOptions,
|
|
550
|
+
): this {
|
|
551
|
+
this.#msg.attachments.push({
|
|
552
|
+
filename: options?.filename ?? cid,
|
|
553
|
+
content,
|
|
554
|
+
cid,
|
|
555
|
+
contentType: options?.contentType,
|
|
556
|
+
contentDisposition: options?.contentDisposition ?? "inline",
|
|
557
|
+
encoding: options?.encoding,
|
|
558
|
+
headers: options?.headers,
|
|
559
|
+
});
|
|
141
560
|
return this;
|
|
142
561
|
}
|
|
143
562
|
|
|
@@ -146,6 +565,105 @@ export class MessageBuilder {
|
|
|
146
565
|
return this;
|
|
147
566
|
}
|
|
148
567
|
|
|
568
|
+
/**
|
|
569
|
+
* Body transfer encoding (AdonisJS `encoding`) — `7bit`, `base64`,
|
|
570
|
+
* `quoted-printable`… SMTP only: the provider HTTP APIs encode the payload
|
|
571
|
+
* themselves and expose no equivalent.
|
|
572
|
+
*/
|
|
573
|
+
encoding(encoding: string): this {
|
|
574
|
+
this.#msg.encoding = encoding;
|
|
575
|
+
return this;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// ── RFC 2369 List-* headers ───────────────────────────────────────────
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* Define a `List-<key>` header (AdonisJS `addListHeader`). `key` carries no
|
|
582
|
+
* `List-` prefix — `addListHeader('archive', url)` emits `List-Archive`.
|
|
583
|
+
* Calling it again for the same key replaces the value.
|
|
584
|
+
*/
|
|
585
|
+
addListHeader(
|
|
586
|
+
key: string,
|
|
587
|
+
value: ListHeader | ListHeader[] | ListHeader[][],
|
|
588
|
+
): this {
|
|
589
|
+
this.#msg.list ??= {};
|
|
590
|
+
this.#msg.list[key] = value;
|
|
591
|
+
return this;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* `List-Unsubscribe` (AdonisJS `listUnsubscribe`).
|
|
596
|
+
*
|
|
597
|
+
* `{ oneClick: true }` also emits the RFC 8058 `List-Unsubscribe-Post`
|
|
598
|
+
* header. Gmail and Yahoo require BOTH for bulk senders, and only a `https:`
|
|
599
|
+
* URL is a valid one-click target — a `mailto:` cannot answer a POST, so
|
|
600
|
+
* pairing them is refused rather than silently shipped.
|
|
601
|
+
*/
|
|
602
|
+
listUnsubscribe(
|
|
603
|
+
value: ListHeader | ListHeader[] | ListHeader[][],
|
|
604
|
+
options?: { oneClick?: boolean },
|
|
605
|
+
): this {
|
|
606
|
+
if (options?.oneClick === true) {
|
|
607
|
+
for (const url of listUrls(value)) {
|
|
608
|
+
if (!url.toLowerCase().startsWith("http")) {
|
|
609
|
+
throw new RoverError(
|
|
610
|
+
"INVALID_LIST_HEADER",
|
|
611
|
+
`listUnsubscribe({ oneClick: true }) needs an https URL that can answer a POST, got "${url}".`,
|
|
612
|
+
{
|
|
613
|
+
hint: "Keep the mailto: form without oneClick, or add an https endpoint alongside it.",
|
|
614
|
+
},
|
|
615
|
+
);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
// A RAW header, not a `List-*` entry: nodemailer wraps every list value
|
|
619
|
+
// in angle brackets, and `<List-Unsubscribe=One-Click>` is not what
|
|
620
|
+
// RFC 8058 specifies — receivers would ignore it.
|
|
621
|
+
this.#msg.headers["List-Unsubscribe-Post"] = "List-Unsubscribe=One-Click";
|
|
622
|
+
}
|
|
623
|
+
return this.addListHeader("unsubscribe", value);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/** `List-Subscribe` (AdonisJS `listSubscribe`). */
|
|
627
|
+
listSubscribe(value: ListHeader | ListHeader[] | ListHeader[][]): this {
|
|
628
|
+
return this.addListHeader("subscribe", value);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/** `List-Help` (AdonisJS `listHelp`). */
|
|
632
|
+
listHelp(value: ListHeader | ListHeader[] | ListHeader[][]): this {
|
|
633
|
+
return this.addListHeader("help", value);
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
// ── Calendar invitations ──────────────────────────────────────────────
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* Attach a calendar invitation from an ICS string (AdonisJS `icalEvent`).
|
|
640
|
+
*
|
|
641
|
+
* Named deviation: upstream also accepts a `(calendar: ICalCalendar) => void`
|
|
642
|
+
* builder, which is `ical-generator`'s API. rover carries no such
|
|
643
|
+
* dependency, so it takes the ICS text — produced by whichever generator you
|
|
644
|
+
* prefer. {@link icalEventFromFile} and {@link icalEventFromUrl} are the
|
|
645
|
+
* other two upstream forms, unchanged.
|
|
646
|
+
*/
|
|
647
|
+
icalEvent(contents: string, options?: CalendarEventOptions): this {
|
|
648
|
+
this.#msg.icalEvent = { ...options, content: contents };
|
|
649
|
+
return this;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/** Calendar invitation read from a file at {@link build} time (AdonisJS `icalEventFromFile`). */
|
|
653
|
+
icalEventFromFile(file: string | URL, options?: CalendarEventOptions): this {
|
|
654
|
+
this.#msg.icalEvent = {
|
|
655
|
+
...options,
|
|
656
|
+
path: file instanceof URL ? fileURLToPath(file) : file,
|
|
657
|
+
};
|
|
658
|
+
return this;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/** Calendar invitation the transport fetches from a URL (AdonisJS `icalEventFromUrl`). */
|
|
662
|
+
icalEventFromUrl(url: string, options?: CalendarEventOptions): this {
|
|
663
|
+
this.#msg.icalEvent = { ...options, href: url };
|
|
664
|
+
return this;
|
|
665
|
+
}
|
|
666
|
+
|
|
149
667
|
/**
|
|
150
668
|
* Queue an HTML template render. The render happens lazily at `build()` time
|
|
151
669
|
* so the fluent chain stays synchronous; `build()` is async and awaits the
|
|
@@ -172,6 +690,38 @@ export class MessageBuilder {
|
|
|
172
690
|
);
|
|
173
691
|
this.#pendingView = null;
|
|
174
692
|
}
|
|
693
|
+
if (this.#pendingTextView !== null) {
|
|
694
|
+
this.#msg.text = await renderTemplateFile(
|
|
695
|
+
this.#pendingTextView.path,
|
|
696
|
+
this.#pendingTextView.data,
|
|
697
|
+
undefined,
|
|
698
|
+
viewsRoot,
|
|
699
|
+
);
|
|
700
|
+
this.#pendingTextView = null;
|
|
701
|
+
}
|
|
702
|
+
// Path-based attachments and invitations are read here, not when they were
|
|
703
|
+
// declared, so the fluent chain stays synchronous. A read failure raises:
|
|
704
|
+
// an attachment the recipient expects must never ship as an empty part.
|
|
705
|
+
for (const attachment of this.#msg.attachments) {
|
|
706
|
+
if (attachment.path === undefined) continue;
|
|
707
|
+
attachment.content = await readAttachment(
|
|
708
|
+
attachment.path,
|
|
709
|
+
attachment.filename,
|
|
710
|
+
);
|
|
711
|
+
}
|
|
712
|
+
const ical = this.#msg.icalEvent;
|
|
713
|
+
if (ical?.path !== undefined && ical.content === undefined) {
|
|
714
|
+
ical.content = (
|
|
715
|
+
await readAttachment(ical.path, "the calendar event")
|
|
716
|
+
).toString("utf8");
|
|
717
|
+
}
|
|
718
|
+
// `List-*` headers are rendered here, once, rather than in each transport:
|
|
719
|
+
// every transport already forwards `headers`, and only nodemailer has a
|
|
720
|
+
// structured `list` field. `#msg.list` stays as the structured record the
|
|
721
|
+
// `hasListHeader` inspection reads.
|
|
722
|
+
for (const [key, value] of Object.entries(this.#msg.list ?? {})) {
|
|
723
|
+
this.#msg.headers[`List-${titleCaseKey(key)}`] = renderListHeader(value);
|
|
724
|
+
}
|
|
175
725
|
return this.#msg;
|
|
176
726
|
}
|
|
177
727
|
}
|
package/src/RoverProvider.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { setMail } from "./services/main.js";
|
|
|
9
9
|
*/
|
|
10
10
|
interface RoverContainer {
|
|
11
11
|
singleton(token: unknown, factory: () => unknown): void;
|
|
12
|
-
resolve<T = unknown>(token: unknown): T
|
|
12
|
+
resolve<T = unknown>(token: unknown): Promise<T>;
|
|
13
13
|
}
|
|
14
14
|
interface RoverConfigStore {
|
|
15
15
|
get<T = unknown>(key: string): T | undefined;
|
|
@@ -23,7 +23,7 @@ export default class RoverProvider {
|
|
|
23
23
|
constructor(protected app: RoverAppContext) {}
|
|
24
24
|
|
|
25
25
|
register() {
|
|
26
|
-
this.app.container.singleton(Mail, () => {
|
|
26
|
+
this.app.container.singleton(Mail, async () => {
|
|
27
27
|
const config = this.app.config.get<MailConfig>("mail");
|
|
28
28
|
return new Mail(
|
|
29
29
|
config ?? {
|
|
@@ -36,10 +36,10 @@ export default class RoverProvider {
|
|
|
36
36
|
// `QueueManager` is registered in the container, Mail gets
|
|
37
37
|
// queue support for `sendLater()`. If not, `sendLater()`
|
|
38
38
|
// throws `MAIL_QUEUE_REQUIRED` at call time (by design).
|
|
39
|
-
queue: tryResolve<BayQueueLike>(this.app, "QueueManager"),
|
|
39
|
+
queue: await tryResolve<BayQueueLike>(this.app, "QueueManager"),
|
|
40
40
|
// Same pattern for the event bus `Emitter` — enables `mail.sent`
|
|
41
41
|
// / `mail.failed` emission when available.
|
|
42
|
-
emitter: tryResolve<EmitterLike>(this.app, "Emitter"),
|
|
42
|
+
emitter: await tryResolve<EmitterLike>(this.app, "Emitter"),
|
|
43
43
|
},
|
|
44
44
|
);
|
|
45
45
|
});
|
|
@@ -52,7 +52,7 @@ export default class RoverProvider {
|
|
|
52
52
|
// Populate the `@c9up/rover/services/main` singleton with the
|
|
53
53
|
// container-resolved Mail instance so apps can
|
|
54
54
|
// `import mail from '@c9up/rover/services/main'` from anywhere.
|
|
55
|
-
setMail(this.app.container.resolve<Mail>(Mail));
|
|
55
|
+
setMail(await this.app.container.resolve<Mail>(Mail));
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
async shutdown() {}
|
|
@@ -63,9 +63,12 @@ export default class RoverProvider {
|
|
|
63
63
|
* registered. Rover never hard-depends on Bay or the event bus — both wire-points
|
|
64
64
|
* are purely opt-in.
|
|
65
65
|
*/
|
|
66
|
-
function tryResolve<T>(
|
|
66
|
+
async function tryResolve<T>(
|
|
67
|
+
app: RoverAppContext,
|
|
68
|
+
token: string,
|
|
69
|
+
): Promise<T | undefined> {
|
|
67
70
|
try {
|
|
68
|
-
return app.container.resolve<T>(token);
|
|
71
|
+
return await app.container.resolve<T>(token);
|
|
69
72
|
} catch {
|
|
70
73
|
return undefined;
|
|
71
74
|
}
|