1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| const hijackHistory = (): void => { window.history.pushState = (state: any, title: string, url?: string, ...rest) => { originalPush.apply(window.history, [state, title, url, ...rest]); const eventName = 'pushState'; handleStateChange(createPopStateEvent(state, eventName), url, eventName); };
window.history.replaceState = (state: any, title: string, url?: string, ...rest) => { originalReplace.apply(window.history, [state, title, url, ...rest]); const eventName = 'replaceState'; handleStateChange(createPopStateEvent(state, eventName), url, eventName); };
window.addEventListener('popstate', urlChange, false); window.addEventListener('hashchange', urlChange, false); };
const handleStateChange = (event: PopStateEvent, url: string, method: RouteType) => { setHistoryEvent(event); reroute(url, method); };
function reroute (url: string, type: RouteType | 'init' | 'popstate'| 'hashchange' ) { const { pathname, query, hash } = urlParse(url, true); const unmountApps = []; const activeApps = []; getMicroApps().forEach((microApp: AppConfig) => { const shouldBeActive = microApp.checkActive(url); if (shouldBeActive) { activeApps.push(microApp); } else { unmountApps.push(microApp); } }); Promise.all( unmountApps.map(async (unmountApp) => { if (unmountApp.status === MOUNTED || unmountApp.status === LOADING_ASSETS) { globalConfiguration.onAppLeave(unmountApp); } await unmountMicroApp(unmountApp.name); }).concat(activeApps.map(async (activeApp) => { if (activeApp.status !== MOUNTED) { globalConfiguration.onAppEnter(activeApp); } await createMicroApp(activeApp); })) ) };
async function loadAppModule(appConfig: AppConfig) { const appSandbox = createSandbox(appConfig.sandbox); const { url, container, entry, entryContent, name } = appConfig; const appAssets = url ? getUrlAssets(url) : await getEntryAssets({ ... }); await appendAssets(appAssets, appSandbox); lifecycle = { mount: getCache(AppLifeCycleEnum.AppEnter), unmount: getCache(AppLifeCycleEnum.AppLeave), }; return combineLifecyle(lifecycle, appConfig); }
export async function appendAssets(assets: Assets, sandbox?: Sandbox) { await loadAndAppendCssAssets(assets); await loadAndAppendJsAssets(assets, sandbox); }
export async function loadAndAppendJsAssets(assets: Assets, sandbox?: Sandbox) { const jsContents = await fetchScripts(jsList); jsContents.forEach(script => { sandbox.execScriptInSandbox(script); }); }
class Sandbox { private eventListeners = {}; private timeoutIds: number[] = []; private intervalIds: number[] = [];
constructor(props: SandboxProps = {}) { this.sandbox = null; }
execScriptInSandbox(script: string): void { this.createProxySandbox(); const execScript = `with (sandbox) {;${script}\n}`; const code = new Function('sandbox', execScript).bind(this.sandbox); code(this.sandbox); }
createProxySandbox() { const proxyWindow = Object.create(null) as Window; const originalWindow = window; const originalAddEventListener = window.addEventListener; const originalRemoveEventListener = window.removeEventListener; const originalSetInerval = window.setInterval; const originalSetTimeout = window.setTimeout; proxyWindow.addEventListener = (eventName, fn, ...rest) => { const listeners = this.eventListeners[eventName] || []; listeners.push(fn); return originalAddEventListener.apply(originalWindow, [eventName, fn, ...rest]); }; proxyWindow.removeEventListener = (eventName, fn, ...rest) => { const listeners = this.eventListeners[eventName] || []; if (listeners.includes(fn)) { listeners.splice(listeners.indexOf(fn), 1); } return originalRemoveEventListener.apply(originalWindow, [eventName, fn, ...rest]); }; proxyWindow.setTimeout = (...args) => { const timerId = originalSetTimeout(...args); this.timeoutIds.push(timerId); return timerId; }; proxyWindow.setInterval = (...args) => { const intervalId = originalSetInerval(...args); this.intervalIds.push(intervalId); return intervalId; };
const sandbox = new Proxy(proxyWindow, { set(target: Window, p: PropertyKey, value: any): boolean {
}, get(target: Window, p: PropertyKey): any {
}, has(target: Window, p: PropertyKey): boolean { return p in target || p in originalWindow; }, }); this.sandbox = sandbox; } clear() { Object.keys(this.eventListeners).forEach((eventName) => { (this.eventListeners[eventName] || []).forEach(listener => { window.removeEventListener(eventName, listener); }); }); this.timeoutIds.forEach(id => window.clearTimeout(id)); this.intervalIds.forEach(id => window.clearInterval(id)); } }
async function createMicroApp(app: string | AppConfig, appLifecyle?: AppLifecylceOptions) { const appConfig = getAppConfigForLoad(app, appLifecyle); lifeCycle = await loadAppModule(appConfig); mountMicroApp(appConfig.name); }
|
Comments