@@ -15,9 +15,9 @@ function noopResolves () {
1515 return Promise . resolve ( )
1616}
1717
18- async function sendSuccess ( res , { info, verbatim, statusOk } ) {
19- res . statusCode = statusOk
18+ async function sendSuccess ( res , { info, verbatim, statusOk, headers } ) {
2019 res . setHeader ( 'Content-Type' , 'application/json' )
20+ res . writeHead ( statusOk , headers )
2121 if ( info ) {
2222 return res . end (
2323 JSON . stringify (
@@ -34,7 +34,7 @@ async function sendSuccess (res, { info, verbatim, statusOk }) {
3434}
3535
3636async function sendFailure ( res , options ) {
37- const { error, onSendFailureDuringShutdown, exposeStackTraces, statusCode, statusError } = options
37+ const { error, headers , onSendFailureDuringShutdown, exposeStackTraces, statusCode, statusError } = options
3838
3939 function replaceErrors ( _ , value ) {
4040 if ( value instanceof Error ) {
@@ -54,8 +54,8 @@ async function sendFailure (res, options) {
5454 if ( onSendFailureDuringShutdown ) {
5555 await onSendFailureDuringShutdown ( )
5656 }
57- res . statusCode = statusCode || statusError
5857 res . setHeader ( 'Content-Type' , 'application/json' )
58+ res . writeHead ( statusCode || statusError , headers )
5959 if ( error ) {
6060 return res . end ( JSON . stringify ( {
6161 status : 'error' ,
@@ -73,7 +73,7 @@ const intialState = {
7373function noop ( ) { }
7474
7575function decorateWithHealthCheck ( server , state , options ) {
76- const { healthChecks, logger, onSendFailureDuringShutdown, sendFailuresDuringShutdown, caseInsensitive, statusOk, statusError } = options
76+ const { healthChecks, logger, headers , onSendFailureDuringShutdown, sendFailuresDuringShutdown, caseInsensitive, statusOk, statusError } = options
7777
7878 let hasSetHandler = false
7979 const createHandler = ( listener ) => {
@@ -88,16 +88,29 @@ function decorateWithHealthCheck (server, state, options) {
8888 info = await healthCheck ( { state } )
8989 } catch ( error ) {
9090 logger ( 'healthcheck failed' , error )
91- return sendFailure ( res , { error : error . causes , exposeStackTraces : healthChecks . __unsafeExposeStackTraces , statusCode : error . statusCode , statusError } )
91+ return sendFailure (
92+ res ,
93+ {
94+ error : error . causes ,
95+ headers,
96+ exposeStackTraces : healthChecks . __unsafeExposeStackTraces ,
97+ statusCode : error . statusCode ,
98+ statusError
99+ }
100+ )
92101 }
93- return sendSuccess ( res , { info, verbatim : healthChecks . verbatim , statusOk } )
102+ return sendSuccess (
103+ res ,
104+ { info, verbatim : healthChecks . verbatim , statusOk, headers }
105+ )
94106 }
95107
96108 hasSetHandler = true
97109
98110 return async ( req , res ) => {
99111 const url = caseInsensitive ? req . url . toLowerCase ( ) : req . url
100112 const healthCheck = healthChecks [ url ]
113+
101114 if ( healthCheck ) {
102115 return check ( healthCheck , res )
103116 } else {
@@ -164,7 +177,8 @@ function terminus (server, options = {}) {
164177 logger = noop ,
165178 caseInsensitive = false ,
166179 statusOk = 200 ,
167- statusError = 503
180+ statusError = 503 ,
181+ headers = options . headers || { }
168182 } = options
169183 const onSignal = options . onSignal || options . onSigterm || noopResolves
170184 const state = Object . assign ( { } , intialState )
@@ -177,7 +191,8 @@ function terminus (server, options = {}) {
177191 onSendFailureDuringShutdown,
178192 caseInsensitive,
179193 statusOk,
180- statusError
194+ statusError,
195+ headers
181196 } )
182197 }
183198
@@ -190,7 +205,8 @@ function terminus (server, options = {}) {
190205 beforeShutdown,
191206 onShutdown,
192207 timeout,
193- logger
208+ logger,
209+ headers
194210 } )
195211
196212 return server
0 commit comments