* @param {import("node:http").IncomingMessage} req * @param {import("node:http").ServerResponse} res * @param {string} target * @param {object} [options] * @param {Record } [options.headers] Set on the upstream request * @param {(proxyRes: import("node:http").IncomingMe
(req, res, target, { headers, onResponse } = {})
| 369 | * rewriteJsonResponse). |
| 370 | */ |
| 371 | function proxyHttp(req, res, target, { headers, onResponse } = {}) { |
| 372 | if (xfwd) forwardClientOrigin(req); |
| 373 | metrics.activeHttpRequests += 1; |
| 374 | metrics.totalHttpRequests += 1; |
| 375 | const finish = once(() => { |
| 376 | metrics.activeHttpRequests = Math.max(0, metrics.activeHttpRequests - 1); |
| 377 | }); |
| 378 | res.on("close", finish); |
| 379 | res.on("finish", finish); |
| 380 | res.on("error", finish); |
| 381 | |
| 382 | const handleProxyError = (err) => { |
| 383 | metrics.totalErrors += 1; |
| 384 | if (!isBenignSocketError(err)) { |
| 385 | console.error( |
| 386 | `[${label}] Proxy error for ${req.url} -> ${target}:`, |
| 387 | err, |
| 388 | ); |
| 389 | } |
| 390 | writeProxyError( |
| 391 | res, |
| 392 | err instanceof Error ? err.message : String(err), |
| 393 | errorBody, |
| 394 | ); |
| 395 | finish(); |
| 396 | }; |
| 397 | |
| 398 | const options = { target, headers }; |
| 399 | if (onResponse) { |
| 400 | responseHandlers.set(req, onResponse); |
| 401 | options.selfHandleResponse = true; |
| 402 | } |
| 403 | try { |
| 404 | proxy.web(req, res, options).catch(handleProxyError); |
| 405 | } catch (err) { |
| 406 | handleProxyError(err); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | function proxyWebSocket(req, socket, head, target) { |
| 411 | if (xfwd) forwardClientOrigin(req); |
nothing calls this directly
no test coverage detected