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

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