@comunica/actor-rdf-update-quads-hypermedia
Version:
A hypermedia rdf-update-quads actor
105 lines • 4.89 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActorRdfUpdateQuadsHypermedia = void 0;
const bus_rdf_update_quads_1 = require("@comunica/bus-rdf-update-quads");
const core_1 = require("@comunica/core");
const utils_query_operation_1 = require("@comunica/utils-query-operation");
const lru_cache_1 = require("lru-cache");
/**
* A comunica Hypermedia RDF Update Quads Actor.
*/
class ActorRdfUpdateQuadsHypermedia extends bus_rdf_update_quads_1.ActorRdfUpdateQuadsDestination {
mediatorDereferenceRdf;
mediatorMetadata;
mediatorMetadataExtract;
mediatorRdfUpdateHypermedia;
cacheSize;
httpInvalidator;
cache;
constructor(args) {
super(args);
this.mediatorDereferenceRdf = args.mediatorDereferenceRdf;
this.mediatorMetadata = args.mediatorMetadata;
this.mediatorMetadataExtract = args.mediatorMetadataExtract;
this.mediatorRdfUpdateHypermedia = args.mediatorRdfUpdateHypermedia;
this.cacheSize = args.cacheSize;
this.httpInvalidator = args.httpInvalidator;
this.cache = this.cacheSize ? new lru_cache_1.LRUCache({ max: this.cacheSize }) : undefined;
const cache = this.cache;
if (cache) {
this.httpInvalidator.addInvalidateListener(({ url }) => url ? cache.delete(url) : cache.clear());
}
}
async test(action) {
const url = (0, utils_query_operation_1.getContextDestinationUrl)((0, utils_query_operation_1.getContextDestination)(action.context));
if (!url) {
return (0, core_1.failTest)(`Actor ${this.name} can only update quads against a single destination URL.`);
}
return (0, core_1.passTestVoid)();
}
getDestination(context) {
const dataDestination = (0, utils_query_operation_1.getContextDestination)(context);
let url = (0, utils_query_operation_1.getContextDestinationUrl)(dataDestination);
// Try to read from cache
if (this.cache) {
const ret = this.cache.get(url);
if (ret) {
return (async () => {
const retMaterialized = await ret;
if (retMaterialized.cachePolicy &&
!await retMaterialized.cachePolicy?.satisfiesWithoutRevalidation({ url, context })) {
// If it's not valid, delete cache entry, and re-fetch immediately
// LIMITATION: we're not sending re-validation requests. So if the server sends a 304, we will perform a new
// request and re-index the source. If an HTTP-level cache is active, the actual HTTP request will not be
// sent, so only local re-indexing will happen, which is negligible in most cases.
this.cache.delete(url);
return this.getDestination(context);
}
return retMaterialized.destination;
})();
}
}
// Otherwise, call mediators
const ret = (async () => {
let metadata;
let exists;
let cachePolicy;
try {
// Dereference destination URL
const dereferenceRdfOutput = await this.mediatorDereferenceRdf
.mediate({ context, url, acceptErrors: true });
exists = dereferenceRdfOutput.exists;
url = dereferenceRdfOutput.url;
cachePolicy = dereferenceRdfOutput.cachePolicy;
// Determine the metadata
const rdfMetadataOuput = await this.mediatorMetadata.mediate({ context, url, quads: dereferenceRdfOutput.data, triples: dereferenceRdfOutput.metadata?.triples });
metadata = (await this.mediatorMetadataExtract.mediate({
context,
url,
metadata: rdfMetadataOuput.metadata,
headers: dereferenceRdfOutput.headers,
requestTime: dereferenceRdfOutput.requestTime,
})).metadata;
}
catch {
metadata = {};
exists = false;
}
// Obtain destination
const { destination } = await this.mediatorRdfUpdateHypermedia.mediate({
context,
url,
metadata,
exists,
forceDestinationType: (0, utils_query_operation_1.getDataDestinationType)(dataDestination),
});
return { destination, cachePolicy };
})();
if (this.cache) {
this.cache.set(url, ret);
}
return ret.then(({ destination }) => destination);
}
}
exports.ActorRdfUpdateQuadsHypermedia = ActorRdfUpdateQuadsHypermedia;
//# sourceMappingURL=ActorRdfUpdateQuadsHypermedia.js.map