Skip to content

Commit f0bb0f3

Browse files
committed
[cli] cache http agents; add MOPS_VERIFY_QUERY_SIGNATURES env support
1 parent 0323aa8 commit f0bb0f3

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

cli/api/actors.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,42 @@ import {_SERVICE as _STORAGE_SERVICE} from '../declarations/storage/storage.did.
88
import {getEndpoint} from './network.js';
99
import {getNetwork} from './network.js';
1010

11-
export let mainActor = async (identity ?: Identity) : Promise<_SERVICE> => {
12-
let network = getNetwork();
13-
let host = getEndpoint(network).host;
14-
let canisterId = getEndpoint(network).canisterId;
11+
let agentPromiseByPrincipal = new Map <string, Promise<HttpAgent>>();
12+
13+
let getAgent = async (identity ?: Identity) : Promise<HttpAgent> => {
14+
let principal = identity ? identity?.getPrincipal().toText() : '';
15+
let agentPromise = agentPromiseByPrincipal.get(principal);
1516

16-
// @ts-ignore exactOptionalPropertyTypes
17-
let agent = new HttpAgent({host, identity});
17+
if (!agentPromise) {
18+
let network = getNetwork();
19+
let host = getEndpoint(network).host;
1820

19-
if (network === 'local') {
20-
await agent.fetchRootKey();
21+
agentPromise = HttpAgent.create({
22+
host,
23+
identity,
24+
shouldFetchRootKey: network === 'local',
25+
verifyQuerySignatures: process.env.MOPS_VERIFY_QUERY_SIGNATURES !== 'false',
26+
});
27+
28+
agentPromiseByPrincipal.set(principal, agentPromise);
2129
}
2230

31+
return agentPromise;
32+
};
33+
34+
export let mainActor = async (identity ?: Identity) : Promise<_SERVICE> => {
35+
let agent = await getAgent(identity);
36+
let network = getNetwork();
37+
let canisterId = getEndpoint(network).canisterId;
38+
2339
return Actor.createActor(idlFactory, {
2440
agent,
2541
canisterId,
2642
});
2743
};
2844

2945
export let storageActor = async (storageId : Principal, identity ?: Identity) : Promise<_STORAGE_SERVICE> => {
30-
let network = getNetwork();
31-
let host = getEndpoint(network).host;
32-
33-
// @ts-ignore exactOptionalPropertyTypes
34-
let agent = new HttpAgent({host, identity});
35-
36-
if (network === 'local') {
37-
await agent.fetchRootKey();
38-
}
46+
let agent = await getAgent(identity);
3947

4048
return Actor.createActor(storageIdlFactory, {
4149
agent,

0 commit comments

Comments
 (0)