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

Commit

Permalink
fix(devCdn): requests were not using node-fetch API (#949)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew-Mallimo authored Mar 9, 2023
1 parent 08d94b5 commit 8a3ff0a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 43 deletions.
15 changes: 12 additions & 3 deletions __mocks__/node-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@ fetch.mockReturnJsonOnce = (obj) => {
return fetch.mockImplementationOnce(() => Promise.reject(obj));
}

return fetch.mockImplementationOnce(() => Promise.resolve({ body: JSON.stringify(obj) }));
return fetch.mockImplementationOnce(() => Promise.resolve({
json: () => Promise.resolve(obj),
text: () => Promise.resolve(JSON.stringify(obj)),
status: 200,
}));
};

fetch.mockReturnFileOnce = (body) => {
fetch.mockReturnFileOnce = (body, status = 200) => {
if (body instanceof Error) {
return fetch.mockImplementationOnce(() => Promise.reject(body));
}

return fetch.mockImplementationOnce(() => Promise.resolve({ body, statusCode: 200 }));
return fetch.mockImplementationOnce(
() => Promise.resolve({
text: () => Promise.resolve(body),
status,
})
);
};
module.exports = fetch;
43 changes: 22 additions & 21 deletions __tests__/server/utils/devCdnFactory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,7 @@ describe('one-app-dev-cdn', () => {
},
},
};
const defaultRemoteMap = {
key: '234234',
modules: {
'module-b': {
node: {
url: 'https://example.com/cdn/module-b/1.0.0/module-b.node.js',
integrity: '123',
},
browser: {
url: 'https://example.com/cdn/module-b/1.0.0/module-b.browser.js',
integrity: '234',
},
legacyBrowser: {
url: 'https://example.com/cdn/module-b/1.0.0/module-b.legacy.browser.js',
integrity: '345',
},
},
},
};
let defaultRemoteMap;

const defaultPublicDirContentsSetting = {
moduleMapContent: JSON.stringify(defaultLocalMap),
Expand All @@ -92,6 +74,7 @@ describe('one-app-dev-cdn', () => {
if (!allowCacheWrite) {
mkdirp(pathToCache, { mode: 444 });
}

const modulesDir = path.join(mockLocalDevPublicPath, 'modules');

mkdirp.sync(modulesDir);
Expand Down Expand Up @@ -138,7 +121,25 @@ describe('one-app-dev-cdn', () => {
jest
.resetAllMocks()
.resetModules();

defaultRemoteMap = {
key: '234234',
modules: {
'module-b': {
node: {
url: 'https://example.com/cdn/module-b/1.0.0/module-b.node.js',
integrity: '123',
},
browser: {
url: 'https://example.com/cdn/module-b/1.0.0/module-b.browser.js',
integrity: '234',
},
legacyBrowser: {
url: 'https://example.com/cdn/module-b/1.0.0/module-b.legacy.browser.js',
integrity: '345',
},
},
},
};
fetch.mockImplementation((url) => Promise.reject(new Error(`no mock for ${url} set up`)));
});

Expand Down Expand Up @@ -629,7 +630,7 @@ describe('one-app-dev-cdn', () => {
await fcdn.inject()
.get('/module-map.json');

fetch.mockReturnFileOnce(gotError);
fetch.mockReturnFileOnce('body', 501);
const moduleResponse = await fcdn.inject()
.get('/cdn/module-b/1.0.0/some-langpack.json');
expect(moduleResponse.statusCode).toBe(501);
Expand Down
32 changes: 13 additions & 19 deletions src/server/utils/devCdnFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const consumeRemoteRequest = async (remoteModuleMapUrl, hostAddress, remoteModul
// clear out remoteModuleBaseUrls as the new module map now has different urls in it
// not clearing would result in an ever growing array
remoteModuleBaseUrls.splice(0, remoteModuleBaseUrls.length);
const remoteModuleMap = JSON.parse(response.body);
const remoteModuleMap = await response.json();
const { modules } = remoteModuleMap;
const oneAppDevStaticsAddress = `${hostAddress}/static`;
Object.keys(modules).map((moduleName) => {
Expand Down Expand Up @@ -135,13 +135,14 @@ export const oneAppDevCdnFactory = ({
// merge local with remote, with local taking preference
oneAppDevCdn.get(`${routePrefix}/module-map.json`, async (req, reply) => {
const hostAddress = useHost ? `http://${req.headers.host}` : `http://localhost:${process.env.HTTP_ONE_APP_DEV_CDN_PORT}`;

const localMap = useLocalModules ? JSON.parse(getLocalModuleMap({
pathToModuleMap: path.join(localDevPublicPath, 'module-map.json'),
oneAppDevCdnAddress: hostAddress,
})) : {};

const remoteMap = remoteModuleMapUrl != null ? await consumeRemoteRequest(remoteModuleMapUrl, hostAddress, remoteModuleBaseUrls) : {}; // eslint-disable-line max-len
const remoteMap = remoteModuleMapUrl != null
? await consumeRemoteRequest(remoteModuleMapUrl, hostAddress, remoteModuleBaseUrls)
: {};
// remoteMap always fulfilled
const map = {
...remoteMap,
Expand All @@ -166,22 +167,15 @@ export const oneAppDevCdnFactory = ({
remoteModuleBaseUrls
);
const remoteModuleBaseUrlOrigin = new URL(knownRemoteModuleBaseUrl).origin;
try {
const remoteModuleResponse = await fetch(`${remoteModuleBaseUrlOrigin}/${incomingRequestPath}`, {

headers: { connection: 'keep-alive' },
agent: new ProxyAgent(),
});
reply
.code(remoteModuleResponse.statusCode)
.type(path.extname(req.url))
.send(remoteModuleResponse.body);
} catch (err) {
const status = err.statusCode === 'ERR_NON_2XX_3XX_RESPONSE' ? err.response.statusCode : 500;
return reply
.code(status)
.send(err);
}
const remoteModuleResponse = await fetch(`${remoteModuleBaseUrlOrigin}/${incomingRequestPath}`, {
headers: { connection: 'keep-alive' },
agent: new ProxyAgent(),
});
const { status, type } = remoteModuleResponse;
reply
.code(status)
.type(type)
.send(await remoteModuleResponse.text());
} else {
reply
.code(404)
Expand Down

0 comments on commit 8a3ff0a

Please sign in to comment.