forked from ar90n/serverless-s3-local
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
339 lines (301 loc) · 11.1 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
const S3rver = require('s3rver');
const fs = require('fs-extra'); // Using fs-extra to ensure destination directory exist
const AWS = require('aws-sdk');
const shell = require('shelljs');
const path = require('path');
const uuid = require('uuid/v1');
const functionHelper = require('serverless-offline/src/functionHelper');
const createLambdaContext = require('serverless-offline/src/createLambdaContext');
require("rxjs/add/operator/map");
require("rxjs/add/operator/mergeMap");
const defaultOptions = {
port: 4569,
host: 'localhost',
location: '.'
}
class ServerlessS3Local {
constructor(serverless, options) {
this.serverless = serverless;
this.service = serverless.service;
this.options = options;
this.provider = 'aws';
this.client = null;
this.commands = {
s3: {
commands: {
start: {
usage: 'Start S3 local server.',
lifecycleEvents: ['startHandler'],
options: {
port: {
shortcut: 'p',
usage:
'The port number that S3 will use to communicate with your application. If you do not specify this option, the default port is 4569',
},
directory: {
shortcut: 'd',
usage:
'The directory where S3 will store its objects. If you do not specify this option, the file will be written to the current directory.',
},
buckets: {
shortcut: 'b',
usage: 'After starting S3 local, create specified buckets',
},
cors: {
shortcut: 'c',
usage: 'Enable CORS',
},
noStart: {
shortcut: 'n',
default: false,
usage: 'Do not start S3 local (in case it is already running)',
},
},
},
create: {
usage: 'Create local S3 buckets.',
lifecycleEvents: ['createHandler'],
options: {
port: {
shortcut: 'p',
usage:
'The port number that S3 will use to communicate with your application. If you do not specify this option, the default port is 4569',
},
buckets: {
shortcut: 'b',
usage: 'After starting S3 local, create specified buckets',
},
}
},
remove: {
usage: 'Remove local S3 buckets.',
lifecycleEvents: ['createHandler'],
options: {
port: {
shortcut: 'p',
usage:
'The port number that S3 will use to communicate with your application. If you do not specify this option, the default port is 4569',
},
buckets: {
shortcut: 'b',
usage: 'After starting S3 local, create specified buckets',
},
}
}
},
},
};
this.hooks = {
's3:start:startHandler': this.startHandler.bind(this),
's3:create:createHandler': this.createHandler.bind(this),
's3:remove:createHandler': this.removeHandler.bind(this),
'before:offline:start:init': this.startHandler.bind(this),
'before:offline:start': this.startHandler.bind(this),
'before:offline:start:end': this.endHandler.bind(this),
};
}
startHandler() {
return new Promise((resolve, reject) => {
this._setOptions();
const { noStart, port, host, cors } = this.options;
if (noStart) {
return this.createBuckets().then(resolve, reject);
}
const dirPath = this.options.directory || './buckets';
fs.ensureDirSync(dirPath); // Create destination directory if not exist
const directory = fs.realpathSync(dirPath);
const corsPolicy = cors ?
fs.readFileSync(path.resolve(this.serverless.config.servicePath, cors) , 'utf8') :
cors ;
this.client = new S3rver({
port,
hostname: host,
silent: false,
directory,
cors: corsPolicy,
}).run((err, s3Host, s3Port) => {
if (err) {
console.error('Error occurred while starting S3 local.');
reject(err);
return;
}
this.options.port = s3Port
console.log(`S3 local started ( port:${s3Port} )`);
this.createBuckets().then(resolve, reject);
});
this.eventHandlers = this.getEventHandlers();
this.client.s3Event.map((event) => {
const bucketName = event.Records[0].s3.bucket.name;
const eventName = event.Records[0].eventName;
const key = event.Records[0].s3.object.key;
return this.eventHandlers
.filter(handler => handler.name == bucketName)
.filter(handler => eventName.match(handler.pattern) !== null)
.filter(handler => handler.rules.every(rule => key.match(rule)))
.map(handler => () => handler.func(event));
}).mergeMap((handler) => {
return handler;
}).subscribe((handler) => {
handler();
});
});
}
endHandler() {
if (!this.options.noStart) {
this.client.close();
console.log('S3 local closed');
}
}
createHandler() {
this._setOptions();
return this.createBuckets();
}
removeHandler() {
this._setOptions();
return this.removeBuckets();
}
createBuckets() {
return Promise.resolve().then(() => {
const buckets = this.buckets();
if (!buckets.length) return;
const s3Client = this.getClient();
return Promise.all(buckets.map(Bucket => {
this.serverless.cli.log(`creating bucket: ${Bucket}`);
return s3Client.createBucket({ Bucket }).promise();
}));
})
.catch(x => {});
}
removeBuckets() {
return Promise.resolve().then(() => {
const { port } = this.options;
const buckets = this.buckets();
if (!buckets.length) return;
return Promise.all(buckets.map(bucket => {
this.serverless.cli.log(`removing bucket: ${bucket}`);
return removeBucket({ port, bucket });
}));
});
}
getClient() {
return new AWS.S3({
s3ForcePathStyle: true,
endpoint: new AWS.Endpoint(`http://localhost:${this.options.port}`),
});
}
getEventHandlers() {
if (typeof this.service !== 'object' || typeof this.service.functions !== 'object') {
return {}
}
const eventHandlers = [];
//Allow integration with serverless-offline and serverless-webpack
this.options = Object.assign({}, this.options, (this.service.custom || {})['serverless-offline']);
const servicePath = path.join(this.serverless.config.servicePath, this.options.location);
Object.keys(this.service.functions).forEach(key => {
const serviceFunction = this.service.getFunction(key);
let handler = null;
const lambdaContext = createLambdaContext(serviceFunction);
const funOptions = functionHelper.getFunctionOptions(serviceFunction, key, servicePath);
const func = (s3Event) => {
handler = handler || functionHelper.createHandler(funOptions, this.options);
const oldEnv = process.env;
try {
process.env = Object.assign(
{},
oldEnv,
serviceFunction.environment
);
handler(s3Event, lambdaContext, lambdaContext.done);
}
finally {
process.env = oldEnv;
}
};
serviceFunction.events.forEach(event => {
const s3 = (event && event.s3) || undefined;
if (!s3) {
return;
}
const handlerBucketName = (typeof s3 === 'object') ? s3.bucket : s3;
const bucketResource = this.getResourceForBucket(handlerBucketName);
const name = bucketResource ? bucketResource.Properties.BucketName : handlerBucketName ;
const pattern = (typeof s3 === 'object') ? s3.event.replace(/^s3:/,'').replace('*', '.*') :'.*';
const rule2regex = (rule) => Object.keys(rule).map( key => key == 'prefix' && `^${rule[key]}` || `${rule[key]}$`);
const rules = (typeof s3 === 'object') ? [].concat(...(s3.rules || []).map(rule2regex)) : [];
eventHandlers.push({
name,
pattern,
rules,
func
});
this.serverless.cli.log(`Found S3 event listener for ${name}`);
});
});
return eventHandlers;
}
getResourceForBucket(bucketName){
const logicalResourceName = `S3Bucket${bucketName.charAt(0).toUpperCase()}${bucketName.substr(1)}`;
return this.service.resources ? this.service.resources.Resources[logicalResourceName] : false ;
}
getAdditionalStacks() {
const serviceAdditionalStacks = this.service.custom.additionalStacks || {};
const additionalStacks = [];
Object.keys(serviceAdditionalStacks).forEach((stack) => {
additionalStacks.push(serviceAdditionalStacks[stack]);
});
return additionalStacks;
}
hasAdditionalStacksPlugin() {
return (
this.service &&
this.service.plugins &&
this.service.plugins.indexOf('serverless-plugin-additional-stacks') >= 0
);
}
/**
* Get bucket list from serverless.yml resources and additional stacks
*
* @return {object} Array of bucket name
*/
buckets() {
const resources = (this.service.resources && this.service.resources.Resources) || {};
if (this.hasAdditionalStacksPlugin()) {
let additionalStacks = [];
additionalStacks = additionalStacks.concat(this.getAdditionalStacks());
additionalStacks.forEach((stack) => {
if (stack.Resources) {
Object.keys(stack.Resources).forEach((key) => {
if (stack.Resources[key].Type === 'AWS::S3::Bucket') {
resources[key] = stack.Resources[key];
}
});
}
});
}
return Object.keys(resources)
.map((key) => {
if (resources[key].Type === 'AWS::S3::Bucket' && resources[key].Properties && resources[key].Properties.BucketName) {
return resources[key].Properties.BucketName;
}
return null;
})
.concat(this.options.buckets)
.filter(n => n);
}
_setOptions() {
const config = (this.serverless.service.custom && this.serverless.service.custom.s3) || {};
this.options = Object.assign({}, defaultOptions, (this.service.custom || {})['serverless-offline'], this.options, config);
}
}
const removeBucket = ({ bucket, port }) => new Promise((resolve, reject) => {
shell.exec(
`aws --endpoint http://localhost:${port} s3 rb "s3://${bucket}" --force`,
{ silent: true },
function (code, stdout, stderr) {
if (code === 0) return resolve();
if (stderr && stderr.indexOf('NoSuchBucket') !== -1) return resolve();
reject(new Error(`failed to delete bucket ${bucket}: ${stderr || stdout}`));
}
);
});
module.exports = ServerlessS3Local;