Skip to content

fix(ext/node): use primordials in ext/node/polyfills/internal/util.mjs #29430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2025
Merged
Changes from 1 commit
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
46 changes: 28 additions & 18 deletions ext/node/polyfills/internal/util.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// Copyright 2018-2025 the Deno authors. MIT license.

// TODO(petamoriken): enable prefer-primordials for node polyfills
// deno-lint-ignore-file prefer-primordials

import { validateFunction } from "ext:deno_node/internal/validators.mjs";
import {
normalizeEncoding,
Expand All @@ -17,31 +14,42 @@ import { ERR_UNKNOWN_SIGNAL } from "ext:deno_node/internal/errors.ts";
import { os } from "ext:deno_node/internal_binding/constants.ts";
import { primordials } from "ext:core/mod.js";
const {
ArrayPrototypePush,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectFreeze,
ObjectGetPrototypeOf,
ObjectGetOwnPropertyDescriptors,
ObjectSetPrototypeOf,
Promise,
ReflectApply,
SafeWeakRef,
SymbolFor,
WeakRefPrototypeDeref,
} = primordials;

export const customInspectSymbol = Symbol.for("nodejs.util.inspect.custom");
export const kEnumerableProperty = Object.create(null);
export const customInspectSymbol = SymbolFor("nodejs.util.inspect.custom");
export const kEnumerableProperty = ObjectCreate(null);
kEnumerableProperty.enumerable = true;

export const kEmptyObject = Object.freeze(Object.create(null));
export const kEmptyObject = ObjectFreeze(ObjectCreate(null));

export function once(callback) {
let called = false;
return function (...args) {
if (called) return;
called = true;
Reflect.apply(callback, this, args);
ReflectApply(callback, this, args);
};
}

// In addition to being accessible through util.promisify.custom,
// this symbol is registered globally and can be accessed in any environment as
// Symbol.for('nodejs.util.promisify.custom').
const kCustomPromisifiedSymbol = Symbol.for("nodejs.util.promisify.custom");
const kCustomPromisifiedSymbol = SymbolFor("nodejs.util.promisify.custom");
// This is an internal Node symbol used by functions returning multiple
// arguments, e.g. ['bytesRead', 'buffer'] for fs.read().
const kCustomPromisifyArgsSymbol = Symbol.for(
const kCustomPromisifyArgsSymbol = SymbolFor(
"nodejs.util.promisify.customArgs",
);

Expand All @@ -56,7 +64,8 @@ export function promisify(

validateFunction(fn, "util.promisify.custom");

return Object.defineProperty(fn, kCustomPromisifiedSymbol, {
return ObjectDefineProperty(fn, kCustomPromisifiedSymbol, {
__proto__: null,
value: fn,
enumerable: false,
writable: false,
Expand All @@ -69,7 +78,7 @@ export function promisify(
const argumentNames = original[kCustomPromisifyArgsSymbol];
function fn(...args) {
return new Promise((resolve, reject) => {
args.push((err, ...values) => {
ArrayPrototypePush(args, (err, ...values) => {
if (err) {
return reject(err);
}
Expand All @@ -83,21 +92,22 @@ export function promisify(
resolve(values[0]);
}
});
Reflect.apply(original, this, args);
ReflectApply(original, this, args);
});
}

Object.setPrototypeOf(fn, Object.getPrototypeOf(original));
ObjectSetPrototypeOf(fn, ObjectGetPrototypeOf(original));

Object.defineProperty(fn, kCustomPromisifiedSymbol, {
ObjectDefineProperty(fn, kCustomPromisifiedSymbol, {
__proto__: null,
value: fn,
enumerable: false,
writable: false,
configurable: true,
});
return Object.defineProperties(
return ObjectDefineProperties(
fn,
Object.getOwnPropertyDescriptors(original),
ObjectGetOwnPropertyDescriptors(original),
);
}

Expand Down Expand Up @@ -139,7 +149,7 @@ export class WeakReference {
incRef() {
this.#refCount++;
if (this.#refCount === 1) {
const derefed = this.#weak.deref();
const derefed = WeakRefPrototypeDeref(this.#weak);
if (derefed !== undefined) {
this.#strong = derefed;
}
Expand All @@ -156,7 +166,7 @@ export class WeakReference {
}

get() {
return this.#weak.deref();
return WeakRefPrototypeDeref(this.#weak);
}
}

Expand Down
Loading