Skip to content

Commit 1b54371

Browse files
BridgeARmcollina
authored andcommitted
stream: use more explicit statements
Using objectMode with stream_wrap has not worked properly before and would end in an error. Therefore prohibit the usage of objectMode alltogether. This also improves the handling performance due to the cheaper chunk check and by using explicit statements as they produce better code from the compiler. PR-URL: #13863 Reviewed-By: Matteo Collina <[email protected]>
1 parent a1ecdcf commit 1b54371

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

doc/api/errors.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -829,10 +829,11 @@ Node.js does not allow `stdout` or `stderr` Streams to be closed by user code.
829829
Used when an attempt is made to close the `process.stdout` stream. By design,
830830
Node.js does not allow `stdout` or `stderr` Streams to be closed by user code.
831831

832-
<a id="ERR_STREAM_HAS_STRINGDECODER"></a>
833-
### ERR_STREAM_HAS_STRINGDECODER
832+
<a id="ERR_STREAM_WRAP"></a>
833+
### ERR_STREAM_WRAP
834834

835-
Used to prevent an abort if a string decoder was set on the Socket.
835+
Used to prevent an abort if a string decoder was set on the Socket or if in
836+
`objectMode`.
836837

837838
Example
838839
```js

lib/_stream_transform.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function afterTransform(er, data) {
7676

7777
var cb = ts.writecb;
7878

79-
if (!cb) {
79+
if (cb === null) {
8080
return this.emit('error', new errors.Error('ERR_MULTIPLE_CALLBACK'));
8181
}
8282

lib/_stream_wrap.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ const assert = require('assert');
44
const util = require('util');
55
const Socket = require('net').Socket;
66
const JSStream = process.binding('js_stream').JSStream;
7-
// TODO(bmeurer): Change this back to const once hole checks are
8-
// properly optimized away early in Ignition+TurboFan.
9-
var Buffer = require('buffer').Buffer;
107
const uv = process.binding('uv');
118
const debug = util.debuglog('stream_wrap');
129
const errors = require('internal/errors');
@@ -47,12 +44,12 @@ function StreamWrap(stream) {
4744
self.emit('error', err);
4845
});
4946
this.stream.on('data', function ondata(chunk) {
50-
if (!(chunk instanceof Buffer)) {
47+
if (typeof chunk === 'string' || this._readableState.objectMode === true) {
5148
// Make sure that no further `data` events will happen
5249
this.pause();
5350
this.removeListener('data', ondata);
5451

55-
self.emit('error', new errors.Error('ERR_STREAM_HAS_STRINGDECODER'));
52+
self.emit('error', new errors.Error('ERR_STREAM_WRAP'));
5653
return;
5754
}
5855

lib/internal/errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536');
171171
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running');
172172
E('ERR_STDERR_CLOSE', 'process.stderr cannot be closed');
173173
E('ERR_STDOUT_CLOSE', 'process.stdout cannot be closed');
174-
E('ERR_STREAM_HAS_STRINGDECODER', 'Stream has StringDecoder');
174+
E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode');
175175
E('ERR_TRANSFORM_ALREADY_TRANSFORMING',
176176
'Calling transform done when still transforming');
177177
E('ERR_TRANSFORM_WITH_LENGTH_0',
+32-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
11
'use strict';
22
const common = require('../common');
3-
const assert = require('assert');
43

54
const StreamWrap = require('_stream_wrap');
65
const Duplex = require('stream').Duplex;
76

8-
const stream = new Duplex({
9-
read: function() {
10-
},
11-
write: function() {
12-
}
13-
});
7+
{
8+
const stream = new Duplex({
9+
read() {},
10+
write() {}
11+
});
1412

15-
stream.setEncoding('ascii');
13+
stream.setEncoding('ascii');
1614

17-
const wrap = new StreamWrap(stream);
15+
const wrap = new StreamWrap(stream);
1816

19-
wrap.on('error', common.mustCall(function(err) {
20-
assert(/StringDecoder/.test(err.message));
21-
}));
17+
wrap.on('error', common.expectsError({
18+
type: Error,
19+
code: 'ERR_STREAM_WRAP',
20+
message: 'Stream has StringDecoder set or is in objectMode'
21+
}));
2222

23-
stream.push('ohai');
23+
stream.push('ohai');
24+
}
25+
26+
{
27+
const stream = new Duplex({
28+
read() {},
29+
write() {},
30+
objectMode: true
31+
});
32+
33+
const wrap = new StreamWrap(stream);
34+
35+
wrap.on('error', common.expectsError({
36+
type: Error,
37+
code: 'ERR_STREAM_WRAP',
38+
message: 'Stream has StringDecoder set or is in objectMode'
39+
}));
40+
41+
stream.push(new Error('foo'));
42+
}

0 commit comments

Comments
 (0)