import { getCurrentHub } from '@sentry/core' ; import { parseSemver } from '@sentry/utils' ; import * as http from 'http' ; import * as https from 'https' ; import { URL } from 'url' ; const NODE_VERSION = parseSemver ( process . versions . node ) ; param url export function isSentryRequest ( url ) { const dsn = getCurrentHub ( ) . getClient ( ) ?. getDsn ( ) ; return dsn ? url . includes ( dsn . host ) : false ; } param requestOptions returns export function extractUrl ( requestOptions ) { const protocol = requestOptions . protocol || '' ; const hostname = requestOptions . hostname || requestOptions . host || '' ; const port = ! requestOptions . port || requestOptions . port === 80 || requestOptions . port === 443 ? '' : `:${ requestOptions . port }` ; const path = requestOptions . path ? requestOptions . path : '/' ; return `${ protocol }//${ hostname }${ port }${ path }` ; } param description param requestOptions param Request returns export function cleanSpanDescription ( description , requestOptions , request ) { if ( ! description ) { return description ; } let [ method , requestUrl ] = description . split ( ' ' ) ; if ( requestOptions . host && ! requestOptions . protocol ) { requestOptions . protocol = ( request as any ) ?. agent ?. protocol ; requestUrl = extractUrl ( requestOptions ) ; } if ( requestUrl ?. startsWith ( '///' ) ) { requestUrl = requestUrl . slice ( 2 ) ; } return `${ method } ${ requestUrl }` ; } export type RequestOptions = http . RequestOptions & { hash ? : string ; search ? : string ; pathname ? : string ; href ? : string ; } ; type RequestCallback = ( response : http . IncomingMessage ) => void ; export type RequestMethodArgs = [ RequestOptions | string | URL , RequestCallback ? ] | [ string | URL , RequestOptions , RequestCallback ? ] ; export type RequestMethod = ( ... args : RequestMethodArgs ) => http . ClientRequest ; /**
 * Convert a URL object into a RequestOptions object.
 *
 * Copied from Node's internals (where it's used in http(s).request() and http(s).get()), modified only to use the
 * RequestOptions type above.
 *
 * See https://github.com/nodejs/node/blob/master/lib/internal/url.js.
 */ export function urlToOptions ( url ) { const options = { protocol : url . protocol , hostname : typeof url . hostname === 'string' && url . hostname . startsWith ( '[' ) ? url . hostname . slice ( 1 , - 1 ) : url . hostname , hash : url . hash , search : url . search , pathname : url . pathname , path : `${ url . pathname || '' }${ url . search || '' }` , href : url . href , } ; if ( url . port !== '' ) { options . port = Number ( url . port ) ; } if ( url . username || url . password ) { options . auth = `${ url . username }:${ url . password }` ; } return options ; } param requestArgs returns export function normalizeRequestArgs ( httpModule , requestArgs ) { let callback , requestOptions ; if ( typeof requestArgs [ requestArgs . length - 1 ] === 'function' ) { callback = requestArgs . pop ( ) as RequestCallback ; } if ( typeof requestArgs [ 0 ] === 'string' ) { requestOptions = urlToOptions ( new URL ( requestArgs [ 0 ] ) ) ; } else if ( requestArgs [ 0 ] instanceof URL ) { requestOptions = urlToOptions ( requestArgs [ 0 ] ) ; } else { requestOptions = requestArgs [ 0 ] ; } if ( requestArgs . length === 2 ) { requestOptions = { ... requestOptions , ... requestArgs [ 1 ] } ; } if ( requestOptions . protocol === undefined ) { if ( NODE_VERSION . major && NODE_VERSION . major > 8 ) { requestOptions . protocol = ( httpModule ?. globalAgent as any ) ?. protocol || ( requestOptions . agent as any ) ?. protocol || ( requestOptions . _defaultAgent as any ) ?. protocol ; } else { requestOptions . protocol = ( requestOptions . agent as any ) ?. protocol || ( requestOptions . _defaultAgent as any ) ?. protocol || ( httpModule ?. globalAgent as any ) ?. protocol ; } } if ( callback ) { return [ requestOptions , callback ] ; } else { return [ requestOptions ] ; } }