Skip to content

Commit

Permalink
More node tests passing (#16269)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfgithub authored Jan 11, 2025
1 parent d9ed436 commit 1e67665
Show file tree
Hide file tree
Showing 11 changed files with 1,075 additions and 29 deletions.
16 changes: 10 additions & 6 deletions src/js/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2607,14 +2607,18 @@ function getStringWidth(str, removeControlChars = true) {
}

// Regex used for ansi escape code splitting
// Adopted from https://github.com/chalk/ansi-regex/blob/HEAD/index.js
// License: MIT, authors: @sindresorhus, Qix-, arjunmehta and LitoMore
// Ref: https://github.com/chalk/ansi-regex/blob/f338e1814144efb950276aac84135ff86b72dc8e/index.js
// License: MIT by Sindre Sorhus <[email protected]>
// Matches all ansi escape code sequences in a string
const ansiPattern =
const ansiPattern = new RegExp(
"[\\u001B\\u009B][[\\]()#;?]*" +
"(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*" +
"|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)" +
"|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))";
"(?:(?:(?:(?:;[-a-zA-Z\\d\\/\\#&.:=?%@~_]+)*" +
"|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/\\#&.:=?%@~_]*)*)?" +
"(?:\\u0007|\\u001B\\u005C|\\u009C))" +
"|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?" +
"[\\dA-PR-TZcf-nq-uy=><~]))",
"g",
);
const ansi = new RegExp(ansiPattern, "g");
/** Remove all VT control characters. Use to estimate displayed string width. */
function stripVTControlCharacters(str) {
Expand Down
72 changes: 49 additions & 23 deletions src/js/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ const formatWithOptions = utl.formatWithOptions;
const format = utl.format;
const stripVTControlCharacters = utl.stripVTControlCharacters;

const codesWarned = new Set();
function deprecate(fn, msg, code) {
if (process.noDeprecation === true) {
return fn;
}
if (code !== undefined) validateString(code, "code");

var warned = false;
function deprecated() {
Expand All @@ -46,7 +48,15 @@ function deprecate(fn, msg, code) {
} else if (process.traceDeprecation) {
console.trace(msg);
} else {
console.error(msg);
if (code !== undefined) {
// only warn for each code once
if (codesWarned.has(code)) {
process.emitWarning(msg, "DeprecationWarning", code);
}
codesWarned.add(code);
} else {
process.emitWarning(msg, "DeprecationWarning");
}
}
warned = true;
}
Expand Down Expand Up @@ -149,7 +159,13 @@ var inherits = function inherits(ctor, superCtor) {
if (superCtor.prototype === undefined) {
throw $ERR_INVALID_ARG_TYPE("superCtor.prototype", "object", superCtor.prototype);
}
ctor.super_ = superCtor;
Object.defineProperty(ctor, "super_", {
// @ts-ignore
__proto__: null,
value: superCtor,
writable: true,
configurable: true,
});
Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
};
var _extend = function (origin, add) {
Expand All @@ -172,30 +188,40 @@ function callbackifyOnRejected(reason, cb) {
return cb(reason);
}
function callbackify(original) {
if (typeof original !== "function") {
throw new TypeError('The "original" argument must be of type Function');
}
function callbackified() {
var args = Array.prototype.slice.$call(arguments);
var maybeCb = args.pop();
if (typeof maybeCb !== "function") {
throw new TypeError("The last argument must be of type Function");
}
var self = this;
var cb = function () {
return maybeCb.$apply(self, arguments);
};
const { validateFunction } = require("internal/validators");
validateFunction(original, "original");

// We DO NOT return the promise as it gives the user a false sense that
// the promise is actually somehow related to the callback's execution
// and that the callback throwing will reject the promise.
function callbackified(...args) {
const maybeCb = Array.prototype.pop.$call(args);
validateFunction(maybeCb, "last argument");
const cb = Function.prototype.bind.$call(maybeCb, this);
// In true node style we process the callback on `nextTick` with all the
// implications (stack, `uncaughtException`, `async_hooks`)
original.$apply(this, args).then(
function (ret) {
process.nextTick(cb, null, ret);
},
function (rej) {
process.nextTick(callbackifyOnRejected, rej, cb);
},
ret => process.nextTick(cb, null, ret),
rej => process.nextTick(callbackifyOnRejected, rej, cb),
);
}
Object.setPrototypeOf(callbackified, Object.getPrototypeOf(original));
Object.defineProperties(callbackified, getOwnPropertyDescriptors(original));

const descriptors = Object.getOwnPropertyDescriptors(original);
// It is possible to manipulate a functions `length` or `name` property. This
// guards against the manipulation.
if (typeof descriptors.length.value === "number") {
descriptors.length.value++;
}
if (typeof descriptors.name.value === "string") {
descriptors.name.value += "Callbackified";
}
const propertiesValues = Object.values(descriptors);
for (let i = 0; i < propertiesValues.length; i++) {
// We want to use null-prototype objects to not rely on globally mutable
// %Object.prototype%.
Object.setPrototypeOf(propertiesValues[i], null);
}
Object.defineProperties(callbackified, descriptors);
return callbackified;
}
var toUSVString = input => {
Expand Down
Loading

0 comments on commit 1e67665

Please sign in to comment.