From 3c1681c0fbcc4c3fb441a4f207c311e86694e577 Mon Sep 17 00:00:00 2001 From: Zack Schuster Date: Fri, 1 May 2020 13:30:22 -0700 Subject: [PATCH] smtp: stricten property access modifiers --- smtp/client.ts | 11 ++++++----- smtp/message.ts | 10 +++++----- smtp/response.ts | 2 +- smtp/smtp.ts | 35 ++++++++++++++++++----------------- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/smtp/client.ts b/smtp/client.ts index f4f1b55..c882062 100644 --- a/smtp/client.ts +++ b/smtp/client.ts @@ -17,11 +17,12 @@ export interface MessageStack { } export class Client { - public smtp: SMTPConnection; - public queue: MessageStack[] = []; - public timer: NodeJS.Timer | null = null; - public sending = false; - public ready = false; + public readonly smtp: SMTPConnection; + public readonly queue: MessageStack[] = []; + + protected sending = false; + protected ready = false; + protected timer: NodeJS.Timer | null = null; /** * @param {SMTPConnectionOptions} server smtp options diff --git a/smtp/message.ts b/smtp/message.ts index fca6b34..eebfc4d 100644 --- a/smtp/message.ts +++ b/smtp/message.ts @@ -93,16 +93,16 @@ function convertDashDelimitedTextToSnakeCase(text: string) { } export class Message { - attachments: any[] = []; - alternative: AlternateMessageAttachment | null = null; - header: Partial = { + public readonly attachments: any[] = []; + public readonly header: Partial = { 'message-id': `<${new Date().getTime()}.${counter++}.${ process.pid }@${hostname()}>`, date: getRFC2822Date(), }; - content = 'text/plain; charset=utf-8'; - text?: string; + public readonly content = 'text/plain; charset=utf-8'; + public readonly text?: string; + public alternative: AlternateMessageAttachment | null = null; constructor(headers: Partial) { for (const header in headers) { diff --git a/smtp/response.ts b/smtp/response.ts index 5d8b097..5a00a1e 100644 --- a/smtp/response.ts +++ b/smtp/response.ts @@ -3,7 +3,7 @@ import type { Socket } from 'net'; // eslint-disable-line no-unused-vars import type { TLSSocket } from 'tls'; // eslint-disable-line no-unused-vars export class SMTPResponse { - public stop: (err?: Error) => void; + public readonly stop: (err?: Error) => void; constructor( stream: Socket | TLSSocket, diff --git a/smtp/smtp.ts b/smtp/smtp.ts index a1b5fc3..33c9b47 100644 --- a/smtp/smtp.ts +++ b/smtp/smtp.ts @@ -91,24 +91,30 @@ export interface ConnectOptions { } export class SMTPConnection extends EventEmitter { - private _state: 0 | 1 | 2 = SMTPState.NOTCONNECTED; - private _secure = false; + public readonly user: () => string; + public readonly password: () => string; + public readonly timeout: number = DEFAULT_TIMEOUT; + + protected readonly log = log; + protected readonly authentication: (keyof typeof AUTH_METHODS)[] = [ + AUTH_METHODS['CRAM-MD5'], + AUTH_METHODS.LOGIN, + AUTH_METHODS.PLAIN, + AUTH_METHODS.XOAUTH2, + ]; + + protected _state: 0 | 1 | 2 = SMTPState.NOTCONNECTED; + protected _secure = false; + protected loggedin = false; protected sock: Socket | TLSSocket | null = null; protected features: Indexed | null = null; protected monitor: SMTPResponse | null = null; - protected authentication: (keyof typeof AUTH_METHODS)[]; protected domain = hostname(); protected host = 'localhost'; protected ssl: boolean | SMTPSocketOptions = false; protected tls: boolean | SMTPSocketOptions = false; protected port: number; - protected loggedin = false; - protected log = log; - - public user: () => string; - public password: () => string; - public timeout: number = DEFAULT_TIMEOUT; /** * SMTP class written using python's (2.7) smtplib.py as a base @@ -127,14 +133,9 @@ export class SMTPConnection extends EventEmitter { }: Partial = {}) { super(); - this.authentication = Array.isArray(authentication) - ? authentication - : [ - AUTH_METHODS['CRAM-MD5'], - AUTH_METHODS.LOGIN, - AUTH_METHODS.PLAIN, - AUTH_METHODS.XOAUTH2, - ]; + if (Array.isArray(authentication)) { + this.authentication = authentication; + } if (typeof timeout === 'number') { this.timeout = timeout;