@@ -15,7 +15,7 @@ import { logger, parseRetryAfterHeader, PromiseBuffer, SentryError } from '@sent
15
15
import * as fs from 'fs' ;
16
16
import * as http from 'http' ;
17
17
import * as https from 'https' ;
18
- import * as url from 'url' ;
18
+ import { URL } from 'url' ;
19
19
20
20
import { SDK_NAME } from '../version' ;
21
21
@@ -30,7 +30,7 @@ export interface HTTPModule {
30
30
* @param callback Callback when request is finished
31
31
*/
32
32
request (
33
- options : http . RequestOptions | https . RequestOptions | string | url . URL ,
33
+ options : http . RequestOptions | https . RequestOptions | string | URL ,
34
34
callback ?: ( res : http . IncomingMessage ) => void ,
35
35
) : http . ClientRequest ;
36
36
@@ -39,12 +39,15 @@ export interface HTTPModule {
39
39
// versions:
40
40
41
41
// request(
42
- // url: string | url. URL,
42
+ // url: string | URL,
43
43
// options: http.RequestOptions | https.RequestOptions,
44
44
// callback?: (res: http.IncomingMessage) => void,
45
45
// ): http.ClientRequest;
46
46
}
47
47
48
+ export type URLParts = Pick < URL , 'hostname' | 'pathname' | 'port' | 'protocol' > ;
49
+ export type UrlParser = ( url : string ) => URLParts ;
50
+
48
51
const CATEGORY_MAPPING : {
49
52
[ key in SentryRequestType ] : string ;
50
53
} = {
@@ -76,6 +79,9 @@ export abstract class BaseTransport implements Transport {
76
79
this . _api = new API ( options . dsn , options . _metadata ) ;
77
80
}
78
81
82
+ /** Default function used to parse URLs */
83
+ public urlParser : UrlParser = url => new URL ( url ) ;
84
+
79
85
/**
80
86
* @inheritDoc
81
87
*/
@@ -119,12 +125,12 @@ export abstract class BaseTransport implements Transport {
119
125
}
120
126
121
127
/** Returns a build request option object used by request */
122
- protected _getRequestOptions ( uri : url . URL ) : http . RequestOptions | https . RequestOptions {
128
+ protected _getRequestOptions ( urlParts : URLParts ) : http . RequestOptions | https . RequestOptions {
123
129
const headers = {
124
130
...this . _api . getRequestHeaders ( SDK_NAME , SDK_VERSION ) ,
125
131
...this . options . headers ,
126
132
} ;
127
- const { hostname, pathname, port, protocol } = uri ;
133
+ const { hostname, pathname, port, protocol } = urlParts ;
128
134
// See https://github.com/nodejs/node/blob/38146e717fed2fabe3aacb6540d839475e0ce1c6/lib/internal/url.js#L1268-L1290
129
135
// We ignore the query string on purpose
130
136
const path = `${ pathname } ` ;
@@ -224,7 +230,7 @@ export abstract class BaseTransport implements Transport {
224
230
if ( ! this . module ) {
225
231
throw new SentryError ( 'No module available' ) ;
226
232
}
227
- const options = this . _getRequestOptions ( new url . URL ( sentryReq . url ) ) ;
233
+ const options = this . _getRequestOptions ( this . urlParser ( sentryReq . url ) ) ;
228
234
const req = this . module . request ( options , ( res : http . IncomingMessage ) => {
229
235
const statusCode = res . statusCode || 500 ;
230
236
const status = Status . fromHttpCode ( statusCode ) ;
0 commit comments