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

Commit

Permalink
fix(load-modules): unnecessary calls to updateModuleRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
JAdshead committed Mar 6, 2023
1 parent 9b0c4b3 commit 17a3b4d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ exports[`loadModules updates the client module map cache 1`] = `
"browser": {
"modules": {
"some-root": {
"baseUrl": "https://example.com/cdn/some-root/2.2.2/",
"baseUrl": "https://example.com/cdn/some-root/1.1.2/",
"browser": {
"integrity": "nggdfhr34",
"url": "https://example.com/cdn/some-root/2.2.2/some-root.browser.js",
"url": "https://example.com/cdn/some-root/1.1.2/some-root.browser.js",
},
},
},
},
"legacyBrowser": {
"modules": {
"some-root": {
"baseUrl": "https://example.com/cdn/some-root/2.2.2/",
"baseUrl": "https://example.com/cdn/some-root/1.1.2/",
"legacyBrowser": {
"integrity": "7567ee",
"url": "https://example.com/cdn/some-root/2.2.2/some-root.legacy.browser.js",
"url": "https://example.com/cdn/some-root/1.1.2/some-root.legacy.browser.js",
},
},
},
Expand Down
101 changes: 66 additions & 35 deletions __tests__/server/utils/loadModules.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,47 +44,36 @@ jest.mock('../../../src/server/plugins/csp', () => ({

const RootModule = () => ({});

describe('loadModules', () => {
const moduleMap = {
modules: {
'some-root': {
node: {
url: 'https://example.com/cdn/some-root/2.2.2/some-root.node.js',
integrity: '4y45hr',
},
browser: {
url: 'https://example.com/cdn/some-root/2.2.2/some-root.browser.js',
integrity: 'nggdfhr34',
},
legacyBrowser: {
url: 'https://example.com/cdn/some-root/2.2.2/some-root.legacy.browser.js',
integrity: '7567ee',
},
let modules;
let fetchedModuleMap;
function setFetchedRootModuleVersion(version) {
modules = {
'some-root': {
node: {
url: `https://example.com/cdn/some-root/${version}/some-root.node.js`,
integrity: '4y45hr',
},
browser: {
url: `https://example.com/cdn/some-root/${version}/some-root.browser.js`,
integrity: 'nggdfhr34',
},
legacyBrowser: {
url: `https://example.com/cdn/some-root/${version}/some-root.legacy.browser.js`,
integrity: '7567ee',
},
},
};

fetchedModuleMap = { modules };
}

describe('loadModules', () => {
beforeAll(() => {
getModule.mockImplementation(() => RootModule);
updateModuleRegistry.mockImplementation(() => ({
'some-root': {
node: {
url: 'https://example.com/cdn/some-root/2.2.2/some-root.node.js',
integrity: '4y45hr',
},
browser: {
url: 'https://example.com/cdn/some-root/2.2.2/some-root.browser.js',
integrity: 'nggdfhr34',
},
legacyBrowser: {
url: 'https://example.com/cdn/some-root/2.2.2/some-root.legacy.browser.js',
integrity: '7567ee',
},
},
}));
updateModuleRegistry.mockImplementation(() => modules);

global.fetch = jest.fn(() => Promise.resolve({
json: () => Promise.resolve(moduleMap),
json: () => Promise.resolve(fetchedModuleMap),
}));
});

Expand All @@ -94,21 +83,45 @@ describe('loadModules', () => {
});

it('updates the holocron module registry', async () => {
setFetchedRootModuleVersion('1.1.1');
await loadModules();
expect(updateModuleRegistry).toHaveBeenCalledWith({
moduleMap: addBaseUrlToModuleMap(moduleMap),
moduleMap: addBaseUrlToModuleMap(fetchedModuleMap),
batchModulesToUpdate: require('../../../src/server/utils/batchModulesToUpdate').default,
getModulesToUpdate: require('../../../src/server/utils/getModulesToUpdate').default,
onModuleLoad: require('../../../src/server/utils/onModuleLoad').default,
});
});

it('updates the client module map cache', async () => {
setFetchedRootModuleVersion('1.1.2');
await loadModules();
expect(getClientModuleMapCache()).toMatchSnapshot();
});

it('returns loaded modules', async () => {
setFetchedRootModuleVersion('2.0.0');
const loadedModules = await loadModules();
expect(loadedModules).toEqual({
'some-root': {
browser: {
integrity: 'nggdfhr34',
url: 'https://example.com/cdn/some-root/2.0.0/some-root.browser.js',
},
legacyBrowser: {
integrity: '7567ee',
url: 'https://example.com/cdn/some-root/2.0.0/some-root.legacy.browser.js',
},
node: {
integrity: '4y45hr',
url: 'https://example.com/cdn/some-root/2.0.0/some-root.node.js',
},
},
});
});

it('doesnt update caches when there are no changes', async () => {
setFetchedRootModuleVersion('1.1.3');
updateModuleRegistry.mockImplementationOnce(() => ({}));
await loadModules();
expect(getClientModuleMapCache()).toMatchSnapshot();
Expand All @@ -118,12 +131,13 @@ describe('loadModules', () => {
RootModule[CONFIGURATION_KEY] = {
csp: "default-src 'none';",
};

setFetchedRootModuleVersion('1.1.4');
await loadModules();
expect(updateCSP).toHaveBeenCalledWith("default-src 'none';");
});

it('calls updateCSP even when csp is not set', async () => {
setFetchedRootModuleVersion('1.1.5');
delete RootModule[CONFIGURATION_KEY].csp;
await loadModules();
expect(updateCSP).toHaveBeenCalledWith(undefined);
Expand All @@ -150,8 +164,25 @@ describe('loadModules', () => {
});

it('does not attempt to update CSP', async () => {
setFetchedRootModuleVersion('1.1.6');
await loadModules();
expect(updateCSP).not.toHaveBeenCalled();
});
});

describe('when module map does not change', () => {
beforeAll(async () => {
setFetchedRootModuleVersion('1.1.1');
await loadModules();
jest.clearAllMocks();
});
it('does not updateModuleRegistry', async () => {
await loadModules();
expect(updateModuleRegistry).not.toHaveBeenCalled();
});
it('does not return any modules', async () => {
const loadedModules = await loadModules();
expect(loadedModules).toEqual({});
});
});
});
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"lean-intl": "^4.2.2",
"matcher": "^4.0.0",
"node-fetch": "^2.6.7",
"object-hash": "^3.0.0",
"opossum": "^7.1.0",
"opossum-prometheus": "^0.3.0",
"pidusage": "^3.0.2",
Expand Down
9 changes: 9 additions & 0 deletions src/server/utils/loadModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { getModule } from 'holocron';
import { updateModuleRegistry } from 'holocron/server';

import hash from 'object-hash';
import onModuleLoad, { CONFIGURATION_KEY } from './onModuleLoad';
import batchModulesToUpdate from './batchModulesToUpdate';
import getModulesToUpdate from './getModulesToUpdate';
Expand All @@ -25,9 +26,17 @@ import { setClientModuleMapCache } from './clientModuleMapCache';
import { updateCSP } from '../plugins/csp';
import addBaseUrlToModuleMap from './addBaseUrlToModuleMap';

let cashedModuleMapHash;

const loadModules = async () => {
const moduleMapResponse = await fetch(process.env.HOLOCRON_MODULE_MAP_URL);
const moduleMap = addBaseUrlToModuleMap(await moduleMapResponse.json());

const moduleMapHash = hash(moduleMap);
if (cashedModuleMapHash && cashedModuleMapHash === moduleMapHash) {
return {};
}
cashedModuleMapHash = moduleMapHash;
const serverConfig = getServerStateConfig();

const loadedModules = await updateModuleRegistry({
Expand Down

0 comments on commit 17a3b4d

Please sign in to comment.