-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathinvoke.js
More file actions
141 lines (118 loc) · 3.91 KB
/
invoke.js
File metadata and controls
141 lines (118 loc) · 3.91 KB
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
'use strict';
const path = require('path');
const validate = require('./lib/validate');
const stdin = require('get-stdin');
const formatLambdaLogEvent = require('./utils/format-lambda-log-event');
const ServerlessError = require('../../serverless-error');
const { writeText, style } = require('../../utils/serverless-utils/log');
class AwsInvoke {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options || {};
this.provider = this.serverless.getProvider('aws');
Object.assign(this, validate);
this.hooks = {
'invoke:invoke': async () => {
await this.extendedValidate();
this.log(await this.invoke());
},
};
}
async validateFile(key) {
const absolutePath = path.resolve(this.serverless.serviceDir, this.options[key]);
try {
return await this.serverless.utils.readFile(absolutePath);
} catch (err) {
if (err.code === 'ENOENT') {
throw new ServerlessError('The file you provided does not exist.', 'FILE_NOT_FOUND');
}
throw err;
}
}
async extendedValidate() {
this.validate();
// validate function exists in service
this.options.functionObj = this.serverless.service.getFunction(this.options.function);
this.options.data = this.options.data || '';
if (!this.options.data) {
if (this.options.path) {
this.options.data = await this.validateFile('path');
} else {
try {
this.options.data = await stdin();
} catch {
// continue if no stdin was provided
}
}
}
if (!this.options.context && this.options.contextPath) {
this.options.context = await this.validateFile('contextPath');
}
try {
if (!this.options.raw) {
this.options.data = JSON.parse(this.options.data);
}
} catch (exception) {
// do nothing if it's a simple string or object already
}
try {
if (!this.options.raw && this.options.context) {
this.options.context = JSON.parse(this.options.context);
}
} catch (exception) {
// do nothing if it's a simple string or object already
}
}
async 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: Buffer.from(JSON.stringify(this.options.data || {})),
};
if (this.options.context) {
params.ClientContext = Buffer.from(JSON.stringify(this.options.context)).toString('base64');
}
if (this.options.qualifier) {
params.Qualifier = this.options.qualifier;
}
return this.provider.request('Lambda', 'invoke', params);
}
payloadToString(payload) {
if (payload instanceof Uint8Array) {
return new TextDecoder().decode(payload);
}
if (Buffer.isBuffer(payload)) {
return payload.toString();
}
return payload;
}
log(invocationReply) {
if (invocationReply.Payload) {
const response = JSON.parse(this.payloadToString(invocationReply.Payload));
writeText(JSON.stringify(response, null, 4));
}
if (invocationReply.LogResult) {
writeText(
style.aside('--------------------------------------------------------------------')
);
const logResult = Buffer.from(invocationReply.LogResult, 'base64').toString();
logResult.split('\n').forEach((line) => {
if (line.includes('SERVERLESS_ENTERPRISE') || line.startsWith('END')) {
return;
}
writeText(formatLambdaLogEvent(line));
});
}
if (invocationReply.FunctionError) {
throw new ServerlessError('Invoked function failed', 'AWS_LAMBDA_INVOCATION_FAILED');
}
}
}
module.exports = AwsInvoke;