Skip to content
Merged
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
47 changes: 47 additions & 0 deletions ext/node/polyfills/internal/util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const {
ObjectDefineProperty,
ObjectFreeze,
ObjectGetPrototypeOf,
ObjectGetOwnPropertyDescriptor,
ObjectGetOwnPropertyDescriptors,
ObjectPrototypeIsPrototypeOf,
ObjectSetPrototypeOf,
Expand Down Expand Up @@ -167,6 +168,50 @@ export function convertToValidSignal(signal) {

const codesWarned = new SafeSet();

const experimentalWarnings = new SafeSet();

export function emitExperimentalWarning(feature, messagePrefix, code, ctor) {
if (SetPrototypeHas(experimentalWarnings, feature)) return;
SetPrototypeAdd(experimentalWarnings, feature);
let msg =
`${feature} is an experimental feature and might change at any time`;
if (messagePrefix) {
msg = messagePrefix + msg;
}
globalThis.process.emitWarning(msg, "ExperimentalWarning", code, ctor);
}

const pendingCodesWarned = new SafeSet();

// Internal deprecator for pending --pending-deprecation. Emits the warning only
// when --pending-deprecation is set and --no-deprecation is not.
export function pendingDeprecate(fn, msg, code) {
function deprecated(...args) {
const process = globalThis.process;
if (
process.execArgv?.includes("--pending-deprecation") &&
!process.noDeprecation
) {
if (code !== undefined) {
if (!SetPrototypeHas(pendingCodesWarned, code)) {
process.emitWarning(msg, "DeprecationWarning", code, deprecated);
SetPrototypeAdd(pendingCodesWarned, code);
}
} else {
process.emitWarning(msg, "DeprecationWarning", deprecated);
}
}
return ReflectApply(fn, this, args);
}

ObjectDefineProperty(deprecated, "length", {
__proto__: null,
...ObjectGetOwnPropertyDescriptor(fn, "length"),
});

return deprecated;
}

export function deprecateInstantiation(Constructor, deprecationCode, ...args) {
if (!SetPrototypeHas(codesWarned, deprecationCode)) {
SetPrototypeAdd(codesWarned, deprecationCode);
Expand Down Expand Up @@ -230,11 +275,13 @@ export default {
customInspectSymbol,
customPromisifyArgs,
deprecateInstantiation,
emitExperimentalWarning,
isError,
kEmptyObject,
kEnumerableProperty,
normalizeEncoding,
once,
pendingDeprecate,
promisify,
removeColors,
sleep,
Expand Down
34 changes: 25 additions & 9 deletions ext/node/polyfills/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
NumberPrototypeToString,
ObjectCreate,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor,
ObjectKeys,
ObjectSetPrototypeOf,
ReflectApply,
Expand Down Expand Up @@ -201,8 +202,16 @@ const codesWarned = new SafeSet();
// 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.
// deno-lint-ignore no-explicit-any
export function deprecate(fn: any, msg: string, code?: any) {
export function deprecate(
// deno-lint-ignore no-explicit-any
fn: any,
msg: string,
// deno-lint-ignore no-explicit-any
code?: any,
{ modifyPrototype = true }: { __proto__: null; modifyPrototype?: boolean } = {
__proto__: null,
},
) {
process ??= lazyLoadProcess();
if (process.noDeprecation === true) {
return fn;
Expand Down Expand Up @@ -233,13 +242,20 @@ export function deprecate(fn: any, msg: string, code?: any) {
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"),
});
}

return deprecated;
Expand Down
2 changes: 2 additions & 0 deletions tests/node_compat/config.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -3240,6 +3240,8 @@
"parallel/test-utf8-scripts.js": {},
"parallel/test-util-convert-signal-to-exit-code.mjs": {},
"parallel/test-util-deprecate-invalid-code.js": {},
"parallel/test-util-deprecate.js": {},
"parallel/test-util-emit-experimental-warning.js": {},
"parallel/test-util-getcallsites-preparestacktrace.js": {},
"parallel/test-util-getcallsites.js": {},
"parallel/test-util-inherits.js": {},
Expand Down
Loading