stream-chunker
Advanced tools
Comparing version 1.0.7 to 1.1.0
@@ -11,3 +11,7 @@ // Create sample input stream with 10 byte chunks | ||
var Chunker = require('../index.js'); | ||
var chunker = Chunker(16, true); // split the stream of data into 4 byte chunks | ||
var opts = { | ||
flush: true, | ||
encoding: 'utf8' | ||
}; | ||
var chunker = Chunker(16, opts); // split the stream of data into 4 byte chunks | ||
// make sure to add any data event listeners to chunker stream | ||
@@ -18,4 +22,4 @@ // before you write any data to it | ||
// notice the last chunk is the flushed data | ||
console.log('Chunk: ' + data.toString('utf8')); | ||
console.log('Chunk: ' + data; | ||
}); | ||
sampleStream.pipe(chunker); // write some data to chunker to get chunked | ||
sampleStream.pipe(chunker); // write some data to chunker to get chunked |
11
index.js
@@ -16,4 +16,8 @@ /** | ||
*/ | ||
module.exports = function (chunkSize, flush) { | ||
module.exports = function (chunkSize, opts) { | ||
if (!opts) opts = {}; | ||
var flush = opts.flush; | ||
var encoding = opts.encoding; | ||
// buffer to store the last few bytes of incoming data | ||
@@ -23,3 +27,4 @@ // if it does not divide evenly into chunkSize | ||
var opts = { | ||
var transformOpts = { | ||
encoding: encoding, | ||
halfOpen: false, | ||
@@ -50,4 +55,4 @@ objectMode: false | ||
return through2(opts, transformFunction, flushFunction); | ||
return through2(transformOpts, transformFunction, flushFunction); | ||
}; |
{ | ||
"name": "stream-chunker", | ||
"version": "1.0.7", | ||
"version": "1.1.0", | ||
"description": "A transform stream which chunks incoming data into chunkSize byte chunks", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -11,3 +11,3 @@ # stream-chunker | ||
#### `var chunker = require('stream-chunker')(chunkSize, [flush])` | ||
#### `var chunker = require('stream-chunker')(chunkSize, [opts])` | ||
Returns a new chunker. Chunker is a duplex (tansform) stream. You can write data into the | ||
@@ -19,3 +19,5 @@ chunker, and regardless of the incoming data, the readable side will emit data | ||
- `chunkSize`: `integer` - Size in bytes of the desired chunks. | ||
- `flush`: `boolean` - Optional. Flush incomplete chunk data on stream end. Default is `false`. | ||
- opts | ||
- `flush`: `boolean` - Optional. Flush incomplete chunk data on stream end. Default is `false`. | ||
- `encoding`: `string` - Optional. Encoding of String chunks. Must be a valid Buffer encoding, such as `utf8` or `ascii`. | ||
@@ -44,3 +46,7 @@ ## Simple example | ||
var Chunker = require('stream-chunker'); | ||
var chunker = Chunker(16, true); // split the stream of data into 4 byte chunks | ||
var opts = { | ||
flush: true, | ||
encoding: 'utf8' | ||
}; | ||
var chunker = Chunker(16, opts); // split the stream of data into 4 byte chunks | ||
// make sure to add any data event listeners to chunker stream | ||
@@ -51,5 +57,6 @@ // before you write any data to it | ||
// notice the last chunk is the flushed data | ||
console.log('Chunk: ' + data.toString('utf8')); | ||
console.log('Chunk: ' + data; | ||
}); | ||
sampleStream.pipe(chunker); // write some data to chunker to get chunked | ||
``` | ||
@@ -56,0 +63,0 @@ |
@@ -8,7 +8,12 @@ var Chunker = require('../index.js'); | ||
t.plan(2); | ||
var opts = { | ||
flush: false, | ||
encoding: 'utf8' | ||
} | ||
function check(data) { | ||
t.equals(data.toString('utf8'), '1234', 'Received only full chunks'); | ||
t.equals(data, '1234', 'Received only full chunks'); | ||
} | ||
var chunker = Chunker(4); | ||
var chunker = Chunker(4, opts); | ||
var concatStream = concat(check); | ||
@@ -21,7 +26,11 @@ chunker.pipe(concatStream); | ||
var optsFlush = { | ||
flush: true, | ||
encoding: 'utf8' | ||
} | ||
function checkFlush(data) { | ||
t.equals(data.toString('utf8'), '123456', 'Received flush data'); | ||
t.equals(data, '123456', 'Received flush data'); | ||
} | ||
var chunkerFlush = Chunker(4, true); | ||
var chunkerFlush = Chunker(4, optsFlush); | ||
var concatStreamFlush = concat(checkFlush); | ||
@@ -28,0 +37,0 @@ chunkerFlush.pipe(concatStreamFlush); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8364
131
62