Skip to content

Commit fdc3ea9

Browse files
committed
fix: Domain require
1 parent 5550201 commit fdc3ea9

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

packages/hub/src/hub.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,14 @@ export function getCurrentHub(): Hub {
454454
*/
455455
function getHubFromActiveDomain(registry: Carrier): Hub {
456456
try {
457-
const req = require;
458-
const domain = req('domain');
457+
const property = 'domain';
458+
const carrier = getMainCarrier();
459+
const sentry = carrier.__SENTRY__;
460+
// tslint:disable-next-line: strict-type-predicates
461+
if (!sentry || !sentry.extensions || !sentry.extensions[property]) {
462+
return getHubFromCarrier(registry);
463+
}
464+
const domain = sentry.extensions[property] as any;
459465
const activeDomain = domain.active;
460466

461467
// If there no active domain, just return global hub

packages/node/src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export { defaultIntegrations, init, lastEventId, flush, close } from './sdk';
3939
export { SDK_NAME, SDK_VERSION } from './version';
4040

4141
import { Integrations as CoreIntegrations } from '@sentry/core';
42+
import { getMainCarrier } from '@sentry/hub';
43+
import * as domain from 'domain';
4244

4345
import * as Handlers from './handlers';
4446
import * as NodeIntegrations from './integrations';
@@ -50,3 +52,14 @@ const INTEGRATIONS = {
5052
};
5153

5254
export { INTEGRATIONS as Integrations, Transports, Handlers };
55+
56+
// We need to patch domain on the global __SENTRY__ object to make it work for node
57+
// if we don't do this, browser bundlers will have troubles resolving require('domain')
58+
const carrier = getMainCarrier();
59+
if (carrier.__SENTRY__) {
60+
carrier.__SENTRY__.extensions = carrier.__SENTRY__.extensions || {};
61+
if (!carrier.__SENTRY__.extensions.domain) {
62+
// @ts-ignore
63+
carrier.__SENTRY__.extensions.domain = domain;
64+
}
65+
}

packages/node/test/domain.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { getCurrentHub, Hub } from '@sentry/core';
22
import * as domain from 'domain';
33

4+
import * as Sentry from '../src';
5+
6+
console.log(Sentry.SDK_NAME);
7+
48
describe('domains', () => {
59
test('without domain', () => {
610
expect(domain.active).toBeFalsy();

0 commit comments

Comments
 (0)