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

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