forked from maasglobal/serverless-wrapper-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
216 lines (174 loc) · 7.63 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
'use strict';
const path = require('path');
const fs = require('fs-extra');
const codeTemplate = 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.preAction.bind(this), {
action: 'codeDeployLambda',
event: 'pre',
});
S.addHook(this.postAction.bind(this), {
action: 'codeDeployLambda',
event: 'post',
});
S.addHook(this.preAction.bind(this), {
action: 'functionRun',
event: 'pre',
});
S.addHook(this.postAction.bind(this), {
action: 'functionRun',
event: 'post',
});
return Promise.resolve();
}
// Pre event
// ------------------------------------------------------------------------
/**
* Intercept the codeDeployLammbda hook and wrap the serverless handler
* function in the configured wrapper function.
*/
preAction(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);
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);
}
// 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
*/
postAction(evt) {
// Get function
const project = S.getProject();
const func = project.getFunction(evt.options.name);
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 isLocalRun = !evt.options.pathDist;
const savedHandlerPath = this._getSavedHandlerPath(func, pathSource);
const action = isLocalRun
? fs.move(
savedHandlerPath,
this._getServerlessHandlerPath(func.handler, pathSource),
{ overwrite: true }
)
: fs.remove(savedHandlerPath);
return action.then(() => 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;
const isLocalRun = !pathDist;
// Get the name of the handler function (within the handler module)
const handler = func.handler;
const handlerFunction = handler.split('.').pop();
// Information about the serverless framework version of the handler
// [NOTE: the version in the package directory (pathDist)]
const serverlessHandlerPath = this._getServerlessHandlerPath(
isLocalRun ? handler : func.getHandler(),
isLocalRun ? pathSource : 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.pathExists(savedHandlerPath).then(tmpHandlerExists => {
if (isLocalRun && tmpHandlerExists) {
throw new Error(
`Cannot wrap lambda: Temporary wrapper file found at ${savedHandlerPath}\n` +
'It\'s likely that previous run crashed, and left broken state.\n' +
'If it\'s the case, then replacing lambda handler file with content of ' +
`${savedHandlerFilename}, and removing the ${savedHandlerFilename} should fix the issue.`
);
}
return fs.move(serverlessHandlerPath, savedHandlerPath, { overwrite: true })
.then(() => {
// 2. Generate wrapped handler code
return codeTemplate({
orig_handler_path: `./${savedHandlerFilename}`,
wrapper_path: relativeWrapperPath.replace(/\\/g, '/'), // Support Windows env
handler_name: handlerFunction,
});
})
.then(code => {
// 3. Write code to wrapped handler
return fs.outputFile(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(serverlessHandler, targetDir) {
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;
};