Files
DefinitelyTyped/types/smtp-server/smtp-server-tests.ts
Piotr Roszatycki 692d55bf9f smtp-server: definitions for v3.3 (#20661)
* Type definitions for smtp-server 3.3

* Older version 1 must have a path mapping for itself

* DATA stream is Readable

* `err` argument for callbacks is optional if there is no other arg
2017-10-23 13:16:36 -07:00

76 lines
2.4 KiB
TypeScript

import { SMTPServer, SMTPServerAddress, SMTPServerAuthentication, SMTPServerAuthenticationResponse, SMTPServerOptions, SMTPServerSession } from 'smtp-server';
import { Readable } from 'stream';
const options: SMTPServerOptions = {
hideSTARTTLS: true,
onConnect,
onMailFrom,
onRcptTo,
onData,
onClose,
port: 2525
};
function onConnect(session: SMTPServerSession, callback: (err?: Error) => void): void {
console.log(`[${session.id}] onConnect`);
callback();
}
function onAuth(auth: SMTPServerAuthentication, session: SMTPServerSession, callback: (err: Error | undefined, response?: SMTPServerAuthenticationResponse) => void): void {
if (auth.method === 'PLAIN' && auth.username === 'username' && auth.password === 'password') {
callback(undefined, { user: auth.username });
} else {
callback(new Error('Invalid username or password'));
}
}
function onMailFrom(from: SMTPServerAddress, session: SMTPServerSession, callback: (err?: Error) => void): void {
console.log(`[${session.id}] onMailFrom ${from.address}`);
if (from.address.split('@')[1] === 'spammer.com') {
// code 421 disconnects SMTP session immediately
callback(Object.assign(new Error('we do not like spam!'), { responseCode: 421 }));
} else {
callback();
}
}
function onRcptTo(to: SMTPServerAddress, session: SMTPServerSession, callback: (err?: Error) => void): void {
console.log(`[${session.id}] onRcptTo ${to.address}`);
callback();
}
function onData(stream: Readable, session: SMTPServerSession, callback: (err?: Error) => void): void {
console.log(`[${session.id}] onData started`);
let messageLength = 0;
stream.on('data', (chunk: Buffer) => {
console.log(`[${session.id}] onData got data chunk ${chunk.length} bytes`);
messageLength += chunk.length;
});
stream.once('end', () => {
console.log(`[${session.id}] onData finished after reading ${messageLength} bytes`);
callback();
});
}
function onClose(session: SMTPServerSession) {
console.log(`[${session.id}] onClose`);
}
const server = new SMTPServer(options);
server.on('error', (err) => {
console.log(`Server got error:`, err);
});
server.on('close', () => {
console.log('Server closed');
});
server.listen(options.port, () => {
const address = server.server.address();
console.log(`Listening on [${address.address}]:${address.port}`);
});