@nxtedition/deepstream.io-client-js 31.0.0 → 31.0.2

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.0",
3
+ "version": "31.0.2",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "type": "module",
@@ -76,7 +76,7 @@ Connection.prototype.authenticate = function (authParams, callback) {
76
76
  }
77
77
 
78
78
  Connection.prototype.sendMsg = function (topic, action, data) {
79
- return this.send(messageBuilder.getMsg(topic, action, data))
79
+ return this.send(messageBuilder.getMsg(topic, action, data, utils.isNode))
80
80
  }
81
81
 
82
82
  Connection.prototype.close = function () {
@@ -118,7 +118,10 @@ Connection.prototype.send = function (message) {
118
118
  C.TOPIC.CONNECTION,
119
119
  C.EVENT.CONNECTION_ERROR,
120
120
  err,
121
- message.split(C.MESSAGE_PART_SEPERATOR).map((x) => x.slice(0, 256)),
121
+ message
122
+ .toString()
123
+ .split(C.MESSAGE_PART_SEPERATOR)
124
+ .map((x) => x.slice(0, 256)),
122
125
  )
123
126
  return false
124
127
  }
@@ -1,25 +1,107 @@
1
1
  import * as C from '../constants/constants.js'
2
+ import varint from 'varint'
2
3
 
3
4
  const SEP = C.MESSAGE_PART_SEPERATOR
5
+ const MAX_MESSAGE_SIZE = 1024 * 1024
4
6
 
5
- export function getMsg(topic, action, data) {
7
+ let poolBuf
8
+ let poolPos = 0
9
+
10
+ export function getMsg(topic, action, data, binary) {
6
11
  if (data && !(data instanceof Array)) {
7
12
  throw new Error('data must be an array')
8
13
  }
9
14
 
10
- const sendData = [topic, action]
15
+ if (binary) {
16
+ let headerSize = 0
11
17
 
12
- if (data) {
13
- for (let i = 0; i < data.length; i++) {
14
- if (typeof data[i] === 'object') {
15
- sendData.push(JSON.stringify(data[i]))
16
- } else {
17
- sendData.push(data[i])
18
+ // Estimate headerSize
19
+ if (data) {
20
+ headerSize = 1
21
+ for (let n = 0; n < data.length; n++) {
22
+ if (typeof data[n] !== 'string') {
23
+ throw new Error(`invalid data[${n}]`)
24
+ }
25
+ headerSize += varint.encodingLength(data[n].length)
26
+ }
27
+ // Allow for some multi chars here and there...
28
+ headerSize += 2
29
+ }
30
+
31
+ if (!poolBuf || poolBuf.byteLength - poolPos < MAX_MESSAGE_SIZE) {
32
+ poolBuf = Buffer.allocUnsafeSlow(8 * MAX_MESSAGE_SIZE)
33
+ poolPos = 0
34
+ }
35
+
36
+ let msgPos = poolPos
37
+
38
+ for (let i = 0; i < headerSize; i++) {
39
+ poolBuf[poolPos + i] = 0
40
+ }
41
+
42
+ let headerPos = poolPos
43
+ poolBuf[headerPos++] = 128 + headerSize
44
+
45
+ const dataStart = poolPos + headerSize
46
+
47
+ let dataPos = dataStart
48
+ if (topic.length === 1) {
49
+ poolBuf[dataPos++] = topic.charCodeAt(0)
50
+ } else {
51
+ throw new Error('invalid topic: ' + topic)
52
+ }
53
+
54
+ if (action.length === 1) {
55
+ poolBuf[dataPos++] = 31
56
+ poolBuf[dataPos++] = action.charCodeAt(0)
57
+ } else if (action.length === 2) {
58
+ poolBuf[dataPos++] = 31
59
+ poolBuf[dataPos++] = action.charCodeAt(0)
60
+ poolBuf[dataPos++] = action.charCodeAt(1)
61
+ } else if (action.length === 3) {
62
+ poolBuf[dataPos++] = 31
63
+ poolBuf[dataPos++] = action.charCodeAt(0)
64
+ poolBuf[dataPos++] = action.charCodeAt(1)
65
+ poolBuf[dataPos++] = action.charCodeAt(2)
66
+ } else {
67
+ throw new Error('invalid action: ' + action)
68
+ }
69
+
70
+ if (data) {
71
+ for (let i = 0; i < data.length; i++) {
72
+ poolBuf[dataPos++] = 31
73
+ const len = poolBuf.write(data[i], dataPos)
74
+ dataPos += len
75
+
76
+ if (headerSize > 0) {
77
+ const encodingLength = varint.encodingLength(len + 1)
78
+ if (headerPos + encodingLength > dataStart) {
79
+ // Overflow. Discard the header and fallback to only separators.
80
+ msgPos = dataStart
81
+ headerSize = 0
82
+ } else {
83
+ varint.encode(len + 1, poolBuf, headerPos)
84
+ headerPos += varint.encode.bytes
85
+ }
86
+ }
18
87
  }
19
88
  }
20
- }
21
89
 
22
- return sendData.join(SEP)
90
+ poolPos = dataPos
91
+ return poolBuf.subarray(msgPos, dataPos)
92
+ } else {
93
+ const sendData = [topic, action]
94
+ if (data) {
95
+ for (let i = 0; i < data.length; i++) {
96
+ if (typeof data[i] === 'object') {
97
+ sendData.push(JSON.stringify(data[i]))
98
+ } else {
99
+ sendData.push(data[i])
100
+ }
101
+ }
102
+ }
103
+ return sendData.join(SEP)
104
+ }
23
105
  }
24
106
 
25
107
  export function typed(value) {
@@ -377,6 +377,8 @@ class Record {
377
377
 
378
378
  const update = [this._name, nextVersion, jsonPath.stringify(nextData), prevVersion]
379
379
 
380
+ connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.UPDATE, update)
381
+
380
382
  if (!this._updating) {
381
383
  this._onUpdating(true)
382
384
  }
@@ -387,8 +389,6 @@ class Record {
387
389
  throw new Error('invalid state')
388
390
  }
389
391
 
390
- connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.UPDATE, update)
391
-
392
392
  this._data = nextData
393
393
  this._version = nextVersion
394
394
  }