aboutsummaryrefslogtreecommitdiff
path: root/src/js/node/util.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/node/util.js')
-rw-r--r--src/js/node/util.js343
1 files changed, 10 insertions, 333 deletions
diff --git a/src/js/node/util.js b/src/js/node/util.js
index b19dce6fe..c544cb049 100644
--- a/src/js/node/util.js
+++ b/src/js/node/util.js
@@ -1,20 +1,13 @@
// Hardcoded module "node:util"
const types = require("node:util/types");
+/** @type {import('node-inspect-extracted')} */
+const utl = require("internal/util/inspect");
var cjs_exports = {};
-function isBufferInterface({ copy, fill, readUint8 }) {
- return typeof copy === "function" && typeof fill === "function" && typeof readUint8 === "function";
-}
-
function isBuffer(value) {
- return (
- Buffer.isBuffer(value) ||
- // incase it ends up as a browserify buffer
- (typeof value === "object" && isBufferInterface(value || {}))
- );
+ return Buffer.isBuffer(value);
}
-
function isFunction(value) {
return typeof value === "function";
}
@@ -22,55 +15,11 @@ function isFunction(value) {
const deepEquals = Bun.deepEquals;
const isDeepStrictEqual = (a, b) => deepEquals(a, b, true);
var getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors;
-var formatRegExp = /%[sdjfoc%]/g;
-// This function is nowhere near what Node.js does but it is close enough of a shim.
-function formatWithOptions(inspectOptions, f) {
- if (!isString(f)) {
- var objects = [];
- for (var i = 1; i < arguments.length; i++) {
- objects.push(inspect(arguments[i], inspectOptions));
- }
- return objects.join(" ");
- }
- var i = 2;
- var args = arguments;
- var len = args.length;
- var str = String(f).replace(formatRegExp, function (x2) {
- if (x2 === "%%") return "%";
- if (i >= len) return x2;
- switch (x2) {
- case "%s":
- return String(args[i++]);
- case "%f":
- return Number(args[i++]);
- case "%d":
- return Math.round(Number(args[i++]));
- case "%j":
- try {
- return JSON.stringify(args[i++]);
- } catch (_) {
- return "[Circular]";
- }
- case "%o":
- return inspect(args[i++], { showHidden: true, showProxy: true, ...inspectOptions });
- case "%O":
- return inspect(args[i++], { showHidden: true, showProxy: true, ...inspectOptions });
- default:
- return x2;
- }
- });
- for (var x = args[i]; i < len; x = args[++i]) {
- if (isNull(x) || !isObject(x)) {
- str += " " + x;
- } else {
- str += " " + inspect(x, inspectOptions);
- }
- }
- return str;
-}
-function format(...args) {
- return formatWithOptions({}, ...args);
-}
+
+const inspect = utl.inspect;
+const formatWithOptions = utl.formatWithOptions;
+const format = utl.format;
+const stripVTControlCharacters = utl.stripVTControlCharacters;
function deprecate(fn, msg, code) {
if (process.noDeprecation === true) {
@@ -123,277 +72,6 @@ function debuglog(set) {
}
return debugs[set];
}
-var kInspectCustom = Symbol.for("nodejs.util.inspect.custom");
-function inspect(obj, opts) {
- var ctx = {
- seen: [],
- stylize: stylizeNoColor,
- };
- if (arguments.length >= 3) ctx.depth = arguments[2];
- if (arguments.length >= 4) ctx.colors = arguments[3];
- if (isBoolean(opts)) {
- ctx.showHidden = opts;
- } else if (opts) {
- _extend(ctx, opts);
- }
- if (isUndefined(ctx.showHidden)) ctx.showHidden = false;
- if (isUndefined(ctx.depth)) ctx.depth = 2;
- if (isUndefined(ctx.colors)) ctx.colors = false;
- if (isUndefined(ctx.customInspect)) ctx.customInspect = true;
- if (ctx.colors) ctx.stylize = stylizeWithColor;
- return formatValue(ctx, obj, ctx.depth);
-}
-inspect.colors = {
- bold: [1, 22],
- italic: [3, 23],
- underline: [4, 24],
- inverse: [7, 27],
- white: [37, 39],
- grey: [90, 39],
- black: [30, 39],
- blue: [34, 39],
- cyan: [36, 39],
- green: [32, 39],
- magenta: [35, 39],
- red: [31, 39],
- yellow: [33, 39],
-};
-inspect.styles = {
- special: "cyan",
- number: "yellow",
- boolean: "yellow",
- undefined: "grey",
- null: "bold",
- string: "green",
- date: "magenta",
- regexp: "red",
-};
-inspect.custom = kInspectCustom;
-// JS polyfill doesnt support all these options
-inspect.defaultOptions = {
- showHidden: false,
- depth: 2,
- colors: false,
- customInspect: true,
- showProxy: false,
- maxArrayLength: 100,
- maxStringLength: 10000,
- breakLength: 80,
- compact: 3,
- sorted: false,
- getters: false,
- numericSeparator: false,
-};
-function stylizeWithColor(str, styleType) {
- const style = inspect.styles[styleType];
- if (style !== undefined) {
- const color = inspect.colors[style];
- if (color !== undefined) return `\u001b[${color[0]}m${str}\u001b[${color[1]}m`;
- }
- return str;
-}
-function stylizeNoColor(str, styleType) {
- return str;
-}
-function arrayToHash(array) {
- var hash = {};
- array.forEach(function (val, idx) {
- hash[val] = true;
- });
- return hash;
-}
-function formatValue(ctx, value, recurseTimes) {
- if (ctx.customInspect && value) {
- const customInspect = value[kInspectCustom];
- if (isFunction(customInspect)) {
- var ret = customInspect.call(value, recurseTimes, ctx, inspect);
- if (!isString(ret)) {
- ret = formatValue(ctx, ret, recurseTimes);
- }
- return ret;
- }
- }
- var primitive = formatPrimitive(ctx, value);
- if (primitive) {
- return primitive;
- }
- var keys = Object.keys(value).concat(Object.getOwnPropertySymbols(value));
- var visibleKeys = arrayToHash(keys);
- if (ctx.showHidden) {
- keys = Object.getOwnPropertyNames(value);
- }
- if (isError(value) && (keys.indexOf("message") >= 0 || keys.indexOf("description") >= 0)) {
- return formatError(value);
- }
- if (keys.length === 0) {
- if (isFunction(value)) {
- var name = value.name ? ": " + value.name : "";
- return ctx.stylize("[Function" + name + "]", "special");
- }
- if (isRegExp(value)) {
- return ctx.stylize(RegExp.prototype.toString.call(value), "regexp");
- }
- if (isDate(value)) {
- return ctx.stylize(Date.prototype.toString.call(value), "date");
- }
- if (isError(value)) {
- return formatError(value);
- }
- }
- var base = "",
- array = false,
- braces = ["{", "}"];
- if ($isArray(value)) {
- array = true;
- braces = ["[", "]"];
- }
- if (isFunction(value)) {
- var n = value.name ? ": " + value.name : "";
- base = " [Function" + n + "]";
- }
- if (isRegExp(value)) {
- base = " " + RegExp.prototype.toString.call(value);
- }
- if (isDate(value)) {
- base = " " + Date.prototype.toUTCString.call(value);
- }
- if (isError(value)) {
- base = " " + formatError(value);
- }
- if (keys.length === 0 && (!array || value.length == 0)) {
- return braces[0] + base + braces[1];
- }
- if (recurseTimes < 0) {
- if (isRegExp(value)) {
- return ctx.stylize(RegExp.prototype.toString.call(value), "regexp");
- } else {
- return ctx.stylize("[Object]", "special");
- }
- }
- ctx.seen.push(value);
- var output;
- if (array) {
- output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
- } else {
- output = keys.map(function (key) {
- return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
- });
- }
- ctx.seen.pop();
- return reduceToSingleString(output, base, braces);
-}
-function formatPrimitive(ctx, value) {
- if (isUndefined(value)) return ctx.stylize("undefined", "undefined");
- if (isString(value)) {
- var simple = "'" + JSON.stringify(value).replace(/^"|"$/g, "").replace(/'/g, "\\'").replace(/\\"/g, '"') + "'";
- return ctx.stylize(simple, "string");
- }
- if (isNumber(value)) return ctx.stylize("" + value, "number");
- if (isBoolean(value)) return ctx.stylize("" + value, "boolean");
- if (isNull(value)) return ctx.stylize("null", "null");
-}
-function formatError(value) {
- return "[" + Error.prototype.toString.call(value) + "]";
-}
-function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
- var output = [];
- for (var i = 0, l = value.length; i < l; ++i) {
- if (hasOwnProperty(value, String(i))) {
- output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, String(i), true));
- } else {
- output.push("");
- }
- }
- keys.forEach(function (key) {
- if (!key.match(/^\d+$/)) {
- output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, key, true));
- }
- });
- return output;
-}
-function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
- var name, str, desc;
- desc = Object.getOwnPropertyDescriptor(value, key) || {
- value: value[key],
- };
- if (desc.get) {
- if (desc.set) {
- str = ctx.stylize("[Getter/Setter]", "special");
- } else {
- str = ctx.stylize("[Getter]", "special");
- }
- } else {
- if (desc.set) {
- str = ctx.stylize("[Setter]", "special");
- }
- }
- if (!hasOwnProperty(visibleKeys, key)) {
- name = "[" + (typeof key === "symbol" ? key.description : key) + "]";
- }
- if (typeof key === "symbol") {
- name = "[" + ctx.stylize(`Symbol(${key.description})`, "string") + "]";
- }
- if (!str) {
- if (ctx.seen.indexOf(desc.value) < 0) {
- if (isNull(recurseTimes)) {
- str = formatValue(ctx, desc.value, null);
- } else {
- str = formatValue(ctx, desc.value, recurseTimes - 1);
- }
- if (str.indexOf("\n") > -1) {
- if (array) {
- str = str
- .split("\n")
- .map(function (line) {
- return " " + line;
- })
- .join("\n")
- .substr(2);
- } else {
- str =
- "\n" +
- str
- .split("\n")
- .map(function (line) {
- return " " + line;
- })
- .join("\n");
- }
- }
- } else {
- str = ctx.stylize("[Circular]", "special");
- }
- }
- if (isUndefined(name)) {
- if (array && key.match(/^\d+$/)) {
- return str;
- }
- name = JSON.stringify("" + key);
- if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
- name = name.substr(1, name.length - 2);
- name = ctx.stylize(name, "name");
- } else {
- name = name
- .replace(/'/g, "\\'")
- .replace(/\\"/g, '"')
- .replace(/(^"|"$)/g, "'");
- name = ctx.stylize(name, "string");
- }
- }
- return name + ": " + str;
-}
-function reduceToSingleString(output, base, braces) {
- var numLinesEst = 0;
- var length = output.reduce(function (prev, cur) {
- numLinesEst++;
- if (cur.indexOf("\n") >= 0) numLinesEst++;
- return prev + cur.replace(/\u001b\[\d\d?m/g, "").length + 1;
- }, 0);
- if (length > 60) {
- return braces[0] + (base === "" ? "" : base + "\n ") + " " + output.join(",\n ") + " " + braces[1];
- }
- return braces[0] + base + " " + output.join(", ") + " " + braces[1];
-}
function isBoolean(arg) {
return typeof arg === "boolean";
@@ -469,9 +147,7 @@ var _extend = function (origin, add) {
}
return origin;
};
-function hasOwnProperty(obj, prop) {
- return Object.prototype.hasOwnProperty.call(obj, prop);
-}
+
var kCustomPromisifiedSymbol = Symbol.for("util.promisify.custom");
var promisify = function promisify(original) {
if (typeof original !== "function") throw new TypeError('The "original" argument must be of type Function');
@@ -566,6 +242,7 @@ var toUSVString = input => {
export default Object.assign(cjs_exports, {
format,
formatWithOptions,
+ stripVTControlCharacters,
deprecate,
debuglog,
_extend,