1515import * as httpModule from 'http' ;
1616import { Agent , ClientRequest , ClientRequestArgs , request } from 'http' ;
1717import * as httpsModule from 'https' ;
18- import * as semver from 'semver' ;
1918import * as shimmer from 'shimmer' ;
2019import { URL , UrlWithStringQuery } from 'url' ;
2120
@@ -31,12 +30,7 @@ const ERR_HTTP_HEADERS_SENT_MSG = "Can't set headers after they are sent.";
3130// URL is used for type checking, but doesn't exist in Node <7.
3231// This function works around that.
3332// eslint-disable-next-line @typescript-eslint/no-explicit-any
34- const isURL = semver . satisfies ( process . version , '>=7' )
35- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
36- ( value : any ) : value is URL => value instanceof URL
37- : // eslint-disable-next-line
38- ( value : any ) : value is URL => false ;
39- // tslint:enable:no-any
33+ const isURL = ( value : any ) : value is URL => value instanceof URL ;
4034
4135function getSpanName ( options : ClientRequestArgs | URL ) {
4236 // c.f. _http_client.js ClientRequest constructor
@@ -268,28 +262,26 @@ function patchHttp(http: HttpModule, api: Tracer) {
268262 return makeRequestTrace ( 'http:' , request , api ) ;
269263 } ) ;
270264
271- if ( semver . satisfies ( process . version , '>=8.0.0' ) ) {
272- // http.get in Node 8 calls the private copy of request rather than the one
273- // we have patched on module.export, so patch get as well.
274- shimmer . wrap ( http , 'get' , ( ) : typeof http . get => {
275- // Re-implement http.get. This needs to be done (instead of using
276- // makeRequestTrace to patch it) because we need to set the trace
277- // context header before the returned ClientRequest is ended.
278- // The Node.js docs state that the only differences between request and
279- // get are that (1) get defaults to the HTTP GET method and (2) the
280- // returned request object is ended immediately.
281- // The former is already true (at least in supported Node versions up to
282- // v9), so we simply follow the latter.
283- // Ref:
284- // https://nodejs.org/dist/latest/docs/api/http.html#http_http_get_options_callback
285- return function getTrace ( this : never ) {
286- // eslint-disable-next-line prefer-rest-params
287- const req = http . request . apply ( this , arguments ) ;
288- req . end ( ) ;
289- return req ;
290- } ;
291- } ) ;
292- }
265+ // http.get in Node 8 calls the private copy of request rather than the one
266+ // we have patched on module.export, so patch get as well.
267+ shimmer . wrap ( http , 'get' , ( ) : typeof http . get => {
268+ // Re-implement http.get. This needs to be done (instead of using
269+ // makeRequestTrace to patch it) because we need to set the trace
270+ // context header before the returned ClientRequest is ended.
271+ // The Node.js docs state that the only differences between request and
272+ // get are that (1) get defaults to the HTTP GET method and (2) the
273+ // returned request object is ended immediately.
274+ // The former is already true (at least in supported Node versions up to
275+ // v9), so we simply follow the latter.
276+ // Ref:
277+ // https://nodejs.org/dist/latest/docs/api/http.html#http_http_get_options_callback
278+ return function getTrace ( this : never ) {
279+ // eslint-disable-next-line prefer-rest-params
280+ const req = http . request . apply ( this , arguments ) ;
281+ req . end ( ) ;
282+ return req ;
283+ } ;
284+ } ) ;
293285}
294286
295287// https.get depends on Node http internals in 8.9.0 and 9+ instead of the
@@ -310,9 +302,7 @@ function patchHttps(https: HttpsModule, api: Tracer) {
310302
311303function unpatchHttp ( http : HttpModule ) {
312304 shimmer . unwrap ( http , 'request' ) ;
313- if ( semver . satisfies ( process . version , '>=8.0.0' ) ) {
314- shimmer . unwrap ( http , 'get' ) ;
315- }
305+ shimmer . unwrap ( http , 'get' ) ;
316306}
317307
318308function unpatchHttps ( https : HttpsModule ) {
0 commit comments