1 | Object.defineProperty(exports, "__esModule", { value: true });
|
2 | var tslib_1 = require("tslib");
|
3 | var utils_1 = require("@sentry/utils");
|
4 | var flags_1 = require("./flags");
|
5 | var scope_1 = require("./scope");
|
6 | var session_1 = require("./session");
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | exports.API_VERSION = 4;
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | var DEFAULT_BREADCRUMBS = 100;
|
21 |
|
22 |
|
23 |
|
24 | var Hub = (function () {
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function Hub(client, scope, _version) {
|
34 | if (scope === void 0) { scope = new scope_1.Scope(); }
|
35 | if (_version === void 0) { _version = exports.API_VERSION; }
|
36 | this._version = _version;
|
37 |
|
38 | this._stack = [{}];
|
39 | this.getStackTop().scope = scope;
|
40 | if (client) {
|
41 | this.bindClient(client);
|
42 | }
|
43 | }
|
44 | |
45 |
|
46 |
|
47 | Hub.prototype.isOlderThan = function (version) {
|
48 | return this._version < version;
|
49 | };
|
50 | |
51 |
|
52 |
|
53 | Hub.prototype.bindClient = function (client) {
|
54 | var top = this.getStackTop();
|
55 | top.client = client;
|
56 | if (client && client.setupIntegrations) {
|
57 | client.setupIntegrations();
|
58 | }
|
59 | };
|
60 | |
61 |
|
62 |
|
63 | Hub.prototype.pushScope = function () {
|
64 |
|
65 | var scope = scope_1.Scope.clone(this.getScope());
|
66 | this.getStack().push({
|
67 | client: this.getClient(),
|
68 | scope: scope,
|
69 | });
|
70 | return scope;
|
71 | };
|
72 | |
73 |
|
74 |
|
75 | Hub.prototype.popScope = function () {
|
76 | if (this.getStack().length <= 1)
|
77 | return false;
|
78 | return !!this.getStack().pop();
|
79 | };
|
80 | |
81 |
|
82 |
|
83 | Hub.prototype.withScope = function (callback) {
|
84 | var scope = this.pushScope();
|
85 | try {
|
86 | callback(scope);
|
87 | }
|
88 | finally {
|
89 | this.popScope();
|
90 | }
|
91 | };
|
92 | |
93 |
|
94 |
|
95 | Hub.prototype.getClient = function () {
|
96 | return this.getStackTop().client;
|
97 | };
|
98 |
|
99 | Hub.prototype.getScope = function () {
|
100 | return this.getStackTop().scope;
|
101 | };
|
102 |
|
103 | Hub.prototype.getStack = function () {
|
104 | return this._stack;
|
105 | };
|
106 |
|
107 | Hub.prototype.getStackTop = function () {
|
108 | return this._stack[this._stack.length - 1];
|
109 | };
|
110 | |
111 |
|
112 |
|
113 |
|
114 | Hub.prototype.captureException = function (exception, hint) {
|
115 | var eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : utils_1.uuid4());
|
116 | var finalHint = hint;
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | if (!hint) {
|
122 | var syntheticException = void 0;
|
123 | try {
|
124 | throw new Error('Sentry syntheticException');
|
125 | }
|
126 | catch (exception) {
|
127 | syntheticException = exception;
|
128 | }
|
129 | finalHint = {
|
130 | originalException: exception,
|
131 | syntheticException: syntheticException,
|
132 | };
|
133 | }
|
134 | this._invokeClient('captureException', exception, tslib_1.__assign(tslib_1.__assign({}, finalHint), { event_id: eventId }));
|
135 | return eventId;
|
136 | };
|
137 | |
138 |
|
139 |
|
140 | Hub.prototype.captureMessage = function (message, level, hint) {
|
141 | var eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : utils_1.uuid4());
|
142 | var finalHint = hint;
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | if (!hint) {
|
148 | var syntheticException = void 0;
|
149 | try {
|
150 | throw new Error(message);
|
151 | }
|
152 | catch (exception) {
|
153 | syntheticException = exception;
|
154 | }
|
155 | finalHint = {
|
156 | originalException: message,
|
157 | syntheticException: syntheticException,
|
158 | };
|
159 | }
|
160 | this._invokeClient('captureMessage', message, level, tslib_1.__assign(tslib_1.__assign({}, finalHint), { event_id: eventId }));
|
161 | return eventId;
|
162 | };
|
163 | |
164 |
|
165 |
|
166 | Hub.prototype.captureEvent = function (event, hint) {
|
167 | var eventId = hint && hint.event_id ? hint.event_id : utils_1.uuid4();
|
168 | if (event.type !== 'transaction') {
|
169 | this._lastEventId = eventId;
|
170 | }
|
171 | this._invokeClient('captureEvent', event, tslib_1.__assign(tslib_1.__assign({}, hint), { event_id: eventId }));
|
172 | return eventId;
|
173 | };
|
174 | |
175 |
|
176 |
|
177 | Hub.prototype.lastEventId = function () {
|
178 | return this._lastEventId;
|
179 | };
|
180 | |
181 |
|
182 |
|
183 | Hub.prototype.addBreadcrumb = function (breadcrumb, hint) {
|
184 | var _a = this.getStackTop(), scope = _a.scope, client = _a.client;
|
185 | if (!scope || !client)
|
186 | return;
|
187 |
|
188 | var _b = (client.getOptions && client.getOptions()) || {}, _c = _b.beforeBreadcrumb, beforeBreadcrumb = _c === void 0 ? null : _c, _d = _b.maxBreadcrumbs, maxBreadcrumbs = _d === void 0 ? DEFAULT_BREADCRUMBS : _d;
|
189 | if (maxBreadcrumbs <= 0)
|
190 | return;
|
191 | var timestamp = utils_1.dateTimestampInSeconds();
|
192 | var mergedBreadcrumb = tslib_1.__assign({ timestamp: timestamp }, breadcrumb);
|
193 | var finalBreadcrumb = beforeBreadcrumb
|
194 | ? utils_1.consoleSandbox(function () { return beforeBreadcrumb(mergedBreadcrumb, hint); })
|
195 | : mergedBreadcrumb;
|
196 | if (finalBreadcrumb === null)
|
197 | return;
|
198 | scope.addBreadcrumb(finalBreadcrumb, maxBreadcrumbs);
|
199 | };
|
200 | |
201 |
|
202 |
|
203 | Hub.prototype.setUser = function (user) {
|
204 | var scope = this.getScope();
|
205 | if (scope)
|
206 | scope.setUser(user);
|
207 | };
|
208 | |
209 |
|
210 |
|
211 | Hub.prototype.setTags = function (tags) {
|
212 | var scope = this.getScope();
|
213 | if (scope)
|
214 | scope.setTags(tags);
|
215 | };
|
216 | |
217 |
|
218 |
|
219 | Hub.prototype.setExtras = function (extras) {
|
220 | var scope = this.getScope();
|
221 | if (scope)
|
222 | scope.setExtras(extras);
|
223 | };
|
224 | |
225 |
|
226 |
|
227 | Hub.prototype.setTag = function (key, value) {
|
228 | var scope = this.getScope();
|
229 | if (scope)
|
230 | scope.setTag(key, value);
|
231 | };
|
232 | |
233 |
|
234 |
|
235 | Hub.prototype.setExtra = function (key, extra) {
|
236 | var scope = this.getScope();
|
237 | if (scope)
|
238 | scope.setExtra(key, extra);
|
239 | };
|
240 | |
241 |
|
242 |
|
243 |
|
244 | Hub.prototype.setContext = function (name, context) {
|
245 | var scope = this.getScope();
|
246 | if (scope)
|
247 | scope.setContext(name, context);
|
248 | };
|
249 | |
250 |
|
251 |
|
252 | Hub.prototype.configureScope = function (callback) {
|
253 | var _a = this.getStackTop(), scope = _a.scope, client = _a.client;
|
254 | if (scope && client) {
|
255 | callback(scope);
|
256 | }
|
257 | };
|
258 | |
259 |
|
260 |
|
261 | Hub.prototype.run = function (callback) {
|
262 | var oldHub = makeMain(this);
|
263 | try {
|
264 | callback(this);
|
265 | }
|
266 | finally {
|
267 | makeMain(oldHub);
|
268 | }
|
269 | };
|
270 | |
271 |
|
272 |
|
273 | Hub.prototype.getIntegration = function (integration) {
|
274 | var client = this.getClient();
|
275 | if (!client)
|
276 | return null;
|
277 | try {
|
278 | return client.getIntegration(integration);
|
279 | }
|
280 | catch (_oO) {
|
281 | flags_1.IS_DEBUG_BUILD && utils_1.logger.warn("Cannot retrieve integration " + integration.id + " from the current Hub");
|
282 | return null;
|
283 | }
|
284 | };
|
285 | |
286 |
|
287 |
|
288 | Hub.prototype.startSpan = function (context) {
|
289 | return this._callExtensionMethod('startSpan', context);
|
290 | };
|
291 | |
292 |
|
293 |
|
294 | Hub.prototype.startTransaction = function (context, customSamplingContext) {
|
295 | return this._callExtensionMethod('startTransaction', context, customSamplingContext);
|
296 | };
|
297 | |
298 |
|
299 |
|
300 | Hub.prototype.traceHeaders = function () {
|
301 | return this._callExtensionMethod('traceHeaders');
|
302 | };
|
303 | |
304 |
|
305 |
|
306 | Hub.prototype.captureSession = function (endSession) {
|
307 | if (endSession === void 0) { endSession = false; }
|
308 |
|
309 | if (endSession) {
|
310 | return this.endSession();
|
311 | }
|
312 |
|
313 | this._sendSessionUpdate();
|
314 | };
|
315 | |
316 |
|
317 |
|
318 | Hub.prototype.endSession = function () {
|
319 | var layer = this.getStackTop();
|
320 | var scope = layer && layer.scope;
|
321 | var session = scope && scope.getSession();
|
322 | if (session) {
|
323 | session.close();
|
324 | }
|
325 | this._sendSessionUpdate();
|
326 |
|
327 | if (scope) {
|
328 | scope.setSession();
|
329 | }
|
330 | };
|
331 | |
332 |
|
333 |
|
334 | Hub.prototype.startSession = function (context) {
|
335 | var _a = this.getStackTop(), scope = _a.scope, client = _a.client;
|
336 | var _b = (client && client.getOptions()) || {}, release = _b.release, environment = _b.environment;
|
337 |
|
338 | var global = utils_1.getGlobalObject();
|
339 | var userAgent = (global.navigator || {}).userAgent;
|
340 | var session = new session_1.Session(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({ release: release,
|
341 | environment: environment }, (scope && { user: scope.getUser() })), (userAgent && { userAgent: userAgent })), context));
|
342 | if (scope) {
|
343 |
|
344 | var currentSession = scope.getSession && scope.getSession();
|
345 | if (currentSession && currentSession.status === 'ok') {
|
346 | currentSession.update({ status: 'exited' });
|
347 | }
|
348 | this.endSession();
|
349 |
|
350 | scope.setSession(session);
|
351 | }
|
352 | return session;
|
353 | };
|
354 | |
355 |
|
356 |
|
357 | Hub.prototype._sendSessionUpdate = function () {
|
358 | var _a = this.getStackTop(), scope = _a.scope, client = _a.client;
|
359 | if (!scope)
|
360 | return;
|
361 | var session = scope.getSession && scope.getSession();
|
362 | if (session) {
|
363 | if (client && client.captureSession) {
|
364 | client.captureSession(session);
|
365 | }
|
366 | }
|
367 | };
|
368 | |
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | Hub.prototype._invokeClient = function (method) {
|
376 | var _a;
|
377 | var args = [];
|
378 | for (var _i = 1; _i < arguments.length; _i++) {
|
379 | args[_i - 1] = arguments[_i];
|
380 | }
|
381 | var _b = this.getStackTop(), scope = _b.scope, client = _b.client;
|
382 | if (client && client[method]) {
|
383 |
|
384 | (_a = client)[method].apply(_a, tslib_1.__spread(args, [scope]));
|
385 | }
|
386 | };
|
387 | |
388 |
|
389 |
|
390 |
|
391 |
|
392 | Hub.prototype._callExtensionMethod = function (method) {
|
393 | var args = [];
|
394 | for (var _i = 1; _i < arguments.length; _i++) {
|
395 | args[_i - 1] = arguments[_i];
|
396 | }
|
397 | var carrier = getMainCarrier();
|
398 | var sentry = carrier.__SENTRY__;
|
399 | if (sentry && sentry.extensions && typeof sentry.extensions[method] === 'function') {
|
400 | return sentry.extensions[method].apply(this, args);
|
401 | }
|
402 | flags_1.IS_DEBUG_BUILD && utils_1.logger.warn("Extension method " + method + " couldn't be found, doing nothing.");
|
403 | };
|
404 | return Hub;
|
405 | }());
|
406 | exports.Hub = Hub;
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 | function getMainCarrier() {
|
415 | var carrier = utils_1.getGlobalObject();
|
416 | carrier.__SENTRY__ = carrier.__SENTRY__ || {
|
417 | extensions: {},
|
418 | hub: undefined,
|
419 | };
|
420 | return carrier;
|
421 | }
|
422 | exports.getMainCarrier = getMainCarrier;
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 | function makeMain(hub) {
|
429 | var registry = getMainCarrier();
|
430 | var oldHub = getHubFromCarrier(registry);
|
431 | setHubOnCarrier(registry, hub);
|
432 | return oldHub;
|
433 | }
|
434 | exports.makeMain = makeMain;
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 | function getCurrentHub() {
|
443 |
|
444 | var registry = getMainCarrier();
|
445 |
|
446 | if (!hasHubOnCarrier(registry) || getHubFromCarrier(registry).isOlderThan(exports.API_VERSION)) {
|
447 | setHubOnCarrier(registry, new Hub());
|
448 | }
|
449 |
|
450 | if (utils_1.isNodeEnv()) {
|
451 | return getHubFromActiveDomain(registry);
|
452 | }
|
453 |
|
454 | return getHubFromCarrier(registry);
|
455 | }
|
456 | exports.getCurrentHub = getCurrentHub;
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 | function getActiveDomain() {
|
464 | flags_1.IS_DEBUG_BUILD && utils_1.logger.warn('Function `getActiveDomain` is deprecated and will be removed in a future version.');
|
465 | var sentry = getMainCarrier().__SENTRY__;
|
466 | return sentry && sentry.extensions && sentry.extensions.domain && sentry.extensions.domain.active;
|
467 | }
|
468 | exports.getActiveDomain = getActiveDomain;
|
469 |
|
470 |
|
471 |
|
472 |
|
473 | function getHubFromActiveDomain(registry) {
|
474 | try {
|
475 | var sentry = getMainCarrier().__SENTRY__;
|
476 | var activeDomain = sentry && sentry.extensions && sentry.extensions.domain && sentry.extensions.domain.active;
|
477 |
|
478 | if (!activeDomain) {
|
479 | return getHubFromCarrier(registry);
|
480 | }
|
481 |
|
482 | if (!hasHubOnCarrier(activeDomain) || getHubFromCarrier(activeDomain).isOlderThan(exports.API_VERSION)) {
|
483 | var registryHubTopStack = getHubFromCarrier(registry).getStackTop();
|
484 | setHubOnCarrier(activeDomain, new Hub(registryHubTopStack.client, scope_1.Scope.clone(registryHubTopStack.scope)));
|
485 | }
|
486 |
|
487 | return getHubFromCarrier(activeDomain);
|
488 | }
|
489 | catch (_Oo) {
|
490 |
|
491 | return getHubFromCarrier(registry);
|
492 | }
|
493 | }
|
494 |
|
495 |
|
496 |
|
497 |
|
498 | function hasHubOnCarrier(carrier) {
|
499 | return !!(carrier && carrier.__SENTRY__ && carrier.__SENTRY__.hub);
|
500 | }
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 | function getHubFromCarrier(carrier) {
|
508 | return utils_1.getGlobalSingleton('hub', function () { return new Hub(); }, carrier);
|
509 | }
|
510 | exports.getHubFromCarrier = getHubFromCarrier;
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 | function setHubOnCarrier(carrier, hub) {
|
518 | if (!carrier)
|
519 | return false;
|
520 | var __SENTRY__ = (carrier.__SENTRY__ = carrier.__SENTRY__ || {});
|
521 | __SENTRY__.hub = hub;
|
522 | return true;
|
523 | }
|
524 | exports.setHubOnCarrier = setHubOnCarrier;
|
525 |
|
\ | No newline at end of file |