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.

polyfills-browser.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. import BackgroundTimer from 'react-native-background-timer';
  2. import '@webcomponents/url'; // Polyfill for URL constructor
  3. import { Platform } from '../../react';
  4. // XXX The library lib-jitsi-meet utilizes window.localStorage at the time of
  5. // this writing and, consequently, the browser-related polyfills implemented
  6. // here by the feature base/lib-jitsi-meet for the purposes of the library
  7. // lib-jitsi-meet are incomplete without the Web Storage API! Should the library
  8. // lib-jitsi-meet (and its dependencies) stop utilizing window.localStorage,
  9. // the following import may be removed:
  10. import '../../storage';
  11. /**
  12. * Gets the first common prototype of two specified Objects (treating the
  13. * objects themselves as prototypes as well).
  14. *
  15. * @param {Object} a - The first prototype chain to climb in search of a common
  16. * prototype.
  17. * @param {Object} b - The second prototype chain to climb in search of a common
  18. * prototype.
  19. * @returns {Object|undefined} - The first common prototype of a and b.
  20. */
  21. function _getCommonPrototype(a, b) {
  22. // Allow the arguments to be prototypes themselves.
  23. if (a === b) {
  24. return a;
  25. }
  26. let p;
  27. if (((p = Object.getPrototypeOf(a)) && (p = _getCommonPrototype(b, p)))
  28. || ((p = Object.getPrototypeOf(b))
  29. && (p = _getCommonPrototype(a, p)))) {
  30. return p;
  31. }
  32. return undefined;
  33. }
  34. /**
  35. * Implements an absolute minimum of the common logic of Document.querySelector
  36. * and Element.querySelector. Implements the most simple of selectors necessary
  37. * to satisfy the call sites at the time of this writing i.e. select by tagName.
  38. *
  39. * @param {Node} node - The Node which is the root of the tree to query.
  40. * @param {string} selectors - The group of CSS selectors to match on.
  41. * @returns {Element} - The first Element which is a descendant of the specified
  42. * node and matches the specified group of selectors.
  43. */
  44. function _querySelector(node, selectors) {
  45. let element = null;
  46. node && _visitNode(node, n => {
  47. if (n.nodeType === 1 /* ELEMENT_NODE */
  48. && n.nodeName === selectors) {
  49. element = n;
  50. return true;
  51. }
  52. return false;
  53. });
  54. return element;
  55. }
  56. /**
  57. * Visits each Node in the tree of a specific root Node (using depth-first
  58. * traversal) and invokes a specific callback until the callback returns true.
  59. *
  60. * @param {Node} node - The root Node which represents the tree of Nodes to
  61. * visit.
  62. * @param {Function} callback - The callback to invoke with each visited Node.
  63. * @returns {boolean} - True if the specified callback returned true for a Node
  64. * (at which point the visiting stopped); otherwise, false.
  65. */
  66. function _visitNode(node, callback) {
  67. if (callback(node)) {
  68. return true;
  69. }
  70. /* eslint-disable no-param-reassign, no-extra-parens */
  71. if ((node = node.firstChild)) {
  72. do {
  73. if (_visitNode(node, callback)) {
  74. return true;
  75. }
  76. } while ((node = node.nextSibling));
  77. }
  78. /* eslint-enable no-param-reassign, no-extra-parens */
  79. return false;
  80. }
  81. (global => {
  82. const { DOMParser } = require('xmldom');
  83. // DOMParser
  84. //
  85. // Required by:
  86. // - lib-jitsi-meet requires this if using WebSockets
  87. global.DOMParser = DOMParser;
  88. // addEventListener
  89. //
  90. // Required by:
  91. // - jQuery
  92. if (typeof global.addEventListener === 'undefined') {
  93. // eslint-disable-next-line no-empty-function
  94. global.addEventListener = () => {};
  95. }
  96. // removeEventListener
  97. //
  98. // Required by:
  99. // - features/base/conference/middleware
  100. if (typeof global.removeEventListener === 'undefined') {
  101. // eslint-disable-next-line no-empty-function
  102. global.removeEventListener = () => {};
  103. }
  104. // document
  105. //
  106. // Required by:
  107. // - jQuery
  108. // - Strophe
  109. if (typeof global.document === 'undefined') {
  110. const document
  111. = new DOMParser().parseFromString(
  112. '<html><head></head><body></body></html>',
  113. 'text/xml');
  114. // document.addEventListener
  115. //
  116. // Required by:
  117. // - jQuery
  118. if (typeof document.addEventListener === 'undefined') {
  119. // eslint-disable-next-line no-empty-function
  120. document.addEventListener = () => {};
  121. }
  122. // document.cookie
  123. //
  124. // Required by:
  125. // - herment
  126. if (typeof document.cookie === 'undefined') {
  127. document.cookie = '';
  128. }
  129. // document.implementation.createHTMLDocument
  130. //
  131. // Required by:
  132. // - jQuery
  133. if (typeof document.implementation.createHTMLDocument === 'undefined') {
  134. document.implementation.createHTMLDocument = function(title = '') {
  135. const htmlDocument
  136. = new DOMParser().parseFromString(
  137. `<html>
  138. <head><title>${title}</title></head>
  139. <body></body>
  140. </html>`,
  141. 'text/xml');
  142. Object.defineProperty(htmlDocument, 'body', {
  143. get() {
  144. return htmlDocument.getElementsByTagName('body')[0];
  145. }
  146. });
  147. return htmlDocument;
  148. };
  149. }
  150. // Element.querySelector
  151. //
  152. // Required by:
  153. // - lib-jitsi-meet/modules/xmpp
  154. const elementPrototype
  155. = Object.getPrototypeOf(document.documentElement);
  156. if (elementPrototype) {
  157. if (typeof elementPrototype.querySelector === 'undefined') {
  158. elementPrototype.querySelector = function(selectors) {
  159. return _querySelector(this, selectors);
  160. };
  161. }
  162. // Element.remove
  163. //
  164. // Required by:
  165. // - lib-jitsi-meet ChatRoom#onPresence parsing
  166. if (typeof elementPrototype.remove === 'undefined') {
  167. elementPrototype.remove = function() {
  168. if (this.parentNode !== null) {
  169. this.parentNode.removeChild(this);
  170. }
  171. };
  172. }
  173. // Element.innerHTML
  174. //
  175. // Required by:
  176. // - jQuery's .append method
  177. if (!elementPrototype.hasOwnProperty('innerHTML')) {
  178. Object.defineProperty(elementPrototype, 'innerHTML', {
  179. get() {
  180. return this.childNodes.toString();
  181. },
  182. set(innerHTML) {
  183. // MDN says: removes all of element's children, parses
  184. // the content string and assigns the resulting nodes as
  185. // children of the element.
  186. // Remove all of element's children.
  187. this.textContent = '';
  188. // Parse the content string.
  189. const d
  190. = new DOMParser().parseFromString(
  191. `<div>${innerHTML}</div>`,
  192. 'text/xml');
  193. // Assign the resulting nodes as children of the
  194. // element.
  195. const documentElement = d.documentElement;
  196. let child;
  197. // eslint-disable-next-line no-cond-assign
  198. while (child = documentElement.firstChild) {
  199. this.appendChild(child);
  200. }
  201. }
  202. });
  203. }
  204. // Element.children
  205. //
  206. // Required by:
  207. // - lib-jitsi-meet ChatRoom#onPresence parsing
  208. if (!elementPrototype.hasOwnProperty('children')) {
  209. Object.defineProperty(elementPrototype, 'children', {
  210. get() {
  211. const nodes = this.childNodes;
  212. const children = [];
  213. let i = 0;
  214. let node = nodes[i];
  215. while (node) {
  216. if (node.nodeType === 1) {
  217. children.push(node);
  218. }
  219. i += 1;
  220. node = nodes[i];
  221. }
  222. return children;
  223. }
  224. });
  225. }
  226. }
  227. // FIXME There is a weird infinite loop related to console.log and
  228. // Document and/or Element at the time of this writing. Work around it
  229. // by patching Node and/or overriding console.log.
  230. const documentPrototype = Object.getPrototypeOf(document);
  231. const nodePrototype
  232. = _getCommonPrototype(documentPrototype, elementPrototype);
  233. if (nodePrototype
  234. // XXX The intention was to find Node from which Document and
  235. // Element extend. If for whatever reason we've reached Object,
  236. // then it doesn't sound like what expected.
  237. && nodePrototype !== Object.getPrototypeOf({})) {
  238. // Override console.log.
  239. const { console } = global;
  240. if (console) {
  241. const loggerLevels = require('jitsi-meet-logger').levels;
  242. Object.keys(loggerLevels).forEach(key => {
  243. const level = loggerLevels[key];
  244. const consoleLog = console[level];
  245. /* eslint-disable prefer-rest-params */
  246. if (typeof consoleLog === 'function') {
  247. console[level] = function(...args) {
  248. // XXX If console's disableYellowBox is truthy, then
  249. // react-native will not automatically display the
  250. // yellow box for the warn level. However, it will
  251. // still display the red box for the error level.
  252. // But I disable the yellow box when I don't want to
  253. // have react-native automatically show me the
  254. // console's output just like in the Release build
  255. // configuration. Because I didn't find a way to
  256. // disable the red box, downgrade the error level to
  257. // warn. The red box will still be displayed but not
  258. // for the error level.
  259. if (console.disableYellowBox && level === 'error') {
  260. console.warn(...args);
  261. return;
  262. }
  263. const { length } = args;
  264. for (let i = 0; i < length; ++i) {
  265. let arg = args[i];
  266. if (arg
  267. && typeof arg !== 'string'
  268. // Limit the console.log override to
  269. // Node (instances).
  270. && nodePrototype.isPrototypeOf(arg)) {
  271. const toString = arg.toString;
  272. if (toString) {
  273. arg = toString.call(arg);
  274. }
  275. }
  276. args[i] = arg;
  277. }
  278. consoleLog.apply(this, args);
  279. };
  280. }
  281. /* eslint-enable prefer-rest-params */
  282. });
  283. }
  284. }
  285. global.document = document;
  286. }
  287. // location
  288. if (typeof global.location === 'undefined') {
  289. global.location = {
  290. href: '',
  291. // Required by:
  292. // - lib-jitsi-meet/modules/xmpp/xmpp.js
  293. search: ''
  294. };
  295. }
  296. const { navigator } = global;
  297. if (navigator) {
  298. // userAgent
  299. //
  300. // Required by:
  301. // - lib-jitsi-meet/modules/browser/BrowserDetection.js
  302. let userAgent = navigator.userAgent || '';
  303. // react-native/version
  304. const { name, version } = require('react-native/package.json');
  305. let rn = name || 'react-native';
  306. version && (rn += `/${version}`);
  307. if (userAgent.indexOf(rn) === -1) {
  308. userAgent = userAgent ? `${rn} ${userAgent}` : rn;
  309. }
  310. // (OS version)
  311. const os = `(${Platform.OS} ${Platform.Version})`;
  312. if (userAgent.indexOf(os) === -1) {
  313. userAgent = userAgent ? `${userAgent} ${os}` : os;
  314. }
  315. navigator.userAgent = userAgent;
  316. }
  317. // WebRTC
  318. require('./polyfills-webrtc');
  319. require('react-native-callstats/csio-polyfill');
  320. // XMLHttpRequest
  321. if (global.XMLHttpRequest) {
  322. const { prototype } = global.XMLHttpRequest;
  323. // XMLHttpRequest.responseXML
  324. //
  325. // Required by:
  326. // - Strophe
  327. if (prototype && !prototype.hasOwnProperty('responseXML')) {
  328. Object.defineProperty(prototype, 'responseXML', {
  329. get() {
  330. const { responseText } = this;
  331. return (
  332. responseText
  333. && new DOMParser().parseFromString(
  334. responseText,
  335. 'text/xml'));
  336. }
  337. });
  338. }
  339. }
  340. // Timers
  341. //
  342. // React Native's timers won't run while the app is in the background, this
  343. // is a known limitation. Replace them with a background-friendly
  344. // alternative.
  345. //
  346. // Required by:
  347. // - lib-jitsi-meet
  348. // - Strophe
  349. global.clearTimeout = BackgroundTimer.clearTimeout.bind(BackgroundTimer);
  350. global.clearInterval = BackgroundTimer.clearInterval.bind(BackgroundTimer);
  351. global.setInterval = BackgroundTimer.setInterval.bind(BackgroundTimer);
  352. global.setTimeout = (fn, ms = 0) => BackgroundTimer.setTimeout(fn, ms);
  353. })(global || window || this); // eslint-disable-line no-invalid-this