-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfbp.js
67 lines (54 loc) · 2.16 KB
/
fbp.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
'use strict';
const AWS = require('aws-sdk');
const BbPromise = require('bluebird');
const shared = require('./src/shared');
const fbpGraph = require('.fbp.json'); // eslint-disable-line import/no-unresolved
// eslint-disable-next-line consistent-return
const fromSnsSubscription = handler => (event, context, callback) => {
let message;
try {
message = event.Records[0].Sns.Message;
} catch (e) {
console.log('Could not find sns message content in event: ', JSON.stringify(event)); // eslint-disable-line no-console
return null;
}
return handler(JSON.parse(message), context, callback);
};
function snsArnFromLambdaArn(arn, topic) {
const parseAccountId = /arn:aws:lambda:(.+?):(.+?):/;
const region = parseAccountId.exec(arn)[1];
const accountId = parseAccountId.exec(arn)[2];
return `arn:aws:sns:${region}:${accountId}:${topic}`;
}
const publishToSnsTopics = (topics, context, callback) => (err, result) => {
const sns = new AWS.SNS();
const message = JSON.stringify(result);
if (!context.invokedFunctionArn) { // Invoked local
console.log(`Invoked local - would publish to ${topics.join(',')}, but stopping here.`); // eslint-disable-line no-console
return;
}
BbPromise.each(topics, (topic) => {
const params = {
Message: message,
TopicArn: snsArnFromLambdaArn(context.invokedFunctionArn, topic),
};
return sns.publish(params, (error, data) => { // eslint-disable-line no-unused-vars
if (error) {
callback(error);
}
});
}).then(() => callback(null, { message: 'Message successfully published to SNS topics' }));
};
module.exports.register = (component, handler) => (event, context, callback) => {
const connections = shared.connectionsForComponent(fbpGraph, component);
let enrichedHandler = handler;
if (connections.input.length > 0) {
enrichedHandler = fromSnsSubscription(handler);
}
let enrichedCallback = callback;
if (connections.output.length > 0) {
const topics = connections.output.map(output => shared.snsTopicForComponent(output));
enrichedCallback = publishToSnsTopics(topics, context, callback);
}
return enrichedHandler(event, context, enrichedCallback);
};