Skip to content

Commit cd68d86

Browse files
committed
stream: Remove output function from _transform
Just use stream.push(outputChunk) instead.
1 parent 049903e commit cd68d86

8 files changed

+69
-69
lines changed

doc/api/stream.markdown

+14-14
Original file line numberDiff line numberDiff line change
@@ -589,11 +589,9 @@ In classes that extend the Transform class, make sure to call the
589589
constructor so that the buffering settings can be properly
590590
initialized.
591591
592-
### transform.\_transform(chunk, outputFn, callback)
592+
### transform.\_transform(chunk, callback)
593593
594594
* `chunk` {Buffer} The chunk to be transformed.
595-
* `outputFn` {Function} Call this function with any output data to be
596-
passed to the readable interface.
597595
* `callback` {Function} Call this function (optionally with an error
598596
argument) when you are done processing the supplied chunk.
599597
@@ -609,20 +607,21 @@ Transform class, to handle the bytes being written, and pass them off
609607
to the readable portion of the interface. Do asynchronous I/O,
610608
process things, and so on.
611609
610+
Call `transform.push(outputChunk)` 0 or more times to generate output
611+
from this input chunk, depending on how much data you want to output
612+
as a result of this chunk.
613+
612614
Call the callback function only when the current chunk is completely
613-
consumed. Note that this may mean that you call the `outputFn` zero
614-
or more times, depending on how much data you want to output as a
615-
result of this chunk.
615+
consumed. Note that there may or may not be output as a result of any
616+
particular input chunk.
616617
617618
This method is prefixed with an underscore because it is internal to
618619
the class that defines it, and should not be called directly by user
619620
programs. However, you **are** expected to override this method in
620621
your own extension classes.
621622
622-
### transform.\_flush(outputFn, callback)
623+
### transform.\_flush(callback)
623624
624-
* `outputFn` {Function} Call this function with any output data to be
625-
passed to the readable interface.
626625
* `callback` {Function} Call this function (optionally with an error
627626
argument) when you are done flushing any remaining data.
628627
@@ -639,8 +638,9 @@ can with what is left, so that the data will be complete.
639638
In those cases, you can implement a `_flush` method, which will be
640639
called at the very end, after all the written data is consumed, but
641640
before emitting `end` to signal the end of the readable side. Just
642-
like with `_transform`, call `outputFn` zero or more times, as
643-
appropriate, and call `callback` when the flush operation is complete.
641+
like with `_transform`, call `transform.push(chunk)` zero or more
642+
times, as appropriate, and call `callback` when the flush operation is
643+
complete.
644644
645645
This method is prefixed with an underscore because it is internal to
646646
the class that defines it, and should not be called directly by user
@@ -671,7 +671,7 @@ function SimpleProtocol(options) {
671671
SimpleProtocol.prototype = Object.create(
672672
Transform.prototype, { constructor: { value: SimpleProtocol }});
673673

674-
SimpleProtocol.prototype._transform = function(chunk, output, done) {
674+
SimpleProtocol.prototype._transform = function(chunk, done) {
675675
if (!this._inBody) {
676676
// check if the chunk has a \n\n
677677
var split = -1;
@@ -707,11 +707,11 @@ SimpleProtocol.prototype._transform = function(chunk, output, done) {
707707
this.emit('header', this.header);
708708

709709
// now, because we got some extra data, emit this first.
710-
output(b);
710+
this.push(b);
711711
}
712712
} else {
713713
// from there on, just provide the data to our consumer as-is.
714-
output(b);
714+
this.push(b);
715715
}
716716
done();
717717
};

lib/_stream_passthrough.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ function PassThrough(options) {
3636
Transform.call(this, options);
3737
}
3838

39-
PassThrough.prototype._transform = function(chunk, output, cb) {
39+
PassThrough.prototype._transform = function(chunk, cb) {
4040
cb(null, chunk);
4141
};

lib/_stream_transform.js

+21-19
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ util.inherits(Transform, Duplex);
7171

7272
function TransformState(options, stream) {
7373
var ts = this;
74-
this.output = function(chunk) {
75-
ts.needTransform = false;
76-
stream.push(chunk);
77-
};
7874

7975
this.afterTransform = function(er, data) {
8076
return afterTransform(stream, er, data);
@@ -99,7 +95,7 @@ function afterTransform(stream, er, data) {
9995
ts.writecb = null;
10096

10197
if (data !== null && data !== undefined)
102-
ts.output(data);
98+
stream.push(data);
10399

104100
if (cb)
105101
cb(er);
@@ -132,20 +128,25 @@ function Transform(options) {
132128

133129
this.once('finish', function() {
134130
if ('function' === typeof this._flush)
135-
this._flush(ts.output, function(er) {
131+
this._flush(function(er) {
136132
done(stream, er);
137133
});
138134
else
139135
done(stream);
140136
});
141137
}
142138

139+
Transform.prototype.push = function(chunk) {
140+
this._transformState.needTransform = false;
141+
return Duplex.prototype.push.call(this, chunk);
142+
};
143+
143144
// This is the part where you do stuff!
144145
// override this function in implementation classes.
145146
// 'chunk' is an input chunk.
146147
//
147-
// Call `output(newChunk)` to pass along transformed output
148-
// to the readable side. You may call 'output' zero or more times.
148+
// Call `push(newChunk)` to pass along transformed output
149+
// to the readable side. You may call 'push' zero or more times.
149150
//
150151
// Call `cb(err)` when you are done with this chunk. If you pass
151152
// an error, then that'll put the hurt on the whole operation. If you
@@ -158,11 +159,13 @@ Transform.prototype._write = function(chunk, cb) {
158159
var ts = this._transformState;
159160
ts.writecb = cb;
160161
ts.writechunk = chunk;
161-
if (ts.transforming)
162-
return;
163-
var rs = this._readableState;
164-
if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark)
165-
this._read(rs.bufferSize);
162+
if (!ts.transforming) {
163+
var rs = this._readableState;
164+
if (ts.needTransform ||
165+
rs.needReadable ||
166+
rs.length < rs.highWaterMark)
167+
this._read(rs.bufferSize);
168+
}
166169
};
167170

168171
// Doesn't matter what the args are here.
@@ -173,13 +176,12 @@ Transform.prototype._read = function(n) {
173176

174177
if (ts.writechunk && ts.writecb && !ts.transforming) {
175178
ts.transforming = true;
176-
this._transform(ts.writechunk, ts.output, ts.afterTransform);
177-
return;
179+
this._transform(ts.writechunk, ts.afterTransform);
180+
} else {
181+
// mark that we need a transform, so that any data that comes in
182+
// will get processed, now that we've asked for it.
183+
ts.needTransform = true;
178184
}
179-
180-
// mark that we need a transform, so that any data that comes in
181-
// will get processed, now that we've asked for it.
182-
ts.needTransform = true;
183185
};
184186

185187

lib/crypto.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,13 @@ function Hash(algorithm, options) {
160160

161161
util.inherits(Hash, stream.Transform);
162162

163-
Hash.prototype._transform = function(chunk, output, callback) {
163+
Hash.prototype._transform = function(chunk, callback) {
164164
this._binding.update(chunk);
165165
callback();
166166
};
167167

168-
Hash.prototype._flush = function(output, callback) {
169-
output(this._binding.digest());
168+
Hash.prototype._flush = function(callback) {
169+
this.push(this._binding.digest());
170170
callback();
171171
};
172172

@@ -226,13 +226,13 @@ function Cipher(cipher, password, options) {
226226

227227
util.inherits(Cipher, stream.Transform);
228228

229-
Cipher.prototype._transform = function(chunk, output, callback) {
230-
output(this._binding.update(chunk));
229+
Cipher.prototype._transform = function(chunk, callback) {
230+
this.push(this._binding.update(chunk));
231231
callback();
232232
};
233233

234-
Cipher.prototype._flush = function(output, callback) {
235-
output(this._binding.final());
234+
Cipher.prototype._flush = function(callback) {
235+
this.push(this._binding.final());
236236
callback();
237237
};
238238

lib/zlib.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ Zlib.prototype.reset = function reset() {
308308
return this._binding.reset();
309309
};
310310

311-
Zlib.prototype._flush = function(output, callback) {
312-
this._transform(null, output, callback);
311+
Zlib.prototype._flush = function(callback) {
312+
this._transform(null, callback);
313313
};
314314

315315
Zlib.prototype.flush = function(callback) {
@@ -320,12 +320,10 @@ Zlib.prototype.flush = function(callback) {
320320
ws.needDrain = true;
321321
var self = this;
322322
this.once('drain', function() {
323-
self._flush(ts.output, callback);
323+
self._flush(callback);
324324
});
325-
return;
326-
}
327-
328-
this._flush(ts.output, callback || function() {});
325+
} else
326+
this._flush(callback || function() {});
329327
};
330328

331329
Zlib.prototype.close = function(callback) {
@@ -345,7 +343,7 @@ Zlib.prototype.close = function(callback) {
345343
});
346344
};
347345

348-
Zlib.prototype._transform = function(chunk, output, cb) {
346+
Zlib.prototype._transform = function(chunk, cb) {
349347
var flushFlag;
350348
var ws = this._writableState;
351349
var ending = ws.ending || ws.ended;
@@ -392,7 +390,7 @@ Zlib.prototype._transform = function(chunk, output, cb) {
392390
var out = self._buffer.slice(self._offset, self._offset + have);
393391
self._offset += have;
394392
// serve some output to the consumer.
395-
output(out);
393+
self.push(out);
396394
}
397395

398396
// exhausted the output buffer, or used all the input create a new one.

test/simple/test-stream2-transform.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ test('writable side consumption', function(t) {
6767
});
6868

6969
var transformed = 0;
70-
tx._transform = function(chunk, output, cb) {
70+
tx._transform = function(chunk, cb) {
7171
transformed += chunk.length;
72-
output(chunk);
72+
tx.push(chunk);
7373
cb();
7474
};
7575

@@ -106,10 +106,10 @@ test('passthrough', function(t) {
106106

107107
test('simple transform', function(t) {
108108
var pt = new Transform;
109-
pt._transform = function(c, output, cb) {
109+
pt._transform = function(c, cb) {
110110
var ret = new Buffer(c.length);
111111
ret.fill('x');
112-
output(ret);
112+
pt.push(ret);
113113
cb();
114114
};
115115

@@ -128,9 +128,9 @@ test('simple transform', function(t) {
128128

129129
test('async passthrough', function(t) {
130130
var pt = new Transform;
131-
pt._transform = function(chunk, output, cb) {
131+
pt._transform = function(chunk, cb) {
132132
setTimeout(function() {
133-
output(chunk);
133+
pt.push(chunk);
134134
cb();
135135
}, 10);
136136
};
@@ -154,11 +154,11 @@ test('assymetric transform (expand)', function(t) {
154154
var pt = new Transform;
155155

156156
// emit each chunk 2 times.
157-
pt._transform = function(chunk, output, cb) {
157+
pt._transform = function(chunk, cb) {
158158
setTimeout(function() {
159-
output(chunk);
159+
pt.push(chunk);
160160
setTimeout(function() {
161-
output(chunk);
161+
pt.push(chunk);
162162
cb();
163163
}, 10)
164164
}, 10);
@@ -189,24 +189,24 @@ test('assymetric transform (compress)', function(t) {
189189
// or whatever's left.
190190
pt.state = '';
191191

192-
pt._transform = function(chunk, output, cb) {
192+
pt._transform = function(chunk, cb) {
193193
if (!chunk)
194194
chunk = '';
195195
var s = chunk.toString();
196196
setTimeout(function() {
197197
this.state += s.charAt(0);
198198
if (this.state.length === 3) {
199-
output(new Buffer(this.state));
199+
pt.push(new Buffer(this.state));
200200
this.state = '';
201201
}
202202
cb();
203203
}.bind(this), 10);
204204
};
205205

206-
pt._flush = function(output, cb) {
206+
pt._flush = function(cb) {
207207
// just output whatever we have.
208208
setTimeout(function() {
209-
output(new Buffer(this.state));
209+
pt.push(new Buffer(this.state));
210210
this.state = '';
211211
cb();
212212
}.bind(this), 10);
@@ -359,9 +359,9 @@ test('passthrough facaded', function(t) {
359359
test('object transform (json parse)', function(t) {
360360
console.error('json parse stream');
361361
var jp = new Transform({ objectMode: true });
362-
jp._transform = function(data, output, cb) {
362+
jp._transform = function(data, cb) {
363363
try {
364-
output(JSON.parse(data));
364+
jp.push(JSON.parse(data));
365365
cb();
366366
} catch (er) {
367367
cb(er);
@@ -399,9 +399,9 @@ test('object transform (json parse)', function(t) {
399399
test('object transform (json stringify)', function(t) {
400400
console.error('json parse stream');
401401
var js = new Transform({ objectMode: true });
402-
js._transform = function(data, output, cb) {
402+
js._transform = function(data, cb) {
403403
try {
404-
output(JSON.stringify(data));
404+
js.push(JSON.stringify(data));
405405
cb();
406406
} catch (er) {
407407
cb(er);

test/simple/test-stream2-unpipe-drain.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function TestWriter() {
3232
}
3333
util.inherits(TestWriter, stream.Writable);
3434

35-
TestWriter.prototype._write = function (buffer, callback) {
35+
TestWriter.prototype._write = function (buffer, encoding, callback) {
3636
console.log('write called');
3737
// super slow write stream (callback never called)
3838
};

test/simple/test-stream2-unpipe-leak.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function TestWriter() {
3131
}
3232
util.inherits(TestWriter, stream.Writable);
3333

34-
TestWriter.prototype._write = function(buffer, callback) {
34+
TestWriter.prototype._write = function(buffer, encoding, callback) {
3535
callback(null);
3636
};
3737

0 commit comments

Comments
 (0)