@strapi/data-transfer 4.9.0 → 4.10.0-beta.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 (156) hide show
  1. package/jest.config.js +14 -0
  2. package/lib/engine/diagnostic.d.ts +40 -0
  3. package/lib/engine/diagnostic.js +50 -0
  4. package/lib/engine/errors.d.ts +28 -0
  5. package/lib/engine/errors.js +29 -0
  6. package/lib/engine/index.d.ts +44 -0
  7. package/lib/engine/index.js +506 -0
  8. package/lib/engine/validation/index.d.ts +2 -0
  9. package/lib/engine/validation/index.js +19 -0
  10. package/lib/engine/validation/provider.d.ts +3 -0
  11. package/lib/engine/validation/provider.js +18 -0
  12. package/lib/engine/validation/schemas/index.d.ts +7 -0
  13. package/lib/engine/validation/schemas/index.js +53 -0
  14. package/lib/errors/base.d.ts +8 -0
  15. package/lib/errors/base.js +13 -0
  16. package/lib/errors/constants.d.ts +3 -0
  17. package/lib/errors/constants.js +9 -0
  18. package/lib/errors/index.d.ts +2 -0
  19. package/lib/errors/index.js +19 -0
  20. package/lib/errors/providers.d.ts +21 -0
  21. package/lib/errors/providers.js +32 -0
  22. package/lib/file/index.d.ts +1 -0
  23. package/lib/file/index.js +28 -0
  24. package/lib/file/providers/destination/index.d.ts +45 -0
  25. package/lib/file/providers/destination/index.js +174 -0
  26. package/lib/file/providers/destination/utils.d.ts +9 -0
  27. package/lib/file/providers/destination/utils.js +61 -0
  28. package/lib/file/providers/index.d.ts +2 -0
  29. package/lib/file/providers/index.js +19 -0
  30. package/lib/file/providers/source/index.d.ts +40 -0
  31. package/lib/file/providers/source/index.js +220 -0
  32. package/lib/index.d.ts +4 -0
  33. package/lib/index.js +31 -0
  34. package/lib/strapi/index.d.ts +3 -0
  35. package/lib/strapi/index.js +30 -0
  36. package/lib/strapi/providers/index.d.ts +3 -0
  37. package/lib/strapi/providers/index.js +22 -0
  38. package/lib/strapi/providers/local-destination/index.d.ts +34 -0
  39. package/lib/strapi/providers/local-destination/index.js +220 -0
  40. package/lib/strapi/providers/local-destination/strategies/index.d.ts +1 -0
  41. package/lib/strapi/providers/local-destination/strategies/index.js +28 -0
  42. package/lib/strapi/providers/local-destination/strategies/restore/configuration.d.ts +5 -0
  43. package/lib/strapi/providers/local-destination/strategies/restore/configuration.js +51 -0
  44. package/lib/strapi/providers/local-destination/strategies/restore/entities.d.ts +11 -0
  45. package/lib/strapi/providers/local-destination/strategies/restore/entities.js +103 -0
  46. package/lib/strapi/providers/local-destination/strategies/restore/index.d.ts +32 -0
  47. package/lib/strapi/providers/local-destination/strategies/restore/index.js +107 -0
  48. package/lib/strapi/providers/local-destination/strategies/restore/links.d.ts +4 -0
  49. package/lib/strapi/providers/local-destination/strategies/restore/links.js +32 -0
  50. package/lib/strapi/providers/local-source/assets.d.ts +5 -0
  51. package/lib/strapi/providers/local-source/assets.js +31 -0
  52. package/lib/strapi/providers/local-source/configuration.d.ts +5 -0
  53. package/lib/strapi/providers/local-source/configuration.js +36 -0
  54. package/lib/strapi/providers/local-source/entities.d.ts +10 -0
  55. package/lib/strapi/providers/local-source/entities.js +85 -0
  56. package/lib/strapi/providers/local-source/index.d.ts +27 -0
  57. package/lib/strapi/providers/local-source/index.js +98 -0
  58. package/lib/strapi/providers/local-source/links.d.ts +5 -0
  59. package/lib/strapi/providers/local-source/links.js +23 -0
  60. package/lib/strapi/providers/remote-destination/index.d.ts +37 -0
  61. package/lib/strapi/providers/remote-destination/index.js +284 -0
  62. package/lib/strapi/providers/remote-destination/utils.d.ts +31 -0
  63. package/lib/strapi/providers/remote-destination/utils.js +72 -0
  64. package/lib/strapi/queries/entity.d.ts +19 -0
  65. package/lib/strapi/queries/entity.js +130 -0
  66. package/lib/strapi/queries/index.d.ts +2 -0
  67. package/lib/strapi/queries/index.js +29 -0
  68. package/lib/strapi/queries/link.d.ts +6 -0
  69. package/lib/strapi/queries/link.js +215 -0
  70. package/lib/strapi/remote/constants.d.ts +2 -0
  71. package/lib/strapi/remote/constants.js +6 -0
  72. package/lib/strapi/remote/controllers/index.d.ts +1 -0
  73. package/lib/strapi/remote/controllers/index.js +18 -0
  74. package/lib/strapi/remote/controllers/push.d.ts +26 -0
  75. package/lib/strapi/remote/controllers/push.js +116 -0
  76. package/lib/strapi/remote/flows/default.d.ts +3 -0
  77. package/lib/strapi/remote/flows/default.js +41 -0
  78. package/lib/strapi/remote/flows/index.d.ts +18 -0
  79. package/lib/strapi/remote/flows/index.js +59 -0
  80. package/lib/strapi/remote/handlers.d.ts +10 -0
  81. package/lib/strapi/remote/handlers.js +285 -0
  82. package/lib/strapi/remote/index.d.ts +3 -0
  83. package/lib/strapi/remote/index.js +30 -0
  84. package/lib/utils/encryption/decrypt.d.ts +11 -0
  85. package/lib/utils/encryption/decrypt.js +47 -0
  86. package/lib/utils/encryption/encrypt.d.ts +11 -0
  87. package/lib/utils/encryption/encrypt.js +47 -0
  88. package/lib/utils/encryption/index.d.ts +2 -0
  89. package/lib/utils/encryption/index.js +19 -0
  90. package/lib/utils/index.d.ts +5 -0
  91. package/lib/utils/index.js +32 -0
  92. package/lib/utils/json.d.ts +30 -0
  93. package/lib/utils/json.js +68 -0
  94. package/lib/utils/providers.d.ts +2 -0
  95. package/lib/utils/providers.js +11 -0
  96. package/lib/utils/schema.d.ts +7 -0
  97. package/lib/utils/schema.js +29 -0
  98. package/lib/utils/stream.d.ts +27 -0
  99. package/lib/utils/stream.js +59 -0
  100. package/lib/utils/transaction.d.ts +3 -0
  101. package/lib/utils/transaction.js +88 -0
  102. package/package.json +16 -17
  103. package/dist/engine/diagnostic.js.map +0 -1
  104. package/dist/engine/errors.js.map +0 -1
  105. package/dist/engine/index.js.map +0 -1
  106. package/dist/engine/validation/index.js.map +0 -1
  107. package/dist/engine/validation/provider.js.map +0 -1
  108. package/dist/engine/validation/schemas/index.js.map +0 -1
  109. package/dist/errors/base.js.map +0 -1
  110. package/dist/errors/constants.js.map +0 -1
  111. package/dist/errors/index.js.map +0 -1
  112. package/dist/errors/providers.js.map +0 -1
  113. package/dist/file/index.js.map +0 -1
  114. package/dist/file/providers/destination/index.js.map +0 -1
  115. package/dist/file/providers/destination/utils.js.map +0 -1
  116. package/dist/file/providers/index.js.map +0 -1
  117. package/dist/file/providers/source/index.js.map +0 -1
  118. package/dist/index.js.map +0 -1
  119. package/dist/strapi/index.js.map +0 -1
  120. package/dist/strapi/providers/index.js.map +0 -1
  121. package/dist/strapi/providers/local-destination/index.js.map +0 -1
  122. package/dist/strapi/providers/local-destination/strategies/index.js.map +0 -1
  123. package/dist/strapi/providers/local-destination/strategies/restore/configuration.js.map +0 -1
  124. package/dist/strapi/providers/local-destination/strategies/restore/entities.js.map +0 -1
  125. package/dist/strapi/providers/local-destination/strategies/restore/index.js.map +0 -1
  126. package/dist/strapi/providers/local-destination/strategies/restore/links.js.map +0 -1
  127. package/dist/strapi/providers/local-source/assets.js.map +0 -1
  128. package/dist/strapi/providers/local-source/configuration.js.map +0 -1
  129. package/dist/strapi/providers/local-source/entities.js.map +0 -1
  130. package/dist/strapi/providers/local-source/index.js.map +0 -1
  131. package/dist/strapi/providers/local-source/links.js.map +0 -1
  132. package/dist/strapi/providers/remote-destination/index.js.map +0 -1
  133. package/dist/strapi/providers/remote-source/index.js.map +0 -1
  134. package/dist/strapi/providers/utils.js.map +0 -1
  135. package/dist/strapi/queries/entity.js.map +0 -1
  136. package/dist/strapi/queries/index.js.map +0 -1
  137. package/dist/strapi/queries/link.js.map +0 -1
  138. package/dist/strapi/remote/constants.js.map +0 -1
  139. package/dist/strapi/remote/flows/default.js.map +0 -1
  140. package/dist/strapi/remote/flows/index.js.map +0 -1
  141. package/dist/strapi/remote/handlers/abstract.js.map +0 -1
  142. package/dist/strapi/remote/handlers/constants.js.map +0 -1
  143. package/dist/strapi/remote/handlers/index.js.map +0 -1
  144. package/dist/strapi/remote/handlers/pull.js.map +0 -1
  145. package/dist/strapi/remote/handlers/push.js.map +0 -1
  146. package/dist/strapi/remote/handlers/utils.js.map +0 -1
  147. package/dist/strapi/remote/index.js.map +0 -1
  148. package/dist/utils/encryption/decrypt.js.map +0 -1
  149. package/dist/utils/encryption/encrypt.js.map +0 -1
  150. package/dist/utils/encryption/index.js.map +0 -1
  151. package/dist/utils/index.js.map +0 -1
  152. package/dist/utils/json.js.map +0 -1
  153. package/dist/utils/providers.js.map +0 -1
  154. package/dist/utils/schema.js.map +0 -1
  155. package/dist/utils/stream.js.map +0 -1
  156. package/dist/utils/transaction.js.map +0 -1
@@ -0,0 +1,220 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ var _LocalFileSourceProvider_instances, _LocalFileSourceProvider_metadata, _LocalFileSourceProvider_getBackupStream, _LocalFileSourceProvider_streamJsonlDirectory, _LocalFileSourceProvider_parseJSONFile;
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.createLocalFileSourceProvider = void 0;
19
+ const fs_extra_1 = __importDefault(require("fs-extra"));
20
+ const zlib_1 = __importDefault(require("zlib"));
21
+ const tar_1 = __importDefault(require("tar"));
22
+ const path_1 = __importDefault(require("path"));
23
+ const fp_1 = require("lodash/fp");
24
+ const stream_chain_1 = require("stream-chain");
25
+ const stream_1 = require("stream");
26
+ const Parser_1 = require("stream-json/jsonl/Parser");
27
+ const encryption_1 = require("../../../utils/encryption");
28
+ const stream_2 = require("../../../utils/stream");
29
+ const providers_1 = require("../../../errors/providers");
30
+ /**
31
+ * Constant for the metadata file path
32
+ */
33
+ const METADATA_FILE_PATH = 'metadata.json';
34
+ const createLocalFileSourceProvider = (options) => {
35
+ return new LocalFileSourceProvider(options);
36
+ };
37
+ exports.createLocalFileSourceProvider = createLocalFileSourceProvider;
38
+ class LocalFileSourceProvider {
39
+ constructor(options) {
40
+ _LocalFileSourceProvider_instances.add(this);
41
+ this.type = 'source';
42
+ this.name = 'source::local-file';
43
+ _LocalFileSourceProvider_metadata.set(this, void 0);
44
+ this.options = options;
45
+ const { encryption } = this.options;
46
+ if (encryption.enabled && encryption.key === undefined) {
47
+ throw new Error('Missing encryption key');
48
+ }
49
+ }
50
+ /**
51
+ * Pre flight checks regarding the provided options, making sure that the file can be opened (decrypted, decompressed), etc.
52
+ */
53
+ async bootstrap() {
54
+ const { path: filePath } = this.options.file;
55
+ try {
56
+ // Read the metadata to ensure the file can be parsed
57
+ __classPrivateFieldSet(this, _LocalFileSourceProvider_metadata, await this.getMetadata(), "f");
58
+ }
59
+ catch (e) {
60
+ if (this.options?.encryption?.enabled) {
61
+ throw new providers_1.ProviderInitializationError(`Key is incorrect or the file '${filePath}' is not a valid Strapi data file.`);
62
+ }
63
+ throw new providers_1.ProviderInitializationError(`File '${filePath}' is not a valid Strapi data file.`);
64
+ }
65
+ }
66
+ getMetadata() {
67
+ // TODO: need to read the file & extract the metadata json file
68
+ // => we might also need to read the schema.jsonl files & implements a custom stream-check
69
+ const backupStream = __classPrivateFieldGet(this, _LocalFileSourceProvider_instances, "m", _LocalFileSourceProvider_getBackupStream).call(this);
70
+ return __classPrivateFieldGet(this, _LocalFileSourceProvider_instances, "m", _LocalFileSourceProvider_parseJSONFile).call(this, backupStream, METADATA_FILE_PATH);
71
+ }
72
+ async getSchemas() {
73
+ const schemas = await (0, stream_2.collect)(this.createSchemasReadStream());
74
+ return (0, fp_1.keyBy)('uid', schemas);
75
+ }
76
+ createEntitiesReadStream() {
77
+ return __classPrivateFieldGet(this, _LocalFileSourceProvider_instances, "m", _LocalFileSourceProvider_streamJsonlDirectory).call(this, 'entities');
78
+ }
79
+ createSchemasReadStream() {
80
+ return __classPrivateFieldGet(this, _LocalFileSourceProvider_instances, "m", _LocalFileSourceProvider_streamJsonlDirectory).call(this, 'schemas');
81
+ }
82
+ createLinksReadStream() {
83
+ return __classPrivateFieldGet(this, _LocalFileSourceProvider_instances, "m", _LocalFileSourceProvider_streamJsonlDirectory).call(this, 'links');
84
+ }
85
+ createConfigurationReadStream() {
86
+ // NOTE: TBD
87
+ return __classPrivateFieldGet(this, _LocalFileSourceProvider_instances, "m", _LocalFileSourceProvider_streamJsonlDirectory).call(this, 'configuration');
88
+ }
89
+ createAssetsReadStream() {
90
+ const inStream = __classPrivateFieldGet(this, _LocalFileSourceProvider_instances, "m", _LocalFileSourceProvider_getBackupStream).call(this);
91
+ const outStream = new stream_1.PassThrough({ objectMode: true });
92
+ (0, stream_1.pipeline)([
93
+ inStream,
94
+ new tar_1.default.Parse({
95
+ filter(filePath, entry) {
96
+ if (entry.type !== 'File') {
97
+ return false;
98
+ }
99
+ const parts = filePath.split('/');
100
+ return parts[0] === 'assets' && parts[1] === 'uploads';
101
+ },
102
+ onentry(entry) {
103
+ const { path: filePath, size = 0 } = entry;
104
+ const file = path_1.default.basename(filePath);
105
+ const asset = {
106
+ filename: file,
107
+ filepath: filePath,
108
+ stats: { size },
109
+ stream: entry,
110
+ };
111
+ outStream.write(asset);
112
+ },
113
+ }),
114
+ ], () => outStream.end());
115
+ return outStream;
116
+ }
117
+ }
118
+ _LocalFileSourceProvider_metadata = new WeakMap(), _LocalFileSourceProvider_instances = new WeakSet(), _LocalFileSourceProvider_getBackupStream = function _LocalFileSourceProvider_getBackupStream() {
119
+ const { file, encryption, compression } = this.options;
120
+ const streams = [];
121
+ try {
122
+ streams.push(fs_extra_1.default.createReadStream(file.path));
123
+ }
124
+ catch (e) {
125
+ throw new Error(`Could not read backup file path provided at "${this.options.file.path}"`);
126
+ }
127
+ if (encryption.enabled && encryption.key) {
128
+ streams.push((0, encryption_1.createDecryptionCipher)(encryption.key));
129
+ }
130
+ if (compression.enabled) {
131
+ streams.push(zlib_1.default.createGunzip());
132
+ }
133
+ return (0, stream_chain_1.chain)(streams);
134
+ }, _LocalFileSourceProvider_streamJsonlDirectory = function _LocalFileSourceProvider_streamJsonlDirectory(directory) {
135
+ const inStream = __classPrivateFieldGet(this, _LocalFileSourceProvider_instances, "m", _LocalFileSourceProvider_getBackupStream).call(this);
136
+ const outStream = new stream_1.PassThrough({ objectMode: true });
137
+ (0, stream_1.pipeline)([
138
+ inStream,
139
+ new tar_1.default.Parse({
140
+ filter(filePath, entry) {
141
+ if (entry.type !== 'File') {
142
+ return false;
143
+ }
144
+ const parts = path_1.default.relative('.', filePath).split('/');
145
+ // TODO: this method is limiting us from having additional subdirectories and is requiring us to remove any "./" prefixes (the path.relative line above)
146
+ if (parts.length !== 2) {
147
+ return false;
148
+ }
149
+ return parts[0] === directory;
150
+ },
151
+ async onentry(entry) {
152
+ const transforms = [
153
+ // JSONL parser to read the data chunks one by one (line by line)
154
+ (0, Parser_1.parser)({
155
+ checkErrors: true,
156
+ }),
157
+ // The JSONL parser returns each line as key/value
158
+ (line) => line.value,
159
+ ];
160
+ const stream = entry.pipe((0, stream_chain_1.chain)(transforms));
161
+ try {
162
+ for await (const chunk of stream) {
163
+ outStream.write(chunk);
164
+ }
165
+ }
166
+ catch (e) {
167
+ outStream.destroy(new providers_1.ProviderTransferError(`Error parsing backup files from backup file ${entry.path}: ${e.message}`, {
168
+ details: {
169
+ error: e,
170
+ },
171
+ }));
172
+ }
173
+ },
174
+ }),
175
+ ], async () => {
176
+ // Manually send the 'end' event to the out stream
177
+ // once every entry has finished streaming its content
178
+ outStream.end();
179
+ });
180
+ return outStream;
181
+ }, _LocalFileSourceProvider_parseJSONFile =
182
+ // For collecting an entire JSON file then parsing it, not for streaming JSONL
183
+ async function _LocalFileSourceProvider_parseJSONFile(fileStream, filePath) {
184
+ return new Promise((resolve, reject) => {
185
+ (0, stream_1.pipeline)([
186
+ fileStream,
187
+ // Custom backup archive parsing
188
+ new tar_1.default.Parse({
189
+ /**
190
+ * Filter the parsed entries to only keep the one that matches the given filepath
191
+ */
192
+ filter(entryPath, entry) {
193
+ return !path_1.default.relative(filePath, entryPath).length && entry.type === 'File';
194
+ },
195
+ async onentry(entry) {
196
+ // Collect all the content of the entry file
197
+ const content = await entry.collect();
198
+ try {
199
+ // Parse from buffer to string to JSON
200
+ const parsedContent = JSON.parse(content.toString());
201
+ // Resolve the Promise with the parsed content
202
+ resolve(parsedContent);
203
+ }
204
+ catch (e) {
205
+ reject(e);
206
+ }
207
+ finally {
208
+ // Cleanup (close the stream associated to the entry)
209
+ entry.destroy();
210
+ }
211
+ },
212
+ }),
213
+ ], () => {
214
+ // If the promise hasn't been resolved and we've parsed all
215
+ // the archive entries, then the file doesn't exist
216
+ reject(new Error(`File "${filePath}" not found`));
217
+ });
218
+ });
219
+ };
220
+ //# sourceMappingURL=index.js.map
package/lib/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * as engine from './engine';
2
+ export * as strapi from './strapi';
3
+ export * as file from './file';
4
+ export * as utils from './utils';
package/lib/index.js ADDED
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.utils = exports.file = exports.strapi = exports.engine = void 0;
27
+ exports.engine = __importStar(require("./engine"));
28
+ exports.strapi = __importStar(require("./strapi"));
29
+ exports.file = __importStar(require("./file"));
30
+ exports.utils = __importStar(require("./utils"));
31
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ export * as providers from './providers';
2
+ export * as queries from './queries';
3
+ export * as remote from './remote';
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.remote = exports.queries = exports.providers = void 0;
27
+ exports.providers = __importStar(require("./providers"));
28
+ exports.queries = __importStar(require("./queries"));
29
+ exports.remote = __importStar(require("./remote"));
30
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ export * from './local-destination';
2
+ export * from './local-source';
3
+ export * from './remote-destination';
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ // Local
18
+ __exportStar(require("./local-destination"), exports);
19
+ __exportStar(require("./local-source"), exports);
20
+ // Remote
21
+ __exportStar(require("./remote-destination"), exports);
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,34 @@
1
+ /// <reference types="lodash" />
2
+ /// <reference types="node" />
3
+ import { Writable } from 'stream';
4
+ import type { IDestinationProvider, IMetadata, ProviderType, Transaction } from '../../../../types';
5
+ import { restore } from './strategies';
6
+ export declare const VALID_CONFLICT_STRATEGIES: string[];
7
+ export declare const DEFAULT_CONFLICT_STRATEGY = "restore";
8
+ export interface ILocalStrapiDestinationProviderOptions {
9
+ getStrapi(): Strapi.Strapi | Promise<Strapi.Strapi>;
10
+ autoDestroy?: boolean;
11
+ restore?: restore.IRestoreOptions;
12
+ strategy: 'restore' | 'merge';
13
+ }
14
+ declare class LocalStrapiDestinationProvider implements IDestinationProvider {
15
+ #private;
16
+ name: string;
17
+ type: ProviderType;
18
+ options: ILocalStrapiDestinationProviderOptions;
19
+ strapi?: Strapi.Strapi;
20
+ transaction?: Transaction;
21
+ constructor(options: ILocalStrapiDestinationProviderOptions);
22
+ bootstrap(): Promise<void>;
23
+ close(): Promise<void>;
24
+ rollback(): Promise<void>;
25
+ beforeTransfer(): Promise<void>;
26
+ getMetadata(): IMetadata;
27
+ getSchemas(): import("lodash").Dictionary<Partial<import("@strapi/strapi").Schema>>;
28
+ createEntitiesWriteStream(): Writable;
29
+ createAssetsWriteStream(): Promise<Writable>;
30
+ createConfigurationWriteStream(): Promise<Writable>;
31
+ createLinksWriteStream(): Promise<Writable>;
32
+ }
33
+ export declare const createLocalStrapiDestinationProvider: (options: ILocalStrapiDestinationProviderOptions) => LocalStrapiDestinationProvider;
34
+ export {};
@@ -0,0 +1,220 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
26
+ if (kind === "m") throw new TypeError("Private method is not writable");
27
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
28
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
29
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
30
+ };
31
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
32
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
33
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
34
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
35
+ };
36
+ var __importDefault = (this && this.__importDefault) || function (mod) {
37
+ return (mod && mod.__esModule) ? mod : { "default": mod };
38
+ };
39
+ var _LocalStrapiDestinationProvider_instances, _LocalStrapiDestinationProvider_entitiesMapper, _LocalStrapiDestinationProvider_validateOptions, _LocalStrapiDestinationProvider_deleteAll;
40
+ Object.defineProperty(exports, "__esModule", { value: true });
41
+ exports.createLocalStrapiDestinationProvider = exports.DEFAULT_CONFLICT_STRATEGY = exports.VALID_CONFLICT_STRATEGIES = void 0;
42
+ const stream_1 = require("stream");
43
+ const path_1 = __importDefault(require("path"));
44
+ const fse = __importStar(require("fs-extra"));
45
+ const strategies_1 = require("./strategies");
46
+ const utils = __importStar(require("../../../utils"));
47
+ const providers_1 = require("../../../errors/providers");
48
+ const providers_2 = require("../../../utils/providers");
49
+ exports.VALID_CONFLICT_STRATEGIES = ['restore', 'merge'];
50
+ exports.DEFAULT_CONFLICT_STRATEGY = 'restore';
51
+ class LocalStrapiDestinationProvider {
52
+ constructor(options) {
53
+ _LocalStrapiDestinationProvider_instances.add(this);
54
+ this.name = 'destination::local-strapi';
55
+ this.type = 'destination';
56
+ /**
57
+ * The entities mapper is used to map old entities to their new IDs
58
+ */
59
+ _LocalStrapiDestinationProvider_entitiesMapper.set(this, void 0);
60
+ this.options = options;
61
+ __classPrivateFieldSet(this, _LocalStrapiDestinationProvider_entitiesMapper, {}, "f");
62
+ }
63
+ async bootstrap() {
64
+ __classPrivateFieldGet(this, _LocalStrapiDestinationProvider_instances, "m", _LocalStrapiDestinationProvider_validateOptions).call(this);
65
+ this.strapi = await this.options.getStrapi();
66
+ this.transaction = utils.transaction.createTransaction(this.strapi);
67
+ }
68
+ async close() {
69
+ const { autoDestroy } = this.options;
70
+ this.transaction?.end();
71
+ // Basically `!== false` but more deterministic
72
+ if (autoDestroy === undefined || autoDestroy === true) {
73
+ await this.strapi?.destroy();
74
+ }
75
+ }
76
+ async rollback() {
77
+ await this.transaction?.rollback();
78
+ }
79
+ async beforeTransfer() {
80
+ if (!this.strapi) {
81
+ throw new Error('Strapi instance not found');
82
+ }
83
+ await this.transaction?.attach(async () => {
84
+ try {
85
+ if (this.options.strategy === 'restore') {
86
+ await __classPrivateFieldGet(this, _LocalStrapiDestinationProvider_instances, "m", _LocalStrapiDestinationProvider_deleteAll).call(this);
87
+ }
88
+ }
89
+ catch (error) {
90
+ throw new Error(`restore failed ${error}`);
91
+ }
92
+ });
93
+ }
94
+ getMetadata() {
95
+ const strapiVersion = strapi.config.get('info.strapi');
96
+ const createdAt = new Date().toISOString();
97
+ return {
98
+ createdAt,
99
+ strapi: {
100
+ version: strapiVersion,
101
+ },
102
+ };
103
+ }
104
+ getSchemas() {
105
+ (0, providers_2.assertValidStrapi)(this.strapi, 'Not able to get Schemas');
106
+ const schemas = {
107
+ ...this.strapi.contentTypes,
108
+ ...this.strapi.components,
109
+ };
110
+ return utils.schema.mapSchemasValues(schemas);
111
+ }
112
+ createEntitiesWriteStream() {
113
+ (0, providers_2.assertValidStrapi)(this.strapi, 'Not able to import entities');
114
+ const { strategy } = this.options;
115
+ const updateMappingTable = (type, oldID, newID) => {
116
+ if (!__classPrivateFieldGet(this, _LocalStrapiDestinationProvider_entitiesMapper, "f")[type]) {
117
+ __classPrivateFieldGet(this, _LocalStrapiDestinationProvider_entitiesMapper, "f")[type] = {};
118
+ }
119
+ Object.assign(__classPrivateFieldGet(this, _LocalStrapiDestinationProvider_entitiesMapper, "f")[type], { [oldID]: newID });
120
+ };
121
+ if (strategy === 'restore') {
122
+ return strategies_1.restore.createEntitiesWriteStream({
123
+ strapi: this.strapi,
124
+ updateMappingTable,
125
+ transaction: this.transaction,
126
+ });
127
+ }
128
+ throw new providers_1.ProviderValidationError(`Invalid strategy ${this.options.strategy}`, {
129
+ check: 'strategy',
130
+ strategy: this.options.strategy,
131
+ validStrategies: exports.VALID_CONFLICT_STRATEGIES,
132
+ });
133
+ }
134
+ // TODO: Move this logic to the restore strategy
135
+ async createAssetsWriteStream() {
136
+ (0, providers_2.assertValidStrapi)(this.strapi, 'Not able to stream Assets');
137
+ const assetsDirectory = path_1.default.join(this.strapi.dirs.static.public, 'uploads');
138
+ const backupDirectory = path_1.default.join(this.strapi.dirs.static.public, `uploads_backup_${Date.now()}`);
139
+ await fse.move(assetsDirectory, backupDirectory);
140
+ await fse.mkdir(assetsDirectory);
141
+ // Create a .gitkeep file to ensure the directory is not empty
142
+ await fse.outputFile(path_1.default.join(assetsDirectory, '.gitkeep'), '');
143
+ return new stream_1.Writable({
144
+ objectMode: true,
145
+ async final(next) {
146
+ await fse.rm(backupDirectory, { recursive: true, force: true });
147
+ next();
148
+ },
149
+ async write(chunk, _encoding, callback) {
150
+ const entryPath = path_1.default.join(assetsDirectory, chunk.filename);
151
+ const writableStream = fse.createWriteStream(entryPath);
152
+ chunk.stream
153
+ .pipe(writableStream)
154
+ .on('close', () => {
155
+ callback(null);
156
+ })
157
+ .on('error', async (error) => {
158
+ const errorMessage = error.code === 'ENOSPC'
159
+ ? " Your server doesn't have space to proceed with the import. "
160
+ : ' ';
161
+ try {
162
+ await fse.rm(assetsDirectory, { recursive: true, force: true });
163
+ await fse.move(backupDirectory, assetsDirectory);
164
+ this.destroy(new providers_1.ProviderTransferError(`There was an error during the transfer process.${errorMessage}The original files have been restored to ${assetsDirectory}`));
165
+ }
166
+ catch (err) {
167
+ throw new providers_1.ProviderTransferError(`There was an error doing the rollback process. The original files are in ${backupDirectory}, but we failed to restore them to ${assetsDirectory}`);
168
+ }
169
+ finally {
170
+ callback(error);
171
+ }
172
+ });
173
+ },
174
+ });
175
+ }
176
+ async createConfigurationWriteStream() {
177
+ (0, providers_2.assertValidStrapi)(this.strapi, 'Not able to stream Configurations');
178
+ const { strategy } = this.options;
179
+ if (strategy === 'restore') {
180
+ return strategies_1.restore.createConfigurationWriteStream(this.strapi, this.transaction);
181
+ }
182
+ throw new providers_1.ProviderValidationError(`Invalid strategy ${strategy}`, {
183
+ check: 'strategy',
184
+ strategy,
185
+ validStrategies: exports.VALID_CONFLICT_STRATEGIES,
186
+ });
187
+ }
188
+ async createLinksWriteStream() {
189
+ if (!this.strapi) {
190
+ throw new Error('Not able to stream links. Strapi instance not found');
191
+ }
192
+ const { strategy } = this.options;
193
+ const mapID = (uid, id) => __classPrivateFieldGet(this, _LocalStrapiDestinationProvider_entitiesMapper, "f")[uid]?.[id];
194
+ if (strategy === 'restore') {
195
+ return strategies_1.restore.createLinksWriteStream(mapID, this.strapi, this.transaction);
196
+ }
197
+ throw new providers_1.ProviderValidationError(`Invalid strategy ${strategy}`, {
198
+ check: 'strategy',
199
+ strategy,
200
+ validStrategies: exports.VALID_CONFLICT_STRATEGIES,
201
+ });
202
+ }
203
+ }
204
+ _LocalStrapiDestinationProvider_entitiesMapper = new WeakMap(), _LocalStrapiDestinationProvider_instances = new WeakSet(), _LocalStrapiDestinationProvider_validateOptions = function _LocalStrapiDestinationProvider_validateOptions() {
205
+ if (!exports.VALID_CONFLICT_STRATEGIES.includes(this.options.strategy)) {
206
+ throw new providers_1.ProviderValidationError(`Invalid strategy ${this.options.strategy}`, {
207
+ check: 'strategy',
208
+ strategy: this.options.strategy,
209
+ validStrategies: exports.VALID_CONFLICT_STRATEGIES,
210
+ });
211
+ }
212
+ }, _LocalStrapiDestinationProvider_deleteAll = async function _LocalStrapiDestinationProvider_deleteAll() {
213
+ (0, providers_2.assertValidStrapi)(this.strapi);
214
+ return strategies_1.restore.deleteRecords(this.strapi, this.options.restore);
215
+ };
216
+ const createLocalStrapiDestinationProvider = (options) => {
217
+ return new LocalStrapiDestinationProvider(options);
218
+ };
219
+ exports.createLocalStrapiDestinationProvider = createLocalStrapiDestinationProvider;
220
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ export * as restore from './restore';
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.restore = void 0;
27
+ exports.restore = __importStar(require("./restore"));
28
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,5 @@
1
+ /// <reference types="node" />
2
+ import { Writable } from 'stream';
3
+ import { IConfiguration, Transaction } from '../../../../../../types';
4
+ export declare const restoreConfigs: (strapi: Strapi.Strapi, config: IConfiguration) => Promise<any>;
5
+ export declare const createConfigurationWriteStream: (strapi: Strapi.Strapi, transaction?: Transaction | undefined) => Promise<Writable>;