navi-hey 1.11.0 → 1.11.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.
@@ -225,6 +225,26 @@ class ResourceRequest {
225
225
  return /\{:\w+\}/.test(this.url);
226
226
  }
227
227
 
228
+ /**
229
+ * Returns true if the URL template contains at least one {:placeholder} token
230
+ * whose key is absent or null in the given parameters. An empty string is
231
+ * considered a present value and does not count as unresolved. Extra keys in
232
+ * parameters with no matching token are irrelevant to this check.
233
+ * @param {object} [parameters={}] Key-value map of URL parameters.
234
+ * @returns {boolean} true if at least one token remains unresolved, false otherwise.
235
+ */
236
+ hasUnresolvedTokens(parameters = {}) {
237
+ const matches = this.url.matchAll(/\{:(\w+)\}/g);
238
+
239
+ for (const [, key] of matches) {
240
+ if (!Object.prototype.hasOwnProperty.call(parameters, key) || parameters[key] === null) {
241
+ return true;
242
+ }
243
+ }
244
+
245
+ return false;
246
+ }
247
+
228
248
  /**
229
249
  * Creates a list of ResourceRequest instances from an array of objects.
230
250
  * @param {Array<{ url: string, status: number }>} array list of objects with attributes to create a new ResourceRequest
@@ -116,22 +116,57 @@ class ApiEngineStartHandler extends SecuredRequestHandler {
116
116
  */
117
117
  #validTarget(target) {
118
118
  if (!target || typeof target.namespace !== 'string' || !target.namespace.trim()) return false;
119
+ if (target.parameters !== undefined && !this.#validParameters(target.parameters)) return false;
119
120
  if (target.resources === undefined) return true;
120
121
 
121
- return Array.isArray(target.resources) && target.resources.every((name) => typeof name === 'string');
122
+ return Array.isArray(target.resources) && target.resources.every((entry) => this.#validResourceEntry(entry));
123
+ }
124
+
125
+ /**
126
+ * Validates a single `resources[]` entry: either a bare string (unchanged, including
127
+ * an empty string), or a plain object with a non-blank `name` and, if present, a
128
+ * `parameters` value that passes `#validParameters`. Unknown keys are ignored.
129
+ * @param {*} entry - The candidate resource entry.
130
+ * @returns {boolean} True when the entry is well-formed.
131
+ * @private
132
+ */
133
+ #validResourceEntry(entry) {
134
+ if (typeof entry === 'string') return true;
135
+
136
+ if (!entry || typeof entry !== 'object' || Array.isArray(entry)) return false;
137
+ if (typeof entry.name !== 'string' || !entry.name.trim()) return false;
138
+
139
+ return entry.parameters === undefined || this.#validParameters(entry.parameters);
140
+ }
141
+
142
+ /**
143
+ * Validates a parameter map: a plain object (not an array, not null) whose every
144
+ * value is a string, number, boolean, or null.
145
+ * @param {*} value - The candidate parameters value.
146
+ * @returns {boolean} True when the value is a well-formed parameter map.
147
+ * @private
148
+ */
149
+ #validParameters(value) {
150
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return false;
151
+
152
+ return Object.values(value).every((paramValue) => (
153
+ paramValue === null || ['string', 'number', 'boolean'].includes(typeof paramValue)
154
+ ));
122
155
  }
123
156
 
124
157
  /**
125
158
  * Aggregates the per-target enqueue results into one flat `{ enqueued, skippedResources }`.
126
- * @param {Array<{namespace: string, resources: Array<string>|undefined}>} targets - The targets to enqueue.
159
+ * @param {Array<{namespace: string, resources: Array<string|object>|undefined, parameters: object|undefined}>} targets - The targets to enqueue.
127
160
  * @param {{enqueued: Array<string>, skippedResources: Array<object>}} [seed] - Initial accumulator.
128
161
  * @returns {{enqueued: Array<string>, skippedResources: Array<object>}} The aggregated result.
129
162
  * @private
130
163
  */
131
164
  #aggregate(targets, seed = { enqueued: [], skippedResources: [] }) {
132
- return targets.reduce((acc, { namespace, resources }) => {
165
+ return targets.reduce((acc, { namespace, resources, parameters }) => {
133
166
  const enqueuer = new ResourceEnqueuer(namespace);
134
- const result = resources === undefined ? enqueuer.enqueueAll() : enqueuer.enqueue(resources);
167
+ const result = resources === undefined
168
+ ? enqueuer.enqueueAll()
169
+ : enqueuer.enqueue(resources, { parameters: parameters ?? {} });
135
170
 
136
171
  return {
137
172
  enqueued: [...acc.enqueued, ...result.enqueued],
@@ -24,15 +24,20 @@ class ResourceEnqueuer {
24
24
  }
25
25
 
26
26
  /**
27
- * Enqueues the given resource names.
28
- * @param {Array<string>} names - Resource names to enqueue.
29
- * @returns {{enqueued: Array<string>, skippedResources: Array<{name: string, reason: string}>}} The enqueued names and any skipped resources.
27
+ * Enqueues the given resource entries.
28
+ * @param {Array<string|{name: string, parameters?: object}>} entries - Resource names to
29
+ * enqueue, either as bare names or as objects carrying per-resource parameter overrides.
30
+ * @param {object} [options={}] Enqueue options.
31
+ * @param {object} [options.parameters={}] Target-level default parameter map, merged
32
+ * underneath each entry's own `parameters` (per-resource values win on key conflict).
33
+ * @returns {{enqueued: Array<string>, skippedResources: Array<{name: string, reason: string, parameters?: object}>}} The enqueued names and any skipped resources.
30
34
  */
31
- enqueue(names) {
35
+ enqueue(entries, { parameters: targetParameters = {} } = {}) {
32
36
  const enqueued = [];
33
37
  const skippedResources = [];
34
38
 
35
- names.forEach((name) => {
39
+ entries.forEach((entry) => {
40
+ const { name, parameters: merged } = this.#normalizeEntry(entry, targetParameters);
36
41
  const resource = this.#findResource(name);
37
42
 
38
43
  if (!resource) {
@@ -45,13 +50,15 @@ class ResourceEnqueuer {
45
50
  return;
46
51
  }
47
52
 
48
- if (resource.resourceRequests.some((request) => request.needsParams())) {
49
- skippedResources.push({ name, reason: 'needs_params' });
53
+ if (resource.resourceRequests.some((request) => request.hasUnresolvedTokens(merged))) {
54
+ const skipped = { name, reason: 'needs_params' };
55
+ if (Object.keys(merged).length > 0) skipped.parameters = merged;
56
+ skippedResources.push(skipped);
50
57
  return;
51
58
  }
52
59
 
53
60
  resource.resourceRequests.forEach((resourceRequest) => {
54
- JobRegistry.enqueue('ResourceRequestJob', { resourceRequest, parameters: {} });
61
+ JobRegistry.enqueue('ResourceRequestJob', { resourceRequest, parameters: merged });
55
62
  });
56
63
  enqueued.push(name);
57
64
  });
@@ -79,6 +86,24 @@ class ResourceEnqueuer {
79
86
  return { enqueued: [], skippedResources: [] };
80
87
  }
81
88
 
89
+ /**
90
+ * Normalizes a resource entry into its resolved name and merged parameter map.
91
+ * A bare string entry inherits the target-level parameters as-is; an object
92
+ * entry's own `parameters` are shallow-merged on top of the target-level
93
+ * defaults, with the per-resource value winning on key conflict.
94
+ * @param {string|{name: string, parameters?: object}} entry - The raw resource entry.
95
+ * @param {object} targetParameters - The target-level default parameter map.
96
+ * @returns {{name: string, parameters: object}} The resolved name and merged parameters.
97
+ * @private
98
+ */
99
+ #normalizeEntry(entry, targetParameters) {
100
+ if (typeof entry === 'string') {
101
+ return { name: entry, parameters: targetParameters };
102
+ }
103
+
104
+ return { name: entry.name, parameters: { ...targetParameters, ...entry.parameters } };
105
+ }
106
+
82
107
  /**
83
108
  * Looks up a resource, by name, in the target namespace, returning `null`
84
109
  * instead of raising when the resource (or the namespace itself) cannot be found.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "navi-hey",
3
3
  "type": "module",
4
- "version": "1.11.0",
4
+ "version": "1.11.1",
5
5
  "description": "Navi cache warmer CLI",
6
6
  "readme": "README.md",
7
7
  "main": "index.js",