feat: support direct AWS SDK Invoke requests (#106)#331
Open
naorpeled wants to merge 1 commit into
Open
Conversation
Add an opt-in `directInvoke` option so Lambda API can process requests
that bypass API Gateway/ALB and are invoked directly via the AWS SDK
`Invoke` API (both RequestResponse and Event invocation types).
When `directInvoke: true`, an event without a `requestContext` is
detected as the new `lambda` interface and routed normally. Instead of
the API Gateway proxy envelope (whose stringified `body` forces callers
to double-parse), a direct invocation returns an unwrapped
`{ statusCode, body }` payload — the parsed value plus the status code,
which is the only success/failure signal since Lambda API never throws.
Binary responses keep `isBase64Encoded: true` with the raw base64 body.
The option defaults to `false`, so API Gateway/ALB behavior (which always
carry a `requestContext`) is unchanged and one handler serves all three.
- src/index.js: parse `directInvoke` config
- src/lib/request.js: gate `lambda` interface detection
- src/lib/response.js: unwrapped response for the `lambda` interface
- index.d.ts: add `Options.directInvoke` and `Request.interface`
- __tests__: Lambda Direct Invoke suite + fixture (shape, routing,
errors, base64, HEAD, backward-compat and dual-mode guards)
- README.md: document the option, interface value, and new section
Closes jeremydaly#106
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #106. Adds opt-in support for processing requests that bypass API Gateway/ALB and are invoked directly via the AWS SDK
InvokeAPI — bothRequestResponseandEventinvocation types.Enable it at init:
With the flag on, an event that has no
requestContextis detected as the newlambdainterface and routed normally. Direct invocations return an unwrapped response instead of the API Gateway proxy envelope:Why unwrapped instead of the proxy envelope?
The
{ statusCode, headers, body, isBase64Encoded }envelope is specifically the API Gateway/ALB/Function-URL proxy contract, not a generalInvokecontract — returning it forces a direct caller toJSON.parsetwice (thePayload, then the stringifiedbody) and ships meaninglessheaders/isBase64Encoded. The unwrapped{ statusCode, body }gives single-parse ergonomics while preservingstatusCode, which is the only success/failure signal available since Lambda API never throws out ofrun(). Binary responses keepisBase64Encoded: truewith the raw base64body.Backward compatibility
directInvokedefaults tofalse, so nothing changes by default. API Gateway and ALB events always carry arequestContext, so they keep the normal envelope even with the flag on — one handler transparently serves all three sources. Tests include explicit backward-compat and dual-mode guards.Changes
src/index.js— parse thedirectInvokeconfig optionsrc/lib/request.js— gatelambdainterface detection (only whendirectInvokeis on and norequestContext)src/lib/response.js— return the unwrapped{ statusCode, body }payload for thelambdainterfaceindex.d.ts— addOptions.directInvokeandRequest.interface__tests__/— newLambda Direct Invokesuite (unwrapped shape, routing, unwrapped errors, base64, HEAD, backward-compat + dual-mode guards) and asample-event-lambda1.jsonfixtureREADME.md— document the option, thelambdainterface value, and a new Direct Lambda Invocations sectionNotes
Event(async) invocation type needs no special handling — AWS discards the function's return value, so behavior is identical toRequestResponsefrom the framework's side.req.ip/clientType/clientCountryfall back to their defaults for direct invocations.Testing
npm testgreen: build (CJS/ESM/types) +tsd+jest unit(492 passed) +jest module-compat.eslintandprettier --checkclean.