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
Provenance
- Commit
821a408- Tree
c6e1b43- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
26ff521
3 files changed+85 added−28 removed
modified:runtime_tests/lambda/index.test.ts+45 added-20 removed
7 unmodified lines8910111213141516171819202118 unmodified lines4041424338444546474849505152535455565758596061626364656667688 unmodified lines7778798056575859606162638182838485868788899091929326 unmodified lines120121122123991241251261271031041281291301311321336 unmodified lines14014114214311912012112212312412514414514614714814915015115215315428 unmodified lines1831841851861621871881897 unmodified lines app.get('/', (c) => { return c.text('Hello Lambda!') })
app.get('/binary', (c) => { return c.body('Fake Image', 200, { 'Content-Type': 'image/png', }) })
app.post('/post', async (c) => { const body = (await c.req.parseBody()) as { message: string } return c.text(body.message) })18 unmodified lines }
const response = await handler(event) expect(response.statusCode).toBe(200) expect(response.body).toBe('SGVsbG8gTGFtYmRhIQ==') expect(response.body).toBe('Hello Lambda!') expect(response.headers['content-type']).toMatch(/^text\/plain/) expect(response.isBase64Encoded).toBe(false) })
it('Should handle a GET request and return a 200 response with binary', async () => { const event = { httpMethod: 'GET', headers: {}, path: '/binary', body: null, isBase64Encoded: false, requestContext: { domainName: 'example.com', }, }
const response = await handler(event) expect(response.statusCode).toBe(200) expect(response.body).toBe('RmFrZSBJbWFnZQ==') expect(response.headers['content-type']).toMatch(/^image\/png/) expect(response.isBase64Encoded).toBe(true) })
it('Should handle a GET request and return a 200 response (LambdaFunctionUrlEvent)', async () => {8 unmodified lines http: { method: 'GET', }, }, }; const response = await handler(event); expect(response.statusCode).toBe(200); expect(response.body).toBe('SGVsbG8gTGFtYmRhIQ=='); expect(response.headers['content-type']).toMatch(/^text\/plain/); expect(response.isBase64Encoded).toBe(true); }); }
const response = await handler(event) expect(response.statusCode).toBe(200) expect(response.body).toBe('Hello Lambda!') expect(response.headers['content-type']).toMatch(/^text\/plain/) expect(response.isBase64Encoded).toBe(false) })
it('Should handle a GET request and return a 404 response', async () => { const event = { httpMethod: 'GET', headers: { 'content-type': 'text/plain' },26 unmodified lines }
const response = await handler(event) expect(response.statusCode).toBe(200) expect(response.body).toBe('R29vZCBNb3JuaW5nIExhbWJkYSE=') expect(response.body).toBe('Good Morning Lambda!') })
it('Should handle a POST request and return a 200 response (LambdaFunctionUrlEvent)', async () => { const searchParam = new URLSearchParams(); searchParam.append('message', 'Good Morning Lambda!'); const searchParam = new URLSearchParams() searchParam.append('message', 'Good Morning Lambda!') const event = { headers: { 'Content-Type': 'application/x-www-form-urlencoded', },6 unmodified lines http: { method: 'POST', }, }, }; const response = await handler(event); expect(response.statusCode).toBe(200); expect(response.body).toBe('R29vZCBNb3JuaW5nIExhbWJkYSE='); }); }
const response = await handler(event) expect(response.statusCode).toBe(200) expect(response.body).toBe('Good Morning Lambda!') })
it('Should handle a request and return a 401 response with Basic auth', async () => { const event = { httpMethod: 'GET', headers: {28 unmodified lines }
const response = await handler(event) expect(response.statusCode).toBe(200) expect(response.body).toBe('R29vZCBOaWdodCBMYW1iZGEh') expect(response.body).toBe('Good Night Lambda!') })})added:src/adapter/aws-lambda/handler.test.ts+15 added
123456789101112131415import { isContentTypeBinary } from './handler'
describe('isContentTypeBinary', () => { it('Should determine whether it is binary', () => { expect(isContentTypeBinary('image/png')).toBe(true) expect(isContentTypeBinary('font/woff2')).toBe(true) expect(isContentTypeBinary('text/plain')).toBe(false) expect(isContentTypeBinary('text/plain; charset=UTF-8')).toBe(false) expect(isContentTypeBinary('text/css')).toBe(false) expect(isContentTypeBinary('text/javascript')).toBe(false) expect(isContentTypeBinary('application/json')).toBe(false) expect(isContentTypeBinary('application/ld+json')).toBe(false) expect(isContentTypeBinary('application/json; charset=UTF-8')).toBe(false) })})modified:src/adapter/aws-lambda/handler.ts+25 added-8 removed
38 unmodified lines39 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 lines68 }69 }70 71 const createResult = async (res: Response): Promise<APIGatewayProxyResult> => {72+ const contentType = res.headers.get('content-type')73+ const isBase64Encoded = contentType && isContentTypeBinary(contentType) ? true : false74+75+ const body = isBase64Encoded ? await fromReadableToString(res) : await res.text()76+77 const result: APIGatewayProxyResult = {73- body: await fromReadableToString(res),78+ body: body,79 headers: {},80 statusCode: res.status,76- isBase64Encoded: true,81+ isBase64Encoded,82 }83 84 res.headers.forEach((value, key) => {85 result.headers[key] = value1 unmodified line87 88 return result89 }90 86-const createRequest = (event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent) => {91+const 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}` : urlPath99 100 const headers = new Headers()101 for (const [k, v] of Object.entries(event.headers)) {2 unmodified lines104 105 const method = 'httpMethod' in event ? event.httpMethod : event.requestContext.http.method106 const requestInit: RequestInit = {107 headers,99- method108+ method,109 }110 111 if (event.body) {112 requestInit.body = event.isBase64Encoded ? atob(event.body) : event.body1 unmodified line114 115 return new Request(url, requestInit)116 }117 109-const extractQueryString = (event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent) => {118+const 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 lines152 }153 154 return btoa(string)155 }156+157+export const isContentTypeBinary = (contentType: string) => {158+ return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml)$/.test(159+ contentType160+ )161+}