diff --git a/lib/fs.js b/lib/fs.js index 04b7a190998bd5..9e9d23941f114d 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -94,6 +94,8 @@ const { }, SideEffectFreeRegExpPrototypeExec, defineLazyProperties, + isWindows, + isMacOS, } = require('internal/util'); const { constants: { @@ -166,9 +168,6 @@ let kResistStopPropagation; let FileReadStream; let FileWriteStream; -const isWindows = process.platform === 'win32'; -const isOSX = process.platform === 'darwin'; - function showTruncateDeprecation() { if (truncateWarn) { process.emitWarning( @@ -2457,7 +2456,7 @@ function watch(filename, options, listener) { // TODO(anonrig): Remove non-native watcher when/if libuv supports recursive. // As of November 2022, libuv does not support recursive file watch on all platforms, // e.g. Linux due to the limitations of inotify. - if (options.recursive && !isOSX && !isWindows) { + if (options.recursive && !isMacOS && !isWindows) { const nonNativeWatcher = require('internal/fs/recursive_watch'); watcher = new nonNativeWatcher.FSWatcher(options); watcher[watchers.kFSWatchStart](path); diff --git a/lib/internal/fs/glob.js b/lib/internal/fs/glob.js index 3c225900301309..215257e1951ae9 100644 --- a/lib/internal/fs/glob.js +++ b/lib/internal/fs/glob.js @@ -20,6 +20,8 @@ const { join, resolve, basename, isAbsolute } = require('path'); const { kEmptyObject, + isWindows, + isMacOS, } = require('internal/util'); const { validateFunction, @@ -35,9 +37,6 @@ function lazyMinimatch() { return minimatch; } -const isWindows = process.platform === 'win32'; -const isOSX = process.platform === 'darwin'; - /** * @param {string} path * @returns {Promise} @@ -217,7 +216,7 @@ class Glob { } this.matchers = ArrayPrototypeMap(patterns, (pattern) => new (lazyMinimatch().Minimatch)(pattern, { __proto__: null, - nocase: isWindows || isOSX, + nocase: isWindows || isMacOS, windowsPathsNoEscape: true, nonegate: true, nocomment: true, diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 1d4dbb18bc895c..8fe48a30cda3b9 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -97,6 +97,8 @@ const { kEmptyObject, lazyDOMException, promisify, + isWindows, + isMacOS, } = require('internal/util'); const EventEmitter = require('events'); const { StringDecoder } = require('string_decoder'); @@ -126,9 +128,6 @@ const { const getDirectoryEntriesPromise = promisify(getDirents); const validateRmOptionsPromise = promisify(validateRmOptions); -const isWindows = process.platform === 'win32'; -const isOSX = process.platform === 'darwin'; - let cpPromises; function lazyLoadCpPromises() { return cpPromises ??= require('internal/fs/cp/cp').cpFn; @@ -1256,7 +1255,7 @@ async function* _watch(filename, options = kEmptyObject) { // TODO(anonrig): Remove non-native watcher when/if libuv supports recursive. // As of November 2022, libuv does not support recursive file watch on all platforms, // e.g. Linux due to the limitations of inotify. - if (options.recursive && !isOSX && !isWindows) { + if (options.recursive && !isMacOS && !isWindows) { const watcher = new nonNativeWatcher.FSWatcher(options); watcher[kFSWatchStart](filename); yield* watcher; @@ -1306,7 +1305,7 @@ module.exports = { writeFile, appendFile, readFile, - watch: !isOSX && !isWindows ? _watch : watch, + watch: !isMacOS && !isWindows ? _watch : watch, constants, }, diff --git a/lib/internal/fs/rimraf.js b/lib/internal/fs/rimraf.js index 877f238011cb95..967ca6600a3274 100644 --- a/lib/internal/fs/rimraf.js +++ b/lib/internal/fs/rimraf.js @@ -30,11 +30,10 @@ const { } = fs; const { sep } = require('path'); const { setTimeout } = require('timers'); -const { sleep } = require('internal/util'); +const { sleep, isWindows } = require('internal/util'); const notEmptyErrorCodes = new SafeSet(['ENOTEMPTY', 'EEXIST', 'EPERM']); const retryErrorCodes = new SafeSet( ['EBUSY', 'EMFILE', 'ENFILE', 'ENOTEMPTY', 'EPERM']); -const isWindows = process.platform === 'win32'; const epermHandler = isWindows ? fixWinEPERM : _rmdir; const epermHandlerSync = isWindows ? fixWinEPERMSync : _rmdirSync; const readdirEncoding = 'buffer'; diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 1ff6d8a3dd5158..d01010c399cf29 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -49,6 +49,7 @@ const { kEmptyObject, once, deprecate, + isWindows, } = require('internal/util'); const { toPathIfFileURL } = require('internal/url'); const { @@ -145,8 +146,6 @@ const kWriteFileMaxChunkSize = 512 * 1024; const kMaxUserId = 2 ** 32 - 1; -const isWindows = process.platform === 'win32'; - let fs; function lazyLoadFs() { if (!fs) { diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 64388e6f0ae025..825c119674828c 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -127,6 +127,7 @@ const { kEmptyObject, setOwnProperty, getLazy, + isWindows, } = require('internal/util'); const { makeContextifyScript, @@ -193,8 +194,6 @@ let { startTimer, endTimer } = debugWithTimer('module_timer', (start, end) => { const { tracingChannel } = require('diagnostics_channel'); const onRequire = getLazy(() => tracingChannel('module.require')); -const isWindows = process.platform === 'win32'; - const relativeResolveCache = { __proto__: null }; let requireDepth = 0; diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 48d86f611d98ee..973e8853cc099d 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -52,7 +52,7 @@ const { fileURLToPath, pathToFileURL, URL } = require('internal/url'); let debug = require('internal/util/debuglog').debuglog('esm', (fn) => { debug = fn; }); -const { emitExperimentalWarning, kEmptyObject, setOwnProperty } = require('internal/util'); +const { emitExperimentalWarning, kEmptyObject, setOwnProperty, isWindows } = require('internal/util'); const { ERR_UNKNOWN_BUILTIN_MODULE, ERR_INVALID_RETURN_PROPERTY_VALUE, @@ -416,7 +416,6 @@ translators.set('builtin', function builtinStrategy(url) { }); // Strategy for loading a JSON file -const isWindows = process.platform === 'win32'; translators.set('json', function jsonStrategy(url, source) { emitExperimentalWarning('Importing JSON modules'); assertBufferSource(source, true, 'load'); diff --git a/lib/internal/url.js b/lib/internal/url.js index fc5e8ac8874804..b73e92a32d5a0e 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -46,6 +46,7 @@ const { kEnumerableProperty, kEmptyObject, SideEffectFreeRegExpPrototypeSymbolReplace, + isWindows, } = require('internal/util'); const { @@ -85,9 +86,6 @@ const { const querystring = require('querystring'); -const { platform } = process; -const isWindows = platform === 'win32'; - const bindingUrl = internalBinding('url'); const FORWARD_SLASH = /\//g; @@ -1471,7 +1469,7 @@ function getPathFromURLWin32(url) { function getPathFromURLPosix(url) { if (url.hostname !== '') { - throw new ERR_INVALID_FILE_URL_HOST(platform); + throw new ERR_INVALID_FILE_URL_HOST(process.platform); } const pathname = url.pathname; for (let n = 0; n < pathname.length; n++) { diff --git a/lib/internal/util.js b/lib/internal/util.js index e8763d204ee348..f9de6cd77c71f4 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -71,6 +71,9 @@ const { encodings } = internalBinding('string_decoder'); const noCrypto = !process.versions.openssl; +const isWindows = process.platform === 'win32'; +const isMacOS = process.platform === 'darwin'; + const experimentalWarnings = new SafeSet(); const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex @@ -917,6 +920,8 @@ module.exports = { isArrayBufferDetached, isError, isInsideNodeModules, + isMacOS, + isWindows, join, lazyDOMException, lazyDOMExceptionClass, diff --git a/lib/net.js b/lib/net.js index 6c33bb4dcd6520..4de6f9c5f6f23a 100644 --- a/lib/net.js +++ b/lib/net.js @@ -115,7 +115,7 @@ const { } = require('internal/errors'); const { isUint8Array } = require('internal/util/types'); const { queueMicrotask } = require('internal/process/task_queues'); -const { kEmptyObject, guessHandleType, promisify } = require('internal/util'); +const { kEmptyObject, guessHandleType, promisify, isWindows } = require('internal/util'); const { validateAbortSignal, validateBoolean, @@ -142,8 +142,6 @@ const { kTimeout } = require('internal/timers'); const DEFAULT_IPV4_ADDR = '0.0.0.0'; const DEFAULT_IPV6_ADDR = '::'; -const isWindows = process.platform === 'win32'; - const noop = () => {}; const kPerfHooksNetConnectContext = Symbol('kPerfHooksNetConnectContext'); diff --git a/lib/path.js b/lib/path.js index e9dec3f7d361d3..f092910e73808f 100644 --- a/lib/path.js +++ b/lib/path.js @@ -50,13 +50,12 @@ const { const { getLazy, emitExperimentalWarning, + isWindows, + isMacOS, } = require('internal/util'); const lazyMinimatch = getLazy(() => require('internal/deps/minimatch/index')); -const platformIsWin32 = (process.platform === 'win32'); -const platformIsOSX = (process.platform === 'darwin'); - function isPathSeparator(code) { return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH; } @@ -167,7 +166,7 @@ function glob(path, pattern, windows) { validateString(pattern, 'pattern'); return lazyMinimatch().minimatch(path, pattern, { __proto__: null, - nocase: platformIsOSX || platformIsWin32, + nocase: isMacOS || isWindows, windowsPathsNoEscape: true, nonegate: true, nocomment: true, @@ -1100,7 +1099,7 @@ const win32 = { }; const posixCwd = (() => { - if (platformIsWin32) { + if (isWindows) { // Converts Windows' backslash path separators to POSIX forward slashes // and truncates any drive indicator const regexp = /\\/g; @@ -1575,4 +1574,4 @@ posix.posix = win32.posix = posix; win32._makeLong = win32.toNamespacedPath; posix._makeLong = posix.toNamespacedPath; -module.exports = platformIsWin32 ? win32 : posix; +module.exports = isWindows ? win32 : posix;