hooks.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. var {
  2. _optionalChain
  3. } = require('@sentry/utils');
  4. Object.defineProperty(exports, '__esModule', { value: true });
  5. const core = require('@sentry/core');
  6. const async_hooks = require('async_hooks');
  7. let asyncStorage;
  8. /**
  9. * Sets the async context strategy to use AsyncLocalStorage which requires Node v12.17.0 or v13.10.0.
  10. */
  11. function setHooksAsyncContextStrategy() {
  12. if (!asyncStorage) {
  13. asyncStorage = new (async_hooks ).AsyncLocalStorage();
  14. }
  15. function getCurrentHub() {
  16. return asyncStorage.getStore();
  17. }
  18. function createNewHub(parent) {
  19. const carrier = {};
  20. core.ensureHubOnCarrier(carrier, parent);
  21. return core.getHubFromCarrier(carrier);
  22. }
  23. function runWithAsyncContext(callback, options) {
  24. const existingHub = getCurrentHub();
  25. if (existingHub && _optionalChain([options, 'optionalAccess', _ => _.reuseExisting])) {
  26. // We're already in an async context, so we don't need to create a new one
  27. // just call the callback with the current hub
  28. return callback();
  29. }
  30. const newHub = createNewHub(existingHub);
  31. return asyncStorage.run(newHub, () => {
  32. return callback();
  33. });
  34. }
  35. core.setAsyncContextStrategy({ getCurrentHub, runWithAsyncContext });
  36. }
  37. exports.setHooksAsyncContextStrategy = setHooksAsyncContextStrategy;
  38. //# sourceMappingURL=hooks.js.map