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

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