@strapi/data-transfer 5.12.1 → 5.12.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 (213) hide show
  1. package/dist/engine/errors.js +39 -0
  2. package/dist/engine/errors.js.map +1 -0
  3. package/dist/engine/errors.mjs +34 -0
  4. package/dist/engine/errors.mjs.map +1 -0
  5. package/dist/engine/index.js +797 -0
  6. package/dist/engine/index.js.map +1 -0
  7. package/dist/engine/index.mjs +792 -0
  8. package/dist/engine/index.mjs.map +1 -0
  9. package/dist/engine/validation/provider.js +19 -0
  10. package/dist/engine/validation/provider.js.map +1 -0
  11. package/dist/engine/validation/provider.mjs +17 -0
  12. package/dist/engine/validation/provider.mjs.map +1 -0
  13. package/dist/engine/validation/schemas/index.js +57 -0
  14. package/dist/engine/validation/schemas/index.js.map +1 -0
  15. package/dist/engine/validation/schemas/index.mjs +55 -0
  16. package/dist/engine/validation/schemas/index.mjs.map +1 -0
  17. package/dist/errors/base.js +13 -0
  18. package/dist/errors/base.js.map +1 -0
  19. package/dist/errors/base.mjs +11 -0
  20. package/dist/errors/base.mjs.map +1 -0
  21. package/dist/errors/constants.js +10 -0
  22. package/dist/errors/constants.js.map +1 -0
  23. package/dist/errors/constants.mjs +8 -0
  24. package/dist/errors/constants.mjs.map +1 -0
  25. package/dist/errors/providers.js +41 -0
  26. package/dist/errors/providers.js.map +1 -0
  27. package/dist/errors/providers.mjs +36 -0
  28. package/dist/errors/providers.mjs.map +1 -0
  29. package/dist/file/index.js +8 -0
  30. package/dist/file/index.js.map +1 -0
  31. package/dist/file/index.mjs +3 -0
  32. package/dist/file/index.mjs.map +1 -0
  33. package/dist/file/providers/destination/index.js +248 -0
  34. package/dist/file/providers/destination/index.js.map +1 -0
  35. package/dist/file/providers/destination/index.mjs +246 -0
  36. package/dist/file/providers/destination/index.mjs.map +1 -0
  37. package/dist/file/providers/destination/utils.js +63 -0
  38. package/dist/file/providers/destination/utils.js.map +1 -0
  39. package/dist/file/providers/destination/utils.mjs +60 -0
  40. package/dist/file/providers/destination/utils.mjs.map +1 -0
  41. package/dist/file/providers/index.js +10 -0
  42. package/dist/file/providers/index.js.map +1 -0
  43. package/dist/file/providers/index.mjs +3 -0
  44. package/dist/file/providers/index.mjs.map +1 -0
  45. package/dist/file/providers/source/index.js +288 -0
  46. package/dist/file/providers/source/index.js.map +1 -0
  47. package/dist/file/providers/source/index.mjs +286 -0
  48. package/dist/file/providers/source/index.mjs.map +1 -0
  49. package/dist/file/providers/source/utils.js +56 -0
  50. package/dist/file/providers/source/utils.js.map +1 -0
  51. package/dist/file/providers/source/utils.mjs +52 -0
  52. package/dist/file/providers/source/utils.mjs.map +1 -0
  53. package/dist/index.js +8 -5692
  54. package/dist/index.js.map +1 -1
  55. package/dist/index.mjs +8 -5674
  56. package/dist/index.mjs.map +1 -1
  57. package/dist/strapi/index.js +12 -0
  58. package/dist/strapi/index.js.map +1 -0
  59. package/dist/strapi/index.mjs +7 -0
  60. package/dist/strapi/index.mjs.map +1 -0
  61. package/dist/strapi/providers/index.js +16 -0
  62. package/dist/strapi/providers/index.js.map +1 -0
  63. package/dist/strapi/providers/index.mjs +7 -0
  64. package/dist/strapi/providers/index.mjs.map +1 -0
  65. package/dist/strapi/providers/local-destination/index.js +419 -0
  66. package/dist/strapi/providers/local-destination/index.js.map +1 -0
  67. package/dist/strapi/providers/local-destination/index.mjs +396 -0
  68. package/dist/strapi/providers/local-destination/index.mjs.map +1 -0
  69. package/dist/strapi/providers/local-destination/strategies/restore/configuration.js +52 -0
  70. package/dist/strapi/providers/local-destination/strategies/restore/configuration.js.map +1 -0
  71. package/dist/strapi/providers/local-destination/strategies/restore/configuration.mjs +49 -0
  72. package/dist/strapi/providers/local-destination/strategies/restore/configuration.mjs.map +1 -0
  73. package/dist/strapi/providers/local-destination/strategies/restore/entities.js +64 -0
  74. package/dist/strapi/providers/local-destination/strategies/restore/entities.js.map +1 -0
  75. package/dist/strapi/providers/local-destination/strategies/restore/entities.mjs +62 -0
  76. package/dist/strapi/providers/local-destination/strategies/restore/entities.mjs.map +1 -0
  77. package/dist/strapi/providers/local-destination/strategies/restore/index.js +126 -0
  78. package/dist/strapi/providers/local-destination/strategies/restore/index.js.map +1 -0
  79. package/dist/strapi/providers/local-destination/strategies/restore/index.mjs +122 -0
  80. package/dist/strapi/providers/local-destination/strategies/restore/index.mjs.map +1 -0
  81. package/dist/strapi/providers/local-destination/strategies/restore/links.js +60 -0
  82. package/dist/strapi/providers/local-destination/strategies/restore/links.js.map +1 -0
  83. package/dist/strapi/providers/local-destination/strategies/restore/links.mjs +58 -0
  84. package/dist/strapi/providers/local-destination/strategies/restore/links.mjs.map +1 -0
  85. package/dist/strapi/providers/local-source/assets.js +123 -0
  86. package/dist/strapi/providers/local-source/assets.js.map +1 -0
  87. package/dist/strapi/providers/local-source/assets.mjs +121 -0
  88. package/dist/strapi/providers/local-source/assets.mjs.map +1 -0
  89. package/dist/strapi/providers/local-source/configuration.js +39 -0
  90. package/dist/strapi/providers/local-source/configuration.js.map +1 -0
  91. package/dist/strapi/providers/local-source/configuration.mjs +37 -0
  92. package/dist/strapi/providers/local-source/configuration.mjs.map +1 -0
  93. package/dist/strapi/providers/local-source/entities.js +62 -0
  94. package/dist/strapi/providers/local-source/entities.js.map +1 -0
  95. package/dist/strapi/providers/local-source/entities.mjs +59 -0
  96. package/dist/strapi/providers/local-source/entities.mjs.map +1 -0
  97. package/dist/strapi/providers/local-source/index.js +154 -0
  98. package/dist/strapi/providers/local-source/index.js.map +1 -0
  99. package/dist/strapi/providers/local-source/index.mjs +152 -0
  100. package/dist/strapi/providers/local-source/index.mjs.map +1 -0
  101. package/dist/strapi/providers/local-source/links.js +26 -0
  102. package/dist/strapi/providers/local-source/links.js.map +1 -0
  103. package/dist/strapi/providers/local-source/links.mjs +24 -0
  104. package/dist/strapi/providers/local-source/links.mjs.map +1 -0
  105. package/dist/strapi/providers/remote-destination/index.js +392 -0
  106. package/dist/strapi/providers/remote-destination/index.js.map +1 -0
  107. package/dist/strapi/providers/remote-destination/index.mjs +390 -0
  108. package/dist/strapi/providers/remote-destination/index.mjs.map +1 -0
  109. package/dist/strapi/providers/remote-source/index.js +405 -0
  110. package/dist/strapi/providers/remote-source/index.js.map +1 -0
  111. package/dist/strapi/providers/remote-source/index.mjs +403 -0
  112. package/dist/strapi/providers/remote-source/index.mjs.map +1 -0
  113. package/dist/strapi/providers/utils.js +173 -0
  114. package/dist/strapi/providers/utils.js.map +1 -0
  115. package/dist/strapi/providers/utils.mjs +169 -0
  116. package/dist/strapi/providers/utils.mjs.map +1 -0
  117. package/dist/strapi/queries/entity.js +125 -0
  118. package/dist/strapi/queries/entity.js.map +1 -0
  119. package/dist/strapi/queries/entity.mjs +123 -0
  120. package/dist/strapi/queries/entity.mjs.map +1 -0
  121. package/dist/strapi/queries/index.js +10 -0
  122. package/dist/strapi/queries/index.js.map +1 -0
  123. package/dist/strapi/queries/index.mjs +5 -0
  124. package/dist/strapi/queries/index.mjs.map +1 -0
  125. package/dist/strapi/queries/link.js +298 -0
  126. package/dist/strapi/queries/link.js.map +1 -0
  127. package/dist/strapi/queries/link.mjs +295 -0
  128. package/dist/strapi/queries/link.mjs.map +1 -0
  129. package/dist/strapi/remote/constants.js +11 -0
  130. package/dist/strapi/remote/constants.js.map +1 -0
  131. package/dist/strapi/remote/constants.mjs +8 -0
  132. package/dist/strapi/remote/constants.mjs.map +1 -0
  133. package/dist/strapi/remote/flows/default.js +43 -0
  134. package/dist/strapi/remote/flows/default.js.map +1 -0
  135. package/dist/strapi/remote/flows/default.mjs +41 -0
  136. package/dist/strapi/remote/flows/default.mjs.map +1 -0
  137. package/dist/strapi/remote/flows/index.js +54 -0
  138. package/dist/strapi/remote/flows/index.js.map +1 -0
  139. package/dist/strapi/remote/flows/index.mjs +52 -0
  140. package/dist/strapi/remote/flows/index.mjs.map +1 -0
  141. package/dist/strapi/remote/handlers/constants.js +10 -0
  142. package/dist/strapi/remote/handlers/constants.js.map +1 -0
  143. package/dist/strapi/remote/handlers/constants.mjs +8 -0
  144. package/dist/strapi/remote/handlers/constants.mjs.map +1 -0
  145. package/dist/strapi/remote/handlers/index.js +12 -0
  146. package/dist/strapi/remote/handlers/index.js.map +1 -0
  147. package/dist/strapi/remote/handlers/index.mjs +4 -0
  148. package/dist/strapi/remote/handlers/index.mjs.map +1 -0
  149. package/dist/strapi/remote/handlers/pull.js +348 -0
  150. package/dist/strapi/remote/handlers/pull.js.map +1 -0
  151. package/dist/strapi/remote/handlers/pull.mjs +346 -0
  152. package/dist/strapi/remote/handlers/pull.mjs.map +1 -0
  153. package/dist/strapi/remote/handlers/push.js +400 -0
  154. package/dist/strapi/remote/handlers/push.js.map +1 -0
  155. package/dist/strapi/remote/handlers/push.mjs +398 -0
  156. package/dist/strapi/remote/handlers/push.mjs.map +1 -0
  157. package/dist/strapi/remote/handlers/utils.js +316 -0
  158. package/dist/strapi/remote/handlers/utils.js.map +1 -0
  159. package/dist/strapi/remote/handlers/utils.mjs +310 -0
  160. package/dist/strapi/remote/handlers/utils.mjs.map +1 -0
  161. package/dist/strapi/remote/index.js +10 -0
  162. package/dist/strapi/remote/index.js.map +1 -0
  163. package/dist/strapi/remote/index.mjs +5 -0
  164. package/dist/strapi/remote/index.mjs.map +1 -0
  165. package/dist/utils/components.js +178 -0
  166. package/dist/utils/components.js.map +1 -0
  167. package/dist/utils/components.mjs +171 -0
  168. package/dist/utils/components.mjs.map +1 -0
  169. package/dist/utils/diagnostic.js +51 -0
  170. package/dist/utils/diagnostic.js.map +1 -0
  171. package/dist/utils/diagnostic.mjs +49 -0
  172. package/dist/utils/diagnostic.mjs.map +1 -0
  173. package/dist/utils/encryption/decrypt.js +47 -0
  174. package/dist/utils/encryption/decrypt.js.map +1 -0
  175. package/dist/utils/encryption/decrypt.mjs +45 -0
  176. package/dist/utils/encryption/decrypt.mjs.map +1 -0
  177. package/dist/utils/encryption/encrypt.js +47 -0
  178. package/dist/utils/encryption/encrypt.js.map +1 -0
  179. package/dist/utils/encryption/encrypt.mjs +45 -0
  180. package/dist/utils/encryption/encrypt.mjs.map +1 -0
  181. package/dist/utils/encryption/index.js +10 -0
  182. package/dist/utils/encryption/index.js.map +1 -0
  183. package/dist/utils/encryption/index.mjs +3 -0
  184. package/dist/utils/encryption/index.mjs.map +1 -0
  185. package/dist/utils/index.js +20 -0
  186. package/dist/utils/index.js.map +1 -0
  187. package/dist/utils/index.mjs +15 -0
  188. package/dist/utils/index.mjs.map +1 -0
  189. package/dist/utils/json.js +96 -0
  190. package/dist/utils/json.js.map +1 -0
  191. package/dist/utils/json.mjs +94 -0
  192. package/dist/utils/json.mjs.map +1 -0
  193. package/dist/utils/middleware.js +14 -0
  194. package/dist/utils/middleware.js.map +1 -0
  195. package/dist/utils/middleware.mjs +12 -0
  196. package/dist/utils/middleware.mjs.map +1 -0
  197. package/dist/utils/providers.js +12 -0
  198. package/dist/utils/providers.js.map +1 -0
  199. package/dist/utils/providers.mjs +10 -0
  200. package/dist/utils/providers.mjs.map +1 -0
  201. package/dist/utils/schema.js +32 -0
  202. package/dist/utils/schema.js.map +1 -0
  203. package/dist/utils/schema.mjs +29 -0
  204. package/dist/utils/schema.mjs.map +1 -0
  205. package/dist/utils/stream.js +59 -0
  206. package/dist/utils/stream.js.map +1 -0
  207. package/dist/utils/stream.mjs +55 -0
  208. package/dist/utils/stream.mjs.map +1 -0
  209. package/dist/utils/transaction.js +93 -0
  210. package/dist/utils/transaction.js.map +1 -0
  211. package/dist/utils/transaction.mjs +91 -0
  212. package/dist/utils/transaction.mjs.map +1 -0
  213. package/package.json +5 -5
@@ -0,0 +1,792 @@
1
+ import { Transform, PassThrough } from 'stream';
2
+ import { pipeline } from 'stream/promises';
3
+ import { extname } from 'path';
4
+ import { EOL } from 'os';
5
+ import { chain } from 'stream-chain';
6
+ import { isEmpty, last, set, pick, isNumber, uniq } from 'lodash/fp';
7
+ import { diff as diff$1 } from 'semver';
8
+ import { compareSchemas } from './validation/schemas/index.mjs';
9
+ import { validateProvider } from './validation/provider.mjs';
10
+ import { TransferEngineError, TransferEngineValidationError } from './errors.mjs';
11
+ import * as errors from './errors.mjs';
12
+ export { errors };
13
+ import { createDiagnosticReporter } from '../utils/diagnostic.mjs';
14
+ import 'crypto';
15
+ import { filter, map } from '../utils/stream.mjs';
16
+ import { diff } from '../utils/json.mjs';
17
+ import 'events';
18
+ import { runMiddleware } from '../utils/middleware.mjs';
19
+ import { ProviderTransferError } from '../errors/providers.mjs';
20
+
21
+ function _class_private_field_loose_base(receiver, privateKey) {
22
+ if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {
23
+ throw new TypeError("attempted to use private field on non-instance");
24
+ }
25
+ return receiver;
26
+ }
27
+ var id = 0;
28
+ function _class_private_field_loose_key(name) {
29
+ return "__private_" + id++ + "_" + name;
30
+ }
31
+ const TRANSFER_STAGES = Object.freeze([
32
+ 'entities',
33
+ 'links',
34
+ 'assets',
35
+ 'schemas',
36
+ 'configuration'
37
+ ]);
38
+ /**
39
+ * Preset filters for only/exclude options
40
+ * */ const TransferGroupPresets = {
41
+ content: {
42
+ links: true,
43
+ entities: true
44
+ },
45
+ files: {
46
+ assets: true
47
+ },
48
+ config: {
49
+ configuration: true
50
+ }
51
+ };
52
+ const DEFAULT_VERSION_STRATEGY = 'ignore';
53
+ const DEFAULT_SCHEMA_STRATEGY = 'strict';
54
+ var _metadata = /*#__PURE__*/ _class_private_field_loose_key("_metadata"), _schema = /*#__PURE__*/ _class_private_field_loose_key("_schema"), _handlers = /*#__PURE__*/ _class_private_field_loose_key("_handlers"), _currentStreamController = /*#__PURE__*/ _class_private_field_loose_key("_currentStreamController"), _aborted = /*#__PURE__*/ _class_private_field_loose_key("_aborted"), /**
55
+ * Create and return a transform stream based on the given stage and options.
56
+ *
57
+ * Allowed transformations includes 'filter' and 'map'.
58
+ */ _createStageTransformStream = /*#__PURE__*/ _class_private_field_loose_key("_createStageTransformStream"), /**
59
+ * Update the Engine's transfer progress data for a given stage.
60
+ *
61
+ * Providing aggregate options enable custom computation to get the size (bytes) or the aggregate key associated with the data
62
+ */ _updateTransferProgress = /*#__PURE__*/ _class_private_field_loose_key("_updateTransferProgress"), /**
63
+ * Create and return a PassThrough stream.
64
+ *
65
+ * Upon writing data into it, it'll update the Engine's transfer progress data and trigger stage update events.
66
+ */ _progressTracker = /*#__PURE__*/ _class_private_field_loose_key("_progressTracker"), /**
67
+ * Shorthand method used to trigger transfer update events to every listeners
68
+ */ _emitTransferUpdate = /*#__PURE__*/ _class_private_field_loose_key("_emitTransferUpdate"), /**
69
+ * Shorthand method used to trigger stage update events to every listeners
70
+ */ _emitStageUpdate = /*#__PURE__*/ _class_private_field_loose_key("_emitStageUpdate"), /**
71
+ * Run a version check between two strapi version (source and destination) using the strategy given to the engine during initialization.
72
+ *
73
+ * If there is a mismatch, throws a validation error.
74
+ */ _assertStrapiVersionIntegrity = /*#__PURE__*/ _class_private_field_loose_key("_assertStrapiVersionIntegrity"), /**
75
+ * Run a check between two set of schemas (source and destination) using the strategy given to the engine during initialization.
76
+ *
77
+ * If there are differences and/or incompatibilities between source and destination schemas, then throw a validation error.
78
+ */ _assertSchemasMatching = /*#__PURE__*/ _class_private_field_loose_key("_assertSchemasMatching"), _transferStage = /*#__PURE__*/ _class_private_field_loose_key("_transferStage"), _resolveProviderResource = /*#__PURE__*/ _class_private_field_loose_key("_resolveProviderResource"), _getSchemas = /*#__PURE__*/ _class_private_field_loose_key("_getSchemas");
79
+ class TransferEngine {
80
+ onSchemaDiff(handler) {
81
+ _class_private_field_loose_base(this, _handlers)[_handlers]?.schemaDiff?.push(handler);
82
+ }
83
+ addErrorHandler(handlerName, handler) {
84
+ if (!_class_private_field_loose_base(this, _handlers)[_handlers].errors[handlerName]) {
85
+ _class_private_field_loose_base(this, _handlers)[_handlers].errors[handlerName] = [];
86
+ }
87
+ _class_private_field_loose_base(this, _handlers)[_handlers].errors[handlerName]?.push(handler);
88
+ }
89
+ async attemptResolveError(error) {
90
+ const context = {};
91
+ if (error instanceof ProviderTransferError && error.details?.details.code) {
92
+ const errorCode = error.details?.details.code;
93
+ if (!_class_private_field_loose_base(this, _handlers)[_handlers].errors[errorCode]) {
94
+ _class_private_field_loose_base(this, _handlers)[_handlers].errors[errorCode] = [];
95
+ }
96
+ await runMiddleware(context ?? {}, _class_private_field_loose_base(this, _handlers)[_handlers].errors[errorCode] ?? []);
97
+ }
98
+ return !!context.ignore;
99
+ }
100
+ /**
101
+ * Report a fatal error and throw it
102
+ */ panic(error) {
103
+ this.reportError(error, 'fatal');
104
+ throw error;
105
+ }
106
+ /**
107
+ * Report an error diagnostic
108
+ */ reportError(error, severity) {
109
+ this.diagnostics.report({
110
+ kind: 'error',
111
+ details: {
112
+ severity,
113
+ createdAt: new Date(),
114
+ name: error.name,
115
+ message: error.message,
116
+ error
117
+ }
118
+ });
119
+ }
120
+ /**
121
+ * Report a warning diagnostic
122
+ */ reportWarning(message, origin1) {
123
+ this.diagnostics.report({
124
+ kind: 'warning',
125
+ details: {
126
+ createdAt: new Date(),
127
+ message,
128
+ origin: origin1
129
+ }
130
+ });
131
+ }
132
+ /**
133
+ * Report an info diagnostic
134
+ */ reportInfo(message, params) {
135
+ this.diagnostics.report({
136
+ kind: 'info',
137
+ details: {
138
+ createdAt: new Date(),
139
+ message,
140
+ params,
141
+ origin: 'engine'
142
+ }
143
+ });
144
+ }
145
+ shouldSkipStage(stage) {
146
+ const { exclude, only } = this.options;
147
+ // schemas must always be included
148
+ if (stage === 'schemas') {
149
+ return false;
150
+ }
151
+ // everything is included by default unless 'only' has been set
152
+ let included = isEmpty(only);
153
+ if (only && only.length > 0) {
154
+ included = only.some((transferGroup)=>{
155
+ return TransferGroupPresets[transferGroup][stage];
156
+ });
157
+ }
158
+ if (exclude && exclude.length > 0) {
159
+ if (included) {
160
+ included = !exclude.some((transferGroup)=>{
161
+ return TransferGroupPresets[transferGroup][stage];
162
+ });
163
+ }
164
+ }
165
+ return !included;
166
+ }
167
+ // Cause an ongoing transfer to abort gracefully
168
+ async abortTransfer() {
169
+ _class_private_field_loose_base(this, _aborted)[_aborted] = true;
170
+ _class_private_field_loose_base(this, _currentStreamController)[_currentStreamController]?.abort();
171
+ throw new TransferEngineError('fatal', 'Transfer aborted.');
172
+ }
173
+ async init() {
174
+ // Resolve providers' resource and store
175
+ // them in the engine's internal state
176
+ await _class_private_field_loose_base(this, _resolveProviderResource)[_resolveProviderResource]();
177
+ // Update the destination provider's source metadata
178
+ const { source: sourceMetadata } = _class_private_field_loose_base(this, _metadata)[_metadata];
179
+ if (sourceMetadata) {
180
+ this.destinationProvider.setMetadata?.('source', sourceMetadata);
181
+ }
182
+ }
183
+ /**
184
+ * Run the bootstrap method in both source and destination providers
185
+ */ async bootstrap() {
186
+ const results = await Promise.allSettled([
187
+ this.sourceProvider.bootstrap?.(this.diagnostics),
188
+ this.destinationProvider.bootstrap?.(this.diagnostics)
189
+ ]);
190
+ results.forEach((result)=>{
191
+ if (result.status === 'rejected') {
192
+ this.panic(result.reason);
193
+ }
194
+ });
195
+ }
196
+ /**
197
+ * Run the close method in both source and destination providers
198
+ */ async close() {
199
+ const results = await Promise.allSettled([
200
+ this.sourceProvider.close?.(),
201
+ this.destinationProvider.close?.()
202
+ ]);
203
+ results.forEach((result)=>{
204
+ if (result.status === 'rejected') {
205
+ this.panic(result.reason);
206
+ }
207
+ });
208
+ }
209
+ async integrityCheck() {
210
+ const sourceMetadata = await this.sourceProvider.getMetadata();
211
+ const destinationMetadata = await this.destinationProvider.getMetadata();
212
+ if (sourceMetadata && destinationMetadata) {
213
+ _class_private_field_loose_base(this, _assertStrapiVersionIntegrity)[_assertStrapiVersionIntegrity](sourceMetadata?.strapi?.version, destinationMetadata?.strapi?.version);
214
+ }
215
+ const { sourceSchemas, destinationSchemas } = await _class_private_field_loose_base(this, _getSchemas)[_getSchemas]();
216
+ try {
217
+ if (sourceSchemas && destinationSchemas) {
218
+ _class_private_field_loose_base(this, _assertSchemasMatching)[_assertSchemasMatching](sourceSchemas, destinationSchemas);
219
+ }
220
+ } catch (error) {
221
+ // if this is a schema matching error, allow handlers to resolve it
222
+ if (error instanceof TransferEngineValidationError && error.details?.details?.diffs) {
223
+ const schemaDiffs = error.details?.details?.diffs;
224
+ const context = {
225
+ ignoredDiffs: {},
226
+ diffs: schemaDiffs,
227
+ source: this.sourceProvider,
228
+ destination: this.destinationProvider
229
+ };
230
+ // if we don't have any handlers, throw the original error
231
+ if (isEmpty(_class_private_field_loose_base(this, _handlers)[_handlers].schemaDiff)) {
232
+ throw error;
233
+ }
234
+ await runMiddleware(context, _class_private_field_loose_base(this, _handlers)[_handlers].schemaDiff);
235
+ // if there are any remaining diffs that weren't ignored
236
+ const unresolvedDiffs = diff(context.diffs, context.ignoredDiffs);
237
+ if (unresolvedDiffs.length) {
238
+ this.panic(new TransferEngineValidationError('Unresolved differences in schema', {
239
+ check: 'schema.changes',
240
+ unresolvedDiffs
241
+ }));
242
+ }
243
+ return;
244
+ }
245
+ throw error;
246
+ }
247
+ }
248
+ async transfer() {
249
+ // reset data between transfers
250
+ this.progress.data = {};
251
+ try {
252
+ _class_private_field_loose_base(this, _emitTransferUpdate)[_emitTransferUpdate]('init');
253
+ await this.bootstrap();
254
+ await this.init();
255
+ await this.integrityCheck();
256
+ _class_private_field_loose_base(this, _emitTransferUpdate)[_emitTransferUpdate]('start');
257
+ await this.beforeTransfer();
258
+ // Run the transfer stages
259
+ await this.transferSchemas();
260
+ await this.transferEntities();
261
+ await this.transferAssets();
262
+ await this.transferLinks();
263
+ await this.transferConfiguration();
264
+ // Gracefully close the providers
265
+ await this.close();
266
+ _class_private_field_loose_base(this, _emitTransferUpdate)[_emitTransferUpdate]('finish');
267
+ } catch (e) {
268
+ _class_private_field_loose_base(this, _emitTransferUpdate)[_emitTransferUpdate]('error', {
269
+ error: e
270
+ });
271
+ const lastDiagnostic = last(this.diagnostics.stack.items);
272
+ // Do not report an error diagnostic if the last one reported the same error
273
+ if (e instanceof Error && (!lastDiagnostic || lastDiagnostic.kind !== 'error' || lastDiagnostic.details.error !== e)) {
274
+ this.reportError(e, e.severity || 'fatal');
275
+ }
276
+ // Rollback the destination provider if an exception is thrown during the transfer
277
+ // Note: This will be configurable in the future
278
+ await this.destinationProvider.rollback?.(e);
279
+ throw e;
280
+ }
281
+ return {
282
+ source: this.sourceProvider.results,
283
+ destination: this.destinationProvider.results,
284
+ engine: this.progress.data
285
+ };
286
+ }
287
+ async beforeTransfer() {
288
+ const runWithDiagnostic = async (provider)=>{
289
+ try {
290
+ await provider.beforeTransfer?.();
291
+ } catch (error) {
292
+ if (error instanceof Error) {
293
+ const resolved = await this.attemptResolveError(error);
294
+ if (resolved) {
295
+ return;
296
+ }
297
+ this.panic(error);
298
+ } else {
299
+ this.panic(new Error(`Unknwon error when executing "beforeTransfer" on the ${origin} provider`));
300
+ }
301
+ }
302
+ };
303
+ await runWithDiagnostic(this.sourceProvider);
304
+ await runWithDiagnostic(this.destinationProvider);
305
+ }
306
+ async transferSchemas() {
307
+ const stage = 'schemas';
308
+ if (this.shouldSkipStage(stage)) {
309
+ return;
310
+ }
311
+ const source = await this.sourceProvider.createSchemasReadStream?.();
312
+ const destination = await this.destinationProvider.createSchemasWriteStream?.();
313
+ const transform = _class_private_field_loose_base(this, _createStageTransformStream)[_createStageTransformStream](stage);
314
+ const tracker = _class_private_field_loose_base(this, _progressTracker)[_progressTracker](stage, {
315
+ key: (value)=>value.modelType
316
+ });
317
+ await _class_private_field_loose_base(this, _transferStage)[_transferStage]({
318
+ stage,
319
+ source,
320
+ destination,
321
+ transform,
322
+ tracker
323
+ });
324
+ }
325
+ async transferEntities() {
326
+ const stage = 'entities';
327
+ if (this.shouldSkipStage(stage)) {
328
+ return;
329
+ }
330
+ const source = await this.sourceProvider.createEntitiesReadStream?.();
331
+ const destination = await this.destinationProvider.createEntitiesWriteStream?.();
332
+ const transform = chain([
333
+ _class_private_field_loose_base(this, _createStageTransformStream)[_createStageTransformStream](stage),
334
+ new Transform({
335
+ objectMode: true,
336
+ transform: async (entity, _encoding, callback)=>{
337
+ const { destinationSchemas: schemas } = await _class_private_field_loose_base(this, _getSchemas)[_getSchemas]();
338
+ if (!schemas) {
339
+ return callback(null, entity);
340
+ }
341
+ // TODO: this would be safer if we only ignored things in ignoredDiffs, otherwise continue and let an error be thrown
342
+ const availableContentTypes = Object.entries(schemas).filter(([, schema])=>schema.modelType === 'contentType').map(([uid])=>uid);
343
+ // If the type of the transferred entity doesn't exist in the destination, then discard it
344
+ if (!availableContentTypes.includes(entity.type)) {
345
+ return callback(null, undefined);
346
+ }
347
+ const { type, data } = entity;
348
+ const attributes = schemas[type].attributes;
349
+ const attributesToKeep = Object.keys(attributes).concat('documentId');
350
+ const updatedEntity = set('data', pick(attributesToKeep, data), entity);
351
+ callback(null, updatedEntity);
352
+ }
353
+ })
354
+ ]);
355
+ const tracker = _class_private_field_loose_base(this, _progressTracker)[_progressTracker](stage, {
356
+ key: (value)=>value.type
357
+ });
358
+ await _class_private_field_loose_base(this, _transferStage)[_transferStage]({
359
+ stage,
360
+ source,
361
+ destination,
362
+ transform,
363
+ tracker
364
+ });
365
+ }
366
+ async transferLinks() {
367
+ const stage = 'links';
368
+ if (this.shouldSkipStage(stage)) {
369
+ return;
370
+ }
371
+ const source = await this.sourceProvider.createLinksReadStream?.();
372
+ const destination = await this.destinationProvider.createLinksWriteStream?.();
373
+ const transform = chain([
374
+ _class_private_field_loose_base(this, _createStageTransformStream)[_createStageTransformStream](stage),
375
+ new Transform({
376
+ objectMode: true,
377
+ transform: async (link, _encoding, callback)=>{
378
+ const { destinationSchemas: schemas } = await _class_private_field_loose_base(this, _getSchemas)[_getSchemas]();
379
+ if (!schemas) {
380
+ return callback(null, link);
381
+ }
382
+ // TODO: this would be safer if we only ignored things in ignoredDiffs, otherwise continue and let an error be thrown
383
+ const availableContentTypes = Object.keys(schemas);
384
+ const isValidType = (uid)=>availableContentTypes.includes(uid);
385
+ if (!isValidType(link.left.type) || !isValidType(link.right.type)) {
386
+ return callback(null, undefined); // ignore the link
387
+ }
388
+ callback(null, link);
389
+ }
390
+ })
391
+ ]);
392
+ const tracker = _class_private_field_loose_base(this, _progressTracker)[_progressTracker](stage);
393
+ await _class_private_field_loose_base(this, _transferStage)[_transferStage]({
394
+ stage,
395
+ source,
396
+ destination,
397
+ transform,
398
+ tracker
399
+ });
400
+ }
401
+ async transferAssets() {
402
+ const stage = 'assets';
403
+ if (this.shouldSkipStage(stage)) {
404
+ return;
405
+ }
406
+ const source = await this.sourceProvider.createAssetsReadStream?.();
407
+ const destination = await this.destinationProvider.createAssetsWriteStream?.();
408
+ const transform = _class_private_field_loose_base(this, _createStageTransformStream)[_createStageTransformStream](stage);
409
+ const tracker = _class_private_field_loose_base(this, _progressTracker)[_progressTracker](stage, {
410
+ size: (value)=>value.stats.size,
411
+ key: (value)=>extname(value.filename) || 'No extension'
412
+ });
413
+ await _class_private_field_loose_base(this, _transferStage)[_transferStage]({
414
+ stage,
415
+ source,
416
+ destination,
417
+ transform,
418
+ tracker
419
+ });
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 = _class_private_field_loose_base(this, _createStageTransformStream)[_createStageTransformStream](stage);
429
+ const tracker = _class_private_field_loose_base(this, _progressTracker)[_progressTracker](stage);
430
+ await _class_private_field_loose_base(this, _transferStage)[_transferStage]({
431
+ stage,
432
+ source,
433
+ destination,
434
+ transform,
435
+ tracker
436
+ });
437
+ }
438
+ constructor(sourceProvider, destinationProvider, options){
439
+ Object.defineProperty(this, _createStageTransformStream, {
440
+ value: createStageTransformStream
441
+ });
442
+ Object.defineProperty(this, _updateTransferProgress, {
443
+ value: updateTransferProgress
444
+ });
445
+ Object.defineProperty(this, _progressTracker, {
446
+ value: progressTracker
447
+ });
448
+ Object.defineProperty(this, _emitTransferUpdate, {
449
+ value: emitTransferUpdate
450
+ });
451
+ Object.defineProperty(this, _emitStageUpdate, {
452
+ value: emitStageUpdate
453
+ });
454
+ Object.defineProperty(this, _assertStrapiVersionIntegrity, {
455
+ value: assertStrapiVersionIntegrity
456
+ });
457
+ Object.defineProperty(this, _assertSchemasMatching, {
458
+ value: assertSchemasMatching
459
+ });
460
+ Object.defineProperty(this, _transferStage, {
461
+ value: transferStage
462
+ });
463
+ Object.defineProperty(this, _resolveProviderResource, {
464
+ value: resolveProviderResource
465
+ });
466
+ Object.defineProperty(this, _getSchemas, {
467
+ value: getSchemas
468
+ });
469
+ Object.defineProperty(this, _metadata, {
470
+ writable: true,
471
+ value: void 0
472
+ });
473
+ Object.defineProperty(this, _schema, {
474
+ writable: true,
475
+ value: void 0
476
+ });
477
+ Object.defineProperty(this, _handlers, {
478
+ writable: true,
479
+ value: void 0
480
+ });
481
+ Object.defineProperty(this, _currentStreamController, {
482
+ writable: true,
483
+ value: void 0
484
+ });
485
+ Object.defineProperty(this, _aborted, {
486
+ writable: true,
487
+ value: void 0
488
+ });
489
+ _class_private_field_loose_base(this, _metadata)[_metadata] = {};
490
+ _class_private_field_loose_base(this, _schema)[_schema] = {};
491
+ _class_private_field_loose_base(this, _handlers)[_handlers] = {
492
+ schemaDiff: [],
493
+ errors: {}
494
+ };
495
+ _class_private_field_loose_base(this, _aborted)[_aborted] = false;
496
+ this.diagnostics = createDiagnosticReporter();
497
+ validateProvider('source', sourceProvider);
498
+ validateProvider('destination', destinationProvider);
499
+ this.sourceProvider = sourceProvider;
500
+ this.destinationProvider = destinationProvider;
501
+ this.options = options;
502
+ this.progress = {
503
+ data: {},
504
+ stream: new PassThrough({
505
+ objectMode: true
506
+ })
507
+ };
508
+ }
509
+ }
510
+ function createStageTransformStream(key, options = {}) {
511
+ const { includeGlobal = true } = options;
512
+ const { throttle } = this.options;
513
+ const { global: globalTransforms, [key]: stageTransforms } = this.options?.transforms ?? {};
514
+ let stream = new PassThrough({
515
+ objectMode: true
516
+ });
517
+ const applyTransforms = (transforms = [])=>{
518
+ const chainTransforms = [];
519
+ for (const transform of transforms){
520
+ if ('filter' in transform) {
521
+ chainTransforms.push(filter(transform.filter));
522
+ }
523
+ if ('map' in transform) {
524
+ chainTransforms.push(map(transform.map));
525
+ }
526
+ }
527
+ if (chainTransforms.length) {
528
+ stream = stream.pipe(chain(chainTransforms));
529
+ }
530
+ };
531
+ if (includeGlobal) {
532
+ applyTransforms(globalTransforms);
533
+ }
534
+ if (isNumber(throttle) && throttle > 0) {
535
+ stream = stream.pipe(new PassThrough({
536
+ objectMode: true,
537
+ async transform (data, _encoding, callback) {
538
+ await new Promise((resolve)=>{
539
+ setTimeout(resolve, throttle);
540
+ });
541
+ callback(null, data);
542
+ }
543
+ }));
544
+ }
545
+ applyTransforms(stageTransforms);
546
+ return stream;
547
+ }
548
+ function updateTransferProgress(stage, data, aggregate) {
549
+ if (!this.progress.data[stage]) {
550
+ this.progress.data[stage] = {
551
+ count: 0,
552
+ bytes: 0,
553
+ startTime: Date.now()
554
+ };
555
+ }
556
+ const stageProgress = this.progress.data[stage];
557
+ if (!stageProgress) {
558
+ return;
559
+ }
560
+ const size = aggregate?.size?.(data) ?? JSON.stringify(data).length;
561
+ const key = aggregate?.key?.(data);
562
+ stageProgress.count += 1;
563
+ stageProgress.bytes += size;
564
+ // Handle aggregate updates if necessary
565
+ if (key) {
566
+ if (!stageProgress.aggregates) {
567
+ stageProgress.aggregates = {};
568
+ }
569
+ const { aggregates } = stageProgress;
570
+ if (!aggregates[key]) {
571
+ aggregates[key] = {
572
+ count: 0,
573
+ bytes: 0
574
+ };
575
+ }
576
+ aggregates[key].count += 1;
577
+ aggregates[key].bytes += size;
578
+ }
579
+ }
580
+ function progressTracker(stage, aggregate) {
581
+ return new PassThrough({
582
+ objectMode: true,
583
+ transform: (data, _encoding, callback)=>{
584
+ _class_private_field_loose_base(this, _updateTransferProgress)[_updateTransferProgress](stage, data, aggregate);
585
+ _class_private_field_loose_base(this, _emitStageUpdate)[_emitStageUpdate]('progress', stage);
586
+ callback(null, data);
587
+ }
588
+ });
589
+ }
590
+ function emitTransferUpdate(type, payload) {
591
+ this.progress.stream.emit(`transfer::${type}`, payload);
592
+ }
593
+ function emitStageUpdate(type, transferStage) {
594
+ this.progress.stream.emit(`stage::${type}`, {
595
+ data: this.progress.data,
596
+ stage: transferStage
597
+ });
598
+ }
599
+ function assertStrapiVersionIntegrity(sourceVersion, destinationVersion) {
600
+ const strategy = this.options.versionStrategy || DEFAULT_VERSION_STRATEGY;
601
+ const reject = ()=>{
602
+ throw new 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}`, {
603
+ check: 'strapi.version',
604
+ strategy,
605
+ versions: {
606
+ source: sourceVersion,
607
+ destination: destinationVersion
608
+ }
609
+ });
610
+ };
611
+ if (!sourceVersion || !destinationVersion || strategy === 'ignore' || destinationVersion === sourceVersion) {
612
+ return;
613
+ }
614
+ let diff;
615
+ try {
616
+ diff = diff$1(sourceVersion, destinationVersion);
617
+ } catch {
618
+ reject();
619
+ }
620
+ if (!diff) {
621
+ return;
622
+ }
623
+ const validPatch = [
624
+ 'prelease',
625
+ 'build'
626
+ ];
627
+ const validMinor = [
628
+ ...validPatch,
629
+ 'patch',
630
+ 'prepatch'
631
+ ];
632
+ const validMajor = [
633
+ ...validMinor,
634
+ 'minor',
635
+ 'preminor'
636
+ ];
637
+ if (strategy === 'patch' && validPatch.includes(diff)) {
638
+ return;
639
+ }
640
+ if (strategy === 'minor' && validMinor.includes(diff)) {
641
+ return;
642
+ }
643
+ if (strategy === 'major' && validMajor.includes(diff)) {
644
+ return;
645
+ }
646
+ reject();
647
+ }
648
+ function assertSchemasMatching(sourceSchemas, destinationSchemas) {
649
+ const strategy = this.options.schemaStrategy || DEFAULT_SCHEMA_STRATEGY;
650
+ if (strategy === 'ignore') {
651
+ return;
652
+ }
653
+ const keys = uniq(Object.keys(sourceSchemas).concat(Object.keys(destinationSchemas)));
654
+ const diffs = {};
655
+ keys.forEach((key)=>{
656
+ const sourceSchema = sourceSchemas[key];
657
+ const destinationSchema = destinationSchemas[key];
658
+ const schemaDiffs = compareSchemas(sourceSchema, destinationSchema, strategy);
659
+ if (schemaDiffs.length) {
660
+ diffs[key] = schemaDiffs;
661
+ }
662
+ });
663
+ if (!isEmpty(diffs)) {
664
+ const formattedDiffs = Object.entries(diffs).map(([uid, ctDiffs])=>{
665
+ let msg = `- ${uid}:${EOL}`;
666
+ msg += ctDiffs.sort((a, b)=>a.kind > b.kind ? -1 : 1).map((diff)=>{
667
+ const path = diff.path.join('.');
668
+ if (diff.kind === 'added') {
669
+ return `${path} exists in destination schema but not in source schema and the data will not be transferred.`;
670
+ }
671
+ if (diff.kind === 'deleted') {
672
+ return `${path} exists in source schema but not in destination schema and the data will not be transferred.`;
673
+ }
674
+ if (diff.kind === 'modified') {
675
+ if (diff.types[0] === diff.types[1]) {
676
+ return `Schema value changed at "${path}": "${diff.values[0]}" (${diff.types[0]}) => "${diff.values[1]}" (${diff.types[1]})`;
677
+ }
678
+ return `Schema has differing data types at "${path}": "${diff.values[0]}" (${diff.types[0]}) => "${diff.values[1]}" (${diff.types[1]})`;
679
+ }
680
+ throw new TransferEngineValidationError(`Invalid diff found for "${uid}"`, {
681
+ check: `schema on ${uid}`
682
+ });
683
+ }).map((line)=>` - ${line}`).join(EOL);
684
+ return msg;
685
+ }).join(EOL);
686
+ throw new TransferEngineValidationError(`Invalid schema changes detected during integrity checks (using the ${strategy} strategy). Please find a summary of the changes below:\n${formattedDiffs}`, {
687
+ check: 'schema.changes',
688
+ strategy,
689
+ diffs
690
+ });
691
+ }
692
+ }
693
+ async function transferStage(options) {
694
+ if (_class_private_field_loose_base(this, _aborted)[_aborted]) {
695
+ throw new TransferEngineError('fatal', 'Transfer aborted.');
696
+ }
697
+ const { stage, source, destination, transform, tracker } = options;
698
+ const updateEndTime = ()=>{
699
+ const stageData = this.progress.data[stage];
700
+ if (stageData) {
701
+ stageData.endTime = Date.now();
702
+ }
703
+ };
704
+ if (!source || !destination || this.shouldSkipStage(stage)) {
705
+ // Wait until source and destination are closed
706
+ const results = await Promise.allSettled([
707
+ source,
708
+ destination
709
+ ].map((stream)=>{
710
+ // if stream is undefined or already closed, resolve immediately
711
+ if (!stream || stream.destroyed) {
712
+ return Promise.resolve();
713
+ }
714
+ // Wait until the close event is produced and then destroy the stream and resolve
715
+ return new Promise((resolve, reject)=>{
716
+ stream.on('close', resolve).on('error', reject).destroy();
717
+ });
718
+ }));
719
+ results.forEach((state)=>{
720
+ if (state.status === 'rejected') {
721
+ this.reportWarning(state.reason, `transfer(${stage})`);
722
+ }
723
+ });
724
+ _class_private_field_loose_base(this, _emitStageUpdate)[_emitStageUpdate]('skip', stage);
725
+ return;
726
+ }
727
+ _class_private_field_loose_base(this, _emitStageUpdate)[_emitStageUpdate]('start', stage);
728
+ try {
729
+ const streams = [
730
+ source
731
+ ];
732
+ if (transform) {
733
+ streams.push(transform);
734
+ }
735
+ if (tracker) {
736
+ streams.push(tracker);
737
+ }
738
+ streams.push(destination);
739
+ // NOTE: to debug/confirm backpressure issues from misbehaving stream, uncomment the following lines
740
+ // source.on('pause', () => console.log(`[${stage}] Source paused due to backpressure`));
741
+ // source.on('resume', () => console.log(`[${stage}] Source resumed`));
742
+ // destination.on('drain', () =>
743
+ // console.log(`[${stage}] Destination drained, resuming data flow`)
744
+ // );
745
+ // destination.on('error', (err) => console.error(`[${stage}] Destination error:`, err));
746
+ const controller = new AbortController();
747
+ const { signal } = controller;
748
+ // Store the controller so you can cancel later
749
+ _class_private_field_loose_base(this, _currentStreamController)[_currentStreamController] = controller;
750
+ await pipeline(streams, {
751
+ signal
752
+ });
753
+ _class_private_field_loose_base(this, _emitStageUpdate)[_emitStageUpdate]('finish', stage);
754
+ } catch (e) {
755
+ updateEndTime();
756
+ _class_private_field_loose_base(this, _emitStageUpdate)[_emitStageUpdate]('error', stage);
757
+ this.reportError(e, 'error');
758
+ if (!destination.destroyed) {
759
+ destination.destroy(e);
760
+ }
761
+ } finally{
762
+ updateEndTime();
763
+ }
764
+ }
765
+ async function resolveProviderResource() {
766
+ const sourceMetadata = await this.sourceProvider.getMetadata();
767
+ const destinationMetadata = await this.destinationProvider.getMetadata();
768
+ if (sourceMetadata) {
769
+ _class_private_field_loose_base(this, _metadata)[_metadata].source = sourceMetadata;
770
+ }
771
+ if (destinationMetadata) {
772
+ _class_private_field_loose_base(this, _metadata)[_metadata].destination = destinationMetadata;
773
+ }
774
+ }
775
+ async function getSchemas() {
776
+ if (!_class_private_field_loose_base(this, _schema)[_schema].source) {
777
+ _class_private_field_loose_base(this, _schema)[_schema].source = await this.sourceProvider.getSchemas?.();
778
+ }
779
+ if (!_class_private_field_loose_base(this, _schema)[_schema].destination) {
780
+ _class_private_field_loose_base(this, _schema)[_schema].destination = await this.destinationProvider.getSchemas?.();
781
+ }
782
+ return {
783
+ sourceSchemas: _class_private_field_loose_base(this, _schema)[_schema].source,
784
+ destinationSchemas: _class_private_field_loose_base(this, _schema)[_schema].destination
785
+ };
786
+ }
787
+ const createTransferEngine = (sourceProvider, destinationProvider, options)=>{
788
+ return new TransferEngine(sourceProvider, destinationProvider, options);
789
+ };
790
+
791
+ export { DEFAULT_SCHEMA_STRATEGY, DEFAULT_VERSION_STRATEGY, TRANSFER_STAGES, TransferGroupPresets, createTransferEngine };
792
+ //# sourceMappingURL=index.mjs.map