contentful-export 7.14.57 → 7.15.1
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/tasks/download-assets.js +25 -22
- package/dist/utils/embargoedAssets.js +23 -21
- package/package.json +4 -8
@@ -11,9 +11,13 @@ var _bluebird = _interopRequireDefault(require("bluebird"));
|
|
11
11
|
|
12
12
|
var _figures = _interopRequireDefault(require("figures"));
|
13
13
|
|
14
|
-
var
|
14
|
+
var _fs = require("fs");
|
15
15
|
|
16
|
-
var
|
16
|
+
var _nodeFetch = _interopRequireDefault(require("node-fetch"));
|
17
|
+
|
18
|
+
var _stream = require("stream");
|
19
|
+
|
20
|
+
var _util = require("util");
|
17
21
|
|
18
22
|
var _embargoedAssets = require("../utils/embargoedAssets");
|
19
23
|
|
@@ -21,38 +25,37 @@ var _getEntityName = _interopRequireDefault(require("contentful-batch-libs/dist/
|
|
21
25
|
|
22
26
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
23
27
|
|
24
|
-
|
28
|
+
const streamPipeline = (0, _util.promisify)(_stream.pipeline);
|
29
|
+
|
30
|
+
async function downloadAsset({
|
25
31
|
url,
|
26
32
|
directory
|
27
33
|
}) {
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
} // build local file path from the url for the download
|
33
|
-
|
34
|
+
// handle urls without protocol
|
35
|
+
if (url.startsWith('//')) {
|
36
|
+
url = 'https:' + url;
|
37
|
+
} // build local file path from the url for the download
|
34
38
|
|
35
|
-
const parsedUrl = new URL(url);
|
36
39
|
|
37
|
-
|
40
|
+
const parsedUrl = new URL(url);
|
38
41
|
|
42
|
+
const localFile = _path.default.join(directory, parsedUrl.host, parsedUrl.pathname); // ensure directory exists and create file stream
|
39
43
|
|
40
|
-
_fsExtra.default.mkdirsSync(_path.default.dirname(localFile));
|
41
44
|
|
42
|
-
|
45
|
+
await _fs.promises.mkdir(_path.default.dirname(localFile), {
|
46
|
+
recursive: true
|
47
|
+
});
|
48
|
+
const file = (0, _fs.createWriteStream)(localFile); // download asset
|
43
49
|
|
50
|
+
const assetRequest = await (0, _nodeFetch.default)(url);
|
44
51
|
|
45
|
-
|
52
|
+
if (!assetRequest.ok) {
|
53
|
+
throw new Error(`error response status: ${assetRequest.status}`);
|
54
|
+
} // Wait for stream to be consumed before returning local file
|
46
55
|
|
47
56
|
|
48
|
-
|
49
|
-
|
50
|
-
}).on('response', response => {
|
51
|
-
if (response.statusCode >= 400) {
|
52
|
-
reject(new Error('error response status: ' + response.statusCode));
|
53
|
-
}
|
54
|
-
}).pipe(file).on('finish', () => resolve(localFile));
|
55
|
-
});
|
57
|
+
await streamPipeline(assetRequest.body, file);
|
58
|
+
return localFile;
|
56
59
|
}
|
57
60
|
|
58
61
|
function downloadAssets(options) {
|
@@ -7,32 +7,30 @@ exports.calculateExpiryTimestamp = calculateExpiryTimestamp;
|
|
7
7
|
exports.isEmbargoedAsset = isEmbargoedAsset;
|
8
8
|
exports.signUrl = signUrl;
|
9
9
|
|
10
|
-
var _requestPromise = _interopRequireDefault(require("request-promise"));
|
11
|
-
|
12
10
|
var _jsonwebtoken = _interopRequireDefault(require("jsonwebtoken"));
|
13
11
|
|
12
|
+
var _nodeFetch = _interopRequireDefault(require("node-fetch"));
|
13
|
+
|
14
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
15
15
|
|
16
16
|
const SIX_HOURS_IN_MS = 6 * 60 * 60 * 1000;
|
17
17
|
const assetKeyCache = new Map();
|
18
18
|
|
19
19
|
function createAssetKey(host, accessToken, spaceId, environmentId, expiresAtMs) {
|
20
|
-
|
20
|
+
return (0, _nodeFetch.default)(`https://${host}/spaces/${spaceId}/environments/${environmentId}/asset_keys`, {
|
21
21
|
method: 'POST',
|
22
|
-
|
23
|
-
body: {
|
22
|
+
body: JSON.stringify({
|
24
23
|
expiresAt: Math.floor(expiresAtMs / 1000) // in seconds
|
25
24
|
|
26
|
-
},
|
25
|
+
}),
|
27
26
|
headers: {
|
28
|
-
Authorization: `Bearer ${accessToken}
|
29
|
-
|
30
|
-
|
31
|
-
};
|
32
|
-
return (0, _requestPromise.default)(options);
|
27
|
+
Authorization: `Bearer ${accessToken}`,
|
28
|
+
'Content-Type': 'application/json'
|
29
|
+
}
|
30
|
+
});
|
33
31
|
}
|
34
32
|
|
35
|
-
function createCachedAssetKey(host, accessToken, spaceId, environmentId, minExpiresAtMs) {
|
33
|
+
async function createCachedAssetKey(host, accessToken, spaceId, environmentId, minExpiresAtMs) {
|
36
34
|
const cacheKey = `${host}:${spaceId}:${environmentId}`;
|
37
35
|
let cacheItem = assetKeyCache.get(cacheKey);
|
38
36
|
|
@@ -43,7 +41,14 @@ function createCachedAssetKey(host, accessToken, spaceId, environmentId, minExpi
|
|
43
41
|
throw new Error(`Cannot fetch an asset key so far in the future: ${minExpiresAtMs} > ${expiresAtMs}`);
|
44
42
|
}
|
45
43
|
|
46
|
-
|
44
|
+
try {
|
45
|
+
const assetKeyPromise = createAssetKey(host, accessToken, spaceId, environmentId, expiresAtMs);
|
46
|
+
cacheItem = {
|
47
|
+
expiresAtMs,
|
48
|
+
promise: assetKeyPromise
|
49
|
+
};
|
50
|
+
assetKeyCache.set(cacheKey, cacheItem);
|
51
|
+
} catch (err) {
|
47
52
|
// If we encounter an error, make sure to clear the cache item if this is the most recent fetch.
|
48
53
|
const curCacheItem = assetKeyCache.get(cacheKey);
|
49
54
|
|
@@ -52,15 +57,12 @@ function createCachedAssetKey(host, accessToken, spaceId, environmentId, minExpi
|
|
52
57
|
}
|
53
58
|
|
54
59
|
return Promise.reject(err);
|
55
|
-
}
|
56
|
-
|
57
|
-
|
58
|
-
promise
|
59
|
-
};
|
60
|
-
assetKeyCache.set(cacheKey, cacheItem);
|
61
|
-
}
|
60
|
+
}
|
61
|
+
} // cacheItem.promise is the return type of node-fetch.
|
62
|
+
|
62
63
|
|
63
|
-
|
64
|
+
const response = await cacheItem.promise;
|
65
|
+
return response.json();
|
64
66
|
}
|
65
67
|
|
66
68
|
function generateSignedToken(secret, urlWithoutQueryParams, expiresAtMs) {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "contentful-export",
|
3
|
-
"version": "7.
|
3
|
+
"version": "7.15.1",
|
4
4
|
"description": "this tool allows you to export a space to a JSON dump",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "types.d.ts",
|
@@ -50,7 +50,6 @@
|
|
50
50
|
"contentful-batch-libs": "^9.2.1",
|
51
51
|
"contentful-management": "^10.0.0",
|
52
52
|
"figures": "^3.2.0",
|
53
|
-
"fs-extra": "^10.0.0",
|
54
53
|
"jsonwebtoken": "^8.5.1",
|
55
54
|
"listr": "^0.14.1",
|
56
55
|
"listr-update-renderer": "^0.5.0",
|
@@ -58,8 +57,7 @@
|
|
58
57
|
"lodash": "^4.17.10",
|
59
58
|
"mkdirp": "^1.0.3",
|
60
59
|
"moment": "^2.22.2",
|
61
|
-
"
|
62
|
-
"request-promise": "^4.2.6",
|
60
|
+
"node-fetch": "^2.6.7",
|
63
61
|
"yargs": "^17.1.1"
|
64
62
|
},
|
65
63
|
"devDependencies": {
|
@@ -70,11 +68,10 @@
|
|
70
68
|
"@babel/template": "^7.0.0",
|
71
69
|
"@babel/types": "^7.0.0",
|
72
70
|
"@types/jest": "^27.0.2",
|
73
|
-
"babel-core": "^7.0.0-bridge.0",
|
74
71
|
"babel-eslint": "^10.1.0",
|
75
72
|
"babel-jest": "^28.0.0",
|
76
73
|
"babel-plugin-add-module-exports": "^1.0.2",
|
77
|
-
"cz-conventional-changelog": "^3.
|
74
|
+
"cz-conventional-changelog": "^3.3.0",
|
78
75
|
"eslint": "^7.2.0",
|
79
76
|
"eslint-config-standard": "^16.0.1",
|
80
77
|
"eslint-plugin-import": "^2.12.0",
|
@@ -89,8 +86,7 @@
|
|
89
86
|
"nock": "^13.0.0",
|
90
87
|
"opener": "^1.4.1",
|
91
88
|
"rimraf": "^3.0.2",
|
92
|
-
"semantic-release": "^19.0.2"
|
93
|
-
"travis-deploy-once": "^5.0.0"
|
89
|
+
"semantic-release": "^19.0.2"
|
94
90
|
},
|
95
91
|
"files": [
|
96
92
|
"bin",
|