2 var util = require('util');
3 var Transform = require('stream').Transform;
5 function ctor(options, transform) {
6 util.inherits(FirstChunk, Transform);
8 if (typeof options === 'function') {
13 if (typeof transform !== 'function') {
14 throw new Error('transform function required');
17 function FirstChunk(options2) {
18 if (!(this instanceof FirstChunk)) {
19 return new FirstChunk(options2);
22 Transform.call(this, options2);
24 this._firstChunk = true;
25 this._transformCalled = false;
26 this._minSize = options.minSize;
29 FirstChunk.prototype._transform = function (chunk, enc, cb) {
32 if (this._firstChunk) {
33 this._firstChunk = false;
35 if (this._minSize == null) {
36 transform.call(this, chunk, enc, cb);
37 this._transformCalled = true;
46 if (this._minSize == null) {
52 if (this._buffer.length < this._minSize) {
53 this._buffer = Buffer.concat([this._buffer, chunk]);
58 if (this._buffer.length >= this._minSize) {
59 transform.call(this, this._buffer.slice(), enc, function () {
63 this._transformCalled = true;
72 FirstChunk.prototype._flush = function (cb) {
78 if (this._transformCalled) {
79 this.push(this._buffer);
82 transform.call(this, this._buffer.slice(), this._enc, cb);
89 module.exports = function () {
90 return ctor.apply(ctor, arguments)();
93 module.exports.ctor = ctor;