Skip to content

Commit ad72c76

Browse files
committed
i did it...
1 parent 8740e23 commit ad72c76

File tree

8 files changed

+79
-38
lines changed

8 files changed

+79
-38
lines changed

packages/browser/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"dependencies": {
1818
"@sentry/core": "4.1.1",
1919
"@sentry/types": "4.1.0",
20-
"@sentry/utils": "4.1.1"
20+
"@sentry/utils": "4.1.1",
21+
"rollup-plugin-shim": "^1.0.0"
2122
},
2223
"devDependencies": {
2324
"@types/md5": "2.1.32",

packages/browser/rollup.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import uglify from 'rollup-plugin-uglify';
33
import resolve from 'rollup-plugin-node-resolve';
44
import typescript from 'rollup-plugin-typescript2';
55
import license from 'rollup-plugin-license';
6+
import shim from 'rollup-plugin-shim';
67

78
const commitHash = require('child_process')
89
.execSync('git rev-parse --short HEAD', { encoding: 'utf-8' })
@@ -17,6 +18,9 @@ const bundleConfig = {
1718
},
1819
context: 'window',
1920
plugins: [
21+
shim({
22+
domain: `export default {}`,
23+
}),
2024
typescript({
2125
tsconfig: 'tsconfig.build.json',
2226
tsconfigOverride: { compilerOptions: { declaration: false } },

packages/hub/src/hub.ts

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,20 @@ import {
99
} from '@sentry/types';
1010
import { logger } from '@sentry/utils/logger';
1111
import { getGlobalObject, uuid4 } from '@sentry/utils/misc';
12+
import * as domain from 'domain';
1213
import { Carrier, Layer } from './interfaces';
1314
import { Scope } from './scope';
1415

16+
declare module 'domain' {
17+
export let active: Domain;
18+
/**
19+
* Extension for domain interface
20+
*/
21+
export interface Domain {
22+
__SENTRY__?: Carrier;
23+
}
24+
}
25+
1526
/**
1627
* API compatibility version of this hub.
1728
*
@@ -291,26 +302,21 @@ export function getMainCarrier(): Carrier {
291302
carrier.__SENTRY__ = carrier.__SENTRY__ || {
292303
hub: undefined,
293304
};
294-
return carrier.__SENTRY__;
305+
return carrier;
295306
}
296307

297308
/**
298309
* Replaces the current main hub with the passed one on the global object
299310
*
300311
* @returns The old replaced hub
301312
*/
302-
export function makeMain(hub?: Hub): Hub | undefined {
313+
export function makeMain(hub: Hub): Hub {
303314
const registry = getMainCarrier();
304-
const oldHub = registry.hub;
305-
registry.hub = hub;
315+
const oldHub = getHubFromCarrier(registry);
316+
setHubOnCarrier(registry, hub);
306317
return oldHub;
307318
}
308319

309-
/** Domain interface with attached Hub */
310-
interface SentryDomain extends NodeJS.Domain {
311-
__SENTRY__?: Carrier;
312-
}
313-
314320
/**
315321
* Returns the default hub instance.
316322
*
@@ -319,26 +325,46 @@ interface SentryDomain extends NodeJS.Domain {
319325
* Otherwise, the currently registered hub will be returned.
320326
*/
321327
export function getCurrentHub(): Hub {
328+
// Get main carrier (global for every environment)
322329
const registry = getMainCarrier();
323330

324-
if (!registry.hub || registry.hub.isOlderThan(API_VERSION)) {
325-
registry.hub = new Hub();
331+
// If there's no hub, or its an old API, assign a new one
332+
if (!hasHubOnCarrier(registry) || getHubFromCarrier(registry).isOlderThan(API_VERSION)) {
333+
setHubOnCarrier(registry, new Hub());
326334
}
327335

336+
// Prefer domains over global if they are there
328337
try {
329-
const domain = process.domain as SentryDomain;
338+
const activeDomain = domain.active;
330339

331-
if (!domain.__SENTRY__) {
332-
return registry.hub;
340+
// If there no active domain, just return global hub
341+
if (!activeDomain) {
342+
return getHubFromCarrier(registry);
333343
}
334344

335-
if (!domain.__SENTRY__.hub || domain.__SENTRY__.hub.isOlderThan(API_VERSION)) {
336-
domain.__SENTRY__.hub = new Hub(registry.hub.getStackTop().client, Scope.clone(registry.hub.getStackTop().scope));
345+
// If there's no hub on current domain, or its an old API, assign a new one
346+
if (!hasHubOnCarrier(activeDomain) || getHubFromCarrier(activeDomain).isOlderThan(API_VERSION)) {
347+
const registryHubTopStack = getHubFromCarrier(registry).getStackTop();
348+
setHubOnCarrier(activeDomain, new Hub(registryHubTopStack.client, Scope.clone(registryHubTopStack.scope)));
337349
}
338350

339-
return domain.__SENTRY__.hub;
351+
// Return hub that lives on a domain
352+
return getHubFromCarrier(activeDomain);
340353
} catch (_Oo) {
341-
return registry.hub;
354+
// Return hub that lives on a global object
355+
return getHubFromCarrier(registry);
356+
}
357+
}
358+
359+
/**
360+
* This will tell whether a carrier has a hub on it or not
361+
* @param carrier object
362+
*/
363+
export function hasHubOnCarrier(carrier: any): boolean {
364+
if (carrier && carrier.__SENTRY__ && carrier.__SENTRY__.hub) {
365+
return true;
366+
} else {
367+
return false;
342368
}
343369
}
344370

packages/hub/src/interfaces.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ export interface Layer {
99

1010
/** An object that contains a hub and maintains a scope stack. */
1111
export interface Carrier {
12-
hub?: Hub;
12+
__SENTRY__?: {
13+
hub?: Hub;
14+
};
1315
}

packages/node/src/sdk.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
22
import { getMainCarrier, setHubOnCarrier } from '@sentry/hub';
3+
import * as domain from 'domain';
34
import { NodeOptions } from './backend';
45
import { NodeClient } from './client';
56
import { Console, Http, LinkedErrors, OnUncaughtException, OnUnhandledRejection } from './integrations';
@@ -68,7 +69,7 @@ export function init(options: NodeOptions = {}): void {
6869
options.defaultIntegrations = defaultIntegrations;
6970
}
7071

71-
if (process.domain) {
72+
if (domain.active) {
7273
setHubOnCarrier(getMainCarrier(), getCurrentHub());
7374
}
7475

packages/node/test/domain.test.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,39 @@ describe('domains', () => {
2626
const d = domain.create();
2727
d.run(() => {
2828
expect(getCurrentHub()).toBe(getCurrentHub());
29-
d.exit();
3029
});
3130
});
3231

3332
test('concurrent domain hubs', done => {
3433
const d1 = domain.create();
3534
const d2 = domain.create();
35+
let d1done = false;
36+
let d2done = false;
3637

3738
d1.run(() => {
38-
getCurrentHub()
39-
.getStack()
40-
.push({ client: 'process' });
41-
39+
const hub = getCurrentHub();
40+
hub.getStack().push({ client: 'process' });
41+
expect(hub.getStack()[1]).toEqual({ client: 'process' });
42+
// Just in case so we don't have to worry which one finishes first
43+
// (although it always should be d2)
4244
setTimeout(() => {
43-
expect(getCurrentHub().getStack()[1]).toEqual({ client: 'process' });
44-
d1.exit();
45-
}, 50);
45+
d1done = true;
46+
if (d2done) {
47+
done();
48+
}
49+
});
4650
});
4751

4852
d2.run(() => {
49-
getCurrentHub()
50-
.getStack()
51-
.push({ client: 'local' });
52-
53+
const hub = getCurrentHub();
54+
hub.getStack().push({ client: 'local' });
55+
expect(hub.getStack()[1]).toEqual({ client: 'local' });
5356
setTimeout(() => {
54-
expect(getCurrentHub().getStack()[1]).toEqual({ client: 'local' });
55-
d2.exit();
56-
done();
57-
}, 100);
57+
d2done = true;
58+
if (d1done) {
59+
done();
60+
}
61+
});
5862
});
5963
});
6064
});

packages/node/test/index.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ describe('SentryNode', () => {
217217
expect(event.message).toBe('test domain');
218218
expect(event.exception).toBeUndefined();
219219
done();
220-
d.exit();
221220
return event;
222221
},
223222
dsn,

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7859,6 +7859,10 @@ rollup-plugin-npm@^2.0.0:
78597859
version "2.0.0"
78607860
resolved "https://registry.yarnpkg.com/rollup-plugin-npm/-/rollup-plugin-npm-2.0.0.tgz#8a28ffdb5160bc8e1e371de39ed71faf009d655c"
78617861

7862+
rollup-plugin-shim@^1.0.0:
7863+
version "1.0.0"
7864+
resolved "https://registry.yarnpkg.com/rollup-plugin-shim/-/rollup-plugin-shim-1.0.0.tgz#b00f5cb44cdae81358c5342fe82fa71a1602b56b"
7865+
78627866
rollup-plugin-typescript2@^0.13.0:
78637867
version "0.13.0"
78647868
resolved "https://registry.yarnpkg.com/rollup-plugin-typescript2/-/rollup-plugin-typescript2-0.13.0.tgz#5fc838657d05af82e04554832cadf06cdb32f657"

0 commit comments

Comments
 (0)