From 9eb10aa2169042b9ed1a7a4b90f616acefe33421 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Fri, 8 Dec 2023 11:08:59 +0000 Subject: [PATCH] Simplify patching `util` module [refactor] --- lib/init/module.js | 67 +++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/lib/init/module.js b/lib/init/module.js index 0833a1a6..ba9edcf7 100644 --- a/lib/init/module.js +++ b/lib/init/module.js @@ -60,38 +60,6 @@ function createWrappedRequire(require, filename) { return wrappedRequire; } -// Patches to built-in modules -const modifyBuiltins = { - __proto__: null, - - util(util) { - // Modify `util.promisify()` to record promisified functions - const promisifyOriginal = util.promisify, - {custom} = promisifyOriginal; - function promisify(original) { - const promisifiedFn = promisifyOriginal(original); - // Don't record if has custom promisified function defined using `custom` symbol. - // `util.promisify` doesn't create function internally in that case. - // NB: No need to check `original` is truthy or that `original[custom]` is function, - // as `util.promisify` will have thrown already in these cases. - if (!original[custom]) specialFunctions.set(promisifiedFn, {type: 'promisify', fn: original}); - return promisifiedFn; - } - promisify.custom = custom; - util.promisify = promisify; - - // Modify `util.debuglog()` to record debuglog functions - const debuglogOriginal = util.debuglog; - util.debuglog = function debuglog(set, cb) { - const res = debuglogOriginal.call(this, set, cb); - specialFunctions.set(res, {type: 'debuglog', set, cb}); - return res; - }; - - if (util.debug === debuglogOriginal) util.debug = util.debuglog; - } -}; - /** * If module is a NodeJS built-in module * @param {string} path - Path `require()` called with @@ -107,8 +75,39 @@ function catalogIfBuiltInModule(path, exports) { builtinModulesUncataloged.delete(path); - const modifyBuiltin = modifyBuiltins[path]; - if (modifyBuiltin) modifyBuiltin(exports); + if (path === 'util') patchUtilModule(exports); catalogBuiltInModule(path, exports, globals); } + +/** + * Patch `util.promisify` and `util.debuglog` functions to record functions they return. + * @param {Object} util - `util` module `exports` object + * @returns {undefined} + */ +function patchUtilModule(util) { + // Modify `util.promisify()` to record promisified functions + const promisifyOriginal = util.promisify, + {custom} = promisifyOriginal; + function promisify(original) { + const promisifiedFn = promisifyOriginal(original); + // Don't record if has custom promisified function defined using `custom` symbol. + // `util.promisify` doesn't create function internally in that case. + // NB: No need to check `original` is truthy or that `original[custom]` is function, + // as `util.promisify` will have thrown already in these cases. + if (!original[custom]) specialFunctions.set(promisifiedFn, {type: 'promisify', fn: original}); + return promisifiedFn; + } + promisify.custom = custom; + util.promisify = promisify; + + // Modify `util.debuglog()` to record debuglog functions + const debuglogOriginal = util.debuglog; + util.debuglog = function debuglog(set, cb) { + const res = debuglogOriginal.call(this, set, cb); + specialFunctions.set(res, {type: 'debuglog', set, cb}); + return res; + }; + + if (util.debug === debuglogOriginal) util.debug = util.debuglog; +}