fix(server): wrap awareness origin as TransactionOrigin#1094
Merged
Conversation
The v4 awareness listener in Hocuspocus.ts uses isTransactionOrigin to
read payload.connection, but MessageReceiver and
Document.applyAwarenessUpdate pass the raw Connection, so the guard
fails and payload.connection is always undefined.
Mirror the sync-handler pattern in the same file: wrap origin as
{ source: 'connection', connection } at the producer sites, extract
via the type guard in Document.handleAwarenessUpdate, and tighten the
related parameter types so future producers cannot regress.
Soft API break: listeners attached directly via
document.awareness.on('update', ...) now receive a TransactionOrigin
instead of a raw Connection. The recommended onAwarenessUpdate hook
keeps exposing the resolved connection via payload.connection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
onAwarenessUpdate({ connection })is alwaysundefinedin v4. The awareness listener inHocuspocus.tsreads it viaisTransactionOrigin(origin), but the producers (MessageReceiverawareness branch andDocument.applyAwarenessUpdate) pass the rawConnection. The type guard returnsfalse, so the connection is never forwarded to the hook and server-side awareness work that depends on the per-update authenticated user cannot function.Fix
Wrap origin as
{ source: 'connection', connection }at the producer sites, matching the sync handlers in the sameMessageReceiver. UpdateDocument.handleAwarenessUpdateto extract the connection through the existing type guard. Tighten the related parameter types withsatisfies ConnectionTransactionOriginandTransactionOrigin | nullso future producers cannot regress.I suspect this matches the original v4 intent:
Hocuspocus.ts's awareness listener was already written forTransactionOrigin, the sync handlers already wrap, and the previousoriginConnection: Connection | nullparameter onhandleAwarenessUpdatelooks like a leftover pre-v4 signature. The y-protocolsorigin: anytyping hid the inconsistency from the compiler.Soft API break
Listeners attached directly via
document.awareness.on('update', ...)now receive aTransactionOrigin(or{ source: 'local' }for non-connection origins) instead of a rawConnection. Migration:The supported
onAwarenessUpdatehook is unaffected and gains the resolvedpayload.connection.Alternative
If full backwards compatibility is preferred, I can instead make the consumer side tolerant of both raw
Connectionand wrappedTransactionOrigin, leaving the producers untouched. Happy to switch.