decap-cms-lib-util 3.1.0 → 3.3.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.
Files changed (52) hide show
  1. package/README.md +1 -1
  2. package/dist/decap-cms-lib-util.js +1 -1
  3. package/dist/decap-cms-lib-util.js.LICENSE.txt +9 -0
  4. package/dist/decap-cms-lib-util.js.map +1 -1
  5. package/dist/esm/API.js +37 -61
  6. package/dist/esm/APIError.js +3 -17
  7. package/dist/esm/APIUtils.js +10 -23
  8. package/dist/esm/AccessTokenError.js +3 -14
  9. package/dist/esm/Cursor.js +16 -28
  10. package/dist/esm/EditorialWorkflowError.js +3 -15
  11. package/dist/esm/asyncLock.js +5 -12
  12. package/dist/esm/backendUtil.js +21 -34
  13. package/dist/esm/getBlobSHA.js +4 -11
  14. package/dist/esm/git-lfs.js +27 -46
  15. package/dist/esm/implementation.js +32 -51
  16. package/dist/esm/index.js +71 -409
  17. package/dist/esm/loadScript.js +1 -7
  18. package/dist/esm/localForage.js +4 -11
  19. package/dist/esm/path.js +4 -13
  20. package/dist/esm/promise.js +5 -14
  21. package/dist/esm/stega.js +129 -0
  22. package/dist/esm/types/semaphore.d.js +0 -1
  23. package/dist/esm/types.js +7 -0
  24. package/dist/esm/unsentRequest.js +21 -31
  25. package/package.json +5 -2
  26. package/CHANGELOG.md +0 -376
  27. package/src/API.ts +0 -376
  28. package/src/APIError.ts +0 -17
  29. package/src/APIUtils.ts +0 -38
  30. package/src/AccessTokenError.ts +0 -11
  31. package/src/Cursor.ts +0 -178
  32. package/src/EditorialWorkflowError.ts +0 -12
  33. package/src/__tests__/api.spec.js +0 -13
  34. package/src/__tests__/apiUtils.spec.js +0 -74
  35. package/src/__tests__/asyncLock.spec.js +0 -85
  36. package/src/__tests__/backendUtil.spec.js +0 -97
  37. package/src/__tests__/implementation.spec.js +0 -58
  38. package/src/__tests__/path.spec.js +0 -53
  39. package/src/__tests__/unsentRequest.spec.js +0 -19
  40. package/src/asyncLock.ts +0 -43
  41. package/src/backendUtil.ts +0 -120
  42. package/src/getBlobSHA.ts +0 -12
  43. package/src/git-lfs.ts +0 -133
  44. package/src/implementation.ts +0 -575
  45. package/src/index.ts +0 -213
  46. package/src/loadScript.js +0 -24
  47. package/src/localForage.ts +0 -21
  48. package/src/path.ts +0 -86
  49. package/src/promise.ts +0 -21
  50. package/src/types/semaphore.d.ts +0 -5
  51. package/src/unsentRequest.js +0 -133
  52. package/webpack.config.js +0 -3
@@ -1,28 +1,8 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createPointerFile = createPointerFile;
7
- exports.getLargeMediaFilteredMediaFiles = getLargeMediaFilteredMediaFiles;
8
- exports.getLargeMediaPatternsFromGitAttributesFile = void 0;
9
- exports.getPointerFileForMediaFileObj = getPointerFileForMediaFileObj;
10
- exports.parsePointerFile = void 0;
11
- var _map2 = _interopRequireDefault(require("lodash/fp/map"));
12
- var _fromPairs2 = _interopRequireDefault(require("lodash/fp/fromPairs"));
13
- var _flow2 = _interopRequireDefault(require("lodash/fp/flow"));
14
- var _filter2 = _interopRequireDefault(require("lodash/fp/filter"));
15
- var _getBlobSHA = _interopRequireDefault(require("./getBlobSHA"));
16
- const _excluded = ["size", "oid"]; //
1
+ //
17
2
  // Pointer file parsing
18
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
20
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
21
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
22
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
23
- function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
24
- function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
25
- function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
3
+
4
+ import { filter, flow, fromPairs, map } from 'lodash/fp';
5
+ import getBlobSHA from './getBlobSHA';
26
6
  function splitIntoLines(str) {
27
7
  return str.split('\n');
28
8
  }
@@ -32,18 +12,16 @@ function splitIntoWords(str) {
32
12
  function isNonEmptyString(str) {
33
13
  return str !== '';
34
14
  }
35
- const withoutEmptyLines = (0, _flow2.default)([(0, _map2.default)(str => str.trim()), (0, _filter2.default)(isNonEmptyString)]);
36
- const parsePointerFile = exports.parsePointerFile = (0, _flow2.default)([splitIntoLines, withoutEmptyLines, (0, _map2.default)(splitIntoWords), _fromPairs2.default, _ref => {
37
- let {
38
- size,
39
- oid
40
- } = _ref,
41
- rest = _objectWithoutProperties(_ref, _excluded);
42
- return _objectSpread({
43
- size: parseInt(size),
44
- sha: oid === null || oid === void 0 ? void 0 : oid.split(':')[1]
45
- }, rest);
46
- }]);
15
+ const withoutEmptyLines = flow([map(str => str.trim()), filter(isNonEmptyString)]);
16
+ export const parsePointerFile = flow([splitIntoLines, withoutEmptyLines, map(splitIntoWords), fromPairs, ({
17
+ size,
18
+ oid,
19
+ ...rest
20
+ }) => ({
21
+ size: parseInt(size),
22
+ sha: oid?.split(':')[1],
23
+ ...rest
24
+ })]);
47
25
 
48
26
  //
49
27
  // .gitattributes file parsing
@@ -65,11 +43,11 @@ function parseGitPatternAttribute(attributeString) {
65
43
  }
66
44
  return [attributeString, true];
67
45
  }
68
- const parseGitPatternAttributes = (0, _flow2.default)([(0, _map2.default)(parseGitPatternAttribute), _fromPairs2.default]);
69
- const parseGitAttributesPatternLine = (0, _flow2.default)([splitIntoWords, ([pattern, ...attributes]) => [pattern, parseGitPatternAttributes(attributes)]]);
70
- const parseGitAttributesFileToPatternAttributePairs = (0, _flow2.default)([splitIntoLines, (0, _map2.default)(removeGitAttributesCommentsFromLine), withoutEmptyLines, (0, _map2.default)(parseGitAttributesPatternLine)]);
71
- const getLargeMediaPatternsFromGitAttributesFile = exports.getLargeMediaPatternsFromGitAttributesFile = (0, _flow2.default)([parseGitAttributesFileToPatternAttributePairs, (0, _filter2.default)(([, attributes]) => attributes.filter === 'lfs' && attributes.diff === 'lfs' && attributes.merge === 'lfs'), (0, _map2.default)(([pattern]) => pattern)]);
72
- function createPointerFile({
46
+ const parseGitPatternAttributes = flow([map(parseGitPatternAttribute), fromPairs]);
47
+ const parseGitAttributesPatternLine = flow([splitIntoWords, ([pattern, ...attributes]) => [pattern, parseGitPatternAttributes(attributes)]]);
48
+ const parseGitAttributesFileToPatternAttributePairs = flow([splitIntoLines, map(removeGitAttributesCommentsFromLine), withoutEmptyLines, map(parseGitAttributesPatternLine)]);
49
+ export const getLargeMediaPatternsFromGitAttributesFile = flow([parseGitAttributesFileToPatternAttributePairs, filter(([, attributes]) => attributes.filter === 'lfs' && attributes.diff === 'lfs' && attributes.merge === 'lfs'), map(([pattern]) => pattern)]);
50
+ export function createPointerFile({
73
51
  size,
74
52
  sha
75
53
  }) {
@@ -79,12 +57,12 @@ oid sha256:${sha}
79
57
  size ${size}
80
58
  `;
81
59
  }
82
- async function getPointerFileForMediaFileObj(client, fileObj, path) {
60
+ export async function getPointerFileForMediaFileObj(client, fileObj, path) {
83
61
  const {
84
62
  name,
85
63
  size
86
64
  } = fileObj;
87
- const sha = await (0, _getBlobSHA.default)(fileObj);
65
+ const sha = await getBlobSHA(fileObj);
88
66
  await client.uploadResource({
89
67
  sha,
90
68
  size
@@ -97,7 +75,7 @@ async function getPointerFileForMediaFileObj(client, fileObj, path) {
97
75
  const pointerFile = new File([pointerFileBlob], name, {
98
76
  type: 'text/plain'
99
77
  });
100
- const pointerFileSHA = await (0, _getBlobSHA.default)(pointerFile);
78
+ const pointerFileSHA = await getBlobSHA(pointerFile);
101
79
  return {
102
80
  fileObj: pointerFile,
103
81
  size: pointerFileBlob.size,
@@ -106,7 +84,7 @@ async function getPointerFileForMediaFileObj(client, fileObj, path) {
106
84
  path
107
85
  };
108
86
  }
109
- async function getLargeMediaFilteredMediaFiles(client, mediaFiles) {
87
+ export async function getLargeMediaFilteredMediaFiles(client, mediaFiles) {
110
88
  return await Promise.all(mediaFiles.map(async mediaFile => {
111
89
  const {
112
90
  fileObj,
@@ -117,6 +95,9 @@ async function getLargeMediaFilteredMediaFiles(client, mediaFiles) {
117
95
  return mediaFile;
118
96
  }
119
97
  const pointerFileDetails = await getPointerFileForMediaFileObj(client, fileObj, path);
120
- return _objectSpread(_objectSpread({}, mediaFile), pointerFileDetails);
98
+ return {
99
+ ...mediaFile,
100
+ ...pointerFileDetails
101
+ };
121
102
  }));
122
103
  }
@@ -1,31 +1,10 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.allEntriesByFolder = allEntriesByFolder;
7
- exports.blobToFileObj = blobToFileObj;
8
- exports.entriesByFiles = entriesByFiles;
9
- exports.entriesByFolder = entriesByFolder;
10
- exports.getLocalTree = getLocalTree;
11
- exports.getMediaAsBlob = getMediaAsBlob;
12
- exports.getMediaDisplayURL = getMediaDisplayURL;
13
- exports.persistLocalTree = persistLocalTree;
14
- exports.runWithLock = runWithLock;
15
- exports.unpublishedEntries = unpublishedEntries;
16
- var _sortBy2 = _interopRequireDefault(require("lodash/sortBy"));
17
- var _unionBy2 = _interopRequireDefault(require("lodash/unionBy"));
18
- var _semaphore = _interopRequireDefault(require("semaphore"));
19
- var _path = require("./path");
20
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
22
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
23
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
24
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
25
- function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
1
+ import semaphore from 'semaphore';
2
+ import unionBy from 'lodash/unionBy';
3
+ import sortBy from 'lodash/sortBy';
4
+ import { basename } from './path';
26
5
  const MAX_CONCURRENT_DOWNLOADS = 10;
27
6
  async function fetchFiles(files, readFile, readFileMetadata, apiName) {
28
- const sem = (0, _semaphore.default)(MAX_CONCURRENT_DOWNLOADS);
7
+ const sem = semaphore(MAX_CONCURRENT_DOWNLOADS);
29
8
  const promises = [];
30
9
  files.forEach(file => {
31
10
  promises.push(new Promise(resolve => sem.take(async () => {
@@ -34,7 +13,10 @@ async function fetchFiles(files, readFile, readFileMetadata, apiName) {
34
13
  parseText: true
35
14
  }), readFileMetadata(file.path, file.id)]);
36
15
  resolve({
37
- file: _objectSpread(_objectSpread({}, file), fileMetadata),
16
+ file: {
17
+ ...file,
18
+ ...fileMetadata
19
+ },
38
20
  data: data
39
21
  });
40
22
  sem.leave();
@@ -49,14 +31,14 @@ async function fetchFiles(files, readFile, readFileMetadata, apiName) {
49
31
  });
50
32
  return Promise.all(promises).then(loadedEntries => loadedEntries.filter(loadedEntry => !loadedEntry.error));
51
33
  }
52
- async function entriesByFolder(listFiles, readFile, readFileMetadata, apiName) {
34
+ export async function entriesByFolder(listFiles, readFile, readFileMetadata, apiName) {
53
35
  const files = await listFiles();
54
36
  return fetchFiles(files, readFile, readFileMetadata, apiName);
55
37
  }
56
- async function entriesByFiles(files, readFile, readFileMetadata, apiName) {
38
+ export async function entriesByFiles(files, readFile, readFileMetadata, apiName) {
57
39
  return fetchFiles(files, readFile, readFileMetadata, apiName);
58
40
  }
59
- async function unpublishedEntries(listEntriesKeys) {
41
+ export async function unpublishedEntries(listEntriesKeys) {
60
42
  try {
61
43
  const keys = await listEntriesKeys();
62
44
  return keys;
@@ -67,13 +49,13 @@ async function unpublishedEntries(listEntriesKeys) {
67
49
  throw error;
68
50
  }
69
51
  }
70
- function blobToFileObj(name, blob) {
52
+ export function blobToFileObj(name, blob) {
71
53
  const options = name.match(/.svg$/) ? {
72
54
  type: 'image/svg+xml'
73
55
  } : {};
74
56
  return new File([blob], name, options);
75
57
  }
76
- async function getMediaAsBlob(path, id, readFile) {
58
+ export async function getMediaAsBlob(path, id, readFile) {
77
59
  let blob;
78
60
  if (path.match(/.svg$/)) {
79
61
  const text = await readFile(path, id, {
@@ -89,14 +71,14 @@ async function getMediaAsBlob(path, id, readFile) {
89
71
  }
90
72
  return blob;
91
73
  }
92
- async function getMediaDisplayURL(displayURL, readFile, semaphore) {
74
+ export async function getMediaDisplayURL(displayURL, readFile, semaphore) {
93
75
  const {
94
76
  path,
95
77
  id
96
78
  } = displayURL;
97
79
  return new Promise((resolve, reject) => semaphore.take(() => getMediaAsBlob(path, id, readFile).then(blob => URL.createObjectURL(blob)).then(resolve, reject).finally(() => semaphore.leave())));
98
80
  }
99
- async function runWithLock(lock, func, message) {
81
+ export async function runWithLock(lock, func, message) {
100
82
  try {
101
83
  const acquired = await lock.acquire();
102
84
  if (!acquired) {
@@ -117,7 +99,7 @@ function getLocalKey({
117
99
  }) {
118
100
  return `${LOCAL_KEY}.${branch}.${folder}.${extension}.${depth}`;
119
101
  }
120
- async function persistLocalTree({
102
+ export async function persistLocalTree({
121
103
  localForage,
122
104
  localTree,
123
105
  branch,
@@ -132,7 +114,7 @@ async function persistLocalTree({
132
114
  depth
133
115
  }), localTree);
134
116
  }
135
- async function getLocalTree({
117
+ export async function getLocalTree({
136
118
  localForage,
137
119
  branch,
138
120
  folder,
@@ -156,31 +138,28 @@ async function getDiffFromLocalTree({
156
138
  getFileId
157
139
  }) {
158
140
  const diff = await getDifferences(branch.sha, localTree.head);
159
- const diffFiles = diff.filter(d => {
160
- var _d$oldPath, _d$newPath;
161
- return ((_d$oldPath = d.oldPath) === null || _d$oldPath === void 0 ? void 0 : _d$oldPath.startsWith(folder)) || ((_d$newPath = d.newPath) === null || _d$newPath === void 0 ? void 0 : _d$newPath.startsWith(folder));
162
- }).reduce((acc, d) => {
141
+ const diffFiles = diff.filter(d => d.oldPath?.startsWith(folder) || d.newPath?.startsWith(folder)).reduce((acc, d) => {
163
142
  if (d.status === 'renamed') {
164
143
  acc.push({
165
144
  path: d.oldPath,
166
- name: (0, _path.basename)(d.oldPath),
145
+ name: basename(d.oldPath),
167
146
  deleted: true
168
147
  });
169
148
  acc.push({
170
149
  path: d.newPath,
171
- name: (0, _path.basename)(d.newPath),
150
+ name: basename(d.newPath),
172
151
  deleted: false
173
152
  });
174
153
  } else if (d.status === 'deleted') {
175
154
  acc.push({
176
155
  path: d.oldPath,
177
- name: (0, _path.basename)(d.oldPath),
156
+ name: basename(d.oldPath),
178
157
  deleted: true
179
158
  });
180
159
  } else {
181
160
  acc.push({
182
161
  path: d.newPath || d.oldPath,
183
- name: (0, _path.basename)(d.newPath || d.oldPath),
162
+ name: basename(d.newPath || d.oldPath),
184
163
  deleted: false
185
164
  });
186
165
  }
@@ -189,18 +168,20 @@ async function getDiffFromLocalTree({
189
168
  const diffFilesWithIds = await Promise.all(diffFiles.map(async file => {
190
169
  if (!file.deleted) {
191
170
  const id = await getFileId(file.path);
192
- return _objectSpread(_objectSpread({}, file), {}, {
171
+ return {
172
+ ...file,
193
173
  id
194
- });
174
+ };
195
175
  } else {
196
- return _objectSpread(_objectSpread({}, file), {}, {
176
+ return {
177
+ ...file,
197
178
  id: ''
198
- });
179
+ };
199
180
  }
200
181
  }));
201
182
  return diffFilesWithIds;
202
183
  }
203
- async function allEntriesByFolder({
184
+ export async function allEntriesByFolder({
204
185
  listAllFiles,
205
186
  readFile,
206
187
  readFileMetadata,
@@ -227,7 +208,7 @@ async function allEntriesByFolder({
227
208
  files: files.map(f => ({
228
209
  id: f.id,
229
210
  path: f.path,
230
- name: (0, _path.basename)(f.path)
211
+ name: basename(f.path)
231
212
  }))
232
213
  },
233
214
  branch: branch.name,
@@ -278,7 +259,7 @@ async function allEntriesByFolder({
278
259
  acc[d.path] = d.deleted;
279
260
  return acc;
280
261
  }, {});
281
- const newCopy = (0, _sortBy2.default)((0, _unionBy2.default)(diff.filter(d => !deleted[d.path]), localTree.files.filter(f => !deleted[f.path]), file => file.path), file => file.path);
262
+ const newCopy = sortBy(unionBy(diff.filter(d => !deleted[d.path]), localTree.files.filter(f => !deleted[f.path]), file => file.path), file => file.path);
282
263
  await persistLocalTree({
283
264
  localForage,
284
265
  localTree: {