forked from serverless/serverless
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
110 lines (85 loc) · 3.18 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
'use strict';
const BbPromise = require('bluebird');
const chalk = require('chalk');
const path = require('path');
const SDK = require('../');
const moment = require('moment');
const validate = require('../lib/validate');
class AwsInvoke {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options || {};
this.provider = 'aws';
this.sdk = new SDK(serverless);
Object.assign(this, validate);
this.hooks = {
'invoke:invoke': () => BbPromise.bind(this)
.then(this.extendedValidate)
.then(this.invoke)
.then(this.log),
};
}
extendedValidate() {
this.validate();
// validate function exists in service
this.options.functionObj = this.serverless.service.getFunction(this.options.function);
if (this.options.path) {
if (!this.serverless.utils
.fileExistsSync(path.join(this.serverless.config.servicePath, this.options.path))) {
throw new this.serverless.classes.Error('The file you provided does not exist.');
}
this.options.data = this.serverless.utils
.readFileSync(path.join(this.serverless.config.servicePath, this.options.path));
}
return BbPromise.resolve();
}
invoke() {
const invocationType = this.options.type || 'RequestResponse';
if (invocationType !== 'RequestResponse') {
this.options.log = 'None';
} else {
this.options.log = this.options.log ? 'Tail' : 'None';
}
const params = {
FunctionName: this.options.functionObj.name,
InvocationType: invocationType,
LogType: this.options.log,
Payload: new Buffer(JSON.stringify(this.options.data || {})),
};
return this.sdk.request('Lambda', 'invoke', params, this.options.stage, this.options.region);
}
log(invocationReply) {
const color = !invocationReply.FunctionError ? 'white' : 'red';
if (invocationReply.Payload) {
const response = JSON.parse(invocationReply.Payload);
this.consoleLog(chalk[color](JSON.stringify(response, null, 4)));
}
const formatLambdaLogEvent = (msg) => {
const dateFormat = 'YYYY-MM-DD HH:mm:ss.SSS (Z)';
if (msg.startsWith('START') || msg.startsWith('END') || msg.startsWith('REPORT')) {
return chalk.gray(msg);
} else if (msg.trim() === 'Process exited before completing request') {
return chalk.red(msg);
}
const splitted = msg.split('\t');
if (splitted.length < 3 || new Date(splitted[0]) === 'Invalid Date') {
return msg;
}
const reqId = splitted[1];
const time = chalk.green(moment(splitted[0]).format(dateFormat));
const text = msg.split(`${reqId}\t`)[1];
return `${time}\t${chalk.yellow(reqId)}\t${text}`;
};
if (invocationReply.LogResult) {
this.consoleLog(chalk
.gray('--------------------------------------------------------------------'));
const logResult = new Buffer(invocationReply.LogResult, 'base64').toString();
logResult.split('\n').forEach(line => this.consoleLog(formatLambdaLogEvent(line)));
}
return BbPromise.resolve();
}
consoleLog(msg) {
console.log(msg); // eslint-disable-line no-console
}
}
module.exports = AwsInvoke;