forked from OKEAMAH/js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.ts
More file actions
30 lines (26 loc) · 1.13 KB
/
cache.ts
File metadata and controls
30 lines (26 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { DocumentLoader, Options, getDocumentLoader } from '@iden3/js-jsonld-merklization';
import { RemoteDocument, Url } from 'jsonld/jsonld-spec';
import { VerifiableConstants } from '../../verifiable';
/**
* cacheLoader returns a remote document with additional logic for caching the urls remote documents.
* If the same url is called more then once, remote document will be not downloaded again but will returned from memory cache.
* @param {Options } context - JSONLD loader options
* @returns Promise<DocumentLoader>
*/
const doc = JSON.parse(VerifiableConstants.JSONLD_SCHEMA.W3C_VC_DOCUMENT_2018);
export const cacheLoader = (opts?: Options): DocumentLoader => {
const cache = new Map<string, RemoteDocument>();
cache.set(VerifiableConstants.JSONLD_SCHEMA.W3C_CREDENTIAL_2018, {
document: doc,
documentUrl: VerifiableConstants.JSONLD_SCHEMA.W3C_CREDENTIAL_2018
});
return async (url: Url): Promise<RemoteDocument> => {
let remoteDoc = cache.get(url);
if (remoteDoc) {
return remoteDoc;
}
remoteDoc = await getDocumentLoader(opts)(url);
cache.set(url, remoteDoc);
return remoteDoc;
};
};