Skip to content

Shared Module

Code shared between client and server. Import from datasole (root).

New here? Start with the Developer Guide and Configuration Reference, then come back here for shared type details.

Types

Protocol

  • Frame — Binary frame structure
  • Envelope — Frame with protocol metadata
  • Opcode — Message type enum

State

  • StatePatch — RFC 6902 JSON Patch operation
  • StateSnapshot — Keyed state with version

RPC

  • RpcRequest / RpcResponse — RPC envelope types
  • RpcError — Error payload

Events

  • EventPayload — Event envelope type

Auth

  • AuthCredentials / AuthResult / AuthContext — Auth types

Data Flow

  • DataFlowPattern'rpc' | 'server-event' | 'client-event' | 'bidirectional-event' | 'server-live-state' | 'client-live-state' | 'bidirectional-crdt'
  • SyncGranularity'immediate' | 'batched' | 'debounced' | 'manual'
  • LiveStateConfig / LiveStateHandle / DataChannel — Typed channel setup

CRDTs

Built-in conflict-free replicated data types for bidirectional sync:

TypeDescriptionUse Case
LWWRegister<T>Last-writer-wins scalarSingle value (username, status)
PNCounterPositive-negative counterVotes, likes, online count
LWWMap<T>LWW map of registersShared document, form fields

All implement the Crdt<T> interface:

typescript
interface Crdt<T> {
  readonly type: CrdtType;
  readonly nodeId: string;
  value(): T;
  apply(op: CrdtOperation): void;
  merge(remote: CrdtState<T>): void;
  state(): CrdtState<T>;
}

Tutorial: Bidirectional CRDT — A Shared Counter

Build Constants

BUILD_CONSTANTS in src/shared/build-constants.ts is the single source of truth:

ConstantValueDescription
PACKAGE_NAMEdatasolenpm package name
VERSION(from package.json)Injected at build time by Rollup
PROTOCOL_VERSION1Wire protocol version
DEFAULT_WS_PATH/__dsDefault WebSocket path
MAX_FRAME_SIZE10485761MB max frame
COMPRESSION_THRESHOLD256Compress above this size