@subiz/wsclient 4.0.23 → 4.0.25

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 (3) hide show
  1. package/package.json +3 -3
  2. package/realtime.js +11 -13
  3. package/webrtc.js +10 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subiz/wsclient",
3
- "version": "4.0.23",
3
+ "version": "4.0.25",
4
4
  "main": "realtime.js",
5
5
  "scripts": {
6
6
  "dev": "vite",
@@ -15,8 +15,8 @@
15
15
  "homepage": "https://github.com/subiz/wsclient#readme",
16
16
  "description": "",
17
17
  "devDependencies": {
18
- "tape": "^5.7.3",
19
- "vite": "^5.0.4",
18
+ "tape": "^5.8.1",
19
+ "vite": "^5.3.4",
20
20
  "vite-plugin-commonjs": "^0.10.1"
21
21
  },
22
22
  "dependencies": {
package/realtime.js CHANGED
@@ -14,18 +14,18 @@ var {xhrsend, parseJSON, Pubsub} = require('./common.js')
14
14
  //
15
15
  // Once dead, onDead will be called. After that, the connection is
16
16
  // useless, all resources will be released.
17
- function Conn(apiUrl, credential, onDead, onEvents, callAPI) {
17
+ function Conn(apiUrl, credential, onDead, onEvents, callAPI, accid) {
18
18
  callAPI = callAPI || xhrsend // allow hook
19
19
  credential = credential || {}
20
20
  // tell if connection is dead
21
21
  var dead = false
22
22
 
23
23
  var lastToken = ''
24
-
25
24
  // kill force the connection to dead state
26
25
  this.kill = function () {
27
26
  dead = true
28
27
  }
28
+ if (!accid) accid = ''
29
29
 
30
30
  // long polling loop, polling will run sequentialy. Each polling will start right
31
31
  // after the previous returned. In case of error, polling loop will pause based on
@@ -34,7 +34,7 @@ function Conn(apiUrl, credential, onDead, onEvents, callAPI) {
34
34
  // the polling loop starts after the first subscribe call finished successfully
35
35
  var polling = function (backoff) {
36
36
  if (dead) return
37
- callAPI('get', apiUrl + 'poll?token=' + lastToken, undefined, function (body, code) {
37
+ callAPI('get', apiUrl + 'poll?token=' + lastToken + '&account_id=' + accid, undefined, function (body, code) {
38
38
  if (dead) return
39
39
  if (retryable(code)) return setTimeout(polling, calcNextBackoff(backoff), backoff + 1)
40
40
  if (code !== 200) {
@@ -66,16 +66,12 @@ function Conn(apiUrl, credential, onDead, onEvents, callAPI) {
66
66
  return new Promise(function (rs) {
67
67
  var query = '?token=' + lastToken
68
68
  credential.getAccessToken().then(function (access_token) {
69
- if (credential.user_ref)
70
- query +=
71
- '&user_ref=' +
72
- encodeURIComponent(credential.user_ref) +
73
- '&account_id=' +
74
- encodeURIComponent(credential.account_id)
69
+ if (credential.user_ref) query += '&user_ref=' + encodeURIComponent(credential.user_ref)
75
70
  else if (credential.user_mask) query += '&user-mask=' + encodeURIComponent(credential.user_mask)
76
71
  else if (access_token) query += '&access-token=' + access_token
77
72
 
78
- callAPI('post', apiUrl + 'subs' + query, JSON.stringify({events: events}), function (body, code) {
73
+ let fullurl = apiUrl + 'subs' + query + '&account_id=' + encodeURIComponent(accid || credential.account_id)
74
+ callAPI('post', fullurl, JSON.stringify({events: events}), function (body, code) {
79
75
  if (dead) {
80
76
  out = repeat('dead', events.length)
81
77
  return rs(false) // break loop
@@ -126,7 +122,7 @@ function Conn(apiUrl, credential, onDead, onEvents, callAPI) {
126
122
  // additional features compare to conn:
127
123
  // + auto recreate and resub if the last conn is dead
128
124
  // + don't subscribe already subscribed events
129
- function Realtime(apiUrls, credential, callAPI) {
125
+ function Realtime(apiUrls, credential, callAPI, accid) {
130
126
  if (typeof apiUrls === 'string' || apiUrls instanceof String) apiUrls = [apiUrls]
131
127
  credential = credential || {}
132
128
  if (!credential.getAccessToken) {
@@ -134,7 +130,7 @@ function Realtime(apiUrls, credential, callAPI) {
134
130
  return Promise.resolve('')
135
131
  }
136
132
  }
137
-
133
+ if (!accid) accid = credential.account_id || ''
138
134
  var pubsub = new Pubsub()
139
135
 
140
136
  // holds all topics that realtime must subscribed
@@ -148,9 +144,10 @@ function Realtime(apiUrls, credential, callAPI) {
148
144
  // stop the connection
149
145
  var stop = false
150
146
  this.stop = function () {
151
- if (stop) return
147
+ if (!conn || stop) return
152
148
  stop = true
153
149
  conn.kill()
150
+ pubsub.emit('interrupted')
154
151
  }
155
152
 
156
153
  this.onEvent = function (cb) {
@@ -204,6 +201,7 @@ function Realtime(apiUrls, credential, callAPI) {
204
201
  for (var i = 0; i < topics.length; i++) pubsub.emit('event', topics[i])
205
202
  },
206
203
  callAPI,
204
+ accid,
207
205
  )
208
206
 
209
207
  // resubscribe all subscribed events
package/webrtc.js CHANGED
@@ -48,7 +48,7 @@ function WebRTCConn(options) {
48
48
 
49
49
  peer.onicecandidate = (event) => {
50
50
  if (!event.candidate) return
51
- let url = `${apiUrl}ice-candidates?x-access-token=${access_token}&connection_id=${connId}`
51
+ let url = `${apiUrl}ice-candidates?x-access-token=${access_token}&connection_id=${connId}&account_id=${accid}`
52
52
  callAPI('post', url, jsonify(event.candidate))
53
53
  }
54
54
 
@@ -86,7 +86,7 @@ function WebRTCConn(options) {
86
86
  }
87
87
 
88
88
  let syncCallWithServer = () => {
89
- let url = `${apiUrl}calls?x-access-token=${access_token}`
89
+ let url = `${apiUrl}calls?x-access-token=${access_token}&account_id=${accid}`
90
90
  callAPI('get', url, undefined, function (body, code) {
91
91
  if (code != 200) return
92
92
  body = parseJSON(body) || []
@@ -297,7 +297,7 @@ function WebRTCConn(options) {
297
297
  })
298
298
  .then((answer) =>
299
299
  peer.setLocalDescription(answer).then(() => {
300
- let url = `${apiUrl}finish?x-access-token=${access_token}&connection_id=${connId}&version=2&negotiation_id=${negid}`
300
+ let url = `${apiUrl}finish?x-access-token=${access_token}&connection_id=${connId}&version=2&negotiation_id=${negid}&account_id=${accid}`
301
301
  callAPI('post', url, jsonify(answer), function (body, code) {
302
302
  if (code !== 200) {
303
303
  logError(accid, agid, body, 'finish:295')
@@ -365,7 +365,7 @@ function WebRTCConn(options) {
365
365
  return rs({error: err})
366
366
  }
367
367
 
368
- let url = `${apiUrl}call?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&number=${number}&from_number=${fromnumber}`
368
+ let url = `${apiUrl}call?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&number=${number}&from_number=${fromnumber}&account_id=${accid}`
369
369
  if (outboundcallentryid) url += `&outbound_call_entry_id=${outboundcallentryid}`
370
370
  if (campaignid) url += `&campaign_id=${campaignid}`
371
371
  callAPI('post', url, undefined, (body, code) => {
@@ -404,13 +404,13 @@ function WebRTCConn(options) {
404
404
  call.hangup_code = 'cancel'
405
405
  call.ended = env.Date.now()
406
406
  publish({created: env.Date.now(), type: 'call_ended', data: {call_info: {call_id: callid}}})
407
- callAPI('post', `${apiUrl}hangup?x-access-token=${access_token}&call_id=${callid}`)
407
+ callAPI('post', `${apiUrl}hangup?x-access-token=${access_token}&call_id=${callid}&account_id=${accid}`)
408
408
  }
409
409
 
410
410
  this.listenCall = (callid, cb) => {
411
411
  let start = env.Date.now()
412
412
  initSession((err, connId) => {
413
- let url = `${apiUrl}listen?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}`
413
+ let url = `${apiUrl}listen?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&account_id=${accid}`
414
414
  callAPI('post', url, undefined, (body, code) => {
415
415
  if (code != 200) logError(accid, agid, body, 'listenCall:399')
416
416
  collect('listen_call', callid, env.Date.now() - start)
@@ -424,7 +424,7 @@ function WebRTCConn(options) {
424
424
  initSession((err, connId) => {
425
425
  call2Connection[callid] = connId
426
426
  streams[connId] = stream
427
- let url = `${apiUrl}listen?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&talk=true`
427
+ let url = `${apiUrl}listen?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&talk=true&account_id={accid}`
428
428
  callAPI('post', url, undefined, (body, code) => {
429
429
  if (code != 200) logError(accid, agid, body, 'joinCall:413')
430
430
  collect('join_call', callid, env.Date.now() - start)
@@ -449,7 +449,7 @@ function WebRTCConn(options) {
449
449
  publish({created: env.Date.now(), type: 'call_ended', data: {call_info: {call_id: callid}}})
450
450
  return rs({error: err})
451
451
  }
452
- let url = `${apiUrl}answer?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}`
452
+ let url = `${apiUrl}answer?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&account_id=${accid}`
453
453
  callAPI('post', url, undefined, (body, code) => {
454
454
  body = parseJSON(body)
455
455
  if (code != 200) {
@@ -470,7 +470,7 @@ function WebRTCConn(options) {
470
470
  if (!callid) return rs({body: {}})
471
471
  let connId = call2Connection[callid]
472
472
  if (!connId) return rs({body: {}})
473
- let url = `${apiUrl}dtml?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&key={key}`
473
+ let url = `${apiUrl}dtml?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&key={key}&account_id=${accid}`
474
474
  initSession(() =>
475
475
  callAPI('post', url, undefined, (body, code) => {
476
476
  if (code != 200) logError(accid, agid, body, 'sendDtmf:463')
@@ -485,7 +485,7 @@ function WebRTCConn(options) {
485
485
  if (!callid) return rs({})
486
486
  let connId = call2Connection[callid]
487
487
  if (!connId) return rs({body: {}})
488
- let url = `${apiUrl}refer?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&number={tonumber}`
488
+ let url = `${apiUrl}refer?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&number={tonumber}&account_id=${accid}`
489
489
  initSession(() =>
490
490
  callAPI('post', url, undefined, (body, code) => {
491
491
  if (code != 200) logError(accid, agid, body, 'transferCall:478')