sdk.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import { inboundFiltersIntegration, functionToStringIntegration, getIntegrationsToSetup, initAndBind, getReportDialogEndpoint, getCurrentHub, startSession, captureSession, getClient } from '@sentry/core';
  2. import { stackParserFromStackParserOptions, supportsFetch, logger, addHistoryInstrumentationHandler } from '@sentry/utils';
  3. import { BrowserClient } from './client.js';
  4. import { DEBUG_BUILD } from './debug-build.js';
  5. import { WINDOW, wrap as wrap$1 } from './helpers.js';
  6. import { breadcrumbsIntegration } from './integrations/breadcrumbs.js';
  7. import { dedupeIntegration } from './integrations/dedupe.js';
  8. import { globalHandlersIntegration } from './integrations/globalhandlers.js';
  9. import { httpContextIntegration } from './integrations/httpcontext.js';
  10. import { linkedErrorsIntegration } from './integrations/linkederrors.js';
  11. import { browserApiErrorsIntegration } from './integrations/trycatch.js';
  12. import { defaultStackParser } from './stack-parsers.js';
  13. import { makeFetchTransport } from './transports/fetch.js';
  14. import { makeXHRTransport } from './transports/xhr.js';
  15. /** @deprecated Use `getDefaultIntegrations(options)` instead. */
  16. const defaultIntegrations = [
  17. inboundFiltersIntegration(),
  18. functionToStringIntegration(),
  19. browserApiErrorsIntegration(),
  20. breadcrumbsIntegration(),
  21. globalHandlersIntegration(),
  22. linkedErrorsIntegration(),
  23. dedupeIntegration(),
  24. httpContextIntegration(),
  25. ];
  26. /** Get the default integrations for the browser SDK. */
  27. function getDefaultIntegrations(_options) {
  28. // We return a copy of the defaultIntegrations here to avoid mutating this
  29. return [
  30. // eslint-disable-next-line deprecation/deprecation
  31. ...defaultIntegrations,
  32. ];
  33. }
  34. /**
  35. * A magic string that build tooling can leverage in order to inject a release value into the SDK.
  36. */
  37. /**
  38. * The Sentry Browser SDK Client.
  39. *
  40. * To use this SDK, call the {@link init} function as early as possible when
  41. * loading the web page. To set context information or send manual events, use
  42. * the provided methods.
  43. *
  44. * @example
  45. *
  46. * ```
  47. *
  48. * import { init } from '@sentry/browser';
  49. *
  50. * init({
  51. * dsn: '__DSN__',
  52. * // ...
  53. * });
  54. * ```
  55. *
  56. * @example
  57. * ```
  58. *
  59. * import { configureScope } from '@sentry/browser';
  60. * configureScope((scope: Scope) => {
  61. * scope.setExtra({ battery: 0.7 });
  62. * scope.setTag({ user_mode: 'admin' });
  63. * scope.setUser({ id: '4711' });
  64. * });
  65. * ```
  66. *
  67. * @example
  68. * ```
  69. *
  70. * import { addBreadcrumb } from '@sentry/browser';
  71. * addBreadcrumb({
  72. * message: 'My Breadcrumb',
  73. * // ...
  74. * });
  75. * ```
  76. *
  77. * @example
  78. *
  79. * ```
  80. *
  81. * import * as Sentry from '@sentry/browser';
  82. * Sentry.captureMessage('Hello, world!');
  83. * Sentry.captureException(new Error('Good bye'));
  84. * Sentry.captureEvent({
  85. * message: 'Manual',
  86. * stacktrace: [
  87. * // ...
  88. * ],
  89. * });
  90. * ```
  91. *
  92. * @see {@link BrowserOptions} for documentation on configuration options.
  93. */
  94. function init(options = {}) {
  95. if (options.defaultIntegrations === undefined) {
  96. options.defaultIntegrations = getDefaultIntegrations();
  97. }
  98. if (options.release === undefined) {
  99. // This allows build tooling to find-and-replace __SENTRY_RELEASE__ to inject a release value
  100. if (typeof __SENTRY_RELEASE__ === 'string') {
  101. options.release = __SENTRY_RELEASE__;
  102. }
  103. // This supports the variable that sentry-webpack-plugin injects
  104. if (WINDOW.SENTRY_RELEASE && WINDOW.SENTRY_RELEASE.id) {
  105. options.release = WINDOW.SENTRY_RELEASE.id;
  106. }
  107. }
  108. if (options.autoSessionTracking === undefined) {
  109. options.autoSessionTracking = true;
  110. }
  111. if (options.sendClientReports === undefined) {
  112. options.sendClientReports = true;
  113. }
  114. const clientOptions = {
  115. ...options,
  116. stackParser: stackParserFromStackParserOptions(options.stackParser || defaultStackParser),
  117. integrations: getIntegrationsToSetup(options),
  118. transport: options.transport || (supportsFetch() ? makeFetchTransport : makeXHRTransport),
  119. };
  120. initAndBind(BrowserClient, clientOptions);
  121. if (options.autoSessionTracking) {
  122. startSessionTracking();
  123. }
  124. }
  125. const showReportDialog = (
  126. // eslint-disable-next-line deprecation/deprecation
  127. options = {},
  128. // eslint-disable-next-line deprecation/deprecation
  129. hub = getCurrentHub(),
  130. ) => {
  131. // doesn't work without a document (React Native)
  132. if (!WINDOW.document) {
  133. DEBUG_BUILD && logger.error('Global document not defined in showReportDialog call');
  134. return;
  135. }
  136. // eslint-disable-next-line deprecation/deprecation
  137. const { client, scope } = hub.getStackTop();
  138. const dsn = options.dsn || (client && client.getDsn());
  139. if (!dsn) {
  140. DEBUG_BUILD && logger.error('DSN not configured for showReportDialog call');
  141. return;
  142. }
  143. if (scope) {
  144. options.user = {
  145. ...scope.getUser(),
  146. ...options.user,
  147. };
  148. }
  149. // TODO(v8): Remove this entire if statement. `eventId` will be a required option.
  150. // eslint-disable-next-line deprecation/deprecation
  151. if (!options.eventId) {
  152. // eslint-disable-next-line deprecation/deprecation
  153. options.eventId = hub.lastEventId();
  154. }
  155. const script = WINDOW.document.createElement('script');
  156. script.async = true;
  157. script.crossOrigin = 'anonymous';
  158. script.src = getReportDialogEndpoint(dsn, options);
  159. if (options.onLoad) {
  160. script.onload = options.onLoad;
  161. }
  162. const { onClose } = options;
  163. if (onClose) {
  164. const reportDialogClosedMessageHandler = (event) => {
  165. if (event.data === '__sentry_reportdialog_closed__') {
  166. try {
  167. onClose();
  168. } finally {
  169. WINDOW.removeEventListener('message', reportDialogClosedMessageHandler);
  170. }
  171. }
  172. };
  173. WINDOW.addEventListener('message', reportDialogClosedMessageHandler);
  174. }
  175. const injectionPoint = WINDOW.document.head || WINDOW.document.body;
  176. if (injectionPoint) {
  177. injectionPoint.appendChild(script);
  178. } else {
  179. DEBUG_BUILD && logger.error('Not injecting report dialog. No injection point found in HTML');
  180. }
  181. };
  182. /**
  183. * This function is here to be API compatible with the loader.
  184. * @hidden
  185. */
  186. function forceLoad() {
  187. // Noop
  188. }
  189. /**
  190. * This function is here to be API compatible with the loader.
  191. * @hidden
  192. */
  193. function onLoad(callback) {
  194. callback();
  195. }
  196. /**
  197. * Wrap code within a try/catch block so the SDK is able to capture errors.
  198. *
  199. * @deprecated This function will be removed in v8.
  200. * It is not part of Sentry's official API and it's easily replaceable by using a try/catch block
  201. * and calling Sentry.captureException.
  202. *
  203. * @param fn A function to wrap.
  204. *
  205. * @returns The result of wrapped function call.
  206. */
  207. // TODO(v8): Remove this function
  208. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  209. function wrap(fn) {
  210. return wrap$1(fn)();
  211. }
  212. /**
  213. * Enable automatic Session Tracking for the initial page load.
  214. */
  215. function startSessionTracking() {
  216. if (typeof WINDOW.document === 'undefined') {
  217. DEBUG_BUILD && logger.warn('Session tracking in non-browser environment with @sentry/browser is not supported.');
  218. return;
  219. }
  220. // The session duration for browser sessions does not track a meaningful
  221. // concept that can be used as a metric.
  222. // Automatically captured sessions are akin to page views, and thus we
  223. // discard their duration.
  224. startSession({ ignoreDuration: true });
  225. captureSession();
  226. // We want to create a session for every navigation as well
  227. addHistoryInstrumentationHandler(({ from, to }) => {
  228. // Don't create an additional session for the initial route or if the location did not change
  229. if (from !== undefined && from !== to) {
  230. startSession({ ignoreDuration: true });
  231. captureSession();
  232. }
  233. });
  234. }
  235. /**
  236. * Captures user feedback and sends it to Sentry.
  237. */
  238. function captureUserFeedback(feedback) {
  239. const client = getClient();
  240. if (client) {
  241. client.captureUserFeedback(feedback);
  242. }
  243. }
  244. export { captureUserFeedback, defaultIntegrations, forceLoad, getDefaultIntegrations, init, onLoad, showReportDialog, wrap };
  245. //# sourceMappingURL=sdk.js.map