@omnitron-dev/titan-mailer 0.0.0-stage → 0.3.3
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 +21 -0
- package/README.md +174 -2
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +25 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/mailer.module.d.ts +10 -0
- package/dist/mailer.module.d.ts.map +1 -0
- package/dist/mailer.module.js +52 -0
- package/dist/mailer.module.js.map +1 -0
- package/dist/mailer.service.d.ts +16 -0
- package/dist/mailer.service.d.ts.map +1 -0
- package/dist/mailer.service.js +100 -0
- package/dist/mailer.service.js.map +1 -0
- package/dist/notifications.d.ts +58 -0
- package/dist/notifications.d.ts.map +1 -0
- package/dist/notifications.js +186 -0
- package/dist/notifications.js.map +1 -0
- package/dist/postgres-readiness.d.ts +92 -0
- package/dist/postgres-readiness.d.ts.map +1 -0
- package/dist/postgres-readiness.js +35 -0
- package/dist/postgres-readiness.js.map +1 -0
- package/dist/postgres.d.ts +57 -0
- package/dist/postgres.d.ts.map +1 -0
- package/dist/postgres.js +159 -0
- package/dist/postgres.js.map +1 -0
- package/dist/smtp.d.ts +4 -0
- package/dist/smtp.d.ts.map +1 -0
- package/dist/smtp.js +54 -0
- package/dist/smtp.js.map +1 -0
- package/dist/templates.d.ts +12 -0
- package/dist/templates.d.ts.map +1 -0
- package/dist/templates.js +28 -0
- package/dist/templates.js.map +1 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +61 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 LuxQuant
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,175 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @omnitron-dev/titan-mailer
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Transactional email for Titan: lifecycle-managed Nodemailer SMTP pools, typed templates,
|
|
4
|
+
a concrete email notification channel, and a durable outbox consumer using Titan Notifications.
|
|
5
|
+
|
|
6
|
+
## Application module
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { MailerModule, MailerService } from '@omnitron-dev/titan-mailer';
|
|
10
|
+
|
|
11
|
+
const mailModule = MailerModule.forRoot({
|
|
12
|
+
smtp: process.env.SMTP_URL!, // smtps://...:465 or smtp://...:587 with STARTTLS
|
|
13
|
+
from: 'Research <noreply@example.org>',
|
|
14
|
+
pool: { maxConnections: 3 },
|
|
15
|
+
maxPending: 1000,
|
|
16
|
+
});
|
|
17
|
+
// Import mailModule in your Titan @Module; inject MailerService or MAILER.
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`forRootAsync({ imports, inject, useFactory })` resolves configuration through Nexus.
|
|
21
|
+
`verifyOnStart: true` performs SMTP authentication during provider creation. Otherwise
|
|
22
|
+
call `verify()` from a readiness probe; it sends no email. Titan disposal calls
|
|
23
|
+
`onDestroy()`, which stops new submissions, drains accepted operations and closes the pool.
|
|
24
|
+
SMTP timeouts and `maxPending` bound waiting work. The caller owns admission/retry policy.
|
|
25
|
+
|
|
26
|
+
`send()` accepts **one envelope recipient**, explicit UTF-8 plaintext, optional HTML,
|
|
27
|
+
memory-backed attachments and a stable Message-ID. It returns SMTP acceptance, not inbox
|
|
28
|
+
delivery. Fan-out creates a separate durable delivery for each recipient, avoiding partial
|
|
29
|
+
recipient success and accidental address disclosure. Arbitrary SMTP envelopes, filesystem
|
|
30
|
+
paths, URL attachments, raw MIME and security overrides are not forwarded.
|
|
31
|
+
|
|
32
|
+
TLS certificate verification and TLS 1.2+ are mandatory. Cleartext is only available with
|
|
33
|
+
`allowInsecureLocalhost: true` and a loopback host. SMTP URL query options are rejected.
|
|
34
|
+
Public errors contain a classification, never credentials, server transcripts or bodies.
|
|
35
|
+
`MailDeliveryError.retryable` distinguishes temporary failures from invalid/configuration/
|
|
36
|
+
permanent failures. Nodemailer's hidden pool requeue is disabled: one queue owns retries.
|
|
37
|
+
|
|
38
|
+
## Templates
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { MailTemplates, escapeMailHtml } from '@omnitron-dev/titan-mailer';
|
|
42
|
+
const templates = new MailTemplates<{ welcome: { name: string } }>({
|
|
43
|
+
welcome: ({ name }) => ({
|
|
44
|
+
subject: 'Welcome',
|
|
45
|
+
text: `Hello, ${name}`,
|
|
46
|
+
html: `<p>Hello, ${escapeMailHtml(name)}</p>`,
|
|
47
|
+
}),
|
|
48
|
+
});
|
|
49
|
+
const rendered = templates.render('welcome', { name: 'Ada' });
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The registry is renderer-independent: Handlebars, MJML or a function may return the same
|
|
53
|
+
`RenderedMail` contract. Render trusted templates with escaped data, then persist that
|
|
54
|
+
result in the application's business transaction. Rendering never makes a network request.
|
|
55
|
+
Plaintext is required; the module does not invent it by stripping arbitrary scientific HTML.
|
|
56
|
+
|
|
57
|
+
## Redis notification workers
|
|
58
|
+
|
|
59
|
+
Use `NotificationsModule.forRoot({ redis })`, inject `NOTIFICATIONS_TRANSPORT`, and create
|
|
60
|
+
`MailDeliveryWorker` from `@omnitron-dev/titan-mailer/notifications` with that bus,
|
|
61
|
+
a `MailOutbox`, `MailerService`, and `{ channel, group }`. Start it in the supervised
|
|
62
|
+
worker process and stop it **before** destroying its database, mailer and transport.
|
|
63
|
+
The worker does not own injected dependencies; the Titan container disposes them.
|
|
64
|
+
|
|
65
|
+
`MailOutbox` is the application's durable transaction boundary:
|
|
66
|
+
|
|
67
|
+
- `pending(limit)` lists due opaque IDs, without message bodies or credentials.
|
|
68
|
+
- `deliver(id, send)` locks/claims one due row. Completed or cancelled IDs are harmless.
|
|
69
|
+
- SMTP acceptance and deletion/completion are recorded in the same database transaction.
|
|
70
|
+
- A failed attempt retains the message with backoff, or records an explicit terminal failure.
|
|
71
|
+
- Expired/replaced tokens cancel their queued mail. Database failure rejects; Redis is not ACKed.
|
|
72
|
+
|
|
73
|
+
Only an ID travels through Redis. Polling reconciliation republishes due IDs after lost or
|
|
74
|
+
trimmed wake-ups; it is bounded by `batchSize`, `intervalMs` and `maxConcurrent`. Concurrent
|
|
75
|
+
consumers share a group. Worker health covers transport connectivity and failed reconciliation/
|
|
76
|
+
delivery. Failed delivery policy and administrative re-drive remain in the durable store.
|
|
77
|
+
|
|
78
|
+
### PostgreSQL delivery store
|
|
79
|
+
|
|
80
|
+
`@omnitron-dev/titan-mailer/postgres` exports `PostgresMailOutbox`, the database port,
|
|
81
|
+
and `POSTGRES_MAIL_OUTBOX_SCHEMA`. Apply the schema in your application migration and
|
|
82
|
+
pass an existing pool/transaction executor; the adapter never creates a second connection pool.
|
|
83
|
+
An existing Titan database transaction can supply this executor. Applications with an older
|
|
84
|
+
mail schema must migrate its columns explicitly before enabling the worker.
|
|
85
|
+
|
|
86
|
+
The same entry exports `POSTGRES_MAIL_OUTBOX_REQUIREMENTS`, a plain-data readiness
|
|
87
|
+
contract owned alongside the schema. Before starting the worker, pass an existing
|
|
88
|
+
pool and this contract to `requirePostgresSchema` from
|
|
89
|
+
`@omnitron-dev/titan-database/readiness` (or `@omnitron-dev/omnitron/readiness`).
|
|
90
|
+
The check reads schema metadata; it neither migrates nor sends mail. It detects
|
|
91
|
+
missing columns, changed retry defaults and lost delivery-ID uniqueness before
|
|
92
|
+
delivery begins. The PostgreSQL entry and the root mailer keep their existing
|
|
93
|
+
dependencies: the contract itself imports no database engine or driver.
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
const outbox = new PostgresMailOutbox(database, {
|
|
97
|
+
messageIdDomain: 'example.org',
|
|
98
|
+
maxAttempts: 6,
|
|
99
|
+
retrySeconds: 30,
|
|
100
|
+
maxRetrySeconds: 3600,
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Insert the recipient, subject, `body` (plain text), optional HTML and expiry into `mail_outbox`
|
|
105
|
+
inside the **same transaction as the business operation**. The application can add foreign
|
|
106
|
+
keys to cancel queued messages when an account or one-time credential is deleted. Do not
|
|
107
|
+
store SMTP credentials in the outbox.
|
|
108
|
+
|
|
109
|
+
`status(id)` distinguishes queued, retrying, accepted, failed and expired. `failed(limit)`
|
|
110
|
+
and `health()` return bounded operational information without addresses or message content.
|
|
111
|
+
`health().oldestDueSeconds` identifies an overdue queue even when Redis reports connected.
|
|
112
|
+
After repairing a terminal failure, `retry(id)` resets its retry budget, retains the stable
|
|
113
|
+
Message-ID and cumulative attempt count, and refuses completed or expired messages.
|
|
114
|
+
`pruneReceipts(days)` removes only old receipts with no remaining queued payload.
|
|
115
|
+
No failed payload is silently dropped. Keep database backups and monitor failed/overdue mail.
|
|
116
|
+
|
|
117
|
+
The delivery contract is **at least once**, assuming durable storage, eventual recovery and retry of retained failures. An SMTP server may accept a message before a worker
|
|
118
|
+
loses its database transaction. A stable Message-ID aids correlation but cannot make SMTP
|
|
119
|
+
and SQL atomic, and mail clients need not deduplicate it. Do not call this exactly once.
|
|
120
|
+
Use a dedicated Redis database/ACL, bounded streams and retention for durable delivery events.
|
|
121
|
+
|
|
122
|
+
`MailerEmailChannel` adapts the service to the standard notification channel registry.
|
|
123
|
+
It shares, and does not close, its injected MailerService. Critical security messages use
|
|
124
|
+
the durable outbox path, not optional marketing notification preferences.
|
|
125
|
+
|
|
126
|
+
## Delivery diagnostics
|
|
127
|
+
|
|
128
|
+
Use the `PostgresMailOutbox` option `onDelivery` to forward committed outcomes to
|
|
129
|
+
Titan Logger and therefore Omnitron's Console and CLI:
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
const outbox = new PostgresMailOutbox(database, {
|
|
133
|
+
messageIdDomain: 'example.org',
|
|
134
|
+
onDelivery: (event) => {
|
|
135
|
+
const level = event.state === 'failed' ? 'error'
|
|
136
|
+
: event.state === 'accepted' ? 'info' : 'warn';
|
|
137
|
+
logger[level](
|
|
138
|
+
{ event: 'mail.delivery', ...event },
|
|
139
|
+
`mail.delivery ${event.state} delivery=${event.deliveryId}`,
|
|
140
|
+
);
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
`MailDeliveryEvent` contains `deliveryId`, `state`, `attempts` in the current retry
|
|
146
|
+
cycle and `errorKind`. It has no recipient, body, credential or server response.
|
|
147
|
+
The callback runs only after the transaction commits. Async callbacks are supported;
|
|
148
|
+
a throwing or rejected observer cannot change a committed outcome or trigger another
|
|
149
|
+
send. These are best-effort operational logs, not a second durable event queue:
|
|
150
|
+
`status(id)` and the SQL receipt remain authoritative if the process exits before logging.
|
|
151
|
+
|
|
152
|
+
`MailDeliveryWorker.onError(stage, code)` reports `outbox_read_failed`,
|
|
153
|
+
`notification_publish_failed`, `outbox_delivery_failed` or `notification_ack_failed`.
|
|
154
|
+
It never passes the raw exception. A recorded SMTP rejection belongs to the outbox
|
|
155
|
+
outcome; it is not an uncommitted worker error. Report queue counts and
|
|
156
|
+
`oldestDueSeconds` in an application's standard health checks as well as transport
|
|
157
|
+
connectivity; a running process or connected Redis alone is insufficient.
|
|
158
|
+
|
|
159
|
+
For authentication mail, record the request's `queued` or `skipped` decision in the
|
|
160
|
+
application **after commit**, with its opaque delivery ID. Keep the public recovery
|
|
161
|
+
response uniform for missing, disabled and existing accounts. Use a keyed, domain-separated
|
|
162
|
+
subject reference for operator correlation, not a raw email or unkeyed email hash.
|
|
163
|
+
An unknown account produces no mail; `queued` means SQL committed; `accepted` means
|
|
164
|
+
SMTP accepted the message; none of these establishes arrival in the recipient's inbox.
|
|
165
|
+
|
|
166
|
+
## Verification
|
|
167
|
+
|
|
168
|
+
`pnpm test` uses a real local SMTP server and independent MIME parser, tests STARTTLS
|
|
169
|
+
refusal, reply/header injection, mixed content, rejected recipients, queue capacity and
|
|
170
|
+
actual Titan DI disposal. Worker contract tests cover lost wake-ups, failed commits, poison
|
|
171
|
+
envelopes and draining. Consumer integration tests also exercise the complete PostgreSQL → Redis → SMTP path
|
|
172
|
+
with competing workers and checks the resulting Mailpit messages.
|
|
173
|
+
|
|
174
|
+
Design references: [NestJS Mailer](https://nest-modules.github.io/mailer/docs/configuration/),
|
|
175
|
+
[Nodemailer SMTP](https://nodemailer.com/smtp), [pool lifecycle](https://nodemailer.com/smtp/pooled).
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type MailFailureKind = 'transient' | 'permanent' | 'configuration' | 'invalid' | 'closed' | 'capacity';
|
|
2
|
+
export declare class MailDeliveryError extends Error {
|
|
3
|
+
readonly kind: MailFailureKind;
|
|
4
|
+
readonly retryable: boolean;
|
|
5
|
+
constructor(kind: MailFailureKind);
|
|
6
|
+
}
|
|
7
|
+
export declare function mailFailure(error: unknown): MailDeliveryError;
|
|
8
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,eAAe,GAAG,WAAW,GAAG,WAAW,GAAG,eAAe,GAAG,SAAS,GAAG,QAAQ,GAAG,UAAU,CAAC;AAE9G,qBAAa,iBAAkB,SAAQ,KAAK;IAE9B,QAAQ,CAAC,IAAI,EAAE,eAAe;IAD1C,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,YAAqB,IAAI,EAAE,eAAe,EAIzC;CACF;AACD,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,iBAAiB,CAkB7D"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export class MailDeliveryError extends Error {
|
|
2
|
+
constructor(kind) {
|
|
3
|
+
super(`Mail delivery failed (${kind})`);
|
|
4
|
+
this.kind = kind;
|
|
5
|
+
this.name = 'MailDeliveryError';
|
|
6
|
+
this.retryable = kind === 'transient' || kind === 'capacity';
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function mailFailure(error) {
|
|
10
|
+
if (error instanceof MailDeliveryError)
|
|
11
|
+
return error;
|
|
12
|
+
const value = error;
|
|
13
|
+
const code = value?.originalCode ?? value?.code;
|
|
14
|
+
if (code === 'CERT_HAS_EXPIRED' ||
|
|
15
|
+
code === 'ERR_TLS_CERT_ALTNAME_INVALID' ||
|
|
16
|
+
code === 'DEPTH_ZERO_SELF_SIGNED_CERT')
|
|
17
|
+
return new MailDeliveryError('configuration');
|
|
18
|
+
const status = value?.responseCode;
|
|
19
|
+
if (typeof status === 'number' && Number.isInteger(status) && status >= 400 && status < 500)
|
|
20
|
+
return new MailDeliveryError('transient');
|
|
21
|
+
if (code === 'EAUTH' || code === 'ETLS')
|
|
22
|
+
return new MailDeliveryError('configuration');
|
|
23
|
+
return new MailDeliveryError(typeof status === 'number' && status >= 500 && status < 600 ? 'permanent' : 'transient');
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAE1C,YAAqB,IAAqB;QACxC,KAAK,CAAC,yBAAyB,IAAI,GAAG,CAAC,CAAC;oBADrB,IAAI;QAEvB,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;QAChC,IAAI,CAAC,SAAS,GAAG,IAAI,KAAK,WAAW,IAAI,IAAI,KAAK,UAAU,CAAC;IAC/D,CAAC;CACF;AACD,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,IAAI,KAAK,YAAY,iBAAiB;QAAE,OAAO,KAAK,CAAC;IACrD,MAAM,KAAK,GAAG,KAAkF,CAAC;IACjG,MAAM,IAAI,GAAG,KAAK,EAAE,YAAY,IAAI,KAAK,EAAE,IAAI,CAAC;IAChD,IACE,IAAI,KAAK,kBAAkB;QAC3B,IAAI,KAAK,8BAA8B;QACvC,IAAI,KAAK,6BAA6B;QAEtC,OAAO,IAAI,iBAAiB,CAAC,eAAe,CAAC,CAAC;IAChD,MAAM,MAAM,GAAG,KAAK,EAAE,YAAY,CAAC;IAInC,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,GAAG,IAAI,MAAM,GAAG,GAAG;QACzF,OAAO,IAAI,iBAAiB,CAAC,WAAW,CAAC,CAAC;IAC5C,IAAI,IAAI,KAAK,OAAO,IAAI,IAAI,KAAK,MAAM;QAAE,OAAO,IAAI,iBAAiB,CAAC,eAAe,CAAC,CAAC;IACvF,OAAO,IAAI,iBAAiB,CAAC,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,IAAI,GAAG,IAAI,MAAM,GAAG,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC;AACxH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { MailerService } from './mailer.service.js';
|
|
2
|
+
export { MailerModule, MAILER, MAILER_OPTIONS } from './mailer.module.js';
|
|
3
|
+
export { MailDeliveryError, mailFailure } from './errors.js';
|
|
4
|
+
export type { MailFailureKind } from './errors.js';
|
|
5
|
+
export { smtpOptions } from './smtp.js';
|
|
6
|
+
export { MailTemplates, escapeMailHtml, validateRenderedMail } from './templates.js';
|
|
7
|
+
export type { MailTemplate } from './templates.js';
|
|
8
|
+
export type { MailerOptions, MailerAsyncOptions, MailMessage, MailReceipt, MailSender, RenderedMail } from './types.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAC1E,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC7D,YAAY,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AACrF,YAAY,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACnD,YAAY,EAAE,aAAa,EAAE,kBAAkB,EAAE,WAAW,EAAE,WAAW,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { MailerService } from './mailer.service.js';
|
|
2
|
+
export { MailerModule, MAILER, MAILER_OPTIONS } from './mailer.module.js';
|
|
3
|
+
export { MailDeliveryError, mailFailure } from './errors.js';
|
|
4
|
+
export { smtpOptions } from './smtp.js';
|
|
5
|
+
export { MailTemplates, escapeMailHtml, validateRenderedMail } from './templates.js';
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAC1E,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE7D,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Token, type DynamicModule } from '@omnitron-dev/titan/nexus';
|
|
2
|
+
import { MailerService } from './mailer.service.js';
|
|
3
|
+
import type { MailerOptions, MailerAsyncOptions } from './types.js';
|
|
4
|
+
export declare const MAILER_OPTIONS: Token<MailerOptions>;
|
|
5
|
+
export declare const MAILER: Token<MailerService>;
|
|
6
|
+
export declare class MailerModule {
|
|
7
|
+
static forRoot(options: MailerOptions, global?: boolean): DynamicModule;
|
|
8
|
+
static forRootAsync<Args extends unknown[]>(options: MailerAsyncOptions<Args>): DynamicModule;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=mailer.module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mailer.module.d.ts","sourceRoot":"","sources":["../src/mailer.module.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,KAAK,KAAK,EAAE,KAAK,aAAa,EAAE,MAAM,2BAA2B,CAAC;AACxF,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AACpE,eAAO,MAAM,cAAc,EAAE,KAAK,CAAC,aAAa,CAA+C,CAAC;AAChG,eAAO,MAAM,MAAM,EAAE,KAAK,CAAC,aAAa,CAA+C,CAAC;AACxF,qBACa,YAAY;IACvB,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAQ,GAAG,aAAa,CAEpE;IACD,MAAM,CAAC,YAAY,CAAC,IAAI,SAAS,OAAO,EAAE,EAAE,OAAO,EAAE,kBAAkB,CAAC,IAAI,CAAC,GAAG,aAAa,CA2B5F;CACF"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var MailerModule_1;
|
|
8
|
+
import { Module } from '@omnitron-dev/titan/decorators';
|
|
9
|
+
import { createToken } from '@omnitron-dev/titan/nexus';
|
|
10
|
+
import { MailerService } from './mailer.service.js';
|
|
11
|
+
export const MAILER_OPTIONS = createToken('MailerOptions');
|
|
12
|
+
export const MAILER = createToken('MailerService');
|
|
13
|
+
let MailerModule = MailerModule_1 = class MailerModule {
|
|
14
|
+
static forRoot(options, global = false) {
|
|
15
|
+
return this.forRootAsync({ useFactory: () => options, global });
|
|
16
|
+
}
|
|
17
|
+
static forRootAsync(options) {
|
|
18
|
+
return {
|
|
19
|
+
module: MailerModule_1,
|
|
20
|
+
imports: options.imports,
|
|
21
|
+
global: options.global,
|
|
22
|
+
providers: [
|
|
23
|
+
[MAILER_OPTIONS, { useFactory: options.useFactory, inject: options.inject ?? [] }],
|
|
24
|
+
[
|
|
25
|
+
MailerService,
|
|
26
|
+
{
|
|
27
|
+
useFactory: async (config) => {
|
|
28
|
+
const service = new MailerService(config);
|
|
29
|
+
try {
|
|
30
|
+
if (config.verifyOnStart)
|
|
31
|
+
await service.verify();
|
|
32
|
+
return service;
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
await service.close();
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
inject: [MAILER_OPTIONS],
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
[MAILER, { useToken: MailerService }],
|
|
43
|
+
],
|
|
44
|
+
exports: [MAILER, MailerService],
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
MailerModule = MailerModule_1 = __decorate([
|
|
49
|
+
Module()
|
|
50
|
+
], MailerModule);
|
|
51
|
+
export { MailerModule };
|
|
52
|
+
//# sourceMappingURL=mailer.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mailer.module.js","sourceRoot":"","sources":["../src/mailer.module.ts"],"names":[],"mappings":";;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AACxD,OAAO,EAAE,WAAW,EAAkC,MAAM,2BAA2B,CAAC;AACxF,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,MAAM,CAAC,MAAM,cAAc,G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E;GACI,YAAY,CAgCxB"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { MailerOptions, MailMessage, MailReceipt, MailSender } from './types.js';
|
|
2
|
+
export declare class MailerService implements MailSender {
|
|
3
|
+
private readonly options;
|
|
4
|
+
private readonly transport;
|
|
5
|
+
private readonly pending;
|
|
6
|
+
private readonly maxPending;
|
|
7
|
+
private closed;
|
|
8
|
+
private closing?;
|
|
9
|
+
constructor(options: MailerOptions);
|
|
10
|
+
private run;
|
|
11
|
+
send(message: MailMessage): Promise<MailReceipt>;
|
|
12
|
+
verify(): Promise<void>;
|
|
13
|
+
close(): Promise<void>;
|
|
14
|
+
onDestroy(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=mailer.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mailer.service.d.ts","sourceRoot":"","sources":["../src/mailer.service.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AActF,qBAAa,aAAc,YAAW,UAAU;IAMlC,OAAO,CAAC,QAAQ,CAAC,OAAO;IALpC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAwC;IAClE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA+B;IACvD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,OAAO,CAAC,CAAgB;IAChC,YAA6B,OAAO,EAAE,aAAa,EAMlD;IACD,OAAO,CAAC,GAAG;IAYX,IAAI,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CA0C/C;IACD,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAItB;IACD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAGrB;IACD,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC,CAEzB;CACF"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import nodemailer from 'nodemailer';
|
|
2
|
+
import addressparser from 'nodemailer/lib/addressparser/index.js';
|
|
3
|
+
import { MailDeliveryError, mailFailure } from './errors.js';
|
|
4
|
+
import { smtpOptions } from './smtp.js';
|
|
5
|
+
import { validateRenderedMail } from './templates.js';
|
|
6
|
+
function address(value) {
|
|
7
|
+
if (typeof value !== 'string' || /[\r\n\0]/.test(value))
|
|
8
|
+
throw new MailDeliveryError('invalid');
|
|
9
|
+
const parsed = addressparser(value);
|
|
10
|
+
if (parsed.length !== 1 ||
|
|
11
|
+
!parsed[0] ||
|
|
12
|
+
'group' in parsed[0] ||
|
|
13
|
+
!parsed[0].address ||
|
|
14
|
+
!/^[^\s<>@]+@[^\s<>@]+$/.test(parsed[0].address))
|
|
15
|
+
throw new MailDeliveryError('invalid');
|
|
16
|
+
}
|
|
17
|
+
export class MailerService {
|
|
18
|
+
constructor(options) {
|
|
19
|
+
this.options = options;
|
|
20
|
+
this.pending = new Set();
|
|
21
|
+
this.closed = false;
|
|
22
|
+
address(options.from);
|
|
23
|
+
if (options.replyTo)
|
|
24
|
+
address(options.replyTo);
|
|
25
|
+
this.maxPending = options.maxPending ?? 1000;
|
|
26
|
+
if (!Number.isSafeInteger(this.maxPending) || this.maxPending < 1)
|
|
27
|
+
throw new MailDeliveryError('configuration');
|
|
28
|
+
this.transport = nodemailer.createTransport(smtpOptions(options));
|
|
29
|
+
}
|
|
30
|
+
run(work) {
|
|
31
|
+
if (this.closed)
|
|
32
|
+
return Promise.reject(new MailDeliveryError('closed'));
|
|
33
|
+
if (this.pending.size >= this.maxPending)
|
|
34
|
+
return Promise.reject(new MailDeliveryError('capacity'));
|
|
35
|
+
const task = Promise.resolve()
|
|
36
|
+
.then(work)
|
|
37
|
+
.catch((error) => {
|
|
38
|
+
throw mailFailure(error);
|
|
39
|
+
});
|
|
40
|
+
this.pending.add(task);
|
|
41
|
+
void task.finally(() => this.pending.delete(task)).catch(() => { });
|
|
42
|
+
return task;
|
|
43
|
+
}
|
|
44
|
+
send(message) {
|
|
45
|
+
return this.run(async () => {
|
|
46
|
+
validateRenderedMail(message);
|
|
47
|
+
address(message.to);
|
|
48
|
+
if (message.from)
|
|
49
|
+
address(message.from);
|
|
50
|
+
if (message.replyTo)
|
|
51
|
+
address(message.replyTo);
|
|
52
|
+
if (message.messageId && !/^<[^<>\s@]+@[^<>\s@]+>$/.test(message.messageId))
|
|
53
|
+
throw new MailDeliveryError('invalid');
|
|
54
|
+
for (const [key, value] of Object.entries(message.headers ?? {})) {
|
|
55
|
+
if (!/^[A-Za-z0-9-]+$/.test(key) ||
|
|
56
|
+
/^(to|cc|bcc|from|sender|reply-to|subject|message-id|return-path)$/i.test(key) ||
|
|
57
|
+
/[\r\n\0]/.test(value))
|
|
58
|
+
throw new MailDeliveryError('invalid');
|
|
59
|
+
}
|
|
60
|
+
for (const file of message.attachments ?? []) {
|
|
61
|
+
if (!Buffer.isBuffer(file.content) || !file.filename || /[\r\n\0]/.test(file.filename))
|
|
62
|
+
throw new MailDeliveryError('invalid');
|
|
63
|
+
}
|
|
64
|
+
const info = await this.transport.sendMail({
|
|
65
|
+
from: message.from ?? this.options.from,
|
|
66
|
+
to: message.to,
|
|
67
|
+
replyTo: message.replyTo ?? this.options.replyTo,
|
|
68
|
+
subject: message.subject,
|
|
69
|
+
text: message.text,
|
|
70
|
+
html: message.html,
|
|
71
|
+
messageId: message.messageId,
|
|
72
|
+
headers: message.headers,
|
|
73
|
+
attachments: message.attachments?.map((f) => ({
|
|
74
|
+
filename: f.filename,
|
|
75
|
+
content: f.content,
|
|
76
|
+
contentType: f.contentType,
|
|
77
|
+
cid: f.cid,
|
|
78
|
+
})),
|
|
79
|
+
disableFileAccess: true,
|
|
80
|
+
disableUrlAccess: true,
|
|
81
|
+
});
|
|
82
|
+
if (info.accepted.length !== 1 || info.rejected.length)
|
|
83
|
+
throw new MailDeliveryError('permanent');
|
|
84
|
+
return { messageId: info.messageId, accepted: info.accepted.map(String) };
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
verify() {
|
|
88
|
+
return this.run(async () => {
|
|
89
|
+
await this.transport.verify();
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
close() {
|
|
93
|
+
this.closed = true;
|
|
94
|
+
return (this.closing ??= Promise.allSettled([...this.pending]).then(() => this.transport.close()));
|
|
95
|
+
}
|
|
96
|
+
onDestroy() {
|
|
97
|
+
return this.close();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=mailer.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mailer.service.js","sourceRoot":"","sources":["../src/mailer.service.ts"],"names":[],"mappings":"AAAA,OAAO,UAAgC,MAAM,YAAY,CAAC;AAE1D,OAAO,aAAa,MAAM,uCAAuC,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAGtD,SAAS,OAAO,CAAC,KAAa;IAC5B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,MAAM,IAAI,iBAAiB,CAAC,SAAS,CAAC,CAAC;IAChG,MAAM,MAAM,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IACpC,IACE,MAAM,CAAC,MAAM,KAAK,CAAC;QACnB,CAAC,MAAM,CAAC,CAAC,CAAC;QACV,OAAO,IAAI,MAAM,CAAC,CAAC,CAAC;QACpB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO;QAClB,CAAC,uBAAuB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;QAEhD,MAAM,IAAI,iBAAiB,CAAC,SAAS,CAAC,CAAC;AAC3C,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AC,MAAM,GAAG,IAAI,CAAC;QACnB,OAAO,CAAC,IAAI,CAAC,OAAO,KAAK,OAAO,CAAC,UAAU,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IACrG,CAAC;IACD,SAAS;QACP,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;CACF"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { MessagingTransport } from '@omnitron-dev/titan-notifications';
|
|
2
|
+
import { AbstractEmailChannel } from '@omnitron-dev/titan-notifications';
|
|
3
|
+
import type { EmailContent } from '@omnitron-dev/titan-notifications';
|
|
4
|
+
import type { MailerService } from './mailer.service.js';
|
|
5
|
+
import type { MailMessage, MailReceipt, MailSender } from './types.js';
|
|
6
|
+
export interface MailOutbox {
|
|
7
|
+
pending(limit: number): Promise<string[]>;
|
|
8
|
+
deliver(id: string, send: (mail: MailMessage) => Promise<MailReceipt>): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
export interface MailDeliveryWorkerOptions {
|
|
11
|
+
channel: string;
|
|
12
|
+
group: string;
|
|
13
|
+
consumer?: string;
|
|
14
|
+
intervalMs?: number;
|
|
15
|
+
batchSize?: number;
|
|
16
|
+
maxConcurrent?: number;
|
|
17
|
+
onError?: (stage: 'relay' | 'delivery', code: MailWorkerFailureCode) => void | Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
export type MailWorkerFailureCode = 'outbox_read_failed' | 'notification_publish_failed' | 'outbox_delivery_failed' | 'notification_ack_failed';
|
|
20
|
+
export declare class MailDeliveryWorker {
|
|
21
|
+
private readonly bus;
|
|
22
|
+
private readonly store;
|
|
23
|
+
private readonly mailer;
|
|
24
|
+
private readonly options;
|
|
25
|
+
private subscription?;
|
|
26
|
+
private starting?;
|
|
27
|
+
private stopping?;
|
|
28
|
+
private timer?;
|
|
29
|
+
private relay;
|
|
30
|
+
private active;
|
|
31
|
+
private closed;
|
|
32
|
+
private failure;
|
|
33
|
+
private deliveryFailure;
|
|
34
|
+
private readonly maxConcurrent;
|
|
35
|
+
readonly interval: number;
|
|
36
|
+
readonly batchSize: number;
|
|
37
|
+
constructor(bus: MessagingTransport, store: MailOutbox, mailer: MailSender, options: MailDeliveryWorkerOptions);
|
|
38
|
+
start(): Promise<void>;
|
|
39
|
+
private subscribe;
|
|
40
|
+
private accept;
|
|
41
|
+
reconcile(): Promise<void>;
|
|
42
|
+
private report;
|
|
43
|
+
health(): Promise<{
|
|
44
|
+
available: boolean;
|
|
45
|
+
active: number;
|
|
46
|
+
}>;
|
|
47
|
+
stop(): Promise<void>;
|
|
48
|
+
onDestroy(): Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
export declare class MailerEmailChannel extends AbstractEmailChannel {
|
|
51
|
+
private readonly mailer;
|
|
52
|
+
constructor(mailer: MailerService);
|
|
53
|
+
isAvailable(): Promise<boolean>;
|
|
54
|
+
sendEmail(to: string, content: EmailContent): Promise<{
|
|
55
|
+
messageId: string;
|
|
56
|
+
}>;
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=notifications.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"notifications.d.ts","sourceRoot":"","sources":["../src/notifications.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,kBAAkB,EAEnB,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AACtE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAGvE,MAAM,WAAW,UAAU;IACzB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG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