From ab64a7047b445fda242aceaf3ec518cde046305b Mon Sep 17 00:00:00 2001 From: Kuitos Date: Fri, 15 Oct 2021 14:03:04 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20rename=20variable=20for=20code?= =?UTF-8?q?=20readability=20(#1765)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sandbox/proxySandbox.ts | 67 +++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/src/sandbox/proxySandbox.ts b/src/sandbox/proxySandbox.ts index 57e9c6d..2810b21 100644 --- a/src/sandbox/proxySandbox.ts +++ b/src/sandbox/proxySandbox.ts @@ -57,11 +57,11 @@ const unscopables = { Float32Array: true, }; -type SymbolTarget = 'target' | 'rawWindow'; +type SymbolTarget = 'target' | 'globalContext'; type FakeWindow = Window & Record; -function createFakeWindow(global: Window) { +function createFakeWindow(globalContext: Window) { // map always has the fastest performance in has check scenario // see https://jsperf.com/array-indexof-vs-set-has/23 const propertiesWithGetter = new Map(); @@ -72,13 +72,13 @@ function createFakeWindow(global: Window) { see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/getOwnPropertyDescriptor > A property cannot be reported as non-configurable, if it does not exists as an own property of the target object or if it exists as a configurable own property of the target object. */ - Object.getOwnPropertyNames(global) + Object.getOwnPropertyNames(globalContext) .filter((p) => { - const descriptor = Object.getOwnPropertyDescriptor(global, p); + const descriptor = Object.getOwnPropertyDescriptor(globalContext, p); return !descriptor?.configurable; }) .forEach((p) => { - const descriptor = Object.getOwnPropertyDescriptor(global, p); + const descriptor = Object.getOwnPropertyDescriptor(globalContext, p); if (descriptor) { const hasGetter = Object.prototype.hasOwnProperty.call(descriptor, 'get'); @@ -183,19 +183,18 @@ export default class ProxySandbox implements SandBox { this.type = SandBoxType.Proxy; const { updatedValueSet } = this; - const rawWindow = globalContext; - const { fakeWindow, propertiesWithGetter } = createFakeWindow(rawWindow); + const { fakeWindow, propertiesWithGetter } = createFakeWindow(globalContext); const descriptorTargetMap = new Map(); - const hasOwnProperty = (key: PropertyKey) => fakeWindow.hasOwnProperty(key) || rawWindow.hasOwnProperty(key); + const hasOwnProperty = (key: PropertyKey) => fakeWindow.hasOwnProperty(key) || globalContext.hasOwnProperty(key); const proxy = new Proxy(fakeWindow, { set: (target: FakeWindow, p: PropertyKey, value: any): boolean => { if (this.sandboxRunning) { this.registerRunningApp(name, proxy); - // We must kept its description while the property existed in rawWindow before - if (!target.hasOwnProperty(p) && rawWindow.hasOwnProperty(p)) { - const descriptor = Object.getOwnPropertyDescriptor(rawWindow, p); + // We must kept its description while the property existed in globalContext before + if (!target.hasOwnProperty(p) && globalContext.hasOwnProperty(p)) { + const descriptor = Object.getOwnPropertyDescriptor(globalContext, p); const { writable, configurable, enumerable } = descriptor!; if (writable) { Object.defineProperty(target, p, { @@ -212,7 +211,7 @@ export default class ProxySandbox implements SandBox { if (variableWhiteList.indexOf(p) !== -1) { // @ts-ignore - rawWindow[p] = value; + globalContext[p] = value; } updatedValueSet.add(p); @@ -251,42 +250,38 @@ export default class ProxySandbox implements SandBox { (process.env.NODE_ENV === 'test' && (p === 'mockTop' || p === 'mockSafariTop')) ) { // if your master app in an iframe context, allow these props escape the sandbox - if (rawWindow === rawWindow.parent) { + if (globalContext === globalContext.parent) { return proxy; } - return (rawWindow as any)[p]; + return (globalContext as any)[p]; } - // proxy.hasOwnProperty would invoke getter firstly, then its value represented as rawWindow.hasOwnProperty + // proxy.hasOwnProperty would invoke getter firstly, then its value represented as globalContext.hasOwnProperty if (p === 'hasOwnProperty') { return hasOwnProperty; } - // mark the symbol to document while accessing as document.createElement could know is invoked by which sandbox for dynamic append patcher - if (p === 'document' || p === 'eval') { - switch (p) { - case 'document': - return document; - case 'eval': - // eslint-disable-next-line no-eval - return eval; - // no default - } + if (p === 'document') { + return document; + } + + if (p === 'eval') { + return eval; } // eslint-disable-next-line no-nested-ternary const value = propertiesWithGetter.has(p) - ? (rawWindow as any)[p] + ? (globalContext as any)[p] : p in target ? (target as any)[p] - : (rawWindow as any)[p]; - return getTargetValue(rawWindow, value); + : (globalContext as any)[p]; + return getTargetValue(globalContext, value); }, // trap in operator // see https://github.com/styled-components/styled-components/blob/master/packages/styled-components/src/constants.js#L12 has(target: FakeWindow, p: string | number | symbol): boolean { - return p in unscopables || p in target || p in rawWindow; + return p in unscopables || p in target || p in globalContext; }, getOwnPropertyDescriptor(target: FakeWindow, p: string | number | symbol): PropertyDescriptor | undefined { @@ -301,9 +296,9 @@ export default class ProxySandbox implements SandBox { return descriptor; } - if (rawWindow.hasOwnProperty(p)) { - const descriptor = Object.getOwnPropertyDescriptor(rawWindow, p); - descriptorTargetMap.set(p, 'rawWindow'); + if (globalContext.hasOwnProperty(p)) { + const descriptor = Object.getOwnPropertyDescriptor(globalContext, p); + descriptorTargetMap.set(p, 'globalContext'); // A property cannot be reported as non-configurable, if it does not exists as an own property of the target object if (descriptor && !descriptor.configurable) { descriptor.configurable = true; @@ -316,7 +311,7 @@ export default class ProxySandbox implements SandBox { // trap to support iterator with sandbox ownKeys(target: FakeWindow): ArrayLike { - return uniq(Reflect.ownKeys(rawWindow).concat(Reflect.ownKeys(target))); + return uniq(Reflect.ownKeys(globalContext).concat(Reflect.ownKeys(target))); }, defineProperty(target: Window, p: PropertyKey, attributes: PropertyDescriptor): boolean { @@ -326,8 +321,8 @@ export default class ProxySandbox implements SandBox { otherwise it would cause a TypeError with illegal invocation. */ switch (from) { - case 'rawWindow': - return Reflect.defineProperty(rawWindow, p, attributes); + case 'globalContext': + return Reflect.defineProperty(globalContext, p, attributes); default: return Reflect.defineProperty(target, p, attributes); } @@ -348,7 +343,7 @@ export default class ProxySandbox implements SandBox { // makes sure `window instanceof Window` returns truthy in micro app getPrototypeOf() { - return Reflect.getPrototypeOf(rawWindow); + return Reflect.getPrototypeOf(globalContext); }, });