Skip to content

Commit 54368d7

Browse files
committed
Fix for corner-case in which no response was returned if the socket was immediately closed
1 parent 5aef431 commit 54368d7

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/smtp/smtp.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ export const checkSMTP = async (
1515
): Promise<OutputFormat> => {
1616
const timeout = 1000 * 10 // 10 seconds
1717
return new Promise(r => {
18+
let receivedData: boolean = false;
1819
const socket = net.createConnection(25, exchange)
1920
socket.setEncoding('ascii')
2021
socket.setTimeout(timeout)
2122
socket.on('error', error => {
2223
log('error', error)
2324
socket.emit('fail', error)
2425
})
25-
26+
socket.on('close', hadError => {
27+
if (!receivedData && !hadError) {
28+
socket.emit('fail', 'Mail server closed connection without sending any data.')
29+
}
30+
})
2631
socket.on('fail', msg => {
2732
r(createOutput('smtp', msg))
2833
if (socket.writable && !socket.destroyed) {
@@ -65,6 +70,7 @@ export const checkSMTP = async (
6570

6671
socket.on('connect', () => {
6772
socket.on('data', msg => {
73+
receivedData = true;
6874
log('data', msg)
6975
if (hasCode(msg, 220) || hasCode(msg, 250)) {
7076
socket.emit('next', msg)

test/__snapshots__/index.test.ts.snap

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,31 @@ Object {
100100
}
101101
`;
102102

103+
exports[`validation tests fails with bad mailbox 3`] = `
104+
Object {
105+
"reason": "smtp",
106+
"valid": false,
107+
"validators": Object {
108+
"disposable": Object {
109+
"valid": true,
110+
},
111+
"mx": Object {
112+
"valid": true,
113+
},
114+
"regex": Object {
115+
"valid": true,
116+
},
117+
"smtp": Object {
118+
"reason": "Mailbox not found.",
119+
"valid": false,
120+
},
121+
"typo": Object {
122+
"valid": true,
123+
},
124+
},
125+
}
126+
`;
127+
103128
exports[`validation tests fails with bad regex 1`] = `
104129
Object {
105130
"reason": "regex",

test/index.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import { validate } from '../src/index'
55
const elevenSeconds = 11 * 1000
66

77
describe('validation tests', () => {
8+
it('fails without sending data', async () => {
9+
const res = await validate({
10+
11+
sender: '[email protected]',
12+
validateTypo: false,
13+
validateDisposable: false,
14+
})
15+
console.log(res)
16+
}, 30000)
817
it('fails with bad regex', async () => {
918
const res = await validate('david.gmail.com')
1019
expect(res.valid).toBe(false)

0 commit comments

Comments
 (0)