Skip to content

Commit

Permalink
fix: 修复window被proxy,导致作用域错误
Browse files Browse the repository at this point in the history
  • Loading branch information
fanzehong committed Aug 30, 2022
1 parent 24a8a4c commit 25efbbe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 9 additions & 1 deletion packages/wujie-core/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import { renderElementToContainer } from "./shadow";
import { pushUrlToWindow } from "./sync";
import { documentProxyProperties, rawDocumentQuerySelector } from "./common";
import { WUJIE_TIPS_RELOAD_DISABLED } from "./constant";
import { getTargetValue, anchorElementGenerator, getDegradeIframe, isCallable, warn } from "./utils";
import {
getTargetValue,
anchorElementGenerator,
getDegradeIframe,
isCallable,
checkProxyFunction,
warn,
} from "./utils";

/**
* location href 的set劫持操作
Expand Down Expand Up @@ -58,6 +65,7 @@ export function proxyGenerator(
},

set: (target: Window, p: PropertyKey, value: any) => {
checkProxyFunction(value);
target[p] = value;
return true;
},
Expand Down
11 changes: 10 additions & 1 deletion packages/wujie-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,18 @@ export function isConstructable(fn: () => any | FunctionConstructor) {
return constructable;
}

const setFnCacheMap = new WeakMap<CallableFunction, boolean>();
export function checkProxyFunction(value: any) {
if (isCallable(value) && !isBoundedFunction(value) && !isConstructable(value)) {
if (!setFnCacheMap.has(value)) {
setFnCacheMap.set(value, true);
}
}
}

export function getTargetValue(target: any, p: any): any {
const value = target[p];
if (isCallable(value) && !isBoundedFunction(value) && !isConstructable(value)) {
if (isCallable(value) && !isBoundedFunction(value) && !isConstructable(value) && !setFnCacheMap.has(value)) {
const boundValue = Function.prototype.bind.call(value, target);

for (const key in value) {
Expand Down

0 comments on commit 25efbbe

Please sign in to comment.