Skip to content

fix: Domain require #2527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/hub/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,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;
}
}
4 changes: 4 additions & 0 deletions packages/node/test/domain.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { getCurrentHub, Hub } from '@sentry/core';
import * as domain from 'domain';

import * as Sentry from '../src';

console.log(Sentry.SDK_NAME);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug leftover.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed another commit, we need the import and without usage, tslint complains.


describe('domains', () => {
test('without domain', () => {
expect(domain.active).toBeFalsy();
Expand Down