Skip to content

Commit

Permalink
fix: Domain require (#2527)
Browse files Browse the repository at this point in the history
* fix: Domain require

* meta: Comment

* ref: Remove domain declaration

* fix: Add any
  • Loading branch information
HazAT authored Mar 27, 2020
1 parent 5550201 commit a047ef2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 8 additions & 12 deletions packages/hub/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion packages/node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
6 changes: 6 additions & 0 deletions packages/node/test/domain.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down

0 comments on commit a047ef2

Please sign in to comment.