From c920ff670fe6409bda7e121553fe7e08f646d568 Mon Sep 17 00:00:00 2001 From: ScriptedAlchemy Date: Tue, 23 Jan 2024 22:03:06 -0800 Subject: [PATCH 1/2] fix(runtime): support restricted unsafe-eval environments --- packages/runtime/src/global.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/runtime/src/global.ts b/packages/runtime/src/global.ts index 969ecb0d167..c45206ac793 100644 --- a/packages/runtime/src/global.ts +++ b/packages/runtime/src/global.ts @@ -22,8 +22,14 @@ export interface Federation { __PRELOADED_MAP__: Map; } -// export const nativeGlobal: typeof global = new Function('return this')(); -export const nativeGlobal: typeof global = new Function('return this')(); +export const nativeGlobal: typeof global = (() => { + try { + return new Function('return this'); + } catch { + return globalThis; + } +})() as typeof global; + export const Global = nativeGlobal; declare global { From ca91ccb9001925a7fe522750906169fd0ec358ca Mon Sep 17 00:00:00 2001 From: "zhouxiao.shaw" Date: Wed, 24 Jan 2024 14:39:35 +0800 Subject: [PATCH 2/2] fix(runtime): fix preload unit test --- .../runtime/__tests__/preload-remote.spec.ts | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/runtime/__tests__/preload-remote.spec.ts b/packages/runtime/__tests__/preload-remote.spec.ts index 922af1b67db..35623a6e8da 100644 --- a/packages/runtime/__tests__/preload-remote.spec.ts +++ b/packages/runtime/__tests__/preload-remote.spec.ts @@ -237,7 +237,7 @@ describe('preload-remote inBrowser', () => { beforeEach(() => { document.head.innerHTML = ''; document.body.innerHTML = ''; - Global.__FEDERATION__.__PRELOADED_MAP__.clear(); + globalThis.__FEDERATION__.__PRELOADED_MAP__.clear(); }); const FMInstance = init({ @@ -284,12 +284,14 @@ describe('preload-remote inBrowser', () => { ]); expect(getPreloadElInfos()).toMatchSnapshot(); - expect(Global.__FEDERATION__.__PRELOADED_MAP__.size).toBe(2); + expect(globalThis.__FEDERATION__.__PRELOADED_MAP__.size).toBe(2); expect( - Global.__FEDERATION__.__PRELOADED_MAP__.get('@federation/sub1/button'), + globalThis.__FEDERATION__.__PRELOADED_MAP__.get( + '@federation/sub1/button', + ), ).toBe(true); expect( - Global.__FEDERATION__.__PRELOADED_MAP__.get( + globalThis.__FEDERATION__.__PRELOADED_MAP__.get( '@federation/sub1-button/button', ), ).toBe(true); @@ -307,17 +309,21 @@ describe('preload-remote inBrowser', () => { ]); expect(getPreloadElInfos()).toMatchSnapshot(); - expect(Global.__FEDERATION__.__PRELOADED_MAP__.size).toBe(3); + expect(globalThis.__FEDERATION__.__PRELOADED_MAP__.size).toBe(3); expect( - Global.__FEDERATION__.__PRELOADED_MAP__.get('@federation/sub2/button'), + globalThis.__FEDERATION__.__PRELOADED_MAP__.get( + '@federation/sub2/button', + ), ).toBe(true); expect( - Global.__FEDERATION__.__PRELOADED_MAP__.get( + globalThis.__FEDERATION__.__PRELOADED_MAP__.get( '@federation/sub2-button/button', ), ).toBe(true); expect( - Global.__FEDERATION__.__PRELOADED_MAP__.get('@federation/sub2-add/add'), + globalThis.__FEDERATION__.__PRELOADED_MAP__.get( + '@federation/sub2-add/add', + ), ).toBe(true); reset(); }); @@ -325,7 +331,7 @@ describe('preload-remote inBrowser', () => { it('3 preload with expose config ', async () => { const reset = addGlobalSnapshot(mockSnapshot); - expect(Global.__FEDERATION__.__PRELOADED_MAP__.size).toBe(0); + expect(globalThis.__FEDERATION__.__PRELOADED_MAP__.size).toBe(0); await FMInstance.preloadRemote([ { nameOrAlias: '@federation/sub3', @@ -334,9 +340,10 @@ describe('preload-remote inBrowser', () => { }, ]); expect(getPreloadElInfos()).toMatchSnapshot(); - expect(Global.__FEDERATION__.__PRELOADED_MAP__.size).toBe(1); + + expect(globalThis.__FEDERATION__.__PRELOADED_MAP__.size).toBe(1); expect( - Global.__FEDERATION__.__PRELOADED_MAP__.get('@federation/sub3/add'), + globalThis.__FEDERATION__.__PRELOADED_MAP__.get('@federation/sub3/add'), ).toBe(true); await FMInstance.preloadRemote([