streamwire
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

streamwire

workflow npm npm NPM

streamwire is a node.js package designed to make working with text-based protocols (e.g. SMTP, IMAP) easier.

This works for any Stream that is both Readable and Writable (Duplex), for example: net.Socket.

Installation

streamwire is available on npm, you can install it with either npm or yarn:

npm install streamwire
# or:
yarn install streamwire

Example

Simple SMTP client implementation:

import { Wire } from 'streamwire';
import { connect } from 'net';
const socket = connect({ host: 'example.com', port: 25 });

async function smtpRead(wire: Wire): Promise<[number, string[]]> {
  const lines: string[] = [];

  while (true) {
    const line = await wire.readLine();

    const code = parseInt(line.split('-')[0].split(' ')[0]);
    if (!code || line.length < 4) {
      throw new Error('Invalid server response.');
    }
    const ended = line.charAt(3) !== '-';
    lines.push(line.substring(4));

    if (ended) {
      return [code, lines];
    }
  }
}

async function smtpCommand(
  wire: Wire,
  command: string,
  expectedCode = 250
): Promise<[number, string[]]> {
  wire.writeLine(`${command}\r\n`);
  const response = await smtpRead(wire);
  if (response[0] !== expectedCode) {
    throw new Error('Unexpected response.');
  }

  return response;
}

socket.on('connect', async () => {
  const wire = new Wire(socket);
  const welcome = await smtpRead(wire);
  if (welcome[0] !== 220) {
    throw new Error();
  }

  await smtpCommand(wire, 'EHLO');
  await smtpCommand(wire, 'MAIL FROM:<a@example.com>');
  await smtpCommand(wire, 'RCPT TO:<b@example.com>');
  await smtpCommand(wire, 'DATA', 354);
  await smtpCommand(wire, 'E-mail contents.\r\n.');

  // Success!
});

Package Sidebar

Install

npm i streamwire

Weekly Downloads

2

Version

1.0.2

License

BSD-3-Clause-Clear

Unpacked Size

38.4 kB

Total Files

10

Last publish

Collaborators

  • mat-sz