@adobe/spacecat-shared-utils 1.47.0 → 1.48.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [@adobe/spacecat-shared-utils-v1.48.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.47.0...@adobe/spacecat-shared-utils-v1.48.0) (2025-08-18)
2
+
3
+
4
+ ### Features
5
+
6
+ * added `urlMatchesFilter` in `spacecat-shared-utils` ([#921](https://github.com/adobe/spacecat-shared/issues/921)) ([74e11e4](https://github.com/adobe/spacecat-shared/commit/74e11e4124137b13942b0c58ead620905c438538))
7
+
1
8
  # [@adobe/spacecat-shared-utils-v1.47.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.46.0...@adobe/spacecat-shared-utils-v1.47.0) (2025-08-15)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-utils",
3
- "version": "1.47.0",
3
+ "version": "1.48.0",
4
4
  "description": "Shared modules of the Spacecat Services - utils",
5
5
  "type": "module",
6
6
  "engines": {
package/src/index.js CHANGED
@@ -64,6 +64,7 @@ export {
64
64
  resolveCanonicalUrl,
65
65
  getSpacecatRequestHeaders,
66
66
  ensureHttps,
67
+ urlMatchesFilter,
67
68
  } from './url-helpers.js';
68
69
 
69
70
  export { getStoredMetrics, storeMetrics } from './metrics-store.js';
@@ -171,6 +171,75 @@ async function resolveCanonicalUrl(urlString, method = 'HEAD') {
171
171
  }
172
172
  }
173
173
 
174
+ /**
175
+ * Normalize a URL by trimming whitespace and handling trailing slashes
176
+ * @param {string} url - The URL to normalize
177
+ * @returns {string} The normalized URL
178
+ */
179
+ function normalizeUrl(url) {
180
+ if (!url || typeof url !== 'string') return url;
181
+ // Trim whitespace from beginning and end
182
+ let normalized = url.trim();
183
+ // Handle trailing slashes - normalize multiple trailing slashes to single slash
184
+ // or no slash depending on whether it's a root path
185
+ if (normalized.endsWith('/')) {
186
+ // Remove all trailing slashes
187
+ normalized = normalized.replace(/\/+$/, '');
188
+ // Add back a single slash if it's a root path (domain only)
189
+ const parts = normalized.split('/');
190
+ if (parts.length === 1 || (parts.length === 2 && parts[1] === '')) {
191
+ normalized += '/';
192
+ }
193
+ }
194
+ return normalized;
195
+ }
196
+
197
+ /**
198
+ * Normalize a pathname by removing trailing slashes
199
+ * @param {string} pathname - The pathname to normalize
200
+ * @returns {string} The normalized pathname
201
+ */
202
+ function normalizePathname(pathname) {
203
+ if (!pathname || typeof pathname !== 'string') return pathname;
204
+ if (pathname === '/') return '/';
205
+ return pathname.replace(/\/+$/, '');
206
+ }
207
+
208
+ /**
209
+ * Check if a URL matches any of the filter URLs by comparing pathnames
210
+ * @param {string} url - URL to check (format: https://domain.com/path)
211
+ * @param {string[]} filterUrls - Array of filter URLs (format: domain.com/path)
212
+ * @returns {boolean} True if URL matches any filter URL, false if any URL is invalid
213
+ */
214
+ function urlMatchesFilter(url, filterUrls) {
215
+ if (!filterUrls || filterUrls.length === 0) return true;
216
+ try {
217
+ // Normalize the input URL
218
+ const normalizedInputUrl = normalizeUrl(url);
219
+ const normalizedUrl = prependSchema(normalizedInputUrl);
220
+ const urlPath = normalizePathname(new URL(normalizedUrl).pathname);
221
+ return filterUrls.some((filterUrl) => {
222
+ try {
223
+ // Normalize each filter URL
224
+ const normalizedInputFilterUrl = normalizeUrl(filterUrl);
225
+ const normalizedFilterUrl = prependSchema(normalizedInputFilterUrl);
226
+ const filterPath = normalizePathname(new URL(normalizedFilterUrl).pathname);
227
+ return urlPath === filterPath;
228
+ } catch (error) {
229
+ // If any filter URL is invalid, skip it and continue checking others
230
+ /* eslint-disable-next-line no-console */
231
+ console.warn(`Invalid filter URL: ${filterUrl}`, error.message);
232
+ return false;
233
+ }
234
+ });
235
+ } catch (error) {
236
+ // If the main URL is invalid, return false
237
+ /* eslint-disable-next-line no-console */
238
+ console.warn(`Invalid URL: ${url}`, error.message);
239
+ return false;
240
+ }
241
+ }
242
+
174
243
  export {
175
244
  ensureHttps,
176
245
  getSpacecatRequestHeaders,
@@ -182,4 +251,5 @@ export {
182
251
  stripTrailingDot,
183
252
  stripTrailingSlash,
184
253
  stripWWW,
254
+ urlMatchesFilter,
185
255
  };