navi-hey 0.4.0 → 0.5.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 (40) hide show
  1. package/README.md +167 -0
  2. package/lib/exceptions/ForbiddenError.js +14 -0
  3. package/lib/exceptions/InvalidHtmlResponseBody.js +19 -0
  4. package/lib/exceptions/NotFoundError.js +13 -0
  5. package/lib/models/ActionProcessingJob.js +18 -9
  6. package/lib/models/AssetDownloadJob.js +58 -0
  7. package/lib/models/AssetRequest.js +39 -0
  8. package/lib/models/AssetRequestEnqueuer.js +68 -0
  9. package/lib/models/Config.js +4 -1
  10. package/lib/models/HtmlParseJob.js +71 -0
  11. package/lib/models/Job.js +12 -2
  12. package/lib/models/LogConfig.js +35 -0
  13. package/lib/models/ResourceRequest.js +24 -1
  14. package/lib/models/ResourceRequestJob.js +35 -3
  15. package/lib/models/WorkersConfig.js +5 -1
  16. package/lib/registry/JobRegistry.js +18 -0
  17. package/lib/registry/JobRegistryInstance.js +55 -2
  18. package/lib/serializers/JobIndexSerializer.js +26 -0
  19. package/lib/serializers/JobSerializer.js +26 -0
  20. package/lib/serializers/JobShowSerializer.js +29 -0
  21. package/lib/serializers/Serializer.js +36 -0
  22. package/lib/server/AssetsRequestHandler.js +50 -0
  23. package/lib/server/IndexRequestHandler.js +31 -0
  24. package/lib/server/JobRequestHandler.js +33 -0
  25. package/lib/server/JobsRequestHandler.js +30 -0
  26. package/lib/server/PathValidator.js +41 -0
  27. package/lib/server/RouteRegister.js +27 -1
  28. package/lib/server/Router.js +27 -3
  29. package/lib/services/Application.js +19 -2
  30. package/lib/services/Client.js +45 -6
  31. package/lib/services/ConfigParser.js +13 -2
  32. package/lib/utils/HtmlElementParser.js +39 -0
  33. package/lib/utils/HtmlParser.js +49 -0
  34. package/lib/utils/collections/Collection.js +9 -0
  35. package/lib/utils/collections/IdentifyableCollection.js +9 -0
  36. package/lib/utils/collections/Queue.js +8 -0
  37. package/package.json +10 -4
  38. package/static/assets/index-DBkz-poX.css +1 -0
  39. package/static/assets/index-sYKRvQgv.js +11 -0
  40. package/static/index.html +13 -0
@@ -65,11 +65,24 @@ class Client {
65
65
  try {
66
66
  return await this.#request(resourceRequest, requestUrl);
67
67
  } catch (error) {
68
- Logger.error(`Request failed: ${error}`);
69
- if (error.response) {
70
- throw new RequestFailed(error.response.status, requestUrl);
71
- }
72
- throw error;
68
+ this.#handleError(error, requestUrl);
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Performs an HTTP request to the given fully-resolved URL and validates the expected status.
74
+ * Unlike `perform`, this method accepts an absolute URL directly and does not prepend `baseUrl`.
75
+ * @param {string} absoluteUrl The fully-resolved URL to request.
76
+ * @param {number} expectedStatus The expected HTTP response status code.
77
+ * @returns {Promise<object>} The Axios response object.
78
+ * @throws {RequestFailed} If the response status does not match `expectedStatus`.
79
+ */
80
+ async performUrl(absoluteUrl, expectedStatus) {
81
+ Logger.info(`[Client:${this.name}] Requesting ${absoluteUrl}`);
82
+ try {
83
+ return await this.#requestUrl(absoluteUrl, expectedStatus);
84
+ } catch (error) {
85
+ this.#handleError(error, absoluteUrl);
73
86
  }
74
87
  }
75
88
 
@@ -82,19 +95,45 @@ class Client {
82
95
  * @throws {RequestFailed} Throws an error if the response status does not match.
83
96
  */
84
97
  async #request(resourceRequest, requestUrl) {
98
+ return this.#requestUrl(requestUrl, resourceRequest.status);
99
+ }
100
+
101
+ /**
102
+ * Performs the HTTP GET request to the given URL and validates the response status.
103
+ * @param {string} requestUrl The full URL to request.
104
+ * @param {number} expectedStatus The expected HTTP response status code.
105
+ * @returns {Promise<object>} The Axios response object.
106
+ * @throws {RequestFailed} Throws an error if the response status does not match.
107
+ */
108
+ async #requestUrl(requestUrl, expectedStatus) {
85
109
  const response = await axios.get(requestUrl, {
86
110
  timeout: this.timeout,
87
111
  responseType: 'text',
88
112
  headers: this.headers,
89
113
  });
90
114
 
91
- if (response.status !== resourceRequest.status) {
115
+ if (response.status !== expectedStatus) {
92
116
  throw new RequestFailed(response.status, requestUrl);
93
117
  }
94
118
 
95
119
  return response;
96
120
  }
97
121
 
122
+ /**
123
+ * Handles a caught request error, logging it and re-throwing as RequestFailed when applicable.
124
+ * @param {Error} error The caught error.
125
+ * @param {string} requestUrl The URL that was requested.
126
+ * @throws {RequestFailed} If the error has a response object.
127
+ * @throws {Error} Re-throws the original error otherwise.
128
+ */
129
+ #handleError(error, requestUrl) {
130
+ Logger.error(`Request failed: ${error}`);
131
+ if (error.response) {
132
+ throw new RequestFailed(error.response.status, requestUrl);
133
+ }
134
+ throw error;
135
+ }
136
+
98
137
  /**
99
138
  * Builds the complete URL by combining base URL and resource URL path.
100
139
  * @param {string} resourceUrl URL path from the resource request.
@@ -1,6 +1,7 @@
1
1
  import { Client } from './Client.js';
2
2
  import { MissingClientsConfig } from '../exceptions/MissingClientsConfig.js';
3
3
  import { MissingResourceConfig } from '../exceptions/MissingResourceConfig.js';
4
+ import { LogConfig } from '../models/LogConfig.js';
4
5
  import { Resource } from '../models/Resource.js';
5
6
  import { WebConfig } from '../models/WebConfig.js';
6
7
  import { WorkersConfig } from '../models/WorkersConfig.js';
@@ -39,8 +40,9 @@ class ConfigParser {
39
40
  * resources: Record<string, Resource>,
40
41
  * clients: Record<string, Client>,
41
42
  * workersConfig: WorkersConfig,
42
- * webConfig: WebConfig|null
43
- * }} Parsed configuration with resources, clients, workersConfig, and webConfig.
43
+ * webConfig: WebConfig|null,
44
+ * logConfig: LogConfig
45
+ * }} Parsed configuration with resources, clients, workersConfig, webConfig, and logConfig.
44
46
  */
45
47
  parse() {
46
48
  const mappedResources = Object.fromEntries(
@@ -56,6 +58,7 @@ class ConfigParser {
56
58
  clients: mappedClients,
57
59
  workersConfig: this.#workersConfig(),
58
60
  webConfig: this.#webConfig(),
61
+ logConfig: this.#logConfig(),
59
62
  };
60
63
  }
61
64
 
@@ -76,6 +79,14 @@ class ConfigParser {
76
79
  return new WebConfig(this.config.web);
77
80
  }
78
81
 
82
+ /**
83
+ * Creates a LogConfig from the parsed YAML log section.
84
+ * @returns {LogConfig} The log configuration instance.
85
+ */
86
+ #logConfig() {
87
+ return LogConfig.fromObject(this.config.log);
88
+ }
89
+
79
90
  /**
80
91
  * Maps the resources to entries suitable for Object.fromEntries.
81
92
  * @returns {Array<Array>} Entries of resource name and Resource instance.
@@ -0,0 +1,39 @@
1
+ import { Logger } from './logging/Logger.js';
2
+
3
+ /**
4
+ * HtmlElementParser wraps a single parsed HTML element and extracts attribute values,
5
+ * logging a warning when the requested attribute is absent.
6
+ * @author darthjee
7
+ */
8
+ class HtmlElementParser {
9
+ #element;
10
+ #selector;
11
+
12
+ /**
13
+ * @param {object} element A parsed HTML element with a `getAttribute` method.
14
+ * @param {string} selector The CSS selector that matched this element (used in warning messages).
15
+ */
16
+ constructor(element, selector) {
17
+ this.#element = element;
18
+ this.#selector = selector;
19
+ }
20
+
21
+ /**
22
+ * Returns the value of the named attribute, or `null` if the attribute is absent.
23
+ * Logs a warning when the attribute is absent.
24
+ * @param {string} attribute Attribute name to extract.
25
+ * @returns {string|null} The attribute value, or `null` when absent.
26
+ */
27
+ getAttribute(attribute) {
28
+ const value = this.#element.getAttribute(attribute);
29
+
30
+ if (value === undefined || value === null) {
31
+ Logger.warn(`HtmlParser: element matched by "${this.#selector}" is missing attribute "${attribute}"`);
32
+ return null;
33
+ }
34
+
35
+ return value;
36
+ }
37
+ }
38
+
39
+ export { HtmlElementParser };
@@ -0,0 +1,49 @@
1
+ import { parse } from 'node-html-parser';
2
+ import { HtmlElementParser } from './HtmlElementParser.js';
3
+ import { InvalidHtmlResponseBody } from '../exceptions/InvalidHtmlResponseBody.js';
4
+ import { Logger } from './logging/Logger.js';
5
+
6
+ /**
7
+ * HtmlParser parses a raw HTML string and extracts attribute values using CSS selectors.
8
+ * @author darthjee
9
+ */
10
+ class HtmlParser {
11
+ /**
12
+ * Parses the given HTML string, finds all elements matching the selector,
13
+ * and returns an array of the specified attribute value from each matched element.
14
+ *
15
+ * Logs a warning when the selector matches zero elements.
16
+ * Logs a warning and skips elements missing the target attribute.
17
+ * @param {string} rawHtml The raw HTML string to parse.
18
+ * @param {string} selector CSS selector to match elements.
19
+ * @param {string} attribute Attribute name whose value is extracted from each matched element.
20
+ * @returns {Array<string>} Array of attribute values from matched elements.
21
+ * @throws {InvalidHtmlResponseBody} If the HTML cannot be parsed.
22
+ */
23
+ static parse(rawHtml, selector, attribute) {
24
+ let root;
25
+
26
+ try {
27
+ root = parse(rawHtml);
28
+ } catch (cause) {
29
+ throw new InvalidHtmlResponseBody(rawHtml, cause);
30
+ }
31
+
32
+ const elements = root.querySelectorAll(selector);
33
+
34
+ if (elements.length === 0) {
35
+ Logger.warn(`HtmlParser: selector "${selector}" matched zero elements`);
36
+ return [];
37
+ }
38
+
39
+ const values = [];
40
+ for (const element of elements) {
41
+ const value = new HtmlElementParser(element, selector).getAttribute(attribute);
42
+ if (value !== null) values.push(value);
43
+ }
44
+
45
+ return values;
46
+ }
47
+ }
48
+
49
+ export { HtmlParser };
@@ -12,6 +12,15 @@ class Collection {
12
12
  hasAny() {
13
13
  return this.size() > 0;
14
14
  }
15
+
16
+ /**
17
+ * Searches for an item with the given id.
18
+ * @param {string} id - The id to search for.
19
+ * @returns {object|null} The matching item, or null if not found.
20
+ */
21
+ findById(id) {
22
+ return this.list().find(item => item.id === id) ?? null;
23
+ }
15
24
  }
16
25
 
17
26
  export { Collection };
@@ -54,6 +54,15 @@ class IdentifyableCollection extends Collection {
54
54
  return this.#items[id] !== undefined;
55
55
  }
56
56
 
57
+ /**
58
+ * Searches for an item with the given id.
59
+ * @param {string} id - The id to search for.
60
+ * @returns {object|null} The matching item, or null if not found.
61
+ */
62
+ findById(id) {
63
+ return this.get(id) ?? null;
64
+ }
65
+
57
66
  /**
58
67
  * Retrieves an item from the collection by index.
59
68
  * @param {number} index - The index of the item to retrieve.
@@ -40,6 +40,14 @@ class Queue extends Collection {
40
40
  size() {
41
41
  return this.items.length;
42
42
  }
43
+
44
+ /**
45
+ * Returns a shallow copy of all items in the queue, preserving FIFO order.
46
+ * @returns {Array} A copy of all items.
47
+ */
48
+ list() {
49
+ return [...this.items];
50
+ }
43
51
  }
44
52
 
45
53
  export { Queue };
package/package.json CHANGED
@@ -1,19 +1,24 @@
1
1
  {
2
2
  "name": "navi-hey",
3
3
  "type": "module",
4
- "version": "0.4.0",
4
+ "version": "0.5.0",
5
5
  "description": "Navi cache warmer CLI",
6
+ "readme": "README.md",
6
7
  "main": "index.js",
7
8
  "bin": {
8
9
  "navi-hey": "bin/navi.js"
9
10
  },
10
- "files": ["bin", "lib"],
11
+ "files": [
12
+ "bin",
13
+ "lib",
14
+ "static"
15
+ ],
11
16
  "scripts": {
12
17
  "spec": "npx jasmine spec/**/*.js",
13
18
  "test": "npx c8 jasmine spec/**/*.js",
14
19
  "coverage": "npx c8 --reporter=lcov jasmine spec/**/*.js",
15
- "lint": "eslint .",
16
- "lint_fix": "eslint . --fix",
20
+ "lint": "eslint lib spec",
21
+ "lint_fix": "eslint lib spec --fix",
17
22
  "lint_report": "eslint . --format html --output-file report/eslint-report.html",
18
23
  "report": "jscpd lib spec --reporters console,html --output ./report/jscpd",
19
24
  "docs": "jsdoc -c jsdoc.json",
@@ -24,6 +29,7 @@
24
29
  "dependencies": {
25
30
  "axios": "^1.13.0",
26
31
  "express": "5.2.1",
32
+ "node-html-parser": "7.1.0",
27
33
  "yaml": "^2.8.2"
28
34
  },
29
35
  "devDependencies": {