-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add contexts, testLoader, & documentLoader to separate files.
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/*! | ||
* Copyright (c) 2019-2023 Digital Bazaar, Inc. All rights reserved. | ||
*/ | ||
import * as vc from '../lib/index.js'; | ||
import { | ||
invalidContexts, | ||
validContexts | ||
} from './contexts/index.js'; | ||
import jsigs from 'jsonld-signatures'; | ||
import jsonld from 'jsonld'; | ||
import {MultiLoader} from './MultiLoader.js'; | ||
|
||
export const contexts = new Map(); | ||
|
||
// add the invalid contexts to the loader | ||
for(const key in invalidContexts) { | ||
const {url, value} = invalidContexts[key]; | ||
contexts.set(url, value); | ||
} | ||
// add the valid contexts to the loader | ||
for(const key in validContexts) { | ||
const {url, value} = validContexts[key]; | ||
contexts.set(url, value); | ||
} | ||
const {extendContextLoader} = jsigs; | ||
const {defaultDocumentLoader} = vc; | ||
|
||
const testContextLoader = extendContextLoader(async url => { | ||
const context = contexts.get(url); | ||
if(context) { | ||
return { | ||
contextUrl: null, | ||
document: jsonld.clone(context), | ||
documentUrl: url | ||
}; | ||
} | ||
return defaultDocumentLoader(url); | ||
}); | ||
|
||
// documents are added to this documentLoader incrementally | ||
export const testLoader = new MultiLoader({ | ||
documentLoader: [ | ||
testContextLoader | ||
] | ||
}); | ||
export const documentLoader = testLoader.documentLoader.bind(testLoader); | ||
|