emailjs 5.0.5 → 6.0.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/README.md +10 -5
- package/dist/auth-admission.d.ts +13 -0
- package/dist/auth-admission.js +112 -0
- package/dist/auth-admission.js.map +1 -0
- package/dist/auth-admission.test.d.ts +1 -0
- package/dist/auth-admission.test.js +51 -0
- package/dist/auth-admission.test.js.map +1 -0
- package/dist/auth.integration.test.js +1 -3
- package/dist/auth.integration.test.js.map +1 -1
- package/dist/client.auth-interruption.integration.test.d.ts +1 -0
- package/dist/client.auth-interruption.integration.test.js +152 -0
- package/dist/client.auth-interruption.integration.test.js.map +1 -0
- package/dist/client.auth-starttls.integration.test.d.ts +1 -0
- package/dist/client.auth-starttls.integration.test.js +58 -0
- package/dist/client.auth-starttls.integration.test.js.map +1 -0
- package/dist/client.d.ts +9 -7
- package/dist/client.data-ownership.test.d.ts +1 -0
- package/dist/client.data-ownership.test.js +221 -0
- package/dist/client.data-ownership.test.js.map +1 -0
- package/dist/client.integration.test.js +161 -21
- package/dist/client.integration.test.js.map +1 -1
- package/dist/client.js +88 -84
- package/dist/client.js.map +1 -1
- package/dist/client.test.js +177 -138
- package/dist/client.test.js.map +1 -1
- package/dist/connection.auth-ownership.test.d.ts +1 -0
- package/dist/connection.auth-ownership.test.js +474 -0
- package/dist/connection.auth-ownership.test.js.map +1 -0
- package/dist/connection.body-ownership.test.d.ts +1 -0
- package/dist/connection.body-ownership.test.js +73 -0
- package/dist/connection.body-ownership.test.js.map +1 -0
- package/dist/connection.cached-login.test.d.ts +1 -0
- package/dist/connection.cached-login.test.js +290 -0
- package/dist/connection.cached-login.test.js.map +1 -0
- package/dist/connection.d.ts +18 -7
- package/dist/connection.errors.integration.test.js +86 -32
- package/dist/connection.errors.integration.test.js.map +1 -1
- package/dist/connection.greeting-ownership.test.d.ts +1 -0
- package/dist/connection.greeting-ownership.test.js +76 -0
- package/dist/connection.greeting-ownership.test.js.map +1 -0
- package/dist/connection.integration.test.js +108 -41
- package/dist/connection.integration.test.js.map +1 -1
- package/dist/connection.js +312 -188
- package/dist/connection.js.map +1 -1
- package/dist/connection.login-boundaries.test.d.ts +1 -0
- package/dist/connection.login-boundaries.test.js +151 -0
- package/dist/connection.login-boundaries.test.js.map +1 -0
- package/dist/connection.test.js +74 -8
- package/dist/connection.test.js.map +1 -1
- package/dist/message.integration.test.js +2 -5
- package/dist/message.integration.test.js.map +1 -1
- package/dist/response.js +19 -20
- package/dist/response.js.map +1 -1
- package/dist/response.test.js +17 -2
- package/dist/response.test.js.map +1 -1
- package/dist/smtp-auth.d.ts +13 -0
- package/dist/smtp-auth.js +40 -0
- package/dist/smtp-auth.js.map +1 -0
- package/dist/smtp-data.d.ts +18 -0
- package/dist/smtp-data.js +75 -0
- package/dist/smtp-data.js.map +1 -0
- package/dist/smtp-session.d.ts +7 -0
- package/dist/smtp-session.js +18 -0
- package/dist/smtp-session.js.map +1 -0
- package/dist/smtp-transport.d.ts +31 -0
- package/dist/smtp-transport.js +143 -0
- package/dist/smtp-transport.js.map +1 -0
- package/dist/tls-upgrade.d.ts +5 -0
- package/dist/tls-upgrade.js +30 -0
- package/dist/tls-upgrade.js.map +1 -0
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -1,21 +1,29 @@
|
|
|
1
1
|
import { addressparser, flattenAddressGroups } from './address.js';
|
|
2
2
|
import { Message } from './message.js';
|
|
3
|
+
import { SMTPError, SMTPErrorStates } from './error.js';
|
|
4
|
+
import { getSession } from './smtp-session.js';
|
|
3
5
|
import { SMTPConnection, SMTPState } from './connection.js';
|
|
4
6
|
export class SMTPClient {
|
|
5
|
-
smtp;
|
|
7
|
+
#smtp;
|
|
6
8
|
queue = [];
|
|
7
9
|
sending = false;
|
|
8
|
-
ready = false;
|
|
9
10
|
timer = null;
|
|
11
|
+
waitingForQuitToFinish = false;
|
|
10
12
|
constructor(server) {
|
|
11
|
-
this
|
|
12
|
-
this
|
|
13
|
+
this.#smtp = new SMTPConnection(server);
|
|
14
|
+
this.#smtp.on('close', () => {
|
|
13
15
|
if (this.timer != null) {
|
|
14
16
|
clearTimeout(this.timer);
|
|
15
17
|
this.timer = null;
|
|
16
18
|
}
|
|
19
|
+
if (this.waitingForQuitToFinish)
|
|
20
|
+
this.resumeQueueAfterQuit();
|
|
17
21
|
});
|
|
18
22
|
}
|
|
23
|
+
/** Close the current connection. A later send reconnects automatically. */
|
|
24
|
+
close() {
|
|
25
|
+
this.#smtp.close();
|
|
26
|
+
}
|
|
19
27
|
send(msg, callback) {
|
|
20
28
|
const message = msg instanceof Message
|
|
21
29
|
? msg
|
|
@@ -80,57 +88,62 @@ export class SMTPClient {
|
|
|
80
88
|
}
|
|
81
89
|
return stack;
|
|
82
90
|
}
|
|
91
|
+
resumeQueueAfterQuit() {
|
|
92
|
+
// Defer reconnecting so the old QUIT callback can't close the new connection.
|
|
93
|
+
queueMicrotask(() => {
|
|
94
|
+
if (!this.waitingForQuitToFinish)
|
|
95
|
+
return;
|
|
96
|
+
this.waitingForQuitToFinish = false;
|
|
97
|
+
this._poll();
|
|
98
|
+
});
|
|
99
|
+
}
|
|
83
100
|
_poll() {
|
|
101
|
+
if (this.sending || this.waitingForQuitToFinish)
|
|
102
|
+
return;
|
|
84
103
|
if (this.timer != null) {
|
|
85
104
|
clearTimeout(this.timer);
|
|
86
105
|
}
|
|
106
|
+
// Setup owns a delivery until it succeeds or closes the connection.
|
|
107
|
+
// An idle, connected client therefore already has a prepared session.
|
|
108
|
+
const state = this.#smtp.state();
|
|
87
109
|
if (this.queue.length) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
this.
|
|
94
|
-
this._sendmail(this.queue.shift());
|
|
95
|
-
}
|
|
110
|
+
const delivery = this.queue.shift();
|
|
111
|
+
this.sending = true;
|
|
112
|
+
if (state === SMTPState.NOTCONNECTED)
|
|
113
|
+
this._connect(delivery);
|
|
114
|
+
else
|
|
115
|
+
this._sendmail(delivery);
|
|
96
116
|
}
|
|
97
|
-
//
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
117
|
+
// Give new mail one second to arrive before closing an idle connection.
|
|
118
|
+
else if (state === SMTPState.CONNECTED) {
|
|
119
|
+
this.timer = setTimeout(() => {
|
|
120
|
+
this.timer = null;
|
|
121
|
+
this.waitingForQuitToFinish = true;
|
|
122
|
+
this.#smtp.quit();
|
|
123
|
+
}, 1000);
|
|
101
124
|
}
|
|
102
125
|
}
|
|
103
126
|
_connect(stack) {
|
|
104
|
-
|
|
105
|
-
if (
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
stack.callback(err, stack.message);
|
|
127
|
-
// clear out the queue so all callbacks can be called with the same error message
|
|
128
|
-
this.queue.shift();
|
|
129
|
-
this._poll();
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
this.ready = false;
|
|
133
|
-
this.smtp.connect(connect);
|
|
127
|
+
this.#smtp.connect((err) => {
|
|
128
|
+
if (err)
|
|
129
|
+
return this._senddone(err, stack);
|
|
130
|
+
const session = getSession(this.#smtp);
|
|
131
|
+
const done = (err) => this.finishSetup(stack, session, err);
|
|
132
|
+
if (this.#smtp.authorized())
|
|
133
|
+
this.#smtp.ehlo_or_helo_if_needed(done);
|
|
134
|
+
else
|
|
135
|
+
this.#smtp.login(done);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
finishSetup(stack, session, err) {
|
|
139
|
+
if (err) {
|
|
140
|
+
// Discard failed setup before releasing this delivery to its caller.
|
|
141
|
+
session?.close();
|
|
142
|
+
return this._senddone(err, stack);
|
|
143
|
+
}
|
|
144
|
+
if (session !== getSession(this.#smtp))
|
|
145
|
+
return this._senddone(SMTPError.create('connection closed during setup', SMTPErrorStates.CONNECTIONCLOSED), stack);
|
|
146
|
+
this._sendmail(stack);
|
|
134
147
|
}
|
|
135
148
|
_canMakeMessage(msg) {
|
|
136
149
|
return (msg.from &&
|
|
@@ -153,50 +166,41 @@ export class SMTPClient {
|
|
|
153
166
|
attachment.alternative === true) ||
|
|
154
167
|
false);
|
|
155
168
|
}
|
|
156
|
-
_sendsmtp(stack, next) {
|
|
157
|
-
return (err) => {
|
|
158
|
-
if (!err && next) {
|
|
159
|
-
next.apply(this, [stack]);
|
|
160
|
-
}
|
|
161
|
-
else {
|
|
162
|
-
// if we snag on SMTP commands, call done, passing the error
|
|
163
|
-
// but first reset SMTP state so queue can continue polling
|
|
164
|
-
this.smtp.rset(() => this._senddone(err, stack));
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
169
|
_sendmail(stack) {
|
|
169
|
-
const from = stack.returnPath || stack.from
|
|
170
|
-
|
|
171
|
-
this
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const to = stack.to.shift()?.address;
|
|
178
|
-
this.smtp.rcpt(this._sendsmtp(stack, stack.to.length ? this._sendrcpt : this._senddata), `<${to}>`);
|
|
179
|
-
}
|
|
180
|
-
_senddata(stack) {
|
|
181
|
-
this.smtp.data(this._sendsmtp(stack, this._sendmessage));
|
|
182
|
-
}
|
|
183
|
-
_sendmessage(stack) {
|
|
184
|
-
const stream = stack.message.stream();
|
|
185
|
-
stream.on('data', (data) => this.smtp.message(data));
|
|
186
|
-
stream.on('end', () => {
|
|
187
|
-
this.smtp.data_end(this._sendsmtp(stack, () => this._senddone(null, stack)));
|
|
170
|
+
const from = `<${stack.returnPath || stack.from}>`;
|
|
171
|
+
const recipients = stack.to.map(({ address }) => `<${address}>`);
|
|
172
|
+
const transfer = this.#smtp.captureDataTransfer();
|
|
173
|
+
this.#smtp.sendEnvelope(from, recipients, (err) => {
|
|
174
|
+
if (err)
|
|
175
|
+
this._senddone(err, stack);
|
|
176
|
+
else
|
|
177
|
+
this._senddata(stack, transfer);
|
|
188
178
|
});
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
179
|
+
}
|
|
180
|
+
_senddata(stack, transfer) {
|
|
181
|
+
transfer.open((err) => {
|
|
182
|
+
if (err)
|
|
183
|
+
this._senddone(err, stack);
|
|
184
|
+
else
|
|
185
|
+
this._sendmessage(stack, transfer);
|
|
194
186
|
});
|
|
195
187
|
}
|
|
188
|
+
_sendmessage(stack, transfer = this.#smtp.captureDataTransfer()) {
|
|
189
|
+
transfer.stream((body) => {
|
|
190
|
+
const stream = stack.message.stream();
|
|
191
|
+
stream.on('data', body.write);
|
|
192
|
+
stream.on('end', body.end);
|
|
193
|
+
stream.on('error', body.abort);
|
|
194
|
+
}, (err) => this._senddone(err, stack));
|
|
195
|
+
}
|
|
196
196
|
_senddone(err, stack) {
|
|
197
197
|
this.sending = false;
|
|
198
|
-
|
|
199
|
-
|
|
198
|
+
try {
|
|
199
|
+
stack.callback(err || null, stack.message);
|
|
200
|
+
}
|
|
201
|
+
finally {
|
|
202
|
+
this._poll();
|
|
203
|
+
}
|
|
200
204
|
}
|
|
201
205
|
}
|
|
202
206
|
//# sourceMappingURL=client.js.map
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAA;AAElE,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,cAAc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O,GAAG,KAAK,CAAA;QACpB,IAAI,CAAC;YACJ,KAAK,CAAC,QAAQ,CAAC,GAAG,IAAI,IAAI,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;QAC3C,CAAC;gBAAS,CAAC;YACV,IAAI,CAAC,KAAK,EAAE,CAAA;QACb,CAAC;IACF,CAAC;CACD"}
|
package/dist/client.test.js
CHANGED
|
@@ -1,22 +1,72 @@
|
|
|
1
|
-
import { describe, it, expect, vi } from 'vitest';
|
|
1
|
+
import { describe, it, expect, onTestFinished, vi } from 'vitest';
|
|
2
|
+
import { Socket } from 'node:net';
|
|
3
|
+
import { setImmediate } from 'node:timers/promises';
|
|
2
4
|
import { Readable } from 'stream';
|
|
3
5
|
import { SMTPClient } from './client.js';
|
|
4
6
|
import { Message } from './message.js';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
import { SMTPErrorStates } from './error.js';
|
|
8
|
+
// Script just the peer: the client still opens, greets and closes its own connection.
|
|
9
|
+
function scriptedRelay(client) {
|
|
10
|
+
function peer(socket) {
|
|
11
|
+
const write = vi.spyOn(socket, 'write').mockReturnValue(true);
|
|
12
|
+
const reply = (line) => socket.emit('data', `${line}\r\n`);
|
|
13
|
+
return {
|
|
14
|
+
write,
|
|
15
|
+
reply,
|
|
16
|
+
disconnect: () => socket.emit('end'),
|
|
17
|
+
greet(features = '250 hello') {
|
|
18
|
+
reply('220 ready');
|
|
19
|
+
reply(features);
|
|
20
|
+
},
|
|
21
|
+
async acceptMessage() {
|
|
22
|
+
reply('250 sender');
|
|
23
|
+
reply('250 recipient');
|
|
24
|
+
reply('354 continue');
|
|
25
|
+
await setImmediate(); // Let the real message stream finish.
|
|
26
|
+
expect(write).toHaveBeenLastCalledWith('\r\n.\r\n');
|
|
27
|
+
reply('250 accepted');
|
|
28
|
+
},
|
|
14
29
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
30
|
+
}
|
|
31
|
+
const peers = [];
|
|
32
|
+
const dial = vi
|
|
33
|
+
.spyOn(Socket.prototype, 'connect')
|
|
34
|
+
.mockImplementation(function () {
|
|
35
|
+
peers.push(peer(this));
|
|
36
|
+
return this;
|
|
37
|
+
});
|
|
38
|
+
onTestFinished(() => {
|
|
39
|
+
client.close();
|
|
40
|
+
dial.mockRestore();
|
|
41
|
+
});
|
|
42
|
+
return peers;
|
|
43
|
+
}
|
|
44
|
+
describe('SMTPClient', () => {
|
|
45
|
+
it('does not expose its managed connection, even as the logger receiver', () => {
|
|
46
|
+
const logger = vi.fn();
|
|
47
|
+
const client = new SMTPClient({ logger });
|
|
48
|
+
scriptedRelay(client);
|
|
49
|
+
expect('smtp' in client).toBe(false);
|
|
50
|
+
client.send(new Message({ from: 'me', to: 'you', text: 'hi' }), vi.fn());
|
|
51
|
+
expect(logger).toHaveBeenCalled();
|
|
52
|
+
for (const receiver of logger.mock.contexts)
|
|
53
|
+
expect(receiver).toBeUndefined();
|
|
54
|
+
});
|
|
55
|
+
it('passes a DATA transfer, not its connection, to the message hook', async () => {
|
|
56
|
+
class ForwardingClient extends SMTPClient {
|
|
57
|
+
_sendmessage(...[stack, transfer]) {
|
|
58
|
+
expect(transfer).toBeDefined();
|
|
59
|
+
expect(transfer).not.toHaveProperty('smtp');
|
|
60
|
+
super._sendmessage(stack, transfer);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const client = new ForwardingClient({});
|
|
64
|
+
const peers = scriptedRelay(client);
|
|
65
|
+
const msg = new Message({ from: 'me', to: 'you', text: 'hi' });
|
|
66
|
+
const delivery = client.sendAsync(msg);
|
|
67
|
+
peers[0].greet();
|
|
68
|
+
await peers[0].acceptMessage();
|
|
69
|
+
await expect(delivery).resolves.toBe(msg);
|
|
20
70
|
});
|
|
21
71
|
it('client deduplicates recipients', () => {
|
|
22
72
|
const msg = {
|
|
@@ -81,21 +131,17 @@ describe('SMTPClient', () => {
|
|
|
81
131
|
});
|
|
82
132
|
it.each(['to', 'cc', 'bcc'])('rejects an empty %s group before attempting a connection', async (field) => {
|
|
83
133
|
const client = new SMTPClient({ host: '127.0.0.1' });
|
|
84
|
-
const
|
|
85
|
-
.spyOn(client.smtp, 'connect')
|
|
86
|
-
.mockImplementation((callback) => {
|
|
87
|
-
callback(new Error('Unexpected SMTP connection'));
|
|
88
|
-
});
|
|
134
|
+
const peers = scriptedRelay(client);
|
|
89
135
|
const msg = new Message({
|
|
90
136
|
from: 'sender@origin.test',
|
|
91
137
|
[field]: 'Private:;',
|
|
92
138
|
text: 'Local regression fixture',
|
|
93
139
|
});
|
|
94
140
|
await expect(client.sendAsync(msg)).rejects.toThrow('No recipients found in message');
|
|
95
|
-
expect(
|
|
141
|
+
expect(peers).toHaveLength(0);
|
|
96
142
|
expect(client.queue).toEqual([]);
|
|
97
143
|
});
|
|
98
|
-
it('
|
|
144
|
+
it('sends BCC group recipients with only empty visible groups and reports a rejected RCPT', async () => {
|
|
99
145
|
const client = new SMTPClient({ host: '127.0.0.1' });
|
|
100
146
|
const msg = new Message({
|
|
101
147
|
from: 'sender@origin.test',
|
|
@@ -103,19 +149,25 @@ describe('SMTPClient', () => {
|
|
|
103
149
|
cc: 'Review:;',
|
|
104
150
|
bcc: 'Archive: mira@delivery.test, otis@delivery.test;, archive@delivery.test',
|
|
105
151
|
});
|
|
106
|
-
const
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
expect(
|
|
114
|
-
'RCPT TO:<mira@delivery.test
|
|
115
|
-
'RCPT TO:<otis@delivery.test
|
|
116
|
-
'RCPT TO:<archive@delivery.test
|
|
152
|
+
const peers = scriptedRelay(client);
|
|
153
|
+
const delivery = client.sendAsync(msg);
|
|
154
|
+
const [relay] = peers;
|
|
155
|
+
relay.greet();
|
|
156
|
+
relay.reply('250 sender');
|
|
157
|
+
relay.reply('250 recipient');
|
|
158
|
+
relay.reply('250 recipient');
|
|
159
|
+
expect(relay.write.mock.calls.slice(2)).toEqual([
|
|
160
|
+
['RCPT TO:<mira@delivery.test>\r\n'],
|
|
161
|
+
['RCPT TO:<otis@delivery.test>\r\n'],
|
|
162
|
+
['RCPT TO:<archive@delivery.test>\r\n'],
|
|
117
163
|
]);
|
|
118
|
-
|
|
164
|
+
relay.reply('550 Recipient rejected');
|
|
165
|
+
expect(relay.write).toHaveBeenLastCalledWith('rset\r\n');
|
|
166
|
+
relay.reply('250 reset');
|
|
167
|
+
await expect(delivery).rejects.toMatchObject({
|
|
168
|
+
code: SMTPErrorStates.BADRESPONSE,
|
|
169
|
+
smtp: expect.stringContaining('550 Recipient rejected'),
|
|
170
|
+
});
|
|
119
171
|
});
|
|
120
172
|
});
|
|
121
173
|
it('client accepts return path', () => {
|
|
@@ -249,19 +301,7 @@ describe('SMTPClient', () => {
|
|
|
249
301
|
});
|
|
250
302
|
it('client handles stream error during sending', async () => {
|
|
251
303
|
const client = new SMTPClient({ host: 'localhost' });
|
|
252
|
-
|
|
253
|
-
client.smtp.state = () => currentState;
|
|
254
|
-
client.smtp.connect = (cb) => {
|
|
255
|
-
currentState = SMTPState.CONNECTED;
|
|
256
|
-
cb(null);
|
|
257
|
-
};
|
|
258
|
-
client.smtp.ehlo_or_helo_if_needed = (cb) => cb(null);
|
|
259
|
-
client.smtp.mail = (cb) => cb(null);
|
|
260
|
-
client.smtp.rcpt = (cb) => cb(null);
|
|
261
|
-
client.smtp.data = (cb) => cb(null);
|
|
262
|
-
client.smtp.message = () => { };
|
|
263
|
-
client.smtp.close = () => { };
|
|
264
|
-
client.smtp.login = (cb) => cb(null);
|
|
304
|
+
const peers = scriptedRelay(client);
|
|
265
305
|
const stream = new Readable({
|
|
266
306
|
read() {
|
|
267
307
|
this.emit('error', new Error('stream kaboom'));
|
|
@@ -273,27 +313,13 @@ describe('SMTPClient', () => {
|
|
|
273
313
|
text: 'hi',
|
|
274
314
|
attachment: { stream, name: 'fail.txt' },
|
|
275
315
|
});
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
currentState = SMTPState.CONNECTED;
|
|
284
|
-
cb(null);
|
|
285
|
-
};
|
|
286
|
-
client.smtp.ehlo_or_helo_if_needed = (cb) => cb(null);
|
|
287
|
-
client.smtp.mail = (cb) => cb(null);
|
|
288
|
-
client.smtp.rcpt = (cb) => cb(new Error('Recipient rejected'));
|
|
289
|
-
client.smtp.rset = (cb) => cb(null);
|
|
290
|
-
const msg = new Message({ from: 'me', to: 'you', text: 'hi' });
|
|
291
|
-
await expect(client.sendAsync(msg)).rejects.toThrow('Recipient rejected');
|
|
292
|
-
});
|
|
293
|
-
it('throws if stack.to is invalid in _sendrcpt', () => {
|
|
294
|
-
const client = new SMTPClient({ host: 'localhost' });
|
|
295
|
-
// @ts-expect-error testing invalid input
|
|
296
|
-
expect(() => client._sendrcpt({ to: null })).toThrow('stack.to must be array');
|
|
316
|
+
const delivery = client.sendAsync(msg);
|
|
317
|
+
const [relay] = peers;
|
|
318
|
+
relay.greet();
|
|
319
|
+
relay.reply('250 sender');
|
|
320
|
+
relay.reply('250 recipient');
|
|
321
|
+
relay.reply('354 continue');
|
|
322
|
+
await expect(delivery).rejects.toThrow('stream kaboom');
|
|
297
323
|
});
|
|
298
324
|
it('client creates message stack with default callback', () => {
|
|
299
325
|
const client = new SMTPClient({ host: 'localhost' });
|
|
@@ -302,85 +328,98 @@ describe('SMTPClient', () => {
|
|
|
302
328
|
expect(typeof stack.callback).toBe('function');
|
|
303
329
|
stack.callback(null, msg); // Execute it to ensure it does nothing safely
|
|
304
330
|
});
|
|
305
|
-
it('
|
|
306
|
-
const client = new SMTPClient({
|
|
307
|
-
const
|
|
308
|
-
connect: (cb) => cb(),
|
|
309
|
-
authorized: () => false,
|
|
310
|
-
login: (cb) => cb(new Error('Login failed')),
|
|
311
|
-
state: () => SMTPState.NOTCONNECTED,
|
|
312
|
-
};
|
|
313
|
-
// @ts-expect-error mocking internals
|
|
314
|
-
client.smtp = mockSmtp;
|
|
331
|
+
it('keeps a retry queued when the login failure callback sends again', () => {
|
|
332
|
+
const client = new SMTPClient({ user: 'user', password: 'password' });
|
|
333
|
+
const peers = scriptedRelay(client);
|
|
315
334
|
const msg = new Message({ from: 'me', to: 'you', text: 'hi' });
|
|
316
|
-
|
|
335
|
+
const retry = vi.fn();
|
|
336
|
+
const first = vi.fn(() => client.send(msg, retry));
|
|
337
|
+
client.send(msg, first);
|
|
338
|
+
expect(peers).toHaveLength(1);
|
|
339
|
+
for (let i = 0; i < 2; i++) {
|
|
340
|
+
peers[i].greet('250 AUTH PLAIN');
|
|
341
|
+
expect(peers[i].write).toHaveBeenLastCalledWith(expect.stringMatching(/^AUTH PLAIN /i));
|
|
342
|
+
expect(retry).not.toHaveBeenCalled();
|
|
343
|
+
peers[i].reply('535 login failed');
|
|
344
|
+
}
|
|
345
|
+
const failed = expect.objectContaining({ code: SMTPErrorStates.AUTHFAILED });
|
|
346
|
+
expect(first).toHaveBeenCalledExactlyOnceWith(failed, msg);
|
|
347
|
+
expect(retry).toHaveBeenCalledExactlyOnceWith(failed, msg);
|
|
348
|
+
expect(first).toHaveBeenCalledBefore(retry);
|
|
349
|
+
expect(peers).toHaveLength(2);
|
|
317
350
|
});
|
|
318
351
|
it('sendAsync rejects if _canMakeMessage fails', async () => {
|
|
319
352
|
const client = new SMTPClient({ host: 'localhost' });
|
|
320
353
|
await expect(client.sendAsync({ from: 'me', text: 'hi' })).rejects.toThrow('message is not a valid Message instance');
|
|
321
354
|
await expect(client.sendAsync({ from: 'me', to: 'you' })).rejects.toThrow('message is not a valid Message instance');
|
|
322
355
|
});
|
|
323
|
-
it('
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
client
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
expect(
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
await client.sendAsync(msg);
|
|
350
|
-
// Should clear previous timer
|
|
351
|
-
expect(spy).toHaveBeenCalledWith(firstTimer);
|
|
352
|
-
spy.mockRestore();
|
|
356
|
+
it.each(['from a callback', 'while idle'])('does not send idle QUIT over another message sent %s', async (when) => {
|
|
357
|
+
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
|
|
358
|
+
onTestFinished(() => {
|
|
359
|
+
vi.useRealTimers();
|
|
360
|
+
});
|
|
361
|
+
const client = new SMTPClient({});
|
|
362
|
+
const peers = scriptedRelay(client);
|
|
363
|
+
const message = new Message({ from: 'me', to: 'you', text: 'hi' });
|
|
364
|
+
const onSecond = vi.fn();
|
|
365
|
+
client.send(message, () => {
|
|
366
|
+
if (when === 'from a callback')
|
|
367
|
+
client.send(message, onSecond);
|
|
368
|
+
});
|
|
369
|
+
const [relay] = peers;
|
|
370
|
+
relay.greet();
|
|
371
|
+
await relay.acceptMessage();
|
|
372
|
+
if (when === 'while idle')
|
|
373
|
+
client.send(message, onSecond);
|
|
374
|
+
vi.advanceTimersByTime(1000);
|
|
375
|
+
expect(relay.write).not.toHaveBeenCalledWith('quit\r\n');
|
|
376
|
+
await relay.acceptMessage();
|
|
377
|
+
expect(onSecond).toHaveBeenCalledExactlyOnceWith(null, message);
|
|
378
|
+
vi.advanceTimersByTime(999);
|
|
379
|
+
expect(relay.write).not.toHaveBeenCalledWith('quit\r\n');
|
|
380
|
+
vi.advanceTimersByTime(1);
|
|
381
|
+
expect(relay.write).toHaveBeenLastCalledWith('quit\r\n');
|
|
353
382
|
});
|
|
354
|
-
it('
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
// @ts-expect-error mocking internals
|
|
368
|
-
client._senddone(null, stack);
|
|
369
|
-
};
|
|
370
|
-
client.smtp.state = () => SMTPState.CONNECTED;
|
|
371
|
-
const msg = new Message({
|
|
372
|
-
from: 'me@example.com',
|
|
373
|
-
to: 'you@example.com',
|
|
374
|
-
text: 'hi',
|
|
383
|
+
it.each(['relay reply', 'explicit close', 'relay disconnect'])('resumes queued mail after idle QUIT ends with %s', async (shutdown) => {
|
|
384
|
+
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
|
|
385
|
+
onTestFinished(() => {
|
|
386
|
+
vi.useRealTimers();
|
|
387
|
+
});
|
|
388
|
+
const message = new Message({ from: 'me', to: 'you', text: 'hi' });
|
|
389
|
+
const onQueued = vi.fn();
|
|
390
|
+
const client = new SMTPClient({
|
|
391
|
+
logger(entry) {
|
|
392
|
+
if (shutdown === 'relay disconnect' &&
|
|
393
|
+
entry === 'smtp connection destroyed!')
|
|
394
|
+
client.send(message, onQueued);
|
|
395
|
+
},
|
|
375
396
|
});
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
expect(
|
|
397
|
+
const peers = scriptedRelay(client);
|
|
398
|
+
const delivery = client.sendAsync(message);
|
|
399
|
+
const [relay] = peers;
|
|
400
|
+
relay.greet();
|
|
401
|
+
await relay.acceptMessage();
|
|
402
|
+
await delivery;
|
|
403
|
+
vi.advanceTimersByTime(1000);
|
|
404
|
+
expect(relay.write).toHaveBeenLastCalledWith('quit\r\n');
|
|
405
|
+
if (shutdown !== 'relay disconnect')
|
|
406
|
+
client.send(message, onQueued);
|
|
407
|
+
if (shutdown === 'relay reply')
|
|
408
|
+
relay.reply('221 bye');
|
|
409
|
+
else if (shutdown === 'explicit close')
|
|
410
|
+
client.close();
|
|
411
|
+
else
|
|
412
|
+
relay.disconnect();
|
|
413
|
+
// Don't reconnect inside the old QUIT callback or the close logger.
|
|
414
|
+
expect(peers).toHaveLength(1);
|
|
415
|
+
expect(onQueued).not.toHaveBeenCalled();
|
|
416
|
+
await Promise.resolve();
|
|
417
|
+
expect(peers).toHaveLength(2);
|
|
418
|
+
peers[1].greet();
|
|
419
|
+
await peers[1].acceptMessage();
|
|
420
|
+
expect(onQueued).toHaveBeenCalledExactlyOnceWith(null, message);
|
|
421
|
+
client.close();
|
|
422
|
+
expect(vi.getTimerCount()).toBe(0);
|
|
384
423
|
});
|
|
385
424
|
});
|
|
386
425
|
//# sourceMappingURL=client.test.js.map
|