(upstreamUrl: string, req: IncomingMessage)
| 206 | } |
| 207 | |
| 208 | function rewriteUploadDirectUrl(upstreamUrl: string, req: IncomingMessage): string | null { |
| 209 | let parsed: URL; |
| 210 | try { |
| 211 | parsed = new URL(upstreamUrl); |
| 212 | } catch { |
| 213 | return null; |
| 214 | } |
| 215 | |
| 216 | if (!parsed.pathname.startsWith('/upload/')) { |
| 217 | return null; |
| 218 | } |
| 219 | |
| 220 | const host = typeof req.headers.host === 'string' ? req.headers.host : ''; |
| 221 | if (!host) return null; |
| 222 | |
| 223 | const requestPath = new URL(req.url ?? '/', 'http://127.0.0.1').pathname; |
| 224 | const uploadIndex = requestPath.lastIndexOf('/upload/preflight'); |
| 225 | const uploadPrefix = uploadIndex >= 0 ? requestPath.slice(0, uploadIndex) : ''; |
| 226 | const forwardedProto = req.headers['x-forwarded-proto']; |
| 227 | const proto = Array.isArray(forwardedProto) ? forwardedProto[0] : forwardedProto; |
| 228 | const rewritten = new URL(`${proto || 'http'}://${host}`); |
| 229 | rewritten.pathname = `${uploadPrefix}${parsed.pathname}`; |
| 230 | rewritten.search = parsed.search; |
| 231 | return rewritten.toString(); |
| 232 | } |
| 233 | |
| 234 | async function pipeProxyResponseBody(response: Response, res: ServerResponse): Promise<void> { |
| 235 | if (!response.body) { |
no test coverage detected
searching dependent graphs…