fetch-sparql-endpoint
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -5,2 +5,3 @@ /// <reference types="node" /> | ||
import { ISettings, SparqlJsonParser } from "sparqljson-parse"; | ||
import { SparqlXmlParser } from "sparqlxml-parse"; | ||
/** | ||
@@ -12,5 +13,11 @@ * A SparqlEndpointFetcher can send queries to SPARQL endpoints, | ||
static CONTENTTYPE_SPARQL_JSON: string; | ||
static CONTENTTYPE_SPARQL_XML: string; | ||
static CONTENTTYPE_SPARQL: string; | ||
static CONTENTTYPE_TURTLE: string; | ||
readonly fetchCb: (input?: Request | string, init?: RequestInit) => Promise<Response>; | ||
readonly sparqlParsers: { | ||
[contentType: string]: ISparqlResultsParser; | ||
}; | ||
readonly sparqlJsonParser: SparqlJsonParser; | ||
readonly sparqlXmlParser: SparqlXmlParser; | ||
constructor(args?: ISparqlEndpointFetcherArgs); | ||
@@ -56,5 +63,5 @@ /** | ||
* @param {string} acceptHeader The HTTP accept to use. | ||
* @return {Promise<NodeJS.ReadableStream>} The SPARQL endpoint response stream. | ||
* @return {Promise<[string, NodeJS.ReadableStream]>} The content type and SPARQL endpoint response stream. | ||
*/ | ||
fetchRawStream(endpoint: string, query: string, acceptHeader: string): Promise<NodeJS.ReadableStream>; | ||
fetchRawStream(endpoint: string, query: string, acceptHeader: string): Promise<[string, NodeJS.ReadableStream]>; | ||
} | ||
@@ -67,1 +74,5 @@ export interface ISparqlEndpointFetcherArgs extends ISettings { | ||
} | ||
export interface ISparqlResultsParser { | ||
parseResultsStream(sparqlResponseStream: NodeJS.ReadableStream): NodeJS.ReadableStream; | ||
parseBooleanStream(sparqlResponseStream: NodeJS.ReadableStream): Promise<boolean>; | ||
} |
@@ -7,2 +7,3 @@ "use strict"; | ||
const sparqljson_parse_1 = require("sparqljson-parse"); | ||
const sparqlxml_parse_1 = require("sparqlxml-parse"); | ||
// tslint:disable-next-line:no-var-requires | ||
@@ -19,2 +20,13 @@ const n3 = require('n3'); | ||
this.sparqlJsonParser = new sparqljson_parse_1.SparqlJsonParser(args); | ||
this.sparqlXmlParser = new sparqlxml_parse_1.SparqlXmlParser(args); | ||
this.sparqlParsers = { | ||
[SparqlEndpointFetcher.CONTENTTYPE_SPARQL_JSON]: { | ||
parseBooleanStream: (sparqlResponseStream) => this.sparqlJsonParser.parseJsonBooleanStream(sparqlResponseStream), | ||
parseResultsStream: (sparqlResponseStream) => this.sparqlJsonParser.parseJsonResultsStream(sparqlResponseStream), | ||
}, | ||
[SparqlEndpointFetcher.CONTENTTYPE_SPARQL_XML]: { | ||
parseBooleanStream: (sparqlResponseStream) => this.sparqlXmlParser.parseXmlBooleanStream(sparqlResponseStream), | ||
parseResultsStream: (sparqlResponseStream) => this.sparqlXmlParser.parseXmlResultsStream(sparqlResponseStream), | ||
}, | ||
}; | ||
} | ||
@@ -43,3 +55,9 @@ /** | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
return this.sparqlJsonParser.parseJsonResultsStream(yield this.fetchRawStream(endpoint, query, SparqlEndpointFetcher.CONTENTTYPE_SPARQL_JSON)); | ||
const [contentType, responseStream] = yield this | ||
.fetchRawStream(endpoint, query, SparqlEndpointFetcher.CONTENTTYPE_SPARQL); | ||
const parser = this.sparqlParsers[contentType]; | ||
if (!parser) { | ||
throw new Error('Unknown SPARQL results content type: ' + contentType); | ||
} | ||
return parser.parseResultsStream(responseStream); | ||
}); | ||
@@ -55,3 +73,9 @@ } | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
return this.sparqlJsonParser.parseJsonBooleanStream(yield this.fetchRawStream(endpoint, query, SparqlEndpointFetcher.CONTENTTYPE_SPARQL_JSON)); | ||
const [contentType, responseStream] = yield this | ||
.fetchRawStream(endpoint, query, SparqlEndpointFetcher.CONTENTTYPE_SPARQL); | ||
const parser = this.sparqlParsers[contentType]; | ||
if (!parser) { | ||
throw new Error('Unknown SPARQL results content type: ' + contentType); | ||
} | ||
return parser.parseBooleanStream(responseStream); | ||
}); | ||
@@ -67,3 +91,3 @@ } | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const rawStream = yield this.fetchRawStream(endpoint, query, SparqlEndpointFetcher.CONTENTTYPE_TURTLE); | ||
const rawStream = (yield this.fetchRawStream(endpoint, query, SparqlEndpointFetcher.CONTENTTYPE_TURTLE))[1]; | ||
return rawStream.pipe(new n3.StreamParser({ format: SparqlEndpointFetcher.CONTENTTYPE_TURTLE })); | ||
@@ -80,3 +104,3 @@ }); | ||
* @param {string} acceptHeader The HTTP accept to use. | ||
* @return {Promise<NodeJS.ReadableStream>} The SPARQL endpoint response stream. | ||
* @return {Promise<[string, NodeJS.ReadableStream]>} The content type and SPARQL endpoint response stream. | ||
*/ | ||
@@ -94,2 +118,7 @@ fetchRawStream(endpoint, query, acceptHeader) { | ||
? httpResponse.body : require('node-web-streams').toNodeReadable(httpResponse.body); | ||
// Determine the content type and emit it to the stream | ||
let contentType = httpResponse.headers.get('Content-Type') || ''; | ||
if (contentType.indexOf(';') > 0) { | ||
contentType = contentType.substr(0, contentType.indexOf(';')); | ||
} | ||
// Emit an error if the server returned an invalid response | ||
@@ -99,3 +128,3 @@ if (!httpResponse.ok) { | ||
} | ||
return responseStream; | ||
return [contentType, responseStream]; | ||
}); | ||
@@ -105,4 +134,6 @@ } | ||
SparqlEndpointFetcher.CONTENTTYPE_SPARQL_JSON = 'application/sparql-results+json'; | ||
SparqlEndpointFetcher.CONTENTTYPE_SPARQL_XML = 'application/sparql-results+xml'; | ||
SparqlEndpointFetcher.CONTENTTYPE_SPARQL = `${SparqlEndpointFetcher.CONTENTTYPE_SPARQL_JSON};q=1.0,${SparqlEndpointFetcher.CONTENTTYPE_SPARQL_XML};q=0.7`; | ||
SparqlEndpointFetcher.CONTENTTYPE_TURTLE = 'text/turtle'; | ||
exports.SparqlEndpointFetcher = SparqlEndpointFetcher; | ||
//# sourceMappingURL=SparqlEndpointFetcher.js.map |
{ | ||
"name": "fetch-sparql-endpoint", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A simple, lightweight module to send queries to SPARQL endpoints and retrieve their results in a streaming fashion.", | ||
@@ -46,7 +46,9 @@ "keywords": [ | ||
"sparqljs": "^2.0.3", | ||
"sparqljson-parse": "^1.2.0", | ||
"sparqljson-parse": "^1.3.0", | ||
"sparqlxml-parse": "^1.0.0", | ||
"stream-to-string": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^21.1.8", | ||
"@rdfjs/data-model": "^1.1.0", | ||
"@types/jest": "^23.3.1", | ||
"@types/minimist": "^1.2.0", | ||
@@ -57,11 +59,10 @@ "@types/rdf-js": "^1.0.1", | ||
"coveralls": "^3.0.0", | ||
"jest": "^21.2.1", | ||
"jest": "^23.5.0", | ||
"jest-rdf": "^1.0.0", | ||
"pre-commit": "^1.2.2", | ||
"@rdfjs/data-model": "^1.1.0", | ||
"streamify-string": "^1.0.1", | ||
"ts-jest": "^21.2.3", | ||
"ts-jest": "^23.1.4", | ||
"tslint": "^5.8.0", | ||
"tslint-eslint-rules": "^4.1.1", | ||
"typescript": "^2.6.2" | ||
"tslint-eslint-rules": "^5.4.0", | ||
"typescript": "^3.0.1" | ||
}, | ||
@@ -68,0 +69,0 @@ "jest": { |
@@ -5,3 +5,3 @@ # Fetch SPARQL Endpoint | ||
[![Coverage Status](https://coveralls.io/repos/github/rubensworks/fetch-sparql-endpoint.js/badge.svg?branch=master)](https://coveralls.io/github/rubensworks/fetch-sparql-endpoint.js?branch=master) | ||
[![npm version](https://badge.fury.io/js/fetch-sparql-endpoint.svg)](https://www.npmjs.com/package/fetch-sparql-endpoint) | ||
[![npm version](https://badge.fury.io/js/fetch-sparql-endpoint.svg)](https://www.npmjs.com/package/fetch-sparql-endpoint) [![Greenkeeper badge](https://badges.greenkeeper.io/rubensworks/fetch-sparql-endpoint.js.svg)](https://greenkeeper.io/) | ||
@@ -15,2 +15,7 @@ A simple, lightweight module to send queries to [_SPARQL endpoints_](https://www.w3.org/TR/sparql11-protocol/) and retrieve their results in a _streaming_ fashion. | ||
Internally, this library supports SPARQL results in | ||
[SPARQL JSON](https://www.w3.org/TR/sparql11-results-json/), | ||
[SPARQL XML](https://www.w3.org/TR/rdf-sparql-XMLres/), | ||
and [Turtle](https://www.w3.org/TR/turtle/). | ||
## Install | ||
@@ -114,3 +119,3 @@ | ||
``` | ||
$ fetch-sparql-endpoint https://dbpedia.org/sparql [-q] 'SELECT * WHERE { ?s ?p ?o } 100' | ||
$ fetch-sparql-endpoint https://dbpedia.org/sparql [-q] 'SELECT * WHERE { ?s ?p ?o } LIMIT 100' | ||
$ fetch-sparql-endpoint https://dbpedia.org/sparql -f query.sparql | ||
@@ -117,0 +122,0 @@ $ cat query.sparql | fetch-sparql-endpoint https://dbpedia.org/sparql |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22543
10
306
126
10
+ Addedsparqlxml-parse@^1.0.0
+ Addeddebug@2.6.9(transitive)
+ Addedms@2.0.0(transitive)
+ Addedsax@1.4.1(transitive)
+ Addedsax-stream@1.3.0(transitive)
+ Addedsparqlxml-parse@1.5.0(transitive)
Updatedsparqljson-parse@^1.3.0