@@ -55,7 +55,7 @@ const DEFAULT_BREADCRUMBS = 100;
5555 */
5656export interface AsyncContextStrategy {
5757 getCurrentHub : ( ) => Hub | undefined ;
58- runWithAsyncContext < T , A > ( callback : ( hub : Hub , ... args : A [ ] ) => T , ...args : A [ ] ) : T ;
58+ runWithAsyncContext < T > ( callback : ( hub : Hub ) => T , ...args : unknown [ ] ) : T ;
5959}
6060
6161/**
@@ -536,25 +536,44 @@ export function getCurrentHub(): Hub {
536536 }
537537 }
538538
539+ // Prefer domains over global if they are there (applicable only to Node environment)
540+ if ( isNodeEnv ( ) ) {
541+ return getHubFromActiveDomain ( registry ) ;
542+ }
543+
544+ // Return hub that lives on a global object
545+ return getGlobalHub ( registry ) ;
546+ }
547+
548+ function getGlobalHub ( registry : Carrier = getMainCarrier ( ) ) : Hub {
539549 // If there's no hub, or its an old API, assign a new one
540550 if ( ! hasHubOnCarrier ( registry ) || getHubFromCarrier ( registry ) . isOlderThan ( API_VERSION ) ) {
541551 setHubOnCarrier ( registry , new Hub ( ) ) ;
542552 }
543553
544- // Prefer domains over global if they are there (applicable only to Node environment)
545- if ( isNodeEnv ( ) ) {
546- return getHubFromActiveDomain ( registry ) ;
547- }
548554 // Return hub that lives on a global object
549555 return getHubFromCarrier ( registry ) ;
550556}
551557
558+ /**
559+ * @private Private API with no semver guarantees!
560+ *
561+ * If the carrier does not contain a hub, a new hub is created with the global hub client and scope.
562+ */
563+ export function ensureHubOnCarrier ( carrier : Carrier ) : void {
564+ // If there's no hub on current domain, or it's an old API, assign a new one
565+ if ( ! hasHubOnCarrier ( carrier ) || getHubFromCarrier ( carrier ) . isOlderThan ( API_VERSION ) ) {
566+ const globalHubTopStack = getGlobalHub ( ) . getStackTop ( ) ;
567+ setHubOnCarrier ( carrier , new Hub ( globalHubTopStack . client , Scope . clone ( globalHubTopStack . scope ) ) ) ;
568+ }
569+ }
570+
552571/**
553572 * @private Private API with no semver guarantees!
554573 *
555574 * Sets the global async context strategy
556575 */
557- export function setAsyncContextStrategy ( strategy : AsyncContextStrategy ) : void {
576+ export function setAsyncContextStrategy ( strategy : AsyncContextStrategy | undefined ) : void {
558577 // Get main carrier (global for every environment)
559578 const registry = getMainCarrier ( ) ;
560579 registry . __SENTRY__ = registry . __SENTRY__ || { } ;
@@ -566,15 +585,15 @@ export function setAsyncContextStrategy(strategy: AsyncContextStrategy): void {
566585 *
567586 * Runs the given callback function with the global async context strategy
568587 */
569- export function runWithAsyncContext < T , A > ( callback : ( hub : Hub , ... args : A [ ] ) => T , ...args : A [ ] ) : T {
588+ export function runWithAsyncContext < T > ( callback : ( hub : Hub ) => T , ...args : unknown [ ] ) : T {
570589 const registry = getMainCarrier ( ) ;
571590
572591 if ( registry . __SENTRY__ && registry . __SENTRY__ . acs ) {
573592 return registry . __SENTRY__ . acs . runWithAsyncContext ( callback , ...args ) ;
574593 }
575594
576595 // if there was no strategy, fallback to just calling the callback
577- return callback ( getCurrentHub ( ) , ... args ) ;
596+ return callback ( getCurrentHub ( ) ) ;
578597}
579598
580599/**
0 commit comments