Skip to content

Commit 9221201

Browse files
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 7885b1d commit 9221201

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
@@ -136,26 +136,33 @@ function readableAddChunk(stream, state, chunk, encoding, addToFront) {
136136
const e = new Error('stream.unshift() after end event');
137137
stream.emit('error', e);
138138
} else {
139-
if (state.decoder && !addToFront && !encoding)
139+
var skipAdd;
140+
if (state.decoder && !addToFront && !encoding) {
140141
chunk = state.decoder.write(chunk);
142+
skipAdd = (!state.objectMode && chunk.length === 0);
143+
}
141144

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

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

161168
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)