mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
fix: Enable strict TypeScript settings
This commit is contained in:
@@ -2,5 +2,5 @@
|
||||
* Credentials identifying an entity accessing or owning data.
|
||||
*/
|
||||
export interface Credentials {
|
||||
webID: string;
|
||||
webID?: string;
|
||||
}
|
||||
|
||||
@@ -14,5 +14,6 @@ export class SimpleCredentialsExtractor extends CredentialsExtractor {
|
||||
if (input.headers.authorization) {
|
||||
return { webID: input.headers.authorization };
|
||||
}
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,12 +46,12 @@ export interface AuthenticatedLdpHandlerArgs {
|
||||
* The central manager that connects all the necessary handlers to go from an incoming request to an executed operation.
|
||||
*/
|
||||
export class AuthenticatedLdpHandler extends HttpHandler {
|
||||
private readonly requestParser: RequestParser;
|
||||
private readonly credentialsExtractor: CredentialsExtractor;
|
||||
private readonly permissionsExtractor: PermissionsExtractor;
|
||||
private readonly authorizer: Authorizer;
|
||||
private readonly operationHandler: OperationHandler;
|
||||
private readonly responseWriter: ResponseWriter;
|
||||
private readonly requestParser!: RequestParser;
|
||||
private readonly credentialsExtractor!: CredentialsExtractor;
|
||||
private readonly permissionsExtractor!: PermissionsExtractor;
|
||||
private readonly authorizer!: Authorizer;
|
||||
private readonly operationHandler!: OperationHandler;
|
||||
private readonly responseWriter!: ResponseWriter;
|
||||
|
||||
/**
|
||||
* Creates the handler.
|
||||
@@ -87,17 +87,14 @@ export class AuthenticatedLdpHandler extends HttpHandler {
|
||||
* @returns A promise resolving when the handling is finished.
|
||||
*/
|
||||
public async handle(input: { request: HttpRequest; response: HttpResponse }): Promise<void> {
|
||||
let err: Error;
|
||||
let description: ResponseDescription;
|
||||
let writeData: { response: HttpResponse; result: ResponseDescription | Error };
|
||||
|
||||
try {
|
||||
description = await this.runHandlers(input.request);
|
||||
writeData = { response: input.response, result: await this.runHandlers(input.request) };
|
||||
} catch (error) {
|
||||
err = error;
|
||||
writeData = { response: input.response, result: error };
|
||||
}
|
||||
|
||||
const writeData = { response: input.response, description, error: err };
|
||||
|
||||
await this.responseWriter.handleSafe(writeData);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,14 +25,14 @@ export class AcceptPreferenceParser extends PreferenceParser {
|
||||
|
||||
public async handle(input: HttpRequest): Promise<RepresentationPreferences> {
|
||||
const result: RepresentationPreferences = {};
|
||||
const headers: { [T in keyof RepresentationPreferences]: { val: string; func: (input: string) => AcceptHeader[] }} = {
|
||||
const headers: { [T in keyof RepresentationPreferences]: { val?: string; func: (input: string) => AcceptHeader[] }} = {
|
||||
type: { val: input.headers.accept, func: parseAccept },
|
||||
charset: { val: input.headers['accept-charset'] as string, func: parseAcceptCharset },
|
||||
encoding: { val: input.headers['accept-encoding'] as string, func: parseAcceptEncoding },
|
||||
language: { val: input.headers['accept-language'], func: parseAcceptLanguage },
|
||||
};
|
||||
Object.keys(headers).forEach((key: keyof RepresentationPreferences): void => {
|
||||
const preferences = this.parseHeader(headers[key].val, headers[key].func);
|
||||
(Object.keys(headers) as (keyof RepresentationPreferences)[]).forEach((key): void => {
|
||||
const preferences = this.parseHeader(headers[key]!.val, headers[key]!.func);
|
||||
if (preferences.length > 0) {
|
||||
result[key] = preferences;
|
||||
}
|
||||
@@ -53,7 +53,7 @@ export class AcceptPreferenceParser extends PreferenceParser {
|
||||
*
|
||||
* @returns A list of {@link RepresentationPreference}. Returns an empty list if input was not defined.
|
||||
*/
|
||||
private parseHeader(input: string, parseFunction: (input: string) => AcceptHeader[]): RepresentationPreference[] {
|
||||
private parseHeader(input: string | undefined, parseFunction: (input: string) => AcceptHeader[]): RepresentationPreference[] {
|
||||
if (!input) {
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -5,4 +5,4 @@ import { Representation } from '../representation/Representation';
|
||||
/**
|
||||
* Parses the body of an incoming {@link HttpRequest} and converts it to a {@link Representation}.
|
||||
*/
|
||||
export abstract class BodyParser extends AsyncHandler<HttpRequest, Representation> {}
|
||||
export abstract class BodyParser extends AsyncHandler<HttpRequest, Representation | undefined> {}
|
||||
|
||||
@@ -6,4 +6,4 @@ import { ResponseDescription } from '../operations/ResponseDescription';
|
||||
* Writes to the HttpResponse.
|
||||
* Response depends on the operation result and potentially which errors was thrown.
|
||||
*/
|
||||
export abstract class ResponseWriter extends AsyncHandler<{ response: HttpResponse; description?: ResponseDescription; error?: Error }> {}
|
||||
export abstract class ResponseWriter extends AsyncHandler<{ response: HttpResponse; result: ResponseDescription | Error }> {}
|
||||
|
||||
@@ -28,7 +28,9 @@ export class SimpleBodyParser extends BodyParser {
|
||||
}
|
||||
}
|
||||
|
||||
public async handle(input: HttpRequest): Promise<QuadRepresentation> {
|
||||
// Note that the only reason this is a union is in case the body is empty.
|
||||
// If this check gets moved away from the BodyParsers this union could be removed
|
||||
public async handle(input: HttpRequest): Promise<QuadRepresentation | undefined> {
|
||||
const contentType = input.headers['content-type'];
|
||||
|
||||
if (!contentType) {
|
||||
|
||||
@@ -19,9 +19,9 @@ export interface SimpleRequestParserArgs {
|
||||
* of a {@link TargetExtractor}, {@link PreferenceParser}, and {@link BodyParser}.
|
||||
*/
|
||||
export class SimpleRequestParser extends RequestParser {
|
||||
private readonly targetExtractor: TargetExtractor;
|
||||
private readonly preferenceParser: PreferenceParser;
|
||||
private readonly bodyParser: BodyParser;
|
||||
private readonly targetExtractor!: TargetExtractor;
|
||||
private readonly preferenceParser!: PreferenceParser;
|
||||
private readonly bodyParser!: BodyParser;
|
||||
|
||||
public constructor(args: SimpleRequestParserArgs) {
|
||||
super();
|
||||
@@ -42,6 +42,9 @@ export class SimpleRequestParser extends RequestParser {
|
||||
const preferences = await this.preferenceParser.handleSafe(input);
|
||||
const body = await this.bodyParser.handleSafe(input);
|
||||
|
||||
if (!input.method) {
|
||||
throw new Error('Missing method.');
|
||||
}
|
||||
return { method: input.method, target, preferences, body };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,40 +8,38 @@ import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
|
||||
* Writes to an {@link HttpResponse} based on the incoming {@link ResponseDescription} or error.
|
||||
*/
|
||||
export class SimpleResponseWriter extends ResponseWriter {
|
||||
public async canHandle(input: { response: HttpResponse; description?: ResponseDescription; error?: Error }): Promise<void> {
|
||||
if (!input.description && !input.error) {
|
||||
throw new UnsupportedHttpError('Either a description or an error is required for output.');
|
||||
}
|
||||
if (input.description && input.description.body) {
|
||||
if (input.description.body.dataType !== 'binary' && input.description.body.dataType !== 'string') {
|
||||
public async canHandle(input: { response: HttpResponse; result: ResponseDescription | Error }): Promise<void> {
|
||||
if (!(input.result instanceof Error)) {
|
||||
const dataType = input.result.body?.dataType;
|
||||
if (dataType && dataType !== 'binary' && dataType !== 'string') {
|
||||
throw new UnsupportedHttpError('Only string or binary results are supported.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async handle(input: { response: HttpResponse; description?: ResponseDescription; error?: Error }): Promise<void> {
|
||||
if (input.description) {
|
||||
input.response.setHeader('location', input.description.identifier.path);
|
||||
if (input.description.body) {
|
||||
const contentType = input.description.body.metadata.contentType || 'text/plain';
|
||||
public async handle(input: { response: HttpResponse; result: ResponseDescription | Error }): Promise<void> {
|
||||
if (input.result instanceof Error) {
|
||||
let code = 500;
|
||||
if (input.result instanceof HttpError) {
|
||||
code = input.result.statusCode;
|
||||
}
|
||||
input.response.setHeader('content-type', 'text/plain');
|
||||
input.response.writeHead(code);
|
||||
input.response.end(`${input.result.name}: ${input.result.message}\n${input.result.stack}`);
|
||||
} else {
|
||||
input.response.setHeader('location', input.result.identifier.path);
|
||||
if (input.result.body) {
|
||||
const contentType = input.result.body.metadata.contentType ?? 'text/plain';
|
||||
input.response.setHeader('content-type', contentType);
|
||||
input.description.body.data.pipe(input.response);
|
||||
input.result.body.data.pipe(input.response);
|
||||
}
|
||||
|
||||
input.response.writeHead(200);
|
||||
|
||||
if (!input.description.body) {
|
||||
if (!input.result.body) {
|
||||
// If there is an input body the response will end once the input stream ends
|
||||
input.response.end();
|
||||
}
|
||||
} else {
|
||||
let code = 500;
|
||||
if (input.error instanceof HttpError) {
|
||||
code = input.error.statusCode;
|
||||
}
|
||||
input.response.setHeader('content-type', 'text/plain');
|
||||
input.response.writeHead(code);
|
||||
input.response.end(`${input.error.name}: ${input.error.message}\n${input.error.stack}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,9 @@ export class SimplePostOperationHandler extends OperationHandler {
|
||||
}
|
||||
|
||||
public async handle(input: Operation): Promise<ResponseDescription> {
|
||||
if (!input.body) {
|
||||
throw new UnsupportedHttpError('POST operations require a body.');
|
||||
}
|
||||
const identifier = await this.store.addResource(input.target, input.body);
|
||||
return { identifier };
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ export class ExpressHttpServer {
|
||||
app.use(cors({
|
||||
// Based on https://github.com/solid/solid-spec/blob/master/recommendations-server.md#cors---cross-origin-resource-sharing
|
||||
// By default origin is always '*', this forces it to be the origin header if there is one
|
||||
origin: (origin, callback): void => callback(null, (origin || '*') as any),
|
||||
origin: (origin, callback): void => callback(null, (origin ?? '*') as any),
|
||||
methods: [ 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE' ],
|
||||
}));
|
||||
|
||||
|
||||
@@ -39,8 +39,8 @@ export class SimpleSparqlUpdatePatchHandler extends PatchHandler {
|
||||
}
|
||||
|
||||
const def = defaultGraph();
|
||||
const deletes = op.delete || [];
|
||||
const inserts = op.insert || [];
|
||||
const deletes = op.delete ?? [];
|
||||
const inserts = op.insert ?? [];
|
||||
|
||||
if (!deletes.every((pattern): boolean => pattern.graph.equals(def))) {
|
||||
throw new UnsupportedHttpError('GRAPH statements are not supported.');
|
||||
@@ -48,7 +48,7 @@ export class SimpleSparqlUpdatePatchHandler extends PatchHandler {
|
||||
if (!inserts.every((pattern): boolean => pattern.graph.equals(def))) {
|
||||
throw new UnsupportedHttpError('GRAPH statements are not supported.');
|
||||
}
|
||||
if (op.where || deletes.some((pattern): boolean => someTerms(pattern, (term): boolean => term.termType === 'Variable'))) {
|
||||
if (op.where ?? deletes.some((pattern): boolean => someTerms(pattern, (term): boolean => term.termType === 'Variable'))) {
|
||||
throw new UnsupportedHttpError('WHERE statements are not supported.');
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ export abstract class HttpError extends Error {
|
||||
* @param name - Error name. Useful for logging and stack tracing.
|
||||
* @param message - Message to be thrown.
|
||||
*/
|
||||
protected constructor(statusCode: number, name: string, message: string) {
|
||||
protected constructor(statusCode: number, name: string, message?: string) {
|
||||
super(message);
|
||||
this.statusCode = statusCode;
|
||||
this.name = name;
|
||||
|
||||
@@ -10,6 +10,6 @@ export class UnsupportedHttpError extends HttpError {
|
||||
* @param message - Optional, more specific, message.
|
||||
*/
|
||||
public constructor(message?: string) {
|
||||
super(400, 'UnsupportedHttpError', message || 'The given input is not supported by the server configuration.');
|
||||
super(400, 'UnsupportedHttpError', message ?? 'The given input is not supported by the server configuration.');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user