Skip to content
Myr

fix(lambda): don't encode text (#1091)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by GitHub · parent 26ff521 · Browse files · 3 files+85 added−28 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+85 added−28 removed

  • tests:+60 added−20 removed
  • other:+25 added−8 removed

3 files+85 added−28 removed

modified:runtime_tests/lambda/index.test.ts+45 added-20 removed
added:src/adapter/aws-lambda/handler.test.ts+15 added
modified:src/adapter/aws-lambda/handler.ts+25 added-8 removed
38 unmodified lines
39 rawQueryString: string40 body: string | null41 isBase64Encoded: boolean42 requestContext: {43 domainName: string,43 domainName: string44 http: {45 method: string,45 method: string46 }47 }48}49
18 unmodified lines
68 }69}7071const createResult = async (res: Response): Promise<APIGatewayProxyResult> => {72 const contentType = res.headers.get('content-type')73 const isBase64Encoded = contentType && isContentTypeBinary(contentType) ? true : false7475 const body = isBase64Encoded ? await fromReadableToString(res) : await res.text()7677 const result: APIGatewayProxyResult = {73 body: await fromReadableToString(res),78 body: body,79 headers: {},80 statusCode: res.status,76 isBase64Encoded: true,81 isBase64Encoded,82 }8384 res.headers.forEach((value, key) => {85 result.headers[key] = value
1 unmodified line
8788 return result89}9086const createRequest = (event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent) => {91const createRequest = (92 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent93) => {94 const queryString = extractQueryString(event)88 const urlPath = `https://${event.requestContext.domainName}${isProxyEvent(event) ? event.path : event.rawPath}`95 const urlPath = `https://${event.requestContext.domainName}${96 isProxyEvent(event) ? event.path : event.rawPath97 }`98 const url = queryString ? `${urlPath}?${queryString}` : urlPath99100 const headers = new Headers()101 for (const [k, v] of Object.entries(event.headers)) {
2 unmodified lines
104105 const method = 'httpMethod' in event ? event.httpMethod : event.requestContext.http.method106 const requestInit: RequestInit = {107 headers,99 method108 method,109 }110111 if (event.body) {112 requestInit.body = event.isBase64Encoded ? atob(event.body) : event.body
1 unmodified line
114115 return new Request(url, requestInit)116}117109const extractQueryString = (event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent) => {118const extractQueryString = (119 event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent120) => {121 if (isProxyEvent(event)) {122 return Object.entries(event.queryStringParameters || {})123 .filter(([, value]) => value)124 .map(([key, value]) => `${key}=${value}`)
27 unmodified lines
152 }153154 return btoa(string)155}156157export const isContentTypeBinary = (contentType: string) => {158 return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml)$/.test(159 contentType160 )161}