comprodls-sdk 2.89.0 → 2.90.3-development
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/comprodls-sdk.js +306 -731
- package/dist/comprodls-sdk.min.js +1 -1
- package/lib/config/index.js +3 -5
- package/lib/helpers/lib/errors.js +1 -1
- package/lib/services/auth/index.js +2 -178
- package/lib/services/authextn/index.js +111 -49
- package/lib/services/product/index.js +4 -12
- package/lib/services/pub/index.js +5 -12
- package/lib/services/pushX/index.js +128 -185
- package/lib/services/pushX/pubnubClientWrapper.js +3 -230
- package/lib/services/spaces/index.js +0 -5
- package/lib/services/spacesextn/index.js +1 -45
- package/lib/services/workflows/index.js +2 -4
- package/lib/services/xapi/index.js +43 -1
- package/package.json +2 -2
- package/test.js +38 -0
|
@@ -26,7 +26,6 @@
|
|
|
26
26
|
|
|
27
27
|
var q = require('q');
|
|
28
28
|
var request = require('superagent');
|
|
29
|
-
var Agent = require('agentkeepalive');
|
|
30
29
|
|
|
31
30
|
var helpers = require('../../helpers');
|
|
32
31
|
var pubnubClientWrapper = require('./pubnubClientWrapper');
|
|
@@ -37,25 +36,18 @@ var DLSError = helpers.errors.DLSError;
|
|
|
37
36
|
**********************************/
|
|
38
37
|
module.exports = pushX;
|
|
39
38
|
|
|
40
|
-
var keepaliveAgent = new Agent({
|
|
41
|
-
timeout: 60000,
|
|
42
|
-
freeSocketTimeout: 30000
|
|
43
|
-
});
|
|
44
|
-
|
|
45
39
|
/*********************************
|
|
46
40
|
* Public Function definitions
|
|
47
41
|
**********************************/
|
|
48
42
|
function pushX() {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
"getPushedEvents": getPushedEvents.bind(this)
|
|
58
|
-
};
|
|
43
|
+
var _pubnubClientWrapper = pubnubClientWrapper();
|
|
44
|
+
return {
|
|
45
|
+
"connect": _connect.bind(this, _pubnubClientWrapper),
|
|
46
|
+
"cleanup": _cleanup.bind(this, _pubnubClientWrapper),
|
|
47
|
+
"grantByUserOrgId": grantByUserOrgId.bind(this),
|
|
48
|
+
"grantByAccountId": grantByAccountId.bind(this),
|
|
49
|
+
"grantByAccountIdOnExtUserId": grantByAccountIdOnExtUserId.bind(this)
|
|
50
|
+
};
|
|
59
51
|
}
|
|
60
52
|
|
|
61
53
|
/*********************************
|
|
@@ -63,193 +55,144 @@ function pushX() {
|
|
|
63
55
|
**********************************/
|
|
64
56
|
|
|
65
57
|
function _connect(pubnubCW, options) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
},
|
|
85
|
-
'pollingEndpoint': options.pollingEndpoint,
|
|
86
|
-
'pollingIterations': options.pollingIterations || 10, // Default polling iterations is 10
|
|
87
|
-
'pollingInterval': options.pollingInterval, // Default polling interval is exponential backoff
|
|
88
|
-
});
|
|
58
|
+
var bpubnubV7 = true; // If pubnub v7 or higher in package.json, set it true
|
|
59
|
+
if (bpubnubV7 && !options.userid) {
|
|
60
|
+
var err = {};
|
|
61
|
+
err.message = err.description = 'Mandatory parameter userid not found in request options.';
|
|
62
|
+
err = new DLSError(helpers.errors.ERROR_TYPES.PUSHX_ERROR, err);
|
|
63
|
+
throw err;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Adding SSL flag
|
|
67
|
+
return pubnubCW.setup({
|
|
68
|
+
'userid': options.userid,
|
|
69
|
+
'pubnub': {
|
|
70
|
+
'publishKey': options.publishKey,
|
|
71
|
+
'subscribeKey': options.subscribeKey,
|
|
72
|
+
'authKey': options.authKey,
|
|
73
|
+
'ssl': true
|
|
74
|
+
}
|
|
75
|
+
});
|
|
89
76
|
}
|
|
90
77
|
|
|
91
78
|
function _cleanup(pubnubCW) { pubnubCW.cleanup(); }
|
|
92
79
|
|
|
93
|
-
/*options = {
|
|
80
|
+
/*options = {
|
|
81
|
+
authKey: <authKey>
|
|
82
|
+
}*/
|
|
94
83
|
function grantByUserOrgId(options) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
84
|
+
var self = this;
|
|
85
|
+
var dfd = q.defer(); // Initializing promise
|
|
86
|
+
// Validations
|
|
87
|
+
var err = helpers.validations.isAuthenticated(self.orgId, self.token);
|
|
88
|
+
if(err) { dfd.reject(err); }
|
|
89
|
+
else {
|
|
90
|
+
// Passed all validations, Construct API url
|
|
91
|
+
var url = self.config.DEFAULT_HOSTS.PUSHX + self.config.PUSHX_API_URLS.grantByUserOrgId;
|
|
92
|
+
url = helpers.api.constructAPIUrl(url, { orgId: self.orgId });
|
|
93
|
+
// Setup request with URL and Params
|
|
94
|
+
var requestAPI = request.post(url)
|
|
95
|
+
.set('Content-Type', 'application/json')
|
|
96
|
+
.set('Accept', 'application/json');
|
|
97
|
+
|
|
98
|
+
var body = {};
|
|
99
|
+
if(options.authKey) { body.authKey = options.authKey; }
|
|
100
|
+
|
|
101
|
+
requestAPI.send(body);
|
|
102
|
+
|
|
103
|
+
//Setup token in Authorization header
|
|
104
|
+
requestAPI = helpers.api.setupAPIToken(requestAPI, self.token);
|
|
105
|
+
if(self.traceid) { requestAPI.set('X-Amzn-Trace-Id', self.traceid); }
|
|
106
|
+
|
|
107
|
+
// Call Change Password Api
|
|
108
|
+
requestAPI.end(function(err, response) {
|
|
109
|
+
if(err) {
|
|
110
|
+
err = new DLSError(helpers.errors.ERROR_TYPES.API_ERROR, err);
|
|
111
|
+
dfd.reject(err);
|
|
112
|
+
}
|
|
113
|
+
else { dfd.resolve(response.body); }
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return dfd.promise;
|
|
113
117
|
}
|
|
114
118
|
|
|
115
119
|
/*options = {
|
|
116
120
|
accountId: <accountid>,
|
|
117
|
-
refId: <extRefId
|
|
121
|
+
refId: <extRefId>,
|
|
122
|
+
authKey: <authKey>
|
|
118
123
|
}*/
|
|
119
124
|
function grantByAccountId(options) {
|
|
120
|
-
var err ={};
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
125
|
+
var self = this, err ={};
|
|
126
|
+
var dfd = q.defer(); // Initializing promise
|
|
127
|
+
|
|
128
|
+
if(options.accountId && options.refId) {
|
|
129
|
+
// Passed all validations, Construct API url
|
|
130
|
+
var url = self.config.DEFAULT_HOSTS.PUSHX + self.config.PUSHX_API_URLS.grantByAccountId;
|
|
131
|
+
url = helpers.api.constructAPIUrl(url, { accountId: options.accountId });
|
|
132
|
+
// Setup request with URL and Params
|
|
133
|
+
var requestAPI = request.post(url)
|
|
134
|
+
.set('Content-Type', 'application/json')
|
|
135
|
+
.set('Accept', 'application/json');
|
|
136
|
+
if(self.traceid) { requestAPI.set('X-Amzn-Trace-Id', self.traceid); }
|
|
137
|
+
|
|
138
|
+
var body = { refId: options.refId };
|
|
139
|
+
if(options.authKey) { body.authKey = options.authKey; }
|
|
140
|
+
|
|
141
|
+
requestAPI.send(body);
|
|
142
|
+
requestAPI.end(function(err, response) {
|
|
143
|
+
if(err) {
|
|
144
|
+
err = new DLSError(helpers.errors.ERROR_TYPES.API_ERROR, err);
|
|
145
|
+
dfd.reject(err);
|
|
146
|
+
}
|
|
147
|
+
else { dfd.resolve(response.body); }
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
err.message = err.description = "Required parameter ['accountId', 'refId'] " +
|
|
152
|
+
"not found in request options";
|
|
153
|
+
err = new DLSError(helpers.errors.ERROR_TYPES.SDK_ERROR, err);
|
|
154
|
+
dfd.reject(err);
|
|
155
|
+
}
|
|
156
|
+
return dfd.promise;
|
|
143
157
|
}
|
|
144
158
|
|
|
145
159
|
/*options = {
|
|
146
160
|
accountId: <accountid>,
|
|
147
161
|
extUserId: <extUserId>, //mandatory
|
|
162
|
+
authKey: <authKey>
|
|
148
163
|
}*/
|
|
149
164
|
function grantByAccountIdOnExtUserId(options) {
|
|
150
|
-
var err ={};
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
function grantV2(options) {
|
|
181
|
-
var self = this, err = {};
|
|
182
|
-
var dfd = q.defer();
|
|
183
|
-
var body = { channels: {} };
|
|
184
|
-
if(!options.channels) {
|
|
185
|
-
err.message = err.description = "channels not given in input body.";
|
|
186
|
-
err = new DLSError(helpers.errors.ERROR_TYPES.SDK_ERROR, err);
|
|
187
|
-
dfd.reject(err);
|
|
188
|
-
return dfd.promise;
|
|
189
|
-
}
|
|
190
|
-
body.channels = options.channels;
|
|
191
|
-
// Passed all validations, Construct API url
|
|
192
|
-
var url = self.config.DEFAULT_HOSTS.PUSHX + self.config.PUSHX_API_URLS.grantV2;
|
|
193
|
-
// Setup request with URL and Params
|
|
194
|
-
var requestAPI = request.post(url)
|
|
195
|
-
.set('Content-Type', 'application/json')
|
|
196
|
-
.set('Accept', 'application/json');
|
|
197
|
-
if(self.traceid) { requestAPI.set('X-Amzn-Trace-Id', self.traceid); }
|
|
198
|
-
|
|
199
|
-
requestAPI.send(body);
|
|
200
|
-
requestAPI.end(function(err, response) {
|
|
201
|
-
if(err) {
|
|
202
|
-
err = new DLSError(helpers.errors.ERROR_TYPES.API_ERROR, err);
|
|
203
|
-
dfd.reject(err);
|
|
165
|
+
var self = this, err ={};
|
|
166
|
+
var dfd = q.defer(); // Initializing promise
|
|
167
|
+
|
|
168
|
+
if(options.accountId && options.extUserId) {
|
|
169
|
+
// Passed all validations, Construct API url
|
|
170
|
+
var url = self.config.DEFAULT_HOSTS.PUSHX + self.config.PUSHX_API_URLS.grantByAccountId;
|
|
171
|
+
url = helpers.api.constructAPIUrl(url, { accountId: options.accountId });
|
|
172
|
+
// Setup request with URL and Params
|
|
173
|
+
var requestAPI = request.post(url)
|
|
174
|
+
.set('Content-Type', 'application/json')
|
|
175
|
+
.set('Accept', 'application/json');
|
|
176
|
+
if(self.traceid) { requestAPI.set('X-Amzn-Trace-Id', self.traceid); }
|
|
177
|
+
|
|
178
|
+
var body = { extUserId: options.extUserId };
|
|
179
|
+
if(options.authKey) { body.authKey = options.authKey; }
|
|
180
|
+
|
|
181
|
+
requestAPI.send(body);
|
|
182
|
+
requestAPI.end(function(err, response) {
|
|
183
|
+
if(err) {
|
|
184
|
+
err = new DLSError(helpers.errors.ERROR_TYPES.API_ERROR, err);
|
|
185
|
+
dfd.reject(err);
|
|
186
|
+
}
|
|
187
|
+
else { dfd.resolve(response.body); }
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
err.message = err.description = "Required parameter ['accountId', 'extUserId'] " +
|
|
192
|
+
"not found in request options";
|
|
193
|
+
err = new DLSError(helpers.errors.ERROR_TYPES.SDK_ERROR, err);
|
|
194
|
+
dfd.reject(err);
|
|
204
195
|
}
|
|
205
|
-
|
|
206
|
-
});
|
|
207
|
-
return dfd.promise;
|
|
196
|
+
return dfd.promise;
|
|
208
197
|
}
|
|
209
198
|
|
|
210
|
-
/**
|
|
211
|
-
* @param {
|
|
212
|
-
* *accountid: "string",
|
|
213
|
-
* *channelName: "string",
|
|
214
|
-
* *starttime: number,
|
|
215
|
-
* *endtime: number
|
|
216
|
-
* } options
|
|
217
|
-
*/
|
|
218
|
-
function getPushedEvents(options) {
|
|
219
|
-
var self = this;
|
|
220
|
-
var dfd = q.defer(); // Initializing promise
|
|
221
|
-
var accountid = options.accountid,
|
|
222
|
-
channelname = options.channelname,
|
|
223
|
-
starttime = options.starttime,
|
|
224
|
-
endtime = options.endtime;
|
|
225
|
-
|
|
226
|
-
if (accountid && channelname && starttime && endtime) {
|
|
227
|
-
// Passed all validations, Construct API url
|
|
228
|
-
var url = self.config.DEFAULT_HOSTS.PUSHX + self.config.PUSHX_API_URLS.getPushedEvents;
|
|
229
|
-
url = helpers.api.constructAPIUrl(url, { accountId: accountid });
|
|
230
|
-
|
|
231
|
-
// Setup request with URL and Params
|
|
232
|
-
var params = {channelname: channelname, starttime: starttime, endtime: endtime};
|
|
233
|
-
var requestAPI = request.get(url).query(params);
|
|
234
|
-
if(self.traceid) { requestAPI.set('X-Amzn-Trace-Id', self.traceid); }
|
|
235
|
-
|
|
236
|
-
requestAPI
|
|
237
|
-
.agent(keepaliveAgent)
|
|
238
|
-
.end(function(error, response) {
|
|
239
|
-
if (error) {
|
|
240
|
-
error = new DLSError(helpers.errors.ERROR_TYPES.API_ERROR, error);
|
|
241
|
-
dfd.reject(error);
|
|
242
|
-
} else {
|
|
243
|
-
dfd.resolve(response.body);
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
else {
|
|
248
|
-
var err = {};
|
|
249
|
-
err.message = err.description = 'Mandatory parameters [accountId, channelname,' +
|
|
250
|
-
' starttime, endtime] not found in request options';
|
|
251
|
-
err = new DLSError(helpers.errors.ERROR_TYPES.SDK_ERROR, err);
|
|
252
|
-
dfd.reject(err);
|
|
253
|
-
}
|
|
254
|
-
return dfd.promise;
|
|
255
|
-
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
var pubNub = require("pubnub");
|
|
2
|
-
var request = require('superagent');
|
|
3
2
|
var EventEmitter = require("events").EventEmitter;
|
|
4
3
|
var helpers = require('../../helpers');
|
|
5
4
|
|
|
@@ -22,13 +21,6 @@ module.exports = function () {
|
|
|
22
21
|
var _globalSubscription = [];
|
|
23
22
|
var _globalSubscriptionStatus = {};
|
|
24
23
|
var bStatusSubscribed = false;
|
|
25
|
-
/** ID of the setTimeout - used for Polling events when PubNub fails */
|
|
26
|
-
var _setTimeoutIDForPolling;
|
|
27
|
-
/** Timestamp from when the events need to be fetched */
|
|
28
|
-
var _startTimestampForPolling;
|
|
29
|
-
/** Counter for polling */
|
|
30
|
-
var _pollingCounter;
|
|
31
|
-
var bPollingInitiated = false;
|
|
32
24
|
|
|
33
25
|
/** ###### END OF MODULE GLOBALS */
|
|
34
26
|
|
|
@@ -44,9 +36,6 @@ module.exports = function () {
|
|
|
44
36
|
//Returning the adaptor (Plain Javascript object)
|
|
45
37
|
return {
|
|
46
38
|
"on": function (channelObj, handler) {
|
|
47
|
-
// If PubNub fails, we will fetch the events from this timestamp
|
|
48
|
-
_startTimestampForPolling = Date.now();
|
|
49
|
-
|
|
50
39
|
var pubNubChannel;
|
|
51
40
|
var channelContext = [];
|
|
52
41
|
|
|
@@ -75,9 +64,6 @@ module.exports = function () {
|
|
|
75
64
|
_eventEmitter.on(channelObj.channel, handler);
|
|
76
65
|
bStatusSubscribed = true;
|
|
77
66
|
}
|
|
78
|
-
|
|
79
|
-
// Clean up the old events
|
|
80
|
-
_checkOldBubbledEvents();
|
|
81
67
|
},
|
|
82
68
|
"getMySubscriptionStatus": __getMySubscriptionStatus
|
|
83
69
|
};
|
|
@@ -89,12 +75,8 @@ module.exports = function () {
|
|
|
89
75
|
_eventEmitter.emit(subscribedChannel, message);
|
|
90
76
|
};
|
|
91
77
|
|
|
92
|
-
var _translatePubnubStatus = function(status
|
|
78
|
+
var _translatePubnubStatus = function(status) {
|
|
93
79
|
var channels = [], error, successObj;
|
|
94
|
-
|
|
95
|
-
// If polling has been initiated, ignore all punnub status
|
|
96
|
-
if (bPollingInitiated) return;
|
|
97
|
-
|
|
98
80
|
switch (status.category) {
|
|
99
81
|
case "PNConnectedCategory":
|
|
100
82
|
if(status.operation === "PNSubscribeOperation") {
|
|
@@ -122,10 +104,8 @@ module.exports = function () {
|
|
|
122
104
|
break;
|
|
123
105
|
case "PNAccessDeniedCategory":
|
|
124
106
|
if(status.operation === "PNSubscribeOperation") {
|
|
125
|
-
var errorText = status.errorData.response && status.errorData.response.text;
|
|
126
|
-
var errorJSON = errorText ? JSON.parse(errorText) : undefined;
|
|
127
107
|
var errorData = {
|
|
128
|
-
payload:
|
|
108
|
+
payload: JSON.parse(status.errorData.response.text).payload,
|
|
129
109
|
message: 'Forbidden: Subscription failed.',
|
|
130
110
|
errorDetails: {
|
|
131
111
|
operation: status.operation,
|
|
@@ -154,47 +134,6 @@ module.exports = function () {
|
|
|
154
134
|
error = new PUSHXError(helpers.errors.ERROR_CATEGORY.PUSHX, error);
|
|
155
135
|
_eventEmitter.emit('pushx_status', error);
|
|
156
136
|
break;
|
|
157
|
-
case "PNNetworkIssuesCategory":
|
|
158
|
-
if(status.operation === 'PNSubscribeOperation') {
|
|
159
|
-
if(!options.accountId || !_userOptions.pollingEndpoint) {
|
|
160
|
-
error = {
|
|
161
|
-
message: "Polling initiation error: Missing mandatory parameters to initiate polling operation - [accountId, pollingEndpoint]",
|
|
162
|
-
status: status.statusCode
|
|
163
|
-
};
|
|
164
|
-
error = new PUSHXError(helpers.errors.ERROR_CATEGORY.PUSHX, error);
|
|
165
|
-
_eventEmitter.emit('pushx_status', error);
|
|
166
|
-
break;
|
|
167
|
-
}
|
|
168
|
-
else {
|
|
169
|
-
/**
|
|
170
|
-
* Handle network issue category status for 'subscription' opreration
|
|
171
|
-
* Start the events polling here.
|
|
172
|
-
*
|
|
173
|
-
* The wrapper supports multiple channels, but the APP currently uses a single channel only.
|
|
174
|
-
* Polling will also limited to a single channel.
|
|
175
|
-
*/
|
|
176
|
-
_pollingCounter = 0;
|
|
177
|
-
_pollForEvents();
|
|
178
|
-
bPollingInitiated = true;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
// Handle network issue category status for other operations like 'unsubscription'
|
|
183
|
-
}
|
|
184
|
-
break;
|
|
185
|
-
case "PNReconnectedCategory":
|
|
186
|
-
// Handle reconnected category status.
|
|
187
|
-
break;
|
|
188
|
-
default:
|
|
189
|
-
// Emit error for other status-category received from pubnub
|
|
190
|
-
error = {
|
|
191
|
-
message: "PushX Error: unexpected error",
|
|
192
|
-
status: status.statusCode,
|
|
193
|
-
pushXError: status
|
|
194
|
-
};
|
|
195
|
-
error = new PUSHXError(helpers.errors.ERROR_CATEGORY.PUSHX, error);
|
|
196
|
-
_eventEmitter.emit('pushx_status', error);
|
|
197
|
-
break;
|
|
198
137
|
}
|
|
199
138
|
};
|
|
200
139
|
|
|
@@ -226,15 +165,11 @@ module.exports = function () {
|
|
|
226
165
|
* @returns PROMISE.
|
|
227
166
|
*/
|
|
228
167
|
var __setup = function (userOptions) {
|
|
229
|
-
|
|
230
168
|
var pubnubConfig = userOptions.pubnub;
|
|
231
169
|
pubnubConfig.uuid = userOptions.userid;
|
|
232
|
-
var accountId = userOptions.accountid;
|
|
233
|
-
var token = userOptions.token;
|
|
234
170
|
|
|
235
171
|
if (!_pubnubClient && pubnubConfig) {
|
|
236
172
|
_pubnubClient = new pubNub(pubnubConfig); //Connect with PubNub SDK
|
|
237
|
-
_pubnubClient.setToken(token);
|
|
238
173
|
processSetup();
|
|
239
174
|
} else {
|
|
240
175
|
return new Error('Already Initialized');
|
|
@@ -250,8 +185,7 @@ module.exports = function () {
|
|
|
250
185
|
_translatePubnubMessage(data);
|
|
251
186
|
},
|
|
252
187
|
"status": function (status) {
|
|
253
|
-
|
|
254
|
-
_translatePubnubStatus(status, statusOptions);
|
|
188
|
+
_translatePubnubStatus(status);
|
|
255
189
|
}
|
|
256
190
|
});
|
|
257
191
|
}
|
|
@@ -265,169 +199,8 @@ module.exports = function () {
|
|
|
265
199
|
_globalSubscription = [];
|
|
266
200
|
bStatusSubscribed = false;
|
|
267
201
|
_globalSubscriptionStatus = {};
|
|
268
|
-
bPollingInitiated = false;
|
|
269
202
|
}
|
|
270
203
|
_pubnubClient = undefined;
|
|
271
|
-
|
|
272
|
-
// Clear the Polling
|
|
273
|
-
if (_setTimeoutIDForPolling) {
|
|
274
|
-
clearTimeout(_setTimeoutIDForPolling);
|
|
275
|
-
// Setting the start time to now to clear the old events
|
|
276
|
-
_startTimestampForPolling = Date.now();
|
|
277
|
-
_checkOldBubbledEvents();
|
|
278
|
-
_setTimeoutIDForPolling = undefined;
|
|
279
|
-
_pollingCounter = undefined;
|
|
280
|
-
_startTimestampForPolling = undefined;
|
|
281
|
-
}
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* Polling function
|
|
286
|
-
*/
|
|
287
|
-
var _pollForEvents = function () {
|
|
288
|
-
// Setup request params
|
|
289
|
-
var params = {
|
|
290
|
-
accountid: _userOptions.accountid,
|
|
291
|
-
channelname: _globalSubscription[0],
|
|
292
|
-
starttime: _startTimestampForPolling,
|
|
293
|
-
endtime: Date.now()
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
// Safe check to avoid poll for events if the channel name or start time is not set
|
|
297
|
-
// This handles an edge condition where the clearTimeout doesn't work as expected
|
|
298
|
-
if (!params.channelname || !params.starttime) {
|
|
299
|
-
return;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
var requestAPI = request.get(_userOptions.pollingEndpoint).query(params);
|
|
303
|
-
|
|
304
|
-
requestAPI
|
|
305
|
-
.end(function(error, response) {
|
|
306
|
-
|
|
307
|
-
if (!error) {
|
|
308
|
-
// Bubble the connected status
|
|
309
|
-
if (_pollingCounter === 0) {
|
|
310
|
-
// Set the status of the channel to subscribed
|
|
311
|
-
if(_globalSubscriptionStatus[_globalSubscription[0]]) {
|
|
312
|
-
_globalSubscriptionStatus[_globalSubscription[0]].status = 'subscribed';
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
var successObj = {
|
|
316
|
-
category: 'PUSHX',
|
|
317
|
-
type: 'CHANNEL_SUBSCRIPTION',
|
|
318
|
-
status: 'SUCCESS',
|
|
319
|
-
message: 'Success: Subscribed successfully.',
|
|
320
|
-
httpcode: 200,
|
|
321
|
-
data: {
|
|
322
|
-
payload: {
|
|
323
|
-
channels: _globalSubscription
|
|
324
|
-
},
|
|
325
|
-
message: 'Success: Subscribed successfully.'
|
|
326
|
-
}
|
|
327
|
-
};
|
|
328
|
-
_eventEmitter.emit('pushx_status', successObj);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
// Bubble the received events
|
|
332
|
-
_bubblePolledEvents(response.body);
|
|
333
|
-
|
|
334
|
-
// Increase the counter
|
|
335
|
-
_pollingCounter++;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
// Polling will be done only for specific number of times
|
|
339
|
-
if (_pollingCounter > _userOptions.pollingIterations) {
|
|
340
|
-
var error = {
|
|
341
|
-
message: "Polling error: Polling limit exceeded",
|
|
342
|
-
status: 429
|
|
343
|
-
};
|
|
344
|
-
error = new PUSHXError(helpers.errors.ERROR_CATEGORY.PUSHX, error);
|
|
345
|
-
_eventEmitter.emit('pushx_status', error);
|
|
346
|
-
} else {
|
|
347
|
-
var timeoutDelay;
|
|
348
|
-
|
|
349
|
-
// If the polling interval is provided, use that
|
|
350
|
-
if (_userOptions.pollingInterval) {
|
|
351
|
-
timeoutDelay = _userOptions.pollingInterval * 60 * 1000;
|
|
352
|
-
} else {
|
|
353
|
-
// Else, use exponential backoff
|
|
354
|
-
timeoutDelay = parseInt(Math.pow(1.3, _pollingCounter) * 10 * 1000, 10);
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
// Set timeout for next poll
|
|
358
|
-
if (timeoutDelay) {
|
|
359
|
-
_setTimeoutIDForPolling = setTimeout(_pollForEvents, timeoutDelay);
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
});
|
|
363
|
-
};
|
|
364
|
-
|
|
365
|
-
/**
|
|
366
|
-
* Bubble the polled events to the FE APP
|
|
367
|
-
* @param {*} events - Events recieved from the polling endpoint
|
|
368
|
-
*/
|
|
369
|
-
var _bubblePolledEvents = function (events) {
|
|
370
|
-
// Load the old events from the session storage
|
|
371
|
-
var oldEventData = _checkOldBubbledEvents();
|
|
372
|
-
|
|
373
|
-
var oldEvents = oldEventData.events;
|
|
374
|
-
var sessionStorageKey = oldEventData.key;
|
|
375
|
-
|
|
376
|
-
for (var i = 0; i < events.entities.length; i++) {
|
|
377
|
-
var event = events.entities[i];
|
|
378
|
-
var eventId = event.pk + '_' + event.sk;
|
|
379
|
-
|
|
380
|
-
// Check if the event is already emitted
|
|
381
|
-
if (!oldEvents[eventId]) {
|
|
382
|
-
oldEvents[eventId] = event.context.start_time;
|
|
383
|
-
|
|
384
|
-
// Emit the event
|
|
385
|
-
_eventEmitter.emit(_globalSubscription[0], event);
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
// Save the bubbled events to session storage
|
|
390
|
-
sessionStorage.setItem(sessionStorageKey, JSON.stringify(oldEvents));
|
|
391
|
-
};
|
|
392
|
-
|
|
393
|
-
/**
|
|
394
|
-
* Check the old bubbled events and remove the events that are older than the start timestamp
|
|
395
|
-
* @returns {object} - Object containing the key and the events
|
|
396
|
-
*/
|
|
397
|
-
var _checkOldBubbledEvents = function () {
|
|
398
|
-
var sessionStorageKey = 'comprodls.old_pushx_events.' + _userOptions.userid + '.' + _globalSubscription[0];
|
|
399
|
-
var oldEvents = sessionStorage.getItem(sessionStorageKey);
|
|
400
|
-
|
|
401
|
-
if (oldEvents) {
|
|
402
|
-
try {
|
|
403
|
-
oldEvents = JSON.parse(oldEvents);
|
|
404
|
-
} catch (e) {
|
|
405
|
-
oldEvents = {};
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
var oldEventIds = Object.keys(oldEvents);
|
|
409
|
-
|
|
410
|
-
// Remove the events that are older than the start timestamp
|
|
411
|
-
for (var i = 0; i < oldEventIds.length; i++) {
|
|
412
|
-
var oldEventId = oldEventIds[i];
|
|
413
|
-
|
|
414
|
-
if (oldEvents[oldEventId] < _startTimestampForPolling) {
|
|
415
|
-
delete oldEvents[oldEventId];
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
// If the number of events has changed, save the events
|
|
420
|
-
if (oldEventIds.length !== Object.keys(oldEvents).length) {
|
|
421
|
-
sessionStorage.setItem(sessionStorageKey, JSON.stringify(oldEvents));
|
|
422
|
-
}
|
|
423
|
-
} else {
|
|
424
|
-
oldEvents = {};
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
return {
|
|
428
|
-
key: sessionStorageKey,
|
|
429
|
-
events: oldEvents
|
|
430
|
-
};
|
|
431
204
|
};
|
|
432
205
|
|
|
433
206
|
return { // Return public methods for the wrapper
|