cat-stream

0.1.3 • Public • Published

Cat Stream Build Status

Buffers a readable stream and validates its length. This can be considered a streams2 version of concat-stream with validation, however, it only deals with binary streams - object or string streams are not supported.

You may also be interested in:

API

var cat = require('cat-stream')
var stream = fs.createReadStream('some file.txt')
 
stream.pipe(cat(function (err, buf) {
 
}))

cat([options], [callback])

Creates a duplex stream. You must pipe the readable stream into cat.

The options are:

  • expect (also aliased as expected) - Either an integer in bytes of the expected content length or true. If true, will check for source.headers['content-length']. If the expected length does not match the resulting buffer, it will return a err.key === 'length-mismatch' error.
  • limit - An integer in bytes of the maximum resulting buffer size. If the incoming data exceeds this limit, it will return a err.key === 'length-exceeded' error.

callback(err, buf) is an optional callback. It simplifies the stream API and allows for only one exit. buf is the resulting buffer.

Event: 'readable'

You can retrieve the buffer the streams2 way by listening to the readable event:

stream.pipe(cat()).on('readable', function () {
  var buf = this.read(1)
})

but you should also listen to the error event as well. This is annoying, thus the callback option.

Event: 'progress'

Every time data is collected, a progress event is emitted. To check how many bytes have been received, call this.received. To check the expected length, call this.expected. To check the total progress, calculate this.received / this.expected.

Strings

cat assumes the source stream is a binary stream. cat will never support any other type of stream, so please don't do anything like this:

stream.setEncoding('utf8')
stream.pipe(cat(function (err, buf) {
 
}))

Instead, you should do this:

stream.pipe(cat(function (err, buf) {
  var string = buf.toString('utf8')
}))

If your stream does not emit Buffer instances by default, it is not a well-formed stream. Please properly use a Readable or PassThrough stream.

License

The MIT License (MIT)

Copyright (c) 2013 Jonathan Ong me@jongleberry.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme

Keywords

none

Package Sidebar

Install

npm i cat-stream

Weekly Downloads

3

Version

0.1.3

License

MIT

Last publish

Collaborators

  • jongleberry