0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

fix: use static Reflect methods in nodeGlobalThis proxy (#17696)

Co-authored-by: David Sherret <dsherret@gmail.com>
This commit is contained in:
Kamil Ogórek 2023-02-09 01:11:12 +01:00 committed by GitHub
parent 286e5d0be9
commit ef9b66950f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 32 deletions

View file

@ -5,3 +5,6 @@ Download http://localhost:4545/npm/registry/@types/node/node-18.8.2.tgz
Check file:///[WILDCARD]/npm/compare_globals/main.ts Check file:///[WILDCARD]/npm/compare_globals/main.ts
true true
[] []
5
undefined
undefined

View file

@ -12,3 +12,16 @@ type _TestHasNodeJsGlobal = NodeJS.Architecture;
const controller = new AbortController(); const controller = new AbortController();
controller.abort("reason"); // in the NodeJS declaration it doesn't have a reason controller.abort("reason"); // in the NodeJS declaration it doesn't have a reason
// Super edge case where some Node code deletes a global where the
// Node code has its own global and the Deno code has the same global,
// but it's different. Basically if some Node code deletes
// one of these globals then we don't want it to suddenly inherit
// the Deno global.
globals.withNodeGlobalThis((nodeGlobalThis: any) => {
(globalThis as any).setTimeout = 5;
console.log(setTimeout);
delete nodeGlobalThis["setTimeout"];
console.log(nodeGlobalThis["setTimeout"]); // should be undefined
console.log(globalThis["setTimeout"]); // should be undefined
});

View file

@ -11,3 +11,5 @@ type AssertTrue<T extends true> = never;
type _TestHasProcessGlobal = AssertTrue< type _TestHasProcessGlobal = AssertTrue<
typeof globalThis extends { process: any } ? true : false typeof globalThis extends { process: any } ? true : false
>; >;
export function withNodeGlobalThis(action: (global: typeof globalThis) => void): void;

View file

@ -1,3 +1,7 @@
exports.globalThis = globalThis; exports.globalThis = globalThis;
exports.global = global; exports.global = global;
exports.process = process; exports.process = process;
exports.withNodeGlobalThis = function (action) {
action(globalThis);
};

View file

@ -12,8 +12,12 @@ const {
ObjectDefineProperty, ObjectDefineProperty,
Proxy, Proxy,
ReflectDefineProperty, ReflectDefineProperty,
ReflectDeleteProperty,
ReflectGet,
ReflectGetOwnPropertyDescriptor, ReflectGetOwnPropertyDescriptor,
ReflectHas,
ReflectOwnKeys, ReflectOwnKeys,
ReflectSet,
Set, Set,
SetPrototypeHas, SetPrototypeHas,
} = primordials; } = primordials;
@ -27,62 +31,54 @@ function assert(cond) {
let initialized = false; let initialized = false;
const nodeGlobals = {}; const nodeGlobals = {};
const nodeGlobalThis = new Proxy(globalThis, { const nodeGlobalThis = new Proxy(globalThis, {
get(_target, prop, _receiver) { get(target, prop) {
if (prop in nodeGlobals) { if (ReflectHas(nodeGlobals, prop)) {
return nodeGlobals[prop]; return ReflectGet(nodeGlobals, prop);
} else { } else {
return globalThis[prop]; return ReflectGet(target, prop);
} }
}, },
set(_target, prop, value) { set(target, prop, value) {
if (prop in nodeGlobals) { if (ReflectHas(nodeGlobals, prop)) {
nodeGlobals[prop] = value; return ReflectSet(nodeGlobals, prop, value);
} else { } else {
globalThis[prop] = value; return ReflectSet(target, prop, value);
} }
return true;
}, },
deleteProperty(_target, prop) { has(target, prop) {
let success = false; return ReflectHas(nodeGlobals, prop) || ReflectHas(target, prop);
if (prop in nodeGlobals) {
delete nodeGlobals[prop];
success = true;
}
if (prop in globalThis) {
delete globalThis[prop];
success = true;
}
return success;
}, },
ownKeys(_target) { deleteProperty(target, prop) {
const globalThisKeys = ReflectOwnKeys(globalThis); const nodeDeleted = ReflectDeleteProperty(nodeGlobals, prop);
const targetDeleted = ReflectDeleteProperty(target, prop);
return nodeDeleted || targetDeleted;
},
ownKeys(target) {
const targetKeys = ReflectOwnKeys(target);
const nodeGlobalsKeys = ReflectOwnKeys(nodeGlobals); const nodeGlobalsKeys = ReflectOwnKeys(nodeGlobals);
const nodeGlobalsKeySet = new Set(nodeGlobalsKeys); const nodeGlobalsKeySet = new Set(nodeGlobalsKeys);
return [ return [
...ArrayPrototypeFilter( ...ArrayPrototypeFilter(
globalThisKeys, targetKeys,
(k) => !SetPrototypeHas(nodeGlobalsKeySet, k), (k) => !SetPrototypeHas(nodeGlobalsKeySet, k),
), ),
...nodeGlobalsKeys, ...nodeGlobalsKeys,
]; ];
}, },
defineProperty(_target, prop, desc) { defineProperty(target, prop, desc) {
if (prop in nodeGlobals) { if (ReflectHas(nodeGlobals, prop)) {
return ReflectDefineProperty(nodeGlobals, prop, desc); return ReflectDefineProperty(nodeGlobals, prop, desc);
} else { } else {
return ReflectDefineProperty(globalThis, prop, desc); return ReflectDefineProperty(target, prop, desc);
} }
}, },
getOwnPropertyDescriptor(_target, prop) { getOwnPropertyDescriptor(target, prop) {
if (prop in nodeGlobals) { if (ReflectHas(nodeGlobals, prop)) {
return ReflectGetOwnPropertyDescriptor(nodeGlobals, prop); return ReflectGetOwnPropertyDescriptor(nodeGlobals, prop);
} else { } else {
return ReflectGetOwnPropertyDescriptor(globalThis, prop); return ReflectGetOwnPropertyDescriptor(target, prop);
} }
}, },
has(_target, prop) {
return prop in nodeGlobals || prop in globalThis;
},
}); });
const nativeModuleExports = ObjectCreate(null); const nativeModuleExports = ObjectCreate(null);