@@ -4,79 +4,71 @@ import { readFileSync } from 'fs';
44import { URL } from 'url' ;
55
66import { BaseTransport } from '@sentry/transport-base' ;
7- import { Transport , TransportOptions , TransportRequest , TransportRequestMaker , TransportResponse } from '@sentry/types' ;
7+ import { Transport , TransportRequest , TransportMakeRequestResponse } from '@sentry/types' ;
88
99// TODO: `x-sentry-error` header?
1010export class HTTPTransport extends BaseTransport implements Transport {
1111 private _certs ?: Buffer ;
1212
13- constructor ( private readonly _options : TransportOptions ) {
14- super ( _options ) ;
15- }
16-
17- public sendRequest < T > ( request : TransportRequest < T > ) : PromiseLike < TransportResponse > {
18- const requestMaker : TransportRequestMaker < T > = request => {
19- return new Promise ( ( resolve , reject ) => {
20- const { hostname, pathname, port, protocol } = new URL ( this . _dsn . getEnvelopeEndpoint ( ) ) ;
21- const httpModule = protocol === 'https:' ? https : http ;
22- const proxy = this . _options . proxy || process . env . http_proxy ;
23- const agent = proxy
24- ? new ( require ( 'https-proxy-agent' ) ) ( proxy )
25- : new httpModule . Agent ( { keepAlive : false , maxSockets : 30 , timeout : 2000 } ) ;
13+ protected _makeRequest < T > ( request : TransportRequest < T > ) : PromiseLike < TransportMakeRequestResponse > {
14+ return new Promise ( ( resolve , reject ) => {
15+ const { hostname, pathname, port, protocol } = new URL ( this . _dsn . getEnvelopeEndpoint ( ) ) ;
16+ const httpModule = protocol === 'https:' ? https : http ;
17+ const proxy = this . _options . proxy || process . env . http_proxy ;
18+ const agent = proxy
19+ ? new ( require ( 'https-proxy-agent' ) ) ( proxy )
20+ : new httpModule . Agent ( { keepAlive : false , maxSockets : 30 , timeout : 2000 } ) ;
2621
27- const requestOptions : https . RequestOptions = {
28- agent,
29- hostname,
30- method : 'POST' ,
31- path : pathname ,
32- port,
33- protocol,
34- headers : {
35- ...this . _options . headers ,
36- ...this . _dsn . getEnvelopeEndpointAuthHeaders ( ) ,
37- } ,
38- } ;
22+ const requestOptions : https . RequestOptions = {
23+ agent,
24+ hostname,
25+ method : 'POST' ,
26+ path : pathname ,
27+ port,
28+ protocol,
29+ headers : {
30+ ...this . _options . headers ,
31+ ...this . _dsn . getEnvelopeEndpointAuthHeaders ( ) ,
32+ } ,
33+ } ;
3934
40- if ( this . _options . caCerts ) {
41- if ( ! this . _certs ) {
42- try {
43- this . _certs = readFileSync ( this . _options . caCerts ) ;
44- } catch ( _oO ) {
45- // no-empty
46- }
35+ if ( this . _options . caCerts ) {
36+ if ( ! this . _certs ) {
37+ try {
38+ this . _certs = readFileSync ( this . _options . caCerts ) ;
39+ } catch ( _oO ) {
40+ // no-empty
4741 }
42+ }
4843
49- if ( this . _certs ) {
50- requestOptions . ca = this . _certs ;
51- }
44+ if ( this . _certs ) {
45+ requestOptions . ca = this . _certs ;
5246 }
47+ }
5348
54- // TODO: Add gzip compresison
55- const req = httpModule . request ( requestOptions , ( res : http . IncomingMessage ) => {
56- res . setEncoding ( 'utf8' ) ;
49+ // TODO: Add gzip compresison
50+ const req = httpModule . request ( requestOptions , ( res : http . IncomingMessage ) => {
51+ res . setEncoding ( 'utf8' ) ;
5752
58- let body = '' ;
59- res . on ( 'data' , chunk => {
60- body += chunk ;
61- } ) ;
62- res . on ( 'end' , ( ) => {
63- resolve ( {
64- body,
65- headers : {
66- 'x-sentry-rate-limits' : res . headers [ 'X-Sentry-Rate-Limits' ] as string ,
67- 'retry-after' : res . headers [ 'Retry-After' ] as string ,
68- } ,
69- reason : res . statusMessage ,
70- statusCode : res . statusCode || 500 ,
71- } ) ;
53+ let body = '' ;
54+ res . on ( 'data' , chunk => {
55+ body += chunk ;
56+ } ) ;
57+ res . on ( 'end' , ( ) => {
58+ resolve ( {
59+ body,
60+ headers : {
61+ 'x-sentry-rate-limits' : res . headers [ 'X-Sentry-Rate-Limits' ] as string ,
62+ 'retry-after' : res . headers [ 'Retry-After' ] as string ,
63+ } ,
64+ reason : res . statusMessage ,
65+ statusCode : res . statusCode || 500 ,
7266 } ) ;
7367 } ) ;
74-
75- req . on ( 'error' , error => reject ( error ) ) ;
76- req . end ( request . body ) ;
7768 } ) ;
78- } ;
7969
80- return super . sendRequest ( request , requestMaker ) ;
70+ req . on ( 'error' , error => reject ( error ) ) ;
71+ req . end ( request . body ) ;
72+ } ) ;
8173 }
8274}
0 commit comments