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

Commit

Permalink
fix(utils): replace watchLocalModules cdn-url string to be ip/port
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephanie Coates authored and Stephanie Coates committed Jan 22, 2020
1 parent 75ba553 commit 0ccdae0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
74 changes: 74 additions & 0 deletions __tests__/server/utils/watchLocalModules.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ import {
getModules,
resetModuleRegistry,
} from 'holocron/moduleRegistry';
import { address } from 'ip';
import watchLocalModules from '../../../src/server/utils/watchLocalModules';

const ip = address();


jest.mock('chokidar', () => {
const listeners = {};
const watcher = () => null;
Expand Down Expand Up @@ -64,6 +68,13 @@ jest.mock('fs', () => {

describe('watchLocalModules', () => {
beforeEach(() => jest.clearAllMocks());
let origOneAppDevCDNPort;
beforeAll(() => {
origOneAppDevCDNPort = process.env.HTTP_ONE_APP_DEV_CDN_PORT;
});
afterAll(() => {
process.env.HTTP_ONE_APP_DEV_CDN_PORT = origOneAppDevCDNPort;
});

it('should watch the modules directory', () => {
watchLocalModules();
Expand Down Expand Up @@ -129,4 +140,67 @@ describe('watchLocalModules', () => {
await changeListener(changedPath);
expect(loadModule).not.toHaveBeenCalled();
});

it('should replace [one-app-dev-cdn-url] with correct ip and port', async () => {
const moduleName = 'some-module';
const moduleVersion = '1.0.1';
process.env.HTTP_ONE_APP_DEV_CDN_PORT = 3002;
const moduleMapSample = {
key: '123',
modules: {
[moduleName]: {
node: {
integrity: '133',
url: `[one-app-dev-cdn-url]/cdn/${moduleName}/${moduleVersion}/${moduleName}-node.js`,
},
browser: {
integrity: '234',
url: `[one-app-dev-cdn-url]/cdn/${moduleName}/${moduleVersion}/${moduleName}-browser.js`,
},
legacyBrowser: {
integrity: '134633',
url: `[one-app-dev-cdn-url]/cdn/${moduleName}/${moduleVersion}/${moduleName}-legacy.browser.js`,
},
},
},
};
const oneAppDevCdnAddress = `http://${ip}:${process.env.HTTP_ONE_APP_DEV_CDN_PORT || 3001}`;
const updatedModuleMapSample = {
key: '123',
modules: {
[moduleName]: {
node: {
integrity: '133',
url: `${oneAppDevCdnAddress}/cdn/${moduleName}/${moduleVersion}/${moduleName}-node.js`,
},
browser: {
integrity: '234',
url: `${oneAppDevCdnAddress}/cdn/${moduleName}/${moduleVersion}/${moduleName}-browser.js`,
},
legacyBrowser: {
integrity: '134633',
url: `${oneAppDevCdnAddress}/cdn/${moduleName}/${moduleVersion}/${moduleName}-legacy.browser.js`,
},
},
},
};
fs.readFileSync.mockImplementationOnce(() => JSON.stringify(moduleMapSample));
const modulePath = path.resolve(__dirname, `../../../static/modules/${moduleName}/${moduleVersion}/${moduleName}.node.js`);
const originalModule = () => null;
const updatedModule = () => null;
const modules = fromJS({ [moduleName]: originalModule });
const moduleMap = fromJS(moduleMapSample);
resetModuleRegistry(modules, moduleMap);
watchLocalModules();
const changeListener = chokidar.getListeners().change;
expect(getModules().get(moduleName)).toBe(originalModule);
loadModule.mockReturnValueOnce(Promise.resolve(updatedModule));
await changeListener(modulePath);
expect(loadModule).toHaveBeenCalledWith(
moduleName,
updatedModuleMapSample.modules[moduleName],
require('../../../src/server/utils/onModuleLoad').default
);
expect(getModules().get(moduleName)).toBe(updatedModule);
});
});
12 changes: 11 additions & 1 deletion src/server/utils/watchLocalModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import {
getModuleMap,
resetModuleRegistry,
} from 'holocron/moduleRegistry';
import { address } from 'ip';
import onModuleLoad from './onModuleLoad';

const ip = address();

export default function watchLocalModules() {
const staticsDirectoryPath = path.resolve(__dirname, '../../../static');
const moduleDirectory = path.resolve(staticsDirectoryPath, 'modules');
Expand All @@ -44,9 +47,16 @@ export default function watchLocalModules() {

const moduleMap = JSON.parse(fs.readFileSync(moduleMapPath, 'utf8'));

const moduleData = moduleMap.modules[moduleNameChangeDetectedIn];
const oneAppDevCdnAddress = `http://${ip}:${process.env.HTTP_ONE_APP_DEV_CDN_PORT || 3001}`;

moduleData.browser.url = moduleData.browser.url.replace('[one-app-dev-cdn-url]', oneAppDevCdnAddress);
moduleData.legacyBrowser.url = moduleData.legacyBrowser.url.replace('[one-app-dev-cdn-url]', oneAppDevCdnAddress);
moduleData.node.url = moduleData.node.url.replace('[one-app-dev-cdn-url]', oneAppDevCdnAddress);

const module = await loadModule(
moduleNameChangeDetectedIn,
moduleMap.modules[moduleNameChangeDetectedIn],
moduleData,
onModuleLoad
);

Expand Down

0 comments on commit 0ccdae0

Please sign in to comment.