util: add internal `assignFunctionName()` function by LiviaMedeiros · Pull Request #57916 · nodejs/node · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/child_process.js
5 changes: 3 additions & 2 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const {
} = primordials;

const {
assignFunctionName,
assertCrypto,
customInspectSymbol: kInspect,
kEmptyObject,
Expand Down Expand Up @@ -3405,7 +3406,7 @@ function connect(authority, options, listener) {
// Support util.promisify
ObjectDefineProperty(connect, promisify.custom, {
__proto__: null,
value: (authority, options) => {
value: assignFunctionName('connect', function(authority, options) {
Comment thread
jasnell marked this conversation as resolved.
return new Promise((resolve, reject) => {
const server = connect(authority, options, () => {
server.removeListener('error', reject);
Expand All @@ -3414,7 +3415,7 @@ ObjectDefineProperty(connect, promisify.custom, {

server.once('error', reject);
});
},
}),
});

function createSecureServer(options, handler) {
Expand Down
28 changes: 28 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const {
StringPrototypeToUpperCase,
Symbol,
SymbolFor,
SymbolPrototypeGetDescription,
SymbolReplace,
SymbolSplit,
} = primordials;
Expand Down Expand Up @@ -850,10 +851,37 @@ const encodingsMap = { __proto__: null };
for (let i = 0; i < encodings.length; ++i)
encodingsMap[encodings[i]] = i;

/**
* 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,
});
}

module.exports = {
getLazy,
assertCrypto,
assertTypeScript,
assignFunctionName,
cachedResult,
convertToValidSignal,
createClassWrapper,
Expand Down
29 changes: 28 additions & 1 deletion test/parallel/test-internal-util-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require('../common');
const assert = require('assert');
const { types } = require('util');
const { isError } = require('internal/util');
const { assignFunctionName, isError } = require('internal/util');
const vm = require('vm');

// Special cased errors. Test the internal function which is used in
Expand Down Expand Up @@ -35,3 +35,30 @@ const vm = require('vm');
assert(!(differentRealmErr instanceof Error));
assert(isError(differentRealmErr));
}

{
const nameMap = new Map([
[ 'meaningfulName', 'meaningfulName' ],
[ '', '' ],
[ Symbol.asyncIterator, '[Symbol.asyncIterator]' ],
[ Symbol('notWellKnownSymbol'), '[notWellKnownSymbol]' ],
]);
for (const fn of [
() => {},
function() {},
function value() {},
({ value() {} }).value,
new Function(),
Function(),
function() {}.bind(null),
class {},
class value {},
]) {
for (const [ stringOrSymbol, expectedName ] of nameMap) {
const namedFn = assignFunctionName(stringOrSymbol, fn);
assert.strictEqual(fn, namedFn);
assert.strictEqual(namedFn.name, expectedName);
assert.strictEqual(namedFn.bind(null).name, `bound ${expectedName}`);
}
}
}
20 changes: 19 additions & 1 deletion test/parallel/test-util-promisify-custom-names.mjs