(server, state, options)
| 71 | function noop () {} |
| 72 | |
| 73 | function decorateWithHealthCheck (server, state, options) { |
| 74 | const { healthChecks, logger, headers, onSendFailureDuringShutdown, sendFailuresDuringShutdown, caseInsensitive, statusOk, statusOkResponse, statusError, statusErrorResponse } = options |
| 75 | |
| 76 | let hasSetHandler = false |
| 77 | const createHandler = (listener) => { |
| 78 | const check = hasSetHandler |
| 79 | ? () => {} |
| 80 | : async (healthCheck, res) => { |
| 81 | if (state.isShuttingDown && sendFailuresDuringShutdown) { |
| 82 | return sendFailure(res, { onSendFailureDuringShutdown, statusError, statusErrorResponse }) |
| 83 | } |
| 84 | let info |
| 85 | try { |
| 86 | info = await healthCheck({ state }) |
| 87 | } catch (error) { |
| 88 | logger('healthcheck failed', error) |
| 89 | const statusCode = error.statusCode |
| 90 | const statusResponse = error.statusResponse |
| 91 | return sendFailure( |
| 92 | res, |
| 93 | { |
| 94 | error: error.causes, |
| 95 | headers, |
| 96 | exposeStackTraces: healthChecks.__unsafeExposeStackTraces, |
| 97 | statusCode, |
| 98 | statusResponse, |
| 99 | statusError, |
| 100 | statusErrorResponse |
| 101 | } |
| 102 | ) |
| 103 | } |
| 104 | return sendSuccess( |
| 105 | res, |
| 106 | { info, verbatim: healthChecks.verbatim, statusOk, statusOkResponse, headers } |
| 107 | ) |
| 108 | } |
| 109 | |
| 110 | hasSetHandler = true |
| 111 | |
| 112 | return async (req, res) => { |
| 113 | const url = caseInsensitive ? req.url.toLowerCase() : req.url |
| 114 | const healthCheck = healthChecks[url] |
| 115 | |
| 116 | if (healthCheck) { |
| 117 | return check(healthCheck, res) |
| 118 | } else { |
| 119 | listener(req, res) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | server.listeners('request').forEach((listener) => { |
| 125 | server.removeListener('request', listener) |
| 126 | server.on('request', createHandler(listener)) |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | function decorateWithSignalHandler (server, state, options) { |
no test coverage detected
searching dependent graphs…