@typescript-plus/stream-capture
TypeScript icon, indicating that this package has built-in type declarations

0.4.1 • Public • Published

Stream Capture

A TypeScript library for capturing streams.

CircleCI

Install

$ npm i @typescript-plus/stream-capture --save

Usage

Sync

import { capture } from '@typescript-plus/stream-capture';
import * as fs from 'fs';

const stream = fs.createWriteStream('file');
const captured = capture(stream, (buffer) => {
  stream.write(':)');
  return buffer.join('');
});
captured === ':)'; // true
stream.end(':(', () => {
  fs.readFileSync('file').toString() === ':('; // true
});

Async

import { capture } from '@typescript-plus/stream-capture';
import * as fs from 'fs';

async function asyncFunc() {
  /* ... */
}

(async () => {
  const stream = fs.createWriteStream('file');
  const captured = await capture(stream, async (buffer) => {
    await asyncFunc();
    stream.write(':D');
    return buffer.join('');
  });
  captured === ':D'; // true
  stream.end(':P', () => {
    fs.readFileSync('file').toString() === ':P'; // true
  });
})();

Capturing stdout

Stream Capture accepts stream.Writable objects. So you can capture stdout. It's useful for CLI testing, for example:

import { capture } from '@typescript-plus/stream-capture';

function hello() {
  console.log('Hello :) World');
}

it('works', () => {
  const captured = capture(process.stdout, (buffer) => {
    hello();
    return buffer.join('');
  });
  expect(captured).toEqual('Hello :) World\n'); // success
});

Readme

Keywords

none

Package Sidebar

Install

npm i @typescript-plus/stream-capture

Weekly Downloads

0

Version

0.4.1

License

MIT

Unpacked Size

7.51 kB

Total Files

6

Last publish

Collaborators

  • mosop