🎨 optimize current proxy setting logic (#1486)
This commit is contained in:
parent
64337f01e8
commit
761287ac3b
|
|
@ -123,7 +123,11 @@ test('hasOwnProperty should works well', () => {
|
||||||
test('descriptor of non-configurable and non-enumerable property existed in raw window should be the same after modified in sandbox', () => {
|
test('descriptor of non-configurable and non-enumerable property existed in raw window should be the same after modified in sandbox', () => {
|
||||||
Object.defineProperty(window, 'nonConfigurableProp', { configurable: false, writable: true });
|
Object.defineProperty(window, 'nonConfigurableProp', { configurable: false, writable: true });
|
||||||
// eslint-disable-next-line getter-return
|
// eslint-disable-next-line getter-return
|
||||||
Object.defineProperty(window, 'nonConfigurablePropWithAccessor', { configurable: false, get() {}, set() {} });
|
Object.defineProperty(window, 'nonConfigurablePropWithAccessor', {
|
||||||
|
configurable: false,
|
||||||
|
get() {},
|
||||||
|
set() {},
|
||||||
|
});
|
||||||
Object.defineProperty(window, 'enumerableProp', { enumerable: true, writable: true });
|
Object.defineProperty(window, 'enumerableProp', { enumerable: true, writable: true });
|
||||||
Object.defineProperty(window, 'nonEnumerableProp', { enumerable: false, writable: true });
|
Object.defineProperty(window, 'nonEnumerableProp', { enumerable: false, writable: true });
|
||||||
|
|
||||||
|
|
@ -297,6 +301,7 @@ test('the prototype should be kept while we create a function with prototype on
|
||||||
const proxy = new ProxySandbox('new-function').proxy as any;
|
const proxy = new ProxySandbox('new-function').proxy as any;
|
||||||
|
|
||||||
function test() {}
|
function test() {}
|
||||||
|
|
||||||
proxy.fn = test;
|
proxy.fn = test;
|
||||||
expect(proxy.fn === test).toBeFalsy();
|
expect(proxy.fn === test).toBeFalsy();
|
||||||
expect(proxy.fn.prototype).toBe(test.prototype);
|
expect(proxy.fn.prototype).toBe(test.prototype);
|
||||||
|
|
@ -348,6 +353,18 @@ it('should return true while [[GetPrototypeOf]] invoked by proxy object', () =>
|
||||||
expect(Reflect.getPrototypeOf(proxy)).toBe(Object.getPrototypeOf(window));
|
expect(Reflect.getPrototypeOf(proxy)).toBe(Object.getPrototypeOf(window));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should get current running sandbox proxy correctly', async () => {
|
||||||
|
const { proxy } = new ProxySandbox('running');
|
||||||
|
|
||||||
|
await Promise.resolve().then(() => {
|
||||||
|
expect(getCurrentRunningSandboxProxy()).toBeNull();
|
||||||
|
// @ts-ignore
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
const unused = proxy.accessing;
|
||||||
|
expect(getCurrentRunningSandboxProxy()).toBe(proxy);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('native window function calling should always be bound with window', () => {
|
it('native window function calling should always be bound with window', () => {
|
||||||
window.nativeWindowFunction = function nativeWindowFunction(this: any) {
|
window.nativeWindowFunction = function nativeWindowFunction(this: any) {
|
||||||
if (this !== undefined && this !== window) {
|
if (this !== undefined && this !== window) {
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
import { isBoundedFunction, isCallable, isConstructable } from '../utils';
|
import { isBoundedFunction, isCallable, isConstructable } from '../utils';
|
||||||
|
|
||||||
let currentRunningSandboxProxy: WindowProxy | null;
|
let currentRunningSandboxProxy: WindowProxy | null = null;
|
||||||
export function getCurrentRunningSandboxProxy() {
|
export function getCurrentRunningSandboxProxy() {
|
||||||
return currentRunningSandboxProxy;
|
return currentRunningSandboxProxy;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -215,6 +215,12 @@ export default class ProxySandbox implements SandBox {
|
||||||
},
|
},
|
||||||
|
|
||||||
get(target: FakeWindow, p: PropertyKey): any {
|
get(target: FakeWindow, p: PropertyKey): any {
|
||||||
|
setCurrentRunningSandboxProxy(proxy);
|
||||||
|
// FIXME if you have any other good ideas
|
||||||
|
// remove the mark in next tick, thus we can identify whether it in micro app or not
|
||||||
|
// this approach is just a workaround, it could not cover all complex cases, such as the micro app runs in the same task context with master in some case
|
||||||
|
nextTick(() => setCurrentRunningSandboxProxy(null));
|
||||||
|
|
||||||
if (p === Symbol.unscopables) return unscopables;
|
if (p === Symbol.unscopables) return unscopables;
|
||||||
|
|
||||||
// avoid who using window.window or window.self to escape the sandbox environment to touch the really window
|
// avoid who using window.window or window.self to escape the sandbox environment to touch the really window
|
||||||
|
|
@ -247,11 +253,6 @@ export default class ProxySandbox implements SandBox {
|
||||||
|
|
||||||
// mark the symbol to document while accessing as document.createElement could know is invoked by which sandbox for dynamic append patcher
|
// 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') {
|
if (p === 'document' || p === 'eval') {
|
||||||
setCurrentRunningSandboxProxy(proxy);
|
|
||||||
// FIXME if you have any other good ideas
|
|
||||||
// remove the mark in next tick, thus we can identify whether it in micro app or not
|
|
||||||
// this approach is just a workaround, it could not cover all complex cases, such as the micro app runs in the same task context with master in some case
|
|
||||||
nextTick(() => setCurrentRunningSandboxProxy(null));
|
|
||||||
switch (p) {
|
switch (p) {
|
||||||
case 'document':
|
case 'document':
|
||||||
return document;
|
return document;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user