@nxtedition/deepstream.io-client-js 26.0.18 → 26.0.21

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.18",
3
+ "version": "26.0.21",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "type": "module",
@@ -2,7 +2,6 @@ import * as utils from '../utils/utils.js'
2
2
  import * as messageParser from './message-parser.js'
3
3
  import * as messageBuilder from './message-builder.js'
4
4
  import * as C from '../constants/constants.js'
5
- import xxhash from 'xxhash-wasm'
6
5
  import FixedQueue from '../utils/fixed-queue.js'
7
6
  import Emitter from 'component-emitter2'
8
7
 
@@ -38,12 +37,6 @@ export default function Connection(client, url, options) {
38
37
  this._url = new URL(url)
39
38
 
40
39
  this._state = C.CONNECTION_STATE.CLOSED
41
-
42
- this.hasher = null
43
- xxhash().then((hasher) => {
44
- this.hasher = hasher
45
- this._createEndpoint()
46
- })
47
40
  }
48
41
 
49
42
  Emitter(Connection.prototype)
@@ -1,25 +1,104 @@
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
+ for (let n = 0; n < headerSize; n++) {
55
+ poolBuffer[poolOffset++] = 0
56
+ }
57
+
58
+ let headerPos = start
59
+ poolBuffer[headerPos++] = 128 + headerSize
60
+
61
+ poolBuffer[poolOffset++] = topic.charCodeAt(0)
62
+ poolBuffer[poolOffset++] = 31
63
+ for (let n = 0; n < action.length; n++) {
64
+ poolBuffer[poolOffset++] = action.charCodeAt(n)
65
+ }
11
66
 
12
67
  if (data) {
13
68
  for (let i = 0; i < data.length; i++) {
14
- if (typeof data[i] === 'object') {
15
- sendData.push(JSON.stringify(data[i]))
69
+ const type = typeof data[i]
70
+ let len
71
+ if (data[i] == null) {
72
+ poolBuffer[poolOffset++] = 31
73
+ len = 0
74
+ } else if (type === 'object') {
75
+ poolBuffer[poolOffset++] = 31
76
+ len = writeString(poolBuffer, JSON.stringify(data[i]), poolOffset)
77
+ } else if (type === 'bigint') {
78
+ poolBuffer[poolOffset++] = 31
79
+ poolView.setBigUint64(poolOffset, data[i], false)
80
+ len = 8
81
+ } else if (type === 'string') {
82
+ poolBuffer[poolOffset++] = 31
83
+ len = writeString(poolBuffer, data[i], poolOffset)
16
84
  } else {
17
- sendData.push(data[i])
85
+ throw new Error('invalid data')
86
+ }
87
+ poolOffset += len
88
+
89
+ varint.encode(len + 1, poolBuffer, headerPos)
90
+ headerPos += varint.encode.bytes
91
+ if (headerPos - start >= headerSize) {
92
+ throw new Error('header too large')
93
+ }
94
+
95
+ if (poolOffset >= poolSize) {
96
+ throw new Error('message too large')
18
97
  }
19
98
  }
20
99
  }
21
100
 
22
- return sendData.join(SEP)
101
+ return new Uint8Array(poolBuffer.buffer, start, poolOffset - start)
23
102
  }
24
103
 
25
104
  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)
@@ -1,5 +1,6 @@
1
+ import * as rxjs from 'rxjs'
1
2
  import * as C from '../constants/constants.js'
2
- import rxjs from 'rxjs'
3
+ import { h64ToString } from '../utils/utils.js'
3
4
 
4
5
  export default class Listener {
5
6
  constructor(topic, pattern, callback, handler, { recursive = false, stringify = null } = {}) {
@@ -151,7 +152,7 @@ export default class Listener {
151
152
  }
152
153
 
153
154
  const body = typeof value !== 'string' ? this._stringify(value) : value
154
- const hash = this._connection.hasher.h64ToString(body)
155
+ const hash = h64ToString(body)
155
156
  const version = `INF-${hash}`
156
157
 
157
158
  if (provider.version !== version) {
@@ -1,9 +1,9 @@
1
+ import * as rxjs from 'rxjs'
1
2
  import * as C from '../constants/constants.js'
2
- import rx from 'rxjs/operators'
3
- import rxjs from 'rxjs'
3
+ import { h64ToString } from '../utils/utils.js'
4
4
 
5
- const PIPE = rxjs.pipe(
6
- rx.map((value) => {
5
+ const valuePipe = rxjs.pipe(
6
+ rxjs.map((value) => {
7
7
  let data
8
8
  if (value && typeof value === 'string') {
9
9
  if (value.charAt(0) !== '{' && value.charAt(0) !== '[') {
@@ -18,7 +18,7 @@ const PIPE = rxjs.pipe(
18
18
 
19
19
  return data
20
20
  }),
21
- rx.distinctUntilChanged(),
21
+ rxjs.distinctUntilChanged(),
22
22
  )
23
23
 
24
24
  export default class Listener {
@@ -69,14 +69,14 @@ export default class Listener {
69
69
  }
70
70
 
71
71
  if (value$) {
72
- const subscription = value$.pipe(PIPE).subscribe({
72
+ const subscription = value$.pipe(valuePipe).subscribe({
73
73
  next: (data) => {
74
74
  if (data == null) {
75
75
  this._connection.sendMsg(this._topic, C.ACTIONS.LISTEN_REJECT, [this._pattern, name])
76
76
  this._subscriptions.delete(name)
77
77
  subscription.unsubscribe()
78
78
  } else {
79
- const version = `INF-${this._connection.hasher.h64ToString(data)}`
79
+ const version = `INF-${h64ToString(data)}`
80
80
  this._connection.sendMsg(this._topic, C.ACTIONS.UPDATE, [name, version, data])
81
81
  }
82
82
  },
@@ -1,3 +1,5 @@
1
+ import xxhash from 'xxhash-wasm'
2
+
1
3
  const NODE_ENV = typeof process !== 'undefined' && process.env && process.env.NODE_ENV
2
4
  export const isNode = typeof process !== 'undefined' && process.toString() === '[object process]'
3
5
  export const isProduction = NODE_ENV === 'production'
@@ -174,3 +176,17 @@ export function removeAbortListener(signal, handler) {
174
176
  }
175
177
  }
176
178
  }
179
+
180
+ const HASHER = await xxhash()
181
+
182
+ export function h64(str) {
183
+ return HASHER.h64(str)
184
+ }
185
+
186
+ export function h64ToString(str) {
187
+ return HASHER.h64ToString(str)
188
+ }
189
+
190
+ export function h64Raw(str) {
191
+ return HASHER.h64Raw(str)
192
+ }