Skip to content

Commit c03656c

Browse files
committed
feat: v27 proto
1 parent bfb8895 commit c03656c

File tree

5 files changed

+48
-29
lines changed

5 files changed

+48
-29
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"xxhash-wasm": "^1.0.2"
7272
},
7373
"devDependencies": {
74+
"@types/node": "^22.0.0",
7475
"eslint": "^8.0.0",
7576
"eslint-config-prettier": "^9.1.0",
7677
"eslint-config-standard": "^17.1.0",

src/message/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ Connection.prototype._sendAuthParams = function () {
161161
this._setState(C.CONNECTION_STATE.AUTHENTICATING)
162162
const authMessage = messageBuilder.getMsg(C.TOPIC.AUTH, C.ACTIONS.REQUEST, [
163163
this._authParams,
164-
'26.0.0',
164+
'27.0.0',
165165
utils.isNode
166166
? `Node/${process.version}`
167167
: globalThis.navigator && globalThis.navigator.userAgent,

src/record/record.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import invariant from 'invariant'
77
import cloneDeep from 'lodash.clonedeep'
88
import * as timers from '../utils/timers.js'
99

10-
class Record {
10+
export default class Record {
1111
static STATE = C.RECORD_STATE
1212

1313
constructor(name, handler) {
1414
const connection = handler._connection
1515

1616
this._handler = handler
1717
this._name = name
18+
this._key = utils.h64(name)
1819
this._version = ''
1920
this._data = jsonPath.EMPTY
2021
this._state = C.RECORD_STATE.VOID
@@ -24,7 +25,15 @@ class Record {
2425

2526
/** @type Map? */ this._updating = null
2627
/** @type Array? */ this._patching = null
27-
this._subscribed = connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SUBSCRIBE, [this._name])
28+
this._subscribed = connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SUBSCRIBE, [
29+
this._key,
30+
this._name,
31+
])
32+
}
33+
34+
/** @type {bigint} */
35+
get key() {
36+
return this._key
2837
}
2938

3039
/** @type {string} */
@@ -62,7 +71,8 @@ class Record {
6271
if (this._refs === 1) {
6372
this._handler._onPruning(this, false)
6473
this._subscribed =
65-
this._subscribed || connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SUBSCRIBE, [this._name])
74+
this._subscribed ||
75+
connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SUBSCRIBE, [this._key, this._name])
6676
}
6777
return this
6878
}
@@ -324,7 +334,8 @@ class Record {
324334

325335
if (connected) {
326336
this._subscribed =
327-
this._refs > 0 && connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SUBSCRIBE, [this._name])
337+
this._refs > 0 &&
338+
connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SUBSCRIBE, [this._key, this._name])
328339

329340
if (this._updating) {
330341
for (const update of this._updating.values()) {
@@ -349,7 +360,7 @@ class Record {
349360
invariant(!this._updating, 'must not have updates')
350361

351362
if (this._subscribed) {
352-
connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.UNSUBSCRIBE, [this._name])
363+
connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.UNSUBSCRIBE, [this._key])
353364
this._subscribed = false
354365
}
355366

@@ -371,7 +382,7 @@ class Record {
371382
const prevVersion = this._version
372383
const nextVersion = this._makeVersion(parseInt(prevVersion) + 1)
373384

374-
const update = [this._name, nextVersion, jsonPath.stringify(nextData), prevVersion]
385+
const update = [this._key, nextVersion, jsonPath.stringify(nextData), prevVersion]
375386

376387
if (!this._updating) {
377388
this._onUpdating(true)
@@ -573,5 +584,3 @@ Object.defineProperty(Record.prototype, 'hasProvider', {
573584
return this.state >= C.RECORD_STATE.PROVIDER
574585
},
575586
})
576-
577-
export default Record

src/utils/multicast-listener.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as rxjs from 'rxjs'
22
import * as C from '../constants/constants.js'
3-
import { h64ToString } from '../utils/utils.js'
3+
import { h64, h64ToString } from '../utils/utils.js'
44

55
export default class Listener {
66
constructor(topic, pattern, callback, handler, { recursive = false, stringify = null } = {}) {
@@ -48,15 +48,20 @@ export default class Listener {
4848

4949
const name = message.data[1]
5050

51+
// TOOD (fix): Validate name
52+
53+
const key = h64(name)
54+
5155
if (message.action === C.ACTIONS.SUBSCRIPTION_FOR_PATTERN_FOUND) {
52-
if (this._subscriptions.has(name)) {
56+
if (this._subscriptions.has(key)) {
5357
this._error(name, 'invalid add: listener exists')
5458
return
5559
}
5660

5761
// TODO (refactor): Move to class
5862
const provider = {
5963
name,
64+
key,
6065
value$: null,
6166
sending: false,
6267
accepted: false,
@@ -69,7 +74,7 @@ export default class Listener {
6974
if (this.connected && provider.accepted) {
7075
this._connection.sendMsg(this._topic, C.ACTIONS.LISTEN_REJECT, [
7176
this._pattern,
72-
provider.name,
77+
provider.key,
7378
])
7479
}
7580

@@ -102,7 +107,7 @@ export default class Listener {
102107
this._connection.sendMsg(
103108
this._topic,
104109
accepted ? C.ACTIONS.LISTEN_ACCEPT : C.ACTIONS.LISTEN_REJECT,
105-
[this._pattern, provider.name],
110+
[this._pattern, provider.key],
106111
)
107112

108113
provider.version = null
@@ -158,7 +163,7 @@ export default class Listener {
158163
if (provider.version !== version) {
159164
provider.version = version
160165
this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.UPDATE, [
161-
provider.name,
166+
provider.key,
162167
version,
163168
body,
164169
])
@@ -182,9 +187,9 @@ export default class Listener {
182187

183188
provider.start()
184189

185-
this._subscriptions.set(provider.name, provider)
190+
this._subscriptions.set(provider.key, provider)
186191
} else if (message.action === C.ACTIONS.LISTEN_ACCEPT) {
187-
const provider = this._subscriptions.get(name)
192+
const provider = this._subscriptions.get(key)
188193
if (!provider?.value$) {
189194
return
190195
}
@@ -196,13 +201,13 @@ export default class Listener {
196201
provider.valueSubscription = provider.value$.subscribe(provider.observer)
197202
}
198203
} else if (message.action === C.ACTIONS.SUBSCRIPTION_FOR_PATTERN_REMOVED) {
199-
const provider = this._subscriptions.get(name)
204+
const provider = this._subscriptions.get(key)
200205

201206
if (!provider) {
202207
this._error(name, 'invalid remove: listener missing')
203208
} else {
204209
provider.stop()
205-
this._subscriptions.delete(provider.name)
210+
this._subscriptions.delete(provider.key)
206211
}
207212
} else {
208213
return false

src/utils/unicast-listener.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as rxjs from 'rxjs'
22
import * as C from '../constants/constants.js'
3-
import { h64ToString } from '../utils/utils.js'
3+
import { h64, h64ToString } from '../utils/utils.js'
44

55
const valuePipe = rxjs.pipe(
66
rxjs.map((value) => {
@@ -55,8 +55,12 @@ export default class Listener {
5555
_$onMessage(message) {
5656
const name = message.data[1]
5757

58+
// TODO (fix): Validate name
59+
60+
const key = h64(name)
61+
5862
if (message.action === C.ACTIONS.LISTEN_ACCEPT) {
59-
if (this._subscriptions.has(name)) {
63+
if (this._subscriptions.has(key)) {
6064
this._error(name, 'invalid accept: listener exists')
6165
return
6266
}
@@ -72,29 +76,29 @@ export default class Listener {
7276
const subscription = value$.pipe(valuePipe).subscribe({
7377
next: (data) => {
7478
if (data == null) {
75-
this._connection.sendMsg(this._topic, C.ACTIONS.LISTEN_REJECT, [this._pattern, name])
76-
this._subscriptions.delete(name)
79+
this._connection.sendMsg(this._topic, C.ACTIONS.LISTEN_REJECT, [this._pattern, key])
80+
this._subscriptions.delete(key)
7781
subscription.unsubscribe()
7882
} else {
7983
const version = `INF-${h64ToString(data)}`
80-
this._connection.sendMsg(this._topic, C.ACTIONS.UPDATE, [name, version, data])
84+
this._connection.sendMsg(this._topic, C.ACTIONS.UPDATE, [key, version, data])
8185
}
8286
},
8387
error: (err) => {
8488
this._error(name, err)
85-
this._connection.sendMsg(this._topic, C.ACTIONS.LISTEN_REJECT, [this._pattern, name])
86-
this._subscriptions.delete(name)
89+
this._connection.sendMsg(this._topic, C.ACTIONS.LISTEN_REJECT, [this._pattern, key])
90+
this._subscriptions.delete(key)
8791
},
8892
})
89-
this._subscriptions.set(name, subscription)
93+
this._subscriptions.set(key, subscription)
9094
} else {
91-
this._connection.sendMsg(this._topic, C.ACTIONS.LISTEN_REJECT, [this._pattern, name])
95+
this._connection.sendMsg(this._topic, C.ACTIONS.LISTEN_REJECT, [this._pattern, key])
9296
}
9397
} else if (message.action === C.ACTIONS.LISTEN_REJECT) {
94-
const subscription = this._subscriptions.get(name)
98+
const subscription = this._subscriptions.get(key)
9599

96100
if (subscription) {
97-
this._subscriptions.delete(name)
101+
this._subscriptions.delete(key)
98102
subscription.unsubscribe()
99103
} else {
100104
this._error(name, 'invalid remove: listener missing')

0 commit comments

Comments
 (0)