sdk.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { logger, consoleSandbox } from '@sentry/utils';
  2. import { DEBUG_BUILD } from './debug-build.js';
  3. import { getCurrentScope } from './exports.js';
  4. import { getCurrentHub } from './hub.js';
  5. /** A class object that can instantiate Client objects. */
  6. /**
  7. * Internal function to create a new SDK client instance. The client is
  8. * installed and then bound to the current scope.
  9. *
  10. * @param clientClass The client class to instantiate.
  11. * @param options Options to pass to the client.
  12. */
  13. function initAndBind(
  14. clientClass,
  15. options,
  16. ) {
  17. if (options.debug === true) {
  18. if (DEBUG_BUILD) {
  19. logger.enable();
  20. } else {
  21. // use `console.warn` rather than `logger.warn` since by non-debug bundles have all `logger.x` statements stripped
  22. consoleSandbox(() => {
  23. // eslint-disable-next-line no-console
  24. console.warn('[Sentry] Cannot initialize SDK with `debug` option using a non-debug bundle.');
  25. });
  26. }
  27. }
  28. const scope = getCurrentScope();
  29. scope.update(options.initialScope);
  30. const client = new clientClass(options);
  31. setCurrentClient(client);
  32. initializeClient(client);
  33. }
  34. /**
  35. * Make the given client the current client.
  36. */
  37. function setCurrentClient(client) {
  38. // eslint-disable-next-line deprecation/deprecation
  39. const hub = getCurrentHub();
  40. // eslint-disable-next-line deprecation/deprecation
  41. const top = hub.getStackTop();
  42. top.client = client;
  43. top.scope.setClient(client);
  44. }
  45. /**
  46. * Initialize the client for the current scope.
  47. * Make sure to call this after `setCurrentClient()`.
  48. */
  49. function initializeClient(client) {
  50. if (client.init) {
  51. client.init();
  52. // TODO v8: Remove this fallback
  53. // eslint-disable-next-line deprecation/deprecation
  54. } else if (client.setupIntegrations) {
  55. // eslint-disable-next-line deprecation/deprecation
  56. client.setupIntegrations();
  57. }
  58. }
  59. export { initAndBind, setCurrentClient };
  60. //# sourceMappingURL=sdk.js.map