client.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const core = require('@sentry/core');
  3. const utils = require('@sentry/utils');
  4. const debugBuild = require('./debug-build.js');
  5. const eventbuilder = require('./eventbuilder.js');
  6. const helpers = require('./helpers.js');
  7. const userfeedback = require('./userfeedback.js');
  8. /**
  9. * Configuration options for the Sentry Browser SDK.
  10. * @see @sentry/types Options for more information.
  11. */
  12. /**
  13. * The Sentry Browser SDK Client.
  14. *
  15. * @see BrowserOptions for documentation on configuration options.
  16. * @see SentryClient for usage documentation.
  17. */
  18. class BrowserClient extends core.BaseClient {
  19. /**
  20. * Creates a new Browser SDK instance.
  21. *
  22. * @param options Configuration options for this SDK.
  23. */
  24. constructor(options) {
  25. const sdkSource = helpers.WINDOW.SENTRY_SDK_SOURCE || utils.getSDKSource();
  26. core.applySdkMetadata(options, 'browser', ['browser'], sdkSource);
  27. super(options);
  28. if (options.sendClientReports && helpers.WINDOW.document) {
  29. helpers.WINDOW.document.addEventListener('visibilitychange', () => {
  30. if (helpers.WINDOW.document.visibilityState === 'hidden') {
  31. this._flushOutcomes();
  32. }
  33. });
  34. }
  35. }
  36. /**
  37. * @inheritDoc
  38. */
  39. eventFromException(exception, hint) {
  40. return eventbuilder.eventFromException(this._options.stackParser, exception, hint, this._options.attachStacktrace);
  41. }
  42. /**
  43. * @inheritDoc
  44. */
  45. eventFromMessage(
  46. message,
  47. // eslint-disable-next-line deprecation/deprecation
  48. level = 'info',
  49. hint,
  50. ) {
  51. return eventbuilder.eventFromMessage(this._options.stackParser, message, level, hint, this._options.attachStacktrace);
  52. }
  53. /**
  54. * Sends user feedback to Sentry.
  55. */
  56. captureUserFeedback(feedback) {
  57. if (!this._isEnabled()) {
  58. debugBuild.DEBUG_BUILD && utils.logger.warn('SDK not enabled, will not capture user feedback.');
  59. return;
  60. }
  61. const envelope = userfeedback.createUserFeedbackEnvelope(feedback, {
  62. metadata: this.getSdkMetadata(),
  63. dsn: this.getDsn(),
  64. tunnel: this.getOptions().tunnel,
  65. });
  66. // _sendEnvelope should not throw
  67. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  68. this._sendEnvelope(envelope);
  69. }
  70. /**
  71. * @inheritDoc
  72. */
  73. _prepareEvent(event, hint, scope) {
  74. event.platform = event.platform || 'javascript';
  75. return super._prepareEvent(event, hint, scope);
  76. }
  77. /**
  78. * Sends client reports as an envelope.
  79. */
  80. _flushOutcomes() {
  81. const outcomes = this._clearOutcomes();
  82. if (outcomes.length === 0) {
  83. debugBuild.DEBUG_BUILD && utils.logger.log('No outcomes to send');
  84. return;
  85. }
  86. // This is really the only place where we want to check for a DSN and only send outcomes then
  87. if (!this._dsn) {
  88. debugBuild.DEBUG_BUILD && utils.logger.log('No dsn provided, will not send outcomes');
  89. return;
  90. }
  91. debugBuild.DEBUG_BUILD && utils.logger.log('Sending outcomes:', outcomes);
  92. const envelope = utils.createClientReportEnvelope(outcomes, this._options.tunnel && utils.dsnToString(this._dsn));
  93. // _sendEnvelope should not throw
  94. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  95. this._sendEnvelope(envelope);
  96. }
  97. }
  98. exports.BrowserClient = BrowserClient;
  99. //# sourceMappingURL=client.js.map