feat(adaptor): Accessable Lambda "Context" (#1523)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* adding lambda-context * format * type any * ignore denoify * export type
authored by watany · committed by GitHub · parent dddd497 · Browse files · 4 files·+89 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+89 added−2 removed
- tests:+34 added−0 removed
- other:+55 added−2 removed
Provenance
- Commit
43749a0- Tree
5742c1d- Author
watany <76135106+watany-dev@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
dddd497
4 files changed+89 added−2 removed
modified:runtime_tests/lambda/index.test.ts+34 added
1 unmodified line2 ApiGatewayRequestContext,3 LambdaFunctionUrlRequestContext,4 } from '../../src/adapter/aws-lambda/custom-context'5 import { handle } from '../../src/adapter/aws-lambda/handler'6+import type { LambdaContext } from '../../src/adapter/aws-lambda/types'7 import { Hono } from '../../src/hono'8 import { basicAuth } from '../../src/middleware/basic-auth'9 10 type Bindings = {11+ lambdaContext: LambdaContext12 requestContext: ApiGatewayRequestContext | LambdaFunctionUrlRequestContext13 }14 15 describe('AWS Lambda Adapter for Hono', () => {23 unmodified lines39 const password = 'hono-password-a'40 app.use('/auth/*', basicAuth({ username, password }))41 app.get('/auth/abc', (c) => c.text('Good Night Lambda!'))42 43+ app.get('/lambda-context', (c) => {44+ const fnctx = c.env.lambdaContext45+ return c.json(fnctx)46+ })47+48 app.get('/custom-context/apigw', (c) => {49 const lambdaContext = c.env.requestContext50 return c.json(lambdaContext)51 })236 unmodified lines288 const response = await handler(event)289 expect(response.statusCode).toBe(200)290 expect(JSON.parse(response.body).customProperty).toEqual('customValue')291 })292+293+ it('Should handle a GET request and context', async () => {294+ const event = {295+ httpMethod: 'GET',296+ headers: { 'content-type': 'application/json' },297+ path: '/lambda-context',298+ body: null,299+ isBase64Encoded: false,300+ requestContext: testApiGatewayRequestContext,301+ }302+ const context: LambdaContext = {303+ callbackWaitsForEmptyEventLoop: false,304+ functionName: 'myLambdaFunction',305+ functionVersion: '1.0.0',306+ invokedFunctionArn: 'arn:aws:lambda:us-west-2:123456789012:function:myLambdaFunction',307+ memoryLimitInMB: '128',308+ awsRequestId: 'c6af9ac6-a7b0-11e6-80f5-76304dec7eb7',309+ logGroupName: '/aws/lambda/myLambdaFunction',310+ logStreamName: '2016/11/14/[$LATEST]f2d4b21cfb33490da2e8f8ef79a483s4',311+ getRemainingTimeInMillis: () => {312+ return 60000 // 60 seconds313+ },314+ }315+ const response = await handler(event, context)316+ expect(response.statusCode).toBe(200)317+ expect(JSON.parse(response.body).callbackWaitsForEmptyEventLoop).toEqual(false)318+ })319 })modified:src/adapter/aws-lambda/handler.ts+7 added-2 removed
3 unmodified lines45678910111249 unmodified lines626364656566676869707170727374757677787995 unmodified lines3 unmodified linesimport type { Env, Schema } from '../../types'
import { encodeBase64 } from '../../utils/encode'import type { ApiGatewayRequestContext, LambdaFunctionUrlRequestContext } from './custom-context'import type { LambdaContext } from './types'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment// @ts-ignoreglobalThis.crypto ??= crypto49 unmodified linesexport const handle = <E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>( app: Hono<E, S, BasePath>) => { return async ( event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent, lambdaContext?: LambdaContext ): Promise<APIGatewayProxyResult> => { const req = createRequest(event) const requestContext = getRequestContext(event)
const res = await app.fetch(req, { requestContext }) const res = await app.fetch(req, { requestContext, lambdaContext, })
return createResult(res) }}95 unmodified linesmodified:src/adapter/aws-lambda/index.ts+1 added
1234// @denoify-ignoreexport { handle } from './handler'export type { ApiGatewayRequestContext, LambdaFunctionUrlRequestContext } from './custom-context'export type { LambdaContext } from './types'added:src/adapter/aws-lambda/types.ts+47 added
1+// @denoify-ignore2+export interface CognitoIdentity {3+ cognitoIdentityId: string4+ cognitoIdentityPoolId: string5+}6+7+export interface ClientContext {8+ client: ClientContextClient9+ // eslint-disable-next-line @typescript-eslint/no-explicit-any10+ Custom?: any11+ env: ClientContextEnv12+}13+14+export interface ClientContextClient {15+ installationId: string16+ appTitle: string17+ appVersionName: string18+ appVersionCode: string19+ appPackageName: string20+}21+22+export interface ClientContextEnv {23+ platformVersion: string24+ platform: string25+ make: string26+ model: string27+ locale: string28+}29+30+/**31+ * {@link Handler} context parameter.32+ * See {@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html AWS documentation}.33+ */34+export interface LambdaContext {35+ callbackWaitsForEmptyEventLoop: boolean36+ functionName: string37+ functionVersion: string38+ invokedFunctionArn: string39+ memoryLimitInMB: string40+ awsRequestId: string41+ logGroupName: string42+ logStreamName: string43+ identity?: CognitoIdentity | undefined44+ clientContext?: ClientContext | undefined45+46+ getRemainingTimeInMillis(): number47+}