Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] @ice/sandbox-1.1.5 #616

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/sandbox/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,47 @@ describe('callable functions in sandbox', () => {
expect(error).toBe(null);
});
});

describe('mock function cacheDeps', () => {
// mock window
const windowAsAny = (window as any);
test('load second dependency with first dependency in multiMode', () => {
windowAsAny['test'] = { a: 123, b: 456 };

let sandbox = new Sandbox({ multiMode: true });

sandbox.execScriptInSandbox(`
window['test'] = { a: 234, b: 456 };
`);
const firstDepPropertyAdded = sandbox.getAddedProperties();
sandbox.clear();
sandbox = new Sandbox({ multiMode: true });
sandbox.createProxySandbox(firstDepPropertyAdded);
sandbox.execScriptInSandbox(`
expect(window.test.a).toBe(234);
`);
sandbox.clear();
expect(windowAsAny.test.a).toBe(123);

});

test('load second dependency with first dependency in singleMode', () => {
windowAsAny['test'] = { a: 123, b: 456 };

let sandbox = new Sandbox({ multiMode: false });

sandbox.execScriptInSandbox(`
window['test'] = { a: 234, b: 456 };
`);
const firstDepPropertyAdded = sandbox.getAddedProperties();
sandbox.clear();
sandbox = new Sandbox({ multiMode: true });
sandbox.createProxySandbox(firstDepPropertyAdded);
sandbox.execScriptInSandbox(`
expect(window.test.a).toBe(234);
`);
sandbox.clear();
expect(windowAsAny.test.a).toBe(123);
})

})
2 changes: 1 addition & 1 deletion packages/sandbox/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ice/sandbox",
"version": "1.1.4",
"version": "1.1.5",
"description": "sandbox for execute scripts",
"main": "lib/index.js",
"scripts": {
Expand Down
13 changes: 9 additions & 4 deletions packages/sandbox/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ export default class Sandbox {
} else if (!originalValues.hasOwnProperty(p)) {
// if it is already been setted in original window, record it's original value
originalValues[p] = originalWindow[p];
// because target in sandbox will be changed, record the change in propertyAdded, for user can get what has been changed in last-loading
propertyAdded[p] = value;
}
// set new value to original window in case of jsonp, js bundle which will be execute outof sandbox
if (!multiMode) {
Expand Down Expand Up @@ -214,13 +216,16 @@ export default class Sandbox {
// clear timeout
this.timeoutIds.forEach((id) => window.clearTimeout(id));
this.intervalIds.forEach((id) => window.clearInterval(id));
// recover original values
Object.keys(this.originalValues).forEach((key) => {
window[key] = this.originalValues[key];
});
// some properties has been in original window, when they're loaded in sandbox
// the change has been recorded in propertyAdded
// delete the modified properties
Object.keys(this.propertyAdded).forEach((key) => {
delete window[key];
});
// then recover original values
Object.keys(this.originalValues).forEach((key) => {
window[key] = this.originalValues[key];
});
}
}
}