@subiz/wsclient 3.1.7 → 3.1.9

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.
Files changed (2) hide show
  1. package/package.json +4 -3
  2. package/realtime.js +31 -25
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subiz/wsclient",
3
- "version": "3.1.7",
3
+ "version": "3.1.9",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "node realtime.test.js"
@@ -14,9 +14,10 @@
14
14
  "homepage": "https://github.com/subiz/wsclient#readme",
15
15
  "description": "",
16
16
  "devDependencies": {
17
- "tape": "^5.3.1"
17
+ "tape": "^5.6.1"
18
18
  },
19
19
  "dependencies": {
20
- "@subiz/flow": "^1.0.9"
20
+ "@subiz/flow": "^1.0.9",
21
+ "request": "^2.88.2"
21
22
  }
22
23
  }
package/realtime.js CHANGED
@@ -22,7 +22,7 @@ function Conn(apiUrl, credential, onDead, onEvents, callAPI) {
22
22
  var lastToken = ''
23
23
 
24
24
  // kill force the connection to dead state
25
- this.kill = function() {
25
+ this.kill = function () {
26
26
  dead = true
27
27
  }
28
28
 
@@ -31,9 +31,9 @@ function Conn(apiUrl, credential, onDead, onEvents, callAPI) {
31
31
  // exponential backoff algorithm. Polling loop will terminate if it encounter an
32
32
  // un-retryable error
33
33
  // the polling loop starts after the first subscribe call finished successfully
34
- var polling = function(backoff) {
34
+ var polling = function (backoff) {
35
35
  if (dead) return
36
- callAPI('get', apiUrl + 'poll?token=' + lastToken, undefined, function(body, code) {
36
+ callAPI('get', apiUrl + 'poll?token=' + lastToken, undefined, function (body, code) {
37
37
  if (dead) return
38
38
  if (retryable(code)) return setTimeout(polling, calcNextBackoff(backoff), backoff + 1)
39
39
  if (code !== 200) {
@@ -55,20 +55,26 @@ function Conn(apiUrl, credential, onDead, onEvents, callAPI) {
55
55
  })
56
56
  }
57
57
 
58
- var subQueue = flow.batch(50, 100, function(events) {
58
+ var subQueue = flow.batch(50, 100, function (events) {
59
59
  if (dead) return ['dead']
60
60
  if (events.length <= 0) return []
61
61
 
62
62
  var out = []
63
63
  return flow
64
- .loop(function() {
65
- return new Promise(function(rs) {
64
+ .loop(function () {
65
+ return new Promise(function (rs) {
66
66
  var query = '?token=' + lastToken
67
- credential.getAccessToken().then(function(access_token) {
68
- if (credential.user_mask) query += '&user-mask=' + encodeURIComponent(credential.user_mask)
67
+ credential.getAccessToken().then(function (access_token) {
68
+ if (credential.user_ref)
69
+ query +=
70
+ '&user_ref=' +
71
+ encodeURIComponent(credential.user_ref) +
72
+ '&account_id=' +
73
+ encodeURIComponent(credential.account_id)
74
+ else if (credential.user_mask) query += '&user-mask=' + encodeURIComponent(credential.user_mask)
69
75
  else if (access_token) query += '&access-token=' + access_token
70
76
 
71
- callAPI('post', apiUrl + 'subs' + query, JSON.stringify({events: events}), function(body, code) {
77
+ callAPI('post', apiUrl + 'subs' + query, JSON.stringify({events: events}), function (body, code) {
72
78
  if (dead) {
73
79
  out = repeat('dead', events.length)
74
80
  return rs(false) // break loop
@@ -101,7 +107,7 @@ function Conn(apiUrl, credential, onDead, onEvents, callAPI) {
101
107
  })
102
108
  })
103
109
  })
104
- .then(function() {
110
+ .then(function () {
105
111
  return out
106
112
  })
107
113
  })
@@ -123,7 +129,7 @@ function Realtime(apiUrls, credential, callAPI) {
123
129
  if (typeof apiUrls === 'string' || apiUrls instanceof String) apiUrls = [apiUrls]
124
130
  credential = credential || {}
125
131
  if (!credential.getAccessToken) {
126
- credential.getAccessToken = function() {
132
+ credential.getAccessToken = function () {
127
133
  return Promise.resolve('')
128
134
  }
129
135
  }
@@ -140,22 +146,22 @@ function Realtime(apiUrls, credential, callAPI) {
140
146
 
141
147
  // stop the connection
142
148
  var stop = false
143
- this.stop = function() {
149
+ this.stop = function () {
144
150
  if (stop) return
145
151
  stop = true
146
152
  conn.kill()
147
153
  }
148
154
 
149
- this.onEvent = function(cb) {
155
+ this.onEvent = function (cb) {
150
156
  return pubsub.on('event', cb)
151
157
  }
152
158
 
153
- this.onInterrupted = function(cb) {
159
+ this.onInterrupted = function (cb) {
154
160
  return pubsub.on('interrupted', cb)
155
161
  }
156
162
 
157
163
  var conn
158
- this.subscribe = function(events) {
164
+ this.subscribe = function (events) {
159
165
  if (stop) return Promise.resolve({})
160
166
  if (typeof events === 'string') events = [events]
161
167
  if (!Array.isArray(events)) return Promise.resolve({error: 'param should be an array or string'})
@@ -168,7 +174,7 @@ function Realtime(apiUrls, credential, callAPI) {
168
174
  if (!topics[topic]) all.push(conn.subscribe(topic))
169
175
  }
170
176
  if (all.length === 0) return Promise.resolve({})
171
- return Promise.all(all).then(function(errs) {
177
+ return Promise.all(all).then(function (errs) {
172
178
  for (var i = 0; i < errs.length; i++) if (errs[i]) return {error: errs[i]}
173
179
  for (var j = 0; j < events.length; j++) topics[events[j]] = true
174
180
  return {}
@@ -177,7 +183,7 @@ function Realtime(apiUrls, credential, callAPI) {
177
183
 
178
184
  // reconnect make sure there is alway a Conn running in the background
179
185
  // if the Conn is dead, it recreate a new one
180
- var reconnect = function() {
186
+ var reconnect = function () {
181
187
  if (stop) return
182
188
  // reset subscribed topic
183
189
  var allTopics = Object.keys(topics)
@@ -187,12 +193,12 @@ function Realtime(apiUrls, credential, callAPI) {
187
193
  conn = new Conn(
188
194
  randomUrl,
189
195
  credential,
190
- function(code, body, status) {
196
+ function (code, body, status) {
191
197
  if (stop) return
192
198
  pubsub.emit('interrupted', code, body, status)
193
199
  setTimeout(reconnect, 2000) // reconnect and resubscribe after 2 sec
194
200
  },
195
- function(topics) {
201
+ function (topics) {
196
202
  if (stop) return
197
203
  for (var i = 0; i < topics.length; i++) pubsub.emit('event', topics[i])
198
204
  },
@@ -206,7 +212,7 @@ function Realtime(apiUrls, credential, callAPI) {
206
212
  reconnect()
207
213
  }
208
214
 
209
- var dofetch = function(method, url, body, cb) {
215
+ var dofetch = function (method, url, body, cb) {
210
216
  let headers = {}
211
217
  if (body) headers['content-type'] = 'text/plain'
212
218
  fetch(url, {
@@ -218,16 +224,16 @@ var dofetch = function(method, url, body, cb) {
218
224
  .catch((err) => cb && cb(err, -1))
219
225
  }
220
226
  // xhrsend sends an HTTP request
221
- var xhrsend = function(method, url, body, cb) {
227
+ var xhrsend = function (method, url, body, cb) {
222
228
  if (typeof XMLHttpRequest === 'undefined') return dofetch(method, url, body, cb)
223
229
  var request = new XMLHttpRequest()
224
- request.onreadystatechange = function(e) {
230
+ request.onreadystatechange = function (e) {
225
231
  if (request.readyState !== 4) return
226
232
  cb && cb(request.responseText, request.status)
227
233
  cb = undefined // dont call cb anymore
228
234
  }
229
235
 
230
- request.onerror = function() {
236
+ request.onerror = function () {
231
237
  cb && cb(request.responseText, -1) // network error
232
238
  cb = undefined // dont call cb anymore
233
239
  }
@@ -255,7 +261,7 @@ function Pubsub() {
255
261
 
256
262
  // emit notifies any callback functions that previously
257
263
  // subscribed to the topic (by the on function)
258
- this.emit = function(topic) {
264
+ this.emit = function (topic) {
259
265
  // skip the first item in arguments, which is the topic
260
266
  var args = []
261
267
  for (var i = 1; i < arguments.length; i++) args.push(arguments[i])
@@ -268,7 +274,7 @@ function Pubsub() {
268
274
  // register a callback function for a topic
269
275
  // when something is sent to the topic (by the emit function),
270
276
  // the callback function will be called
271
- this.on = function(topic, cb) {
277
+ this.on = function (topic, cb) {
272
278
  if (!listeners[topic]) listeners[topic] = []
273
279
  listeners[topic].push(cb)
274
280
  }