@@ -153,6 +153,18 @@ function makeCallback(cb) {
153153 } ;
154154}
155155
156+ function validateFd ( fd ) {
157+ let err ;
158+
159+ if ( ! isUint32 ( fd ) )
160+ err = new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
161+
162+ if ( err !== undefined ) {
163+ Error . captureStackTrace ( err , validateFd ) ;
164+ throw err ;
165+ }
166+ }
167+
156168// Special case of `makeCallback()` that is specific to async `*stat()` calls as
157169// an optimization, since the data passed back to the callback needs to be
158170// transformed anyway.
@@ -649,17 +661,14 @@ fs.readFileSync = function(path, options) {
649661} ;
650662
651663fs . close = function ( fd , callback ) {
652- if ( ! isUint32 ( fd ) )
653- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
654-
664+ validateFd ( fd ) ;
655665 const req = new FSReqWrap ( ) ;
656666 req . oncomplete = makeCallback ( callback ) ;
657667 binding . close ( fd , req ) ;
658668} ;
659669
660670fs . closeSync = function ( fd ) {
661- if ( ! isUint32 ( fd ) )
662- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
671+ validateFd ( fd ) ;
663672
664673 const ctx = { } ;
665674 binding . close ( fd , undefined , ctx ) ;
@@ -720,8 +729,7 @@ fs.openSync = function(path, flags, mode) {
720729} ;
721730
722731fs . read = function ( fd , buffer , offset , length , position , callback ) {
723- if ( ! isUint32 ( fd ) )
724- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
732+ validateFd ( fd ) ;
725733 if ( ! isUint8Array ( buffer ) )
726734 throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'buffer' ,
727735 [ 'Buffer' , 'Uint8Array' ] ) ;
@@ -759,8 +767,7 @@ Object.defineProperty(fs.read, internalUtil.customPromisifyArgs,
759767 { value : [ 'bytesRead' , 'buffer' ] , enumerable : false } ) ;
760768
761769fs . readSync = function ( fd , buffer , offset , length , position ) {
762- if ( ! isUint32 ( fd ) )
763- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
770+ validateFd ( fd ) ;
764771 if ( ! isUint8Array ( buffer ) )
765772 throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'buffer' ,
766773 [ 'Buffer' , 'Uint8Array' ] ) ;
@@ -794,8 +801,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
794801 callback ( err , written || 0 , buffer ) ;
795802 }
796803
797- if ( ! isUint32 ( fd ) )
798- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
804+ validateFd ( fd ) ;
799805
800806 const req = new FSReqWrap ( ) ;
801807 req . oncomplete = wrapper ;
@@ -839,8 +845,7 @@ Object.defineProperty(fs.write, internalUtil.customPromisifyArgs,
839845// OR
840846// fs.writeSync(fd, string[, position[, encoding]]);
841847fs . writeSync = function ( fd , buffer , offset , length , position ) {
842- if ( ! isUint32 ( fd ) )
843- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
848+ validateFd ( fd ) ;
844849 if ( isUint8Array ( buffer ) ) {
845850 if ( position === undefined )
846851 position = null ;
@@ -956,8 +961,7 @@ fs.ftruncate = function(fd, len = 0, callback) {
956961 callback = len ;
957962 len = 0 ;
958963 }
959- if ( ! isUint32 ( fd ) )
960- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
964+ validateFd ( fd ) ;
961965 if ( ! isInt32 ( len ) )
962966 throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'len' , 'integer' ) ;
963967 len = Math . max ( 0 , len ) ;
@@ -967,8 +971,7 @@ fs.ftruncate = function(fd, len = 0, callback) {
967971} ;
968972
969973fs . ftruncateSync = function ( fd , len = 0 ) {
970- if ( ! isUint32 ( fd ) )
971- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
974+ validateFd ( fd ) ;
972975 if ( ! isInt32 ( len ) )
973976 throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'len' , 'integer' ) ;
974977 len = Math . max ( 0 , len ) ;
@@ -1000,30 +1003,26 @@ fs.rmdirSync = function(path) {
10001003} ;
10011004
10021005fs . fdatasync = function ( fd , callback ) {
1003- if ( ! isUint32 ( fd ) )
1004- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1006+ validateFd ( fd ) ;
10051007 const req = new FSReqWrap ( ) ;
10061008 req . oncomplete = makeCallback ( callback ) ;
10071009 binding . fdatasync ( fd , req ) ;
10081010} ;
10091011
10101012fs . fdatasyncSync = function ( fd ) {
1011- if ( ! isUint32 ( fd ) )
1012- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1013+ validateFd ( fd ) ;
10131014 return binding . fdatasync ( fd ) ;
10141015} ;
10151016
10161017fs . fsync = function ( fd , callback ) {
1017- if ( ! isUint32 ( fd ) )
1018- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1018+ validateFd ( fd ) ;
10191019 const req = new FSReqWrap ( ) ;
10201020 req . oncomplete = makeCallback ( callback ) ;
10211021 binding . fsync ( fd , req ) ;
10221022} ;
10231023
10241024fs . fsyncSync = function ( fd ) {
1025- if ( ! isUint32 ( fd ) )
1026- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1025+ validateFd ( fd ) ;
10271026 return binding . fsync ( fd ) ;
10281027} ;
10291028
@@ -1089,8 +1088,7 @@ fs.readdirSync = function(path, options) {
10891088} ;
10901089
10911090fs . fstat = function ( fd , callback ) {
1092- if ( ! isUint32 ( fd ) )
1093- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1091+ validateFd ( fd ) ;
10941092 const req = new FSReqWrap ( ) ;
10951093 req . oncomplete = makeStatsCallback ( callback ) ;
10961094 binding . fstat ( fd , req ) ;
@@ -1125,8 +1123,7 @@ fs.stat = function(path, callback) {
11251123} ;
11261124
11271125fs . fstatSync = function ( fd ) {
1128- if ( ! isUint32 ( fd ) )
1129- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1126+ validateFd ( fd ) ;
11301127 binding . fstat ( fd ) ;
11311128 return statsFromValues ( ) ;
11321129} ;
@@ -1336,8 +1333,7 @@ fs.unlinkSync = function(path) {
13361333
13371334fs . fchmod = function ( fd , mode , callback ) {
13381335 mode = modeNum ( mode ) ;
1339- if ( ! isUint32 ( fd ) )
1340- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1336+ validateFd ( fd ) ;
13411337 if ( ! isUint32 ( mode ) )
13421338 throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'mode' , 'integer' ) ;
13431339 if ( mode < 0 || mode > 0o777 )
@@ -1350,8 +1346,7 @@ fs.fchmod = function(fd, mode, callback) {
13501346
13511347fs . fchmodSync = function ( fd , mode ) {
13521348 mode = modeNum ( mode ) ;
1353- if ( ! isUint32 ( fd ) )
1354- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1349+ validateFd ( fd ) ;
13551350 if ( ! isUint32 ( mode ) )
13561351 throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'mode' , 'integer' ) ;
13571352 if ( mode < 0 || mode > 0o777 )
@@ -1448,8 +1443,7 @@ if (constants.O_SYMLINK !== undefined) {
14481443}
14491444
14501445fs . fchown = function ( fd , uid , gid , callback ) {
1451- if ( ! isUint32 ( fd ) )
1452- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1446+ validateFd ( fd ) ;
14531447 if ( ! isUint32 ( uid ) )
14541448 throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'uid' , 'integer' ) ;
14551449 if ( ! isUint32 ( gid ) )
@@ -1461,8 +1455,7 @@ fs.fchown = function(fd, uid, gid, callback) {
14611455} ;
14621456
14631457fs . fchownSync = function ( fd , uid , gid ) {
1464- if ( ! isUint32 ( fd ) )
1465- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1458+ validateFd ( fd ) ;
14661459 if ( ! isUint32 ( uid ) )
14671460 throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'uid' , 'integer' ) ;
14681461 if ( ! isUint32 ( gid ) )
@@ -1562,8 +1555,7 @@ fs.utimesSync = function(path, atime, mtime) {
15621555} ;
15631556
15641557fs . futimes = function ( fd , atime , mtime , callback ) {
1565- if ( ! isUint32 ( fd ) )
1566- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1558+ validateFd ( fd ) ;
15671559 atime = toUnixTimestamp ( atime , 'atime' ) ;
15681560 mtime = toUnixTimestamp ( mtime , 'mtime' ) ;
15691561 const req = new FSReqWrap ( ) ;
@@ -1572,8 +1564,7 @@ fs.futimes = function(fd, atime, mtime, callback) {
15721564} ;
15731565
15741566fs . futimesSync = function ( fd , atime , mtime ) {
1575- if ( ! isUint32 ( fd ) )
1576- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1567+ validateFd ( fd ) ;
15771568 atime = toUnixTimestamp ( atime , 'atime' ) ;
15781569 mtime = toUnixTimestamp ( mtime , 'mtime' ) ;
15791570 binding . futimes ( fd , atime , mtime ) ;
0 commit comments