Skip to content

Commit c861623

Browse files
committedMar 15, 2016
stream: Support writing null to stream.Transform
As discussed in nodejs#5711, writing null to a stream.Transform puts the stream into an unrecoverable state. This commit fixes the issue by passing it to ._transform, like the other falsey values. It updates the falsey value test for Transform to include null (and undefined, which was previously omitted) and adds a test for the behavior of Writable in objectMode when falsey values are written to match Transform. Fixes: nodejs#5711
1 parent 99a5d07 commit c861623

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed
 

Diff for: ‎lib/_stream_transform.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ Transform.prototype._write = function(chunk, encoding, cb) {
162162
Transform.prototype._read = function(n) {
163163
var ts = this._transformState;
164164

165-
if (ts.writechunk !== null && ts.writecb && !ts.transforming) {
165+
if (ts.writecb && !ts.transforming) {
166166
ts.transforming = true;
167167
this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);
168168
} else {

Diff for: ‎test/parallel/test-stream2-objects.js

+25
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,31 @@ test('can write strings as objects', function(t) {
302302
w.end();
303303
});
304304

305+
test('can write falsey values', function(t) {
306+
var w = new Writable({
307+
objectMode: true
308+
});
309+
var list = [];
310+
311+
w._write = function(chunk, encoding, cb) {
312+
list.push(chunk);
313+
process.nextTick(cb);
314+
};
315+
316+
w.on('finish', function() {
317+
assert.deepEqual(list, [false, 0, '', null, undefined]);
318+
319+
t.end();
320+
});
321+
322+
w.write(false);
323+
w.write(0);
324+
w.write('');
325+
w.write(null);
326+
w.write(undefined);
327+
w.end();
328+
});
329+
305330
test('buffers finish until cb is called', function(t) {
306331
var w = new Writable({
307332
objectMode: true

Diff for: ‎test/parallel/test-stream2-transform.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ test('simple transform', function(t) {
131131
test('simple object transform', function(t) {
132132
var pt = new Transform({ objectMode: true });
133133
pt._transform = function(c, e, cb) {
134-
pt.push(JSON.stringify(c));
134+
pt.push(JSON.stringify(c) || 'undefined');
135135
cb();
136136
};
137137

@@ -142,6 +142,8 @@ test('simple object transform', function(t) {
142142
pt.write('foo');
143143
pt.write('');
144144
pt.write({ a: 'b'});
145+
pt.write(null);
146+
pt.write(undefined);
145147
pt.end();
146148

147149
t.equal(pt.read(), '1');
@@ -151,6 +153,8 @@ test('simple object transform', function(t) {
151153
t.equal(pt.read(), '"foo"');
152154
t.equal(pt.read(), '""');
153155
t.equal(pt.read(), '{"a":"b"}');
156+
t.equal(pt.read(), 'null');
157+
t.equal(pt.read(), 'undefined');
154158
t.end();
155159
});
156160

0 commit comments

Comments
 (0)
Please sign in to comment.