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