Skip to content

Commit

Permalink
lib: further cleanup platform utility methods
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbayley committed Jun 23, 2024
1 parent 777d5d3 commit 593e177
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 28 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 @@ -2442,7 +2441,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
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
6 changes: 1 addition & 5 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,9 +145,6 @@ const kWriteFileMaxChunkSize = 512 * 1024;

const kMaxUserId = 2 ** 32 - 1;

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

let fs;
function lazyLoadFs() {
if (!fs) {
Expand Down Expand Up @@ -971,8 +969,6 @@ module.exports = {
getValidatedFd,
getValidatedPath,
handleErrorFromBinding,
isMacOS,
isWindows,
preprocessSymlinkDestination,
realpathCacheKey: Symbol('realpathCacheKey'),
getStatFsFromBinding,
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 @@ -126,6 +126,7 @@ const {
kEmptyObject,
setOwnProperty,
getLazy,
isWindows,
} = require('internal/util');
const {
makeContextifyScript,
Expand Down Expand Up @@ -192,8 +193,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
7 changes: 5 additions & 2 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ 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 +420,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 @@ -1465,7 +1463,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 @@ -70,6 +70,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 @@ -913,6 +916,8 @@ module.exports = {
isArrayBufferDetached,
isError,
isInsideNodeModules,
isMacOS,
isWindows,
join,
lazyDOMException,
lazyDOMExceptionClass,
Expand Down
9 changes: 6 additions & 3 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ 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 +147,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
2 changes: 1 addition & 1 deletion lib/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const {

const { safeGetenv } = internalBinding('credentials');
const constants = internalBinding('constants').os;
const isWindows = process.platform === 'win32';
const { isWindows } = require('internal/util');

const {
codes: {
Expand Down

0 comments on commit 593e177

Please sign in to comment.