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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. import Iterator from 'es6-iterator';
  2. import BackgroundTimer from 'react-native-background-timer';
  3. import 'url-polyfill'; // 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. // Array.prototype[@@iterator]
  98. //
  99. // Required by:
  100. // - for...of statement use(s) in lib-jitsi-meet
  101. const arrayPrototype = Array.prototype;
  102. if (typeof arrayPrototype['@@iterator'] === 'undefined') {
  103. arrayPrototype['@@iterator'] = function() {
  104. return new Iterator(this);
  105. };
  106. }
  107. // document
  108. //
  109. // Required by:
  110. // - jQuery
  111. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  112. // - Strophe
  113. if (typeof global.document === 'undefined') {
  114. const document
  115. = new DOMParser().parseFromString(
  116. '<html><head></head><body></body></html>',
  117. 'text/xml');
  118. // document.addEventListener
  119. //
  120. // Required by:
  121. // - jQuery
  122. if (typeof document.addEventListener === 'undefined') {
  123. // eslint-disable-next-line no-empty-function
  124. document.addEventListener = () => {};
  125. }
  126. // document.cookie
  127. //
  128. // Required by:
  129. // - herment
  130. if (typeof document.cookie === 'undefined') {
  131. document.cookie = '';
  132. }
  133. // document.implementation
  134. //
  135. // Required by:
  136. // - jQuery
  137. if (typeof document.implementation === 'undefined') {
  138. document.implementation = {};
  139. }
  140. // document.implementation.createHTMLDocument
  141. //
  142. // Required by:
  143. // - jQuery
  144. if (typeof document.implementation.createHTMLDocument === 'undefined') {
  145. document.implementation.createHTMLDocument = function(title = '') {
  146. const htmlDocument
  147. = new DOMParser().parseFromString(
  148. `<html>
  149. <head><title>${title}</title></head>
  150. <body></body>
  151. </html>`,
  152. 'text/xml');
  153. Object.defineProperty(htmlDocument, 'body', {
  154. get() {
  155. return htmlDocument.getElementsByTagName('body')[0];
  156. }
  157. });
  158. return htmlDocument;
  159. };
  160. }
  161. // Element.querySelector
  162. //
  163. // Required by:
  164. // - lib-jitsi-meet/modules/xmpp
  165. const elementPrototype
  166. = Object.getPrototypeOf(document.documentElement);
  167. if (elementPrototype) {
  168. if (typeof elementPrototype.querySelector === 'undefined') {
  169. elementPrototype.querySelector = function(selectors) {
  170. return _querySelector(this, selectors);
  171. };
  172. }
  173. // Element.remove
  174. //
  175. // Required by:
  176. // - lib-jitsi-meet ChatRoom#onPresence parsing
  177. if (typeof elementPrototype.remove === 'undefined') {
  178. elementPrototype.remove = function() {
  179. if (this.parentNode !== null) {
  180. this.parentNode.removeChild(this);
  181. }
  182. };
  183. }
  184. // Element.innerHTML
  185. //
  186. // Required by:
  187. // - jQuery's .append method
  188. if (!elementPrototype.hasOwnProperty('innerHTML')) {
  189. Object.defineProperty(elementPrototype, 'innerHTML', {
  190. get() {
  191. return this.childNodes.toString();
  192. },
  193. set(innerHTML) {
  194. // MDN says: removes all of element's children, parses
  195. // the content string and assigns the resulting nodes as
  196. // children of the element.
  197. // Remove all of element's children.
  198. this.textContent = '';
  199. // Parse the content string.
  200. const d
  201. = new DOMParser().parseFromString(
  202. `<div>${innerHTML}</div>`,
  203. 'text/xml');
  204. // Assign the resulting nodes as children of the
  205. // element.
  206. const documentElement = d.documentElement;
  207. let child;
  208. // eslint-disable-next-line no-cond-assign
  209. while (child = documentElement.firstChild) {
  210. this.appendChild(child);
  211. }
  212. }
  213. });
  214. }
  215. // Element.children
  216. //
  217. // Required by:
  218. // - lib-jitsi-meet ChatRoom#onPresence parsing
  219. if (!elementPrototype.hasOwnProperty('children')) {
  220. Object.defineProperty(elementPrototype, 'children', {
  221. get() {
  222. const nodes = this.childNodes;
  223. const children = [];
  224. let i = 0;
  225. let node = nodes[i];
  226. while (node) {
  227. if (node.nodeType === 1) {
  228. children.push(node);
  229. }
  230. i += 1;
  231. node = nodes[i];
  232. }
  233. return children;
  234. }
  235. });
  236. }
  237. }
  238. // FIXME There is a weird infinite loop related to console.log and
  239. // Document and/or Element at the time of this writing. Work around it
  240. // by patching Node and/or overriding console.log.
  241. const documentPrototype = Object.getPrototypeOf(document);
  242. const nodePrototype
  243. = _getCommonPrototype(documentPrototype, elementPrototype);
  244. if (nodePrototype
  245. // XXX The intention was to find Node from which Document and
  246. // Element extend. If for whatever reason we've reached Object,
  247. // then it doesn't sound like what expected.
  248. && nodePrototype !== Object.getPrototypeOf({})) {
  249. // Override console.log.
  250. const { console } = global;
  251. if (console) {
  252. const loggerLevels = require('jitsi-meet-logger').levels;
  253. Object.keys(loggerLevels).forEach(key => {
  254. const level = loggerLevels[key];
  255. const consoleLog = console[level];
  256. /* eslint-disable prefer-rest-params */
  257. if (typeof consoleLog === 'function') {
  258. console[level] = function(...args) {
  259. // XXX If console's disableYellowBox is truthy, then
  260. // react-native will not automatically display the
  261. // yellow box for the warn level. However, it will
  262. // still display the red box for the error level.
  263. // But I disable the yellow box when I don't want to
  264. // have react-native automatically show me the
  265. // console's output just like in the Release build
  266. // configuration. Because I didn't find a way to
  267. // disable the red box, downgrade the error level to
  268. // warn. The red box will still be displayed but not
  269. // for the error level.
  270. if (console.disableYellowBox && level === 'error') {
  271. console.warn(...args);
  272. return;
  273. }
  274. const { length } = args;
  275. for (let i = 0; i < length; ++i) {
  276. let arg = args[i];
  277. if (arg
  278. && typeof arg !== 'string'
  279. // Limit the console.log override to
  280. // Node (instances).
  281. && nodePrototype.isPrototypeOf(arg)) {
  282. const toString = arg.toString;
  283. if (toString) {
  284. arg = toString.call(arg);
  285. }
  286. }
  287. args[i] = arg;
  288. }
  289. consoleLog.apply(this, args);
  290. };
  291. }
  292. /* eslint-enable prefer-rest-params */
  293. });
  294. }
  295. }
  296. global.document = document;
  297. }
  298. // location
  299. if (typeof global.location === 'undefined') {
  300. global.location = {
  301. href: '',
  302. // Required by:
  303. // - lib-jitsi-meet/modules/xmpp/xmpp.js
  304. search: ''
  305. };
  306. }
  307. const { navigator } = global;
  308. if (navigator) {
  309. // platform
  310. //
  311. // Required by:
  312. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  313. if (typeof navigator.platform === 'undefined') {
  314. navigator.platform = '';
  315. }
  316. // plugins
  317. //
  318. // Required by:
  319. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  320. if (typeof navigator.plugins === 'undefined') {
  321. navigator.plugins = [];
  322. }
  323. // userAgent
  324. //
  325. // Required by:
  326. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  327. // - lib-jitsi-meet/modules/browser/BrowserDetection.js
  328. let userAgent = navigator.userAgent || '';
  329. // react-native/version
  330. const { name, version } = require('react-native/package.json');
  331. let rn = name || 'react-native';
  332. version && (rn += `/${version}`);
  333. if (userAgent.indexOf(rn) === -1) {
  334. userAgent = userAgent ? `${rn} ${userAgent}` : rn;
  335. }
  336. // (OS version)
  337. const os = `(${Platform.OS} ${Platform.Version})`;
  338. if (userAgent.indexOf(os) === -1) {
  339. userAgent = userAgent ? `${userAgent} ${os}` : os;
  340. }
  341. navigator.userAgent = userAgent;
  342. }
  343. // WebRTC
  344. require('./polyfills-webrtc');
  345. require('react-native-callstats/csio-polyfill');
  346. // XMLHttpRequest
  347. if (global.XMLHttpRequest) {
  348. const { prototype } = global.XMLHttpRequest;
  349. // XMLHttpRequest.responseXML
  350. //
  351. // Required by:
  352. // - Strophe
  353. if (prototype && !prototype.hasOwnProperty('responseXML')) {
  354. Object.defineProperty(prototype, 'responseXML', {
  355. get() {
  356. const { responseText } = this;
  357. return (
  358. responseText
  359. && new DOMParser().parseFromString(
  360. responseText,
  361. 'text/xml'));
  362. }
  363. });
  364. }
  365. }
  366. // Timers
  367. //
  368. // React Native's timers won't run while the app is in the background, this
  369. // is a known limitation. Replace them with a background-friendly
  370. // alternative.
  371. //
  372. // Required by:
  373. // - lib-jitsi-meet
  374. // - Strophe
  375. global.clearTimeout = BackgroundTimer.clearTimeout.bind(BackgroundTimer);
  376. global.clearInterval = BackgroundTimer.clearInterval.bind(BackgroundTimer);
  377. global.setInterval = BackgroundTimer.setInterval.bind(BackgroundTimer);
  378. global.setTimeout = (fn, ms = 0) => BackgroundTimer.setTimeout(fn, ms);
  379. })(global || window || this); // eslint-disable-line no-invalid-this