@micro-zoe/micro-app 1.0.0-alpha.0 → 1.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -127,7 +127,7 @@ declare module '@micro-zoe/micro-app/libs/utils' {
127
127
  /**
128
128
  * create URL as MicroLocation
129
129
  */
130
- export const createURL: (p: string | URL, b?: string | undefined) => MicroLocation;
130
+ export const createURL: (path: string | URL, base?: string | undefined) => MicroLocation;
131
131
  /**
132
132
  * Add address protocol
133
133
  * @param url address
package/lib/index.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- const version = '1.0.0-alpha.0';
1
+ const version = '1.0.0-alpha.1';
2
2
  // do not use isUndefined
3
3
  const isBrowser = typeof window !== 'undefined';
4
4
  // do not use isUndefined
@@ -107,7 +107,7 @@ const createURL = (function () {
107
107
  * @param url address
108
108
  */
109
109
  function addProtocol(url) {
110
- return url.startsWith('//') ? `${location.protocol}${url}` : url;
110
+ return url.startsWith('//') ? `${globalThis.location.protocol}${url}` : url;
111
111
  }
112
112
  /**
113
113
  * format URL address
@@ -199,24 +199,15 @@ function promiseStream(promiseList, successCb, errorCb, finallyCb) {
199
199
  promiseList.forEach((p, i) => {
200
200
  if (isPromise(p)) {
201
201
  p.then((res) => {
202
- successCb({
203
- data: res,
204
- index: i,
205
- });
202
+ successCb({ data: res, index: i });
206
203
  isFinished();
207
204
  }).catch((err) => {
208
- errorCb({
209
- error: err,
210
- index: i,
211
- });
205
+ errorCb({ error: err, index: i });
212
206
  isFinished();
213
207
  });
214
208
  }
215
209
  else {
216
- successCb({
217
- data: p,
218
- index: i,
219
- });
210
+ successCb({ data: p, index: i });
220
211
  isFinished();
221
212
  }
222
213
  });
@@ -457,7 +448,15 @@ var keepAliveStates;
457
448
  keepAliveStates["KEEP_ALIVE_SHOW"] = "keep_alive_show";
458
449
  keepAliveStates["KEEP_ALIVE_HIDDEN"] = "keep_alive_hidden";
459
450
  })(keepAliveStates || (keepAliveStates = {}));
460
- const globalKeyToBeCached = 'window,self,globalThis,Array,Object,String,Boolean,Math,Number,Symbol,Date,Promise,Function,Proxy,WeakMap,WeakSet,Set,Map,Reflect,Element,Node,Document,RegExp,Error,TypeError,JSON,isNaN,parseFloat,parseInt,performance,console,decodeURI,encodeURI,decodeURIComponent,encodeURIComponent,navigator,undefined,location,history,EventSource,fetch,XMLHttpRequest';
451
+ /**
452
+ * global key must be static key, they can not rewrite
453
+ * e.g.
454
+ * window.Promise = newValue
455
+ * new Promise ==> still get old value, not newValue, because they are cached by top function
456
+ * NOTE:
457
+ * 1. Do not add fetch, XMLHttpRequest, EventSource
458
+ */
459
+ const globalKeyToBeCached = 'window,self,globalThis,Array,Object,String,Boolean,Math,Number,Symbol,Date,Function,Proxy,WeakMap,WeakSet,Set,Map,Reflect,Element,Node,Document,RegExp,Error,TypeError,JSON,isNaN,parseFloat,parseInt,performance,console,decodeURI,encodeURI,decodeURIComponent,encodeURIComponent,navigator,undefined,location,history';
461
460
 
462
461
  /**
463
462
  * fetch source of html, js, css
@@ -1015,6 +1014,64 @@ function formatDynamicLink(url, info, app, originLink, replaceStyle) {
1015
1014
  });
1016
1015
  }
1017
1016
 
1017
+ class Adapter {
1018
+ constructor() {
1019
+ // keys that can only assigned to rawWindow
1020
+ this.escapeSetterKeyList = [
1021
+ 'location',
1022
+ ];
1023
+ // keys that can escape to rawWindow
1024
+ this.staticEscapeProperties = [
1025
+ 'System',
1026
+ '__cjsWrapper',
1027
+ ];
1028
+ // keys that scoped in child app
1029
+ this.staticScopeProperties = [
1030
+ 'webpackJsonp',
1031
+ 'webpackHotUpdate',
1032
+ 'Vue',
1033
+ ];
1034
+ this.injectReactHRMProperty();
1035
+ }
1036
+ // TODO: __DEV__ process.env.NODE_ENV !== 'production'
1037
+ // adapter for react
1038
+ injectReactHRMProperty() {
1039
+ if (process.env.NODE_ENV !== 'production') {
1040
+ // react child in non-react env
1041
+ this.staticEscapeProperties.push('__REACT_ERROR_OVERLAY_GLOBAL_HOOK__');
1042
+ // in react parent
1043
+ if (globalEnv.rawWindow.__REACT_ERROR_OVERLAY_GLOBAL_HOOK__) {
1044
+ this.staticScopeProperties = this.staticScopeProperties.concat([
1045
+ '__REACT_ERROR_OVERLAY_GLOBAL_HOOK__',
1046
+ '__reactRefreshInjected',
1047
+ ]);
1048
+ }
1049
+ }
1050
+ }
1051
+ }
1052
+ // Fix conflict of babel-polyfill@6.x
1053
+ function fixBabelPolyfill6() {
1054
+ if (globalEnv.rawWindow._babelPolyfill)
1055
+ globalEnv.rawWindow._babelPolyfill = false;
1056
+ }
1057
+ /**
1058
+ * Fix error of hot reload when parent&child created by create-react-app in development environment
1059
+ * Issue: https://github.com/micro-zoe/micro-app/issues/382
1060
+ */
1061
+ function fixReactHMRConflict(app) {
1062
+ var _a;
1063
+ if (process.env.NODE_ENV !== 'production') {
1064
+ const rawReactErrorHook = globalEnv.rawWindow.__REACT_ERROR_OVERLAY_GLOBAL_HOOK__;
1065
+ const childReactErrorHook = (_a = app.sandBox) === null || _a === void 0 ? void 0 : _a.proxyWindow.__REACT_ERROR_OVERLAY_GLOBAL_HOOK__;
1066
+ if (rawReactErrorHook && childReactErrorHook) {
1067
+ globalEnv.rawWindow.__REACT_ERROR_OVERLAY_GLOBAL_HOOK__ = childReactErrorHook;
1068
+ defer(() => {
1069
+ globalEnv.rawWindow.__REACT_ERROR_OVERLAY_GLOBAL_HOOK__ = rawReactErrorHook;
1070
+ });
1071
+ }
1072
+ }
1073
+ }
1074
+
1018
1075
  // Record element and map element
1019
1076
  const dynamicElementInMicroAppMap = new WeakMap();
1020
1077
  /**
@@ -1108,6 +1165,12 @@ function invokePrototypeMethod(app, rawMethod, parent, targetChild, passiveChild
1108
1165
  }
1109
1166
  return targetChild;
1110
1167
  }
1168
+ // TODO: __DEV__
1169
+ if (process.env.NODE_ENV !== 'production' &&
1170
+ targetChild instanceof HTMLIFrameElement &&
1171
+ rawMethod === globalEnv.rawAppendChild) {
1172
+ fixReactHMRConflict(app);
1173
+ }
1111
1174
  return invokeRawMethod(rawMethod, hijackElement, targetChild, passiveChild);
1112
1175
  }
1113
1176
  return invokeRawMethod(rawMethod, parent, targetChild, passiveChild);
@@ -2381,8 +2444,7 @@ function releaseEffectDocumentEvent() {
2381
2444
  * Rewrite side-effect events
2382
2445
  * @param microAppWindow micro window
2383
2446
  */
2384
- function effect(microAppWindow) {
2385
- const appName = microAppWindow.__MICRO_APP_NAME__;
2447
+ function effect(appName, microAppWindow) {
2386
2448
  const eventListenerMap = new Map();
2387
2449
  const intervalIdMap = new Map();
2388
2450
  const timeoutIdMap = new Map();
@@ -3309,24 +3371,108 @@ function removeStateAndPathFromBrowser(appName) {
3309
3371
  updateBrowserURL(removeMicroPathFromURL(appName), removeMicroState(appName, globalEnv.rawWindow.history.state));
3310
3372
  }
3311
3373
 
3312
- // Variables that can escape to rawWindow
3313
- const staticEscapeProperties = [
3314
- '__REACT_ERROR_OVERLAY_GLOBAL_HOOK__',
3315
- 'System',
3316
- '__cjsWrapper',
3317
- ];
3318
- // Variables that can only assigned to rawWindow
3319
- const escapeSetterKeyList = [
3320
- 'location',
3321
- ];
3374
+ /**
3375
+ * https://developer.mozilla.org/en-US/docs/Web/API/fetch
3376
+ * Promise<Response> fetch(input[, init])
3377
+ * input: string/Request
3378
+ * init?: object
3379
+ * @param url app url
3380
+ * @param target proxy target
3381
+ */
3382
+ function createMicroFetch(url, target) {
3383
+ if (!isUndefined(target) && !isFunction(target))
3384
+ return target;
3385
+ const rawFetch = target || globalEnv.rawWindow.fetch;
3386
+ return function microFetch(input, init, ...rests) {
3387
+ if (isString(input) || isURL(input)) {
3388
+ input = createURL(input, url).toString();
3389
+ }
3390
+ return rawFetch.call(globalEnv.rawWindow, input, init, ...rests);
3391
+ };
3392
+ }
3393
+ /**
3394
+ * https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
3395
+ * https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest
3396
+ * @param url app url
3397
+ * @param target proxy target
3398
+ */
3399
+ function createMicroXMLHttpRequest(url, target) {
3400
+ if (!isUndefined(target) && !isFunction(target))
3401
+ return target;
3402
+ const rawXMLHttpRequest = target || globalEnv.rawWindow.XMLHttpRequest;
3403
+ return class MicroXMLHttpRequest extends rawXMLHttpRequest {
3404
+ open(method, reqUrl, ...rests) {
3405
+ if ((isString(reqUrl) && !/^f(ile|tp):\/\//.test(reqUrl)) || isURL(reqUrl)) {
3406
+ reqUrl = createURL(reqUrl, url).toString();
3407
+ }
3408
+ super.open(method, reqUrl, ...rests);
3409
+ }
3410
+ };
3411
+ }
3412
+ function useMicroEventSource() {
3413
+ let eventSourceMap;
3414
+ /**
3415
+ * https://developer.mozilla.org/en-US/docs/Web/API/EventSource
3416
+ * pc = new EventSource(url[, configuration])
3417
+ * url: string/Request
3418
+ * configuration?: object
3419
+ * @param url app url
3420
+ * @param target proxy target
3421
+ */
3422
+ function createMicroEventSource(appName, url, target) {
3423
+ if (!isUndefined(target) && !isFunction(target))
3424
+ return target;
3425
+ const rawEventSource = target || globalEnv.rawWindow.EventSource;
3426
+ return class MicroEventSource extends rawEventSource {
3427
+ constructor(eventSourceUrl, eventSourceInitDict, ...rests) {
3428
+ if (isString(eventSourceUrl) || isURL(eventSourceUrl)) {
3429
+ eventSourceUrl = createURL(eventSourceUrl, url).toString();
3430
+ }
3431
+ super(eventSourceUrl, eventSourceInitDict, ...rests);
3432
+ if (eventSourceMap) {
3433
+ const eventSourceList = eventSourceMap.get(appName);
3434
+ if (eventSourceList) {
3435
+ eventSourceList.add(this);
3436
+ }
3437
+ else {
3438
+ eventSourceMap.set(appName, new Set([this]));
3439
+ }
3440
+ }
3441
+ else {
3442
+ eventSourceMap = new Map([[appName, new Set([this])]]);
3443
+ }
3444
+ }
3445
+ close() {
3446
+ var _a;
3447
+ super.close();
3448
+ (_a = eventSourceMap.get(appName)) === null || _a === void 0 ? void 0 : _a.delete(this);
3449
+ }
3450
+ };
3451
+ }
3452
+ function clearMicroEventSource(appName) {
3453
+ const eventSourceList = eventSourceMap === null || eventSourceMap === void 0 ? void 0 : eventSourceMap.get(appName);
3454
+ if (eventSourceList === null || eventSourceList === void 0 ? void 0 : eventSourceList.size) {
3455
+ eventSourceList.forEach(item => {
3456
+ item.close();
3457
+ });
3458
+ eventSourceList.clear();
3459
+ }
3460
+ }
3461
+ return {
3462
+ createMicroEventSource,
3463
+ clearMicroEventSource,
3464
+ };
3465
+ }
3466
+
3467
+ const { createMicroEventSource, clearMicroEventSource } = useMicroEventSource();
3322
3468
  const globalPropertyList = ['window', 'self', 'globalThis'];
3323
3469
  class SandBox {
3324
3470
  constructor(appName, url, useMemoryRouter = true) {
3325
3471
  /**
3326
3472
  * Scoped global Properties(Properties that can only get and set in microAppWindow, will not escape to rawWindow)
3327
- * https://github.com/micro-zoe/micro-app/issues/234
3473
+ * Fix https://github.com/micro-zoe/micro-app/issues/234
3328
3474
  */
3329
- this.scopeProperties = ['webpackJsonp', 'Vue'];
3475
+ this.scopeProperties = [];
3330
3476
  // Properties that can be escape to rawWindow
3331
3477
  this.escapeProperties = [];
3332
3478
  // Properties newly added to microAppWindow
@@ -3336,14 +3482,15 @@ class SandBox {
3336
3482
  // sandbox state
3337
3483
  this.active = false;
3338
3484
  this.microAppWindow = {}; // Proxy target
3485
+ this.adapter = new Adapter();
3339
3486
  // get scopeProperties and escapeProperties from plugins
3340
3487
  this.getSpecialProperties(appName);
3341
3488
  // create proxyWindow with Proxy(microAppWindow)
3342
3489
  this.proxyWindow = this.createProxyWindow(appName);
3343
- // inject global properties
3344
- this.initMicroAppWindow(this.microAppWindow, appName, url, useMemoryRouter);
3345
3490
  // Rewrite global event listener & timeout
3346
- assign(this, effect(this.microAppWindow));
3491
+ assign(this, effect(appName, this.microAppWindow));
3492
+ // inject global properties
3493
+ this.initStaticGlobalKeys(this.microAppWindow, appName, url, useMemoryRouter);
3347
3494
  }
3348
3495
  start(baseRoute, useMemoryRouter = true, defaultPage = '') {
3349
3496
  if (!this.active) {
@@ -3356,20 +3503,34 @@ class SandBox {
3356
3503
  else {
3357
3504
  this.microAppWindow.__MICRO_APP_BASE_ROUTE__ = this.microAppWindow.__MICRO_APP_BASE_URL__ = baseRoute;
3358
3505
  }
3506
+ // prevent the key deleted during sandBox.stop after rewrite
3507
+ this.initGlobalKeysWhenStart(this.microAppWindow, this.proxyWindow.__MICRO_APP_NAME__, this.proxyWindow.__MICRO_APP_URL__);
3359
3508
  if (++SandBox.activeCount === 1) {
3360
3509
  effectDocumentEvent();
3361
3510
  patchElementPrototypeMethods();
3362
3511
  listenUmountOfNestedApp();
3363
3512
  }
3364
- // BUG FIX: bable-polyfill@6.x
3365
- globalEnv.rawWindow._babelPolyfill && (globalEnv.rawWindow._babelPolyfill = false);
3513
+ fixBabelPolyfill6();
3366
3514
  }
3367
3515
  }
3368
- stop(keepRouteState = false) {
3516
+ stop(keepRouteState, clearEventSource) {
3369
3517
  if (this.active) {
3370
3518
  this.releaseEffect();
3371
3519
  this.microAppWindow.microApp.clearDataListener();
3372
3520
  this.microAppWindow.microApp.clearGlobalDataListener();
3521
+ if (this.removeHistoryListener) {
3522
+ this.clearRouteState(keepRouteState);
3523
+ // release listener of popstate
3524
+ this.removeHistoryListener();
3525
+ }
3526
+ if (clearEventSource) {
3527
+ clearMicroEventSource(this.proxyWindow.__MICRO_APP_NAME__);
3528
+ }
3529
+ /**
3530
+ * NOTE:
3531
+ * 1. injectedKeys and escapeKeys must be placed at the back
3532
+ * 2. if key in initial microAppWindow, and then rewrite, this key will be delete from microAppWindow when stop, and lost when restart
3533
+ */
3373
3534
  this.injectedKeys.forEach((key) => {
3374
3535
  Reflect.deleteProperty(this.microAppWindow, key);
3375
3536
  });
@@ -3378,11 +3539,6 @@ class SandBox {
3378
3539
  Reflect.deleteProperty(globalEnv.rawWindow, key);
3379
3540
  });
3380
3541
  this.escapeKeys.clear();
3381
- if (this.removeHistoryListener) {
3382
- this.clearRouteState(keepRouteState);
3383
- // release listener of popstate
3384
- this.removeHistoryListener();
3385
- }
3386
3542
  if (--SandBox.activeCount === 0) {
3387
3543
  releaseEffectDocumentEvent();
3388
3544
  releasePatches();
@@ -3409,15 +3565,16 @@ class SandBox {
3409
3565
  rebuildDataCenterSnapshot(this.microAppWindow.microApp);
3410
3566
  }
3411
3567
  /**
3412
- * get scopeProperties and escapeProperties from plugins
3568
+ * get scopeProperties and escapeProperties from plugins & adapter
3413
3569
  * @param appName app name
3414
3570
  */
3415
3571
  getSpecialProperties(appName) {
3416
3572
  var _a;
3417
- if (!isPlainObject(microApp.plugins))
3418
- return;
3419
- this.commonActionForSpecialProperties(microApp.plugins.global);
3420
- this.commonActionForSpecialProperties((_a = microApp.plugins.modules) === null || _a === void 0 ? void 0 : _a[appName]);
3573
+ this.scopeProperties = this.scopeProperties.concat(this.adapter.staticScopeProperties);
3574
+ if (isPlainObject(microApp.plugins)) {
3575
+ this.commonActionForSpecialProperties(microApp.plugins.global);
3576
+ this.commonActionForSpecialProperties((_a = microApp.plugins.modules) === null || _a === void 0 ? void 0 : _a[appName]);
3577
+ }
3421
3578
  }
3422
3579
  // common action for global plugins and module plugins
3423
3580
  commonActionForSpecialProperties(plugins) {
@@ -3451,7 +3608,7 @@ class SandBox {
3451
3608
  },
3452
3609
  set: (target, key, value) => {
3453
3610
  if (this.active) {
3454
- if (escapeSetterKeyList.includes(key)) {
3611
+ if (this.adapter.escapeSetterKeyList.includes(key)) {
3455
3612
  Reflect.set(rawWindow, key, value);
3456
3613
  }
3457
3614
  else if (
@@ -3475,7 +3632,8 @@ class SandBox {
3475
3632
  this.injectedKeys.add(key);
3476
3633
  }
3477
3634
  if ((this.escapeProperties.includes(key) ||
3478
- (staticEscapeProperties.includes(key) && !Reflect.has(rawWindow, key))) &&
3635
+ (this.adapter.staticEscapeProperties.includes(key) &&
3636
+ !Reflect.has(rawWindow, key))) &&
3479
3637
  !this.scopeProperties.includes(key)) {
3480
3638
  Reflect.set(rawWindow, key, value);
3481
3639
  this.escapeKeys.add(key);
@@ -3531,26 +3689,35 @@ class SandBox {
3531
3689
  * @param microAppWindow micro window
3532
3690
  * @param appName app name
3533
3691
  * @param url app url
3692
+ * @param useMemoryRouter whether use memory router
3534
3693
  */
3535
- initMicroAppWindow(microAppWindow, appName, url, useMemoryRouter) {
3694
+ initStaticGlobalKeys(microAppWindow, appName, url, useMemoryRouter) {
3536
3695
  microAppWindow.__MICRO_APP_ENVIRONMENT__ = true;
3537
3696
  microAppWindow.__MICRO_APP_NAME__ = appName;
3538
3697
  microAppWindow.__MICRO_APP_URL__ = url;
3539
3698
  microAppWindow.__MICRO_APP_PUBLIC_PATH__ = getEffectivePath(url);
3540
3699
  microAppWindow.__MICRO_APP_WINDOW__ = microAppWindow;
3700
+ microAppWindow.rawWindow = globalEnv.rawWindow;
3701
+ microAppWindow.rawDocument = globalEnv.rawDocument;
3541
3702
  microAppWindow.microApp = assign(new EventCenterForMicroApp(appName), {
3542
3703
  removeDomScope,
3543
3704
  pureCreateElement,
3544
3705
  router,
3545
3706
  });
3546
- microAppWindow.rawWindow = globalEnv.rawWindow;
3547
- microAppWindow.rawDocument = globalEnv.rawDocument;
3548
- microAppWindow.hasOwnProperty = (key) => rawHasOwnProperty.call(microAppWindow, key) || rawHasOwnProperty.call(globalEnv.rawWindow, key);
3549
3707
  this.setMappingPropertiesWithRawDescriptor(microAppWindow);
3550
- this.setHijackProperties(microAppWindow, appName);
3551
- // this.patchHijackRequest(microAppWindow, appName, url)
3552
3708
  if (useMemoryRouter)
3553
- this.setRouterApi(microAppWindow, appName, url);
3709
+ this.setMicroAppRouter(microAppWindow, appName, url);
3710
+ }
3711
+ /**
3712
+ * init global properties of microAppWindow when exec sandBox.start
3713
+ * @param microAppWindow micro window
3714
+ * @param appName app name
3715
+ * @param url app url
3716
+ */
3717
+ initGlobalKeysWhenStart(microAppWindow, appName, url) {
3718
+ microAppWindow.hasOwnProperty = (key) => rawHasOwnProperty.call(microAppWindow, key) || rawHasOwnProperty.call(globalEnv.rawWindow, key);
3719
+ this.setHijackProperties(microAppWindow, appName);
3720
+ this.patchHijackRequest(microAppWindow, appName, url);
3554
3721
  }
3555
3722
  // properties associated with the native window
3556
3723
  setMappingPropertiesWithRawDescriptor(microAppWindow) {
@@ -3584,7 +3751,7 @@ class SandBox {
3584
3751
  let modifiedEval, modifiedImage;
3585
3752
  rawDefineProperties(microAppWindow, {
3586
3753
  document: {
3587
- configurable: false,
3754
+ configurable: true,
3588
3755
  enumerable: true,
3589
3756
  get() {
3590
3757
  throttleDeferForSetAppName(appName);
@@ -3615,65 +3782,46 @@ class SandBox {
3615
3782
  },
3616
3783
  });
3617
3784
  }
3618
- // private patchHijackRequest (microAppWindow: microAppWindowType, appName: string, url: string) {
3619
- // let modifiedImage: unknown
3620
- // function EventSource (...rests: any[]) {
3621
- // console.log(appName + ' EventSource', rests)
3622
- // if (typeof rests[0] === 'string') {
3623
- // rests[0] = (new URL(rests[0], url)).toString()
3624
- // }
3625
- // return new globalEnv.rawWindow.EventSource(...rests)
3626
- // }
3627
- // function patchFetch (...rests: any[]) {
3628
- // console.log(appName + ' fetch', rests)
3629
- // if (typeof rests[0] === 'string') {
3630
- // rests[0] = (new URL(rests[0], url)).toString()
3631
- // }
3632
- // return globalEnv.rawWindow.fetch(...rests)
3633
- // }
3634
- // const rawXMLHttpRequest = globalEnv.rawWindow.XMLHttpRequest
3635
- // class XMLHttpRequest extends rawXMLHttpRequest {
3636
- // open (method: string, reqUrl: string) {
3637
- // console.log(appName + ' XMLHttpRequest', method, reqUrl)
3638
- // reqUrl = (new URL(reqUrl, url)).toString()
3639
- // super.open(method, reqUrl)
3640
- // }
3641
- // }
3642
- // rawDefineProperties(microAppWindow, {
3643
- // EventSource: {
3644
- // configurable: true,
3645
- // enumerable: true,
3646
- // get () {
3647
- // return EventSource
3648
- // },
3649
- // set: (value) => {
3650
- // modifiedImage = value
3651
- // },
3652
- // },
3653
- // fetch: {
3654
- // configurable: true,
3655
- // enumerable: true,
3656
- // get () {
3657
- // return patchFetch
3658
- // },
3659
- // set: (value) => {
3660
- // modifiedImage = value
3661
- // },
3662
- // },
3663
- // XMLHttpRequest: {
3664
- // configurable: true,
3665
- // enumerable: true,
3666
- // get () {
3667
- // return XMLHttpRequest
3668
- // },
3669
- // set: (value) => {
3670
- // modifiedImage = value
3671
- // },
3672
- // },
3673
- // })
3674
- // }
3785
+ // rewrite fetch, XMLHttpRequest, EventSource
3786
+ patchHijackRequest(microAppWindow, appName, url) {
3787
+ let microFetch = createMicroFetch(url);
3788
+ let microXMLHttpRequest = createMicroXMLHttpRequest(url);
3789
+ let microEventSource = createMicroEventSource(appName, url);
3790
+ rawDefineProperties(microAppWindow, {
3791
+ fetch: {
3792
+ configurable: true,
3793
+ enumerable: true,
3794
+ get() {
3795
+ return microFetch;
3796
+ },
3797
+ set(value) {
3798
+ microFetch = createMicroFetch(url, value);
3799
+ },
3800
+ },
3801
+ XMLHttpRequest: {
3802
+ configurable: true,
3803
+ enumerable: true,
3804
+ get() {
3805
+ return microXMLHttpRequest;
3806
+ },
3807
+ set(value) {
3808
+ microXMLHttpRequest = createMicroXMLHttpRequest(url, value);
3809
+ },
3810
+ },
3811
+ EventSource: {
3812
+ configurable: true,
3813
+ enumerable: true,
3814
+ get() {
3815
+ return microEventSource;
3816
+ },
3817
+ set(value) {
3818
+ microEventSource = createMicroEventSource(appName, url, value);
3819
+ },
3820
+ },
3821
+ });
3822
+ }
3675
3823
  // set location & history for memory router
3676
- setRouterApi(microAppWindow, appName, url) {
3824
+ setMicroAppRouter(microAppWindow, appName, url) {
3677
3825
  const { microLocation, microHistory } = createMicroRouter(appName, url);
3678
3826
  rawDefineProperties(microAppWindow, {
3679
3827
  location: {
@@ -3990,8 +4138,13 @@ class CreateApp {
3990
4138
  else if (this.umdMode && this.container.childElementCount) {
3991
4139
  cloneContainer(this.container, this.source.html, false);
3992
4140
  }
3993
- // this.container maybe contains micro-app element, stop sandbox should exec after cloneContainer
3994
- (_a = this.sandBox) === null || _a === void 0 ? void 0 : _a.stop(this.keepRouteState && !destroy);
4141
+ /**
4142
+ * this.container maybe contains micro-app element, stop sandbox should exec after cloneContainer
4143
+ * NOTE:
4144
+ * 1. if destroy is true, clear route state
4145
+ * 2. umd mode and keep-alive will not clear EventSource
4146
+ */
4147
+ (_a = this.sandBox) === null || _a === void 0 ? void 0 : _a.stop(this.keepRouteState && !destroy, !this.umdMode || destroy);
3995
4148
  if (!getActiveApps().length) {
3996
4149
  releasePatchSetAttribute();
3997
4150
  }