-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.js
60 lines (49 loc) · 1.88 KB
/
handlers.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
'use strict';
const Hoek = require('@hapi/hoek');
exports.hook = (sns, {handlers, skipPayloadValidation, topic}) => {
handlers = Hoek.applyToDefaults({
subscriptionconfirmation: confirmSubscription.bind(null, sns, topic)
}, handlers);
return (req, h) => {
return sns
.validatePayload(req.payload, skipPayloadValidation)
.then(dispatchToHandler.bind(null, handlers, req, h));
};
};
function dispatchToHandler(handlers, req, h, payload) {
const handler = handlers[payload.Type.toLowerCase()];
if (!handler) {
const msg = `Unable to handle message with type: ${payload.Type}`;
req.log(['hookido', 'error'], msg);
throw new Error(msg);
}
return handler(req, h, payload);
}
async function confirmSubscription(sns, topicOpts, req, h, payload) {
const topicArn = Hoek.reach(topicOpts, 'arn');
if (!topicArn) {
return Promise.reject(new Error('Can\'t confirm subscription when no topic.arn is configured'));
}
if (payload.TopicArn !== topicArn) {
const err = `Confirm subscription request for ${payload.TopicArn} doesn't match configured topic arn`;
req.log(['hookido', 'error'], err);
return Promise.reject(new Error(err));
}
try {
await fetch(payload.SubscribeURL);
req.log(['hookido', 'info'], `SNS subscription confirmed for ${payload.TopicArn}`);
} catch (err) {
req.log(['hookido', 'error'], `Unable to confirm SNS subscription for ${payload.TopicArn}, err: ${err.message}`);
throw err;
}
const susbscriptionAttr = Hoek.reach(topicOpts, 'subscribe.attributes');
if (susbscriptionAttr) {
try {
const arn = await sns.findSubscriptionArn(topicOpts.arn, topicOpts.subscribe.protocol, topicOpts.subscribe.endpoint);
await sns.setSubscriptionAttributes(arn, susbscriptionAttr);
} catch (err) {
req.log(['hookido', 'error'], `Unable to update subscription attributes for ${payload.TopicArn}, err: ${err.message}`);
}
}
return h.response().code(200);
}