Skip to content

Commit

Permalink
Port waitForEmptyEventLoop in Warmup middlware (middyjs#345) to v.1.0…
Browse files Browse the repository at this point in the history
….0 branch (middyjs#354)

* Port waitForEmptyEventLoop in Warmup middlware (middyjs#345) to v.1.0.0 branch

* version bump
  • Loading branch information
lmammino authored and benjifs committed May 21, 2020
1 parent 8dac7e5 commit 63b8de8
Show file tree
Hide file tree
Showing 26 changed files with 90 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"packages": [
"packages/*"
],
"version": "1.0.0-alpha.33"
"version": "1.0.0-alpha.34"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "middy-monorepo",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "🛵 The stylish Node.js middleware engine for AWS Lambda",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/cache/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/cache",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Cache middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/core",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "🛵 The stylish Node.js middleware engine for AWS Lambda (core package)",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/do-not-wait-for-empty-event-loop/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/do-not-wait-for-empty-event-loop",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Middleware for the middy framework that allows to easily disable the wait for empty event loop in a Lambda function",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/error-logger/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/error-logger",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Input and output logger middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/function-shield/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/function-shield",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Hardens AWS Lambda execution environment",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-content-negotiation/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-content-negotiation",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Http content negotiation middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-cors/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-cors",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "CORS (Cross-Origin Resource Sharing) middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-error-handler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-error-handler",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Http error handler middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-event-normalizer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-event-normalizer",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Http event normalizer middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-header-normalizer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-header-normalizer",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Http header normalizer middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-json-body-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-json-body-parser",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Http JSON body parser middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-partial-response/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-partial-response",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Http partial response middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-response-serializer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-response-serializer",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Http response serializer middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-security-header/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-security-header",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Applies best practice security headers to responses. It's a simplified port of HelmetJS",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/http-urlencode-body-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/http-urlencode-body-parser",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Urlencode body parser middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/input-output-logger/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/input-output-logger",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Input and output logger middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/s3-key-normalizer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/s3-key-normalizer",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "S3 key normalizer middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/secrets-manager/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/secrets-manager",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Secrets Manager middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/ssm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/ssm",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "SSM (EC2 Systems Manager) parameters middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
2 changes: 1 addition & 1 deletion packages/validator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/validator",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Validator middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down
1 change: 1 addition & 0 deletions packages/warmup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ npm install --save @middy/warmup
- `isWarmingUp`: a function that accepts the `event` object as a parameter
and returns `true` if the current event is a warmup event and `false` if it's a regular execution. The default function will check if the `event` object has a `source` property set to `serverless-plugin-warmup`.
- `onWarmup`: a function that gets executed before the handler exits in case of warmup. By default the function just prints: `Exiting early via warmup Middleware`.
- `waitForEmptyEventLoop`: a boolean value (`null` by default), that if set will change the current value for `context.callbackWaitsForEmptyEventLoop`. In some circumstances it might be useful to force this value to be `false` to make sure that the lambda quits as early as possible in case of warmup (for instance if you have created a database connection in a previous middleware, this might be hanging and keeping you lambda active until timeout).


## Sample usage
Expand Down
65 changes: 65 additions & 0 deletions packages/warmup/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,69 @@ describe('🥃 Warmup', () => {
endTest()
})
})

test(`Should execute handler with callbackWaitsForEmptyEventLoop if waitForEmptyEventLoop true`, (endTest) => {
console.log = jest.fn()

const handler = middy((event, context, cb) => {
cb()
})
handler.use(lambdaIsWarmingUp({
waitForEmptyEventLoop: true
}))

const event = {
source: 'serverless-plugin-warmup'
}
const context = {}
handler(event, context, (_, response) => {
expect(context.callbackWaitsForEmptyEventLoop).toBe(true)
expect(response).toBe('warmup')
endTest()
})
})

test(`Should execute handler with callbackWaitsForEmptyEventLoop if waitForEmptyEventLoop false`, (endTest) => {
console.log = jest.fn()

const handler = middy((event, context, cb) => {
cb()
})
handler.use(lambdaIsWarmingUp({
waitForEmptyEventLoop: false
}))

const event = {
source: 'serverless-plugin-warmup'
}
const context = {
callbackWaitsForEmptyEventLoop: true
}
handler(event, context, (_, response) => {
expect(context.callbackWaitsForEmptyEventLoop).toBe(false)
expect(response).toBe('warmup')
endTest()
})
})

test(`Should execute handler with callbackWaitsForEmptyEventLoop unchanged if waitForEmptyEventLoop is not set`, (endTest) => {
console.log = jest.fn()

const handler = middy((event, context, cb) => {
cb()
})
handler.use(lambdaIsWarmingUp({}))

const event = {
source: 'serverless-plugin-warmup'
}
const context = {
callbackWaitsForEmptyEventLoop: true
}
handler(event, context, (_, response) => {
expect(context.callbackWaitsForEmptyEventLoop).toBe(true)
expect(response).toBe('warmup')
endTest()
})
})
})
1 change: 1 addition & 0 deletions packages/warmup/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import middy from '../core'
interface IWarmupOptions {
isWarmingUp?: (event: any) => boolean;
onWarmup?: (event: any) => void;
waitForEmptyEventLoop?: boolean;
}

declare function warmup(opts?: IWarmupOptions): middy.IMiddyMiddlewareObject;
Expand Down
2 changes: 1 addition & 1 deletion packages/warmup/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@middy/warmup",
"version": "1.0.0-alpha.33",
"version": "1.0.0-alpha.34",
"description": "Warmup (cold start mitigation) middleware for the middy framework",
"engines": {
"node": ">=6.10"
Expand Down

0 comments on commit 63b8de8

Please sign in to comment.