Skip to content

Commit 25a25c7

Browse files
committed
factor out getActiveDomain as new function
1 parent 4a1a3b5 commit 25a25c7

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

packages/hub/src/hub.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -458,28 +458,31 @@ export function getCurrentHub(): Hub {
458458
}
459459

460460
/**
461-
* Try to read the hub from an active domain, fallback to the registry if one doesnt exist
461+
* Returns the active domain, if one exists
462+
*
463+
* @returns The domain, or undefined if there is no active domain
464+
*/
465+
export function getActiveDomain(): any | undefined {
466+
const sentry = getMainCarrier().__SENTRY__;
467+
468+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
469+
return sentry && sentry.extensions && sentry.extensions.domain && (sentry.extensions.domain as any).active;
470+
}
471+
472+
/**
473+
* Try to read the hub from an active domain, and fallback to the registry if one doesn't exist
462474
* @returns discovered hub
463475
*/
464476
function getHubFromActiveDomain(registry: Carrier): Hub {
465477
try {
466-
const property = 'domain';
467-
const carrier = getMainCarrier();
468-
const sentry = carrier.__SENTRY__;
469-
if (!sentry || !sentry.extensions || !sentry.extensions[property]) {
470-
return getHubFromCarrier(registry);
471-
}
472-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
473-
const domain = sentry.extensions[property] as any;
474-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
475-
const activeDomain = domain.active;
478+
const activeDomain = getActiveDomain();
476479

477480
// If there's no active domain, just return global hub
478481
if (!activeDomain) {
479482
return getHubFromCarrier(registry);
480483
}
481484

482-
// If there's no hub on current domain, or its an old API, assign a new one
485+
// If there's no hub on current domain, or it's an old API, assign a new one
483486
if (!hasHubOnCarrier(activeDomain) || getHubFromCarrier(activeDomain).isOlderThan(API_VERSION)) {
484487
const registryHubTopStack = getHubFromCarrier(registry).getStackTop();
485488
setHubOnCarrier(activeDomain, new Hub(registryHubTopStack.client, Scope.clone(registryHubTopStack.scope)));

packages/hub/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
export { Carrier, Layer } from './interfaces';
22
export { addGlobalEventProcessor, Scope } from './scope';
3-
export { getCurrentHub, getHubFromCarrier, getMainCarrier, Hub, makeMain, setHubOnCarrier } from './hub';
3+
export {
4+
getActiveDomain,
5+
getCurrentHub,
6+
getHubFromCarrier,
7+
getMainCarrier,
8+
Hub,
9+
makeMain,
10+
setHubOnCarrier,
11+
} from './hub';

0 commit comments

Comments
 (0)