contentful-export 7.14.58 → 7.15.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.
@@ -13,7 +13,11 @@ var _figures = _interopRequireDefault(require("figures"));
13
13
 
14
14
  var _fsExtra = _interopRequireDefault(require("fs-extra"));
15
15
 
16
- var _request = _interopRequireDefault(require("request"));
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
- function downloadAsset({
28
+ const streamPipeline = (0, _util.promisify)(_stream.pipeline);
29
+
30
+ async function downloadAsset({
25
31
  url,
26
32
  directory
27
33
  }) {
28
- return new _bluebird.default(function (resolve, reject) {
29
- // handle urls without protocol
30
- if (url.startsWith('//')) {
31
- url = 'https:' + url;
32
- } // build local file path from the url for the download
34
+ // handle urls without protocol
35
+ if (url.startsWith('//')) {
36
+ url = 'https:' + url;
37
+ } // build local file path from the url for the download
33
38
 
34
39
 
35
- const parsedUrl = new URL(url);
40
+ const parsedUrl = new URL(url);
36
41
 
37
- const localFile = _path.default.join(directory, parsedUrl.host, parsedUrl.pathname); // ensure directory exists and create file stream
42
+ const localFile = _path.default.join(directory, parsedUrl.host, parsedUrl.pathname); // ensure directory exists and create file stream
38
43
 
39
44
 
40
- _fsExtra.default.mkdirsSync(_path.default.dirname(localFile));
45
+ _fsExtra.default.mkdirsSync(_path.default.dirname(localFile));
41
46
 
42
- const file = _fsExtra.default.createWriteStream(localFile); // download asset
47
+ const file = _fsExtra.default.createWriteStream(localFile); // download asset
43
48
 
44
49
 
45
- const assetRequest = _request.default.get(url); // pipe response content to file
50
+ const assetRequest = await (0, _nodeFetch.default)(url);
46
51
 
52
+ if (!assetRequest.ok) {
53
+ throw new Error(`error response status: ${assetRequest.status}`);
54
+ } // Wait for stream to be consumed before returning local file
47
55
 
48
- assetRequest.on('error', err => {
49
- reject(err);
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
- });
56
+
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
- const options = {
20
+ return (0, _nodeFetch.default)(`https://${host}/spaces/${spaceId}/environments/${environmentId}/asset_keys`, {
21
21
  method: 'POST',
22
- uri: `https://${host}/spaces/${spaceId}/environments/${environmentId}/asset_keys`,
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
- json: true
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
- const promise = createAssetKey(host, accessToken, spaceId, environmentId, expiresAtMs).catch(err => {
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
- cacheItem = {
57
- expiresAtMs,
58
- promise
59
- };
60
- assetKeyCache.set(cacheKey, cacheItem);
61
- }
60
+ }
61
+ } // cacheItem.promise is the return type of node-fetch.
62
+
62
63
 
63
- return cacheItem.promise;
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.14.58",
3
+ "version": "7.15.0",
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",
@@ -58,8 +58,7 @@
58
58
  "lodash": "^4.17.10",
59
59
  "mkdirp": "^1.0.3",
60
60
  "moment": "^2.22.2",
61
- "request": "^2.87.0",
62
- "request-promise": "^4.2.6",
61
+ "node-fetch": "^2.6.7",
63
62
  "yargs": "^17.1.1"
64
63
  },
65
64
  "devDependencies": {