contentful-export 7.18.17 → 7.18.19
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/dist/index.js +2 -32
- package/dist/parseOptions.js +4 -28
- package/dist/tasks/download-assets.js +8 -28
- package/dist/tasks/get-space-data.js +2 -24
- package/dist/tasks/init-client.js +0 -12
- package/dist/usageParams.js +0 -5
- package/dist/utils/embargoedAssets.js +1 -19
- package/dist/utils/headers.js +2 -7
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -4,61 +4,41 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = runContentfulExport;
|
7
|
-
|
8
7
|
var _fs = require("fs");
|
9
|
-
|
10
8
|
var _bfj = _interopRequireDefault(require("bfj"));
|
11
|
-
|
12
9
|
var _bluebird = _interopRequireDefault(require("bluebird"));
|
13
|
-
|
14
10
|
var _cliTable = _interopRequireDefault(require("cli-table3"));
|
15
|
-
|
16
11
|
var _listr = _interopRequireDefault(require("listr"));
|
17
|
-
|
18
12
|
var _listrUpdateRenderer = _interopRequireDefault(require("listr-update-renderer"));
|
19
|
-
|
20
13
|
var _listrVerboseRenderer = _interopRequireDefault(require("listr-verbose-renderer"));
|
21
|
-
|
22
14
|
var _lodash = _interopRequireDefault(require("lodash.startcase"));
|
23
|
-
|
24
15
|
var _mkdirp = _interopRequireDefault(require("mkdirp"));
|
25
|
-
|
26
16
|
var _differenceInSeconds = _interopRequireDefault(require("date-fns/differenceInSeconds"));
|
27
|
-
|
28
17
|
var _formatDistance = _interopRequireDefault(require("date-fns/formatDistance"));
|
29
|
-
|
30
18
|
var _contentfulBatchLibs = require("contentful-batch-libs");
|
31
|
-
|
32
19
|
var _downloadAssets = _interopRequireDefault(require("./tasks/download-assets"));
|
33
|
-
|
34
20
|
var _getSpaceData = _interopRequireDefault(require("./tasks/get-space-data"));
|
35
|
-
|
36
21
|
var _initClient = _interopRequireDefault(require("./tasks/init-client"));
|
37
|
-
|
38
22
|
var _parseOptions = _interopRequireDefault(require("./parseOptions"));
|
39
|
-
|
40
23
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
41
|
-
|
42
24
|
const accessP = _bluebird.default.promisify(_fs.access);
|
43
|
-
|
44
25
|
function createListrOptions(options) {
|
45
26
|
if (options.useVerboseRenderer) {
|
46
27
|
return {
|
47
28
|
renderer: _listrVerboseRenderer.default
|
48
29
|
};
|
49
30
|
}
|
50
|
-
|
51
31
|
return {
|
52
32
|
renderer: _listrUpdateRenderer.default,
|
53
33
|
collapse: false
|
54
34
|
};
|
55
35
|
}
|
56
|
-
|
57
36
|
function runContentfulExport(params) {
|
58
37
|
const log = [];
|
59
38
|
const options = (0, _parseOptions.default)(params);
|
60
|
-
const listrOptions = createListrOptions(options);
|
39
|
+
const listrOptions = createListrOptions(options);
|
61
40
|
|
41
|
+
// Setup custom error listener to store errors for later
|
62
42
|
(0, _contentfulBatchLibs.setupLogging)(log);
|
63
43
|
const tasks = new _listr.default([{
|
64
44
|
title: 'Initialize client',
|
@@ -66,12 +46,10 @@ function runContentfulExport(params) {
|
|
66
46
|
try {
|
67
47
|
// CMA client
|
68
48
|
ctx.client = (0, _initClient.default)(options);
|
69
|
-
|
70
49
|
if (options.deliveryToken && !options.includeDrafts) {
|
71
50
|
// CDA client for fetching only public entries
|
72
51
|
ctx.cdaClient = (0, _initClient.default)(options, true);
|
73
52
|
}
|
74
|
-
|
75
53
|
return _bluebird.default.resolve();
|
76
54
|
} catch (err) {
|
77
55
|
return _bluebird.default.reject(err);
|
@@ -136,7 +114,6 @@ function runContentfulExport(params) {
|
|
136
114
|
data: {}
|
137
115
|
}).then(ctx => {
|
138
116
|
const resultTypes = Object.keys(ctx.data);
|
139
|
-
|
140
117
|
if (resultTypes.length) {
|
141
118
|
const resultTable = new _cliTable.default();
|
142
119
|
resultTable.push([{
|
@@ -150,7 +127,6 @@ function runContentfulExport(params) {
|
|
150
127
|
} else {
|
151
128
|
console.log('No data was exported');
|
152
129
|
}
|
153
|
-
|
154
130
|
if ('assetDownloads' in ctx) {
|
155
131
|
const downloadsTable = new _cliTable.default();
|
156
132
|
downloadsTable.push([{
|
@@ -162,16 +138,13 @@ function runContentfulExport(params) {
|
|
162
138
|
downloadsTable.push(['Errors ', ctx.assetDownloads.errorCount]);
|
163
139
|
console.log(downloadsTable.toString());
|
164
140
|
}
|
165
|
-
|
166
141
|
const endTime = new Date();
|
167
142
|
const durationHuman = (0, _formatDistance.default)(endTime, options.startTime);
|
168
143
|
const durationSeconds = (0, _differenceInSeconds.default)(endTime, options.startTime);
|
169
144
|
console.log(`The export took ${durationHuman} (${durationSeconds}s)`);
|
170
|
-
|
171
145
|
if (options.saveFile) {
|
172
146
|
console.log(`\nStored space data to json file at: ${options.logFilePath}`);
|
173
147
|
}
|
174
|
-
|
175
148
|
return ctx.data;
|
176
149
|
}).catch(err => {
|
177
150
|
log.push({
|
@@ -184,7 +157,6 @@ function runContentfulExport(params) {
|
|
184
157
|
const errorLog = log.filter(logMessage => logMessage.level !== 'info' && logMessage.level !== 'warning');
|
185
158
|
const displayLog = log.filter(logMessage => logMessage.level !== 'info');
|
186
159
|
(0, _contentfulBatchLibs.displayErrorLog)(displayLog);
|
187
|
-
|
188
160
|
if (errorLog.length) {
|
189
161
|
return (0, _contentfulBatchLibs.writeErrorLogFile)(options.errorLogFile, errorLog).then(() => {
|
190
162
|
const multiError = new Error('Errors occured');
|
@@ -193,10 +165,8 @@ function runContentfulExport(params) {
|
|
193
165
|
throw multiError;
|
194
166
|
});
|
195
167
|
}
|
196
|
-
|
197
168
|
console.log('The export was successful.');
|
198
169
|
return data;
|
199
170
|
});
|
200
171
|
}
|
201
|
-
|
202
172
|
module.exports = exports.default;
|
package/dist/parseOptions.js
CHANGED
@@ -4,27 +4,16 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = parseOptions;
|
7
|
-
|
8
7
|
var _contentfulBatchLibs = require("contentful-batch-libs");
|
9
|
-
|
10
8
|
var _format = _interopRequireDefault(require("date-fns/format"));
|
11
|
-
|
12
9
|
var _path = require("path");
|
13
|
-
|
14
10
|
var _querystring = _interopRequireDefault(require("querystring"));
|
15
|
-
|
16
11
|
var _package = require("../package");
|
17
|
-
|
18
12
|
var _headers = require("./utils/headers");
|
19
|
-
|
20
13
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21
|
-
|
22
14
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
23
|
-
|
24
15
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
25
|
-
|
26
16
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
27
|
-
|
28
17
|
function parseOptions(params) {
|
29
18
|
const defaultOptions = {
|
30
19
|
environmentId: 'master',
|
@@ -42,68 +31,55 @@ function parseOptions(params) {
|
|
42
31
|
rawProxy: false
|
43
32
|
};
|
44
33
|
const configFile = params.config ? require((0, _path.resolve)(process.cwd(), params.config)) : {};
|
45
|
-
|
46
34
|
const options = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, defaultOptions), configFile), params), {}, {
|
47
35
|
headers: (0, _contentfulBatchLibs.addSequenceHeader)(params.headers || (0, _headers.getHeadersConfig)(params.header))
|
48
|
-
});
|
49
|
-
|
36
|
+
});
|
50
37
|
|
38
|
+
// Validation
|
51
39
|
if (!options.spaceId) {
|
52
40
|
throw new Error('The `spaceId` option is required.');
|
53
41
|
}
|
54
|
-
|
55
42
|
if (!options.managementToken) {
|
56
43
|
throw new Error('The `managementToken` option is required.');
|
57
44
|
}
|
58
|
-
|
59
45
|
const proxySimpleExp = /.+:\d+/;
|
60
46
|
const proxyAuthExp = /.+:.+@.+:\d+/;
|
61
|
-
|
62
47
|
if (options.proxy && !(proxySimpleExp.test(options.proxy) || proxyAuthExp.test(options.proxy))) {
|
63
48
|
throw new Error('Please provide the proxy config in the following format:\nhost:port or user:password@host:port');
|
64
49
|
}
|
65
|
-
|
66
50
|
options.startTime = new Date();
|
67
51
|
options.contentFile = options.contentFile || `contentful-export-${options.spaceId}-${options.environmentId}-${(0, _format.default)(options.startTime, "yyyy-MM-dd'T'HH-mm-ss")}.json`;
|
68
52
|
options.logFilePath = (0, _path.resolve)(options.exportDir, options.contentFile);
|
69
|
-
|
70
53
|
if (!options.errorLogFile) {
|
71
54
|
options.errorLogFile = (0, _path.resolve)(options.exportDir, `contentful-export-error-log-${options.spaceId}-${options.environmentId}-${(0, _format.default)(options.startTime, "yyyy-MM-dd'T'HH-mm-ss")}.json`);
|
72
55
|
} else {
|
73
56
|
options.errorLogFile = (0, _path.resolve)(process.cwd(), options.errorLogFile);
|
74
|
-
}
|
75
|
-
|
57
|
+
}
|
76
58
|
|
59
|
+
// Further processing
|
77
60
|
options.accessToken = options.managementToken;
|
78
|
-
|
79
61
|
if (typeof options.proxy === 'string') {
|
80
62
|
options.proxy = (0, _contentfulBatchLibs.proxyStringToObject)(options.proxy);
|
81
63
|
}
|
82
|
-
|
83
64
|
if (!options.rawProxy && options.proxy) {
|
84
65
|
options.httpsAgent = (0, _contentfulBatchLibs.agentFromProxy)(options.proxy);
|
85
66
|
delete options.proxy;
|
86
67
|
}
|
87
|
-
|
88
68
|
if (options.queryEntries && options.queryEntries.length > 0) {
|
89
69
|
const querystr = options.queryEntries.join('&');
|
90
70
|
options.queryEntries = _querystring.default.parse(querystr);
|
91
71
|
}
|
92
|
-
|
93
72
|
if (options.queryAssets && options.queryAssets.length > 0) {
|
94
73
|
const querystr = options.queryAssets.join('&');
|
95
74
|
options.queryAssets = _querystring.default.parse(querystr);
|
96
75
|
}
|
97
|
-
|
98
76
|
if (options.contentOnly) {
|
99
77
|
options.skipRoles = true;
|
100
78
|
options.skipContentModel = true;
|
101
79
|
options.skipWebhooks = true;
|
102
80
|
}
|
103
|
-
|
104
81
|
options.application = options.managementApplication || `contentful.export/${_package.version}`;
|
105
82
|
options.feature = options.managementFeature || 'library-export';
|
106
83
|
return options;
|
107
84
|
}
|
108
|
-
|
109
85
|
module.exports = exports.default;
|
@@ -4,29 +4,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = downloadAssets;
|
7
|
-
|
8
7
|
var _bluebird = _interopRequireDefault(require("bluebird"));
|
9
|
-
|
10
8
|
var _contentfulBatchLibs = require("contentful-batch-libs");
|
11
|
-
|
12
9
|
var _figures = _interopRequireDefault(require("figures"));
|
13
|
-
|
14
10
|
var _fs = require("fs");
|
15
|
-
|
16
11
|
var _nodeFetch = _interopRequireDefault(require("node-fetch"));
|
17
|
-
|
18
12
|
var _path = _interopRequireDefault(require("path"));
|
19
|
-
|
20
13
|
var _stream = require("stream");
|
21
|
-
|
22
14
|
var _util = require("util");
|
23
|
-
|
24
15
|
var _embargoedAssets = require("../utils/embargoedAssets");
|
25
|
-
|
26
16
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
27
|
-
|
28
17
|
const streamPipeline = (0, _util.promisify)(_stream.pipeline);
|
29
|
-
|
30
18
|
async function downloadAsset({
|
31
19
|
url,
|
32
20
|
directory
|
@@ -34,30 +22,28 @@ async function downloadAsset({
|
|
34
22
|
// handle urls without protocol
|
35
23
|
if (url.startsWith('//')) {
|
36
24
|
url = 'https:' + url;
|
37
|
-
}
|
38
|
-
|
25
|
+
}
|
39
26
|
|
27
|
+
// build local file path from the url for the download
|
40
28
|
const parsedUrl = new URL(url);
|
29
|
+
const localFile = _path.default.join(directory, parsedUrl.host, parsedUrl.pathname);
|
41
30
|
|
42
|
-
|
43
|
-
|
44
|
-
|
31
|
+
// ensure directory exists and create file stream
|
45
32
|
await _fs.promises.mkdir(_path.default.dirname(localFile), {
|
46
33
|
recursive: true
|
47
34
|
});
|
48
|
-
const file = (0, _fs.createWriteStream)(localFile);
|
35
|
+
const file = (0, _fs.createWriteStream)(localFile);
|
49
36
|
|
37
|
+
// download asset
|
50
38
|
const assetRequest = await (0, _nodeFetch.default)(url);
|
51
|
-
|
52
39
|
if (!assetRequest.ok) {
|
53
40
|
throw new Error(`error response status: ${assetRequest.status}`);
|
54
|
-
}
|
55
|
-
|
41
|
+
}
|
56
42
|
|
43
|
+
// Wait for stream to be consumed before returning local file
|
57
44
|
await streamPipeline(assetRequest.body, file);
|
58
45
|
return localFile;
|
59
46
|
}
|
60
|
-
|
61
47
|
function downloadAssets(options) {
|
62
48
|
return (ctx, task) => {
|
63
49
|
let successCount = 0;
|
@@ -69,22 +55,18 @@ function downloadAssets(options) {
|
|
69
55
|
warningCount++;
|
70
56
|
return;
|
71
57
|
}
|
72
|
-
|
73
58
|
const locales = Object.keys(asset.fields.file);
|
74
59
|
return _bluebird.default.mapSeries(locales, locale => {
|
75
60
|
const url = asset.fields.file[locale].url;
|
76
|
-
|
77
61
|
if (!url) {
|
78
62
|
task.output = `${_figures.default.cross} asset '${(0, _contentfulBatchLibs.getEntityName)(asset)}' doesn't contain an url in path asset.fields.file[${locale}].url`;
|
79
63
|
errorCount++;
|
80
64
|
return _bluebird.default.resolve();
|
81
65
|
}
|
82
|
-
|
83
66
|
let startingPromise = _bluebird.default.resolve({
|
84
67
|
url,
|
85
68
|
directory: options.exportDir
|
86
69
|
});
|
87
|
-
|
88
70
|
if ((0, _embargoedAssets.isEmbargoedAsset)(url)) {
|
89
71
|
const {
|
90
72
|
host,
|
@@ -98,7 +80,6 @@ function downloadAssets(options) {
|
|
98
80
|
directory: options.exportDir
|
99
81
|
}));
|
100
82
|
}
|
101
|
-
|
102
83
|
return startingPromise.then(downloadAsset).then(downLoadedFile => {
|
103
84
|
task.output = `${_figures.default.tick} downloaded ${(0, _contentfulBatchLibs.getEntityName)(downLoadedFile)} (${url})`;
|
104
85
|
successCount++;
|
@@ -118,5 +99,4 @@ function downloadAssets(options) {
|
|
118
99
|
});
|
119
100
|
};
|
120
101
|
}
|
121
|
-
|
122
102
|
module.exports = exports.default;
|
@@ -4,24 +4,18 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = getFullSourceSpace;
|
7
|
-
|
8
7
|
var _bluebird = _interopRequireDefault(require("bluebird"));
|
9
|
-
|
10
8
|
var _contentfulBatchLibs = require("contentful-batch-libs");
|
11
|
-
|
12
9
|
var _listr = _interopRequireDefault(require("listr"));
|
13
|
-
|
14
10
|
var _listrVerboseRenderer = _interopRequireDefault(require("listr-verbose-renderer"));
|
15
|
-
|
16
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17
|
-
|
18
12
|
const MAX_ALLOWED_LIMIT = 1000;
|
19
13
|
let pageLimit = MAX_ALLOWED_LIMIT;
|
14
|
+
|
20
15
|
/**
|
21
16
|
* Gets all the content from a space via the management API. This includes
|
22
17
|
* content in draft state.
|
23
18
|
*/
|
24
|
-
|
25
19
|
function getFullSourceSpace({
|
26
20
|
client,
|
27
21
|
cdaClient,
|
@@ -90,14 +84,12 @@ function getFullSourceSpace({
|
|
90
84
|
title: 'Fetching content entries data',
|
91
85
|
task: (0, _contentfulBatchLibs.wrapTask)(ctx => {
|
92
86
|
const source = cdaClient || ctx.environment;
|
93
|
-
|
94
87
|
if (cdaClient) {
|
95
88
|
// let's not fetch children when using Content Delivery API
|
96
89
|
queryEntries = queryEntries || {};
|
97
90
|
queryEntries.include = 0;
|
98
91
|
queryEntries.locale = '*';
|
99
92
|
}
|
100
|
-
|
101
93
|
return pagedGet({
|
102
94
|
source,
|
103
95
|
method: 'getEntries',
|
@@ -157,31 +149,27 @@ function getFullSourceSpace({
|
|
157
149
|
skip: () => skipRoles || environmentId !== 'master' && 'Roles can only be exported from master environment'
|
158
150
|
}], listrOptions);
|
159
151
|
}
|
160
|
-
|
161
152
|
function getEditorInterfaces(contentTypes) {
|
162
153
|
return _bluebird.default.map(contentTypes, contentType => {
|
163
154
|
return contentType.getEditorInterface().then(editorInterface => {
|
164
155
|
_contentfulBatchLibs.logEmitter.emit('info', `Fetched editor interface for ${contentType.name}`);
|
165
|
-
|
166
156
|
return editorInterface;
|
167
157
|
}).catch(() => {
|
168
158
|
// old contentTypes may not have an editor interface but we'll handle in a later stage
|
169
159
|
// but it should not stop getting the data process
|
170
160
|
_contentfulBatchLibs.logEmitter.emit('warning', `No editor interface found for ${contentType}`);
|
171
|
-
|
172
161
|
return _bluebird.default.resolve(null);
|
173
162
|
});
|
174
163
|
}, {
|
175
164
|
concurrency: 6
|
176
165
|
});
|
177
166
|
}
|
167
|
+
|
178
168
|
/**
|
179
169
|
* Gets all the existing entities based on pagination parameters.
|
180
170
|
* The first call will have no aggregated response. Subsequent calls will
|
181
171
|
* concatenate the new responses to the original one.
|
182
172
|
*/
|
183
|
-
|
184
|
-
|
185
173
|
function pagedGet({
|
186
174
|
source,
|
187
175
|
method,
|
@@ -190,14 +178,12 @@ function pagedGet({
|
|
190
178
|
query = null
|
191
179
|
}) {
|
192
180
|
let queryLimit = query && query.limit;
|
193
|
-
|
194
181
|
if (queryLimit && aggregatedResponse) {
|
195
182
|
if (skip + pageLimit > queryLimit) {
|
196
183
|
// this means the total we are about to fetch is > our limit
|
197
184
|
queryLimit = queryLimit % pageLimit;
|
198
185
|
}
|
199
186
|
}
|
200
|
-
|
201
187
|
const fullQuery = Object.assign({}, {
|
202
188
|
skip: skip,
|
203
189
|
order: 'sys.createdAt,sys.id'
|
@@ -210,13 +196,10 @@ function pagedGet({
|
|
210
196
|
} else {
|
211
197
|
aggregatedResponse.items = aggregatedResponse.items.concat(response.items);
|
212
198
|
}
|
213
|
-
|
214
199
|
const total = queryLimit && queryLimit < response.total ? queryLimit : response.total;
|
215
200
|
const page = Math.ceil(skip / pageLimit) + 1;
|
216
201
|
const pages = Math.ceil(total / pageLimit);
|
217
|
-
|
218
202
|
_contentfulBatchLibs.logEmitter.emit('info', `Fetched ${aggregatedResponse.items.length} of ${total} items (Page ${page}/${pages})`);
|
219
|
-
|
220
203
|
if (skip + pageLimit < total) {
|
221
204
|
return pagedGet({
|
222
205
|
source,
|
@@ -226,22 +209,17 @@ function pagedGet({
|
|
226
209
|
query
|
227
210
|
});
|
228
211
|
}
|
229
|
-
|
230
212
|
return aggregatedResponse;
|
231
213
|
});
|
232
214
|
}
|
233
|
-
|
234
215
|
function extractItems(response) {
|
235
216
|
return response.items;
|
236
217
|
}
|
237
|
-
|
238
218
|
function filterDrafts(items, includeDrafts, cdaClient) {
|
239
219
|
// CDA filters drafts based on host, no need to do filtering here
|
240
220
|
return includeDrafts || cdaClient ? items : items.filter(item => !!item.sys.publishedVersion || !!item.sys.archivedVersion);
|
241
221
|
}
|
242
|
-
|
243
222
|
function filterArchived(items, includeArchived) {
|
244
223
|
return includeArchived ? items : items.filter(item => !item.sys.archivedVersion);
|
245
224
|
}
|
246
|
-
|
247
225
|
module.exports = exports.default;
|
@@ -4,31 +4,21 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = initClient;
|
7
|
-
|
8
7
|
var _contentful = require("contentful");
|
9
|
-
|
10
8
|
var _contentfulBatchLibs = require("contentful-batch-libs");
|
11
|
-
|
12
9
|
var _contentfulManagement = require("contentful-management");
|
13
|
-
|
14
10
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
15
|
-
|
16
11
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
17
|
-
|
18
12
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
19
|
-
|
20
13
|
function logHandler(level, data) {
|
21
14
|
_contentfulBatchLibs.logEmitter.emit(level, data);
|
22
15
|
}
|
23
|
-
|
24
16
|
function initClient(opts, useCda = false) {
|
25
17
|
const defaultOpts = {
|
26
18
|
timeout: 10000,
|
27
19
|
logHandler
|
28
20
|
};
|
29
|
-
|
30
21
|
const config = _objectSpread(_objectSpread({}, defaultOpts), opts);
|
31
|
-
|
32
22
|
if (useCda) {
|
33
23
|
const cdaConfig = {
|
34
24
|
space: config.spaceId,
|
@@ -38,8 +28,6 @@ function initClient(opts, useCda = false) {
|
|
38
28
|
};
|
39
29
|
return (0, _contentful.createClient)(cdaConfig);
|
40
30
|
}
|
41
|
-
|
42
31
|
return (0, _contentfulManagement.createClient)(config);
|
43
32
|
}
|
44
|
-
|
45
33
|
module.exports = exports.default;
|
package/dist/usageParams.js
CHANGED
@@ -4,13 +4,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = void 0;
|
7
|
-
|
8
7
|
var _yargs = _interopRequireDefault(require("yargs"));
|
9
|
-
|
10
8
|
var _package = _interopRequireDefault(require("../package"));
|
11
|
-
|
12
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13
|
-
|
14
10
|
var _default = _yargs.default.version(_package.default.version || 'Version only available on installed package').usage('Usage: $0 [options]').option('space-id', {
|
15
11
|
describe: 'ID of Space with source data',
|
16
12
|
type: 'string',
|
@@ -100,6 +96,5 @@ var _default = _yargs.default.version(_package.default.version || 'Version only
|
|
100
96
|
type: 'string',
|
101
97
|
describe: 'Pass an additional HTTP Header'
|
102
98
|
}).config('config', 'An optional configuration JSON file containing all the options for a single run').argv;
|
103
|
-
|
104
99
|
exports.default = _default;
|
105
100
|
module.exports = exports.default;
|
@@ -6,41 +6,32 @@ Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
exports.calculateExpiryTimestamp = calculateExpiryTimestamp;
|
7
7
|
exports.isEmbargoedAsset = isEmbargoedAsset;
|
8
8
|
exports.signUrl = signUrl;
|
9
|
-
|
10
9
|
var _jsonwebtoken = _interopRequireDefault(require("jsonwebtoken"));
|
11
|
-
|
12
10
|
var _nodeFetch = _interopRequireDefault(require("node-fetch"));
|
13
|
-
|
14
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
15
|
-
|
16
12
|
const SIX_HOURS_IN_MS = 6 * 60 * 60 * 1000;
|
17
13
|
const assetKeyCache = new Map();
|
18
|
-
|
19
14
|
function createAssetKey(host, accessToken, spaceId, environmentId, expiresAtMs) {
|
20
15
|
return (0, _nodeFetch.default)(`https://${host}/spaces/${spaceId}/environments/${environmentId}/asset_keys`, {
|
21
16
|
method: 'POST',
|
22
17
|
body: JSON.stringify({
|
23
18
|
expiresAt: Math.floor(expiresAtMs / 1000) // in seconds
|
24
|
-
|
25
19
|
}),
|
20
|
+
|
26
21
|
headers: {
|
27
22
|
Authorization: `Bearer ${accessToken}`,
|
28
23
|
'Content-Type': 'application/json'
|
29
24
|
}
|
30
25
|
});
|
31
26
|
}
|
32
|
-
|
33
27
|
async function createCachedAssetKey(host, accessToken, spaceId, environmentId, minExpiresAtMs) {
|
34
28
|
const cacheKey = `${host}:${spaceId}:${environmentId}`;
|
35
29
|
let cacheItem = assetKeyCache.get(cacheKey);
|
36
|
-
|
37
30
|
if (!cacheItem || cacheItem.expiresAtMs < minExpiresAtMs) {
|
38
31
|
const expiresAtMs = calculateExpiryTimestamp();
|
39
|
-
|
40
32
|
if (minExpiresAtMs > expiresAtMs) {
|
41
33
|
throw new Error(`Cannot fetch an asset key so far in the future: ${minExpiresAtMs} > ${expiresAtMs}`);
|
42
34
|
}
|
43
|
-
|
44
35
|
try {
|
45
36
|
const assetKeyPromise = createAssetKey(host, accessToken, spaceId, environmentId, expiresAtMs);
|
46
37
|
const resolvedAssetKeyPromise = await assetKeyPromise;
|
@@ -53,18 +44,14 @@ async function createCachedAssetKey(host, accessToken, spaceId, environmentId, m
|
|
53
44
|
} catch (err) {
|
54
45
|
// If we encounter an error, make sure to clear the cache item if this is the most recent fetch.
|
55
46
|
const curCacheItem = assetKeyCache.get(cacheKey);
|
56
|
-
|
57
47
|
if (curCacheItem === cacheItem) {
|
58
48
|
assetKeyCache.delete(cacheKey);
|
59
49
|
}
|
60
|
-
|
61
50
|
return Promise.reject(err);
|
62
51
|
}
|
63
52
|
}
|
64
|
-
|
65
53
|
return cacheItem.result;
|
66
54
|
}
|
67
|
-
|
68
55
|
function generateSignedToken(secret, urlWithoutQueryParams, expiresAtMs) {
|
69
56
|
// Convert expiresAtMs to seconds, if defined
|
70
57
|
const exp = expiresAtMs ? Math.floor(expiresAtMs / 1000) : undefined;
|
@@ -75,7 +62,6 @@ function generateSignedToken(secret, urlWithoutQueryParams, expiresAtMs) {
|
|
75
62
|
algorithm: 'HS256'
|
76
63
|
});
|
77
64
|
}
|
78
|
-
|
79
65
|
function generateSignedUrl(policy, secret, url, expiresAtMs) {
|
80
66
|
const parsedUrl = new URL(url);
|
81
67
|
const urlWithoutQueryParams = parsedUrl.origin + parsedUrl.pathname;
|
@@ -84,22 +70,18 @@ function generateSignedUrl(policy, secret, url, expiresAtMs) {
|
|
84
70
|
parsedUrl.searchParams.set('policy', policy);
|
85
71
|
return parsedUrl.toString();
|
86
72
|
}
|
87
|
-
|
88
73
|
function isEmbargoedAsset(url) {
|
89
74
|
const pattern = /((images)|(assets)|(downloads)|(videos))\.secure\./;
|
90
75
|
return pattern.test(url);
|
91
76
|
}
|
92
|
-
|
93
77
|
function calculateExpiryTimestamp() {
|
94
78
|
return Date.now() + SIX_HOURS_IN_MS;
|
95
79
|
}
|
96
|
-
|
97
80
|
function signUrl(host, accessToken, spaceId, environmentId, url, expiresAtMs) {
|
98
81
|
// handle urls without protocol
|
99
82
|
if (url.startsWith('//')) {
|
100
83
|
url = 'https:' + url;
|
101
84
|
}
|
102
|
-
|
103
85
|
return createCachedAssetKey(host, accessToken, spaceId, environmentId, expiresAtMs).then(({
|
104
86
|
policy,
|
105
87
|
secret
|
package/dist/utils/headers.js
CHANGED
@@ -4,13 +4,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.getHeadersConfig = getHeadersConfig;
|
7
|
-
|
8
7
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
9
|
-
|
10
8
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
11
|
-
|
12
9
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
13
|
-
|
14
10
|
/**
|
15
11
|
* Turn header option into an object. Invalid header values
|
16
12
|
* are ignored.
|
@@ -29,16 +25,15 @@ function getHeadersConfig(value) {
|
|
29
25
|
if (!value) {
|
30
26
|
return {};
|
31
27
|
}
|
32
|
-
|
33
28
|
const values = Array.isArray(value) ? value : [value];
|
34
29
|
return values.reduce((headers, value) => {
|
35
30
|
value = value.trim();
|
36
|
-
const separatorIndex = value.indexOf(':');
|
31
|
+
const separatorIndex = value.indexOf(':');
|
37
32
|
|
33
|
+
// Invalid header format
|
38
34
|
if (separatorIndex === -1) {
|
39
35
|
return headers;
|
40
36
|
}
|
41
|
-
|
42
37
|
const headerKey = value.slice(0, separatorIndex).trim();
|
43
38
|
const headerValue = value.slice(separatorIndex + 1).trim();
|
44
39
|
return _objectSpread(_objectSpread({}, headers), {}, {
|