Skip to content

Commit 04db9ef

Browse files
mscdexMyles Borins
authored and
Myles Borins
committed
stream: fix no data on partial decode
Before this commit, it was possible to push a partial character to a readable stream where it was decoded as an empty string and then added to the internal buffer. This caused the stream to not emit any data, even when the rest of the character bytes were pushed separately, because of a non-zero length check of the first chunk in the internal buffer. Fixes: #5223 PR-URL: #5226 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 9c51f3f commit 04db9ef

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

lib/_stream_readable.js

+22-15
Original file line numberDiff line numberDiff line change
@@ -135,26 +135,33 @@ function readableAddChunk(stream, state, chunk, encoding, addToFront) {
135135
const e = new Error('stream.unshift() after end event');
136136
stream.emit('error', e);
137137
} else {
138-
if (state.decoder && !addToFront && !encoding)
138+
var skipAdd;
139+
if (state.decoder && !addToFront && !encoding) {
139140
chunk = state.decoder.write(chunk);
141+
skipAdd = (!state.objectMode && chunk.length === 0);
142+
}
140143

141144
if (!addToFront)
142145
state.reading = false;
143146

144-
// if we want the data now, just emit it.
145-
if (state.flowing && state.length === 0 && !state.sync) {
146-
stream.emit('data', chunk);
147-
stream.read(0);
148-
} else {
149-
// update the buffer info.
150-
state.length += state.objectMode ? 1 : chunk.length;
151-
if (addToFront)
152-
state.buffer.unshift(chunk);
153-
else
154-
state.buffer.push(chunk);
155-
156-
if (state.needReadable)
157-
emitReadable(stream);
147+
// Don't add to the buffer if we've decoded to an empty string chunk and
148+
// we're not in object mode
149+
if (!skipAdd) {
150+
// if we want the data now, just emit it.
151+
if (state.flowing && state.length === 0 && !state.sync) {
152+
stream.emit('data', chunk);
153+
stream.read(0);
154+
} else {
155+
// update the buffer info.
156+
state.length += state.objectMode ? 1 : chunk.length;
157+
if (addToFront)
158+
state.buffer.unshift(chunk);
159+
else
160+
state.buffer.push(chunk);
161+
162+
if (state.needReadable)
163+
emitReadable(stream);
164+
}
158165
}
159166

160167
maybeReadMore(stream, state);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
require('../common');
3+
const Readable = require('_stream_readable');
4+
const assert = require('assert');
5+
6+
var buf = '';
7+
const euro = new Buffer([0xE2, 0x82, 0xAC]);
8+
const cent = new Buffer([0xC2, 0xA2]);
9+
const source = Buffer.concat([euro, cent]);
10+
11+
const readable = Readable({ encoding: 'utf8' });
12+
readable.push(source.slice(0, 2));
13+
readable.push(source.slice(2, 4));
14+
readable.push(source.slice(4, 6));
15+
readable.push(null);
16+
17+
readable.on('data', function(data) {
18+
buf += data;
19+
});
20+
21+
process.on('exit', function() {
22+
assert.strictEqual(buf, '€¢');
23+
});

0 commit comments

Comments
 (0)