@nxtedition/deepstream.io-client-js 25.0.1 → 25.0.3
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 +1 -1
- package/src/message/message-builder.js +18 -12
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as C from '../constants/constants.js'
|
|
2
2
|
import varint from 'varint'
|
|
3
|
+
import * as utils from '../utils/utils.js'
|
|
3
4
|
|
|
4
5
|
const poolEncoder = new TextEncoder()
|
|
5
6
|
|
|
@@ -8,9 +9,11 @@ let poolBuffer
|
|
|
8
9
|
let poolView
|
|
9
10
|
let poolOffset
|
|
10
11
|
|
|
11
|
-
function
|
|
12
|
+
function allocPool(size) {
|
|
12
13
|
poolSize = size || poolSize || 1024 * 1024
|
|
13
|
-
poolBuffer =
|
|
14
|
+
poolBuffer = utils.isNode
|
|
15
|
+
? globalThis.Buffer.allocUnsafe(poolSize)
|
|
16
|
+
: new Uint8Array(new ArrayBuffer(poolSize))
|
|
14
17
|
poolView = new DataView(poolBuffer.buffer)
|
|
15
18
|
poolOffset = 0
|
|
16
19
|
}
|
|
@@ -23,13 +26,22 @@ function alignPool() {
|
|
|
23
26
|
}
|
|
24
27
|
}
|
|
25
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
|
+
}
|
|
37
|
+
|
|
26
38
|
export function getMsg(topic, action, data) {
|
|
27
39
|
if (data && !(data instanceof Array)) {
|
|
28
40
|
throw new Error('data must be an array')
|
|
29
41
|
}
|
|
30
42
|
|
|
31
43
|
if (!poolSize || poolOffset + poolSize / 16 >= poolSize) {
|
|
32
|
-
|
|
44
|
+
allocPool()
|
|
33
45
|
} else {
|
|
34
46
|
alignPool()
|
|
35
47
|
}
|
|
@@ -56,23 +68,17 @@ export function getMsg(topic, action, data) {
|
|
|
56
68
|
len = 0
|
|
57
69
|
} else if (type === 'object') {
|
|
58
70
|
poolBuffer[poolOffset++] = 31
|
|
59
|
-
|
|
60
|
-
JSON.stringify(data[i]),
|
|
61
|
-
new Uint8Array(poolBuffer.buffer, poolOffset)
|
|
62
|
-
)
|
|
63
|
-
len = res.written
|
|
71
|
+
len = writeString(poolBuffer, JSON.stringify(data[i]), poolOffset)
|
|
64
72
|
} else if (type === 'bigint') {
|
|
65
73
|
poolBuffer[poolOffset++] = 31
|
|
66
74
|
poolView.setBigUint64(poolOffset, data[i], false)
|
|
67
75
|
len = 8
|
|
68
76
|
} else if (type === 'string') {
|
|
69
77
|
poolBuffer[poolOffset++] = 31
|
|
70
|
-
|
|
71
|
-
len = res.written
|
|
78
|
+
len = writeString(poolBuffer, data[i], poolOffset)
|
|
72
79
|
} else {
|
|
73
80
|
throw new Error('invalid data')
|
|
74
81
|
}
|
|
75
|
-
|
|
76
82
|
poolOffset += len
|
|
77
83
|
|
|
78
84
|
varint.encode(len + 1, poolBuffer, headerPos)
|
|
@@ -82,7 +88,7 @@ export function getMsg(topic, action, data) {
|
|
|
82
88
|
}
|
|
83
89
|
|
|
84
90
|
if (poolOffset >= poolBuffer.length) {
|
|
85
|
-
|
|
91
|
+
allocPool(start === 0 ? poolSize * 2 : poolSize)
|
|
86
92
|
return getMsg(topic, action, data)
|
|
87
93
|
}
|
|
88
94
|
}
|