Skip to content

Commit 0a01abb

Browse files
danielbayleyaduh95
authored andcommitted
lib: refactor platform utility methods
PR-URL: #53817 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent fbc5cbb commit 0a01abb

11 files changed

Lines changed: 27 additions & 34 deletions

File tree

‎lib/fs.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ const {
9494
},
9595
SideEffectFreeRegExpPrototypeExec,
9696
defineLazyProperties,
97+
isWindows,
98+
isMacOS,
9799
}=require('internal/util');
98100
const{
99101
constants: {
@@ -166,9 +168,6 @@ let kResistStopPropagation;
166168
letFileReadStream;
167169
letFileWriteStream;
168170

169-
constisWindows=process.platform==='win32';
170-
constisOSX=process.platform==='darwin';
171-
172171
functionshowTruncateDeprecation(){
173172
if(truncateWarn){
174173
process.emitWarning(
@@ -2457,7 +2456,7 @@ function watch(filename, options, listener) {
24572456
// TODO(anonrig): Remove non-native watcher when/if libuv supports recursive.
24582457
// As of November 2022, libuv does not support recursive file watch on all platforms,
24592458
// e.g. Linux due to the limitations of inotify.
2460-
if(options.recursive&&!isOSX&&!isWindows){
2459+
if(options.recursive&&!isMacOS&&!isWindows){
24612460
constnonNativeWatcher=require('internal/fs/recursive_watch');
24622461
watcher=newnonNativeWatcher.FSWatcher(options);
24632462
watcher[watchers.kFSWatchStart](path);

‎lib/internal/fs/glob.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const { join, resolve, basename, isAbsolute } = require('path');
2020

2121
const{
2222
kEmptyObject,
23+
isWindows,
24+
isMacOS,
2325
}=require('internal/util');
2426
const{
2527
validateFunction,
@@ -35,9 +37,6 @@ function lazyMinimatch() {
3537
returnminimatch;
3638
}
3739

38-
constisWindows=process.platform==='win32';
39-
constisOSX=process.platform==='darwin';
40-
4140
/**
4241
* @param {string} path
4342
* @returns {Promise<DirentFromStats|null>}
@@ -217,7 +216,7 @@ class Glob {
217216
}
218217
this.matchers=ArrayPrototypeMap(patterns,(pattern)=>new(lazyMinimatch().Minimatch)(pattern,{
219218
__proto__: null,
220-
nocase: isWindows||isOSX,
219+
nocase: isWindows||isMacOS,
221220
windowsPathsNoEscape: true,
222221
nonegate: true,
223222
nocomment: true,

‎lib/internal/fs/promises.js‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ const {
9797
kEmptyObject,
9898
lazyDOMException,
9999
promisify,
100+
isWindows,
101+
isMacOS,
100102
}=require('internal/util');
101103
constEventEmitter=require('events');
102104
const{ StringDecoder }=require('string_decoder');
@@ -126,9 +128,6 @@ const {
126128
constgetDirectoryEntriesPromise=promisify(getDirents);
127129
constvalidateRmOptionsPromise=promisify(validateRmOptions);
128130

129-
constisWindows=process.platform==='win32';
130-
constisOSX=process.platform==='darwin';
131-
132131
letcpPromises;
133132
functionlazyLoadCpPromises(){
134133
returncpPromises??=require('internal/fs/cp/cp').cpFn;
@@ -1256,7 +1255,7 @@ async function* _watch(filename, options = kEmptyObject) {
12561255
// TODO(anonrig): Remove non-native watcher when/if libuv supports recursive.
12571256
// As of November 2022, libuv does not support recursive file watch on all platforms,
12581257
// e.g. Linux due to the limitations of inotify.
1259-
if(options.recursive&&!isOSX&&!isWindows){
1258+
if(options.recursive&&!isMacOS&&!isWindows){
12601259
constwatcher=newnonNativeWatcher.FSWatcher(options);
12611260
watcher[kFSWatchStart](filename);
12621261
yield*watcher;
@@ -1306,7 +1305,7 @@ module.exports = {
13061305
writeFile,
13071306
appendFile,
13081307
readFile,
1309-
watch: !isOSX&&!isWindows ? _watch : watch,
1308+
watch: !isMacOS&&!isWindows ? _watch : watch,
13101309
constants,
13111310
},
13121311

‎lib/internal/fs/rimraf.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ const {
3030
}=fs;
3131
const{ sep }=require('path');
3232
const{ setTimeout }=require('timers');
33-
const{ sleep }=require('internal/util');
33+
const{ sleep, isWindows}=require('internal/util');
3434
constnotEmptyErrorCodes=newSafeSet(['ENOTEMPTY','EEXIST','EPERM']);
3535
constretryErrorCodes=newSafeSet(
3636
['EBUSY','EMFILE','ENFILE','ENOTEMPTY','EPERM']);
37-
constisWindows=process.platform==='win32';
3837
constepermHandler=isWindows ? fixWinEPERM : _rmdir;
3938
constepermHandlerSync=isWindows ? fixWinEPERMSync : _rmdirSync;
4039
constreaddirEncoding='buffer';

‎lib/internal/fs/utils.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const {
4949
kEmptyObject,
5050
once,
5151
deprecate,
52+
isWindows,
5253
}=require('internal/util');
5354
const{ toPathIfFileURL }=require('internal/url');
5455
const{
@@ -145,8 +146,6 @@ const kWriteFileMaxChunkSize = 512 * 1024;
145146

146147
constkMaxUserId=2**32-1;
147148

148-
constisWindows=process.platform==='win32';
149-
150149
letfs;
151150
functionlazyLoadFs(){
152151
if(!fs){

‎lib/internal/modules/cjs/loader.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ const {
127127
kEmptyObject,
128128
setOwnProperty,
129129
getLazy,
130+
isWindows,
130131
}=require('internal/util');
131132
const{
132133
makeContextifyScript,
@@ -193,8 +194,6 @@ let { startTimer, endTimer } = debugWithTimer('module_timer', (start, end) => {
193194
const{ tracingChannel }=require('diagnostics_channel');
194195
constonRequire=getLazy(()=>tracingChannel('module.require'));
195196

196-
constisWindows=process.platform==='win32';
197-
198197
constrelativeResolveCache={__proto__: null};
199198

200199
letrequireDepth=0;

‎lib/internal/modules/esm/translators.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const { fileURLToPath, pathToFileURL, URL } = require('internal/url');
5252
letdebug=require('internal/util/debuglog').debuglog('esm',(fn)=>{
5353
debug=fn;
5454
});
55-
const{ emitExperimentalWarning, kEmptyObject, setOwnProperty }=require('internal/util');
55+
const{ emitExperimentalWarning, kEmptyObject, setOwnProperty, isWindows}=require('internal/util');
5656
const{
5757
ERR_UNKNOWN_BUILTIN_MODULE,
5858
ERR_INVALID_RETURN_PROPERTY_VALUE,
@@ -416,7 +416,6 @@ translators.set('builtin', function builtinStrategy(url) {
416416
});
417417

418418
// Strategy for loading a JSON file
419-
constisWindows=process.platform==='win32';
420419
translators.set('json',functionjsonStrategy(url,source){
421420
emitExperimentalWarning('Importing JSON modules');
422421
assertBufferSource(source,true,'load');

‎lib/internal/url.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const {
4646
kEnumerableProperty,
4747
kEmptyObject,
4848
SideEffectFreeRegExpPrototypeSymbolReplace,
49+
isWindows,
4950
}=require('internal/util');
5051

5152
const{
@@ -85,9 +86,6 @@ const {
8586

8687
constquerystring=require('querystring');
8788

88-
const{ platform }=process;
89-
constisWindows=platform==='win32';
90-
9189
constbindingUrl=internalBinding('url');
9290

9391
constFORWARD_SLASH=/\//g;
@@ -1471,7 +1469,7 @@ function getPathFromURLWin32(url) {
14711469

14721470
functiongetPathFromURLPosix(url){
14731471
if(url.hostname!==''){
1474-
thrownewERR_INVALID_FILE_URL_HOST(platform);
1472+
thrownewERR_INVALID_FILE_URL_HOST(process.platform);
14751473
}
14761474
constpathname=url.pathname;
14771475
for(letn=0;n<pathname.length;n++){

‎lib/internal/util.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ const { encodings } = internalBinding('string_decoder');
7171

7272
constnoCrypto=!process.versions.openssl;
7373

74+
constisWindows=process.platform==='win32';
75+
constisMacOS=process.platform==='darwin';
76+
7477
constexperimentalWarnings=newSafeSet();
7578

7679
constcolorRegExp=/\u001b\[\d\d?m/g;// eslint-disable-line no-control-regex
@@ -917,6 +920,8 @@ module.exports = {
917920
isArrayBufferDetached,
918921
isError,
919922
isInsideNodeModules,
923+
isMacOS,
924+
isWindows,
920925
join,
921926
lazyDOMException,
922927
lazyDOMExceptionClass,

‎lib/net.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const {
115115
}=require('internal/errors');
116116
const{ isUint8Array }=require('internal/util/types');
117117
const{ queueMicrotask }=require('internal/process/task_queues');
118-
const{ kEmptyObject, guessHandleType, promisify }=require('internal/util');
118+
const{ kEmptyObject, guessHandleType, promisify, isWindows}=require('internal/util');
119119
const{
120120
validateAbortSignal,
121121
validateBoolean,
@@ -142,8 +142,6 @@ const { kTimeout } = require('internal/timers');
142142
constDEFAULT_IPV4_ADDR='0.0.0.0';
143143
constDEFAULT_IPV6_ADDR='::';
144144

145-
constisWindows=process.platform==='win32';
146-
147145
constnoop=()=>{};
148146

149147
constkPerfHooksNetConnectContext=Symbol('kPerfHooksNetConnectContext');

0 commit comments

Comments
 (0)