How to use the jsonld.documentLoaders 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 gobengo / distbin / src / util.ts View on Github external
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();
  // or grab the XHR one: jsonldLib.documentLoaders.xhr()
  // or grab the jquery one: jsonldLib.documentLoaders.jquery()

  // change the default document loader using the callback API
  // (you can also do this using the promise-based API, return a promise instead
  // of using a callback)
  const customLoader = (someUrl: string, callback: Errback) => {
    if (someUrl in CONTEXTS) {
      return callback(null, {
        contextUrl: null, // this is for a context via a link header
        document: CONTEXTS[someUrl], // this is the actual document that was loaded
        documentUrl: someUrl, // this is the actual context URL after redirects
      });
    }
    // call the underlining documentLoader using the callback API.
    nodeDocumentLoader(someUrl, callback);
github OpenSocial / activitystreams.js / src / utils.js View on Github external
val = Math.max(0, Math.floor(val));
  this.set(key, val, {type: vocabs.xsd.nonNegativeInteger});
};

exports.set_duration_val = function(key, val) {
  throwif(!is_number(val) && !is_string(val), 'Duration must be a string or a number');
  if (is_number(val)) {
    set_non_negative_int.call(this, key, val);
  }
  else if (is_string(val)) {
    this.set(key, val);
  }
  return this;
};

var default_doc_loader = jsonld.documentLoaders.node();
var custom_doc_loader = function(url, callback) {
  var u = url;
  if (u[u.length-1] !== '#') u += '#';
  if (u === vocabs.as.ns)
    return callback(null, {
      contextUrl: null,
      document: as_context, 
      documentUrl: url
    });
  default_doc_loader(url, callback);
};

exports.jsonld = Object.create({
  default_doc_loader: jsonld.documentLoaders.node(), // TODO: get proper document loader...,
  custom_doc_loader: function(url, callback) {
    var u = url;
github decentralized-identity / github-did / packages / lib / src / v2 / func / wrappedDocumentLoader.js View on Github external
const jsonld = require("jsonld");
// const resolver = require("./resolver");

const _nodejs =
  // tslint:disable-next-line
  typeof process !== "undefined" && process.versions && process.versions.node;
const _browser =
  // tslint:disable-next-line
  !_nodejs && (typeof window !== "undefined" || typeof self !== "undefined");

const documentLoader = _browser
  ? jsonld.documentLoaders.xhr()
  : jsonld.documentLoaders.node();

const wrappedDocumentLoader = args => {
  return async url => {
    // TODO: handle DIDs...

    if (url.startsWith("https://w3id.org/did/v1")) {
      return documentLoader(
        "https://raw.githubusercontent.com/w3c/did-core/master/contexts/did-v0.11.jsonld"
      );
    }

    if (url.startsWith("https://w3id.org/identity/v1")) {
      return documentLoader(
        "https://web-payments.org/contexts/identity-v1.jsonld"
      );
    }
github poetapp / poet-js / src / util / DataDocumentLoader.ts View on Github external
/* tslint:disable:no-relative-imports */
import * as JSONLD from 'jsonld'
import * as ParseDataUrl from 'parse-data-url'

import { getSigningAlgorithm } from '../Interfaces'
import { SupportedAlgorithms } from './KeyHelper'

const nodeDocumentLoader = JSONLD.documentLoaders.node({ usePromise: true })

const fromBase64 = (base64String: string): { algorithm: string; publicKey: string } =>
  JSON.parse(Buffer.from(base64String, 'base64').toString())

export const dataDocumentLoader = async (url: string, callback: (error: any, data: any) => any) => {
  const parsedData = ParseDataUrl(url)

  if (parsedData) {
    const extractedParsedData = parsedData.base64 ? fromBase64(parsedData.data) : parsedData.data
    const publicKey = SupportedAlgorithms[getSigningAlgorithm(extractedParsedData.algorithm)].publicKey(
      url,
      extractedParsedData.publicKey,
    )
    return callback(null, {
      contextUrl: ['https://w3id.org/security/v2'],
      document: {
github decentralized-identity / github-did / packages / lib / src / v2 / func / wrappedDocumentLoader.js View on Github external
const jsonld = require("jsonld");
// const resolver = require("./resolver");

const _nodejs =
  // tslint:disable-next-line
  typeof process !== "undefined" && process.versions && process.versions.node;
const _browser =
  // tslint:disable-next-line
  !_nodejs && (typeof window !== "undefined" || typeof self !== "undefined");

const documentLoader = _browser
  ? jsonld.documentLoaders.xhr()
  : jsonld.documentLoaders.node();

const wrappedDocumentLoader = args => {
  return async url => {
    // TODO: handle DIDs...

    if (url.startsWith("https://w3id.org/did/v1")) {
      return documentLoader(
        "https://raw.githubusercontent.com/w3c/did-core/master/contexts/did-v0.11.jsonld"
      );
    }

    if (url.startsWith("https://w3id.org/identity/v1")) {
      return documentLoader(
        "https://web-payments.org/contexts/identity-v1.jsonld"
      );
github OpenSocial / activitystreams.js / src / utils.js View on Github external
var default_doc_loader = jsonld.documentLoaders.node();
var custom_doc_loader = function(url, callback) {
  var u = url;
  if (u[u.length-1] !== '#') u += '#';
  if (u === vocabs.as.ns)
    return callback(null, {
      contextUrl: null,
      document: as_context, 
      documentUrl: url
    });
  default_doc_loader(url, callback);
};

exports.jsonld = Object.create({
  default_doc_loader: jsonld.documentLoaders.node(), // TODO: get proper document loader...,
  custom_doc_loader: function(url, callback) {
    var u = url;
    if (u[u.length-1] !== '#') u += '#';
    if (u === vocabs.as.ns) {
      return callback(null, {
        contextUrl: null,
        document: as_context, 
        documentUrl: url
      });
    }
    exports.jsonld.default_doc_loader(url, callback);
  },
  compact: function(ret, callback) {
    exports.throwif(typeof callback !== 'function', 'A callback function must be specified');
    jsonld.compact(
      ret, {'@context': vocabs.as.ns},
github TangleID / TangleID / packages / core / src / jsonld / documentLoader.ts View on Github external
import { IdenityRegistry, decodeFromDidUrl, isDidUrl } from '@tangleid/did';
import { findNodeById } from '@tangleid/jsonld';
// @ts-ignore
import * as jsonld from 'jsonld';
// @ts-ignore
const nodeDocumentLoader = jsonld.documentLoaders.node();

type CachedDocuments = {
  [index: string]: any;
};

export class DocumentLoader {
  registry: IdenityRegistry;
  documents: CachedDocuments = {};

  constructor(registry: IdenityRegistry) {
    this.registry = registry;
  }

  getDocuments() {
    return this.documents;
  }