sync

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ProtocolVersion is the current wire protocol version.
	ProtocolVersion uint16 = 3

	// MaxMessageSize is the maximum message payload (64KB).
	MaxMessageSize = 65536
)

Variables

This section is empty.

Functions

func EncodeEnvPayload

func EncodeEnvPayload(p EnvPayload) ([]byte, error)

EncodeEnvPayload serializes an env payload into bytes.

func SendMessage

func SendMessage(conn *crypto.SecureConn, msg Message) error

SendMessage sends a typed message over a secure connection.

Types

type EnvPayload

type EnvPayload struct {
	Version             uint16
	Sequence            int64
	Timestamp           int64
	BaseRevisionID      string
	RevisionID          string
	AncestorRevisionIDs []string
	FileName            string
	Data                []byte
	Checksum            [32]byte
}

EnvPayload is the payload of an ENV_PUSH or ENV_PULL_RESP message.

func DecodeEnvPayload

func DecodeEnvPayload(data []byte) (EnvPayload, error)

DecodeEnvPayload deserializes an env payload from bytes.

func NewEnvPayload

func NewEnvPayload(fileName string, data []byte, sequence int64, baseRevisionID, revisionID string) EnvPayload

NewEnvPayload creates an EnvPayload from raw .env content.

func NewEnvPayloadWithAncestors

func NewEnvPayloadWithAncestors(fileName string, data []byte, sequence int64, baseRevisionID, revisionID string, ancestorRevisionIDs []string) EnvPayload

type Message

type Message struct {
	Type    MessageType
	Payload []byte
}

Message represents a wire protocol message.

func ReceiveMessage

func ReceiveMessage(conn *crypto.SecureConn) (Message, error)

ReceiveMessage receives a typed message from a secure connection.

type MessageType

type MessageType uint8

MessageType identifies the type of wire protocol message.

const (
	MsgEnvPush     MessageType = 0x01
	MsgEnvPullReq  MessageType = 0x02
	MsgEnvPullResp MessageType = 0x03
	MsgAck         MessageType = 0x10
	MsgNack        MessageType = 0x11
	MsgPeerInfo    MessageType = 0x20
	MsgPing        MessageType = 0x30
	MsgPong        MessageType = 0x31
)

type OrchestratorOptions

type OrchestratorOptions struct {
	EnvFilePath         string
	TeamID              string
	KeyPair             *crypto.KeyPair
	NoiseKeypair        noise.DHKey
	RelayClient         *relay.Client
	RelayURL            string
	Sequence            int64
	BaseRevisionID      string
	RevisionID          string
	AncestorRevisionIDs []string
	OnStatus            func(status string)
}

OrchestratorOptions configures the sync orchestrator.

type OrchestratorResult

type OrchestratorResult struct {
	Method         string
	PeerCount      int
	DeliveredCount int
	QueuedCount    int
	Duration       time.Duration
	Error          error
}

OrchestratorResult summarizes the sync.

func Orchestrate

func Orchestrate(ctx context.Context, opts OrchestratorOptions) *OrchestratorResult

Orchestrate runs the honest fallback chain: LAN direct delivery first, then encrypted relay queueing for remaining trusted peers.

type PullOptions

type PullOptions struct {
	EnvFilePath string
	Port        int
	TeamID      string

	KeyPair      *crypto.KeyPair
	NoiseKeypair noise.DHKey

	ConfirmBeforeApply bool
	ConflictPolicy     apply.Policy
	Interactive        bool
	ProjectID          string
	BackupEnabled      bool
	BackupKey          [32]byte
	MaxVersions        int
	Advertise          bool
	AdvertiseVersion   string

	OnListening        func(port int)
	OnReceived         func(payload EnvPayload, diff *envfile.DiffResult)
	OnConfirm          func(diff *envfile.DiffResult) bool
	OnResolveConflicts func(conflicts []envfile.Conflict) ([]apply.ConflictResolution, bool)
	OnApplied          func(fileName string)
}

PullOptions configures a pull operation.

type PullResult

type PullResult struct {
	FileName                 string
	FileSize                 int
	VarCount                 int
	Applied                  bool
	DiffSummary              string
	ConflictPolicyApplied    string
	BackupCreated            bool
	InteractiveRequired      bool
	ManualInterventionNeeded bool
}

PullResult summarizes the pull operation.

func Pull

func Pull(ctx context.Context, opts PullOptions) (*PullResult, error)

Pull listens for an incoming push and applies the received .env file.

type PushOptions

type PushOptions struct {
	// EnvFilePath is the path to the .env file to push.
	EnvFilePath string

	// TeamID identifies the team to push to.
	TeamID string

	// KeyPair is the local identity.
	KeyPair *crypto.KeyPair

	// NoiseKeypair derived from KeyPair.
	NoiseKeypair noise.DHKey

	// Sequence is the version sequence number.
	Sequence int64

	BaseRevisionID      string
	RevisionID          string
	AncestorRevisionIDs []string

	// OnPeerFound is called when a peer is discovered.
	OnPeerFound func(peer discovery.Peer)

	// OnHandshake is called after noise handshake succeeds.
	OnHandshake func(fingerprint string)

	// OnComplete is called when push to a peer completes.
	OnComplete func(peer discovery.Peer)

	// OnError is called when push to a peer fails.
	OnError func(peer discovery.Peer, err error)
}

PushOptions configures a push operation.

type PushResult

type PushResult struct {
	FileName    string
	FileSize    int
	VarCount    int
	PeersFound  int
	PeersSynced int
	Errors      []error
}

PushResult summarizes the push operation.

func Push

func Push(ctx context.Context, opts PushOptions) (*PushResult, error)

Push discovers peers and sends the .env file to each one.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL