@adobe/spacecat-shared-utils 1.44.3 → 1.46.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,17 @@
1
+ # [@adobe/spacecat-shared-utils-v1.46.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.45.0...@adobe/spacecat-shared-utils-v1.46.0) (2025-08-14)
2
+
3
+
4
+ ### Features
5
+
6
+ * return null instead of throwing error ([#914](https://github.com/adobe/spacecat-shared/issues/914)) ([90760c0](https://github.com/adobe/spacecat-shared/commit/90760c040c46eab127c1bfc780a454875475fb64))
7
+
8
+ # [@adobe/spacecat-shared-utils-v1.45.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.44.3...@adobe/spacecat-shared-utils-v1.45.0) (2025-08-11)
9
+
10
+
11
+ ### Features
12
+
13
+ * Create shared utils method for resolving canonical url ([#895](https://github.com/adobe/spacecat-shared/issues/895)) ([054ebf7](https://github.com/adobe/spacecat-shared/commit/054ebf7c0ef90269eac75558d3050ebee712c725))
14
+
1
15
  # [@adobe/spacecat-shared-utils-v1.44.3](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.44.2...@adobe/spacecat-shared-utils-v1.44.3) (2025-08-09)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-utils",
3
- "version": "1.44.3",
3
+ "version": "1.46.0",
4
4
  "description": "Shared modules of the Spacecat Services - utils",
5
5
  "type": "module",
6
6
  "engines": {
package/src/index.js CHANGED
@@ -61,6 +61,9 @@ export {
61
61
  stripTrailingDot,
62
62
  stripTrailingSlash,
63
63
  stripWWW,
64
+ resolveCanonicalUrl,
65
+ getSpacecatRequestHeaders,
66
+ ensureHttps,
64
67
  } from './url-helpers.js';
65
68
 
66
69
  export { getStoredMetrics, storeMetrics } from './metrics-store.js';
@@ -11,6 +11,7 @@
11
11
  */
12
12
 
13
13
  import { context as h2, h1 } from '@adobe/fetch';
14
+ import { SPACECAT_USER_AGENT } from './tracing-fetch.js';
14
15
 
15
16
  /* c8 ignore next 3 */
16
17
  export const { fetch } = process.env.HELIX_FETCH_FORCE_HTTP1
@@ -105,7 +106,75 @@ async function composeAuditURL(url, userAgent) {
105
106
  return stripTrailingSlash(finalUrl);
106
107
  }
107
108
 
109
+ /**
110
+ * Ensures the URL is HTTPS.
111
+ * @param {string} url - The URL to ensure is HTTPS.
112
+ * @returns {string} The HTTPS URL.
113
+ */
114
+ function ensureHttps(url) {
115
+ const urlObj = new URL(url);
116
+ urlObj.protocol = 'https';
117
+ return urlObj.toString();
118
+ }
119
+
120
+ /**
121
+ * Gets spacecat HTTP headers with appropriate user agent for the request type
122
+ * @returns {Object} - HTTP headers object
123
+ */
124
+ function getSpacecatRequestHeaders() {
125
+ return {
126
+ Accept: 'text/html,application/xhtml+xml,application/xml,text/css,application/javascript,text/javascript;q=0.9,image/avif,image/webp,*/*;q=0.8',
127
+ 'Accept-Language': 'en-US,en;q=0.5',
128
+ 'Cache-Control': 'no-cache',
129
+ Pragma: 'no-cache',
130
+ Referer: 'https://www.adobe.com/',
131
+ 'User-Agent': SPACECAT_USER_AGENT,
132
+ };
133
+ }
134
+
135
+ /**
136
+ * Resolve canonical URL for a given URL string by following redirect chain.
137
+ * @param {string} urlString - The URL string to normalize.
138
+ * @param {string} method - HTTP method to use ('HEAD' or 'GET').
139
+ * @returns {Promise<string|null>} A Promise that resolves to the canonical URL or null if failed.
140
+ */
141
+ async function resolveCanonicalUrl(urlString, method = 'HEAD') {
142
+ const headers = getSpacecatRequestHeaders();
143
+ let resp;
144
+
145
+ try {
146
+ resp = await fetch(urlString, { headers, method });
147
+
148
+ if (resp.ok) {
149
+ return ensureHttps(resp.url);
150
+ }
151
+
152
+ // Handle redirect chains
153
+ if (urlString !== resp.url) {
154
+ return resolveCanonicalUrl(resp.url, method);
155
+ }
156
+
157
+ if (method === 'HEAD') {
158
+ return resolveCanonicalUrl(urlString, 'GET');
159
+ }
160
+
161
+ // If the URL is not found and we've tried both HEAD and GET, return null
162
+ return null;
163
+ } catch {
164
+ // If HEAD failed with network error and we haven't tried GET yet, retry with GET
165
+ if (method === 'HEAD') {
166
+ return resolveCanonicalUrl(urlString, 'GET');
167
+ }
168
+
169
+ // For all errors (both HTTP status and network), return null
170
+ return null;
171
+ }
172
+ }
173
+
108
174
  export {
175
+ ensureHttps,
176
+ getSpacecatRequestHeaders,
177
+ resolveCanonicalUrl,
109
178
  composeBaseURL,
110
179
  composeAuditURL,
111
180
  prependSchema,