Files
Docwell/Server/internal/collab/protocol.go
T
tajniak81andClaude Opus 5.5 ba394b8fac Finish Phase 0: lint, codegen, contributor docs and the two remaining spikes
Repository basics:
- CONTRIBUTING.md and CODE_OF_CONDUCT.md (Contributor Covenant 2.1)
- SPDX AGPL-3.0-or-later headers on all hand-written sources, checked in CI
- golangci-lint (shared .golangci.yml), ESLint and Prettier, all run in CI

API codegen from api/openapi.yaml (npm run generate, drift fails CI):
- Go types via oapi-codegen in Server/internal/api
- a contract test that the router and the spec list the same routes
- Dart models via Phone-App/tool/generate_api.dart; the app uses them

Spike: shared Tiptap editor in a Flutter WebView (ADR-4)
- @docwell/editor-bridge builds the editor into one HTML page with a
  versioned JSON protocol; Phone-App hosts it with webview_flutter and a
  native toolbar. Integration test passes on an Android API 35 emulator.
- flutter_inappwebview dropped: its Android plugin fails on AGP 9.

Spike: Wails v3 desktop (ADR-11)
- system tray, docwell:// deep links (page and space only) with
  single-instance hand-off, and the built-in updater with a required
  ed25519 signing key. Verified on Windows.
- regenerated build assets register the scheme and drop template
  placeholders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 16:11:07 +01:00

107 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-License-Identifier: AGPL-3.0-or-later
package collab
import (
"encoding/binary"
"errors"
"slices"
)
// The y-websocket wire format (y-protocols sync and awareness, lib0 encoding):
//
// [varuint messageType] ...
// sync: [varuint step] [varUint8Array payload]
// awareness: [varUint8Array update]
const (
msgSync = 0
msgAwareness = 1
msgQueryAwareness = 3
syncStep1 = 0 // payload: the sender's state vector
syncStep2 = 1 // payload: an update with what the receiver is missing
syncUpdate = 2 // payload: an incremental update
)
var errMalformed = errors.New("malformed message")
type decoder struct{ buf []byte }
func (d *decoder) uint() (uint64, error) {
v, n := binary.Uvarint(d.buf)
if n <= 0 {
return 0, errMalformed
}
d.buf = d.buf[n:]
return v, nil
}
func (d *decoder) bytes() ([]byte, error) {
n, err := d.uint()
if err != nil || uint64(len(d.buf)) < n {
return nil, errMalformed
}
b := d.buf[:n]
d.buf = d.buf[n:]
return b, nil
}
func appendUint(b []byte, v uint64) []byte { return binary.AppendUvarint(b, v) }
func appendBytes(b, v []byte) []byte { return append(appendUint(b, uint64(len(v))), v...) }
func syncMessage(step uint64, payload []byte) []byte {
b := make([]byte, 0, len(payload)+8)
return appendBytes(appendUint(appendUint(b, msgSync), step), payload)
}
func awarenessMessage(update []byte) []byte {
return appendBytes(appendUint(make([]byte, 0, len(update)+8), msgAwareness), update)
}
// awarenessState is one client's entry in an awareness update.
type awarenessState struct {
clock uint64
state string // JSON; "null" means the client left
}
// decodeAwareness reads [varuint n] then n × [varuint clientID][varuint clock][varString state].
func decodeAwareness(update []byte) (map[uint64]awarenessState, error) {
d := decoder{update}
n, err := d.uint()
if err != nil || n > 1<<16 {
return nil, errMalformed
}
out := make(map[uint64]awarenessState, n)
for range n {
id, err := d.uint()
if err != nil {
return nil, err
}
clock, err := d.uint()
if err != nil {
return nil, err
}
state, err := d.bytes()
if err != nil {
return nil, err
}
out[id] = awarenessState{clock: clock, state: string(state)}
}
return out, nil
}
func encodeAwareness(states map[uint64]awarenessState) []byte {
b := appendUint(nil, uint64(len(states)))
ids := make([]uint64, 0, len(states))
for id := range states {
ids = append(ids, id)
}
slices.Sort(ids)
for _, id := range ids {
s := states[id]
b = appendBytes(appendUint(appendUint(b, id), s.clock), []byte(s.state))
}
return b
}