Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const { Buffer } = require('buffer');
const {
copyObject,
getOptions,
getValidatedFd,
validatePath,
} = require('internal/fs/utils');
const { Readable, Writable, finished } = require('stream');
const { toPathIfFileURL } = require('internal/url');
Expand Down Expand Up @@ -119,7 +121,7 @@ function close(stream, err, cb) {

function importFd(stream, options) {
stream.fd = null;
if (options.fd) {
if (options.fd != null) {
if (typeof options.fd === 'number') {
// When fd is a raw descriptor, we must keep our fingers crossed
// that the descriptor won't get closed, or worse, replaced with
Expand Down Expand Up @@ -185,6 +187,13 @@ function ReadStream(path, options) {
this.pos = this.start;
}

// If fd has been set, validate, otherwise validate path.
if (this.fd != null) {
this.fd = getValidatedFd(this.fd);
} else {
validatePath(this.path);
}

if (this.end === undefined) {
this.end = Infinity;
} else if (this.end !== Infinity) {
Expand Down Expand Up @@ -344,6 +353,13 @@ function WriteStream(path, options) {
this.closed = false;
this[kIsPerformingIO] = false;

// If fd has been set, validate, otherwise validate path.
if (this.fd != null) {
this.fd = getValidatedFd(this.fd);
} else {
validatePath(this.path);
}

if (this.start !== undefined) {
validateInteger(this.start, 'start', 0);

Expand Down
49 changes: 49 additions & 0 deletions test/parallel/test-fs-stream-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';
require('../common');

const assert = require('assert');
const fs = require('fs');

{
const fd = 'k';

assert.throws(
() => {
fs.createReadStream(null, { fd });
},
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});

assert.throws(
() => {
fs.createWriteStream(null, { fd });
},
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});
}

{
const path = 46;

assert.throws(
() => {
fs.createReadStream(path);
},
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});

assert.throws(
() => {
fs.createWriteStream(path);
},
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});
}