Skip to content

Commit

Permalink
feat: added cancellation for most polykey vaults commands
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Dec 12, 2024
1 parent 7b14e96 commit d518114
Show file tree
Hide file tree
Showing 17 changed files with 278 additions and 199 deletions.
2 changes: 1 addition & 1 deletion src/client/handlers/AgentStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type {
} from '../types';
import type PolykeyAgent from '../../PolykeyAgent';
import { UnaryHandler } from '@matrixai/rpc';
import * as nodesUtils from '../../nodes/utils';
import config from '../../config';
import * as nodesUtils from '../../nodes/utils';

class AgentStatus extends UnaryHandler<
{
Expand Down
5 changes: 3 additions & 2 deletions src/client/handlers/AuditEventsGet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { JSONValue } from '@matrixai/rpc';
import type { ClientRPCRequestParams, ClientRPCResponseResult } from '../types';
import type {
AuditEvent,
Expand Down Expand Up @@ -42,8 +43,8 @@ class AuditEventsGet extends ServerHandler<
}> & {
paths: Array<TopicSubPath>;
},
_cancel,
_meta,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): AsyncGenerator<ClientRPCResponseResult<AuditEventSerialized>> {
const { audit }: { audit: Audit } = this.container;
Expand Down
2 changes: 1 addition & 1 deletion src/client/handlers/GestaltsActionsGetByIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import type GestaltGraph from '../../gestalts/GestaltGraph';
import type { GestaltAction } from '../../gestalts/types';
import type { IdentityId, ProviderId } from '../../ids';
import { UnaryHandler } from '@matrixai/rpc';
import * as ids from '../../ids';
import { validateSync } from '../../validation';
import { matchSync } from '../../utils';
import * as ids from '../../ids';

class GestaltsActionsGetByIdentity extends UnaryHandler<
{
Expand Down
27 changes: 7 additions & 20 deletions src/client/handlers/VaultsClone.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
CloneMessage,
SuccessMessage,
} from '../types';
import type VaultManager from '../../vaults/VaultManager';
import type { NodeId } from '../../ids';
import { UnaryHandler } from '@matrixai/rpc';
import * as ids from '../../ids';
import { validateSync } from '../../validation';
import { matchSync } from '../../utils';

class VaultsClone extends UnaryHandler<
{
Expand All @@ -22,27 +21,15 @@ class VaultsClone extends UnaryHandler<
> {
public handle = async (
input: ClientRPCRequestParams<CloneMessage>,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): Promise<ClientRPCResponseResult<SuccessMessage>> => {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
const {
nodeId,
}: {
nodeId: NodeId;
} = validateSync(
(keyPath, value) => {
return matchSync(keyPath)(
[['nodeId'], () => ids.parseNodeId(value)],
() => value,
);
},
{
nodeId: input.nodeIdEncoded,
},
);
// Vault id
const nodeId = ids.parseNodeId(input.nodeIdEncoded);
await db.withTransactionF(async (tran) => {
await vaultManager.cloneVault(nodeId, input.nameOrId, tran);
await vaultManager.cloneVault(nodeId, input.nameOrId, ctx, tran);
});
return { type: 'success', success: true };
};
Expand Down
13 changes: 7 additions & 6 deletions src/client/handlers/VaultsCreate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
Expand All @@ -19,17 +21,16 @@ class VaultsCreate extends UnaryHandler<
> {
public handle = async (
input: ClientRPCRequestParams<VaultNameMessage>,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): Promise<ClientRPCResponseResult<VaultIdMessage>> => {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;

const vaultId = await db.withTransactionF((tran) =>
vaultManager.createVault(input.vaultName, tran),
vaultManager.createVault(input.vaultName, ctx, tran),
);

return {
vaultIdEncoded: vaultsUtils.encodeVaultId(vaultId),
};
return { vaultIdEncoded: vaultsUtils.encodeVaultId(vaultId) };
};
}

Expand Down
16 changes: 11 additions & 5 deletions src/client/handlers/VaultsDelete.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
SuccessMessage,
VaultIdentifierMessage,
} from '../types';
import type VaultManager from '../../vaults/VaultManager';
import type { VaultName } from '../../vaults/types';
import { UnaryHandler } from '@matrixai/rpc';
import * as vaultsUtils from '../../vaults/utils';
import * as vaultsErrors from '../../vaults/errors';
import * as vaultsUtils from '../../vaults/utils';

class VaultsDelete extends UnaryHandler<
{
Expand All @@ -21,20 +22,25 @@ class VaultsDelete extends UnaryHandler<
> {
public handle = async (
input: ClientRPCRequestParams<VaultIdentifierMessage>,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): Promise<ClientRPCResponseResult<SuccessMessage>> => {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
await db.withTransactionF(async (tran) => {
const vaultIdFromName = await vaultManager.getVaultId(
input.nameOrId as VaultName,
input.nameOrId,
tran,
);
const vaultId =
vaultIdFromName ?? vaultsUtils.decodeVaultId(input.nameOrId);
if (vaultId == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined();
throw new vaultsErrors.ErrorVaultsVaultUndefined(
`Vault "${input.nameOrId}" does not exist`,
);
}
await vaultManager.destroyVault(vaultId, tran);
await vaultManager.destroyVault(vaultId, ctx, tran);
});
return { type: 'success', success: true };
};
Expand Down
14 changes: 8 additions & 6 deletions src/client/handlers/VaultsList.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
VaultListMessage,
} from '../types';
import type VaultManager from '../../vaults/VaultManager';
import type { JSONValue } from '@matrixai/rpc';
import { ServerHandler } from '@matrixai/rpc';
import * as vaultsUtils from '../../vaults/utils';

Expand All @@ -17,21 +19,21 @@ class VaultsList extends ServerHandler<
ClientRPCResponseResult<VaultListMessage>
> {
public handle = async function* (
_input,
_cancel,
_meta,
ctx,
_input: ClientRPCRequestParams,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): AsyncGenerator<ClientRPCResponseResult<VaultListMessage>> {
if (ctx.signal.aborted) throw ctx.signal.reason;
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
const vaults = await db.withTransactionF((tran) =>
vaultManager.listVaults(tran),
vaultManager.listVaults(ctx, tran),
);
for await (const [vaultName, vaultId] of vaults) {
if (ctx.signal.aborted) throw ctx.signal.reason;
yield {
vaultName,
vaultName: vaultName,
vaultIdEncoded: vaultsUtils.encodeVaultId(vaultId),
};
}
Expand Down
16 changes: 10 additions & 6 deletions src/client/handlers/VaultsLog.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
LogEntryMessage,
VaultsLogMessage,
} from '../types';
import type VaultManager from '../../vaults/VaultManager';
import type { VaultName } from '../../vaults/types';
import { ServerHandler } from '@matrixai/rpc';
import * as vaultsUtils from '../../vaults/utils';
import * as vaultsErrors from '../../vaults/errors';
Expand All @@ -21,29 +22,32 @@ class VaultsLog extends ServerHandler<
> {
public handle = async function* (
input: ClientRPCRequestParams<VaultsLogMessage>,
_cancel,
_meta,
ctx,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): AsyncGenerator<ClientRPCResponseResult<LogEntryMessage>> {
if (ctx.signal.aborted) throw ctx.signal.reason;
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
const log = await db.withTransactionF(async (tran) => {
const vaultIdFromName = await vaultManager.getVaultId(
input.nameOrId as VaultName,
input.nameOrId,
tran,
);
const vaultId =
vaultIdFromName ?? vaultsUtils.decodeVaultId(input.nameOrId);
if (vaultId == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined();
throw new vaultsErrors.ErrorVaultsVaultUndefined(
`Vault "${input.nameOrId}" does not exist`,
);
}
// Getting the log
return await vaultManager.withVaults(
[vaultId],
async (vault) => {
return await vault.log(input.commitId, input.depth);
},
ctx,
tran,
);
});
Expand Down
12 changes: 8 additions & 4 deletions src/client/handlers/VaultsPermissionGet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
Expand Down Expand Up @@ -26,9 +28,9 @@ class VaultsPermissionGet extends ServerHandler<
> {
public handle = async function* (
input: ClientRPCRequestParams<VaultIdentifierMessage>,
_cancel,
_meta,
ctx,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): AsyncGenerator<ClientRPCResponseResult<VaultPermissionMessage>> {
if (ctx.signal.aborted) throw ctx.signal.reason;
const {
Expand All @@ -45,7 +47,9 @@ class VaultsPermissionGet extends ServerHandler<
const vaultId =
vaultIdFromName ?? vaultsUtils.decodeVaultId(input.nameOrId);
if (vaultId == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined();
throw new vaultsErrors.ErrorVaultsVaultUndefined(
`Vault "${input.nameOrId}" does not exist`,
);
}
// Getting permissions
return [await acl.getVaultPerm(vaultId, tran), vaultId];
Expand Down
36 changes: 12 additions & 24 deletions src/client/handlers/VaultsPermissionSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ import type {
SuccessMessage,
} from '../types';
import type ACL from '../../acl/ACL';
import type { VaultAction, VaultActions } from '../../vaults/types';
import type { NodeId } from '../../ids';
import type { VaultActions } from '../../vaults/types';
import type VaultManager from '../../vaults/VaultManager';
import type NotificationsManager from '../../notifications/NotificationsManager';
import type GestaltGraph from '../../gestalts/GestaltGraph';
import { UnaryHandler } from '@matrixai/rpc';
import * as ids from '../../ids';
import * as vaultsUtils from '../../vaults/utils';
import * as vaultsErrors from '../../vaults/errors';
import { validateSync } from '../../validation';
import { matchSync } from '../../utils';

class VaultsPermissionSet extends UnaryHandler<
{
Expand Down Expand Up @@ -53,30 +50,21 @@ class VaultsPermissionSet extends UnaryHandler<
const vaultId =
vaultIdFromName ?? vaultsUtils.decodeVaultId(input.nameOrId);
if (vaultId == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined();
throw new vaultsErrors.ErrorVaultsVaultUndefined(
`Vault "${input.nameOrId}" does not exist`,
);
}
const {
nodeId,
actions,
}: {
nodeId: NodeId;
actions: Array<VaultAction>;
} = validateSync(
(keyPath, value) => {
return matchSync(keyPath)(
[['nodeId'], () => ids.parseNodeId(value)],
[['actions'], () => value.map(vaultsUtils.parseVaultAction)],
() => value,
);
},
{
nodeId: input.nodeIdEncoded,
actions: input.vaultPermissionList,
},
const nodeId = ids.parseNodeId(input.nodeIdEncoded);
const actions = input.vaultPermissionList.map(
vaultsUtils.parseVaultAction,
);
// Checking if vault exists
const vaultMeta = await vaultManager.getVaultMeta(vaultId, tran);
if (!vaultMeta) throw new vaultsErrors.ErrorVaultsVaultUndefined();
if (!vaultMeta) {
throw new vaultsErrors.ErrorVaultsVaultUndefined(
`Vault "${input.nameOrId}" does not exist`,
);
}
// Setting permissions
const actionsSet: VaultActions = {};
await gestaltGraph.setGestaltAction(['node', nodeId], 'scan', tran);
Expand Down
Loading

0 comments on commit d518114

Please sign in to comment.