Skip to content

Commit a978bed

Browse files
committed
stream: Allow strings in Readable.push/unshift
Fix #4909
1 parent b0f6789 commit a978bed

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

lib/_stream_readable.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,15 @@ function Readable(options) {
9898
// write() some more.
9999
Readable.prototype.push = function(chunk) {
100100
var state = this._readableState;
101+
if (typeof chunk === 'string' && !state.objectMode)
102+
chunk = new Buffer(chunk, arguments[1]);
101103
return readableAddChunk(this, state, chunk, false);
102104
};
103105

104106
Readable.prototype.unshift = function(chunk) {
105107
var state = this._readableState;
108+
if (typeof chunk === 'string' && !state.objectMode)
109+
chunk = new Buffer(chunk, arguments[1]);
106110
return readableAddChunk(this, state, chunk, true);
107111
};
108112

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
25+
var Readable = require('stream').Readable;
26+
var util = require('util');
27+
28+
util.inherits(MyStream, Readable);
29+
function MyStream(options) {
30+
Readable.call(this, options);
31+
this._chunks = 3;
32+
}
33+
34+
MyStream.prototype._read = function(n) {
35+
switch (this._chunks--) {
36+
case 0:
37+
return this.push(null);
38+
case 1:
39+
return setTimeout(function() {
40+
this.push('last chunk');
41+
}.bind(this), 100);
42+
case 2:
43+
return this.push('second to last chunk');
44+
case 3:
45+
return process.nextTick(function() {
46+
this.push('first chunk');
47+
}.bind(this));
48+
default:
49+
throw new Error('?');
50+
}
51+
};
52+
53+
var ms = new MyStream();
54+
var results = [];
55+
ms.on('readable', function() {
56+
var chunk;
57+
while (null !== (chunk = ms.read()))
58+
results.push(chunk + '');
59+
});
60+
61+
var expect = [ 'first chunksecond to last chunk', 'last chunk' ];
62+
process.on('exit', function() {
63+
assert.equal(ms._chunks, -1);
64+
assert.deepEqual(results, expect);
65+
console.log('ok');
66+
});

0 commit comments

Comments
 (0)