您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

strophe.util.js 4.7KB

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