@@ -3,12 +3,14 @@ import { DsnComponents, DsnLike, DsnProtocol } from '@sentry/types';
3
3
import { isDebugBuild } from './env' ;
4
4
import { SentryError } from './error' ;
5
5
6
+ export interface Dsn extends DsnComponents {
7
+ /** Protocol used to connect to Sentry. */
8
+ toString ( withPassword : boolean ) : string ;
9
+ }
10
+
6
11
/** Regular expression used to parse a Dsn. */
7
12
const DSN_REGEX = / ^ (?: ( \w + ) : ) \/ \/ (?: ( \w + ) (?: : ( \w + ) ) ? @ ) ( [ \w . - ] + ) (?: : ( \d + ) ) ? \/ ( .+ ) / ;
8
13
9
- /** Error message */
10
- const ERROR_MESSAGE = 'Invalid Dsn' ;
11
-
12
14
function isValidProtocol ( protocol ?: string ) : protocol is DsnProtocol {
13
15
return protocol === 'http' || protocol === 'https' ;
14
16
}
@@ -34,7 +36,7 @@ function dsnFromString(str: string): Dsn {
34
36
const match = DSN_REGEX . exec ( str ) ;
35
37
36
38
if ( ! match ) {
37
- throw new SentryError ( ERROR_MESSAGE ) ;
39
+ throw new SentryError ( 'Invalid Dsn' ) ;
38
40
}
39
41
40
42
const [ protocol , publicKey , pass = '' , host , port = '' , lastPath ] = match . slice ( 1 ) ;
@@ -54,11 +56,7 @@ function dsnFromString(str: string): Dsn {
54
56
}
55
57
}
56
58
57
- if ( isValidProtocol ( protocol ) ) {
58
- return dsnFromComponents ( { host, pass, path, projectId, port, protocol : protocol , publicKey } ) ;
59
- }
60
-
61
- throw new SentryError ( ERROR_MESSAGE ) ;
59
+ return dsnFromComponents ( { host, pass, path, projectId, port, protocol : protocol as DsnProtocol , publicKey } ) ;
62
60
}
63
61
64
62
function dsnFromComponents ( components : DsnComponents ) : Dsn {
@@ -83,55 +81,39 @@ function validateDsn(dsn: Dsn): boolean {
83
81
if ( isDebugBuild ( ) ) {
84
82
const { port, projectId, protocol } = dsn ;
85
83
86
- [ 'protocol' , 'publicKey' , 'host' , 'projectId' ] . forEach ( component => {
84
+ const requiredComponents : ReadonlyArray < keyof DsnComponents > = [ 'protocol' , 'publicKey' , 'host' , 'projectId' ] ;
85
+ requiredComponents . forEach ( component => {
87
86
if ( ! dsn [ component ] ) {
88
- throw new SentryError ( `${ ERROR_MESSAGE } : ${ component } missing` ) ;
87
+ throw new SentryError ( `Invalid Dsn : ${ component } missing` ) ;
89
88
}
90
89
} ) ;
91
90
92
91
if ( ! projectId . match ( / ^ \d + $ / ) ) {
93
- throw new SentryError ( `${ ERROR_MESSAGE } : Invalid projectId ${ projectId } ` ) ;
92
+ throw new SentryError ( `Invalid Dsn : Invalid projectId ${ projectId } ` ) ;
94
93
}
95
94
96
95
if ( isValidProtocol ( protocol ) ) {
97
- throw new SentryError ( `${ ERROR_MESSAGE } : Invalid protocol ${ protocol } ` ) ;
96
+ throw new SentryError ( `Invalid Dsn : Invalid protocol ${ protocol } ` ) ;
98
97
}
99
98
100
99
if ( port && isNaN ( parseInt ( port , 10 ) ) ) {
101
- throw new SentryError ( `${ ERROR_MESSAGE } : Invalid port ${ port } ` ) ;
100
+ throw new SentryError ( `Invalid Dsn : Invalid port ${ port } ` ) ;
102
101
}
103
102
}
104
103
105
104
return true ;
106
105
}
107
106
108
- function makeDsn ( from : DsnLike ) : Dsn {
109
- let dsn = typeof from === 'string' ? dsnFromString ( from ) : dsnFromComponents ( from ) ;
107
+ /** The Sentry Dsn, identifying a Sentry instance and project. */
108
+ export function makeDsn ( from : DsnLike ) : Dsn {
109
+ const components = typeof from === 'string' ? dsnFromString ( from ) : dsnFromComponents ( from ) ;
110
110
111
- return {
112
- ...dsn ,
113
- toString : ( ) => dsntoString ( dsn ) ,
111
+ validateDsn ( components ) ;
112
+
113
+ const dsn : Dsn = {
114
+ ...components ,
115
+ toString : ( withPassword : boolean ) => dsntoString ( dsn ) ,
114
116
} ;
115
- }
116
- /** The Sentry Dsn, identifying a Sentry instance and project. */
117
- export class Dsn implements DsnComponents {
118
- /** Protocol used to connect to Sentry. */
119
- public protocol ! : DsnProtocol ;
120
- /** Public authorization key (deprecated, renamed to publicKey). */
121
- public user ! : string ;
122
- /** Public authorization key. */
123
- public publicKey ! : string ;
124
- /** Private authorization key (deprecated, optional). */
125
- public pass ! : string ;
126
- /** Hostname of the Sentry instance. */
127
- public host ! : string ;
128
- /** Port of the Sentry instance. */
129
- public port ! : string ;
130
- /** Path */
131
- public path ! : string ;
132
- /** Project ID */
133
- public projectId ! : string ;
134
-
135
- /** Creates a new Dsn component */
136
- public constructor ( from : DsnLike ) { }
117
+
118
+ return dsn ;
137
119
}
0 commit comments