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,
|
|
|
|
ArrayIsArray,
|
2020-11-16 16:39:28 +01:00
|
|
|
ArrayPrototypePop,
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeSlice,
|
|
|
|
ArrayPrototypeSort,
|
2020-01-02 19:19:02 +01:00
|
|
|
Error,
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectCreate,
|
|
|
|
ObjectDefineProperties,
|
|
|
|
ObjectDefineProperty,
|
|
|
|
ObjectGetOwnPropertyDescriptor,
|
|
|
|
ObjectGetOwnPropertyDescriptors,
|
|
|
|
ObjectGetPrototypeOf,
|
|
|
|
ObjectSetPrototypeOf,
|
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,
|
2020-11-16 16:39:28 +01:00
|
|
|
RegExpPrototypeTest,
|
|
|
|
SafeMap,
|
|
|
|
SafeSet,
|
|
|
|
StringPrototypeReplace,
|
|
|
|
StringPrototypeToLowerCase,
|
|
|
|
StringPrototypeToUpperCase,
|
2019-11-30 16:55:29 +01:00
|
|
|
Symbol,
|
2019-12-08 18:33:33 +01:00
|
|
|
SymbolFor,
|
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_INVALID_ARG_TYPE,
|
|
|
|
ERR_NO_CRYPTO,
|
|
|
|
ERR_UNKNOWN_SIGNAL
|
|
|
|
},
|
2019-09-16 15:32:15 -05:00
|
|
|
uvErrmapGet,
|
|
|
|
overrideStackTrace,
|
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 {
|
|
|
|
getHiddenValue,
|
|
|
|
setHiddenValue,
|
|
|
|
arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex,
|
2019-12-03 23:02:49 -05:00
|
|
|
decorated_private_symbol: kDecoratedPrivateSymbolIndex,
|
|
|
|
sleep: _sleep
|
2018-09-03 11:16:48 -04:00
|
|
|
} = internalBinding('util');
|
2019-03-19 13:09:42 +08:00
|
|
|
const { isNativeError } = internalBinding('types');
|
2018-08-06 14:40:30 -07:00
|
|
|
|
2017-02-15 13:00:21 -08:00
|
|
|
const noCrypto = !process.versions.openssl;
|
2016-05-31 19:58:31 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
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.
|
2017-02-15 13:00:21 -08:00
|
|
|
function deprecate(fn, msg, code) {
|
2015-06-15 21:51:41 -07:00
|
|
|
if (process.noDeprecation === true) {
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
|
2016-12-04 12:47:01 -08:00
|
|
|
if (code !== undefined && typeof code !== 'string')
|
2018-03-19 13:33:46 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
|
2016-12-04 12:47:01 -08:00
|
|
|
|
2018-02-16 07:28:38 -05:00
|
|
|
let warned = false;
|
2017-02-15 13:00:21 -08:00
|
|
|
function deprecated(...args) {
|
2016-08-18 11:51:10 -07:00
|
|
|
if (!warned) {
|
|
|
|
warned = true;
|
2016-12-04 12:47:01 -08:00
|
|
|
if (code !== undefined) {
|
2019-06-06 17:08:00 -04:00
|
|
|
if (!codesWarned.has(code)) {
|
2017-10-22 12:26:32 -07:00
|
|
|
process.emitWarning(msg, 'DeprecationWarning', code, deprecated);
|
2019-06-06 17:08:00 -04:00
|
|
|
codesWarned.add(code);
|
2017-10-22 12:26:32 -07:00
|
|
|
}
|
2016-12-04 12:47:01 -08:00
|
|
|
} else {
|
|
|
|
process.emitWarning(msg, 'DeprecationWarning', deprecated);
|
|
|
|
}
|
2016-08-18 11:51:10 -07:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-02-15 13:00:21 -08:00
|
|
|
function decorateErrorStack(err) {
|
|
|
|
if (!(isError(err) && err.stack) ||
|
2017-10-26 21:34:15 -07:00
|
|
|
getHiddenValue(err, kDecoratedPrivateSymbolIndex) === true)
|
2015-11-25 22:37:43 +01:00
|
|
|
return;
|
|
|
|
|
2017-10-26 21:34:15 -07:00
|
|
|
const arrow = getHiddenValue(err, kArrowMessagePrivateSymbolIndex);
|
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;
|
2017-10-26 21:34:15 -07:00
|
|
|
setHiddenValue(err, kDecoratedPrivateSymbolIndex, 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
|
|
|
|
|
|
|
// Return undefined if there is no match.
|
2018-02-14 23:48:35 +01:00
|
|
|
// Move the "slow cases" to a separate function to make sure this function gets
|
|
|
|
// inlined properly. That prioritizes the common case.
|
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';
|
2020-11-16 16:39:28 +01: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' ||
|
|
|
|
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';
|
2020-11-16 16:39:28 +01: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';
|
2020-11-16 16:39:28 +01: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' ||
|
2020-11-16 16:39:28 +01: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' ||
|
2020-11-16 16:39:28 +01: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' ||
|
|
|
|
StringPrototypeToLowerCase(`${enc}`) === 'base64url')
|
|
|
|
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
|
|
|
|
2017-10-25 12:11:10 -07:00
|
|
|
function emitExperimentalWarning(feature) {
|
|
|
|
if (experimentalWarnings.has(feature)) return;
|
|
|
|
const msg = `${feature} is an experimental feature. This feature could ` +
|
|
|
|
'change at any time';
|
|
|
|
experimentalWarnings.add(feature);
|
|
|
|
process.emitWarning(msg, 'ExperimentalWarning');
|
|
|
|
}
|
|
|
|
|
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, {
|
2017-07-10 20:55:21 -04:00
|
|
|
name: { value: type.name },
|
|
|
|
length: { 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;
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
signalsToNamesMapping = ObjectCreate(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;
|
|
|
|
}
|
|
|
|
|
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}`;
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
|
2017-06-20 23:20:10 +02:00
|
|
|
function promisify(original) {
|
|
|
|
if (typeof original !== 'function')
|
2018-03-19 13:33:46 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('original', 'Function', original);
|
2017-04-14 18:28:16 +02:00
|
|
|
|
2017-06-20 23:20:10 +02:00
|
|
|
if (original[kCustomPromisifiedSymbol]) {
|
|
|
|
const fn = original[kCustomPromisifiedSymbol];
|
2017-04-14 18:28:16 +02:00
|
|
|
if (typeof fn !== 'function') {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('util.promisify.custom', 'Function', fn);
|
2017-04-14 18:28:16 +02:00
|
|
|
}
|
2019-11-22 18:04:46 +01:00
|
|
|
return ObjectDefineProperty(fn, kCustomPromisifiedSymbol, {
|
2017-04-14 18:28:16 +02:00
|
|
|
value: fn, enumerable: false, writable: false, configurable: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
});
|
2020-11-16 16:39:28 +01:00
|
|
|
ReflectApply(original, this, args);
|
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, {
|
2017-04-14 18:28:16 +02:00
|
|
|
value: fn, enumerable: false, writable: false, configurable: true
|
|
|
|
});
|
2019-11-22 18:04:46 +01:00
|
|
|
return ObjectDefineProperties(
|
2017-06-20 23:20:10 +02:00
|
|
|
fn,
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectGetOwnPropertyDescriptors(original)
|
2017-06-20 23:20:10 +02:00
|
|
|
);
|
2017-04-14 18:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
promisify.custom = kCustomPromisifiedSymbol;
|
|
|
|
|
2017-08-21 23:20:59 -03:00
|
|
|
// The build-in Array#join is slower in v8 6.0
|
|
|
|
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];
|
2020-11-16 16:39:28 +01:00
|
|
|
ArrayPrototypePop(list);
|
2017-10-15 23:59:53 +08:00
|
|
|
}
|
|
|
|
|
2018-03-22 00:30:21 +01:00
|
|
|
const kNodeModulesRE = /^(.*)[\\/]node_modules[\\/]/;
|
|
|
|
|
|
|
|
let getStructuredStack;
|
|
|
|
|
|
|
|
function isInsideNodeModules() {
|
|
|
|
if (getStructuredStack === undefined) {
|
|
|
|
// Lazy-load to avoid a circular dependency.
|
|
|
|
const { runInNewContext } = require('vm');
|
|
|
|
// Use `runInNewContext()` to get something tamper-proof and
|
|
|
|
// side-effect-free. Since this is currently only used for a deprecated API,
|
|
|
|
// the perf implications should be okay.
|
2018-04-14 19:01:37 +02:00
|
|
|
getStructuredStack = runInNewContext(`(function() {
|
2018-03-22 00:30:21 +01:00
|
|
|
Error.stackTraceLimit = Infinity;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const stack = getStructuredStack();
|
|
|
|
|
|
|
|
// Iterate over all stack frames and look for the first one not coming
|
|
|
|
// from inside Node.js itself:
|
2019-11-23 10:09:05 +01:00
|
|
|
if (ArrayIsArray(stack)) {
|
2018-04-25 00:56:35 +02:00
|
|
|
for (const frame of stack) {
|
|
|
|
const filename = frame.getFileName();
|
|
|
|
// If a filename does not start with / or contain \,
|
|
|
|
// it's likely from Node.js core.
|
2020-11-16 16:39:28 +01:00
|
|
|
if (!RegExpPrototypeTest(/^\/|\\/, filename))
|
2018-04-25 00:56:35 +02:00
|
|
|
continue;
|
2020-11-16 16:39:28 +01:00
|
|
|
return RegExpPrototypeTest(kNodeModulesRE, filename);
|
2018-04-25 00:56:35 +02:00
|
|
|
}
|
2018-03-22 00:30:21 +01:00
|
|
|
}
|
2018-04-25 00:56:35 +02:00
|
|
|
return false;
|
2018-03-22 00:30:21 +01:00
|
|
|
}
|
|
|
|
|
2018-09-10 09:57:15 -04:00
|
|
|
function once(callback) {
|
|
|
|
let called = false;
|
|
|
|
return function(...args) {
|
|
|
|
if (called) return;
|
|
|
|
called = true;
|
2020-11-16 16:39:28 +01:00
|
|
|
ReflectApply(callback, this, args);
|
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);
|
|
|
|
}
|
|
|
|
|
2017-05-12 08:37:32 -07:00
|
|
|
module.exports = {
|
|
|
|
assertCrypto,
|
|
|
|
cachedResult,
|
|
|
|
convertToValidSignal,
|
|
|
|
createClassWrapper,
|
|
|
|
decorateErrorStack,
|
|
|
|
deprecate,
|
2017-10-25 12:11:10 -07:00
|
|
|
emitExperimentalWarning,
|
2017-05-12 08:37:32 -07:00
|
|
|
filterDuplicateStrings,
|
|
|
|
getConstructorOf,
|
2018-01-17 03:21:16 +08:00
|
|
|
getSystemErrorName,
|
2017-05-12 08:37:32 -07:00
|
|
|
isError,
|
2018-03-22 00:30:21 +01:00
|
|
|
isInsideNodeModules,
|
2017-10-15 23:59:53 +08:00
|
|
|
join,
|
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,
|
2019-12-03 23:02:49 -05:00
|
|
|
sleep,
|
2017-10-15 23:59:53 +08:00
|
|
|
spliceOne,
|
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'),
|
2019-01-30 23:13:45 +01:00
|
|
|
kVmBreakFirstLineSymbol: Symbol('kVmBreakFirstLineSymbol')
|
2017-05-12 08:37:32 -07:00
|
|
|
};
|