'use strict' ; import Net from 'net' ; import { InteractsWithHeaders } from './interacts-with-headers' ; export class Request extends InteractsWithHeaders { /**
     * The wrapped request instance.
     */ private readonly request : any ; param { Object } request constructor ( request ) { request = request || { } ; super ( request . headers ) ; this . request = request ; } returns { String | undefined } getClientIp ( ) { return this . fromHeaders ( ) ?? this . fromConnection ( ) ?? this . fromSocket ( ) ?? this . fromInfo ( ) ?? this . fromRaw ( ) ?? this . fromRequestContext ( ) ; } returns { String | undefined } fromHeaders ( ) { if ( this . hasHeaders ( ) ) { if ( this . hasIpInForwardedFor ( ) ) { return this . getFromForwardedFor ( ) ; } if ( this . hasIpInHeader ( 'x-client-ip' ) ) { return this . ipInHeader ( 'x-client-ip' ) ; } if ( this . hasIpInHeader ( 'x-real-ip' ) ) { return this . header ( 'x-real-ip' ) ; } if ( this . hasIpInHeader ( 'cf-connecting-ip' ) ) { return this . header ( 'cf-connecting-ip' ) ; } if ( this . hasIpInHeader ( 'fastly-client-ip' ) ) { return this . header ( 'fastly-client-ip' ) ; } if ( this . hasIpInHeader ( 'true-client-ip' ) ) { return this . header ( 'true-client-ip' ) ; } if ( this . hasIpInHeader ( 'x-cluster-client-ip' ) ) { return this . header ( 'x-cluster-client-ip' ) ; } } } returns { Boolean } hasIpInForwardedFor ( ) { return this . isIp ( this . getFromForwardedFor ( ) ) ; } returns { String | undefined } getFromForwardedFor ( ) { if ( this . hasIpInHeader ( 'x-forwarded-for' ) ) { return this . ipInHeader ( 'x-forwarded-for' ) ; } if ( this . hasIpInHeader ( 'x-forwarded' ) ) { return this . ipInHeader ( 'x-forwarded' ) ; } if ( this . hasIpInHeader ( 'forwarded-for' ) ) { return this . ipInHeader ( 'forwarded-for' ) ; } if ( this . hasIpInHeader ( 'forwarded' ) ) { return this . ipInHeader ( 'forwarded' ) ; } } param { String } name returns { Boolean } hasIpInHeader ( name ) { return ! ! this . ipInHeader ( name ) ; } param { String } name returns { String | undefined } ipInHeader ( name ) { return this . findIp ( this . header ( name ) ?. split ( ',' ) ) ; } param { Array } ips returns { String | undefined } findIp ( ips = [ ] ) { return ips . map ( ip => ip . trim ( ) ) . map ( ip => this . removePortFrom ( ip ) ) . find ( ip => this . isIp ( ip ) ) ; } param { String } ip returns { String } removePortFrom ( ip ) { if ( this . isIpv6 ( ip ) ) { return ip ; } return ip . includes ( ':' ) ? ip . split ( ':' ) [ 0 ] : ip ; } returns { String | undefined } fromConnection ( ) { if ( ! this . hasConnection ( ) ) { return ; } if ( this . isIp ( this . request . connection . remoteAddress ) ) { return this . request . connection . remoteAddress ; } if ( ! this . request . connection . socket ) { return ; } if ( this . isIp ( this . request . connection . socket . remoteAddress ) ) { return this . request . connection . socket . remoteAddress ; } } returns { Boolean } hasConnection ( ) { return ! ! this . request . connection ; } returns { String | undefined } fromSocket ( ) { if ( ! this . hasSocket ( ) ) { return ; } if ( this . isIp ( this . request . socket . remoteAddress ) ) { return this . request . socket . remoteAddress ; } } returns { Boolean } hasSocket ( ) { return ! ! this . request . socket ; } returns { String | undefined } fromInfo ( ) { if ( ! this . hasInfo ( ) ) { return ; } if ( this . isIp ( this . request . info . remoteAddress ) ) { return this . request . info . remoteAddress ; } } returns { Boolean } hasInfo ( ) { return ! ! this . request . info ; } returns { String | undefined } fromRaw ( ) { if ( this . hasRaw ( ) ) { return new Request ( this . request . raw ) . getClientIp ( ) ; } } returns { Boolean } hasRaw ( ) { return ! ! this . raw ( ) ; } returns { Object } raw ( ) { return this . request . raw ; } returns { String | undefined } fromRequestContext ( ) { if ( ! this . hasRequestContext ( ) ) { return ; } if ( ! this . requestContext ( ) . identity ) { return ; } if ( this . isIp ( this . requestContext ( ) . identity . sourceIp ) ) { return this . requestContext ( ) . identity . sourceIp ; } } returns { Boolean } hasRequestContext ( ) { return ! ! this . requestContext ( ) ; } returns { * } requestContext ( ) { return this . request . requestContext ; } param { String } ip returns { Boolean } isIp ( ip ? ) { return this . isIpv4 ( ip ) || this . isIpv6 ( ip ) ; } param { String } ip returns { Boolean } isIpv4 ( ip ? ) { return Net . isIP ( ip ?? '' ) === 4 ; } param { String } ip returns { Boolean } isIpv6 ( ip ? ) { return Net . isIP ( ip ?? '' ) === 6 ; } }