@adobe/helix-rum-enhancer 2.51.1 → 2.53.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-rum-enhancer",
3
- "version": "2.51.1",
3
+ "version": "2.53.0",
4
4
  "description": "Helix RUM Enhancer",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -12,9 +12,9 @@
12
12
  "lint": "eslint .",
13
13
  "docs": "npx jsdoc2md -c .jsdoc.json --files 'src/*.js' > docs/API.md",
14
14
  "build-bundle": "rollup --config",
15
- "prepack": "npm run build-bundle",
15
+ "prepack": "npm run build-bundle && node release-manifest.mjs write",
16
+ "test-release": "node --test .github/tests/*.test.mjs",
16
17
  "semantic-release": "semantic-release",
17
- "semantic-release-dry": "semantic-release --dry-run --branches $CI_BRANCH",
18
18
  "prepare": "husky"
19
19
  },
20
20
  "repository": {
@@ -33,7 +33,7 @@
33
33
  "reporter-options": "configFile=.mocha-multi.json"
34
34
  },
35
35
  "devDependencies": {
36
- "@adobe/eslint-config-helix": "3.0.34",
36
+ "@adobe/eslint-config-helix": "3.0.35",
37
37
  "@adobe/helix-rum-js": "2.17.0",
38
38
  "@adobe/rollup-plugin-checksum": "1.2.0",
39
39
  "@eslint/config-helpers": "0.7.0",
@@ -41,11 +41,10 @@
41
41
  "@rollup/plugin-babel": "7.1.0",
42
42
  "@semantic-release/changelog": "7.0.0",
43
43
  "@semantic-release/exec": "7.1.0",
44
- "@semantic-release/git": "11.0.1",
45
44
  "@semantic-release/npm": "13.2.0",
46
45
  "@web/test-runner": "1.0.0",
47
46
  "@web/test-runner-commands": "1.0.1",
48
- "@web/test-runner-junit-reporter": "1.0.0",
47
+ "@web/test-runner-junit-reporter": "1.0.1",
49
48
  "@web/test-runner-mocha": "1.0.0",
50
49
  "@web/test-runner-playwright": "1.0.0",
51
50
  "c8": "12.0.0",
@@ -53,14 +52,14 @@
53
52
  "eslint": "9.39.5",
54
53
  "eslint-plugin-header": "3.1.1",
55
54
  "eslint-plugin-import": "2.32.0",
56
- "globals": "17.13.0",
55
+ "globals": "17.14.0",
57
56
  "husky": "9.1.7",
58
57
  "jsdoc-to-markdown": "9.1.3",
59
58
  "junit-report-builder": "5.1.2",
60
59
  "lint-staged": "17.6.0",
61
60
  "mocha": "12.0.3",
62
61
  "mocha-multi-reporters": "1.5.2",
63
- "rollup": "4.64.5",
62
+ "rollup": "4.68.2",
64
63
  "rollup-plugin-eslint-bundle": "9.0.0",
65
64
  "semantic-release": "25.0.9",
66
65
  "semantic-release-slack-bot": "^4.0.2",
package/plugins/cwv.js CHANGED
@@ -9,62 +9,61 @@
9
9
  * OF ANY KIND, either express or implied. See the License for the specific language
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
- // eslint-disable-next-line max-len
13
- // eslint-disable-next-line import/no-extraneous-dependencies, import/no-relative-packages, no-unused-vars
14
- import * as webVitals from '../node_modules/web-vitals/dist/web-vitals.iife.js';
12
+ // web-vitals is bundled into this plugin from the lockfile-pinned package. There is no
13
+ // runtime script load, so nothing outside this release's manifest is executed.
14
+ /* eslint-disable import/no-extraneous-dependencies, import/no-relative-packages */
15
+ import {
16
+ onCLS, onINP, onLCP, onTTFB,
17
+ } from '../node_modules/web-vitals/dist/web-vitals.js';
18
+ /* eslint-enable import/no-extraneous-dependencies, import/no-relative-packages */
19
+
20
+ const METRICS = {
21
+ CLS: onCLS, INP: onINP, LCP: onLCP, TTFB: onTTFB,
22
+ };
15
23
 
16
24
  export default function addCWVTracking({
17
25
  sampleRUM, sourceSelector, targetSelector, fflags,
18
26
  }) {
19
27
  setTimeout(() => {
20
28
  try {
29
+ // Older enhancers loaded web-vitals from this URL. If the page includes it itself,
30
+ // keep the previous behavior and don't register a second set of observers.
21
31
  const cwvScript = new URL('.rum/web-vitals/dist/web-vitals.iife.js', sampleRUM.baseURL).href;
22
32
  /* c8 ignore next 3 */
23
33
  if (document.querySelector(`script[src="${cwvScript}"]`)) {
24
- // web vitals script has been loaded already
25
34
  return;
26
35
  }
27
- const script = document.createElement('script');
28
- script.src = cwvScript;
29
- script.onload = () => {
30
- const storeCWV = (measurement) => {
31
- const data = { cwv: {} };
32
- data.cwv[measurement.name] = measurement.value;
33
- if (measurement.name === 'LCP' && measurement.entries.length > 0) {
34
- const { element } = measurement.entries.pop();
35
- data.target = targetSelector(element);
36
- data.source = sourceSelector(element) || (element && element.outerHTML.slice(0, 30));
37
- }
38
- if (measurement.name === 'CLS' && measurement.entries.length > 0) {
39
- // Attribute CLS to the element of the single largest layout shift
40
- // (mirrors web-vitals' `largestShiftTarget`, derived from the standard entries).
41
- const largest = measurement.entries
42
- .reduce((a, b) => (b.value > (a ? a.value : 0) ? b : a), undefined);
43
- const node = largest && largest.sources
44
- && largest.sources.map((s) => s.node).find((n) => n && n.nodeType === 1);
45
- if (node) {
46
- data.source = sourceSelector(node) || node.outerHTML.slice(0, 30);
47
- }
36
+ const storeCWV = (measurement) => {
37
+ const data = { cwv: {} };
38
+ data.cwv[measurement.name] = measurement.value;
39
+ if (measurement.name === 'LCP' && measurement.entries.length > 0) {
40
+ const { element } = measurement.entries.pop();
41
+ data.target = targetSelector(element);
42
+ data.source = sourceSelector(element) || (element && element.outerHTML.slice(0, 30));
43
+ }
44
+ if (measurement.name === 'CLS' && measurement.entries.length > 0) {
45
+ // Attribute CLS to the element of the single largest layout shift
46
+ // (mirrors web-vitals' `largestShiftTarget`, derived from the standard entries).
47
+ const largest = measurement.entries
48
+ .reduce((a, b) => (b.value > (a ? a.value : 0) ? b : a), undefined);
49
+ const node = largest && largest.sources
50
+ && largest.sources.map((s) => s.node).find((n) => n && n.nodeType === 1);
51
+ if (node) {
52
+ data.source = sourceSelector(node) || node.outerHTML.slice(0, 30);
48
53
  }
49
- sampleRUM('cwv', data);
50
- };
54
+ }
55
+ sampleRUM('cwv', data);
56
+ };
51
57
 
52
- const isEager = (metric) => ['CLS', 'LCP'].includes(metric);
58
+ const isEager = (metric) => ['CLS', 'LCP'].includes(metric);
53
59
 
54
- // When loading `web-vitals` using a classic script, all the public
55
- // methods can be found on the `webVitals` global namespace.
56
- ['INP', 'TTFB', 'CLS', 'LCP'].forEach((metric) => {
57
- const metricFn = window.webVitals[`on${metric}`];
58
- if (typeof metricFn === 'function') {
59
- let opts = {};
60
- fflags.enabled('eagercwv', () => {
61
- opts = { reportAllChanges: isEager(metric) };
62
- });
63
- metricFn(storeCWV, opts);
64
- }
60
+ ['INP', 'TTFB', 'CLS', 'LCP'].forEach((metric) => {
61
+ let opts = {};
62
+ fflags.enabled('eagercwv', () => {
63
+ opts = { reportAllChanges: isEager(metric) };
65
64
  });
66
- };
67
- document.head.appendChild(script);
65
+ METRICS[metric](storeCWV, opts);
66
+ });
68
67
  /* c8 ignore next 3 */
69
68
  } catch (error) {
70
69
  // something went wrong
@@ -9,12 +9,55 @@
9
9
  * OF ANY KIND, either express or implied. See the License for the specific language
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
- function trackConsent() {
13
- const cmpCookie = document.cookie.split(';')
14
- .map((c) => c.trim())
15
- .find((cookie) => cookie.startsWith('OptanonAlertBoxClosed='));
12
+ import { getCookie, classifyConsent } from '../modules/consent.js';
16
13
 
17
- if (cmpCookie) {
14
+ function readConsent() {
15
+ const raw = getCookie('OptanonConsent');
16
+ if (!raw || raw.length > 4096) {
17
+ return null;
18
+ }
19
+ try {
20
+ // Some integrations encode the entire cookie, others just its field values.
21
+ return new URLSearchParams(raw.includes('=') ? raw : decodeURIComponent(raw));
22
+ } catch {
23
+ return null;
24
+ }
25
+ }
26
+
27
+ function getChoice(consent) {
28
+ if (!consent || consent.getAll('groups').length !== 1
29
+ || consent.getAll('AwaitingReconsent').length > 1
30
+ || ![null, 'false'].includes(consent.get('AwaitingReconsent'))) {
31
+ return undefined;
32
+ }
33
+
34
+ // Report the stored level, not evidence of an interaction on a sampled page.
35
+ const entries = consent.get('groups').split(',');
36
+ if (entries.length > 65 || entries.some((entry) => !/^(C\d{4}|\d{1,3}):[01]$/.test(entry))) {
37
+ return undefined;
38
+ }
39
+ const groups = new Map(entries.map((entry) => entry.split(':')));
40
+ if (groups.size !== entries.length) {
41
+ return undefined;
42
+ }
43
+ // The observed standard and numeric schemas use these IDs for necessary cookies.
44
+ const required = ['C0001', '1'].filter((id) => groups.has(id));
45
+ if (required.length !== 1 || groups.get(required[0]) !== '1') {
46
+ return undefined;
47
+ }
48
+ groups.delete(required[0]);
49
+ return classifyConsent([...groups.values()].map((value) => value === '1'));
50
+ }
51
+
52
+ export function getConsentTarget() {
53
+ const consent = readConsent();
54
+ const choice = getChoice(consent);
55
+ if (choice) {
56
+ return choice;
57
+ }
58
+
59
+ const closed = getCookie('OptanonAlertBoxClosed');
60
+ if (closed !== undefined && consent?.get('AwaitingReconsent') !== 'true') {
18
61
  return 'hidden';
19
62
  }
20
63
 
@@ -33,6 +76,6 @@ export default function addCookieConsentTracking({ sampleRUM }) {
33
76
  return;
34
77
  }
35
78
 
36
- sampleRUM('consent', { source: 'onetrust', target: trackConsent() });
79
+ sampleRUM('consent', { source: 'onetrust', target: getConsentTarget() });
37
80
  hasSentData = true;
38
81
  }
@@ -9,12 +9,48 @@
9
9
  * OF ANY KIND, either express or implied. See the License for the specific language
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
- function trackConsent() {
13
- const cookies = document.cookie.split(';')
14
- .map((c) => c.trim())
15
- .filter((cookie) => (cookie.startsWith('notice_gdpr_prefs=') || cookie.startsWith('notice_preferences=')));
12
+ import { getCookie, classifyConsent } from '../modules/consent.js';
16
13
 
17
- if (cookies.length === 2) {
14
+ function readPreference(raw) {
15
+ if (!raw || raw.length > 4096) {
16
+ return undefined;
17
+ }
18
+ try {
19
+ // Strip timestamps / consent-resolution suffixes; never transmit them.
20
+ return decodeURIComponent(raw).split(':')[0];
21
+ } catch {
22
+ return undefined;
23
+ }
24
+ }
25
+
26
+ function getChoice(granular, legacy) {
27
+ if (granular !== undefined) {
28
+ const value = readPreference(granular);
29
+ if (!value || !/^[012](,[012])*$/.test(value)) {
30
+ return undefined;
31
+ }
32
+ const groups = value.split(',');
33
+ if (!groups.includes('0') || new Set(groups).size !== groups.length) {
34
+ return undefined;
35
+ }
36
+ // Standard TrustArc has required (0) and two optional buckets (1, 2).
37
+ // Unknown custom buckets cannot safely be interpreted as this three-bucket schema.
38
+ return classifyConsent(['1', '2'].map((id) => groups.includes(id)));
39
+ }
40
+ // The legacy slider is inclusive, unlike the granular approved-category list.
41
+ const level = readPreference(legacy);
42
+ return /^[012]$/.test(level) ? ['rejected', 'customized', 'granted'][Number(level)] : undefined;
43
+ }
44
+
45
+ export function getConsentTarget() {
46
+ const granular = getCookie('notice_gdpr_prefs');
47
+ const legacy = getCookie('notice_preferences');
48
+ const choice = getChoice(granular, legacy);
49
+ if (choice) {
50
+ return choice;
51
+ }
52
+
53
+ if (granular !== undefined && legacy !== undefined) {
18
54
  return 'hidden';
19
55
  }
20
56
 
@@ -33,6 +69,6 @@ export default function addCookieConsentTracking({ sampleRUM }) {
33
69
  return;
34
70
  }
35
71
 
36
- sampleRUM('consent', { source: 'trustarc', target: trackConsent() });
72
+ sampleRUM('consent', { source: 'trustarc', target: getConsentTarget() });
37
73
  hasSentData = true;
38
74
  }
@@ -9,13 +9,61 @@
9
9
  * OF ANY KIND, either express or implied. See the License for the specific language
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
- function trackConsent() {
13
- if (localStorage.getItem('uc_gcm')) {
12
+ import { getCookie, getLocalStorage, classifyConsent } from '../modules/consent.js';
13
+
14
+ function getChoice(gcm) {
15
+ if (gcm) {
16
+ if (gcm.length > 4096) {
17
+ return undefined;
18
+ }
19
+ try {
20
+ const data = JSON.parse(gcm);
21
+ const signals = ['adStorage', 'analyticsStorage', 'adPersonalization', 'adUserData'];
22
+ return classifyConsent(signals.map((key) => {
23
+ if (data?.[key] === 'granted') {
24
+ return true;
25
+ }
26
+ if (data?.[key] === 'denied') {
27
+ return false;
28
+ }
29
+ return undefined;
30
+ }));
31
+ } catch {
32
+ return undefined;
33
+ }
34
+ }
35
+
36
+ // Explicitly supported category-cookie integration observed on Hamleys.
37
+ // Do not inspect arbitrary uc_* cookies or expose uc_settings / controller IDs.
38
+ if (getCookie('uc_consent_status_essential') !== 'true') {
39
+ return undefined;
40
+ }
41
+ const categories = ['functional', 'marketing', 'analytics', 'personalisation'];
42
+ return classifyConsent(categories.map((category) => {
43
+ const value = getCookie(`uc_consent_status_${category}`);
44
+ if (value === 'true') {
45
+ return true;
46
+ }
47
+ if (value === 'false') {
48
+ return false;
49
+ }
50
+ return undefined;
51
+ }));
52
+ }
53
+
54
+ export function getConsentTarget() {
55
+ const gcm = getLocalStorage('uc_gcm');
56
+ const choice = getChoice(gcm);
57
+ if (choice) {
58
+ return choice;
59
+ }
60
+
61
+ if (gcm || getCookie('uc_consent_status_essential') !== undefined) {
14
62
  return 'hidden';
15
63
  }
16
64
 
17
- const { shadowRoot } = document.querySelector('#usercentrics-root');
18
- const banner = shadowRoot.querySelector('#uc-center-container') || shadowRoot.querySelector('#uc-fading-wrapper');
65
+ const shadowRoot = document.querySelector('#usercentrics-root')?.shadowRoot;
66
+ const banner = shadowRoot?.querySelector('#uc-center-container') || shadowRoot?.querySelector('#uc-fading-wrapper');
19
67
 
20
68
  if (banner && banner.offsetHeight > 0) {
21
69
  return 'show';
@@ -30,6 +78,6 @@ export default function addCookieConsentTracking({ sampleRUM }) {
30
78
  return;
31
79
  }
32
80
 
33
- sampleRUM('consent', { source: 'usercentrics', target: trackConsent() });
81
+ sampleRUM('consent', { source: 'usercentrics', target: getConsentTarget() });
34
82
  hasSentData = true;
35
83
  }
@@ -0,0 +1,110 @@
1
+ {
2
+ "schema": 1,
3
+ "package": "@adobe/helix-rum-enhancer",
4
+ "version": "2.53.0",
5
+ "source": {
6
+ "commit": "87b7ec070caaa095432be0384ab875b7f89945b6"
7
+ },
8
+ "files": {
9
+ "src/index.js": {
10
+ "integrity": "sha384-zoEXfXJ6U6Zbnao59dApLcpEYsT50jfDbMYYZ2dPJCdPcXl6doTyKRJW+RLJcplc",
11
+ "size": 15982
12
+ },
13
+ "src/index.map.js": {
14
+ "integrity": "sha384-xoUHRbXNs2A7fa776FyS8iQczYOu7QopdE9oR5Ch5HrEwx1O4EFw9Xg92E4R5zoz",
15
+ "size": 80735
16
+ },
17
+ "src/plugins/a11y.js": {
18
+ "integrity": "sha384-2RWhCFDrrIACD3dJFrB3zYkQeQkddRnn55SklGqDIe9qdWc+QFaXSI8KUOVKeQzL",
19
+ "size": 7385
20
+ },
21
+ "src/plugins/a11y.map.js": {
22
+ "integrity": "sha384-jzH75+jWwn/kUg/xz7B5+9ftjXfIH1XJbAZ5Y8G9TOmE/+9hAO/R9qQKb5r50xGU",
23
+ "size": 31606
24
+ },
25
+ "src/plugins/cwv.js": {
26
+ "integrity": "sha384-2RLWDWANtyU3cKnrO7OG/j1/v/S/2MVb+UV55PbbIFmEnn6t7s3r/9/x0Zzc983l",
27
+ "size": 15393
28
+ },
29
+ "src/plugins/cwv.map.js": {
30
+ "integrity": "sha384-8Beryvtjg8EqrcJZd5LwWjCLR3LCFa3dmnYLq4BvDh5aESXKOSJHKENGfNHJ1w36",
31
+ "size": 36702
32
+ },
33
+ "src/plugins/form.js": {
34
+ "integrity": "sha384-D2kpQFeuFT9w15uEqYf1aRulQotD9Z4bi+lx+UnW8drBmp3ax5iwaAjVzlPEbv5x",
35
+ "size": 3313
36
+ },
37
+ "src/plugins/form.map.js": {
38
+ "integrity": "sha384-/dRR2V+bfrT/fZdCYnjtXvWnbiEpE+qO/s66UFVjGy8XIHpgwg/nQCouylum7Kqt",
39
+ "size": 13730
40
+ },
41
+ "src/plugins/martech.js": {
42
+ "integrity": "sha384-bjSMPledoGxb/e4BNSwHzyeyb8Y/yGkOrhDcp287OQ8zzO6OYjmfD6unHFbuRpAB",
43
+ "size": 2183
44
+ },
45
+ "src/plugins/martech.map.js": {
46
+ "integrity": "sha384-R3c0RQVteXkwkI1giSIIsL9w2oGTOhixYWdYWkh2nVNcY1ORvf2fkMOv7MTGi1tQ",
47
+ "size": 8692
48
+ },
49
+ "src/plugins/onetrust.js": {
50
+ "integrity": "sha384-aTd37I2f3FvZ4VU+XK2b0cgdRjXlgVUVR1SdNY5gbrCJcM7beeoOJnYbyaXwS82W",
51
+ "size": 3065
52
+ },
53
+ "src/plugins/onetrust.map.js": {
54
+ "integrity": "sha384-SFs3IRkAIgc+4n2zrSk/c89EUXMlbe9kO2BMXGUGIA9y3F0wKYgI9o4f4YPKMXa2",
55
+ "size": 14582
56
+ },
57
+ "src/plugins/redirect.js": {
58
+ "integrity": "sha384-t7RPT7rYvH/T2yenaKr8e/f0NHD80Unb0SNwY9zV1auqa73N70WdxFv3jQeSD38H",
59
+ "size": 1680
60
+ },
61
+ "src/plugins/redirect.map.js": {
62
+ "integrity": "sha384-28yYguicbHnBJzYqr3sV42DK76knLMR7nQ97qCSBN45EITpNCRlCrWeNGMnHRqVm",
63
+ "size": 6649
64
+ },
65
+ "src/plugins/softnav.js": {
66
+ "integrity": "sha384-Lg5CdmI+5HyvMzi1yaJ+g/lECMjk7ERQyAIawsFPEZGGjmRkHpzx7yMcTG/XL0XA",
67
+ "size": 4098
68
+ },
69
+ "src/plugins/softnav.map.js": {
70
+ "integrity": "sha384-MTglqjtjtacvfVIwqRm1emkj2S7nOt06ESvyezF51If7xl2a9gFj76Yl3S/hrQWr",
71
+ "size": 23331
72
+ },
73
+ "src/plugins/trustarc.js": {
74
+ "integrity": "sha384-taZbjtJGEslKmH7k+EBOcs6CEd3ndt2MMDZSjJwEvwbVGtiMRVmafWM/vBYl23ZW",
75
+ "size": 2775
76
+ },
77
+ "src/plugins/trustarc.map.js": {
78
+ "integrity": "sha384-j/F0QWIcb9XH8VYJkbaG8oKl43b6p+FT13Qwc4thz0fCMxCBJVnpIFAj5eQLuXpK",
79
+ "size": 13168
80
+ },
81
+ "src/plugins/usercentrics.js": {
82
+ "integrity": "sha384-A08HPM+M4zeEoFtN0hFoW0KeY+P8NODz/GgBmTqRDq3admynYarw2x2xny82w5tM",
83
+ "size": 3119
84
+ },
85
+ "src/plugins/usercentrics.map.js": {
86
+ "integrity": "sha384-Sbo4wkupk+4/aQsnmASV3pk6WVDk5dgcBU+3UUhzPz6SNOvEoZ2XTYCfqlElFQe2",
87
+ "size": 13944
88
+ },
89
+ "src/plugins/video.js": {
90
+ "integrity": "sha384-X22EqiA6/F0gvyKQeAspj/c11tI8FUO1HgzycoK7gLhi/WXbwR5SVFe/en4zGKUn",
91
+ "size": 761
92
+ },
93
+ "src/plugins/video.map.js": {
94
+ "integrity": "sha384-BmiwAJ8Pr+JiNXgshH1+pcqBlGc95BfqOhgMU07DcxWvQlyKeCFxz159tWJZCvRl",
95
+ "size": 2310
96
+ },
97
+ "src/plugins/webcomponent.js": {
98
+ "integrity": "sha384-pKsfDE3NwfW+vmc+77M78UJyclkkLmBZZX4ARjZJJeyNAokJKW1hKgB0v8D9mgfs",
99
+ "size": 2734
100
+ },
101
+ "src/plugins/webcomponent.map.js": {
102
+ "integrity": "sha384-mY561D6ivOrQJX4/r45Dq7eCmhqhWSbrjcj4b7ksLuf/WlvflDMr7XuMExyklZBa",
103
+ "size": 12079
104
+ },
105
+ "src/vendor/web-vitals.iife.js": {
106
+ "integrity": "sha384-sKh//42d8+X4ztiyB4ChMShr0tmbWDWrkHfy9pLz9napB81dM74xVylUQVmZ94PE",
107
+ "size": 8991
108
+ }
109
+ }
110
+ }
@@ -0,0 +1,183 @@
1
+ #!/usr/bin/env node
2
+ /*
3
+ * Copyright 2026 Adobe. All rights reserved.
4
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License. You may obtain a copy
6
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
7
+ *
8
+ * Unless required by applicable law or agreed to in writing, software distributed under
9
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
10
+ * OF ANY KIND, either express or implied. See the License for the specific language
11
+ * governing permissions and limitations under the License.
12
+ */
13
+
14
+ // Release manifest: every executable file a release serves, with its SHA-384 digest.
15
+ //
16
+ // node release-manifest.mjs write [manifest] after the build (prepack)
17
+ // node release-manifest.mjs verify <manifest> <version> before publishing branches
18
+ // node release-manifest.mjs files <manifest> one path per line, for tagger.sh
19
+ //
20
+ // The file list comes from the build output, not from a hand-maintained list, so a new
21
+ // plugin cannot be silently left out of a release.
22
+ import { createHash } from 'node:crypto';
23
+ import { execFileSync } from 'node:child_process';
24
+ import {
25
+ lstatSync, readdirSync, readFileSync, writeFileSync,
26
+ } from 'node:fs';
27
+ import { join, resolve, sep } from 'node:path';
28
+ import process from 'node:process';
29
+ import { pathToFileURL } from 'node:url';
30
+
31
+ export const SCHEMA = 1;
32
+ export const DEFAULT_MANIFEST = 'release-manifest.json';
33
+
34
+ // Build output directories per package. Only .js files are executable release assets.
35
+ export const BUILD_ROOTS = {
36
+ '@adobe/helix-rum-js': ['dist'],
37
+ '@adobe/helix-rum-enhancer': ['src'],
38
+ };
39
+
40
+ function fail(message) {
41
+ throw new Error(`release-manifest: ${message}`);
42
+ }
43
+
44
+ function readPackage(cwd) {
45
+ return JSON.parse(readFileSync(join(cwd, 'package.json'), 'utf8'));
46
+ }
47
+
48
+ function walk(cwd, dir) {
49
+ const out = [];
50
+ readdirSync(join(cwd, dir), { withFileTypes: true }).forEach((entry) => {
51
+ const path = join(dir, entry.name);
52
+ if (entry.isSymbolicLink()) {
53
+ fail(`symlink in build output: ${path}`);
54
+ } else if (entry.isDirectory()) {
55
+ out.push(...walk(cwd, path));
56
+ } else if (entry.isFile() && entry.name.endsWith('.js')) {
57
+ out.push(path.split(sep).join('/'));
58
+ }
59
+ });
60
+ return out;
61
+ }
62
+
63
+ export function builtFiles(cwd, name) {
64
+ const roots = BUILD_ROOTS[name];
65
+ if (!roots) {
66
+ fail(`no build roots configured for ${name}`);
67
+ }
68
+ const files = roots.flatMap((root) => walk(cwd, root)).sort();
69
+ if (files.length === 0) {
70
+ fail(`no built .js files under ${roots.join(', ')}; run the build first`);
71
+ }
72
+ return files;
73
+ }
74
+
75
+ export function digest(cwd, path) {
76
+ const stat = lstatSync(join(cwd, path));
77
+ if (!stat.isFile()) {
78
+ fail(`not a regular file: ${path}`);
79
+ }
80
+ const bytes = readFileSync(join(cwd, path));
81
+ return {
82
+ integrity: `sha384-${createHash('sha384').update(bytes).digest('base64')}`,
83
+ size: bytes.length,
84
+ };
85
+ }
86
+
87
+ function sourceCommit(cwd) {
88
+ try {
89
+ return execFileSync('git', ['rev-parse', 'HEAD'], { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim();
90
+ } catch (e) {
91
+ return undefined;
92
+ }
93
+ }
94
+
95
+ export function createManifest(cwd) {
96
+ const pkg = readPackage(cwd);
97
+ const files = {};
98
+ builtFiles(cwd, pkg.name).forEach((path) => {
99
+ files[path] = digest(cwd, path);
100
+ });
101
+ const commit = sourceCommit(cwd);
102
+ return {
103
+ schema: SCHEMA,
104
+ package: pkg.name,
105
+ version: pkg.version,
106
+ ...(commit ? { source: { commit } } : {}),
107
+ files,
108
+ };
109
+ }
110
+
111
+ export function readManifest(cwd, manifestPath) {
112
+ const manifest = JSON.parse(readFileSync(resolve(cwd, manifestPath), 'utf8'));
113
+ if (manifest.schema !== SCHEMA || typeof manifest.files !== 'object' || !manifest.files) {
114
+ fail(`${manifestPath} is not a schema ${SCHEMA} release manifest`);
115
+ }
116
+ Object.keys(manifest.files).forEach((path) => {
117
+ if (path.startsWith('/') || path.split('/').includes('..')) {
118
+ fail(`unsafe path in manifest: ${path}`);
119
+ }
120
+ });
121
+ return manifest;
122
+ }
123
+
124
+ export function verifyManifest(cwd, manifestPath, version) {
125
+ const manifest = readManifest(cwd, manifestPath);
126
+ const pkg = readPackage(cwd);
127
+ const problems = [];
128
+ if (manifest.package !== pkg.name) {
129
+ problems.push(`package ${manifest.package} does not match ${pkg.name}`);
130
+ }
131
+ if (manifest.version !== version) {
132
+ problems.push(`version ${manifest.version} does not match ${version}`);
133
+ }
134
+ // Provenance: the manifest must name the commit being published (tagger.sh builds the
135
+ // artifact commit on HEAD), so a copied or edited manifest cannot misattribute a release.
136
+ const head = sourceCommit(cwd);
137
+ const recorded = manifest.source && manifest.source.commit;
138
+ if (!head) {
139
+ problems.push('cannot determine the source commit: git rev-parse HEAD failed');
140
+ } else if (recorded !== head) {
141
+ problems.push(`source commit ${recorded || '(missing)'} does not match HEAD ${head}`);
142
+ }
143
+ const listed = Object.keys(manifest.files).sort();
144
+ const built = builtFiles(cwd, pkg.name);
145
+ built.filter((path) => !listed.includes(path)).forEach((path) => problems.push(`built but not in manifest: ${path}`));
146
+ listed.filter((path) => !built.includes(path)).forEach((path) => problems.push(`in manifest but not built: ${path}`));
147
+ listed.filter((path) => built.includes(path)).forEach((path) => {
148
+ const actual = digest(cwd, path);
149
+ const expected = manifest.files[path];
150
+ if (actual.integrity !== expected.integrity || actual.size !== expected.size) {
151
+ problems.push(`digest mismatch: ${path}`);
152
+ }
153
+ });
154
+ if (problems.length) {
155
+ fail(`${manifestPath} failed verification:\n ${problems.join('\n ')}`);
156
+ }
157
+ return manifest;
158
+ }
159
+
160
+ function main([command, ...args]) {
161
+ const cwd = process.cwd();
162
+ if (command === 'write') {
163
+ const path = args[0] || DEFAULT_MANIFEST;
164
+ const manifest = createManifest(cwd);
165
+ writeFileSync(resolve(cwd, path), `${JSON.stringify(manifest, null, 2)}\n`);
166
+ process.stdout.write(`Wrote ${path}: ${manifest.package}@${manifest.version}, ${Object.keys(manifest.files).length} files\n`);
167
+ } else if (command === 'verify' && args.length === 2) {
168
+ verifyManifest(cwd, args[0], args[1]);
169
+ } else if (command === 'files' && args.length === 1) {
170
+ process.stdout.write(Object.keys(readManifest(cwd, args[0]).files).map((p) => `${p}\n`).join(''));
171
+ } else {
172
+ fail('usage: write [manifest] | verify <manifest> <version> | files <manifest>');
173
+ }
174
+ }
175
+
176
+ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
177
+ try {
178
+ main(process.argv.slice(2));
179
+ } catch (e) {
180
+ process.stderr.write(`${e.message}\n`);
181
+ process.exit(1);
182
+ }
183
+ }