Documentation
¶
Index ¶
- Constants
- Variables
- func AdmissionToken(key []byte) (string, error)
- func ContentHash(content []byte) string
- func DecodeKey(encoded string) ([]byte, error)
- func EncodeAttachment(a Attachment) ([]byte, error)
- func EncodeCanvas(m CanvasMessage) ([]byte, error)
- func EncodeMessage(t MessageType, payload []byte) []byte
- func GenerateKey() string
- func ValidBlobID(s string) bool
- func ValidHash(s string) bool
- type Attachment
- type AttachmentKind
- type BlobKeys
- type CanvasKind
- type CanvasMessage
- type Cipher
- type Client
- type ClientOptions
- type CloseError
- type Conn
- type Dialer
- type MessageType
- type Status
Constants ¶
const AdmissionHeader = "X-Pedit-Admission"
AdmissionHeader carries a room capability, never the encryption key.
const AdmissionProtocolPrefix = "pedit-admission."
const KeyBytes = 32
KeyBytes is the size of a room key (AES-256).
const (
MaxCanvasEditBytes = 960 << 10
)
Limits in UTF-8 bytes, the same as the web client's. An edit carries two copies of the canvas and must fit in one relayed frame (1 MiB).
const MaxWantHashes = 256
MaxWantHashes caps the hashes in one Want message.
const RoomClosed = 4001
RoomClosed is the WebSocket close code sent to everyone in a room when its host leaves. Clients must not reconnect on it: the session is over.
const SocketProtocol = "pedit-v1"
Variables ¶
var ErrUnknownAttachmentKind = errors.New("unknown attachment kind")
ErrUnknownAttachmentKind marks an attachment kind this version does not know, from a newer peer. Clients skip such messages instead of reporting them.
var ErrUnknownCanvasKind = errors.New("unknown canvas kind")
ErrUnknownCanvasKind marks a canvas kind this version does not know, from a newer peer. Clients skip such messages instead of reporting them.
var ErrUnknownMessageType = errors.New("unknown message type")
ErrUnknownMessageType marks a message type this version does not know, from a newer peer. Clients skip such messages instead of reporting them.
Functions ¶
func AdmissionToken ¶
AdmissionToken derives a capability for relay access, domain-separated from all encryption keys. Knowing this token does not reveal document plaintext.
func ContentHash ¶
ContentHash names an attachment by its content; the host uses it as the file name.
func EncodeAttachment ¶
func EncodeAttachment(a Attachment) ([]byte, error)
func EncodeCanvas ¶
func EncodeCanvas(m CanvasMessage) ([]byte, error)
func EncodeMessage ¶
func EncodeMessage(t MessageType, payload []byte) []byte
EncodeMessage prefixes a payload with a one-byte message type.
func GenerateKey ¶
func GenerateKey() string
GenerateKey returns a new room key, encoded as base64url for use in a URL fragment.
Types ¶
type Attachment ¶
type Attachment struct {
Kind AttachmentKind
Hash string
Mime string
Path string
Reason string
Hashes []string
}
Attachment is one attachment message. Which fields are set depends on Kind: Announce has Hash and Mime, Want has Hashes, Stored has Hash and Path, Rejected has Hash and Reason.
func DecodeAttachment ¶
func DecodeAttachment(payload []byte) (Attachment, error)
DecodeAttachment fails on malformed payloads, and with ErrUnknownAttachmentKind on kinds this version does not know.
type AttachmentKind ¶
type AttachmentKind uint64
AttachmentKind says what an attachment message is about.
const ( // AttachmentAnnounce: someone uploaded the attachment with this content hash. AttachmentAnnounce AttachmentKind = 0 // AttachmentWant: someone needs these attachments and could not fetch them. AttachmentWant AttachmentKind = 1 // AttachmentStored: the host wrote the attachment to Path, relative to the shared file. AttachmentStored AttachmentKind = 2 // AttachmentRejected: the host refused it, for Reason. AttachmentRejected AttachmentKind = 3 )
Attachment messages coordinate images whose bytes travel through the server's blob store, never through the relay.
type BlobKeys ¶
type BlobKeys struct {
// Admission is safe to send to the relay; it cannot decrypt blobs or frames.
Admission string
// contains filtered or unexported fields
}
BlobKeys holds the attachment keys, derived from the room key with HKDF-SHA256. They are separate from the frame key, so a blob can never be replayed as a frame.
func DeriveBlobKeys ¶
DeriveBlobKeys derives the attachment keys from a raw room key.
func (*BlobKeys) BlobID ¶
BlobID is where the attachment is stored on the server. Anyone in the room can derive it from a link in the document, but the server cannot tell which content it is.
type CanvasKind ¶
type CanvasKind uint64
CanvasKind says what a canvas message is about.
const ( // CanvasEdit: someone changed the canvas from Base to Next, both JSON Canvas text. CanvasEdit CanvasKind = 0 // CanvasApplied: the host applied the edit with this ID. CanvasApplied CanvasKind = 1 // CanvasRejected: the host did not apply the edit with this ID, for Reason. CanvasRejected CanvasKind = 2 )
Canvas messages carry JSON someone edited by hand in a canvas room. The browser does not change the shared structure itself: it sends what it started from and what it made, and the host applies the change the way it applies an edit to the file made outside pedit.
type CanvasMessage ¶
type CanvasMessage struct {
Kind CanvasKind
ID string
Base string
Next string
Reason string
}
CanvasMessage is one canvas message. Which fields are set depends on Kind: Edit has ID, Base and Next, Applied has ID, Rejected has ID and Reason.
An edit's ID must be unique within the room, such as a random one: the host applies an ID once and answers a repeat, sent again after a reconnect, without applying it, so two guests sharing an ID would lose the second edit.
func DecodeCanvas ¶
func DecodeCanvas(payload []byte) (CanvasMessage, error)
DecodeCanvas fails on malformed payloads, and with ErrUnknownCanvasKind on kinds this version does not know.
type Cipher ¶
type Cipher struct {
// contains filtered or unexported fields
}
Cipher encrypts frames with AES-GCM as `iv || ciphertext`, the same layout the web client uses.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client keeps a Doc and Awareness in sync with every other peer in a room.
The server is a dumb relay, so the peers run a symmetric protocol: on connect a peer sends SyncStep1 (to pull what it is missing), its full state as SyncStep2 (to push what others are missing) and its awareness state. Every peer answers SyncStep1 with SyncStep2. All frames are end-to-end encrypted.
Changes applied from the room carry the Client as their origin.
func NewClient ¶
func NewClient(opts ClientOptions) (*Client, error)
func (*Client) Connect ¶
func (c *Client) Connect()
Connect starts connecting (and reconnecting) in the background.
func (*Client) Destroy ¶
func (c *Client) Destroy()
Destroy announces departure, closes the connection and stops reconnecting.
func (*Client) Do ¶
func (c *Client) Do(fn func())
Do runs fn while no remote update is being applied, so fn can read the document and then change it without a remote edit slipping in between.
func (*Client) SendAttachment ¶
func (c *Client) SendAttachment(a Attachment) error
SendAttachment sends an attachment message to everyone else in the room. Like every other frame, it is dropped while disconnected; retrying is up to the caller.
func (*Client) SendCanvas ¶
func (c *Client) SendCanvas(m CanvasMessage) error
SendCanvas sends a canvas message to everyone else in the room. Like every other frame, it is dropped while disconnected.
type ClientOptions ¶
type ClientOptions struct {
// URL is the WebSocket URL of the room. It must never contain the key.
URL string
Key []byte
Doc *crdt.Doc
Awareness *awareness.Awareness
// Header holds extra handshake headers.
Header http.Header
Dial Dialer
MinBackoff time.Duration
MaxBackoff time.Duration
OnStatus func(Status)
OnError func(error)
// OnSynced is called once, on the connection's read loop, when the first
// SyncStep2 from a peer has been applied: the doc now holds what the room
// had. Return quickly.
OnSynced func()
// OnAttachment is called with each attachment message from the room, on
// the connection's read loop: return quickly.
OnAttachment func(Attachment)
// OnCanvas is called with each canvas message from the room, on the
// connection's read loop: return quickly.
OnCanvas func(CanvasMessage)
}
type CloseError ¶
type CloseError struct {
Code int
}
CloseError reports that the connection was closed with a close code.
func (*CloseError) Error ¶
func (e *CloseError) Error() string
type Conn ¶
type Conn interface {
// Read returns the next binary frame. A close frame from the peer is
// reported as a *CloseError.
Read(ctx context.Context) ([]byte, error)
Write(ctx context.Context, data []byte) error
Close() error
}
Conn is the subset of a WebSocket connection used by Client.
type MessageType ¶
type MessageType byte
const ( MessageSync MessageType = 0 MessageAwareness MessageType = 1 MessageAttachment MessageType = 2 MessageCanvas MessageType = 3 )
func DecodeMessage ¶
func DecodeMessage(data []byte) (MessageType, []byte, error)