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.

uri.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /**
  2. * The {@link RegExp} pattern of the authority of a URI.
  3. *
  4. * @private
  5. * @type {string}
  6. */
  7. const _URI_AUTHORITY_PATTERN = '(//[^/?#]+)';
  8. /**
  9. * The {@link RegExp} pattern of the path of a URI.
  10. *
  11. * @private
  12. * @type {string}
  13. */
  14. const _URI_PATH_PATTERN = '([^?#]*)';
  15. /**
  16. * The {@link RegExp} pattern of the protocol of a URI.
  17. *
  18. * @private
  19. * @type {string}
  20. */
  21. const _URI_PROTOCOL_PATTERN = '([a-z][a-z0-9\\.\\+-]*:)';
  22. /**
  23. * Fixes the hier-part of a specific URI (string) so that the URI is well-known.
  24. * For example, certain Jitsi Meet deployments are not conventional but it is
  25. * possible to translate their URLs into conventional.
  26. *
  27. * @param {string} uri - The URI (string) to fix the hier-part of.
  28. * @private
  29. * @returns {string}
  30. */
  31. function _fixURIStringHierPart(uri) {
  32. // Rewrite the specified URL in order to handle special cases such as
  33. // hipchat.com and enso.me which do not follow the common pattern of most
  34. // Jitsi Meet deployments.
  35. // hipchat.com
  36. let regex
  37. = new RegExp(
  38. `^${_URI_PROTOCOL_PATTERN}//hipchat\\.com/video/call/`,
  39. 'gi');
  40. let match = regex.exec(uri);
  41. if (!match) {
  42. // enso.me
  43. regex
  44. = new RegExp(
  45. `^${_URI_PROTOCOL_PATTERN}//enso\\.me/(?:call|meeting)/`,
  46. 'gi');
  47. match = regex.exec(uri);
  48. }
  49. if (match) {
  50. /* eslint-disable no-param-reassign, prefer-template */
  51. uri
  52. = match[1] /* protocol */
  53. + '//enso.hipchat.me/'
  54. + uri.substring(regex.lastIndex); /* room (name) */
  55. /* eslint-enable no-param-reassign, prefer-template */
  56. }
  57. return uri;
  58. }
  59. /**
  60. * Fixes the scheme part of a specific URI (string) so that it contains a
  61. * well-known scheme such as HTTP(S). For example, the mobile app implements an
  62. * app-specific URI scheme in addition to Universal Links. The app-specific
  63. * scheme may precede or replace the well-known scheme. In such a case, dealing
  64. * with the app-specific scheme only complicates the logic and it is simpler to
  65. * get rid of it (by translating the app-specific scheme into a well-known
  66. * scheme).
  67. *
  68. * @param {string} uri - The URI (string) to fix the scheme of.
  69. * @private
  70. * @returns {string}
  71. */
  72. function _fixURIStringScheme(uri: string) {
  73. const regex = new RegExp(`^${_URI_PROTOCOL_PATTERN}+`, 'gi');
  74. const match = regex.exec(uri);
  75. if (match) {
  76. // As an implementation convenience, pick up the last scheme and make
  77. // sure that it is a well-known one.
  78. let protocol = match[match.length - 1].toLowerCase();
  79. if (protocol !== 'http:' && protocol !== 'https:') {
  80. protocol = 'https:';
  81. }
  82. /* eslint-disable no-param-reassign */
  83. uri = uri.substring(regex.lastIndex);
  84. if (uri.startsWith('//')) {
  85. // The specified URL was not a room name only, it contained an
  86. // authority.
  87. uri = protocol + uri;
  88. }
  89. /* eslint-enable no-param-reassign */
  90. }
  91. return uri;
  92. }
  93. /**
  94. * Gets the (Web application) context root defined by a specific location (URI).
  95. *
  96. * @param {Object} location - The location (URI) which defines the (Web
  97. * application) context root.
  98. * @public
  99. * @returns {string} - The (Web application) context root defined by the
  100. * specified {@code location} (URI).
  101. */
  102. export function getLocationContextRoot(location: Object) {
  103. const pathname = location.pathname;
  104. const contextRootEndIndex = pathname.lastIndexOf('/');
  105. return (
  106. contextRootEndIndex === -1
  107. ? '/'
  108. : pathname.substring(0, contextRootEndIndex + 1));
  109. }
  110. /**
  111. * Constructs a new {@code Array} with URL parameter {@code String}s out of a
  112. * specific {@code Object}.
  113. *
  114. * @param {Object} obj - The {@code Object} to turn into URL parameter
  115. * {@code String}s.
  116. * @returns {Array<string>} The {@code Array} with URL parameter {@code String}s
  117. * constructed out of the specified {@code obj}.
  118. */
  119. function _objectToURLParamsArray(obj = {}) {
  120. const params = [];
  121. for (const key in obj) { // eslint-disable-line guard-for-in
  122. try {
  123. params.push(
  124. `${key}=${encodeURIComponent(JSON.stringify(obj[key]))}`);
  125. } catch (e) {
  126. console.warn(`Error encoding ${key}: ${e}`);
  127. }
  128. }
  129. return params;
  130. }
  131. /**
  132. * Parses a specific URI string into an object with the well-known properties of
  133. * the {@link Location} and/or {@link URL} interfaces implemented by Web
  134. * browsers. The parsing attempts to be in accord with IETF's RFC 3986.
  135. *
  136. * @param {string} str - The URI string to parse.
  137. * @public
  138. * @returns {{
  139. * hash: string,
  140. * host: (string|undefined),
  141. * hostname: (string|undefined),
  142. * pathname: string,
  143. * port: (string|undefined),
  144. * protocol: (string|undefined),
  145. * search: string
  146. * }}
  147. */
  148. export function parseStandardURIString(str: string) {
  149. /* eslint-disable no-param-reassign */
  150. const obj = {
  151. toString: _standardURIToString
  152. };
  153. let regex;
  154. let match;
  155. // protocol
  156. regex = new RegExp(`^${_URI_PROTOCOL_PATTERN}`, 'gi');
  157. match = regex.exec(str);
  158. if (match) {
  159. obj.protocol = match[1].toLowerCase();
  160. str = str.substring(regex.lastIndex);
  161. }
  162. // authority
  163. regex = new RegExp(`^${_URI_AUTHORITY_PATTERN}`, 'gi');
  164. match = regex.exec(str);
  165. if (match) {
  166. let authority = match[1].substring(/* // */ 2);
  167. str = str.substring(regex.lastIndex);
  168. // userinfo
  169. const userinfoEndIndex = authority.indexOf('@');
  170. if (userinfoEndIndex !== -1) {
  171. authority = authority.substring(userinfoEndIndex + 1);
  172. }
  173. obj.host = authority;
  174. // port
  175. const portBeginIndex = authority.lastIndexOf(':');
  176. if (portBeginIndex !== -1) {
  177. obj.port = authority.substring(portBeginIndex + 1);
  178. authority = authority.substring(0, portBeginIndex);
  179. }
  180. // hostname
  181. obj.hostname = authority;
  182. }
  183. // pathname
  184. regex = new RegExp(`^${_URI_PATH_PATTERN}`, 'gi');
  185. match = regex.exec(str);
  186. let pathname;
  187. if (match) {
  188. pathname = match[1];
  189. str = str.substring(regex.lastIndex);
  190. }
  191. if (pathname) {
  192. pathname.startsWith('/') || (pathname = `/${pathname}`);
  193. } else {
  194. pathname = '/';
  195. }
  196. obj.pathname = pathname;
  197. // query
  198. if (str.startsWith('?')) {
  199. let hashBeginIndex = str.indexOf('#', 1);
  200. if (hashBeginIndex === -1) {
  201. hashBeginIndex = str.length;
  202. }
  203. obj.search = str.substring(0, hashBeginIndex);
  204. str = str.substring(hashBeginIndex);
  205. } else {
  206. obj.search = ''; // Google Chrome
  207. }
  208. // fragment
  209. obj.hash = str.startsWith('#') ? str : '';
  210. /* eslint-enable no-param-reassign */
  211. return obj;
  212. }
  213. /**
  214. * Parses a specific URI which (supposedly) references a Jitsi Meet resource
  215. * (location).
  216. *
  217. * @param {(string|undefined)} uri - The URI to parse which (supposedly)
  218. * references a Jitsi Meet resource (location).
  219. * @public
  220. * @returns {{
  221. * room: (string|undefined)
  222. * }}
  223. */
  224. export function parseURIString(uri: ?string) {
  225. if (typeof uri !== 'string') {
  226. return undefined;
  227. }
  228. const obj
  229. = parseStandardURIString(
  230. _fixURIStringHierPart(_fixURIStringScheme(uri)));
  231. // Add the properties that are specific to a Jitsi Meet resource (location)
  232. // such as contextRoot, room:
  233. // contextRoot
  234. obj.contextRoot = getLocationContextRoot(obj);
  235. // The room (name) is the last component of pathname.
  236. const { pathname } = obj;
  237. obj.room = pathname.substring(pathname.lastIndexOf('/') + 1) || undefined;
  238. return obj;
  239. }
  240. /**
  241. * Implements {@code href} and {@code toString} for the {@code Object} returned
  242. * by {@link #parseStandardURIString}.
  243. *
  244. * @param {Object} [thiz] - An {@code Object} returned by
  245. * {@code #parseStandardURIString} if any; otherwise, it is presumed that the
  246. * function is invoked on such an instance.
  247. * @returns {string}
  248. */
  249. function _standardURIToString(thiz: ?Object) {
  250. // eslint-disable-next-line no-invalid-this
  251. const { hash, host, pathname, protocol, search } = thiz || this;
  252. let str = '';
  253. protocol && (str += protocol);
  254. // TODO userinfo
  255. host && (str += `//${host}`);
  256. str += pathname || '/';
  257. search && (str += search);
  258. hash && (str += hash);
  259. return str;
  260. }
  261. /**
  262. * Attempts to return a {@code String} representation of a specific
  263. * {@code Object} which is supposed to represent a URL. Obviously, if a
  264. * {@code String} is specified, it is returned. If a {@code URL} is specified,
  265. * its {@code URL#href} is returned. Additionally, an {@code Object} similar to
  266. * the one accepted by the constructor of Web's ExternalAPI is supported on both
  267. * mobile/React Native and Web/React.
  268. *
  269. * @param {string|Object} obj - The URL to return a {@code String}
  270. * representation of.
  271. * @returns {string} - A {@code String} representation of the specified
  272. * {@code obj} which is supposed to represent a URL.
  273. */
  274. export function toURLString(obj: ?(string | Object)): ?string {
  275. let str;
  276. switch (typeof obj) {
  277. case 'object':
  278. if (obj) {
  279. if (obj instanceof URL) {
  280. str = obj.href;
  281. } else {
  282. str = urlObjectToString(obj);
  283. }
  284. }
  285. break;
  286. case 'string':
  287. str = String(obj);
  288. break;
  289. }
  290. return str;
  291. }
  292. /**
  293. * Attempts to return a {@code String} representation of a specific
  294. * {@code Object} similar to the one accepted by the constructor
  295. * of Web's ExternalAPI.
  296. *
  297. * @param {Object} o - The URL to return a {@code String} representation of.
  298. * @returns {string} - A {@code String} representation of the specified
  299. * {@code Object}.
  300. */
  301. export function urlObjectToString(o: Object): ?string {
  302. const url = parseStandardURIString(o.url || '');
  303. // protocol
  304. if (!url.protocol) {
  305. let protocol = o.protocol || o.scheme;
  306. if (protocol) {
  307. // Protocol is supposed to be the scheme and the final ':'. Anyway,
  308. // do not make a fuss if the final ':' is not there.
  309. protocol.endsWith(':') || (protocol += ':');
  310. url.protocol = protocol;
  311. }
  312. }
  313. // authority & pathname
  314. let { pathname } = url;
  315. if (!url.host) {
  316. // Web's ExternalAPI domain
  317. //
  318. // It may be host/hostname and pathname with the latter denoting the
  319. // tenant.
  320. const { host, hostname, pathname: contextRoot, port }
  321. = parseStandardURIString(o.domain || o.host || o.hostname);
  322. // authority
  323. if (host) {
  324. url.host = host;
  325. url.hostname = hostname;
  326. url.port = port;
  327. }
  328. // pathname
  329. pathname === '/' && contextRoot !== '/' && (pathname = contextRoot);
  330. }
  331. // pathname
  332. // Web's ExternalAPI roomName
  333. const room = o.roomName || o.room;
  334. if (room
  335. && (url.pathname.endsWith('/')
  336. || !url.pathname.endsWith(`/${room}`))) {
  337. pathname.endsWith('/') || (pathname += '/');
  338. pathname += room;
  339. }
  340. url.pathname = pathname;
  341. // query/search
  342. // Web's ExternalAPI jwt
  343. const { jwt } = o;
  344. if (jwt) {
  345. let { search } = url;
  346. if (search.indexOf('?jwt=') === -1 && search.indexOf('&jwt=') === -1) {
  347. search.startsWith('?') || (search = `?${search}`);
  348. search.length === 1 || (search += '&');
  349. search += `jwt=${jwt}`;
  350. url.search = search;
  351. }
  352. }
  353. // fragment/hash
  354. let { hash } = url;
  355. for (const configName of [ 'config', 'interfaceConfig' ]) {
  356. const urlParamsArray
  357. = _objectToURLParamsArray(
  358. o[`${configName}Overwrite`]
  359. || o[configName]
  360. || o[`${configName}Override`]);
  361. if (urlParamsArray.length) {
  362. let urlParamsString
  363. = `${configName}.${urlParamsArray.join(`&${configName}.`)}`;
  364. if (hash.length) {
  365. urlParamsString = `&${urlParamsString}`;
  366. } else {
  367. hash = '#';
  368. }
  369. hash += urlParamsString;
  370. }
  371. }
  372. url.hash = hash;
  373. return url.toString() || undefined;
  374. }