forked from maasglobal/serverless-wrapper-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
200 lines (159 loc) · 7.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'use strict';
const Promise = require('bluebird');
const path = require('path');
const fs = Promise.promisifyAll(require('fs-extra'));
const CODE_TEMPLATE = require('./lib/wrapped_handler.js');
const SAVED_HANDLER_SUFFIX = '__orig__';
module.exports = function getPlugin(S) {
const SCli = require(S.getServerlessPath('utils/cli'));
class ServerlessWrappper extends S.classes.Plugin {
static getName() {
return `com.serverless.${ServerlessWrappper.name}`;
}
registerHooks() {
S.addHook(this.onCodeDeployPre.bind(this), {
action: 'codeDeployLambda',
event: 'pre'
});
S.addHook(this.onCodeDeployPost.bind(this), {
action: 'codeDeployLambda',
event: 'post'
});
return Promise.resolve();
}
// Pre event
// ------------------------------------------------------------------------
/**
* Intercept the codeDeployLammbda hook and wrap the serverless handler
* function in the configured wrapper function.
*/
onCodeDeployPre(evt) {
// Validate: Check Serverless version
if (parseInt(S._version.split('.')[1], 10) < 5) {
SCli.log('WARNING: This version of the Serverless Wrapper Plugin ' +
'will not work with a version of Serverless that is less or greater than than v0.5.x');
}
// Get function
const project = S.getProject();
const func = project.getFunction(evt.options.name);
if (func.runtime === 'nodejs' || func.runtime === 'nodejs4.3') {
const funcConfig = func.custom;
const projConfig = project.custom;
if (funcConfig && funcConfig.wrapper === false) {
// If wrapper.path exists and is 'false', skip wrapping
SCli.log('Wrapper skipped by function-specific config');
return Promise.resolve(evt);
} else if (funcConfig && funcConfig.wrapper && funcConfig.wrapper.path) {
SCli.log('Using function-specific wrapper file');
return this._wrapHandler(project, func, evt, funcConfig.wrapper.path);
} else if (projConfig && projConfig.wrapper && projConfig.wrapper.path) {
SCli.log('Using project wrapper file');
return this._wrapHandler(project, func, evt, projConfig.wrapper.path);
}
return Promise.resolve(evt);
}
// If we can't handle this event, just pass it through
return Promise.resolve(evt);
}
// Post event
// ------------------------------------------------------------------------
/**
* Once the codeDeployLambda operation has completed,
* clean up any intermediate files
*/
onCodeDeployPost(evt) {
// Get function
const project = S.getProject();
const func = project.getFunction(evt.options.name);
if (func.runtime === 'nodejs' || func.runtime === 'nodejs4.3') {
const funcConfig = func.custom;
const projConfig = project.custom;
// No wrapper due to function config override, skip cleanup
if (funcConfig && funcConfig.wrapper === false) {
return Promise.resolve(evt);
}
// If we do have a wrapper, we need to clean up intermediate files
else if (funcConfig && funcConfig.wrapper && funcConfig.wrapper.path ||
projConfig && projConfig.wrapper && projConfig.wrapper.path) {
const pathSource = path.dirname(func.getFilePath());
const savedHandlerPath = this._getSavedHandlerPath(func, pathSource);
// 1. Remove the saved version
return fs.unlinkAsync(savedHandlerPath)
.then(() => {
// 2. Resolve the event
return evt;
});
}
}
// If we can't handle this event, just pass it through
return Promise.resolve(evt);
}
// Helpers
// ------------------------------------------------------------------------
_wrapHandler(project, func, evt, wrapperPath) {
const pathSource = path.dirname(func.getFilePath());
const pathDist = evt.options.pathDist;
// Get the name of the handler function (within the handler module)
const handler = func.handler;
const handlerFunction = handler.split('.').shift();
// Information about the serverless framework version of the handler
// [NOTE: the version in the package directory (pathDist)]
const serverlessHandlerPath = this._getServerlessHandlerPath(func, pathDist);
// The new wrapped handler.
// This will take the place of the original serverless framework handler
const wrappedServerlessHandlerPath = serverlessHandlerPath;
// We will save the original serverless framework handler
// so that it can be included by the wrapped handler
// [NOTE: temporarily saved in the source directory]
const savedHandlerFilename = this._getSavedHandlerFilename(func);
const savedHandlerPath = this._getSavedHandlerPath(func, pathSource);
// Relative path to the wrapper function, from the serverless handler function
const rootPath = project.getRootPath();
const relativeWrapperPath =
path.relative(pathSource, path.join(rootPath, wrapperPath));
const absolutePath = path.join(pathSource, relativeWrapperPath);
// 0. Check if file exist in path
return fs.statAsync(absolutePath)
// 1. Move the original serverless framework handler to savedHandlerPath
// [NOTE: force this write if necessary]
.then(() => fs.moveAsync(serverlessHandlerPath, savedHandlerPath, { clobber: true }))
.then(() => {
// 2. Generate wrapped handler code
return CODE_TEMPLATE({
orig_handler_path: `./${savedHandlerFilename}`,
wrapper_path: relativeWrapperPath,
handler_name: handlerFunction,
});
})
.then(code => {
// 3. Write code to wrapped handler
return fs.writeFileAsync(wrappedServerlessHandlerPath, code)
})
.then(() => {
// 4. Resolve the event
SCli.log(`Wrapping ${handler} with ${absolutePath}`);
return evt;
})
}
// Information about the serverless framework version of the handler
_getServerlessHandlerPath(func, targetDir) {
const serverlessHandler = func.getHandler();
const serverlessHandlerName = serverlessHandler.split('.')[0];
const serverlessHandlerFilename = `${serverlessHandlerName}.js`;
return path.join(targetDir, serverlessHandlerFilename);
}
// Information about the serverless framework version of the handler
_getSavedHandlerFilename(func) {
const serverlessHandler = func.getHandler();
const serverlessHandlerName = serverlessHandler.split('.')[0];
// Information about the saved version of the original serverless handler
return `${serverlessHandlerName}${SAVED_HANDLER_SUFFIX}.js`;
}
// Information about the intermediary saved version of the serverless handler
_getSavedHandlerPath(func, targetDir) {
const savedHandlerFilename = this._getSavedHandlerFilename(func);
return path.join(targetDir, savedHandlerFilename);
}
}
return ServerlessWrappper;
};