cat-stream

Buffer and validate a readable stream.

Cat Stream

Buffers a readable stream and validates its length. This can be considered a streams2 version of concat-stream with validation.

You may also be interested in:

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

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

The options are:

  • expect (also aliased as expected and expectedLength) - 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.
  • passError - will pass any errors from the source stream to cat. Not necessary if you use domains and don't care to continue logic.
  • passClose - will pass any close events from the source stream to cat. Not necessary if you don't care to continue logic.

callback(err, buf) is an optional callback. It simplifies the stream API and allows for only one exit. buf is the resulting buffer, but if cat was destroyed, it will be undefined. Don't set passClose: true and never cat().destroy() if you don't want to bother to check if (!buf).

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.

By default, 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 (errbuf) {
 
}))

Instead, you should do this:

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

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.