Skip to content

Commit

Permalink
lib: refactor platform utility methods
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbayley committed Jul 13, 2024
1 parent 22f555a commit 1d413e8
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 34 deletions.
7 changes: 3 additions & 4 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ const {
},
SideEffectFreeRegExpPrototypeExec,
defineLazyProperties,
isWindows,
isMacOS,
} = require('internal/util');
const {
constants: {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions lib/internal/fs/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const { join, resolve, basename, isAbsolute } = require('path');

const {
kEmptyObject,
isWindows,
isMacOS,
} = require('internal/util');
const {
validateFunction,
Expand All @@ -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>}
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ const {
kEmptyObject,
lazyDOMException,
promisify,
isWindows,
isMacOS,
} = require('internal/util');
const EventEmitter = require('events');
const { StringDecoder } = require('string_decoder');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1307,7 +1306,7 @@ module.exports = {
writeFile,
appendFile,
readFile,
watch: !isOSX && !isWindows ? _watch : watch,
watch: !isMacOS && !isWindows ? _watch : watch,
constants,
},

Expand Down
3 changes: 1 addition & 2 deletions lib/internal/fs/rimraf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const {
kEmptyObject,
once,
deprecate,
isWindows,
} = require('internal/util');
const { toPathIfFileURL } = require('internal/url');
const {
Expand Down Expand Up @@ -144,8 +145,6 @@ const kWriteFileMaxChunkSize = 512 * 1024;

const kMaxUserId = 2 ** 32 - 1;

const isWindows = process.platform === 'win32';

let fs;
function lazyLoadFs() {
if (!fs) {
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ const {
kEmptyObject,
setOwnProperty,
getLazy,
isWindows,
} = require('internal/util');
const {
makeContextifyScript,
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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');
Expand Down
6 changes: 2 additions & 4 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const {
kEnumerableProperty,
kEmptyObject,
SideEffectFreeRegExpPrototypeSymbolReplace,
isWindows,
} = require('internal/util');

const {
Expand Down Expand Up @@ -85,9 +86,6 @@ const {

const querystring = require('querystring');

const { platform } = process;
const isWindows = platform === 'win32';

const bindingUrl = internalBinding('url');

const FORWARD_SLASH = /\//g;
Expand Down Expand Up @@ -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++) {
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -917,6 +920,8 @@ module.exports = {
isArrayBufferDetached,
isError,
isInsideNodeModules,
isMacOS,
isWindows,
join,
lazyDOMException,
lazyDOMExceptionClass,
Expand Down
4 changes: 1 addition & 3 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
Expand Down
11 changes: 5 additions & 6 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

0 comments on commit 1d413e8

Please sign in to comment.