From 37c1d36b9dc330924b1dd5cb33dac447ae9aef81 Mon Sep 17 00:00:00 2001 From: Aryan Jassal Date: Tue, 1 Oct 2024 13:52:30 +1000 Subject: [PATCH] feat: VaultsSecretsMkdir now supports specifying multiple secret paths chore: undid changes to vaultOps.mkdir chore: working on continuing on errors chore: still trying to return errors chore: properly checking errors throws it feat: updated tests for VaultsSecretsMkdir chore: merged makeDirectories into mkdir chore: updated tests for vaultOps.mkdir fix: removed redundant comments chore: simplified RPC handler chore: updated metadata types chore: added error type for mkdir chore: updated SuccessMessage type chore: updated all handlers to be consistent with container deconstruction fix: lint chore: simplified mkdir vaultop chore: using type narrowing instead of using 'as' chore: updated metadata types --- src/client/callers/vaultsSecretsMkdir.ts | 4 +- src/client/handlers/AgentLockAll.ts | 3 +- src/client/handlers/AgentStatus.ts | 2 +- src/client/handlers/AgentStop.ts | 2 +- src/client/handlers/AuditEventsGet.ts | 6 +- src/client/handlers/AuditMetricGet.ts | 2 +- .../handlers/GestaltsActionsGetByIdentity.ts | 3 +- .../handlers/GestaltsActionsGetByNode.ts | 5 +- .../handlers/GestaltsActionsSetByIdentity.ts | 5 +- .../handlers/GestaltsActionsSetByNode.ts | 3 +- .../GestaltsActionsUnsetByIdentity.ts | 5 +- .../handlers/GestaltsActionsUnsetByNode.ts | 5 +- .../handlers/GestaltsDiscoveryByIdentity.ts | 2 +- .../handlers/GestaltsDiscoveryByNode.ts | 2 +- src/client/handlers/GestaltsDiscoveryQueue.ts | 6 +- .../handlers/GestaltsGestaltGetByIdentity.ts | 5 +- .../handlers/GestaltsGestaltGetByNode.ts | 5 +- src/client/handlers/GestaltsGestaltList.ts | 9 +- .../GestaltsGestaltTrustByIdentity.ts | 9 +- .../handlers/GestaltsGestaltTrustByNode.ts | 9 +- src/client/handlers/IdentitiesAuthenticate.ts | 15 +- .../handlers/IdentitiesAuthenticatedGet.ts | 15 +- src/client/handlers/IdentitiesClaim.ts | 3 +- .../handlers/IdentitiesInfoConnectedGet.ts | 7 +- src/client/handlers/IdentitiesInfoGet.ts | 7 +- src/client/handlers/IdentitiesInvite.ts | 10 +- .../handlers/IdentitiesProvidersList.ts | 3 +- src/client/handlers/IdentitiesTokenDelete.ts | 5 +- src/client/handlers/IdentitiesTokenGet.ts | 5 +- src/client/handlers/IdentitiesTokenPut.ts | 5 +- src/client/handlers/KeysCertsChainGet.ts | 6 +- src/client/handlers/KeysCertsGet.ts | 2 +- src/client/handlers/KeysDecrypt.ts | 2 +- src/client/handlers/KeysEncrypt.ts | 2 +- src/client/handlers/KeysKeyPair.ts | 2 +- src/client/handlers/KeysKeyPairRenew.ts | 2 +- src/client/handlers/KeysKeyPairReset.ts | 2 +- src/client/handlers/KeysPasswordChange.ts | 2 +- src/client/handlers/KeysPublicKey.ts | 2 +- src/client/handlers/KeysSign.ts | 2 +- src/client/handlers/KeysVerify.ts | 6 +- src/client/handlers/NodesAdd.ts | 5 +- src/client/handlers/NodesClaim.ts | 10 +- src/client/handlers/NodesFind.ts | 3 +- src/client/handlers/NodesGetAll.ts | 6 +- src/client/handlers/NodesListConnections.ts | 8 +- src/client/handlers/NodesPing.ts | 6 +- .../handlers/NotificationsInboxClear.ts | 5 +- src/client/handlers/NotificationsInboxRead.ts | 5 +- .../handlers/NotificationsInboxRemove.ts | 5 +- .../handlers/NotificationsOutboxClear.ts | 5 +- .../handlers/NotificationsOutboxRead.ts | 5 +- .../handlers/NotificationsOutboxRemove.ts | 5 +- src/client/handlers/NotificationsSend.ts | 4 +- src/client/handlers/VaultsClone.ts | 7 +- src/client/handlers/VaultsCreate.ts | 3 +- src/client/handlers/VaultsDelete.ts | 7 +- src/client/handlers/VaultsList.ts | 7 +- src/client/handlers/VaultsLog.ts | 7 +- src/client/handlers/VaultsPermissionGet.ts | 10 +- src/client/handlers/VaultsPermissionSet.ts | 25 +- src/client/handlers/VaultsPermissionUnset.ts | 16 +- src/client/handlers/VaultsPull.ts | 7 +- src/client/handlers/VaultsRename.ts | 5 +- src/client/handlers/VaultsScan.ts | 6 +- src/client/handlers/VaultsSecretsEnv.ts | 4 +- src/client/handlers/VaultsSecretsGet.ts | 5 +- src/client/handlers/VaultsSecretsList.ts | 10 +- src/client/handlers/VaultsSecretsMkdir.ts | 79 ++--- src/client/handlers/VaultsSecretsNew.ts | 9 +- src/client/handlers/VaultsSecretsNewDir.ts | 12 +- src/client/handlers/VaultsSecretsRemove.ts | 7 +- src/client/handlers/VaultsSecretsRename.ts | 9 +- src/client/handlers/VaultsSecretsStat.ts | 5 +- src/client/handlers/VaultsSecretsWriteFile.ts | 9 +- src/client/handlers/VaultsVersion.ts | 5 +- src/client/types.ts | 18 +- src/vaults/VaultOps.ts | 47 +-- tests/client/handlers/vaults.test.ts | 274 +++++++++++++----- tests/vaults/VaultOps.test.ts | 33 ++- 80 files changed, 563 insertions(+), 332 deletions(-) diff --git a/src/client/callers/vaultsSecretsMkdir.ts b/src/client/callers/vaultsSecretsMkdir.ts index fb83985c7..71a19c7f2 100644 --- a/src/client/callers/vaultsSecretsMkdir.ts +++ b/src/client/callers/vaultsSecretsMkdir.ts @@ -1,10 +1,10 @@ import type { HandlerTypes } from '@matrixai/rpc'; import type VaultsSecretsMkdir from '../handlers/VaultsSecretsMkdir'; -import { UnaryCaller } from '@matrixai/rpc'; +import { DuplexCaller } from '@matrixai/rpc'; type CallerTypes = HandlerTypes; -const vaultsSecretsMkdir = new UnaryCaller< +const vaultsSecretsMkdir = new DuplexCaller< CallerTypes['input'], CallerTypes['output'] >(); diff --git a/src/client/handlers/AgentLockAll.ts b/src/client/handlers/AgentLockAll.ts index 5b919a55e..6e7c4b0b7 100644 --- a/src/client/handlers/AgentLockAll.ts +++ b/src/client/handlers/AgentLockAll.ts @@ -12,7 +12,8 @@ class AgentLockAll extends UnaryHandler< ClientRPCResponseResult > { public handle = async (): Promise => { - const { db, sessionManager } = this.container; + const { db, sessionManager }: { db: DB; sessionManager: SessionManager } = + this.container; await db.withTransactionF((tran) => sessionManager.resetKey(tran)); return {}; }; diff --git a/src/client/handlers/AgentStatus.ts b/src/client/handlers/AgentStatus.ts index 201d6240f..249af6e58 100644 --- a/src/client/handlers/AgentStatus.ts +++ b/src/client/handlers/AgentStatus.ts @@ -18,7 +18,7 @@ class AgentStatus extends UnaryHandler< public handle = async (): Promise< ClientRPCResponseResult > => { - const { polykeyAgent } = this.container; + const { polykeyAgent }: { polykeyAgent: PolykeyAgent } = this.container; return { pid: process.pid, nodeIdEncoded: nodesUtils.encodeNodeId(polykeyAgent.keyRing.getNodeId()), diff --git a/src/client/handlers/AgentStop.ts b/src/client/handlers/AgentStop.ts index c57733adf..2ebf2f570 100644 --- a/src/client/handlers/AgentStop.ts +++ b/src/client/handlers/AgentStop.ts @@ -11,7 +11,7 @@ class AgentStop extends UnaryHandler< ClientRPCResponseResult > { public handle = async (): Promise => { - const { polykeyAgent } = this.container; + const { polykeyAgent }: { polykeyAgent: PolykeyAgent } = this.container; // If not running or in stopping status, then respond successfully if (!polykeyAgent[running] || polykeyAgent[status] === 'stopping') { return {}; diff --git a/src/client/handlers/AuditEventsGet.ts b/src/client/handlers/AuditEventsGet.ts index 6bea891e1..8abeefcf5 100644 --- a/src/client/handlers/AuditEventsGet.ts +++ b/src/client/handlers/AuditEventsGet.ts @@ -25,7 +25,7 @@ class AuditEventsGet extends ServerHandler< }>, ClientRPCResponseResult > { - public async *handle( + public handle = async function* ( { paths, seek, @@ -46,7 +46,7 @@ class AuditEventsGet extends ServerHandler< _meta, ctx: ContextTimed, ): AsyncGenerator> { - const { audit } = this.container; + const { audit }: { audit: Audit } = this.container; const iterators: Array> = []; let seek_: AuditEventId | number | undefined; if (seek != null) { @@ -109,7 +109,7 @@ class AuditEventsGet extends ServerHandler< data: auditEvent.data, }; } - } + }; } export default AuditEventsGet; diff --git a/src/client/handlers/AuditMetricGet.ts b/src/client/handlers/AuditMetricGet.ts index 2160ac035..5a99e7d0f 100644 --- a/src/client/handlers/AuditMetricGet.ts +++ b/src/client/handlers/AuditMetricGet.ts @@ -35,7 +35,7 @@ class AuditMetricGet extends UnaryHandler< _meta, _ctx, ): Promise>> => { - const { audit } = this.container; + const { audit }: { audit: Audit } = this.container; let seek_: AuditEventId | number | undefined; if (seek != null) { seek_ = diff --git a/src/client/handlers/GestaltsActionsGetByIdentity.ts b/src/client/handlers/GestaltsActionsGetByIdentity.ts index d974e5b75..5b5544e4d 100644 --- a/src/client/handlers/GestaltsActionsGetByIdentity.ts +++ b/src/client/handlers/GestaltsActionsGetByIdentity.ts @@ -29,7 +29,8 @@ class GestaltsActionsGetByIdentity extends UnaryHandler< actionsList: Array; }> > => { - const { db, gestaltGraph } = this.container; + const { db, gestaltGraph }: { db: DB; gestaltGraph: GestaltGraph } = + this.container; const { providerId, identityId, diff --git a/src/client/handlers/GestaltsActionsGetByNode.ts b/src/client/handlers/GestaltsActionsGetByNode.ts index 4567203fd..56e5dda3b 100644 --- a/src/client/handlers/GestaltsActionsGetByNode.ts +++ b/src/client/handlers/GestaltsActionsGetByNode.ts @@ -15,8 +15,8 @@ import { matchSync } from '../../utils'; class GestaltsActionsGetByNode extends UnaryHandler< { - gestaltGraph: GestaltGraph; db: DB; + gestaltGraph: GestaltGraph; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -24,7 +24,8 @@ class GestaltsActionsGetByNode extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { db, gestaltGraph } = this.container; + const { db, gestaltGraph }: { db: DB; gestaltGraph: GestaltGraph } = + this.container; const { nodeId }: { nodeId: NodeId } = validateSync( (keyPath, value) => { return matchSync(keyPath)( diff --git a/src/client/handlers/GestaltsActionsSetByIdentity.ts b/src/client/handlers/GestaltsActionsSetByIdentity.ts index a8cb52bd6..e1ca12806 100644 --- a/src/client/handlers/GestaltsActionsSetByIdentity.ts +++ b/src/client/handlers/GestaltsActionsSetByIdentity.ts @@ -15,8 +15,8 @@ import { matchSync } from '../../utils'; class GestaltsActionsSetByIdentityHandler extends UnaryHandler< { - gestaltGraph: GestaltGraph; db: DB; + gestaltGraph: GestaltGraph; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -24,7 +24,8 @@ class GestaltsActionsSetByIdentityHandler extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { db, gestaltGraph } = this.container; + const { db, gestaltGraph }: { db: DB; gestaltGraph: GestaltGraph } = + this.container; const { action, providerId, diff --git a/src/client/handlers/GestaltsActionsSetByNode.ts b/src/client/handlers/GestaltsActionsSetByNode.ts index 16ec4eaaf..dd8803d6f 100644 --- a/src/client/handlers/GestaltsActionsSetByNode.ts +++ b/src/client/handlers/GestaltsActionsSetByNode.ts @@ -24,7 +24,8 @@ class GestaltsActionsSetByNode extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { db, gestaltGraph } = this.container; + const { db, gestaltGraph }: { db: DB; gestaltGraph: GestaltGraph } = + this.container; const { nodeId, action }: { nodeId: NodeId; action: GestaltAction } = validateSync( (keyPath, value) => { diff --git a/src/client/handlers/GestaltsActionsUnsetByIdentity.ts b/src/client/handlers/GestaltsActionsUnsetByIdentity.ts index d1b48cf64..a36d53624 100644 --- a/src/client/handlers/GestaltsActionsUnsetByIdentity.ts +++ b/src/client/handlers/GestaltsActionsUnsetByIdentity.ts @@ -15,8 +15,8 @@ import { matchSync } from '../../utils'; class GestaltsActionsUnsetByIdentity extends UnaryHandler< { - gestaltGraph: GestaltGraph; db: DB; + gestaltGraph: GestaltGraph; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -24,7 +24,8 @@ class GestaltsActionsUnsetByIdentity extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { db, gestaltGraph } = this.container; + const { db, gestaltGraph }: { db: DB; gestaltGraph: GestaltGraph } = + this.container; const { action, providerId, diff --git a/src/client/handlers/GestaltsActionsUnsetByNode.ts b/src/client/handlers/GestaltsActionsUnsetByNode.ts index 3e975bd5c..732dfb395 100644 --- a/src/client/handlers/GestaltsActionsUnsetByNode.ts +++ b/src/client/handlers/GestaltsActionsUnsetByNode.ts @@ -15,8 +15,8 @@ import { matchSync } from '../../utils'; class GestaltsActionsUnsetByNode extends UnaryHandler< { - gestaltGraph: GestaltGraph; db: DB; + gestaltGraph: GestaltGraph; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -24,7 +24,8 @@ class GestaltsActionsUnsetByNode extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { db, gestaltGraph } = this.container; + const { db, gestaltGraph }: { db: DB; gestaltGraph: GestaltGraph } = + this.container; const { nodeId, action }: { nodeId: NodeId; action: GestaltAction } = validateSync( (keyPath, value) => { diff --git a/src/client/handlers/GestaltsDiscoveryByIdentity.ts b/src/client/handlers/GestaltsDiscoveryByIdentity.ts index c56028e0e..5ff692cbf 100644 --- a/src/client/handlers/GestaltsDiscoveryByIdentity.ts +++ b/src/client/handlers/GestaltsDiscoveryByIdentity.ts @@ -20,7 +20,7 @@ class GestaltsDiscoveryByIdentity extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { discovery } = this.container; + const { discovery }: { discovery: Discovery } = this.container; const { providerId, identityId, diff --git a/src/client/handlers/GestaltsDiscoveryByNode.ts b/src/client/handlers/GestaltsDiscoveryByNode.ts index e4ba663e0..06ba9974e 100644 --- a/src/client/handlers/GestaltsDiscoveryByNode.ts +++ b/src/client/handlers/GestaltsDiscoveryByNode.ts @@ -20,7 +20,7 @@ class GestaltsDiscoveryByNode extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { discovery } = this.container; + const { discovery }: { discovery: Discovery } = this.container; const { nodeId }: { nodeId: NodeId } = validateSync( (keyPath, value) => { return matchSync(keyPath)( diff --git a/src/client/handlers/GestaltsDiscoveryQueue.ts b/src/client/handlers/GestaltsDiscoveryQueue.ts index d747c679d..511a267f9 100644 --- a/src/client/handlers/GestaltsDiscoveryQueue.ts +++ b/src/client/handlers/GestaltsDiscoveryQueue.ts @@ -11,18 +11,18 @@ class GestaltsDiscoveryQueue extends ServerHandler< ClientRPCRequestParams, ClientRPCResponseResult > { - public async *handle( + public handle = async function* ( _input: ClientRPCRequestParams, _cancel, _meta, ctx: ContextTimed, ): AsyncGenerator> { - const { discovery } = this.container; + const { discovery }: { discovery: Discovery } = this.container; for await (const discoveryQueueInfo of discovery.getDiscoveryQueue()) { ctx.signal.throwIfAborted(); yield discoveryQueueInfo; } - } + }; } export default GestaltsDiscoveryQueue; diff --git a/src/client/handlers/GestaltsGestaltGetByIdentity.ts b/src/client/handlers/GestaltsGestaltGetByIdentity.ts index 97ba06078..a2797c936 100644 --- a/src/client/handlers/GestaltsGestaltGetByIdentity.ts +++ b/src/client/handlers/GestaltsGestaltGetByIdentity.ts @@ -15,8 +15,8 @@ import { matchSync } from '../../utils'; class GestaltsGestaltGetByIdentity extends UnaryHandler< { - gestaltGraph: GestaltGraph; db: DB; + gestaltGraph: GestaltGraph; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -24,7 +24,8 @@ class GestaltsGestaltGetByIdentity extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { db, gestaltGraph } = this.container; + const { db, gestaltGraph }: { db: DB; gestaltGraph: GestaltGraph } = + this.container; const { providerId, identityId, diff --git a/src/client/handlers/GestaltsGestaltGetByNode.ts b/src/client/handlers/GestaltsGestaltGetByNode.ts index 23d14f842..30b19d3ee 100644 --- a/src/client/handlers/GestaltsGestaltGetByNode.ts +++ b/src/client/handlers/GestaltsGestaltGetByNode.ts @@ -15,8 +15,8 @@ import { matchSync } from '../../utils'; class GestaltsGestaltGetByNode extends UnaryHandler< { - gestaltGraph: GestaltGraph; db: DB; + gestaltGraph: GestaltGraph; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -24,7 +24,8 @@ class GestaltsGestaltGetByNode extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { db, gestaltGraph } = this.container; + const { db, gestaltGraph }: { db: DB; gestaltGraph: GestaltGraph } = + this.container; const { nodeId }: { nodeId: NodeId } = validateSync( (keyPath, value) => { return matchSync(keyPath)( diff --git a/src/client/handlers/GestaltsGestaltList.ts b/src/client/handlers/GestaltsGestaltList.ts index ae0094531..11ba2819a 100644 --- a/src/client/handlers/GestaltsGestaltList.ts +++ b/src/client/handlers/GestaltsGestaltList.ts @@ -10,19 +10,20 @@ import * as nodesUtils from '../../nodes/utils'; class GestaltsGestaltList extends ServerHandler< { - gestaltGraph: GestaltGraph; db: DB; + gestaltGraph: GestaltGraph; }, ClientRPCRequestParams, ClientRPCResponseResult > { - public async *handle( + public handle = async function* ( _input, _cancel, _meta, ctx, ): AsyncGenerator> { - const { db, gestaltGraph } = this.container; + const { db, gestaltGraph }: { db: DB; gestaltGraph: GestaltGraph } = + this.container; yield* db.withTransactionG(async function* (tran): AsyncGenerator< ClientRPCResponseResult > { @@ -57,7 +58,7 @@ class GestaltsGestaltList extends ServerHandler< yield gestaltMessage; } }); - } + }; } export default GestaltsGestaltList; diff --git a/src/client/handlers/GestaltsGestaltTrustByIdentity.ts b/src/client/handlers/GestaltsGestaltTrustByIdentity.ts index b1e45e3fc..67c2504d4 100644 --- a/src/client/handlers/GestaltsGestaltTrustByIdentity.ts +++ b/src/client/handlers/GestaltsGestaltTrustByIdentity.ts @@ -14,8 +14,8 @@ import { matchSync } from '../../utils'; class GestaltsGestaltTrustByIdentity extends UnaryHandler< { - gestaltGraph: GestaltGraph; db: DB; + gestaltGraph: GestaltGraph; discovery: Discovery; }, ClientRPCRequestParams, @@ -24,7 +24,12 @@ class GestaltsGestaltTrustByIdentity extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { db, gestaltGraph, discovery } = this.container; + const { + db, + gestaltGraph, + discovery, + }: { db: DB; gestaltGraph: GestaltGraph; discovery: Discovery } = + this.container; const { providerId, identityId, diff --git a/src/client/handlers/GestaltsGestaltTrustByNode.ts b/src/client/handlers/GestaltsGestaltTrustByNode.ts index 291c84c52..3b3e0aa34 100644 --- a/src/client/handlers/GestaltsGestaltTrustByNode.ts +++ b/src/client/handlers/GestaltsGestaltTrustByNode.ts @@ -14,8 +14,8 @@ import { matchSync } from '../../utils'; class GestaltsGestaltTrustByNode extends UnaryHandler< { - gestaltGraph: GestaltGraph; db: DB; + gestaltGraph: GestaltGraph; discovery: Discovery; }, ClientRPCRequestParams, @@ -24,7 +24,12 @@ class GestaltsGestaltTrustByNode extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { db, gestaltGraph, discovery } = this.container; + const { + db, + gestaltGraph, + discovery, + }: { db: DB; gestaltGraph: GestaltGraph; discovery: Discovery } = + this.container; const { nodeId }: { nodeId: NodeId } = validateSync( (keyPath, value) => { return matchSync(keyPath)( diff --git a/src/client/handlers/IdentitiesAuthenticate.ts b/src/client/handlers/IdentitiesAuthenticate.ts index 7dad24045..86645f71e 100644 --- a/src/client/handlers/IdentitiesAuthenticate.ts +++ b/src/client/handlers/IdentitiesAuthenticate.ts @@ -15,22 +15,19 @@ class IdentitiesAuthenticate extends ServerHandler< { identitiesManager: IdentitiesManager; }, - ClientRPCRequestParams<{ - providerId: string; - }>, + ClientRPCRequestParams<{ providerId: string }>, ClientRPCResponseResult > { public timeout = 120000; // 2 Minutes - public async *handle( - input: ClientRPCRequestParams<{ - providerId: string; - }>, + public handle = async function* ( + input: ClientRPCRequestParams<{ providerId: string }>, _cancel, _meta, ctx, ): AsyncGenerator> { if (ctx.signal.aborted) throw ctx.signal.reason; - const { identitiesManager } = this.container; + const { identitiesManager }: { identitiesManager: IdentitiesManager } = + this.container; const { providerId, }: { @@ -72,7 +69,7 @@ class IdentitiesAuthenticate extends ServerHandler< identityId: authFlowResult.value, }, }; - } + }; } export default IdentitiesAuthenticate; diff --git a/src/client/handlers/IdentitiesAuthenticatedGet.ts b/src/client/handlers/IdentitiesAuthenticatedGet.ts index 72434554a..dd6de73e8 100644 --- a/src/client/handlers/IdentitiesAuthenticatedGet.ts +++ b/src/client/handlers/IdentitiesAuthenticatedGet.ts @@ -14,21 +14,18 @@ class IdentitiesAuthenticatedGet extends ServerHandler< { identitiesManager: IdentitiesManager; }, - ClientRPCRequestParams<{ - providerId?: string; - }>, + ClientRPCRequestParams<{ providerId?: string }>, ClientRPCResponseResult > { - public async *handle( - input: ClientRPCRequestParams<{ - providerId?: string; - }>, + public handle = async function* ( + input: ClientRPCRequestParams<{ providerId?: string }>, _cancel, _meta, ctx, ): AsyncGenerator> { if (ctx.signal.aborted) throw ctx.signal.reason; - const { identitiesManager } = this.container; + const { identitiesManager }: { identitiesManager: IdentitiesManager } = + this.container; let providerId: ProviderId | undefined; if (input.providerId != null) { providerId = validateSync( @@ -61,7 +58,7 @@ class IdentitiesAuthenticatedGet extends ServerHandler< }; } } - } + }; } export default IdentitiesAuthenticatedGet; diff --git a/src/client/handlers/IdentitiesClaim.ts b/src/client/handlers/IdentitiesClaim.ts index a80fa1ab2..14cbcc40e 100644 --- a/src/client/handlers/IdentitiesClaim.ts +++ b/src/client/handlers/IdentitiesClaim.ts @@ -21,7 +21,8 @@ class IdentitiesClaim extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { identitiesManager } = this.container; + const { identitiesManager }: { identitiesManager: IdentitiesManager } = + this.container; const { providerId, identityId, diff --git a/src/client/handlers/IdentitiesInfoConnectedGet.ts b/src/client/handlers/IdentitiesInfoConnectedGet.ts index 4212c9dd2..09178cded 100644 --- a/src/client/handlers/IdentitiesInfoConnectedGet.ts +++ b/src/client/handlers/IdentitiesInfoConnectedGet.ts @@ -20,14 +20,15 @@ class IdentitiesInfoConnectedGet extends ServerHandler< ClientRPCRequestParams, ClientRPCResponseResult > { - public async *handle( + public handle = async function* ( input: ClientRPCRequestParams, _cancel, _meta, ctx, ): AsyncGenerator> { if (ctx.signal.aborted) throw ctx.signal.reason; - const { identitiesManager } = this.container; + const { identitiesManager }: { identitiesManager: IdentitiesManager } = + this.container; const { providerIds, }: { @@ -105,7 +106,7 @@ class IdentitiesInfoConnectedGet extends ServerHandler< count++; } } - } + }; } export default IdentitiesInfoConnectedGet; diff --git a/src/client/handlers/IdentitiesInfoGet.ts b/src/client/handlers/IdentitiesInfoGet.ts index 51ebeda64..a85ff6d21 100644 --- a/src/client/handlers/IdentitiesInfoGet.ts +++ b/src/client/handlers/IdentitiesInfoGet.ts @@ -21,14 +21,15 @@ class IdentitiesInfoGet extends ServerHandler< ClientRPCRequestParams, ClientRPCResponseResult > { - public async *handle( + public handle = async function* ( input: ClientRPCRequestParams, _cancel, _meta, ctx, ): AsyncGenerator> { if (ctx.signal.aborted) throw ctx.signal.reason; - const { identitiesManager } = this.container; + const { identitiesManager }: { identitiesManager: IdentitiesManager } = + this.container; const { providerIds, identityId, @@ -99,7 +100,7 @@ class IdentitiesInfoGet extends ServerHandler< } } } - } + }; } export default IdentitiesInfoGet; diff --git a/src/client/handlers/IdentitiesInvite.ts b/src/client/handlers/IdentitiesInvite.ts index cc9d4c773..b2c1ebaab 100644 --- a/src/client/handlers/IdentitiesInvite.ts +++ b/src/client/handlers/IdentitiesInvite.ts @@ -24,7 +24,15 @@ class IdentitiesInvite extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { acl, notificationsManager, logger } = this.container; + const { + acl, + notificationsManager, + logger, + }: { + acl: ACL; + notificationsManager: NotificationsManager; + logger: Logger; + } = this.container; const { nodeId, }: { diff --git a/src/client/handlers/IdentitiesProvidersList.ts b/src/client/handlers/IdentitiesProvidersList.ts index 68da4e13b..b4ffde933 100644 --- a/src/client/handlers/IdentitiesProvidersList.ts +++ b/src/client/handlers/IdentitiesProvidersList.ts @@ -16,7 +16,8 @@ class IdentitiesProvidersList extends UnaryHandler< providerIds: Array; }> > => { - const { identitiesManager } = this.container; + const { identitiesManager }: { identitiesManager: IdentitiesManager } = + this.container; const providers = identitiesManager.getProviders(); return { providerIds: Object.keys(providers), diff --git a/src/client/handlers/IdentitiesTokenDelete.ts b/src/client/handlers/IdentitiesTokenDelete.ts index c7ae10bf0..b770f56fe 100644 --- a/src/client/handlers/IdentitiesTokenDelete.ts +++ b/src/client/handlers/IdentitiesTokenDelete.ts @@ -22,7 +22,10 @@ class IdentitiesTokenDeleteHandler extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { identitiesManager, db } = this.container; + const { + db, + identitiesManager, + }: { db: DB; identitiesManager: IdentitiesManager } = this.container; const { providerId, identityId, diff --git a/src/client/handlers/IdentitiesTokenGet.ts b/src/client/handlers/IdentitiesTokenGet.ts index 00ad77006..3c569f675 100644 --- a/src/client/handlers/IdentitiesTokenGet.ts +++ b/src/client/handlers/IdentitiesTokenGet.ts @@ -23,7 +23,10 @@ class IdentitiesTokenGet extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise>> => { - const { identitiesManager, db } = this.container; + const { + db, + identitiesManager, + }: { db: DB; identitiesManager: IdentitiesManager } = this.container; const { providerId, identityId, diff --git a/src/client/handlers/IdentitiesTokenPut.ts b/src/client/handlers/IdentitiesTokenPut.ts index c525bfe9e..0f6a4d1a0 100644 --- a/src/client/handlers/IdentitiesTokenPut.ts +++ b/src/client/handlers/IdentitiesTokenPut.ts @@ -23,7 +23,10 @@ class IdentitiesTokenPut extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { identitiesManager, db } = this.container; + const { + db, + identitiesManager, + }: { db: DB; identitiesManager: IdentitiesManager } = this.container; const { providerId, identityId, diff --git a/src/client/handlers/KeysCertsChainGet.ts b/src/client/handlers/KeysCertsChainGet.ts index 939f42797..c3466c550 100644 --- a/src/client/handlers/KeysCertsChainGet.ts +++ b/src/client/handlers/KeysCertsChainGet.ts @@ -13,20 +13,20 @@ class KeysCertsChainGet extends ServerHandler< ClientRPCRequestParams, ClientRPCResponseResult > { - public async *handle( + public handle = async function* ( _input, _cancel, _meta, ctx, ): AsyncGenerator> { - const { certManager } = this.container; + const { certManager }: { certManager: CertManager } = this.container; for (const certPEM of await certManager.getCertPEMsChain()) { if (ctx.signal.aborted) throw ctx.signal.reason; yield { cert: certPEM, }; } - } + }; } export default KeysCertsChainGet; diff --git a/src/client/handlers/KeysCertsGet.ts b/src/client/handlers/KeysCertsGet.ts index 8b499c187..7684af221 100644 --- a/src/client/handlers/KeysCertsGet.ts +++ b/src/client/handlers/KeysCertsGet.ts @@ -14,7 +14,7 @@ class KeysCertsGet extends UnaryHandler< ClientRPCResponseResult > { public handle = async (): Promise> => { - const { certManager } = this.container; + const { certManager }: { certManager: CertManager } = this.container; const cert = await certManager.getCurrentCertPEM(); return { cert, diff --git a/src/client/handlers/KeysDecrypt.ts b/src/client/handlers/KeysDecrypt.ts index ce23caf01..d61c96bc0 100644 --- a/src/client/handlers/KeysDecrypt.ts +++ b/src/client/handlers/KeysDecrypt.ts @@ -17,7 +17,7 @@ class KeysDecrypt extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { keyRing } = this.container; + const { keyRing }: { keyRing: KeyRing } = this.container; const data = keyRing.decrypt(Buffer.from(input.data, 'binary')); if (data == null) never(); return { diff --git a/src/client/handlers/KeysEncrypt.ts b/src/client/handlers/KeysEncrypt.ts index 2dfa5578e..5f6dd385e 100644 --- a/src/client/handlers/KeysEncrypt.ts +++ b/src/client/handlers/KeysEncrypt.ts @@ -21,7 +21,7 @@ class KeysEncrypt extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { keyRing } = this.container; + const { keyRing }: { keyRing: KeyRing } = this.container; let publicKey: PublicKey | undefined; try { diff --git a/src/client/handlers/KeysKeyPair.ts b/src/client/handlers/KeysKeyPair.ts index 45d3289fe..cd85e2676 100644 --- a/src/client/handlers/KeysKeyPair.ts +++ b/src/client/handlers/KeysKeyPair.ts @@ -18,7 +18,7 @@ class KeysKeyPair extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { keyRing } = this.container; + const { keyRing }: { keyRing: KeyRing } = this.container; const privateJWK = keysUtils.privateKeyToJWK(keyRing.keyPair.privateKey); const privateKeyJwe = keysUtils.wrapWithPassword( input.password, diff --git a/src/client/handlers/KeysKeyPairRenew.ts b/src/client/handlers/KeysKeyPairRenew.ts index 236c87690..29e3c298b 100644 --- a/src/client/handlers/KeysKeyPairRenew.ts +++ b/src/client/handlers/KeysKeyPairRenew.ts @@ -16,7 +16,7 @@ class KeysKeyPairRenew extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { certManager } = this.container; + const { certManager }: { certManager: CertManager } = this.container; // Other domains will be updated accordingly via the `EventBus` so we // only need to modify the KeyManager diff --git a/src/client/handlers/KeysKeyPairReset.ts b/src/client/handlers/KeysKeyPairReset.ts index 92dd634ce..3ab41360c 100644 --- a/src/client/handlers/KeysKeyPairReset.ts +++ b/src/client/handlers/KeysKeyPairReset.ts @@ -16,7 +16,7 @@ class KeysKeyPairReset extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { certManager } = this.container; + const { certManager }: { certManager: CertManager } = this.container; // Other domains will be updated accordingly via the `EventBus` so we // only need to modify the KeyManager await certManager.resetCertWithNewKeyPair(input.password); diff --git a/src/client/handlers/KeysPasswordChange.ts b/src/client/handlers/KeysPasswordChange.ts index 14640e4e8..f1db2af8d 100644 --- a/src/client/handlers/KeysPasswordChange.ts +++ b/src/client/handlers/KeysPasswordChange.ts @@ -16,7 +16,7 @@ class KeysPasswordChange extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { keyRing } = this.container; + const { keyRing }: { keyRing: KeyRing } = this.container; await keyRing.changePassword(input.password); return {}; }; diff --git a/src/client/handlers/KeysPublicKey.ts b/src/client/handlers/KeysPublicKey.ts index e5f73926a..398a958a2 100644 --- a/src/client/handlers/KeysPublicKey.ts +++ b/src/client/handlers/KeysPublicKey.ts @@ -17,7 +17,7 @@ class KeysPublicKey extends UnaryHandler< public handle = async (): Promise< ClientRPCResponseResult > => { - const { keyRing } = this.container; + const { keyRing }: { keyRing: KeyRing } = this.container; const publicKeyJwk = keysUtils.publicKeyToJWK(keyRing.keyPair.publicKey); return { publicKeyJwk, diff --git a/src/client/handlers/KeysSign.ts b/src/client/handlers/KeysSign.ts index 9033686a8..da629b162 100644 --- a/src/client/handlers/KeysSign.ts +++ b/src/client/handlers/KeysSign.ts @@ -17,7 +17,7 @@ class KeysSign extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { keyRing } = this.container; + const { keyRing }: { keyRing: KeyRing } = this.container; const signature = keyRing.sign(Buffer.from(input.data, 'binary')); return { signature: signature.toString('binary'), diff --git a/src/client/handlers/KeysVerify.ts b/src/client/handlers/KeysVerify.ts index 6675273df..24d29f451 100644 --- a/src/client/handlers/KeysVerify.ts +++ b/src/client/handlers/KeysVerify.ts @@ -21,7 +21,7 @@ class KeysVerify extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { keyRing } = this.container; + const { keyRing }: { keyRing: KeyRing } = this.container; let publicKey: PublicKey | undefined; try { const jwk = input.publicKeyJwk; @@ -35,9 +35,7 @@ class KeysVerify extends UnaryHandler< Buffer.from(input.data, 'binary'), Buffer.from(input.signature, 'binary') as Signature, ); - return { - success, - }; + return { type: 'success', success: success }; }; } diff --git a/src/client/handlers/NodesAdd.ts b/src/client/handlers/NodesAdd.ts index 77240eeac..68d4985da 100644 --- a/src/client/handlers/NodesAdd.ts +++ b/src/client/handlers/NodesAdd.ts @@ -16,8 +16,8 @@ import { validateSync } from '../../validation'; class NodesAdd extends UnaryHandler< { - nodeManager: NodeManager; db: DB; + nodeManager: NodeManager; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -25,7 +25,8 @@ class NodesAdd extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { nodeManager, db } = this.container; + const { db, nodeManager }: { db: DB; nodeManager: NodeManager } = + this.container; const { nodeId, host, diff --git a/src/client/handlers/NodesClaim.ts b/src/client/handlers/NodesClaim.ts index 748fa5c43..9056f07bd 100644 --- a/src/client/handlers/NodesClaim.ts +++ b/src/client/handlers/NodesClaim.ts @@ -14,8 +14,8 @@ import { validateSync } from '../../validation'; class NodesClaim extends UnaryHandler< { - nodeManager: NodeManager; db: DB; + nodeManager: NodeManager; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -23,8 +23,8 @@ class NodesClaim extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { nodeManager, db } = this.container; - + const { db, nodeManager }: { db: DB; nodeManager: NodeManager } = + this.container; const { nodeId, }: { @@ -45,9 +45,7 @@ class NodesClaim extends UnaryHandler< // if there is no permission then we get an error await nodeManager.claimNode(nodeId, tran); }); - return { - success: true, - }; + return { type: 'success', success: true }; }; } diff --git a/src/client/handlers/NodesFind.ts b/src/client/handlers/NodesFind.ts index 5e6e954b2..d8de9d7ba 100644 --- a/src/client/handlers/NodesFind.ts +++ b/src/client/handlers/NodesFind.ts @@ -26,8 +26,7 @@ class NodesFind extends UnaryHandler< _meta, ctx: ContextTimed, ): Promise> => { - const { nodeManager } = this.container; - + const { nodeManager }: { nodeManager: NodeManager } = this.container; const { nodeId, }: { diff --git a/src/client/handlers/NodesGetAll.ts b/src/client/handlers/NodesGetAll.ts index ba7bf94b6..1477edc2d 100644 --- a/src/client/handlers/NodesGetAll.ts +++ b/src/client/handlers/NodesGetAll.ts @@ -3,7 +3,6 @@ import type { ClientRPCResponseResult, NodesGetMessage, } from '../types'; -import type KeyRing from '../../keys/KeyRing'; import type NodeGraph from '../../nodes/NodeGraph'; import { ServerHandler } from '@matrixai/rpc'; import * as nodesUtils from '../../nodes/utils'; @@ -11,12 +10,11 @@ import * as nodesUtils from '../../nodes/utils'; class NodesGetAll extends ServerHandler< { nodeGraph: NodeGraph; - keyRing: KeyRing; }, ClientRPCRequestParams, ClientRPCResponseResult > { - public async *handle( + public handle = async function* ( _input, _cancel, _meta, @@ -38,7 +36,7 @@ class NodesGetAll extends ServerHandler< }; } } - } + }; } export default NodesGetAll; diff --git a/src/client/handlers/NodesListConnections.ts b/src/client/handlers/NodesListConnections.ts index 98d4c972b..0e7710189 100644 --- a/src/client/handlers/NodesListConnections.ts +++ b/src/client/handlers/NodesListConnections.ts @@ -14,13 +14,15 @@ class NodesListConnections extends ServerHandler< ClientRPCRequestParams, ClientRPCResponseResult > { - public async *handle( + public handle = async function* ( _input, _cancel, _meta, ctx, ): AsyncGenerator> { - const { nodeConnectionManager } = this.container; + const { + nodeConnectionManager, + }: { nodeConnectionManager: NodeConnectionManager } = this.container; const connections = nodeConnectionManager.listConnections(); for (const connection of connections) { if (ctx.signal.aborted) throw ctx.signal.reason; @@ -33,7 +35,7 @@ class NodesListConnections extends ServerHandler< usageCount: connection.usageCount, }; } - } + }; } export default NodesListConnections; diff --git a/src/client/handlers/NodesPing.ts b/src/client/handlers/NodesPing.ts index 74c9f5658..7d9e16f03 100644 --- a/src/client/handlers/NodesPing.ts +++ b/src/client/handlers/NodesPing.ts @@ -21,7 +21,7 @@ class NodesPing extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { nodeManager } = this.container; + const { nodeManager }: { nodeManager: NodeManager } = this.container; const { nodeId, }: { @@ -38,9 +38,7 @@ class NodesPing extends UnaryHandler< }, ); const result = await nodeManager.pingNode(nodeId); - return { - success: result != null, - }; + return { type: 'success', success: result != null }; }; } diff --git a/src/client/handlers/NotificationsInboxClear.ts b/src/client/handlers/NotificationsInboxClear.ts index 51008676a..1789a5bc0 100644 --- a/src/client/handlers/NotificationsInboxClear.ts +++ b/src/client/handlers/NotificationsInboxClear.ts @@ -12,7 +12,10 @@ class NotificationsInboxClear extends UnaryHandler< ClientRPCResponseResult > { public handle = async (): Promise => { - const { db, notificationsManager } = this.container; + const { + db, + notificationsManager, + }: { db: DB; notificationsManager: NotificationsManager } = this.container; await db.withTransactionF((tran) => notificationsManager.clearInboxNotifications(tran), ); diff --git a/src/client/handlers/NotificationsInboxRead.ts b/src/client/handlers/NotificationsInboxRead.ts index afd145e22..232204d0d 100644 --- a/src/client/handlers/NotificationsInboxRead.ts +++ b/src/client/handlers/NotificationsInboxRead.ts @@ -25,7 +25,10 @@ class NotificationsInboxRead extends ServerHandler< ctx, ): AsyncGenerator> { if (ctx.signal.aborted) throw ctx.signal.reason; - const { db, notificationsManager } = this.container; + const { + db, + notificationsManager, + }: { db: DB; notificationsManager: NotificationsManager } = this.container; const { seek, seekEnd, unread, order, limit } = input; let seek_: NotificationId | number | undefined; diff --git a/src/client/handlers/NotificationsInboxRemove.ts b/src/client/handlers/NotificationsInboxRemove.ts index 08a44ee2a..ae061132f 100644 --- a/src/client/handlers/NotificationsInboxRemove.ts +++ b/src/client/handlers/NotificationsInboxRemove.ts @@ -20,7 +20,10 @@ class NotificationsInboxRemove extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { db, notificationsManager } = this.container; + const { + db, + notificationsManager, + }: { db: DB; notificationsManager: NotificationsManager } = this.container; const notificationId = notificationsUtils.decodeNotificationId( input.notificationIdEncoded, ); diff --git a/src/client/handlers/NotificationsOutboxClear.ts b/src/client/handlers/NotificationsOutboxClear.ts index c3b9092b1..3bbe1b5b1 100644 --- a/src/client/handlers/NotificationsOutboxClear.ts +++ b/src/client/handlers/NotificationsOutboxClear.ts @@ -12,7 +12,10 @@ class NotificationsOutboxClear extends UnaryHandler< ClientRPCResponseResult > { public handle = async (): Promise => { - const { db, notificationsManager } = this.container; + const { + db, + notificationsManager, + }: { db: DB; notificationsManager: NotificationsManager } = this.container; await db.withTransactionF((tran) => notificationsManager.clearOutboxNotifications(tran), ); diff --git a/src/client/handlers/NotificationsOutboxRead.ts b/src/client/handlers/NotificationsOutboxRead.ts index b5e0ddbfc..8160f1d1b 100644 --- a/src/client/handlers/NotificationsOutboxRead.ts +++ b/src/client/handlers/NotificationsOutboxRead.ts @@ -25,7 +25,10 @@ class NotificationsOutboxRead extends ServerHandler< ctx, ): AsyncGenerator> { if (ctx.signal.aborted) throw ctx.signal.reason; - const { db, notificationsManager } = this.container; + const { + db, + notificationsManager, + }: { db: DB; notificationsManager: NotificationsManager } = this.container; const { seek, seekEnd, order, limit } = input; let seek_: NotificationId | number | undefined; diff --git a/src/client/handlers/NotificationsOutboxRemove.ts b/src/client/handlers/NotificationsOutboxRemove.ts index 677ed381a..10412a4f7 100644 --- a/src/client/handlers/NotificationsOutboxRemove.ts +++ b/src/client/handlers/NotificationsOutboxRemove.ts @@ -20,7 +20,10 @@ class NotificationsOutboxRemove extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { db, notificationsManager } = this.container; + const { + db, + notificationsManager, + }: { db: DB; notificationsManager: NotificationsManager } = this.container; const notificationId = notificationsUtils.decodeNotificationId( input.notificationIdEncoded, ); diff --git a/src/client/handlers/NotificationsSend.ts b/src/client/handlers/NotificationsSend.ts index 162cb3e81..86de7d3e9 100644 --- a/src/client/handlers/NotificationsSend.ts +++ b/src/client/handlers/NotificationsSend.ts @@ -21,7 +21,9 @@ class NotificationsSend extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise => { - const { notificationsManager } = this.container; + const { + notificationsManager, + }: { notificationsManager: NotificationsManager } = this.container; const { nodeId, }: { diff --git a/src/client/handlers/VaultsClone.ts b/src/client/handlers/VaultsClone.ts index 35f8e5b98..ff2b99376 100644 --- a/src/client/handlers/VaultsClone.ts +++ b/src/client/handlers/VaultsClone.ts @@ -23,7 +23,8 @@ class VaultsClone extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { db, vaultManager } = this.container; + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = + this.container; const { nodeId, }: { @@ -43,9 +44,7 @@ class VaultsClone extends UnaryHandler< await db.withTransactionF(async (tran) => { await vaultManager.cloneVault(nodeId, input.nameOrId, tran); }); - return { - success: true, - }; + return { type: 'success', success: true }; }; } diff --git a/src/client/handlers/VaultsCreate.ts b/src/client/handlers/VaultsCreate.ts index 2b2aba40f..cd7f503d0 100644 --- a/src/client/handlers/VaultsCreate.ts +++ b/src/client/handlers/VaultsCreate.ts @@ -20,7 +20,8 @@ class VaultsCreate extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { db, vaultManager } = this.container; + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = + this.container; const vaultId = await db.withTransactionF((tran) => vaultManager.createVault(input.vaultName, tran), diff --git a/src/client/handlers/VaultsDelete.ts b/src/client/handlers/VaultsDelete.ts index efc7938c9..d341d4f66 100644 --- a/src/client/handlers/VaultsDelete.ts +++ b/src/client/handlers/VaultsDelete.ts @@ -22,7 +22,8 @@ class VaultsDelete extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { db, vaultManager } = this.container; + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = + this.container; await db.withTransactionF(async (tran) => { const vaultIdFromName = await vaultManager.getVaultId( input.nameOrId as VaultName, @@ -35,9 +36,7 @@ class VaultsDelete extends UnaryHandler< } await vaultManager.destroyVault(vaultId, tran); }); - return { - success: true, - }; + return { type: 'success', success: true }; }; } diff --git a/src/client/handlers/VaultsList.ts b/src/client/handlers/VaultsList.ts index 106856bae..fb0c0aa3d 100644 --- a/src/client/handlers/VaultsList.ts +++ b/src/client/handlers/VaultsList.ts @@ -16,14 +16,15 @@ class VaultsList extends ServerHandler< ClientRPCRequestParams, ClientRPCResponseResult > { - public async *handle( + public handle = async function* ( _input, _cancel, _meta, ctx, ): AsyncGenerator> { if (ctx.signal.aborted) throw ctx.signal.reason; - const { db, vaultManager } = this.container; + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = + this.container; const vaults = await db.withTransactionF((tran) => vaultManager.listVaults(tran), ); @@ -34,7 +35,7 @@ class VaultsList extends ServerHandler< vaultIdEncoded: vaultsUtils.encodeVaultId(vaultId), }; } - } + }; } export default VaultsList; diff --git a/src/client/handlers/VaultsLog.ts b/src/client/handlers/VaultsLog.ts index a0f0b48a1..591b6ca9c 100644 --- a/src/client/handlers/VaultsLog.ts +++ b/src/client/handlers/VaultsLog.ts @@ -19,14 +19,15 @@ class VaultsLog extends ServerHandler< ClientRPCRequestParams, ClientRPCResponseResult > { - public async *handle( + public handle = async function* ( input: ClientRPCRequestParams, _cancel, _meta, ctx, ): AsyncGenerator> { if (ctx.signal.aborted) throw ctx.signal.reason; - const { db, vaultManager } = this.container; + 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, @@ -55,7 +56,7 @@ class VaultsLog extends ServerHandler< message: entry.message, }; } - } + }; } export default VaultsLog; diff --git a/src/client/handlers/VaultsPermissionGet.ts b/src/client/handlers/VaultsPermissionGet.ts index de4dfe00a..9e08d6bff 100644 --- a/src/client/handlers/VaultsPermissionGet.ts +++ b/src/client/handlers/VaultsPermissionGet.ts @@ -24,14 +24,18 @@ class VaultsPermissionGet extends ServerHandler< ClientRPCRequestParams, ClientRPCResponseResult > { - public async *handle( + public handle = async function* ( input: ClientRPCRequestParams, _cancel, _meta, ctx, ): AsyncGenerator> { if (ctx.signal.aborted) throw ctx.signal.reason; - const { db, vaultManager, acl } = this.container; + const { + db, + vaultManager, + acl, + }: { db: DB; vaultManager: VaultManager; acl: ACL } = this.container; const [rawPermissions, vaultId] = await db.withTransactionF( async (tran) => { const vaultIdFromName = await vaultManager.getVaultId( @@ -65,7 +69,7 @@ class VaultsPermissionGet extends ServerHandler< vaultPermissionList: actions, }; } - } + }; } export default VaultsPermissionGet; diff --git a/src/client/handlers/VaultsPermissionSet.ts b/src/client/handlers/VaultsPermissionSet.ts index 2d3cff44d..358fd8340 100644 --- a/src/client/handlers/VaultsPermissionSet.ts +++ b/src/client/handlers/VaultsPermissionSet.ts @@ -5,12 +5,12 @@ import type { PermissionSetMessage, SuccessMessage, } from '../types'; -import type VaultManager from '../../vaults/VaultManager'; -import type GestaltGraph from '../../gestalts/GestaltGraph'; import type ACL from '../../acl/ACL'; -import type NotificationsManager from '../../notifications/NotificationsManager'; import type { VaultAction, VaultActions } from '../../vaults/types'; import type { NodeId } from '../../ids'; +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'; @@ -32,8 +32,19 @@ class VaultsPermissionSet extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { db, vaultManager, gestaltGraph, acl, notificationsManager } = - this.container; + const { + db, + vaultManager, + gestaltGraph, + acl, + notificationsManager, + }: { + db: DB; + vaultManager: VaultManager; + gestaltGraph: GestaltGraph; + acl: ACL; + notificationsManager: NotificationsManager; + } = this.container; await db.withTransactionF(async (tran) => { const vaultIdFromName = await vaultManager.getVaultId( input.nameOrId, @@ -84,9 +95,7 @@ class VaultsPermissionSet extends UnaryHandler< }, }); }); - return { - success: true, - }; + return { type: 'success', success: true }; }; } diff --git a/src/client/handlers/VaultsPermissionUnset.ts b/src/client/handlers/VaultsPermissionUnset.ts index 188fa2591..4e19a91d7 100644 --- a/src/client/handlers/VaultsPermissionUnset.ts +++ b/src/client/handlers/VaultsPermissionUnset.ts @@ -30,7 +30,17 @@ class VaultsPermissionUnset extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { db, vaultManager, gestaltGraph, acl } = this.container; + const { + db, + vaultManager, + gestaltGraph, + acl, + }: { + db: DB; + vaultManager: VaultManager; + gestaltGraph: GestaltGraph; + acl: ACL; + } = this.container; await db.withTransactionF(async (tran) => { const vaultIdFromName = await vaultManager.getVaultId( input.nameOrId, @@ -83,9 +93,7 @@ class VaultsPermissionUnset extends UnaryHandler< } }); // Formatting response - return { - success: true, - }; + return { type: 'success', success: true }; }; } diff --git a/src/client/handlers/VaultsPull.ts b/src/client/handlers/VaultsPull.ts index e5b0e506b..328ea8a78 100644 --- a/src/client/handlers/VaultsPull.ts +++ b/src/client/handlers/VaultsPull.ts @@ -26,7 +26,8 @@ class VaultsPull extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { db, vaultManager } = this.container; + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = + this.container; let pullVaultId; pullVaultId = vaultsUtils.decodeVaultId(input.pullVault); pullVaultId = pullVaultId ?? input.pullVault; @@ -62,9 +63,7 @@ class VaultsPull extends UnaryHandler< tran, }); }); - return { - success: true, - }; + return { type: 'success', success: true }; }; } diff --git a/src/client/handlers/VaultsRename.ts b/src/client/handlers/VaultsRename.ts index 5d891739f..3720e792e 100644 --- a/src/client/handlers/VaultsRename.ts +++ b/src/client/handlers/VaultsRename.ts @@ -12,8 +12,8 @@ import * as vaultsErrors from '../../vaults/errors'; class VaultsRename extends UnaryHandler< { - vaultManager: VaultManager; db: DB; + vaultManager: VaultManager; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -21,7 +21,8 @@ class VaultsRename extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { db, vaultManager } = this.container; + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = + this.container; return await db.withTransactionF(async (tran) => { const vaultIdFromName = await vaultManager.getVaultId( input.nameOrId, diff --git a/src/client/handlers/VaultsScan.ts b/src/client/handlers/VaultsScan.ts index 8f4eaa596..7d0880399 100644 --- a/src/client/handlers/VaultsScan.ts +++ b/src/client/handlers/VaultsScan.ts @@ -18,14 +18,14 @@ class VaultsScan extends ServerHandler< ClientRPCRequestParams, ClientRPCResponseResult > { - public async *handle( + public handle = async function* ( input: ClientRPCRequestParams, _cancel, _meta, ctx, ): AsyncGenerator> { if (ctx.signal.aborted) throw ctx.signal.reason; - const { vaultManager } = this.container; + const { vaultManager }: { vaultManager: VaultManager } = this.container; const { nodeId, }: { @@ -53,7 +53,7 @@ class VaultsScan extends ServerHandler< permissions: vaultPermissions, }; } - } + }; } export default VaultsScan; diff --git a/src/client/handlers/VaultsSecretsEnv.ts b/src/client/handlers/VaultsSecretsEnv.ts index 6b0a230ac..acfc78119 100644 --- a/src/client/handlers/VaultsSecretsEnv.ts +++ b/src/client/handlers/VaultsSecretsEnv.ts @@ -12,8 +12,8 @@ import * as vaultsErrors from '../../vaults/errors'; class VaultsSecretsList extends DuplexHandler< { - vaultManager: VaultManager; db: DB; + vaultManager: VaultManager; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -27,7 +27,7 @@ class VaultsSecretsList extends DuplexHandler< ctx, ): AsyncGenerator> { if (ctx.signal.aborted) throw ctx.signal.reason; - const { vaultManager, db }: { vaultManager: VaultManager; db: DB } = + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = this.container; return yield* db.withTransactionG(async function* (tran): AsyncGenerator< diff --git a/src/client/handlers/VaultsSecretsGet.ts b/src/client/handlers/VaultsSecretsGet.ts index b32618cc1..361016490 100644 --- a/src/client/handlers/VaultsSecretsGet.ts +++ b/src/client/handlers/VaultsSecretsGet.ts @@ -13,8 +13,8 @@ import * as vaultOps from '../../vaults/VaultOps'; class VaultsSecretsGet extends DuplexHandler< { - vaultManager: VaultManager; db: DB; + vaultManager: VaultManager; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -26,7 +26,8 @@ class VaultsSecretsGet extends DuplexHandler< ctx, ): AsyncGenerator> { if (ctx.signal.aborted) throw ctx.signal.reason; - const { vaultManager, db } = this.container; + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = + this.container; yield* db.withTransactionG(async function* (tran): AsyncGenerator< ClientRPCResponseResult > { diff --git a/src/client/handlers/VaultsSecretsList.ts b/src/client/handlers/VaultsSecretsList.ts index 277fcdd90..5e5735357 100644 --- a/src/client/handlers/VaultsSecretsList.ts +++ b/src/client/handlers/VaultsSecretsList.ts @@ -13,17 +13,17 @@ import * as vaultsErrors from '../../vaults/errors'; class VaultsSecretsList extends ServerHandler< { - vaultManager: VaultManager; db: DB; + vaultManager: VaultManager; }, ClientRPCRequestParams, ClientRPCResponseResult > { - public async *handle( + public handle = async function* ( input: ClientRPCRequestParams, - _cancel: any, ): AsyncGenerator, void, void> { - const { vaultManager, db } = this.container; + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = + this.container; const vaultId = await db.withTransactionF(async (tran) => { const vaultIdFromName = await vaultManager.getVaultId( input.nameOrId, @@ -65,7 +65,7 @@ class VaultsSecretsList extends ServerHandler< } }); }); - } + }; } export default VaultsSecretsList; diff --git a/src/client/handlers/VaultsSecretsMkdir.ts b/src/client/handlers/VaultsSecretsMkdir.ts index cdcfd86ce..0249db33f 100644 --- a/src/client/handlers/VaultsSecretsMkdir.ts +++ b/src/client/handlers/VaultsSecretsMkdir.ts @@ -2,50 +2,61 @@ import type { DB } from '@matrixai/db'; import type { ClientRPCRequestParams, ClientRPCResponseResult, - SecretMkdirMessage, - SuccessMessage, + SecretDirMessage, + SuccessOrErrorMessage, } from '../types'; import type VaultManager from '../../vaults/VaultManager'; -import { UnaryHandler } from '@matrixai/rpc'; +import type { POJO } from '../../types'; +import { DuplexHandler } from '@matrixai/rpc'; import * as vaultsUtils from '../../vaults/utils'; import * as vaultsErrors from '../../vaults/errors'; import * as vaultOps from '../../vaults/VaultOps'; -class VaultsSecretsMkdir extends UnaryHandler< +class VaultsSecretsMkdir extends DuplexHandler< { - vaultManager: VaultManager; db: DB; + vaultManager: VaultManager; }, - ClientRPCRequestParams, - ClientRPCResponseResult + ClientRPCRequestParams, + ClientRPCResponseResult > { - public handle = async ( - input: ClientRPCRequestParams, - ): Promise> => { - const { vaultManager, db } = this.container; - await db.withTransactionF(async (tran) => { - const vaultIdFromName = await vaultManager.getVaultId( - input.nameOrId, - tran, - ); - const vaultId = - vaultIdFromName ?? vaultsUtils.decodeVaultId(input.nameOrId); - if (vaultId == null) { - throw new vaultsErrors.ErrorVaultsVaultUndefined(); - } - await vaultManager.withVaults( - [vaultId], - async (vault) => { - await vaultOps.mkdir(vault, input.dirName, { - recursive: input.recursive, - }); - }, - tran, - ); - }); - return { - success: true, - }; + public handle = async function* ( + input: AsyncIterable>, + ): AsyncGenerator> { + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = + this.container; + let metadata: POJO; + yield* db.withTransactionG( + async function* (tran): AsyncGenerator { + for await (const secretDirMessage of input) { + // Unpack input + if (metadata == null) metadata = secretDirMessage.metadata ?? {}; + const nameOrId = secretDirMessage.nameOrId; + const dirName = secretDirMessage.dirName; + // Get vaultId + const vaultIdFromName = await vaultManager.getVaultId(nameOrId, tran); + const vaultId = + vaultIdFromName ?? vaultsUtils.decodeVaultId(nameOrId); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } + // Write directories. This doesn't need to be grouped by vault names, + // as no commit is created for empty directories anyways. The + // vaultOps.mkdir() method also returns an object of type + // SuccessOrErrorMessage. As such, we can return the result without + // doing any type conversion or extra processing. + yield await vaultManager.withVaults( + [vaultId], + async (vault) => { + return await vaultOps.mkdir(vault, dirName, { + recursive: metadata?.options?.recursive, + }); + }, + tran, + ); + } + }, + ); }; } diff --git a/src/client/handlers/VaultsSecretsNew.ts b/src/client/handlers/VaultsSecretsNew.ts index 61744ba24..33b21fd12 100644 --- a/src/client/handlers/VaultsSecretsNew.ts +++ b/src/client/handlers/VaultsSecretsNew.ts @@ -13,8 +13,8 @@ import * as vaultOps from '../../vaults/VaultOps'; class VaultsSecretsNew extends UnaryHandler< { - vaultManager: VaultManager; db: DB; + vaultManager: VaultManager; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -22,7 +22,8 @@ class VaultsSecretsNew extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { vaultManager, db } = this.container; + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = + this.container; await db.withTransactionF(async (tran) => { const vaultIdFromName = await vaultManager.getVaultId( input.nameOrId, @@ -42,9 +43,7 @@ class VaultsSecretsNew extends UnaryHandler< tran, ); }); - return { - success: true, - }; + return { type: 'success', success: true }; }; } diff --git a/src/client/handlers/VaultsSecretsNewDir.ts b/src/client/handlers/VaultsSecretsNewDir.ts index 2aede5f95..22d436914 100644 --- a/src/client/handlers/VaultsSecretsNewDir.ts +++ b/src/client/handlers/VaultsSecretsNewDir.ts @@ -14,9 +14,9 @@ import * as vaultOps from '../../vaults/VaultOps'; class VaultsSecretsNewDir extends UnaryHandler< { - vaultManager: VaultManager; db: DB; fs: FileSystem; + vaultManager: VaultManager; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -24,7 +24,11 @@ class VaultsSecretsNewDir extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { vaultManager, db, fs } = this.container; + const { + db, + fs, + vaultManager, + }: { db: DB; fs: FileSystem; vaultManager: VaultManager } = this.container; await db.withTransactionF(async (tran) => { const vaultIdFromName = await vaultManager.getVaultId( input.nameOrId, @@ -43,9 +47,7 @@ class VaultsSecretsNewDir extends UnaryHandler< tran, ); }); - return { - success: true, - }; + return { type: 'success', success: true }; }; } diff --git a/src/client/handlers/VaultsSecretsRemove.ts b/src/client/handlers/VaultsSecretsRemove.ts index b5f99308e..c06807639 100644 --- a/src/client/handlers/VaultsSecretsRemove.ts +++ b/src/client/handlers/VaultsSecretsRemove.ts @@ -13,8 +13,8 @@ import * as vaultOps from '../../vaults/VaultOps'; class VaultsSecretsRemove extends ClientHandler< { - vaultManager: VaultManager; db: DB; + vaultManager: VaultManager; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -22,7 +22,8 @@ class VaultsSecretsRemove extends ClientHandler< public handle = async ( input: AsyncIterable>, ): Promise> => { - const { vaultManager, db } = this.container; + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = + this.container; // Create a record of secrets to be removed, grouped by vault names const vaultGroups: Record> = {}; const secretNames: Array<[string, string]> = []; @@ -58,7 +59,7 @@ class VaultsSecretsRemove extends ClientHandler< } }); - return { success: true }; + return { type: 'success', success: true }; }; } diff --git a/src/client/handlers/VaultsSecretsRename.ts b/src/client/handlers/VaultsSecretsRename.ts index a2b59011c..433b409ee 100644 --- a/src/client/handlers/VaultsSecretsRename.ts +++ b/src/client/handlers/VaultsSecretsRename.ts @@ -13,8 +13,8 @@ import * as vaultOps from '../../vaults/VaultOps'; class VaultsSecretsRename extends UnaryHandler< { - vaultManager: VaultManager; db: DB; + vaultManager: VaultManager; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -22,7 +22,8 @@ class VaultsSecretsRename extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { vaultManager, db } = this.container; + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = + this.container; await db.withTransactionF(async (tran) => { const vaultIdFromName = await vaultManager.getVaultId( input.nameOrId, @@ -45,9 +46,7 @@ class VaultsSecretsRename extends UnaryHandler< tran, ); }); - return { - success: true, - }; + return { type: 'success', success: true }; }; } diff --git a/src/client/handlers/VaultsSecretsStat.ts b/src/client/handlers/VaultsSecretsStat.ts index 81fb3f5eb..456ff611e 100644 --- a/src/client/handlers/VaultsSecretsStat.ts +++ b/src/client/handlers/VaultsSecretsStat.ts @@ -13,8 +13,8 @@ import * as vaultOps from '../../vaults/VaultOps'; class VaultsSecretsStat extends UnaryHandler< { - vaultManager: VaultManager; db: DB; + vaultManager: VaultManager; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -22,7 +22,8 @@ class VaultsSecretsStat extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { vaultManager, db } = this.container; + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = + this.container; return await db.withTransactionF(async (tran) => { const vaultIdFromName = await vaultManager.getVaultId( input.nameOrId, diff --git a/src/client/handlers/VaultsSecretsWriteFile.ts b/src/client/handlers/VaultsSecretsWriteFile.ts index da11f5e68..7a1cc7d5d 100644 --- a/src/client/handlers/VaultsSecretsWriteFile.ts +++ b/src/client/handlers/VaultsSecretsWriteFile.ts @@ -13,8 +13,8 @@ import * as vaultOps from '../../vaults/VaultOps'; class VaultsSecretsWriteFile extends UnaryHandler< { - vaultManager: VaultManager; db: DB; + vaultManager: VaultManager; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -22,7 +22,8 @@ class VaultsSecretsWriteFile extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { vaultManager, db } = this.container; + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = + this.container; await db.withTransactionF(async (tran) => { const vaultIdFromName = await vaultManager.getVaultId( input.nameOrId, @@ -42,9 +43,7 @@ class VaultsSecretsWriteFile extends UnaryHandler< tran, ); }); - return { - success: true, - }; + return { type: 'success', success: true }; }; } diff --git a/src/client/handlers/VaultsVersion.ts b/src/client/handlers/VaultsVersion.ts index 23ab02dc6..f32daaa9f 100644 --- a/src/client/handlers/VaultsVersion.ts +++ b/src/client/handlers/VaultsVersion.ts @@ -12,8 +12,8 @@ import * as vaultsErrors from '../../vaults/errors'; class VaultsVersion extends UnaryHandler< { - vaultManager: VaultManager; db: DB; + vaultManager: VaultManager; }, ClientRPCRequestParams, ClientRPCResponseResult @@ -21,7 +21,8 @@ class VaultsVersion extends UnaryHandler< public handle = async ( input: ClientRPCRequestParams, ): Promise> => { - const { vaultManager, db } = this.container; + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = + this.container; return await db.withTransactionF(async (tran) => { const vaultIdFromName = await vaultManager.getVaultId( input.nameOrId, diff --git a/src/client/types.ts b/src/client/types.ts index faca7089e..32acc7046 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -201,9 +201,19 @@ type SignatureMessage = { type VerifySignatureMessage = PublicKeyMessage & DataMessage & SignatureMessage; type SuccessMessage = { + type: 'success'; success: boolean; }; +type ErrorMessage = { + type: 'error'; + code: string; + reason?: string; + data?: JSONObject; +}; + +type SuccessOrErrorMessage = SuccessMessage | ErrorMessage; + // Notifications messages type NotificationReadMessage = { @@ -317,11 +327,6 @@ type ContentWithErrorMessage = ContentMessage & { type SecretContentMessage = SecretIdentifierMessage & ContentMessage; -type SecretMkdirMessage = VaultIdentifierMessage & { - dirName: string; - recursive: boolean; -}; - type SecretDirMessage = VaultIdentifierMessage & { dirName: string; }; @@ -398,6 +403,8 @@ export type { NodesGetMessage, NodesAddMessage, SuccessMessage, + ErrorMessage, + SuccessOrErrorMessage, NotificationInboxMessage, NotificationOutboxMessage, NotificationReadMessage, @@ -423,7 +430,6 @@ export type { ContentMessage, ContentWithErrorMessage, SecretContentMessage, - SecretMkdirMessage, SecretDirMessage, SecretRenameMessage, SecretFilesMessage, diff --git a/src/vaults/VaultOps.ts b/src/vaults/VaultOps.ts index 6cffeab9a..ea559ab1d 100644 --- a/src/vaults/VaultOps.ts +++ b/src/vaults/VaultOps.ts @@ -4,6 +4,7 @@ import type Logger from '@matrixai/logger'; import type { Vault } from './Vault'; import type { Stat } from 'encryptedfs'; +import type { SuccessOrErrorMessage } from '../client/types'; import path from 'path'; import * as vaultsErrors from './errors'; import * as vaultsUtils from './utils'; @@ -173,29 +174,35 @@ async function mkdir( dirPath: string, fileOptions?: FileOptions, logger?: Logger, -): Promise { +): Promise { const recursive = fileOptions?.recursive ?? false; - - await vault.writeF(async (efs) => { - try { + // Technically, writing an empty directory won't make a commit, and doesn't + // need a write resource as git doesn't track empty directories. It is + // still being used to allow concurrency. + try { + await vault.writeF(async (efs) => { await efs.mkdir(dirPath, fileOptions); - } catch (e) { - if (e.code === 'ENOENT' && !recursive) { - throw new vaultsErrors.ErrorVaultsRecursive( - `Could not create directory '${dirPath}' without recursive option`, - { cause: e }, - ); - } - if (e.code === 'EEXIST') { - throw new vaultsErrors.ErrorSecretsSecretDefined( - `${dirPath} already exists`, - { cause: e }, - ); - } - throw e; + logger?.info(`Created secret directory at '${dirPath}'`); + }); + return { type: 'success', success: true }; + } catch (e) { + logger?.error(`Failed to create directory '${dirPath}'. Reason: ${e.code}`); + if (e.code === 'ENOENT' && !recursive) { + return { + type: 'error', + code: e.code, + reason: dirPath, + }; } - logger?.info(`Created secret directory at '${dirPath}'`); - }); + if (e.code === 'EEXIST') { + return { + type: 'error', + code: e.code, + reason: dirPath, + }; + } + throw e; + } } /** diff --git a/tests/client/handlers/vaults.test.ts b/tests/client/handlers/vaults.test.ts index df44c11ce..6bd41b77d 100644 --- a/tests/client/handlers/vaults.test.ts +++ b/tests/client/handlers/vaults.test.ts @@ -64,6 +64,7 @@ import { vaultsSecretsStat, vaultsVersion, } from '@/client/callers'; +import * as utils from '@/utils'; import * as keysUtils from '@/keys/utils'; import * as nodesUtils from '@/nodes/utils'; import * as vaultsUtils from '@/vaults/utils'; @@ -1341,18 +1342,149 @@ describe('vaultsSecretsMkdir', () => { const vaultName = 'test-vault'; const vaultId = await vaultManager.createVault(vaultName); const dirPath = 'dir/dir1/dir2'; - const response = await rpcClient.methods.vaultsSecretsMkdir({ - recursive: true, + const response = await rpcClient.methods.vaultsSecretsMkdir(); + const writer = response.writable.getWriter(); + await writer.write({ nameOrId: vaultsUtils.encodeVaultId(vaultId), dirName: dirPath, + metadata: { options: { recursive: true } }, }); - expect(response.success).toBeTruthy(); + await writer.close(); + + for await (const data of response.readable) { + expect(data.type).toEqual('success'); + } await vaultManager.withVaults([vaultId], async (vault) => { await vault.readF(async (efs) => { expect(await efs.exists(dirPath)).toBeTruthy(); }); }); }); + test('fails to make directories without recursive', async () => { + const vaultName = 'test-vault'; + const vaultId = await vaultManager.createVault(vaultName); + const encodeVaultId = vaultsUtils.encodeVaultId(vaultId); + const dirPath = 'dir/dir1/dir2'; + const response = await rpcClient.methods.vaultsSecretsMkdir(); + const writer = response.writable.getWriter(); + await writer.write({ nameOrId: encodeVaultId, dirName: dirPath }); + await writer.close(); + for await (const data of response.readable) { + expect(data.type).toEqual('error'); + if (data.type !== 'error') utils.never(); + expect(data.code).toEqual('ENOENT'); + expect(data.reason).toEqual(dirPath); + } + await vaultManager.withVaults([vaultId], async (vault) => { + await vault.readF(async (efs) => { + expect(await efs.exists(dirPath)).toBeFalsy(); + }); + }); + }); + test('makes directories across multiple vaults', async () => { + const vaultName1 = 'test-vault1'; + const vaultName2 = 'test-vault2'; + const vaultId1 = await vaultManager.createVault(vaultName1); + const vaultId2 = await vaultManager.createVault(vaultName2); + const vaultIdEncoded1 = vaultsUtils.encodeVaultId(vaultId1); + const vaultIdEncoded2 = vaultsUtils.encodeVaultId(vaultId2); + const dirPath1 = 'dir-1'; + const dirPath2 = 'dir-2'; + const dirPath3 = 'dir-3'; + // Attempt to make directories + const response = await rpcClient.methods.vaultsSecretsMkdir(); + const writer = response.writable.getWriter(); + await writer.write({ + nameOrId: vaultIdEncoded1, + dirName: dirPath1, + metadata: { options: { recursive: true } }, + }); + await writer.write({ nameOrId: vaultIdEncoded2, dirName: dirPath2 }); + await writer.write({ nameOrId: vaultIdEncoded1, dirName: dirPath3 }); + await writer.close(); + // Check if the operation concluded as expected + for await (const data of response.readable) { + expect(data.type).toEqual('success'); + } + await vaultManager.withVaults( + [vaultId1, vaultId2], + async (vault1, vault2) => { + await vault1.readF(async (efs) => { + expect(await efs.exists(dirPath1)).toBeTruthy(); + expect(await efs.exists(dirPath3)).toBeTruthy(); + }); + await vault2.readF(async (efs) => { + expect(await efs.exists(dirPath2)).toBeTruthy(); + }); + }, + ); + }); + test('continues on error', async () => { + const vaultName1 = 'test-vault1'; + const vaultName2 = 'test-vault2'; + const vaultId1 = await vaultManager.createVault(vaultName1); + const vaultId2 = await vaultManager.createVault(vaultName2); + const vaultIdEncoded1 = vaultsUtils.encodeVaultId(vaultId1); + const vaultIdEncoded2 = vaultsUtils.encodeVaultId(vaultId2); + const dirPath1 = 'dir-1'; + const dirPath2 = 'dir-2'; + const dirPath3 = 'nodir/dir-3'; + // Attempt to make directories + const response = await rpcClient.methods.vaultsSecretsMkdir(); + const writer = response.writable.getWriter(); + await writer.write({ nameOrId: vaultIdEncoded1, dirName: dirPath1 }); + await writer.write({ nameOrId: vaultIdEncoded2, dirName: dirPath2 }); + await writer.write({ nameOrId: vaultIdEncoded1, dirName: dirPath3 }); + await writer.close(); + // Check if the operation concluded as expected + for await (const data of response.readable) { + if (data.type !== 'success') { + expect(data.code).toEqual('ENOENT'); + expect(data.reason).toEqual(dirPath3); + } + } + await vaultManager.withVaults( + [vaultId1, vaultId2], + async (vault1, vault2) => { + await vault1.readF(async (efs) => { + expect(await efs.exists(dirPath1)).toBeTruthy(); + expect(await efs.exists(dirPath3)).toBeFalsy(); + }); + await vault2.readF(async (efs) => { + expect(await efs.exists(dirPath2)).toBeTruthy(); + }); + }, + ); + }); + test('fails if secret with same name exists', async () => { + const vaultName = 'test-vault'; + const vaultId = await vaultManager.createVault(vaultName); + const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId); + const dirPath = 'secret-first'; + await vaultManager.withVaults([vaultId], async (vault) => { + await vault.writeF(async (efs) => { + await efs.writeFile(dirPath, dirPath); + }); + }); + // Attempt to make directory + const response = await rpcClient.methods.vaultsSecretsMkdir(); + const writer = response.writable.getWriter(); + await writer.write({ nameOrId: vaultIdEncoded, dirName: dirPath }); + await writer.close(); + // Check if the operation concluded as expected + for await (const data of response.readable) { + expect(data.type).toEqual('error'); + if (data.type !== 'error') utils.never(); + expect(data.code).toEqual('EEXIST'); + expect(data.reason).toEqual(dirPath); + } + await vaultManager.withVaults([vaultId], async (vault) => { + await vault.readF(async (efs) => { + const stat = await efs.stat(dirPath); + expect(stat.isFile).toBeTruthy(); + }); + }); + }); }); describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { const logger = new Logger('vaultsSecretsNewDeleteGet test', LogLevel.WARN, [ @@ -1377,35 +1509,45 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { }>; let vaultManager: VaultManager; // Helper function to create secrets in a vault - const createVaultSecret = async (vaultName: string, secretName: string) => { - const createResponse = await rpcClient.methods.vaultsSecretsNew({ - nameOrId: vaultName, - secretName: secretName, - secretContent: Buffer.from(secretName).toString('binary'), + const createVaultSecret = async ( + vaultId: VaultId, + secretName: string, + content: string, + ) => { + await vaultManager.withVaults([vaultId], async (vault) => { + await vault.writeF(async (efs) => { + await efs.writeFile(secretName, content); + expect(await efs.exists(secretName)).toBeTruthy(); + }); }); - expect(createResponse.success).toBeTruthy(); }; // Helper function to ensure each file path was deleted - const checkSecretIsDeleted = async ( - vaultName: string, - secretName: string, - ) => { - await testsUtils.expectRemoteError( - rpcClient.methods.vaultsSecretsStat({ - nameOrId: vaultName, - secretName: secretName, - }), - vaultsErrors.ErrorSecretsSecretUndefined, - ); + const checkSecretIsDeleted = async (vaultId: VaultId, secretName: string) => { + await vaultManager.withVaults([vaultId], async (vault) => { + await vault.readF(async (efs) => { + expect(await efs.exists(secretName)).toBeFalsy(); + }); + }); }; // Helper function to ensure each file path exists in the vault - const checkSecretExists = async (vaultName: string, secretName: string) => { - await expect( - rpcClient.methods.vaultsSecretsStat({ - nameOrId: vaultName, - secretName: secretName, - }), - ).toResolve(); + const checkSecretExists = async (vaultId: VaultId, secretName: string) => { + await vaultManager.withVaults([vaultId], async (vault) => { + await vault.readF(async (efs) => { + expect(await efs.exists(secretName)).toBeTruthy(); + }); + }); + }; + // Helper function to create a directory + const createVaultDir = async ( + vaultId: VaultId, + dirName: string, + recursive: boolean = false, + ) => { + await vaultManager.withVaults([vaultId], async (vault) => { + await vault.writeF(async (efs) => { + await efs.mkdir(dirName, { recursive: recursive }); + }); + }); }; beforeEach(async () => { dataDir = await fs.promises.mkdtemp( @@ -1554,9 +1696,9 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { const secretName3 = 'test-secret3'; const vaultId = await vaultManager.createVault('test-vault'); const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId); - await createVaultSecret(vaultIdEncoded, secretName1); - await createVaultSecret(vaultIdEncoded, secretName2); - await createVaultSecret(vaultIdEncoded, secretName3); + await createVaultSecret(vaultId, secretName1, secretName1); + await createVaultSecret(vaultId, secretName2, secretName2); + await createVaultSecret(vaultId, secretName3, secretName3); // Get secrets const getStream = await rpcClient.methods.vaultsSecretsGet(); const getWriter = getStream.writable.getWriter(); @@ -1587,8 +1729,8 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { const secretName2 = 'test-secret2'; const vaultId = await vaultManager.createVault('test-vault'); const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId); - await createVaultSecret(vaultIdEncoded, secretName1); - await createVaultSecret(vaultIdEncoded, secretName2); + await createVaultSecret(vaultId, secretName1, secretName1); + await createVaultSecret(vaultId, secretName2, secretName2); // Get secrets const getStream = await rpcClient.methods.vaultsSecretsGet(); const getWriter = getStream.writable.getWriter(); @@ -1622,8 +1764,8 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { const secretName2 = 'test-secret2'; const vaultId = await vaultManager.createVault('test-vault'); const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId); - await createVaultSecret(vaultIdEncoded, secretName1); - await createVaultSecret(vaultIdEncoded, secretName2); + await createVaultSecret(vaultId, secretName1, secretName1); + await createVaultSecret(vaultId, secretName2, secretName2); // Delete secrets const deleteStream = await rpcClient.methods.vaultsSecretsRemove(); const deleteWriter = deleteStream.writable.getWriter(); @@ -1638,8 +1780,8 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { await deleteWriter.close(); expect((await deleteStream.output).success).toBeTruthy(); // Check each secret was deleted - await checkSecretIsDeleted(vaultIdEncoded, secretName1); - await checkSecretIsDeleted(vaultIdEncoded, secretName2); + await checkSecretIsDeleted(vaultId, secretName1); + await checkSecretIsDeleted(vaultId, secretName2); }); test('gets secrets from multiple vaults', async () => { // Create secret @@ -1650,9 +1792,9 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { const vaultId2 = await vaultManager.createVault('test-vault2'); const vaultIdEncoded1 = vaultsUtils.encodeVaultId(vaultId1); const vaultIdEncoded2 = vaultsUtils.encodeVaultId(vaultId2); - await createVaultSecret(vaultIdEncoded1, secretName1); - await createVaultSecret(vaultIdEncoded1, secretName2); - await createVaultSecret(vaultIdEncoded2, secretName3); + await createVaultSecret(vaultId1, secretName1, secretName1); + await createVaultSecret(vaultId1, secretName2, secretName2); + await createVaultSecret(vaultId2, secretName3, secretName3); // Get secret const getStream = await rpcClient.methods.vaultsSecretsGet(); const getWriter = getStream.writable.getWriter(); @@ -1686,9 +1828,9 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { const vaultId2 = await vaultManager.createVault('test-vault2'); const vaultIdEncoded1 = vaultsUtils.encodeVaultId(vaultId1); const vaultIdEncoded2 = vaultsUtils.encodeVaultId(vaultId2); - await createVaultSecret(vaultIdEncoded1, secretName1); - await createVaultSecret(vaultIdEncoded1, secretName2); - await createVaultSecret(vaultIdEncoded2, secretName3); + await createVaultSecret(vaultId1, secretName1, secretName1); + await createVaultSecret(vaultId1, secretName2, secretName2); + await createVaultSecret(vaultId2, secretName3, secretName3); // Get log size let logLength1 = 0; let logLength2 = 0; @@ -1726,15 +1868,6 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { ); }); test('should recursively delete directories', async () => { - // Helper function to create directories - const createDir = async (vaultName: string, dirName: string) => { - const createResponse = await rpcClient.methods.vaultsSecretsMkdir({ - nameOrId: vaultName, - dirName: dirName, - recursive: false, - }); - expect(createResponse.success).toBeTruthy(); - }; // Create secrets const vaultId = await vaultManager.createVault('test-vault'); const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId); @@ -1742,10 +1875,10 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { const secretName1 = `${secretDir}/test-secret1`; const secretName2 = `${secretDir}/test-secret2`; const secretName3 = `${secretDir}/test-secret3`; - await createDir(vaultIdEncoded, secretDir); - await createVaultSecret(vaultIdEncoded, secretName1); - await createVaultSecret(vaultIdEncoded, secretName2); - await createVaultSecret(vaultIdEncoded, secretName3); + await createVaultDir(vaultId, secretDir); + await createVaultSecret(vaultId, secretName1, secretName1); + await createVaultSecret(vaultId, secretName2, secretName2); + await createVaultSecret(vaultId, secretName3, secretName3); // Deleting directory with recursive set should not fail const deleteStream = await rpcClient.methods.vaultsSecretsRemove(); await (async () => { @@ -1759,9 +1892,9 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { })(); expect((await deleteStream.output).success).toBeTruthy(); // Check each secret and the secret directory were deleted - await checkSecretIsDeleted(vaultIdEncoded, secretName1); - await checkSecretIsDeleted(vaultIdEncoded, secretName2); - await checkSecretIsDeleted(vaultIdEncoded, secretName3); + await checkSecretIsDeleted(vaultId, secretName1); + await checkSecretIsDeleted(vaultId, secretName2); + await checkSecretIsDeleted(vaultId, secretName3); await testsUtils.expectRemoteError( rpcClient.methods.vaultsSecretsStat({ nameOrId: vaultIdEncoded, @@ -1771,15 +1904,6 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { ); }); test('should fail to delete directory without recursive option', async () => { - // Helper function to create directories - const createDir = async (vaultName: string, dirName: string) => { - const createResponse = await rpcClient.methods.vaultsSecretsMkdir({ - nameOrId: vaultName, - dirName: dirName, - recursive: false, - }); - expect(createResponse.success).toBeTruthy(); - }; // Create secrets const vaultId = await vaultManager.createVault('test-vault'); const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId); @@ -1787,10 +1911,10 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { const secretName1 = `${secretDir}/test-secret1`; const secretName2 = `${secretDir}/test-secret2`; const secretName3 = `${secretDir}/test-secret3`; - await createDir(vaultIdEncoded, secretDir); - await createVaultSecret(vaultIdEncoded, secretName1); - await createVaultSecret(vaultIdEncoded, secretName2); - await createVaultSecret(vaultIdEncoded, secretName3); + await createVaultDir(vaultId, secretDir); + await createVaultSecret(vaultId, secretName1, secretName1); + await createVaultSecret(vaultId, secretName2, secretName2); + await createVaultSecret(vaultId, secretName3, secretName3); // Deleting directory with recursive unset should fail const failDeleteStream = await rpcClient.methods.vaultsSecretsRemove(); await (async () => { @@ -1803,10 +1927,10 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { vaultsErrors.ErrorVaultsRecursive, ); // Check each secret and the secret directory exist - await checkSecretExists(vaultIdEncoded, secretName1); - await checkSecretExists(vaultIdEncoded, secretName2); - await checkSecretExists(vaultIdEncoded, secretName3); - await checkSecretExists(vaultIdEncoded, secretDir); + await checkSecretExists(vaultId, secretName1); + await checkSecretExists(vaultId, secretName2); + await checkSecretExists(vaultId, secretName3); + await checkSecretExists(vaultId, secretDir); }); }); describe('vaultsSecretsNewDir and vaultsSecretsList', () => { diff --git a/tests/vaults/VaultOps.test.ts b/tests/vaults/VaultOps.test.ts index bd329d464..078676580 100644 --- a/tests/vaults/VaultOps.test.ts +++ b/tests/vaults/VaultOps.test.ts @@ -4,6 +4,7 @@ import type KeyRing from '@/keys/KeyRing'; import type { LevelPath } from '@matrixai/db'; import type { FileTree } from '@/vaults/types'; import type { ContentNode, TreeNode } from '@/vaults/types'; +import type { ErrorMessage } from '@/client/types'; import fs from 'fs'; import path from 'path'; import os from 'os'; @@ -323,32 +324,40 @@ describe('VaultOps', () => { }); describe('mkdir', () => { test('can create directory', async () => { - await vaultOps.mkdir(vault, dirName); + const response = await vaultOps.mkdir(vault, dirName); + expect(response.type).toEqual('success'); await expectDirExists(dirName); }); test('can create recursive directory', async () => { const dirPath = path.join(dirName, dirName); - await vaultOps.mkdir(vault, dirPath, { recursive: true }); + const response = await vaultOps.mkdir(vault, dirPath, { + recursive: true, + }); + expect(response.type).toEqual('success'); await expectDirExists(dirPath); }); - test('creating recursive directory fails without recursive set', async () => { + test('creating directories fails without recursive', async () => { const dirPath = path.join(dirName, dirName); - await expect(vaultOps.mkdir(vault, dirPath)).rejects.toThrow( - vaultsErrors.ErrorVaultsRecursive, - ); + const response = await vaultOps.mkdir(vault, dirPath); + expect(response.type).toEqual('error'); + const error = response as ErrorMessage; + expect(error.code).toEqual('ENOENT'); await expectDirExistsNot(dirPath); }); test('creating existing directory should fail', async () => { await mkdir(dirName); - await expect(vaultOps.mkdir(vault, dirName)).rejects.toThrow( - vaultsErrors.ErrorSecretsSecretDefined, - ); + const response = await vaultOps.mkdir(vault, dirName); + expect(response.type).toEqual('error'); + const error = response as ErrorMessage; + expect(error.code).toEqual('EEXIST'); }); test('creating existing secret should fail', async () => { await writeSecret(secretName, secretContent); - await expect(vaultOps.mkdir(vault, secretName)).rejects.toThrow( - vaultsErrors.ErrorSecretsSecretDefined, - ); + const response = await vaultOps.mkdir(vault, secretName); + expect(response.type).toEqual('error'); + const error = response as ErrorMessage; + expect(error.code).toEqual('EEXIST'); + await expectSecret(secretName, secretContent); }); }); describe('addSecretDirectory', () => {