Socket
Socket
Sign inDemoInstall

stream-chunker

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stream-chunker - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

.npmignore

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
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc