Apply Updates Server-Side

Hello! This project is amazing and I really appreciate all the work on it.

I have a goal of modifying y-websocket-server to accept HTTP requests that will then update the specified document accordingly.

I would like to be able to apply updates to documents that are either already in memory or are dormant in my persistence layer.

I wrote a simple persistence layer using MySQL that stores encoded states as updates as base64. I’m able to load and save documents via a client editor without issue.

In my testing, I have been unable to load and save documents from an HTTP request, even without modification.

The resulting document saved in MySQL is the erroneous base64 representation of an empty document.

// in my http request handler
const doc = utils.getYDoc(documentName);

const yXmlFragment = doc.getXmlFragment('default');

yXmlFragment.forEach((xml) => {
    // eventual goal of changing this attribute value
    console.log(xml.getAttribute('myAttributeName'));
});

if (doc.conns.size === 0) {
    utils.getPersistence().writeState(doc.name, doc).then(() => {
        doc.destroy()
    });
    utils.docs.delete(doc.name);
}

Can someone please point me in a direction or help me understand why this is happening?

Thank you

The behavior I’m seeing seems to be the result of a race condition.

I’m attempting to read/save the document before state is finished binding.