stream-chunker
Advanced tools
Comparing version 0.0.1 to 0.0.2
20
index.js
var through2 = require('through2'); | ||
module.exports = ByteBatcher; | ||
function ByteBatcher (size) { | ||
module.exports = function (chunkSize) { | ||
// buffer to store the last few bytes of incoming data | ||
// if it does not divide evenly into size | ||
// if it does not divide evenly into chunkSize | ||
var buffer = new Buffer(0); | ||
return through2(function (data, enc, next) { | ||
data = Buffer.concat([buffer, data]); | ||
var length = data.length; | ||
var remainder = length%size; | ||
var cutoff = length - remainder; | ||
for (var i=0 ; i<cutoff ; i+= size) { | ||
var chunk = data.slice(i, i+size); | ||
var allData = Buffer.concat([buffer, data]); | ||
var totalLength = allData.length; | ||
var remainder = totalLength % chunkSize; | ||
var cutoff = totalLength - remainder; | ||
for (var i=0 ; i<cutoff ; i+=chunkSize) { | ||
var chunk = allData.slice(i, i+chunkSize); | ||
this.push(chunk); | ||
} | ||
buffer = data.slice(cutoff, length); | ||
buffer = allData.slice(cutoff, totalLength); | ||
next(); | ||
}); | ||
} |
{ | ||
"name": "stream-chunker", | ||
"version": "0.0.1", | ||
"description": "Splits a stream into chunks of a certain size.", | ||
"version": "0.0.2", | ||
"description": "A transform stream which chunks incoming data into chunkSize byte chunks", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "tape test/*" | ||
}, | ||
@@ -16,3 +16,4 @@ "keywords": [ | ||
"byte", | ||
"buffer" | ||
"buffer", | ||
"binary" | ||
], | ||
@@ -22,16 +23,17 @@ "author": "Kristian Lyngbaek", | ||
"dependencies": { | ||
"through2": "~0.4.1" | ||
"through2": "~2.0.0" | ||
}, | ||
"devDependencies": { | ||
"tape": "~2.12.3", | ||
"chunky": "~0.0.0" | ||
"chunky": "~0.0.0", | ||
"loremipstream": "latest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:klyngbaek/node-stream-chunker.git" | ||
"url": "git@github.com:klyngbaek/stream-chunker.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/klyngbaek/node-stream-chunker/issues" | ||
"url": "https://github.com/klyngbaek/stream-chunker/issues" | ||
}, | ||
"homepage": "https://github.com/klyngbaek/node-stream-chunker" | ||
"homepage": "https://github.com/klyngbaek/stream-chunker" | ||
} |
@@ -1,9 +0,40 @@ | ||
# An example | ||
# stream-chunker | ||
A transform stream which chunks incoming data into `chunkSize` byte chunks. | ||
Chunker = require('stream-chunker'); | ||
var chunker = Chunker(16) // split the stream of data into 16 byte chunks | ||
// make sure to add the event listeners/pipes before writing data | ||
chunker.on('data', function(data) { | ||
// do something with a 16 byte chunk of data | ||
}); | ||
someStream.pipe(chunker); // pipe some data to chunker to get chunked | ||
[![build status](https://secure.travis-ci.org/klyngbaek/stream-chunker.png)](http://travis-ci.org/klyngbaek/stream-chunker) | ||
[![NPM](https://nodei.co/npm/stream-chunker.png)](https://nodei.co/npm/stream-chunker/) | ||
## api | ||
### `var chunker = require('stream-chunker')(chunkSize)` | ||
Returns a new chunker. Chunker is a duplex (tansform) stream. You can write data into the | ||
chunker, and regardless of the incoming data, the readable side will emit data | ||
in `chunkSize` byte chunks. | ||
## An example | ||
```javascript | ||
// 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('stream-chunker'); | ||
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 | ||
``` | ||
##License | ||
MIT |
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
5776
8
64
41
3
+ Addedisarray@1.0.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedthrough2@2.0.5(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedisarray@0.0.1(transitive)
- Removedobject-keys@0.4.0(transitive)
- Removedreadable-stream@1.0.34(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedthrough2@0.4.2(transitive)
- Removedxtend@2.1.2(transitive)
Updatedthrough2@~2.0.0