aboutsummaryrefslogtreecommitdiff
/**
 * This file is part of Haketilo.
 *
 * Function: Storage through messages (server side).
 *
 * Copyright (C) 2021 Wojtek Kosior
 * Redistribution terms are gathered in the `copyright' file.
 */

/*
 * IMPORTS_START
 * IMPORT listen_for_connection
 * IMPORT get_storage
 * IMPORT list_prefixes
 * IMPORT CONNECTION_TYPE
 * IMPORTS_END
 */

var storage;

async function handle_remote_call(port, message)
{
    let [call_id, func, args] = message;

    try {
	let result = await Promise.resolve(storage[func](...args));
	port.postMessage({call_id, result});
    } catch (error) {
	error = error + '';
	port.postMessage({call_id, error});
    }
}

function remove_storage_listener(cb)
{
    storage.remove_change_listener(cb);
}

function new_connection(port)
{
    console.log("new remote storage connection!");

    const message = {};
    for (const prefix of list_prefixes)
	message[prefix] = storage.get_all(prefix);

    port.postMessage(message);

    let handle_change = change => port.postMessage(change);

    storage.add_change_listener(handle_change);

    port.onMessage.addListener(m => handle_remote_call(port, m));
    port.onDisconnect.addListener(() =>
				  remove_storage_listener(handle_change));
}

async function start_storage_server()
{
    storage = await get_storage();

    listen_for_connection(CONNECTION_TYPE.REMOTE_STORAGE, new_connection);
}

/*
 * EXPORTS_START
 * EXPORT start_storage_server
 * EXPORTS_END
 */
d: Require Guile >= 2.0.13....Ludovic Courtès 2018-01-07daemon: Make libbz2 an optional dependency....Ludovic Courtès 2018-01-07daemon: Add gzip log compression....Ludovic Courtès 2017-02-06config-daemon.ac: detect host AR...Sergei Trofimovich 2016-11-25offload: Use Guile-SSH instead of GNU lsh....Ludovic Courtès 2016-11-16daemon: Add "builtin:download" derivation builder....Ludovic Courtès 2016-07-16build: Substitute URLs now default to "mirror.hydra.gnu.org" alone....Ludovic Courtès 2016-03-19build: Protect against misconfiguration of localstatedir....Ludovic Courtès 2016-03-16build: Default to "https://mirror.hydra.gnu.org/" for substitutes....Ludovic Courtès 2015-12-09build: Always check for gzip/bzip2/xz....Ludovic Courtès 2015-10-09build: Fix libgcrypt detection on FHS systems....Ludovic Courtès 2015-06-03Merge branch 'nix'....Ludovic Courtès 2015-05-19build: Make sure $CXX supports C++11....Ludovic Courtès 2015-03-25Rename 'guix substitute-binary' to 'guix substitute'....Ludovic Courtès 2015-02-24build: Reject or warn against file name length limit overruns....Ludovic Courtès 2015-02-04build: Make 'chroot' a hard requirement....Ludovic Courtès 2015-01-04build: Check for <tr1/unordered_set>, now used by the daemon....Ludovic Courtès 2014-09-02daemon: Do not use 'vfork'....Ludovic Courtès 2014-05-17build: Add --with-libgcrypt-libdir=DIR to support Debian's multi-arch layout....Ludovic Courtès 2014-04-24build: Add missing function checks for optional daemon features....Ludovic Courtès 2014-01-24Add 'guix offload' as a daemon build hook....Ludovic Courtès 2013-12-20daemon: Implement signed archive import/export....Ludovic Courtès 2013-05-12build: Use separate `AC_CONFIG_FILES' invocations for executable files....Ludovic Courtès 2013-04-12substitute-binary: Implement `--substitute'....Ludovic Courtès 2013-04-03Add preliminary binary substituter....Ludovic Courtès 2012-12-13build: When `--enable-daemon', don't error out when Nix programs are missing....Ludovic Courtès 2012-12-06build: Run all the tests against the just-built daemon....Ludovic Courtès 2012-12-05daemon: Add `list-runtime-roots' script....Ludovic Courtès 2012-12-04build: daemon: Add missing feature tests....Ludovic Courtès 2012-12-04build: daemon: Remove extraneous quotes in CPP macro `SYSTEM'....Ludovic Courtès 2012-12-03build: Include a copy of Nix's libstore and daemon; build it....Ludovic Courtès