Skip to content

Latest commit

 

History

History
54 lines (34 loc) · 1.92 KB

README.md

File metadata and controls

54 lines (34 loc) · 1.92 KB

Negentropy Javascript Implementation

The library is contained in a single javascript file. It shouldn't need any dependencies, in either a browser or node.js:

const Negentropy = require('Negentropy.js');

Storage

First, you need to create a storage instance. Currently only Vector is implemented:

let storage = new NegentropyStorageVector();

Next, add all the items in your collection, and seal():

for (let item of myItems) {
    storage.insert(item.timestamp, item.id);
}

ne.seal();
  • timestamp should be a JS Number
  • id should be a hex string, Uint8Array, or node.js Buffer

Reconciliation

Create a Negentropy object:

let ne = new Negentropy(storage, 50_000);
  • The second parameter (50_000 above) is the frameSizeLimit. This can be omitted (or 0) to permit unlimited-sized frames.

On the client-side, create an initial message, and then transmit it to the server, receive the response, and reconcile until complete (signified by returning null for newMsg):

let msg = await ne.initiate();

while (msg !== null) {
    let response = queryServer(msg);
    let [newMsg, have, need] = await ne.reconcile(msg);
    msg = newMsg;
    // handle have/need (there may be duplicates from previous calls to reconcile())
}
  • The output msgs and the IDs in the have/need arrays are hex strings, but you can set ne.wantUint8ArrayOutput = true if you want Uint8Arrays instead.

The server-side is similar, except it doesn't create an initial message, there are no have/need arrays, and newMsg will never be null:

while (1) {
    let msg = receiveMsgFromClient();
    let [newMsg] = await ne.reconcile(msg);
    respondToClient(newMsg);
}
  • The initiate() and reconcile() methods are async because the crypto.subtle.digest() browser API is async.
  • Timestamp values greater than Number.MAX_VALUE will currently cause failures.