From 4e5550595627cfd5898842ccdd02cfdaa1b8917d Mon Sep 17 00:00:00 2001 From: Oleg Elifantiev Date: Sun, 13 Oct 2013 23:45:56 +0400 Subject: [PATCH] Docs for size chunker --- README.md | 66 ++++++++++++++++++++++++++++++++++++++++----- lib/size-chunker.js | 6 ++++- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f9b249e..0f611a3 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,10 @@ Simple transform stream which counts lines (`\n` is a separator) and emit data c ### Configuration ```javascript - new LineCounter({ +new LineCounter({ numLines: 1, // number of lines in a single output chunk. 1 is default - flushTail: false // on stream, end flush or not remaining buffer. false is default - }); + flushTail: false // on stream end, flush or not remaining buffer. false is default +}); ``` SeparatorChunker @@ -40,8 +40,60 @@ By default separator sequence is set to `\n`, so it is equals to LineCounter wit ### Configuration ```javascript - new SeparatorChunker({ - separator: '\n', // separator sequence - flushTail: false // on stream, end flush or not remaining buffer. false is default - }); +new SeparatorChunker({ + separator: '\n', // separator sequence + flushTail: false // on stream end, flush or not remaining buffer. false is default +}); +``` + +SizeChunker +----------- + +Split streams into chunks having at least specified size in bytes (but maybe more). It is **object mode** stream! +Each data chunk is an object with the following fields: + + - id: number of chunk (starts from 1) + - data: `Buffer` with data + +SizeChunker has 2 additional events: + + - chunkStart: emitted on each chunk start. + - chunkEnd: emitted on each chunk finish. + +Both event handlers must accept two arguments: + + - id: number of chunk + - done: callback function, **must** be called then processing is completed + +### Configuration + +```javascript +new SizeChunker({ + chunkSize: 1024 // must be a number greater than zero +}); +``` + +### Example +```javascript +var input = fs.createReadStream('./input'), + chunker = new SizeChunker({ + chunkSize: 1024 + }), + output; + +chunker.on('chunkStart', function(id, done) { + output = fs.createWriteStream('./output-' + id); + done(); +}); + +chunker.on('chunkEnd', function(id, done) { + output.end(); + done(); +}); + +chunker.on('data', function(data) { + output.write(data.chunk); +}); + +input.pipe(chunker); ``` \ No newline at end of file diff --git a/lib/size-chunker.js b/lib/size-chunker.js index 7e0379f..c30fdaf 100644 --- a/lib/size-chunker.js +++ b/lib/size-chunker.js @@ -14,7 +14,11 @@ Transform.call(this, options); this._bytesPassed = 0; this._currentChunk = 0; - this._chunkSize = options && options.chunkSize || 10; + this._chunkSize = +(options && options.chunkSize); + + if(isNaN(this._chunkSize) || this._chunkSize <= 0) { + throw new Error("Invalid chunk size. Must be a number greater than zero."); + } this._readableState.objectMode = true;