Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

strophe.util.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* global __filename */
  2. /**
  3. * Strophe logger implementation. Logs from level WARN and above.
  4. */
  5. import { getLogger } from 'jitsi-meet-logger';
  6. import { Strophe } from 'strophe.js';
  7. import GlobalOnErrorHandler from '../util/GlobalOnErrorHandler';
  8. const logger = getLogger(__filename);
  9. /**
  10. * This is the last HTTP error status captured from Strophe debug logs.
  11. * The purpose of storing it is to distinguish between the network and
  12. * infrastructure reason for connection being dropped (see connectionHandler in
  13. * xmpp.js). The value will be cleared (-1) if the subsequent request succeeds
  14. * which means that the failure could be transient.
  15. *
  16. * FIXME in the latest Strophe (not released on npm) there is API to handle
  17. * particular HTTP errors, but there is no way to learn if the subsequent
  18. * request succeeded in order to tell if the error was one time incident or if
  19. * it was the reason for dropping the connection by Strophe (the connection is
  20. * dropped after 5 subsequent failures). Ideally Strophe should provide more
  21. * details about the reason on why the connection stopped.
  22. *
  23. * @type {number}
  24. */
  25. let lastErrorStatus = -1;
  26. /**
  27. * A regular expression used to catch Strophe's log message indicating that the
  28. * last BOSH request was successful. When there is such message seen the
  29. * {@link lastErrorStatus} will be set back to '-1'.
  30. * @type {RegExp}
  31. */
  32. const resetLastErrorStatusRegExpr = /request id \d+.\d+ got 200/;
  33. /**
  34. * A regular expression used to capture the current value of the BOSH request
  35. * error status (HTTP error code or '0' or something else).
  36. * @type {RegExp}
  37. */
  38. const lastErrorStatusRegExpr
  39. = /request errored, status: (\d+), number of errors: \d+/;
  40. /**
  41. *
  42. */
  43. export default function() {
  44. Strophe.log = function(level, msg) {
  45. // Our global handler reports uncaught errors to the stats which may
  46. // interpret those as partial call failure.
  47. // Strophe log entry about secondary request timeout does not mean that
  48. // it's a final failure(the request will be restarted), so we lower it's
  49. // level here to a warning.
  50. logger.trace('Strophe', level, msg);
  51. if (typeof msg === 'string'
  52. && msg.indexOf('Request ') !== -1
  53. && msg.indexOf('timed out (secondary), restarting') !== -1) {
  54. // eslint-disable-next-line no-param-reassign
  55. level = Strophe.LogLevel.WARN;
  56. }
  57. /* eslint-disable no-case-declarations */
  58. switch (level) {
  59. case Strophe.LogLevel.DEBUG:
  60. // The log message which reports successful status is logged on
  61. // Strophe's DEBUG level.
  62. if (lastErrorStatus !== -1
  63. && resetLastErrorStatusRegExpr.test(msg)) {
  64. logger.debug('Reset lastErrorStatus');
  65. lastErrorStatus = -1;
  66. }
  67. break;
  68. case Strophe.LogLevel.WARN:
  69. logger.warn(`Strophe: ${msg}`);
  70. const errStatusCapture = lastErrorStatusRegExpr.exec(msg);
  71. if (errStatusCapture && errStatusCapture.length === 2) {
  72. lastErrorStatus = parseInt(errStatusCapture[1], 10);
  73. logger.debug(`lastErrorStatus set to: ${lastErrorStatus}`);
  74. }
  75. break;
  76. case Strophe.LogLevel.ERROR:
  77. case Strophe.LogLevel.FATAL:
  78. // eslint-disable-next-line no-param-reassign
  79. msg = `Strophe: ${msg}`;
  80. GlobalOnErrorHandler.callErrorHandler(new Error(msg));
  81. logger.error(msg);
  82. break;
  83. }
  84. /* eslint-enable no-case-declarations */
  85. };
  86. /**
  87. * Returns error status (HTTP error code) of the last BOSH request.
  88. *
  89. * @return {number} HTTP error code, '0' for unknown or "god knows what"
  90. * (this is a hack).
  91. */
  92. Strophe.getLastErrorStatus = function() {
  93. return lastErrorStatus;
  94. };
  95. Strophe.getStatusString = function(status) {
  96. switch (status) {
  97. case Strophe.Status.BINDREQUIRED:
  98. return 'BINDREQUIRED';
  99. case Strophe.Status.ERROR:
  100. return 'ERROR';
  101. case Strophe.Status.CONNECTING:
  102. return 'CONNECTING';
  103. case Strophe.Status.CONNFAIL:
  104. return 'CONNFAIL';
  105. case Strophe.Status.AUTHENTICATING:
  106. return 'AUTHENTICATING';
  107. case Strophe.Status.AUTHFAIL:
  108. return 'AUTHFAIL';
  109. case Strophe.Status.CONNECTED:
  110. return 'CONNECTED';
  111. case Strophe.Status.DISCONNECTED:
  112. return 'DISCONNECTED';
  113. case Strophe.Status.DISCONNECTING:
  114. return 'DISCONNECTING';
  115. case Strophe.Status.ATTACHED:
  116. return 'ATTACHED';
  117. default:
  118. return 'unknown';
  119. }
  120. };
  121. }