@nxtedition/deepstream.io-client-js 26.0.2 → 26.0.4

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.
@@ -1,7 +1,7 @@
1
1
  import jsonPath from '@nxtedition/json-path'
2
2
  import * as utils from '../utils/utils.js'
3
3
  import * as C from '../constants/constants.js'
4
- import { convertTyped } from '../message/message-parser.js'
4
+ import messageParser from '../message/message-parser.js'
5
5
  import xuid from 'xuid'
6
6
  import invariant from 'invariant'
7
7
  import cloneDeep from 'lodash.clonedeep'
@@ -12,7 +12,7 @@ import * as timers from '../utils/timers.js'
12
12
  class Record {
13
13
  static STATE = C.RECORD_STATE
14
14
 
15
- constructor(key, name, handler) {
15
+ constructor (key, name, handler) {
16
16
  this._handler = handler
17
17
  this._name = name
18
18
  this._key = key
@@ -28,40 +28,35 @@ class Record {
28
28
  this._subscribed = false
29
29
  }
30
30
 
31
- /** @type {bigint} */
32
- get key() {
33
- return this._key
34
- }
35
-
36
31
  /** @type {string} */
37
- get name() {
32
+ get name () {
38
33
  return this._name
39
34
  }
40
35
 
41
36
  /** @type {string} */
42
- get version() {
37
+ get version () {
43
38
  return this._version
44
39
  }
45
40
 
46
41
  /** @type {Object} */
47
- get data() {
42
+ get data () {
48
43
  return this._data
49
44
  }
50
45
 
51
46
  /** @type {Number} */
52
- get state() {
47
+ get state () {
53
48
  return this._state
54
49
  }
55
50
 
56
51
  /** @type {Number} */
57
- get refs() {
52
+ get refs () {
58
53
  return this._refs
59
54
  }
60
55
 
61
56
  /**
62
57
  * @returns {Record}
63
58
  */
64
- ref() {
59
+ ref () {
65
60
  const connection = this._handler._connection
66
61
 
67
62
  this._refs += 1
@@ -77,7 +72,7 @@ class Record {
77
72
  /**
78
73
  * @returns {Record}
79
74
  */
80
- unref() {
75
+ unref () {
81
76
  this._refs -= 1
82
77
  if (this._refs === 0) {
83
78
  this._handler._onPruning(this, true)
@@ -90,7 +85,7 @@ class Record {
90
85
  * @param {*} opaque
91
86
  * @returns {Record}
92
87
  */
93
- subscribe(fn, opaque = null) {
88
+ subscribe (fn, opaque = null) {
94
89
  if (this._emitting) {
95
90
  this._subscriptions = this._subscriptions.slice()
96
91
  this._emitting = false
@@ -107,7 +102,7 @@ class Record {
107
102
  * @param {*} opaque
108
103
  * @returns {Record}
109
104
  */
110
- unsubscribe(fn, opaque = null) {
105
+ unsubscribe (fn, opaque = null) {
111
106
  if (this._emitting) {
112
107
  this._subscriptions = this._subscriptions.slice()
113
108
  this._emitting = false
@@ -131,7 +126,7 @@ class Record {
131
126
  return this
132
127
  }
133
128
 
134
- get(path) {
129
+ get (path) {
135
130
  if (!path) {
136
131
  return this._data
137
132
  } else if (typeof path === 'string' || Array.isArray(path)) {
@@ -143,7 +138,7 @@ class Record {
143
138
  }
144
139
  }
145
140
 
146
- set(pathOrData, dataOrNil) {
141
+ set (pathOrData, dataOrNil) {
147
142
  const prevData = this._data
148
143
  const prevVersion = this._version
149
144
 
@@ -190,7 +185,7 @@ class Record {
190
185
  }
191
186
  }
192
187
 
193
- when(stateOrNil, optionsOrNil) {
188
+ when (stateOrNil, optionsOrNil) {
194
189
  invariant(this._refs > 0, 'missing refs')
195
190
 
196
191
  if (stateOrNil != null && stateOrNil === 'object') {
@@ -255,8 +250,8 @@ class Record {
255
250
 
256
251
  onDone(
257
252
  Object.assign(new Error(`timeout ${this.name} [${current}<${expected}]`), {
258
- code: 'ETIMEDOUT',
259
- }),
253
+ code: 'ETIMEDOUT'
254
+ })
260
255
  )
261
256
  }, timeout)
262
257
  }
@@ -268,14 +263,14 @@ class Record {
268
263
  })
269
264
  }
270
265
 
271
- update(...args) {
266
+ update (...args) {
272
267
  invariant(this._refs > 0, 'missing refs')
273
268
 
274
269
  if (this._version.charAt(0) === 'I') {
275
270
  this._handler._client._$onError(C.TOPIC.RECORD, C.EVENT.UPDATE_ERROR, 'cannot update', [
276
271
  this._name,
277
272
  this._version,
278
- this._state,
273
+ this._state
279
274
  ])
280
275
  return Promise.resolve()
281
276
  }
@@ -314,7 +309,7 @@ class Record {
314
309
  })
315
310
  }
316
311
 
317
- _$onMessage(message) {
312
+ _$onMessage (message) {
318
313
  if (message.action === C.ACTIONS.UPDATE) {
319
314
  this._onUpdate(message.data)
320
315
  } else if (message.action === C.ACTIONS.SUBSCRIPTION_HAS_PROVIDER) {
@@ -326,7 +321,7 @@ class Record {
326
321
  return true
327
322
  }
328
323
 
329
- _$onConnectionStateChange(connected) {
324
+ _$onConnectionStateChange (connected) {
330
325
  const connection = this._handler._connection
331
326
 
332
327
  if (connected) {
@@ -349,7 +344,7 @@ class Record {
349
344
  }
350
345
  }
351
346
 
352
- _$dispose() {
347
+ _$dispose () {
353
348
  const connection = this._handler._connection
354
349
 
355
350
  invariant(!this._refs, 'must not have refs')
@@ -367,7 +362,7 @@ class Record {
367
362
  }
368
363
  }
369
364
 
370
- _update(nextData) {
365
+ _update (nextData) {
371
366
  invariant(this._version, 'must have version')
372
367
 
373
368
  const connection = this._handler._connection
@@ -397,7 +392,7 @@ class Record {
397
392
  this._version = nextVersion
398
393
  }
399
394
 
400
- _onUpdate([, version, data, hasProvider]) {
395
+ _onUpdate ([, version, data]) {
401
396
  const prevData = this._data
402
397
  const prevVersion = this._version
403
398
  const prevState = this._state
@@ -434,20 +429,12 @@ class Record {
434
429
  this._state = this._version.charAt(0) === 'I' ? C.RECORD_STATE.STALE : C.RECORD_STATE.SERVER
435
430
  }
436
431
 
437
- if (hasProvider != null) {
438
- this._state = convertTyped(hasProvider, this._handler._client)
439
- ? C.RECORD_STATE.PROVIDER
440
- : this._version.charAt(0) === 'I'
441
- ? C.RECORD_STATE.STALE
442
- : C.RECORD_STATE.SERVER
443
- }
444
-
445
432
  if (this._state !== prevState || this._data !== prevData || this._version !== prevVersion) {
446
433
  this._emitUpdate()
447
434
  }
448
435
  }
449
436
 
450
- _onPatching(value) {
437
+ _onPatching (value) {
451
438
  invariant(this._refs > 0, 'missing refs')
452
439
 
453
440
  if (value) {
@@ -461,7 +448,7 @@ class Record {
461
448
  this._handler._onPatching(this, value)
462
449
  }
463
450
 
464
- _onUpdating(value) {
451
+ _onUpdating (value) {
465
452
  invariant(this._refs > 0, 'missing refs')
466
453
 
467
454
  if (value) {
@@ -475,7 +462,7 @@ class Record {
475
462
  this._handler._onUpdating(this, value)
476
463
  }
477
464
 
478
- _onSubscriptionHasProvider([, hasProvider]) {
465
+ _onSubscriptionHasProvider ([, hasProvider]) {
479
466
  if (this._state < C.RECORD_STATE.SERVER) {
480
467
  return
481
468
  }
@@ -483,7 +470,7 @@ class Record {
483
470
  const prevState = this._state
484
471
 
485
472
  this._state =
486
- hasProvider && convertTyped(hasProvider, this._handler._client)
473
+ hasProvider && messageParser.convertTyped(hasProvider, this._handler._client)
487
474
  ? C.RECORD_STATE.PROVIDER
488
475
  : this._version.charAt(0) === 'I'
489
476
  ? C.RECORD_STATE.STALE
@@ -494,16 +481,16 @@ class Record {
494
481
  }
495
482
  }
496
483
 
497
- _error(event, msgOrError, data) {
484
+ _error (event, msgOrError, data) {
498
485
  this._handler._client._$onError(C.TOPIC.RECORD, event, msgOrError, [
499
486
  ...(Array.isArray(data) ? data : []),
500
487
  this._name,
501
488
  this._version,
502
- this._state,
489
+ this._state
503
490
  ])
504
491
  }
505
492
 
506
- _makeVersion(start) {
493
+ _makeVersion (start) {
507
494
  let revid = `${xuid()}-${this._handler._client.user || ''}`
508
495
  if (revid.length === 32 || revid.length === 16) {
509
496
  // HACK: https://github.com/apache/couchdb/issues/2015
@@ -512,7 +499,7 @@ class Record {
512
499
  return `${start}-${revid}`
513
500
  }
514
501
 
515
- _emitUpdate() {
502
+ _emitUpdate () {
516
503
  this._emitting = true
517
504
 
518
505
  const arr = this._subscriptions
@@ -534,58 +521,58 @@ Record.prototype.destroy = Record.prototype.unref
534
521
 
535
522
  // TODO (fix): Remove
536
523
  Object.defineProperty(Record.prototype, 'connected', {
537
- get: function connected() {
524
+ get: function connected () {
538
525
  return this._handler._client.getConnectionState() === C.CONNECTION_STATE.OPEN
539
- },
526
+ }
540
527
  })
541
528
 
542
529
  // TODO (fix): Remove
543
530
  Object.defineProperty(Record.prototype, 'empty', {
544
- get: function empty() {
531
+ get: function empty () {
545
532
  return Object.keys(this.data).length === 0
546
- },
533
+ }
547
534
  })
548
535
 
549
536
  // TODO (fix): Remove
550
537
  Object.defineProperty(Record.prototype, 'ready', {
551
- get: function ready() {
538
+ get: function ready () {
552
539
  return this._state >= C.RECORD_STATE.SERVER
553
- },
540
+ }
554
541
  })
555
542
 
556
543
  // TODO (fix): Remove
557
544
  Object.defineProperty(Record.prototype, 'provided', {
558
- get: function provided() {
545
+ get: function provided () {
559
546
  return this.state >= C.RECORD_STATE.PROVIDER
560
- },
547
+ }
561
548
  })
562
549
 
563
550
  // TODO (fix): Remove
564
551
  Object.defineProperty(Record.prototype, 'usages', {
565
- get: function usages() {
552
+ get: function usages () {
566
553
  return this._refs
567
- },
554
+ }
568
555
  })
569
556
 
570
557
  // TODO (fix): Remove
571
558
  Object.defineProperty(Record.prototype, 'stale', {
572
- get: function ready() {
559
+ get: function ready () {
573
560
  return !this.version
574
- },
561
+ }
575
562
  })
576
563
 
577
564
  // TODO (fix): Remove
578
565
  Object.defineProperty(Record.prototype, 'isReady', {
579
- get: function isReady() {
566
+ get: function isReady () {
580
567
  return this._state >= C.RECORD_STATE.SERVER
581
- },
568
+ }
582
569
  })
583
570
 
584
571
  // TODO (fix): Remove
585
572
  Object.defineProperty(Record.prototype, 'hasProvider', {
586
- get: function hasProvider() {
573
+ get: function hasProvider () {
587
574
  return this.state >= C.RECORD_STATE.PROVIDER
588
- },
575
+ }
589
576
  })
590
577
 
591
578
  export default Record
@@ -1,6 +1,6 @@
1
1
  import * as C from '../constants/constants.js'
2
2
  import RpcResponse from './rpc-response.js'
3
- import { convertTyped } from '../message/message-parser.js'
3
+ import messageParser from '../message/message-parser.js'
4
4
  import * as messageBuilder from '../message/message-builder.js'
5
5
  import xuid from 'xuid'
6
6
 
@@ -20,19 +20,19 @@ const RpcHandler = function (options, connection, client) {
20
20
  }
21
21
 
22
22
  Object.defineProperty(RpcHandler.prototype, 'connected', {
23
- get: function connected() {
23
+ get: function connected () {
24
24
  return this._client.getConnectionState() === C.CONNECTION_STATE.OPEN
25
- },
25
+ }
26
26
  })
27
27
 
28
28
  Object.defineProperty(RpcHandler.prototype, 'stats', {
29
- get: function stats() {
29
+ get: function stats () {
30
30
  return {
31
31
  ...this._stats,
32
32
  listeners: this._providers.size,
33
- rpcs: this._rpcs.size,
33
+ rpcs: this._rpcs.size
34
34
  }
35
- },
35
+ }
36
36
  })
37
37
 
38
38
  RpcHandler.prototype.provide = function (name, callback) {
@@ -95,7 +95,7 @@ RpcHandler.prototype.make = function (name, data, callback) {
95
95
  id,
96
96
  name,
97
97
  data,
98
- callback,
98
+ callback
99
99
  })
100
100
  this._connection.sendMsg(C.TOPIC.RPC, C.ACTIONS.REQUEST, [name, id, messageBuilder.typed(data)])
101
101
 
@@ -111,7 +111,7 @@ RpcHandler.prototype._respond = function (message) {
111
111
  if (callback) {
112
112
  let promise
113
113
  try {
114
- promise = Promise.resolve(callback(convertTyped(data, this._client), response))
114
+ promise = Promise.resolve(callback(messageParser.convertTyped(data, this._client), response))
115
115
  } catch (err) {
116
116
  promise = Promise.reject(err)
117
117
  }
@@ -152,11 +152,11 @@ RpcHandler.prototype._$handle = function (message) {
152
152
  Object.assign(new Error(data), {
153
153
  rpcId: rpc.id,
154
154
  rpcName: rpc.name,
155
- rpcData: rpc.data,
155
+ rpcData: rpc.data
156
156
  })
157
157
  )
158
158
  } else {
159
- rpc.callback(null, convertTyped(data, this._client))
159
+ rpc.callback(null, messageParser.convertTyped(data, this._client))
160
160
  }
161
161
  }
162
162
  }
@@ -27,7 +27,7 @@ RpcResponse.prototype.error = function (error) {
27
27
  this._name,
28
28
  this._id,
29
29
  error.message || error,
30
- true,
30
+ true
31
31
  ])
32
32
  }
33
33
 
@@ -40,7 +40,7 @@ RpcResponse.prototype.send = function (data) {
40
40
  this._connection.sendMsg(C.TOPIC.RPC, C.ACTIONS.RESPONSE, [
41
41
  this._name,
42
42
  this._id,
43
- messageBuilder.typed(data),
43
+ messageBuilder.typed(data)
44
44
  ])
45
45
  }
46
46
 
@@ -51,27 +51,27 @@ const kMask = kSize - 1
51
51
  // but allows much quicker checks.
52
52
 
53
53
  class FixedCircularBuffer {
54
- constructor() {
54
+ constructor () {
55
55
  this.bottom = 0
56
56
  this.top = 0
57
57
  this.list = new Array(kSize)
58
58
  this.next = null
59
59
  }
60
60
 
61
- isEmpty() {
61
+ isEmpty () {
62
62
  return this.top === this.bottom
63
63
  }
64
64
 
65
- isFull() {
65
+ isFull () {
66
66
  return ((this.top + 1) & kMask) === this.bottom
67
67
  }
68
68
 
69
- push(data) {
69
+ push (data) {
70
70
  this.list[this.top] = data
71
71
  this.top = (this.top + 1) & kMask
72
72
  }
73
73
 
74
- shift() {
74
+ shift () {
75
75
  const nextItem = this.list[this.bottom]
76
76
  if (nextItem === undefined) return null
77
77
  this.list[this.bottom] = undefined
@@ -81,15 +81,15 @@ class FixedCircularBuffer {
81
81
  }
82
82
 
83
83
  export default class FixedQueue {
84
- constructor() {
84
+ constructor () {
85
85
  this.head = this.tail = new FixedCircularBuffer()
86
86
  }
87
87
 
88
- isEmpty() {
88
+ isEmpty () {
89
89
  return this.head.isEmpty()
90
90
  }
91
91
 
92
- push(data) {
92
+ push (data) {
93
93
  if (this.head.isFull()) {
94
94
  // Head is full: Creates a new queue, sets the old queue's `.next` to it,
95
95
  // and sets it as the new main queue.
@@ -98,7 +98,7 @@ export default class FixedQueue {
98
98
  this.head.push(data)
99
99
  }
100
100
 
101
- shift() {
101
+ shift () {
102
102
  const tail = this.tail
103
103
  const next = tail.shift()
104
104
  if (tail.isEmpty() && tail.next !== null) {
@@ -2,7 +2,7 @@ import * as C from '../constants/constants.js'
2
2
  import rxjs from 'rxjs'
3
3
 
4
4
  class Listener {
5
- constructor(topic, pattern, callback, handler, { recursive = false, stringify = null } = {}) {
5
+ constructor (topic, pattern, callback, handler, { recursive = false, stringify = null } = {}) {
6
6
  this._topic = topic
7
7
  this._pattern = pattern
8
8
  this._callback = callback
@@ -16,17 +16,17 @@ class Listener {
16
16
  this._$onConnectionStateChange()
17
17
  }
18
18
 
19
- get connected() {
19
+ get connected () {
20
20
  return this._connection.connected
21
21
  }
22
22
 
23
- get stats() {
23
+ get stats () {
24
24
  return {
25
- subscriptions: this._subscriptions.size,
25
+ subscriptions: this._subscriptions.size
26
26
  }
27
27
  }
28
28
 
29
- _$destroy() {
29
+ _$destroy () {
30
30
  this._reset()
31
31
 
32
32
  if (this.connected) {
@@ -34,13 +34,13 @@ class Listener {
34
34
  }
35
35
  }
36
36
 
37
- _$onMessage(message) {
37
+ _$onMessage (message) {
38
38
  if (!this.connected) {
39
39
  this._client._$onError(
40
40
  C.TOPIC.RECORD,
41
41
  C.EVENT.NOT_CONNECTED,
42
42
  new Error('received message while not connected'),
43
- message,
43
+ message
44
44
  )
45
45
  return
46
46
  }
@@ -63,13 +63,13 @@ class Listener {
63
63
  version: null,
64
64
  timeout: null,
65
65
  patternSubscription: null,
66
- valueSubscription: null,
66
+ valueSubscription: null
67
67
  }
68
68
  provider.stop = () => {
69
69
  if (this.connected && provider.accepted) {
70
70
  this._connection.sendMsg(this._topic, C.ACTIONS.LISTEN_REJECT, [
71
71
  this._pattern,
72
- provider.key,
72
+ provider.key
73
73
  ])
74
74
  }
75
75
 
@@ -102,7 +102,7 @@ class Listener {
102
102
  this._connection.sendMsg(
103
103
  this._topic,
104
104
  accepted ? C.ACTIONS.LISTEN_ACCEPT : C.ACTIONS.LISTEN_REJECT,
105
- [this._pattern, provider.key],
105
+ [this._pattern, provider.key]
106
106
  )
107
107
 
108
108
  provider.version = null
@@ -160,12 +160,12 @@ class Listener {
160
160
  this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.UPDATE, [
161
161
  provider.key,
162
162
  version,
163
- body,
163
+ body
164
164
  ])
165
165
  }
166
166
  }
167
167
  },
168
- error: provider.error,
168
+ error: provider.error
169
169
  }
170
170
  provider.start = () => {
171
171
  try {
@@ -211,7 +211,7 @@ class Listener {
211
211
  return true
212
212
  }
213
213
 
214
- _$onConnectionStateChange() {
214
+ _$onConnectionStateChange () {
215
215
  if (this.connected) {
216
216
  this._connection.sendMsg(this._topic, C.ACTIONS.LISTEN, [this._pattern])
217
217
  } else {
@@ -219,15 +219,11 @@ class Listener {
219
219
  }
220
220
  }
221
221
 
222
- _error(name, err) {
223
- this._client._$onError(this._topic, C.EVENT.LISTENER_ERROR, err, [
224
- this._pattern,
225
- name,
226
- this._handler.getKey(name).toString(),
227
- ])
222
+ _error (name, err) {
223
+ this._client._$onError(this._topic, C.EVENT.LISTENER_ERROR, err, [this._pattern, name])
228
224
  }
229
225
 
230
- _reset() {
226
+ _reset () {
231
227
  for (const provider of this._subscriptions.values()) {
232
228
  provider.stop()
233
229
  }
@@ -3,7 +3,7 @@ let fastNowTimeout
3
3
 
4
4
  const fastTimers = []
5
5
 
6
- function onTimeout() {
6
+ function onTimeout () {
7
7
  fastNow = Date.now()
8
8
 
9
9
  let len = fastTimers.length
@@ -36,7 +36,7 @@ function onTimeout() {
36
36
  }
37
37
  }
38
38
 
39
- function refreshTimeout() {
39
+ function refreshTimeout () {
40
40
  if (fastNowTimeout && fastNowTimeout.refresh) {
41
41
  fastNowTimeout.refresh()
42
42
  } else {
@@ -49,7 +49,7 @@ function refreshTimeout() {
49
49
  }
50
50
 
51
51
  class Timeout {
52
- constructor(callback, delay, opaque) {
52
+ constructor (callback, delay, opaque) {
53
53
  this.callback = callback
54
54
  this.delay = delay
55
55
  this.opaque = opaque
@@ -63,7 +63,7 @@ class Timeout {
63
63
  this.refresh()
64
64
  }
65
65
 
66
- refresh() {
66
+ refresh () {
67
67
  if (this.state === -2) {
68
68
  fastTimers.push(this)
69
69
  if (!fastNowTimeout || fastTimers.length === 1) {
@@ -74,18 +74,18 @@ class Timeout {
74
74
  this.state = 0
75
75
  }
76
76
 
77
- clear() {
77
+ clear () {
78
78
  this.state = -1
79
79
  }
80
80
  }
81
81
 
82
- export function setTimeout(callback, delay, opaque) {
82
+ export function setTimeout (callback, delay, opaque) {
83
83
  return delay < 1e3
84
84
  ? globalThis.setTimeout(callback, delay, opaque)
85
85
  : new Timeout(callback, delay, opaque)
86
86
  }
87
87
 
88
- export function clearTimeout(timeout) {
88
+ export function clearTimeout (timeout) {
89
89
  if (timeout instanceof Timeout) {
90
90
  timeout.clear()
91
91
  } else {