(request: {
method?: string;
url?: string;
headers?: {
[key: string]: string | string[] | undefined;
};
protocol?: string;
socket?: {
encrypted?: boolean;
remoteAddress?: string;
};
})
| 196 | * we want to be more specific and generally require a http.IncomingMessage-like object. |
| 197 | */ |
| 198 | export function httpRequestToRequestData(request: { |
| 199 | method?: string; |
| 200 | url?: string; |
| 201 | headers?: { |
| 202 | [key: string]: string | string[] | undefined; |
| 203 | }; |
| 204 | protocol?: string; |
| 205 | socket?: { |
| 206 | encrypted?: boolean; |
| 207 | remoteAddress?: string; |
| 208 | }; |
| 209 | }): RequestEventData { |
| 210 | const headers = request.headers || {}; |
| 211 | |
| 212 | // Check for x-forwarded-host first, then fall back to host header |
| 213 | const forwardedHost = typeof headers['x-forwarded-host'] === 'string' ? headers['x-forwarded-host'] : undefined; |
| 214 | const host = forwardedHost || (typeof headers.host === 'string' ? headers.host : undefined); |
| 215 | |
| 216 | // Check for x-forwarded-proto first, then fall back to existing protocol detection |
| 217 | const forwardedProto = typeof headers['x-forwarded-proto'] === 'string' ? headers['x-forwarded-proto'] : undefined; |
| 218 | const protocol = forwardedProto || request.protocol || (request.socket?.encrypted ? 'https' : 'http'); |
| 219 | |
| 220 | const url = request.url || ''; |
| 221 | |
| 222 | const absoluteUrl = getAbsoluteUrl({ |
| 223 | url, |
| 224 | host, |
| 225 | protocol, |
| 226 | }); |
| 227 | |
| 228 | // This is non-standard, but may be sometimes set |
| 229 | // It may be overwritten later by our own body handling |
| 230 | const data = (request as PolymorphicRequest).body || undefined; |
| 231 | |
| 232 | // This is non-standard, but may be set on e.g. Next.js or Express requests |
| 233 | const cookies = (request as PolymorphicRequest).cookies; |
| 234 | |
| 235 | return { |
| 236 | url: absoluteUrl, |
| 237 | method: request.method, |
| 238 | query_string: extractQueryParamsFromUrl(url), |
| 239 | headers: headersToDict(headers), |
| 240 | cookies, |
| 241 | data, |
| 242 | }; |
| 243 | } |
| 244 | |
| 245 | function getAbsoluteUrl({ |
| 246 | url, |
no test coverage detected