Socket
Socket
Sign inDemoInstall

then-read-stream

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

then-read-stream

Read from a readable stream just like a file


Version published
Weekly downloads
7.5K
increased by12.41%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status NPM version npm downloads Dependencies Coverage Status NSP Status A promise based asynchronous stream reader, which makes reading from a stream easy.

Allows to read from a Readable Stream similar as you would read from a file.

Usage

The then-read-stream contains one class: StreamReader. The constructor of the StreamReader if provided with the stream.Readable you want to read from.

Examles:

In the following example we read the first 16 bytes from a stream and store them in our buffer.

var stream_reader = require("then-read-stream");

var readThisStream = ... // Some stream of type stream.Readable
var streamReader = new stream_reader.StreamReader(readThisStream);

var buffer = new Buffer(16);

return streamReader.read(buf, 0, 16)
  .then( function(bytesRead) {
    // If all went well, buf contains the promised 16 bytes of data read
  })
  .catch( function(err) {
    if(err === stream_reader.StreamReader.EndOfStream) {
      // Rejected, end of the stream has been reached
    }
  })

With peek you can read ahead:

return streamReader.peek(buffer, 0, 1)
  .then( function(bytesRead) {
    if(bytesRead !== 2 || buffer[0] !== 0xFF){
      throw new Error('Stream should start with 0xFF');
    }
    // Read 16 bytes, start at the same offset as peek, so the first byte will be 0xFF
    return streamReader.peek(buffer, 0, 16);
  })

If you have to skip a part of the data, you can use ignore:

return streamReader.ignore(16)
  .then( function(bytesIgnored) {
    if(bytesIgnored<16){
      console.log('Remaing stream length was %s, expected 16', bytesIgnored)
    }
  })
TypeScript:

TypeScript definitions are build in. No need to install additional modules.

import {StreamReader} from "then-read-stream";

const readThisStream = ... // Some stream of type stream.Readable
const streamReader = new StreamReader(readThisStream);

const buf = new Buffer(16);
  
return streamReader.read(buf, 0, 16).then((bytesRead) => {
    // If all went well, buf contains the promised 16 bytes of data read
  }).catch((err) => {
    if(err === StreamReader.EndOfStream) {
      // Rejected, end of the stream has been reached
    }
  })

Keywords

FAQs

Package last updated on 25 Apr 2018

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