-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproxy-integration.js
83 lines (67 loc) · 2.05 KB
/
proxy-integration.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
const url = require('url');
const fileType = require('file-type');
const { Integration } = require('./integration');
class ProxyIntegration extends Integration {
build() {
const req = this.req;
const body = this.requestBody;
const settings = this.settings;
req.egContext.lambda = Object.assign({},
req.egContext.lambda || {},
{
apiEndpoint: req.egContext.apiEndpoint,
resourcePath: req.route.path,
httpMethod: req.method,
requestId: req.egContext.requestID
});
let requestPath = req.url;
if (settings.stripPath) {
requestPath =
`/${req.params[0] || ''}${req._parsedUrl.search || ''}`;
}
if (settings.ignorePath) {
requestPath = '/';
}
return {
FunctionName: settings.functionName,
Qualifier: settings.qualifier,
Payload: Buffer.from(JSON.stringify({
httpMethod: req.method,
path: requestPath,
resource: req.route.path,
queryStringParameters: url.parse(req.url, true).query,
pathParameters: req.params,
headers: req.headers,
requestContext: req.egContext.lambda,
isBase64Encoded: this.isRequestBodyBinary,
body: body.length > 0
? body.toString(this.isRequestBodyBinary ? 'base64' : 'utf8')
: null
}))
};
}
respond(payload) {
const { statusCode,
headers,
body,
isBase64Encoded } = JSON.parse(payload);
const res = this.res;
res.statusCode = statusCode || 200;
let contentType;
for (const name in headers) {
if (name.toLowerCase() === 'content-type') {
contentType = headers[name];
}
res.setHeader(name, headers[name]);
}
if (!contentType) {
res.setHeader('Content-Type', this.guessContentType(body, isBase64Encoded));
}
res.end(isBase64Encoded ? Buffer.from(body, 'base64') : body);
}
static invoke(options) {
const integration = new ProxyIntegration(options);
return integration.invoke();
}
}
module.exports = { ProxyIntegration };