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 15KB

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