@nxtedition/deepstream.io-client-js 31.0.3 → 31.0.5

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": "@nxtedition/deepstream.io-client-js",
3
- "version": "31.0.3",
3
+ "version": "31.0.5",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "type": "module",
@@ -2,10 +2,10 @@ import * as C from '../constants/constants.js'
2
2
  import varint from 'varint'
3
3
 
4
4
  const SEP = C.MESSAGE_PART_SEPERATOR
5
- const MAX_MESSAGE_SIZE = 1024 * 1024
6
5
 
7
6
  let poolBuf
8
7
  let poolPos = 0
8
+ let poolSize = 1024 * 1024
9
9
 
10
10
  export function getMsg(topic, action, data, binary) {
11
11
  if (data && !(data instanceof Array)) {
@@ -14,6 +14,7 @@ export function getMsg(topic, action, data, binary) {
14
14
 
15
15
  if (binary) {
16
16
  let headerSize = 0
17
+ let estimatedSize = 0
17
18
 
18
19
  // Estimate headerSize
19
20
  if (data) {
@@ -22,14 +23,18 @@ export function getMsg(topic, action, data, binary) {
22
23
  if (typeof data[n] !== 'string') {
23
24
  throw new Error(`invalid data[${n}]`)
24
25
  }
26
+ estimatedSize += data[n].length + 1 // +1 for the separator
25
27
  headerSize += varint.encodingLength(data[n].length)
26
28
  }
27
- // Allow for some multi chars here and there...
29
+ // Allow extra space for some multi chars here and there...
28
30
  headerSize += 2
31
+
32
+ estimatedSize += headerSize + topic.length + action.length + 2 // +2 for the topic and action separators
33
+ estimatedSize += 32 // Allow for some extra space
29
34
  }
30
35
 
31
- if (!poolBuf || poolBuf.byteLength - poolPos < MAX_MESSAGE_SIZE) {
32
- poolBuf = Buffer.allocUnsafeSlow(8 * MAX_MESSAGE_SIZE)
36
+ if (!poolBuf || poolBuf.byteLength - poolPos < estimatedSize) {
37
+ poolBuf = Buffer.allocUnsafeSlow(poolSize)
33
38
  poolPos = 0
34
39
  }
35
40
 
@@ -73,6 +78,13 @@ export function getMsg(topic, action, data, binary) {
73
78
  const len = poolBuf.write(data[i], dataPos)
74
79
  dataPos += len
75
80
 
81
+ if (dataPos >= poolPos + poolBuf.byteLength) {
82
+ poolSize *= poolPos === 0 ? 2 : 1
83
+ poolBuf = null
84
+ poolPos = 0
85
+ return getMsg(topic, action, data, binary)
86
+ }
87
+
76
88
  if (headerSize > 0) {
77
89
  const encodingLength = varint.encodingLength(len + 1)
78
90
  if (headerPos + encodingLength > dataStart) {
@@ -4,7 +4,6 @@ import UnicastListener from '../utils/unicast-listener.js'
4
4
  import * as C from '../constants/constants.js'
5
5
  import * as rxjs from 'rxjs'
6
6
  import invariant from 'invariant'
7
- import EventEmitter from 'component-emitter2'
8
7
  import jsonPath from '@nxtedition/json-path'
9
8
  import * as utils from '../utils/utils.js'
10
9
  import xuid from 'xuid'
@@ -121,8 +120,7 @@ class RecordHandler {
121
120
  }
122
121
 
123
122
  this._syncQueue = []
124
- this._syncEmitter = new EventEmitter()
125
- this._readyEmitter = new EventEmitter()
123
+ this._syncMap = {}
126
124
 
127
125
  this.set = this.set.bind(this)
128
126
  this.get = this.get.bind(this)
@@ -649,7 +647,23 @@ class RecordHandler {
649
647
  }
650
648
 
651
649
  if (message.action === C.ACTIONS.SYNC) {
652
- this._syncEmitter.emit(message.data[0], message.data[1]?.toString())
650
+ const [token] = message.data
651
+ if (!token) {
652
+ return true
653
+ }
654
+
655
+ const sync = this._syncMap[token]
656
+ delete this._syncMap[token]
657
+
658
+ if (!sync) {
659
+ return true
660
+ }
661
+
662
+ const { queue } = sync
663
+ for (let n = 0; n < queue.length; n += 2) {
664
+ queue[n](queue[n + 1])
665
+ }
666
+
653
667
  return true
654
668
  }
655
669
 
@@ -682,9 +696,17 @@ class RecordHandler {
682
696
  this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.PUT, update)
683
697
  }
684
698
 
685
- for (const token of this._syncEmitter.eventNames()) {
686
- this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SYNC, [token])
699
+ const syncMap = {}
700
+ for (const sync of Object.values(this._syncMap)) {
701
+ const token = xuid()
702
+ syncMap[token] = sync
703
+ this._connection.sendMsg(
704
+ C.TOPIC.RECORD,
705
+ C.ACTIONS.SYNC,
706
+ sync.type ? [token, sync.type] : [token],
707
+ )
687
708
  }
709
+ this._syncMap = syncMap
688
710
  } else {
689
711
  this._connected = 0
690
712
  }
@@ -710,11 +732,8 @@ class RecordHandler {
710
732
  // sync requests from different sockets.
711
733
  const token = xuid()
712
734
  const queue = this._syncQueue.splice(0)
713
- this._syncEmitter.once(token, () => {
714
- for (let n = 0; n < queue.length; n += 2) {
715
- queue[n](queue[n + 1])
716
- }
717
- })
735
+
736
+ this._syncMap[token] = { queue, type }
718
737
  this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SYNC, type ? [token, type] : [token])
719
738
  }, 1)
720
739
  }