aboutsummaryrefslogtreecommitdiff
path: root/node_modules/first-chunk-stream
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/first-chunk-stream')
-rw-r--r--node_modules/first-chunk-stream/index.js93
-rw-r--r--node_modules/first-chunk-stream/package.json36
-rw-r--r--node_modules/first-chunk-stream/readme.md62
3 files changed, 191 insertions, 0 deletions
diff --git a/node_modules/first-chunk-stream/index.js b/node_modules/first-chunk-stream/index.js
new file mode 100644
index 000000000..24815508a
--- /dev/null
+++ b/node_modules/first-chunk-stream/index.js
@@ -0,0 +1,93 @@
+'use strict';
+var util = require('util');
+var Transform = require('stream').Transform;
+
+function ctor(options, transform) {
+ util.inherits(FirstChunk, Transform);
+
+ if (typeof options === 'function') {
+ transform = options;
+ options = {};
+ }
+
+ if (typeof transform !== 'function') {
+ throw new Error('transform function required');
+ }
+
+ function FirstChunk(options2) {
+ if (!(this instanceof FirstChunk)) {
+ return new FirstChunk(options2);
+ }
+
+ Transform.call(this, options2);
+
+ this._firstChunk = true;
+ this._transformCalled = false;
+ this._minSize = options.minSize;
+ }
+
+ FirstChunk.prototype._transform = function (chunk, enc, cb) {
+ this._enc = enc;
+
+ if (this._firstChunk) {
+ this._firstChunk = false;
+
+ if (this._minSize == null) {
+ transform.call(this, chunk, enc, cb);
+ this._transformCalled = true;
+ return;
+ }
+
+ this._buffer = chunk;
+ cb();
+ return;
+ }
+
+ if (this._minSize == null) {
+ this.push(chunk);
+ cb();
+ return;
+ }
+
+ if (this._buffer.length < this._minSize) {
+ this._buffer = Buffer.concat([this._buffer, chunk]);
+ cb();
+ return;
+ }
+
+ if (this._buffer.length >= this._minSize) {
+ transform.call(this, this._buffer.slice(), enc, function () {
+ this.push(chunk);
+ cb();
+ }.bind(this));
+ this._transformCalled = true;
+ this._buffer = false;
+ return;
+ }
+
+ this.push(chunk);
+ cb();
+ };
+
+ FirstChunk.prototype._flush = function (cb) {
+ if (!this._buffer) {
+ cb();
+ return;
+ }
+
+ if (this._transformCalled) {
+ this.push(this._buffer);
+ cb();
+ } else {
+ transform.call(this, this._buffer.slice(), this._enc, cb);
+ }
+ };
+
+ return FirstChunk;
+}
+
+module.exports = function () {
+ return ctor.apply(ctor, arguments)();
+};
+
+module.exports.ctor = ctor;
diff --git a/node_modules/first-chunk-stream/package.json b/node_modules/first-chunk-stream/package.json
new file mode 100644
index 000000000..dd1f8770a
--- /dev/null
+++ b/node_modules/first-chunk-stream/package.json
@@ -0,0 +1,36 @@
+{
+ "name": "first-chunk-stream",
+ "version": "1.0.0",
+ "description": "Transform the first chunk in a stream",
+ "license": "MIT",
+ "repository": "sindresorhus/first-chunk-stream",
+ "author": {
+ "name": "Sindre Sorhus",
+ "email": "sindresorhus@gmail.com",
+ "url": "http://sindresorhus.com"
+ },
+ "engines": {
+ "node": ">=0.10.0"
+ },
+ "scripts": {
+ "test": "mocha"
+ },
+ "files": [
+ "index.js"
+ ],
+ "keywords": [
+ "buffer",
+ "stream",
+ "streams",
+ "transform",
+ "first",
+ "chunk",
+ "size",
+ "min",
+ "minimum"
+ ],
+ "devDependencies": {
+ "concat-stream": "^1.4.5",
+ "mocha": "*"
+ }
+}
diff --git a/node_modules/first-chunk-stream/readme.md b/node_modules/first-chunk-stream/readme.md
new file mode 100644
index 000000000..f8909c8f7
--- /dev/null
+++ b/node_modules/first-chunk-stream/readme.md
@@ -0,0 +1,62 @@
+# first-chunk-stream [![Build Status](https://travis-ci.org/sindresorhus/first-chunk-stream.svg?branch=master)](https://travis-ci.org/sindresorhus/first-chunk-stream)
+
+> Transform the first chunk in a stream
+
+Useful if you want to do something to the first chunk.
+
+You can also set the minimum size of that chunk.
+
+
+## Install
+
+```sh
+$ npm install --save first-chunk-stream
+```
+
+
+## Usage
+
+```js
+var fs = require('fs');
+var concat = require('concat-stream');
+var firstChunk = require('first-chunk-stream');
+
+// unicorn.txt => unicorn rainbow
+// `highWaterMark: 1` means it will only read 1 byte at the time
+fs.createReadStream('unicorn.txt', {highWaterMark: 1})
+ .pipe(firstChunk({minSize: 7}, function (chunk, enc, cb) {
+ this.push(chunk.toUpperCase());
+ cb();
+ }))
+ .pipe(concat(function (data) {
+ console.log(data);
+ //=> UNICORN rainbow
+ }));
+```
+
+
+## API
+
+### firstChunk([options], transform)
+
+#### options.minSize
+
+Type: `number`
+
+The minimum size of the first chunk.
+
+#### transform(chunk, encoding, callback)
+
+*Required*
+Type: `function`
+
+The [function](http://nodejs.org/docs/latest/api/stream.html#stream_transform_transform_chunk_encoding_callback) that gets the first chunk.
+
+### firstChunk.ctor()
+
+Instead of returning a [stream.Transform](http://nodejs.org/docs/latest/api/stream.html#stream_class_stream_transform_1) instance, `firstChunk.ctor()` returns a constructor for a custom Transform. This is useful when you want to use the same transform logic in multiple instances.
+
+
+## License
+
+MIT © [Sindre Sorhus](http://sindresorhus.com)