File: wrappers.js

package info (click to toggle)
r-cran-jsonld 2.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 892 kB
  • sloc: javascript: 12,209; sh: 19; makefile: 13
file content (84 lines) | stat: -rw-r--r-- 2,539 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//Fix missing polyfill in jsonld 1.0.0
String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};

function setTimeout(fun){
  fun();
}

/* V8 does not have network support, so we call back to R */
jsonld.documentLoaders.curl = function(options){
  LINK_HEADER_REL = 'http://www.w3.org/ns/json-ld#context';
  function loader(url, callback){
    var res = console.r.call('jsonld:::download', {url : url});
    var linkHeader = res.link;
    var contentType = res.content_type;
    var target = null;

    // copied from 'jsonld.documentLoaders.jquery'
    if(linkHeader && linkHeader.length && contentType !== 'application/ld+json') {
      // First check for alternate location: https://www.w3.org/TR/json-ld11/#alternate-document-location
      var linkdata = jsonld.parseLinkHeader(linkHeader);
      if(linkdata && linkdata.alternate && linkdata.alternate.target){
        var newurl = res.final_url + linkdata.alternate.target;
        return loader(newurl, callback);
      }

      // Check for jsonld link: https://www.w3.org/TR/json-ld11/#interpreting-json-as-json-ld
      var link_rel = linkdata[LINK_HEADER_REL];
      if(Array.isArray(link_rel)) {
        throw new JsonLdError(
          'URL could not be dereferenced, it has more than one ' +
          'associated HTTP Link Header.',
          'jsonld.InvalidUrl',
          {code: 'multiple context link headers', url: url});
      }
      if(link_rel) {
        target = link_rel.target;
      }
    }

    var doc = {contextUrl: target, documentUrl: res.final_url, document: JSON.parse(res.response_text)};
    callback(null, doc);
  }
  return loader;
};

jsonld.useDocumentLoader('curl');

function store_value(err, val){
  console.r.call('jsonld:::store_val', {set: {err : err, val : val}});
}

function store_json(err, val){
  store_value(err, JSON.stringify(val, null, 2));
}

function r_compact(doc, context, options){
  jsonld.compact(doc, context, options, store_json);
}

function r_expand(compacted, options){
  jsonld.expand(compacted, options, store_json);
}

function r_flatten(doc, context, options){
  jsonld.flatten(doc, context, options, store_json);
}

function r_frame(doc, frame, options){
  jsonld.frame(doc, frame, options, store_json);
}

function r_normalize(doc, options){
  jsonld.normalize(doc, options, store_value);
}

function r_to_rdf(doc, options){
  jsonld.toRDF(doc, options, store_value);
}

function r_from_rdf(text, options){
  jsonld.fromRDF(text, options, store_json);
}