npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

smtp-stack

v0.1.2

Published

Configurable SMTP stack and server with async middleware

Downloads

1

Readme

smtp-stack

Configurable SMTP stack and server with async middleware.

Supports TLS and the following SMTP extensions:

  • AUTH PLAIN LOGIN XOAUTH2 CRAM-MD5
  • XCLIENT
  • XFORWARD
  • PROXY

Installation

npm install smtp-stack

Usage and Options

    const fs = require('fs');
    const { SmtpServer, LogLevels } = require('smtp-stack');

    const server = new SmtpServer({
        // server name
        name: 'mail.example.com',
    
        // security options
        secure: true, // TLS or not
        authMethods: ['PLAIN', 'LOGIN', 'XOAUTH2', 'CRAM-MD5'],
        disabledCommands: ['STARTTLS'],
        allowInsecureAuth: false,
        noAuthReply: true, // delegate final auth reply to middleware

        // TLS certificate
        key: fs.readFileSync('key.pem').toString(),
        cert: fs.readFileSync('cert.pem').toString(),

        // TLS connection upgrade options (see tls.TLSSocket)
        session: undefined,
        requestCert: false,
        rejectUnauthorized: true,
        requestOCSP: false,

        // SNI TLS extension options (see tls.createSecureContext)
        sessionIdContext: Math.random().toString().substring(2),
        honorCipherOrder: true,
        minVersion: 'TLSv1',
        sniOptions: {
            'mail.example.com': {
                // custom TLS options
                honorCipherOrder: true,
            }
        },
        SNICallback(hostname, callback) {
            callback(null, server.secureContext.get(hostname));
        },

        // timeouts
        socketTimeout: 5 * 1000, // connection idle timeout
        closeTimeout: 10 * 1000, // connections close grace period
        upgradeTimeout: 2 * 1000, // connection TLS upgrade timeout

        // logger configuration
        logLevel: LogLevels.info,
        logBackend: console,

        // max message body size
        size: 1024,

        // enable proxy
        useProxy: true,

        // override default hooks triggered before middleware
        // return error as first argument of callback to cancel
        // operation and close client connection
        onSocket(socket, callback) {
            setImmediate(callback);
        },
        onConnect(session, callback) {
            setImmediate(callback);
        },
        onAuth(session, callback) {
            setImmediate(() => callback(null, { user: 'anonymous' }));
        },
        onMailFrom(session, callback) {
            setImmediate(callback);
        },
        onRcptTo(session, callback) {
            setImmediate(callback);
        },
        onData(session, callback) {
            setImmediate(callback);
        },
        onClose(connection, callback) {
            setImmediate(callback);
        }
    });

    server.on('error', () => {
        // handle error
    });

    // handle AUTH command
    server.use(async (ctx, next) => {

        await next();

        if (ctx.session.auth) {
            let valid = true;

            // todo: validate credentials:
            // - ctx.session.auth.method
            // - ctx.session.auth.username
            // - ctx.session.auth.password

            if (valid) {
                // set user principal
                ctx.session.user = {
                    name: 'John Smith',
                };

                // send final reply
                ctx.send(235, 'Authentication successful');
            } else {
                ctx.send(535, 'Authentication failed');
                ctx.close(); // close connection
            }

            ctx.session.auth = null;
        }
    });

    // handle MAIL FROM/RCPT TO commands
    server.use(async (ctx, next) => {
        if (ctx.commandName === 'MAIL') {
            // todo: parse, validate, alter
            // - ctx.session.envelope.mailFrom
        }

        if (ctx.commandName === 'RCPT') {
            // todo: parse, validate, alter
            // - ctx.session.envelope.rcptTo.forEach
            // - ctx.session.envelope.lastTo
        }

        await next();
    });

    // handle complete MAIL message
    server.use(async (ctx, next) => {
        
        await next();

        if (ctx.commandName === 'DATA' && ctx.session.envelope.dataStream) {
            // copy mail message
            const message = Object.assign({}, ctx.session.envelope);

            // read message body
            const chunks = [];

            ctx.session.envelope.dataStream.on('data', (chunk) => chunks.push(chunk));
            ctx.session.envelope.dataStream.once('end', () => {
                // todo: process the message
                message.body = Buffer.concat(chunks).toString();
            });
        }
    });

    server.start(25, 'localhost', () => {
        console.log('SMTP server started');
    });

Testing

Tested with the following SMTP clients:

  • nodemailer client NPM package (integrations tests in test directory)
  • ssmtp CLI SMTP client (see the instructions)
  • Mozilla Thunderbird (make sure you have valid certificate for testing w/ TLS)