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

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