From dab489ea1ff63f281ff9f89f3651cee81f5bc683 Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Mon, 30 Jun 2025 16:00:16 -0300 Subject: [PATCH] lib: modify the prototype based on cond on deprecate --- lib/async_hooks.js | 4 ++-- lib/internal/util.js | 27 +++++++++++++++------------ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/async_hooks.js b/lib/async_hooks.js index 7dc7bfab328498..4216ee3f240c48 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -262,10 +262,10 @@ class AsyncResource { enumerable: true, get: deprecate(function() { return self; - }, 'The asyncResource property on bound functions is deprecated', 'DEP0172'), + }, 'The asyncResource property on bound functions is deprecated', 'DEP0172', false), set: deprecate(function(val) { self = val; - }, 'The asyncResource property on bound functions is deprecated', 'DEP0172'), + }, 'The asyncResource property on bound functions is deprecated', 'DEP0172', false), }, }); return bound; diff --git a/lib/internal/util.js b/lib/internal/util.js index 180ca49b3207eb..7d7e6e81eb492d 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -169,7 +169,7 @@ function pendingDeprecate(fn, msg, code) { // Mark that a method should not be used. // Returns a modified function which warns once by default. // If --no-deprecation is set, then it is a no-op. -function deprecate(fn, msg, code, useEmitSync) { +function deprecate(fn, msg, code, useEmitSync, modifyPrototype = true) { // Lazy-load to avoid a circular dependency. if (validateString === undefined) ({ validateString } = require('internal/validators')); @@ -192,19 +192,22 @@ function deprecate(fn, msg, code, useEmitSync) { return ReflectApply(fn, this, args); } - // The wrapper will keep the same prototype as fn to maintain prototype chain - ObjectSetPrototypeOf(deprecated, fn); - if (fn.prototype) { - // Setting this (rather than using Object.setPrototype, as above) ensures - // that calling the unwrapped constructor gives an instanceof the wrapped - // constructor. - deprecated.prototype = fn.prototype; + if (modifyPrototype) { + // The wrapper will keep the same prototype as fn to maintain prototype chain + ObjectSetPrototypeOf(deprecated, fn); + if (fn.prototype) { + // Setting this (rather than using Object.setPrototype, as above) ensures + // that calling the unwrapped constructor gives an instanceof the wrapped + // constructor. + deprecated.prototype = fn.prototype; + } + + ObjectDefineProperty(deprecated, 'length', { + __proto__: null, + ...ObjectGetOwnPropertyDescriptor(fn, 'length'), + }); } - ObjectDefineProperty(deprecated, 'length', { - __proto__: null, - ...ObjectGetOwnPropertyDescriptor(fn, 'length'), - }); return deprecated; }