@nxtedition/deepstream.io-client-js 25.0.0 → 25.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
|
@@ -209,9 +209,10 @@ Connection.prototype._onClose = function () {
|
|
|
209
209
|
Connection.prototype._onMessage = function (raw) {
|
|
210
210
|
if (typeof raw === 'string') {
|
|
211
211
|
raw = this._encoder.encode(raw)
|
|
212
|
+
} else if (!utils.isNode) {
|
|
213
|
+
raw = new Uint8Array(raw)
|
|
212
214
|
}
|
|
213
215
|
|
|
214
|
-
raw = new Uint8Array(raw)
|
|
215
216
|
const len = raw.byteLength
|
|
216
217
|
|
|
217
218
|
const start = 0
|
|
@@ -224,11 +225,13 @@ Connection.prototype._onMessage = function (raw) {
|
|
|
224
225
|
pos += headerSize
|
|
225
226
|
}
|
|
226
227
|
|
|
228
|
+
// TODO (perf): Use numbers instead of string..
|
|
227
229
|
const topic = String.fromCharCode(raw[pos++])
|
|
228
230
|
pos++
|
|
229
231
|
|
|
230
232
|
let action = ''
|
|
231
233
|
while (pos < len && raw[pos] !== 31) {
|
|
234
|
+
// TODO (perf): Use numbers instead of string..
|
|
232
235
|
action += String.fromCharCode(raw[pos++])
|
|
233
236
|
}
|
|
234
237
|
pos++
|
|
@@ -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 poolBuffer.write(str, poolOffset)
|
|
32
|
+
} else {
|
|
33
|
+
const res = poolEncoder.encodeInto(str, new Uint8Array(poolBuffer.buffer, poolOffset))
|
|
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
|
}
|