22
33const Http = require ( 'http' ) ;
44const Https = require ( 'https' ) ;
5+ const Url = require ( 'url' ) ;
56
67const Hoek = require ( '@hapi/hoek' ) ;
78const Validate = require ( '@hapi/validate' ) ;
@@ -64,9 +65,8 @@ internals.schema = Validate.object({
6465exports . plugin = {
6566 pkg : require ( '../package.json' ) ,
6667 requirements : {
67- hapi : '>=19 .0.0'
68+ hapi : '>=20 .0.0'
6869 } ,
69-
7070 register : function ( server , options ) {
7171
7272 internals . defaults = Hoek . applyToDefaults ( internals . defaults , options ) ;
@@ -83,7 +83,7 @@ internals.handler = function (route, handlerOptions) {
8383 const settings = Hoek . applyToDefaults ( internals . defaults , handlerOptions , { shallow : [ 'agent' ] } ) ;
8484 Validate . assert ( handlerOptions , internals . schema , 'Invalid proxy handler options (' + route . path + ')' ) ;
8585 Hoek . assert ( ! route . settings . payload || ( ( route . settings . payload . output === 'data' || route . settings . payload . output === 'stream' ) && ! route . settings . payload . parse ) , 'Cannot proxy if payload is parsed or if output is not stream or data' ) ;
86- settings . mapUri = handlerOptions . mapUri || internals . mapUri ( handlerOptions . protocol , handlerOptions . host , handlerOptions . port , handlerOptions . uri ) ;
86+ settings . mapUri = handlerOptions . mapUri ?? internals . mapUri ( handlerOptions . protocol , handlerOptions . host , handlerOptions . port , handlerOptions . uri ) ;
8787
8888 if ( settings . ttl === 'upstream' ) {
8989 settings . _upstreamTtl = true ;
@@ -158,7 +158,7 @@ internals.handler = function (route, handlerOptions) {
158158
159159 let downstreamStartTime ;
160160 if ( settings . downstreamResponseTime ) {
161- downstreamStartTime = process . hrtime ( ) ;
161+ downstreamStartTime = process . hrtime . bigint ( ) ;
162162 }
163163
164164 const promise = settings . httpClient . request ( request . method , uri , options ) ;
@@ -175,14 +175,14 @@ internals.handler = function (route, handlerOptions) {
175175 try {
176176 var res = await promise ;
177177 if ( settings . downstreamResponseTime ) {
178- const downstreamResponseTime = process . hrtime ( downstreamStartTime ) ;
179- request . log ( [ 'h2o2' , 'success' ] , { downstreamResponseTime : downstreamResponseTime [ 0 ] * internals . NS_PER_SEC + downstreamResponseTime [ 1 ] } ) ;
178+ const downstreamResponseTime = Number ( process . hrtime . bigint ( ) - downstreamStartTime ) ;
179+ request . log ( [ 'h2o2' , 'success' ] , { downstreamResponseTime } ) ;
180180 }
181181 }
182182 catch ( err ) {
183183 if ( settings . downstreamResponseTime ) {
184- const downstreamResponseTime = process . hrtime ( downstreamStartTime ) ;
185- request . log ( [ 'h2o2' , 'error' ] , { downstreamResponseTime : downstreamResponseTime [ 0 ] * internals . NS_PER_SEC + downstreamResponseTime [ 1 ] } ) ;
184+ const downstreamResponseTime = Number ( process . hrtime . bigint ( ) - downstreamStartTime ) ;
185+ request . log ( [ 'h2o2' , 'error' ] , { downstreamResponseTime } ) ;
186186 }
187187
188188 if ( settings . onResponse ) {
@@ -266,10 +266,10 @@ internals.mapUri = function (protocol, host, port, uri) {
266266 protocol += ':' ;
267267 }
268268
269- protocol = protocol || 'http:' ;
269+ protocol = protocol ?? 'http:' ;
270270
271- port = port || ( protocol === 'http:' ? 80 : 443 ) ;
272- const baseUrl = protocol + '//' + host + ':' + port ;
271+ port = port ?? ( protocol === 'http:' ? 80 : 443 ) ;
272+ const baseUrl = Url . format ( { protocol, hostname : host , port } ) ;
273273
274274 return function ( request ) {
275275
0 commit comments