@adobe/helix-html-pipeline 1.0.1 → 1.0.4
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 +21 -0
- package/package.json +1 -1
- package/src/PipelineResponse.js +1 -1
- package/src/html-pipe.js +5 -1
- package/src/index.js +5 -0
- package/src/steps/extract-metadata.js +10 -51
- package/src/steps/fetch-content.js +1 -1
- package/src/steps/set-custom-response-headers.js +2 -13
- package/src/utils/metadata.js +66 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
## [1.0.4](https://github.com/adobe/helix-html-pipeline/compare/v1.0.3...v1.0.4) (2022-03-08)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* no special headers init for PipelineResponse ([#9](https://github.com/adobe/helix-html-pipeline/issues/9)) ([7288677](https://github.com/adobe/helix-html-pipeline/commit/72886779f6ba0f8d07ea14757a8097034262e215))
|
|
7
|
+
|
|
8
|
+
## [1.0.3](https://github.com/adobe/helix-html-pipeline/compare/v1.0.2...v1.0.3) (2022-03-08)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* fix canonical url and 404.html response, clean up meta ([5f3e999](https://github.com/adobe/helix-html-pipeline/commit/5f3e999a6e305fb6ecf7d2fefe3a38274a135433))
|
|
14
|
+
|
|
15
|
+
## [1.0.2](https://github.com/adobe/helix-html-pipeline/compare/v1.0.1...v1.0.2) (2022-03-08)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* canonical url and 404.html response ([9f4e473](https://github.com/adobe/helix-html-pipeline/commit/9f4e47372d6aea1252f179ad661b0d2fb03429bd))
|
|
21
|
+
|
|
1
22
|
## [1.0.1](https://github.com/adobe/helix-html-pipeline/compare/v1.0.0...v1.0.1) (2022-03-07)
|
|
2
23
|
|
|
3
24
|
|
package/package.json
CHANGED
package/src/PipelineResponse.js
CHANGED
|
@@ -19,7 +19,7 @@ export class PipelineResponse {
|
|
|
19
19
|
* Creates the pipeline response
|
|
20
20
|
*/
|
|
21
21
|
constructor(body = undefined, init = {}) {
|
|
22
|
-
let headers = init.headers ?? new Map(
|
|
22
|
+
let headers = init.headers ?? new Map();
|
|
23
23
|
if (typeof headers.get !== 'function') {
|
|
24
24
|
headers = new Map(Object.entries(init.headers));
|
|
25
25
|
}
|
package/src/html-pipe.js
CHANGED
|
@@ -54,7 +54,11 @@ export async function htmlPipe(state, req) {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
/** @type PipelineResponse */
|
|
57
|
-
const res = new PipelineResponse(
|
|
57
|
+
const res = new PipelineResponse('', {
|
|
58
|
+
headers: {
|
|
59
|
+
'content-type': 'text/html; charset=utf-8',
|
|
60
|
+
},
|
|
61
|
+
});
|
|
58
62
|
|
|
59
63
|
try { // fetch config first, since we need to compute the content-bus-id from the fstab ...
|
|
60
64
|
await fetchConfig(state, req, res);
|
package/src/index.js
CHANGED
|
@@ -16,3 +16,8 @@ export * from './PipelineRequest.js';
|
|
|
16
16
|
export * from './PipelineResponse.js';
|
|
17
17
|
export * from './PipelineState.js';
|
|
18
18
|
export * from './PipelineStatusError.js';
|
|
19
|
+
|
|
20
|
+
export { default as fetchMetadata } from './steps/fetch-metadata.js';
|
|
21
|
+
export { default as fetchConfig } from './steps/fetch-config.js';
|
|
22
|
+
export { default as setCustomResponseHeaders } from './steps/set-custom-response-headers.js';
|
|
23
|
+
export { getOriginalHost } from './steps/utils.js';
|
|
@@ -11,17 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import { resolve } from 'url';
|
|
13
13
|
import { getAbsoluteUrl, makeCanonicalHtmlUrl, optimizeImageURL } from './utils.js';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Converts all non-valid characters to `-`.
|
|
17
|
-
* @param {string} text input text
|
|
18
|
-
* @returns {string} the meta name
|
|
19
|
-
*/
|
|
20
|
-
function toMetaName(text) {
|
|
21
|
-
return text
|
|
22
|
-
.toLowerCase()
|
|
23
|
-
.replace(/[^0-9a-z:_]/gi, '-');
|
|
24
|
-
}
|
|
14
|
+
import { filterGlobalMetadata, toMetaName, ALLOWED_RESPONSE_HEADERS } from '../utils/metadata.js';
|
|
25
15
|
|
|
26
16
|
/**
|
|
27
17
|
* Cleans up comma-separated string lists and returns an array.
|
|
@@ -98,40 +88,6 @@ function readBlockConfig($block) {
|
|
|
98
88
|
return config;
|
|
99
89
|
}
|
|
100
90
|
|
|
101
|
-
function applyMetaRule(target, obj) {
|
|
102
|
-
Object.keys(obj).forEach((key) => {
|
|
103
|
-
const metaKey = toMetaName(key);
|
|
104
|
-
if (metaKey !== 'url' && obj[key]) {
|
|
105
|
-
target[metaKey] = obj[key];
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function globToRegExp(glob) {
|
|
111
|
-
const reString = glob
|
|
112
|
-
.replace(/\*\*/g, '_')
|
|
113
|
-
.replace(/\*/g, '[0-9a-z-.]*')
|
|
114
|
-
.replace(/_/g, '.*');
|
|
115
|
-
return new RegExp(`^${reString}$`);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export function filterGlobalMetadata(metaRules, path) {
|
|
119
|
-
const metaConfig = {};
|
|
120
|
-
metaRules.forEach((rule) => {
|
|
121
|
-
const glob = rule.url || rule.URL || rule.Url;
|
|
122
|
-
if (glob && typeof glob === 'string' && /[0-9a-z-/*]/.test(glob)) {
|
|
123
|
-
if (glob.indexOf('*') >= 0) {
|
|
124
|
-
if (globToRegExp(glob).test(path)) {
|
|
125
|
-
applyMetaRule(metaConfig, rule);
|
|
126
|
-
}
|
|
127
|
-
} else if (glob === path) {
|
|
128
|
-
applyMetaRule(metaConfig, rule);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
return metaConfig;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
91
|
/**
|
|
136
92
|
* Looks for metadata in the document.
|
|
137
93
|
* @param {HTMLDocument} document The document
|
|
@@ -196,11 +152,13 @@ export default function extractMetaData(state, req) {
|
|
|
196
152
|
});
|
|
197
153
|
if (Object.keys(metaConfig).length > 0) {
|
|
198
154
|
// add rest to meta.custom
|
|
199
|
-
meta.custom = Object.
|
|
200
|
-
name
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
155
|
+
meta.custom = Object.entries(metaConfig)
|
|
156
|
+
.filter(([name]) => !ALLOWED_RESPONSE_HEADERS.includes(name))
|
|
157
|
+
.map(([name, value]) => ({
|
|
158
|
+
name,
|
|
159
|
+
value,
|
|
160
|
+
property: name.includes(':'),
|
|
161
|
+
}));
|
|
204
162
|
}
|
|
205
163
|
|
|
206
164
|
if (meta.keywords) {
|
|
@@ -233,7 +191,8 @@ export default function extractMetaData(state, req) {
|
|
|
233
191
|
});
|
|
234
192
|
meta.description = `${desc.slice(0, 25).join(' ')}${desc.length > 25 ? ' ...' : ''}`;
|
|
235
193
|
}
|
|
236
|
-
|
|
194
|
+
// use the req.url and not the state.info.path in case of folder mapping
|
|
195
|
+
meta.url = makeCanonicalHtmlUrl(getAbsoluteUrl(req.headers, req.url.pathname));
|
|
237
196
|
if (!meta.canonical) {
|
|
238
197
|
meta.canonical = meta.url;
|
|
239
198
|
}
|
|
@@ -74,7 +74,7 @@ export default async function fetchContent(state, req, res) {
|
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
// keep 404 response status
|
|
77
|
-
res.body =
|
|
77
|
+
res.body = ret404.body;
|
|
78
78
|
res.headers.set('last-modified', ret404.headers.get('last-modified'));
|
|
79
79
|
res.headers.set('content-type', 'text/html; charset=utf-8');
|
|
80
80
|
res.headers.set('x-surrogate-key', `${ref}--${repo}--${owner}_404`);
|
|
@@ -10,18 +10,7 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
import { cleanupHeaderValue } from '@adobe/helix-shared-utils';
|
|
13
|
-
import { filterGlobalMetadata } from '
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Array of headers allowed in the metadata.json file.
|
|
17
|
-
*/
|
|
18
|
-
const allowList = [
|
|
19
|
-
'content-security-policy',
|
|
20
|
-
'content-security-policy-report-only',
|
|
21
|
-
'access-control-allow-origin',
|
|
22
|
-
'access-control-allow-methods',
|
|
23
|
-
'link',
|
|
24
|
-
];
|
|
13
|
+
import { filterGlobalMetadata, ALLOWED_RESPONSE_HEADERS } from '../utils/metadata.js';
|
|
25
14
|
|
|
26
15
|
/**
|
|
27
16
|
* Decorates the pipeline response object with the headers defined in metadata.json.
|
|
@@ -34,7 +23,7 @@ const allowList = [
|
|
|
34
23
|
export default function setCustomResponseHeaders(state, req, res) {
|
|
35
24
|
const meta = filterGlobalMetadata(state.metadata, state.info.path);
|
|
36
25
|
Object.entries(meta).forEach(([name, value]) => {
|
|
37
|
-
if (
|
|
26
|
+
if (ALLOWED_RESPONSE_HEADERS.includes(name)) {
|
|
38
27
|
res.headers.set(name, cleanupHeaderValue(value));
|
|
39
28
|
}
|
|
40
29
|
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2022 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
|
+
/**
|
|
13
|
+
* Converts all non-valid characters to `-`.
|
|
14
|
+
* @param {string} text input text
|
|
15
|
+
* @returns {string} the meta name
|
|
16
|
+
*/
|
|
17
|
+
export function toMetaName(text) {
|
|
18
|
+
return text
|
|
19
|
+
.toLowerCase()
|
|
20
|
+
.replace(/[^0-9a-z:_]/gi, '-');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function applyMetaRule(target, obj) {
|
|
24
|
+
Object.keys(obj).forEach((key) => {
|
|
25
|
+
const metaKey = toMetaName(key);
|
|
26
|
+
if (metaKey !== 'url' && obj[key]) {
|
|
27
|
+
target[metaKey] = obj[key];
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function globToRegExp(glob) {
|
|
33
|
+
const reString = glob
|
|
34
|
+
.replace(/\*\*/g, '_')
|
|
35
|
+
.replace(/\*/g, '[0-9a-z-.]*')
|
|
36
|
+
.replace(/_/g, '.*');
|
|
37
|
+
return new RegExp(`^${reString}$`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function filterGlobalMetadata(metaRules, path) {
|
|
41
|
+
const metaConfig = {};
|
|
42
|
+
metaRules.forEach((rule) => {
|
|
43
|
+
const glob = rule.url || rule.URL || rule.Url;
|
|
44
|
+
if (glob && typeof glob === 'string' && /[0-9a-z-/*]/.test(glob)) {
|
|
45
|
+
if (glob.indexOf('*') >= 0) {
|
|
46
|
+
if (globToRegExp(glob).test(path)) {
|
|
47
|
+
applyMetaRule(metaConfig, rule);
|
|
48
|
+
}
|
|
49
|
+
} else if (glob === path) {
|
|
50
|
+
applyMetaRule(metaConfig, rule);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
return metaConfig;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Array of headers allowed in the metadata.json file.
|
|
59
|
+
*/
|
|
60
|
+
export const ALLOWED_RESPONSE_HEADERS = [
|
|
61
|
+
'content-security-policy',
|
|
62
|
+
'content-security-policy-report-only',
|
|
63
|
+
'access-control-allow-origin',
|
|
64
|
+
'access-control-allow-methods',
|
|
65
|
+
'link',
|
|
66
|
+
];
|