Skip to content

Commit 593e177

Browse files
committed
lib: further cleanup platform utility methods
1 parent 777d5d3 commit 593e177

File tree

10 files changed

+29
-28
lines changed

10 files changed

+29
-28
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(
@@ -2442,7 +2441,7 @@ function watch(filename, options, listener) {
24422441
// TODO(anonrig): Remove non-native watcher when/if libuv supports recursive.
24432442
// As of November 2022, libuv does not support recursive file watch on all platforms,
24442443
// e.g. Linux due to the limitations of inotify.
2445-
if (options.recursive && !isOSX && !isWindows) {
2444+
if (options.recursive && !isMacOS && !isWindows) {
24462445
const nonNativeWatcher = require('internal/fs/recursive_watch');
24472446
watcher = new nonNativeWatcher.FSWatcher(options);
24482447
watcher[watchers.kFSWatchStart](path);

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 & 5 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,9 +145,6 @@ const kWriteFileMaxChunkSize = 512 * 1024;
144145

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

147-
const isWindows = process.platform === 'win32';
148-
const isMacOS = process.platform === 'darwin';
149-
150148
let fs;
151149
function lazyLoadFs() {
152150
if (!fs) {
@@ -971,8 +969,6 @@ module.exports = {
971969
getValidatedFd,
972970
getValidatedPath,
973971
handleErrorFromBinding,
974-
isMacOS,
975-
isWindows,
976972
preprocessSymlinkDestination,
977973
realpathCacheKey: Symbol('realpathCacheKey'),
978974
getStatFsFromBinding,

lib/internal/modules/cjs/loader.js

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

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

199198
let requireDepth = 0;

lib/internal/modules/esm/translators.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ 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 {
56+
emitExperimentalWarning,
57+
kEmptyObject,
58+
setOwnProperty,
59+
isWindows } = require('internal/util');
5660
const {
5761
ERR_UNKNOWN_BUILTIN_MODULE,
5862
ERR_INVALID_RETURN_PROPERTY_VALUE,
@@ -416,7 +420,6 @@ translators.set('builtin', function builtinStrategy(url) {
416420
});
417421

418422
// Strategy for loading a JSON file
419-
const isWindows = process.platform === 'win32';
420423
translators.set('json', function jsonStrategy(url, source) {
421424
emitExperimentalWarning('Importing JSON modules');
422425
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;
@@ -1465,7 +1463,7 @@ function getPathFromURLWin32(url) {
14651463

14661464
function getPathFromURLPosix(url) {
14671465
if (url.hostname !== '') {
1468-
throw new ERR_INVALID_FILE_URL_HOST(platform);
1466+
throw new ERR_INVALID_FILE_URL_HOST(process.platform);
14691467
}
14701468
const pathname = url.pathname;
14711469
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
@@ -70,6 +70,9 @@ const { encodings } = internalBinding('string_decoder');
7070

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

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

7578
const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex
@@ -913,6 +916,8 @@ module.exports = {
913916
isArrayBufferDetached,
914917
isError,
915918
isInsideNodeModules,
919+
isMacOS,
920+
isWindows,
916921
join,
917922
lazyDOMException,
918923
lazyDOMExceptionClass,

lib/net.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ 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 {
119+
kEmptyObject,
120+
guessHandleType,
121+
promisify,
122+
isWindows
123+
} = require('internal/util');
119124
const {
120125
validateAbortSignal,
121126
validateBoolean,
@@ -142,8 +147,6 @@ const { kTimeout } = require('internal/timers');
142147
const DEFAULT_IPV4_ADDR = '0.0.0.0';
143148
const DEFAULT_IPV6_ADDR = '::';
144149

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

149152
const kPerfHooksNetConnectContext = Symbol('kPerfHooksNetConnectContext');

lib/os.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const {
3333

3434
const { safeGetenv } = internalBinding('credentials');
3535
const constants = internalBinding('constants').os;
36-
const isWindows = process.platform === 'win32';
36+
const { isWindows } = require('internal/util');
3737

3838
const {
3939
codes: {

0 commit comments

Comments
 (0)