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

ref: Use the VM to execute local config files #171

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
11 changes: 10 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as echarts from 'echarts';
import {promises as fsPromises} from 'node:fs';
import path from 'node:path';
import {runInNewContext} from 'node:vm';

Expand All @@ -24,6 +25,14 @@ async function loadViaHttp(url: string, ac?: AbortController) {

const configJavascript = await resp.text();

return evalConfigFile(configJavascript);
}

/**
* Loads a config file by using the vm module to execute the file cotnents as a
* string.
*/
function evalConfigFile(configJavascript: string) {
const exports = {default: null};
const module = {exports};

Expand Down Expand Up @@ -77,7 +86,7 @@ export class ConfigService {
*/
async fetchConfig(deadline: number) {
if (!this.configIsViaHttp) {
return require(/* webpackIgnore: true */ path.resolve(this.#uri)).default;
return evalConfigFile(await fsPromises.readFile(path.resolve(this.#uri), 'utf8'));
}

let config: any = null;
Expand Down
77 changes: 42 additions & 35 deletions tests/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,75 @@
import fetchMock from 'jest-fetch-mock';
import {promises as fsPromises} from 'node:fs';

fetchMock.enableMocks();

import {ConfigService} from 'app/config';

jest.mock('node:fs', () => ({
...jest.requireActual('node:fs'),
promises: {readFile: jest.fn()},
}));

describe('config', () => {
beforeEach(() => {
fetchMock.resetMocks();
});

it('can load config via files', async () => {
const init = jest.fn();
jest.mock(
'/myConfig',
() => {
const renderConfig = new Map();
renderConfig.set('fileExample', {
key: 'fileExample',
height: 200,
width: 100,
getOption: (series: any) => ({series}),
});

return {default: {renderConfig, init, version: 'abc'}};
},
{virtual: true}
);
const configModule = `
var renderConfig = new Map();
renderConfig.set('fileExample', {
key: 'example',
height: 200,
width: 100,
getOption: series => ({series}),
});

module.exports = {renderConfig, version: 'abc'};`;

jest.mocked(fsPromises.readFile).mockResolvedValue(Buffer.from(configModule));

const config = new ConfigService('/myConfig');
await config.resolveEnsured();

expect(fsPromises.readFile).toHaveBeenCalledWith('/myConfig', 'utf8');
expect(config.getConfig('fileExample')?.height).toEqual(200);
expect(init).toHaveBeenCalledTimes(1);
});

it('does not require init', async () => {
jest.mock(
'/myConfig',
() => {
const renderConfig = new Map();
renderConfig.set('fileExample', {
key: 'fileExample',
height: 200,
width: 100,
getOption: (series: any) => ({series}),
});

return {default: {renderConfig, version: 'abc'}};
},
{virtual: true}
);
const configModule = `
var renderConfig = new Map();
renderConfig.set('example', {
key: 'example',
height: 200,
width: 100,
getOption: series => ({series}),
});

module.exports = {
renderConfig,
version: 'abc',
init: _ => console.log('init called')
};`;

const consoleSpy = jest.spyOn(console, 'log');
consoleSpy.mockImplementation(() => {});

jest.mocked(fsPromises.readFile).mockResolvedValue(Buffer.from(configModule));

const config = new ConfigService('/myConfig');
await config.resolveEnsured();

expect(config.getConfig('fileExample')?.height).toEqual(200);
expect(config.getConfig('example')?.height).toEqual(200);
expect(consoleSpy).toHaveBeenCalledWith('init called');
});

it('can load config via http', async () => {
const configModule = `
var renderConfig = new Map();
renderConfig.set('example', {
key: 'example',
height: 100,
height: 200,
width: 100,
getOption: series => ({series}),
});
Expand All @@ -75,6 +82,6 @@ describe('config', () => {
await config.resolveEnsured();

expect(fetchMock.mock.calls[0][0]).toEqual('https://example.com/myConfig.js');
expect(config.getConfig('example')?.height).toEqual(100);
expect(config.getConfig('example')?.height).toEqual(200);
});
});