Commit 67798df8842 for nodejs

commit 67798df8842a0c079a861e66e902d23f76bfead7
Author: James M Snell <jasnell@gmail.com>
Date:   Sun Nov 15 17:37:26 2026 +0000

    util: share helpers between debounce() and throttle()

    util.debounce() and util.throttle() duplicated the construction of the
    AbortError used for cancel()/abort, and the property definitions for
    the returned function's control methods, state getters, length, and
    name. Move both into internal/util/wrapped_function.

    Assisted-by: Claude
    Signed-off-by: James M Snell <jasnell@gmail.com>
    PR-URL: https://github.com/nodejs/node/pull/67495
    Reviewed-By: Guilherme Araújo <arauujogui@gmail.com>
    Reviewed-By: Filip Skokan <panva.ip@gmail.com>
    Reviewed-By: Robert Nagy <ronagy@icloud.com>
    Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>

diff --git a/lib/internal/util/debounce.js b/lib/internal/util/debounce.js
index c2c05ccc394..ad6ca24f2b4 100644
--- a/lib/internal/util/debounce.js
+++ b/lib/internal/util/debounce.js
@@ -2,7 +2,6 @@

 const {
   ArrayPrototypePush,
-  ObjectDefineProperties,
   PromiseReject,
   PromiseWithResolvers,
   ReflectApply,
@@ -20,6 +19,10 @@ const {
   validateObject,
 } = require('internal/validators');
 const { addAbortListener } = require('internal/events/abort_listener');
+const {
+  createAbortError,
+  defineWrapperProperties,
+} = require('internal/util/wrapped_function');
 const { kEmptyObject } = require('internal/util');
 const { TIMEOUT_MAX } = require('internal/timers');
 const { clearTimeout, setTimeout } = require('timers');
@@ -85,17 +88,12 @@ function debounce(fn, wait, options = kEmptyObject) {

   function cancel(reason) {
     if (timeout === undefined) return;
-    const error = reason === undefined ?
-      new AbortError() :
-      new AbortError(undefined, { __proto__: null, cause: reason });
-    cancelWithError(error);
+    cancelWithError(createAbortError(reason));
   }

   function abort() {
     if (abortError === undefined) {
-      abortError = signal.reason === undefined ?
-        new AbortError() :
-        new AbortError(undefined, { __proto__: null, cause: signal.reason });
+      abortError = createAbortError(signal.reason);
     }
     cancelWithError(abortError);
   }
@@ -206,44 +204,18 @@ function debounce(fn, wait, options = kEmptyObject) {
     return call.promise;
   }

-  function createFn(value) {
-    return {
-      __proto__: null,
-      configurable: true,
-      enumerable: true,
-      writable: true,
-      value,
-    };
-  }
-
-  ObjectDefineProperties(debounced, {
+  defineWrapperProperties(debounced, fn, {
     __proto__: null,
-    cancel: createFn(cancel),
-    flush: createFn(flush),
-    ref: createFn(ref),
-    unref: createFn(unref),
-    pending: {
-      __proto__: null,
-      enumerable: false,
-      get() {
-        return pendingCalls.length === 0 ? null : pendingCalls[pendingCalls.length - 1].promise;
-      },
-    },
-    pendingCount: {
-      __proto__: null,
-      enumerable: false,
-      get() { return pendingCalls.length; },
-    },
-    length: {
-      __proto__: null,
-      configurable: true,
-      value: fn.length,
-    },
-    name: {
-      __proto__: null,
-      configurable: true,
-      value: fn.name,
+    cancel,
+    flush,
+    ref,
+    unref,
+  }, {
+    __proto__: null,
+    pending() {
+      return pendingCalls.length === 0 ? null : pendingCalls[pendingCalls.length - 1].promise;
     },
+    pendingCount() { return pendingCalls.length; },
   });

   return debounced;
diff --git a/lib/internal/util/throttle.js b/lib/internal/util/throttle.js
index ec3b360dd81..17eb8cc0186 100644
--- a/lib/internal/util/throttle.js
+++ b/lib/internal/util/throttle.js
@@ -3,7 +3,6 @@
 const {
   ArrayPrototypePush,
   ArrayPrototypeSlice,
-  ObjectDefineProperties,
   PromisePrototypeThen,
   PromiseReject,
   PromiseResolve,
@@ -27,6 +26,10 @@ const {
   validateOneOf,
 } = require('internal/validators');
 const { addAbortListener } = require('internal/events/abort_listener');
+const {
+  createAbortError,
+  defineWrapperProperties,
+} = require('internal/util/wrapped_function');
 const { kEmptyObject } = require('internal/util');
 const { TIMEOUT_MAX } = require('internal/timers');
 const { clearTimeout, setTimeout } = require('timers');
@@ -139,17 +142,12 @@ function throttle(fn, limit, interval, options = kEmptyObject) {
   }

   function cancel(reason) {
-    const error = reason === undefined ?
-      new AbortError() :
-      new AbortError(undefined, { __proto__: null, cause: reason });
-    cancelWithError(error);
+    cancelWithError(createAbortError(reason));
   }

   function abort() {
     if (abortError === undefined) {
-      abortError = signal.reason === undefined ?
-        new AbortError() :
-        new AbortError(undefined, { __proto__: null, cause: signal.reason });
+      abortError = createAbortError(signal.reason);
     }
     cancelWithError(abortError);
   }
@@ -330,51 +328,21 @@ function throttle(fn, limit, interval, options = kEmptyObject) {
     return call.promise;
   }

-  function createFn(value) {
-    return {
-      __proto__: null,
-      configurable: true,
-      enumerable: true,
-      writable: true,
-      value,
-    };
-  }
-
-  ObjectDefineProperties(throttled, {
+  defineWrapperProperties(throttled, fn, {
     __proto__: null,
-    cancel: createFn(cancel),
-    hasImmediateCapacity: createFn(hasImmediateCapacity),
-    ref: createFn(ref),
-    unref: createFn(unref),
-    pending: {
-      __proto__: null,
-      enumerable: false,
-      get() {
-        return pendingCount() === 0 ? null :
-          pendingCalls[pendingCalls.length - 1].promise;
-      },
-    },
-    pendingCount: {
-      __proto__: null,
-      enumerable: false,
-      get: pendingCount,
-    },
-    activeCount: {
-      __proto__: null,
-      enumerable: false,
-      get() {
-        return activeCount;
-      },
-    },
-    length: {
-      __proto__: null,
-      configurable: true,
-      value: fn.length,
+    cancel,
+    hasImmediateCapacity,
+    ref,
+    unref,
+  }, {
+    __proto__: null,
+    pending() {
+      return pendingCount() === 0 ? null :
+        pendingCalls[pendingCalls.length - 1].promise;
     },
-    name: {
-      __proto__: null,
-      configurable: true,
-      value: fn.name,
+    pendingCount,
+    activeCount() {
+      return activeCount;
     },
   });

diff --git a/lib/internal/util/wrapped_function.js b/lib/internal/util/wrapped_function.js
new file mode 100644
index 00000000000..27b3c796ede
--- /dev/null
+++ b/lib/internal/util/wrapped_function.js
@@ -0,0 +1,69 @@
+'use strict';
+
+// Helpers shared by util.debounce() and util.throttle().
+
+const {
+  ObjectDefineProperties,
+} = primordials;
+
+const {
+  AbortError,
+} = require('internal/errors');
+
+/**
+ * Creates the AbortError used to reject calls that were cancelled or aborted.
+ * @param {any} reason
+ * @returns {AbortError}
+ */
+function createAbortError(reason) {
+  return reason === undefined ?
+    new AbortError() :
+    new AbortError(undefined, { __proto__: null, cause: reason });
+}
+
+/**
+ * Defines the control methods, state getters, `length`, and `name` on a
+ * function returned by util.debounce() or util.throttle().
+ * @param {Function} wrapper The function returned to the user.
+ * @param {Function} fn The wrapped user function.
+ * @param {{ [name: string]: Function }} methods Enumerable, writable methods.
+ * @param {{ [name: string]: Function }} getters Non-enumerable getters.
+ * @returns {Function} `wrapper`.
+ */
+function defineWrapperProperties(wrapper, fn, methods, getters) {
+  const descriptors = {
+    __proto__: null,
+    length: {
+      __proto__: null,
+      configurable: true,
+      value: fn.length,
+    },
+    name: {
+      __proto__: null,
+      configurable: true,
+      value: fn.name,
+    },
+  };
+  for (const name in methods) {
+    descriptors[name] = {
+      __proto__: null,
+      configurable: true,
+      enumerable: true,
+      writable: true,
+      value: methods[name],
+    };
+  }
+  for (const name in getters) {
+    descriptors[name] = {
+      __proto__: null,
+      enumerable: false,
+      get: getters[name],
+    };
+  }
+  return ObjectDefineProperties(wrapper, descriptors);
+}
+
+module.exports = {
+  createAbortError,
+  defineWrapperProperties,
+};