Skip to content

Commit 9c26b16

Browse files
committed
wip: fixing up vaults agent handlers
1 parent ea2928a commit 9c26b16

File tree

11 files changed

+207
-180
lines changed

11 files changed

+207
-180
lines changed

package-lock.json

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"@matrixai/resources": "^1.1.5",
7171
"@matrixai/timer": "^1.1.1",
7272
"@matrixai/workers": "^1.3.7",
73-
"@matrixai/quic": "^0.0.13",
73+
"@matrixai/quic": "^0.0.14",
7474
"@peculiar/asn1-pkcs8": "^2.3.0",
7575
"@peculiar/asn1-schema": "^2.3.0",
7676
"@peculiar/asn1-x509": "^2.3.0",

src/agent/handlers/clientManifest.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import type {
1212
VaultsGitPackGetMessage,
1313
VaultsScanMessage,
1414
} from './types';
15-
import { DuplexCaller, ServerCaller, UnaryCaller } from '../../rpc/callers';
15+
import {
16+
DuplexCaller,
17+
RawCaller,
18+
ServerCaller,
19+
UnaryCaller,
20+
} from '../../rpc/callers';
1621

1722
const nodesClaimsGet = new ServerCaller<
1823
AgentRPCRequestParams<ClaimIdMessage>,
@@ -39,10 +44,7 @@ const notificationsSend = new UnaryCaller<
3944
AgentRPCResponseResult
4045
>();
4146

42-
const vaultsGitInfoGet = new ServerCaller<
43-
AgentRPCRequestParams<VaultsGitInfoGetMessage>,
44-
AgentRPCResponseResult<VaultInfo | GitPackMessage>
45-
>();
47+
const vaultsGitInfoGet = new RawCaller();
4648

4749
const vaultsGitPackGet = new ServerCaller<
4850
AgentRPCRequestParams<VaultsGitPackGetMessage>,

src/agent/handlers/serverManifest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ const serverManifest = (container: {
4141
nodesCrossSignClaim: new NodesCrossSignClaimHandler(container),
4242
nodesHolePunchMessageSend: new NodesHolePunchMessageSendHandler(container),
4343
notificationsSend: new NotificationsSendHandler(container),
44-
VaultsGitInfoGet: new VaultsGitInfoGetHandler(container),
45-
VaultsGitPackGet: new VaultsGitPackGetHandler(container),
44+
vaultsGitInfoGet: new VaultsGitInfoGetHandler(container),
45+
vaultsGitPackGet: new VaultsGitPackGetHandler(container),
4646
vaultsScan: new VaultsScanHandler(container),
4747
};
4848
};

src/agent/handlers/vaultsGitInfoGet.ts

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,56 @@ import type { ACL } from '../../acl';
66
import type Logger from '@matrixai/logger';
77
import type { VaultsGitInfoGetMessage } from './types';
88
import type { VaultAction } from '../../vaults/types';
9+
import type { JSONRPCRequest } from '@/rpc/types';
10+
import type { ContextTimed } from '@matrixai/contexts';
11+
import type { JSONValue } from '@/types';
12+
import { ReadableStream } from 'stream/web';
913
import * as agentErrors from '../errors';
1014
import * as vaultsUtils from '../../vaults/utils';
1115
import * as vaultsErrors from '../../vaults/errors';
12-
import { ServerHandler } from '../../rpc/handlers';
16+
import { RawHandler } from '../../rpc/handlers';
1317
import { validateSync } from '../../validation';
14-
import { matchSync } from '../../utils';
18+
import { matchSync, never } from '../../utils';
1519
import * as validationUtils from '../../validation/utils';
1620
import * as nodesUtils from '../../nodes/utils';
1721
import * as agentUtils from '../utils';
22+
import * as utils from '../../utils';
1823

19-
class VaultsGitInfoGetHandler extends ServerHandler<
20-
{
21-
db: DB;
22-
vaultManager: VaultManager;
23-
acl: ACL;
24-
logger: Logger;
25-
},
26-
AgentRPCRequestParams<VaultsGitInfoGetMessage>,
27-
AgentRPCResponseResult<VaultInfo | GitPackMessage>
28-
> {
29-
public async *handle(
30-
input: AgentRPCRequestParams<VaultsGitInfoGetMessage>,
31-
_cancel,
32-
meta,
33-
): AsyncGenerator<VaultInfo | GitPackMessage> {
24+
class VaultsGitInfoGetHandler extends RawHandler<{
25+
db: DB;
26+
vaultManager: VaultManager;
27+
acl: ACL;
28+
logger: Logger;
29+
}> {
30+
public async handle(
31+
input: [JSONRPCRequest, ReadableStream<Uint8Array>],
32+
cancel: (reason?: any) => void,
33+
meta: Record<string, JSONValue> | undefined,
34+
ctx: ContextTimed,
35+
): Promise<[JSONValue, ReadableStream<Uint8Array>]> {
3436
const { db, vaultManager, acl } = this.container;
35-
yield* db.withTransactionG(async function* (
36-
tran,
37-
): AsyncGenerator<VaultInfo | GitPackMessage> {
37+
const [headerMessage, inputStream] = input;
38+
const params = headerMessage.params;
39+
if (params == null || !utils.isObject(params)) never();
40+
if (
41+
!('vaultNameOrId' in params) ||
42+
typeof params.vaultNameOrId != 'string'
43+
) {
44+
never();
45+
}
46+
if (!('action' in params) || typeof params.action != 'string') never();
47+
const vaultNameOrId = params.vaultNameOrId;
48+
const actionType = validationUtils.parseVaultAction(params.action);
49+
const data = await db.withTransactionF(async (tran) => {
3850
const vaultIdFromName = await vaultManager.getVaultId(
39-
input.vaultNameOrId,
51+
vaultNameOrId,
4052
tran,
4153
);
4254
const vaultId =
43-
vaultIdFromName ?? vaultsUtils.decodeVaultId(input.vaultNameOrId);
55+
vaultIdFromName ?? vaultsUtils.decodeVaultId(vaultNameOrId);
4456
if (vaultId == null) {
4557
throw new vaultsErrors.ErrorVaultsVaultUndefined();
4658
}
47-
const {
48-
actionType,
49-
}: {
50-
actionType: VaultAction;
51-
} = validateSync(
52-
(keyPath, value) => {
53-
return matchSync(keyPath)(
54-
[['actionType'], () => validationUtils.parseVaultAction(value)],
55-
() => value,
56-
);
57-
},
58-
{
59-
actionType: input.action,
60-
},
61-
);
6259
const vaultName = (await vaultManager.getVaultMeta(vaultId, tran))
6360
?.vaultName;
6461
if (vaultName == null) {
@@ -85,20 +82,35 @@ class VaultsGitInfoGetHandler extends ServerHandler<
8582
);
8683
}
8784

88-
yield {
89-
vaultName: vaultName,
90-
vaultIdEncoded: vaultsUtils.encodeVaultId(vaultId),
85+
return {
86+
vaultId,
87+
vaultName,
9188
};
92-
for await (const byte of vaultManager.handleInfoRequest(vaultId, tran)) {
93-
if (byte !== null) {
94-
yield {
95-
chunk: byte.toString('binary'),
96-
};
97-
} else {
98-
return;
89+
});
90+
91+
// TODO: Needs to handle cancellation
92+
const stream = new ReadableStream({
93+
start: async (controller) => {
94+
for await (const buffer of vaultManager.handleInfoRequest(
95+
data.vaultId,
96+
)) {
97+
if (buffer != null) {
98+
controller.enqueue(buffer);
99+
} else {
100+
break;
101+
}
99102
}
100-
}
103+
controller.close();
104+
},
101105
});
106+
107+
return [
108+
{
109+
vaultName: data.vaultName,
110+
vaultIdEncoded: vaultsUtils.encodeVaultId(data.vaultId),
111+
},
112+
stream,
113+
];
102114
}
103115
}
104116

src/nodes/NodeConnection.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import * as rpcUtils from '../rpc/utils';
2424
import * as keysUtils from '../keys/utils';
2525
import * as nodesUtils from '../nodes/utils';
2626
import { never } from '../utils';
27+
import * as utils from '../utils';
2728

2829
/**
2930
* Encapsulates the unidirectional client-side connection of one node to another.
@@ -93,9 +94,9 @@ class NodeConnection<M extends ClientManifest> extends EventTarget {
9394
targetHostname,
9495
crypto,
9596
tlsConfig,
97+
manifest,
9698
quicConfig = {},
9799
quicSocket,
98-
manifest,
99100
logger = new Logger(this.name),
100101
}: {
101102
handleStream: (stream: RPCStream<Uint8Array, Uint8Array>) => void;
@@ -105,9 +106,9 @@ class NodeConnection<M extends ClientManifest> extends EventTarget {
105106
targetHostname?: Hostname;
106107
crypto: ClientCrypto;
107108
tlsConfig: TLSConfig;
109+
manifest: M;
108110
quicConfig?: QuicConfig;
109111
quicSocket?: QUICSocket;
110-
manifest: M;
111112
logger?: Logger;
112113
},
113114
@context ctx: ContextTimed,
@@ -140,6 +141,8 @@ class NodeConnection<M extends ClientManifest> extends EventTarget {
140141
crypto: {
141142
ops: crypto,
142143
},
144+
reasonToCode: utils.reasonToCode,
145+
codeToReason: utils.codeToReason,
143146
logger: logger.getChild(QUICClient.name),
144147
},
145148
ctx,

src/rpc/RPCServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ class RPCServer extends EventTarget {
568568
let handlerResult: [JSONValue | undefined, ReadableStream<Uint8Array>];
569569
const headerWriter = rpcStream.writable.getWriter();
570570
try {
571-
handlerResult = handler(
571+
handlerResult = await handler(
572572
[headerMessage.value, inputStream],
573573
rpcStream.cancel,
574574
rpcStream.meta,

src/rpc/handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ abstract class RawHandler<
2929
cancel: (reason?: any) => void,
3030
meta: Record<string, JSONValue> | undefined,
3131
ctx: ContextTimed,
32-
): [JSONValue, ReadableStream<Uint8Array>];
32+
): Promise<[JSONValue, ReadableStream<Uint8Array>]>;
3333
}
3434

3535
abstract class DuplexHandler<

src/utils/utils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,20 @@ function lexiUnpackBuffer(b: Buffer): number {
429429
return lexi.unpack([...b]);
430430
}
431431

432+
// TODO: remove this, quick hack to allow errors to jump the network
433+
const codeMap = new Map<number, any>();
434+
let code = 1;
435+
432436
const reasonToCode = (_type: 'recv' | 'send', _reason?: any): number => {
433-
return 0;
437+
codeMap.set(code, _reason);
438+
const returnCode = code;
439+
code++;
440+
return returnCode;
434441
};
435442

436443
const codeToReason = (type: 'recv' | 'send', code: number): any => {
437-
return Error(`${type} ${code}`);
444+
const asd = codeMap.get(code);
445+
return asd;
438446
};
439447

440448
export {

0 commit comments

Comments
 (0)