@subiz/wsclient 4.0.31 → 4.0.33

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subiz/wsclient",
3
- "version": "4.0.31",
3
+ "version": "4.0.33",
4
4
  "main": "realtime.js",
5
5
  "scripts": {
6
6
  "dev": "vite",
package/realtime.js CHANGED
@@ -17,13 +17,11 @@ var {xhrsend, parseJSON, Pubsub} = require('./common.js')
17
17
  function Conn(apiUrl, credential, onDead, onEvents, callAPI, accid) {
18
18
  callAPI = callAPI || xhrsend // allow hook
19
19
  credential = credential || {}
20
-
21
20
  var lastToken = ''
22
21
  if (!accid) accid = ''
23
-
24
22
  var is_dead = false // tell if the connection is dead
25
23
  var is_polling_stuck = false // tell if we've having trouble doing /poll
26
- var is_sub_stuck = false // tell if we've having trouble doing /subscribe
24
+ var is_sub_stuck = true // tell if we've having trouble doing /subscribe
27
25
 
28
26
  // The long polling loop runs sequentially, with each poll starting immediately
29
27
  // after the previous one completes. If an error occurs, the loop will pause
@@ -32,7 +30,7 @@ function Conn(apiUrl, credential, onDead, onEvents, callAPI, accid) {
32
30
  // The first loop begins after the first subscription call completes successfully
33
31
  var polling = function (backoff) {
34
32
  if (is_dead) return
35
- callAPI('get', apiUrl + 'poll?token=' + lastToken + '&account-id=' + accid, undefined, function (body, code) {
33
+ callAPI('get', apiUrl + 'poll?token=' + lastToken + '&v=5&account-id=' + accid, undefined, function (body, code) {
36
34
  if (is_dead) return
37
35
  if (retryable(code)) {
38
36
  is_polling_stuck = true
@@ -77,6 +75,7 @@ function Conn(apiUrl, credential, onDead, onEvents, callAPI, accid) {
77
75
  // too long, kill
78
76
  if (done) return
79
77
  done = true
78
+ is_sub_stuck = true
80
79
  resolve(repeat('dead', topics.length))
81
80
  if (is_dead) return
82
81
  kill('subs')
@@ -100,7 +99,8 @@ function Conn(apiUrl, credential, onDead, onEvents, callAPI, accid) {
100
99
  else if (credential.user_mask) query += '&user-mask=' + encodeURIComponent(credential.user_mask)
101
100
  else if (access_token) query += '&access-token=' + access_token
102
101
 
103
- var fullurl = apiUrl + 'subs' + query + '&account-id=' + encodeURIComponent(accid || credential.account_id)
102
+ var fullurl =
103
+ apiUrl + 'subs' + query + '&v=5&account-id=' + encodeURIComponent(accid || credential.account_id)
104
104
  callAPI('post', fullurl, JSON.stringify({events: topics}), function (body, code) {
105
105
  if (done) return
106
106
  if (is_dead) {
@@ -210,7 +210,7 @@ function Realtime(apiUrls, credential, callAPI, accid, skipautoreconnect) {
210
210
  return conn.getStatus()
211
211
  }
212
212
 
213
- this.subscribe = function (events) {
213
+ var subscribe = function (events) {
214
214
  if (!conn) return Promise.resolve({error: 'dead'})
215
215
  if (typeof events === 'string') events = [events]
216
216
  if (!Array.isArray(events)) return Promise.resolve({error: 'param should be an array or string'})
@@ -230,20 +230,22 @@ function Realtime(apiUrls, credential, callAPI, accid, skipautoreconnect) {
230
230
  return {}
231
231
  })
232
232
  }
233
+ this.subscribe = subscribe
233
234
 
234
235
  // reconnect make sure there is alway a Conn running in the background
235
236
  // if the Conn is dead, it recreate a new one
236
237
  var conn
237
- this.reconnect = function () {
238
+ var reconnect = function () {
238
239
  var randomUrl = apiUrls[Math.floor(Math.random() * apiUrls.length)]
239
240
  if (conn) conn.kill() // kill old connection
240
- let myconn = new Conn(
241
+ var myconn = new Conn(
241
242
  randomUrl,
242
243
  credential,
243
244
  function (code, body, status) {
244
245
  if (myconn != conn) return // outdated
246
+ conn = undefined // prevent dupplicate dead fire
245
247
  pubsub.emit('interrupted', code, body, status)
246
- if (!skipautoreconnect) setTimeout(this.reconnect, 2000) // reconnect and resubscribe after 2 sec
248
+ // if (!skipautoreconnect) setTimeout(reconnect, 2000) // reconnect and resubscribe after 2 sec
247
249
  },
248
250
  function (events) {
249
251
  if (myconn != conn) return
@@ -253,13 +255,13 @@ function Realtime(apiUrls, credential, callAPI, accid, skipautoreconnect) {
253
255
  accid,
254
256
  )
255
257
  conn = myconn
256
-
257
258
  var copyTopics = Object.keys(topics)
258
259
  topics = {}
259
260
  // resubscribe all subscribed events
260
- return this.subscribe(copyTopics)
261
+ return subscribe(copyTopics)
261
262
  }
262
- this.reconnect()
263
+ this.reconnect = reconnect
264
+ reconnect()
263
265
  }
264
266
 
265
267
  // calcNextBackoff returns number of seconds we must wait
package/realtime.test.js CHANGED
@@ -11,7 +11,7 @@ async function testScript(t, script) {
11
11
  calls.push({method, url, body, cb})
12
12
  },
13
13
  'acctest',
14
- true,
14
+ !script.auto_reconnect,
15
15
  )
16
16
  realtime.onEvent((ev) => events.push(ev))
17
17
 
@@ -100,7 +100,6 @@ async function testScript(t, script) {
100
100
  }
101
101
  t.true(false, script.name + '[' + stepi + ']=' + step.type)
102
102
  }
103
-
104
103
  // check events
105
104
  let scriptevents = script.events || []
106
105
  t.equal(events.length, scriptevents.length)
@@ -278,11 +277,59 @@ test.only('realtime', async (t) => {
278
277
  {type: 'status', status: 'active'},
279
278
  ],
280
279
  },
280
+ {
281
+ name: 'reconect while poll',
282
+ steps: [
283
+ {type: 'subscribe', topics: ['message_sent']},
284
+ {type: 'call', call_id: 1, path: '/subs'},
285
+ {type: 'response', call_id: '1', body: {initial_token: 'token1'}, code: 200},
286
+ {type: 'call', call_id: '2', path: '/poll', query: {token: 'token1'}, method: 'get'},
287
+ {type: 'status', status: 'active'},
288
+ {type: 'wait', timeout: 1000},
289
+ {type: 'response', call_id: '2', body: {events: [{text: 'xin chao'}], sequential_token: 'token2'}, code: 200},
290
+ {type: 'call', call_id: '3', path: '/poll', query: {token: 'token2'}, method: 'get'},
291
+ {type: 'response', call_id: '3', body: '400 dead', code: 400},
292
+ {type: 'status', status: 'dead'},
293
+ {type: 'reconnect'},
294
+ {type: 'wait', timeout: 1000},
295
+ {type: 'call', call_id: 4, path: '/subs'},
296
+ {type: 'wait', timeout: 1000},
297
+ {type: 'response', call_id: 4, body: {initial_token: 'token1'}, code: 200},
298
+ {type: 'status', status: 'active'},
299
+ ],
300
+ events: [{text: 'xin chao'}],
301
+ },
302
+ {
303
+ name: 'resub',
304
+ auto_reconnect: true,
305
+ steps: [
306
+ {type: 'subscribe', topics: ['message_sent']},
307
+ {type: 'call', call_id: 1, path: '/subs'},
308
+ {type: 'response', call_id: '1', body: 'wait a sec', code: 500},
309
+ {type: 'status', status: 'connecting'},
310
+ {type: 'wait', timeout: 1000},
311
+ {type: 'call', call_id: 2, path: '/subs'},
312
+ {type: 'wait', timeout: 61000},
313
+ {type: 'status', status: 'connecting'},
314
+ {type: 'call', call_id: 3, path: '/subs'},
315
+ {type: 'wait', timeout: 1000},
316
+ {type: 'response', call_id: '3', body: 'wait a sec', code: 500},
317
+ {type: 'status', status: 'connecting'},
318
+ {type: 'wait', timeout: 1000},
319
+ {type: 'call', call_id: 4, path: '/subs'},
320
+ {type: 'wait', timeout: 1000},
321
+ {type: 'response', call_id: '4', body: {initial_token: 'token1'}, code: 200},
322
+ {type: 'call', call_id: '5', path: '/poll', query: {token: 'token1'}, method: 'get'},
323
+ {type: 'status', status: 'active'},
324
+ {type: 'response', call_id: '5', body: {events: [{text: 'xin chao'}], sequential_token: 'token2'}, code: 200},
325
+ ],
326
+ events: [{text: 'xin chao'}],
327
+ },
281
328
  ]
282
329
 
283
330
  let ps = []
284
331
  for (const script of scripts) {
285
- // if (script.name != 'double subscribe -> do nothing') continue
332
+ // if (script.name != 'resub') continue
286
333
  ps.push(testScript(t, script))
287
334
  }
288
335
  await Promise.all(ps)