Skip to content

Commit

Permalink
Initial renaming and refactor (removing the pods)
Browse files Browse the repository at this point in the history
  • Loading branch information
maartyman committed Sep 18, 2024
1 parent 12da888 commit e465587
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 238 deletions.
10 changes: 3 additions & 7 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,17 @@
"@type": "NativeFetch"
},
{
"@id": "urn:solid-aggregator:pod",
"@type": "PodCss"
},
{
"@id": "urn:solid-aggregator:aggregator-service-aggregation",
"@id": "urn:solid-aggregator:service-aggregation",
"@type": "ServiceAggregation",
"fetch": { "@id": "urn:solid-aggregator:fetch" },
"pod": { "@id": "urn:solid-aggregator:pod" }
"endpoint": { "@id": "urn:solid-aggregator:endpoint" }
},
{
"@id": "urn:solid-aggregator:service-registry",
"@type": "ServiceRegistryHardcodedTestOnly",
"costQueueFactory": { "@id": "urn:solid-aggregator:cost-queue-factory" },
"aggregatorServices": [
{ "@id": "urn:solid-aggregator:aggregator-service-aggregation" }
{ "@id": "urn:solid-aggregator:service-aggregation" }
]
}
]
Expand Down
29 changes: 0 additions & 29 deletions lib/core/AsyncConstructor.ts

This file was deleted.

32 changes: 29 additions & 3 deletions lib/endpoint/Endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { IncomingMessage, Server, ServerResponse } from 'node:http';
import { createServer } from 'node:http';
import { v4 as uuidv4 } from 'uuid';
import type { IServiceRegistry } from '../service-registry/IServiceRegistry';

export class Endpoint {
public readonly serviceRegistry: IServiceRegistry;
public readonly endpointHandlers: IEndpointHandler[];
private readonly httpServer: Server;
private readonly endpoints =
new Map<string, (request: IncomingMessage, response: ServerResponse) => void>();

public constructor(serviceRegistry: IServiceRegistry, endpointHandlers: IEndpointHandler[]) {
this.serviceRegistry = serviceRegistry;
Expand All @@ -14,17 +17,34 @@ export class Endpoint {
}

public async start(): Promise<void> {
await this.serviceRegistry.initializeServices();

// TODO [2024-10-01]: Make port configurable
this.httpServer.listen(1612);
this.httpServer.on('request', (request: IncomingMessage, response: ServerResponse): void => {
this.handleRequest(request, response)
.catch((error): void => {
// TODO [2024-03-01]: implement proper logging
// TODO [2024-10-01]: implement proper logging
// eslint-disable-next-line no-console
console.error(error);
});
});
// TODO [2024-10-01]: Make port configurable
this.httpServer.listen(1613);
this.httpServer.on('request', (request: IncomingMessage, response: ServerResponse): void => {
const endpointLocation = request.url?.split('/')[1];
if (endpointLocation === undefined) {
response.statusCode = 404;
response.end();
return;
}
const endpoint = this.endpoints.get(endpointLocation);
if (endpoint === undefined) {
response.statusCode = 404;
response.end();
return;
}
endpoint(request, response);
response.end();
});
}

private async handleRequest(request: IncomingMessage, response: ServerResponse): Promise<void> {
Expand Down Expand Up @@ -54,6 +74,12 @@ export class Endpoint {
});
});
}

public newServiceEndpoint(handleFunction: (request: IncomingMessage, response: ServerResponse) => void): string {
const endpointLocation = uuidv4();
this.endpoints.set(endpointLocation, handleFunction);
return endpointLocation;
}
}

export interface IEndpointHandler {
Expand Down
4 changes: 2 additions & 2 deletions lib/endpoint/test/Endpoint-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Endpoint', (): void => {
'testDescription',
]) as any,
run: jest.fn(
async(): Promise<undefined> => undefined,
async(): Promise<void> => {},
),
};
});
Expand Down Expand Up @@ -191,7 +191,7 @@ describe('Endpoint', (): void => {
'testDescription',
]) as any,
run: jest.fn(
async(): Promise<undefined> => undefined,
async(): Promise<void> => {},
),
};

Expand Down
4 changes: 2 additions & 2 deletions lib/endpoint/test/EndpointHandlerServiceDescription-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('EndpointHandlerServiceDescription', (): void => {
initializeServices: jest.fn().mockResolvedValue(undefined),
descriptions,
run: jest.fn(
async(): Promise<undefined> => undefined,
async(): Promise<void> => {},
),
};

Expand All @@ -39,7 +39,7 @@ describe('EndpointHandlerServiceDescription', (): void => {
initializeServices: jest.fn().mockResolvedValue(undefined),
descriptions,
run: jest.fn(
async(): Promise<undefined> => undefined,
async(): Promise<void> => {},
),
};

Expand Down
5 changes: 0 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from './core/AsyncConstructor';

export * from './cost-queue/ICostQueue';
export * from './cost-queue/CostQueueTime';

Expand All @@ -11,9 +9,6 @@ export * from './fetch/NativeFetch';

export * from './init/AppRunner';

export * from './pod/IPod';
export * from './pod/PodCss';

export * from './service/IService';
export * from './service/ServiceEmpty';
export * from './service/ServiceAggregation';
Expand Down
7 changes: 0 additions & 7 deletions lib/pod/IPod.ts

This file was deleted.

64 changes: 0 additions & 64 deletions lib/pod/PodCss.ts

This file was deleted.

1 change: 0 additions & 1 deletion lib/pod/assets/.gitignore

This file was deleted.

37 changes: 0 additions & 37 deletions lib/pod/assets/css-config.json

This file was deleted.

9 changes: 0 additions & 9 deletions lib/pod/assets/seed.json

This file was deleted.

5 changes: 2 additions & 3 deletions lib/service-registry/IServiceRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { IOperation, IOperationResult } from '../service/IService';
import type { IOperation } from '../service/IService';

export interface IServiceRegistry {
get descriptions(): string[];
initializeServices: () => Promise<void>;
run: (operation: IOperation) => Promise<IOperationResult | undefined>;
run: (operation: IOperation) => Promise<void>;
}
52 changes: 52 additions & 0 deletions lib/service-registry/ServiceRegistryHardcoded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { IOperation, IService } from '../service/IService';
import type { ICostQueueFactory } from '../cost-queue/ICostQueue';
import type { IServiceRegistry } from './IServiceRegistry';

export class ServiceRegistryHardcodedTestOnly implements IServiceRegistry {
public readonly costQueueFactory: ICostQueueFactory;
public readonly services: IService[];

public constructor(
aggregatorServices: { service: IService; operations: IOperation[] }[],
costQueueFactory: ICostQueueFactory,
) {
this.services = aggregatorServices.map((aggregatorService): IService => aggregatorService.service);
this.costQueueFactory = costQueueFactory;
for (const aggregatorService of aggregatorServices) {
for (const operation of aggregatorService.operations) {
aggregatorService.service.test(operation).then(
(testResult): void => {
if (!testResult.runnable) {
throw new Error('Operation not runnable');
}
aggregatorService.service.run(operation).catch(
(error): void => {
throw new Error(
`Aggregator Service:\n${aggregatorService.service.description.toString()}\n` +
`failed to 'run' on operation:\n${JSON.stringify(operation)}\nwith error:\n${error}`,
);
},
);
},
).catch((error): void => {
throw new Error(
`Aggregator Service:\n${aggregatorService.service.description.toString()}\n` +
`failed to 'test' on operation:\n${JSON.stringify(operation)}\nwith error:\n${error}`,
);
});
}
}
}

public async run(operation: IOperation): Promise<void> {
throw new Error(`Service registry not changeable ${JSON.stringify(operation)}.`);
}

public get descriptions(): string[] {
const result = [];
for (const service of this.services) {
result.push(service.description.toString());
}
return result;
}
}
17 changes: 3 additions & 14 deletions lib/service-registry/ServiceRegistryHardcodedTestOnly.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IOperation, IOperationResult, IService } from '../service/IService';
import type { IOperation, IService } from '../service/IService';
import type { ICostQueueFactory } from '../cost-queue/ICostQueue';
import type { IServiceRegistry } from './IServiceRegistry';

Expand All @@ -11,16 +11,7 @@ export class ServiceRegistryHardcodedTestOnly implements IServiceRegistry {
this.costQueueFactory = costQueueFactory;
}

public async initializeServices(): Promise<void> {
await Promise.all(
this.services.map(
async(aggregatorService): Promise<void> => new Promise<void>((resolve): void =>
aggregatorService.subscribeInitialized(resolve)),
),
);
}

public async run(operation: IOperation): Promise<IOperationResult | undefined> {
public async run(operation: IOperation): Promise<void> {
const costQueue = this.costQueueFactory.create();

await Promise.all(this.services.map(
Expand All @@ -36,12 +27,10 @@ export class ServiceRegistryHardcodedTestOnly implements IServiceRegistry {
let operationResult = costQueue.pop();
while (operationResult === undefined || operationResult.operationResult === undefined) {
if (costQueue.length === 0) {
return undefined;
throw new Error('No aggregator services can handel operation.');
}
operationResult = costQueue.pop();
}

return operationResult.operationResult;
}

public get descriptions(): string[] {
Expand Down
Loading

0 comments on commit e465587

Please sign in to comment.