Skip to content

Commit ca547ae

Browse files
committed
#608: moved loading segments, health checks and middleware to their own packages
1 parent 4328bc9 commit ca547ae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+576
-133
lines changed
File renamed without changes.
File renamed without changes.

examples/construction/package.json renamed to examples/resources/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "jitar-construction-example",
2+
"name": "jitar-resources-example",
33
"private": true,
44
"type": "module",
55
"scripts": {

packages/build/src/BuildManager.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import type { RuntimeConfiguration } from '@jitar/configuration';
33
import { Logger, LogLevel } from '@jitar/logging';
4-
import { Files, FileManager } from '@jitar/sourcing';
4+
import { Files, LocalFileManager } from '@jitar/sourcing';
55

66
import { ApplicationReader } from './source';
77
import { ApplicationBuilder } from './target';
@@ -20,10 +20,10 @@ export default class BuildManager
2020
{
2121
this.#logger = new Logger(logLevel);
2222

23-
const sourceFileManager = new FileManager(configuration.source);
24-
const targetFileManager = new FileManager(configuration.target);
25-
const resourceFileManager = new FileManager(configuration.resources);
26-
const segmentFileManager = new FileManager(configuration.segments);
23+
const sourceFileManager = new LocalFileManager(configuration.source);
24+
const targetFileManager = new LocalFileManager(configuration.target);
25+
const resourceFileManager = new LocalFileManager(configuration.resources);
26+
const segmentFileManager = new LocalFileManager(configuration.segments);
2727

2828
this.#fileManager = new ProjectFileManager(sourceFileManager, targetFileManager, resourceFileManager, segmentFileManager);
2929

packages/cli/src/commands/StartServer.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ConfigurationManager, RuntimeConfiguration, ServerConfiguration } from
33
import { HttpRemoteBuilder, HttpServer } from '@jitar/http';
44
import { LogLevel, LogLevelParser } from '@jitar/logging';
55
import { ServerBuilder } from '@jitar/runtime';
6-
import { FileManager, SourcingManager } from '@jitar/sourcing';
6+
import { LocalSourcingManager } from '@jitar/sourcing';
77

88
import ArgumentProcessor from '../ArgumentProcessor';
99
import Command from '../Command';
@@ -49,8 +49,7 @@ export default class StartServer implements Command
4949
{
5050
const [, , port] = serverConfiguration.url.split(':');
5151

52-
const fileManager = new FileManager(runtimeConfiguration.target);
53-
const sourcingManager = new SourcingManager(fileManager);
52+
const sourcingManager = new LocalSourcingManager(runtimeConfiguration.target);
5453
const remoteBuilder = new HttpRemoteBuilder();
5554
const serverBuilder = new ServerBuilder(sourcingManager, remoteBuilder);
5655

packages/configuration/src/ConfigurationManager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { Validator } from '@jitar/validation';
3-
import { FileManager } from '@jitar/sourcing';
3+
import { LocalFileManager } from '@jitar/sourcing';
44

55
import { EnvironmentConfigurator } from './environment';
66
import { RuntimeConfiguration, RuntimeConfigurationBuilder } from './runtime';
@@ -18,7 +18,7 @@ export default class ConfigurationManager
1818

1919
constructor(rootPath: string = DEFAULT_ROOT_PATH)
2020
{
21-
const fileManager = new FileManager(rootPath);
21+
const fileManager = new LocalFileManager(rootPath);
2222
const reader = new ConfigurationReader(fileManager);
2323
const validator = new Validator();
2424

packages/execution/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"clean": "rm -rf dist"
1717
},
1818
"dependencies": {
19-
"@jitar/errors": "*"
19+
"@jitar/errors": "*",
20+
"@jitar/sourcing": "*"
2021
}
2122
}

packages/execution/src/ExecutionManager.ts

+38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
import type { ModuleImporter } from '@jitar/sourcing';
3+
24
import StatusCodes from './definitions/StatusCodes';
35
import RunModes from './definitions/RunModes';
46

@@ -22,10 +24,36 @@ import ErrorConverter from './utils/ErrorConverter';
2224

2325
export default class ExecutionManager implements Runner
2426
{
27+
readonly #moduleImporter: ModuleImporter;
28+
readonly #segmentFiles: string[];
29+
2530
readonly #argumentConstructor: ArgumentConstructor = new ArgumentConstructor();
2631
readonly #errorConverter: ErrorConverter = new ErrorConverter();
2732
readonly #application: Application = new Application();
2833

34+
constructor(moduleImporter: ModuleImporter, segmentFiles: string[] = [])
35+
{
36+
this.#moduleImporter = moduleImporter;
37+
this.#segmentFiles = segmentFiles;
38+
}
39+
40+
async start(): Promise<void>
41+
{
42+
return this.#loadSegments();
43+
}
44+
45+
async stop(): Promise<void>
46+
{
47+
return this.#clearSegments();
48+
}
49+
50+
async loadSegment(filename: string): Promise<void>
51+
{
52+
const module = await this.#moduleImporter.import(filename);
53+
54+
this.addSegment(module.default as Segment);
55+
}
56+
2957
async addSegment(segment: Segment): Promise<void>
3058
{
3159
if ((segment instanceof Segment) === false)
@@ -85,6 +113,16 @@ export default class ExecutionManager implements Runner
85113
return this.#runImplementation(request, implementation, args);
86114
}
87115

116+
async #loadSegments(): Promise<void>
117+
{
118+
await Promise.all(this.#segmentFiles.map(filename => this.loadSegment(filename)));
119+
}
120+
121+
#clearSegments(): void
122+
{
123+
this.#application.clearSegments();
124+
}
125+
88126
#getImplementation(fqn: string, version: Version): Implementation
89127
{
90128
const procedure = this.#application.getProcedure(fqn);

packages/execution/src/models/Application.ts

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export default class Application
1212
this.#segments.set(segment.id, segment);
1313
}
1414

15+
clearSegments(): void
16+
{
17+
this.#segments.clear();
18+
}
19+
1520
getClassNames(): string[]
1621
{
1722
const names = new Set<string>();

packages/health/src/HealthManager.ts

+38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
import type { ModuleImporter } from '@jitar/sourcing';
3+
24
import InvalidHealthCheck from './errors/InvalidHealthCheck';
35
import type HealthCheck from './interfaces/HealthCheck';
46

@@ -10,8 +12,34 @@ type HealthCheckResult =
1012

1113
export default class HealthManager
1214
{
15+
readonly #moduleImporter: ModuleImporter;
16+
readonly #healthCheckFiles: string[];
17+
1318
readonly #healthChecks = new Map<string, HealthCheck>();
1419

20+
constructor(moduleImporter: ModuleImporter, healthCheckFiles: string[] = [])
21+
{
22+
this.#moduleImporter = moduleImporter;
23+
this.#healthCheckFiles = healthCheckFiles;
24+
}
25+
26+
async start(): Promise<void>
27+
{
28+
return this.#loadHealthChecks();
29+
}
30+
31+
async stop(): Promise<void>
32+
{
33+
return this.#clearHealthChecks();
34+
}
35+
36+
async loadHealthCheck(filename: string): Promise<void>
37+
{
38+
const module = await this.#moduleImporter.import(filename);
39+
40+
this.addHealthCheck(module.default as HealthCheck);
41+
}
42+
1543
addHealthCheck(healthCheck: HealthCheck): void
1644
{
1745
if (healthCheck.isHealthy === undefined)
@@ -63,6 +91,16 @@ export default class HealthManager
6391
.then(() => healthChecks);
6492
}
6593

94+
async #loadHealthChecks(): Promise<void>
95+
{
96+
await Promise.all(this.#healthCheckFiles.map(filename => this.loadHealthCheck(filename)));
97+
}
98+
99+
#clearHealthChecks(): void
100+
{
101+
this.#healthChecks.clear();
102+
}
103+
66104
#handleHealthCheckResult(result: PromiseSettledResult<HealthCheckResult>, healthChecks: Map<string, boolean>): void
67105
{
68106
if (result.status !== 'fulfilled')

packages/middleware/src/MiddlewareManager.ts

+37
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11

22
import { Request, Response, StatusCodes } from '@jitar/execution';
3+
import type { ModuleImporter } from '@jitar/sourcing';
34

45
import InvalidMiddleware from './errors/InvalidMiddleware';
56
import type Middleware from './interfaces/Middleware';
67
import type NextHandler from './types/NextHandler';
78

89
export default class MiddlewareManager
910
{
11+
readonly #moduleImporter: ModuleImporter;
12+
readonly #middlewareFiles: string[];
13+
1014
#middlewares: Middleware[] = [];
1115

16+
constructor(moduleImporter: ModuleImporter, middlewareFiles: string[] = [])
17+
{
18+
this.#moduleImporter = moduleImporter;
19+
this.#middlewareFiles = middlewareFiles;
20+
}
21+
22+
async start(): Promise<void>
23+
{
24+
return this.#loadMiddlewares();
25+
}
26+
27+
async stop(): Promise<void>
28+
{
29+
return this.#clearMiddlewares();
30+
}
31+
32+
async loadMiddleware(filename:string): Promise<void>
33+
{
34+
const module = await this.#moduleImporter.import(filename);
35+
36+
this.addMiddleware(module.default as Middleware);
37+
}
38+
1239
addMiddleware(middleware: Middleware): void
1340
{
1441
if (middleware?.handle === undefined)
@@ -38,6 +65,16 @@ export default class MiddlewareManager
3865
return startHandler();
3966
}
4067

68+
async #loadMiddlewares(): Promise<void>
69+
{
70+
await Promise.all(this.#middlewareFiles.map(filename => this.loadMiddleware(filename)));
71+
}
72+
73+
#clearMiddlewares(): void
74+
{
75+
this.#middlewares.slice();
76+
}
77+
4178
#getNextHandler(request: Request, index: number): NextHandler
4279
{
4380
const next = this.#middlewares[index];

packages/plugin-vite/src/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ export default function viteJitar(pluginConfig: PluginConfig): PluginOption
198198

199199
const remoteBuilder = 'const remoteBuilder = new HttpRemoteBuilder();';
200200
const clientBuilder = 'const clientBuilder = new ClientBuilder(remoteBuilder);';
201-
const build = 'clientBuilder.build({remoteUrl, segments, middleware});';
202-
const client = [remoteBuilder, clientBuilder, build].join('\n');
201+
const build = 'const client = clientBuilder.build({remoteUrl, segments, middleware});';
202+
const start = 'client.start();';
203+
const client = [remoteBuilder, clientBuilder, build, start].join('\n');
203204

204205
const exports = `export * from "${JITAR_CLIENT_ID}";`;
205206

packages/runtime/src/client/Client.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,18 @@ export default class Client extends Runtime
3333
});
3434

3535
this.#middlewareManager = configuration.middlewareManager;
36-
37-
const procedureRunner = new ProcedureRunner(this.#worker);
38-
this.#middlewareManager.addMiddleware(procedureRunner);
3936
}
4037

4138
get worker() { return this.#worker; }
4239

4340
start(): Promise<void>
4441
{
45-
return this.#worker.start();
42+
return this.#setUp();
4643
}
4744

4845
stop(): Promise<void>
4946
{
50-
return this.#worker.stop();
47+
return this.#tearDown();
5148
}
5249

5350
getTrustKey(): string | undefined
@@ -64,4 +61,25 @@ export default class Client extends Runtime
6461
{
6562
return this.#middlewareManager.handle(request);
6663
}
64+
65+
async #setUp(): Promise<void>
66+
{
67+
await Promise.all(
68+
[
69+
this.#worker.start(),
70+
this.#middlewareManager.start()
71+
]);
72+
73+
const procedureRunner = new ProcedureRunner(this.#worker);
74+
this.#middlewareManager.addMiddleware(procedureRunner);
75+
}
76+
77+
async #tearDown(): Promise<void>
78+
{
79+
await Promise.all(
80+
[
81+
this.#middlewareManager.stop(),
82+
this.#worker.stop()
83+
]);
84+
}
6785
}

packages/runtime/src/client/ClientBuilder.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { Segment, ExecutionManager } from '@jitar/execution';
33
import { RemoteBuilder } from '@jitar/services';
44
import { Middleware, MiddlewareManager } from '@jitar/middleware';
5+
import { RemoteSourcingManager } from '@jitar/sourcing';
56

67
import Client from './Client';
78

@@ -24,25 +25,29 @@ export default class ClientBuilder
2425
build(configuration: ClientConfiguration): Client
2526
{
2627
const remoteUrl = configuration.remoteUrl;
28+
const middleware = configuration.middleware;
29+
const segments = configuration.segments;
30+
2731
const remote = this.#remoteBuilder.build(remoteUrl);
28-
const middlewareManager = this.#buildMiddlewareManager(configuration.middleware ?? []);
29-
const executionManager = this.#buildExecutionManager(configuration.segments ?? []);
32+
const sourcingManager = new RemoteSourcingManager(remoteUrl);
33+
const middlewareManager = this.#buildMiddlewareManager(sourcingManager, middleware);
34+
const executionManager = this.#buildExecutionManager(sourcingManager, segments);
3035

3136
return new Client({ remoteUrl, remote, middlewareManager, executionManager });
3237
}
3338

34-
#buildMiddlewareManager(middleware: Middleware[]): MiddlewareManager
39+
#buildMiddlewareManager(sourcingManager: RemoteSourcingManager, middleware: Middleware[] = []): MiddlewareManager
3540
{
36-
const manager = new MiddlewareManager();
41+
const manager = new MiddlewareManager(sourcingManager);
3742

3843
middleware.forEach(middleware => manager.addMiddleware(middleware));
3944

4045
return manager;
4146
}
4247

43-
#buildExecutionManager(segments: Segment[]): ExecutionManager
48+
#buildExecutionManager(sourcingManager: RemoteSourcingManager, segments: Segment[] = []): ExecutionManager
4449
{
45-
const manager = new ExecutionManager();
50+
const manager = new ExecutionManager(sourcingManager);
4651

4752
segments.forEach(segment => manager.addSegment(segment));
4853

0 commit comments

Comments
 (0)