Skip to content

Commit 23176b7

Browse files
authored
Merge pull request #846 from MatrixAI/feature-eng-432-review-and-refactor-rpc-handlers-to-handle-cancellation
Add cancellation to RPC handlers for the `vaults` domain
2 parents 906c32e + 2972ad3 commit 23176b7

File tree

78 files changed

+2741
-1392
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+2741
-1392
lines changed

src/acl/ACL.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,7 @@ class ACL {
144144
if (permId in permIds) {
145145
nodePerm = permIds[permId];
146146
// Get the first existing perm object
147-
let perm: Permission;
148-
for (const nodeId_ in nodePerm) {
149-
perm = nodePerm[nodeId_];
150-
break;
151-
}
147+
const perm = Object.values(nodePerm)[0];
152148
// All perm objects are shared
153149
nodePerm[nodeId] = perm!;
154150
} else {
@@ -614,8 +610,8 @@ class ACL {
614610
[...this.aclNodesDbPath, nodeId.toBuffer()],
615611
true,
616612
);
617-
// Skip if the nodeId doesn't exist
618-
// this means that it previously been removed
613+
// Skip if the nodeId doesn't exist. This means that it has previously
614+
// been removed.
619615
if (permId == null) {
620616
continue;
621617
}

src/client/callers/vaultsSecretsGet.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { HandlerTypes } from '@matrixai/rpc';
22
import type VaultsSecretsGet from '../handlers/VaultsSecretsGet';
3-
import { ServerCaller } from '@matrixai/rpc';
3+
import { UnaryCaller } from '@matrixai/rpc';
44

55
type CallerTypes = HandlerTypes<VaultsSecretsGet>;
66

7-
const vaultsSecretsGet = new ServerCaller<
7+
const vaultsSecretsGet = new UnaryCaller<
88
CallerTypes['input'],
99
CallerTypes['output']
1010
>();

src/client/handlers/AgentStatus.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import type {
55
} from '../types';
66
import type PolykeyAgent from '../../PolykeyAgent';
77
import { UnaryHandler } from '@matrixai/rpc';
8-
import * as nodesUtils from '../../nodes/utils';
98
import config from '../../config';
9+
import * as nodesUtils from '../../nodes/utils';
1010

1111
class AgentStatus extends UnaryHandler<
1212
{

src/client/handlers/AuditEventsGet.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ContextTimed } from '@matrixai/contexts';
2+
import type { JSONValue } from '@matrixai/rpc';
23
import type { ClientRPCRequestParams, ClientRPCResponseResult } from '../types';
34
import type {
45
AuditEvent,
@@ -42,8 +43,8 @@ class AuditEventsGet extends ServerHandler<
4243
}> & {
4344
paths: Array<TopicSubPath>;
4445
},
45-
_cancel,
46-
_meta,
46+
_cancel: (reason?: any) => void,
47+
_meta: Record<string, JSONValue>,
4748
ctx: ContextTimed,
4849
): AsyncGenerator<ClientRPCResponseResult<AuditEventSerialized>> {
4950
const { audit }: { audit: Audit } = this.container;

src/client/handlers/GestaltsActionsGetByIdentity.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import type GestaltGraph from '../../gestalts/GestaltGraph';
88
import type { GestaltAction } from '../../gestalts/types';
99
import type { IdentityId, ProviderId } from '../../ids';
1010
import { UnaryHandler } from '@matrixai/rpc';
11-
import * as ids from '../../ids';
1211
import { validateSync } from '../../validation';
1312
import { matchSync } from '../../utils';
13+
import * as ids from '../../ids';
1414

1515
class GestaltsActionsGetByIdentity extends UnaryHandler<
1616
{

src/client/handlers/IdentitiesAuthenticate.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { ContextTimed } from '@matrixai/contexts';
2+
import type { JSONValue } from '@matrixai/rpc';
13
import type {
24
AuthProcessMessage,
35
ClientRPCRequestParams,
@@ -21,11 +23,10 @@ class IdentitiesAuthenticate extends ServerHandler<
2123
public timeout = 120000; // 2 Minutes
2224
public handle = async function* (
2325
input: ClientRPCRequestParams<{ providerId: string }>,
24-
_cancel,
25-
_meta,
26-
ctx,
26+
_cancel: (reason?: any) => void,
27+
_meta: Record<string, JSONValue>,
28+
ctx: ContextTimed,
2729
): AsyncGenerator<ClientRPCResponseResult<AuthProcessMessage>> {
28-
if (ctx.signal.aborted) throw ctx.signal.reason;
2930
const { identitiesManager }: { identitiesManager: IdentitiesManager } =
3031
this.container;
3132
const {
@@ -52,7 +53,7 @@ class IdentitiesAuthenticate extends ServerHandler<
5253
if (authFlowResult.done) {
5354
never('authFlow signalled done too soon');
5455
}
55-
if (ctx.signal.aborted) throw ctx.signal.reason;
56+
ctx.signal.throwIfAborted();
5657
yield {
5758
request: {
5859
url: authFlowResult.value.url,
@@ -63,7 +64,7 @@ class IdentitiesAuthenticate extends ServerHandler<
6364
if (!authFlowResult.done) {
6465
never('authFlow did not signal done when expected');
6566
}
66-
if (ctx.signal.aborted) throw ctx.signal.reason;
67+
ctx.signal.throwIfAborted();
6768
yield {
6869
response: {
6970
identityId: authFlowResult.value,

src/client/handlers/IdentitiesAuthenticatedGet.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { ContextTimed } from '@matrixai/contexts';
2+
import type { JSONValue } from '@matrixai/rpc';
13
import type {
24
ClientRPCRequestParams,
35
ClientRPCResponseResult,
@@ -19,11 +21,10 @@ class IdentitiesAuthenticatedGet extends ServerHandler<
1921
> {
2022
public handle = async function* (
2123
input: ClientRPCRequestParams<{ providerId?: string }>,
22-
_cancel,
23-
_meta,
24-
ctx,
24+
_cancel: (reason?: any) => void,
25+
_meta: Record<string, JSONValue>,
26+
ctx: ContextTimed,
2527
): AsyncGenerator<ClientRPCResponseResult<IdentityMessage>> {
26-
if (ctx.signal.aborted) throw ctx.signal.reason;
2728
const { identitiesManager }: { identitiesManager: IdentitiesManager } =
2829
this.container;
2930
let providerId: ProviderId | undefined;
@@ -46,12 +47,10 @@ class IdentitiesAuthenticatedGet extends ServerHandler<
4647
: [providerId];
4748
for (const providerId of providerIds) {
4849
const provider = identitiesManager.getProvider(providerId);
49-
if (provider == null) {
50-
continue;
51-
}
50+
if (provider == null) continue;
5251
const identities = await provider.getAuthIdentityIds();
5352
for (const identityId of identities) {
54-
if (ctx.signal.aborted) throw ctx.signal.reason;
53+
ctx.signal.throwIfAborted();
5554
yield {
5655
providerId: provider.id,
5756
identityId: identityId,

src/client/handlers/IdentitiesInfoConnectedGet.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { ContextTimed } from '@matrixai/contexts';
2+
import type { JSONValue } from '@matrixai/rpc';
13
import type {
24
ClientRPCRequestParams,
35
ClientRPCResponseResult,
@@ -22,11 +24,10 @@ class IdentitiesInfoConnectedGet extends ServerHandler<
2224
> {
2325
public handle = async function* (
2426
input: ClientRPCRequestParams<ProviderSearchMessage>,
25-
_cancel,
26-
_meta,
27-
ctx,
27+
_cancel: (reason?: any) => void,
28+
_meta: Record<string, JSONValue>,
29+
ctx: ContextTimed,
2830
): AsyncGenerator<ClientRPCResponseResult<IdentityInfoMessage>> {
29-
if (ctx.signal.aborted) throw ctx.signal.reason;
3031
const { identitiesManager }: { identitiesManager: IdentitiesManager } =
3132
this.container;
3233
const {
@@ -71,6 +72,7 @@ class IdentitiesInfoConnectedGet extends ServerHandler<
7172
}
7273
const identities: Array<AsyncGenerator<IdentityData>> = [];
7374
for (const providerId of providerIds) {
75+
ctx.signal.throwIfAborted();
7476
// Get provider from id
7577
const provider = identitiesManager.getProvider(providerId);
7678
if (provider === undefined) {
@@ -94,7 +96,7 @@ class IdentitiesInfoConnectedGet extends ServerHandler<
9496
let count = 0;
9597
for (const gen of identities) {
9698
for await (const identity of gen) {
97-
if (ctx.signal.aborted) throw ctx.signal.reason;
99+
ctx.signal.throwIfAborted();
98100
if (input.limit !== undefined && count >= input.limit) break;
99101
yield {
100102
providerId: identity.providerId,

src/client/handlers/IdentitiesInfoGet.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { ContextTimed } from '@matrixai/contexts';
2+
import type { JSONValue } from '@matrixai/rpc';
13
import type {
24
ClientRPCRequestParams,
35
ClientRPCResponseResult,
@@ -8,11 +10,11 @@ import type { IdentityId, ProviderId } from '../../ids';
810
import type IdentitiesManager from '../../identities/IdentitiesManager';
911
import type { IdentityData } from '../../identities/types';
1012
import { ServerHandler } from '@matrixai/rpc';
13+
import { validateSync } from '../../validation';
14+
import { matchSync } from '../../utils';
1115
import * as ids from '../../ids';
1216
import * as identitiesErrors from '../../identities/errors';
1317
import * as identitiesUtils from '../../identities/utils';
14-
import { validateSync } from '../../validation';
15-
import { matchSync } from '../../utils';
1618

1719
class IdentitiesInfoGet extends ServerHandler<
1820
{
@@ -23,9 +25,9 @@ class IdentitiesInfoGet extends ServerHandler<
2325
> {
2426
public handle = async function* (
2527
input: ClientRPCRequestParams<ProviderSearchMessage>,
26-
_cancel,
27-
_meta,
28-
ctx,
28+
_cancel: (reason?: any) => void,
29+
_meta: Record<string, JSONValue>,
30+
ctx: ContextTimed,
2931
): AsyncGenerator<ClientRPCResponseResult<IdentityInfoMessage>> {
3032
if (ctx.signal.aborted) throw ctx.signal.reason;
3133
const { identitiesManager }: { identitiesManager: IdentitiesManager } =
@@ -86,7 +88,7 @@ class IdentitiesInfoGet extends ServerHandler<
8688
input.limit = identities.length;
8789
}
8890
for (let i = 0; i < input.limit; i++) {
89-
if (ctx.signal.aborted) throw ctx.signal.reason;
91+
ctx.signal.throwIfAborted();
9092
const identity = identities[i];
9193
if (identity !== undefined) {
9294
if (identitiesUtils.matchIdentityData(identity, searchTerms)) {

src/client/handlers/KeysCertsChainGet.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { ContextTimed } from '@matrixai/contexts';
2+
import type { JSONValue } from '@matrixai/rpc';
13
import type {
24
CertMessage,
35
ClientRPCRequestParams,
@@ -14,17 +16,15 @@ class KeysCertsChainGet extends ServerHandler<
1416
ClientRPCResponseResult<CertMessage>
1517
> {
1618
public handle = async function* (
17-
_input,
18-
_cancel,
19-
_meta,
20-
ctx,
19+
_input: ClientRPCRequestParams,
20+
_cancel: (reason?: any) => void,
21+
_meta: Record<string, JSONValue>,
22+
ctx: ContextTimed,
2123
): AsyncGenerator<ClientRPCResponseResult<CertMessage>> {
2224
const { certManager }: { certManager: CertManager } = this.container;
2325
for (const certPEM of await certManager.getCertPEMsChain()) {
24-
if (ctx.signal.aborted) throw ctx.signal.reason;
25-
yield {
26-
cert: certPEM,
27-
};
26+
ctx.signal.throwIfAborted();
27+
yield { cert: certPEM };
2828
}
2929
};
3030
}

src/client/handlers/KeysCertsGet.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ class KeysCertsGet extends UnaryHandler<
1616
public handle = async (): Promise<ClientRPCResponseResult<CertMessage>> => {
1717
const { certManager }: { certManager: CertManager } = this.container;
1818
const cert = await certManager.getCurrentCertPEM();
19-
return {
20-
cert,
21-
};
19+
return { cert };
2220
};
2321
}
2422

src/client/handlers/KeysKeyPairRenew.ts

-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ class KeysKeyPairRenew extends UnaryHandler<
1717
input: ClientRPCRequestParams<PasswordMessage>,
1818
): Promise<ClientRPCResponseResult> => {
1919
const { certManager }: { certManager: CertManager } = this.container;
20-
2120
// Other domains will be updated accordingly via the `EventBus` so we
2221
// only need to modify the KeyManager
2322
await certManager.renewCertWithNewKeyPair(input.password);
24-
2523
return {};
2624
};
2725
}

src/client/handlers/KeysVerify.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class KeysVerify extends UnaryHandler<
3535
Buffer.from(input.data, 'binary'),
3636
Buffer.from(input.signature, 'binary') as Signature,
3737
);
38-
return { type: 'success', success: success };
38+
return { success: success };
3939
};
4040
}
4141

src/client/handlers/NodesAdd.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import type { ContextTimed } from '@matrixai/contexts';
12
import type { DB } from '@matrixai/db';
3+
import type { JSONValue } from '@matrixai/rpc';
24
import type {
35
ClientRPCRequestParams,
46
ClientRPCResponseResult,
@@ -8,11 +10,11 @@ import type { NodeId } from '../../ids';
810
import type { Host, Port } from '../../network/types';
911
import type NodeManager from '../../nodes/NodeManager';
1012
import { UnaryHandler } from '@matrixai/rpc';
13+
import { matchSync } from '../../utils';
14+
import { validateSync } from '../../validation';
1115
import * as ids from '../../ids';
1216
import * as networkUtils from '../../network/utils';
1317
import * as nodeErrors from '../../nodes/errors';
14-
import { matchSync } from '../../utils';
15-
import { validateSync } from '../../validation';
1618

1719
class NodesAdd extends UnaryHandler<
1820
{
@@ -24,6 +26,9 @@ class NodesAdd extends UnaryHandler<
2426
> {
2527
public handle = async (
2628
input: ClientRPCRequestParams<NodesAddMessage>,
29+
_cancel: (reason?: any) => void,
30+
_meta: Record<string, JSONValue>,
31+
ctx: ContextTimed,
2732
): Promise<ClientRPCResponseResult> => {
2833
const { db, nodeManager }: { db: DB; nodeManager: NodeManager } =
2934
this.container;
@@ -72,8 +77,8 @@ class NodesAdd extends UnaryHandler<
7277
true,
7378
input.force ?? false,
7479
1500,
75-
undefined,
7680
tran,
81+
ctx,
7782
),
7883
);
7984
return {};

src/client/handlers/NodesClaim.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ class NodesClaim extends UnaryHandler<
4141
},
4242
);
4343
await db.withTransactionF(async (tran) => {
44-
// Attempt to claim the node,
45-
// if there is no permission then we get an error
44+
// Attempt to claim the node. If there is no permission then we get an
45+
// error.
4646
await nodeManager.claimNode(nodeId, tran);
4747
});
48-
return { type: 'success', success: true };
48+
return { success: true };
4949
};
5050
}
5151

src/client/handlers/NodesFind.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { JSONValue } from '@matrixai/rpc';
2+
import type { ContextTimed } from '@matrixai/contexts';
13
import type {
24
ClientRPCRequestParams,
35
ClientRPCResponseResult,
@@ -6,12 +8,11 @@ import type {
68
} from '../types';
79
import type { NodeId } from '../../ids';
810
import type NodeManager from '../../nodes/NodeManager';
9-
import type { ContextTimed } from '@matrixai/contexts';
1011
import { UnaryHandler } from '@matrixai/rpc';
11-
import * as ids from '../../ids';
12-
import * as nodesErrors from '../../nodes/errors';
1312
import { validateSync } from '../../validation';
1413
import { matchSync } from '../../utils';
14+
import * as ids from '../../ids';
15+
import * as nodesErrors from '../../nodes/errors';
1516

1617
class NodesFind extends UnaryHandler<
1718
{
@@ -22,8 +23,8 @@ class NodesFind extends UnaryHandler<
2223
> {
2324
public handle = async (
2425
input: ClientRPCRequestParams<NodeIdMessage>,
25-
_cancel,
26-
_meta,
26+
_cancel: (reason?: any) => void,
27+
_meta: Record<string, JSONValue>,
2728
ctx: ContextTimed,
2829
): Promise<ClientRPCResponseResult<NodesFindMessage>> => {
2930
const { nodeManager }: { nodeManager: NodeManager } = this.container;
@@ -42,12 +43,7 @@ class NodesFind extends UnaryHandler<
4243
nodeId: input.nodeIdEncoded,
4344
},
4445
);
45-
const result = await nodeManager.findNode(
46-
{
47-
nodeId: nodeId,
48-
},
49-
ctx,
50-
);
46+
const result = await nodeManager.findNode({ nodeId: nodeId }, ctx);
5147
if (result == null) {
5248
throw new nodesErrors.ErrorNodeGraphNodeIdNotFound();
5349
}

0 commit comments

Comments
 (0)