MCPcopy
hub / github.com/tiagozip/cap / capCheckpoint

Function capCheckpoint

checkpoints/express/index.js:7–159  ·  view source on GitHub ↗
(userOptions)

Source from the content-addressed store, hash-verified

5import Cap from "@cap.js/server";
6
7export const capCheckpoint = (userOptions) => {
8 const options = {
9 token_validity_hours: 32,
10 tokens_store_path: ".data/middlewareTokens.json",
11 token_size: 16,
12 verification_template_path: join(
13 dirname(fileURLToPath(import.meta.url)),
14 "./index.html",
15 ),
16 ...userOptions,
17 };
18
19 const cap = new Cap({
20 noFSState: true,
21 });
22
23 let tokensCache = null;
24 let cacheLastModified = 0;
25
26 fs.mkdir(dirname(options.tokens_store_path), { recursive: true });
27
28 async function loadCustomTokens() {
29 try {
30 const stats = await fs.stat(options.tokens_store_path);
31
32 if (tokensCache && stats.mtime.getTime() <= cacheLastModified) {
33 return tokensCache;
34 }
35
36 tokensCache = JSON.parse(
37 await fs.readFile(options.tokens_store_path, "utf-8"),
38 );
39 cacheLastModified = Date.now();
40
41 return tokensCache;
42 } catch {
43 tokensCache = {};
44 cacheLastModified = Date.now();
45
46 return tokensCache;
47 }
48 }
49
50 async function saveCustomTokens(tokens) {
51 await fs.writeFile(options.tokens_store_path, JSON.stringify(tokens));
52 tokensCache = tokens;
53 cacheLastModified = Date.now();
54 }
55
56 async function storeCustomToken(token) {
57 const tokens = await loadCustomTokens();
58
59 tokens[token] = Date.now() + options.token_validity_hours * 60 * 60 * 1000;
60 await saveCustomTokens(tokens);
61 return token;
62 }
63
64 async function validateCustomToken(token) {

Callers

nothing calls this directly

Calls 7

redeemChallengeMethod · 0.95
validateTokenMethod · 0.95
createChallengeMethod · 0.95
storeCustomTokenFunction · 0.70
cleanupExpiredTokensFunction · 0.70
validateCustomTokenFunction · 0.70
nextFunction · 0.50

Tested by

no test coverage detected