UNPKG

3.92 kBJavaScriptView Raw
1Object.defineProperty(exports, "__esModule", { value: true });
2var utils_1 = require("@sentry/utils");
3/**
4 * @inheritdoc
5 */
6var Session = /** @class */ (function () {
7 function Session(context) {
8 this.errors = 0;
9 this.sid = utils_1.uuid4();
10 this.duration = 0;
11 this.status = 'ok';
12 this.init = true;
13 this.ignoreDuration = false;
14 // Both timestamp and started are in seconds since the UNIX epoch.
15 var startingTime = utils_1.timestampInSeconds();
16 this.timestamp = startingTime;
17 this.started = startingTime;
18 if (context) {
19 this.update(context);
20 }
21 }
22 /** JSDoc */
23 // eslint-disable-next-line complexity
24 Session.prototype.update = function (context) {
25 if (context === void 0) { context = {}; }
26 if (context.user) {
27 if (!this.ipAddress && context.user.ip_address) {
28 this.ipAddress = context.user.ip_address;
29 }
30 if (!this.did && !context.did) {
31 this.did = context.user.id || context.user.email || context.user.username;
32 }
33 }
34 this.timestamp = context.timestamp || utils_1.timestampInSeconds();
35 if (context.ignoreDuration) {
36 this.ignoreDuration = context.ignoreDuration;
37 }
38 if (context.sid) {
39 // Good enough uuid validation. — Kamil
40 this.sid = context.sid.length === 32 ? context.sid : utils_1.uuid4();
41 }
42 if (context.init !== undefined) {
43 this.init = context.init;
44 }
45 if (!this.did && context.did) {
46 this.did = "" + context.did;
47 }
48 if (typeof context.started === 'number') {
49 this.started = context.started;
50 }
51 if (this.ignoreDuration) {
52 this.duration = undefined;
53 }
54 else if (typeof context.duration === 'number') {
55 this.duration = context.duration;
56 }
57 else {
58 var duration = this.timestamp - this.started;
59 this.duration = duration >= 0 ? duration : 0;
60 }
61 if (context.release) {
62 this.release = context.release;
63 }
64 if (context.environment) {
65 this.environment = context.environment;
66 }
67 if (!this.ipAddress && context.ipAddress) {
68 this.ipAddress = context.ipAddress;
69 }
70 if (!this.userAgent && context.userAgent) {
71 this.userAgent = context.userAgent;
72 }
73 if (typeof context.errors === 'number') {
74 this.errors = context.errors;
75 }
76 if (context.status) {
77 this.status = context.status;
78 }
79 };
80 /** JSDoc */
81 Session.prototype.close = function (status) {
82 if (status) {
83 this.update({ status: status });
84 }
85 else if (this.status === 'ok') {
86 this.update({ status: 'exited' });
87 }
88 else {
89 this.update();
90 }
91 };
92 /** JSDoc */
93 Session.prototype.toJSON = function () {
94 return utils_1.dropUndefinedKeys({
95 sid: "" + this.sid,
96 init: this.init,
97 // Make sure that sec is converted to ms for date constructor
98 started: new Date(this.started * 1000).toISOString(),
99 timestamp: new Date(this.timestamp * 1000).toISOString(),
100 status: this.status,
101 errors: this.errors,
102 did: typeof this.did === 'number' || typeof this.did === 'string' ? "" + this.did : undefined,
103 duration: this.duration,
104 attrs: {
105 release: this.release,
106 environment: this.environment,
107 ip_address: this.ipAddress,
108 user_agent: this.userAgent,
109 },
110 });
111 };
112 return Session;
113}());
114exports.Session = Session;
115//# sourceMappingURL=session.js.map
\No newline at end of file