UNPKG

4.11 kBJavaScriptView Raw
1Object.defineProperty(exports, "__esModule", { value: true });
2var tslib_1 = require("tslib");
3var error_1 = require("./error");
4/** Regular expression used to parse a Dsn. */
5var DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(?::(\w+))?@)([\w.-]+)(?::(\d+))?\/(.+)/;
6/** Error message */
7var ERROR_MESSAGE = 'Invalid Dsn';
8/** The Sentry Dsn, identifying a Sentry instance and project. */
9var Dsn = /** @class */ (function () {
10 /** Creates a new Dsn component */
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 * Renders the string representation of this Dsn.
22 *
23 * By default, this will render the public representation without the password
24 * component. To get the deprecated private representation, set `withPassword`
25 * to true.
26 *
27 * @param withPassword When set to true, the password will be included.
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 /** Parses a string into this Dsn. */
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 /** Maps Dsn components into this instance. */
58 Dsn.prototype._fromComponents = function (components) {
59 // TODO this is for backwards compatibility, and can be removed in a future version
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 /** Validates this Dsn and throws on error. */
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}());
92exports.Dsn = Dsn;
93//# sourceMappingURL=dsn.js.map
\No newline at end of file