feat: Create a RoutingResourceStore that takes routing rules

This commit is contained in:
Joachim Van Herwegen
2020-10-26 09:17:03 +01:00
parent 86de805daa
commit 5287cd1e41
13 changed files with 437 additions and 101 deletions

View File

@@ -2,12 +2,16 @@ import type { Patch } from '../ldp/http/Patch';
import type { Representation } from '../ldp/representation/Representation';
import type { RepresentationPreferences } from '../ldp/representation/RepresentationPreferences';
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { UnsupportedHttpError } from '../util/errors/UnsupportedHttpError';
import type { Conditions } from './Conditions';
import type { ResourceStore } from './ResourceStore';
import type { RouterRule } from './routing/RouterRule';
/**
* Store that calls a specific store based on certain routing defined by the ResourceRouter.
* Store that routes the incoming request to a specific store based on the stored ResourceRouter.
* In case no store was found for one of the functions that take no data (GET/PATCH/DELETE),
* a 404 will be thrown. In the other cases the error of the router will be thrown (which would probably be 400).
*/
export class RoutingResourceStore implements ResourceStore {
private readonly rule: RouterRule;
@@ -18,28 +22,41 @@ export class RoutingResourceStore implements ResourceStore {
public async getRepresentation(identifier: ResourceIdentifier, preferences: RepresentationPreferences,
conditions?: Conditions): Promise<Representation> {
return (await this.rule.getMatchingResourceStore(identifier))
.getRepresentation(identifier, preferences, conditions);
return (await this.getStore(identifier)).getRepresentation(identifier, preferences, conditions);
}
public async addResource(container: ResourceIdentifier, representation: Representation,
conditions?: Conditions): Promise<ResourceIdentifier> {
return (await this.rule.getMatchingResourceStore(container, representation))
.addResource(container, representation, conditions);
return (await this.getStore(container, representation)).addResource(container, representation, conditions);
}
public async setRepresentation(identifier: ResourceIdentifier, representation: Representation,
conditions?: Conditions): Promise<void> {
return (await this.rule.getMatchingResourceStore(identifier, representation))
.setRepresentation(identifier, representation, conditions);
return (await this.getStore(identifier, representation)).setRepresentation(identifier, representation, conditions);
}
public async deleteResource(identifier: ResourceIdentifier, conditions?: Conditions): Promise<void> {
return (await this.rule.getMatchingResourceStore(identifier)).deleteResource(identifier, conditions);
return (await this.getStore(identifier)).deleteResource(identifier, conditions);
}
public async modifyResource(identifier: ResourceIdentifier, patch: Patch, conditions?: Conditions):
Promise<void> {
return (await this.rule.getMatchingResourceStore(identifier)).modifyResource(identifier, patch, conditions);
return (await this.getStore(identifier)).modifyResource(identifier, patch, conditions);
}
private async getStore(identifier: ResourceIdentifier, representation?: Representation): Promise<ResourceStore> {
if (representation) {
return this.rule.handleSafe({ identifier, representation });
}
// In case there is no incoming data we want to return 404 if no store was found
try {
return await this.rule.handleSafe({ identifier });
} catch (error: unknown) {
if (error instanceof UnsupportedHttpError) {
throw new NotFoundHttpError();
}
throw error;
}
}
}

View File

@@ -0,0 +1,80 @@
import type { Representation } from '../../ldp/representation/Representation';
import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError';
import type { ResourceStore } from '../ResourceStore';
import type { PreferenceSupport } from './PreferenceSupport';
import { RouterRule } from './RouterRule';
export interface ConvertingStoreEntry {
store: ResourceStore;
supportChecker: PreferenceSupport;
}
/**
* Rule that directs requests based on how the data would need to be converted.
* In case the given converter can convert the data to the requested type,
* it will be directed to the `convertStore`.
* Otherwise the `defaultStore` will be chosen.
*
* In case there is no data and only an identifier the `defaultStore` will be checked
* if it contains the given identifier.
* If not, the `convertStore` will be returned.
*/
export class ConvertingRouterRule extends RouterRule {
private readonly typedStores: ConvertingStoreEntry[];
private readonly defaultStore: ResourceStore;
public constructor(typedStores: ConvertingStoreEntry[], defaultStore: ResourceStore) {
super();
this.typedStores = typedStores;
this.defaultStore = defaultStore;
}
public async handle(input: { identifier: ResourceIdentifier; representation?: Representation }):
Promise<ResourceStore> {
const { identifier, representation } = input;
let store: ResourceStore;
if (representation) {
// TS type checking is not smart enough to let us reuse the input object
store = await this.findStore(async(entry): Promise<boolean> =>
entry.supportChecker.supports({ identifier, representation }));
} else {
// No content-type given so we can only check if one of the stores has data for the identifier
store = await this.findStore(async(entry): Promise<boolean> =>
this.hasResource(entry.store, input.identifier));
}
return store;
}
/**
* Helper function that checks if the given store contains the given identifier or not.
*/
private async hasResource(store: ResourceStore, identifier: ResourceIdentifier): Promise<boolean> {
try {
const response = await store.getRepresentation(identifier, {});
response.data.destroy();
return true;
} catch (error: unknown) {
if (error instanceof NotFoundHttpError) {
return false;
}
throw error;
}
}
/**
* Helper function that runs the given callback function for all the stores
* and returns the first one that does not throw an error.
*
* Returns the default store if no match was found.
*/
private async findStore(supports: (entry: ConvertingStoreEntry) => Promise<boolean>): Promise<ResourceStore> {
// Try all the stores, return default if there is no match
for (const entry of this.typedStores) {
if (await supports(entry)) {
return entry.store;
}
}
return this.defaultStore;
}
}

View File

@@ -1,35 +0,0 @@
import type { Representation } from '../../ldp/representation/Representation';
import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import type { ResourceStore } from '../ResourceStore';
import type { RouterRule } from './RouterRule';
// TODO:
export class PathRouterRule implements RouterRule {
private readonly pathMap: { [path: string]: ResourceStore };
public constructor(pathMap: { [path: string]: ResourceStore }) {
this.pathMap = pathMap;
}
public async getMatchingResourceStore(identifier: ResourceIdentifier, representation?: Representation):
Promise<ResourceStore> {
const paths = Object.keys(this.pathMap);
const matches = paths.filter((path): boolean => identifier.path.includes(path));
if (matches.length !== 1) {
// Incoming data, need to reject
if (representation) {
throw new UnsupportedHttpError(
`Identifiers need to have exactly 1 of the following in them: [${paths.join(', ')}]`,
);
// Because of the above requirement, we know this will always be a 404 for requests
} else {
throw new NotFoundHttpError();
}
}
return this.pathMap[matches[0]];
}
}

View File

@@ -0,0 +1,31 @@
import type { Representation } from '../../ldp/representation/Representation';
import type { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences';
import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
import type { RepresentationConverter } from '../conversion/RepresentationConverter';
/**
* Helper class that checks if the stored {@link RepresentationConverter} and {@link RepresentationPreferences}
* support the given input {@link RepresentationPreferences} and {@link Representation}.
*
* Creates a new object by combining the input arguments together with the stored preferences and checks
* if the converter can handle that object.
*/
export class PreferenceSupport {
private readonly preferences: RepresentationPreferences;
private readonly converter: RepresentationConverter;
public constructor(preferences: RepresentationPreferences, converter: RepresentationConverter) {
this.preferences = preferences;
this.converter = converter;
}
public async supports(input: { identifier: ResourceIdentifier; representation: Representation }): Promise<boolean> {
const newInput = { ...input, preferences: this.preferences };
try {
await this.converter.canHandle(newInput);
return true;
} catch {
return false;
}
}
}

View File

@@ -1,42 +0,0 @@
import type { Representation } from '../../ldp/representation/Representation';
import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import type { RepresentationConverter } from '../conversion/RepresentationConverter';
import type { ResourceStore } from '../ResourceStore';
import type { RouterRule } from './RouterRule';
// TODO:
export class RdfConvertingRouterRule implements RouterRule {
private readonly rdfStore: ResourceStore;
private readonly binaryStore: ResourceStore;
private readonly converter: RepresentationConverter;
public constructor(rdfStore: ResourceStore, binaryStore: ResourceStore, converter: RepresentationConverter) {
this.rdfStore = rdfStore;
this.binaryStore = binaryStore;
this.converter = converter;
}
public async getMatchingResourceStore(identifier: ResourceIdentifier, representation?: Representation):
Promise<ResourceStore> {
if (representation) {
try {
const preferences = { type: [{ value: INTERNAL_QUADS, weight: 1 }]};
await this.converter.canHandle({ identifier, representation, preferences });
return this.rdfStore;
} catch {
return this.binaryStore;
}
} else {
// No content-type given so we can only check if one of the stores has data for the identifier
// Any of the two stores can be used. Using the binary one here since that one would be faster in current cases.
try {
const response = await this.binaryStore.getRepresentation(identifier, {});
response.data.destroy();
return this.binaryStore;
} catch {
return this.rdfStore;
}
}
}
}

View File

@@ -0,0 +1,61 @@
import type { Representation } from '../../ldp/representation/Representation';
import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { trimTrailingSlashes } from '../../util/Util';
import type { ResourceStore } from '../ResourceStore';
import { RouterRule } from './RouterRule';
/**
* Routes requests to a store based on the path of the identifier.
* The identifier will be stripped of the base URI after which regexes will be used to find the correct store.
* The trailing slash of the base URI will still be present so the first character a regex can match would be that `/`.
* This way regexes such as `/container/` can match containers in any position.
*
* In case none of the regexes match an error will be thrown.
*/
export class RegexRouterRule extends RouterRule {
private readonly base: string;
private readonly regexes: Map<RegExp, ResourceStore>;
/**
* The keys of the `storeMap` will be converted into actual RegExp objects that will be used for testing.
*/
public constructor(base: string, storeMap: Record<string, ResourceStore>) {
super();
this.base = trimTrailingSlashes(base);
this.regexes = new Map(Object.keys(storeMap).map((regex): [ RegExp, ResourceStore ] =>
[ new RegExp(regex, 'u'), storeMap[regex] ]));
}
public async canHandle(input: { identifier: ResourceIdentifier; representation?: Representation }): Promise<void> {
this.matchStore(input.identifier);
}
public async handle(input: { identifier: ResourceIdentifier }): Promise<ResourceStore> {
return this.matchStore(input.identifier);
}
/**
* Finds the store corresponding to the regex that matches the given identifier.
* Throws an error if none is found.
*/
private matchStore(identifier: ResourceIdentifier): ResourceStore {
const path = this.toRelative(identifier);
for (const regex of this.regexes.keys()) {
if (regex.test(path)) {
return this.regexes.get(regex)!;
}
}
throw new UnsupportedHttpError(`No stored regexes match ${identifier.path}`);
}
/**
* Strips the base of the identifier and throws an error if there is no overlap.
*/
private toRelative(identifier: ResourceIdentifier): string {
if (!identifier.path.startsWith(this.base)) {
throw new UnsupportedHttpError(`Identifiers need to start with ${this.base}`);
}
return identifier.path.slice(this.base.length);
}
}

View File

@@ -1,20 +1,11 @@
import type { Representation } from '../../ldp/representation/Representation';
import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
import { AsyncHandler } from '../../util/AsyncHandler';
import type { ResourceStore } from '../ResourceStore';
/**
* A RouterRule represents a rule that decides which instance of a
* ResourceStore should be used to handle the incoming request.
* Finds which store needs to be accessed for the given resource,
* potentially based on the Representation of incoming data.
*/
export interface RouterRule {
/**
* Find the appropriate ResourceStore to which the request should be routed based on the incoming parameters.
* @param identifier - Incoming ResourceIdentifier.
* @param representation - Optional incoming Representation.
*/
getMatchingResourceStore: (
identifier: ResourceIdentifier,
representation?: Representation,
) => Promise<ResourceStore>;
}
export abstract class RouterRule
extends AsyncHandler<{ identifier: ResourceIdentifier; representation?: Representation }, ResourceStore> {}