lambda-request-handler
Advanced tools
Comparing version 0.0.3 to 0.1.0
/// <reference types="node" /> | ||
import { RequestListener } from 'http'; | ||
import { APIGatewayEvent } from './apiGateway'; | ||
declare const handler: (app: RequestListener) => (event: APIGatewayEvent) => Promise<import("./apiGateway").APIGatewayResponse>; | ||
import * as apigw from './apiGateway'; | ||
declare namespace handler { | ||
type APIGatewayEvent = apigw.APIGatewayEvent; | ||
type APIGatewayResponse = apigw.APIGatewayResponse; | ||
type APIGatewayEventHandler = (event: handler.APIGatewayEvent) => Promise<handler.APIGatewayResponse>; | ||
} | ||
declare const handler: { | ||
(app: RequestListener): handler.APIGatewayEventHandler; | ||
deferred: (appPromiseFn: () => Promise<RequestListener>) => handler.APIGatewayEventHandler; | ||
}; | ||
export = handler; |
@@ -8,7 +8,15 @@ "use strict"; | ||
const response_1 = require("./response"); | ||
const handler = (app) => { | ||
const appHandler = in_process_request_1.default(app); | ||
return (event) => { | ||
const reqOptions = eventToRequestOptions_1.default(event); | ||
return appHandler(reqOptions) | ||
; | ||
const handlerPromise = (appPromiseFn) => { | ||
let _p = null; | ||
return event => { | ||
if (!_p) { | ||
_p = appPromiseFn(); | ||
} | ||
return _p | ||
.then(app => { | ||
const reqOptions = eventToRequestOptions_1.default(event); | ||
const appHandler = in_process_request_1.default(app); | ||
return appHandler(reqOptions); | ||
}) | ||
.then(response_1.inProcessResponseToApiGatewayResponse) | ||
@@ -21,2 +29,4 @@ .catch(e => { | ||
}; | ||
const handler = (app) => handlerPromise(() => Promise.resolve(app)); | ||
handler.deferred = handlerPromise; | ||
module.exports = handler; |
{ | ||
"name": "lambda-request-handler", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"license": "(MIT OR Apache-2.0)", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -13,3 +13,2 @@ # lambda-request-handler | ||
Inspired by [aws-serverless-express](https://github.com/awslabs/aws-serverless-express) | ||
@@ -22,2 +21,3 @@ | ||
* Simpler setup as it doesn't require managing the internal http server | ||
* Support for applications that require asynchronous setup (for example reading config from network, or decrypting secrets from KMS) | ||
* It's faster, because it doesn't need to pass the request to the internal server through the unix socket | ||
@@ -54,5 +54,5 @@ * It's free from issues caused by limits in Node.js http module such as header size limit | ||
### Advanced example | ||
### Advanced example with asynchronous setup | ||
Sometimes the application needs to read configuration from remote source before it can start processing requests. For example it may need to decrypt some secrets managed by KMS. | ||
Sometimes the application needs to read configuration from remote source before it can start processing requests. For example it may need to decrypt some secrets managed by KMS. For this use case a special helper `deferred` has been provided. It takes a factory function which returns a Promise that resolves to the app instance. The factory function will be called only once. | ||
@@ -64,35 +64,24 @@ ```javascript | ||
const kms = new AWS.KMS() | ||
const createApp = (secret) => { | ||
const app = express(); | ||
app.get('/secret', (req, res) => { | ||
res.json({ | ||
secret: secret, | ||
}) | ||
}) | ||
} | ||
const myKmsPromise = async () => { | ||
const myAppPromise = async () => { | ||
const kms = new AWS.KMS() | ||
const data = await kms.decrypt({ | ||
CiphertextBlob: Buffer.from(process.env.ENCRYPTED_SECRET, 'base64') | ||
}).promise() | ||
process.env.SECRET = data.Plaintext.toString('ascii') | ||
const secret = data.Plaintext.toString('ascii') | ||
return createApp(secret); | ||
}; | ||
const app = express() | ||
const handler = lambdaRequestHandler.deferred(myAppPromise); | ||
app.get('/secret', (req, res) => { | ||
res.json({ | ||
secret: process.env.SECRET, | ||
}) | ||
}) | ||
const myAppHandler = lambdaRequestHandler(app) | ||
let _myKmsPromise; | ||
const handler = async (event) => { | ||
if (!_myKmsPromise) { | ||
// _myKmsPromise is in global scope so that only one request to KMS is made during this Lambda lifecycle | ||
_myKmsPromise = myKmsPromise(); | ||
} | ||
await _myKmsPromise; | ||
// at this point the secret is decrypted and available as process.env.SECRET to the app | ||
return myAppHandler(event) | ||
} | ||
module.exports = { handler } | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9364
153
84