forked from jeffdonthemic/node-red-contrib-salesforce
-
Notifications
You must be signed in to change notification settings - Fork 17
/
streaming.js
148 lines (138 loc) · 4.37 KB
/
streaming.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
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
142
143
144
145
146
147
148
const actionHelper = require('./lib/action_helper');
//array to back-up the node & disconnect the streaming client when redeploying flow
const clients = {};
/**
* Configure and/or reset a subscription node
* @param {NodeRED node} node - the Subscription Node
* @param {Node configuration object} config the stored configuration
*/
const setupSubscriptionNode = (node, config) => {
node.config = config;
node.subscriptionActive = false;
node.client = {}; // The faye client
// back-up the node and disconnect/clean-up when
// redeploying flow to avoid duplicated subscription
if (!clients[node.id]) {
clients[node.id] = node;
node.startSubscriptionId = config.replayId;
} else {
// On redeployment we want to start
node.startSubscriptionId = clients[node.id].lastReceivedId || config.replayId;
if (clients[node.id].client.disconnect) {
clients[node.id].client.disconnect();
clients[node.id] = node;
}
}
};
const handleStreamError = (node, err) => {
node.log('Subscription error!!!');
actionHelper.error(node, err.message, err);
node.client.disconnect();
node.subscriptionActive = false;
return node.error(err, err.message);
};
const handleStreamData = (node, data) => {
const lastReplayId = node.lastReplayId || node.startSubscriptionId;
const newReplayId = data.event.replayId;
if (lastReplayId === newReplayId) {
// TODO: should a duplicate replay Error be swallowed?
const err = new Error('Duplicate replay id');
actionHelper.error(node, newReplayId, err);
return;
}
actionHelper.receiving(node);
node.send({
payload: data
});
actionHelper.subscribed(node, node.subscriptionMessage);
};
const createSubscription = (subscribeOptions) => {
const node = subscribeOptions.node;
if (node.subscriptionActive) {
if (node.config.resubScribeOnDoubeSubscription) {
// Terminate subscription first, ignore the resolve/reject
terminateSubscription(node, () => {}, () => {});
} else {
// We don't subscribe again
subscribeOptions.resolve();
return;
}
}
const config = node.config;
const msg = subscribeOptions.msg;
const streamOpts = {
// Topic in message takes priority over configuration
topic: msg.topic || config.pushTopic,
replayId: msg.replayId || node.startSubscriptionId,
oauth: subscribeOptions.oauth
};
try {
const org = subscribeOptions.org;
node.subscriptionMessage = 'Subscribed to:' + streamOpts.topic;
const fayeOptions = {
oauth: subscribeOptions.oauth
//timeout: 90,
//retry: 60
};
node.client = org.createStreamClient(fayeOptions);
const stream = node.client.subscribe(streamOpts);
node.log(node.subscriptionMessage);
node.subscriptionActive = true;
stream.on('error', (err) => handleStreamError(node, err));
stream.on('data', (data) => handleStreamData(node, data));
} catch (ex) {
subscribeOptions.reject(ex);
}
actionHelper.subscribed(node, node.subscriptionMessage);
subscribeOptions.resolve();
};
const terminateSubscription = (subscribeOptions) => {
const node = subscribeOptions.node;
if (node.subscriptionActive) {
try {
if (node.client.disconnect) {
node.client.disconnect();
}
} catch (err) {
subscribeOptions.reject(err);
}
}
// Terminate in any case
node.subscriptionActive = false;
actionHelper.idle(node);
subscribeOptions.resolve();
};
const handleInput = (node, msg) => {
const action = msg.action || (node.subscriptionActive ? 'unsubscribe' : 'subscribe');
const realAction = (org, payload, nforce) => {
return new Promise((resolve, reject) => {
const subscribeOptions = {
node,
org,
msg,
oauth: payload.oauth,
nforce,
resolve,
reject
};
if (action === 'subscribe') {
createSubscription(subscribeOptions);
} else {
terminateSubscription(subscribeOptions);
}
});
};
actionHelper.inputToSFAction(node, msg, realAction);
};
// Make available to NodeRED
module.exports = function(RED) {
function Streaming(config) {
const node = this;
RED.nodes.createNode(node, config);
node.connection = RED.nodes.getNode(config.connection);
setupSubscriptionNode(node, config);
node.on('input', (msg) => handleInput(node, msg));
actionHelper.idle(node);
}
RED.nodes.registerType('streaming', Streaming);
};