Yjs varint encoding

来自WHY42
Riguz留言 | 贡献2024年10月24日 (四) 23:56的版本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

y-protocol defines protocol that is used when exchanging data between yjs clients[1].

Yjs uses varint to encode content, which is similar in protobuf. Yjs encodes unsigned integer within range of 0-2^53-1, which is the max number supported by javascript[2].

123456=0b111|1000100|1000000
->(1)1000000|(1)1000100|(0)____111
-> 0xc0     | 0xc4     | 0x07

The first bit indicates that whether there's a continuous byte, if it's 0, then the number is finished.

In short, the protocol defines the following methods:

  • varUint(number)
  • varByteArray(buffer) := varUint(length(buffer)) buffer
  • utf8(string)
  • varString(string) := varByteArray(utf8(string))
  • json(object) := varString(JSON.stringify(object))

Online tool to calculate varUint encoding: https://bluecrewforensics.com/varint-converter/