@@ -153,6 +153,18 @@ function makeCallback(cb) {
153
153
} ;
154
154
}
155
155
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
+
156
168
// Special case of `makeCallback()` that is specific to async `*stat()` calls as
157
169
// an optimization, since the data passed back to the callback needs to be
158
170
// transformed anyway.
@@ -649,17 +661,14 @@ fs.readFileSync = function(path, options) {
649
661
} ;
650
662
651
663
fs . close = function ( fd , callback ) {
652
- if ( ! isUint32 ( fd ) )
653
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
654
-
664
+ validateFd ( fd ) ;
655
665
const req = new FSReqWrap ( ) ;
656
666
req . oncomplete = makeCallback ( callback ) ;
657
667
binding . close ( fd , req ) ;
658
668
} ;
659
669
660
670
fs . closeSync = function ( fd ) {
661
- if ( ! isUint32 ( fd ) )
662
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
671
+ validateFd ( fd ) ;
663
672
664
673
const ctx = { } ;
665
674
binding . close ( fd , undefined , ctx ) ;
@@ -720,8 +729,7 @@ fs.openSync = function(path, flags, mode) {
720
729
} ;
721
730
722
731
fs . 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 ) ;
725
733
if ( ! isUint8Array ( buffer ) )
726
734
throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'buffer' ,
727
735
[ 'Buffer' , 'Uint8Array' ] ) ;
@@ -759,8 +767,7 @@ Object.defineProperty(fs.read, internalUtil.customPromisifyArgs,
759
767
{ value : [ 'bytesRead' , 'buffer' ] , enumerable : false } ) ;
760
768
761
769
fs . 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 ) ;
764
771
if ( ! isUint8Array ( buffer ) )
765
772
throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'buffer' ,
766
773
[ 'Buffer' , 'Uint8Array' ] ) ;
@@ -794,8 +801,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
794
801
callback ( err , written || 0 , buffer ) ;
795
802
}
796
803
797
- if ( ! isUint32 ( fd ) )
798
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
804
+ validateFd ( fd ) ;
799
805
800
806
const req = new FSReqWrap ( ) ;
801
807
req . oncomplete = wrapper ;
@@ -839,8 +845,7 @@ Object.defineProperty(fs.write, internalUtil.customPromisifyArgs,
839
845
// OR
840
846
// fs.writeSync(fd, string[, position[, encoding]]);
841
847
fs . 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 ) ;
844
849
if ( isUint8Array ( buffer ) ) {
845
850
if ( position === undefined )
846
851
position = null ;
@@ -956,8 +961,7 @@ fs.ftruncate = function(fd, len = 0, callback) {
956
961
callback = len ;
957
962
len = 0 ;
958
963
}
959
- if ( ! isUint32 ( fd ) )
960
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
964
+ validateFd ( fd ) ;
961
965
if ( ! isInt32 ( len ) )
962
966
throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'len' , 'integer' ) ;
963
967
len = Math . max ( 0 , len ) ;
@@ -967,8 +971,7 @@ fs.ftruncate = function(fd, len = 0, callback) {
967
971
} ;
968
972
969
973
fs . ftruncateSync = function ( fd , len = 0 ) {
970
- if ( ! isUint32 ( fd ) )
971
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
974
+ validateFd ( fd ) ;
972
975
if ( ! isInt32 ( len ) )
973
976
throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'len' , 'integer' ) ;
974
977
len = Math . max ( 0 , len ) ;
@@ -1000,30 +1003,26 @@ fs.rmdirSync = function(path) {
1000
1003
} ;
1001
1004
1002
1005
fs . fdatasync = function ( fd , callback ) {
1003
- if ( ! isUint32 ( fd ) )
1004
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1006
+ validateFd ( fd ) ;
1005
1007
const req = new FSReqWrap ( ) ;
1006
1008
req . oncomplete = makeCallback ( callback ) ;
1007
1009
binding . fdatasync ( fd , req ) ;
1008
1010
} ;
1009
1011
1010
1012
fs . fdatasyncSync = function ( fd ) {
1011
- if ( ! isUint32 ( fd ) )
1012
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1013
+ validateFd ( fd ) ;
1013
1014
return binding . fdatasync ( fd ) ;
1014
1015
} ;
1015
1016
1016
1017
fs . fsync = function ( fd , callback ) {
1017
- if ( ! isUint32 ( fd ) )
1018
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1018
+ validateFd ( fd ) ;
1019
1019
const req = new FSReqWrap ( ) ;
1020
1020
req . oncomplete = makeCallback ( callback ) ;
1021
1021
binding . fsync ( fd , req ) ;
1022
1022
} ;
1023
1023
1024
1024
fs . fsyncSync = function ( fd ) {
1025
- if ( ! isUint32 ( fd ) )
1026
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1025
+ validateFd ( fd ) ;
1027
1026
return binding . fsync ( fd ) ;
1028
1027
} ;
1029
1028
@@ -1089,8 +1088,7 @@ fs.readdirSync = function(path, options) {
1089
1088
} ;
1090
1089
1091
1090
fs . fstat = function ( fd , callback ) {
1092
- if ( ! isUint32 ( fd ) )
1093
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1091
+ validateFd ( fd ) ;
1094
1092
const req = new FSReqWrap ( ) ;
1095
1093
req . oncomplete = makeStatsCallback ( callback ) ;
1096
1094
binding . fstat ( fd , req ) ;
@@ -1125,8 +1123,7 @@ fs.stat = function(path, callback) {
1125
1123
} ;
1126
1124
1127
1125
fs . fstatSync = function ( fd ) {
1128
- if ( ! isUint32 ( fd ) )
1129
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1126
+ validateFd ( fd ) ;
1130
1127
binding . fstat ( fd ) ;
1131
1128
return statsFromValues ( ) ;
1132
1129
} ;
@@ -1336,8 +1333,7 @@ fs.unlinkSync = function(path) {
1336
1333
1337
1334
fs . fchmod = function ( fd , mode , callback ) {
1338
1335
mode = modeNum ( mode ) ;
1339
- if ( ! isUint32 ( fd ) )
1340
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1336
+ validateFd ( fd ) ;
1341
1337
if ( ! isUint32 ( mode ) )
1342
1338
throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'mode' , 'integer' ) ;
1343
1339
if ( mode < 0 || mode > 0o777 )
@@ -1350,8 +1346,7 @@ fs.fchmod = function(fd, mode, callback) {
1350
1346
1351
1347
fs . fchmodSync = function ( fd , mode ) {
1352
1348
mode = modeNum ( mode ) ;
1353
- if ( ! isUint32 ( fd ) )
1354
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1349
+ validateFd ( fd ) ;
1355
1350
if ( ! isUint32 ( mode ) )
1356
1351
throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'mode' , 'integer' ) ;
1357
1352
if ( mode < 0 || mode > 0o777 )
@@ -1448,8 +1443,7 @@ if (constants.O_SYMLINK !== undefined) {
1448
1443
}
1449
1444
1450
1445
fs . fchown = function ( fd , uid , gid , callback ) {
1451
- if ( ! isUint32 ( fd ) )
1452
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1446
+ validateFd ( fd ) ;
1453
1447
if ( ! isUint32 ( uid ) )
1454
1448
throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'uid' , 'integer' ) ;
1455
1449
if ( ! isUint32 ( gid ) )
@@ -1461,8 +1455,7 @@ fs.fchown = function(fd, uid, gid, callback) {
1461
1455
} ;
1462
1456
1463
1457
fs . fchownSync = function ( fd , uid , gid ) {
1464
- if ( ! isUint32 ( fd ) )
1465
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1458
+ validateFd ( fd ) ;
1466
1459
if ( ! isUint32 ( uid ) )
1467
1460
throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'uid' , 'integer' ) ;
1468
1461
if ( ! isUint32 ( gid ) )
@@ -1562,8 +1555,7 @@ fs.utimesSync = function(path, atime, mtime) {
1562
1555
} ;
1563
1556
1564
1557
fs . futimes = function ( fd , atime , mtime , callback ) {
1565
- if ( ! isUint32 ( fd ) )
1566
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1558
+ validateFd ( fd ) ;
1567
1559
atime = toUnixTimestamp ( atime , 'atime' ) ;
1568
1560
mtime = toUnixTimestamp ( mtime , 'mtime' ) ;
1569
1561
const req = new FSReqWrap ( ) ;
@@ -1572,8 +1564,7 @@ fs.futimes = function(fd, atime, mtime, callback) {
1572
1564
} ;
1573
1565
1574
1566
fs . futimesSync = function ( fd , atime , mtime ) {
1575
- if ( ! isUint32 ( fd ) )
1576
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'fd' , 'integer' ) ;
1567
+ validateFd ( fd ) ;
1577
1568
atime = toUnixTimestamp ( atime , 'atime' ) ;
1578
1569
mtime = toUnixTimestamp ( mtime , 'mtime' ) ;
1579
1570
binding . futimes ( fd , atime , mtime ) ;
0 commit comments