Skip to content

Commit

Permalink
🐛 add getPrototypeOf trap in sandbox to keep the `window instanceof W…
Browse files Browse the repository at this point in the history
…indow` returns true in microapp (#1465)
  • Loading branch information
kuitos authored May 25, 2021
1 parent 9ec2884 commit e14b082
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/sandbox/__tests__/proxySandbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,18 @@ test('falsy values should return as expected', () => {
expect(proxy.nullvar).toBeNull();
expect(proxy.zero).toBe(0);
});

it('should return true while [[GetPrototypeOf]] invoked by proxy object', () => {
// window.__proto__ not equals window prototype in jest environment
// eslint-disable-next-line no-proto
expect(window.__proto__ === Object.getPrototypeOf(window)).toBeFalsy();
// we must to set the prototype of window as jest modified window `__proto__` property but not changed it internal [[Prototype]] property
// eslint-disable-next-line no-proto
Object.setPrototypeOf(window, window.__proto__);

const { proxy } = new ProxySandbox('window-prototype');
expect(proxy instanceof Window).toBeTruthy();
expect(Object.getPrototypeOf(proxy)).toBe(Object.getPrototypeOf(window));
expect(Reflect.getPrototypeOf(proxy)).toBe(Reflect.getPrototypeOf(window));
expect(Reflect.getPrototypeOf(proxy)).toBe(Object.getPrototypeOf(window));
});
5 changes: 5 additions & 0 deletions src/sandbox/proxySandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ export default class ProxySandbox implements SandBox {

return true;
},

// makes sure `window instanceof Window` returns truthy in micro app
getPrototypeOf() {
return Reflect.getPrototypeOf(rawWindow);
},
});

this.proxy = proxy;
Expand Down

1 comment on commit e14b082

@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-9h3g4zssv-umijs.vercel.app

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

Please sign in to comment.