2015-05-28 17:01:56 +03:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
const {
|
2019-11-23 10:09:05 +01:00
|
|
|
ArrayFrom,
|
2020-11-16 16:39:28 +01:00
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeSlice,
|
|
|
|
ArrayPrototypeSort,
|
2020-01-02 19:19:02 +01:00
|
|
|
Error,
|
2023-11-11 17:25:08 +01:00
|
|
|
ErrorCaptureStackTrace,
|
2022-08-05 10:01:32 +02:00
|
|
|
FunctionPrototypeCall,
|
2025-04-20 00:54:48 -05:00
|
|
|
NumberParseInt,
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectDefineProperties,
|
|
|
|
ObjectDefineProperty,
|
2024-04-21 18:53:08 +02:00
|
|
|
ObjectFreeze,
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectGetOwnPropertyDescriptor,
|
|
|
|
ObjectGetOwnPropertyDescriptors,
|
|
|
|
ObjectGetPrototypeOf,
|
2022-08-05 00:41:48 +02:00
|
|
|
ObjectPrototypeHasOwnProperty,
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectSetPrototypeOf,
|
2022-08-17 20:36:33 +02:00
|
|
|
ObjectValues,
|
2019-12-13 16:46:35 +01:00
|
|
|
Promise,
|
2020-11-16 16:39:28 +01:00
|
|
|
ReflectApply,
|
2019-11-22 18:04:46 +01:00
|
|
|
ReflectConstruct,
|
2021-08-19 21:40:18 +02:00
|
|
|
RegExpPrototypeExec,
|
2022-12-14 15:48:50 +01:00
|
|
|
RegExpPrototypeGetDotAll,
|
|
|
|
RegExpPrototypeGetGlobal,
|
|
|
|
RegExpPrototypeGetHasIndices,
|
|
|
|
RegExpPrototypeGetIgnoreCase,
|
|
|
|
RegExpPrototypeGetMultiline,
|
2024-04-21 18:53:08 +02:00
|
|
|
RegExpPrototypeGetSource,
|
2022-12-14 15:48:50 +01:00
|
|
|
RegExpPrototypeGetSticky,
|
|
|
|
RegExpPrototypeGetUnicode,
|
2020-11-16 16:39:28 +01:00
|
|
|
SafeMap,
|
|
|
|
SafeSet,
|
2022-12-14 15:48:50 +01:00
|
|
|
SafeWeakMap,
|
2023-08-07 16:28:56 +02:00
|
|
|
SafeWeakRef,
|
2025-04-20 00:54:48 -05:00
|
|
|
StringPrototypeIncludes,
|
2020-11-16 16:39:28 +01:00
|
|
|
StringPrototypeReplace,
|
2025-04-20 00:54:48 -05:00
|
|
|
StringPrototypeSlice,
|
2020-11-16 16:39:28 +01:00
|
|
|
StringPrototypeToLowerCase,
|
|
|
|
StringPrototypeToUpperCase,
|
2019-11-30 16:55:29 +01:00
|
|
|
Symbol,
|
2019-12-08 18:33:33 +01:00
|
|
|
SymbolFor,
|
2025-04-18 01:14:24 +08:00
|
|
|
SymbolPrototypeGetDescription,
|
2022-12-14 15:48:50 +01:00
|
|
|
SymbolReplace,
|
|
|
|
SymbolSplit,
|
2019-11-22 18:04:46 +01:00
|
|
|
} = primordials;
|
2019-11-30 16:55:29 +01:00
|
|
|
|
2018-03-04 22:16:24 +01:00
|
|
|
const {
|
2019-08-23 15:47:40 -04:00
|
|
|
codes: {
|
|
|
|
ERR_NO_CRYPTO,
|
2024-10-11 20:41:38 +05:00
|
|
|
ERR_NO_TYPESCRIPT,
|
2023-02-26 11:34:02 +01:00
|
|
|
ERR_UNKNOWN_SIGNAL,
|
2019-08-23 15:47:40 -04:00
|
|
|
},
|
2023-11-11 17:25:08 +01:00
|
|
|
isErrorStackTraceLimitWritable,
|
2019-09-16 15:32:15 -05:00
|
|
|
overrideStackTrace,
|
2024-04-23 19:05:38 +02:00
|
|
|
uvErrmapGet,
|
2019-08-23 15:47:40 -04:00
|
|
|
} = require('internal/errors');
|
2018-10-15 01:41:32 +02:00
|
|
|
const { signals } = internalBinding('constants').os;
|
2017-10-26 21:34:15 -07:00
|
|
|
const {
|
2023-06-05 12:33:13 -04:00
|
|
|
guessHandleType: _guessHandleType,
|
2025-02-18 22:23:01 +01:00
|
|
|
defineLazyProperties,
|
2022-11-04 18:33:03 +01:00
|
|
|
privateSymbols: {
|
|
|
|
arrow_message_private_symbol,
|
|
|
|
decorated_private_symbol,
|
|
|
|
},
|
2021-09-24 13:59:45 +08:00
|
|
|
sleep: _sleep,
|
2018-09-03 11:16:48 -04:00
|
|
|
} = internalBinding('util');
|
2023-09-18 12:29:13 +02:00
|
|
|
const { isNativeError, isPromise } = internalBinding('types');
|
2023-04-05 13:13:23 +02:00
|
|
|
const { getOptionValue } = require('internal/options');
|
2024-11-07 03:03:05 -05:00
|
|
|
const assert = require('internal/assert');
|
2023-12-04 02:46:11 +01:00
|
|
|
const { encodings } = internalBinding('string_decoder');
|
2018-08-06 14:40:30 -07:00
|
|
|
|
2017-02-15 13:00:21 -08:00
|
|
|
const noCrypto = !process.versions.openssl;
|
2024-10-11 20:41:38 +05:00
|
|
|
const noTypeScript = !process.versions.amaro;
|
2016-05-31 19:58:31 +02:00
|
|
|
|
2024-07-15 19:58:11 +01:00
|
|
|
const isWindows = process.platform === 'win32';
|
|
|
|
const isMacOS = process.platform === 'darwin';
|
|
|
|
|
2020-11-16 16:39:28 +01:00
|
|
|
const experimentalWarnings = new SafeSet();
|
2017-10-25 12:11:10 -07:00
|
|
|
|
2018-03-21 22:42:35 -07:00
|
|
|
const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex
|
2017-12-11 06:32:59 -02:00
|
|
|
|
2021-04-06 00:18:34 +03:00
|
|
|
let uvBinding;
|
|
|
|
|
|
|
|
function lazyUv() {
|
|
|
|
uvBinding ??= internalBinding('uv');
|
|
|
|
return uvBinding;
|
|
|
|
}
|
|
|
|
|
2017-12-11 06:32:59 -02:00
|
|
|
function removeColors(str) {
|
2020-11-16 16:39:28 +01:00
|
|
|
return StringPrototypeReplace(str, colorRegExp, '');
|
2017-12-11 06:32:59 -02:00
|
|
|
}
|
|
|
|
|
2017-02-15 13:00:21 -08:00
|
|
|
function isError(e) {
|
2018-11-18 04:19:16 +01:00
|
|
|
// An error could be an instance of Error while not being a native error
|
|
|
|
// or could be from a different realm and not be instance of Error but still
|
|
|
|
// be a native error.
|
|
|
|
return isNativeError(e) || e instanceof Error;
|
2017-02-15 13:00:21 -08:00
|
|
|
}
|
|
|
|
|
2017-10-22 12:26:32 -07:00
|
|
|
// Keep a list of deprecation codes that have been warned on so we only warn on
|
|
|
|
// each one once.
|
2020-11-16 16:39:28 +01:00
|
|
|
const codesWarned = new SafeSet();
|
2017-10-22 12:26:32 -07:00
|
|
|
|
2021-08-05 18:12:21 +04:30
|
|
|
let validateString;
|
|
|
|
|
2023-04-05 13:13:23 +02:00
|
|
|
function getDeprecationWarningEmitter(
|
|
|
|
code, msg, deprecated, useEmitSync,
|
|
|
|
shouldEmitWarning = () => true,
|
|
|
|
) {
|
|
|
|
let warned = false;
|
|
|
|
return function() {
|
|
|
|
if (!warned && shouldEmitWarning()) {
|
|
|
|
warned = true;
|
2024-11-08 16:16:15 +00:00
|
|
|
if (code === 'ExperimentalWarning') {
|
|
|
|
process.emitWarning(msg, code, deprecated);
|
|
|
|
} else if (code !== undefined) {
|
2023-04-05 13:13:23 +02:00
|
|
|
if (!codesWarned.has(code)) {
|
|
|
|
const emitWarning = useEmitSync ?
|
|
|
|
require('internal/process/warning').emitWarningSync :
|
|
|
|
process.emitWarning;
|
|
|
|
emitWarning(msg, 'DeprecationWarning', code, deprecated);
|
|
|
|
codesWarned.add(code);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
process.emitWarning(msg, 'DeprecationWarning', deprecated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function isPendingDeprecation() {
|
|
|
|
return getOptionValue('--pending-deprecation') &&
|
|
|
|
!getOptionValue('--no-deprecation');
|
|
|
|
}
|
|
|
|
|
2025-06-03 11:09:40 +02:00
|
|
|
function deprecateProperty(key, msg, code, isPendingDeprecation) {
|
|
|
|
const emitDeprecationWarning = getDeprecationWarningEmitter(
|
|
|
|
code, msg, undefined, false, isPendingDeprecation,
|
|
|
|
);
|
|
|
|
return (options) => {
|
|
|
|
if (key in options) {
|
|
|
|
emitDeprecationWarning();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-05 13:13:23 +02:00
|
|
|
// Internal deprecator for pending --pending-deprecation. This can be invoked
|
|
|
|
// at snapshot building time as the warning permission is only queried at
|
|
|
|
// run time.
|
|
|
|
function pendingDeprecate(fn, msg, code) {
|
|
|
|
const emitDeprecationWarning = getDeprecationWarningEmitter(
|
|
|
|
code, msg, deprecated, false, isPendingDeprecation,
|
|
|
|
);
|
|
|
|
function deprecated(...args) {
|
|
|
|
emitDeprecationWarning();
|
|
|
|
return ReflectApply(fn, this, args);
|
|
|
|
}
|
2025-04-12 01:26:25 +09:00
|
|
|
|
|
|
|
ObjectDefineProperty(deprecated, 'length', {
|
|
|
|
__proto__: null,
|
|
|
|
...ObjectGetOwnPropertyDescriptor(fn, 'length'),
|
|
|
|
});
|
|
|
|
|
2023-04-05 13:13:23 +02:00
|
|
|
return deprecated;
|
|
|
|
}
|
|
|
|
|
2015-06-15 21:51:41 -07:00
|
|
|
// Mark that a method should not be used.
|
|
|
|
// Returns a modified function which warns once by default.
|
|
|
|
// If --no-deprecation is set, then it is a no-op.
|
2022-10-07 22:26:12 +09:00
|
|
|
function deprecate(fn, msg, code, useEmitSync) {
|
2021-08-05 18:12:21 +04:30
|
|
|
// Lazy-load to avoid a circular dependency.
|
|
|
|
if (validateString === undefined)
|
|
|
|
({ validateString } = require('internal/validators'));
|
|
|
|
|
|
|
|
if (code !== undefined)
|
|
|
|
validateString(code, 'code');
|
2016-12-04 12:47:01 -08:00
|
|
|
|
2023-04-05 13:13:23 +02:00
|
|
|
const emitDeprecationWarning = getDeprecationWarningEmitter(
|
|
|
|
code, msg, deprecated, useEmitSync,
|
|
|
|
);
|
|
|
|
|
2017-02-15 13:00:21 -08:00
|
|
|
function deprecated(...args) {
|
2024-02-26 03:55:07 +01:00
|
|
|
// TODO(joyeecheung): use getOptionValue('--no-deprecation') instead.
|
|
|
|
if (!process.noDeprecation) {
|
|
|
|
emitDeprecationWarning();
|
|
|
|
}
|
2016-07-12 23:09:12 +02:00
|
|
|
if (new.target) {
|
2019-11-22 18:04:46 +01:00
|
|
|
return ReflectConstruct(fn, args, new.target);
|
2016-07-12 23:09:12 +02:00
|
|
|
}
|
2020-11-16 16:39:28 +01:00
|
|
|
return ReflectApply(fn, this, args);
|
2015-06-15 21:51:41 -07:00
|
|
|
}
|
|
|
|
|
2016-07-12 23:09:12 +02:00
|
|
|
// The wrapper will keep the same prototype as fn to maintain prototype chain
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectSetPrototypeOf(deprecated, fn);
|
2016-07-12 23:09:12 +02:00
|
|
|
if (fn.prototype) {
|
2016-08-14 15:27:40 -07:00
|
|
|
// Setting this (rather than using Object.setPrototype, as above) ensures
|
|
|
|
// that calling the unwrapped constructor gives an instanceof the wrapped
|
|
|
|
// constructor.
|
|
|
|
deprecated.prototype = fn.prototype;
|
2016-07-12 23:09:12 +02:00
|
|
|
}
|
|
|
|
|
2025-04-12 01:26:25 +09:00
|
|
|
ObjectDefineProperty(deprecated, 'length', {
|
|
|
|
__proto__: null,
|
|
|
|
...ObjectGetOwnPropertyDescriptor(fn, 'length'),
|
|
|
|
});
|
|
|
|
|
2015-06-15 21:51:41 -07:00
|
|
|
return deprecated;
|
2017-02-15 13:00:21 -08:00
|
|
|
}
|
2015-11-25 22:37:43 +01:00
|
|
|
|
2024-11-07 03:03:05 -05:00
|
|
|
function deprecateInstantiation(target, code, ...args) {
|
|
|
|
assert(typeof code === 'string');
|
|
|
|
|
|
|
|
getDeprecationWarningEmitter(code, `Instantiating ${target.name} without the 'new' keyword has been deprecated.`, target)();
|
|
|
|
|
|
|
|
return ReflectConstruct(target, args);
|
|
|
|
}
|
|
|
|
|
2017-02-15 13:00:21 -08:00
|
|
|
function decorateErrorStack(err) {
|
2022-11-04 18:33:03 +01:00
|
|
|
if (!(isError(err) && err.stack) || err[decorated_private_symbol])
|
2015-11-25 22:37:43 +01:00
|
|
|
return;
|
|
|
|
|
2022-11-04 18:33:03 +01:00
|
|
|
const arrow = err[arrow_message_private_symbol];
|
2015-11-25 22:37:43 +01:00
|
|
|
|
2015-12-15 03:55:35 -05:00
|
|
|
if (arrow) {
|
2015-11-25 22:37:43 +01:00
|
|
|
err.stack = arrow + err.stack;
|
2022-11-04 18:33:03 +01:00
|
|
|
err[decorated_private_symbol] = true;
|
2015-12-15 03:55:35 -05:00
|
|
|
}
|
2017-02-15 13:00:21 -08:00
|
|
|
}
|
2016-03-08 15:31:31 -08:00
|
|
|
|
2017-02-15 13:00:21 -08:00
|
|
|
function assertCrypto() {
|
2016-03-08 15:31:31 -08:00
|
|
|
if (noCrypto)
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_NO_CRYPTO();
|
2017-02-15 13:00:21 -08:00
|
|
|
}
|
2016-12-25 03:32:26 +08:00
|
|
|
|
2024-10-11 20:41:38 +05:00
|
|
|
function assertTypeScript() {
|
|
|
|
if (noTypeScript)
|
|
|
|
throw new ERR_NO_TYPESCRIPT();
|
|
|
|
}
|
|
|
|
|
2024-09-23 18:49:53 -04:00
|
|
|
/**
|
|
|
|
* Move the "slow cases" to a separate function to make sure this function gets
|
|
|
|
* inlined properly. That prioritizes the common case.
|
|
|
|
* @param {unknown} enc
|
|
|
|
* @returns {string | undefined} Returns undefined if there is no match.
|
|
|
|
*/
|
2017-02-15 13:00:21 -08:00
|
|
|
function normalizeEncoding(enc) {
|
2018-02-14 23:48:35 +01:00
|
|
|
if (enc == null || enc === 'utf8' || enc === 'utf-8') return 'utf8';
|
|
|
|
return slowCases(enc);
|
|
|
|
}
|
|
|
|
|
|
|
|
function slowCases(enc) {
|
|
|
|
switch (enc.length) {
|
|
|
|
case 4:
|
|
|
|
if (enc === 'UTF8') return 'utf8';
|
|
|
|
if (enc === 'ucs2' || enc === 'UCS2') return 'utf16le';
|
2023-11-17 04:53:38 +08:00
|
|
|
enc = StringPrototypeToLowerCase(enc);
|
2018-02-14 23:48:35 +01:00
|
|
|
if (enc === 'utf8') return 'utf8';
|
2018-06-22 19:08:58 +08:00
|
|
|
if (enc === 'ucs2') return 'utf16le';
|
2018-02-14 23:48:35 +01:00
|
|
|
break;
|
|
|
|
case 3:
|
2020-11-16 16:39:28 +01:00
|
|
|
if (enc === 'hex' || enc === 'HEX' ||
|
2023-11-17 04:53:38 +08:00
|
|
|
StringPrototypeToLowerCase(enc) === 'hex')
|
2018-02-14 23:48:35 +01:00
|
|
|
return 'hex';
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
if (enc === 'ascii') return 'ascii';
|
|
|
|
if (enc === 'ucs-2') return 'utf16le';
|
|
|
|
if (enc === 'UTF-8') return 'utf8';
|
|
|
|
if (enc === 'ASCII') return 'ascii';
|
|
|
|
if (enc === 'UCS-2') return 'utf16le';
|
2023-11-17 04:53:38 +08:00
|
|
|
enc = StringPrototypeToLowerCase(enc);
|
2018-02-14 23:48:35 +01:00
|
|
|
if (enc === 'utf-8') return 'utf8';
|
|
|
|
if (enc === 'ascii') return 'ascii';
|
2018-06-22 19:08:58 +08:00
|
|
|
if (enc === 'ucs-2') return 'utf16le';
|
2018-02-14 23:48:35 +01:00
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
if (enc === 'base64') return 'base64';
|
|
|
|
if (enc === 'latin1' || enc === 'binary') return 'latin1';
|
|
|
|
if (enc === 'BASE64') return 'base64';
|
|
|
|
if (enc === 'LATIN1' || enc === 'BINARY') return 'latin1';
|
2023-11-17 04:53:38 +08:00
|
|
|
enc = StringPrototypeToLowerCase(enc);
|
2018-02-14 23:48:35 +01:00
|
|
|
if (enc === 'base64') return 'base64';
|
|
|
|
if (enc === 'latin1' || enc === 'binary') return 'latin1';
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
if (enc === 'utf16le' || enc === 'UTF16LE' ||
|
2023-11-17 04:53:38 +08:00
|
|
|
StringPrototypeToLowerCase(enc) === 'utf16le')
|
2016-06-07 16:03:27 -07:00
|
|
|
return 'utf16le';
|
2018-02-14 23:48:35 +01:00
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
if (enc === 'utf-16le' || enc === 'UTF-16LE' ||
|
2023-11-17 04:53:38 +08:00
|
|
|
StringPrototypeToLowerCase(enc) === 'utf-16le')
|
2018-02-14 23:48:35 +01:00
|
|
|
return 'utf16le';
|
|
|
|
break;
|
2021-01-15 21:01:28 +01:00
|
|
|
case 9:
|
|
|
|
if (enc === 'base64url' || enc === 'BASE64URL' ||
|
2023-11-17 04:53:38 +08:00
|
|
|
StringPrototypeToLowerCase(enc) === 'base64url')
|
2021-01-15 21:01:28 +01:00
|
|
|
return 'base64url';
|
|
|
|
break;
|
2018-02-14 23:48:35 +01:00
|
|
|
default:
|
|
|
|
if (enc === '') return 'utf8';
|
2016-06-07 16:03:27 -07:00
|
|
|
}
|
2017-02-15 13:00:21 -08:00
|
|
|
}
|
2016-06-08 08:18:26 -07:00
|
|
|
|
2024-10-18 16:48:19 +02:00
|
|
|
/**
|
|
|
|
* @param {string} feature Feature name used in the warning message
|
|
|
|
* @param {string} messagePrefix Prefix of the warning message
|
|
|
|
* @param {string} code See documentation of process.emitWarning
|
|
|
|
* @param {string} ctor See documentation of process.emitWarning
|
|
|
|
*/
|
|
|
|
function emitExperimentalWarning(feature, messagePrefix, code, ctor) {
|
2017-10-25 12:11:10 -07:00
|
|
|
if (experimentalWarnings.has(feature)) return;
|
|
|
|
experimentalWarnings.add(feature);
|
2024-10-22 18:51:45 +02:00
|
|
|
let msg = `${feature} is an experimental feature and might change at any time`;
|
|
|
|
if (messagePrefix) {
|
|
|
|
msg = messagePrefix + msg;
|
|
|
|
}
|
2024-10-18 16:48:19 +02:00
|
|
|
process.emitWarning(msg, 'ExperimentalWarning', code, ctor);
|
2017-10-25 12:11:10 -07:00
|
|
|
}
|
|
|
|
|
2017-02-15 13:00:21 -08:00
|
|
|
function filterDuplicateStrings(items, low) {
|
2020-11-16 16:39:28 +01:00
|
|
|
const map = new SafeMap();
|
2019-11-12 14:47:03 +00:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
2016-06-08 08:18:26 -07:00
|
|
|
const item = items[i];
|
2020-11-16 16:39:28 +01:00
|
|
|
const key = StringPrototypeToLowerCase(item);
|
2016-06-08 08:18:26 -07:00
|
|
|
if (low) {
|
|
|
|
map.set(key, key);
|
|
|
|
} else {
|
2017-01-07 20:12:08 -08:00
|
|
|
map.set(key, item);
|
2016-06-08 08:18:26 -07:00
|
|
|
}
|
|
|
|
}
|
2020-11-16 16:39:28 +01:00
|
|
|
return ArrayPrototypeSort(ArrayFrom(map.values()));
|
2017-02-15 13:00:21 -08:00
|
|
|
}
|
2016-06-08 08:18:26 -07:00
|
|
|
|
2017-02-15 13:00:21 -08:00
|
|
|
function cachedResult(fn) {
|
2018-02-16 07:28:38 -05:00
|
|
|
let result;
|
2016-06-08 08:18:26 -07:00
|
|
|
return () => {
|
|
|
|
if (result === undefined)
|
|
|
|
result = fn();
|
2020-11-16 16:39:28 +01:00
|
|
|
return ArrayPrototypeSlice(result);
|
2016-06-08 08:18:26 -07:00
|
|
|
};
|
2017-02-15 13:00:21 -08:00
|
|
|
}
|
buffer: convert offset & length to int properly
As per ecma-262 2015's #sec-%typedarray%-buffer-byteoffset-length,
`offset` would be an integer, not a 32 bit unsigned integer. Also,
`length` would be an integer with the maximum value of 2^53 - 1, not a
32 bit unsigned integer.
This would be a problem because, if we create a buffer from an
arraybuffer, from an offset which is greater than 2^32, it would be
actually pointing to a different location in arraybuffer. For example,
if we use 2^40 as offset, then the actual value used will be 0,
because `byteOffset >>>= 0` will convert `byteOffset` to a 32 bit
unsigned int, which is based on 2^32 modulo.
This is a redo, as the ca37fa527f174b547893817fe8c67a3befa02317 broke
CI.
Refer: https://github.com/nodejs/node/pull/9814
Refer: https://github.com/nodejs/node/pull/9492
PR-URL: https://github.com/nodejs/node/pull/9815
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2016-11-27 15:08:17 +05:30
|
|
|
|
2017-02-15 07:43:22 -08:00
|
|
|
// Useful for Wrapping an ES6 Class with a constructor Function that
|
|
|
|
// does not require the new keyword. For instance:
|
|
|
|
// class A { constructor(x) {this.x = x;}}
|
|
|
|
// const B = createClassWrapper(A);
|
|
|
|
// B() instanceof A // true
|
|
|
|
// B() instanceof B // true
|
2017-02-15 13:00:21 -08:00
|
|
|
function createClassWrapper(type) {
|
2017-04-27 17:27:05 -07:00
|
|
|
function fn(...args) {
|
2019-11-22 18:04:46 +01:00
|
|
|
return ReflectConstruct(type, args, new.target || type);
|
2017-04-27 17:27:05 -07:00
|
|
|
}
|
2017-02-15 07:43:22 -08:00
|
|
|
// Mask the wrapper function name and length values
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectDefineProperties(fn, {
|
2022-06-03 10:23:58 +02:00
|
|
|
name: { __proto__: null, value: type.name },
|
|
|
|
length: { __proto__: null, value: type.length },
|
2017-02-15 07:43:22 -08:00
|
|
|
});
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectSetPrototypeOf(fn, type);
|
2017-02-15 07:43:22 -08:00
|
|
|
fn.prototype = type.prototype;
|
|
|
|
return fn;
|
2017-02-15 13:00:21 -08:00
|
|
|
}
|
2016-12-23 14:36:20 +05:30
|
|
|
|
|
|
|
let signalsToNamesMapping;
|
|
|
|
function getSignalsToNamesMapping() {
|
|
|
|
if (signalsToNamesMapping !== undefined)
|
|
|
|
return signalsToNamesMapping;
|
|
|
|
|
2023-01-09 21:38:36 -08:00
|
|
|
signalsToNamesMapping = { __proto__: null };
|
2019-03-26 05:21:27 +01:00
|
|
|
for (const key in signals) {
|
2016-12-23 14:36:20 +05:30
|
|
|
signalsToNamesMapping[signals[key]] = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return signalsToNamesMapping;
|
|
|
|
}
|
|
|
|
|
2017-02-15 13:00:21 -08:00
|
|
|
function convertToValidSignal(signal) {
|
2016-12-23 14:36:20 +05:30
|
|
|
if (typeof signal === 'number' && getSignalsToNamesMapping()[signal])
|
|
|
|
return signal;
|
|
|
|
|
|
|
|
if (typeof signal === 'string') {
|
2020-11-16 16:39:28 +01:00
|
|
|
const signalName = signals[StringPrototypeToUpperCase(signal)];
|
2016-12-23 14:36:20 +05:30
|
|
|
if (signalName) return signalName;
|
|
|
|
}
|
|
|
|
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_UNKNOWN_SIGNAL(signal);
|
2017-02-15 13:00:21 -08:00
|
|
|
}
|
|
|
|
|
2017-04-19 23:07:39 -07:00
|
|
|
function getConstructorOf(obj) {
|
|
|
|
while (obj) {
|
2019-11-22 18:04:46 +01:00
|
|
|
const descriptor = ObjectGetOwnPropertyDescriptor(obj, 'constructor');
|
2017-04-19 23:07:39 -07:00
|
|
|
if (descriptor !== undefined &&
|
|
|
|
typeof descriptor.value === 'function' &&
|
|
|
|
descriptor.value.name !== '') {
|
|
|
|
return descriptor.value;
|
|
|
|
}
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
obj = ObjectGetPrototypeOf(obj);
|
2017-04-19 23:07:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-09-22 09:37:42 +01:00
|
|
|
let cachedURL;
|
|
|
|
let cachedCWD;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current working directory while accounting for the possibility that it has been deleted.
|
|
|
|
* `process.cwd()` can fail if the parent directory is deleted while the process runs.
|
|
|
|
* @returns {URL} The current working directory or the volume root if it cannot be determined.
|
|
|
|
*/
|
|
|
|
function getCWDURL() {
|
|
|
|
const { sep } = require('path');
|
|
|
|
const { pathToFileURL } = require('internal/url');
|
|
|
|
|
|
|
|
let cwd;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// The implementation of `process.cwd()` already uses proper cache when it can.
|
|
|
|
// It's a relatively cheap call performance-wise for the most common use case.
|
|
|
|
cwd = process.cwd();
|
|
|
|
} catch {
|
|
|
|
cachedURL ??= pathToFileURL(sep);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cwd != null && cwd !== cachedCWD) {
|
|
|
|
cachedURL = pathToFileURL(cwd + sep);
|
|
|
|
cachedCWD = cwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cachedURL;
|
|
|
|
}
|
|
|
|
|
2024-10-19 18:15:16 -05:00
|
|
|
function getSystemErrorMessage(err) {
|
|
|
|
return lazyUv().getErrorMessage(err);
|
|
|
|
}
|
|
|
|
|
2018-01-17 03:21:16 +08:00
|
|
|
function getSystemErrorName(err) {
|
2019-08-23 15:47:40 -04:00
|
|
|
const entry = uvErrmapGet(err);
|
2018-01-17 03:21:16 +08:00
|
|
|
return entry ? entry[0] : `Unknown system error ${err}`;
|
|
|
|
}
|
|
|
|
|
2021-04-06 00:18:34 +03:00
|
|
|
function getSystemErrorMap() {
|
|
|
|
return lazyUv().getErrorMap();
|
|
|
|
}
|
|
|
|
|
2020-02-07 11:40:00 +01:00
|
|
|
const kCustomPromisifiedSymbol = SymbolFor('nodejs.util.promisify.custom');
|
2017-04-14 18:28:16 +02:00
|
|
|
const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs');
|
|
|
|
|
2021-08-05 18:12:21 +04:30
|
|
|
let validateFunction;
|
|
|
|
|
2017-06-20 23:20:10 +02:00
|
|
|
function promisify(original) {
|
2021-08-05 18:12:21 +04:30
|
|
|
// Lazy-load to avoid a circular dependency.
|
|
|
|
if (validateFunction === undefined)
|
|
|
|
({ validateFunction } = require('internal/validators'));
|
|
|
|
|
|
|
|
validateFunction(original, 'original');
|
2017-04-14 18:28:16 +02:00
|
|
|
|
2017-06-20 23:20:10 +02:00
|
|
|
if (original[kCustomPromisifiedSymbol]) {
|
|
|
|
const fn = original[kCustomPromisifiedSymbol];
|
2021-08-05 18:12:21 +04:30
|
|
|
|
|
|
|
validateFunction(fn, 'util.promisify.custom');
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
return ObjectDefineProperty(fn, kCustomPromisifiedSymbol, {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2023-02-26 11:34:02 +01:00
|
|
|
value: fn, enumerable: false, writable: false, configurable: true,
|
2017-04-14 18:28:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Names to create an object from in case the callback receives multiple
|
2019-01-03 22:12:40 +09:00
|
|
|
// arguments, e.g. ['bytesRead', 'buffer'] for fs.read.
|
2017-06-20 23:20:10 +02:00
|
|
|
const argumentNames = original[kCustomPromisifyArgsSymbol];
|
2017-04-14 18:28:16 +02:00
|
|
|
|
|
|
|
function fn(...args) {
|
2018-05-19 00:25:07 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-11-16 16:39:28 +01:00
|
|
|
ArrayPrototypePush(args, (err, ...values) => {
|
2017-04-14 18:28:16 +02:00
|
|
|
if (err) {
|
2018-05-19 00:25:07 +02:00
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
if (argumentNames !== undefined && values.length > 1) {
|
2017-04-14 18:28:16 +02:00
|
|
|
const obj = {};
|
2019-11-12 14:47:03 +00:00
|
|
|
for (let i = 0; i < argumentNames.length; i++)
|
2017-04-14 18:28:16 +02:00
|
|
|
obj[argumentNames[i]] = values[i];
|
2018-05-19 00:25:07 +02:00
|
|
|
resolve(obj);
|
2017-04-14 18:28:16 +02:00
|
|
|
} else {
|
2018-05-19 00:25:07 +02:00
|
|
|
resolve(values[0]);
|
2017-04-14 18:28:16 +02:00
|
|
|
}
|
|
|
|
});
|
2023-09-18 12:29:13 +02:00
|
|
|
if (isPromise(ReflectApply(original, this, args))) {
|
|
|
|
process.emitWarning('Calling promisify on a function that returns a Promise is likely a mistake.',
|
|
|
|
'DeprecationWarning', 'DEP0174');
|
|
|
|
}
|
2018-05-19 00:25:07 +02:00
|
|
|
});
|
2017-04-14 18:28:16 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectSetPrototypeOf(fn, ObjectGetPrototypeOf(original));
|
2017-04-14 18:28:16 +02:00
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectDefineProperty(fn, kCustomPromisifiedSymbol, {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2023-02-26 11:34:02 +01:00
|
|
|
value: fn, enumerable: false, writable: false, configurable: true,
|
2017-04-14 18:28:16 +02:00
|
|
|
});
|
2022-08-17 20:36:33 +02:00
|
|
|
|
|
|
|
const descriptors = ObjectGetOwnPropertyDescriptors(original);
|
|
|
|
const propertiesValues = ObjectValues(descriptors);
|
|
|
|
for (let i = 0; i < propertiesValues.length; i++) {
|
|
|
|
// We want to use null-prototype objects to not rely on globally mutable
|
|
|
|
// %Object.prototype%.
|
|
|
|
ObjectSetPrototypeOf(propertiesValues[i], null);
|
|
|
|
}
|
|
|
|
return ObjectDefineProperties(fn, descriptors);
|
2017-04-14 18:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
promisify.custom = kCustomPromisifiedSymbol;
|
|
|
|
|
2021-05-31 08:50:41 -04:00
|
|
|
// The built-in Array#join is slower in v8 6.0
|
2017-08-21 23:20:59 -03:00
|
|
|
function join(output, separator) {
|
2018-02-16 07:28:38 -05:00
|
|
|
let str = '';
|
2017-08-21 23:20:59 -03:00
|
|
|
if (output.length !== 0) {
|
2019-05-08 20:45:10 +02:00
|
|
|
const lastIndex = output.length - 1;
|
|
|
|
for (let i = 0; i < lastIndex; i++) {
|
2017-08-21 23:20:59 -03:00
|
|
|
// It is faster not to use a template string here
|
|
|
|
str += output[i];
|
|
|
|
str += separator;
|
|
|
|
}
|
2019-05-08 20:45:10 +02:00
|
|
|
str += output[lastIndex];
|
2017-08-21 23:20:59 -03:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-05-01 14:22:39 +02:00
|
|
|
// As of V8 6.6, depending on the size of the array, this is anywhere
|
|
|
|
// between 1.5-10x faster than the two-arg version of Array#splice()
|
2017-10-15 23:59:53 +08:00
|
|
|
function spliceOne(list, index) {
|
2018-05-01 14:22:39 +02:00
|
|
|
for (; index + 1 < list.length; index++)
|
|
|
|
list[index] = list[index + 1];
|
2021-04-15 15:05:42 +02:00
|
|
|
list.pop();
|
2017-10-15 23:59:53 +08:00
|
|
|
}
|
|
|
|
|
2024-03-21 02:08:02 +01:00
|
|
|
const kNodeModulesRE = /^(?:.*)[\\/]node_modules[\\/]/;
|
2018-03-22 00:30:21 +01:00
|
|
|
|
2024-10-04 19:29:34 +02:00
|
|
|
function isUnderNodeModules(filename) {
|
|
|
|
return filename && (RegExpPrototypeExec(kNodeModulesRE, filename) !== null);
|
|
|
|
}
|
|
|
|
|
2024-05-19 01:09:09 -04:00
|
|
|
let getStructuredStackImpl;
|
2018-03-22 00:30:21 +01:00
|
|
|
|
2024-05-19 01:09:09 -04:00
|
|
|
function lazyGetStructuredStack() {
|
|
|
|
if (getStructuredStackImpl === undefined) {
|
2018-03-22 00:30:21 +01:00
|
|
|
// Lazy-load to avoid a circular dependency.
|
|
|
|
const { runInNewContext } = require('vm');
|
|
|
|
// Use `runInNewContext()` to get something tamper-proof and
|
2024-05-19 01:09:09 -04:00
|
|
|
// side-effect-free. Since this is currently only used for a deprecated API
|
|
|
|
// and module mocking, the perf implications should be okay.
|
|
|
|
getStructuredStackImpl = runInNewContext(`(function() {
|
2021-04-12 17:48:57 +02:00
|
|
|
try { Error.stackTraceLimit = Infinity; } catch {}
|
2018-03-22 00:30:21 +01:00
|
|
|
return function structuredStack() {
|
2019-09-16 15:32:15 -05:00
|
|
|
const e = new Error();
|
|
|
|
overrideStackTrace.set(e, (err, trace) => trace);
|
|
|
|
return e.stack;
|
2018-03-22 00:30:21 +01:00
|
|
|
};
|
2019-09-16 15:32:15 -05:00
|
|
|
})()`, { overrideStackTrace }, { filename: 'structured-stack' });
|
2018-03-22 00:30:21 +01:00
|
|
|
}
|
|
|
|
|
2024-05-19 01:09:09 -04:00
|
|
|
return getStructuredStackImpl;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getStructuredStack() {
|
|
|
|
const getStructuredStackImpl = lazyGetStructuredStack();
|
|
|
|
|
|
|
|
return getStructuredStackImpl();
|
|
|
|
}
|
|
|
|
|
2024-05-05 01:55:50 +03:00
|
|
|
function once(callback, { preserveReturnValue = false } = kEmptyObject) {
|
2018-09-10 09:57:15 -04:00
|
|
|
let called = false;
|
2024-05-05 01:55:50 +03:00
|
|
|
let returnValue;
|
2018-09-10 09:57:15 -04:00
|
|
|
return function(...args) {
|
2024-05-05 01:55:50 +03:00
|
|
|
if (called) return returnValue;
|
2018-09-10 09:57:15 -04:00
|
|
|
called = true;
|
2024-05-05 01:55:50 +03:00
|
|
|
const result = ReflectApply(callback, this, args);
|
|
|
|
returnValue = preserveReturnValue ? result : undefined;
|
|
|
|
return result;
|
2018-09-10 09:57:15 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-03 23:02:49 -05:00
|
|
|
let validateUint32;
|
|
|
|
|
|
|
|
function sleep(msec) {
|
|
|
|
// Lazy-load to avoid a circular dependency.
|
|
|
|
if (validateUint32 === undefined)
|
|
|
|
({ validateUint32 } = require('internal/validators'));
|
|
|
|
|
|
|
|
validateUint32(msec, 'msec');
|
|
|
|
_sleep(msec);
|
|
|
|
}
|
|
|
|
|
2022-01-05 20:01:49 +09:00
|
|
|
// https://heycam.github.io/webidl/#define-the-operations
|
|
|
|
function defineOperation(target, name, method) {
|
|
|
|
ObjectDefineProperty(target, name, {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2022-01-05 20:01:49 +09:00
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
2023-02-26 11:34:02 +01:00
|
|
|
value: method,
|
2022-01-05 20:01:49 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://heycam.github.io/webidl/#es-interfaces
|
|
|
|
function exposeInterface(target, name, interfaceObject) {
|
|
|
|
ObjectDefineProperty(target, name, {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2022-01-05 20:01:49 +09:00
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
2023-02-26 11:34:02 +01:00
|
|
|
value: interfaceObject,
|
2022-01-05 20:01:49 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-16 00:21:53 +08:00
|
|
|
// https://heycam.github.io/webidl/#es-namespaces
|
|
|
|
function exposeNamespace(target, name, namespaceObject) {
|
|
|
|
ObjectDefineProperty(target, name, {
|
|
|
|
__proto__: null,
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: namespaceObject,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function exposeGetterAndSetter(target, name, getter, setter = undefined) {
|
|
|
|
ObjectDefineProperty(target, name, {
|
|
|
|
__proto__: null,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
get: getter,
|
|
|
|
set: setter,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-16 13:32:22 +01:00
|
|
|
function defineReplaceableLazyAttribute(target, id, keys, writable = true, check) {
|
2022-12-09 23:37:35 +01:00
|
|
|
let mod;
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const key = keys[i];
|
|
|
|
let value;
|
|
|
|
let setterCalled = false;
|
|
|
|
|
|
|
|
function get() {
|
2022-12-16 13:32:22 +01:00
|
|
|
if (check !== undefined) {
|
|
|
|
FunctionPrototypeCall(check, this);
|
|
|
|
}
|
2022-12-09 23:37:35 +01:00
|
|
|
if (setterCalled) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
mod ??= require(id);
|
|
|
|
value ??= mod[key];
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectDefineProperty(get, 'name', {
|
|
|
|
__proto__: null,
|
|
|
|
value: `get ${key}`,
|
|
|
|
});
|
|
|
|
|
|
|
|
function set(val) {
|
|
|
|
setterCalled = true;
|
|
|
|
value = val;
|
|
|
|
}
|
|
|
|
ObjectDefineProperty(set, 'name', {
|
|
|
|
__proto__: null,
|
|
|
|
value: `set ${key}`,
|
|
|
|
});
|
|
|
|
|
|
|
|
ObjectDefineProperty(target, key, {
|
|
|
|
__proto__: null,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
get,
|
|
|
|
set: writable ? set : undefined,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function exposeLazyInterfaces(target, id, keys) {
|
|
|
|
defineLazyProperties(target, id, keys, false);
|
|
|
|
}
|
|
|
|
|
2021-06-28 16:29:54 +08:00
|
|
|
let _DOMException;
|
|
|
|
const lazyDOMExceptionClass = () => {
|
|
|
|
_DOMException ??= internalBinding('messaging').DOMException;
|
|
|
|
return _DOMException;
|
|
|
|
};
|
|
|
|
|
2023-11-11 17:25:08 +01:00
|
|
|
const lazyDOMException = (message, name) => {
|
2021-06-28 16:29:54 +08:00
|
|
|
_DOMException ??= internalBinding('messaging').DOMException;
|
2023-11-11 17:25:08 +01:00
|
|
|
if (isErrorStackTraceLimitWritable()) {
|
|
|
|
const limit = Error.stackTraceLimit;
|
|
|
|
Error.stackTraceLimit = 0;
|
|
|
|
const ex = new _DOMException(message, name);
|
|
|
|
Error.stackTraceLimit = limit;
|
|
|
|
ErrorCaptureStackTrace(ex, lazyDOMException);
|
|
|
|
return ex;
|
|
|
|
}
|
2021-06-28 16:29:54 +08:00
|
|
|
return new _DOMException(message, name);
|
2023-11-11 17:25:08 +01:00
|
|
|
|
|
|
|
};
|
2021-06-21 11:34:48 +08:00
|
|
|
|
2023-01-09 21:38:36 -08:00
|
|
|
const kEnumerableProperty = { __proto__: null };
|
2022-02-11 18:30:47 +01:00
|
|
|
kEnumerableProperty.enumerable = true;
|
2022-05-26 08:00:35 +08:00
|
|
|
ObjectFreeze(kEnumerableProperty);
|
2022-02-11 18:30:47 +01:00
|
|
|
|
2023-01-09 21:38:36 -08:00
|
|
|
const kEmptyObject = ObjectFreeze({ __proto__: null });
|
2022-05-21 17:48:37 +08:00
|
|
|
|
2022-08-05 00:41:48 +02:00
|
|
|
function filterOwnProperties(source, keys) {
|
2023-01-09 21:38:36 -08:00
|
|
|
const filtered = { __proto__: null };
|
2022-08-05 00:41:48 +02:00
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const key = keys[i];
|
|
|
|
if (ObjectPrototypeHasOwnProperty(source, key)) {
|
|
|
|
filtered[key] = source[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mimics `obj[key] = value` but ignoring potential prototype inheritance.
|
|
|
|
* @param {any} obj
|
|
|
|
* @param {string} key
|
|
|
|
* @param {any} value
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
|
|
|
function setOwnProperty(obj, key, value) {
|
|
|
|
return ObjectDefineProperty(obj, key, {
|
|
|
|
__proto__: null,
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
value,
|
|
|
|
writable: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-05 10:01:32 +02:00
|
|
|
let internalGlobal;
|
|
|
|
function getInternalGlobal() {
|
|
|
|
if (internalGlobal == null) {
|
|
|
|
// Lazy-load to avoid a circular dependency.
|
|
|
|
const { runInNewContext } = require('vm');
|
|
|
|
internalGlobal = runInNewContext('this', undefined, { contextName: 'internal' });
|
|
|
|
}
|
|
|
|
return internalGlobal;
|
|
|
|
}
|
|
|
|
|
|
|
|
function SideEffectFreeRegExpPrototypeExec(regex, string) {
|
|
|
|
const { RegExp: RegExpFromAnotherRealm } = getInternalGlobal();
|
|
|
|
return FunctionPrototypeCall(RegExpFromAnotherRealm.prototype.exec, regex, string);
|
|
|
|
}
|
|
|
|
|
2024-09-26 00:35:18 +10:00
|
|
|
const crossRealmRegexes = new SafeWeakMap();
|
|
|
|
function getCrossRealmRegex(regex) {
|
|
|
|
const cached = crossRealmRegexes.get(regex);
|
2022-12-14 15:48:50 +01:00
|
|
|
if (cached) return cached;
|
|
|
|
|
|
|
|
let flagString = '';
|
|
|
|
if (RegExpPrototypeGetHasIndices(regex)) flagString += 'd';
|
|
|
|
if (RegExpPrototypeGetGlobal(regex)) flagString += 'g';
|
|
|
|
if (RegExpPrototypeGetIgnoreCase(regex)) flagString += 'i';
|
|
|
|
if (RegExpPrototypeGetMultiline(regex)) flagString += 'm';
|
|
|
|
if (RegExpPrototypeGetDotAll(regex)) flagString += 's';
|
|
|
|
if (RegExpPrototypeGetUnicode(regex)) flagString += 'u';
|
|
|
|
if (RegExpPrototypeGetSticky(regex)) flagString += 'y';
|
|
|
|
|
|
|
|
const { RegExp: RegExpFromAnotherRealm } = getInternalGlobal();
|
2024-09-26 00:35:18 +10:00
|
|
|
const crossRealmRegex = new RegExpFromAnotherRealm(RegExpPrototypeGetSource(regex), flagString);
|
|
|
|
crossRealmRegexes.set(regex, crossRealmRegex);
|
|
|
|
return crossRealmRegex;
|
2022-12-14 15:48:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function SideEffectFreeRegExpPrototypeSymbolReplace(regex, string, replacement) {
|
2024-09-26 00:35:18 +10:00
|
|
|
return getCrossRealmRegex(regex)[SymbolReplace](string, replacement);
|
2022-12-14 15:48:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function SideEffectFreeRegExpPrototypeSymbolSplit(regex, string, limit = undefined) {
|
2024-09-26 00:35:18 +10:00
|
|
|
return getCrossRealmRegex(regex)[SymbolSplit](string, limit);
|
2022-12-14 15:48:50 +01:00
|
|
|
}
|
|
|
|
|
2022-12-14 00:02:51 +01:00
|
|
|
/**
|
|
|
|
* Helper function to lazy-load an initialize-once value.
|
|
|
|
* @template T Return value of initializer
|
|
|
|
* @param {()=>T} initializer Initializer of the lazily loaded value.
|
|
|
|
* @returns {()=>T}
|
|
|
|
*/
|
|
|
|
function getLazy(initializer) {
|
|
|
|
let value;
|
|
|
|
let initialized = false;
|
|
|
|
return function() {
|
|
|
|
if (initialized === false) {
|
|
|
|
value = initializer();
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-21 13:26:16 -05:00
|
|
|
// Setup user-facing NODE_V8_COVERAGE environment variable that writes
|
|
|
|
// ScriptCoverage objects to a specified directory.
|
|
|
|
function setupCoverageHooks(dir) {
|
|
|
|
const cwd = require('internal/process/execution').tryGetCwd();
|
|
|
|
const { resolve } = require('path');
|
|
|
|
const coverageDirectory = resolve(cwd, dir);
|
|
|
|
const { sourceMapCacheToObject } =
|
|
|
|
require('internal/source_map/source_map_cache');
|
|
|
|
|
|
|
|
if (process.features.inspector) {
|
|
|
|
internalBinding('profiler').setCoverageDirectory(coverageDirectory);
|
|
|
|
internalBinding('profiler').setSourceMapCacheGetter(sourceMapCacheToObject);
|
|
|
|
} else {
|
|
|
|
process.emitWarning('The inspector is disabled, ' +
|
|
|
|
'coverage could not be collected',
|
|
|
|
'Warning');
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return coverageDirectory;
|
|
|
|
}
|
|
|
|
|
2025-04-20 00:54:48 -05:00
|
|
|
// Returns the number of ones in the binary representation of the decimal
|
|
|
|
// number.
|
|
|
|
function countBinaryOnes(n) {
|
|
|
|
// Count the number of bits set in parallel, which is faster than looping
|
|
|
|
n = n - ((n >>> 1) & 0x55555555);
|
|
|
|
n = (n & 0x33333333) + ((n >>> 2) & 0x33333333);
|
|
|
|
return ((n + (n >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCIDR(address, netmask, family) {
|
|
|
|
let ones = 0;
|
|
|
|
let split = '.';
|
|
|
|
let range = 10;
|
|
|
|
let groupLength = 8;
|
|
|
|
let hasZeros = false;
|
|
|
|
let lastPos = 0;
|
|
|
|
|
|
|
|
if (family === 'IPv6') {
|
|
|
|
split = ':';
|
|
|
|
range = 16;
|
|
|
|
groupLength = 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < netmask.length; i++) {
|
|
|
|
if (netmask[i] !== split) {
|
|
|
|
if (i + 1 < netmask.length) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
const part = StringPrototypeSlice(netmask, lastPos, i);
|
|
|
|
lastPos = i + 1;
|
|
|
|
if (part !== '') {
|
|
|
|
if (hasZeros) {
|
|
|
|
if (part !== '0') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const binary = NumberParseInt(part, range);
|
|
|
|
const binaryOnes = countBinaryOnes(binary);
|
|
|
|
ones += binaryOnes;
|
|
|
|
if (binaryOnes !== groupLength) {
|
|
|
|
if (StringPrototypeIncludes(binary.toString(2), '01')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
hasZeros = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${address}/${ones}`;
|
|
|
|
}
|
2023-06-05 12:33:13 -04:00
|
|
|
|
|
|
|
const handleTypes = ['TCP', 'TTY', 'UDP', 'FILE', 'PIPE', 'UNKNOWN'];
|
|
|
|
function guessHandleType(fd) {
|
|
|
|
const type = _guessHandleType(fd);
|
|
|
|
return handleTypes[type];
|
|
|
|
}
|
|
|
|
|
2023-08-07 16:28:56 +02:00
|
|
|
class WeakReference {
|
|
|
|
#weak = null;
|
2024-05-23 21:45:18 +02:00
|
|
|
// eslint-disable-next-line no-unused-private-class-members
|
2023-08-07 16:28:56 +02:00
|
|
|
#strong = null;
|
|
|
|
#refCount = 0;
|
|
|
|
constructor(object) {
|
|
|
|
this.#weak = new SafeWeakRef(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
incRef() {
|
|
|
|
this.#refCount++;
|
|
|
|
if (this.#refCount === 1) {
|
|
|
|
const derefed = this.#weak.deref();
|
|
|
|
if (derefed !== undefined) {
|
|
|
|
this.#strong = derefed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.#refCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
decRef() {
|
|
|
|
this.#refCount--;
|
|
|
|
if (this.#refCount === 0) {
|
|
|
|
this.#strong = null;
|
|
|
|
}
|
|
|
|
return this.#refCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
get() {
|
|
|
|
return this.#weak.deref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-04 02:46:11 +01:00
|
|
|
const encodingsMap = { __proto__: null };
|
|
|
|
for (let i = 0; i < encodings.length; ++i)
|
|
|
|
encodingsMap[encodings[i]] = i;
|
|
|
|
|
2025-04-18 01:14:24 +08:00
|
|
|
/**
|
|
|
|
* Reassigns the .name property of a function.
|
|
|
|
* Should be used when function can't be initially defined with desired name
|
|
|
|
* or when desired name should include `#`, `[`, `]`, etc.
|
|
|
|
* @param {string | symbol} name
|
|
|
|
* @param {Function} fn
|
|
|
|
* @param {object} [descriptor]
|
|
|
|
* @returns {Function} the same function, renamed
|
|
|
|
*/
|
|
|
|
function assignFunctionName(name, fn, descriptor = kEmptyObject) {
|
|
|
|
if (typeof name !== 'string') {
|
|
|
|
const symbolDescription = SymbolPrototypeGetDescription(name);
|
|
|
|
assert(symbolDescription !== undefined, 'Attempted to name function after descriptionless Symbol');
|
|
|
|
name = `[${symbolDescription}]`;
|
|
|
|
}
|
|
|
|
return ObjectDefineProperty(fn, 'name', {
|
|
|
|
__proto__: null,
|
|
|
|
writable: false,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
...ObjectGetOwnPropertyDescriptor(fn, 'name'),
|
|
|
|
...descriptor,
|
|
|
|
value: name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-12 08:37:32 -07:00
|
|
|
module.exports = {
|
2022-12-14 00:02:51 +01:00
|
|
|
getLazy,
|
2017-05-12 08:37:32 -07:00
|
|
|
assertCrypto,
|
2024-10-11 20:41:38 +05:00
|
|
|
assertTypeScript,
|
2025-04-18 01:14:24 +08:00
|
|
|
assignFunctionName,
|
2017-05-12 08:37:32 -07:00
|
|
|
cachedResult,
|
|
|
|
convertToValidSignal,
|
|
|
|
createClassWrapper,
|
|
|
|
decorateErrorStack,
|
2022-01-05 20:01:49 +09:00
|
|
|
defineOperation,
|
2022-12-09 23:37:35 +01:00
|
|
|
defineLazyProperties,
|
|
|
|
defineReplaceableLazyAttribute,
|
2017-05-12 08:37:32 -07:00
|
|
|
deprecate,
|
2024-11-07 03:03:05 -05:00
|
|
|
deprecateInstantiation,
|
2025-06-03 11:09:40 +02:00
|
|
|
deprecateProperty,
|
2017-10-25 12:11:10 -07:00
|
|
|
emitExperimentalWarning,
|
2023-12-04 02:46:11 +01:00
|
|
|
encodingsMap,
|
2022-01-05 20:01:49 +09:00
|
|
|
exposeInterface,
|
2022-12-09 23:37:35 +01:00
|
|
|
exposeLazyInterfaces,
|
2023-03-16 00:21:53 +08:00
|
|
|
exposeNamespace,
|
|
|
|
exposeGetterAndSetter,
|
2017-05-12 08:37:32 -07:00
|
|
|
filterDuplicateStrings,
|
2022-08-05 00:41:48 +02:00
|
|
|
filterOwnProperties,
|
2017-05-12 08:37:32 -07:00
|
|
|
getConstructorOf,
|
2025-04-20 00:54:48 -05:00
|
|
|
getCIDR,
|
2023-09-22 09:37:42 +01:00
|
|
|
getCWDURL,
|
2022-08-05 10:01:32 +02:00
|
|
|
getInternalGlobal,
|
2024-05-19 01:09:09 -04:00
|
|
|
getStructuredStack,
|
2021-04-06 00:18:34 +03:00
|
|
|
getSystemErrorMap,
|
2018-01-17 03:21:16 +08:00
|
|
|
getSystemErrorName,
|
2024-10-19 18:15:16 -05:00
|
|
|
getSystemErrorMessage,
|
2023-06-05 12:33:13 -04:00
|
|
|
guessHandleType,
|
2017-05-12 08:37:32 -07:00
|
|
|
isError,
|
2024-10-04 19:29:34 +02:00
|
|
|
isUnderNodeModules,
|
2024-07-15 19:58:11 +01:00
|
|
|
isMacOS,
|
|
|
|
isWindows,
|
2017-10-15 23:59:53 +08:00
|
|
|
join,
|
2021-06-21 11:34:48 +08:00
|
|
|
lazyDOMException,
|
2021-06-28 16:29:54 +08:00
|
|
|
lazyDOMExceptionClass,
|
2017-05-12 08:37:32 -07:00
|
|
|
normalizeEncoding,
|
2018-09-10 09:57:15 -04:00
|
|
|
once,
|
2017-05-12 08:37:32 -07:00
|
|
|
promisify,
|
2022-08-05 10:01:32 +02:00
|
|
|
SideEffectFreeRegExpPrototypeExec,
|
2022-12-14 15:48:50 +01:00
|
|
|
SideEffectFreeRegExpPrototypeSymbolReplace,
|
|
|
|
SideEffectFreeRegExpPrototypeSymbolSplit,
|
2019-12-03 23:02:49 -05:00
|
|
|
sleep,
|
2017-10-15 23:59:53 +08:00
|
|
|
spliceOne,
|
2022-12-21 13:26:16 -05:00
|
|
|
setupCoverageHooks,
|
2017-12-11 06:32:59 -02:00
|
|
|
removeColors,
|
2017-05-12 08:37:32 -07:00
|
|
|
|
|
|
|
// Symbol used to customize promisify conversion
|
|
|
|
customPromisifyArgs: kCustomPromisifyArgsSymbol,
|
|
|
|
|
|
|
|
// Symbol used to provide a custom inspect function for an object as an
|
|
|
|
// alternative to using 'inspect'
|
2019-12-08 18:33:33 +01:00
|
|
|
customInspectSymbol: SymbolFor('nodejs.util.inspect.custom'),
|
2017-05-12 08:37:32 -07:00
|
|
|
|
|
|
|
// Used by the buffer module to capture an internal reference to the
|
|
|
|
// default isEncoding implementation, just in case userland overrides it.
|
2018-02-26 15:46:50 +01:00
|
|
|
kIsEncodingSymbol: Symbol('kIsEncodingSymbol'),
|
2022-02-11 18:30:47 +01:00
|
|
|
kVmBreakFirstLineSymbol: Symbol('kVmBreakFirstLineSymbol'),
|
|
|
|
|
2022-05-21 17:48:37 +08:00
|
|
|
kEmptyObject,
|
2022-02-11 18:30:47 +01:00
|
|
|
kEnumerableProperty,
|
2022-08-05 00:41:48 +02:00
|
|
|
setOwnProperty,
|
2023-04-05 13:13:23 +02:00
|
|
|
pendingDeprecate,
|
2023-08-07 16:28:56 +02:00
|
|
|
WeakReference,
|
2017-05-12 08:37:32 -07:00
|
|
|
};
|