binary encoding bugfixes & export BinaryEncoder + BinaryDecoder

This commit is contained in:
Kevin Jahns 2017-07-24 15:37:04 +02:00
parent c97130abc4
commit eb4fb3a225
4 changed files with 19 additions and 9 deletions

View File

@ -12,6 +12,7 @@ export default function extendConnector (Y/* :any */) {
if (opts == null) {
opts = {}
}
this.opts = opts
// Prefer to receive untransformed operations. This does only work if
// this client receives operations from only one other client.
// In particular, this does not work with y-webrtc.
@ -152,6 +153,7 @@ export default function extendConnector (Y/* :any */) {
this.currentSyncTarget = syncUser
this.y.db.requestTransaction(function * () {
let encoder = new BinaryEncoder()
encoder.writeVarString(conn.opts.room || '')
encoder.writeVarString('sync step 1')
encoder.writeVarString(conn.authInfo || '')
encoder.writeVarUint(conn.protocolVersion)
@ -197,6 +199,7 @@ export default function extendConnector (Y/* :any */) {
function broadcastOperations () {
if (self.broadcastOpBuffer.length > 0) {
let encoder = new BinaryEncoder()
encoder.writeVarString(self.opts.room)
encoder.writeVarString('update')
let ops = self.broadcastOpBuffer
self.broadcastOpBuffer = []
@ -225,6 +228,8 @@ export default function extendConnector (Y/* :any */) {
}
let decoder = new BinaryDecoder(buffer)
let encoder = new BinaryEncoder()
let roomname = decoder.readVarString() // read room name
encoder.writeVarString(roomname)
let messageType = decoder.readVarString()
let senderConn = this.connections.get(sender)

View File

@ -4,6 +4,7 @@ import { BinaryDecoder } from './Encoding.js'
export function formatYjsMessage (buffer) {
let decoder = new BinaryDecoder(buffer)
decoder.readVarString() // read roomname
let type = decoder.readVarString()
let strBuilder = []
strBuilder.push('\n === ' + type + ' ===\n')
@ -108,7 +109,7 @@ export function logSS (decoder, strBuilder) {
for (let i = 0; i < len; i++) {
let user = decoder.readVarUint()
let clock = decoder.readVarUint()
strBuilder.push(` - ${user}: ${clock}`)
strBuilder.push(` ${user}: ${clock}\n`)
}
}

View File

@ -1061,8 +1061,8 @@ export default function extendTransaction (Y) {
let ss = new Map()
let ssLength = decoder.readUint32()
for (let i = 0; i < ssLength; i++) {
let user = decoder.readUint32()
let clock = decoder.readUint32()
let user = decoder.readVarUint()
let clock = decoder.readVarUint()
ss.set(user, clock)
}
let ops = yield * this.getOperations(ss)
@ -1093,7 +1093,7 @@ export default function extendTransaction (Y) {
* applyOperationsUntransformed (decoder) {
let len = decoder.readUint32()
for (let i = 0; i < len; i++) {
let op = decoder.readOperation()
let op = Y.Struct.binaryDecodeOperation(decoder)
yield * this.os.put(op)
}
yield * this.os.iterate(this, null, null, function * (op) {

View File

@ -1,4 +1,5 @@
/* global crypto */
import { BinaryDecoder, BinaryEncoder } from './Encoding.js'
/*
EventHandler is an helper class for constructing custom types.
@ -24,7 +25,10 @@
*/
export default function Utils (Y) {
Y.utils = {}
Y.utils = {
BinaryDecoder: BinaryDecoder,
BinaryEncoder: BinaryEncoder
}
Y.utils.bubbleEvent = function (type, event) {
type.eventHandler.callEventListeners(event)
@ -821,17 +825,17 @@ export default function Utils (Y) {
Y.utils.createSmallLookupBuffer = createSmallLookupBuffer
function generateUserId () {
if (crypto.getRandomValue != null) {
if (typeof crypto !== 'undefined' && crypto.getRandomValue != null) {
// browser
let arr = new Uint32Array(1)
crypto.getRandomValues(arr)
return arr[0]
} else if (crypto.randomBytes != null) {
} else if (typeof crypto !== 'undefined' && crypto.randomBytes != null) {
// node
let buf = crypto.randomBytes(4)
return new Uint32Array(buf.buffer)[0]
} else {
Math.ceil(Math.random() * 0xFFFFFFFF)
return Math.ceil(Math.random() * 0xFFFFFFFF)
}
}
Y.utils.generateUserId = generateUserId