stream-chunker
Advanced tools
Comparing version 0.0.5 to 1.0.0
58
index.js
@@ -0,8 +1,47 @@ | ||
/** | ||
* @module stream-chunker | ||
*/ | ||
var through2 = require('through2'); | ||
module.exports = function (chunkSize) { | ||
/** | ||
* Returns a transform stream which chunks incoming data into chunkSize byte | ||
* chunks. | ||
* @param {integer} chunkSize Size of chunks in bytes | ||
* @param {boolean} [flush] Flush any remaining data that does not make | ||
* a full chunk when stream is ended. | ||
* @return {Stream.Transform} A transform stream | ||
* | ||
* @example | ||
* // Create sample input stream with 10 byte chunks | ||
* var Lorem = require('loremipstream'); | ||
* var sampleStream = new Lorem({ | ||
* size: 1000, | ||
* dataSize: 10, | ||
* dataInteval: 100 | ||
* }); | ||
* | ||
* // Create stream chunker with 4 byte chunks | ||
* var CHUNK_SIZE = 4; | ||
* Chunker = require('../index.js'); | ||
* var chunker = Chunker(CHUNK_SIZE) // split the stream of data into 4 byte chunks | ||
* // make sure to add any data event listeners to chunker stream | ||
* // before you write any data to it | ||
* chunker.on('data', function(data) { | ||
* // do something with a 16 byte chunk of data | ||
* console.log('Handle '+CHUNK_SIZE+'bytes at a time: ' + data.toString('utf8')); | ||
* }); | ||
* sampleStream.pipe(chunker); // write some data to chunker to get chunked | ||
*/ | ||
module.exports = function (chunkSize, flush) { | ||
// buffer to store the last few bytes of incoming data | ||
// if it does not divide evenly into chunkSize | ||
var buffer = new Buffer(0); | ||
return through2(function (data, enc, next) { | ||
var opts = { | ||
halfOpen: false, | ||
objectMode: false | ||
}; | ||
var transformFunction = function (data, enc, next) { | ||
var allData = Buffer.concat([buffer, data]); | ||
@@ -18,3 +57,14 @@ var totalLength = allData.length; | ||
next(); | ||
}); | ||
} | ||
}; | ||
var flushFunction; | ||
if (flush) { | ||
flushFunction = function (next) { | ||
this.push(buffer); | ||
next(); | ||
}; | ||
} | ||
return through2(opts, transformFunction, flushFunction); | ||
}; |
{ | ||
"name": "stream-chunker", | ||
"version": "0.0.5", | ||
"version": "1.0.0", | ||
"description": "A transform stream which chunks incoming data into chunkSize byte chunks", | ||
@@ -26,5 +26,6 @@ "main": "index.js", | ||
"devDependencies": { | ||
"tape": "~2.12.3", | ||
"chunky": "~0.0.0", | ||
"loremipstream": "latest" | ||
"concat-stream": "^1.5.0", | ||
"loremipstream": "~1.0.3 ", | ||
"tape": "~2.12.3" | ||
}, | ||
@@ -31,0 +32,0 @@ "repository": { |
@@ -11,3 +11,3 @@ # stream-chunker | ||
### `var chunker = require('stream-chunker')(chunkSize)` | ||
### `var chunker = require('stream-chunker')(chunkSize, [flush])` | ||
Returns a new chunker. Chunker is a duplex (tansform) stream. You can write data into the | ||
@@ -17,2 +17,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 inclomplete chunk data on stream end. Default is `false`. | ||
## An example | ||
@@ -19,0 +22,0 @@ ```javascript |
@@ -0,3 +1,3 @@ | ||
var Chunker = require('../index.js'); | ||
var test = require('tape'); | ||
var Chunker = require('../index.js'); | ||
var chunky = require('chunky'); | ||
@@ -19,3 +19,3 @@ | ||
chunker = Chunker(16); | ||
var chunker = Chunker(16); | ||
@@ -22,0 +22,0 @@ var k=0; |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8264
9
133
0
44
4