Skip to content

Commit

Permalink
fs: remove unnecessary argument check
Browse files Browse the repository at this point in the history
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: #29043
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
ronag authored and BridgeAR committed Sep 25, 2019
1 parent b43d2dd commit 67f5de9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
9 changes: 3 additions & 6 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 7 additions & 6 deletions test/parallel/test-fs-write-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,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();
}

0 comments on commit 67f5de9

Please sign in to comment.