diff --git a/CHANGELOG.md b/CHANGELOG.md index 975550c2bcc9..3d3707fa4f36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ - "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott +## 5.15.4 + +- [node] fix: Path domain onto global extension method to not use require (#2527) + ## 5.15.3 - [hub] fix: Restore dynamicRequire, but for `perf_hooks` only (#2524) diff --git a/packages/hub/src/hub.ts b/packages/hub/src/hub.ts index 63b7cd4326e7..ddfd4dc0e9cc 100644 --- a/packages/hub/src/hub.ts +++ b/packages/hub/src/hub.ts @@ -17,16 +17,6 @@ import { consoleSandbox, getGlobalObject, isNodeEnv, logger, timestampWithMs, uu import { Carrier, Layer } from './interfaces'; import { Scope } from './scope'; -declare module 'domain' { - export let active: Domain; - /** - * Extension for domain interface - */ - export interface Domain { - __SENTRY__?: Carrier; - } -} - /** * API compatibility version of this hub. * @@ -454,8 +444,14 @@ export function getCurrentHub(): Hub { */ function getHubFromActiveDomain(registry: Carrier): Hub { try { - const req = require; - const domain = req('domain'); + const property = 'domain'; + const carrier = getMainCarrier(); + const sentry = carrier.__SENTRY__; + // tslint:disable-next-line: strict-type-predicates + if (!sentry || !sentry.extensions || !sentry.extensions[property]) { + return getHubFromCarrier(registry); + } + const domain = sentry.extensions[property] as any; const activeDomain = domain.active; // If there no active domain, just return global hub diff --git a/packages/node/src/index.ts b/packages/node/src/index.ts index d3621d525304..7ab6e1fc3c11 100644 --- a/packages/node/src/index.ts +++ b/packages/node/src/index.ts @@ -39,6 +39,8 @@ export { defaultIntegrations, init, lastEventId, flush, close } from './sdk'; export { SDK_NAME, SDK_VERSION } from './version'; import { Integrations as CoreIntegrations } from '@sentry/core'; +import { getMainCarrier } from '@sentry/hub'; +import * as domain from 'domain'; import * as Handlers from './handlers'; import * as NodeIntegrations from './integrations'; @@ -50,3 +52,14 @@ const INTEGRATIONS = { }; export { INTEGRATIONS as Integrations, Transports, Handlers }; + +// We need to patch domain on the global __SENTRY__ object to make it work for node +// if we don't do this, browser bundlers will have troubles resolving require('domain') +const carrier = getMainCarrier(); +if (carrier.__SENTRY__) { + carrier.__SENTRY__.extensions = carrier.__SENTRY__.extensions || {}; + if (!carrier.__SENTRY__.extensions.domain) { + // @ts-ignore + carrier.__SENTRY__.extensions.domain = domain; + } +} diff --git a/packages/node/src/sdk.ts b/packages/node/src/sdk.ts index 3e883347ef0e..d4421a4553fb 100644 --- a/packages/node/src/sdk.ts +++ b/packages/node/src/sdk.ts @@ -101,7 +101,7 @@ export function init(options: NodeOptions = {}): void { options.environment = process.env.SENTRY_ENVIRONMENT; } - if (domain.active) { + if ((domain as any).active) { setHubOnCarrier(getMainCarrier(), getCurrentHub()); } diff --git a/packages/node/test/domain.test.ts b/packages/node/test/domain.test.ts index 327c8ff80cc2..9bdbf30c751a 100644 --- a/packages/node/test/domain.test.ts +++ b/packages/node/test/domain.test.ts @@ -1,6 +1,12 @@ import { getCurrentHub, Hub } from '@sentry/core'; import * as domain from 'domain'; +// We need this import here to patch domain on the global object +import * as Sentry from '../src'; + +// tslint:disable-next-line: no-console +console.log(Sentry.SDK_NAME); + describe('domains', () => { test('without domain', () => { expect(domain.active).toBeFalsy();