Socket
Socket
Sign inDemoInstall

until-stream

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

until-stream

A PassThrough stream that stops piping when a pattern is reached


Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

until-stream Build Status

Ever wanted to pause a stream when a certain String or a binary signature is reached? UntilStream is the answer. Pipe UntilStream and automatically stop when your pattern is reached or call read() until the returned data matches your pattern, without excessively buffering your stream's content in memory.

--------------------------------------
|Stability - API is somewhat unstable|
--------------------------------------

read() and pipe() are implemented with some limitations. For example, UntilStream supports piping to only a single destination stream.

Installation

$ npm install until-stream

Quick Examples

Pipe

var UntilStream = require('until-stream');
var streamBuffers = require("stream-buffers");

var us = new UntilStream({ pattern: 'World'});

var sourceStream = new streamBuffers.ReadableStreamBuffer();
sourceStream.put("Hello World");
var writableStream = new streamBuffers.WritableStreamBuffer();

sourceStream.pipe(us).pipe(writableStream);

writableStream.once('close', function () {
  //writeableStream contains all data before the pattern occurs
  var str = writableStream.getContentsAsString('utf8'); // 'Hello '
  //Now the next call to read() returns the pattern
  var data = us.read(); // 'World'
});

Read

var UntilStream = require('until-stream');
var streamBuffers = require("stream-buffers");

var us = new UntilStream({ pattern: 'jumps'});

var sourceStream = new streamBuffers.ReadableStreamBuffer({ chunkSize: 8 });
sourceStream.put("The quick brown fox jumps over the lazy dog");

sourceStream.pipe(us);

us.on('readable', function() {
  if (us.read() === 'jumps') {
    console.log('Pattern reached!');
  }
});

API Index

UntilStream

UntilStream also includes stream.Readable and stream.Writable methods. See the node v0.9 [Stream documentation] (http://nodejs.org/docs/v0.9.10/api/stream.html) for more.

API Documentation

## UntilStream ### new UntilStream([options])

Arguments

  • options (optional)
    • pattern - String or Buffer If provided, UntilStream will stop reads or pipes when reached
### us.read([size])

Synchronously consume data from UntilStream's internal buffer. If the specified pattern is detected within the current chunk, slice off the portion prior to the pattern. The next call to read() will return exactly the pattern. Otherwise return the current chunk.

Arguments

  • size (optional) - Mininum number of bytes to read. If not specified return the entire content of the internal buffer or up to the pattern

Return

Buffer | null

Example

var us = new UntilStream({ pattern: '\n' });

us.write("Hello\nWorld");
var hello = us.read();
console.log(hello.toString('utf8'));
us.read(); //matches '\n' pattern!
var world = us.read();
console.log(world.toString('utf8'));
### us.pipe(destination, [options])

Pipe incoming data from UntilStream to the destination WriteStream. If the pattern is reached, leave the pattern on the internal buffer, disconnect the pipe, and call end() on the destination. Back-pressure is properly managed.

Arguments

  • destination - The Stream to pipe data to
  • options (optional)
    • end Boolean Default=false

Return

Stream - the destination stream

Example

var us = new UntilStream({ pattern: '\n' });
var loremIpsumStream = fs.createReadStream('loremIpsum.txt');
var outputStream = fs.createWriteStream(path.join(__dirname, 'loremIpsum.out'));

loremIpsumStream.pipe(us).pipe(outputStream).on('close', function() {
  console.log('single line of Lorem Ipsum written to disk');
});
### us.reconfigure([options])

Reconfigure the pattern option. It's unwise to call this method while piping to a destination stream.

Arguments

  • options (optional)
    • pattern - String or Buffer If provided, UntilStream will stop reads or pipes when reached

Example

var us = new UntilStream();

us.write("Hello\nWorld");
us.reconfigure({ pattern: '\n' });
var hello = us.read();
console.log(hello.toString('utf8'));
us.read(); //matches '\n' pattern!
var world = us.read();
console.log(world.toString('utf8'));

License

MIT

Keywords

FAQs

Package last updated on 17 Mar 2013

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