Skip to content

Commit 67f5de9

Browse files
ronagBridgeAR
authored andcommitted
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: #29043 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent b43d2dd commit 67f5de9

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

lib/internal/fs/streams.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const { Math, Object } = primordials;
44

55
const {
6-
ERR_INVALID_ARG_TYPE,
76
ERR_OUT_OF_RANGE
87
} = require('internal/errors').codes;
98
const { validateNumber } = require('internal/validators');
@@ -235,6 +234,9 @@ function WriteStream(path, options) {
235234

236235
options = copyObject(getOptions(options, {}));
237236

237+
// Only buffers are supported.
238+
options.decodeStrings = true;
239+
238240
// For backwards compat do not emit close on destroy.
239241
if (options.emitClose === undefined) {
240242
options.emitClose = false;
@@ -295,11 +297,6 @@ WriteStream.prototype.open = function() {
295297

296298

297299
WriteStream.prototype._write = function(data, encoding, cb) {
298-
if (!(data instanceof Buffer)) {
299-
const err = new ERR_INVALID_ARG_TYPE('data', 'Buffer', data);
300-
return this.emit('error', err);
301-
}
302-
303300
if (typeof this.fd !== 'number') {
304301
return this.once('open', function() {
305302
this._write(data, encoding, cb);

test/parallel/test-fs-write-stream.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ tmpdir.refresh();
5555
// Throws if data is not of type Buffer.
5656
{
5757
const stream = fs.createWriteStream(file);
58-
common.expectsError(() => {
59-
stream._write(42, null, function() {});
60-
}, {
58+
stream.on('error', common.expectsError({
6159
code: 'ERR_INVALID_ARG_TYPE',
62-
type: TypeError,
63-
message: 'The "data" argument must be of type Buffer. Received type number'
64-
});
60+
type: TypeError
61+
}));
62+
stream.write(42, null, common.expectsError({
63+
code: 'ERR_INVALID_ARG_TYPE',
64+
type: TypeError
65+
}));
6566
stream.destroy();
6667
}

0 commit comments

Comments
 (0)