Skip to content

Commit

Permalink
Reject promises if connection is already ended
Browse files Browse the repository at this point in the history
  • Loading branch information
dex4er committed Sep 12, 2018
1 parent 0f2775e commit 5653ca3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## v4.1.0 2018-09-12

* Response message is always printable US-ASCII.
* Reject promises if connection is already ended.
* Newer typings for `nodemailer` are used.

## v4.0.0 2018-09-10
Expand Down
70 changes: 43 additions & 27 deletions src/smtp-connection-as-promised.ts
Expand Up @@ -27,9 +27,13 @@ export class SMTPConnectionAsPromised {

connect (): Promise<void> {
return new Promise((resolve, reject) => {
if (this.ended) {
return reject(new Error('Cannot connect - SMTP connection is already ended.'))
}

const endHandler = () => {
this.connection.removeListener('error', errorHandler)
reject(new Error('Cannot connect - smtp connection is already ended.'))
reject(new Error('Cannot connect - SMTP connection is already ended.'))
}

const errorHandler = (err: SMTPError) => {
Expand Down Expand Up @@ -68,8 +72,12 @@ export class SMTPConnectionAsPromised {

login (auth: SMTPConnectionAuthenticationCredentials | SMTPConnectionAuthenticationOAuth2 | SMTPConnectionCredentials): Promise<void> {
return new Promise((resolve, reject) => {
if (this.ended) {
return reject(new Error('Cannot login - SMTP connection is already ended.'))
}

const endHandler = () => {
reject(new Error('Cannot login - smtp connection is already ended.'))
reject(new Error('Cannot login - SMTP connection is already ended.'))
}

this.connection.once('end', endHandler)
Expand All @@ -84,9 +92,13 @@ export class SMTPConnectionAsPromised {

send (envelope: SMTPConnectionEnvelope, message: string | Buffer | Readable): Promise<SMTPConnectionSentMessageInfo> {
return new Promise((resolve, reject) => {
if (this.ended) {
return reject(new Error('Cannot send - SMTP connection is already ended.'))
}

const endHandler = () => {
this.connection.removeListener('error', errorHandler)
reject(new Error('Cannot send - smtp connection is already ended.'))
reject(new Error('Cannot send - SMTP connection is already ended.'))
}

const errorHandler = (err: SMTPError) => {
Expand All @@ -110,6 +122,34 @@ export class SMTPConnectionAsPromised {
})
}

reset (): Promise<void> {
return new Promise((resolve, reject) => {
if (this.ended) {
return reject(new Error('Cannot reset - SMTP connection is already ended.'))
}

const endHandler = () => {
this.connection.removeListener('error', errorHandler)
reject(new Error('Cannot reset - SMTP connection is already ended.'))
}

const errorHandler = (err: SMTPError) => {
this.connection.removeListener('end', endHandler)
reject(this.printableAsciiError(err))
}

this.connection.once('end', endHandler)
this.connection.once('error', errorHandler)

this.connection.reset((err?: SMTPError | null) => {
this.connection.removeListener('end', endHandler)
this.connection.removeListener('error', errorHandler)
if (err) reject(this.printableAsciiError(err))
else resolve()
})
})
}

quit (): Promise<void> {
return new Promise((resolve) => {
const socket = this.connection._socket
Expand Down Expand Up @@ -144,30 +184,6 @@ export class SMTPConnectionAsPromised {
})
}

reset (): Promise<void> {
return new Promise((resolve, reject) => {
const endHandler = () => {
this.connection.removeListener('error', errorHandler)
reject(new Error('Cannot reset - smtp connection is already ended.'))
}

const errorHandler = (err: SMTPError) => {
this.connection.removeListener('end', endHandler)
reject(this.printableAsciiError(err))
}

this.connection.once('end', endHandler)
this.connection.once('error', errorHandler)

this.connection.reset((err?: SMTPError | null) => {
this.connection.removeListener('end', endHandler)
this.connection.removeListener('error', errorHandler)
if (err) reject(this.printableAsciiError(err))
else resolve()
})
})
}

destroy (): Promise<void> {
const cleanup = () => {
this.ended = true
Expand Down

0 comments on commit 5653ca3

Please sign in to comment.