domain.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. var {
  2. _optionalChain
  3. } = require('@sentry/utils');
  4. Object.defineProperty(exports, '__esModule', { value: true });
  5. const domain = require('domain');
  6. const core = require('@sentry/core');
  7. function getActiveDomain() {
  8. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
  9. return (domain ).active ;
  10. }
  11. function getCurrentHub() {
  12. const activeDomain = getActiveDomain();
  13. // If there's no active domain, just return undefined and the global hub will be used
  14. if (!activeDomain) {
  15. return undefined;
  16. }
  17. core.ensureHubOnCarrier(activeDomain);
  18. return core.getHubFromCarrier(activeDomain);
  19. }
  20. function createNewHub(parent) {
  21. const carrier = {};
  22. core.ensureHubOnCarrier(carrier, parent);
  23. return core.getHubFromCarrier(carrier);
  24. }
  25. function runWithAsyncContext(callback, options) {
  26. const activeDomain = getActiveDomain();
  27. if (activeDomain && _optionalChain([options, 'optionalAccess', _ => _.reuseExisting])) {
  28. // We're already in a domain, so we don't need to create a new one, just call the callback with the current hub
  29. return callback();
  30. }
  31. const local = domain.create() ;
  32. const parentHub = activeDomain ? core.getHubFromCarrier(activeDomain) : undefined;
  33. const newHub = createNewHub(parentHub);
  34. core.setHubOnCarrier(local, newHub);
  35. return local.bind(() => {
  36. return callback();
  37. })();
  38. }
  39. /**
  40. * Sets the async context strategy to use Node.js domains.
  41. */
  42. function setDomainAsyncContextStrategy() {
  43. core.setAsyncContextStrategy({ getCurrentHub, runWithAsyncContext });
  44. }
  45. exports.setDomainAsyncContextStrategy = setDomainAsyncContextStrategy;
  46. //# sourceMappingURL=domain.js.map