Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
🎨 rename variable for code readability (#1765)
  • Loading branch information
kuitos committed Oct 15, 2021
1 parent e90ca04 commit 5e3b05d
Showing 1 changed file with 31 additions and 36 deletions.
67 changes: 31 additions & 36 deletions src/sandbox/proxySandbox.ts
Expand Up @@ -57,11 +57,11 @@ const unscopables = {
Float32Array: true,
};

type SymbolTarget = 'target' | 'rawWindow';
type SymbolTarget = 'target' | 'globalContext';

type FakeWindow = Window & Record<PropertyKey, any>;

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<PropertyKey, boolean>();
Expand All @@ -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');

Expand Down Expand Up @@ -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<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, {
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, {
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -316,7 +311,7 @@ export default class ProxySandbox implements SandBox {

// trap to support iterator with sandbox
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 {
Expand All @@ -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);
}
Expand All @@ -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);
},
});

Expand Down

1 comment on commit 5e3b05d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for qiankun ready!

✅ Preview
https://qiankun-qk9pjtvkh-umijs.vercel.app

Built with commit 5e3b05d.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.