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

fix(runtime): support restricted unsafe-eval environments #2018

Merged
merged 2 commits into from
Jan 24, 2024
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
29 changes: 18 additions & 11 deletions packages/runtime/__tests__/preload-remote.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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);
Expand All @@ -307,25 +309,29 @@ 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();
});

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',
Expand All @@ -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([
Expand Down
10 changes: 8 additions & 2 deletions packages/runtime/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ export interface Federation {
__PRELOADED_MAP__: Map<string, boolean>;
}

// 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 {
Expand Down
Loading