mirror of
https://github.com/nodejs/node.git
synced 2025-05-13 10:54:13 +00:00

I added a new custom ESLint rule to fix these problems. We have a lot of replaceable codes with primordials. Accessing built-in objects is restricted by existing rule (no-restricted-globals), but accessing property in the built-in objects is not restricted right now. We manually review codes that can be replaced by primordials, but there's a lot of code that actually needs to be fixed. We have often made pull requests to replace the primordials with. Restrict accessing global built-in objects such as `Promise`. Restrict calling static methods such as `Array.from` or `Symbol.for`. Don't restrict prototype methods to prevent false-positive. PR-URL: https://github.com/nodejs/node/pull/35448 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Ben Coe <bencoe@gmail.com>
87 lines
2.0 KiB
JavaScript
87 lines
2.0 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
makeSafe,
|
|
ObjectFreeze,
|
|
SafeSet,
|
|
SafeWeakMap,
|
|
SymbolIterator,
|
|
} = primordials;
|
|
|
|
// TODO(aduh95): Add FinalizationRegistry to primordials
|
|
const SafeFinalizationRegistry = makeSafe(
|
|
globalThis.FinalizationRegistry,
|
|
class SafeFinalizationRegistry extends globalThis.FinalizationRegistry {}
|
|
);
|
|
|
|
// TODO(aduh95): Add WeakRef to primordials
|
|
const SafeWeakRef = makeSafe(
|
|
globalThis.WeakRef,
|
|
class SafeWeakRef extends globalThis.WeakRef {}
|
|
);
|
|
|
|
// This class is modified from the example code in the WeakRefs specification:
|
|
// https://github.com/tc39/proposal-weakrefs
|
|
// Licensed under ECMA's MIT-style license, see:
|
|
// https://github.com/tc39/ecma262/blob/master/LICENSE.md
|
|
class IterableWeakMap {
|
|
#weakMap = new SafeWeakMap();
|
|
#refSet = new SafeSet();
|
|
#finalizationGroup = new SafeFinalizationRegistry(cleanup);
|
|
|
|
set(key, value) {
|
|
const entry = this.#weakMap.get(key);
|
|
if (entry) {
|
|
// If there's already an entry for the object represented by "key",
|
|
// the value can be updated without creating a new WeakRef:
|
|
this.#weakMap.set(key, { value, ref: entry.ref });
|
|
} else {
|
|
const ref = new SafeWeakRef(key);
|
|
this.#weakMap.set(key, { value, ref });
|
|
this.#refSet.add(ref);
|
|
this.#finalizationGroup.register(key, {
|
|
set: this.#refSet,
|
|
ref
|
|
}, ref);
|
|
}
|
|
}
|
|
|
|
get(key) {
|
|
return this.#weakMap.get(key)?.value;
|
|
}
|
|
|
|
has(key) {
|
|
return this.#weakMap.has(key);
|
|
}
|
|
|
|
delete(key) {
|
|
const entry = this.#weakMap.get(key);
|
|
if (!entry) {
|
|
return false;
|
|
}
|
|
this.#weakMap.delete(key);
|
|
this.#refSet.delete(entry.ref);
|
|
this.#finalizationGroup.unregister(entry.ref);
|
|
return true;
|
|
}
|
|
|
|
*[SymbolIterator]() {
|
|
for (const ref of this.#refSet) {
|
|
const key = ref.deref();
|
|
if (!key) continue;
|
|
const { value } = this.#weakMap.get(key);
|
|
yield value;
|
|
}
|
|
}
|
|
}
|
|
|
|
function cleanup({ set, ref }) {
|
|
set.delete(ref);
|
|
}
|
|
|
|
ObjectFreeze(IterableWeakMap.prototype);
|
|
|
|
module.exports = {
|
|
IterableWeakMap,
|
|
};
|