navi-hey 1.4.1 → 1.5.1
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.
- package/README.md +6 -1
- package/lib/enqueuers/AssetRequestEnqueuer.js +4 -2
- package/lib/exceptions/config/ConfigurationIncludeNotFound.js +15 -0
- package/lib/exceptions/registry/NamespaceNotFound.js +16 -0
- package/lib/jobs/AssetDownloadJob.js +3 -2
- package/lib/jobs/ResourceRequestJob.js +6 -2
- package/lib/models/configs/Config.js +25 -14
- package/lib/models/configs/WebConfig.js +5 -1
- package/lib/models/request/Resource.js +14 -6
- package/lib/models/request/ResourceRequest.js +77 -8
- package/lib/models/request/ResourceRequestAction.js +26 -8
- package/lib/models/request/ResourceRequestPaginatedAction.js +60 -9
- package/lib/registry/ClientRegistry.js +3 -59
- package/lib/registry/NamedRegistry.js +11 -0
- package/lib/registry/Namespace.js +26 -0
- package/lib/registry/NamespaceMap.js +228 -0
- package/lib/registry/ResourceRegistry.js +3 -81
- package/lib/server/RouteRegister.js +19 -0
- package/lib/server/Router.js +13 -0
- package/lib/server/SecuredRequestHandler.js +90 -0
- package/lib/server/handlers/LinksHandler.js +7 -2
- package/lib/server/handlers/api/ApiConfigHandler.js +102 -0
- package/lib/server/handlers/api/ApiEngineStartHandler.js +154 -0
- package/lib/server/handlers/api/ApiEngineStopHandler.js +20 -0
- package/lib/server/handlers/engine/EngineStopHandler.js +2 -5
- package/lib/services/Application.js +3 -2
- package/lib/services/ApplicationInstance.js +22 -6
- package/lib/services/Client.js +11 -5
- package/lib/services/ConfigIncluder.js +131 -0
- package/lib/services/ConfigLoader.js +38 -43
- package/lib/services/ConfigParser.js +22 -6
- package/lib/services/Engine.js +42 -1
- package/lib/services/EngineStopService.js +22 -0
- package/lib/services/NamespaceMapBuilder.js +191 -0
- package/lib/utils/ResourceEnqueuer.js +81 -6
- package/lib/utils/ResourceRequestCollector.js +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -100,6 +100,8 @@ resources:
|
|
|
100
100
|
- pages: parsedBody.pagination.pages
|
|
101
101
|
- page_key: page
|
|
102
102
|
- zero_indexed: false
|
|
103
|
+
parameters:
|
|
104
|
+
per_page: headers['x-per-page']
|
|
103
105
|
- url: /categories
|
|
104
106
|
status: 302
|
|
105
107
|
- url: /#/categories
|
|
@@ -147,6 +149,7 @@ When the web server is enabled, the following screens are available:
|
|
|
147
149
|
| `paginated_actions[].pagination[].pages` | Path expression (e.g. `parsedBody.pagination.pages`) resolving to the total page count. |
|
|
148
150
|
| `paginated_actions[].pagination[].page_key` | Parameter name injected as the current page number into each downstream request. |
|
|
149
151
|
| `paginated_actions[].pagination[].zero_indexed` | Boolean. Pages start at `0` when `true`, at `1` when `false` (default). |
|
|
152
|
+
| `paginated_actions[].parameters` | Optional. Path expressions (same syntax as `actions[].parameters`) resolved against the response and merged into each page's request parameters. `page_key`'s value always takes precedence on key collision. |
|
|
150
153
|
| `assets` | Optional list of asset extraction rules. When present on an HTML resource, Navi parses the response body and enqueues a download job for each matched URL. |
|
|
151
154
|
| `assets[].selector` | CSS selector used to find asset elements in the HTML response (e.g. `script[src]`, `link[rel="stylesheet"]`). |
|
|
152
155
|
| `assets[].attribute` | Attribute on the matched element that holds the asset URL (e.g. `src`, `href`). |
|
|
@@ -165,7 +168,7 @@ For example, requesting `/categories.json` might return `[{ "id": 1 }, { "id": 2
|
|
|
165
168
|
|
|
166
169
|
## Paginated Actions
|
|
167
170
|
|
|
168
|
-
`paginated_actions` complement `actions` when the response indicates multiple pages. Instead of iterating over array items, Navi evaluates a `pages` expression against the whole response, then enqueues one `ResourceRequestJob` per page, injecting the page number under the configured `page_key`.
|
|
171
|
+
`paginated_actions` complement `actions` when the response indicates multiple pages. Instead of iterating over array items, Navi evaluates a `pages` expression against the whole response, then enqueues one `ResourceRequestJob` per page, injecting the page number under the configured `page_key`. An optional `parameters` map (same syntax as `actions[].parameters`) is resolved against that same response and merged into every page's request parameters, overriding same-named inherited parameters — though `page_key`'s value always wins on collision.
|
|
169
172
|
|
|
170
173
|
```yaml
|
|
171
174
|
resources:
|
|
@@ -178,6 +181,8 @@ resources:
|
|
|
178
181
|
- pages: parsedBody.pagination.pages
|
|
179
182
|
- page_key: page
|
|
180
183
|
- zero_indexed: false
|
|
184
|
+
parameters:
|
|
185
|
+
per_page: headers['x-per-page']
|
|
181
186
|
products_page:
|
|
182
187
|
- url: /products/{:page}.json
|
|
183
188
|
status: 200
|
|
@@ -17,7 +17,9 @@ class AssetRequestEnqueuer {
|
|
|
17
17
|
* @param {string} rawHtml The raw HTML response body.
|
|
18
18
|
* @param {AssetRequest} assetRequest The asset extraction rule.
|
|
19
19
|
* @param {object} jobRegistry The job registry used to enqueue AssetDownloadJobs.
|
|
20
|
-
* @param {
|
|
20
|
+
* @param {NamespaceMap} clientRegistry The namespace map used for URL resolution.
|
|
21
|
+
* Asset/HTML-parse client resolution is not namespace-aware: lookups always
|
|
22
|
+
* resolve against the `default` namespace.
|
|
21
23
|
*/
|
|
22
24
|
constructor(rawHtml, assetRequest, jobRegistry, clientRegistry) {
|
|
23
25
|
this.#rawHtml = rawHtml;
|
|
@@ -64,7 +66,7 @@ class AssetRequestEnqueuer {
|
|
|
64
66
|
if (url.startsWith('//')) {
|
|
65
67
|
return `https:${url}`;
|
|
66
68
|
}
|
|
67
|
-
const client = this.#clientRegistry.getClient(this.#assetRequest.client);
|
|
69
|
+
const client = this.#clientRegistry.getClient('default', this.#assetRequest.client);
|
|
68
70
|
return `${client.baseUrl}${url}`;
|
|
69
71
|
}
|
|
70
72
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ConfigurationFileNotFound } from './ConfigurationFileNotFound.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ConfigurationIncludeNotFound is thrown when a file referenced by a top-level
|
|
5
|
+
* `include:` list cannot be read.
|
|
6
|
+
* @author darthjee
|
|
7
|
+
*/
|
|
8
|
+
class ConfigurationIncludeNotFound extends ConfigurationFileNotFound {
|
|
9
|
+
constructor(file) {
|
|
10
|
+
super(file);
|
|
11
|
+
this.name = 'ConfigurationIncludeNotFound';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { ConfigurationIncludeNotFound };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ItemNotFound } from './ItemNotFound.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* NamespaceNotFound is a custom error class thrown when a requested namespace
|
|
5
|
+
* cannot be found in the NamespaceMap.
|
|
6
|
+
* @author darthjee
|
|
7
|
+
*/
|
|
8
|
+
class NamespaceNotFound extends ItemNotFound {
|
|
9
|
+
constructor(namespaceName) {
|
|
10
|
+
super(namespaceName, { itemType: 'Namespace' });
|
|
11
|
+
|
|
12
|
+
this.namespaceName = namespaceName;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { NamespaceNotFound };
|
|
@@ -19,7 +19,8 @@ class AssetDownloadJob extends Job {
|
|
|
19
19
|
* @param {string} params.url - The fully-resolved asset URL to fetch.
|
|
20
20
|
* @param {string} [params.client] - Named client to use (defaults to `'default'`).
|
|
21
21
|
* @param {number} params.status - Expected HTTP status code.
|
|
22
|
-
* @param {
|
|
22
|
+
* @param {NamespaceMap} params.clientRegistry - The namespace map used to look up the client.
|
|
23
|
+
* Resolution always targets the `default` namespace (not namespace-aware).
|
|
23
24
|
*/
|
|
24
25
|
constructor({ id, url, client, status, clientRegistry }) {
|
|
25
26
|
super({ id });
|
|
@@ -46,7 +47,7 @@ class AssetDownloadJob extends Job {
|
|
|
46
47
|
logContext.debug(`AssetDownloadJob #${this.id} performing`);
|
|
47
48
|
try {
|
|
48
49
|
this.lastError = undefined;
|
|
49
|
-
const client = this.#clientRegistry.getClient(this.#clientName);
|
|
50
|
+
const client = this.#clientRegistry.getClient('default', this.#clientName);
|
|
50
51
|
return await client.performUrl(this.#url, this.#status, logContext);
|
|
51
52
|
} catch (error) {
|
|
52
53
|
logContext.error(`AssetDownloadJob #${this.id} failed: ${error}`);
|
|
@@ -18,7 +18,7 @@ class ResourceRequestJob extends Job {
|
|
|
18
18
|
* @param {string} params.id - The unique identifier for this job.
|
|
19
19
|
* @param {ResourceRequest} params.resourceRequest - The resource request associated with this job.
|
|
20
20
|
* @param {object} params.parameters - Additional parameters for the request.
|
|
21
|
-
* @param {
|
|
21
|
+
* @param {NamespaceMap} params.clients - The namespace map used to resolve the client for this request.
|
|
22
22
|
*/
|
|
23
23
|
constructor({ id, resourceRequest, parameters, clients }) {
|
|
24
24
|
super({ id });
|
|
@@ -87,7 +87,11 @@ class ResourceRequestJob extends Job {
|
|
|
87
87
|
*/
|
|
88
88
|
#getClient() {
|
|
89
89
|
if (!this.#client) {
|
|
90
|
-
this.#client = this.#clients.getClient(
|
|
90
|
+
this.#client = this.#clients.getClient(
|
|
91
|
+
this.#resourceRequest.namespace,
|
|
92
|
+
this.#resourceRequest.clientName,
|
|
93
|
+
this.#resourceRequest.clientNamespace,
|
|
94
|
+
);
|
|
91
95
|
}
|
|
92
96
|
return this.#client;
|
|
93
97
|
}
|
|
@@ -1,26 +1,31 @@
|
|
|
1
1
|
import { LogConfig } from './LogConfig.js';
|
|
2
|
-
import {
|
|
2
|
+
import { NamespaceMap } from '../../registry/NamespaceMap.js';
|
|
3
3
|
import { ResourceRegistry } from '../../registry/ResourceRegistry.js';
|
|
4
4
|
import { ConfigLoader } from '../../services/ConfigLoader.js';
|
|
5
5
|
|
|
6
|
+
const DEFAULT_NAMESPACE = 'default';
|
|
7
|
+
|
|
6
8
|
/**
|
|
7
9
|
* Config is a class that represents the configuration for the application.
|
|
8
|
-
* It includes the
|
|
10
|
+
* It includes the namespace map (resources/clients, grouped by namespace) that is
|
|
11
|
+
* to be included in the configuration.
|
|
9
12
|
* @author darthjee
|
|
10
13
|
*/
|
|
11
14
|
class Config {
|
|
12
15
|
/**
|
|
13
16
|
* @param {object} params initialization parameters for the Config instance.
|
|
14
|
-
* @param {Record<string,
|
|
15
|
-
* @param {Record<string, Client>} params.clients - A mapping of client names to Client instances.
|
|
17
|
+
* @param {Record<string, Namespace>} params.namespaceMap - A mapping of namespace name to Namespace instance.
|
|
16
18
|
* @param {WorkersConfig} params.workersConfig - The configuration for worker instances.
|
|
17
19
|
* @param {WebConfig|null} [params.webConfig] - Optional web server configuration.
|
|
18
20
|
* @param {LogConfig} [params.logConfig] - Optional log configuration.
|
|
19
21
|
* @param {FailureConfig|null} [params.failureConfig] - Optional failure threshold configuration.
|
|
20
22
|
*/
|
|
21
|
-
constructor({
|
|
22
|
-
this.
|
|
23
|
-
|
|
23
|
+
constructor({ namespaceMap, workersConfig, webConfig, logConfig, failureConfig }) {
|
|
24
|
+
this.namespaceMap = NamespaceMap.build(namespaceMap);
|
|
25
|
+
|
|
26
|
+
const defaultNamespace = namespaceMap[DEFAULT_NAMESPACE];
|
|
27
|
+
this.resourceRegistry = defaultNamespace ? defaultNamespace.resourceRegistry : new ResourceRegistry({});
|
|
28
|
+
|
|
24
29
|
this.workersConfig = workersConfig;
|
|
25
30
|
this.webConfig = webConfig ?? null;
|
|
26
31
|
this.logConfig = logConfig ?? new LogConfig();
|
|
@@ -28,18 +33,22 @@ class Config {
|
|
|
28
33
|
}
|
|
29
34
|
|
|
30
35
|
/**
|
|
31
|
-
* Returns the resource identified by the given name
|
|
36
|
+
* Returns the resource identified by the given name, resolved as if the caller
|
|
37
|
+
* originated from the `default` namespace.
|
|
32
38
|
*
|
|
33
39
|
* @param {string} name The name of the resource to retrieve.
|
|
40
|
+
* @param {string|null} [namespace=null] Explicit target namespace, or null to resolve
|
|
41
|
+
* against `default` (falling back to `default` again when not found there either).
|
|
34
42
|
* @returns {Resource} The matching Resource instance.
|
|
35
43
|
* @throws {Error} Throws when no resource with the given name exists.
|
|
36
44
|
*/
|
|
37
|
-
getResource(name) {
|
|
38
|
-
return this.
|
|
45
|
+
getResource(name, namespace = null) {
|
|
46
|
+
return this.namespaceMap.getResource(DEFAULT_NAMESPACE, name, namespace);
|
|
39
47
|
}
|
|
40
48
|
|
|
41
49
|
/**
|
|
42
|
-
* Returns the client identified by the given name
|
|
50
|
+
* Returns the client identified by the given name, resolved as if the caller
|
|
51
|
+
* originated from the `default` namespace.
|
|
43
52
|
*
|
|
44
53
|
* When `name` is `"default"` or not provided, the method first tries to
|
|
45
54
|
* return the client registered under the `"default"` key. If no such
|
|
@@ -49,11 +58,13 @@ class Config {
|
|
|
49
58
|
* @see ClientRegistry#getClient for the client retrieval logic.
|
|
50
59
|
*
|
|
51
60
|
* @param {string} [name] The name of the client to retrieve.
|
|
61
|
+
* @param {string|null} [namespace=null] Explicit target namespace, or null to resolve
|
|
62
|
+
* against `default` (falling back to `default` again when not found there either).
|
|
52
63
|
* @returns {Client} The matching Client instance.
|
|
53
64
|
* @throws {ClientNotFound} Throws when the named or default client is not found.
|
|
54
65
|
*/
|
|
55
|
-
getClient(name) {
|
|
56
|
-
return this.
|
|
66
|
+
getClient(name, namespace = null) {
|
|
67
|
+
return this.namespaceMap.getClient(DEFAULT_NAMESPACE, name, namespace);
|
|
57
68
|
}
|
|
58
69
|
|
|
59
70
|
/**
|
|
@@ -70,4 +81,4 @@ class Config {
|
|
|
70
81
|
}
|
|
71
82
|
}
|
|
72
83
|
|
|
73
|
-
export { Config };
|
|
84
|
+
export { Config };
|
|
@@ -11,14 +11,18 @@ class WebConfig {
|
|
|
11
11
|
* @param {number} [config.logs_page_size=20] - Maximum number of log entries per page.
|
|
12
12
|
* @param {boolean} [config.enable_shutdown=true] - Whether the shutdown button is enabled.
|
|
13
13
|
* @param {boolean} [config.autostart=true] - Whether the engine starts processing immediately at boot.
|
|
14
|
+
* @param {number} [config.idle_timeout=0] - Seconds of inactivity (no busy workers, no queued jobs) before the application auto-shuts-down. `0` disables auto-shutdown.
|
|
14
15
|
* @param {Array<string|object>} [config.links=[]] - Links shown in the web UI.
|
|
16
|
+
* @param {object} [config.api={}] - Configuration for the token-secured `/api` namespace (`{ token }`).
|
|
15
17
|
*/
|
|
16
|
-
constructor({ port, logs_page_size: logsPageSize = 20, enable_shutdown: enableShutdown = true, autostart = true, links = [] }) {
|
|
18
|
+
constructor({ port, logs_page_size: logsPageSize = 20, enable_shutdown: enableShutdown = true, autostart = true, idle_timeout: idleTimeout = 0, links = [], api = {} }) {
|
|
17
19
|
this.port = port;
|
|
18
20
|
this.logsPageSize = logsPageSize;
|
|
19
21
|
this.enableShutdown = enableShutdown;
|
|
20
22
|
this.autostart = autostart;
|
|
23
|
+
this.idleTimeout = idleTimeout;
|
|
21
24
|
this.links = links.map((link) => Link.fromObject(link));
|
|
25
|
+
this.apiToken = api?.token ?? null;
|
|
22
26
|
}
|
|
23
27
|
}
|
|
24
28
|
|
|
@@ -10,36 +10,44 @@ class Resource {
|
|
|
10
10
|
* @param {object} params - The parameters for creating a Resource instance.
|
|
11
11
|
* @param {string} params.name - The name of the resource.
|
|
12
12
|
* @param {Array} params.resourceRequests - The resource requests associated with the resource.
|
|
13
|
+
* @param {string} [params.namespace='default'] - The namespace this resource was declared in.
|
|
13
14
|
*/
|
|
14
|
-
constructor({ name, resourceRequests }) {
|
|
15
|
+
constructor({ name, resourceRequests, namespace = 'default' }) {
|
|
15
16
|
this.name = name;
|
|
16
17
|
this.resourceRequests = resourceRequests;
|
|
18
|
+
this.namespace = namespace;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
/**
|
|
20
22
|
* Creates a Resource instance from a plain object.
|
|
21
23
|
* @param {object} obj - The plain object representing a resource.
|
|
22
24
|
* @param {string} obj.name - The name of the resource.
|
|
23
|
-
* @param {string} [obj.client] - The
|
|
25
|
+
* @param {string|{name: string, namespace: string}} [obj.client] - The client to use for requests from
|
|
26
|
+
* this resource: either the bare client name, or an object with an explicit target `namespace`.
|
|
24
27
|
* @param {Array<{url: string, status: number}>} obj.resourceRequests -
|
|
25
28
|
* The resource requests associated with the resource.
|
|
29
|
+
* @param {object} [options={}] - Additional options.
|
|
30
|
+
* @param {string} [options.namespace='default'] - The namespace this resource was declared in.
|
|
26
31
|
* @returns {Resource} A new Resource instance.
|
|
27
32
|
*/
|
|
28
|
-
static fromObject(obj) {
|
|
33
|
+
static fromObject(obj, { namespace = 'default' } = {}) {
|
|
29
34
|
return new Resource({
|
|
30
35
|
name: obj.name,
|
|
31
|
-
|
|
36
|
+
namespace,
|
|
37
|
+
resourceRequests: ResourceRequest.fromList(obj.resourceRequests, { clientName: obj.client, namespace })
|
|
32
38
|
});
|
|
33
39
|
}
|
|
34
40
|
|
|
35
41
|
/**
|
|
36
42
|
* Creates a list of Resource instances from a plain object.
|
|
37
43
|
* @param {object} object - The plain object representing multiple resources.
|
|
44
|
+
* @param {object} [options={}] - Additional options.
|
|
45
|
+
* @param {string} [options.namespace='default'] - The namespace these resources were declared in.
|
|
38
46
|
* @returns {Array<Resource>} A list of Resource instances.
|
|
39
47
|
*/
|
|
40
|
-
static fromListObject(object) {
|
|
48
|
+
static fromListObject(object, { namespace = 'default' } = {}) {
|
|
41
49
|
return Object.entries(object).map(([resourceName, requests]) => {
|
|
42
|
-
return Resource.fromObject({ name: resourceName, resourceRequests: requests});
|
|
50
|
+
return Resource.fromObject({ name: resourceName, resourceRequests: requests }, { namespace });
|
|
43
51
|
});
|
|
44
52
|
}
|
|
45
53
|
}
|
|
@@ -12,23 +12,48 @@ import { Application } from '../../services/Application.js';
|
|
|
12
12
|
*/
|
|
13
13
|
class ResourceRequest {
|
|
14
14
|
#clientName;
|
|
15
|
+
#clientNamespace;
|
|
16
|
+
#namespace;
|
|
17
|
+
#disabled;
|
|
15
18
|
|
|
16
19
|
/**
|
|
17
20
|
* @param {object} attributes ResourceRequest attributes
|
|
18
21
|
* @param {string} attributes.url The URL to request.
|
|
19
22
|
* @param {number} attributes.status The expected status code of the response.
|
|
20
|
-
* @param {string} [attributes.clientName] The
|
|
23
|
+
* @param {string|{name: string, namespace: string}} [attributes.clientName] The client to use for this
|
|
24
|
+
* request: either the bare client name (shorthand), or an object with an explicit target `namespace`.
|
|
25
|
+
* @param {string} [attributes.namespace='default'] The namespace of the Resource that owns this request.
|
|
21
26
|
* @param {Array} [attributes.actions=[]] List of raw action config objects.
|
|
22
27
|
* @param {Array} [attributes.assets=[]] List of raw asset extraction rule objects.
|
|
23
28
|
* @param {Array} [attributes.paginated_actions=[]] List of raw paginated action config objects.
|
|
29
|
+
* @param {boolean} [attributes.enabled] Whether this request is enabled. Defaults to enabled
|
|
30
|
+
* when omitted. Ignored when `disabled` is `true`.
|
|
31
|
+
* @param {boolean} [attributes.disabled] Whether this request is disabled. Takes precedence
|
|
32
|
+
* over `enabled` when `true`.
|
|
24
33
|
*/
|
|
25
|
-
constructor({
|
|
34
|
+
constructor({
|
|
35
|
+
url,
|
|
36
|
+
status,
|
|
37
|
+
clientName,
|
|
38
|
+
namespace = 'default',
|
|
39
|
+
actions = [],
|
|
40
|
+
assets = [],
|
|
41
|
+
paginated_actions = [],
|
|
42
|
+
enabled,
|
|
43
|
+
disabled,
|
|
44
|
+
}) {
|
|
26
45
|
this.url = url;
|
|
27
46
|
this.status = status;
|
|
28
|
-
this.#
|
|
29
|
-
this
|
|
47
|
+
this.#namespace = namespace;
|
|
48
|
+
this.#disabled = disabled === true || enabled === false;
|
|
49
|
+
|
|
50
|
+
const parsedClient = ResourceRequest.#parseClient(clientName);
|
|
51
|
+
this.#clientName = parsedClient.name;
|
|
52
|
+
this.#clientNamespace = parsedClient.namespace;
|
|
53
|
+
|
|
54
|
+
this.actions = ResourceRequestAction.fromList(actions, { originNamespace: namespace });
|
|
30
55
|
this.assets = AssetRequest.fromListObject(assets);
|
|
31
|
-
this.paginatedActions = ResourceRequestPaginatedAction.fromList(paginated_actions);
|
|
56
|
+
this.paginatedActions = ResourceRequestPaginatedAction.fromList(paginated_actions, { originNamespace: namespace });
|
|
32
57
|
}
|
|
33
58
|
|
|
34
59
|
/**
|
|
@@ -40,6 +65,34 @@ class ResourceRequest {
|
|
|
40
65
|
return this.#clientName;
|
|
41
66
|
}
|
|
42
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Returns the explicit target namespace of the client associated with this request,
|
|
70
|
+
* as inherited from the parent Resource's client attribute, or null when not given.
|
|
71
|
+
* @returns {string|null} The client's target namespace, or null when not explicitly set.
|
|
72
|
+
*/
|
|
73
|
+
get clientNamespace() {
|
|
74
|
+
return this.#clientNamespace;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Returns the namespace of the Resource that owns this request.
|
|
79
|
+
* @returns {string} The owning resource's namespace.
|
|
80
|
+
*/
|
|
81
|
+
get namespace() {
|
|
82
|
+
return this.#namespace;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Returns whether this resource request is disabled, i.e. it must not be enqueued
|
|
87
|
+
* through any path (startup, manual trigger, or chained actions). A request is
|
|
88
|
+
* disabled when `disabled: true` was given (regardless of `enabled`), or when
|
|
89
|
+
* `enabled: false` was given. Otherwise it is enabled (the default).
|
|
90
|
+
* @returns {boolean} True if the request is disabled.
|
|
91
|
+
*/
|
|
92
|
+
get disabled() {
|
|
93
|
+
return this.#disabled;
|
|
94
|
+
}
|
|
95
|
+
|
|
43
96
|
/**
|
|
44
97
|
* Enqueues one ActionProcessingJob per action associated with the resource request.
|
|
45
98
|
* Returns immediately if there are no actions.
|
|
@@ -116,11 +169,27 @@ class ResourceRequest {
|
|
|
116
169
|
* Creates a list of ResourceRequest instances from an array of objects.
|
|
117
170
|
* @param {Array<{ url: string, status: number }>} array list of objects with attributes to create a new ResourceRequest
|
|
118
171
|
* @param {object} [options={}] optional options to assign to each ResourceRequest
|
|
119
|
-
* @param {string} [options.clientName] optional client
|
|
172
|
+
* @param {string|{name: string, namespace: string}} [options.clientName] optional client reference
|
|
173
|
+
* to assign to each ResourceRequest.
|
|
174
|
+
* @param {string} [options.namespace='default'] the namespace of the owning Resource.
|
|
120
175
|
* @returns {Array<ResourceRequest>} list of ResourceRequest instances
|
|
121
176
|
*/
|
|
122
|
-
static fromList(array, { clientName } = {}) {
|
|
123
|
-
return array.map((attrs) => new ResourceRequest({ ...attrs, clientName }));
|
|
177
|
+
static fromList(array, { clientName, namespace = 'default' } = {}) {
|
|
178
|
+
return array.map((attrs) => new ResourceRequest({ ...attrs, clientName, namespace }));
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Parses a raw client reference into a name/namespace pair.
|
|
183
|
+
* Accepts either a bare string (shorthand, resolved in the requester's own namespace)
|
|
184
|
+
* or an object with an explicit target `namespace`.
|
|
185
|
+
* @param {string|{name: string, namespace: string}} [client] The raw client reference.
|
|
186
|
+
* @returns {{name: string|undefined, namespace: string|null}} The parsed client name and target namespace.
|
|
187
|
+
*/
|
|
188
|
+
static #parseClient(client) {
|
|
189
|
+
if (client && typeof client === 'object') {
|
|
190
|
+
return { name: client.name, namespace: client.namespace ?? null };
|
|
191
|
+
}
|
|
192
|
+
return { name: client, namespace: null };
|
|
124
193
|
}
|
|
125
194
|
}
|
|
126
195
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { JobRegistry as DefaultJobRegistry } from '../../background/JobRegistry.js';
|
|
2
2
|
import { MissingActionResource } from '../../exceptions/registry/MissingActionResource.js';
|
|
3
3
|
import { LogRegistry } from '../../registry/LogRegistry.js';
|
|
4
|
-
import {
|
|
4
|
+
import { NamespaceMap as DefaultNamespaceMap } from '../../registry/NamespaceMap.js';
|
|
5
5
|
import { Application } from '../../services/Application.js';
|
|
6
6
|
import { ParametersMapper } from '../response/ParametersMapper.js';
|
|
7
7
|
|
|
@@ -18,23 +18,37 @@ import { ParametersMapper } from '../response/ParametersMapper.js';
|
|
|
18
18
|
class ResourceRequestAction {
|
|
19
19
|
#mapper;
|
|
20
20
|
#jobRegistry;
|
|
21
|
-
#
|
|
21
|
+
#namespaceMap;
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* @param {object} attributes Action attributes.
|
|
25
25
|
* @param {string} attributes.resource Name of the resource to act upon.
|
|
26
|
+
* @param {string|null} [attributes.namespace=null] Explicit target namespace for the resource
|
|
27
|
+
* lookup. When omitted, resolution first tries `originNamespace`, then falls back to `default`.
|
|
28
|
+
* @param {string} [attributes.originNamespace='default'] The namespace of the resource that owns
|
|
29
|
+
* this action (i.e. the namespace this action was declared under). Threaded in at construction
|
|
30
|
+
* time, not read from YAML.
|
|
26
31
|
* @param {object} [attributes.parameters={}] Key-value map where keys are destination
|
|
27
32
|
* variable names and values are path expressions (e.g. `parsedBody.id`, `headers['page']`).
|
|
28
33
|
* @param {object} [attributes.jobRegistry=JobRegistry] The job registry to enqueue jobs to.
|
|
29
|
-
* @param {object} [attributes.
|
|
34
|
+
* @param {object} [attributes.namespaceMap=NamespaceMap] The namespace map used to look up resources.
|
|
30
35
|
* @throws {MissingActionResource} If resource is absent or falsy.
|
|
31
36
|
*/
|
|
32
|
-
constructor({
|
|
37
|
+
constructor({
|
|
38
|
+
resource,
|
|
39
|
+
namespace = null,
|
|
40
|
+
originNamespace = 'default',
|
|
41
|
+
parameters = {},
|
|
42
|
+
jobRegistry = DefaultJobRegistry,
|
|
43
|
+
namespaceMap = DefaultNamespaceMap,
|
|
44
|
+
}) {
|
|
33
45
|
if (!resource) throw new MissingActionResource();
|
|
34
46
|
this.resource = resource;
|
|
47
|
+
this.namespace = namespace;
|
|
48
|
+
this.originNamespace = originNamespace;
|
|
35
49
|
this.#mapper = new ParametersMapper(parameters);
|
|
36
50
|
this.#jobRegistry = jobRegistry;
|
|
37
|
-
this.#
|
|
51
|
+
this.#namespaceMap = namespaceMap;
|
|
38
52
|
}
|
|
39
53
|
|
|
40
54
|
/**
|
|
@@ -48,9 +62,11 @@ class ResourceRequestAction {
|
|
|
48
62
|
execute(responseWrapper) {
|
|
49
63
|
if (Application.isStopped()) return;
|
|
50
64
|
const vars = this.#mapper.map(responseWrapper);
|
|
51
|
-
const resource = this.#
|
|
65
|
+
const resource = this.#namespaceMap.getResource(this.originNamespace, this.resource, this.namespace);
|
|
52
66
|
|
|
53
67
|
for (const resourceRequest of resource.resourceRequests) {
|
|
68
|
+
if (resourceRequest.disabled) continue;
|
|
69
|
+
|
|
54
70
|
this.#jobRegistry.enqueue('ResourceRequestJob', { resourceRequest, parameters: vars });
|
|
55
71
|
}
|
|
56
72
|
}
|
|
@@ -59,12 +75,14 @@ class ResourceRequestAction {
|
|
|
59
75
|
* Creates a list of ResourceRequestAction instances from an array of raw config objects.
|
|
60
76
|
* Entries that fail to construct (e.g. missing resource) are logged and skipped.
|
|
61
77
|
* @param {Array<object>} [array=[]] Raw action config entries.
|
|
78
|
+
* @param {object} [options={}] Additional options.
|
|
79
|
+
* @param {string} [options.originNamespace='default'] The namespace of the resource that owns these actions.
|
|
62
80
|
* @returns {Array<ResourceRequestAction>} Valid action instances.
|
|
63
81
|
*/
|
|
64
|
-
static fromList(array = []) {
|
|
82
|
+
static fromList(array = [], { originNamespace = 'default' } = {}) {
|
|
65
83
|
return array.flatMap((attrs) => {
|
|
66
84
|
try {
|
|
67
|
-
return [new ResourceRequestAction(attrs)];
|
|
85
|
+
return [new ResourceRequestAction({ ...attrs, originNamespace })];
|
|
68
86
|
} catch (error) {
|
|
69
87
|
LogRegistry.error(`Skipping action: ${error}`);
|
|
70
88
|
return [];
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { JobRegistry as DefaultJobRegistry } from '../../background/JobRegistry.js';
|
|
2
2
|
import { MissingActionResource } from '../../exceptions/registry/MissingActionResource.js';
|
|
3
3
|
import { LogRegistry } from '../../registry/LogRegistry.js';
|
|
4
|
-
import {
|
|
4
|
+
import { NamespaceMap as DefaultNamespaceMap } from '../../registry/NamespaceMap.js';
|
|
5
5
|
import { Application } from '../../services/Application.js';
|
|
6
6
|
import { PaginationConfig } from '../configs/PaginationConfig.js';
|
|
7
|
+
import { ParametersMapper } from '../response/ParametersMapper.js';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Represents a single paginated action to execute after a successful resource request response.
|
|
@@ -14,29 +15,70 @@ import { PaginationConfig } from '../configs/PaginationConfig.js';
|
|
|
14
15
|
*/
|
|
15
16
|
class ResourceRequestPaginatedAction {
|
|
16
17
|
#resource;
|
|
18
|
+
#namespace;
|
|
19
|
+
#originNamespace;
|
|
17
20
|
#pagination;
|
|
21
|
+
#mapper;
|
|
18
22
|
#jobRegistry;
|
|
19
|
-
#
|
|
23
|
+
#namespaceMap;
|
|
20
24
|
|
|
21
25
|
/**
|
|
22
26
|
* @param {object} attributes Action attributes.
|
|
23
27
|
* @param {string} attributes.resource Name of the resource to act upon.
|
|
28
|
+
* @param {string|null} [attributes.namespace=null] Explicit target namespace for the resource
|
|
29
|
+
* lookup. When omitted, resolution first tries `originNamespace`, then falls back to `default`.
|
|
30
|
+
* @param {string} [attributes.originNamespace='default'] The namespace of the resource that owns
|
|
31
|
+
* this paginated action. Threaded in at construction time, not read from YAML.
|
|
24
32
|
* @param {Array<object>} attributes.pagination Pagination config list.
|
|
33
|
+
* @param {object} [attributes.parameters={}] Key-value map where keys are destination
|
|
34
|
+
* variable names and values are path expressions (e.g. `parsedBody.id`, `headers['page']`),
|
|
35
|
+
* resolved against the same response used to compute `pages` and merged into each page's
|
|
36
|
+
* parameters (overriding same-named inherited parameters, but always losing to `page_key`).
|
|
25
37
|
* @param {object} [attributes.jobRegistry=JobRegistry] The job registry to enqueue jobs to.
|
|
26
|
-
* @param {object} [attributes.
|
|
38
|
+
* @param {object} [attributes.namespaceMap=NamespaceMap] The namespace map used to look up resources.
|
|
27
39
|
* @throws {MissingActionResource} If resource is absent or falsy.
|
|
28
40
|
*/
|
|
29
41
|
constructor({
|
|
30
42
|
resource,
|
|
43
|
+
namespace = null,
|
|
44
|
+
originNamespace = 'default',
|
|
31
45
|
pagination,
|
|
46
|
+
parameters = {},
|
|
32
47
|
jobRegistry = DefaultJobRegistry,
|
|
33
|
-
|
|
48
|
+
namespaceMap = DefaultNamespaceMap,
|
|
34
49
|
}) {
|
|
35
50
|
if (!resource) throw new MissingActionResource();
|
|
36
51
|
this.#resource = resource;
|
|
52
|
+
this.#namespace = namespace;
|
|
53
|
+
this.#originNamespace = originNamespace;
|
|
37
54
|
this.#pagination = PaginationConfig.fromList(pagination);
|
|
55
|
+
this.#mapper = new ParametersMapper(parameters);
|
|
38
56
|
this.#jobRegistry = jobRegistry;
|
|
39
|
-
this.#
|
|
57
|
+
this.#namespaceMap = namespaceMap;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The name of the resource to act upon.
|
|
62
|
+
* @returns {string} The resource name.
|
|
63
|
+
*/
|
|
64
|
+
get resource() {
|
|
65
|
+
return this.#resource;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The explicit target namespace for the resource lookup, or null when not given.
|
|
70
|
+
* @returns {string|null} The explicit target namespace.
|
|
71
|
+
*/
|
|
72
|
+
get namespace() {
|
|
73
|
+
return this.#namespace;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The namespace of the resource that owns this paginated action.
|
|
78
|
+
* @returns {string} The origin namespace.
|
|
79
|
+
*/
|
|
80
|
+
get originNamespace() {
|
|
81
|
+
return this.#originNamespace;
|
|
40
82
|
}
|
|
41
83
|
|
|
42
84
|
/**
|
|
@@ -51,11 +93,18 @@ class ResourceRequestPaginatedAction {
|
|
|
51
93
|
|
|
52
94
|
const count = this.#pagination.resolvePages(responseWrapper);
|
|
53
95
|
const pages = this.#pagination.pageNumbers(count);
|
|
54
|
-
const resource = this.#
|
|
96
|
+
const resource = this.#namespaceMap.getResource(this.#originNamespace, this.#resource, this.#namespace);
|
|
97
|
+
const mappedParameters = this.#mapper.map(responseWrapper);
|
|
55
98
|
|
|
56
99
|
for (const page of pages) {
|
|
57
|
-
const pageParameters = {
|
|
100
|
+
const pageParameters = {
|
|
101
|
+
...parameters,
|
|
102
|
+
...mappedParameters,
|
|
103
|
+
[this.#pagination.pageKey]: page,
|
|
104
|
+
};
|
|
58
105
|
for (const resourceRequest of resource.resourceRequests) {
|
|
106
|
+
if (resourceRequest.disabled) continue;
|
|
107
|
+
|
|
59
108
|
this.#jobRegistry.enqueue('ResourceRequestJob', { resourceRequest, parameters: pageParameters });
|
|
60
109
|
}
|
|
61
110
|
}
|
|
@@ -64,12 +113,14 @@ class ResourceRequestPaginatedAction {
|
|
|
64
113
|
/**
|
|
65
114
|
* Creates instances from a raw YAML list, skipping invalid entries.
|
|
66
115
|
* @param {Array<object>} [array=[]] Raw paginated action config entries.
|
|
116
|
+
* @param {object} [options={}] Additional options.
|
|
117
|
+
* @param {string} [options.originNamespace='default'] The namespace of the resource that owns these actions.
|
|
67
118
|
* @returns {Array<ResourceRequestPaginatedAction>} Valid paginated action instances.
|
|
68
119
|
*/
|
|
69
|
-
static fromList(array = []) {
|
|
120
|
+
static fromList(array = [], { originNamespace = 'default' } = {}) {
|
|
70
121
|
return array.flatMap((attrs) => {
|
|
71
122
|
try {
|
|
72
|
-
return [new ResourceRequestPaginatedAction(attrs)];
|
|
123
|
+
return [new ResourceRequestPaginatedAction({ ...attrs, originNamespace })];
|
|
73
124
|
} catch (error) {
|
|
74
125
|
LogRegistry.error(`Skipping paginated_action: ${error}`);
|
|
75
126
|
return [];
|