( req: IncomingMessage, res: ServerResponse, options: Required<DaemonProxyOptions>, )
| 44 | } |
| 45 | |
| 46 | async function handleProxyRequest( |
| 47 | req: IncomingMessage, |
| 48 | res: ServerResponse, |
| 49 | options: Required<DaemonProxyOptions>, |
| 50 | ): Promise<void> { |
| 51 | const route = resolveProxyRoute(req.url ?? '/'); |
| 52 | if (req.method === 'GET' && route === '/health') { |
| 53 | await sendProxyHealth(res, options); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | if (!isSupportedDaemonRoute(route, req.method)) { |
| 58 | res.statusCode = 404; |
| 59 | res.end('Not found'); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | let rpcBody: string | undefined; |
| 64 | if (route === '/rpc') { |
| 65 | rpcBody = ( |
| 66 | await readNodeHttpRequestBody( |
| 67 | req, |
| 68 | options.maxRpcBodyBytes, |
| 69 | 'Proxy request body is too large.', |
| 70 | ) |
| 71 | ).toString('utf8'); |
| 72 | } |
| 73 | |
| 74 | if (!isAuthorized(req, options.clientToken, rpcBody)) { |
| 75 | sendUnauthorized(res, route, readJsonRpcId(rpcBody)); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | await forwardProxyRequest({ req, res, route, options, rpcBody }); |
| 80 | } |
| 81 | |
| 82 | async function sendProxyHealth(res: ServerResponse, options: Required<DaemonProxyOptions>) { |
| 83 | const upstream = await readUpstreamHealth(options); |
no test coverage detected