navi-hey 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/bin/navi.js +9 -0
  2. package/lib/exceptions/AppError.js +13 -0
  3. package/lib/exceptions/ClientNotFound.js +16 -0
  4. package/lib/exceptions/ConfigurationFileNotFound.js +10 -0
  5. package/lib/exceptions/ConfigurationFileNotProvided.js +10 -0
  6. package/lib/exceptions/InvalidResponseBody.js +19 -0
  7. package/lib/exceptions/ItemNotFound.js +16 -0
  8. package/lib/exceptions/LockedByOtherWorker.js +14 -0
  9. package/lib/exceptions/MissingActionResource.js +13 -0
  10. package/lib/exceptions/MissingClientsConfig.js +14 -0
  11. package/lib/exceptions/MissingMappingVariable.js +17 -0
  12. package/lib/exceptions/MissingResourceConfig.js +14 -0
  13. package/lib/exceptions/MissingTopLevelConfgKey.js +15 -0
  14. package/lib/exceptions/NullResponse.js +13 -0
  15. package/lib/exceptions/RequestFailed.js +16 -0
  16. package/lib/exceptions/ResourceNotFound.js +16 -0
  17. package/lib/factories/Factory.js +79 -0
  18. package/lib/factories/JobFactory.js +82 -0
  19. package/lib/factories/WorkerFactory.js +35 -0
  20. package/lib/models/ActionEnqueuer.js +34 -0
  21. package/lib/models/ActionProcessingJob.js +51 -0
  22. package/lib/models/ActionsEnqueuer.js +42 -0
  23. package/lib/models/Config.js +68 -0
  24. package/lib/models/Job.js +87 -0
  25. package/lib/models/ParametersMapper.js +43 -0
  26. package/lib/models/PathResolver.js +77 -0
  27. package/lib/models/PathSegmentTraverser.js +82 -0
  28. package/lib/models/Resource.js +47 -0
  29. package/lib/models/ResourceRequest.js +80 -0
  30. package/lib/models/ResourceRequestAction.js +73 -0
  31. package/lib/models/ResourceRequestJob.js +61 -0
  32. package/lib/models/ResponseParser.js +36 -0
  33. package/lib/models/ResponseWrapper.js +86 -0
  34. package/lib/models/WebConfig.js +15 -0
  35. package/lib/models/Worker.js +57 -0
  36. package/lib/models/WorkersConfig.js +18 -0
  37. package/lib/registry/ClientRegistry.js +92 -0
  38. package/lib/registry/JobRegistry.js +125 -0
  39. package/lib/registry/JobRegistryInstance.js +149 -0
  40. package/lib/registry/NamedRegistry.js +73 -0
  41. package/lib/registry/ResourceRegistry.js +91 -0
  42. package/lib/registry/WorkersRegistry.js +106 -0
  43. package/lib/registry/WorkersRegistryInstance.js +139 -0
  44. package/lib/server/RequestHandler.js +15 -0
  45. package/lib/server/RouteRegister.js +27 -0
  46. package/lib/server/Router.js +44 -0
  47. package/lib/server/StatsRequestHandler.js +31 -0
  48. package/lib/server/WebServer.js +45 -0
  49. package/lib/services/Application.js +113 -0
  50. package/lib/services/ArgumentsParser.js +36 -0
  51. package/lib/services/Client.js +108 -0
  52. package/lib/services/ConfigLoader.js +75 -0
  53. package/lib/services/ConfigParser.js +131 -0
  54. package/lib/services/Engine.js +67 -0
  55. package/lib/services/WorkersAllocator.js +74 -0
  56. package/lib/utils/EnvResolver.js +53 -0
  57. package/lib/utils/PromiseAggregator.js +37 -0
  58. package/lib/utils/ResourceRequestCollector.js +35 -0
  59. package/lib/utils/collections/Collection.js +17 -0
  60. package/lib/utils/collections/IdentifyableCollection.js +86 -0
  61. package/lib/utils/collections/Queue.js +45 -0
  62. package/lib/utils/collections/SortedArrayMerger.js +84 -0
  63. package/lib/utils/collections/SortedArraySearcher.js +101 -0
  64. package/lib/utils/collections/SortedCollection.js +169 -0
  65. package/lib/utils/generators/IdGenerator.js +79 -0
  66. package/lib/utils/generators/IncrementalIdGenerator.js +25 -0
  67. package/lib/utils/generators/UUidGenerator.js +49 -0
  68. package/lib/utils/index.js +2 -0
  69. package/lib/utils/logging/BaseLogger.js +93 -0
  70. package/lib/utils/logging/BufferedLogger.js +91 -0
  71. package/lib/utils/logging/ConsoleLogger.js +19 -0
  72. package/lib/utils/logging/Log.js +70 -0
  73. package/lib/utils/logging/LogBuffer.js +101 -0
  74. package/lib/utils/logging/LogFactory.js +32 -0
  75. package/lib/utils/logging/Logger.js +134 -0
  76. package/lib/utils/logging/LoggerGroup.js +100 -0
  77. package/package.json +78 -0
package/bin/navi.js ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { Application } from '../lib/services/Application.js';
4
+ import { ArgumentsParser } from '../lib/services/ArgumentsParser.js';
5
+
6
+ const { config } = ArgumentsParser.parse(process.argv.slice(2));
7
+ const app = new Application();
8
+ app.loadConfig(config);
9
+ app.run();
@@ -0,0 +1,13 @@
1
+ /**
2
+ * AppError is the base class for all custom application errors.
3
+ * It automatically assigns the error name from the subclass name.
4
+ * @author darthjee
5
+ */
6
+ class AppError extends Error {
7
+ constructor(message) {
8
+ super(message);
9
+ this.name = this.constructor.name;
10
+ }
11
+ }
12
+
13
+ export { AppError };
@@ -0,0 +1,16 @@
1
+ import { ItemNotFound } from './ItemNotFound.js';
2
+
3
+ /**
4
+ * ClientNotFound is a custom error class thrown when a requested client
5
+ * cannot be found in config.
6
+ * @author darthjee
7
+ */
8
+ class ClientNotFound extends ItemNotFound {
9
+ constructor(clientName) {
10
+ super(clientName, { itemType: 'Client' });
11
+
12
+ this.clientName = clientName;
13
+ }
14
+ }
15
+
16
+ export { ClientNotFound };
@@ -0,0 +1,10 @@
1
+ import { AppError } from './AppError.js';
2
+
3
+ class ConfigurationFileNotFound extends AppError {
4
+ constructor(file) {
5
+ super(`Configuration file not found: ${file}`);
6
+ this.name = 'ConfigurationFileNotFound';
7
+ }
8
+ }
9
+
10
+ export { ConfigurationFileNotFound };
@@ -0,0 +1,10 @@
1
+ import { AppError } from './AppError.js';
2
+
3
+ class ConfigurationFileNotProvided extends AppError {
4
+ constructor() {
5
+ super('Configuration file not provided. Please specify the configuration file path.');
6
+ this.name = 'ConfigurationFileNotProvided';
7
+ }
8
+ }
9
+
10
+ export { ConfigurationFileNotProvided };
@@ -0,0 +1,19 @@
1
+ import { AppError } from './AppError.js';
2
+
3
+ /**
4
+ * Thrown when a response body cannot be parsed as JSON.
5
+ * @author darthjee
6
+ */
7
+ class InvalidResponseBody extends AppError {
8
+ /**
9
+ * @param {string} raw The raw response body string that failed to parse.
10
+ * @param {Error} cause The original parse error.
11
+ */
12
+ constructor(raw, cause) {
13
+ super(`Invalid response body: ${cause.message}`);
14
+ this.raw = raw;
15
+ this.cause = cause;
16
+ }
17
+ }
18
+
19
+ export { InvalidResponseBody };
@@ -0,0 +1,16 @@
1
+ import { AppError } from './AppError.js';
2
+
3
+ /**
4
+ * ItemNotFound is a custom error class thrown when a requested item
5
+ * cannot be found.
6
+ * @author darthjee
7
+ */
8
+ class ItemNotFound extends AppError {
9
+ constructor(itemName, { itemType = 'Item' } = {}) {
10
+ super(`${itemType} "${itemName}" not found.`);
11
+ this.itemName = itemName;
12
+ this.itemType = itemType;
13
+ }
14
+ }
15
+
16
+ export { ItemNotFound };
@@ -0,0 +1,14 @@
1
+ import { AppError } from './AppError.js';
2
+
3
+ /**
4
+ * LockedByOtherWorker is a custom error class thrown when a worker attempts
5
+ * to lock a JobRegistry that is already locked by another worker.
6
+ * @author darthjee
7
+ */
8
+ class LockedByOtherWorker extends AppError {
9
+ constructor(message = 'Registry is locked by another worker') {
10
+ super(message);
11
+ }
12
+ }
13
+
14
+ export { LockedByOtherWorker };
@@ -0,0 +1,13 @@
1
+ import { AppError } from './AppError.js';
2
+
3
+ /**
4
+ * Thrown when an action config entry is missing the required "resource" field.
5
+ * @author darthjee
6
+ */
7
+ class MissingActionResource extends AppError {
8
+ constructor() {
9
+ super('Action is missing the required "resource" field');
10
+ }
11
+ }
12
+
13
+ export { MissingActionResource };
@@ -0,0 +1,14 @@
1
+ import { MissingTopLevelConfigKey } from './MissingTopLevelConfgKey.js';
2
+
3
+ /**
4
+ * ItemNotFound is a custom error class thrown when a requested item
5
+ * cannot be found.
6
+ * @author darthjee
7
+ */
8
+ class MissingClientsConfig extends MissingTopLevelConfigKey {
9
+ constructor() {
10
+ super('clients');
11
+ }
12
+ }
13
+
14
+ export { MissingClientsConfig };
@@ -0,0 +1,17 @@
1
+ import { AppError } from './AppError.js';
2
+
3
+ /**
4
+ * Thrown when a parameters path expression cannot be resolved against the response wrapper.
5
+ * @author darthjee
6
+ */
7
+ class MissingMappingVariable extends AppError {
8
+ /**
9
+ * @param {string} variable The name of the missing source field.
10
+ */
11
+ constructor(variable) {
12
+ super(`Missing variable in response: ${variable}`);
13
+ this.variable = variable;
14
+ }
15
+ }
16
+
17
+ export { MissingMappingVariable };
@@ -0,0 +1,14 @@
1
+ import { MissingTopLevelConfigKey } from './MissingTopLevelConfgKey.js';
2
+
3
+ /**
4
+ * ItemNotFound is a custom error class thrown when a requested item
5
+ * cannot be found.
6
+ * @author darthjee
7
+ */
8
+ class MissingResourceConfig extends MissingTopLevelConfigKey {
9
+ constructor() {
10
+ super('resources');
11
+ }
12
+ }
13
+
14
+ export { MissingResourceConfig };
@@ -0,0 +1,15 @@
1
+ import { AppError } from './AppError.js';
2
+
3
+ /**
4
+ * ItemNotFound is a custom error class thrown when a requested item
5
+ * cannot be found.
6
+ * @author darthjee
7
+ */
8
+ class MissingTopLevelConfigKey extends AppError {
9
+ constructor(key) {
10
+ super(`Invalid config file: expected a top-level "${key}" key.`);
11
+ this.key = key;
12
+ }
13
+ }
14
+
15
+ export { MissingTopLevelConfigKey };
@@ -0,0 +1,13 @@
1
+ import { AppError } from './AppError.js';
2
+
3
+ /**
4
+ * Thrown when a parsed response body is null.
5
+ * @author darthjee
6
+ */
7
+ class NullResponse extends AppError {
8
+ constructor() {
9
+ super('Response body is null');
10
+ }
11
+ }
12
+
13
+ export { NullResponse };
@@ -0,0 +1,16 @@
1
+ import { AppError } from './AppError.js';
2
+
3
+ /**
4
+ * RequestFailed is a custom error class that represents a failed HTTP request.
5
+ * It includes the status code and URL of the failed request for better error handling and debugging.
6
+ * @author darthjee
7
+ */
8
+ class RequestFailed extends AppError {
9
+ constructor(statusCode, url, message = 'Request failed') {
10
+ super(`${message}: ${statusCode} for ${url}`);
11
+ this.statusCode = statusCode;
12
+ this.url = url;
13
+ }
14
+ }
15
+
16
+ export { RequestFailed };
@@ -0,0 +1,16 @@
1
+ import { ItemNotFound } from './ItemNotFound.js';
2
+
3
+ /**
4
+ * ResourceNotFound is a custom error class thrown when a requested resource
5
+ * cannot be found in config.
6
+ * @author darthjee
7
+ */
8
+ class ResourceNotFound extends ItemNotFound {
9
+ constructor(resourceName) {
10
+ super(resourceName, { itemType: 'Resource' });
11
+
12
+ this.resourceName = resourceName;
13
+ }
14
+ }
15
+
16
+ export { ResourceNotFound };
@@ -0,0 +1,79 @@
1
+ /**
2
+ * A flexible factory class that can be configured with a class and an attributes generator.
3
+ *
4
+ * The factory builds objects using the provided class,
5
+ * and can generate attributes using the attributes generator.
6
+ * @author darthjee
7
+ */
8
+ class Factory {
9
+ #klass;
10
+ #attributesGenerator;
11
+
12
+ /**
13
+ * Creates a new Factory instance.
14
+ * @param {object} params - The parameters for configuring the factory.
15
+ * @param {Function|null} params.klass - A class used to instantiate objects.
16
+ * @param {object|null} params.attributesGenerator - An object with a `generate` method that produces attributes before building.
17
+ * @example <caption>Default factory (builds plain objects)</caption>
18
+ * const factory = new Factory();
19
+ * factory.build(); // => {}
20
+ * @example <caption>Factory with a class</caption>
21
+ * const factory = new Factory({ klass: MyModel });
22
+ * factory.build({ name: 'foo' }); // => new MyModel({ name: 'foo' })
23
+ * @example <caption>Factory with an attributes generator</caption>
24
+ * const idGenerator = new IdGenerator();
25
+ * const factory = new Factory({ klass: MyModel, attributesGenerator: idGenerator });
26
+ * factory.build({ name: 'foo' }); // => new MyModel({ id: '<uuid>', name: 'foo' })
27
+ */
28
+ constructor({ klass = null, attributesGenerator = null } = {}) {
29
+ this.#klass = klass;
30
+ this.#attributesGenerator = attributesGenerator;
31
+ }
32
+
33
+ /**
34
+ * Builds an object using the configured class and optional attributes generator.
35
+ * @param {...*} args - Arguments passed to the attributes generator or directly to the class constructor.
36
+ * @returns {*} The built object.
37
+ * @example <caption>Building without arguments</caption>
38
+ * const factory = new Factory({ klass: MyModel });
39
+ * factory.build(); // => new MyModel()
40
+ * @example <caption>Building with arguments</caption>
41
+ * const factory = new Factory({ klass: MyModel });
42
+ * factory.build({ value: 'custom' }); // => new MyModel({ value: 'custom' })
43
+ * @example <caption>Building with an attributes generator that enriches arguments</caption>
44
+ * const idGenerator = new IdGenerator();
45
+ * const factory = new Factory({ klass: MyModel, attributesGenerator: idGenerator });
46
+ * factory.build({ name: 'foo' }); // => new MyModel({ id: '<uuid>', name: 'foo' })
47
+ */
48
+ build(...args) {
49
+ const attributes = this.#generateAttributes(...args);
50
+ return this.#buildInstance(...attributes);
51
+ }
52
+
53
+ /**
54
+ * Builds an instance using the configured class or returns a plain object.
55
+ * @param {...*} args - Arguments to pass to the class constructor.
56
+ * @returns {*} The built instance.
57
+ */
58
+ #buildInstance(...args) {
59
+ if (this.#klass) {
60
+ return new this.#klass(...args);
61
+ }
62
+ return {};
63
+ }
64
+
65
+ /**
66
+ * Generates attributes by delegating to the attributes generator if one is configured.
67
+ * If no generator is configured, the original arguments are returned as-is.
68
+ * @param {...*} args - Arguments to pass to the attributes generator.
69
+ * @returns {Array} An array of generated attributes.
70
+ */
71
+ #generateAttributes(...args) {
72
+ if (this.#attributesGenerator) {
73
+ return [this.#attributesGenerator.generate(...args)];
74
+ }
75
+ return args;
76
+ }
77
+ }
78
+
79
+ export { Factory };
@@ -0,0 +1,82 @@
1
+ import { Factory } from './Factory.js';
2
+ import { ResourceRequestJob } from '../models/ResourceRequestJob.js';
3
+ import { IdGenerator } from '../utils/generators/IdGenerator.js';
4
+
5
+ /**
6
+ * JobFactory is responsible for creating Job instances with unique identifiers.
7
+ * Constructor-time attributes are merged with build-time params on every build call.
8
+ * @author darthjee
9
+ */
10
+ class JobFactory extends Factory {
11
+ static #factories = new Map();
12
+
13
+ #attributes;
14
+
15
+ /**
16
+ * Creates a new JobFactory instance.
17
+ * @param {object} options - Configuration options for the factory.
18
+ * @param {class} options.klass - The class to instantiate (default is ResourceRequestJob).
19
+ * @param {object} options.attributesGenerator - The generator for unique attributes (default is IdGenerator).
20
+ * @param {object} options.attributes - Attributes injected into every built instance (e.g. { clients }).
21
+ */
22
+ constructor({ klass = ResourceRequestJob, attributesGenerator = new IdGenerator(), attributes = {} } = {}) {
23
+ super({ klass, attributesGenerator });
24
+ this.#attributes = attributes;
25
+ }
26
+
27
+ /**
28
+ * Builds a new Job instance, merging constructor-level attributes with the given params.
29
+ * @param {object} params - The parameters for building a Job instance.
30
+ * @param {ResourceRequest} params.resourceRequest - The resource request associated with the Job.
31
+ * @param {object} params.parameters - Additional parameters for the Job.
32
+ * @returns {Job} A new Job instance.
33
+ * @override
34
+ */
35
+ build(params) {
36
+ return super.build({ ...this.#attributes, ...params });
37
+ }
38
+
39
+ /**
40
+ * Creates and registers a new JobFactory instance under the given name.
41
+ * @param {string} name - The name to register the factory under.
42
+ * @param {object} attributes - Options forwarded to the JobFactory constructor.
43
+ * @param {class} [attributes.klass] - The class to instantiate (default is ResourceRequestJob).
44
+ * @param {object} [attributes.attributesGenerator] - Generator for unique attributes (default is IdGenerator).
45
+ * @param {object} [attributes.attributes] - Attributes injected into every built instance (e.g. { clients }).
46
+ * @returns {JobFactory} The created and registered factory instance.
47
+ */
48
+ static build(name, attributes) {
49
+ const factory = new JobFactory(attributes);
50
+ JobFactory.registry(name, factory);
51
+ return factory;
52
+ }
53
+
54
+ /**
55
+ * Registers a factory instance under the given name.
56
+ * @param {string} name - The name to register the factory under.
57
+ * @param {JobFactory} factory - The factory instance to register.
58
+ * @returns {void}
59
+ */
60
+ static registry(name, factory) {
61
+ JobFactory.#factories.set(name, factory);
62
+ }
63
+
64
+ /**
65
+ * Retrieves a registered factory by name.
66
+ * @param {string} name - The name of the factory to retrieve.
67
+ * @returns {JobFactory|undefined} The registered factory, or undefined if not found.
68
+ */
69
+ static get(name) {
70
+ return JobFactory.#factories.get(name);
71
+ }
72
+
73
+ /**
74
+ * Removes all registered factories. Useful for test isolation.
75
+ * @returns {void}
76
+ */
77
+ static reset() {
78
+ JobFactory.#factories.clear();
79
+ }
80
+ }
81
+
82
+ export { JobFactory };
@@ -0,0 +1,35 @@
1
+ import { Factory } from './Factory.js';
2
+ import { Worker } from '../models/Worker.js';
3
+ import { IdGenerator } from '../utils/generators/IdGenerator.js';
4
+
5
+ /**
6
+ * WorkerFactory is responsible for creating Worker instances with unique identifiers.
7
+ * It uses the Factory base class to generate Worker instances with specified attributes.
8
+ * @author darthjee
9
+ */
10
+ class WorkerFactory extends Factory {
11
+ #registries;
12
+
13
+ /**
14
+ * Creates a new WorkerFactory instance.
15
+ * @param {object} options - Configuration options for the factory.
16
+ * @param {class} options.klass - The class to instantiate (default is Worker).
17
+ * @param {object} options.attributesGenerator - The generator for unique attributes (default is IdGenerator).
18
+ * @param {object} [options.jobRegistry] - The job registry to inject into each Worker.
19
+ * @param {object} [options.workersRegistry] - The workers registry to inject into each Worker.
20
+ */
21
+ constructor({ klass = Worker, attributesGenerator = new IdGenerator(), jobRegistry, workersRegistry } = {}) {
22
+ super({ klass, attributesGenerator });
23
+ this.#registries = { jobRegistry, workersRegistry };
24
+ }
25
+
26
+ /**
27
+ * Builds a new Worker instance.
28
+ * @returns {Worker} The created Worker instance.
29
+ */
30
+ build() {
31
+ return super.build({ ...this.#registries });
32
+ }
33
+ }
34
+
35
+ export { WorkerFactory };
@@ -0,0 +1,34 @@
1
+ import { JobRegistry as DefaultJobRegistry } from '../registry/JobRegistry.js';
2
+
3
+ /**
4
+ * ActionEnqueuer enqueues one ActionProcessingJob per item for a single Action.
5
+ * @author darthjee
6
+ */
7
+ class ActionEnqueuer {
8
+ #action;
9
+ #items;
10
+ #jobRegistry;
11
+
12
+ /**
13
+ * @param {ResourceRequestAction} action The action to enqueue for each item.
14
+ * @param {Array} items List of parsed response items.
15
+ * @param {object} [jobRegistry=JobRegistry] The job registry to enqueue jobs to.
16
+ */
17
+ constructor(action, items, jobRegistry = DefaultJobRegistry) {
18
+ this.#action = action;
19
+ this.#items = items;
20
+ this.#jobRegistry = jobRegistry;
21
+ }
22
+
23
+ /**
24
+ * Enqueues one ActionProcessingJob per item for the configured action.
25
+ * @returns {void}
26
+ */
27
+ enqueue() {
28
+ for (const item of this.#items) {
29
+ this.#jobRegistry.enqueue('Action', { action: this.#action, item });
30
+ }
31
+ }
32
+ }
33
+
34
+ export { ActionEnqueuer };
@@ -0,0 +1,51 @@
1
+ import { Job } from './Job.js';
2
+ import { Logger } from '../utils/logging/Logger.js';
3
+
4
+ /**
5
+ * ActionProcessingJob is a Job that processes a single Action for a given item.
6
+ * Unlike ResourceRequestJob, this job is exhausted after the first failure — it has no
7
+ * retry rights.
8
+ * @author darthjee
9
+ */
10
+ class ActionProcessingJob extends Job {
11
+ #action;
12
+ #item;
13
+
14
+ /**
15
+ * Creates a new ActionProcessingJob instance.
16
+ * @param {object} params - The parameters for creating an ActionProcessingJob instance.
17
+ * @param {string} params.id - The unique identifier for this job.
18
+ * @param {ResourceRequestAction} params.action - The action to execute.
19
+ * @param {object} params.item - The parsed response item to pass to the action.
20
+ */
21
+ constructor({ id, action, item }) {
22
+ super({ id });
23
+ this.#action = action;
24
+ this.#item = item;
25
+ }
26
+
27
+ /**
28
+ * Performs the action for the given item.
29
+ * @returns {Promise<void>}
30
+ */
31
+ async perform() {
32
+ Logger.info(`ActionProcessingJob #${this.id} performing`);
33
+ try {
34
+ this.lastError = undefined;
35
+ await this.#action.execute(this.#item);
36
+ } catch (error) {
37
+ this._fail(error);
38
+ }
39
+ }
40
+
41
+ /**
42
+ * Returns true after the first failed attempt — action jobs have no retry rights.
43
+ * @returns {boolean} True if the job has failed at least once.
44
+ * @override
45
+ */
46
+ exhausted() {
47
+ return this._attempts >= 1;
48
+ }
49
+ }
50
+
51
+ export { ActionProcessingJob };
@@ -0,0 +1,42 @@
1
+ import { ActionEnqueuer } from './ActionEnqueuer.js';
2
+ import { NullResponse } from '../exceptions/NullResponse.js';
3
+
4
+ /**
5
+ * ActionsEnqueuer enqueues one ActionProcessingJob per (item × action) pair.
6
+ *
7
+ * Delegates execution to the job queue instead of running actions inline.
8
+ * Each action is handled by a dedicated ActionEnqueuer that enqueues all items for that action.
9
+ * @author darthjee
10
+ */
11
+ class ActionsEnqueuer {
12
+ #actions;
13
+ #items;
14
+ #jobRegistry;
15
+
16
+ /**
17
+ * @param {Array} actions List of ResourceRequestAction instances.
18
+ * @param {Array} items List of ResponseWrapper instances (one per response item).
19
+ * @param {object} [jobRegistry] The job registry to enqueue jobs to. Defaults to global JobRegistry.
20
+ */
21
+ constructor(actions, items, jobRegistry) {
22
+ this.#actions = actions;
23
+ this.#items = items;
24
+ this.#jobRegistry = jobRegistry;
25
+ }
26
+
27
+ /**
28
+ * Enqueues one ActionProcessingJob per (action × item) pair,
29
+ * delegating per-action item enqueueing to ActionEnqueuer.
30
+ * @returns {void}
31
+ * @throws {NullResponse} If the items list is null.
32
+ */
33
+ enqueue() {
34
+ if (this.#items === null) throw new NullResponse();
35
+
36
+ for (const action of this.#actions) {
37
+ new ActionEnqueuer(action, this.#items, this.#jobRegistry).enqueue();
38
+ }
39
+ }
40
+ }
41
+
42
+ export { ActionsEnqueuer };
@@ -0,0 +1,68 @@
1
+ import { ClientRegistry } from '../registry/ClientRegistry.js';
2
+ import { ResourceRegistry } from '../registry/ResourceRegistry.js';
3
+ import { ConfigLoader } from '../services/ConfigLoader.js';
4
+
5
+ /**
6
+ * Config is a class that represents the configuration for the application.
7
+ * It includes the resources and clients that are to be included in the configuration.
8
+ * @author darthjee
9
+ */
10
+ class Config {
11
+ /**
12
+ * @param {object} params initialization parameters for the Config instance.
13
+ * @param {Record<string, Resource>} params.resources - A mapping of resource names to Resource instances.
14
+ * @param {Record<string, Client>} params.clients - A mapping of client names to Client instances.
15
+ * @param {WorkersConfig} params.workersConfig - The configuration for worker instances.
16
+ * @param {WebConfig|null} [params.webConfig] - Optional web server configuration.
17
+ */
18
+ constructor({ resources, clients, workersConfig, webConfig }) {
19
+ this.resourceRegistry = ResourceRegistry.build(resources);
20
+ this.clientRegistry = new ClientRegistry(clients);
21
+ this.workersConfig = workersConfig;
22
+ this.webConfig = webConfig ?? null;
23
+ }
24
+
25
+ /**
26
+ * Returns the resource identified by the given name.
27
+ *
28
+ * @param {string} name The name of the resource to retrieve.
29
+ * @returns {Resource} The matching Resource instance.
30
+ * @throws {Error} Throws when no resource with the given name exists.
31
+ */
32
+ getResource(name) {
33
+ return this.resourceRegistry.getItem(name);
34
+ }
35
+
36
+ /**
37
+ * Returns the client identified by the given name.
38
+ *
39
+ * When `name` is `"default"` or not provided, the method first tries to
40
+ * return the client registered under the `"default"` key. If no such
41
+ * client exists but exactly one client is registered (under any name),
42
+ * that single client is returned instead.
43
+ *
44
+ * @see ClientRegistry#getClient for the client retrieval logic.
45
+ *
46
+ * @param {string} [name] The name of the client to retrieve.
47
+ * @returns {Client} The matching Client instance.
48
+ * @throws {ClientNotFound} Throws when the named or default client is not found.
49
+ */
50
+ getClient(name) {
51
+ return this.clientRegistry.getClient(name);
52
+ }
53
+
54
+ /**
55
+ * Creates a Config instance from a YAML file.
56
+ *
57
+ * @param {string} filePath Path to the YAML configuration file.
58
+ * @returns {Config} A new Config instance.
59
+ *
60
+ * @throws {ConfigurationFileNotFound} If the configuration file is not found at the specified path.
61
+ * @throws {MissingTopLevelConfigKey} Throws when the file is invalid or does not contain required keys.
62
+ */
63
+ static fromFile(filePath) {
64
+ return new Config( ConfigLoader.fromFile(filePath) );
65
+ }
66
+ }
67
+
68
+ export { Config };