lib: refactor platform utility methods

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Daniel Bayley
2024-07-15 19:58:11 +01:00
committed by GitHub
parent 034a2409bf
commit 953abf4887
11 changed files with 27 additions and 34 deletions

View File

@@ -94,6 +94,8 @@ const {
},
SideEffectFreeRegExpPrototypeExec,
defineLazyProperties,
isWindows,
isMacOS,
} = require('internal/util');
const {
constants: {
@@ -167,9 +169,6 @@ let kResistStopPropagation;
let FileReadStream;
let FileWriteStream;
const isWindows = process.platform === 'win32';
const isOSX = process.platform === 'darwin';
function showTruncateDeprecation() {
if (truncateWarn) {
process.emitWarning(
@@ -2462,7 +2461,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);

View File

@@ -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<DirentFromStats|null>}
@@ -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,

View File

@@ -98,6 +98,8 @@ const {
kEmptyObject,
lazyDOMException,
promisify,
isWindows,
isMacOS,
} = require('internal/util');
const EventEmitter = require('events');
const { StringDecoder } = require('string_decoder');
@@ -127,9 +129,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;
@@ -1257,7 +1256,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;
@@ -1307,7 +1306,7 @@ module.exports = {
writeFile,
appendFile,
readFile,
watch: !isOSX && !isWindows ? _watch : watch,
watch: !isMacOS && !isWindows ? _watch : watch,
constants,
},

View File

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

View File

@@ -48,6 +48,7 @@ const {
kEmptyObject,
once,
deprecate,
isWindows,
} = require('internal/util');
const { toPathIfFileURL } = require('internal/url');
const {
@@ -144,8 +145,6 @@ const kWriteFileMaxChunkSize = 512 * 1024;
const kMaxUserId = 2 ** 32 - 1;
const isWindows = process.platform === 'win32';
let fs;
function lazyLoadFs() {
if (!fs) {

View File

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

View File

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

View File

@@ -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++) {

View File

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

View File

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

View File

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