Skip to content

Commit aadaf03

Browse files
danielbayleyehsankhfr
authored andcommitted
lib: refactor platform utility methods
PR-URL: nodejs#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 c2491a1 commit aadaf03

File tree

11 files changed

+27
-34
lines changed

11 files changed

+27
-34
lines changed

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: {
@@ -167,9 +169,6 @@ let kResistStopPropagation;
167169
let FileReadStream;
168170
let FileWriteStream;
169171

170-
const isWindows = process.platform === 'win32';
171-
const isOSX = process.platform === 'darwin';
172-
173172
function showTruncateDeprecation() {
174173
if (truncateWarn) {
175174
process.emitWarning(
@@ -2462,7 +2461,7 @@ function watch(filename, options, listener) {
24622461
// TODO(anonrig): Remove non-native watcher when/if libuv supports recursive.
24632462
// As of November 2022, libuv does not support recursive file watch on all platforms,
24642463
// e.g. Linux due to the limitations of inotify.
2465-
if (options.recursive && !isOSX && !isWindows) {
2464+
if (options.recursive && !isMacOS && !isWindows) {
24662465
const nonNativeWatcher = require('internal/fs/recursive_watch');
24672466
watcher = new nonNativeWatcher.FSWatcher(options);
24682467
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
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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ const {
9898
kEmptyObject,
9999
lazyDOMException,
100100
promisify,
101+
isWindows,
102+
isMacOS,
101103
} = require('internal/util');
102104
const EventEmitter = require('events');
103105
const { StringDecoder } = require('string_decoder');
@@ -127,9 +129,6 @@ const {
127129
const getDirectoryEntriesPromise = promisify(getDirents);
128130
const validateRmOptionsPromise = promisify(validateRmOptions);
129131

130-
const isWindows = process.platform === 'win32';
131-
const isOSX = process.platform === 'darwin';
132-
133132
let cpPromises;
134133
function lazyLoadCpPromises() {
135134
return cpPromises ??= require('internal/fs/cp/cp').cpFn;
@@ -1257,7 +1256,7 @@ async function* _watch(filename, options = kEmptyObject) {
12571256
// TODO(anonrig): Remove non-native watcher when/if libuv supports recursive.
12581257
// As of November 2022, libuv does not support recursive file watch on all platforms,
12591258
// e.g. Linux due to the limitations of inotify.
1260-
if (options.recursive && !isOSX && !isWindows) {
1259+
if (options.recursive && !isMacOS && !isWindows) {
12611260
const watcher = new nonNativeWatcher.FSWatcher(options);
12621261
watcher[kFSWatchStart](filename);
12631262
yield* watcher;
@@ -1307,7 +1306,7 @@ module.exports = {
13071306
writeFile,
13081307
appendFile,
13091308
readFile,
1310-
watch: !isOSX && !isWindows ? _watch : watch,
1309+
watch: !isMacOS && !isWindows ? _watch : watch,
13111310
constants,
13121311
},
13131312

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
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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const {
4848
kEmptyObject,
4949
once,
5050
deprecate,
51+
isWindows,
5152
} = require('internal/util');
5253
const { toPathIfFileURL } = require('internal/url');
5354
const {
@@ -144,8 +145,6 @@ const kWriteFileMaxChunkSize = 512 * 1024;
144145

145146
const kMaxUserId = 2 ** 32 - 1;
146147

147-
const isWindows = process.platform === 'win32';
148-
149148
let fs;
150149
function lazyLoadFs() {
151150
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
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

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
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

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
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

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
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

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
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');

0 commit comments

Comments
 (0)