1
1
import { API } from '@sentry/core' ;
2
2
import { Event , Response , Status , Transport , TransportOptions } from '@sentry/types' ;
3
- import { logger , parseRetryAfterHeader , PromiseBuffer , SentryError } from '@sentry/utils' ;
3
+ import { logger , parseRetryAfterHeader , PromiseBuffer , SentryError , isString } from '@sentry/utils' ;
4
4
import * as fs from 'fs' ;
5
5
import * as http from 'http' ;
6
6
import * as https from 'https' ;
@@ -22,6 +22,14 @@ export interface HTTPRequest {
22
22
options : http . RequestOptions | https . RequestOptions | string | url . URL ,
23
23
callback ?: ( res : http . IncomingMessage ) => void ,
24
24
) : http . ClientRequest ;
25
+
26
+ // This is the new type for versions that handle URL argument correctly, but it's most likely not needed here just yet
27
+
28
+ // request(
29
+ // url: string | url.URL,
30
+ // options: http.RequestOptions | https.RequestOptions,
31
+ // callback?: (res: http.IncomingMessage) => void,
32
+ // ): http.ClientRequest;
25
33
}
26
34
27
35
/** Base Transport class implementation */
@@ -47,30 +55,30 @@ export abstract class BaseTransport implements Transport {
47
55
}
48
56
49
57
/** Returns a build request option object used by request */
50
- protected _getRequestOptions ( ) : http . RequestOptions | https . RequestOptions {
58
+ protected _getRequestOptions ( address : string | url . URL ) : http . RequestOptions | https . RequestOptions {
59
+ if ( ! isString ( address ) || ! ( address instanceof url . URL ) ) {
60
+ throw new SentryError ( `Incorrect transport url: ${ address } ` ) ;
61
+ }
62
+
51
63
const headers = {
52
64
...this . _api . getRequestHeaders ( SDK_NAME , SDK_VERSION ) ,
53
65
...this . options . headers ,
54
66
} ;
55
- const dsn = this . _api . getDsn ( ) ;
67
+ const addr = address instanceof url . URL ? address : new url . URL ( address ) ;
68
+ const { hostname, pathname : path , port, protocol } = addr ;
56
69
57
- const options : {
58
- [ key : string ] : any ;
59
- } = {
70
+ return {
60
71
agent : this . client ,
61
- headers,
62
- hostname : dsn . host ,
63
72
method : 'POST' ,
64
- path : this . _api . getStoreEndpointPath ( ) ,
65
- port : dsn . port ,
66
- protocol : `${ dsn . protocol } :` ,
73
+ headers,
74
+ hostname,
75
+ path,
76
+ port,
77
+ protocol,
78
+ ...( this . options . caCerts && {
79
+ ca : fs . readFileSync ( this . options . caCerts ) ,
80
+ } ) ,
67
81
} ;
68
-
69
- if ( this . options . caCerts ) {
70
- options . ca = fs . readFileSync ( this . options . caCerts ) ;
71
- }
72
-
73
- return options ;
74
82
}
75
83
76
84
/** JSDoc */
@@ -84,7 +92,7 @@ export abstract class BaseTransport implements Transport {
84
92
}
85
93
return this . _buffer . add (
86
94
new Promise < Response > ( ( resolve , reject ) => {
87
- const req = httpModule . request ( this . _getRequestOptions ( ) , ( res : http . IncomingMessage ) => {
95
+ const req = httpModule . request ( this . _getRequestOptions ( 'http://foo.com/123' ) , ( res : http . IncomingMessage ) => {
88
96
const statusCode = res . statusCode || 500 ;
89
97
const status = Status . fromHttpCode ( statusCode ) ;
90
98
0 commit comments