You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.native.js 999B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // @flow
  2. import Bourne from '@hapi/bourne';
  3. import { NativeModules } from 'react-native';
  4. import { loadScript } from '../util';
  5. import logger from './logger';
  6. export * from './functions.any';
  7. const { JavaScriptSandbox } = NativeModules;
  8. /**
  9. * Loads config.js from a specific remote server.
  10. *
  11. * @param {string} url - The URL to load.
  12. * @returns {Promise<Object>}
  13. */
  14. export async function loadConfig(url: string): Promise<Object> {
  15. try {
  16. const configTxt = await loadScript(url, 10 * 1000 /* Timeout in ms */, true /* skipeval */);
  17. const configJson = await JavaScriptSandbox.evaluate(`${configTxt}\nJSON.stringify(config);`);
  18. const config = Bourne.parse(configJson);
  19. if (typeof config !== 'object') {
  20. throw new Error('config is not an object');
  21. }
  22. logger.info(`Config loaded from ${url}`);
  23. return config;
  24. } catch (err) {
  25. logger.error(`Failed to load config from ${url}`, err);
  26. throw err;
  27. }
  28. }