Skip to content

Commit

Permalink
fix: maximum call when run in sandbox (#133)
Browse files Browse the repository at this point in the history
* fix: maximum call when run in sandbox

* chore: version

* fix: ts type

* test: add test case

* fix: lock eslint-plugin-import
  • Loading branch information
ClarkXia authored Jun 9, 2020
1 parent fbba13f commit d69ea7d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: yarn install
- run: npm run lint
- run: npm run test
- run: npm run coverage
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
"ts-jest": "^24.0.2",
"typescript": "^3.4.4"
},
"resolutions": {
"eslint-plugin-import": "2.20.2"
},
"jest": {
"coverageDirectory": "./coverage/",
"collectCoverage": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/icestark-sandbox/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ice/sandbox",
"version": "1.0.4",
"version": "1.0.5",
"description": "sandbox for execute scripts",
"main": "lib/index.js",
"scripts": {
Expand Down
8 changes: 5 additions & 3 deletions packages/icestark-sandbox/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ export interface SandboxContructor {
}
// check window contructor function, like Object Array
function isConstructor(fn) {
const functionStr = fn.toString();
// generator function and has own prototype properties
const hasConstructor = fn.prototype && fn.prototype.constructor === fn && Object.getOwnPropertyNames(fn.prototype).length > 1;
// unnecessary to call toString if it has contructor function
const functionStr = !hasConstructor && fn.toString();
const upperCaseRegex = /^function\s+[A-Z]/;

return (
// generator function and has own prototype properties
(fn.prototype && fn.prototype.constructor === fn && Object.getOwnPropertyNames(fn.prototype).length > 1) ||
hasConstructor ||
// upper case
upperCaseRegex.test(functionStr) ||
// ES6 class, window function do not have this case
Expand Down
10 changes: 10 additions & 0 deletions packages/icestark-sandbox/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ describe('sandbox: default props', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((window as any).a).toBe(undefined);
});
});

describe('sandbox: access contructor', () => {
const sandbox = new Sandbox();

test('execute global functions', () => {
sandbox.execScriptInSandbox('window.error = new Error("errmsg");Error.toString();');
const globalWindow = sandbox.getSandbox();
expect((globalWindow as any).error.toString()).toBe('Error: errmsg');
});
});
2 changes: 1 addition & 1 deletion setupJest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GlobalWithFetchMock } from 'jest-fetch-mock';

const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock;
const customGlobal: GlobalWithFetchMock = global as any;
customGlobal.fetch = require('jest-fetch-mock');

customGlobal.fetchMock = customGlobal.fetch;

0 comments on commit d69ea7d

Please sign in to comment.