Skip to content

Instantly share code, notes, and snippets.

@mattdesl
Last active March 8, 2024 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattdesl/efdedcf47494a24946273d3b01d2fd79 to your computer and use it in GitHub Desktop.
Save mattdesl/efdedcf47494a24946273d3b01d2fd79 to your computer and use it in GitHub Desktop.
import {
IrohNode,
PublicKey,
NodeAddr,
Query,
SortBy,
SortDirection,
pathToKey,
AuthorId,
keyToPath,
CapabilityKind,
ShareMode,
} from "../../iroh-ffi/index.js";
const tmpDir = `tmp/${Math.floor(Math.random() * 1e14)}`;
const node = await IrohNode.withPath(tmpDir);
const author = await node.authorCreate();
console.log(`Node ID: ${node.nodeId()}`);
console.log(`Current Author: ${author.toString()}`);
const ticket = process.argv[2];
if (!ticket) throw new Error("supply a ticket");
// join doc
const doc = await node.docJoin(ticket);
console.log(`Current Doc: ${doc.id}`);
// set some value on it
const key = "foo";
const val = String(Math.floor(Math.random() * 100000));
console.log("Setting K/V:", key, val);
const resp = await doc.setBytes(author, Buffer.from(key), Buffer.from(val));
console.log("Resp", resp);
// Wait some period to ensure content has been written
await new Promise((r) => setTimeout(r, 5000));
// now leave the document
await doc.leave();
import {
IrohNode,
PublicKey,
NodeAddr,
Query,
SortBy,
SortDirection,
pathToKey,
AuthorId,
keyToPath,
CapabilityKind,
ShareMode,
} from "../../iroh-ffi/index.js";
const tmpDir = `tmp/static`;
const node = await IrohNode.withPath(tmpDir);
const authors = await node.authorList();
const docs = (await node.docList()).filter(
(doc) => doc.capability === CapabilityKind.Write
);
let doc;
if (docs.length === 0) {
doc = await node.docCreate();
} else {
doc = await node.docOpen(docs[0].namespace);
}
let author;
if (authors.length === 0) {
author = await node.authorCreate();
} else {
author = authors[0];
}
const policy = await doc.getDownloadPolicy();
console.log(`Node ID: ${node.nodeId()}`);
console.log(`Download Policy: ${Object.keys(policy)}`);
console.log(`Current Doc: ${doc.id}`);
console.log(`Current Author: ${author.toString()}`);
const ticket = await doc.share(ShareMode.Write);
console.log("Ticket:", ticket);
await listAll();
await listen();
async function listen() {
console.log("Subscribing...");
const sub = await doc.subscribe();
for (const event of sub) {
const keys = Object.keys(event);
for (let type of keys) {
const params = event[type];
switch (type) {
case "InsertLocal":
console.log(type, params.entry.record.hash);
break;
case "InsertRemote":
console.log(type, `(from ${params.from})`);
console.log("Entry:", params.entry);
console.log("Status:", params.content_status);
break;
case "ContentReady":
console.log(type, params.hash);
break;
case "SyncFinished":
console.log(type, params.peer);
break;
case "NeighborUp":
case "NeighborDown":
console.log(type, params);
break;
}
}
}
console.log("Done!");
}
async function listAll() {
// const q = Query.keyExact(key);
const q = Query.all();
console.log("Querying...");
const entries = await doc.getMany(q);
console.log(`Entries ---- ${entries.length}`);
for (let e of entries) {
const author = e.author();
console.log(`--- Entry`);
console.log(` Author: ${author.toString()}`);
console.log(` Key: ${Buffer.from(e.key()).toString()}`);
console.log(` Namespace: ${e.namespace()}`);
console.log(` Bytes: ${e.contentLen()}`);
try {
const content = await e.contentBytes(doc);
console.log(` Content: ${content.toString()}`);
} catch (err) {
console.log(` Content: [Error: ${err.message}]`);
}
}
}
function setValue(key, val) {
console.log("Setting bytes");
return doc.setBytes(author, Buffer.from(key), Buffer.from(val));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment