@nxtedition/deepstream.io-client-js 28.0.1 → 28.1.0

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": "28.0.1",
3
+ "version": "28.1.0",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "type": "module",
@@ -81,6 +81,7 @@ ACTIONS.CHALLENGE = 'CH'
81
81
  ACTIONS.CHALLENGE_RESPONSE = 'CHR'
82
82
  ACTIONS.READ = 'R'
83
83
  ACTIONS.UPDATE = 'U'
84
+ ACTIONS.PUT = 'P'
84
85
  ACTIONS.SUBSCRIBE = 'S'
85
86
  ACTIONS.SYNC = 'SY'
86
87
  ACTIONS.UNSUBSCRIBE = 'US'
@@ -1,81 +1,25 @@
1
1
  import * as C from '../constants/constants.js'
2
- import * as utils from '../utils/utils.js'
3
2
 
4
- const poolEncoder = new globalThis.TextEncoder()
5
-
6
- // TODO (fix): Don't assume maxMesageSize is 1MB
7
- const maxMessageSize = 1024 * 1024
8
- const poolSize = maxMessageSize * 8
9
-
10
- let poolBuffer
11
- let poolOffset
12
-
13
- function reallocPool() {
14
- poolBuffer = utils.isNode
15
- ? globalThis.Buffer.allocUnsafe(poolSize)
16
- : new Uint8Array(new ArrayBuffer(poolSize))
17
- poolOffset = 0
18
- }
19
-
20
- function alignPool() {
21
- // Ensure aligned slices
22
- if (poolOffset & 0x7) {
23
- poolOffset |= 0x7
24
- poolOffset++
25
- }
26
- }
27
-
28
- function writeString(dst, str, offset) {
29
- if (utils.isNode) {
30
- return dst.write(str, offset)
31
- } else {
32
- const res = poolEncoder.encodeInto(str, new Uint8Array(dst.buffer, offset))
33
- return res.written
34
- }
35
- }
3
+ const SEP = C.MESSAGE_PART_SEPERATOR
36
4
 
37
5
  export function getMsg(topic, action, data) {
38
6
  if (data && !(data instanceof Array)) {
39
7
  throw new Error('data must be an array')
40
8
  }
41
9
 
42
- if (!poolBuffer || poolOffset + maxMessageSize > poolSize) {
43
- reallocPool()
44
- } else {
45
- alignPool()
46
- }
47
-
48
- const start = poolOffset
49
-
50
- poolBuffer[poolOffset++] = topic.charCodeAt(0)
51
- poolBuffer[poolOffset++] = 31
52
- for (let n = 0; n < action.length; n++) {
53
- poolBuffer[poolOffset++] = action.charCodeAt(n)
54
- }
10
+ const sendData = [topic, action]
55
11
 
56
12
  if (data) {
57
13
  for (let i = 0; i < data.length; i++) {
58
- const type = typeof data[i]
59
- if (data[i] == null) {
60
- poolBuffer[poolOffset++] = 31
61
- poolOffset += 0
62
- } else if (type === 'object') {
63
- poolBuffer[poolOffset++] = 31
64
- poolOffset += writeString(poolBuffer, JSON.stringify(data[i]), poolOffset)
65
- } else if (type === 'string') {
66
- poolBuffer[poolOffset++] = 31
67
- poolOffset += writeString(poolBuffer, data[i], poolOffset)
14
+ if (typeof data[i] === 'object') {
15
+ sendData.push(JSON.stringify(data[i]))
68
16
  } else {
69
- throw new Error('invalid data')
70
- }
71
-
72
- if (poolOffset >= poolSize) {
73
- throw new Error('message too large')
17
+ sendData.push(data[i])
74
18
  }
75
19
  }
76
20
  }
77
21
 
78
- return new Uint8Array(poolBuffer.buffer, start, poolOffset - start)
22
+ return sendData.join(SEP)
79
23
  }
80
24
 
81
25
  export function typed(value) {
@@ -398,6 +398,15 @@ class RecordHandler {
398
398
  }
399
399
  }
400
400
 
401
+ put(name, ...args) {
402
+ const record = this.getRecord(name)
403
+ try {
404
+ return record.put(...args)
405
+ } finally {
406
+ record.unref()
407
+ }
408
+ }
409
+
401
410
  /**
402
411
  *
403
412
  * @param {*} name
@@ -183,6 +183,26 @@ class Record {
183
183
  }
184
184
  }
185
185
 
186
+ put(name, version, data) {
187
+ const connection = this._handler._connection
188
+
189
+ if (typeof name !== 'string') {
190
+ throw new Error('invalid argument: name')
191
+ }
192
+
193
+ if (typeof version !== 'string' || !/^\d+-.+/.test(version)) {
194
+ throw new Error('invalid argument: version')
195
+ }
196
+
197
+ if (!utils.isPlainObject(data)) {
198
+ throw new Error('invalid argument: data')
199
+ }
200
+
201
+ connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.PUT, [name, version, jsonPath.stringify(data)])
202
+
203
+ this._onUpdate([name, version, jsonPath.stringify(data), 'F'])
204
+ }
205
+
186
206
  when(stateOrNil, optionsOrNil) {
187
207
  invariant(this._refs > 0, 'missing refs')
188
208