Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

feat(index): proxy all modules in a remote module map #4

Merged
merged 2 commits into from
Feb 28, 2020

Conversation

anescobar1991
Copy link
Member

@anescobar1991 anescobar1991 commented Feb 27, 2020

The problem this is solving is that there are times where the modules a remote module map tries to add are not able to be loaded on browsers due to CORS restrictions (many CDNs don't allow localhost as an allowed origin).

Thus without this fix running one app while pointing to a remote module map that does not add localhost to their allowed origins for CORS (i.e. npm run start -- --root-module-name=my-root --module-map-url=https://example.com/module-map.json) does not work because the browser refues to load modules due to CORS.

The solution I implemented is to upon proxying to a remote module map storing all the module URLs from said module map in memory, modifying the module map to have all module URLs point back to one-app-dev-cdn, and then on requests to those modules proxy to the original remote module URL.

Validated locally by running One App with a remote module map, with just serving local modules, with a remote module map AND serving local modules, with proxy env vars set, and without proxy env vars set.

@anescobar1991 anescobar1991 requested a review from a team as a code owner February 27, 2020 00:07
@@ -119,5 +162,36 @@ export default ({
// for locally served modules
oneAppDevCdn.use('/modules', express.static(`${localDevPublicPath}/modules`));

// eslint-disable-next-line consistent-return
oneAppDevCdn.get('*', (req, res) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is ugly but cannot figure out a different way of doing it, definitely open to feedback!

? got(remoteModuleMapUrl).then(
r => JSON.parse(r.body),
? got(remoteModuleMapUrl, {
agent: new ProxyAgent(),
Copy link
Member Author

@anescobar1991 anescobar1991 Feb 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works with HTTP_PROXY, HTTPS_PROXY, and NO_PROXY thanks to our old friend @crosscompile's undocumented addition to the proxy-agent lib.

src/index.js Show resolved Hide resolved
src/index.js Show resolved Hide resolved
},
},
},
};
return supertest(fcdn)
.get('/module-map.json')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you test that if this is hit multiple times that the module map list does not keep growing

JAdshead
JAdshead previously approved these changes Feb 27, 2020
JamesSingleton
JamesSingleton previously approved these changes Feb 27, 2020
@JAdshead JAdshead merged commit c5fb1e7 into master Feb 28, 2020
@JAdshead JAdshead deleted the feature/proxy-modules branch February 28, 2020 00:02
nellyk pushed a commit that referenced this pull request Feb 28, 2020
# [3.1.0](v3.0.0...v3.1.0) (2020-02-28)

### Features

* **index:** proxy all modules in a remote module map ([#4](#4)) ([c5fb1e7](c5fb1e7))
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants