Skip to content

Commit

Permalink
feat: add support for readOnly connections
Browse files Browse the repository at this point in the history
  • Loading branch information
billiegoose committed Feb 1, 2021
1 parent 00c3006 commit aa78639
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
31 changes: 31 additions & 0 deletions bin/readSyncMessageFork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @ts-nocheck
const syncProtocol = require('y-protocols/dist/sync.cjs')

const decoding = require('lib0/dist/decoding.cjs')

/**
* @param {decoding.Decoder} decoder A message received from another client
* @param {encoding.Encoder} encoder The reply message. Will not be sent if empty.
* @param {Y.Doc} doc
* @param {boolean} readOnly If true, updates will be silently ignored instead of applied.
* @param {any} transactionOrigin
*/
const readSyncMessage = (decoder, encoder, doc, readOnly = false, transactionOrigin) => {
const messageType = decoding.readVarUint(decoder)
switch (messageType) {
case syncProtocol.messageYjsSyncStep1:
syncProtocol.readSyncStep1(decoder, encoder, doc)
break
case syncProtocol.messageYjsSyncStep2:
if (!readOnly) syncProtocol.readSyncStep2(decoder, doc, transactionOrigin)
break
case syncProtocol.messageYjsUpdate:
if (!readOnly) syncProtocol.readUpdate(decoder, doc, transactionOrigin)
break
default:
throw new Error('Unknown message type')
}
return messageType
}

module.exports = { readSyncMessage }
3 changes: 2 additions & 1 deletion bin/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const debounce = require('lodash.debounce')

const callbackHandler = require('./callback.js').callbackHandler
const isCallbackSet = require('./callback.js').isCallbackSet
const readSyncMessage = require('./readSyncMessageFork.js').readSyncMessage

const CALLBACK_DEBOUNCE_WAIT = parseInt(process.env.CALLBACK_DEBOUNCE_WAIT) || 2000
const CALLBACK_DEBOUNCE_MAXWAIT = parseInt(process.env.CALLBACK_DEBOUNCE_MAXWAIT) || 10000
Expand Down Expand Up @@ -181,7 +182,7 @@ const messageListener = async (conn, doc, message) => {
await doc.whenSynced
}
encoding.writeVarUint(encoder, messageSync)
syncProtocol.readSyncMessage(decoder, encoder, doc, null)
readSyncMessage(decoder, encoder, doc, conn.readOnly, null)
if (encoding.length(encoder) > 1) {
send(doc, conn, encoding.toUint8Array(encoder))
}
Expand Down

0 comments on commit aa78639

Please sign in to comment.