refactor: Export ContextDocumentLoader from utility file

This commit is contained in:
Joachim Van Herwegen 2023-02-15 16:11:26 +01:00
parent 8d31233075
commit 5f32137d63
3 changed files with 37 additions and 31 deletions

View File

@ -12,6 +12,7 @@
"Dict",
"Error",
"EventEmitter",
"FetchDocumentLoader",
"GenericEventEmitter",
"HashMap",
"HttpErrorOptions",

View File

@ -1,6 +1,41 @@
import fetch from 'cross-fetch';
import { readJsonSync } from 'fs-extra';
import type { IJsonLdContext } from 'jsonld-context-parser';
import { FetchDocumentLoader } from 'jsonld-context-parser';
import type { ValuePreference, ValuePreferences } from '../../http/representation/RepresentationPreferences';
import { INTERNAL_ALL } from '../../util/ContentTypes';
import { InternalServerError } from '../../util/errors/InternalServerError';
import { resolveAssetPath } from '../../util/PathUtil';
/**
* First, checks whether a context is stored locally before letting the super class do a fetch.
* This can be used when converting JSON-LD with Comunica-related libraries, such as `rdf-parse`.
*
* To use this, add this document loader to the options of the call
* using the `KeysRdfParseJsonLd.documentLoader.name` key.
* All extra keys get passed in the Comunica ActionContext
* and this is the key that is used to define the document loader.
* See https://github.com/rubensworks/rdf-parse.js/blob/master/lib/RdfParser.ts
* and https://github.com/comunica/comunica/blob/master/packages/actor-rdf-parse-jsonld/lib/ActorRdfParseJsonLd.ts
*/
export class ContextDocumentLoader extends FetchDocumentLoader {
private readonly contexts: Record<string, IJsonLdContext>;
public constructor(contexts: Record<string, string>) {
super(fetch);
this.contexts = {};
for (const [ key, path ] of Object.entries(contexts)) {
this.contexts[key] = readJsonSync(resolveAssetPath(path));
}
}
public async load(url: string): Promise<IJsonLdContext> {
if (url in this.contexts) {
return this.contexts[url];
}
return super.load(url);
}
}
/**
* Cleans incoming preferences to prevent unwanted behaviour.

View File

@ -1,44 +1,18 @@
import { PassThrough } from 'stream';
import { KeysRdfParseJsonLd } from '@comunica/context-entries';
import type { NamedNode } from '@rdfjs/types';
import fetch from 'cross-fetch';
import { readJsonSync } from 'fs-extra';
import { FetchDocumentLoader } from 'jsonld-context-parser';
import type { IJsonLdContext } from 'jsonld-context-parser';
import rdfParser from 'rdf-parse';
import { BasicRepresentation } from '../../http/representation/BasicRepresentation';
import type { Representation } from '../../http/representation/Representation';
import { RepresentationMetadata } from '../../http/representation/RepresentationMetadata';
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError';
import { resolveAssetPath } from '../../util/PathUtil';
import { pipeSafely } from '../../util/StreamUtil';
import { PREFERRED_PREFIX_TERM, SOLID_META } from '../../util/Vocabularies';
import { BaseTypedRepresentationConverter } from './BaseTypedRepresentationConverter';
import { ContextDocumentLoader } from './ConversionUtil';
import type { RepresentationConverterArgs } from './RepresentationConverter';
/**
* First checks if a context is stored locally before letting the super class do a fetch.
*/
class ContextDocumentLoader extends FetchDocumentLoader {
private readonly contexts: Record<string, IJsonLdContext>;
public constructor(contexts: Record<string, string>) {
super(fetch);
this.contexts = {};
for (const [ key, path ] of Object.entries(contexts)) {
this.contexts[key] = readJsonSync(resolveAssetPath(path));
}
}
public async load(url: string): Promise<IJsonLdContext> {
if (url in this.contexts) {
return this.contexts[url];
}
return super.load(url);
}
}
/**
* Converts most major RDF serializations to `internal/quads`.
*
@ -63,10 +37,6 @@ export class RdfToQuadConverter extends BaseTypedRepresentationConverter {
const rawQuads = rdfParser.parse(representation.data, {
contentType: representation.metadata.contentType!,
baseIRI: identifier.path,
// All extra keys get passed in the Comunica ActionContext
// and this is the key that is used to define the document loader.
// See https://github.com/rubensworks/rdf-parse.js/blob/master/lib/RdfParser.ts
// and https://github.com/comunica/comunica/blob/master/packages/actor-rdf-parse-jsonld/lib/ActorRdfParseJsonLd.ts
[KeysRdfParseJsonLd.documentLoader.name]: this.documentLoader,
} as any)
// This works only for those cases where the data stream has been completely read before accessing the metadata.