Initial commit
[yaffs-website] / node_modules / first-chunk-stream / index.js
1 'use strict';
2 var util = require('util');
3 var Transform = require('stream').Transform;
4
5 function ctor(options, transform) {
6         util.inherits(FirstChunk, Transform);
7
8         if (typeof options === 'function') {
9                 transform = options;
10                 options = {};
11         }
12
13         if (typeof transform !== 'function') {
14                 throw new Error('transform function required');
15         }
16
17         function FirstChunk(options2) {
18                 if (!(this instanceof FirstChunk)) {
19                         return new FirstChunk(options2);
20                 }
21
22                 Transform.call(this, options2);
23
24                 this._firstChunk = true;
25                 this._transformCalled = false;
26                 this._minSize = options.minSize;
27         }
28
29         FirstChunk.prototype._transform = function (chunk, enc, cb) {
30                 this._enc = enc;
31
32                 if (this._firstChunk) {
33                         this._firstChunk = false;
34
35                         if (this._minSize == null) {
36                                 transform.call(this, chunk, enc, cb);
37                                 this._transformCalled = true;
38                                 return;
39                         }
40
41                         this._buffer = chunk;
42                         cb();
43                         return;
44                 }
45
46                 if (this._minSize == null) {
47                         this.push(chunk);
48                         cb();
49                         return;
50                 }
51
52                 if (this._buffer.length < this._minSize) {
53                         this._buffer = Buffer.concat([this._buffer, chunk]);
54                         cb();
55                         return;
56                 }
57
58                 if (this._buffer.length >= this._minSize) {
59                         transform.call(this, this._buffer.slice(), enc, function () {
60                                 this.push(chunk);
61                                 cb();
62                         }.bind(this));
63                         this._transformCalled = true;
64                         this._buffer = false;
65                         return;
66                 }
67
68                 this.push(chunk);
69                 cb();
70         };
71
72         FirstChunk.prototype._flush = function (cb) {
73                 if (!this._buffer) {
74                         cb();
75                         return;
76                 }
77
78                 if (this._transformCalled) {
79                         this.push(this._buffer);
80                         cb();
81                 } else {
82                         transform.call(this, this._buffer.slice(), this._enc, cb);
83                 }
84         };
85
86         return FirstChunk;
87 }
88
89 module.exports = function () {
90         return ctor.apply(ctor, arguments)();
91 };
92
93 module.exports.ctor = ctor;