@nxtedition/deepstream.io-client-js 26.0.17 → 26.0.19

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.17",
3
+ "version": "26.0.19",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "type": "module",
@@ -1,25 +1,101 @@
1
1
  import * as C from '../constants/constants.js'
2
+ import * as utils from '../utils/utils.js'
3
+ import varint from 'varint'
2
4
 
3
- const SEP = C.MESSAGE_PART_SEPERATOR
5
+ const poolEncoder = new globalThis.TextEncoder()
6
+
7
+ // TODO (fix): Don't assume maxMesageSize is 1MB
8
+ const maxMessageSize = 1024 * 1024
9
+ const poolSize = maxMessageSize * 4
10
+
11
+ let poolBuffer
12
+ let poolView
13
+ let poolOffset
14
+
15
+ function reallocPool() {
16
+ poolBuffer = utils.isNode
17
+ ? globalThis.Buffer.allocUnsafe(poolSize)
18
+ : new Uint8Array(new ArrayBuffer(poolSize))
19
+ poolView = new DataView(poolBuffer.buffer)
20
+ poolOffset = 0
21
+ }
22
+
23
+ function alignPool() {
24
+ // Ensure aligned slices
25
+ if (poolOffset & 0x7) {
26
+ poolOffset |= 0x7
27
+ poolOffset++
28
+ }
29
+ }
30
+
31
+ function writeString(dst, str, offset) {
32
+ if (utils.isNode) {
33
+ return dst.write(str, offset)
34
+ } else {
35
+ const res = poolEncoder.encodeInto(str, new Uint8Array(dst.buffer, offset))
36
+ return res.written
37
+ }
38
+ }
4
39
 
5
40
  export function getMsg(topic, action, data) {
6
41
  if (data && !(data instanceof Array)) {
7
42
  throw new Error('data must be an array')
8
43
  }
9
44
 
10
- const sendData = [topic, action]
45
+ if (!poolBuffer || poolOffset + maxMessageSize > poolSize) {
46
+ reallocPool()
47
+ } else {
48
+ alignPool()
49
+ }
50
+
51
+ const start = poolOffset
52
+
53
+ const headerSize = 8
54
+ poolBuffer[poolOffset++] = 128 + headerSize
55
+ let headerPos = poolOffset
56
+ poolOffset += headerSize - 1
57
+
58
+ poolBuffer[poolOffset++] = topic.charCodeAt(0)
59
+ poolBuffer[poolOffset++] = 31
60
+ for (let n = 0; n < action.length; n++) {
61
+ poolBuffer[poolOffset++] = action.charCodeAt(n)
62
+ }
11
63
 
12
64
  if (data) {
13
65
  for (let i = 0; i < data.length; i++) {
14
- if (typeof data[i] === 'object') {
15
- sendData.push(JSON.stringify(data[i]))
66
+ const type = typeof data[i]
67
+ let len
68
+ if (data[i] == null) {
69
+ poolBuffer[poolOffset++] = 31
70
+ len = 0
71
+ } else if (type === 'object') {
72
+ poolBuffer[poolOffset++] = 31
73
+ len = writeString(poolBuffer, JSON.stringify(data[i]), poolOffset)
74
+ } else if (type === 'bigint') {
75
+ poolBuffer[poolOffset++] = 31
76
+ poolView.setBigUint64(poolOffset, data[i], false)
77
+ len = 8
78
+ } else if (type === 'string') {
79
+ poolBuffer[poolOffset++] = 31
80
+ len = writeString(poolBuffer, data[i], poolOffset)
16
81
  } else {
17
- sendData.push(data[i])
82
+ throw new Error('invalid data')
83
+ }
84
+ poolOffset += len
85
+
86
+ varint.encode(len + 1, poolBuffer, headerPos)
87
+ headerPos += varint.encode.bytes
88
+ if (headerPos - start >= headerSize) {
89
+ throw new Error('header too large')
90
+ }
91
+
92
+ if (poolOffset >= poolSize) {
93
+ throw new Error('message too large')
18
94
  }
19
95
  }
20
96
  }
21
97
 
22
- return sendData.join(SEP)
98
+ return new Uint8Array(poolBuffer.buffer, start, poolOffset - start)
23
99
  }
24
100
 
25
101
  export function typed(value) {
@@ -97,11 +97,19 @@ class RecordHandler {
97
97
  this._connection = connection
98
98
  this._client = client
99
99
  this._records = new Map()
100
+ this._cache = new Map()
100
101
  this._listeners = new Map()
101
102
  this._pruning = new Set()
102
103
  this._patching = new Map()
103
104
  this._updating = new Map()
104
105
 
106
+ this._registry = new FinalizationRegistry((name) => {
107
+ const entry = this._cache.get(name)
108
+ if (entry && entry.deref && entry.deref() === undefined) {
109
+ this._cache.delete(name)
110
+ }
111
+ })
112
+
105
113
  this._connected = 0
106
114
  this._stats = {
107
115
  updating: 0,
@@ -134,6 +142,11 @@ class RecordHandler {
134
142
  for (const rec of pruning) {
135
143
  rec._$dispose()
136
144
  this._records.delete(rec.name)
145
+
146
+ if (!this._cache.has(rec.name)) {
147
+ this._cache.set(rec.name, new WeakRef(rec))
148
+ this._registry.register(rec, rec.name)
149
+ }
137
150
  }
138
151
 
139
152
  this._stats.pruning -= pruning.size
@@ -219,7 +232,7 @@ class RecordHandler {
219
232
  let record = this._records.get(name)
220
233
 
221
234
  if (!record) {
222
- record = new Record(name, this)
235
+ record = this._cache.get(name)?.deref() ?? new Record(name, this)
223
236
  this._stats.records += 1
224
237
  this._stats.created += 1
225
238
  this._records.set(name, record)
@@ -389,7 +389,7 @@ class Record {
389
389
  this._version = nextVersion
390
390
  }
391
391
 
392
- _onUpdate([, version, data]) {
392
+ _onUpdate([, version, data, hasProvider]) {
393
393
  const prevData = this._data
394
394
  const prevVersion = this._version
395
395
  const prevState = this._state
@@ -422,7 +422,9 @@ class Record {
422
422
  this._onPatching(false)
423
423
  }
424
424
 
425
- if (this._state < C.RECORD_STATE.SERVER) {
425
+ if (hasProvider === 'T') {
426
+ this._state = C.RECORD_STATE.PROVIDER
427
+ } else if (this._state < C.RECORD_STATE.SERVER) {
426
428
  this._state = this._version.charAt(0) === 'I' ? C.RECORD_STATE.STALE : C.RECORD_STATE.SERVER
427
429
  }
428
430