Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

functions.any.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // @flow
  2. import _ from 'lodash';
  3. import CONFIG_WHITELIST from './configWhitelist';
  4. import { _CONFIG_STORE_PREFIX } from './constants';
  5. import INTERFACE_CONFIG_WHITELIST from './interfaceConfigWhitelist';
  6. import parseURLParams from './parseURLParams';
  7. import logger from './logger';
  8. // XXX The functions getRoomName and parseURLParams are split out of
  9. // functions.js because they are bundled in both app.bundle and
  10. // do_external_connect, webpack 1 does not support tree shaking, and we don't
  11. // want all functions to be bundled in do_external_connect.
  12. export { default as getRoomName } from './getRoomName';
  13. export { parseURLParams };
  14. /**
  15. * Create a "fake" configuration object for the given base URL. This is used in case the config
  16. * couldn't be loaded in the welcome page, so at least we have something to try with.
  17. *
  18. * @param {string} baseURL - URL of the deployment for which we want the fake config.
  19. * @returns {Object}
  20. */
  21. export function createFakeConfig(baseURL: string) {
  22. const url = new URL(baseURL);
  23. return {
  24. hosts: {
  25. domain: url.hostname,
  26. muc: `conference.${url.hostname}`
  27. },
  28. bosh: `${baseURL}http-bind`,
  29. clientNode: 'https://jitsi.org/jitsi-meet',
  30. p2p: {
  31. enabled: true
  32. }
  33. };
  34. }
  35. /* eslint-disable max-params, no-shadow */
  36. /**
  37. * Overrides JSON properties in {@code config} and
  38. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  39. * Overrides only the whitelisted keys.
  40. *
  41. * @param {Object} config - The config Object in which we'll be overriding
  42. * properties.
  43. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  44. * be overriding properties.
  45. * @param {Object} loggingConfig - The loggingConfig Object in which we'll be
  46. * overriding properties.
  47. * @param {Object} json - Object containing configuration properties.
  48. * Destination object is selected based on root property name:
  49. * {
  50. * config: {
  51. * // config.js properties here
  52. * },
  53. * interfaceConfig: {
  54. * // interface_config.js properties here
  55. * },
  56. * loggingConfig: {
  57. * // logging_config.js properties here
  58. * }
  59. * }.
  60. * @returns {void}
  61. */
  62. export function overrideConfigJSON(
  63. config: ?Object, interfaceConfig: ?Object, loggingConfig: ?Object,
  64. json: Object) {
  65. for (const configName of Object.keys(json)) {
  66. let configObj;
  67. if (configName === 'config') {
  68. configObj = config;
  69. } else if (configName === 'interfaceConfig') {
  70. configObj = interfaceConfig;
  71. } else if (configName === 'loggingConfig') {
  72. configObj = loggingConfig;
  73. }
  74. if (configObj) {
  75. const configJSON
  76. = _getWhitelistedJSON(configName, json[configName]);
  77. if (!_.isEmpty(configJSON)) {
  78. logger.info(
  79. `Extending ${configName} with: ${
  80. JSON.stringify(configJSON)}`);
  81. // eslint-disable-next-line arrow-body-style
  82. _.mergeWith(configObj, configJSON, (oldValue, newValue) => {
  83. // XXX We don't want to merge the arrays, we want to
  84. // overwrite them.
  85. return Array.isArray(oldValue) ? newValue : undefined;
  86. });
  87. }
  88. }
  89. }
  90. }
  91. /* eslint-enable max-params, no-shadow */
  92. /**
  93. * Apply whitelist filtering for configs with whitelists, skips this for others
  94. * configs (loggingConfig).
  95. * Only extracts overridden values for keys we allow to be overridden.
  96. *
  97. * @param {string} configName - The config name, one of config,
  98. * interfaceConfig, loggingConfig.
  99. * @param {Object} configJSON - The object with keys and values to override.
  100. * @private
  101. * @returns {Object} - The result object only with the keys
  102. * that are whitelisted.
  103. */
  104. function _getWhitelistedJSON(configName, configJSON) {
  105. if (configName === 'interfaceConfig') {
  106. return _.pick(configJSON, INTERFACE_CONFIG_WHITELIST);
  107. } else if (configName === 'config') {
  108. return _.pick(configJSON, CONFIG_WHITELIST);
  109. }
  110. return configJSON;
  111. }
  112. /**
  113. * Restores a Jitsi Meet config.js from {@code localStorage} if it was
  114. * previously downloaded from a specific {@code baseURL} and stored with
  115. * {@link storeConfig}.
  116. *
  117. * @param {string} baseURL - The base URL from which the config.js was
  118. * previously downloaded and stored with {@code storeConfig}.
  119. * @returns {?Object} The Jitsi Meet config.js which was previously downloaded
  120. * from {@code baseURL} and stored with {@code storeConfig} if it was restored;
  121. * otherwise, {@code undefined}.
  122. */
  123. export function restoreConfig(baseURL: string): ?Object {
  124. let storage;
  125. const key = `${_CONFIG_STORE_PREFIX}/${baseURL}`;
  126. try {
  127. // XXX Even reading the property localStorage of window may throw an
  128. // error (which is user agent-specific behavior).
  129. storage = window.localStorage;
  130. const config = storage.getItem(key);
  131. if (config) {
  132. return JSON.parse(config) || undefined;
  133. }
  134. } catch (e) {
  135. // Somehow incorrect data ended up in the storage. Clean it up.
  136. storage && storage.removeItem(key);
  137. }
  138. return undefined;
  139. }
  140. /* eslint-disable max-params */
  141. /**
  142. * Inspects the hash part of the location URI and overrides values specified
  143. * there in the corresponding config objects given as the arguments. The syntax
  144. * is: {@code https://server.com/room#config.debug=true
  145. * &interfaceConfig.showButton=false&loggingConfig.something=1}.
  146. *
  147. * In the hash part each parameter will be parsed to JSON and then the root
  148. * object will be matched with the corresponding config object given as the
  149. * argument to this function.
  150. *
  151. * @param {Object} config - This is the general config.
  152. * @param {Object} interfaceConfig - This is the interface config.
  153. * @param {Object} loggingConfig - The logging config.
  154. * @param {URI} location - The new location to which the app is navigating to.
  155. * @returns {void}
  156. */
  157. export function setConfigFromURLParams(
  158. config: ?Object,
  159. interfaceConfig: ?Object,
  160. loggingConfig: ?Object,
  161. location: Object) {
  162. const params = parseURLParams(location);
  163. const json = {};
  164. // At this point we have:
  165. // params = {
  166. // "config.disableAudioLevels": false,
  167. // "config.channelLastN": -1,
  168. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  169. // }
  170. // We want to have:
  171. // json = {
  172. // config: {
  173. // "disableAudioLevels": false,
  174. // "channelLastN": -1
  175. // },
  176. // interfaceConfig: {
  177. // "APP_NAME": "Jitsi Meet"
  178. // }
  179. // }
  180. config && (json.config = {});
  181. interfaceConfig && (json.interfaceConfig = {});
  182. loggingConfig && (json.loggingConfig = {});
  183. for (const param of Object.keys(params)) {
  184. let base = json;
  185. const names = param.split('.');
  186. const last = names.pop();
  187. for (const name of names) {
  188. base = base[name] = base[name] || {};
  189. }
  190. base[last] = params[param];
  191. }
  192. overrideConfigJSON(config, interfaceConfig, loggingConfig, json);
  193. }
  194. /* eslint-enable max-params */