From 71ad3c6107d3dabd674cb34cc4aa21544be1f24a Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Thu, 8 Aug 2019 11:18:39 +0200 Subject: [PATCH 1/2] fs: remove unnecessary argument check --- lib/internal/fs/streams.js | 9 +++------ test/parallel/test-fs-write-stream.js | 15 +-------------- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index d51fbb1b580113..d923e8ab6b23fa 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'); @@ -238,6 +237,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; @@ -298,11 +300,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 5e75f4a0b306d2..6d263f450b6a4b 100644 --- a/test/parallel/test-fs-write-stream.js +++ b/test/parallel/test-fs-write-stream.js @@ -20,7 +20,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -const common = require('../common'); +require('../common'); const assert = require('assert'); const path = require('path'); const fs = require('fs'); @@ -52,16 +52,3 @@ tmpdir.refresh(); }); stream.destroy(); } - -// Throws if data is not of type Buffer. -{ - const stream = fs.createWriteStream(file); - common.expectsError(() => { - stream._write(42, null, function() {}); - }, { - code: 'ERR_INVALID_ARG_TYPE', - type: TypeError, - message: 'The "data" argument must be of type Buffer. Received type number' - }); - stream.destroy(); -} From 3b53003b6dfc470220bd0af7d8b5304f175490b5 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 26 Aug 2019 20:21:22 +0200 Subject: [PATCH 2/2] fixup --- test/parallel/test-fs-write-stream.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-fs-write-stream.js b/test/parallel/test-fs-write-stream.js index 6d263f450b6a4b..18374f44ebcdad 100644 --- a/test/parallel/test-fs-write-stream.js +++ b/test/parallel/test-fs-write-stream.js @@ -20,7 +20,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const path = require('path'); const fs = require('fs'); @@ -52,3 +52,17 @@ tmpdir.refresh(); }); stream.destroy(); } + +// Throws if data is not of type Buffer. +{ + const stream = fs.createWriteStream(file); + stream.on('error', common.expectsError({ + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError + })); + stream.write(42, null, common.expectsError({ + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError + })); + stream.destroy(); +}