@adobe/spacecat-shared-utils 1.44.2 → 1.45.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.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)
2
+
3
+
4
+ ### Features
5
+
6
+ * 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))
7
+
8
+ # [@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)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **deps:** update external fixes ([#899](https://github.com/adobe/spacecat-shared/issues/899)) ([c2cc342](https://github.com/adobe/spacecat-shared/commit/c2cc3422a0a4a3f8d1a2724847da456bf801ff59))
14
+
1
15
  # [@adobe/spacecat-shared-utils-v1.44.2](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.44.1...@adobe/spacecat-shared-utils-v1.44.2) (2025-08-04)
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.2",
3
+ "version": "1.45.0",
4
4
  "description": "Shared modules of the Spacecat Services - utils",
5
5
  "type": "module",
6
6
  "engines": {
@@ -39,17 +39,17 @@
39
39
  "chai-as-promised": "8.0.1",
40
40
  "esmock": "2.7.1",
41
41
  "husky": "9.1.7",
42
- "nock": "14.0.8",
42
+ "nock": "14.0.9",
43
43
  "sinon": "20.0.0",
44
44
  "sinon-chai": "4.0.0"
45
45
  },
46
46
  "dependencies": {
47
47
  "@adobe/fetch": "4.2.2",
48
- "@adobe/spacecat-shared-data-access": "2.31.0",
48
+ "@adobe/spacecat-shared-data-access": "2.45.0",
49
49
  "@adobe/spacecat-shared-ims-client": "1.8.3",
50
- "@aws-sdk/client-s3": "3.859.0",
51
- "@aws-sdk/client-secrets-manager": "3.859.0",
52
- "@aws-sdk/client-sqs": "3.859.0",
50
+ "@aws-sdk/client-s3": "3.864.0",
51
+ "@aws-sdk/client-secrets-manager": "3.864.0",
52
+ "@aws-sdk/client-sqs": "3.864.0",
53
53
  "@json2csv/plainjs": "7.0.6",
54
54
  "aws-xray-sdk": "3.10.3",
55
55
  "uuid": "11.1.0"
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,74 @@ 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>} A Promise that resolves to the canonical URL.
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, throw an error
162
+ const errorMessage = `HTTP error! status: ${resp.status}`;
163
+ throw new Error(errorMessage);
164
+ } catch (err) {
165
+ // If HEAD failed with network error and we haven't tried GET yet, retry with GET
166
+ if (method === 'HEAD') {
167
+ return resolveCanonicalUrl(urlString, 'GET');
168
+ }
169
+ throw new Error(`Failed to retrieve URL (${urlString}): ${err.message}`);
170
+ }
171
+ }
172
+
108
173
  export {
174
+ ensureHttps,
175
+ getSpacecatRequestHeaders,
176
+ resolveCanonicalUrl,
109
177
  composeBaseURL,
110
178
  composeAuditURL,
111
179
  prependSchema,