Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

smtp-server-as-promised

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

smtp-server-as-promised

Promisify smtp-server module

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
42
decreased by-28.81%
Maintainers
1
Weekly downloads
 
Created
Source

smtp-server-as-promised

Build Status Coverage Status npm

This module provides promisified version of smtp-server module. The API is the same as for smtp-server, except listen method which return Promise object and callback options which are Promise objects.

Additionally, stream argument for onData promise is changed to PromiseReadable object if options.usePromiseReadable is true.

Requirements

This module requires Node >= 4.

Installation

npm install smtp-server-as-promised

Usage

smtp-server-as-promised can be used like standard smtp-server module:

const {SMTPServerAsPromised} = require('smtp-server-as-promised')
constructor
const server = new SMTPServerAsPromised(options)

Create new SMTPServer instance.

Example:

const server = new SMTPServerAsPromised({
  port: 2525,
  usePromiseReadable: true,
  onConnect, onMailFrom, onData, onError
})

Options are the same as for original smtp-server constructor, except that callback handlers are Promise objects or async functions:

onConnect
async function onConnect (session) {
  console.log(`[${session.id}] onConnect`)
}
onAuth
async function onAuth (auth, session) {
  if (auth.method === 'PLAIN' && auth.username === 'username' && auth.password === 'password') {
    return {user: auth.username}
  } else {
    throw new Error('Invalid username or password')
  }
}

This method must return the object with user property.

onMailFrom
async function onMailFrom (from, session) {
  console.log(`[${session.id}] onMailFrom ${from.address}`)
  if (from.address.split('@')[1] === 'spammer.com') {
    throw new Error('we do not like spam!')
  }
}

An errors can be thrown and then are handled by server in response message.

onRcptTo
async function onRcptTo (to, session) {
  console.log(`[${session.id}] onRcptTo ${to.address}`)
  if (from.address.split('@')[1] === 'spammer.com') {
    throw new Error('we do not like spam!')
  }
}
usePromiseReadable
options.usePromiseReadable = true

Callback handler onData provides stream object as an instance of PromiseReadable class if options.usePromiseReadable options is true

onData
const server = new SMTPServerAsPromised({usePromiseReadable: true, onData})

async function onData (stream, session) {
  console.log(`[${session.id}] onData started`)
  session.messageLength = 0

  for (let chunk; (chunk = await stream.read()) !== null;) {
    console.log(`[${session.id}] onData got data chunk ${chunk.length} bytes`)
    session.messageLength += chunk.length
  }

  console.log(`[${session.id}] onData finished after reading ${session.messageLength} bytes`)
}

stream object is a standard stream.Readable object if options.usePromiseReadable is false.

onError
async function onError (e) {
  console.log('Server error:', e)
}
listen
const promise = server.listen(port[,host][,backlog])

Start the server instance. This method returns promise which returns address as its value.

Example

async function main () {
  const address = await server.listen(2525)
  console.log(`Listening on [${address.address}]:${address.port}`)
}
close
const promise = server.close()

Stop the server from accepting new connections.

Example

async function main () {
  // ...
  await server.close()
  console.log(`Server was stopped`)
}

Promise

This module uses any-promise and any ES6 Promise library or polyfill is supported.

Ie. bluebird can be used as Promise library for this module, if it is registered before.

require('any-promise/register/bluebird')
const {SMTPServerAsPromised} = require('smtp-server-as-promised')

License

Copyright (c) 2016-2017 Piotr Roszatycki piotr.roszatycki@gmail.com

MIT

Keywords

FAQs

Package last updated on 10 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc