@strapi/data-transfer 4.13.0 → 4.13.2

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 (169) hide show
  1. package/dist/engine/diagnostic.d.ts +40 -0
  2. package/dist/engine/diagnostic.js +50 -0
  3. package/dist/engine/diagnostic.js.map +1 -0
  4. package/dist/engine/errors.d.ts +28 -0
  5. package/dist/engine/errors.js +29 -0
  6. package/dist/engine/errors.js.map +1 -0
  7. package/dist/engine/index.d.ts +64 -0
  8. package/dist/engine/index.js +675 -0
  9. package/dist/engine/index.js.map +1 -0
  10. package/dist/engine/validation/index.d.ts +2 -0
  11. package/dist/engine/validation/index.js +19 -0
  12. package/dist/engine/validation/index.js.map +1 -0
  13. package/dist/engine/validation/provider.d.ts +3 -0
  14. package/dist/engine/validation/provider.js +18 -0
  15. package/dist/engine/validation/provider.js.map +1 -0
  16. package/dist/engine/validation/schemas/index.d.ts +7 -0
  17. package/dist/engine/validation/schemas/index.js +70 -0
  18. package/dist/engine/validation/schemas/index.js.map +1 -0
  19. package/dist/errors/base.d.ts +8 -0
  20. package/dist/errors/base.js +13 -0
  21. package/dist/errors/base.js.map +1 -0
  22. package/dist/errors/constants.d.ts +3 -0
  23. package/dist/errors/constants.js +9 -0
  24. package/dist/errors/constants.js.map +1 -0
  25. package/dist/errors/index.d.ts +2 -0
  26. package/dist/errors/index.js +19 -0
  27. package/dist/errors/index.js.map +1 -0
  28. package/dist/errors/providers.d.ts +23 -0
  29. package/dist/errors/providers.js +32 -0
  30. package/dist/errors/providers.js.map +1 -0
  31. package/dist/file/index.d.ts +1 -0
  32. package/dist/file/index.js +28 -0
  33. package/dist/file/index.js.map +1 -0
  34. package/dist/file/providers/destination/index.d.ts +46 -0
  35. package/dist/file/providers/destination/index.js +181 -0
  36. package/dist/file/providers/destination/index.js.map +1 -0
  37. package/dist/file/providers/destination/utils.d.ts +9 -0
  38. package/dist/file/providers/destination/utils.js +62 -0
  39. package/dist/file/providers/destination/utils.js.map +1 -0
  40. package/dist/file/providers/index.d.ts +2 -0
  41. package/dist/file/providers/index.js +19 -0
  42. package/dist/file/providers/index.js.map +1 -0
  43. package/dist/file/providers/source/index.d.ts +40 -0
  44. package/dist/file/providers/source/index.js +242 -0
  45. package/dist/file/providers/source/index.js.map +1 -0
  46. package/dist/file/providers/source/utils.d.ts +37 -0
  47. package/dist/file/providers/source/utils.js +62 -0
  48. package/dist/file/providers/source/utils.js.map +1 -0
  49. package/dist/index.d.ts +4 -0
  50. package/dist/index.js +31 -0
  51. package/dist/index.js.map +1 -0
  52. package/dist/strapi/index.d.ts +3 -0
  53. package/dist/strapi/index.js +30 -0
  54. package/dist/strapi/index.js.map +1 -0
  55. package/dist/strapi/providers/index.d.ts +4 -0
  56. package/dist/strapi/providers/index.js +23 -0
  57. package/dist/strapi/providers/index.js.map +1 -0
  58. package/dist/strapi/providers/local-destination/index.d.ts +34 -0
  59. package/dist/strapi/providers/local-destination/index.js +365 -0
  60. package/dist/strapi/providers/local-destination/index.js.map +1 -0
  61. package/dist/strapi/providers/local-destination/strategies/index.d.ts +1 -0
  62. package/dist/strapi/providers/local-destination/strategies/index.js +28 -0
  63. package/dist/strapi/providers/local-destination/strategies/index.js.map +1 -0
  64. package/dist/strapi/providers/local-destination/strategies/restore/configuration.d.ts +5 -0
  65. package/dist/strapi/providers/local-destination/strategies/restore/configuration.js +51 -0
  66. package/dist/strapi/providers/local-destination/strategies/restore/configuration.js.map +1 -0
  67. package/dist/strapi/providers/local-destination/strategies/restore/entities.d.ts +11 -0
  68. package/dist/strapi/providers/local-destination/strategies/restore/entities.js +103 -0
  69. package/dist/strapi/providers/local-destination/strategies/restore/entities.js.map +1 -0
  70. package/dist/strapi/providers/local-destination/strategies/restore/index.d.ts +32 -0
  71. package/dist/strapi/providers/local-destination/strategies/restore/index.js +109 -0
  72. package/dist/strapi/providers/local-destination/strategies/restore/index.js.map +1 -0
  73. package/dist/strapi/providers/local-destination/strategies/restore/links.d.ts +4 -0
  74. package/dist/strapi/providers/local-destination/strategies/restore/links.js +32 -0
  75. package/dist/strapi/providers/local-destination/strategies/restore/links.js.map +1 -0
  76. package/dist/strapi/providers/local-source/assets.d.ts +5 -0
  77. package/dist/strapi/providers/local-source/assets.js +98 -0
  78. package/dist/strapi/providers/local-source/assets.js.map +1 -0
  79. package/dist/strapi/providers/local-source/configuration.d.ts +5 -0
  80. package/dist/strapi/providers/local-source/configuration.js +36 -0
  81. package/dist/strapi/providers/local-source/configuration.js.map +1 -0
  82. package/dist/strapi/providers/local-source/entities.d.ts +10 -0
  83. package/dist/strapi/providers/local-source/entities.js +85 -0
  84. package/dist/strapi/providers/local-source/entities.js.map +1 -0
  85. package/dist/strapi/providers/local-source/index.d.ts +26 -0
  86. package/dist/strapi/providers/local-source/index.js +98 -0
  87. package/dist/strapi/providers/local-source/index.js.map +1 -0
  88. package/dist/strapi/providers/local-source/links.d.ts +5 -0
  89. package/dist/strapi/providers/local-source/links.js +23 -0
  90. package/dist/strapi/providers/local-source/links.js.map +1 -0
  91. package/dist/strapi/providers/remote-destination/index.d.ts +39 -0
  92. package/dist/strapi/providers/remote-destination/index.js +269 -0
  93. package/dist/strapi/providers/remote-destination/index.js.map +1 -0
  94. package/dist/strapi/providers/remote-source/index.d.ts +39 -0
  95. package/dist/strapi/providers/remote-source/index.js +239 -0
  96. package/dist/strapi/providers/remote-source/index.js.map +1 -0
  97. package/dist/strapi/providers/utils.d.ts +39 -0
  98. package/dist/strapi/providers/utils.js +138 -0
  99. package/dist/strapi/providers/utils.js.map +1 -0
  100. package/dist/strapi/queries/entity.d.ts +2 -0
  101. package/dist/strapi/queries/entity.js +130 -0
  102. package/dist/strapi/queries/entity.js.map +1 -0
  103. package/dist/strapi/queries/index.d.ts +2 -0
  104. package/dist/strapi/queries/index.js +29 -0
  105. package/dist/strapi/queries/index.js.map +1 -0
  106. package/dist/strapi/queries/link.d.ts +7 -0
  107. package/dist/strapi/queries/link.js +215 -0
  108. package/dist/strapi/queries/link.js.map +1 -0
  109. package/dist/strapi/remote/constants.d.ts +4 -0
  110. package/dist/strapi/remote/constants.js +6 -0
  111. package/dist/strapi/remote/constants.js.map +1 -0
  112. package/dist/strapi/remote/flows/default.d.ts +3 -0
  113. package/dist/strapi/remote/flows/default.js +41 -0
  114. package/dist/strapi/remote/flows/default.js.map +1 -0
  115. package/dist/strapi/remote/flows/index.d.ts +18 -0
  116. package/dist/strapi/remote/flows/index.js +59 -0
  117. package/dist/strapi/remote/flows/index.js.map +1 -0
  118. package/dist/strapi/remote/handlers/abstract.d.ts +71 -0
  119. package/dist/strapi/remote/handlers/abstract.js +3 -0
  120. package/dist/strapi/remote/handlers/abstract.js.map +1 -0
  121. package/dist/strapi/remote/handlers/constants.d.ts +2 -0
  122. package/dist/strapi/remote/handlers/constants.js +5 -0
  123. package/dist/strapi/remote/handlers/constants.js.map +1 -0
  124. package/dist/strapi/remote/handlers/index.d.ts +3 -0
  125. package/dist/strapi/remote/handlers/index.js +10 -0
  126. package/dist/strapi/remote/handlers/index.js.map +1 -0
  127. package/dist/strapi/remote/handlers/pull.d.ts +22 -0
  128. package/dist/strapi/remote/handlers/pull.js +243 -0
  129. package/dist/strapi/remote/handlers/pull.js.map +1 -0
  130. package/dist/strapi/remote/handlers/push.d.ts +75 -0
  131. package/dist/strapi/remote/handlers/push.js +311 -0
  132. package/dist/strapi/remote/handlers/push.js.map +1 -0
  133. package/dist/strapi/remote/handlers/utils.d.ts +25 -0
  134. package/dist/strapi/remote/handlers/utils.js +217 -0
  135. package/dist/strapi/remote/handlers/utils.js.map +1 -0
  136. package/dist/strapi/remote/index.d.ts +2 -0
  137. package/dist/strapi/remote/index.js +29 -0
  138. package/dist/strapi/remote/index.js.map +1 -0
  139. package/dist/utils/encryption/decrypt.d.ts +11 -0
  140. package/dist/utils/encryption/decrypt.js +47 -0
  141. package/dist/utils/encryption/decrypt.js.map +1 -0
  142. package/dist/utils/encryption/encrypt.d.ts +11 -0
  143. package/dist/utils/encryption/encrypt.js +47 -0
  144. package/dist/utils/encryption/encrypt.js.map +1 -0
  145. package/dist/utils/encryption/index.d.ts +2 -0
  146. package/dist/utils/encryption/index.js +19 -0
  147. package/dist/utils/encryption/index.js.map +1 -0
  148. package/dist/utils/index.d.ts +6 -0
  149. package/dist/utils/index.js +33 -0
  150. package/dist/utils/index.js.map +1 -0
  151. package/dist/utils/json.d.ts +30 -0
  152. package/dist/utils/json.js +68 -0
  153. package/dist/utils/json.js.map +1 -0
  154. package/dist/utils/middleware.d.ts +2 -0
  155. package/dist/utils/middleware.js +14 -0
  156. package/dist/utils/middleware.js.map +1 -0
  157. package/dist/utils/providers.d.ts +2 -0
  158. package/dist/utils/providers.js +11 -0
  159. package/dist/utils/providers.js.map +1 -0
  160. package/dist/utils/schema.d.ts +6 -0
  161. package/dist/utils/schema.js +29 -0
  162. package/dist/utils/schema.js.map +1 -0
  163. package/dist/utils/stream.d.ts +27 -0
  164. package/dist/utils/stream.js +59 -0
  165. package/dist/utils/stream.js.map +1 -0
  166. package/dist/utils/transaction.d.ts +3 -0
  167. package/dist/utils/transaction.js +88 -0
  168. package/dist/utils/transaction.js.map +1 -0
  169. package/package.json +4 -4
@@ -0,0 +1,675 @@
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 __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
26
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
27
+ 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");
28
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
29
+ };
30
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
31
+ if (kind === "m") throw new TypeError("Private method is not writable");
32
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
33
+ 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");
34
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
35
+ };
36
+ var _TransferEngine_instances, _TransferEngine_metadata, _TransferEngine_schema, _TransferEngine_handlers, _TransferEngine_currentStream, _TransferEngine_createStageTransformStream, _TransferEngine_updateTransferProgress, _TransferEngine_progressTracker, _TransferEngine_emitTransferUpdate, _TransferEngine_emitStageUpdate, _TransferEngine_assertStrapiVersionIntegrity, _TransferEngine_assertSchemasMatching, _TransferEngine_transferStage, _TransferEngine_resolveProviderResource, _TransferEngine_getSchemas;
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.errors = exports.createTransferEngine = exports.DEFAULT_SCHEMA_STRATEGY = exports.DEFAULT_VERSION_STRATEGY = exports.TransferGroupPresets = exports.TRANSFER_STAGES = void 0;
39
+ const stream_1 = require("stream");
40
+ const path_1 = require("path");
41
+ const os_1 = require("os");
42
+ const stream_chain_1 = require("stream-chain");
43
+ const fp_1 = require("lodash/fp");
44
+ const semver_1 = require("semver");
45
+ const validation_1 = require("./validation");
46
+ const errors_1 = require("./errors");
47
+ const diagnostic_1 = require("./diagnostic");
48
+ const utils = __importStar(require("../utils"));
49
+ const providers_1 = require("../errors/providers");
50
+ exports.TRANSFER_STAGES = Object.freeze([
51
+ 'entities',
52
+ 'links',
53
+ 'assets',
54
+ 'schemas',
55
+ 'configuration',
56
+ ]);
57
+ /**
58
+ * Preset filters for only/exclude options
59
+ * */
60
+ exports.TransferGroupPresets = {
61
+ content: {
62
+ links: true,
63
+ entities: true,
64
+ // TODO: If we need to implement filtering on a running stage, it would be done like this, but we still need to implement it
65
+ // [
66
+ // // Example: content processes the entities stage, but filters individual entities
67
+ // {
68
+ // filter(data) {
69
+ // return shouldIncludeThisData(data);
70
+ // },
71
+ // },
72
+ // ],
73
+ },
74
+ files: {
75
+ assets: true,
76
+ },
77
+ config: {
78
+ configuration: true,
79
+ },
80
+ };
81
+ exports.DEFAULT_VERSION_STRATEGY = 'ignore';
82
+ exports.DEFAULT_SCHEMA_STRATEGY = 'strict';
83
+ class TransferEngine {
84
+ onSchemaDiff(handler) {
85
+ __classPrivateFieldGet(this, _TransferEngine_handlers, "f")?.schemaDiff?.push(handler);
86
+ }
87
+ addErrorHandler(handlerName, handler) {
88
+ if (!__classPrivateFieldGet(this, _TransferEngine_handlers, "f").errors[handlerName]) {
89
+ __classPrivateFieldGet(this, _TransferEngine_handlers, "f").errors[handlerName] = [];
90
+ }
91
+ __classPrivateFieldGet(this, _TransferEngine_handlers, "f").errors[handlerName]?.push(handler);
92
+ }
93
+ async attemptResolveError(error) {
94
+ const context = {};
95
+ if (error instanceof providers_1.ProviderTransferError && error.details?.details.code) {
96
+ const errorCode = error.details?.details.code;
97
+ if (!__classPrivateFieldGet(this, _TransferEngine_handlers, "f").errors[errorCode]) {
98
+ __classPrivateFieldGet(this, _TransferEngine_handlers, "f").errors[errorCode] = [];
99
+ }
100
+ await utils.middleware.runMiddleware(context ?? {}, __classPrivateFieldGet(this, _TransferEngine_handlers, "f").errors[errorCode] ?? []);
101
+ }
102
+ return !!context.ignore;
103
+ }
104
+ constructor(sourceProvider, destinationProvider, options) {
105
+ _TransferEngine_instances.add(this);
106
+ _TransferEngine_metadata.set(this, {});
107
+ _TransferEngine_schema.set(this, {});
108
+ _TransferEngine_handlers.set(this, {
109
+ schemaDiff: [],
110
+ errors: {},
111
+ });
112
+ // Save the currently open stream so that we can access it at any time
113
+ _TransferEngine_currentStream.set(this, void 0);
114
+ this.diagnostics = (0, diagnostic_1.createDiagnosticReporter)();
115
+ (0, validation_1.validateProvider)('source', sourceProvider);
116
+ (0, validation_1.validateProvider)('destination', destinationProvider);
117
+ this.sourceProvider = sourceProvider;
118
+ this.destinationProvider = destinationProvider;
119
+ this.options = options;
120
+ this.progress = { data: {}, stream: new stream_1.PassThrough({ objectMode: true }) };
121
+ }
122
+ /**
123
+ * Report a fatal error and throw it
124
+ */
125
+ panic(error) {
126
+ this.reportError(error, 'fatal');
127
+ throw error;
128
+ }
129
+ /**
130
+ * Report an error diagnostic
131
+ */
132
+ reportError(error, severity) {
133
+ this.diagnostics.report({
134
+ kind: 'error',
135
+ details: {
136
+ severity,
137
+ createdAt: new Date(),
138
+ name: error.name,
139
+ message: error.message,
140
+ error,
141
+ },
142
+ });
143
+ }
144
+ /**
145
+ * Report a warning diagnostic
146
+ */
147
+ reportWarning(message, origin) {
148
+ this.diagnostics.report({
149
+ kind: 'warning',
150
+ details: { createdAt: new Date(), message, origin },
151
+ });
152
+ }
153
+ /**
154
+ * Report an info diagnostic
155
+ */
156
+ reportInfo(message, params) {
157
+ this.diagnostics.report({
158
+ kind: 'info',
159
+ details: { createdAt: new Date(), message, params },
160
+ });
161
+ }
162
+ shouldSkipStage(stage) {
163
+ const { exclude, only } = this.options;
164
+ // schemas must always be included
165
+ if (stage === 'schemas') {
166
+ return false;
167
+ }
168
+ // everything is included by default unless 'only' has been set
169
+ let included = (0, fp_1.isEmpty)(only);
170
+ if (only?.length > 0) {
171
+ included = only.some((transferGroup) => {
172
+ return exports.TransferGroupPresets[transferGroup][stage];
173
+ });
174
+ }
175
+ if (exclude?.length > 0) {
176
+ if (included) {
177
+ included = !exclude.some((transferGroup) => {
178
+ return exports.TransferGroupPresets[transferGroup][stage];
179
+ });
180
+ }
181
+ }
182
+ return !included;
183
+ }
184
+ // Cause an ongoing transfer to abort gracefully
185
+ async abortTransfer() {
186
+ const err = new errors_1.TransferEngineError('fatal', 'Transfer aborted.');
187
+ if (!__classPrivateFieldGet(this, _TransferEngine_currentStream, "f")) {
188
+ throw err;
189
+ }
190
+ __classPrivateFieldGet(this, _TransferEngine_currentStream, "f").destroy(err);
191
+ }
192
+ async init() {
193
+ // Resolve providers' resource and store
194
+ // them in the engine's internal state
195
+ await __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_resolveProviderResource).call(this);
196
+ // Update the destination provider's source metadata
197
+ const { source: sourceMetadata } = __classPrivateFieldGet(this, _TransferEngine_metadata, "f");
198
+ if (sourceMetadata) {
199
+ this.destinationProvider.setMetadata?.('source', sourceMetadata);
200
+ }
201
+ }
202
+ /**
203
+ * Run the bootstrap method in both source and destination providers
204
+ */
205
+ async bootstrap() {
206
+ const results = await Promise.allSettled([
207
+ this.sourceProvider.bootstrap?.(),
208
+ this.destinationProvider.bootstrap?.(),
209
+ ]);
210
+ results.forEach((result) => {
211
+ if (result.status === 'rejected') {
212
+ this.panic(result.reason);
213
+ }
214
+ });
215
+ }
216
+ /**
217
+ * Run the close method in both source and destination providers
218
+ */
219
+ async close() {
220
+ const results = await Promise.allSettled([
221
+ this.sourceProvider.close?.(),
222
+ this.destinationProvider.close?.(),
223
+ ]);
224
+ results.forEach((result) => {
225
+ if (result.status === 'rejected') {
226
+ this.panic(result.reason);
227
+ }
228
+ });
229
+ }
230
+ async integrityCheck() {
231
+ const sourceMetadata = await this.sourceProvider.getMetadata();
232
+ const destinationMetadata = await this.destinationProvider.getMetadata();
233
+ if (sourceMetadata && destinationMetadata) {
234
+ __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_assertStrapiVersionIntegrity).call(this, sourceMetadata?.strapi?.version, destinationMetadata?.strapi?.version);
235
+ }
236
+ const { sourceSchemas, destinationSchemas } = await __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_getSchemas).call(this);
237
+ try {
238
+ if (sourceSchemas && destinationSchemas) {
239
+ __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_assertSchemasMatching).call(this, sourceSchemas, destinationSchemas);
240
+ }
241
+ }
242
+ catch (error) {
243
+ // if this is a schema matching error, allow handlers to resolve it
244
+ if (error instanceof errors_1.TransferEngineValidationError && error.details?.details?.diffs) {
245
+ const schemaDiffs = error.details?.details?.diffs;
246
+ const context = {
247
+ ignoredDiffs: {},
248
+ diffs: schemaDiffs,
249
+ source: this.sourceProvider,
250
+ destination: this.destinationProvider,
251
+ };
252
+ // if we don't have any handlers, throw the original error
253
+ if ((0, fp_1.isEmpty)(__classPrivateFieldGet(this, _TransferEngine_handlers, "f").schemaDiff)) {
254
+ throw error;
255
+ }
256
+ await utils.middleware.runMiddleware(context, __classPrivateFieldGet(this, _TransferEngine_handlers, "f").schemaDiff);
257
+ // if there are any remaining diffs that weren't ignored
258
+ const unresolvedDiffs = utils.json.diff(context.diffs, context.ignoredDiffs);
259
+ if (unresolvedDiffs.length) {
260
+ this.panic(new errors_1.TransferEngineValidationError('Unresolved differences in schema', {
261
+ check: 'schema.changes',
262
+ unresolvedDiffs,
263
+ }));
264
+ }
265
+ return;
266
+ }
267
+ throw error;
268
+ }
269
+ }
270
+ async transfer() {
271
+ // reset data between transfers
272
+ this.progress.data = {};
273
+ try {
274
+ __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_emitTransferUpdate).call(this, 'init');
275
+ await this.bootstrap();
276
+ await this.init();
277
+ await this.integrityCheck();
278
+ __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_emitTransferUpdate).call(this, 'start');
279
+ await this.beforeTransfer();
280
+ // Run the transfer stages
281
+ await this.transferSchemas();
282
+ await this.transferEntities();
283
+ await this.transferAssets();
284
+ await this.transferLinks();
285
+ await this.transferConfiguration();
286
+ // Gracefully close the providers
287
+ await this.close();
288
+ __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_emitTransferUpdate).call(this, 'finish');
289
+ }
290
+ catch (e) {
291
+ __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_emitTransferUpdate).call(this, 'error', { error: e });
292
+ const lastDiagnostic = (0, fp_1.last)(this.diagnostics.stack.items);
293
+ // Do not report an error diagnostic if the last one reported the same error
294
+ if (e instanceof Error &&
295
+ (!lastDiagnostic || lastDiagnostic.kind !== 'error' || lastDiagnostic.details.error !== e)) {
296
+ this.reportError(e, e.severity || 'fatal');
297
+ }
298
+ // Rollback the destination provider if an exception is thrown during the transfer
299
+ // Note: This will be configurable in the future
300
+ await this.destinationProvider.rollback?.(e);
301
+ throw e;
302
+ }
303
+ return {
304
+ source: this.sourceProvider.results,
305
+ destination: this.destinationProvider.results,
306
+ engine: this.progress.data,
307
+ };
308
+ }
309
+ async beforeTransfer() {
310
+ const runWithDiagnostic = async (provider) => {
311
+ try {
312
+ await provider.beforeTransfer?.();
313
+ }
314
+ catch (error) {
315
+ if (error instanceof Error) {
316
+ const resolved = await this.attemptResolveError(error);
317
+ if (resolved) {
318
+ return;
319
+ }
320
+ this.panic(error);
321
+ }
322
+ else {
323
+ this.panic(new Error(`Unknwon error when executing "beforeTransfer" on the ${origin} provider`));
324
+ }
325
+ }
326
+ };
327
+ await runWithDiagnostic(this.sourceProvider);
328
+ await runWithDiagnostic(this.destinationProvider);
329
+ }
330
+ async transferSchemas() {
331
+ const stage = 'schemas';
332
+ if (this.shouldSkipStage(stage)) {
333
+ return;
334
+ }
335
+ const source = await this.sourceProvider.createSchemasReadStream?.();
336
+ const destination = await this.destinationProvider.createSchemasWriteStream?.();
337
+ const transform = __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_createStageTransformStream).call(this, stage);
338
+ const tracker = __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_progressTracker).call(this, stage, {
339
+ key: (value) => value.modelType,
340
+ });
341
+ await __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_transferStage).call(this, { stage, source, destination, transform, tracker });
342
+ }
343
+ async transferEntities() {
344
+ const stage = 'entities';
345
+ if (this.shouldSkipStage(stage)) {
346
+ return;
347
+ }
348
+ const source = await this.sourceProvider.createEntitiesReadStream?.();
349
+ const destination = await this.destinationProvider.createEntitiesWriteStream?.();
350
+ const transform = (0, stream_chain_1.chain)([
351
+ __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_createStageTransformStream).call(this, stage),
352
+ new stream_1.Transform({
353
+ objectMode: true,
354
+ transform: async (entity, _encoding, callback) => {
355
+ const { destinationSchemas: schemas } = await __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_getSchemas).call(this);
356
+ if (!schemas) {
357
+ return callback(null, entity);
358
+ }
359
+ // TODO: this would be safer if we only ignored things in ignoredDiffs, otherwise continue and let an error be thrown
360
+ const availableContentTypes = Object.entries(schemas)
361
+ .filter(([, schema]) => schema.modelType === 'contentType')
362
+ .map(([uid]) => uid);
363
+ // If the type of the transferred entity doesn't exist in the destination, then discard it
364
+ if (!availableContentTypes.includes(entity.type)) {
365
+ return callback(null, undefined);
366
+ }
367
+ const { type, data } = entity;
368
+ const attributes = schemas[type].attributes;
369
+ const attributesToRemove = (0, fp_1.difference)(Object.keys(data), Object.keys(attributes));
370
+ const updatedEntity = (0, fp_1.set)('data', (0, fp_1.omit)(attributesToRemove, data), entity);
371
+ callback(null, updatedEntity);
372
+ },
373
+ }),
374
+ ]);
375
+ const tracker = __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_progressTracker).call(this, stage, { key: (value) => value.type });
376
+ await __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_transferStage).call(this, { stage, source, destination, transform, tracker });
377
+ }
378
+ async transferLinks() {
379
+ const stage = 'links';
380
+ if (this.shouldSkipStage(stage)) {
381
+ return;
382
+ }
383
+ const source = await this.sourceProvider.createLinksReadStream?.();
384
+ const destination = await this.destinationProvider.createLinksWriteStream?.();
385
+ const transform = (0, stream_chain_1.chain)([
386
+ __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_createStageTransformStream).call(this, stage),
387
+ new stream_1.Transform({
388
+ objectMode: true,
389
+ transform: async (link, _encoding, callback) => {
390
+ const { destinationSchemas: schemas } = await __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_getSchemas).call(this);
391
+ if (!schemas) {
392
+ return callback(null, link);
393
+ }
394
+ // TODO: this would be safer if we only ignored things in ignoredDiffs, otherwise continue and let an error be thrown
395
+ const availableContentTypes = Object.keys(schemas);
396
+ const isValidType = (uid) => availableContentTypes.includes(uid);
397
+ if (!isValidType(link.left.type) || !isValidType(link.right.type)) {
398
+ return callback(null, undefined); // ignore the link
399
+ }
400
+ callback(null, link);
401
+ },
402
+ }),
403
+ ]);
404
+ const tracker = __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_progressTracker).call(this, stage);
405
+ await __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_transferStage).call(this, { stage, source, destination, transform, tracker });
406
+ }
407
+ async transferAssets() {
408
+ const stage = 'assets';
409
+ if (this.shouldSkipStage(stage)) {
410
+ return;
411
+ }
412
+ const source = await this.sourceProvider.createAssetsReadStream?.();
413
+ const destination = await this.destinationProvider.createAssetsWriteStream?.();
414
+ const transform = __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_createStageTransformStream).call(this, stage);
415
+ const tracker = __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_progressTracker).call(this, stage, {
416
+ size: (value) => value.stats.size,
417
+ key: (value) => (0, path_1.extname)(value.filename) || 'No extension',
418
+ });
419
+ await __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_transferStage).call(this, { stage, source, destination, transform, tracker });
420
+ }
421
+ async transferConfiguration() {
422
+ const stage = 'configuration';
423
+ if (this.shouldSkipStage(stage)) {
424
+ return;
425
+ }
426
+ const source = await this.sourceProvider.createConfigurationReadStream?.();
427
+ const destination = await this.destinationProvider.createConfigurationWriteStream?.();
428
+ const transform = __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_createStageTransformStream).call(this, stage);
429
+ const tracker = __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_progressTracker).call(this, stage);
430
+ await __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_transferStage).call(this, { stage, source, destination, transform, tracker });
431
+ }
432
+ }
433
+ _TransferEngine_metadata = new WeakMap(), _TransferEngine_schema = new WeakMap(), _TransferEngine_handlers = new WeakMap(), _TransferEngine_currentStream = new WeakMap(), _TransferEngine_instances = new WeakSet(), _TransferEngine_createStageTransformStream = function _TransferEngine_createStageTransformStream(key, options = {}) {
434
+ const { includeGlobal = true } = options;
435
+ const { throttle } = this.options;
436
+ const { global: globalTransforms, [key]: stageTransforms } = this.options?.transforms ?? {};
437
+ let stream = new stream_1.PassThrough({ objectMode: true });
438
+ const applyTransforms = (transforms = []) => {
439
+ const chainTransforms = [];
440
+ for (const transform of transforms) {
441
+ if ('filter' in transform) {
442
+ chainTransforms.push(utils.stream.filter(transform.filter));
443
+ }
444
+ if ('map' in transform) {
445
+ chainTransforms.push(utils.stream.map(transform.map));
446
+ }
447
+ }
448
+ if (chainTransforms.length) {
449
+ stream = stream.pipe((0, stream_chain_1.chain)(chainTransforms));
450
+ }
451
+ };
452
+ if (includeGlobal) {
453
+ applyTransforms(globalTransforms);
454
+ }
455
+ if ((0, fp_1.isNumber)(throttle) && throttle > 0) {
456
+ stream = stream.pipe(new stream_1.PassThrough({
457
+ objectMode: true,
458
+ async transform(data, _encoding, callback) {
459
+ await new Promise((resolve) => {
460
+ setTimeout(resolve, throttle);
461
+ });
462
+ callback(null, data);
463
+ },
464
+ }));
465
+ }
466
+ applyTransforms(stageTransforms);
467
+ return stream;
468
+ }, _TransferEngine_updateTransferProgress = function _TransferEngine_updateTransferProgress(stage, data, aggregate) {
469
+ if (!this.progress.data[stage]) {
470
+ this.progress.data[stage] = { count: 0, bytes: 0, startTime: Date.now() };
471
+ }
472
+ const stageProgress = this.progress.data[stage];
473
+ if (!stageProgress) {
474
+ return;
475
+ }
476
+ const size = aggregate?.size?.(data) ?? JSON.stringify(data).length;
477
+ const key = aggregate?.key?.(data);
478
+ stageProgress.count += 1;
479
+ stageProgress.bytes += size;
480
+ // Handle aggregate updates if necessary
481
+ if (key) {
482
+ if (!stageProgress.aggregates) {
483
+ stageProgress.aggregates = {};
484
+ }
485
+ const { aggregates } = stageProgress;
486
+ if (!aggregates[key]) {
487
+ aggregates[key] = { count: 0, bytes: 0 };
488
+ }
489
+ aggregates[key].count += 1;
490
+ aggregates[key].bytes += size;
491
+ }
492
+ }, _TransferEngine_progressTracker = function _TransferEngine_progressTracker(stage, aggregate) {
493
+ return new stream_1.PassThrough({
494
+ objectMode: true,
495
+ transform: (data, _encoding, callback) => {
496
+ __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_updateTransferProgress).call(this, stage, data, aggregate);
497
+ __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_emitStageUpdate).call(this, 'progress', stage);
498
+ callback(null, data);
499
+ },
500
+ });
501
+ }, _TransferEngine_emitTransferUpdate = function _TransferEngine_emitTransferUpdate(type, payload) {
502
+ this.progress.stream.emit(`transfer::${type}`, payload);
503
+ }, _TransferEngine_emitStageUpdate = function _TransferEngine_emitStageUpdate(type, transferStage) {
504
+ this.progress.stream.emit(`stage::${type}`, {
505
+ data: this.progress.data,
506
+ stage: transferStage,
507
+ });
508
+ }, _TransferEngine_assertStrapiVersionIntegrity = function _TransferEngine_assertStrapiVersionIntegrity(sourceVersion, destinationVersion) {
509
+ const strategy = this.options.versionStrategy || exports.DEFAULT_VERSION_STRATEGY;
510
+ const reject = () => {
511
+ throw new errors_1.TransferEngineValidationError(`The source and destination provide are targeting incompatible Strapi versions (using the "${strategy}" strategy). The source (${this.sourceProvider.name}) version is ${sourceVersion} and the destination (${this.destinationProvider.name}) version is ${destinationVersion}`, {
512
+ check: 'strapi.version',
513
+ strategy,
514
+ versions: { source: sourceVersion, destination: destinationVersion },
515
+ });
516
+ };
517
+ if (!sourceVersion ||
518
+ !destinationVersion ||
519
+ strategy === 'ignore' ||
520
+ destinationVersion === sourceVersion) {
521
+ return;
522
+ }
523
+ let diff;
524
+ try {
525
+ diff = (0, semver_1.diff)(sourceVersion, destinationVersion);
526
+ }
527
+ catch {
528
+ reject();
529
+ }
530
+ if (!diff) {
531
+ return;
532
+ }
533
+ const validPatch = ['prelease', 'build'];
534
+ const validMinor = [...validPatch, 'patch', 'prepatch'];
535
+ const validMajor = [...validMinor, 'minor', 'preminor'];
536
+ if (strategy === 'patch' && validPatch.includes(diff)) {
537
+ return;
538
+ }
539
+ if (strategy === 'minor' && validMinor.includes(diff)) {
540
+ return;
541
+ }
542
+ if (strategy === 'major' && validMajor.includes(diff)) {
543
+ return;
544
+ }
545
+ reject();
546
+ }, _TransferEngine_assertSchemasMatching = function _TransferEngine_assertSchemasMatching(sourceSchemas, destinationSchemas) {
547
+ const strategy = this.options.schemaStrategy || exports.DEFAULT_SCHEMA_STRATEGY;
548
+ if (strategy === 'ignore') {
549
+ return;
550
+ }
551
+ const keys = (0, fp_1.uniq)(Object.keys(sourceSchemas).concat(Object.keys(destinationSchemas)));
552
+ const diffs = {};
553
+ keys.forEach((key) => {
554
+ const sourceSchema = sourceSchemas[key];
555
+ const destinationSchema = destinationSchemas[key];
556
+ const schemaDiffs = (0, validation_1.compareSchemas)(sourceSchema, destinationSchema, strategy);
557
+ if (schemaDiffs.length) {
558
+ diffs[key] = schemaDiffs;
559
+ }
560
+ });
561
+ if (!(0, fp_1.isEmpty)(diffs)) {
562
+ const formattedDiffs = Object.entries(diffs)
563
+ .map(([uid, ctDiffs]) => {
564
+ let msg = `- ${uid}:${os_1.EOL}`;
565
+ msg += ctDiffs
566
+ .sort((a, b) => (a.kind > b.kind ? -1 : 1))
567
+ .map((diff) => {
568
+ const path = diff.path.join('.');
569
+ if (diff.kind === 'added') {
570
+ return `${path} exists in destination schema but not in source schema and the data will not be transferred.`;
571
+ }
572
+ if (diff.kind === 'deleted') {
573
+ return `${path} exists in source schema but not in destination schema and the data will not be transferred.`;
574
+ }
575
+ if (diff.kind === 'modified') {
576
+ if (diff.types[0] === diff.types[1]) {
577
+ return `Schema value changed at "${path}": "${diff.values[0]}" (${diff.types[0]}) => "${diff.values[1]}" (${diff.types[1]})`;
578
+ }
579
+ return `Schema has differing data types at "${path}": "${diff.values[0]}" (${diff.types[0]}) => "${diff.values[1]}" (${diff.types[1]})`;
580
+ }
581
+ throw new errors_1.TransferEngineValidationError(`Invalid diff found for "${uid}"`, {
582
+ check: `schema on ${uid}`,
583
+ });
584
+ })
585
+ .map((line) => ` - ${line}`)
586
+ .join(os_1.EOL);
587
+ return msg;
588
+ })
589
+ .join(os_1.EOL);
590
+ throw new errors_1.TransferEngineValidationError(`Invalid schema changes detected during integrity checks (using the ${strategy} strategy). Please find a summary of the changes below:\n${formattedDiffs}`, {
591
+ check: 'schema.changes',
592
+ strategy,
593
+ diffs,
594
+ });
595
+ }
596
+ }, _TransferEngine_transferStage = async function _TransferEngine_transferStage(options) {
597
+ const { stage, source, destination, transform, tracker } = options;
598
+ const updateEndTime = () => {
599
+ const stageData = this.progress.data[stage];
600
+ if (stageData) {
601
+ stageData.endTime = Date.now();
602
+ }
603
+ };
604
+ if (!source || !destination || this.shouldSkipStage(stage)) {
605
+ // Wait until source and destination are closed
606
+ const results = await Promise.allSettled([source, destination].map((stream) => {
607
+ // if stream is undefined or already closed, resolve immediately
608
+ if (!stream || stream.destroyed) {
609
+ return Promise.resolve();
610
+ }
611
+ // Wait until the close event is produced and then destroy the stream and resolve
612
+ return new Promise((resolve, reject) => {
613
+ stream.on('close', resolve).on('error', reject).destroy();
614
+ });
615
+ }));
616
+ results.forEach((state) => {
617
+ if (state.status === 'rejected') {
618
+ this.reportWarning(state.reason, `transfer(${stage})`);
619
+ }
620
+ });
621
+ __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_emitStageUpdate).call(this, 'skip', stage);
622
+ return;
623
+ }
624
+ __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_emitStageUpdate).call(this, 'start', stage);
625
+ await new Promise((resolve, reject) => {
626
+ let stream = source;
627
+ if (transform) {
628
+ stream = stream.pipe(transform);
629
+ }
630
+ if (tracker) {
631
+ stream = stream.pipe(tracker);
632
+ }
633
+ __classPrivateFieldSet(this, _TransferEngine_currentStream, stream
634
+ .pipe(destination)
635
+ .on('error', (e) => {
636
+ updateEndTime();
637
+ __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_emitStageUpdate).call(this, 'error', stage);
638
+ this.reportError(e, 'error');
639
+ destination.destroy(e);
640
+ reject(e);
641
+ })
642
+ .on('close', () => {
643
+ __classPrivateFieldSet(this, _TransferEngine_currentStream, undefined, "f");
644
+ updateEndTime();
645
+ resolve();
646
+ }), "f");
647
+ });
648
+ __classPrivateFieldGet(this, _TransferEngine_instances, "m", _TransferEngine_emitStageUpdate).call(this, 'finish', stage);
649
+ }, _TransferEngine_resolveProviderResource = async function _TransferEngine_resolveProviderResource() {
650
+ const sourceMetadata = await this.sourceProvider.getMetadata();
651
+ const destinationMetadata = await this.destinationProvider.getMetadata();
652
+ if (sourceMetadata) {
653
+ __classPrivateFieldGet(this, _TransferEngine_metadata, "f").source = sourceMetadata;
654
+ }
655
+ if (destinationMetadata) {
656
+ __classPrivateFieldGet(this, _TransferEngine_metadata, "f").destination = destinationMetadata;
657
+ }
658
+ }, _TransferEngine_getSchemas = async function _TransferEngine_getSchemas() {
659
+ if (!__classPrivateFieldGet(this, _TransferEngine_schema, "f").source) {
660
+ __classPrivateFieldGet(this, _TransferEngine_schema, "f").source = (await this.sourceProvider.getSchemas?.());
661
+ }
662
+ if (!__classPrivateFieldGet(this, _TransferEngine_schema, "f").destination) {
663
+ __classPrivateFieldGet(this, _TransferEngine_schema, "f").destination = (await this.destinationProvider.getSchemas?.());
664
+ }
665
+ return {
666
+ sourceSchemas: __classPrivateFieldGet(this, _TransferEngine_schema, "f").source,
667
+ destinationSchemas: __classPrivateFieldGet(this, _TransferEngine_schema, "f").destination,
668
+ };
669
+ };
670
+ const createTransferEngine = (sourceProvider, destinationProvider, options) => {
671
+ return new TransferEngine(sourceProvider, destinationProvider, options);
672
+ };
673
+ exports.createTransferEngine = createTransferEngine;
674
+ exports.errors = __importStar(require("./errors"));
675
+ //# sourceMappingURL=index.js.map