Skip to content

Commit a047ef2

Browse files
authored
fix: Domain require (getsentry#2527)
* fix: Domain require * meta: Comment * ref: Remove domain declaration * fix: Add any
1 parent 5550201 commit a047ef2

File tree

5 files changed

+32
-13
lines changed

5 files changed

+32
-13
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
- "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott
66

7+
## 5.15.4
8+
9+
- [node] fix: Path domain onto global extension method to not use require (#2527)
10+
711
## 5.15.3
812

913
- [hub] fix: Restore dynamicRequire, but for `perf_hooks` only (#2524)

packages/hub/src/hub.ts

+8-12
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ import { consoleSandbox, getGlobalObject, isNodeEnv, logger, timestampWithMs, uu
1717
import { Carrier, Layer } from './interfaces';
1818
import { Scope } from './scope';
1919

20-
declare module 'domain' {
21-
export let active: Domain;
22-
/**
23-
* Extension for domain interface
24-
*/
25-
export interface Domain {
26-
__SENTRY__?: Carrier;
27-
}
28-
}
29-
3020
/**
3121
* API compatibility version of this hub.
3222
*
@@ -454,8 +444,14 @@ export function getCurrentHub(): Hub {
454444
*/
455445
function getHubFromActiveDomain(registry: Carrier): Hub {
456446
try {
457-
const req = require;
458-
const domain = req('domain');
447+
const property = 'domain';
448+
const carrier = getMainCarrier();
449+
const sentry = carrier.__SENTRY__;
450+
// tslint:disable-next-line: strict-type-predicates
451+
if (!sentry || !sentry.extensions || !sentry.extensions[property]) {
452+
return getHubFromCarrier(registry);
453+
}
454+
const domain = sentry.extensions[property] as any;
459455
const activeDomain = domain.active;
460456

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

packages/node/src/index.ts

+13
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/src/sdk.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export function init(options: NodeOptions = {}): void {
101101
options.environment = process.env.SENTRY_ENVIRONMENT;
102102
}
103103

104-
if (domain.active) {
104+
if ((domain as any).active) {
105105
setHubOnCarrier(getMainCarrier(), getCurrentHub());
106106
}
107107

packages/node/test/domain.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { getCurrentHub, Hub } from '@sentry/core';
22
import * as domain from 'domain';
33

4+
// We need this import here to patch domain on the global object
5+
import * as Sentry from '../src';
6+
7+
// tslint:disable-next-line: no-console
8+
console.log(Sentry.SDK_NAME);
9+
410
describe('domains', () => {
511
test('without domain', () => {
612
expect(domain.active).toBeFalsy();

0 commit comments

Comments
 (0)