@adobe/helix-html-pipeline 6.3.3 → 6.4.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
+ # [6.4.0](https://github.com/adobe/helix-html-pipeline/compare/v6.3.4...v6.4.0) (2024-02-06)
2
+
3
+
4
+ ### Features
5
+
6
+ * serve or render sitemap.xml ([#530](https://github.com/adobe/helix-html-pipeline/issues/530)) ([b72fe70](https://github.com/adobe/helix-html-pipeline/commit/b72fe70d1b7b22ce7c0ec7d9fefe72db2f472885)), closes [#472](https://github.com/adobe/helix-html-pipeline/issues/472)
7
+
8
+ ## [6.3.4](https://github.com/adobe/helix-html-pipeline/compare/v6.3.3...v6.3.4) (2024-01-30)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **deps:** update dependency @adobe/remark-gridtables to v3 ([#521](https://github.com/adobe/helix-html-pipeline/issues/521)) ([367d13d](https://github.com/adobe/helix-html-pipeline/commit/367d13de7884d09184f5705b69fb7d5ba0c114db))
14
+
1
15
  ## [6.3.3](https://github.com/adobe/helix-html-pipeline/compare/v6.3.2...v6.3.3) (2024-01-29)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-html-pipeline",
3
- "version": "6.3.3",
3
+ "version": "6.4.0",
4
4
  "description": "Helix HTML Pipeline",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -46,7 +46,7 @@
46
46
  "@adobe/helix-markdown-support": "7.1.0",
47
47
  "@adobe/helix-shared-utils": "3.0.1",
48
48
  "@adobe/mdast-util-gridtables": "4.0.0",
49
- "@adobe/remark-gridtables": "2.0.2",
49
+ "@adobe/remark-gridtables": "3.0.0",
50
50
  "cookie": "0.6.0",
51
51
  "github-slugger": "2.0.0",
52
52
  "hast-util-raw": "9.0.2",
@@ -54,7 +54,8 @@
54
54
  "hast-util-to-html": "9.0.0",
55
55
  "hast-util-to-string": "3.0.0",
56
56
  "hastscript": "9.0.0",
57
- "jose": "5.2.0",
57
+ "jose": "5.2.1",
58
+ "lodash.escape": "4.0.1",
58
59
  "mdast-util-to-hast": "13.1.0",
59
60
  "mdast-util-to-string": "4.0.0",
60
61
  "mime": "4.0.1",
@@ -71,7 +72,7 @@
71
72
  "unist-util-visit-parents": "6.0.1"
72
73
  },
73
74
  "devDependencies": {
74
- "@adobe/eslint-config-helix": "2.0.5",
75
+ "@adobe/eslint-config-helix": "2.0.6",
75
76
  "@markedjs/html-differ": "4.0.2",
76
77
  "@semantic-release/changelog": "6.0.3",
77
78
  "@semantic-release/git": "10.0.1",
@@ -82,11 +83,11 @@
82
83
  "eslint-plugin-header": "3.1.1",
83
84
  "eslint-plugin-import": "2.29.1",
84
85
  "esmock": "2.6.3",
85
- "husky": "9.0.6",
86
+ "husky": "9.0.10",
86
87
  "js-yaml": "4.1.0",
87
88
  "jsdom": "24.0.0",
88
- "junit-report-builder": "3.1.0",
89
- "lint-staged": "15.2.0",
89
+ "junit-report-builder": "3.2.1",
90
+ "lint-staged": "15.2.1",
90
91
  "mocha": "10.2.0",
91
92
  "mocha-multi-reporters": "1.5.1",
92
93
  "mocha-suppress-logs": "0.4.1",
package/src/index.js CHANGED
@@ -13,6 +13,7 @@ export * from './html-pipe.js';
13
13
  export * from './json-pipe.js';
14
14
  export * from './auth-pipe.js';
15
15
  export * from './options-pipe.js';
16
+ export * from './sitemap-pipe.js';
16
17
  export * from './PipelineContent.js';
17
18
  export * from './PipelineRequest.js';
18
19
  export * from './PipelineResponse.js';
@@ -0,0 +1,146 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ import escape from 'lodash.escape';
13
+ import { cleanupHeaderValue } from '@adobe/helix-shared-utils';
14
+ import { authenticate } from './steps/authenticate.js';
15
+ import fetchContent from './steps/fetch-content.js';
16
+ import renderCode from './steps/render-code.js';
17
+ import setXSurrogateKeyHeader from './steps/set-x-surrogate-key-header.js';
18
+ import setCustomResponseHeaders from './steps/set-custom-response-headers.js';
19
+ import { PipelineStatusError } from './PipelineStatusError.js';
20
+ import { PipelineResponse } from './PipelineResponse.js';
21
+ import initConfig from './steps/init-config.js';
22
+ import { extractLastModified, updateLastModified } from './utils/last-modified.js';
23
+
24
+ async function generateSitemap(state, partition) {
25
+ const {
26
+ owner, repo, ref, contentBusId, s3Loader, log,
27
+ previewHost, liveHost, prodHost,
28
+ } = state;
29
+ const ret = await s3Loader.getObject('helix-content-bus', `${contentBusId}/live/sitemap.json`);
30
+ if (ret.status !== 200) {
31
+ return ret;
32
+ }
33
+ let config;
34
+ try {
35
+ config = JSON.parse(ret.body);
36
+ } catch (e) {
37
+ log.info('failed to parse /sitemap.json', e);
38
+ throw new PipelineStatusError(404, `Failed to parse /sitemap.json: ${e.message}`);
39
+ }
40
+ const { data } = config;
41
+ if (!data || !Array.isArray(data)) {
42
+ throw new PipelineStatusError(404, 'Expected \'data\' array not found in /sitemap.json');
43
+ }
44
+ const host = partition === 'preview'
45
+ ? (previewHost || `${ref}--${repo}--${owner}.hlx.page`)
46
+ : (prodHost || liveHost || `${ref}--${repo}--${owner}.hlx.live`);
47
+ const loc = ({ path, lastModified }) => ` <url>
48
+ <loc>https://${host}${escape(path)}</loc>
49
+ <lastmod>${new Date(lastModified * 1000).toISOString().substring(0, 10)}</lastmod>
50
+ </url>`;
51
+ const xml = [
52
+ '<?xml version="1.0" encoding="utf-8"?>',
53
+ '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">',
54
+ ...data.map((record) => loc(record)),
55
+ '</urlset>',
56
+ ].join('\n');
57
+ return new PipelineResponse(xml, {
58
+ status: 200,
59
+ headers: {
60
+ 'content-type': 'application/xml; charset=utf-8',
61
+ 'last-modified': ret.headers.get('last-modified'),
62
+ },
63
+ });
64
+ }
65
+
66
+ /**
67
+ * Serves or renders the sitemap xml. The sitemap is always served from the preview content-bus
68
+ * partition.
69
+ *
70
+ * todo: currently only serves an existing sitemap.xml from the contentbus.
71
+ * generate sitemap on the fly based on the sitemap.json
72
+ *
73
+ * @param {PipelineState} state
74
+ * @param {PipelineRequest} req
75
+ * @returns {PipelineResponse}
76
+ */
77
+ export async function sitemapPipe(state, req) {
78
+ const { partition, log } = state;
79
+ state.type = 'sitemap';
80
+
81
+ // force loading from preview
82
+ state.partition = 'preview';
83
+
84
+ if (state.info?.path !== '/sitemap.xml') {
85
+ // this should not happen as it would mean that the caller used the wrong route. so we respond
86
+ // with a 500 to indicate that something is wrong.
87
+ return new PipelineResponse('', {
88
+ status: 500,
89
+ headers: {
90
+ 'x-error': 'invalid route',
91
+ },
92
+ });
93
+ }
94
+
95
+ /** @type PipelineResponse */
96
+ const res = new PipelineResponse('', {
97
+ headers: {
98
+ 'content-type': 'text/plain; charset=utf-8',
99
+ },
100
+ });
101
+
102
+ try {
103
+ await initConfig(state, req, res);
104
+
105
+ // await requireProject(state, req, res);
106
+ if (res.error !== 401) {
107
+ await authenticate(state, req, res);
108
+ }
109
+
110
+ // ...and apply the folder mapping
111
+ state.timer?.update('content-fetch');
112
+
113
+ // fetch sitemap.xml
114
+ await fetchContent(state, req, res);
115
+ if (res.status === 404) {
116
+ const ret = await generateSitemap(state, partition);
117
+ if (ret.status === 200) {
118
+ res.status = 200;
119
+ updateLastModified(state, res, extractLastModified(ret.headers));
120
+ delete res.error;
121
+ state.content.data = ret.body;
122
+ }
123
+ }
124
+ if (res.error) {
125
+ // if content loading produced an error, we're done.
126
+ throw new PipelineStatusError(res.status, res.error);
127
+ }
128
+
129
+ state.timer?.update('serialize');
130
+ await renderCode(state, req, res);
131
+ await setCustomResponseHeaders(state, req, res);
132
+ await setXSurrogateKeyHeader(state, req, res);
133
+ } catch (e) {
134
+ res.error = e.message;
135
+ res.status = e.code || 500;
136
+
137
+ const level = res.status >= 500 ? 'error' : 'info';
138
+ log[level](`pipeline status: ${res.status} ${res.error}`);
139
+ res.headers.set('x-error', cleanupHeaderValue(res.error));
140
+ if (res.status < 500) {
141
+ await setCustomResponseHeaders(state, req, res);
142
+ await setXSurrogateKeyHeader(state, req, res);
143
+ }
144
+ }
145
+ return res;
146
+ }