@adobe/helix-admin-support 1.0.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/.eslintrc.cjs +31 -0
- package/.husky/pre-commit +4 -0
- package/.mocha-multi.json +6 -0
- package/.nycrc.json +10 -0
- package/.releaserc.cjs +16 -0
- package/CHANGELOG.md +11 -0
- package/CODE_OF_CONDUCT.md +74 -0
- package/CONTRIBUTING.md +74 -0
- package/LICENSE.txt +264 -0
- package/README.md +41 -0
- package/docs/API.md +12 -0
- package/package.json +71 -0
- package/src/contentbus.js +46 -0
- package/src/fetch-config.js +78 -0
- package/src/fetch-crontab.js +78 -0
- package/src/fetch-fstab.js +89 -0
- package/src/fetch-ignore.js +75 -0
- package/src/fetch-index.js +88 -0
- package/src/fetch-s3.js +103 -0
- package/src/fetch-sitemap.js +67 -0
- package/src/flatJson2Object.js +104 -0
- package/src/index.js +21 -0
- package/src/utils.js +38 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2021 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 { logLevelForStatusCode, propagateStatusCode } from '@adobe/helix-shared-utils';
|
|
13
|
+
import { fetchS3 } from './fetch-s3.js';
|
|
14
|
+
import { getContentBusId } from './contentbus.js';
|
|
15
|
+
import { flatJson2object } from './flatJson2Object.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Checks if the response is valid and returns the project config.
|
|
19
|
+
*
|
|
20
|
+
* @param {Logger} log logger
|
|
21
|
+
* @param {Response} response content response from the storage
|
|
22
|
+
* @param {string} key storage key (logging information)
|
|
23
|
+
* @param {string} type type of storage (logging information)
|
|
24
|
+
* @returns {Promise<ProjectConfig>} the helix project config or null
|
|
25
|
+
*/
|
|
26
|
+
async function handleResponse(log, response, key, type) {
|
|
27
|
+
const text = await response.text();
|
|
28
|
+
if (response.ok) {
|
|
29
|
+
let props;
|
|
30
|
+
try {
|
|
31
|
+
const json = JSON.parse(text);
|
|
32
|
+
props = json.data || json.default?.data;
|
|
33
|
+
// eslint-disable-next-line no-empty
|
|
34
|
+
} catch (ignore) { }
|
|
35
|
+
if (!props) {
|
|
36
|
+
log.info(`Empty or invalid .helix/config.json found in ${type} ${key}`);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
return flatJson2object(props);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (response.status === 404) {
|
|
43
|
+
log.info(`No .helix/config.json found in ${type} ${key}, ${text}`);
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
log[logLevelForStatusCode(response.status)](
|
|
48
|
+
`Invalid response (${response.status}) when fetching project configuration from ${type} ${key}`,
|
|
49
|
+
);
|
|
50
|
+
const err = new Error(`Unable to fetch project configuration: ${response.status} ${text}`);
|
|
51
|
+
err.status = propagateStatusCode(response.status);
|
|
52
|
+
throw err;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Retrieves the project config from the underlying storage
|
|
57
|
+
* and stores it in the context as projectConfig.
|
|
58
|
+
* @param {AdminContext} ctx the context
|
|
59
|
+
* @param {LookupOptions} opts lookup options
|
|
60
|
+
* @returns {Promise<object>} the project configuration
|
|
61
|
+
*/
|
|
62
|
+
export async function fetchProjectConfig(ctx, opts) {
|
|
63
|
+
const { log, attributes } = ctx;
|
|
64
|
+
const getConfig = async () => {
|
|
65
|
+
const contentBusId = await getContentBusId(ctx, opts, true);
|
|
66
|
+
if (!contentBusId) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
const key = `${contentBusId}/preview/.helix/config.json`;
|
|
70
|
+
const response = await fetchS3(ctx, 'helix-content-bus', key);
|
|
71
|
+
return handleResponse(log, response, key, 'content-bus');
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
if (attributes.projectConfig === undefined) {
|
|
75
|
+
attributes.projectConfig = await getConfig();
|
|
76
|
+
}
|
|
77
|
+
return attributes.projectConfig;
|
|
78
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2021 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 { logLevelForStatusCode, propagateStatusCode } from '@adobe/helix-shared-utils';
|
|
13
|
+
|
|
14
|
+
import { fetchS3 } from './fetch-s3.js';
|
|
15
|
+
import { getContentBusId } from './contentbus.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Checks if the response is valid and returns the project config.
|
|
19
|
+
*
|
|
20
|
+
* @param {Logger} log logger
|
|
21
|
+
* @param {Response} response content response from the storage
|
|
22
|
+
* @param {string} key storage key (logging information)
|
|
23
|
+
* @param {string} type type of storage (logging information)
|
|
24
|
+
* @returns {Promise<ProjectConfig>} the helix project config or null
|
|
25
|
+
*/
|
|
26
|
+
async function handleResponse(log, response, key, type) {
|
|
27
|
+
const text = await response.text();
|
|
28
|
+
if (response.ok) {
|
|
29
|
+
let props;
|
|
30
|
+
try {
|
|
31
|
+
const json = JSON.parse(text);
|
|
32
|
+
props = json.data;
|
|
33
|
+
// eslint-disable-next-line no-empty
|
|
34
|
+
} catch (ignore) { }
|
|
35
|
+
if (!props) {
|
|
36
|
+
log.info(`Empty or invalid .helix/crontab.json found in ${type} ${key}`);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
return props;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (response.status === 404) {
|
|
43
|
+
log.info(`No .helix/crontab.json found in ${type} ${key}, ${text}`);
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
log[logLevelForStatusCode(response.status)](
|
|
48
|
+
`Invalid response (${response.status}) when fetching crontab from ${type} ${key}`,
|
|
49
|
+
);
|
|
50
|
+
const err = new Error(`Unable to fetch crontab: ${response.status} ${text}`);
|
|
51
|
+
err.status = propagateStatusCode(response.status);
|
|
52
|
+
throw err;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Retrieves the project config from the underlying storage
|
|
57
|
+
* and stores it in the context as projectConfig.
|
|
58
|
+
* @param {AdminContext} ctx the context
|
|
59
|
+
* @param {LookupOptions} opts lookup options
|
|
60
|
+
* @returns {Promise<ProjectConfig>} the project configuration
|
|
61
|
+
*/
|
|
62
|
+
export async function fetchCrontab(ctx, opts) {
|
|
63
|
+
const { log, attributes } = ctx;
|
|
64
|
+
const getConfig = async () => {
|
|
65
|
+
const contentBusId = await getContentBusId(ctx, opts, true);
|
|
66
|
+
if (!contentBusId) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
const key = `${contentBusId}/preview/.helix/crontab.json`;
|
|
70
|
+
const response = await fetchS3(ctx, 'helix-content-bus', key);
|
|
71
|
+
return handleResponse(log, response, key, 'content-bus');
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
if (attributes.crontab === undefined) {
|
|
75
|
+
attributes.crontab = await getConfig();
|
|
76
|
+
}
|
|
77
|
+
return attributes.crontab;
|
|
78
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2021 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 { logLevelForStatusCode, propagateStatusCode } from '@adobe/helix-shared-utils';
|
|
13
|
+
import { MountConfig } from '@adobe/helix-shared-config';
|
|
14
|
+
import { fetchS3 } from './fetch-s3.js';
|
|
15
|
+
import { fetch } from './utils.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Checks if the response is valid and returns a mount config.
|
|
19
|
+
*
|
|
20
|
+
* @param {Logger} log logger
|
|
21
|
+
* @param {Response} response content response from the storage
|
|
22
|
+
* @param {string} key storage key (logging information)
|
|
23
|
+
* @param {string} type type of storage (logging information
|
|
24
|
+
* @returns {Promise<MountConfig>} the mount config or null
|
|
25
|
+
*/
|
|
26
|
+
async function handleResponse(log, response, key, type) {
|
|
27
|
+
const text = await response.text();
|
|
28
|
+
if (response.ok) {
|
|
29
|
+
const cfg = await new MountConfig().withSource(text).init();
|
|
30
|
+
cfg.sourceType = type;
|
|
31
|
+
return cfg;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (response.status === 404) {
|
|
35
|
+
log.info(`No fstab.yaml found in ${type} ${key}, ${text}`);
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
log[logLevelForStatusCode(response.status)](`Invalid response (${response.status}) when fetching fstab from ${type} ${key}`);
|
|
40
|
+
const err = new Error(`Unable to fetch fstab: ${response.status} ${text}`);
|
|
41
|
+
err.status = propagateStatusCode(response.status);
|
|
42
|
+
throw err;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Retrieves the fstab from the underlying storage and stores it in the context as mountConfig.
|
|
47
|
+
* @param {AdminContext} ctx the context
|
|
48
|
+
* @param {LookupOptions} opts lookup options
|
|
49
|
+
* @param {boolean} optional if {@code true}, no error is thrown if the fstab can't be retrieved
|
|
50
|
+
* @returns {Promise<MountConfig>} the fstab
|
|
51
|
+
*/
|
|
52
|
+
export async function fetchFstab(ctx, opts, optional) {
|
|
53
|
+
const { log, attributes } = ctx;
|
|
54
|
+
const loadFstab = async () => {
|
|
55
|
+
const { owner, repo } = opts;
|
|
56
|
+
if (!owner || !repo) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let key = `${owner}/${repo}/main/fstab.yaml`;
|
|
61
|
+
let response = await fetchS3(ctx, 'helix-code-bus', key);
|
|
62
|
+
const cfg = await handleResponse(log, response, key, 'code-bus');
|
|
63
|
+
if (cfg) {
|
|
64
|
+
return cfg;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// try loading from github for non helix-3 repos
|
|
68
|
+
const headers = {};
|
|
69
|
+
key = `${owner}/${repo}/main/fstab.yaml`;
|
|
70
|
+
if (ctx.githubToken) {
|
|
71
|
+
headers.authorization = `token ${ctx.githubToken}`;
|
|
72
|
+
}
|
|
73
|
+
response = await fetch(`https://raw.githubusercontent.com/${key}`, {
|
|
74
|
+
cache: 'no-store',
|
|
75
|
+
headers,
|
|
76
|
+
});
|
|
77
|
+
return handleResponse(log, response, key, 'github');
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
if (!('mountConfig' in attributes)) {
|
|
81
|
+
attributes.mountConfig = await loadFstab(ctx, opts);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (attributes.mountConfig || optional) {
|
|
85
|
+
return attributes.mountConfig;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
throw Error(`no fstab for ${opts.owner}/${opts.repo}/main/fstab.yaml`);
|
|
89
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2021 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 { logLevelForStatusCode, propagateStatusCode } from '@adobe/helix-shared-utils';
|
|
13
|
+
import { IgnoreConfig } from '@adobe/helix-shared-config';
|
|
14
|
+
import { fetch } from './utils.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Checks if the response is valid and returns a IgnoreConfig.
|
|
18
|
+
*
|
|
19
|
+
* @param {Logger} log logger
|
|
20
|
+
* @param {Response} response content response from the storage
|
|
21
|
+
* @param {string} key storage key (logging information)
|
|
22
|
+
* @returns {Promise<MountConfig>} the IgnoreConfig
|
|
23
|
+
*
|
|
24
|
+
* @throws {Error('Unable to fetch hlxignore')}
|
|
25
|
+
*/
|
|
26
|
+
async function handleResponse(log, response, key) {
|
|
27
|
+
const text = await response.text();
|
|
28
|
+
if (response.ok) {
|
|
29
|
+
return new IgnoreConfig().withSource(text).init();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (response.status === 404) {
|
|
33
|
+
log.info(`No .hlxignore found in github ${key}, ${text}`);
|
|
34
|
+
return new IgnoreConfig().withSource('').init();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
log[logLevelForStatusCode(response.status)](`Invalid response (${response.status}) when fetching hlxignore from github ${key}`);
|
|
38
|
+
const err = new Error(`Unable to fetch hlxignore: ${response.status} ${text}`);
|
|
39
|
+
err.status = propagateStatusCode(response.status);
|
|
40
|
+
throw err;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Retrieves the hlxignore from github and stores it in the context as IgnoreConfig.
|
|
45
|
+
* If 404 is encountered, returns an empty IgnoreConfig that considers nothing ignored.
|
|
46
|
+
*
|
|
47
|
+
* @param {AdminContext} ctx the context
|
|
48
|
+
* @param {LookupOptions} opts lookup options
|
|
49
|
+
* @returns {Promise<IgnoreConfig>} the IgnoreConfig
|
|
50
|
+
*
|
|
51
|
+
* @throws
|
|
52
|
+
*/
|
|
53
|
+
export async function fetchIgnore(ctx, opts) {
|
|
54
|
+
const { attributes, log } = ctx;
|
|
55
|
+
const {
|
|
56
|
+
owner, repo, ref, branch,
|
|
57
|
+
} = opts;
|
|
58
|
+
|
|
59
|
+
if (attributes.ignoreConfig) {
|
|
60
|
+
return attributes.ignoreConfig;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const key = `${owner}/${repo}/${branch || ref}/.hlxignore`;
|
|
64
|
+
const headers = {};
|
|
65
|
+
if (ctx.githubToken) {
|
|
66
|
+
headers.authorization = `token ${ctx.githubToken}`;
|
|
67
|
+
}
|
|
68
|
+
const response = await fetch(`https://raw.githubusercontent.com/${key}`, {
|
|
69
|
+
cache: 'no-store',
|
|
70
|
+
headers,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
attributes.ignoreConfig = await handleResponse(log, response, key);
|
|
74
|
+
return attributes.ignoreConfig;
|
|
75
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2021 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 { logLevelForStatusCode, propagateStatusCode } from '@adobe/helix-shared-utils';
|
|
13
|
+
import { IndexConfig } from '@adobe/helix-shared-config';
|
|
14
|
+
import { fetchS3 } from './fetch-s3.js';
|
|
15
|
+
import { fetch } from './utils.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Name of the indexing configuration, subject to be renamed.
|
|
19
|
+
*/
|
|
20
|
+
export const INDEX_CONFIGURATION_NAME = 'helix-query.yaml';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Checks if the response is valid and returns an index config.
|
|
24
|
+
*
|
|
25
|
+
* @param {Logger} log logger
|
|
26
|
+
* @param {Response} response content response from the storage
|
|
27
|
+
* @param {string} key storage key (logging information)
|
|
28
|
+
* @param {string} type type of storage (logging information
|
|
29
|
+
* @returns {Promise<IndexConfig>} the index config or null
|
|
30
|
+
*/
|
|
31
|
+
async function handleResponse(log, response, key, type) {
|
|
32
|
+
const text = await response.text();
|
|
33
|
+
if (response.ok) {
|
|
34
|
+
return new IndexConfig().withSource(text).init();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (response.status === 404) {
|
|
38
|
+
log.info(`No index configuration found in ${type} ${key}, ${text}`);
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
log[logLevelForStatusCode(response.status)](`Invalid response (${response.status}) when fetching index configuration from ${type} ${key}`);
|
|
43
|
+
const err = new Error(`Unable to fetch index configuration: ${response.status} ${text}`);
|
|
44
|
+
err.status = propagateStatusCode(response.status);
|
|
45
|
+
throw err;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Retrieves the indexing configuration from the underlying storage and stores it in the context as
|
|
50
|
+
* indexConfig.
|
|
51
|
+
*
|
|
52
|
+
* @param {AdminContext} ctx the context
|
|
53
|
+
* @param {LookupOptions} opts lookup options
|
|
54
|
+
* @returns {Promise<IndexConfig>} the index config
|
|
55
|
+
*/
|
|
56
|
+
export async function fetchIndex(ctx, opts) {
|
|
57
|
+
const { attributes, log } = ctx;
|
|
58
|
+
const {
|
|
59
|
+
owner, repo, ref, branch,
|
|
60
|
+
} = opts;
|
|
61
|
+
|
|
62
|
+
if (attributes.indexConfig) {
|
|
63
|
+
return attributes.indexConfig;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const key = `${owner}/${repo}/${branch || ref}/${INDEX_CONFIGURATION_NAME}`;
|
|
67
|
+
let response = await fetchS3(ctx, 'helix-code-bus', key);
|
|
68
|
+
attributes.indexConfig = await handleResponse(log, response, key, 'code-bus');
|
|
69
|
+
if (attributes.indexConfig) {
|
|
70
|
+
return attributes.indexConfig;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// try loading from github for non helix-3 repos
|
|
74
|
+
const headers = {};
|
|
75
|
+
if (ctx.githubToken) {
|
|
76
|
+
headers.authorization = `token ${ctx.githubToken}`;
|
|
77
|
+
}
|
|
78
|
+
response = await fetch(`https://raw.githubusercontent.com/${key}`, {
|
|
79
|
+
cache: 'no-store',
|
|
80
|
+
headers,
|
|
81
|
+
});
|
|
82
|
+
attributes.indexConfig = await handleResponse(log, response, key, 'github');
|
|
83
|
+
if (attributes.indexConfig) {
|
|
84
|
+
return attributes.indexConfig;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
throw Error(`no index configuration for ${owner}/${repo}/${ref}`);
|
|
88
|
+
}
|
package/src/fetch-s3.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2021 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 { promisify } from 'util';
|
|
13
|
+
import zlib from 'zlib';
|
|
14
|
+
import { Response } from '@adobe/helix-fetch';
|
|
15
|
+
import {
|
|
16
|
+
S3Client,
|
|
17
|
+
GetObjectCommand,
|
|
18
|
+
HeadObjectCommand,
|
|
19
|
+
NoSuchKey,
|
|
20
|
+
} from '@aws-sdk/client-s3';
|
|
21
|
+
|
|
22
|
+
const gunzip = promisify(zlib.gunzip);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Fetches content from s3
|
|
26
|
+
* @oaram {AdminContext} context the admin context
|
|
27
|
+
* @param {string} bucketId bucket id
|
|
28
|
+
* @param {string} key the resource key
|
|
29
|
+
* @param {boolean} head flag indicating to create a head request
|
|
30
|
+
* @return {Promise<Response>} fetch api response
|
|
31
|
+
*/
|
|
32
|
+
export async function fetchS3(context, bucketId, key, head = false) {
|
|
33
|
+
if (!bucketId) {
|
|
34
|
+
throw new Error('Unknown bucketId, cannot fetch content');
|
|
35
|
+
}
|
|
36
|
+
if (!key) {
|
|
37
|
+
throw new Error('Unknown key, cannot fetch content');
|
|
38
|
+
}
|
|
39
|
+
const { log, env = {} } = context;
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
// the client initialization is a bit picky with undefined properties.
|
|
43
|
+
// so we omit the credentials when we provide them via context.env
|
|
44
|
+
/* c8 ignore next */
|
|
45
|
+
const opts = !env.AWS_S3_ACCESS_KEY_ID ? {} : {
|
|
46
|
+
region: env.AWS_S3_REGION,
|
|
47
|
+
credentials: {
|
|
48
|
+
accessKeyId: env.AWS_S3_ACCESS_KEY_ID,
|
|
49
|
+
secretAccessKey: env.AWS_S3_SECRET_ACCESS_KEY,
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
const s3 = new S3Client(opts);
|
|
53
|
+
const Command = head ? HeadObjectCommand : GetObjectCommand;
|
|
54
|
+
const res = await s3.send(new Command({
|
|
55
|
+
Bucket: bucketId,
|
|
56
|
+
Key: key,
|
|
57
|
+
// todo: add timeout
|
|
58
|
+
}));
|
|
59
|
+
|
|
60
|
+
let buffer = '';
|
|
61
|
+
if (!head) {
|
|
62
|
+
buffer = await new Response(res.Body, {}).buffer();
|
|
63
|
+
if (res.ContentEncoding === 'gzip') {
|
|
64
|
+
buffer = await gunzip(buffer);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const headers = {};
|
|
68
|
+
if (res.LastModified) {
|
|
69
|
+
headers['last-modified'] = res.LastModified.toUTCString();
|
|
70
|
+
}
|
|
71
|
+
if (res.ContentType) {
|
|
72
|
+
headers['content-type'] = res.ContentType;
|
|
73
|
+
}
|
|
74
|
+
if (res.ContentLength) {
|
|
75
|
+
headers['x-source-content-length'] = res.ContentLength;
|
|
76
|
+
}
|
|
77
|
+
if (res.Metadata) {
|
|
78
|
+
Object.entries(res.Metadata).forEach(([name, value]) => {
|
|
79
|
+
headers[name] = value;
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return new Response(buffer, {
|
|
83
|
+
status: 200,
|
|
84
|
+
headers,
|
|
85
|
+
});
|
|
86
|
+
} catch (e) {
|
|
87
|
+
const code = e.Code ?? e.$metadata?.httpStatusCode ?? 500;
|
|
88
|
+
if (code === 'AccessDenied' || code === 404 || e instanceof NoSuchKey) {
|
|
89
|
+
log.info(`Could not find file at ${bucketId}/${key}: ${code}`);
|
|
90
|
+
return new Response('', {
|
|
91
|
+
status: 404,
|
|
92
|
+
});
|
|
93
|
+
} else {
|
|
94
|
+
log.error(`Error while fetching file from ${bucketId}/${key}: ${code} (${e.message})`);
|
|
95
|
+
return new Response('', {
|
|
96
|
+
status: 502,
|
|
97
|
+
headers: {
|
|
98
|
+
'x-error': `error while fetching: ${code}`,
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2021 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 { logLevelForStatusCode, propagateStatusCode } from '@adobe/helix-shared-utils';
|
|
13
|
+
import { SitemapConfig } from '@adobe/helix-shared-config';
|
|
14
|
+
import { fetchS3 } from './fetch-s3.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Checks if the response is valid and returns a sitemap config.
|
|
18
|
+
*
|
|
19
|
+
* @param {Logger} log logger
|
|
20
|
+
* @param {Response} response content response from the storage
|
|
21
|
+
* @param {string} key storage key (logging information)
|
|
22
|
+
* @param {string} type type of storage (logging information
|
|
23
|
+
* @returns {Promise<SitemapConfig>} the sitemap config or null
|
|
24
|
+
*/
|
|
25
|
+
async function handleResponse(log, response, key, type) {
|
|
26
|
+
const text = await response.text();
|
|
27
|
+
if (response.ok) {
|
|
28
|
+
const cfg = await new SitemapConfig().withSource(text).init();
|
|
29
|
+
cfg.sourceType = type;
|
|
30
|
+
return cfg;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (response.status === 404) {
|
|
34
|
+
log.info(`No helix-sitemap.yaml found in ${type} ${key}, ${text}`);
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
log[logLevelForStatusCode(response.status)](
|
|
39
|
+
`Invalid response (${response.status}) when fetching sitemap configuration from ${type} ${key}`,
|
|
40
|
+
);
|
|
41
|
+
const err = new Error(`Unable to fetch sitemap configuration: ${response.status} ${text}`);
|
|
42
|
+
err.status = propagateStatusCode(response.status);
|
|
43
|
+
throw err;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Retrieves the sitemap from the underlying storage and stores it in the context as sitemapConfig.
|
|
48
|
+
* @param {AdminContext} ctx the context
|
|
49
|
+
* @param {LookupOptions} opts lookup options
|
|
50
|
+
* @returns {Promise<SitemapConfig>} the sitemap configuration
|
|
51
|
+
*/
|
|
52
|
+
export async function fetchSitemap(ctx, opts) {
|
|
53
|
+
const { attributes, log } = ctx;
|
|
54
|
+
const { owner, repo, ref } = opts;
|
|
55
|
+
|
|
56
|
+
if (attributes.sitemapConfig) {
|
|
57
|
+
return attributes.sitemapConfig;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const key = `${owner}/${repo}/${ref}/helix-sitemap.yaml`;
|
|
61
|
+
const response = await fetchS3(ctx, 'helix-code-bus', key);
|
|
62
|
+
attributes.sitemapConfig = await handleResponse(log, response, key, 'code-bus');
|
|
63
|
+
if (attributes.sitemapConfig) {
|
|
64
|
+
return attributes.sitemapConfig;
|
|
65
|
+
}
|
|
66
|
+
throw Error(`no sitemap configuration for ${key}`);
|
|
67
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
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
|
+
/**
|
|
14
|
+
* Converts a config row-based name/value pair array with dotted key notation to an object.
|
|
15
|
+
*
|
|
16
|
+
* example:
|
|
17
|
+
*
|
|
18
|
+
* ```
|
|
19
|
+
* host: www.adobe.com
|
|
20
|
+
* document.level: 42,
|
|
21
|
+
* document.title: hello
|
|
22
|
+
* document.body.status: 200
|
|
23
|
+
* acl.allow: /foo
|
|
24
|
+
* acl.allow: /bar
|
|
25
|
+
* cnd.0.type: fastly
|
|
26
|
+
* cnd.0.host: www.adobe.com
|
|
27
|
+
* cnd.1.type: cloudflare
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* becomes:
|
|
31
|
+
*
|
|
32
|
+
* ```
|
|
33
|
+
* {
|
|
34
|
+
* host: 'www.adobe.com',
|
|
35
|
+
* document: {
|
|
36
|
+
* level: 42,
|
|
37
|
+
* title: 'hello',
|
|
38
|
+
* body: {
|
|
39
|
+
* status: 200,
|
|
40
|
+
* }
|
|
41
|
+
* },
|
|
42
|
+
* acl: {
|
|
43
|
+
* allow: ['/foo', '/bar],
|
|
44
|
+
* }
|
|
45
|
+
* cnd: [
|
|
46
|
+
* { type: 'fastly', host: 'www.adobe.com' },
|
|
47
|
+
* { type: 'cloudflare' },
|
|
48
|
+
* ]
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @param {object[]} rows array of columns
|
|
53
|
+
* @param {string} [keyName = 'key'] name of the key column.
|
|
54
|
+
* @param {string} [valueName = 'value'] name of the value column.
|
|
55
|
+
*/
|
|
56
|
+
export function flatJson2object(rows, keyName = 'key', valueName = 'value') {
|
|
57
|
+
const arrays = [];
|
|
58
|
+
const update = (o, segs, value) => {
|
|
59
|
+
const seg = segs.shift();
|
|
60
|
+
if (segs.length) {
|
|
61
|
+
if (seg in o) {
|
|
62
|
+
update(o[seg], segs, value);
|
|
63
|
+
return o;
|
|
64
|
+
}
|
|
65
|
+
const n = Number.parseInt(segs[0], 10);
|
|
66
|
+
if (Number.isNaN(n)) {
|
|
67
|
+
// eslint-disable-next-line no-param-reassign
|
|
68
|
+
o[seg] = update({}, segs, value);
|
|
69
|
+
} else {
|
|
70
|
+
// eslint-disable-next-line no-param-reassign
|
|
71
|
+
o[seg] = update([], segs, value);
|
|
72
|
+
arrays.push(o[seg]);
|
|
73
|
+
}
|
|
74
|
+
return o;
|
|
75
|
+
}
|
|
76
|
+
if (!(seg in o)) {
|
|
77
|
+
// eslint-disable-next-line no-param-reassign
|
|
78
|
+
o[seg] = value;
|
|
79
|
+
} else if (Array.isArray(o[seg])) {
|
|
80
|
+
// eslint-disable-next-line no-param-reassign
|
|
81
|
+
o[seg].push(value);
|
|
82
|
+
} else {
|
|
83
|
+
// eslint-disable-next-line no-param-reassign
|
|
84
|
+
o[seg] = [o[seg], value];
|
|
85
|
+
}
|
|
86
|
+
return o;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const obj = {};
|
|
90
|
+
rows.forEach((row) => {
|
|
91
|
+
update(obj, String(row[keyName]).trim().split('.'), String(row[valueName]).trim());
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// compact arrays
|
|
95
|
+
arrays.forEach((a) => {
|
|
96
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
97
|
+
if (!(i in a)) {
|
|
98
|
+
a.splice(i, 1);
|
|
99
|
+
i -= 1;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
return obj;
|
|
104
|
+
}
|