1 | Object.defineProperty(exports, "__esModule", { value: true });
|
2 | var tslib_1 = require("tslib");
|
3 | var error_1 = require("./error");
|
4 |
|
5 | var DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(?::(\w+))?@)([\w.-]+)(?::(\d+))?\/(.+)/;
|
6 |
|
7 | var ERROR_MESSAGE = 'Invalid Dsn';
|
8 |
|
9 | var Dsn = (function () {
|
10 |
|
11 | function Dsn(from) {
|
12 | if (typeof from === 'string') {
|
13 | this._fromString(from);
|
14 | }
|
15 | else {
|
16 | this._fromComponents(from);
|
17 | }
|
18 | this._validate();
|
19 | }
|
20 | |
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | Dsn.prototype.toString = function (withPassword) {
|
30 | if (withPassword === void 0) { withPassword = false; }
|
31 | var _a = this, host = _a.host, path = _a.path, pass = _a.pass, port = _a.port, projectId = _a.projectId, protocol = _a.protocol, publicKey = _a.publicKey;
|
32 | return (protocol + "://" + publicKey + (withPassword && pass ? ":" + pass : '') +
|
33 | ("@" + host + (port ? ":" + port : '') + "/" + (path ? path + "/" : path) + projectId));
|
34 | };
|
35 |
|
36 | Dsn.prototype._fromString = function (str) {
|
37 | var match = DSN_REGEX.exec(str);
|
38 | if (!match) {
|
39 | throw new error_1.SentryError(ERROR_MESSAGE);
|
40 | }
|
41 | var _a = tslib_1.__read(match.slice(1), 6), protocol = _a[0], publicKey = _a[1], _b = _a[2], pass = _b === void 0 ? '' : _b, host = _a[3], _c = _a[4], port = _c === void 0 ? '' : _c, lastPath = _a[5];
|
42 | var path = '';
|
43 | var projectId = lastPath;
|
44 | var split = projectId.split('/');
|
45 | if (split.length > 1) {
|
46 | path = split.slice(0, -1).join('/');
|
47 | projectId = split.pop();
|
48 | }
|
49 | if (projectId) {
|
50 | var projectMatch = projectId.match(/^\d+/);
|
51 | if (projectMatch) {
|
52 | projectId = projectMatch[0];
|
53 | }
|
54 | }
|
55 | this._fromComponents({ host: host, pass: pass, path: path, projectId: projectId, port: port, protocol: protocol, publicKey: publicKey });
|
56 | };
|
57 |
|
58 | Dsn.prototype._fromComponents = function (components) {
|
59 |
|
60 | if ('user' in components && !('publicKey' in components)) {
|
61 | components.publicKey = components.user;
|
62 | }
|
63 | this.user = components.publicKey || '';
|
64 | this.protocol = components.protocol;
|
65 | this.publicKey = components.publicKey || '';
|
66 | this.pass = components.pass || '';
|
67 | this.host = components.host;
|
68 | this.port = components.port || '';
|
69 | this.path = components.path || '';
|
70 | this.projectId = components.projectId;
|
71 | };
|
72 |
|
73 | Dsn.prototype._validate = function () {
|
74 | var _this = this;
|
75 | ['protocol', 'publicKey', 'host', 'projectId'].forEach(function (component) {
|
76 | if (!_this[component]) {
|
77 | throw new error_1.SentryError(ERROR_MESSAGE + ": " + component + " missing");
|
78 | }
|
79 | });
|
80 | if (!this.projectId.match(/^\d+$/)) {
|
81 | throw new error_1.SentryError(ERROR_MESSAGE + ": Invalid projectId " + this.projectId);
|
82 | }
|
83 | if (this.protocol !== 'http' && this.protocol !== 'https') {
|
84 | throw new error_1.SentryError(ERROR_MESSAGE + ": Invalid protocol " + this.protocol);
|
85 | }
|
86 | if (this.port && isNaN(parseInt(this.port, 10))) {
|
87 | throw new error_1.SentryError(ERROR_MESSAGE + ": Invalid port " + this.port);
|
88 | }
|
89 | };
|
90 | return Dsn;
|
91 | }());
|
92 | exports.Dsn = Dsn;
|
93 |
|
\ | No newline at end of file |