Yjs varint encoding:修订间差异

来自WHY42
Riguz留言 | 贡献
创建页面,内容为“<ref>https://github.com/yjs/y-protocols/blob/master/PROTOCOL.md</ref> Category:Yjs Category:CRDT
 
Riguz留言 | 贡献
无编辑摘要
标签2017版源代码编辑
 
(未显示同一用户的3个中间版本)
第1行: 第1行:
<ref>https://github.com/yjs/y-protocols/blob/master/PROTOCOL.md</ref>
y-protocol defines protocol that is used when exchanging data between yjs clients<ref>https://github.com/yjs/y-protocols/blob/master/PROTOCOL.md</ref>.


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<ref>https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER</ref>.
<syntaxhighlight lang="python">
123456=0b111|1000100|1000000
->(1)1000000|(1)1000100|(0)____111
-> 0xc0    | 0xc4    | 0x07
</syntaxhighlight>
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:
* <syntaxhighlight lang="java" inline>varUint(number)</syntaxhighlight>
* <syntaxhighlight lang="java" inline>varByteArray(buffer) := varUint(length(buffer)) • buffer</syntaxhighlight>
* <syntaxhighlight lang="java" inline>utf8(string)</syntaxhighlight>
* <syntaxhighlight lang="java" inline>varString(string) := varByteArray(utf8(string))</syntaxhighlight>
* <syntaxhighlight lang="java" inline>json(object) := varString(JSON.stringify(object))</syntaxhighlight>
Online tool to calculate varUint encoding:  https://bluecrewforensics.com/varint-converter/


[[Category:Yjs]]
[[Category:Yjs]]
[[Category:CRDT]]
[[Category:CRDT]]

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/