npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

wasm-memory-distributor

v1.0.0

Published

A JavaScript library for distributing WebAssembly memory over multiple machines using peer-to-peer synchronization

Readme

wasm-memory-distributor

A JavaScript library for distributing WebAssembly memory over multiple machines using peer-to-peer synchronization.

Features

  • Page-based memory tracking: Efficiently divides WASM memory into configurable pages (default 8KB)
  • Automatic change detection: Periodically scans memory for changes using fast non-cryptographic hashing
  • Binary diff/patch: Creates compact patches containing only changed bytes
  • Conflict resolution: Uses vector clocks with Last-Write-Wins strategy for deterministic conflict resolution
  • Zero dependencies: Pure JavaScript implementation
  • Transport agnostic: Works with any transport layer (WebRTC, WebSocket, etc.)

Demos

To run the demo locally:

npm install
npm run dev
# Open http://localhost:5173/broadcast-channel-demo.html

For the broadcast channel demo, open the URL in multiple browser tabs to see real-time synchronization across tabs.

Installation

npm install wasm-memory-distributor
# or
pnpm add wasm-memory-distributor
# or  
yarn add wasm-memory-distributor

Usage

import { 
  createMemoryDistributor, 
  scan, 
  applyPatches, 
  onDiff, 
  getVectorClock, 
  resizeMemory, 
  destroy 
} from 'wasm-memory-distributor';

// Create a WASM memory instance
const wasmMemory = new WebAssembly.Memory({ initial: 10 });

// Initialize the distributor
let distributorState = createMemoryDistributor(
  wasmMemory,
  'peer-id-123',
  {
    pageSize: 8192,      // 8KB pages (default)
    scanInterval: 100    // Scan every 100ms (default)
  }
);

// Listen for changes
distributorState = onDiff(distributorState, (patches) => {
  // Send patches to other peers via your transport layer
  sendToOtherPeers(patches);
});

// Manually trigger a scan
const scanResult = scan(distributorState);
distributorState = scanResult.state;
const patches = scanResult.patches;

// Apply patches received from other peers
distributorState = applyPatches(distributorState, receivedPatches);

// Get current vector clock state
const clock = getVectorClock(distributorState);

// Handle memory growth
wasmMemory.grow(5);
distributorState = resizeMemory(distributorState, wasmMemory.buffer);

// Clean up
distributorState = destroy(distributorState);

API

createMemoryDistributor(wasmMemory, peerId, config?)

Creates a new memory distributor state object.

  • wasmMemory: WebAssembly.Memory instance
  • peerId: Unique identifier for this peer (string)
  • config (optional):
    • pageSize: Size of memory pages in bytes (default: 8192)
    • scanInterval: Milliseconds between automatic scans (default: 100, set to 0 to disable)

scan(state): { state, patches }

Manually triggers a memory scan and returns updated state and array of patches for modified pages.

onDiff(state, callback): state

Registers a callback to be invoked when changes are detected during automatic scans. Returns updated state.

applyPatches(state, patches): state

Applies received patches from other peers. Includes automatic conflict resolution using vector clocks. Returns updated state.

getVectorClock(state): VectorClock

Returns the current vector clock state for this peer.

resizeMemory(state, newBuffer): state

Updates the distributor when WASM memory grows. Pass the new buffer from the WebAssembly.Memory instance. Returns updated state.

destroy(state): state

Stops automatic scanning and cleans up resources. Returns updated state.

Patch Format

Each patch contains:

{
  pageIndex: number,           // Which page was modified
  patchData: Array<Change>,    // Binary delta changes
  vectorClock: VectorClock,    // Current vector clock state
  peerId: string               // Peer that made the change
}

Conflict Resolution

The library uses vector clocks to detect concurrent modifications. When conflicts occur:

  1. Patches are compared using vector clocks
  2. If clocks are concurrent (conflicting), Last-Write-Wins is applied
  3. Peer IDs are compared lexicographically - higher ID wins
  4. Both peers independently apply the same resolution logic to converge

License

ISC