nodemailer 10.4.1 → 10.5.0
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/CHANGELOG.md +13 -0
- package/README.md +21 -0
- package/dist/cjs/dkim/sign.js +2 -2
- package/dist/cjs/errors.d.ts +2 -0
- package/dist/cjs/errors.js +11 -1
- package/dist/cjs/json-transport/index.js +2 -0
- package/dist/cjs/mailer/index.d.ts +5 -0
- package/dist/cjs/mailer/index.js +56 -4
- package/dist/cjs/mailer/mail-message.d.ts +10 -0
- package/dist/cjs/mime-funcs/index.d.ts +2 -1
- package/dist/cjs/mime-funcs/index.js +72 -2
- package/dist/cjs/mime-node/index.js +1 -79
- package/dist/cjs/package-info.d.ts +1 -1
- package/dist/cjs/package-info.js +1 -1
- package/dist/cjs/shared/index.d.ts +23 -0
- package/dist/cjs/shared/index.js +79 -0
- package/dist/cjs/smtp-connection/index.js +6 -0
- package/dist/cjs/smtp-pool/index.d.ts +1 -0
- package/dist/cjs/smtp-pool/index.js +57 -6
- package/dist/cjs/smtp-pool/pool-resource.js +26 -4
- package/dist/cjs/smtp-transport/index.d.ts +1 -0
- package/dist/cjs/smtp-transport/index.js +47 -35
- package/dist/esm/dkim/sign.js +2 -2
- package/dist/esm/errors.d.ts +2 -0
- package/dist/esm/errors.js +9 -0
- package/dist/esm/json-transport/index.js +2 -0
- package/dist/esm/mailer/index.d.ts +5 -0
- package/dist/esm/mailer/index.js +56 -4
- package/dist/esm/mailer/mail-message.d.ts +10 -0
- package/dist/esm/mime-funcs/index.d.ts +2 -1
- package/dist/esm/mime-funcs/index.js +72 -2
- package/dist/esm/mime-node/index.js +1 -79
- package/dist/esm/package-info.d.ts +1 -1
- package/dist/esm/package-info.js +1 -1
- package/dist/esm/shared/index.d.ts +23 -0
- package/dist/esm/shared/index.js +78 -0
- package/dist/esm/smtp-connection/index.js +6 -0
- package/dist/esm/smtp-pool/index.d.ts +1 -0
- package/dist/esm/smtp-pool/index.js +57 -6
- package/dist/esm/smtp-pool/pool-resource.js +26 -4
- package/dist/esm/smtp-transport/index.d.ts +1 -0
- package/dist/esm/smtp-transport/index.js +47 -35
- package/package.json +1 -1
|
@@ -56,6 +56,7 @@ const REQUEUE_MAX_DELAY = 2000;
|
|
|
56
56
|
class SMTPPool extends node_events_1.EventEmitter {
|
|
57
57
|
constructor(options) {
|
|
58
58
|
super();
|
|
59
|
+
this.supportsAbortSignal = true;
|
|
59
60
|
options = options || {};
|
|
60
61
|
if (typeof options === 'string') {
|
|
61
62
|
options = {
|
|
@@ -134,26 +135,74 @@ class SMTPPool extends node_events_1.EventEmitter {
|
|
|
134
135
|
return false;
|
|
135
136
|
}
|
|
136
137
|
// counted until the callback runs, whichever path runs it: delivered, failed, requeued
|
|
137
|
-
// until it gives up, or drained by close()
|
|
138
|
+
// until it gives up, aborted, or drained by close()
|
|
138
139
|
let counted = true;
|
|
140
|
+
let detachSignal;
|
|
139
141
|
this._pending++;
|
|
140
|
-
|
|
142
|
+
const entry = {
|
|
141
143
|
mail,
|
|
142
144
|
requeueAttempts: 0,
|
|
143
145
|
callback: (err, info) => {
|
|
144
|
-
if (counted) {
|
|
145
|
-
|
|
146
|
-
this._pending--;
|
|
146
|
+
if (!counted) {
|
|
147
|
+
return;
|
|
147
148
|
}
|
|
149
|
+
counted = false;
|
|
150
|
+
this._pending--;
|
|
151
|
+
detachSignal?.();
|
|
152
|
+
entry.resource = undefined;
|
|
148
153
|
callback(err, info);
|
|
149
154
|
}
|
|
150
|
-
}
|
|
155
|
+
};
|
|
156
|
+
this._queue.push(entry);
|
|
157
|
+
const signal = mail.data.signal;
|
|
158
|
+
if (signal) {
|
|
159
|
+
// one listener for the original entry, retained across connection-close retries
|
|
160
|
+
detachSignal = this._watchAbortSignal(entry, signal, () => counted);
|
|
161
|
+
if (signal.aborted) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
151
165
|
if (this.idling && this._queue.length >= this.options.maxConnections) {
|
|
152
166
|
this.idling = false;
|
|
153
167
|
}
|
|
154
168
|
setImmediate(() => this._processMessages());
|
|
155
169
|
return true;
|
|
156
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* Fails a queued or assigned entry when its signal aborts: removes it from the queue, or
|
|
173
|
+
* closes the connection delivering it, so other deliveries in the pool can continue.
|
|
174
|
+
* Returns a function that removes the listener.
|
|
175
|
+
* @internal
|
|
176
|
+
*/
|
|
177
|
+
_watchAbortSignal(entry, signal, isPending) {
|
|
178
|
+
const onAbort = () => {
|
|
179
|
+
if (!isPending()) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const index = this._queue.indexOf(entry);
|
|
183
|
+
if (index !== -1) {
|
|
184
|
+
this._queue.splice(index, 1);
|
|
185
|
+
}
|
|
186
|
+
const resource = entry.resource;
|
|
187
|
+
if (resource && resource.queueEntry === entry) {
|
|
188
|
+
// Detach before closing: the close handler must not retry this message.
|
|
189
|
+
resource.queueEntry = false;
|
|
190
|
+
resource.close();
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
entry.callback(errors.createAbortError(signal));
|
|
194
|
+
}
|
|
195
|
+
finally {
|
|
196
|
+
// Removing a queued entry can make the pool idle again.
|
|
197
|
+
setImmediate(() => this._processMessages());
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
201
|
+
if (signal.aborted) {
|
|
202
|
+
setImmediate(onAbort);
|
|
203
|
+
}
|
|
204
|
+
return () => signal.removeEventListener('abort', onAbort);
|
|
205
|
+
}
|
|
157
206
|
/**
|
|
158
207
|
* Closes the pool. Idle connections are closed at once and a connection in the middle of a
|
|
159
208
|
* delivery finishes it first, unless `force` is set: then it is closed too and its message
|
|
@@ -254,6 +303,7 @@ class SMTPPool extends node_events_1.EventEmitter {
|
|
|
254
303
|
this.emit('idle');
|
|
255
304
|
}
|
|
256
305
|
const entry = (connection.queueEntry = this._queue.shift());
|
|
306
|
+
entry.resource = connection;
|
|
257
307
|
entry.messageId = (connection.queueEntry.mail.message.getHeader('message-id') || '').replace(/[<>\s]/g, '');
|
|
258
308
|
connection.available = false;
|
|
259
309
|
this.logger.debug({
|
|
@@ -425,6 +475,7 @@ class SMTPPool extends node_events_1.EventEmitter {
|
|
|
425
475
|
messageId: connection.queueEntry.messageId,
|
|
426
476
|
action: 'requeue'
|
|
427
477
|
}, 'Re-queued message <%s> for #%s. Attempt: #%s', connection.queueEntry.messageId, connection.id, connection.queueEntry.requeueAttempts);
|
|
478
|
+
connection.queueEntry.resource = undefined;
|
|
428
479
|
this._queue.unshift(connection.queueEntry);
|
|
429
480
|
connection.queueEntry = false;
|
|
430
481
|
return delay;
|
|
@@ -86,6 +86,7 @@ class PoolResource extends node_events_1.EventEmitter {
|
|
|
86
86
|
}
|
|
87
87
|
this._connection = false;
|
|
88
88
|
this._connected = false;
|
|
89
|
+
this._closed = false;
|
|
89
90
|
this.messages = 0;
|
|
90
91
|
this.available = true;
|
|
91
92
|
this._failed = false;
|
|
@@ -113,6 +114,10 @@ class PoolResource extends node_events_1.EventEmitter {
|
|
|
113
114
|
// the proxy handshake, if any, counts against connectionTimeout as well
|
|
114
115
|
const connectStartedAt = Date.now();
|
|
115
116
|
this.pool.getSocket(Object.assign({}, this.options, { connectStartedAt }), (err, socketOptions) => {
|
|
117
|
+
if (this._closed) {
|
|
118
|
+
socketOptions && socketOptions.connection?.destroy();
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
116
121
|
if (err) {
|
|
117
122
|
// nothing was connected, so no 'close' event is coming that would free the
|
|
118
123
|
// slot this resource holds in the pool, report the failure the way a failed
|
|
@@ -136,7 +141,7 @@ class PoolResource extends node_events_1.EventEmitter {
|
|
|
136
141
|
this.connection = new index_js_1.default(options);
|
|
137
142
|
this.connection._connectStartedAt = connectStartedAt;
|
|
138
143
|
this.connection.on('error', (err) => {
|
|
139
|
-
if (this._sending) {
|
|
144
|
+
if (this._closed || this._sending) {
|
|
140
145
|
// the send callback gets the same error and decides what it means for the message
|
|
141
146
|
return;
|
|
142
147
|
}
|
|
@@ -150,7 +155,7 @@ class PoolResource extends node_events_1.EventEmitter {
|
|
|
150
155
|
return;
|
|
151
156
|
}
|
|
152
157
|
this._fail(err);
|
|
153
|
-
if (returned) {
|
|
158
|
+
if (returned || this._closed) {
|
|
154
159
|
return;
|
|
155
160
|
}
|
|
156
161
|
returned = true;
|
|
@@ -161,7 +166,7 @@ class PoolResource extends node_events_1.EventEmitter {
|
|
|
161
166
|
returned = true;
|
|
162
167
|
});
|
|
163
168
|
this.connection.connect(err => {
|
|
164
|
-
if (returned) {
|
|
169
|
+
if (returned || this._closed) {
|
|
165
170
|
return;
|
|
166
171
|
}
|
|
167
172
|
if (err) {
|
|
@@ -172,7 +177,7 @@ class PoolResource extends node_events_1.EventEmitter {
|
|
|
172
177
|
}
|
|
173
178
|
if (this.auth && (this.connection.allowsAuth || options.forceAuth)) {
|
|
174
179
|
this.connection.login(this.auth, err => {
|
|
175
|
-
if (returned) {
|
|
180
|
+
if (returned || this._closed) {
|
|
176
181
|
return;
|
|
177
182
|
}
|
|
178
183
|
returned = true;
|
|
@@ -200,6 +205,9 @@ class PoolResource extends node_events_1.EventEmitter {
|
|
|
200
205
|
* @param callback Callback function
|
|
201
206
|
*/
|
|
202
207
|
send(mail, callback) {
|
|
208
|
+
if (this._closed) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
203
211
|
if (!this._connected) {
|
|
204
212
|
return this.connect(err => {
|
|
205
213
|
if (err) {
|
|
@@ -231,6 +239,9 @@ class PoolResource extends node_events_1.EventEmitter {
|
|
|
231
239
|
const reused = this.messages > 0;
|
|
232
240
|
const messageStream = mail.message.createReadStream();
|
|
233
241
|
this.connection.send(envelope, messageStream, (err, info) => {
|
|
242
|
+
if (mail.data.signal?.aborted) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
234
245
|
this._sending = false;
|
|
235
246
|
this.messages++;
|
|
236
247
|
if (err) {
|
|
@@ -277,6 +288,9 @@ class PoolResource extends node_events_1.EventEmitter {
|
|
|
277
288
|
* @internal
|
|
278
289
|
*/
|
|
279
290
|
_release() {
|
|
291
|
+
if (this._closed) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
280
294
|
if (this.messages >= this.options.maxMessages) {
|
|
281
295
|
const err = new Error('Resource exhausted');
|
|
282
296
|
err.code = errors.EMAXLIMIT;
|
|
@@ -285,6 +299,9 @@ class PoolResource extends node_events_1.EventEmitter {
|
|
|
285
299
|
return;
|
|
286
300
|
}
|
|
287
301
|
this.pool._checkRateLimit(() => {
|
|
302
|
+
if (this._closed) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
288
305
|
this.available = true;
|
|
289
306
|
this._startIdleTimer();
|
|
290
307
|
this.emit('available');
|
|
@@ -327,6 +344,11 @@ class PoolResource extends node_events_1.EventEmitter {
|
|
|
327
344
|
* Closes the connection
|
|
328
345
|
*/
|
|
329
346
|
close() {
|
|
347
|
+
if (this._closed) {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
this._closed = true;
|
|
351
|
+
this.available = false;
|
|
330
352
|
this._connected = false;
|
|
331
353
|
this._stopIdleTimer();
|
|
332
354
|
if (this.auth && this.auth.oauth2) {
|
|
@@ -93,6 +93,7 @@ export type SMTPTransportSendCallback = ResultCallback<SMTPSentMessageInfo>;
|
|
|
93
93
|
* @param options Connection options
|
|
94
94
|
*/
|
|
95
95
|
declare class SMTPTransport extends EventEmitter {
|
|
96
|
+
readonly supportsAbortSignal = true;
|
|
96
97
|
options: SMTPTransportOptions;
|
|
97
98
|
logger: shared.Logger;
|
|
98
99
|
name: string;
|
|
@@ -59,6 +59,7 @@ const MESSAGE_AUTH_KEYS = ['type', 'user', 'pass', 'accessToken', 'refreshToken'
|
|
|
59
59
|
class SMTPTransport extends node_events_1.EventEmitter {
|
|
60
60
|
constructor(options) {
|
|
61
61
|
super();
|
|
62
|
+
this.supportsAbortSignal = true;
|
|
62
63
|
options = options || {};
|
|
63
64
|
if (typeof options === 'string') {
|
|
64
65
|
options = {
|
|
@@ -153,13 +154,46 @@ class SMTPTransport extends node_events_1.EventEmitter {
|
|
|
153
154
|
* @param callback Callback function
|
|
154
155
|
*/
|
|
155
156
|
send(mail, callback) {
|
|
157
|
+
const signal = mail.data.signal;
|
|
158
|
+
let returned = false;
|
|
159
|
+
let connection;
|
|
160
|
+
let perCallAuth;
|
|
161
|
+
const cleanupPerCallAuth = () => {
|
|
162
|
+
if (perCallAuth && perCallAuth !== this.auth && perCallAuth.oauth2) {
|
|
163
|
+
perCallAuth.oauth2.removeAllListeners();
|
|
164
|
+
}
|
|
165
|
+
perCallAuth = null;
|
|
166
|
+
};
|
|
167
|
+
const finish = (err, info) => {
|
|
168
|
+
if (returned) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
returned = true;
|
|
172
|
+
signal?.removeEventListener('abort', onAbort);
|
|
173
|
+
cleanupPerCallAuth();
|
|
174
|
+
connection?.close();
|
|
175
|
+
callback(err, info);
|
|
176
|
+
};
|
|
177
|
+
function onAbort() {
|
|
178
|
+
finish(errors.createAbortError(signal));
|
|
179
|
+
}
|
|
180
|
+
if (signal) {
|
|
181
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
182
|
+
if (signal.aborted) {
|
|
183
|
+
setImmediate(onAbort);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
156
187
|
// the proxy handshake, if any, counts against connectionTimeout as well
|
|
157
188
|
const connectStartedAt = Date.now();
|
|
158
189
|
this.getSocket(Object.assign({}, this.options, { connectStartedAt }), (err, socketOptions) => {
|
|
190
|
+
if (returned) {
|
|
191
|
+
socketOptions && socketOptions.connection?.destroy();
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
159
194
|
if (err) {
|
|
160
|
-
return
|
|
195
|
+
return finish(err);
|
|
161
196
|
}
|
|
162
|
-
let returned = false;
|
|
163
197
|
let options = this.options;
|
|
164
198
|
if (socketOptions && socketOptions.connection) {
|
|
165
199
|
this.logger.info({
|
|
@@ -173,24 +207,9 @@ class SMTPTransport extends node_events_1.EventEmitter {
|
|
|
173
207
|
// only copy options if we need to modify it
|
|
174
208
|
options = Object.assign(shared.assign(false, options), socketOptions);
|
|
175
209
|
}
|
|
176
|
-
const connection = new index_js_1.default(options);
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
const cleanupPerCallAuth = () => {
|
|
180
|
-
if (perCallAuth && perCallAuth !== this.auth && perCallAuth.oauth2) {
|
|
181
|
-
perCallAuth.oauth2.removeAllListeners();
|
|
182
|
-
}
|
|
183
|
-
perCallAuth = null;
|
|
184
|
-
};
|
|
185
|
-
connection.once('error', err => {
|
|
186
|
-
if (returned) {
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
returned = true;
|
|
190
|
-
cleanupPerCallAuth();
|
|
191
|
-
connection.close();
|
|
192
|
-
return callback(err);
|
|
193
|
-
});
|
|
210
|
+
const smtp = (connection = new index_js_1.default(options));
|
|
211
|
+
smtp._connectStartedAt = connectStartedAt;
|
|
212
|
+
smtp.once('error', err => finish(err));
|
|
194
213
|
const sendMessage = () => {
|
|
195
214
|
const envelope = mail.message.getEnvelope();
|
|
196
215
|
const messageId = mail.message.messageId();
|
|
@@ -209,20 +228,17 @@ class SMTPTransport extends node_events_1.EventEmitter {
|
|
|
209
228
|
tnx: 'send',
|
|
210
229
|
messageId
|
|
211
230
|
}, 'Sending message %s to <%s>', messageId, recipients.join(', '));
|
|
212
|
-
|
|
231
|
+
smtp.send(envelope, mail.message.createReadStream(), (err, info) => {
|
|
213
232
|
if (returned) {
|
|
214
233
|
// the connection error handler has already reported this send
|
|
215
234
|
return;
|
|
216
235
|
}
|
|
217
|
-
returned = true;
|
|
218
|
-
cleanupPerCallAuth();
|
|
219
|
-
connection.close();
|
|
220
236
|
if (err) {
|
|
221
237
|
this.logger.error({
|
|
222
238
|
err,
|
|
223
239
|
tnx: 'send'
|
|
224
240
|
}, 'Send error for %s: %s', messageId, err.message);
|
|
225
|
-
return
|
|
241
|
+
return finish(err);
|
|
226
242
|
}
|
|
227
243
|
info.envelope = {
|
|
228
244
|
from: envelope.from,
|
|
@@ -230,7 +246,7 @@ class SMTPTransport extends node_events_1.EventEmitter {
|
|
|
230
246
|
};
|
|
231
247
|
info.messageId = messageId;
|
|
232
248
|
try {
|
|
233
|
-
return
|
|
249
|
+
return finish(null, info);
|
|
234
250
|
}
|
|
235
251
|
catch (E) {
|
|
236
252
|
this.logger.error({
|
|
@@ -240,27 +256,23 @@ class SMTPTransport extends node_events_1.EventEmitter {
|
|
|
240
256
|
}
|
|
241
257
|
});
|
|
242
258
|
};
|
|
243
|
-
|
|
259
|
+
smtp.connect(err => {
|
|
244
260
|
if (returned) {
|
|
245
261
|
return;
|
|
246
262
|
}
|
|
247
263
|
if (err) {
|
|
248
264
|
// the server closed the connection before the greeting
|
|
249
|
-
|
|
250
|
-
connection.close();
|
|
251
|
-
return callback(err);
|
|
265
|
+
return finish(err);
|
|
252
266
|
}
|
|
253
267
|
perCallAuth = this.getAuth(mail.data.auth);
|
|
254
|
-
if (perCallAuth && (
|
|
255
|
-
|
|
268
|
+
if (perCallAuth && (smtp.allowsAuth || options.forceAuth)) {
|
|
269
|
+
smtp.login(perCallAuth, err => {
|
|
256
270
|
cleanupPerCallAuth();
|
|
257
271
|
if (returned) {
|
|
258
272
|
return;
|
|
259
273
|
}
|
|
260
274
|
if (err) {
|
|
261
|
-
|
|
262
|
-
connection.close();
|
|
263
|
-
return callback(err);
|
|
275
|
+
return finish(err);
|
|
264
276
|
}
|
|
265
277
|
sendMessage();
|
|
266
278
|
});
|
package/dist/esm/dkim/sign.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as shared from '../shared/index.js';
|
|
2
2
|
import * as mimeFuncs from '../mime-funcs/index.js';
|
|
3
3
|
import crypto from 'node:crypto';
|
|
4
4
|
import * as errors from '../errors.js';
|
|
@@ -65,7 +65,7 @@ function generateDKIMHeader(domainName, keySelector, fieldNames, hashAlgo, bodyH
|
|
|
65
65
|
'v=1',
|
|
66
66
|
'a=rsa-' + hashAlgo,
|
|
67
67
|
'c=relaxed/relaxed',
|
|
68
|
-
'd=' +
|
|
68
|
+
'd=' + shared.normalizeDomain(cleanTagValue(domainName), false),
|
|
69
69
|
'q=dns/txt',
|
|
70
70
|
's=' + cleanTagValue(keySelector),
|
|
71
71
|
'bh=' + bodyHash,
|
package/dist/esm/errors.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export declare const ERROR_CODES: {
|
|
|
23
23
|
readonly EMESSAGE: "Message delivery error";
|
|
24
24
|
readonly EOTP: "Invalid one-time passcode data (otp option)";
|
|
25
25
|
readonly ESTREAM: "Stream processing error";
|
|
26
|
+
readonly EABORT: "Message sending aborted";
|
|
26
27
|
readonly EAUTH: "Authentication failed";
|
|
27
28
|
readonly ENOAUTH: "Authentication credentials not provided";
|
|
28
29
|
readonly EOAUTH2: "OAuth2 token generation or refresh error";
|
|
@@ -51,6 +52,7 @@ export declare const EENVELOPE = "EENVELOPE";
|
|
|
51
52
|
export declare const EMESSAGE = "EMESSAGE";
|
|
52
53
|
export declare const EOTP = "EOTP";
|
|
53
54
|
export declare const ESTREAM = "ESTREAM";
|
|
55
|
+
export declare const EABORT = "EABORT";
|
|
54
56
|
export declare const EAUTH = "EAUTH";
|
|
55
57
|
export declare const ENOAUTH = "ENOAUTH";
|
|
56
58
|
export declare const EOAUTH2 = "EOAUTH2";
|
package/dist/esm/errors.js
CHANGED
|
@@ -26,6 +26,7 @@ export const ERROR_CODES = {
|
|
|
26
26
|
EMESSAGE: 'Message delivery error',
|
|
27
27
|
EOTP: 'Invalid one-time passcode data (otp option)',
|
|
28
28
|
ESTREAM: 'Stream processing error',
|
|
29
|
+
EABORT: 'Message sending aborted',
|
|
29
30
|
// Authentication errors
|
|
30
31
|
EAUTH: 'Authentication failed',
|
|
31
32
|
ENOAUTH: 'Authentication credentials not provided',
|
|
@@ -55,6 +56,7 @@ export const EENVELOPE = 'EENVELOPE';
|
|
|
55
56
|
export const EMESSAGE = 'EMESSAGE';
|
|
56
57
|
export const EOTP = 'EOTP';
|
|
57
58
|
export const ESTREAM = 'ESTREAM';
|
|
59
|
+
export const EABORT = 'EABORT';
|
|
58
60
|
export const EAUTH = 'EAUTH';
|
|
59
61
|
export const ENOAUTH = 'ENOAUTH';
|
|
60
62
|
export const EOAUTH2 = 'EOAUTH2';
|
|
@@ -67,6 +69,13 @@ export const EPROXY = 'EPROXY';
|
|
|
67
69
|
export const EFILEACCESS = 'EFILEACCESS';
|
|
68
70
|
export const EURLACCESS = 'EURLACCESS';
|
|
69
71
|
export const EFETCH = 'EFETCH';
|
|
72
|
+
/** Creates the error reported when a caller cancels an individual send. @internal */
|
|
73
|
+
export const createAbortError = (signal) => {
|
|
74
|
+
const err = new Error('Message sending aborted', { cause: signal.reason });
|
|
75
|
+
err.name = 'AbortError';
|
|
76
|
+
err.code = EABORT;
|
|
77
|
+
return err;
|
|
78
|
+
};
|
|
70
79
|
/**
|
|
71
80
|
* Tells whether the error ended the connection rather than rejected what was sent over it
|
|
72
81
|
*/
|
|
@@ -48,6 +48,8 @@ class JSONTransport {
|
|
|
48
48
|
}
|
|
49
49
|
delete data.envelope;
|
|
50
50
|
delete data.normalizedHeaders;
|
|
51
|
+
// the per-message AbortSignal is not message data
|
|
52
|
+
delete data.signal;
|
|
51
53
|
// the message field is the object itself with skipEncoding, the
|
|
52
54
|
// createTransport overload for that option types the result accordingly
|
|
53
55
|
return done(null, {
|
|
@@ -112,6 +112,11 @@ export interface MailMeta {
|
|
|
112
112
|
* declared on Transport, Transporter and Mail
|
|
113
113
|
*/
|
|
114
114
|
export interface Transport<T = SentMessageInfo, D extends TransportOptions = TransportOptions> {
|
|
115
|
+
/**
|
|
116
|
+
* Set only if send() cancels its own work on mail.data.signal and removes its signal listener
|
|
117
|
+
* when finished. Without it the signal is still honored up to dispatch, then ignored.
|
|
118
|
+
*/
|
|
119
|
+
supportsAbortSignal?: boolean | undefined;
|
|
115
120
|
/** Transport name, used for logging */
|
|
116
121
|
name: string;
|
|
117
122
|
/** Transport version, used for logging */
|
package/dist/esm/mailer/index.js
CHANGED
|
@@ -18,6 +18,16 @@ import crypto from 'node:crypto';
|
|
|
18
18
|
* asks a server to accept 100, so a real send is bounded far below this.
|
|
19
19
|
*/
|
|
20
20
|
const DEFAULT_MAX_RECIPIENTS = 100000;
|
|
21
|
+
/**
|
|
22
|
+
* Duck-typed so that signals from another realm or a polyfill are accepted as well
|
|
23
|
+
*/
|
|
24
|
+
function isAbortSignal(value) {
|
|
25
|
+
return (typeof value === 'object' &&
|
|
26
|
+
value !== null &&
|
|
27
|
+
'aborted' in value &&
|
|
28
|
+
typeof value.addEventListener === 'function' &&
|
|
29
|
+
typeof value.removeEventListener === 'function');
|
|
30
|
+
}
|
|
21
31
|
/**
|
|
22
32
|
* Creates an object for exposing the Mail API
|
|
23
33
|
*
|
|
@@ -119,11 +129,43 @@ class Mail extends EventEmitter {
|
|
|
119
129
|
this.getSocket = false;
|
|
120
130
|
}
|
|
121
131
|
const mail = new MailMessage(this, data);
|
|
132
|
+
const signalOption = mail.data.signal;
|
|
133
|
+
const signal = isAbortSignal(signalOption) ? signalOption : undefined;
|
|
134
|
+
let settled = false;
|
|
135
|
+
const removeAbortListener = () => signal?.removeEventListener('abort', onAbort);
|
|
136
|
+
const finish = (err, info) => {
|
|
137
|
+
if (settled) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
settled = true;
|
|
141
|
+
removeAbortListener();
|
|
142
|
+
if (err) {
|
|
143
|
+
mail.releaseStreams();
|
|
144
|
+
}
|
|
145
|
+
done(err, info);
|
|
146
|
+
};
|
|
122
147
|
// a failed message is not going to be read anymore, release its content streams
|
|
123
148
|
const fail = (err) => {
|
|
124
|
-
|
|
125
|
-
done(err);
|
|
149
|
+
finish(err);
|
|
126
150
|
};
|
|
151
|
+
function onAbort() {
|
|
152
|
+
fail(errors.createAbortError(signal));
|
|
153
|
+
}
|
|
154
|
+
if (signalOption !== undefined && signalOption !== null && !signal) {
|
|
155
|
+
const err = new Error('The signal option must be an AbortSignal');
|
|
156
|
+
err.code = errors.ECONFIG;
|
|
157
|
+
setImmediate(() => fail(err));
|
|
158
|
+
return promise;
|
|
159
|
+
}
|
|
160
|
+
// Mail handles a pre-aborted signal and cancellation during plugin processing for every
|
|
161
|
+
// transport; a transport without supportsAbortSignal ignores the signal once it is called
|
|
162
|
+
if (signal) {
|
|
163
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
164
|
+
if (signal.aborted) {
|
|
165
|
+
setImmediate(onAbort);
|
|
166
|
+
return promise;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
127
169
|
this.logger.debug({
|
|
128
170
|
tnx: 'transport',
|
|
129
171
|
name: this.transporter.name,
|
|
@@ -131,6 +173,9 @@ class Mail extends EventEmitter {
|
|
|
131
173
|
action: 'send'
|
|
132
174
|
}, 'Sending mail using %s/%s', this.transporter.name, this.transporter.version);
|
|
133
175
|
this._processPlugins('compile', mail, err => {
|
|
176
|
+
if (settled) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
134
179
|
if (err) {
|
|
135
180
|
this.logger.error({
|
|
136
181
|
err,
|
|
@@ -168,6 +213,9 @@ class Mail extends EventEmitter {
|
|
|
168
213
|
return fail(err);
|
|
169
214
|
}
|
|
170
215
|
this._processPlugins('stream', mail, err => {
|
|
216
|
+
if (settled) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
171
219
|
if (err) {
|
|
172
220
|
this.logger.error({
|
|
173
221
|
err,
|
|
@@ -187,16 +235,17 @@ class Mail extends EventEmitter {
|
|
|
187
235
|
return dkim.sign(input, mail.data._dkim);
|
|
188
236
|
});
|
|
189
237
|
}
|
|
238
|
+
// The transport takes ownership of cancellation from here, including queued work.
|
|
239
|
+
removeAbortListener();
|
|
190
240
|
this.transporter.send(mail, (...args) => {
|
|
191
241
|
if (args[0]) {
|
|
192
|
-
mail.releaseStreams();
|
|
193
242
|
this.logger.error({
|
|
194
243
|
err: args[0],
|
|
195
244
|
tnx: 'transport',
|
|
196
245
|
action: 'send'
|
|
197
246
|
}, 'Send Error: %s', args[0].message);
|
|
198
247
|
}
|
|
199
|
-
|
|
248
|
+
finish(...args);
|
|
200
249
|
});
|
|
201
250
|
});
|
|
202
251
|
});
|
|
@@ -226,6 +275,9 @@ class Mail extends EventEmitter {
|
|
|
226
275
|
let pos = 0;
|
|
227
276
|
let block = 'default';
|
|
228
277
|
const processPlugins = () => {
|
|
278
|
+
if (mail.data.signal?.aborted) {
|
|
279
|
+
return callback(errors.createAbortError(mail.data.signal));
|
|
280
|
+
}
|
|
229
281
|
let curplugins = block === 'default' ? defaultPlugins : userPlugins;
|
|
230
282
|
if (pos >= curplugins.length) {
|
|
231
283
|
if (block === 'default' && userPlugins.length) {
|
|
@@ -13,6 +13,16 @@ import type { SentMessageInfo } from './index.js';
|
|
|
13
13
|
* tree is built from, the fields below are the ones the mailer reads on top of those
|
|
14
14
|
*/
|
|
15
15
|
export interface SendMailOptions extends MailComposerOptions {
|
|
16
|
+
/**
|
|
17
|
+
* Cancel this send, including waiting for plugins or in an SMTP pool queue.
|
|
18
|
+
* Plugins must cooperate with the signal to stop their own work already in progress.
|
|
19
|
+
* Supported by SMTP, pooled SMTP and transports declaring supportsAbortSignal. Other
|
|
20
|
+
* transports ignore the signal once the message is handed to them; a signal that is
|
|
21
|
+
* already aborted, or aborts during plugin processing, still cancels the send.
|
|
22
|
+
* Fails with AbortError/EABORT on cancellation, ECONFIG if the value is not an AbortSignal.
|
|
23
|
+
* Aborting does not prove that the server has not already accepted the message.
|
|
24
|
+
*/
|
|
25
|
+
signal?: AbortSignal | undefined;
|
|
16
26
|
/** DKIM signing options for this message, used instead of the ones of the transporter */
|
|
17
27
|
dkim?: DKIMOptions | undefined;
|
|
18
28
|
/** Extra DKIM options for this message, merged over the options of the signer. The SES transport sets skipFields here, the option is not meant for callers */
|
|
@@ -74,7 +74,8 @@ export declare function hasLongerLines(str: string, lineLength: number): boolean
|
|
|
74
74
|
*/
|
|
75
75
|
export declare function encodeWord(data: string | Buffer, mimeWordEncoding?: string, maxLength?: number): string;
|
|
76
76
|
/**
|
|
77
|
-
* Finds word sequences with non ascii text and
|
|
77
|
+
* Finds word sequences with non ascii text, and words that look like an invalid encoded-word,
|
|
78
|
+
* and converts these to mime words. A valid encoded-word is passed through as it is.
|
|
78
79
|
*
|
|
79
80
|
* @param value String to be encoded
|
|
80
81
|
* @param mimeWordEncoding='Q' Encoding for the mime word, either Q or B
|