vcharpenay / uRDF.js

JavaScript implementation of an RDF store based on the JSON-LD format
MIT License
7 stars 2 forks source link

Error when trying to dereference relative URIs in jsonld #21

Open derwehr opened 3 years ago

derwehr commented 3 years ago

Expected Behavior

The jsonld is loaded into the urdf store and then queried

Current Behavior

Upon loading urdf reports the following error and stops:

(node:38682) UnhandledPromiseRejectionWarning: jsonld.InvalidUrl: Dereferencing a URL did not result in a valid JSON-LD object. Possible causes are an inaccessible URL perhaps due to a same-origin policy (ensure the server uses CORS if you are using client-side JavaScript), too many redirects, a non-JSON response, or more than one HTTP Link Header was provided for a remote context.
    at Promise.all.queue.map (/home/wehr/workspace/uRDF.js/node_modules/jsonld/lib/context.js:1000:15)
    at process._tickCallback (internal/process/next_tick.js:68:7)

Steps to Reproduce

1. Create data and context files

Create a file (called data.jsonld in this example) with the following content:

{"@context":"./context.jsonld","id":"","type":"History"}

and a file (called context.jsonld in this example) with the content:

{
    "@context": {
        "id": "@id",
        "type": "@type"
    }
}

2. Host the files locally

Host the 2 files from 1. locally. For example with apache

3. Create a query file

create a file (called query.sparqle in this example) with the following content:

PREFIX sosa: <http://www.w3.org/ns/sosa/>

SELECT ?s
FROM <http://localhost/data.jsonld>
WHERE 
{
    ?obs sosa:madeBySensor ?s .
}

Execute the query with urdf

From within the urdf directory, run the following command to execute the query: npm run urdf query query.sparql

Context (Environment)

We are trying to use urdf to query the jsonld returned by the sc-ble-adapter. A simliar issue has been resolved in the urdf browser (https://github.com/kianschmalenbach/rdf-browser/issues/11)

aharth commented 3 years ago

Any news?

vcharpenay commented 3 years ago

I get the error if my local server doesn't set Content-Type: application/ld+json.

When I add that header, URIs are properly resolved.

aharth commented 3 years ago

Hm, then something strange is going on.

$ npm run urdf query query.rq 

> urdf@0.1.0 urdf
> node src/cli.js "query" "query.rq"
(node:5754) UnhandledPromiseRejectionWarning: jsonld.InvalidUrl: Dereferencing a URL did not result in a valid JSON-LD object. Possible causes are an inaccessible URL perhaps due to a same-origin policy (ensure the server uses CORS if you are using client-side JavaScript), too many redirects, a non-JSON response, or more than one HTTP Link Header was provided for a remote context.
    at /tmp/uRDF.js/node_modules/jsonld/lib/context.js:1000:15
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:5754) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:5754) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
$
$ curl -v "http://localhost/data.jsonld" > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying ::1:80...
* Connected to localhost (::1) port 80 (#0)
> GET /data.jsonld HTTP/1.1
> Host: localhost
> User-Agent: curl/7.74.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Wed, 24 Feb 2021 22:36:53 GMT
< Server: Apache/2.4.46 (Debian)
< Last-Modified: Wed, 24 Feb 2021 22:35:07 GMT
< ETag: "38-5bc1ca59efe88"
< Accept-Ranges: bytes
< Content-Length: 56
< Access-Control-Allow-Origin: *
< Content-Type: application/ld+json
< 
{ [56 bytes data]
100    56  100    56    0     0  28000      0 --:--:-- --:--:-- --:--:-- 28000
* Connection #0 to host localhost left intact
$ curl -v "http://localhost/context.jsonld" > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying ::1:80...
* Connected to localhost (::1) port 80 (#0)
> GET /context.jsonld HTTP/1.1
> Host: localhost
> User-Agent: curl/7.74.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Wed, 24 Feb 2021 22:37:08 GMT
< Server: Apache/2.4.46 (Debian)
< Last-Modified: Wed, 24 Feb 2021 22:35:17 GMT
< ETag: "48-5bc1ca632b3e4"
< Accept-Ranges: bytes
< Content-Length: 72
< Access-Control-Allow-Origin: *
< Content-Type: application/ld+json
< 
{ [72 bytes data]
100    72  100    72    0     0  36000      0 --:--:-- --:--:-- --:--:-- 72000
* Connection #0 to host localhost left intact
$ 

When I change data.jsonld to (a context with an absolute URI)

{"@context":"http://localhost/context.jsonld","id":"","type":"History"}

it seems to work

$ npm run urdf query query.rq 

> urdf@0.1.0 urdf
> node src/cli.js "query" "query.rq"

No result.
$ 

We would like to use a source which puts relative URIs to context files.

vcharpenay commented 3 years ago

Alright. Can you make these files publicly available so that I can reproduce the error on the same resources as you?

aharth commented 3 years ago

http://harth.org/andreas/2021/urdf/

Any query with FROM http://harth.org/andreas/2021/urdf/index.jsonld

aharth commented 3 years ago

Quick question: we have to prepare a demo for next Wednesday... should we set up an elaborate workaround?

vcharpenay commented 3 years ago

I overlooked your last comment, sorry. 94d482b fixes the issue.

derwehr commented 3 years ago

Thank you, I can confirm this bug is fixed for the query command. However when using loadFrom I still can't process the jsonld from http://harth.org/andreas/2021/urdf/index.jsonld.

I tried to do this executing the following file via node (node loadFrom.js):

const urdf = require('./src/urdf-module.js');

// urdf.query with SELECT FROM - this is working
// const querySelectFrom = 'SELECT * FROM <http://harth.org/andreas/2021/urdf/index.jsonld> WHERE { ?s ?p ?o . }';
// urdf.query(querySelectFrom).then(console.log);

urdf.loadFrom('http://harth.org/andreas/2021/urdf/index.jsonld')
    .then(() => {
        console.log(urdf.size()); // 18
        urdf.query('SELECT * WHERE { ?s ?p ?o . }').then(console.log) // []
    });