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 all commits
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
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);
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