refactor: support inrupt coding standards

This commit is contained in:
Joachim Van Herwegen
2020-05-20 17:27:54 +02:00
parent 5def53180c
commit f8e136cadb
25 changed files with 722 additions and 604 deletions

View File

@@ -0,0 +1,8 @@
import { Representation } from './Representation';
/**
* A representation containing binary data.
*/
export interface BinaryRepresentation extends Representation {
dataType: 'binary';
}

View File

@@ -0,0 +1,9 @@
import { Representation } from './Representation';
import { ResourceIdentifier } from './ResourceIdentifier';
export interface NamedRepresentation extends Representation {
/**
* The identifier of this representation.
*/
identifier?: ResourceIdentifier;
}

4
src/ldp/http/Patch.ts Normal file
View File

@@ -0,0 +1,4 @@
/**
* Represents the changes needed for a PATCH request.
*/
export interface Patch {}

View File

@@ -0,0 +1,8 @@
import { Representation } from './Representation';
/**
* A representation containing quads as data.
*/
export interface QuadRepresentation extends Representation {
dataType: 'quad';
}

View File

@@ -0,0 +1,20 @@
import { Readable } from 'stream';
import { RepresentationMetadata } from './RepresentationMetadata';
/**
* A representation of a resource.
*/
export interface Representation {
/**
* The corresponding metadata.
*/
metadata: RepresentationMetadata;
/**
* The raw data stream for this representation.
*/
data: Readable;
/**
* The data type of the contents in the data stream.
*/
dataType: string;
}

View File

@@ -0,0 +1,35 @@
/**
* Contains metadata relevant to a representation.
*/
import { Quad } from 'rdf-js';
export interface RepresentationMetadata {
/**
* All metadata triples of the resource.
*/
raw: Quad[];
/**
* The metadata profiles.
*/
profiles: string[];
/**
* Optional size of the representation.
*/
byteSize?: number;
/**
* Optional content type of the representation.
*/
contentType?: string;
/**
* Optional encoding of the representation.
*/
encoding?: string;
/**
* Optional language of the representation.
*/
language?: string;
/**
* Optional timestamp of the representation.
*/
dateTime?: Date;
}

View File

@@ -0,0 +1,4 @@
/**
* Contains the preferences of which kind of representation is requested.
*/
export interface RepresentationPreferences {}

View File

@@ -0,0 +1,4 @@
/**
* The unique identifier of a resource.
*/
export interface ResourceIdentifier {}

View File

@@ -0,0 +1,25 @@
import { Representation } from '../http/Representation';
import { RepresentationPreferences } from '../http/RepresentationPreferences';
import { ResourceIdentifier } from '../http/ResourceIdentifier';
/**
* A single REST operation.
*/
export interface Operation {
/**
* The HTTP method (GET/POST/PUT/PATCH/DELETE/etc.).
*/
method: string;
/**
* Identifier of the target.
*/
target: ResourceIdentifier;
/**
* Representation preferences of the response. Will be empty if there are none.
*/
preferences: RepresentationPreferences;
/**
* Optional representation of the body.
*/
body?: Representation;
}

View File

@@ -0,0 +1,21 @@
import { Operation } from './Operation';
/**
* Handler for a specific operation type.
*/
export interface OperationHandler {
/**
* Checks if the handler supports the given operation.
* @param operation - The input operation.
*
* @returns A promise resolving to a boolean indicating if this handler supports the operation.
*/
canHandle: (operation: Operation) => Promise<boolean>;
/**
* Handles the given operation.
* @param operation - The input operation.
*
* @returns A promise resolving when the operation is handled.
*/
handle: (operation: Operation) => Promise<void>;
}

22
src/server/HttpHandler.ts Normal file
View File

@@ -0,0 +1,22 @@
import { IncomingMessage, ServerResponse } from 'http';
/**
* An HTTP request handler.
*/
export interface HttpHandler {
/**
* Checks whether this handler supports the given request.
* @param req - The input request.
*
* @returns A promise that indicates if this request is supported after resolving.
*/
canHandle: (req: Request) => Promise<boolean>;
/**
* Handles the given request.
* @param req - The input request.
* @param res - The response needed for responding to the request.
*
* @returns A promise resolving when the handling is finished.
*/
handle: (req: IncomingMessage, res: ServerResponse) => Promise<void>;
}

View File

@@ -0,0 +1,6 @@
import { ResourceStore } from './ResourceStore';
/**
* A {@link ResourceStore} of which all operations are atomic.
*/
export interface AtomicResourceStore extends ResourceStore {}

35
src/storage/Conditions.ts Normal file
View File

@@ -0,0 +1,35 @@
import { RepresentationMetadata } from '../ldp/http/RepresentationMetadata';
/**
* The conditions of an HTTP conditional request.
*/
export interface Conditions {
/**
* Valid if matching any of the given ETags.
*/
matchesEtag: string[];
/**
* Valid if not matching any of the given ETags.
*/
notMatchesEtag: string[];
/**
* Valid if modified since the given date.
*/
modifiedSince?: Date;
/**
* Valid if not modified since the given date.
*/
unmodifiedSince?: Date;
/**
* Checks validity based on the given metadata.
* @param metadata - Metadata of the representation.
*/
matchesMetadata: (metadata: RepresentationMetadata) => boolean;
/**
* Checks validity based on the given ETag and/org date.
* @param eTag - Condition based on ETag.
* @param lastModified - Condition based on last modified date.
*/
matches: (eTag?: string, lastModified?: Date) => boolean;
}

10
src/storage/Lock.ts Normal file
View File

@@ -0,0 +1,10 @@
/**
* Lock used by a {@link ResourceLocker} for non-atomic operations.
*/
export interface Lock {
/**
* Release this lock.
* @returns A promise resolving when the release is finished.
*/
release: () => Promise<void>;
}

View File

@@ -0,0 +1,24 @@
import { Representation } from '../ldp/http/Representation';
import { RepresentationPreferences } from '../ldp/http/RepresentationPreferences';
/**
* Allows converting from one resource representation to another.
*/
export interface RepresentationConverter {
/**
* Checks if the converter supports converting the given resource based on the given preferences.
* @param representation - The input representation.
* @param preferences - The requested representation preferences.
*
* @returns A promise resolving to a boolean representing whether this conversion can be done.
*/
supports: (representation: Representation, preferences: RepresentationPreferences) => Promise<boolean>;
/**
* Converts the given representation.
* @param representation - The input representation to convert.
* @param preferences - The requested representation preferences.
*
* @returns A promise resolving to the requested representation.
*/
convert: (representation: Representation, preferences: RepresentationPreferences) => Promise<Representation>;
}

View File

@@ -0,0 +1,15 @@
import { Lock } from './Lock';
import { ResourceIdentifier } from '../ldp/http/ResourceIdentifier';
/**
* Allows the locking of resources which is needed for non-atomic {@link ResourceStore}s.
*/
export interface ResourceLocker {
/**
* Lock the given resource.
* @param identifier - Identifier of the resource that needs to be locked.
*
* @returns A promise containing the lock on the resource.
*/
acquire: (identifier: ResourceIdentifier) => Promise<Lock>;
}

View File

@@ -0,0 +1,22 @@
import { RepresentationMetadata } from '../ldp/http/RepresentationMetadata';
/**
* Supports mapping a file to an URL and back.
*/
export interface ResourceMapper {
/**
* Maps the given file to an URL.
* @param file - The input file.
*
* @returns A promise resolving to the corresponding URL and metadata of the representation.
*/
mapFilePathToUrl: (file: File) => Promise<{ url: URL; metadata: RepresentationMetadata }>;
/**
* Maps the given URL and metadata to a file.
* @param url - The input URL.
* @param metadata - The representation metadata.
*
* @returns A promise resolving to the corresponding file.
*/
mapUrlToFilePath: (url: URL, metadata: RepresentationMetadata) => Promise<File>;
}

View File

@@ -0,0 +1,76 @@
import { Conditions } from './Conditions';
import { Patch } from '../ldp/http/Patch';
import { Representation } from '../ldp/http/Representation';
import { RepresentationPreferences } from '../ldp/http/RepresentationPreferences';
import { ResourceIdentifier } from '../ldp/http/ResourceIdentifier';
/**
* A ResourceStore represents a collection of resources.
* It has been designed such that each of its methods
* can be implemented in an atomic way: for each CRUD operation, only one
* dedicated method needs to be called. A fifth method enables the optimization
* of partial updates with PATCH. It is up to the implementer of the interface to
* (not) make an implementation atomic.
*/
export interface ResourceStore {
/**
* Read a resource.
* @param identifier - Identifier of the resource to read.
* @param preferences - Representation preferences.
* @param conditions - Optional conditions.
*
* @returns A promise containing the representation.
*/
getRepresentation: (
identifier: ResourceIdentifier,
preferences: RepresentationPreferences,
conditions?: Conditions,
) => Promise<Representation>;
/**
* Create a resource.
* @param container - Container in which to create a resource.
* @param representation - Representation of the new resource
* @param conditions - Optional conditions.
*
* @returns A promise containing the new identifier.
*/
addResource: (
container: ResourceIdentifier,
representation: Representation,
conditions?: Conditions,
) => Promise<ResourceIdentifier>;
/**
* Fully update a resource.
* @param identifier - Identifier of resource to update.
* @param representation - New representation of the resource.
* @param conditions - Optional conditions.
*
* @returns A promise resolving when the update is finished.
*/
setRepresentation: (
identifier: ResourceIdentifier,
representation: Representation,
conditions?: Conditions,
) => Promise<void>;
/**
* Delete a resource.
* @param identifier - Identifier of resource to delete.
* @param conditions - Optional conditions.
*
* @returns A promise resolving when the delete is finished.
*/
deleteResource: (identifier: ResourceIdentifier, conditions?: Conditions) => Promise<void>;
/**
* Partially update a resource.
* @param identifier - Identifier of resource to update.
* @param patch - Description of which parts to update.
* @param conditions - Optional conditions.
*
* @returns A promise resolving when the update is finished.
*/
modifyResource: (identifier: ResourceIdentifier, patch: Patch, conditions?: Conditions) => Promise<void>;
}