From 67f5de9b34f33f43262d918f4cb4dd66ad3a18b7 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Thu, 8 Aug 2019 11:18:39 +0200 Subject: [PATCH] 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 Reviewed-By: Luigi Pinca Reviewed-By: Rich Trott --- lib/internal/fs/streams.js | 9 +++------ test/parallel/test-fs-write-stream.js | 13 +++++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index b0c4224893e9a6..110ad114930fca 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -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); diff --git a/test/parallel/test-fs-write-stream.js b/test/parallel/test-fs-write-stream.js index e93f65e604c20b..d0364ee8f07641 100644 --- a/test/parallel/test-fs-write-stream.js +++ b/test/parallel/test-fs-write-stream.js @@ -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(); }