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
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const {
} = primordials;

const {
assignFunctionName,
convertToValidSignal,
getSystemErrorName,
kEmptyObject,
Expand Down Expand Up @@ -236,7 +237,7 @@ function exec(command, options, callback) {
}

const customPromiseExecFunction = (orig) => {
return (...args) => {
return assignFunctionName(orig.name, function(...args) {
const { promise, resolve, reject } = PromiseWithResolvers();

promise.child = orig(...args, (err, stdout, stderr) => {
Expand All @@ -250,7 +251,7 @@ const customPromiseExecFunction = (orig) => {
});

return promise;
};
});
};

ObjectDefineProperty(exec, promisify.custom, {
Expand Down
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) {
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import '../common/index.mjs';
import { hasCrypto } from '../common/index.mjs';
import assert from 'node:assert';
import { promisify } from 'node:util';

Expand All @@ -9,6 +9,7 @@ import fs from 'node:fs';
import readline from 'node:readline';
import stream from 'node:stream';
import timers from 'node:timers';
import child_process from 'node:child_process';


assert.strictEqual(
Expand Down Expand Up @@ -38,3 +39,20 @@ assert.strictEqual(
promisify(timers.setTimeout).name,
'setTimeout'
);

assert.strictEqual(
promisify(child_process.exec).name,
'exec'
);
assert.strictEqual(
promisify(child_process.execFile).name,
'execFile'
);

if (hasCrypto) {
const http2 = await import('node:http2');
assert.strictEqual(
promisify(http2.connect).name,
'connect'
);
}
Loading