Skip to content

Commit dc30d36

Browse files
jasnellMylesBorins
authored andcommitted
fs: consistent constants use and cleanup
* only require('buffer') once * use constants directly * fix incorrect constants PR-URL: #20765 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ron Korving <ron@ronkorving.nl> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 657f8cb commit dc30d36

1 file changed

Lines changed: 37 additions & 22 deletions

File tree

‎lib/fs.js‎

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,31 @@
2424

2525
'use strict';
2626

27-
constconstants=process.binding('constants').fs;
28-
const{S_IFIFO,S_IFLNK,S_IFMT,S_IFREG,S_IFSOCK}=constants;
27+
const{fs: constants}=process.binding('constants');
28+
const{
29+
S_IFIFO,
30+
S_IFLNK,
31+
S_IFMT,
32+
S_IFREG,
33+
S_IFSOCK,
34+
F_OK,
35+
R_OK,
36+
W_OK,
37+
X_OK,
38+
O_WRONLY,
39+
O_SYMLINK,
40+
UV_FS_COPYFILE_EXCL,
41+
UV_FS_COPYFILE_FICLONE,
42+
UV_FS_COPYFILE_FICLONE_FORCE
43+
}=constants;
2944
constutil=require('util');
3045
constpathModule=require('path');
3146
const{ isUint8Array }=require('internal/util/types');
3247
const{ createPromise, promiseResolve }=process.binding('util');
3348

3449
constbinding=process.binding('fs');
3550
constfs=exports;
36-
const{ Buffer }=require('buffer');
51+
const{ Buffer, kMaxLength}=require('buffer');
3752
consterrors=require('internal/errors');
3853
const{
3954
ERR_FS_FILE_TOO_LARGE,
@@ -58,6 +73,7 @@ const {
5873
preprocessSymlinkDestination,
5974
Stats,
6075
getStatsFromBinding,
76+
realpathCacheKey,
6177
stringToFlags,
6278
stringToSymlinkType,
6379
toUnixTimestamp,
@@ -103,7 +119,6 @@ function lazyAssert() {
103119
}
104120

105121
constkMinPoolSpace=128;
106-
const{ kMaxLength }=require('buffer');
107122

108123
constisWindows=process.platform==='win32';
109124

@@ -179,16 +194,16 @@ function isFileType(stats, fileType) {
179194

180195
// Don't allow mode to accidentally be overwritten.
181196
Object.defineProperties(fs,{
182-
F_OK: {enumerable: true,value: constants.F_OK||0},
183-
R_OK: {enumerable: true,value: constants.R_OK||0},
184-
W_OK: {enumerable: true,value: constants.W_OK||0},
185-
X_OK: {enumerable: true,value: constants.X_OK||0},
197+
F_OK: {enumerable: true,value: F_OK||0},
198+
R_OK: {enumerable: true,value: R_OK||0},
199+
W_OK: {enumerable: true,value: W_OK||0},
200+
X_OK: {enumerable: true,value: X_OK||0},
186201
});
187202

188203
fs.access=function(path,mode,callback){
189204
if(typeofmode==='function'){
190205
callback=mode;
191-
mode=fs.F_OK;
206+
mode=F_OK;
192207
}
193208

194209
path=getPathFromURL(path);
@@ -205,7 +220,7 @@ fs.accessSync = function(path, mode) {
205220
validatePath(path);
206221

207222
if(mode===undefined)
208-
mode=fs.F_OK;
223+
mode=F_OK;
209224
else
210225
mode=mode|0;
211226

@@ -222,7 +237,7 @@ fs.exists = function(path, callback) {
222237
}
223238

224239
try{
225-
fs.access(path,fs.FS_OK,suppressedCallback);
240+
fs.access(path,F_OK,suppressedCallback);
226241
}catch(err){
227242
returncallback(false);
228243
}
@@ -244,7 +259,7 @@ Object.defineProperty(fs.exists, internalUtil.promisify.custom, {
244259
// TODO(joyeecheung): deprecate the never-throw-on-invalid-arguments behavior
245260
fs.existsSync=function(path){
246261
try{
247-
fs.accessSync(path,fs.FS_OK);
262+
fs.accessSync(path,F_OK);
248263
returntrue;
249264
}catch(e){
250265
returnfalse;
@@ -1054,10 +1069,10 @@ fs.fchmodSync = function(fd, mode) {
10541069
handleErrorFromBinding(ctx);
10551070
};
10561071

1057-
if(constants.O_SYMLINK!==undefined){
1072+
if(O_SYMLINK!==undefined){
10581073
fs.lchmod=function(path,mode,callback){
10591074
callback=maybeCallback(callback);
1060-
fs.open(path,constants.O_WRONLY|constants.O_SYMLINK,function(err,fd){
1075+
fs.open(path,O_WRONLY|O_SYMLINK,function(err,fd){
10611076
if(err){
10621077
callback(err);
10631078
return;
@@ -1073,7 +1088,7 @@ if (constants.O_SYMLINK !== undefined) {
10731088
};
10741089

10751090
fs.lchmodSync=function(path,mode){
1076-
constfd=fs.openSync(path,constants.O_WRONLY|constants.O_SYMLINK);
1091+
constfd=fs.openSync(path,O_WRONLY|O_SYMLINK);
10771092

10781093
// Prefer to return the chmod error, if one occurs,
10791094
// but still try to close, and report closing errors if they occur.
@@ -1110,10 +1125,10 @@ fs.chmodSync = function(path, mode) {
11101125
handleErrorFromBinding(ctx);
11111126
};
11121127

1113-
if(constants.O_SYMLINK!==undefined){
1128+
if(O_SYMLINK!==undefined){
11141129
fs.lchown=function(path,uid,gid,callback){
11151130
callback=maybeCallback(callback);
1116-
fs.open(path,constants.O_WRONLY|constants.O_SYMLINK,function(err,fd){
1131+
fs.open(path,O_WRONLY|O_SYMLINK,function(err,fd){
11171132
if(err){
11181133
callback(err);
11191134
return;
@@ -1129,7 +1144,7 @@ if (constants.O_SYMLINK !== undefined) {
11291144
};
11301145

11311146
fs.lchownSync=function(path,uid,gid){
1132-
constfd=fs.openSync(path,constants.O_WRONLY|constants.O_SYMLINK);
1147+
constfd=fs.openSync(path,O_WRONLY|O_SYMLINK);
11331148
letret;
11341149
try{
11351150
ret=fs.fchownSync(fd,uid,gid);
@@ -1631,7 +1646,7 @@ fs.realpathSync = function realpathSync(p, options) {
16311646
validatePath(p);
16321647
p=pathModule.resolve(p);
16331648

1634-
constcache=options[internalFS.realpathCacheKey];
1649+
constcache=options[realpathCacheKey];
16351650
constmaybeCachedResult=cache&&cache.get(p);
16361651
if(maybeCachedResult){
16371652
returnmaybeCachedResult;
@@ -1936,14 +1951,14 @@ fs.mkdtempSync = function(prefix, options) {
19361951

19371952
// Define copyFile() flags.
19381953
Object.defineProperties(fs.constants,{
1939-
COPYFILE_EXCL: {enumerable: true,value: constants.UV_FS_COPYFILE_EXCL},
1954+
COPYFILE_EXCL: {enumerable: true,value: UV_FS_COPYFILE_EXCL},
19401955
COPYFILE_FICLONE: {
19411956
enumerable: true,
1942-
value: constants.UV_FS_COPYFILE_FICLONE
1957+
value: UV_FS_COPYFILE_FICLONE
19431958
},
19441959
COPYFILE_FICLONE_FORCE: {
19451960
enumerable: true,
1946-
value: constants.UV_FS_COPYFILE_FICLONE_FORCE
1961+
value: UV_FS_COPYFILE_FICLONE_FORCE
19471962
}
19481963
});
19491964

0 commit comments

Comments
 (0)