Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

stream: Do not switch to objectMode implicitly

Only handle objects if explicitly told to do so in the options
object.  Non-buffer/string chunks are an error if not already in
objectMode.

Close #4662
  • Loading branch information...
commit 072c74088a069c43b7ccbc83e19c2ba745879dbf 1 parent 571e87f
Isaac Z. Schlueter authored
15 lib/_stream_readable.js
@@ -268,16 +268,17 @@ function onread(stream, er, chunk) {
268 268 var sync = state.sync;
269 269
270 270 // If we get something that is not a buffer, string, null, or undefined,
271   - // then switch into objectMode. Now stream chunks are all considered
272   - // to be of length=1, and the watermarks determine how many objects to
273   - // keep in the buffer, rather than how many bytes or characters.
  271 + // and we're not in objectMode, then that's an error.
  272 + // Otherwise stream chunks are all considered to be of length=1, and the
  273 + // watermarks determine how many objects to keep in the buffer, rather than
  274 + // how many bytes or characters.
274 275 if (!Buffer.isBuffer(chunk) &&
275 276 'string' !== typeof chunk &&
276 277 chunk !== null &&
277   - chunk !== undefined) {
278   - state.objectMode = true;
279   - state.length = state.buffer.length;
280   - state.decoder = null;
  278 + chunk !== undefined &&
  279 + !state.objectMode &&
  280 + !er) {
  281 + er = new TypeError('Invalid non-string/buffer chunk');
281 282 }
282 283
283 284 state.reading = false;
20 lib/_stream_writable.js
@@ -128,14 +128,22 @@ Writable.prototype.write = function(chunk, encoding, cb) {
128 128 return;
129 129 }
130 130
131   - // Writing something other than a string or buffer will switch
132   - // the stream into objectMode.
133   - if (!state.objectMode &&
134   - typeof chunk !== 'string' &&
  131 + // If we get something that is not a buffer, string, null, or undefined,
  132 + // and we're not in objectMode, then that's an error.
  133 + // Otherwise stream chunks are all considered to be of length=1, and the
  134 + // watermarks determine how many objects to keep in the buffer, rather than
  135 + // how many bytes or characters.
  136 + if (!Buffer.isBuffer(chunk) &&
  137 + 'string' !== typeof chunk &&
135 138 chunk !== null &&
136 139 chunk !== undefined &&
137   - !Buffer.isBuffer(chunk))
138   - state.objectMode = true;
  140 + !state.objectMode) {
  141 + var er = new TypeError('Invalid non-string/buffer chunk');
  142 + if (typeof cb === 'function')
  143 + cb(er);
  144 + this.emit('error', er);
  145 + return;
  146 + }
139 147
140 148 var len;
141 149 if (state.objectMode)
2  test/simple/test-stream2-basic.js
@@ -333,7 +333,7 @@ test('multipipe', function(t) {
333 333 test('back pressure respected', function (t) {
334 334 function noop() {}
335 335
336   - var r = new R();
  336 + var r = new R({ objectMode: true });
337 337 r._read = noop;
338 338 var counter = 0;
339 339 r.push(["one"]);
66 test/simple/test-stream2-objects.js
@@ -60,7 +60,7 @@ process.on('exit', function() {
60 60 process.nextTick(run);
61 61
62 62 function toArray(callback) {
63   - var stream = new Writable();
  63 + var stream = new Writable({ objectMode: true });
64 64 var list = [];
65 65 stream.write = function(chunk) {
66 66 list.push(chunk);
@@ -74,7 +74,7 @@ function toArray(callback) {
74 74 }
75 75
76 76 function fromArray(list) {
77   - var r = new Readable();
  77 + var r = new Readable({ objectMode: true });
78 78 r._read = noop;
79 79 list.forEach(function(chunk) {
80 80 r.push(chunk);
@@ -124,7 +124,7 @@ test('read(n) is ignored', function(t) {
124 124 });
125 125
126 126 test('can read objects from _read (sync)', function(t) {
127   - var r = new Readable();
  127 + var r = new Readable({ objectMode: true });
128 128 var list = [{ one: '1'}, { two: '2' }];
129 129 r._read = function(n, cb) {
130 130 var item = list.shift();
@@ -142,7 +142,7 @@ test('can read objects from _read (sync)', function(t) {
142 142 });
143 143
144 144 test('can read objects from _read (async)', function(t) {
145   - var r = new Readable();
  145 + var r = new Readable({ objectMode: true });
146 146 var list = [{ one: '1'}, { two: '2' }];
147 147 r._read = function(n, cb) {
148 148 var item = list.shift();
@@ -258,62 +258,8 @@ test('high watermark push', function(t) {
258 258 t.end();
259 259 });
260 260
261   -test('stream of buffers converted to object halfway through', function(t) {
262   - var r = new Readable();
263   - r._read = noop;
264   -
265   - r.push(new Buffer('fus'));
266   - r.push(new Buffer('do'));
267   - r.push(new Buffer('rah'));
268   -
269   - var str = r.read(4);
270   -
271   - assert.equal(str, 'fusd');
272   -
273   - r.push({ foo: 'bar' });
274   - r.push(null);
275   -
276   - r.pipe(toArray(function(list) {
277   - assert.deepEqual(list, [
278   - new Buffer('o'),
279   - new Buffer('rah'),
280   - { foo: 'bar'}
281   - ]);
282   -
283   - t.end();
284   - }));
285   -});
286   -
287   -test('stream of strings converted to objects halfway through', function(t) {
288   - var r = new Readable({
289   - encoding: 'utf8'
290   - });
291   - r._read = noop;
292   -
293   - r.push('fus');
294   - r.push('do');
295   - r.push('rah');
296   -
297   - var str = r.read(4);
298   -
299   - assert.equal(str, 'fusd');
300   -
301   - r.push({ foo: 'bar' });
302   - r.push(null);
303   -
304   - r.pipe(toArray(function(list) {
305   - assert.deepEqual(list, [
306   - 'o',
307   - 'rah',
308   - { foo: 'bar'}
309   - ]);
310   -
311   - t.end();
312   - }));
313   -});
314   -
315 261 test('can write objects to stream', function(t) {
316   - var w = new Writable();
  262 + var w = new Writable({ objectMode: true });
317 263
318 264 w._write = function(chunk, cb) {
319 265 assert.deepEqual(chunk, { foo: 'bar' });
@@ -329,7 +275,7 @@ test('can write objects to stream', function(t) {
329 275 });
330 276
331 277 test('can write multiple objects to stream', function(t) {
332   - var w = new Writable();
  278 + var w = new Writable({ objectMode: true });
333 279 var list = [];
334 280
335 281 w._write = function(chunk, cb) {

0 comments on commit 072c740

Please sign in to comment.
Something went wrong with that request. Please try again.