]>
Commit | Line | Data |
---|---|---|
9a27cdc2 | 1 | import * as AsyncLRU from 'async-lru' |
df66d815 | 2 | import * as jsonld from 'jsonld' |
1eddc9a7 C |
3 | import { logger } from './logger' |
4 | ||
5 | const CACHE = { | |
6 | 'https://w3id.org/security/v1': { | |
7 | '@context': { | |
a1587156 C |
8 | id: '@id', |
9 | type: '@type', | |
1eddc9a7 | 10 | |
a1587156 C |
11 | dc: 'http://purl.org/dc/terms/', |
12 | sec: 'https://w3id.org/security#', | |
13 | xsd: 'http://www.w3.org/2001/XMLSchema#', | |
1eddc9a7 | 14 | |
a1587156 C |
15 | EcdsaKoblitzSignature2016: 'sec:EcdsaKoblitzSignature2016', |
16 | Ed25519Signature2018: 'sec:Ed25519Signature2018', | |
17 | EncryptedMessage: 'sec:EncryptedMessage', | |
18 | GraphSignature2012: 'sec:GraphSignature2012', | |
19 | LinkedDataSignature2015: 'sec:LinkedDataSignature2015', | |
20 | LinkedDataSignature2016: 'sec:LinkedDataSignature2016', | |
21 | CryptographicKey: 'sec:Key', | |
1eddc9a7 | 22 | |
a1587156 C |
23 | authenticationTag: 'sec:authenticationTag', |
24 | canonicalizationAlgorithm: 'sec:canonicalizationAlgorithm', | |
25 | cipherAlgorithm: 'sec:cipherAlgorithm', | |
26 | cipherData: 'sec:cipherData', | |
27 | cipherKey: 'sec:cipherKey', | |
28 | created: { '@id': 'dc:created', '@type': 'xsd:dateTime' }, | |
29 | creator: { '@id': 'dc:creator', '@type': '@id' }, | |
30 | digestAlgorithm: 'sec:digestAlgorithm', | |
31 | digestValue: 'sec:digestValue', | |
32 | domain: 'sec:domain', | |
33 | encryptionKey: 'sec:encryptionKey', | |
34 | expiration: { '@id': 'sec:expiration', '@type': 'xsd:dateTime' }, | |
35 | expires: { '@id': 'sec:expiration', '@type': 'xsd:dateTime' }, | |
36 | initializationVector: 'sec:initializationVector', | |
37 | iterationCount: 'sec:iterationCount', | |
38 | nonce: 'sec:nonce', | |
39 | normalizationAlgorithm: 'sec:normalizationAlgorithm', | |
40 | owner: { '@id': 'sec:owner', '@type': '@id' }, | |
41 | password: 'sec:password', | |
42 | privateKey: { '@id': 'sec:privateKey', '@type': '@id' }, | |
43 | privateKeyPem: 'sec:privateKeyPem', | |
44 | publicKey: { '@id': 'sec:publicKey', '@type': '@id' }, | |
45 | publicKeyBase58: 'sec:publicKeyBase58', | |
46 | publicKeyPem: 'sec:publicKeyPem', | |
47 | publicKeyWif: 'sec:publicKeyWif', | |
48 | publicKeyService: { '@id': 'sec:publicKeyService', '@type': '@id' }, | |
49 | revoked: { '@id': 'sec:revoked', '@type': 'xsd:dateTime' }, | |
50 | salt: 'sec:salt', | |
51 | signature: 'sec:signature', | |
52 | signatureAlgorithm: 'sec:signingAlgorithm', | |
53 | signatureValue: 'sec:signatureValue' | |
1eddc9a7 C |
54 | } |
55 | } | |
56 | } | |
9a27cdc2 | 57 | |
9a27cdc2 C |
58 | const nodeDocumentLoader = jsonld.documentLoaders.node() |
59 | ||
60 | const lru = new AsyncLRU({ | |
61 | max: 10, | |
1eddc9a7 | 62 | load: (url, cb) => { |
a1587156 | 63 | if (CACHE[url] !== undefined) { |
1eddc9a7 C |
64 | logger.debug('Using cache for JSON-LD %s.', url) |
65 | ||
66 | return cb(null, { | |
67 | contextUrl: null, | |
a1587156 | 68 | document: CACHE[url], |
1eddc9a7 C |
69 | documentUrl: url |
70 | }) | |
71 | } | |
72 | ||
e9226905 C |
73 | nodeDocumentLoader(url) |
74 | .then(value => cb(null, value)) | |
75 | .catch(err => cb(err)) | |
9a27cdc2 C |
76 | } |
77 | }) | |
78 | ||
e9226905 C |
79 | jsonld.documentLoader = (url) => { |
80 | return new Promise((res, rej) => { | |
81 | lru.get(url, (err, value) => { | |
82 | if (err) return rej(err) | |
83 | ||
84 | return res(value) | |
85 | }) | |
86 | }) | |
9a27cdc2 C |
87 | } |
88 | ||
ad513607 | 89 | export { jsonld } |