Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced sync #8

Merged
merged 3 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 17 additions & 23 deletions munkey/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ abstract class CommandServer {

return this.onVaultLink(hostname, portNum, vaultName);
},
"sync": this.onVaultSync.bind(this),
},
"link": {
"up": this.onLinkUp.bind(this),
Expand Down Expand Up @@ -229,7 +228,6 @@ abstract class CommandServer {
abstract onGetVaultEntry(entryKey: string): Promise<void>;
abstract onListVaults(): Promise<void>;
abstract onVaultLink(hostname: string, portNum: number, vaultName: string, vaultNickname?: string): Promise<void>;
abstract onVaultSync(): Promise<void>;

abstract onLinkUp(): Promise<void>;
abstract onLinkDown(): Promise<void>;
Expand Down Expand Up @@ -257,9 +255,13 @@ class ShellCommandServer extends CommandServer {
return console.error(`Cannot create vault ${vaultName} (already exists)`);
}

const vaultId: string | null = await this.services.vault.createVault(vaultName);
vaultId && this.services.vault.subscribeVaultById(vaultId, () =>
this.services.vault.syncActiveVaults(vaultId, this.services.connection));
try {
const vaultId: string | null = await this.services.vault.createVault(vaultName);
console.info(`Vault created with ID ${vaultId}`);
}
catch (err) {
console.error(err);
}
}

async onUseVault(vaultName: string): Promise<void> {
Expand Down Expand Up @@ -289,7 +291,7 @@ class ShellCommandServer extends CommandServer {

console.info(":: :: Remote Vaults :: ::");
for (let [name, url] of this.services.connection.getAllConnections()) {
console.info(` ${url.name} = RemoteVault[${name}]`);
console.info(` ${url} = RemoteVault[${name}]`);
}
}

Expand Down Expand Up @@ -319,7 +321,6 @@ class ShellCommandServer extends CommandServer {
_rev,
entries: { ...entries, [entryKey]: data },
}).catch(err => console.error(err));
await this.services.vault.syncActiveVaults(vaultId, this.services.connection);
}

async onGetVaultEntry(entryKey: string): Promise<void> {
Expand Down Expand Up @@ -367,29 +368,22 @@ class ShellCommandServer extends CommandServer {
// Query the APL to find the vault ID with that nickname.
let { vaultId = null } = activeDevice?.vaults.find(vault => vault.nickname === vaultName) ?? {};
if (vaultId) {
this.services.connection.publishDatabaseConnection({ hostname, portNum }, vaultName, vaultId);
let localVault: PouchDB.Database<DatabaseDocument> | null = this.services.vault.getVaultById(vaultId);
// TODO: use the queried vault information to validate the vault.
if (!localVault) {
// TODO: allow the user to specify their own local nickname.
// Relying on the remote database's vault name is subject to collisions.
try {
vaultId = await this.services.vault.createVault(vaultNickname, vaultId);
vaultId && this.services.vault.subscribeVaultById(vaultId, () =>
this.services.vault.syncActiveVaults(vaultId, this.services.connection));
let localVault = this.services.vault.getVaultById(vaultId);
let remoteConn = this.services.connection
.publishDatabaseConnection({ hostname, portNum }, vaultName, vaultId, localVault);
remoteConn.catch(err => console.error(err));
}
catch (err) {
console.error("Failed to create local vault: ", err.message);
}
}
else {
console.error(`Vault unavailable: ${vaultName}@${hostname}:${portNum}`);
}
}

async onVaultSync(): Promise<void> {
const vaultId: string = this.services.vault.getActiveVaultId();
if (await this.services.vault.syncActiveVaults(vaultId, this.services.connection) <= 0) {
console.error("No remote vaults found");
}
}

async onLinkUp(): Promise<void> {
await this.services.web.listen()
.catch(() => console.error("Failed to open server"));
Expand Down Expand Up @@ -418,7 +412,7 @@ class ShellCommandServer extends CommandServer {
}

async onPeerList(): Promise<void> {
for (let [hostname, portNum, identity] of this.services.activity.getAllDevices()) {
for (let [{ hostname, portNum }, identity] of this.services.activity.getAllDevices()) {
console.info(` Peer[${identity.uniqueId}]@${hostname}:${portNum}`);
for (let vault of identity.vaults) {
console.info(`\t* "${vault.nickname}": Vault[${vault.vaultId}]`);
Expand Down
20 changes: 20 additions & 0 deletions munkey/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ interface DeviceDiscoveryDecl {
portNum: number;
}

interface PeerLinkResponse extends PeerIdentityDecl {
activePeerList: DeviceDiscoveryDecl[];
}

function isPeerIdentityDecl(decl: Object): decl is PeerIdentityDecl {
return decl &&
("uniqueId" in decl) &&
Expand All @@ -33,11 +37,27 @@ function isPeerIdentityDecl(decl: Object): decl is PeerIdentityDecl {
));
}

function isDeviceDiscoveryDecl(decl: Object): decl is DeviceDiscoveryDecl {
return decl &&
("hostname" in decl) &&
("portNum" in decl);
}

function isPeerLinkResponse(decl: Object): decl is PeerLinkResponse {
return isPeerIdentityDecl(decl) &&
("activePeerList" in decl) &&
(Array.isArray((decl as PeerLinkResponse).activePeerList)) &&
((decl as PeerLinkResponse).activePeerList.every(peer => isDeviceDiscoveryDecl(peer)));
}

export {
PeerVaultDecl,
PeerIdentityDecl,
DeviceDiscoveryDecl,
PeerLinkResponse,

/* Validation Functions */
isPeerIdentityDecl,
isDeviceDiscoveryDecl,
isPeerLinkResponse,
};
5 changes: 3 additions & 2 deletions munkey/munkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ import {
const uniformPrint = winston.format.printf(function(
info: winston.Logform.TransformableInfo & { label: string, timestamp: string }): string
{
return `[${info.level}::${info.label}] ${info.message}`;
let { level, label, message } = info;
return `[${level}::${label}] ${message}`;
});


const addUniformLogger = function(serviceName: string): winston.Logger {
winston.loggers.add(serviceName, {
format: winston.format.combine(
winston.format.splat(),
winston.format.colorize(),
winston.format.label({ label: serviceName }),
winston.format.timestamp(),
uniformPrint,
),
transports: [
Expand Down
Loading