|
| 1 | +/** |
| 2 | + * Lambda adapter utilities. |
| 3 | + * |
| 4 | + * Pure functions to convert between AWS API Gateway events and |
| 5 | + * Web Standard Request/Response objects. No framework dependencies. |
| 6 | + */ |
| 7 | + |
| 8 | +import type { |
| 9 | + LambdaEvent, |
| 10 | + LambdaResult, |
| 11 | + APIGatewayV1Event, |
| 12 | + APIGatewayV2Event, |
| 13 | + APIGatewayV1Result, |
| 14 | + APIGatewayV2Result, |
| 15 | +} from './types.js'; |
| 16 | + |
| 17 | +/** Type guard: true for API Gateway v2 (HTTP API / Function URL) events. */ |
| 18 | +export function isV2Event(event: LambdaEvent): event is APIGatewayV2Event { |
| 19 | + return 'version' in event && (event as any).version === '2.0'; |
| 20 | +} |
| 21 | + |
| 22 | +/** Extract source IP from a Lambda event (v1 or v2). */ |
| 23 | +export function getSourceIp(event: LambdaEvent): string | undefined { |
| 24 | + if (isV2Event(event)) { |
| 25 | + return event.requestContext.http.sourceIp; |
| 26 | + } |
| 27 | + return (event as APIGatewayV1Event).requestContext?.identity?.sourceIp; |
| 28 | +} |
| 29 | + |
| 30 | +/** Convert a Lambda event (v1 or v2) to a Web Standard Request. */ |
| 31 | +export function lambdaEventToRequest(event: LambdaEvent, basePath?: string): Request { |
| 32 | + let method: string; |
| 33 | + let path: string; |
| 34 | + let queryString: string; |
| 35 | + const headers = new Headers(); |
| 36 | + |
| 37 | + if (isV2Event(event)) { |
| 38 | + method = event.requestContext.http.method; |
| 39 | + path = event.rawPath ?? event.requestContext.http.path; |
| 40 | + queryString = event.rawQueryString ?? ''; |
| 41 | + |
| 42 | + // Copy headers |
| 43 | + if (event.headers) { |
| 44 | + for (const [key, value] of Object.entries(event.headers)) { |
| 45 | + if (value !== undefined) { |
| 46 | + headers.set(key, value); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } else { |
| 51 | + const v1 = event as APIGatewayV1Event; |
| 52 | + method = v1.httpMethod; |
| 53 | + path = v1.path; |
| 54 | + |
| 55 | + // Reconstruct query string from multiValueQueryStringParameters (preserves duplicates) |
| 56 | + // Fall back to queryStringParameters if multi-value not present |
| 57 | + if (v1.multiValueQueryStringParameters) { |
| 58 | + const parts: string[] = []; |
| 59 | + for (const [key, values] of Object.entries(v1.multiValueQueryStringParameters)) { |
| 60 | + if (values) { |
| 61 | + for (const val of values) { |
| 62 | + parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(val)}`); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + queryString = parts.join('&'); |
| 67 | + } else if (v1.queryStringParameters) { |
| 68 | + const parts: string[] = []; |
| 69 | + for (const [key, value] of Object.entries(v1.queryStringParameters)) { |
| 70 | + if (value !== undefined) { |
| 71 | + parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`); |
| 72 | + } |
| 73 | + } |
| 74 | + queryString = parts.join('&'); |
| 75 | + } else { |
| 76 | + queryString = ''; |
| 77 | + } |
| 78 | + |
| 79 | + // Copy headers — use multiValueHeaders to preserve duplicates |
| 80 | + if (v1.multiValueHeaders) { |
| 81 | + for (const [key, values] of Object.entries(v1.multiValueHeaders)) { |
| 82 | + if (values) { |
| 83 | + for (const val of values) { |
| 84 | + headers.append(key, val); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } else if (v1.headers) { |
| 89 | + for (const [key, value] of Object.entries(v1.headers)) { |
| 90 | + if (value !== undefined) { |
| 91 | + headers.set(key, value); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Strip basePath prefix |
| 98 | + if (basePath && path.startsWith(basePath)) { |
| 99 | + path = path.slice(basePath.length) || '/'; |
| 100 | + } |
| 101 | + |
| 102 | + const url = `https://lambda.local${path}${queryString ? '?' + queryString : ''}`; |
| 103 | + |
| 104 | + // Decode body |
| 105 | + let body: string | undefined; |
| 106 | + if (event.body != null && event.body !== '') { |
| 107 | + body = event.isBase64Encoded |
| 108 | + ? Buffer.from(event.body, 'base64').toString('utf-8') |
| 109 | + : event.body; |
| 110 | + } |
| 111 | + |
| 112 | + // Only set body on methods that support it |
| 113 | + const hasBody = ['POST', 'PUT', 'PATCH', 'DELETE'].includes(method.toUpperCase()); |
| 114 | + |
| 115 | + return new Request(url, { |
| 116 | + method, |
| 117 | + headers, |
| 118 | + body: hasBody ? (body ?? '') : undefined, |
| 119 | + }); |
| 120 | +} |
| 121 | + |
| 122 | +/** Convert a Web Standard Response to a Lambda result (v1 or v2 format). */ |
| 123 | +export async function responseToLambdaResult( |
| 124 | + response: Response, |
| 125 | + event: LambdaEvent, |
| 126 | +): Promise<LambdaResult> { |
| 127 | + const body = await response.text(); |
| 128 | + const headers: Record<string, string> = {}; |
| 129 | + |
| 130 | + response.headers.forEach((value, key) => { |
| 131 | + headers[key] = value; |
| 132 | + }); |
| 133 | + |
| 134 | + if (isV2Event(event)) { |
| 135 | + const result: APIGatewayV2Result = { |
| 136 | + statusCode: response.status, |
| 137 | + headers, |
| 138 | + body, |
| 139 | + isBase64Encoded: false, |
| 140 | + }; |
| 141 | + return result; |
| 142 | + } |
| 143 | + |
| 144 | + // v1: also include multiValueHeaders for Set-Cookie etc. |
| 145 | + const multiValueHeaders: Record<string, string[]> = {}; |
| 146 | + response.headers.forEach((value, key) => { |
| 147 | + if (!multiValueHeaders[key]) { |
| 148 | + multiValueHeaders[key] = []; |
| 149 | + } |
| 150 | + multiValueHeaders[key].push(value); |
| 151 | + }); |
| 152 | + |
| 153 | + const result: APIGatewayV1Result = { |
| 154 | + statusCode: response.status, |
| 155 | + headers, |
| 156 | + multiValueHeaders, |
| 157 | + body, |
| 158 | + isBase64Encoded: false, |
| 159 | + }; |
| 160 | + return result; |
| 161 | +} |
0 commit comments