fix(aws-lambda): handle multiple cookies in streaming responses (#2926)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix: handle multiple cookies in streaming responses This fix does address that cookies have to be handled specially in lambda responses. Fixes #2921 * test: check cookie handling
authored by Markus Wolf · committed by GitHub · parent 75663ff · Browse files · 4 files·+135 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+135 added−2 removed
- tests:+75 added−0 removed
- other:+60 added−2 removed
Provenance
- Commit
a2a04a9- Tree
6f7437e- Author
Markus Wolf <KnisterPeter@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
75663ff
4 files changed+135 added−2 removed
added:runtime_tests/lambda/stream-mock.ts+43 added
12345678910111213141516171819202122232425262728293031323334353637383940414243import { Writable } from 'node:stream'import { vi } from 'vitest'import type { APIGatewayProxyEvent, APIGatewayProxyEventV2,} from '../../src/adapter/aws-lambda/handler'import type { LambdaContext } from '../../src/adapter/aws-lambda/types'
type StreamifyResponseHandler = ( handlerFunc: ( event: APIGatewayProxyEvent | APIGatewayProxyEventV2, responseStream: Writable, context: LambdaContext ) => Promise<void>) => (event: APIGatewayProxyEvent, context: LambdaContext) => Promise<NodeJS.WritableStream>
const mockStreamifyResponse: StreamifyResponseHandler = (handlerFunc) => { return async (event, context) => { const chunks = [] const mockWritableStream = new Writable({ write(chunk, encoding, callback) { chunks.push(chunk) callback() }, }) mockWritableStream.chunks = chunks await handlerFunc(event, mockWritableStream, context) mockWritableStream.end() return mockWritableStream }}
const awslambda = { streamifyResponse: mockStreamifyResponse, HttpResponseStream: { from: (stream: Writable, httpResponseMetadata: unknown): Writable => { stream.write(Buffer.from(JSON.stringify(httpResponseMetadata))) return stream }, },}
vi.stubGlobal('awslambda', awslambda)added:runtime_tests/lambda/stream.test.ts+75 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475import { streamHandle } from '../../src/adapter/aws-lambda/handler'import type { LambdaEvent } from '../../src/adapter/aws-lambda/handler'import type { ApiGatewayRequestContext, ApiGatewayRequestContextV2, LambdaContext,} from '../../src/adapter/aws-lambda/types'import { Hono } from '../../src/hono'import './stream-mock'
type Bindings = { event: LambdaEvent lambdaContext: LambdaContext requestContext: ApiGatewayRequestContext | ApiGatewayRequestContextV2}
const testApiGatewayRequestContextV2 = { accountId: '123456789012', apiId: 'urlid', authentication: null, authorizer: { iam: { accessKey: 'AKIA...', accountId: '111122223333', callerId: 'AIDA...', cognitoIdentity: null, principalOrgId: null, userArn: 'arn:aws:iam::111122223333:user/example-user', userId: 'AIDA...', }, }, domainName: 'example.com', domainPrefix: '<url-id>', http: { method: 'GET', path: '/my/path', protocol: 'HTTP/1.1', sourceIp: '123.123.123.123', userAgent: 'agent', }, requestId: 'id', routeKey: '$default', stage: '$default', time: '12/Mar/2020:19:03:58 +0000', timeEpoch: 1583348638390, customProperty: 'customValue',}
describe('streamHandle function', () => { const app = new Hono<{ Bindings: Bindings }>()
app.get('/cookies', async (c) => { c.res.headers.append('Set-Cookie', 'cookie1=value1') c.res.headers.append('Set-Cookie', 'cookie2=value2') return c.text('Cookies Set') })
const handler = streamHandle(app)
it('to write multiple cookies into the headers', async () => { const event = { headers: { 'content-type': 'text/plain' }, rawPath: '/cookies', rawQueryString: '', body: null, isBase64Encoded: false, requestContext: testApiGatewayRequestContextV2, }
const stream = await handler(event)
const metadata = JSON.parse(stream.chunks[0].toString()) expect(metadata.cookies).toEqual(['cookie1=value1', 'cookie2=value2']) })})modified:runtime_tests/lambda/vitest.config.ts+5 added-1 removed
7 unmodified lines891011121213141516171819202 unmodified lines7 unmodified lines NAME: 'Node', }, globals: true, include: ['**/runtime_tests/lambda/**/*.+(ts|tsx|js)'], exclude: ['**/runtime_tests/lambda/vitest.config.ts', '**/runtime_tests/lambda/mock.ts'], exclude: [ '**/runtime_tests/lambda/vitest.config.ts', '**/runtime_tests/lambda/mock.ts', '**/runtime_tests/lambda/stream-mock.ts', ], coverage: { ...config.test?.coverage, reportsDirectory: './coverage/raw/runtime-lambda', },2 unmodified linesmodified:src/adapter/aws-lambda/handler.ts+12 added-1 removed
124 unmodified lines125 requestContext,126 context,127 })128 129+ const headers: Record<string, string> = {}130+ const cookies: string[] = []131+ res.headers.forEach((value, name) => {132+ if (name === 'set-cookie') {133+ cookies.push(value)134+ } else {135+ headers[name] = value136+ }137+ })138+139 // Check content type140 const httpResponseMetadata = {141 statusCode: res.status,132- headers: Object.fromEntries(res.headers.entries()),142+ headers,143+ cookies,144 }145 146 // Update response stream147 // @ts-expect-error awslambda is not a standard API340 unmodified lines