@micro-zoe/micro-app 1.0.0-rc.31 → 1.0.0-rc.32

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.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- const version = '1.0.0-rc.31';
1
+ const version = '1.0.0-rc.32';
2
2
  // do not use isUndefined
3
3
  const isBrowser = typeof window !== 'undefined';
4
4
  // do not use isUndefined
@@ -249,13 +249,15 @@ function formatAppURL(url, appName = null) {
249
249
  if (!isString(url) || !url)
250
250
  return '';
251
251
  try {
252
- const { origin, pathname, search } = createURL(addProtocol(url), (window.rawWindow || window).location.href);
252
+ const { protocol, origin: rawOrigin, host, pathname, search } = createURL(addProtocol(url), (window.rawWindow || window).location.href);
253
253
  /**
254
254
  * keep the original url unchanged, such as .html .node .php .net .etc, search, except hash
255
255
  * BUG FIX: Never using '/' to complete url, refer to https://github.com/jd-opensource/micro-app/issues/1147
256
+ * FEATURE: URL.origin is browser-dependent for file protocol url, refer to https://github.com/jd-opensource/micro-app/pull/1695
256
257
  */
258
+ const origin = protocol === 'file:' ? `${protocol}//${host}` : rawOrigin;
257
259
  const fullPath = `${origin}${pathname}${search}`;
258
- return /^https?:\/\//.test(fullPath) ? fullPath : '';
260
+ return /^(https?|file):\/\//.test(fullPath) ? fullPath : '';
259
261
  }
260
262
  catch (e) {
261
263
  logError(e, appName);
@@ -284,7 +286,11 @@ function formatAppName(name) {
284
286
  * @param url app.url
285
287
  */
286
288
  function getEffectivePath(url) {
287
- const { origin, pathname } = createURL(url);
289
+ const { protocol, origin: rawOrigin, host, pathname } = createURL(url);
290
+ /*
291
+ * FEATURE: URL.origin is browser-dependent for file protocol url, refer to https://github.com/jd-opensource/micro-app/pull/1695
292
+ */
293
+ const origin = protocol === 'file:' ? `${protocol}//${host}` : rawOrigin;
288
294
  if (/\.(\w+)$/.test(pathname)) {
289
295
  const pathArr = `${origin}${pathname}`.split('/');
290
296
  pathArr.pop();
@@ -977,7 +983,7 @@ class CSSParser {
977
983
  if (!this.scopecssDisableNextLine &&
978
984
  (!this.scopecssDisable || this.scopecssDisableSelectors.length)) {
979
985
  cssValue = cssValue.replace(/url\((["']?)(.*?)\1\)/gm, (all, _, $1) => {
980
- if (/^((data|blob):|#|%23)/.test($1) || /^(https?:)?\/\//.test($1)) {
986
+ if (/^((data|blob):|#|%23)/.test($1) || /^((https?|file):)?\/\//.test($1)) {
981
987
  return all;
982
988
  }
983
989
  // ./a/b.png ../a/b.png a/b.png
@@ -3418,6 +3424,95 @@ function createMicroDocument(appName, proxyDocument) {
3418
3424
  return MicroDocument;
3419
3425
  }
3420
3426
 
3427
+ const EXCLUDE_URL_PROTOCOLS = [
3428
+ 'blob:'
3429
+ ];
3430
+ // 重写 Worker 构造函数的类型
3431
+ const originalWorker = window.Worker;
3432
+ // create a proxy for the Worker constructor to intercept the creation of new Worker instances
3433
+ const workerInstanceMap = new Map();
3434
+ // release all workers of the app after sandboxes destoried.
3435
+ function releaseWorkersByApp(appName) {
3436
+ var _a;
3437
+ (_a = workerInstanceMap.get(appName)) === null || _a === void 0 ? void 0 : _a.forEach(w => w.terminate());
3438
+ workerInstanceMap.delete(appName);
3439
+ }
3440
+ function isSameOrigin(url) {
3441
+ try {
3442
+ // 检查 URL 是否与当前页面在同一个源
3443
+ const parsedUrl = url instanceof URL ? url : new URL(url);
3444
+ if (EXCLUDE_URL_PROTOCOLS.includes(parsedUrl.protocol)) {
3445
+ return true;
3446
+ }
3447
+ return (parsedUrl.protocol === window.location.protocol &&
3448
+ parsedUrl.hostname === window.location.hostname &&
3449
+ parsedUrl.port === window.location.port);
3450
+ }
3451
+ catch (error) {
3452
+ return false;
3453
+ }
3454
+ }
3455
+ function urlFromScript(script) {
3456
+ let blob;
3457
+ try {
3458
+ blob = new Blob([script], {
3459
+ type: 'application/javascript'
3460
+ });
3461
+ }
3462
+ catch (e) {
3463
+ const BlobBuilder =
3464
+ // @ts-ignore
3465
+ window.BlobBuilder ||
3466
+ // @ts-ignore
3467
+ window.WebKitBlobBuilder ||
3468
+ // @ts-ignore
3469
+ window.MozBlobBuilder ||
3470
+ // @ts-ignore
3471
+ window.MSBlobBuilder;
3472
+ const blobBuilder = new BlobBuilder();
3473
+ blobBuilder.append(script);
3474
+ blob = blobBuilder.getBlob('application/javascript');
3475
+ }
3476
+ const URL = window.URL || window.webkitURL;
3477
+ return URL.createObjectURL(blob);
3478
+ }
3479
+ // @ts-ignore
3480
+ const WorkerProxy = new Proxy(originalWorker, {
3481
+ construct(Target, args) {
3482
+ let [scriptURL, options] = args;
3483
+ options = options || {};
3484
+ const appName = getCurrentAppName();
3485
+ let url = scriptURL;
3486
+ if (appName) {
3487
+ const app = appInstanceMap.get(appName);
3488
+ url = CompletionPath(scriptURL, app.url);
3489
+ }
3490
+ let instance;
3491
+ if (url && !isSameOrigin(url)) {
3492
+ // 如果 scriptURL 是跨域的,使用 Blob URL 加载并执行 worker
3493
+ const workerScriptURL = JSON.stringify(String(url));
3494
+ // check if the type of worker is module or classic
3495
+ const script = options.type === 'module'
3496
+ ? `import ${workerScriptURL};`
3497
+ : `importScripts(${workerScriptURL});`;
3498
+ const workerPath = urlFromScript(script);
3499
+ instance = new Target(workerPath, options);
3500
+ }
3501
+ else {
3502
+ // 如果 scriptURL 是同源的,直接使用原生的 Worker 构造函数
3503
+ instance = new Target(scriptURL, options);
3504
+ }
3505
+ // register worker instance to app
3506
+ if (appName) {
3507
+ if (!workerInstanceMap.has(appName)) {
3508
+ workerInstanceMap.set(appName, new Set());
3509
+ }
3510
+ workerInstanceMap.get(appName).add(instance);
3511
+ }
3512
+ return instance;
3513
+ },
3514
+ });
3515
+
3421
3516
  /**
3422
3517
  * patch window of child app
3423
3518
  * @param appName app name
@@ -3464,9 +3559,18 @@ function patchWindowProperty(microAppWindow) {
3464
3559
  function createProxyWindow(appName, microAppWindow, sandbox) {
3465
3560
  const rawWindow = globalEnv.rawWindow;
3466
3561
  const descriptorTargetMap = new Map();
3562
+ rawDefineProperty(microAppWindow, 'Worker', {
3563
+ value: WorkerProxy,
3564
+ configurable: true,
3565
+ writable: true,
3566
+ });
3467
3567
  const proxyWindow = new Proxy(microAppWindow, {
3468
3568
  get: (target, key) => {
3469
3569
  throttleDeferForSetAppName(appName);
3570
+ // return WorkerProxy if child app accesses window.Worker
3571
+ if (key === 'Worker') {
3572
+ return WorkerProxy;
3573
+ }
3470
3574
  if (Reflect.has(target, key) ||
3471
3575
  (isString(key) && /^__MICRO_APP_/.test(key)) ||
3472
3576
  includes(sandbox.scopeProperties, key)) {
@@ -5162,6 +5266,7 @@ class WithSandBox extends BaseSandbox {
5162
5266
  */
5163
5267
  if (!umdMode || destroy) {
5164
5268
  clearMicroEventSource(this.microAppWindow.__MICRO_APP_NAME__);
5269
+ releaseWorkersByApp(this.microAppWindow.__MICRO_APP_NAME__);
5165
5270
  this.injectedKeys.forEach((key) => {
5166
5271
  Reflect.deleteProperty(this.microAppWindow, key);
5167
5272
  });
@@ -5654,75 +5759,6 @@ const proxy2RawDocumentMethods = [
5654
5759
  'prepend',
5655
5760
  ];
5656
5761
 
5657
- const EXCLUDE_URL_PROTOCOLS = [
5658
- 'blob:'
5659
- ];
5660
- // 重写 Worker 构造函数的类型
5661
- const originalWorker = window.Worker;
5662
- function isSameOrigin(url) {
5663
- try {
5664
- // 检查 URL 是否与当前页面在同一个源
5665
- const parsedUrl = url instanceof URL ? url : new URL(url);
5666
- if (EXCLUDE_URL_PROTOCOLS.includes(parsedUrl.protocol)) {
5667
- return true;
5668
- }
5669
- return (parsedUrl.protocol === window.location.protocol &&
5670
- parsedUrl.hostname === window.location.hostname &&
5671
- parsedUrl.port === window.location.port);
5672
- }
5673
- catch (error) {
5674
- return false;
5675
- }
5676
- }
5677
- function urlFromScript(script) {
5678
- let blob;
5679
- try {
5680
- blob = new Blob([script], {
5681
- type: 'application/javascript'
5682
- });
5683
- }
5684
- catch (e) {
5685
- const BlobBuilder =
5686
- // @ts-ignore
5687
- window.BlobBuilder ||
5688
- // @ts-ignore
5689
- window.WebKitBlobBuilder ||
5690
- // @ts-ignore
5691
- window.MozBlobBuilder ||
5692
- // @ts-ignore
5693
- window.MSBlobBuilder;
5694
- const blobBuilder = new BlobBuilder();
5695
- blobBuilder.append(script);
5696
- blob = blobBuilder.getBlob('application/javascript');
5697
- }
5698
- const URL = window.URL || window.webkitURL;
5699
- return URL.createObjectURL(blob);
5700
- }
5701
- // @ts-ignore
5702
- const WorkerProxy = new Proxy(originalWorker, {
5703
- construct(Target, args) {
5704
- let [scriptURL, options] = args;
5705
- options = options || {};
5706
- const appName = getCurrentAppName();
5707
- let url = scriptURL;
5708
- if (appName) {
5709
- const app = appInstanceMap.get(appName);
5710
- url = CompletionPath(scriptURL, app.url);
5711
- }
5712
- if (url && !isSameOrigin(url)) {
5713
- // 如果 scriptURL 是跨域的,使用 Blob URL 加载并执行 worker
5714
- const script = `import "${scriptURL}";`;
5715
- const workerPath = urlFromScript(script);
5716
- options.type = 'module';
5717
- return new Target(workerPath, options);
5718
- }
5719
- else {
5720
- // 如果 scriptURL 是同源的,直接使用原生的 Worker 构造函数
5721
- return new Target(scriptURL, options);
5722
- }
5723
- },
5724
- });
5725
-
5726
5762
  /**
5727
5763
  * patch window of child app
5728
5764
  * @param appName app name
@@ -6749,6 +6785,7 @@ class IframeSandbox {
6749
6785
  if (!umdMode || destroy) {
6750
6786
  this.deleteIframeElement();
6751
6787
  this.clearHijackUmdHooks();
6788
+ releaseWorkersByApp(this.microAppWindow.__MICRO_APP_NAME__);
6752
6789
  }
6753
6790
  if (--globalEnv.activeSandbox === 0) {
6754
6791
  releasePatchElementAndDocument();