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

✨ support multiply sandbox instance #31

Merged
merged 4 commits into from
Mar 29, 2020
Merged
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
8 changes: 5 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface IImportResult {

assetPublicPath: string;

execScripts<T>(sandbox?: object): Promise<T>;
execScripts<T>(sandbox?: object, strictGlobal?: boolean): Promise<T>;

getExternalScripts(): Promise<string[]>;

Expand All @@ -20,9 +20,11 @@ export type ImportEntryOpts = {
getPublicPath?: (rawPublicPath: string) => string;
getTemplate?: (tpl: string) => string;
}
export type ExecScriptsOpts = {
fetch?: Function;

type ExecScriptsOpts = Pick<ImportEntryOpts, 'fetch'> & {
strictGlobal?: boolean;
}

export type Entry = string | { styles?: string[], scripts?: string[], html?: string };

export function execScripts<T>(entry: string | null, scripts: string[], proxy: Window, opts?: ExecScriptsOpts): Promise<T>;
Expand Down
39 changes: 22 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,20 @@ function getEmbedHTML(template, styles, opts = {}) {
});
}

function getExecutableScript(scriptText, proxy) {
window.proxy = proxy;
return `;(function(window, self){;${scriptText}\n}).bind(window.proxy)(window.proxy, window.proxy);`;
}

// for prefetch
export function getExternalStyleSheets(styles, fetch = defaultFetch) {
return Promise.all(styles.map(styleLink => {
if (styleLink.startsWith('<')) {
// if it is inline style
return getInlineCode(styleLink);
} else {
// external styles
return styleCache[styleLink] ||
if (styleLink.startsWith('<')) {
// if it is inline style
return getInlineCode(styleLink);
} else {
// external styles
return styleCache[styleLink] ||
(styleCache[styleLink] = fetch(styleLink).then(response => response.text()));
}

Expand Down Expand Up @@ -96,12 +101,11 @@ const supportsUserTiming =
typeof performance.clearMeasures === 'function';

export function execScripts(entry, scripts, proxy = window, opts = {}) {
const { fetch = defaultFetch } = opts;
const { fetch = defaultFetch, strictGlobal = false } = opts;

return getExternalScripts(scripts, fetch)
.then(scriptsText => {

window.proxy = proxy;
const geval = eval;

function exec(scriptSrc, inlineScript, resolve) {
Expand All @@ -114,32 +118,33 @@ export function execScripts(entry, scripts, proxy = window, opts = {}) {
}

if (scriptSrc === entry) {
noteGlobalProps();
noteGlobalProps(strictGlobal ? proxy : window);

try {
// bind window.proxy to change `this` reference in script
geval(`;(function(window){;${inlineScript}\n}).bind(window.proxy)(window.proxy);`);
geval(getExecutableScript(inlineScript, proxy));
} catch (e) {
console.error(`error occurs while executing the entry ${scriptSrc}`);
throw e;
}

const exports = proxy[getGlobalProp()] || {};
const exports = proxy[getGlobalProp(strictGlobal ? proxy : window)] || {};
resolve(exports);

} else {

if (typeof inlineScript === 'string') {
try {
// bind window.proxy to change `this` reference in script
geval(`;(function(window){;${inlineScript}\n}).bind(window.proxy)(window.proxy);`);
geval(getExecutableScript(inlineScript, proxy));
} catch (e) {
console.error(`error occurs while executing ${scriptSrc}`);
throw e;
}
} else {
// external script marked with async
inlineScript.async && inlineScript?.content
.then(downloadedScriptText => geval(`;(function(window){;${downloadedScriptText}\n}).bind(window.proxy)(window.proxy);`))
.then(downloadedScriptText => geval(getExecutableScript(downloadedScriptText, proxy)))
.catch(e => {
console.error(`error occurs while executing async script ${scriptSrc?.src}`);
throw e;
Expand Down Expand Up @@ -200,11 +205,11 @@ export default function importHTML(url, opts = {}) {
assetPublicPath,
getExternalScripts: () => getExternalScripts(scripts, fetch),
getExternalStyleSheets: () => getExternalStyleSheets(styles, fetch),
execScripts: proxy => {
execScripts: (proxy, strictGlobal) => {
if (!scripts.length) {
return Promise.resolve();
}
return execScripts(entry, scripts, proxy, { fetch });
return execScripts(entry, scripts, proxy, { fetch, strictGlobal });
},
}));
}));
Expand Down Expand Up @@ -233,11 +238,11 @@ export function importEntry(entry, opts = {}) {
assetPublicPath: getPublicPath('/'),
getExternalScripts: () => getExternalScripts(scripts, fetch),
getExternalStyleSheets: () => getExternalStyleSheets(styles, fetch),
execScripts: proxy => {
execScripts: (proxy, strictGlobal) => {
if (!scripts.length) {
return Promise.resolve();
}
return execScripts(scripts[scripts.length - 1], scripts, proxy, { fetch });
return execScripts(scripts[scripts.length - 1], scripts, proxy, { fetch, strictGlobal });
},
}));

Expand Down
20 changes: 14 additions & 6 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ const isIE = navigator.userAgent.indexOf('Trident') !== -1;

// safari unpredictably lists some new globals first or second in object order
let firstGlobalProp, secondGlobalProp, lastGlobalProp;
export function getGlobalProp() {

export function getGlobalProp(global) {
let cnt = 0;
let lastProp;
let hasIframe = false;

for (let p in global) {
// use Object.keys to make it trigger the trap if global is proxy
const props = Object.keys(global);
for (let i = 0; i < props.length; i++) {
const p = props[i];
// do not check frames cause it could be removed during import
if (
!global.hasOwnProperty(p) ||
Expand All @@ -37,15 +41,18 @@ export function getGlobalProp() {
cnt++;
lastProp = p;
}

if (lastProp !== lastGlobalProp)
return lastProp;
}

export function noteGlobalProps() {
// alternatively Object.keys(global).pop()
// but this may be faster (pending benchmarks)
export function noteGlobalProps(global) {
firstGlobalProp = secondGlobalProp = undefined;
for (let p in global) {

// use Object.keys to make it trigger the trap if global is proxy
const props = Object.keys(global);
for (let i = 0; i < props.length; i++) {
const p = props[i];
// do not check frames cause it could be removed during import
if (
!global.hasOwnProperty(p) ||
Expand All @@ -59,6 +66,7 @@ export function noteGlobalProps() {
secondGlobalProp = p;
lastGlobalProp = p;
}

return lastGlobalProp;
}

Expand Down