serverless-plugin-warmup 6.2.0 → 7.0.0

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/README.md CHANGED
@@ -2,14 +2,15 @@
2
2
  [![Serverless][serverless-badge]](serverless-badge-url)
3
3
  [![npm version][npm-version-badge]][npm-version-badge-url]
4
4
  [![npm monthly downloads][npm-downloads-badge]][npm-version-badge-url]
5
- [![Build Status][travis-badge]][travis-badge-url]
5
+ [![Node.js CI](https://github.com/juanjoDiaz/serverless-plugin-warmup/actions/workflows/on-push.yaml/badge.svg)](https://github.com/juanjoDiaz/serverless-plugin-warmup/actions/workflows/on-push.yaml)
6
6
  [![Coverage Status][coveralls-badge]][coveralls-badge-url]
7
7
  [![license](https://img.shields.io/npm/l/serverless-plugin-warmup.svg)](https://raw.githubusercontent.com/juanjoDiaz/serverless-plugin-warmup/master/LICENSE)
8
8
 
9
9
  Keep your lambdas warm during winter.
10
10
 
11
11
  **Requirements:**
12
- * Serverless *v2.32.x* or higher
12
+ * Node *v14.x* or higher
13
+ * Serverless *v3.x* or higher
13
14
  * AWS provider
14
15
 
15
16
  ## How it works
@@ -603,8 +604,6 @@ This software is released under the MIT license. See [the license file](LICENSE)
603
604
  [npm-version-badge]: https://badge.fury.io/js/serverless-plugin-warmup.svg
604
605
  [npm-version-badge-url]: https://www.npmjs.com/package/serverless-plugin-warmup
605
606
  [npm-downloads-badge]: https://img.shields.io/npm/dm/serverless-plugin-warmup.svg
606
- [travis-badge]: https://travis-ci.org/juanjoDiaz/serverless-plugin-warmup.svg
607
- [travis-badge-url]: https://travis-ci.org/juanjoDiaz/serverless-plugin-warmup
608
607
  [coveralls-badge]: https://coveralls.io/repos/juanjoDiaz/serverless-plugin-warmup/badge.svg?branch=master
609
608
  [coveralls-badge-url]: https://coveralls.io/r/juanjoDiaz/serverless-plugin-warmup?branch=master
610
609
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serverless-plugin-warmup",
3
- "version": "6.2.0",
3
+ "version": "7.0.0",
4
4
  "description": "Keep your lambdas warm during winter.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/config.js CHANGED
@@ -82,7 +82,7 @@ function getFunctionConfig(config, defaultOpts) {
82
82
  *
83
83
  * @return {Array} - List of functions to be warmed up and their specific configs
84
84
  * */
85
- function getFunctionsByWarmer(service, stage, configsByWarmer) {
85
+ function getFunctionsByWarmer(service, stage, configsByWarmer, serverlessClasses) {
86
86
  const functions = service.getAllFunctions()
87
87
  .map((name) => service.getFunction(name))
88
88
  .map((config) => {
@@ -100,7 +100,7 @@ function getFunctionsByWarmer(service, stage, configsByWarmer) {
100
100
  const unknownWarmers = Object.keys(config.warmup)
101
101
  .filter((warmerName) => configsByWarmer[warmerName] === undefined);
102
102
  if (unknownWarmers.length > 0) {
103
- throw new Error(`WarmUp: Invalid function-level warmup configuration (${unknownWarmers.join(', ')}) in function ${config.name}. Every warmer should be declared in the custom section.`);
103
+ throw new serverlessClasses.Error(`WarmUp: Invalid function-level warmup configuration (${unknownWarmers.join(', ')}) in function ${config.name}. Every warmer should be declared in the custom section.`);
104
104
  }
105
105
 
106
106
  return {
@@ -137,7 +137,7 @@ function getFunctionsByWarmer(service, stage, configsByWarmer) {
137
137
  *
138
138
  * @return {Object} - Configuration options to be used by the plugin
139
139
  * */
140
- function getConfigsByWarmer(service, stage) {
140
+ function getConfigsByWarmer({ service, classes }, stage) {
141
141
  const getWarmerDefaultOpts = (warmerName) => ({
142
142
  folderName: path.join('.warmup', warmerName),
143
143
  cleanFolder: true,
@@ -170,7 +170,7 @@ function getConfigsByWarmer(service, stage) {
170
170
  },
171
171
  }), {});
172
172
 
173
- const functionsByWarmer = getFunctionsByWarmer(service, stage, configsByWarmer);
173
+ const functionsByWarmer = getFunctionsByWarmer(service, stage, configsByWarmer, classes);
174
174
 
175
175
  return Object.entries(configsByWarmer).reduce((warmers, [warmerName, warmerConfig]) => ({
176
176
  ...warmers,
package/src/index.js CHANGED
@@ -28,12 +28,13 @@ class WarmUp {
28
28
  * @constructor
29
29
  *
30
30
  * @param {!Object} serverless - Serverless object
31
- * @param {!Object} options - Serverless options
31
+ * @param {!Object} cliOptions - Serverless cliOptions
32
32
  * */
33
- constructor(serverless, options) {
33
+ constructor(serverless, cliOptions, { log }) {
34
34
  /** Serverless variables */
35
35
  this.serverless = serverless;
36
- this.options = options;
36
+ this.cliOptions = cliOptions;
37
+ this.log = log;
37
38
 
38
39
  this.provider = this.serverless.getProvider('aws');
39
40
 
@@ -46,7 +47,7 @@ class WarmUp {
46
47
  cleanupTempDir: { lifecycleEvents: ['cleanup'] },
47
48
  prewarm: {
48
49
  lifecycleEvents: ['start', 'end'],
49
- options: {
50
+ cliOptions: {
50
51
  warmers: {
51
52
  shortcut: 'w',
52
53
  usage: 'Comma-separated list of warmer names to prewarm.',
@@ -85,7 +86,7 @@ class WarmUp {
85
86
  configPlugin() {
86
87
  this.stage = this.stage || this.provider.getStage();
87
88
  this.configsByWarmer = this.configsByWarmer
88
- || getConfigsByWarmer(this.serverless.service, this.stage);
89
+ || getConfigsByWarmer(this.serverless, this.stage);
89
90
  }
90
91
 
91
92
  /**
@@ -122,7 +123,7 @@ class WarmUp {
122
123
  );
123
124
  } catch (err) {
124
125
  if (err.code !== 'ENOENT') {
125
- this.serverless.cli.log(`WarmUp: Couldn't clean up temporary folder ${folderToClean}.`);
126
+ this.log.error(`WarmUp: Couldn't clean up temporary folder ${folderToClean}.`);
126
127
  }
127
128
  }
128
129
  }));
@@ -137,7 +138,7 @@ class WarmUp {
137
138
  }
138
139
  } catch (err) {
139
140
  if (err.code !== 'ENOENT') {
140
- this.serverless.cli.log('WarmUp: Couldn\'t clean up temporary folder .warmup.');
141
+ this.log.error('WarmUp: Couldn\'t clean up temporary folder .warmup.');
141
142
  }
142
143
  }
143
144
  }
@@ -151,8 +152,8 @@ class WarmUp {
151
152
  * @return {Promise}
152
153
  * */
153
154
  async prewarmFunctions() {
154
- const warmerNames = (this.options.warmers)
155
- ? this.options.warmers.split(',')
155
+ const warmerNames = (this.cliOptions.warmers)
156
+ ? this.cliOptions.warmers.split(',')
156
157
  : Object.entries(this.configsByWarmer)
157
158
  .filter(([, warmerConfig]) => warmerConfig.prewarm)
158
159
  .map(([warmerName]) => warmerName);
@@ -160,7 +161,7 @@ class WarmUp {
160
161
  await Promise.all(warmerNames.map(async (warmerName) => {
161
162
  const warmerConfig = this.configsByWarmer[warmerName];
162
163
  if (!warmerConfig) {
163
- throw new Error(`Warmer names ${warmerName} doesn't exist.`);
164
+ throw new this.serverless.classes.Error(`Warmer names ${warmerName} doesn't exist.`);
164
165
  }
165
166
  addWarmUpFunctionToService(this.serverless.service, warmerName, warmerConfig);
166
167
  await this.invokeWarmer(warmerName, warmerConfig);
@@ -173,7 +174,7 @@ class WarmUp {
173
174
  * */
174
175
  async configureWarmer(warmerName, warmerConfig) {
175
176
  if (warmerConfig.functions.length === 0) {
176
- this.serverless.cli.log(`WarmUp: Skipping warmer "${warmerName}" creation. No functions to warm up.`);
177
+ this.log.warning(`WarmUp: Skipping warmer "${warmerName}" creation. No functions to warm up.`);
177
178
  return;
178
179
  }
179
180
 
@@ -183,8 +184,9 @@ class WarmUp {
183
184
  return;
184
185
  }
185
186
 
186
- this.serverless.cli.log(`WarmUp: Creating warmer "${warmerName}" to warm up ${warmerConfig.functions.length} function${warmerConfig.functions.length === 1 ? '' : 's'}:`);
187
- warmerConfig.functions.forEach((func) => this.serverless.cli.log(` * ${func.name}`));
187
+ this.log.notice(`WarmUp: Creating warmer "${warmerName}" to warm up ${warmerConfig.functions.length} function${warmerConfig.functions.length === 1 ? '' : 's'}`);
188
+ this.log.info(':');
189
+ warmerConfig.functions.forEach((func) => this.log.info(` * ${func.name}`));
188
190
 
189
191
  const handlerFolder = path.join(this.serviceDir, warmerConfig.folderName);
190
192
 
@@ -209,11 +211,11 @@ class WarmUp {
209
211
 
210
212
  async invokeWarmer(warmerName, warmerConfig) {
211
213
  if (warmerConfig.functions.length === 0) {
212
- this.serverless.cli.log(`WarmUp: Skipping prewarming using warmer "${warmerName}". No functions to warm up.`);
214
+ this.log.warning(`WarmUp: Skipping prewarming using warmer "${warmerName}". No functions to warm up.`);
213
215
  return;
214
216
  }
215
217
 
216
- this.serverless.cli.log(`WarmUp: Prewarming up your functions using warmer "${warmerName}".`);
218
+ this.log.notice(`WarmUp: Prewarming up your functions using warmer "${warmerName}".`);
217
219
 
218
220
  try {
219
221
  const { SERVERLESS_ALIAS } = this.serverless.service.getFunction(`warmUpPlugin${capitalize(warmerName)}`).environment || {};
@@ -226,9 +228,9 @@ class WarmUp {
226
228
  };
227
229
 
228
230
  await this.provider.request('Lambda', 'invoke', params);
229
- this.serverless.cli.log(`WarmUp: Warmer "${warmerName}" successfully prewarmed your functions.`);
231
+ this.log.notice(`WarmUp: Warmer "${warmerName}" successfully prewarmed your functions.`);
230
232
  } catch (err) {
231
- this.serverless.cli.log(`WarmUp: Error while prewarming your functions using warmer "${warmerName}".`, err);
233
+ this.log.error(`WarmUp: Error while prewarming your functions using warmer "${warmerName}".`, err);
232
234
  }
233
235
  }
234
236
  }
package/src/schema.js CHANGED
@@ -139,6 +139,7 @@ function extendServerlessSchema(serverless) {
139
139
  serverless.configSchemaHandler.defineCustomProperties({
140
140
  properties: {
141
141
  warmup: {
142
+ type: 'object',
142
143
  patternProperties: {
143
144
  '.*': {
144
145
  type: 'object',
@@ -156,6 +157,7 @@ function extendServerlessSchema(serverless) {
156
157
  type: 'object',
157
158
  properties: {
158
159
  warmup: {
160
+ type: 'object',
159
161
  patternProperties: {
160
162
  '.*': {
161
163
  type: 'object',