🎨 rename variable for code readability (#1765)

This commit is contained in:
Kuitos 2021-10-15 14:03:04 +08:00 committed by GitHub
parent a95b252ef0
commit ab64a7047b

View File

@ -57,11 +57,11 @@ const unscopables = {
Float32Array: true, Float32Array: true,
}; };
type SymbolTarget = 'target' | 'rawWindow'; type SymbolTarget = 'target' | 'globalContext';
type FakeWindow = Window & Record<PropertyKey, any>; type FakeWindow = Window & Record<PropertyKey, any>;
function createFakeWindow(global: Window) { function createFakeWindow(globalContext: Window) {
// map always has the fastest performance in has check scenario // map always has the fastest performance in has check scenario
// see https://jsperf.com/array-indexof-vs-set-has/23 // see https://jsperf.com/array-indexof-vs-set-has/23
const propertiesWithGetter = new Map<PropertyKey, boolean>(); const propertiesWithGetter = new Map<PropertyKey, boolean>();
@ -72,13 +72,13 @@ function createFakeWindow(global: Window) {
see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/getOwnPropertyDescriptor 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. > 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) => { .filter((p) => {
const descriptor = Object.getOwnPropertyDescriptor(global, p); const descriptor = Object.getOwnPropertyDescriptor(globalContext, p);
return !descriptor?.configurable; return !descriptor?.configurable;
}) })
.forEach((p) => { .forEach((p) => {
const descriptor = Object.getOwnPropertyDescriptor(global, p); const descriptor = Object.getOwnPropertyDescriptor(globalContext, p);
if (descriptor) { if (descriptor) {
const hasGetter = Object.prototype.hasOwnProperty.call(descriptor, 'get'); const hasGetter = Object.prototype.hasOwnProperty.call(descriptor, 'get');
@ -183,19 +183,18 @@ export default class ProxySandbox implements SandBox {
this.type = SandBoxType.Proxy; this.type = SandBoxType.Proxy;
const { updatedValueSet } = this; const { updatedValueSet } = this;
const rawWindow = globalContext; const { fakeWindow, propertiesWithGetter } = createFakeWindow(globalContext);
const { fakeWindow, propertiesWithGetter } = createFakeWindow(rawWindow);
const descriptorTargetMap = new Map<PropertyKey, SymbolTarget>(); const descriptorTargetMap = new Map<PropertyKey, SymbolTarget>();
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, { const proxy = new Proxy(fakeWindow, {
set: (target: FakeWindow, p: PropertyKey, value: any): boolean => { set: (target: FakeWindow, p: PropertyKey, value: any): boolean => {
if (this.sandboxRunning) { if (this.sandboxRunning) {
this.registerRunningApp(name, proxy); this.registerRunningApp(name, proxy);
// We must kept its description while the property existed in rawWindow before // We must kept its description while the property existed in globalContext before
if (!target.hasOwnProperty(p) && rawWindow.hasOwnProperty(p)) { if (!target.hasOwnProperty(p) && globalContext.hasOwnProperty(p)) {
const descriptor = Object.getOwnPropertyDescriptor(rawWindow, p); const descriptor = Object.getOwnPropertyDescriptor(globalContext, p);
const { writable, configurable, enumerable } = descriptor!; const { writable, configurable, enumerable } = descriptor!;
if (writable) { if (writable) {
Object.defineProperty(target, p, { Object.defineProperty(target, p, {
@ -212,7 +211,7 @@ export default class ProxySandbox implements SandBox {
if (variableWhiteList.indexOf(p) !== -1) { if (variableWhiteList.indexOf(p) !== -1) {
// @ts-ignore // @ts-ignore
rawWindow[p] = value; globalContext[p] = value;
} }
updatedValueSet.add(p); updatedValueSet.add(p);
@ -251,42 +250,38 @@ export default class ProxySandbox implements SandBox {
(process.env.NODE_ENV === 'test' && (p === 'mockTop' || p === 'mockSafariTop')) (process.env.NODE_ENV === 'test' && (p === 'mockTop' || p === 'mockSafariTop'))
) { ) {
// if your master app in an iframe context, allow these props escape the sandbox // 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 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') { if (p === 'hasOwnProperty') {
return 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') {
if (p === 'document' || p === 'eval') {
switch (p) {
case 'document':
return document; return document;
case 'eval':
// eslint-disable-next-line no-eval
return eval;
// no default
} }
if (p === 'eval') {
return eval;
} }
// eslint-disable-next-line no-nested-ternary // eslint-disable-next-line no-nested-ternary
const value = propertiesWithGetter.has(p) const value = propertiesWithGetter.has(p)
? (rawWindow as any)[p] ? (globalContext as any)[p]
: p in target : p in target
? (target as any)[p] ? (target as any)[p]
: (rawWindow as any)[p]; : (globalContext as any)[p];
return getTargetValue(rawWindow, value); return getTargetValue(globalContext, value);
}, },
// trap in operator // trap in operator
// see https://github.com/styled-components/styled-components/blob/master/packages/styled-components/src/constants.js#L12 // 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 { 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 { getOwnPropertyDescriptor(target: FakeWindow, p: string | number | symbol): PropertyDescriptor | undefined {
@ -301,9 +296,9 @@ export default class ProxySandbox implements SandBox {
return descriptor; return descriptor;
} }
if (rawWindow.hasOwnProperty(p)) { if (globalContext.hasOwnProperty(p)) {
const descriptor = Object.getOwnPropertyDescriptor(rawWindow, p); const descriptor = Object.getOwnPropertyDescriptor(globalContext, p);
descriptorTargetMap.set(p, 'rawWindow'); 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 // 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) { if (descriptor && !descriptor.configurable) {
descriptor.configurable = true; descriptor.configurable = true;
@ -316,7 +311,7 @@ export default class ProxySandbox implements SandBox {
// trap to support iterator with sandbox // trap to support iterator with sandbox
ownKeys(target: FakeWindow): ArrayLike<string | symbol> { ownKeys(target: FakeWindow): ArrayLike<string | symbol> {
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 { 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. otherwise it would cause a TypeError with illegal invocation.
*/ */
switch (from) { switch (from) {
case 'rawWindow': case 'globalContext':
return Reflect.defineProperty(rawWindow, p, attributes); return Reflect.defineProperty(globalContext, p, attributes);
default: default:
return Reflect.defineProperty(target, p, attributes); 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 // makes sure `window instanceof Window` returns truthy in micro app
getPrototypeOf() { getPrototypeOf() {
return Reflect.getPrototypeOf(rawWindow); return Reflect.getPrototypeOf(globalContext);
}, },
}); });