2023-07-31 08:34:36 -04:00
|
|
|
import jsonld from 'jsonld'
|
2021-08-27 08:32:44 -04:00
|
|
|
|
2024-04-25 05:21:55 -04:00
|
|
|
const STATIC_CACHE = {
|
2019-04-15 04:49:46 -04:00
|
|
|
'https://w3id.org/security/v1': {
|
|
|
|
'@context': {
|
2020-01-31 10:56:52 -05:00
|
|
|
id: '@id',
|
|
|
|
type: '@type',
|
2019-04-15 04:49:46 -04:00
|
|
|
|
2020-01-31 10:56:52 -05:00
|
|
|
dc: 'http://purl.org/dc/terms/',
|
|
|
|
sec: 'https://w3id.org/security#',
|
|
|
|
xsd: 'http://www.w3.org/2001/XMLSchema#',
|
2019-04-15 04:49:46 -04:00
|
|
|
|
2020-01-31 10:56:52 -05:00
|
|
|
EcdsaKoblitzSignature2016: 'sec:EcdsaKoblitzSignature2016',
|
|
|
|
Ed25519Signature2018: 'sec:Ed25519Signature2018',
|
|
|
|
EncryptedMessage: 'sec:EncryptedMessage',
|
|
|
|
GraphSignature2012: 'sec:GraphSignature2012',
|
|
|
|
LinkedDataSignature2015: 'sec:LinkedDataSignature2015',
|
|
|
|
LinkedDataSignature2016: 'sec:LinkedDataSignature2016',
|
|
|
|
CryptographicKey: 'sec:Key',
|
2019-04-15 04:49:46 -04:00
|
|
|
|
2020-01-31 10:56:52 -05:00
|
|
|
authenticationTag: 'sec:authenticationTag',
|
|
|
|
canonicalizationAlgorithm: 'sec:canonicalizationAlgorithm',
|
|
|
|
cipherAlgorithm: 'sec:cipherAlgorithm',
|
|
|
|
cipherData: 'sec:cipherData',
|
|
|
|
cipherKey: 'sec:cipherKey',
|
|
|
|
created: { '@id': 'dc:created', '@type': 'xsd:dateTime' },
|
|
|
|
creator: { '@id': 'dc:creator', '@type': '@id' },
|
|
|
|
digestAlgorithm: 'sec:digestAlgorithm',
|
|
|
|
digestValue: 'sec:digestValue',
|
|
|
|
domain: 'sec:domain',
|
|
|
|
encryptionKey: 'sec:encryptionKey',
|
|
|
|
expiration: { '@id': 'sec:expiration', '@type': 'xsd:dateTime' },
|
|
|
|
expires: { '@id': 'sec:expiration', '@type': 'xsd:dateTime' },
|
|
|
|
initializationVector: 'sec:initializationVector',
|
|
|
|
iterationCount: 'sec:iterationCount',
|
|
|
|
nonce: 'sec:nonce',
|
|
|
|
normalizationAlgorithm: 'sec:normalizationAlgorithm',
|
|
|
|
owner: { '@id': 'sec:owner', '@type': '@id' },
|
|
|
|
password: 'sec:password',
|
|
|
|
privateKey: { '@id': 'sec:privateKey', '@type': '@id' },
|
|
|
|
privateKeyPem: 'sec:privateKeyPem',
|
|
|
|
publicKey: { '@id': 'sec:publicKey', '@type': '@id' },
|
|
|
|
publicKeyBase58: 'sec:publicKeyBase58',
|
|
|
|
publicKeyPem: 'sec:publicKeyPem',
|
|
|
|
publicKeyWif: 'sec:publicKeyWif',
|
|
|
|
publicKeyService: { '@id': 'sec:publicKeyService', '@type': '@id' },
|
|
|
|
revoked: { '@id': 'sec:revoked', '@type': 'xsd:dateTime' },
|
|
|
|
salt: 'sec:salt',
|
|
|
|
signature: 'sec:signature',
|
|
|
|
signatureAlgorithm: 'sec:signingAlgorithm',
|
|
|
|
signatureValue: 'sec:signatureValue'
|
2019-04-15 04:49:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 09:20:42 -05:00
|
|
|
|
2024-04-25 05:21:55 -04:00
|
|
|
const localCache = new Map<string, any>()
|
|
|
|
|
2023-11-28 11:35:28 -05:00
|
|
|
const nodeDocumentLoader = (jsonld as any).documentLoaders.node();
|
2017-11-17 09:20:42 -05:00
|
|
|
|
2021-02-03 03:33:05 -05:00
|
|
|
/* eslint-disable no-import-assign */
|
2024-04-25 05:21:55 -04:00
|
|
|
(jsonld as any).documentLoader = async (url: string) => {
|
|
|
|
if (url in STATIC_CACHE) {
|
|
|
|
return {
|
2023-11-28 11:35:28 -05:00
|
|
|
contextUrl: null,
|
2024-04-25 05:21:55 -04:00
|
|
|
document: STATIC_CACHE[url],
|
2023-11-28 11:35:28 -05:00
|
|
|
documentUrl: url
|
2024-04-25 05:21:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (localCache.has(url)) return localCache.get(url)
|
|
|
|
|
|
|
|
const remoteDoc = await nodeDocumentLoader(url)
|
|
|
|
|
|
|
|
if (localCache.size < 100) {
|
|
|
|
localCache.set(url, remoteDoc)
|
2023-11-28 11:35:28 -05:00
|
|
|
}
|
|
|
|
|
2024-04-25 05:21:55 -04:00
|
|
|
return remoteDoc
|
2017-11-17 09:20:42 -05:00
|
|
|
}
|
|
|
|
|
2019-08-29 10:15:41 -04:00
|
|
|
export { jsonld }
|