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

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