From eb1440851acd24baab31c3567a13ab36164cd9b9 Mon Sep 17 00:00:00 2001 From: Ruben Verborgh Date: Tue, 26 Jan 2021 22:36:58 +0100 Subject: [PATCH] refactor: Move handlers into subfolder. --- src/authentication/CredentialsExtractor.ts | 2 +- src/authorization/Authorizer.ts | 2 +- src/index.ts | 10 ++++++---- src/init/Initializer.ts | 2 +- src/ldp/http/BodyParser.ts | 2 +- src/ldp/http/PreferenceParser.ts | 2 +- src/ldp/http/RequestParser.ts | 2 +- src/ldp/http/ResponseWriter.ts | 2 +- src/ldp/http/TargetExtractor.ts | 2 +- src/ldp/http/metadata/MetadataExtractor.ts | 2 +- src/ldp/http/metadata/MetadataWriter.ts | 2 +- src/ldp/operations/OperationHandler.ts | 2 +- src/ldp/permissions/PermissionsExtractor.ts | 2 +- src/pods/agent/AgentParser.ts | 2 +- src/server/HttpHandler.ts | 2 +- src/server/WebSocketHandler.ts | 2 +- src/storage/conversion/IfNeededConverter.ts | 2 +- src/storage/conversion/RepresentationConverter.ts | 2 +- src/storage/patch/PatchHandler.ts | 2 +- src/storage/routing/RouterRule.ts | 2 +- src/util/{ => handlers}/AsyncHandler.ts | 0 src/util/{ => handlers}/SequenceHandler.ts | 0 src/util/{ => handlers}/UnsupportedAsyncHandler.ts | 2 +- src/util/{ => handlers}/WaterfallHandler.ts | 10 +++++----- test/unit/util/{ => handlers}/AsyncHandler.test.ts | 4 ++-- test/unit/util/{ => handlers}/SequenceHandler.test.ts | 4 ++-- .../{ => handlers}/UnsupportedAsyncHandler.test.ts | 4 ++-- test/unit/util/{ => handlers}/WaterfallHandler.test.ts | 10 +++++----- test/util/StaticAsyncHandler.ts | 2 +- 29 files changed, 43 insertions(+), 41 deletions(-) rename src/util/{ => handlers}/AsyncHandler.ts (100%) rename src/util/{ => handlers}/SequenceHandler.ts (100%) rename src/util/{ => handlers}/UnsupportedAsyncHandler.ts (87%) rename src/util/{ => handlers}/WaterfallHandler.ts (92%) rename test/unit/util/{ => handlers}/AsyncHandler.test.ts (91%) rename test/unit/util/{ => handlers}/SequenceHandler.test.ts (93%) rename test/unit/util/{ => handlers}/UnsupportedAsyncHandler.test.ts (80%) rename test/unit/util/{ => handlers}/WaterfallHandler.test.ts (92%) diff --git a/src/authentication/CredentialsExtractor.ts b/src/authentication/CredentialsExtractor.ts index 12fafa265..1d7294b2a 100644 --- a/src/authentication/CredentialsExtractor.ts +++ b/src/authentication/CredentialsExtractor.ts @@ -1,5 +1,5 @@ import type { HttpRequest } from '../server/HttpRequest'; -import { AsyncHandler } from '../util/AsyncHandler'; +import { AsyncHandler } from '../util/handlers/AsyncHandler'; import type { Credentials } from './Credentials'; /** diff --git a/src/authorization/Authorizer.ts b/src/authorization/Authorizer.ts index 02f603dcd..8e1dd8e05 100644 --- a/src/authorization/Authorizer.ts +++ b/src/authorization/Authorizer.ts @@ -1,7 +1,7 @@ import type { Credentials } from '../authentication/Credentials'; import type { PermissionSet } from '../ldp/permissions/PermissionSet'; import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier'; -import { AsyncHandler } from '../util/AsyncHandler'; +import { AsyncHandler } from '../util/handlers/AsyncHandler'; /** * Verifies if the given credentials have access to the given permissions on the given resource. diff --git a/src/index.ts b/src/index.ts index b86bbaef1..5de224a14 100644 --- a/src/index.ts +++ b/src/index.ts @@ -187,6 +187,12 @@ export * from './util/errors/SystemError'; export * from './util/errors/UnauthorizedHttpError'; export * from './util/errors/UnsupportedMediaTypeHttpError'; +// Util/Handlers +export * from './util/handlers/AsyncHandler'; +export * from './util/handlers/SequenceHandler'; +export * from './util/handlers/UnsupportedAsyncHandler'; +export * from './util/handlers/WaterfallHandler'; + // Util/Identifiers export * from './util/identifiers/IdentifierStrategy'; export * from './util/identifiers/SingleRootIdentifierStrategy'; @@ -198,16 +204,12 @@ export * from './util/locking/SingleThreadedResourceLocker'; export * from './util/locking/WrappedExpiringResourceLocker'; // Util -export * from './util/AsyncHandler'; export * from './util/ContentTypes'; export * from './util/GuardedStream'; export * from './util/HeaderUtil'; export * from './util/PathUtil'; export * from './util/QuadUtil'; export * from './util/RecordObject'; -export * from './util/SequenceHandler'; export * from './util/StreamUtil'; export * from './util/TermUtil'; -export * from './util/UnsupportedAsyncHandler'; export * from './util/Vocabularies'; -export * from './util/WaterfallHandler'; diff --git a/src/init/Initializer.ts b/src/init/Initializer.ts index 9348beb2e..502475eba 100644 --- a/src/init/Initializer.ts +++ b/src/init/Initializer.ts @@ -1,3 +1,3 @@ -import { AsyncHandler } from '../util/AsyncHandler'; +import { AsyncHandler } from '../util/handlers/AsyncHandler'; export abstract class Initializer extends AsyncHandler {} diff --git a/src/ldp/http/BodyParser.ts b/src/ldp/http/BodyParser.ts index 0d0a774c4..0e36aff2b 100644 --- a/src/ldp/http/BodyParser.ts +++ b/src/ldp/http/BodyParser.ts @@ -1,5 +1,5 @@ import type { HttpRequest } from '../../server/HttpRequest'; -import { AsyncHandler } from '../../util/AsyncHandler'; +import { AsyncHandler } from '../../util/handlers/AsyncHandler'; import type { Representation } from '../representation/Representation'; import type { RepresentationMetadata } from '../representation/RepresentationMetadata'; diff --git a/src/ldp/http/PreferenceParser.ts b/src/ldp/http/PreferenceParser.ts index d8e9c912a..f0000881a 100644 --- a/src/ldp/http/PreferenceParser.ts +++ b/src/ldp/http/PreferenceParser.ts @@ -1,5 +1,5 @@ import type { HttpRequest } from '../../server/HttpRequest'; -import { AsyncHandler } from '../../util/AsyncHandler'; +import { AsyncHandler } from '../../util/handlers/AsyncHandler'; import type { RepresentationPreferences } from '../representation/RepresentationPreferences'; /** diff --git a/src/ldp/http/RequestParser.ts b/src/ldp/http/RequestParser.ts index d0dd1dfe6..281793dc4 100644 --- a/src/ldp/http/RequestParser.ts +++ b/src/ldp/http/RequestParser.ts @@ -1,5 +1,5 @@ import type { HttpRequest } from '../../server/HttpRequest'; -import { AsyncHandler } from '../../util/AsyncHandler'; +import { AsyncHandler } from '../../util/handlers/AsyncHandler'; import type { Operation } from '../operations/Operation'; /** diff --git a/src/ldp/http/ResponseWriter.ts b/src/ldp/http/ResponseWriter.ts index ba7d722ab..926c320c5 100644 --- a/src/ldp/http/ResponseWriter.ts +++ b/src/ldp/http/ResponseWriter.ts @@ -1,5 +1,5 @@ import type { HttpResponse } from '../../server/HttpResponse'; -import { AsyncHandler } from '../../util/AsyncHandler'; +import { AsyncHandler } from '../../util/handlers/AsyncHandler'; import type { ResponseDescription } from './response/ResponseDescription'; /** diff --git a/src/ldp/http/TargetExtractor.ts b/src/ldp/http/TargetExtractor.ts index d6d74b368..1ce24fe0d 100644 --- a/src/ldp/http/TargetExtractor.ts +++ b/src/ldp/http/TargetExtractor.ts @@ -1,5 +1,5 @@ import type { HttpRequest } from '../../server/HttpRequest'; -import { AsyncHandler } from '../../util/AsyncHandler'; +import { AsyncHandler } from '../../util/handlers/AsyncHandler'; import type { ResourceIdentifier } from '../representation/ResourceIdentifier'; /** diff --git a/src/ldp/http/metadata/MetadataExtractor.ts b/src/ldp/http/metadata/MetadataExtractor.ts index 2af766480..0c5f94f33 100644 --- a/src/ldp/http/metadata/MetadataExtractor.ts +++ b/src/ldp/http/metadata/MetadataExtractor.ts @@ -1,5 +1,5 @@ import type { HttpRequest } from '../../../server/HttpRequest'; -import { AsyncHandler } from '../../../util/AsyncHandler'; +import { AsyncHandler } from '../../../util/handlers/AsyncHandler'; import type { RepresentationMetadata } from '../../representation/RepresentationMetadata'; import type { ResourceIdentifier } from '../../representation/ResourceIdentifier'; diff --git a/src/ldp/http/metadata/MetadataWriter.ts b/src/ldp/http/metadata/MetadataWriter.ts index 46dd8aa3f..8b91f7830 100644 --- a/src/ldp/http/metadata/MetadataWriter.ts +++ b/src/ldp/http/metadata/MetadataWriter.ts @@ -1,5 +1,5 @@ import type { HttpResponse } from '../../../server/HttpResponse'; -import { AsyncHandler } from '../../../util/AsyncHandler'; +import { AsyncHandler } from '../../../util/handlers/AsyncHandler'; import type { RepresentationMetadata } from '../../representation/RepresentationMetadata'; /** diff --git a/src/ldp/operations/OperationHandler.ts b/src/ldp/operations/OperationHandler.ts index 014336d70..724cd3d3e 100644 --- a/src/ldp/operations/OperationHandler.ts +++ b/src/ldp/operations/OperationHandler.ts @@ -1,4 +1,4 @@ -import { AsyncHandler } from '../../util/AsyncHandler'; +import { AsyncHandler } from '../../util/handlers/AsyncHandler'; import type { ResponseDescription } from '../http/response/ResponseDescription'; import type { Operation } from './Operation'; diff --git a/src/ldp/permissions/PermissionsExtractor.ts b/src/ldp/permissions/PermissionsExtractor.ts index ea562b107..168ad919d 100644 --- a/src/ldp/permissions/PermissionsExtractor.ts +++ b/src/ldp/permissions/PermissionsExtractor.ts @@ -1,4 +1,4 @@ -import { AsyncHandler } from '../../util/AsyncHandler'; +import { AsyncHandler } from '../../util/handlers/AsyncHandler'; import type { Operation } from '../operations/Operation'; import type { PermissionSet } from './PermissionSet'; diff --git a/src/pods/agent/AgentParser.ts b/src/pods/agent/AgentParser.ts index f1b3c64d3..4098fa6cf 100644 --- a/src/pods/agent/AgentParser.ts +++ b/src/pods/agent/AgentParser.ts @@ -1,5 +1,5 @@ import type { Representation } from '../../ldp/representation/Representation'; -import { AsyncHandler } from '../../util/AsyncHandler'; +import { AsyncHandler } from '../../util/handlers/AsyncHandler'; import type { Agent } from './Agent'; /** diff --git a/src/server/HttpHandler.ts b/src/server/HttpHandler.ts index 7505b232a..a48314ae9 100644 --- a/src/server/HttpHandler.ts +++ b/src/server/HttpHandler.ts @@ -1,4 +1,4 @@ -import { AsyncHandler } from '../util/AsyncHandler'; +import { AsyncHandler } from '../util/handlers/AsyncHandler'; import type { HttpRequest } from './HttpRequest'; import type { HttpResponse } from './HttpResponse'; diff --git a/src/server/WebSocketHandler.ts b/src/server/WebSocketHandler.ts index 32ab66cc8..ec7c40f5a 100644 --- a/src/server/WebSocketHandler.ts +++ b/src/server/WebSocketHandler.ts @@ -1,5 +1,5 @@ import type WebSocket from 'ws'; -import { AsyncHandler } from '../util/AsyncHandler'; +import { AsyncHandler } from '../util/handlers/AsyncHandler'; import type { HttpRequest } from './HttpRequest'; /** diff --git a/src/storage/conversion/IfNeededConverter.ts b/src/storage/conversion/IfNeededConverter.ts index 4a00d2d27..ffcdcfd9f 100644 --- a/src/storage/conversion/IfNeededConverter.ts +++ b/src/storage/conversion/IfNeededConverter.ts @@ -1,7 +1,7 @@ import type { Representation } from '../../ldp/representation/Representation'; import { getLoggerFor } from '../../logging/LogUtil'; import { InternalServerError } from '../../util/errors/InternalServerError'; -import { UnsupportedAsyncHandler } from '../../util/UnsupportedAsyncHandler'; +import { UnsupportedAsyncHandler } from '../../util/handlers/UnsupportedAsyncHandler'; import { hasMatchingMediaTypes } from './ConversionUtil'; import { RepresentationConverter } from './RepresentationConverter'; import type { RepresentationConverterArgs } from './RepresentationConverter'; diff --git a/src/storage/conversion/RepresentationConverter.ts b/src/storage/conversion/RepresentationConverter.ts index 9197ce173..a9bc02d94 100644 --- a/src/storage/conversion/RepresentationConverter.ts +++ b/src/storage/conversion/RepresentationConverter.ts @@ -1,7 +1,7 @@ import type { Representation } from '../../ldp/representation/Representation'; import type { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences'; import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier'; -import { AsyncHandler } from '../../util/AsyncHandler'; +import { AsyncHandler } from '../../util/handlers/AsyncHandler'; export interface RepresentationConverterArgs { /** diff --git a/src/storage/patch/PatchHandler.ts b/src/storage/patch/PatchHandler.ts index e03ec21f1..0a44d8be3 100644 --- a/src/storage/patch/PatchHandler.ts +++ b/src/storage/patch/PatchHandler.ts @@ -1,5 +1,5 @@ import type { Patch } from '../../ldp/http/Patch'; import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier'; -import { AsyncHandler } from '../../util/AsyncHandler'; +import { AsyncHandler } from '../../util/handlers/AsyncHandler'; export abstract class PatchHandler extends AsyncHandler<{identifier: ResourceIdentifier; patch: Patch}> {} diff --git a/src/storage/routing/RouterRule.ts b/src/storage/routing/RouterRule.ts index 83cadd8e6..409cd610f 100644 --- a/src/storage/routing/RouterRule.ts +++ b/src/storage/routing/RouterRule.ts @@ -1,6 +1,6 @@ import type { Representation } from '../../ldp/representation/Representation'; import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier'; -import { AsyncHandler } from '../../util/AsyncHandler'; +import { AsyncHandler } from '../../util/handlers/AsyncHandler'; import type { ResourceStore } from '../ResourceStore'; /** diff --git a/src/util/AsyncHandler.ts b/src/util/handlers/AsyncHandler.ts similarity index 100% rename from src/util/AsyncHandler.ts rename to src/util/handlers/AsyncHandler.ts diff --git a/src/util/SequenceHandler.ts b/src/util/handlers/SequenceHandler.ts similarity index 100% rename from src/util/SequenceHandler.ts rename to src/util/handlers/SequenceHandler.ts diff --git a/src/util/UnsupportedAsyncHandler.ts b/src/util/handlers/UnsupportedAsyncHandler.ts similarity index 87% rename from src/util/UnsupportedAsyncHandler.ts rename to src/util/handlers/UnsupportedAsyncHandler.ts index 936d433e2..7099ea5d8 100644 --- a/src/util/UnsupportedAsyncHandler.ts +++ b/src/util/handlers/UnsupportedAsyncHandler.ts @@ -1,5 +1,5 @@ +import { NotImplementedHttpError } from '../errors/NotImplementedHttpError'; import { AsyncHandler } from './AsyncHandler'; -import { NotImplementedHttpError } from './errors/NotImplementedHttpError'; /** * Handler that does not support any input and will always throw an error. diff --git a/src/util/WaterfallHandler.ts b/src/util/handlers/WaterfallHandler.ts similarity index 92% rename from src/util/WaterfallHandler.ts rename to src/util/handlers/WaterfallHandler.ts index 4ab45b10e..50a5dc5d0 100644 --- a/src/util/WaterfallHandler.ts +++ b/src/util/handlers/WaterfallHandler.ts @@ -1,9 +1,9 @@ -import { getLoggerFor } from '../logging/LogUtil'; +import { getLoggerFor } from '../../logging/LogUtil'; +import { BadRequestHttpError } from '../errors/BadRequestHttpError'; +import { isNativeError } from '../errors/ErrorUtil'; +import { HttpError } from '../errors/HttpError'; +import { InternalServerError } from '../errors/InternalServerError'; import type { AsyncHandler } from './AsyncHandler'; -import { BadRequestHttpError } from './errors/BadRequestHttpError'; -import { isNativeError } from './errors/ErrorUtil'; -import { HttpError } from './errors/HttpError'; -import { InternalServerError } from './errors/InternalServerError'; /** * A composite handler that tries multiple handlers one by one diff --git a/test/unit/util/AsyncHandler.test.ts b/test/unit/util/handlers/AsyncHandler.test.ts similarity index 91% rename from test/unit/util/AsyncHandler.test.ts rename to test/unit/util/handlers/AsyncHandler.test.ts index e22770d3b..30fe80a3f 100644 --- a/test/unit/util/AsyncHandler.test.ts +++ b/test/unit/util/handlers/AsyncHandler.test.ts @@ -1,5 +1,5 @@ -import { AsyncHandler } from '../../../src/util/AsyncHandler'; -import { StaticAsyncHandler } from '../../util/StaticAsyncHandler'; +import { AsyncHandler } from '../../../../src/util/handlers/AsyncHandler'; +import { StaticAsyncHandler } from '../../../util/StaticAsyncHandler'; describe('An AsyncHandler', (): void => { it('supports any input by default.', async(): Promise => { diff --git a/test/unit/util/SequenceHandler.test.ts b/test/unit/util/handlers/SequenceHandler.test.ts similarity index 93% rename from test/unit/util/SequenceHandler.test.ts rename to test/unit/util/handlers/SequenceHandler.test.ts index 8c6be9959..b13f4983c 100644 --- a/test/unit/util/SequenceHandler.test.ts +++ b/test/unit/util/handlers/SequenceHandler.test.ts @@ -1,5 +1,5 @@ -import type { AsyncHandler } from '../../../src/util/AsyncHandler'; -import { SequenceHandler } from '../../../src/util/SequenceHandler'; +import type { AsyncHandler } from '../../../../src/util/handlers/AsyncHandler'; +import { SequenceHandler } from '../../../../src/util/handlers/SequenceHandler'; describe('A SequenceHandler', (): void => { const handlers: jest.Mocked>[] = [ diff --git a/test/unit/util/UnsupportedAsyncHandler.test.ts b/test/unit/util/handlers/UnsupportedAsyncHandler.test.ts similarity index 80% rename from test/unit/util/UnsupportedAsyncHandler.test.ts rename to test/unit/util/handlers/UnsupportedAsyncHandler.test.ts index 7866b9c0e..ae46cd2c5 100644 --- a/test/unit/util/UnsupportedAsyncHandler.test.ts +++ b/test/unit/util/handlers/UnsupportedAsyncHandler.test.ts @@ -1,5 +1,5 @@ -import { NotImplementedHttpError } from '../../../src/util/errors/NotImplementedHttpError'; -import { UnsupportedAsyncHandler } from '../../../src/util/UnsupportedAsyncHandler'; +import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError'; +import { UnsupportedAsyncHandler } from '../../../../src/util/handlers/UnsupportedAsyncHandler'; describe('An UnsupportedAsyncHandler', (): void => { it('throws a default error when no message is set.', async(): Promise => { diff --git a/test/unit/util/WaterfallHandler.test.ts b/test/unit/util/handlers/WaterfallHandler.test.ts similarity index 92% rename from test/unit/util/WaterfallHandler.test.ts rename to test/unit/util/handlers/WaterfallHandler.test.ts index d86d4ff5e..1bb20f4b2 100644 --- a/test/unit/util/WaterfallHandler.test.ts +++ b/test/unit/util/handlers/WaterfallHandler.test.ts @@ -1,8 +1,8 @@ -import type { AsyncHandler } from '../../../src/util/AsyncHandler'; -import { BadRequestHttpError } from '../../../src/util/errors/BadRequestHttpError'; -import { HttpError } from '../../../src/util/errors/HttpError'; -import { WaterfallHandler } from '../../../src/util/WaterfallHandler'; -import { StaticAsyncHandler } from '../../util/StaticAsyncHandler'; +import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError'; +import { HttpError } from '../../../../src/util/errors/HttpError'; +import type { AsyncHandler } from '../../../../src/util/handlers/AsyncHandler'; +import { WaterfallHandler } from '../../../../src/util/handlers/WaterfallHandler'; +import { StaticAsyncHandler } from '../../../util/StaticAsyncHandler'; describe('A WaterfallHandler', (): void => { describe('with no handlers', (): void => { diff --git a/test/util/StaticAsyncHandler.ts b/test/util/StaticAsyncHandler.ts index 2e3946411..c482285d5 100644 --- a/test/util/StaticAsyncHandler.ts +++ b/test/util/StaticAsyncHandler.ts @@ -1,4 +1,4 @@ -import { AsyncHandler } from '../../src/util/AsyncHandler'; +import { AsyncHandler } from '../../src/util/handlers/AsyncHandler'; export class StaticAsyncHandler extends AsyncHandler { private readonly canHandleStatic: boolean;