Skip to content

Commit 0a01abb

Browse files
danielbayleyaduh95
authored andcommitted
lib: refactor platform utility methods
PR-URL: #53817 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Daeyeon Jeong <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Stephen Belanger <[email protected]>
1 parent fbc5cbb commit 0a01abb

File tree

11 files changed

+27
-34
lines changed

11 files changed

+27
-34
lines changed

lib/fs.js

+3-4
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
let FileReadStream;
167169
let FileWriteStream;
168170

169-
const isWindows = process.platform === 'win32';
170-
const isOSX = process.platform === 'darwin';
171-
172171
function showTruncateDeprecation() {
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
const nonNativeWatcher = require('internal/fs/recursive_watch');
24622461
watcher = new nonNativeWatcher.FSWatcher(options);
24632462
watcher[watchers.kFSWatchStart](path);

lib/internal/fs/glob.js

+3-4
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
return minimatch;
3638
}
3739

38-
const isWindows = process.platform === 'win32';
39-
const isOSX = 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

+4-5
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
const EventEmitter = require('events');
102104
const { StringDecoder } = require('string_decoder');
@@ -126,9 +128,6 @@ const {
126128
const getDirectoryEntriesPromise = promisify(getDirents);
127129
const validateRmOptionsPromise = promisify(validateRmOptions);
128130

129-
const isWindows = process.platform === 'win32';
130-
const isOSX = process.platform === 'darwin';
131-
132131
let cpPromises;
133132
function lazyLoadCpPromises() {
134133
return cpPromises ??= 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
const watcher = new nonNativeWatcher.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

+1-2
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
const notEmptyErrorCodes = new SafeSet(['ENOTEMPTY', 'EEXIST', 'EPERM']);
3535
const retryErrorCodes = new SafeSet(
3636
['EBUSY', 'EMFILE', 'ENFILE', 'ENOTEMPTY', 'EPERM']);
37-
const isWindows = process.platform === 'win32';
3837
const epermHandler = isWindows ? fixWinEPERM : _rmdir;
3938
const epermHandlerSync = isWindows ? fixWinEPERMSync : _rmdirSync;
4039
const readdirEncoding = 'buffer';

lib/internal/fs/utils.js

+1-2
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
const kMaxUserId = 2 ** 32 - 1;
147148

148-
const isWindows = process.platform === 'win32';
149-
150149
let fs;
151150
function lazyLoadFs() {
152151
if (!fs) {

lib/internal/modules/cjs/loader.js

+1-2
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
const onRequire = getLazy(() => tracingChannel('module.require'));
195196

196-
const isWindows = process.platform === 'win32';
197-
198197
const relativeResolveCache = { __proto__: null };
199198

200199
let requireDepth = 0;

lib/internal/modules/esm/translators.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const { fileURLToPath, pathToFileURL, URL } = require('internal/url');
5252
let debug = 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-
const isWindows = process.platform === 'win32';
420419
translators.set('json', function jsonStrategy(url, source) {
421420
emitExperimentalWarning('Importing JSON modules');
422421
assertBufferSource(source, true, 'load');

lib/internal/url.js

+2-4
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
const querystring = require('querystring');
8788

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

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

14721470
function getPathFromURLPosix(url) {
14731471
if (url.hostname !== '') {
1474-
throw new ERR_INVALID_FILE_URL_HOST(platform);
1472+
throw new ERR_INVALID_FILE_URL_HOST(process.platform);
14751473
}
14761474
const pathname = url.pathname;
14771475
for (let n = 0; n < pathname.length; n++) {

lib/internal/util.js

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ const { encodings } = internalBinding('string_decoder');
7171

7272
const noCrypto = !process.versions.openssl;
7373

74+
const isWindows = process.platform === 'win32';
75+
const isMacOS = process.platform === 'darwin';
76+
7477
const experimentalWarnings = new SafeSet();
7578

7679
const colorRegExp = /\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

+1-3
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
const DEFAULT_IPV4_ADDR = '0.0.0.0';
143143
const DEFAULT_IPV6_ADDR = '::';
144144

145-
const isWindows = process.platform === 'win32';
146-
147145
const noop = () => {};
148146

149147
const kPerfHooksNetConnectContext = Symbol('kPerfHooksNetConnectContext');

lib/path.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,12 @@ const {
5050
const {
5151
getLazy,
5252
emitExperimentalWarning,
53+
isWindows,
54+
isMacOS,
5355
} = require('internal/util');
5456

5557
const lazyMinimatch = getLazy(() => require('internal/deps/minimatch/index'));
5658

57-
const platformIsWin32 = (process.platform === 'win32');
58-
const platformIsOSX = (process.platform === 'darwin');
59-
6059
function isPathSeparator(code) {
6160
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
6261
}
@@ -167,7 +166,7 @@ function glob(path, pattern, windows) {
167166
validateString(pattern, 'pattern');
168167
return lazyMinimatch().minimatch(path, pattern, {
169168
__proto__: null,
170-
nocase: platformIsOSX || platformIsWin32,
169+
nocase: isMacOS || isWindows,
171170
windowsPathsNoEscape: true,
172171
nonegate: true,
173172
nocomment: true,
@@ -1100,7 +1099,7 @@ const win32 = {
11001099
};
11011100

11021101
const posixCwd = (() => {
1103-
if (platformIsWin32) {
1102+
if (isWindows) {
11041103
// Converts Windows' backslash path separators to POSIX forward slashes
11051104
// and truncates any drive indicator
11061105
const regexp = /\\/g;
@@ -1575,4 +1574,4 @@ posix.posix = win32.posix = posix;
15751574
win32._makeLong = win32.toNamespacedPath;
15761575
posix._makeLong = posix.toNamespacedPath;
15771576

1578-
module.exports = platformIsWin32 ? win32 : posix;
1577+
module.exports = isWindows ? win32 : posix;

0 commit comments

Comments
 (0)