Skip to content

Commit

Permalink
add basic context caching
Browse files Browse the repository at this point in the history
  • Loading branch information
F-Node-Karlsruhe committed Nov 17, 2022
1 parent 5c4dbbb commit e423e44
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions api/src/services/documentLoader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import jsonldSignatures from 'jsonld-signatures';
import { getResolver } from './didresolver.js'

const BASE_CONTEXT_URL: string = 'https://ssi.eecc.de/api/registry/context'
const TRUSTED_CONTEXT_DOMAINS: [string] = ['https://ssi.eecc.de']

const cache = new Map();

const documentLoader: Promise<any> = jsonldSignatures.extendContextLoader(async (url: string) => {

Expand Down Expand Up @@ -42,18 +44,30 @@ const documentLoader: Promise<any> = jsonldSignatures.extendContextLoader(async
};
}

// Warn when context is fetched externally
// TODO create list of trusted sources
if (!url.startsWith(BASE_CONTEXT_URL)) console.warn(`Fetched @context from ${url}. Use with care!`)
let document = cache.get(url);

// fetch if not in cache
if (!document) {

document = await(await fetch(url)).json();

// cache and warn if external
if (!TRUSTED_CONTEXT_DOMAINS.some((trusted) => url.startsWith(trusted))) {

console.warn(`Fetched and cached @context from ${url}. Use with care!`);

const document = await fetch(url);
cache.set(url, document);

}

}

return {
contextUrl: null,
documentUrl: url,
document: await document.json(),
document: document,
};

});

export { documentLoader, BASE_CONTEXT_URL }
export { documentLoader }

0 comments on commit e423e44

Please sign in to comment.