2022-10-21 11:09:15 +00:00
|
|
|
// @ts-ignore
|
2021-06-02 19:21:39 +00:00
|
|
|
import Bourne from '@hapi/bourne';
|
2019-10-11 10:05:45 +00:00
|
|
|
import { NativeModules } from 'react-native';
|
|
|
|
|
2022-10-21 11:09:15 +00:00
|
|
|
import { loadScript } from '../util/loadScript.native';
|
2020-05-20 10:57:03 +00:00
|
|
|
|
2019-10-11 10:05:45 +00:00
|
|
|
import logger from './logger';
|
|
|
|
|
|
|
|
export * from './functions.any';
|
|
|
|
|
|
|
|
const { JavaScriptSandbox } = NativeModules;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads config.js from a specific remote server.
|
|
|
|
*
|
|
|
|
* @param {string} url - The URL to load.
|
|
|
|
* @returns {Promise<Object>}
|
|
|
|
*/
|
|
|
|
export async function loadConfig(url: string): Promise<Object> {
|
|
|
|
try {
|
2020-09-30 07:46:35 +00:00
|
|
|
const configTxt = await loadScript(url, 10 * 1000 /* Timeout in ms */, true /* skipeval */);
|
2019-10-11 10:05:45 +00:00
|
|
|
const configJson = await JavaScriptSandbox.evaluate(`${configTxt}\nJSON.stringify(config);`);
|
2021-06-02 19:21:39 +00:00
|
|
|
const config = Bourne.parse(configJson);
|
2019-10-11 10:05:45 +00:00
|
|
|
|
|
|
|
if (typeof config !== 'object') {
|
|
|
|
throw new Error('config is not an object');
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info(`Config loaded from ${url}`);
|
|
|
|
|
|
|
|
return config;
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(`Failed to load config from ${url}`, err);
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|