@middy/warmup 1.5.2 → 2.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +2 -9
- package/index.d.ts +4 -5
- package/index.js +19 -19
- package/package.json +14 -12
- package/.babelrc +0 -9
- package/__tests__/index.js +0 -138
- package/tsconfig.json +0 -11
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017-2021 Luciano Mammino and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
3
|
+
Copyright (c) 2017-2021 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -54,9 +54,6 @@ npm install --save @middy/warmup
|
|
|
54
54
|
|
|
55
55
|
- `isWarmingUp`: a function that accepts the `event` object as a parameter
|
|
56
56
|
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`.
|
|
57
|
-
- `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`.
|
|
58
|
-
- `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).
|
|
59
|
-
|
|
60
57
|
|
|
61
58
|
## Sample usage
|
|
62
59
|
|
|
@@ -65,17 +62,13 @@ const middy = require('@middy/core')
|
|
|
65
62
|
const warmup = require('@middy/warmup')
|
|
66
63
|
|
|
67
64
|
const isWarmingUp = (event) => event.isWarmingUp === true
|
|
68
|
-
const onWarmup = (event) => console.log('I am just warming up', event)
|
|
69
65
|
|
|
70
66
|
const originalHandler = (event, context, cb) => {
|
|
71
67
|
/* ... */
|
|
72
68
|
}
|
|
73
69
|
|
|
74
70
|
const handler = middy(originalHandler)
|
|
75
|
-
.use(warmup({
|
|
76
|
-
isWarmingUp,
|
|
77
|
-
onWarmup
|
|
78
|
-
}))
|
|
71
|
+
.use(warmup({ isWarmingUp }))
|
|
79
72
|
```
|
|
80
73
|
|
|
81
74
|
|
|
@@ -91,7 +84,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
|
|
|
91
84
|
|
|
92
85
|
## License
|
|
93
86
|
|
|
94
|
-
Licensed under [MIT License](LICENSE). Copyright (c) 2017-
|
|
87
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2021 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
95
88
|
|
|
96
89
|
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
97
90
|
<img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
|
package/index.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import middy from '@middy/core'
|
|
2
2
|
|
|
3
|
-
interface
|
|
4
|
-
isWarmingUp?: (event: any) => boolean
|
|
5
|
-
onWarmup?: (event: any) => void
|
|
6
|
-
waitForEmptyEventLoop?: boolean;
|
|
3
|
+
interface Options {
|
|
4
|
+
isWarmingUp?: (event: any) => boolean
|
|
5
|
+
onWarmup?: (event: any) => void
|
|
7
6
|
}
|
|
8
7
|
|
|
9
|
-
declare
|
|
8
|
+
declare function warmup (options?: Options): middy.MiddlewareObj
|
|
10
9
|
|
|
11
10
|
export default warmup
|
package/index.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
const defaults = {
|
|
3
|
-
isWarmingUp: (event) => event.source === 'serverless-plugin-warmup',
|
|
4
|
-
onWarmup: (event) => console.log('Exiting early via warmup Middleware'),
|
|
5
|
-
waitForEmptyEventLoop: null
|
|
6
|
-
}
|
|
1
|
+
"use strict";
|
|
7
2
|
|
|
8
|
-
|
|
3
|
+
const defaults = {
|
|
4
|
+
isWarmingUp: event => (event === null || event === void 0 ? void 0 : event.source) === 'serverless-plugin-warmup'
|
|
5
|
+
};
|
|
9
6
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (options.waitForEmptyEventLoop !== null) {
|
|
15
|
-
handler.context.callbackWaitsForEmptyEventLoop = Boolean(options.waitForEmptyEventLoop)
|
|
16
|
-
}
|
|
17
|
-
return handler.callback(null, 'warmup')
|
|
18
|
-
}
|
|
7
|
+
const warmupMiddleware = opt => {
|
|
8
|
+
const options = { ...defaults,
|
|
9
|
+
...opt
|
|
10
|
+
};
|
|
19
11
|
|
|
20
|
-
|
|
12
|
+
const warmupMiddlewareBefore = request => {
|
|
13
|
+
if (options.isWarmingUp(request.event)) {
|
|
14
|
+
return 'warmup';
|
|
21
15
|
}
|
|
22
|
-
}
|
|
23
|
-
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
before: warmupMiddlewareBefore
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
module.exports = warmupMiddleware;
|
package/package.json
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/warmup",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.5.3",
|
|
4
4
|
"description": "Warmup (cold start mitigation) middleware for the middy framework",
|
|
5
|
+
"type": "commonjs",
|
|
5
6
|
"engines": {
|
|
6
|
-
"node": ">=
|
|
7
|
+
"node": ">=12"
|
|
7
8
|
},
|
|
8
9
|
"engineStrict": true,
|
|
9
10
|
"publishConfig": {
|
|
10
11
|
"access": "public"
|
|
11
12
|
},
|
|
13
|
+
"main": "index.js",
|
|
14
|
+
"types": "index.d.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"index.d.ts"
|
|
17
|
+
],
|
|
12
18
|
"scripts": {
|
|
13
|
-
"test": "npm run test:
|
|
14
|
-
"test:unit": "
|
|
15
|
-
"test:typings": "typings-tester --config tsconfig.json index.d.ts"
|
|
19
|
+
"test": "npm run test:unit",
|
|
20
|
+
"test:unit": "ava"
|
|
16
21
|
},
|
|
17
22
|
"license": "MIT",
|
|
18
23
|
"keywords": [
|
|
@@ -33,18 +38,15 @@
|
|
|
33
38
|
},
|
|
34
39
|
"repository": {
|
|
35
40
|
"type": "git",
|
|
36
|
-
"url": "
|
|
41
|
+
"url": "github:middyjs/middy",
|
|
42
|
+
"directory": "packages/warmup"
|
|
37
43
|
},
|
|
38
44
|
"bugs": {
|
|
39
45
|
"url": "https://github.com/middyjs/middy/issues"
|
|
40
46
|
},
|
|
41
47
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
42
|
-
"peerDependencies": {
|
|
43
|
-
"@middy/core": ">=1.0.0-alpha"
|
|
44
|
-
},
|
|
45
48
|
"devDependencies": {
|
|
46
|
-
"@middy/core": "^
|
|
47
|
-
"es6-promisify": "^6.0.2"
|
|
49
|
+
"@middy/core": "^2.5.3"
|
|
48
50
|
},
|
|
49
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "690884d43b9cd632aeca9a5eba1612160b987cd4"
|
|
50
52
|
}
|
package/.babelrc
DELETED
package/__tests__/index.js
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
const { invoke } = require('../../test-helpers')
|
|
2
|
-
const middy = require('../../core')
|
|
3
|
-
const lambdaIsWarmingUp = require('../')
|
|
4
|
-
|
|
5
|
-
console.log = jest.fn()
|
|
6
|
-
|
|
7
|
-
beforeEach(() => {
|
|
8
|
-
console.log.mockClear()
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
describe('🥃 Warmup', () => {
|
|
12
|
-
test('Should exit with \'warmup\' if provided warmup check function is provide and returns true', async () => {
|
|
13
|
-
const handler = middy((event, context, cb) => {
|
|
14
|
-
cb()
|
|
15
|
-
})
|
|
16
|
-
handler.use(lambdaIsWarmingUp({
|
|
17
|
-
isWarmingUp: (event) => true,
|
|
18
|
-
onWarmup: () => {}
|
|
19
|
-
}))
|
|
20
|
-
|
|
21
|
-
const event = {}
|
|
22
|
-
const context = {}
|
|
23
|
-
|
|
24
|
-
const response = await invoke(handler, event, context)
|
|
25
|
-
|
|
26
|
-
expect(response).toBe('warmup')
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
test('Should exit with \'warmup\' if event.source === \'serverless-plugin-warmup\' if no warmup check function provided', async () => {
|
|
30
|
-
const handler = middy((event, context, cb) => {
|
|
31
|
-
cb()
|
|
32
|
-
})
|
|
33
|
-
handler.use(lambdaIsWarmingUp({
|
|
34
|
-
onWarmup: () => {}
|
|
35
|
-
}))
|
|
36
|
-
|
|
37
|
-
const event = {
|
|
38
|
-
source: 'serverless-plugin-warmup'
|
|
39
|
-
}
|
|
40
|
-
const context = {}
|
|
41
|
-
|
|
42
|
-
const response = await invoke(handler, event, context)
|
|
43
|
-
|
|
44
|
-
expect(response).toBe('warmup')
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
test('It should print in the console when exiting because of warmup and the onWarmup function is not redefined', async () => {
|
|
48
|
-
const logSpy = jest.spyOn(console, 'log')
|
|
49
|
-
|
|
50
|
-
const handler = middy((event, context, cb) => {
|
|
51
|
-
cb()
|
|
52
|
-
})
|
|
53
|
-
handler.use(lambdaIsWarmingUp({}))
|
|
54
|
-
|
|
55
|
-
const event = {
|
|
56
|
-
source: 'serverless-plugin-warmup'
|
|
57
|
-
}
|
|
58
|
-
const context = {}
|
|
59
|
-
|
|
60
|
-
const response = await invoke(handler, event, context)
|
|
61
|
-
|
|
62
|
-
expect(response).toBe('warmup')
|
|
63
|
-
expect(logSpy).toHaveBeenCalled()
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
test('Should execute handler if provided warmup check function returns false', async () => {
|
|
67
|
-
const handler = middy((event, context, cb) => {
|
|
68
|
-
cb(null, 'handler executed')
|
|
69
|
-
})
|
|
70
|
-
handler.use(lambdaIsWarmingUp({ isWarmingUp: () => false }))
|
|
71
|
-
|
|
72
|
-
const event = {}
|
|
73
|
-
const context = {}
|
|
74
|
-
|
|
75
|
-
const response = await invoke(handler, event, context)
|
|
76
|
-
|
|
77
|
-
expect(response).toBe('handler executed')
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
test('Should execute handler with callbackWaitsForEmptyEventLoop if waitForEmptyEventLoop true', async () => {
|
|
81
|
-
const handler = middy((event, context, cb) => {
|
|
82
|
-
cb()
|
|
83
|
-
})
|
|
84
|
-
handler.use(lambdaIsWarmingUp({
|
|
85
|
-
waitForEmptyEventLoop: true
|
|
86
|
-
}))
|
|
87
|
-
|
|
88
|
-
const event = {
|
|
89
|
-
source: 'serverless-plugin-warmup'
|
|
90
|
-
}
|
|
91
|
-
const context = {}
|
|
92
|
-
|
|
93
|
-
const response = await invoke(handler, event, context)
|
|
94
|
-
|
|
95
|
-
expect(context.callbackWaitsForEmptyEventLoop).toBe(true)
|
|
96
|
-
expect(response).toBe('warmup')
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
test('Should execute handler with callbackWaitsForEmptyEventLoop if waitForEmptyEventLoop false', async () => {
|
|
100
|
-
const handler = middy((event, context, cb) => {
|
|
101
|
-
cb()
|
|
102
|
-
})
|
|
103
|
-
handler.use(lambdaIsWarmingUp({
|
|
104
|
-
waitForEmptyEventLoop: false
|
|
105
|
-
}))
|
|
106
|
-
|
|
107
|
-
const event = {
|
|
108
|
-
source: 'serverless-plugin-warmup'
|
|
109
|
-
}
|
|
110
|
-
const context = {
|
|
111
|
-
callbackWaitsForEmptyEventLoop: true
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const response = await invoke(handler, event, context)
|
|
115
|
-
|
|
116
|
-
expect(context.callbackWaitsForEmptyEventLoop).toBe(false)
|
|
117
|
-
expect(response).toBe('warmup')
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
test('Should execute handler with callbackWaitsForEmptyEventLoop unchanged if waitForEmptyEventLoop is not set', async () => {
|
|
121
|
-
const handler = middy((event, context, cb) => {
|
|
122
|
-
cb()
|
|
123
|
-
})
|
|
124
|
-
handler.use(lambdaIsWarmingUp({}))
|
|
125
|
-
|
|
126
|
-
const event = {
|
|
127
|
-
source: 'serverless-plugin-warmup'
|
|
128
|
-
}
|
|
129
|
-
const context = {
|
|
130
|
-
callbackWaitsForEmptyEventLoop: true
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const response = await invoke(handler, event, context)
|
|
134
|
-
|
|
135
|
-
expect(context.callbackWaitsForEmptyEventLoop).toBe(true)
|
|
136
|
-
expect(response).toBe('warmup')
|
|
137
|
-
})
|
|
138
|
-
})
|