* @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 } = {})
| 337 | * rewriteJsonResponse). |
| 338 | */ |
| 339 | function proxyHttp(req, res, target, { headers, onResponse } = {}) { |
| 340 | metrics.activeHttpRequests += 1; |
| 341 | metrics.totalHttpRequests += 1; |
| 342 | const finish = once(() => { |
| 343 | metrics.activeHttpRequests = Math.max(0, metrics.activeHttpRequests - 1); |
| 344 | }); |
| 345 | res.on("close", finish); |
| 346 | res.on("finish", finish); |
| 347 | res.on("error", finish); |
| 348 | |
| 349 | const handleProxyError = (err) => { |
| 350 | metrics.totalErrors += 1; |
| 351 | if (!isBenignSocketError(err)) { |
| 352 | console.error( |
| 353 | `[${label}] Proxy error for ${req.url} -> ${target}:`, |
| 354 | err, |
| 355 | ); |
| 356 | } |
| 357 | writeProxyError( |
| 358 | res, |
| 359 | err instanceof Error ? err.message : String(err), |
| 360 | errorBody, |
| 361 | ); |
| 362 | finish(); |
| 363 | }; |
| 364 | |
| 365 | const options = { target, headers }; |
| 366 | if (onResponse) { |
| 367 | responseHandlers.set(req, onResponse); |
| 368 | options.selfHandleResponse = true; |
| 369 | } |
| 370 | try { |
| 371 | proxy.web(req, res, options).catch(handleProxyError); |
| 372 | } catch (err) { |
| 373 | handleProxyError(err); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | function proxyWebSocket(req, socket, head, target) { |
| 378 | metrics.activeWebSockets += 1; |
nothing calls this directly
no test coverage detected