browsermesh-primitives
v0.1.1
Published
Shared primitives for browser mesh networking — wire format, identity (Ed25519), CRDTs, capabilities, trust model, and ACL engine
Maintainers
Readme
browsermesh-primitives
Shared primitives for browser mesh networking -- wire format, identity (Ed25519), CRDTs, capabilities, trust model, and ACL engine. Zero dependencies, pure ES modules, runs in browsers and Node.js.
Install
npm install browsermesh-primitivesOr via CDN:
<script type="module">
import { PodIdentity, VectorClock, ACLEngine } from 'https://esm.sh/browsermesh-primitives'
</script>Quick Start
import {
PodIdentity,
VectorClock,
ORSet,
CapabilityToken,
ACLEngine,
encodeMeshMessage,
decodeMeshMessage,
} from 'browsermesh-primitives'
// Generate an Ed25519 identity
const identity = await PodIdentity.generate()
console.log(identity.podId) // base64url-encoded SHA-256 of public key
// Sign and verify data
const data = new TextEncoder().encode('hello mesh')
const sig = await identity.sign(data)
const ok = await PodIdentity.verify(identity.keyPair.publicKey, data, sig)
// CRDTs -- merge state across peers
const clockA = new VectorClock()
clockA.increment('node-a')
const clockB = new VectorClock()
clockB.increment('node-b')
const merged = clockA.merge(clockB)
// Observed-Remove Set
const set = new ORSet()
set.add('item', identity.podId)
console.log(set.has('item')) // trueAPI Overview
Constants & Errors
MESH_TYPE-- message type constantsMESH_ERROR-- error code constantsMeshError,MeshProtocolError,MeshCapabilityError-- error classes
Identity
PodIdentity-- Ed25519 key pair with sign/verifyderivePodId(publicKey)-- SHA-256 hash to base64url pod IDencodeBase64url(bytes)/decodeBase64url(str)-- URL-safe base64
Wire Format
messageTypeRegistry-- extensible registry of message typesencodeMeshMessage(msg)/decodeMeshMessage(bytes)-- binary serialization
Capabilities
CapabilityToken-- scoped capability with expiryparseScope(str)/matchScope(pattern, target)-- scope parsing and matching
Trust
TRUST_CATEGORIES-- predefined trust category constantscreateTrustEdge(from, to, category, score)-- weighted trust edgecomputeTransitiveTrust(edges, source, target)-- transitive trust score
ACL
ACLEngine-- evaluate access grants against resource patternsPermission-- permission level enumAccessGrant-- grant struct with resource pattern, permission, and principalmatchResourcePattern(pattern, resource)-- glob-style resource matchinggenerateGrantId()-- unique grant ID generator
CRDTs
VectorClock-- partial-order logical clock with mergeLWWRegister-- last-writer-wins register with nodeId tiebreakGCounter-- grow-only counterPNCounter-- positive-negative counterORSet-- observed-remove set (add-wins semantics)RGA-- replicated growable array (ordered list)LWWMap-- last-writer-wins map with tombstones
All CRDTs support merge(), toJSON(), and fromJSON() for serialization.
Test Utilities
DeterministicRNG-- seeded RNG for reproducible testsLocalChannel/createLocalChannelPair()-- in-memory transportTestMesh-- lightweight mesh harnessTESTMESH_LIMITS-- default resource limits for test meshes
License
MIT
