From eb7727819ae1dc4995b6ebcfffa07e6618b133f9 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Thu, 28 Aug 2025 14:05:52 -0800 Subject: [PATCH] node:util: move deprecate to internal file so its faster to import (#22197) Co-authored-by: Meghan Denny --- cmake/sources/JavaScriptSources.txt | 1 + src/js/internal/util/deprecate.ts | 45 +++++++++++++++++++++++++++++ src/js/node/_http_outgoing.ts | 2 +- src/js/node/assert.ts | 2 +- src/js/node/dgram.ts | 2 +- src/js/node/util.ts | 41 +------------------------- 6 files changed, 50 insertions(+), 43 deletions(-) create mode 100644 src/js/internal/util/deprecate.ts diff --git a/cmake/sources/JavaScriptSources.txt b/cmake/sources/JavaScriptSources.txt index 4202470fab..3d5ff4538d 100644 --- a/cmake/sources/JavaScriptSources.txt +++ b/cmake/sources/JavaScriptSources.txt @@ -97,6 +97,7 @@ src/js/internal/tls.ts src/js/internal/tty.ts src/js/internal/url.ts src/js/internal/util/colors.ts +src/js/internal/util/deprecate.ts src/js/internal/util/inspect.d.ts src/js/internal/util/inspect.js src/js/internal/util/mime.ts diff --git a/src/js/internal/util/deprecate.ts b/src/js/internal/util/deprecate.ts new file mode 100644 index 0000000000..14adc5ec5d --- /dev/null +++ b/src/js/internal/util/deprecate.ts @@ -0,0 +1,45 @@ +const { validateString } = require("internal/validators"); + +const codesWarned = new Set(); + +function getDeprecationWarningEmitter(code, msg, deprecated, shouldEmitWarning = () => true) { + let warned = false; + return function () { + if (!warned && shouldEmitWarning()) { + warned = true; + if (code !== undefined) { + if (!codesWarned.has(code)) { + process.emitWarning(msg, "DeprecationWarning", code, deprecated); + codesWarned.add(code); + } + } else { + process.emitWarning(msg, "DeprecationWarning", deprecated); + } + } + }; +} + +function deprecate(fn, msg, code) { + // Lazy-load to avoid a circular dependency. + if (code !== undefined) validateString(code, "code"); + + const emitDeprecationWarning = getDeprecationWarningEmitter(code, msg, deprecated); + + function deprecated(...args) { + if (!process.noDeprecation) { + emitDeprecationWarning(); + } + if (new.target) { + return Reflect.construct(fn, args, new.target); + } + return fn.$apply(this, args); + } + + // The wrapper will keep the same prototype as fn to maintain prototype chain + Object.setPrototypeOf(deprecated, fn); + return deprecated; +} + +export default { + deprecate, +}; diff --git a/src/js/node/_http_outgoing.ts b/src/js/node/_http_outgoing.ts index e9530ef708..8a695ce873 100644 --- a/src/js/node/_http_outgoing.ts +++ b/src/js/node/_http_outgoing.ts @@ -1,6 +1,6 @@ const { Stream } = require("internal/stream"); const { isUint8Array, validateString } = require("internal/validators"); -const { deprecate } = require("node:util"); +const { deprecate } = require("internal/util/deprecate"); const ObjectDefineProperty = Object.defineProperty; const ObjectKeys = Object.keys; const { diff --git a/src/js/node/assert.ts b/src/js/node/assert.ts index 4c7a2d6b31..454b2b3b0d 100644 --- a/src/js/node/assert.ts +++ b/src/js/node/assert.ts @@ -974,7 +974,7 @@ var CallTracker; Object.defineProperty(assert, "CallTracker", { get() { if (CallTracker === undefined) { - const { deprecate } = require("node:util"); + const { deprecate } = require("internal/util/deprecate"); CallTracker = deprecate(require("internal/assert/calltracker"), "assert.CallTracker is deprecated.", "DEP0173"); } return CallTracker; diff --git a/src/js/node/dgram.ts b/src/js/node/dgram.ts index dc83eea4b2..ae40dfb472 100644 --- a/src/js/node/dgram.ts +++ b/src/js/node/dgram.ts @@ -57,7 +57,7 @@ const { isIP } = require("node:net"); const EventEmitter = require("node:events"); -const { deprecate } = require("node:util"); +const { deprecate } = require("internal/util/deprecate"); const SymbolDispose = Symbol.dispose; const SymbolAsyncDispose = Symbol.asyncDispose; diff --git a/src/js/node/util.ts b/src/js/node/util.ts index 470f2ca6b6..0d4e6ca1d2 100644 --- a/src/js/node/util.ts +++ b/src/js/node/util.ts @@ -5,6 +5,7 @@ const utl = require("internal/util/inspect"); const { promisify } = require("internal/promisify"); const { validateString, validateOneOf } = require("internal/validators"); const { MIMEType, MIMEParams } = require("internal/util/mime"); +const { deprecate } = require("internal/util/deprecate"); const internalErrorName = $newZigFunction("node_util_binding.zig", "internalErrorName", 1); const parseEnv = $newZigFunction("node_util_binding.zig", "parseEnv", 1); @@ -31,46 +32,6 @@ const formatWithOptions = utl.formatWithOptions; const format = utl.format; const stripVTControlCharacters = utl.stripVTControlCharacters; -const codesWarned = new Set(); - -function getDeprecationWarningEmitter(code, msg, deprecated, shouldEmitWarning = () => true) { - let warned = false; - return function () { - if (!warned && shouldEmitWarning()) { - warned = true; - if (code !== undefined) { - if (!codesWarned.has(code)) { - process.emitWarning(msg, "DeprecationWarning", code, deprecated); - codesWarned.add(code); - } - } else { - process.emitWarning(msg, "DeprecationWarning", deprecated); - } - } - }; -} - -function deprecate(fn, msg, code) { - // Lazy-load to avoid a circular dependency. - if (code !== undefined) validateString(code, "code"); - - const emitDeprecationWarning = getDeprecationWarningEmitter(code, msg, deprecated); - - function deprecated(...args) { - if (!process.noDeprecation) { - emitDeprecationWarning(); - } - if (new.target) { - return Reflect.construct(fn, args, new.target); - } - return fn.$apply(this, args); - } - - // The wrapper will keep the same prototype as fn to maintain prototype chain - Object.setPrototypeOf(deprecated, fn); - return deprecated; -} - var debugs = {}; var debugEnvRegex = /^$/; if (process.env.NODE_DEBUG) {