import * as tfrpc from '/tfrpc.js'; let g_database; let g_hash; tfrpc.register(async function localStorageGet(key) { return app.localStorageGet(key); }); tfrpc.register(async function localStorageSet(key, value) { return app.localStorageSet(key, value); }); tfrpc.register(async function databaseGet(key) { return g_database ? g_database.get(key) : undefined; }); tfrpc.register(async function databaseSet(key, value) { return g_database ? g_database.set(key, value) : undefined; }); tfrpc.register(async function createIdentity() { return ssb.createIdentity(); }); tfrpc.register(async function getIdentities() { return ssb.getIdentities(); }); tfrpc.register(async function getAllIdentities() { return ssb.getAllIdentities(); }); tfrpc.register(async function getBroadcasts() { return ssb.getBroadcasts(); }); tfrpc.register(async function getConnections() { return ssb.connections(); }); tfrpc.register(async function query(sql, args) { let result = []; await ssb.sqlStream(sql, args, function callback(row) { result.push(row); }); return result; }); tfrpc.register(async function appendMessage(id, message) { return ssb.appendMessageWithIdentity(id, message); }); core.register('message', async function message_handler(message) { if (message.event == 'hashChange') { g_hash = message.hash; await tfrpc.rpc.hashChanged(message.hash); } }); tfrpc.register(function getHash(id, message) { return g_hash; }); tfrpc.register(function setHash(hash) { return app.setHash(hash); }); ssb.addEventListener('message', async function(id) { await tfrpc.rpc.notifyNewMessage(id); }); tfrpc.register(async function store_blob(blob) { if (Array.isArray(blob)) { blob = Uint8Array.from(blob); } return await ssb.blobStore(blob); }); tfrpc.register(async function get_blob(id) { return utf8Decode(await ssb.blobGet(id)); }); tfrpc.register(function apps() { return core.apps(); }); ssb.addEventListener('broadcasts', async function() { await tfrpc.rpc.set('broadcasts', await ssb.getBroadcasts()); }); core.register('onConnectionsChanged', async function() { await tfrpc.rpc.set('connections', await ssb.connections()); }); async function main() { if (typeof(database) !== 'undefined') { g_database = await database('ssb'); } await app.setDocument(utf8Decode(await getFile('index.html'))); } main();