How to use the jsonld.documentLoader function in jsonld

To help you get started, we’ve selected a few jsonld examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github digitalbazaar / vc-js / tests / 10-verify.spec.js View on Github external
async function _pluckDidNode(did, target, didDocument) {
  // flatten to isolate target
  jsonld.documentLoader = testLoader.documentLoader.bind(testLoader);
  const flattened = await jsonld.flatten(didDocument);
  // filter out non-DID nodes and find target
  let found = false;
  const filtered = [];
  for(const node of flattened) {
    const id = node['@id'];
    if(id === target) {
      filtered.push(node);
      found = true;
      break;
    }
  }
  // target not found
  if(!found) {
    const err = new Error('Not Found');
    err.httpStatusCode = 404;
github gobengo / distbin / src / util.ts View on Github external
}
  return createRequest(urlOrObj);
};

// given a Link object or url string, return an href string that can be used to refer to it
export const linkToHref = (hrefOrLinkObj: ASLink | string) => {
  if (typeof hrefOrLinkObj === "string") {
    return hrefOrLinkObj;
  }
  if (typeof hrefOrLinkObj === "object") {
    return hrefOrLinkObj.href;
  }
  throw new Error("Unexpected link type: " + typeof hrefOrLinkObj);
};

jsonldLib.documentLoader = createCustomDocumentLoader();

export const jsonld = jsonldLib.promises;

type Errback = (err: Error, ...args: any[]) => void;

function createCustomDocumentLoader() {
  // define a mapping of context URL => context doc
  const CONTEXTS: { [key: string]: string } = {
    "https://www.w3.org/ns/activitystreams": fs.readFileSync(
      path.join(__dirname, "/as2context.json"),
      "utf8",
    ),
  };

  // grab the built-in node.js doc loader
  const nodeDocumentLoader = jsonldLib.documentLoaders.node();