Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: migrate stream errors to internal/errors #15665

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1040,11 +1040,6 @@ function copyFromBuffer(n, list) {
function endReadable(stream) {
var state = stream._readableState;

// If we get here before consuming all the bytes, then that is a
// bug in node. Should never happen.
if (state.length > 0)
throw new Error('"endReadable()" called on non-empty stream');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep this safety check in.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is dead code at the moment. There is no way to trigger it right now and only a false refactoring could trigger this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as reference - there are three occurrences of endReadable right now. Each of those are only triggered in case state.length === 0. Those guards are here
https://github.com/BridgeAR/node/blob/615ab68c02e81bc17dbfb15d489a3558f667dc67/lib/_stream_readable.js#L384
https://github.com/BridgeAR/node/blob/615ab68c02e81bc17dbfb15d489a3558f667dc67/lib/_stream_readable.js#L395
https://github.com/BridgeAR/node/blob/615ab68c02e81bc17dbfb15d489a3558f667dc67/lib/_stream_readable.js#L466
The function can also not be triggered from the outside.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed then, 👍 .


if (!state.endEmitted) {
state.ended = true;
process.nextTick(endReadableNT, state, stream);
Expand Down
2 changes: 1 addition & 1 deletion lib/_stream_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Transform.prototype.push = function(chunk, encoding) {
// an error, then that'll put the hurt on the whole operation. If you
// never call cb(), then you'll never get another chunk.
Transform.prototype._transform = function(chunk, encoding, cb) {
throw new Error('_transform() is not implemented');
throw new errors.Error('ERR_METHOD_NOT_IMPLEMENTED', '_transform');
};

Transform.prototype._write = function(chunk, encoding, cb) {
Expand Down
3 changes: 2 additions & 1 deletion lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const internalUtil = require('internal/util');
const Stream = require('stream');
const Buffer = require('buffer').Buffer;
const destroyImpl = require('internal/streams/destroy');
const errors = require('internal/errors');

util.inherits(Writable, Stream);

Expand Down Expand Up @@ -319,7 +320,7 @@ Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
if (typeof encoding === 'string')
encoding = encoding.toLowerCase();
if (!Buffer.isEncoding(encoding))
throw new TypeError('Unknown encoding: ' + encoding);
throw new errors.TypeError('ERR_UNKNOWN_ENCODING', encoding);
this._writableState.defaultEncoding = encoding;
return this;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ const t2 = new Transform({});
t.end(Buffer.from('blerg'));
t.resume();

assert.throws(() => {
common.expectsError(() => {
t2.end(Buffer.from('blerg'));
}, /^Error: _transform\(\) is not implemented$/);

}, {
type: Error,
code: 'ERR_METHOD_NOT_IMPLEMENTED',
message: 'The _transform method is not implemented'
});

process.on('exit', () => {
assert.strictEqual(t._transform, _transform);
Expand Down
10 changes: 7 additions & 3 deletions test/parallel/test-stream-writable-change-default-encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');

const stream = require('stream');
Expand Down Expand Up @@ -55,13 +55,17 @@ MyWritable.prototype._write = function(chunk, encoding, callback) {
m.end();
}());

assert.throws(function changeDefaultEncodingToInvalidValue() {
common.expectsError(function changeDefaultEncodingToInvalidValue() {
const m = new MyWritable(function(isBuffer, type, enc) {
}, { decodeStrings: false });
m.setDefaultEncoding({});
m.write('bar');
m.end();
}, /^TypeError: Unknown encoding: \[object Object\]$/);
}, {
type: TypeError,
code: 'ERR_UNKNOWN_ENCODING',
message: 'Unknown encoding: [object Object]'
});

(function checkVairableCaseEncoding() {
const m = new MyWritable(function(isBuffer, type, enc) {
Expand Down