🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

fetch-sparql-endpoint

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetch-sparql-endpoint - npm Package Compare versions

Comparing version

to
4.2.1

213

bin/fetch-sparql-endpoint.js
#!/usr/bin/env node
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_fs_1 = require("node:fs");
const n3_1 = require("n3");
const fs_1 = require("fs");
const minimist = require("minimist");
const rdf_string_1 = require("rdf-string");
const streamToString = require("stream-to-string");
const yargs_1 = require("yargs");
const helpers_1 = require("yargs/helpers");
const __1 = require("..");
function getQuery(query, file) {
return __awaiter(this, void 0, void 0, function* () {
if (query) {
return query;
}
if (file) {
return (0, node_fs_1.readFileSync)(file, { encoding: 'utf-8' });
}
return streamToString(process.stdin);
});
const index_1 = require("../index");
// tslint:disable-next-line:no-var-requires
const n3 = require('n3');
process.argv.splice(0, 2);
const args = minimist(process.argv);
if (args._.length === 0 || args._.length > 2 || args.h || args.help) {
// Print command usage
process.stderr.write(`fetch-sparql-endpoint Sends a query to a SPARQL endpoint
Usage:
fetch-sparql-endpoint https://dbpedia.org/sparql [-q] 'SELECT * WHERE { ?s ?p ?o } 100'
fetch-sparql-endpoint https://dbpedia.org/sparql -f query.sparql
cat query.sparql | fetch-sparql-endpoint https://dbpedia.org/sparql
Options:
-q evaluate the given SPARQL query string
-f evaluate the SPARQL query in the given file
-g send query via HTTP GET instead of POST
--help print this help message
`);
process.exit(1);
}
function querySelect(endpoint, fetcher, query) {
return new Promise((resolve, reject) => {
fetcher.fetchBindings(endpoint, query).then(bindingsStream => bindingsStream
.on('data', (bindings) => {
process.stdout.write(`${JSON.stringify(Object.fromEntries(Object.entries(bindings).map(([key, value]) => [
key,
(0, rdf_string_1.termToString)(value),
])))}\n`);
})
.on('error', reject)
.on('end', resolve)).catch(reject);
});
async function getQuery() {
if (args._.length > 1) {
return args._[1];
}
else if (args.q) {
return args.q;
}
else if (args.f) {
return (0, fs_1.readFileSync)(args.f, { encoding: 'utf8' });
}
else {
// tslint:disable-next-line:no-var-requires
return await require('stream-to-string')(process.stdin);
}
}
function queryAsk(endpoint, fetcher, query) {
return new Promise((resolve, reject) => {
fetcher.fetchAsk(endpoint, query)
.then((answer) => {
process.stdout.write(`${answer}\n`);
resolve();
})
.catch(reject);
const endpoint = args._[0];
getQuery().then((query) => {
const fetcher = new index_1.SparqlEndpointFetcher({
method: args.g ? 'GET' : 'POST',
});
const queryType = fetcher.getQueryType(query);
switch (queryType) {
case 'SELECT':
querySelect(fetcher, query);
break;
case 'ASK':
queryAsk(fetcher, query);
break;
case 'CONSTRUCT':
queryConstruct(fetcher, query);
break;
case 'UNKNOWN':
if (fetcher.getUpdateTypes(query) !== 'UNKNOWN') {
update(fetcher, query);
}
break;
}
});
function querySelect(fetcher, query) {
fetcher.fetchBindings(endpoint, query)
.then((bindingsStream) => {
bindingsStream.on('data', (bindings) => {
for (const variable of Object.keys(bindings)) {
bindings[variable] = (0, rdf_string_1.termToString)(bindings[variable]);
}
process.stdout.write(JSON.stringify(bindings) + '\n');
});
bindingsStream.on('error', (error) => process.stderr.write(error.toString()));
})
.catch((error) => {
process.stderr.write(error.message + '\n');
process.exit(1);
});
}
function queryConstruct(endpoint, fetcher, query) {
return new Promise((resolve, reject) => {
fetcher.fetchTriples(endpoint, query)
.then(tripleStream => tripleStream
.on('error', reject)
.pipe(new n3_1.StreamWriter(__1.SparqlEndpointFetcher.CONTENTTYPE_TURTLE))
.pipe(process.stdout)
.on('end', resolve)).catch(reject);
function queryAsk(fetcher, query) {
fetcher.fetchAsk(endpoint, query)
.then((answer) => {
process.stdout.write(answer + '\n');
})
.catch((error) => {
process.stderr.write(error.message + '\n');
process.exit(1);
});
}
function update(endpoint, fetcher, query) {
return new Promise((resolve, reject) => {
fetcher.fetchUpdate(endpoint, query).then(() => {
process.stdout.write('OK\n');
resolve();
}).catch(reject);
function queryConstruct(fetcher, query) {
fetcher.fetchTriples(endpoint, query)
.then((tripleStream) => {
tripleStream
.on('error', (error) => process.stderr.write(error.toString()))
.pipe(new n3.StreamWriter(index_1.SparqlEndpointFetcher.CONTENTTYPE_TURTLE))
.pipe(process.stdout);
})
.catch((error) => {
process.stderr.write(error.message + '\n');
process.exit(1);
});
}
function run(argv) {
return __awaiter(this, void 0, void 0, function* () {
const args = yield (0, yargs_1.default)((0, helpers_1.hideBin)(argv), 'Sends a query to a SPARQL endpoint')
.example('$0 --endpoint https://dbpedia.org/sparql --query \'SELECT * WHERE { ?s ?p ?o } LIMIT 100\'', 'Fetch 100 triples from the DBPedia SPARQL endpoint')
.example('$0 --endpoint https://dbpedia.org/sparql --file query.rq', 'Run the SPARQL query from query.rq against the DBPedia SPARQL endpoint')
.example('cat query.rq | $0 --endpoint https://dbpedia.org/sparql', 'Run the SPARQL query from query.rq against the DBPedia SPARQL endpoint')
.options({
endpoint: { type: 'string', describe: 'Send the query to this SPARQL endpoint', demandOption: true },
query: { type: 'string', describe: 'Evaluate the given SPARQL query string' },
file: { type: 'string', describe: 'Evaluate the SPARQL query in the given file' },
get: { type: 'boolean', describe: 'Send query via HTTP GET instead of POST', default: false },
timeout: { type: 'number', describe: 'The timeout value in seconds to finish the query' },
auth: { choices: ['basic'], describe: 'The type of authentication to use' },
})
.check((arg) => {
if (arg.auth === 'basic' && (!process.env.SPARQL_USERNAME || !process.env.SPARQL_PASSWORD)) {
throw new Error('Basic authentication requires the SPARQL_USERNAME and SPARQL_PASSWORD environment variables.');
}
return true;
})
.version()
.help('help')
.parse();
const queryString = yield getQuery(args.query, args.file);
let defaultHeaders;
if (args.auth === 'basic') {
defaultHeaders = new Headers({
authorization: `Basic ${Buffer.from(`${process.env.SPARQL_USERNAME}:${process.env.SPARQL_PASSWORD}`, 'binary').toString('base64')}`,
});
}
const fetcher = new __1.SparqlEndpointFetcher({
method: args.get ? 'GET' : 'POST',
timeout: args.timeout ? args.timeout * 1000 : undefined,
defaultHeaders,
});
const queryType = fetcher.getQueryType(queryString);
switch (queryType) {
case 'SELECT':
yield querySelect(args.endpoint, fetcher, queryString);
break;
case 'ASK':
yield queryAsk(args.endpoint, fetcher, queryString);
break;
case 'CONSTRUCT':
yield queryConstruct(args.endpoint, fetcher, queryString);
break;
case 'UNKNOWN':
if (fetcher.getUpdateTypes(queryString) !== 'UNKNOWN') {
yield update(args.endpoint, fetcher, queryString);
}
break;
}
function update(fetcher, query) {
fetcher.fetchUpdate(endpoint, query)
.then(() => {
process.stdout.write('OK\n');
})
.catch((error) => {
process.stderr.write(error.message + '\n');
process.exit(1);
});
}
run(process.argv).then().catch((error) => process.stderr.write(`${error.name}: ${error.message}\n${error.stack}`));
//# sourceMappingURL=fetch-sparql-endpoint.js.map

@@ -1,1 +0,1 @@

export * from './lib/SparqlEndpointFetcher';
export * from "./lib/SparqlEndpointFetcher";
/// <reference types="node" />
import type * as RDF from '@rdfjs/types';
import type { Readable } from 'readable-stream';
import { type InsertDeleteOperation, type ManagementOperation } from 'sparqljs';
import { type ISettings as ISparqlJsonParserArgs, SparqlJsonParser } from 'sparqljson-parse';
import { type ISettings as ISparqlXmlParserArgs, SparqlXmlParser } from 'sparqlxml-parse';
/// <reference types="node" />
import "cross-fetch/polyfill";
import * as RDF from "@rdfjs/types";
import { InsertDeleteOperation, ManagementOperation } from "sparqljs";
import { ISettings, SparqlJsonParser } from "sparqljson-parse";
import { SparqlXmlParser } from "sparqlxml-parse";
import { Readable } from "stream";
/**

@@ -12,14 +14,16 @@ * A SparqlEndpointFetcher can send queries to SPARQL endpoints,

export declare class SparqlEndpointFetcher {
static readonly CONTENTTYPE_SPARQL_JSON = "application/sparql-results+json";
static readonly CONTENTTYPE_SPARQL_XML = "application/sparql-results+xml";
static readonly CONTENTTYPE_TURTLE = "text/turtle";
static readonly CONTENTTYPE_SPARQL: string;
protected readonly method: 'GET' | 'POST';
protected readonly timeout?: number;
protected readonly additionalUrlParams: URLSearchParams;
protected readonly defaultHeaders: Headers;
static CONTENTTYPE_SPARQL_JSON: string;
static CONTENTTYPE_SPARQL_XML: string;
static CONTENTTYPE_SPARQL: string;
static CONTENTTYPE_TURTLE: string;
readonly method: 'POST' | 'GET';
readonly additionalUrlParams: URLSearchParams;
readonly defaultHeaders: Headers;
readonly fetchCb?: (input: Request | string, init?: RequestInit) => Promise<Response>;
protected readonly sparqlParsers: Record<string, ISparqlResultsParser>;
protected readonly sparqlJsonParser: SparqlJsonParser;
protected readonly sparqlXmlParser: SparqlXmlParser;
readonly sparqlParsers: {
[contentType: string]: ISparqlResultsParser;
};
readonly sparqlJsonParser: SparqlJsonParser;
readonly sparqlXmlParser: SparqlXmlParser;
readonly timeout: number;
constructor(args?: ISparqlEndpointFetcherArgs);

@@ -32,5 +36,5 @@ /**

* @param {string} query A query.
* @return {'SELECT' | 'ASK' | 'CONSTRUCT' | 'UNKNOWN'} The query type.
* @return {"SELECT" | "ASK" | "CONSTRUCT" | "UNKNOWN"} The query type.
*/
getQueryType(query: string): 'SELECT' | 'ASK' | 'CONSTRUCT' | 'UNKNOWN';
getQueryType(query: string): "SELECT" | "ASK" | "CONSTRUCT" | "UNKNOWN";
/**

@@ -95,3 +99,3 @@ * Get the query type of the given update query.

}
export interface ISparqlEndpointFetcherArgs extends ISparqlJsonParserArgs, ISparqlXmlParserArgs {
export interface ISparqlEndpointFetcherArgs extends ISettings {
/**

@@ -110,9 +114,11 @@ * A custom HTTP method for issuing (non-update) queries, defaults to POST.

}
export interface IBindings {
[key: string]: RDF.Term;
}
export interface ISparqlResultsParser {
parseResultsStream: (sparqlResponseStream: NodeJS.ReadableStream) => NodeJS.ReadableStream;
parseBooleanStream: (sparqlResponseStream: NodeJS.ReadableStream) => Promise<boolean>;
parseResultsStream(sparqlResponseStream: NodeJS.ReadableStream): NodeJS.ReadableStream;
parseBooleanStream(sparqlResponseStream: NodeJS.ReadableStream): Promise<boolean>;
}
export type IBindings = Record<string, RDF.Term>;
export type IUpdateTypes = {
[K in ManagementOperation['type'] | InsertDeleteOperation['updateType']]?: boolean;
};
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SparqlEndpointFetcher = void 0;
const isStream = require("is-stream");
const n3_1 = require("n3");
const readable_from_web_1 = require("readable-from-web");
require("cross-fetch/polyfill");
const sparqljs_1 = require("sparqljs");

@@ -20,2 +9,6 @@ const sparqljson_parse_1 = require("sparqljson-parse");

const stringifyStream = require("stream-to-string");
const readable_web_to_node_stream_1 = require("@smessie/readable-web-to-node-stream");
// tslint:disable:no-var-requires
const n3 = require('n3');
const isStream = require('is-stream');
/**

@@ -27,8 +20,7 @@ * A SparqlEndpointFetcher can send queries to SPARQL endpoints,

constructor(args) {
var _a, _b, _c;
this.method = (_a = args === null || args === void 0 ? void 0 : args.method) !== null && _a !== void 0 ? _a : 'POST';
this.timeout = args === null || args === void 0 ? void 0 : args.timeout;
this.additionalUrlParams = (_b = args === null || args === void 0 ? void 0 : args.additionalUrlParams) !== null && _b !== void 0 ? _b : new URLSearchParams();
this.defaultHeaders = (_c = args === null || args === void 0 ? void 0 : args.defaultHeaders) !== null && _c !== void 0 ? _c : new Headers();
this.fetchCb = args === null || args === void 0 ? void 0 : args.fetch;
args = args || {};
this.method = args.method || 'POST';
this.additionalUrlParams = args.additionalUrlParams || new URLSearchParams();
this.defaultHeaders = args.defaultHeaders || new Headers();
this.fetchCb = args.fetch;
this.sparqlJsonParser = new sparqljson_parse_1.SparqlJsonParser(args);

@@ -38,10 +30,11 @@ this.sparqlXmlParser = new sparqlxml_parse_1.SparqlXmlParser(args);

[SparqlEndpointFetcher.CONTENTTYPE_SPARQL_JSON]: {
parseBooleanStream: sparqlResponseStream => this.sparqlJsonParser.parseJsonBooleanStream(sparqlResponseStream),
parseResultsStream: sparqlResponseStream => this.sparqlJsonParser.parseJsonResultsStream(sparqlResponseStream),
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),
parseBooleanStream: (sparqlResponseStream) => this.sparqlXmlParser.parseXmlBooleanStream(sparqlResponseStream),
parseResultsStream: (sparqlResponseStream) => this.sparqlXmlParser.parseXmlResultsStream(sparqlResponseStream),
},
};
this.timeout = args.timeout;
}

@@ -54,10 +47,8 @@ /**

* @param {string} query A query.
* @return {'SELECT' | 'ASK' | 'CONSTRUCT' | 'UNKNOWN'} The query type.
* @return {"SELECT" | "ASK" | "CONSTRUCT" | "UNKNOWN"} The query type.
*/
getQueryType(query) {
const parsedQuery = new sparqljs_1.Parser({ sparqlStar: true }).parse(query);
if (parsedQuery.type === 'query') {
return parsedQuery.queryType === 'DESCRIBE' ? 'CONSTRUCT' : parsedQuery.queryType;
}
return 'UNKNOWN';
return parsedQuery.type === 'query'
? (parsedQuery.queryType === 'DESCRIBE' ? 'CONSTRUCT' : parsedQuery.queryType) : "UNKNOWN";
}

@@ -86,4 +77,8 @@ /**

}
return 'UNKNOWN';
else {
return "UNKNOWN";
}
;
}
;
/**

@@ -96,11 +91,10 @@ * Send a SELECT query to the given endpoint URL and return the resulting bindings stream.

*/
fetchBindings(endpoint, query) {
return __awaiter(this, void 0, void 0, function* () {
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);
});
async fetchBindings(endpoint, query) {
const [contentType, responseStream] = await 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);
}

@@ -113,11 +107,10 @@ /**

*/
fetchAsk(endpoint, query) {
return __awaiter(this, void 0, void 0, function* () {
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);
});
async fetchAsk(endpoint, query) {
const [contentType, responseStream] = await 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);
}

@@ -130,7 +123,5 @@ /**

*/
fetchTriples(endpoint, query) {
return __awaiter(this, void 0, void 0, function* () {
const [contentType, responseStream] = yield this.fetchRawStream(endpoint, query, SparqlEndpointFetcher.CONTENTTYPE_TURTLE);
return responseStream.pipe(new n3_1.StreamParser({ format: contentType }));
});
async fetchTriples(endpoint, query) {
const rawStream = (await this.fetchRawStream(endpoint, query, SparqlEndpointFetcher.CONTENTTYPE_TURTLE))[1];
return rawStream.pipe(new n3.StreamParser({ format: SparqlEndpointFetcher.CONTENTTYPE_TURTLE }));
}

@@ -143,20 +134,17 @@ /**

*/
fetchUpdate(endpoint, query) {
return __awaiter(this, void 0, void 0, function* () {
const abortController = new AbortController();
const defaultHeadersRaw = {};
// Headers object does not have other means to iterate it according to the typings
// eslint-disable-next-line unicorn/no-array-for-each
this.defaultHeaders.forEach((value, key) => {
defaultHeadersRaw[key] = value;
});
const init = {
method: 'POST',
headers: Object.assign(Object.assign({}, defaultHeadersRaw), { 'content-type': 'application/sparql-update' }),
body: query,
signal: abortController.signal,
};
yield this.handleFetchCall(endpoint, init, { ignoreBody: true });
abortController.abort();
async fetchUpdate(endpoint, query) {
const AbortController = globalThis.AbortController || await Promise.resolve().then(() => require('abort-controller'));
const abortController = new AbortController();
const defaultHeadersRaw = {};
this.defaultHeaders.forEach((value, key) => {
defaultHeadersRaw[key] = value;
});
const init = {
method: 'POST',
headers: Object.assign(Object.assign({}, defaultHeadersRaw), { 'content-type': 'application/sparql-update' }),
body: query,
signal: abortController.signal,
};
await this.handleFetchCall(endpoint, init, { ignoreBody: true });
abortController.abort();
}

@@ -173,23 +161,21 @@ /**

*/
fetchRawStream(endpoint, query, acceptHeader) {
return __awaiter(this, void 0, void 0, function* () {
let url = this.method === 'POST' ? endpoint : `${endpoint}?query=${encodeURIComponent(query)}`;
// Initiate request
let body;
const headers = new Headers(this.defaultHeaders);
headers.append('Accept', acceptHeader);
if (this.method === 'POST') {
headers.append('Content-Type', 'application/x-www-form-urlencoded');
body = new URLSearchParams();
body.set('query', query);
for (const [key, value] of this.additionalUrlParams.entries()) {
body.set(key, value);
}
headers.append('Content-Length', body.toString().length.toString());
}
else if (this.additionalUrlParams.toString().length > 0) {
url += `&${this.additionalUrlParams.toString()}`;
}
return this.handleFetchCall(url, { headers, method: this.method, body });
});
async fetchRawStream(endpoint, query, acceptHeader) {
let url = this.method === 'POST' ? endpoint : endpoint + '?query=' + encodeURIComponent(query);
// Initiate request
const headers = new Headers(this.defaultHeaders);
let body;
headers.append('Accept', acceptHeader);
if (this.method === 'POST') {
headers.append('Content-Type', 'application/x-www-form-urlencoded');
body = new URLSearchParams();
body.set('query', query);
this.additionalUrlParams.forEach((value, key) => {
body.set(key, value);
});
headers.append('Content-Length', body.toString().length.toString());
}
else if (this.additionalUrlParams.toString() !== '') {
url += `&${this.additionalUrlParams.toString()}`;
}
return this.handleFetchCall(url, { headers, method: this.method, body });
}

@@ -204,37 +190,41 @@ /**

*/
handleFetchCall(url, init, options) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
let timeout;
let responseStream;
if (this.timeout) {
const controller = new AbortController();
init.signal = controller.signal;
timeout = setTimeout(() => controller.abort(), this.timeout);
async handleFetchCall(url, init, options = {}) {
let timeoutId;
if (this.timeout) {
const controller = new AbortController();
init.signal = controller.signal;
timeoutId = setTimeout(() => controller.abort(), this.timeout);
}
const httpResponse = await (this.fetchCb || fetch)(url, init);
clearTimeout(timeoutId);
let responseStream;
// Handle response body
if (!options.ignoreBody) {
// Wrap WhatWG readable stream into a Node.js readable stream
// If the body already is a Node.js stream (in the case of node-fetch), don't do explicit conversion.
responseStream = isStream(httpResponse.body)
? httpResponse.body : new readable_web_to_node_stream_1.ReadableWebToNodeStream(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
if (!httpResponse.ok) {
const simpleUrl = /^[^?]*/u.exec(url)[0];
let bodyString = 'empty response';
if (responseStream) {
bodyString = await stringifyStream(responseStream);
}
const httpResponse = yield ((_a = this.fetchCb) !== null && _a !== void 0 ? _a : fetch)(url, init);
clearTimeout(timeout);
// Handle response body
if (!(options === null || options === void 0 ? void 0 : options.ignoreBody) && httpResponse.body) {
// Wrap WhatWG readable stream into a Node.js readable stream
// If the body already is a Node.js stream (in the case of node-fetch), don't do explicit conversion.
responseStream = (isStream(httpResponse.body) ? httpResponse.body : (0, readable_from_web_1.readableFromWeb)(httpResponse.body));
}
// Emit an error if the server returned an invalid response
if (!httpResponse.ok || (!responseStream && !(options === null || options === void 0 ? void 0 : options.ignoreBody))) {
const simpleUrl = url.split('?').at(0);
const bodyString = responseStream ? yield stringifyStream(responseStream) : 'empty response';
throw new Error(`Invalid SPARQL endpoint response from ${simpleUrl} (HTTP status ${httpResponse.status}):\n${bodyString}`);
}
// Determine the content type
const contentType = (_c = (_b = httpResponse.headers.get('Content-Type')) === null || _b === void 0 ? void 0 : _b.split(';').at(0)) !== null && _c !== void 0 ? _c : '';
return [contentType, responseStream];
});
throw new Error(`Invalid SPARQL endpoint response from ${simpleUrl} (HTTP status ${httpResponse.status}):\n${bodyString}`);
}
return [contentType, responseStream];
}
}
exports.SparqlEndpointFetcher = SparqlEndpointFetcher;
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';
SparqlEndpointFetcher.CONTENTTYPE_SPARQL = `${SparqlEndpointFetcher.CONTENTTYPE_SPARQL_JSON};q=1.0,${SparqlEndpointFetcher.CONTENTTYPE_SPARQL_XML};q=0.7`;
exports.SparqlEndpointFetcher = SparqlEndpointFetcher;
//# sourceMappingURL=SparqlEndpointFetcher.js.map
{
"name": "fetch-sparql-endpoint",
"version": "4.2.0",
"packageManager": "yarn@1.22.22",
"version": "4.2.1",
"description": "A simple, lightweight module to send queries to SPARQL endpoints and retrieve their results in a streaming fashion.",
"author": "Ruben Taelman <rubensworks@gmail.com>",
"license": "MIT",
"homepage": "https://github.com/rubensworks/fetch-sparql-endpoint.js#readme",
"repository": "git@github.com:rubensworks/fetch-sparql-endpoint.js.git",
"bugs": {
"url": "https://github.com/rubensworks/fetch-sparql-endpoint.js/issues"
},
"keywords": [

@@ -21,6 +13,14 @@ "sparql",

],
"sideEffects": false,
"main": "index.js",
"typings": "index",
"bin": "bin/fetch-sparql-endpoint.js",
"repository": "git@github.com:rubensworks/fetch-sparql-endpoint.js.git",
"author": "Ruben Taelman <rubensworks@gmail.com>",
"bugs": {
"url": "https://github.com/rubensworks/fetch-sparql-endpoint.js/issues"
},
"homepage": "https://github.com/rubensworks/fetch-sparql-endpoint.js#readme",
"license": "MIT",
"bin": {
"fetch-sparql-endpoint": "bin/fetch-sparql-endpoint.js"
},
"files": [

@@ -30,54 +30,86 @@ "bin/**/*.d.ts",

"bin/**/*.js.map",
"lib/**/*.d.ts",
"lib/**/*.js",
"lib/**/*.js.map",
"index.d.ts",
"index.js",
"index.js.map",
"lib/**/*.d.ts",
"lib/**/*.js",
"lib/**/*.js.map"
"index.js"
],
"scripts": {
"test": "jest",
"test-watch": "jest --watch",
"coveralls": "jest --coverage && cat ./coverage/lcov.info | coveralls",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"build": "tsc",
"validate": "yarn list",
"version": "manual-git-changelog onversion",
"prepare": "yarn husky"
},
"pre-commit": [
"build",
"lint",
"test"
],
"dependencies": {
"@rdfjs/types": "*",
"@types/n3": "^1.0.0",
"@types/readable-stream": "^4.0.0",
"@types/sparqljs": "^3.0.0",
"@types/readable-stream": "^2.3.11",
"@types/sparqljs": "^3.1.3",
"abort-controller": "^3.0.0",
"cross-fetch": "^3.0.6",
"is-stream": "^2.0.0",
"n3": "^1.0.0",
"rdf-string": "^1.0.0",
"readable-from-web": "^1.0.0",
"sparqljs": "^3.0.0",
"sparqljson-parse": "^2.0.0",
"sparqlxml-parse": "^2.0.0",
"stream-to-string": "^1.0.0",
"yargs": "^17.0.0"
"minimist": "^1.2.0",
"n3": "^1.6.3",
"rdf-string": "^1.6.0",
"@smessie/readable-web-to-node-stream": "^3.0.3",
"sparqljs": "^3.1.2",
"sparqljson-parse": "^2.2.0",
"sparqlxml-parse": "^2.1.1",
"stream-to-string": "^1.1.0"
},
"devDependencies": {
"@rubensworks/eslint-config": "^3.0.0",
"@types/jest": "^29.0.0",
"@types/jest": "^29.5.4",
"@types/minimist": "^1.2.0",
"@types/n3": "^1.10.3",
"arrayify-stream": "^2.0.0",
"coveralls": "^3.0.0",
"eslint": "^8.0.0",
"husky": "^9.0.0",
"jest": "^29.0.0",
"jest-rdf": "^1.0.0",
"jest": "^29.7.0",
"jest-rdf": "^1.7.0",
"manual-git-changelog": "^1.0.0",
"rdf-data-factory": "^1.0.0",
"readable-stream-node-to-web": "^1.0.0",
"streamify-string": "^1.0.0",
"ts-jest": "^29.0.0",
"ts-loader": "^9.0.0",
"pre-commit": "^1.2.2",
"rdf-data-factory": "^1.1.0",
"streamify-string": "^1.0.1",
"ts-jest": "^29.1.1",
"ts-loader": "^9.3.1",
"tslint": "^6.0.0",
"tslint-eslint-rules": "^5.4.0",
"typescript": "^5.0.0",
"webpack": "^5.0.0",
"webpack-cli": "^5.0.0"
}
"readable-stream-node-to-web": "^1.0.1",
"webpack": "^5.73.0",
"webpack-cli": "^5.0.0",
"web-streams-ponyfill": "^1.4.2"
},
"jest": {
"transform": {
"^.+\\.ts$": [
"ts-jest",
{
"tsconfig": "test/tsconfig.json"
}
]
},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$"
],
"testRegex": "(/test/.*|(\\.|/)(test|spec))\\.ts$",
"moduleFileExtensions": [
"ts",
"js"
],
"collectCoverage": true,
"setupFilesAfterEnv": [
"jest-rdf"
],
"testEnvironment": "node"
},
"scripts": {
"test": "jest ${1}",
"test-watch": "jest ${1} --watch",
"coveralls": "jest --coverage && cat ./coverage/lcov.info | coveralls",
"lint": "tslint index.ts lib/**/*.ts test/**/*.ts --exclude '**/*.d.ts'",
"build": "tsc",
"validate": "npm ls",
"prepare": "npm run build",
"version": "manual-git-changelog onversion"
},
"sideEffects": false
}

@@ -35,3 +35,3 @@ # Fetch SPARQL Endpoint

```js
import { SparqlEndpointFetcher } from 'fetch-sparql-endpoint';
import {SparqlEndpointFetcher} from "fetch-sparql-endpoint";

@@ -44,16 +44,9 @@ const myFetcher = new SparqlEndpointFetcher();

const myFetcher = new SparqlEndpointFetcher({
// A custom HTTP method for issuing (non-update) queries, defaults to POST. Update queries are always issued via POST.
method: 'POST',
// A set of additional parameters that well be added to fetchAsk, fetchBindings & fetchTriples requests
additionalUrlParams: new URLSearchParams({ infer: 'true', sameAs: 'false' }),
// Optional default headers that will be included in each request
defaultHeaders: new Headers(),
// A custom fetch-API-supporting function
fetch,
// A custom RDFJS data factory
dataFactory: DataFactory,
// If variable names in bindings should be prefixed with '?', defaults to false
prefixVariableQuestionMark: false,
// Timeout for setting up server connection (Once a connection has been made, and the response is being parsed, the timeout does not apply anymore).
timeout: 5000,
method: 'POST', // A custom HTTP method for issuing (non-update) queries, defaults to POST. Update queries are always issued via POST.
additionalUrlParams: new URLSearchParams({'infer': 'true', 'sameAs': 'false'}); // A set of additional parameters that well be added to fetchAsk, fetchBindings & fetchTriples requests
defaultHeaders: new Headers(), // Optional default headers that will be included in each request
fetch: fetch, // A custom fetch-API-supporting function
dataFactory: DataFactory, // A custom RDFJS data factory
prefixVariableQuestionMark: false, // If variable names in bindings should be prefixed with '?', defaults to false
timeout: 5000 // Timeout for setting up server connection (Once a connection has been made, and the response is being parsed, the timeout does not apply anymore).
});

@@ -68,3 +61,3 @@ ```

const bindingsStream = await fetcher.fetchBindings('https://dbpedia.org/sparql', 'SELECT * WHERE { ?s ?p ?o } LIMIT 100');
bindingsStream.on('data', bindings => console.log(bindings));
bindingsStream.on('data', (bindings) => console.log(bindings));
```

@@ -85,5 +78,5 @@

const bindingsStream = await fetcher.fetchBindings('https://dbpedia.org/sparql', 'SELECT * WHERE { ?s ?p ?o } LIMIT 100');
bindingsStream.on('data', bindings => console.log(bindings));
bindingsStream.on('data', (bindings) => console.log(bindings));
// Will print [ variable('s'), variable('p'), variable('o') ]
bindingsStream.on('variables', variables => console.log(variables));
bindingsStream.on('variables', (variables) => console.log(variables));
```

@@ -108,3 +101,3 @@

const tripleStream = await fetcher.fetchTriples('https://dbpedia.org/sparql', 'CONSTRUCT { ?s ?p ?o } LIMIT 100');
tripleStream.on('data', triple => console.log(triple));
tripleStream.on('data', (triple) => console.log(triple));
```

@@ -136,10 +129,7 @@

```js
// Outputs 'SELECT'
fetcher.getQueryType('SELECT * WHERE { ?s ?p ?o } LIMIT 100');
// Outputs 'ASK'
fetcher.getQueryType('ASK WHERE { ?s ?p ?o }');
// Outputs 'CONSTRUCT'
fetcher.getQueryType('CONSTRUCT { ?s ?p ?o } LIMIT 100');
```
fetcher.getQueryType('SELECT * WHERE { ?s ?p ?o } LIMIT 100'); // Outputs 'SELECT'
fetcher.getQueryType('ASK WHERE { ?s ?p ?o }'); // Outputs 'ASK'
fetcher.getQueryType('CONSTRUCT { ?s ?p ?o } LIMIT 100'); // Outputs 'CONSTRUCT'
```

@@ -150,27 +140,18 @@ This method will also throw an error if the query contains a syntax error.

A command-line tool is provided to quickly query or update any SPARQL endpoint.
With basic authentication, the username and password should be made available
via process-scoped environment variables `SPARQL_USERNAME` and `SPARQL_PASSWORD`.
A command-line tool is provided to quickly query or update any SPARQL endpoint:
Usage:
```
fetch-sparql-endpoint Sends a query to a SPARQL endpoint
Usage:
fetch-sparql-endpoint https://dbpedia.org/sparql [-q] 'SELECT * WHERE { ?s ?p ?o } 100'
fetch-sparql-endpoint https://dbpedia.org/sparql -f query.sparql
cat query.sparql | fetch-sparql-endpoint https://dbpedia.org/sparql
Options:
--endpoint Send the query to this SPARQL endpoint [string] [required]
--query Evaluate the given SPARQL query string [string]
--file Evaluate the SPARQL query in the given file [string]
--get Send query via HTTP GET instead of POST [boolean] [default: false]
--timeout The timeout value in seconds to finish the query [number]
--auth The type of authentication to use [choices: "basic"]
--version Show version number [boolean]
--help Show help [boolean]
Examples:
fetch-sparql-endpoint.js --endpoint Fetch 100 triples from the DBPedia
https://dbpedia.org/sparql --query SPARQL endpoint
'SELECT * WHERE { ?s ?p ?o } LIMIT 100'
fetch-sparql-endpoint.js --endpoint Run the SPARQL query from query.rq
https://dbpedia.org/sparql --file against the DBPedia SPARQL endpoint
query.rq
cat query.rq | fetch-sparql-endpoint.js Run the SPARQL query from query.rq
--endpoint https://dbpedia.org/sparql against the DBPedia SPARQL endpoint
-q evaluate the given SPARQL query string
-f evaluate the SPARQL query in the given file
-g send query via HTTP GET instead of POST
--help print this help message
```

@@ -177,0 +158,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet