@nxtedition/deepstream.io-client-js 26.0.12 → 26.0.13

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": "26.0.12",
3
+ "version": "26.0.13",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "type": "module",
@@ -1,25 +1,100 @@
1
1
  import * as C from '../constants/constants.js'
2
+ import varint from 'varint'
3
+ import * as utils from '../utils/utils.js'
2
4
 
3
- const SEP = C.MESSAGE_PART_SEPERATOR
5
+ const poolEncoder = new globalThis.TextEncoder()
6
+
7
+ let poolSize
8
+ let poolBuffer
9
+ let poolView
10
+ let poolOffset
11
+
12
+ function allocPool(size) {
13
+ poolSize = size || poolSize || 1024 * 1024
14
+ poolBuffer = utils.isNode
15
+ ? globalThis.Buffer.allocUnsafe(poolSize)
16
+ : new Uint8Array(new ArrayBuffer(poolSize))
17
+ poolView = new DataView(poolBuffer.buffer)
18
+ poolOffset = 0
19
+ }
20
+
21
+ function alignPool() {
22
+ // Ensure aligned slices
23
+ if (poolOffset & 0x7) {
24
+ poolOffset |= 0x7
25
+ poolOffset++
26
+ }
27
+ }
28
+
29
+ function writeString(dst, str, offset) {
30
+ if (utils.isNode) {
31
+ return dst.write(str, offset)
32
+ } else {
33
+ const res = poolEncoder.encodeInto(str, new Uint8Array(dst.buffer, offset))
34
+ return res.written
35
+ }
36
+ }
4
37
 
5
38
  export function getMsg(topic, action, data) {
6
39
  if (data && !(data instanceof Array)) {
7
40
  throw new Error('data must be an array')
8
41
  }
9
42
 
10
- const sendData = [topic, action]
43
+ if (!poolSize || poolOffset + poolSize / 16 >= poolSize) {
44
+ allocPool()
45
+ } else {
46
+ alignPool()
47
+ }
48
+
49
+ const start = poolOffset
50
+
51
+ const headerSize = 8
52
+ poolBuffer[poolOffset++] = 128 + headerSize
53
+ let headerPos = poolOffset
54
+ poolOffset += headerSize - 1
55
+
56
+ poolBuffer[poolOffset++] = topic.charCodeAt(0)
57
+ poolBuffer[poolOffset++] = 31
58
+ for (let n = 0; n < action.length; n++) {
59
+ poolBuffer[poolOffset++] = action.charCodeAt(n)
60
+ }
11
61
 
12
62
  if (data) {
13
63
  for (let i = 0; i < data.length; i++) {
14
- if (typeof data[i] === 'object') {
15
- sendData.push(JSON.stringify(data[i]))
64
+ const type = typeof data[i]
65
+ let len
66
+ if (data[i] == null) {
67
+ poolBuffer[poolOffset++] = 31
68
+ len = 0
69
+ } else if (type === 'object') {
70
+ poolBuffer[poolOffset++] = 31
71
+ len = writeString(poolBuffer, JSON.stringify(data[i]), poolOffset)
72
+ } else if (type === 'bigint') {
73
+ poolBuffer[poolOffset++] = 31
74
+ poolView.setBigUint64(poolOffset, data[i], false)
75
+ len = 8
76
+ } else if (type === 'string') {
77
+ poolBuffer[poolOffset++] = 31
78
+ len = writeString(poolBuffer, data[i], poolOffset)
16
79
  } else {
17
- sendData.push(data[i])
80
+ throw new Error('invalid data')
81
+ }
82
+ poolOffset += len
83
+
84
+ varint.encode(len + 1, poolBuffer, headerPos)
85
+ headerPos += varint.encode.bytes
86
+ if (headerPos - start >= headerSize) {
87
+ throw new Error(`header too large: ${headerPos - start} ${headerSize}`)
88
+ }
89
+
90
+ if (poolOffset >= poolBuffer.length) {
91
+ allocPool(start === 0 ? poolSize * 2 : poolSize)
92
+ return getMsg(topic, action, data)
18
93
  }
19
94
  }
20
95
  }
21
96
 
22
- return sendData.join(SEP)
97
+ return new Uint8Array(poolBuffer.buffer, start, poolOffset - start)
23
98
  }
24
99
 
25
100
  export function typed(value) {