fs: remove unnecessary argument check

Writable already assures that only Buffer's are passed to _write. Also
this is not the "correct" way to handle errors inside _write.

PR-URL: https://github.com/nodejs/node/pull/29043
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Robert Nagy 2019-08-08 11:18:39 +02:00 committed by Rich Trott
parent a861b84d5d
commit 95d6ad67bf
2 changed files with 10 additions and 12 deletions

View File

@ -3,7 +3,6 @@
const { Math, Object } = primordials;
const {
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE
} = require('internal/errors').codes;
const { validateNumber } = require('internal/validators');
@ -235,6 +234,9 @@ function WriteStream(path, options) {
options = copyObject(getOptions(options, {}));
// Only buffers are supported.
options.decodeStrings = true;
// For backwards compat do not emit close on destroy.
if (options.emitClose === undefined) {
options.emitClose = false;
@ -295,11 +297,6 @@ WriteStream.prototype.open = function() {
WriteStream.prototype._write = function(data, encoding, cb) {
if (!(data instanceof Buffer)) {
const err = new ERR_INVALID_ARG_TYPE('data', 'Buffer', data);
return this.emit('error', err);
}
if (typeof this.fd !== 'number') {
return this.once('open', function() {
this._write(data, encoding, cb);

View File

@ -56,12 +56,13 @@ tmpdir.refresh();
// Throws if data is not of type Buffer.
{
const stream = fs.createWriteStream(file);
common.expectsError(() => {
stream._write(42, null, function() {});
}, {
stream.on('error', common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "data" argument must be of type Buffer. Received type number'
});
type: TypeError
}));
stream.write(42, null, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
}));
stream.destroy();
}