Buffer and validate a readable stream.
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:
var cat = require'cat-stream'var stream = fscreateReadStream'some file.txt'streampipecat
Creates a duplex stream.
You must pipe the readable stream into
The options are:
expect(also aliased as
expected) - Either an integer in bytes of the expected content length or
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.
You can retrieve the buffer the streams2 way by listening to the readable event:
streampipecaton'readable'var buf = thisread1
but you should also listen to the
error event as well.
This is annoying, thus the
Every time data is collected, a
progress event is emitted.
To check how many bytes have been received, call
To check the expected length, call
To check the total progress, calculate
this.received / this.expected.
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:
Instead, you should do this:
streampipecatvar string = buftoString'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.
The MIT License (MIT)
Copyright (c) 2013 Jonathan Ong email@example.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.