Skip to content

Commit

Permalink
add client logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-sz committed Nov 22, 2023
1 parent 65c4845 commit 192f770
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class SMTPClient extends EventEmitter {
private maxSize = defaultSize;
private welcomed = false;

constructor(options: SMTPClientOptions) {
constructor(private options: SMTPClientOptions) {
super();

if (options.secure === SMTPClientSecure.TLS) {
Expand Down Expand Up @@ -95,6 +95,10 @@ export class SMTPClient extends EventEmitter {
const lines: string[] = [];
while (true) {
const line = await this.wire.readLine();
if (this.options.logging) {
console.log('[SMTP IN]', line);
}

const code = parseInt(line.split('-')[0].split(' ')[0]);
if (!code || line.length < 4) {
throw new Error('Invalid server response.');
Expand All @@ -109,6 +113,10 @@ export class SMTPClient extends EventEmitter {
}

private async smtpSend(str: string, expectedCode = 250) {
if (this.options.logging) {
console.log('[SMTP OUT]', str);
}

// TODO: use .wait()
this.wire.writeLine(str.split('\r').join('').split('\n').join('\r\n'));
const response = await this.smtpRead();
Expand Down
1 change: 1 addition & 0 deletions src/client/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface SMTPClientOptions {
port: number;
secure?: SMTPClientSecure;
rejectUnauthorized?: boolean;
logging?: boolean;

// TODO: Authentication
}
4 changes: 3 additions & 1 deletion src/server/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ export class SMTPServerConnection extends EventEmitter {
}

reply(code: number, message?: string) {
console.log('[SMTP OUT]', code, message);
if (this.options.logging) {
console.log('[SMTP OUT]', code, message);
}

try {
if (!message) {
Expand Down

0 comments on commit 192f770

Please sign in to comment.