-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
61 lines (59 loc) · 1.72 KB
/
index.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const jsonld = require('jsonld');
const fsPromises = require('fs').promises;
const LRU = require('lru-cache');
const defaultDocumentLoader = jsonld.documentLoaders.node();
const cache = new LRU({ max: 500 });
module.exports = {
name: 'jsonld.document-loader',
settings: {
cachedContextFiles: [],
localContextUri: null
},
async started() {
for (const contextFile of this.settings.cachedContextFiles) {
const contextFileContent = await fsPromises.readFile(contextFile.file);
const contextJson = JSON.parse(contextFileContent);
cache.set(contextFile.uri, {
contextUrl: null,
documentUrl: contextFile.uri,
document: contextJson
});
}
},
actions: {
async loadWithCache(ctx) {
const { url, options } = ctx.params;
if (url === this.settings.localContextUri) {
// For local context, get it directly as it is frequently updated
// We will use the Redis cache to avoid compiling it every time
return {
contextUrl: null,
documentUrl: url,
document: await ctx.call('jsonld.context.getLocal')
};
}
if (cache.has(url)) {
return cache.get(url);
}
const context = await defaultDocumentLoader(url, options);
if (typeof context.document === 'string') {
context.document = JSON.parse(context.document);
}
cache.set(url, context);
return context;
},
getCache(ctx) {
const { uri } = ctx.params;
const context = cache.get(uri);
return context?.document;
},
setCache(ctx) {
const { uri, json } = ctx.params;
cache.set(uri, {
contextUrl: null,
documentUrl: uri,
document: json
});
}
}
};