@strapi/data-transfer 5.12.0 → 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,398 @@
1
+ import { randomUUID } from 'crypto';
2
+ import { Writable, PassThrough } from 'stream';
3
+ import { ProviderTransferError } from '../../../errors/providers.mjs';
4
+ import { createLocalStrapiDestinationProvider } from '../../providers/local-destination/index.mjs';
5
+ import 'stream-chain';
6
+ import '../../queries/entity.mjs';
7
+ import 'lodash/fp';
8
+ import 'path';
9
+ import 'fs-extra';
10
+ import 'events';
11
+ import 'ws';
12
+ import { createFlow } from '../flows/index.mjs';
13
+ import { handlerControllerFactory, isDataTransferMessage } from './utils.mjs';
14
+ import DEFAULT_TRANSFER_FLOW from '../flows/default.mjs';
15
+
16
+ const VALID_TRANSFER_ACTIONS = [
17
+ 'bootstrap',
18
+ 'close',
19
+ 'rollback',
20
+ 'beforeTransfer',
21
+ 'getMetadata',
22
+ 'getSchemas'
23
+ ];
24
+ const TRANSFER_KIND = 'push';
25
+ const writeAsync = (stream, data)=>{
26
+ return new Promise((resolve, reject)=>{
27
+ stream.write(data, (error)=>{
28
+ if (error) {
29
+ reject(error);
30
+ }
31
+ resolve();
32
+ });
33
+ });
34
+ };
35
+ const createPushController = handlerControllerFactory((proto)=>({
36
+ isTransferStarted () {
37
+ return proto.isTransferStarted.call(this) && this.provider !== undefined;
38
+ },
39
+ verifyAuth () {
40
+ return proto.verifyAuth.call(this, TRANSFER_KIND);
41
+ },
42
+ onInfo (message) {
43
+ this.diagnostics?.report({
44
+ details: {
45
+ message,
46
+ origin: 'push-handler',
47
+ createdAt: new Date()
48
+ },
49
+ kind: 'info'
50
+ });
51
+ },
52
+ onWarning (message) {
53
+ this.diagnostics?.report({
54
+ details: {
55
+ message,
56
+ createdAt: new Date(),
57
+ origin: 'push-handler'
58
+ },
59
+ kind: 'warning'
60
+ });
61
+ },
62
+ cleanup () {
63
+ proto.cleanup.call(this);
64
+ this.streams = {};
65
+ this.assets = {};
66
+ delete this.flow;
67
+ delete this.provider;
68
+ },
69
+ teardown () {
70
+ if (this.provider) {
71
+ this.provider.rollback();
72
+ }
73
+ proto.teardown.call(this);
74
+ },
75
+ assertValidTransfer () {
76
+ proto.assertValidTransfer.call(this);
77
+ if (this.provider === undefined) {
78
+ throw new Error('Invalid Transfer Process');
79
+ }
80
+ },
81
+ assertValidTransferAction (action) {
82
+ if (VALID_TRANSFER_ACTIONS.includes(action)) {
83
+ return;
84
+ }
85
+ throw new ProviderTransferError(`Invalid action provided: "${action}"`, {
86
+ action,
87
+ validActions: Object.keys(VALID_TRANSFER_ACTIONS)
88
+ });
89
+ },
90
+ assertValidStreamTransferStep (stage) {
91
+ const currentStep = this.flow?.get();
92
+ const nextStep = {
93
+ kind: 'transfer',
94
+ stage
95
+ };
96
+ if (currentStep?.kind === 'transfer' && !currentStep.locked) {
97
+ throw new ProviderTransferError(`You need to initialize the transfer stage (${nextStep}) before starting to stream data`);
98
+ }
99
+ if (this.flow?.cannot(nextStep)) {
100
+ throw new ProviderTransferError(`Invalid stage (${nextStep}) provided for the current flow`, {
101
+ step: nextStep
102
+ });
103
+ }
104
+ },
105
+ async createWritableStreamForStep (step) {
106
+ const mapper = {
107
+ entities: ()=>this.provider?.createEntitiesWriteStream(),
108
+ links: ()=>this.provider?.createLinksWriteStream(),
109
+ configuration: ()=>this.provider?.createConfigurationWriteStream(),
110
+ assets: ()=>this.provider?.createAssetsWriteStream()
111
+ };
112
+ if (!(step in mapper)) {
113
+ throw new Error('Invalid transfer step, impossible to create a stream');
114
+ }
115
+ if (!this.streams) {
116
+ throw new Error('Invalid transfer state');
117
+ }
118
+ this.streams[step] = await mapper[step]();
119
+ },
120
+ async onMessage (raw) {
121
+ const msg = JSON.parse(raw.toString());
122
+ if (!isDataTransferMessage(msg)) {
123
+ return;
124
+ }
125
+ if (!msg.uuid) {
126
+ await this.respond(undefined, new Error('Missing uuid in message'));
127
+ }
128
+ if (proto.hasUUID(msg.uuid)) {
129
+ const previousResponse = proto.response;
130
+ if (previousResponse?.uuid === msg.uuid) {
131
+ await this.respond(previousResponse?.uuid, previousResponse.e, previousResponse.data);
132
+ }
133
+ return;
134
+ }
135
+ const { uuid, type } = msg;
136
+ proto.addUUID(uuid);
137
+ // Regular command message (init, end, status)
138
+ if (type === 'command') {
139
+ const { command } = msg;
140
+ this.onInfo(`received command:${command} uuid:${uuid}`);
141
+ await this.executeAndRespond(uuid, ()=>{
142
+ this.assertValidTransferCommand(command);
143
+ // The status command don't have params
144
+ if (command === 'status') {
145
+ return this.status();
146
+ }
147
+ return this[command](msg.params);
148
+ });
149
+ } else if (type === 'transfer') {
150
+ this.onInfo(`received transfer action:${msg.action} step:${msg.kind} uuid:${uuid}`);
151
+ await this.executeAndRespond(uuid, async ()=>{
152
+ await this.verifyAuth();
153
+ this.assertValidTransfer();
154
+ return this.onTransferMessage(msg);
155
+ });
156
+ } else {
157
+ await this.respond(uuid, new Error('Bad Request'));
158
+ }
159
+ },
160
+ async onTransferMessage (msg) {
161
+ const { kind } = msg;
162
+ if (kind === 'action') {
163
+ return this.onTransferAction(msg);
164
+ }
165
+ if (kind === 'step') {
166
+ return this.onTransferStep(msg);
167
+ }
168
+ },
169
+ lockTransferStep (stage) {
170
+ const currentStep = this.flow?.get();
171
+ const nextStep = {
172
+ kind: 'transfer',
173
+ stage
174
+ };
175
+ if (currentStep?.kind === 'transfer' && currentStep.locked) {
176
+ throw new ProviderTransferError(`It's not possible to start a new transfer stage (${stage}) while another one is in progress (${currentStep.stage})`);
177
+ }
178
+ if (this.flow?.cannot(nextStep)) {
179
+ throw new ProviderTransferError(`Invalid stage (${stage}) provided for the current flow`, {
180
+ step: nextStep
181
+ });
182
+ }
183
+ this.flow?.set({
184
+ ...nextStep,
185
+ locked: true
186
+ });
187
+ },
188
+ unlockTransferStep (stage) {
189
+ const currentStep = this.flow?.get();
190
+ const nextStep = {
191
+ kind: 'transfer',
192
+ stage
193
+ };
194
+ // Cannot unlock if not locked (aka: started)
195
+ if (currentStep?.kind === 'transfer' && !currentStep.locked) {
196
+ throw new ProviderTransferError(`You need to initialize the transfer stage (${stage}) before ending it`);
197
+ }
198
+ // Cannot unlock if invalid step provided
199
+ if (this.flow?.cannot(nextStep)) {
200
+ throw new ProviderTransferError(`Invalid stage (${stage}) provided for the current flow`, {
201
+ step: nextStep
202
+ });
203
+ }
204
+ this.flow?.set({
205
+ ...nextStep,
206
+ locked: false
207
+ });
208
+ },
209
+ async onTransferStep (msg) {
210
+ const { step: stage } = msg;
211
+ if (msg.action === 'start') {
212
+ this.lockTransferStep(stage);
213
+ if (this.streams?.[stage] instanceof Writable) {
214
+ throw new Error('Stream already created, something went wrong');
215
+ }
216
+ await this.createWritableStreamForStep(stage);
217
+ this.stats[stage] = {
218
+ started: 0,
219
+ finished: 0
220
+ };
221
+ return {
222
+ ok: true
223
+ };
224
+ }
225
+ if (msg.action === 'stream') {
226
+ this.assertValidStreamTransferStep(stage);
227
+ // Stream operation on the current transfer stage
228
+ const stream = this.streams?.[stage];
229
+ if (!stream) {
230
+ throw new Error('You need to init first');
231
+ }
232
+ // Assets are nested streams
233
+ if (stage === 'assets') {
234
+ return this.streamAsset(msg.data);
235
+ }
236
+ // For all other steps
237
+ await Promise.all(msg.data.map(async (item)=>{
238
+ this.stats[stage].started += 1;
239
+ await writeAsync(stream, item);
240
+ this.stats[stage].finished += 1;
241
+ }));
242
+ }
243
+ if (msg.action === 'end') {
244
+ this.unlockTransferStep(stage);
245
+ const stream = this.streams?.[stage];
246
+ if (stream && !stream.closed) {
247
+ await new Promise((resolve, reject)=>{
248
+ stream.on('close', resolve).on('error', reject).end();
249
+ });
250
+ }
251
+ delete this.streams?.[stage];
252
+ return {
253
+ ok: true,
254
+ stats: this.stats[stage]
255
+ };
256
+ }
257
+ },
258
+ async onTransferAction (msg) {
259
+ const { action } = msg;
260
+ this.assertValidTransferAction(action);
261
+ const step = {
262
+ kind: 'action',
263
+ action
264
+ };
265
+ const isStepRegistered = this.flow?.has(step);
266
+ if (isStepRegistered) {
267
+ if (this.flow?.cannot(step)) {
268
+ throw new ProviderTransferError(`Invalid action "${action}" found for the current flow `, {
269
+ action
270
+ });
271
+ }
272
+ this.flow?.set(step);
273
+ }
274
+ if (action === 'bootstrap') {
275
+ return this.provider?.[action](this.diagnostics);
276
+ }
277
+ return this.provider?.[action]();
278
+ },
279
+ async streamAsset (payload) {
280
+ const assetsStream = this.streams?.assets;
281
+ // TODO: close the stream upon receiving an 'end' event instead
282
+ if (payload === null) {
283
+ this.streams?.assets?.end();
284
+ return;
285
+ }
286
+ for (const item of payload){
287
+ const { action, assetID } = item;
288
+ if (!assetsStream) {
289
+ throw new Error('Stream not defined');
290
+ }
291
+ if (action === 'start') {
292
+ this.stats.assets.started += 1;
293
+ this.assets[assetID] = {
294
+ ...item.data,
295
+ stream: new PassThrough()
296
+ };
297
+ writeAsync(assetsStream, this.assets[assetID]);
298
+ }
299
+ if (action === 'stream') {
300
+ // The buffer has gone through JSON operations and is now of shape { type: "Buffer"; data: UInt8Array }
301
+ // We need to transform it back into a Buffer instance
302
+ const rawBuffer = item.data;
303
+ const chunk = Buffer.from(rawBuffer.data);
304
+ await writeAsync(this.assets[assetID].stream, chunk);
305
+ }
306
+ if (action === 'end') {
307
+ await new Promise((resolve, reject)=>{
308
+ const { stream: assetStream } = this.assets[assetID];
309
+ assetStream.on('close', ()=>{
310
+ this.stats.assets.finished += 1;
311
+ delete this.assets[assetID];
312
+ resolve();
313
+ }).on('error', reject).end();
314
+ });
315
+ }
316
+ }
317
+ },
318
+ onClose () {
319
+ this.teardown();
320
+ },
321
+ onError (err) {
322
+ this.teardown();
323
+ strapi.log.error(err);
324
+ },
325
+ // Commands
326
+ async init (params) {
327
+ if (this.transferID || this.provider) {
328
+ throw new Error('Transfer already in progress');
329
+ }
330
+ await this.verifyAuth();
331
+ this.transferID = randomUUID();
332
+ this.startedAt = Date.now();
333
+ this.assets = {};
334
+ this.streams = {};
335
+ this.stats = {
336
+ assets: {
337
+ started: 0,
338
+ finished: 0
339
+ },
340
+ configuration: {
341
+ started: 0,
342
+ finished: 0
343
+ },
344
+ entities: {
345
+ started: 0,
346
+ finished: 0
347
+ },
348
+ links: {
349
+ started: 0,
350
+ finished: 0
351
+ }
352
+ };
353
+ this.flow = createFlow(DEFAULT_TRANSFER_FLOW);
354
+ this.provider = createLocalStrapiDestinationProvider({
355
+ ...params.options,
356
+ autoDestroy: false,
357
+ getStrapi: ()=>strapi
358
+ });
359
+ this.provider.onWarning = (message)=>{
360
+ this.onWarning(message);
361
+ strapi.log.warn(message);
362
+ };
363
+ return {
364
+ transferID: this.transferID
365
+ };
366
+ },
367
+ async status () {
368
+ const isStarted = this.isTransferStarted();
369
+ if (isStarted) {
370
+ const startedAt = this.startedAt;
371
+ return {
372
+ active: true,
373
+ kind: TRANSFER_KIND,
374
+ startedAt,
375
+ elapsed: Date.now() - startedAt
376
+ };
377
+ }
378
+ return {
379
+ active: false,
380
+ kind: null,
381
+ elapsed: null,
382
+ startedAt: null
383
+ };
384
+ },
385
+ async end (params) {
386
+ await this.verifyAuth();
387
+ if (this.transferID !== params?.transferID) {
388
+ throw new ProviderTransferError('Bad transfer ID provided');
389
+ }
390
+ this.cleanup();
391
+ return {
392
+ ok: true
393
+ };
394
+ }
395
+ }));
396
+
397
+ export { createPushController };
398
+ //# sourceMappingURL=push.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"push.mjs","sources":["../../../../src/strapi/remote/handlers/push.ts"],"sourcesContent":["import { randomUUID } from 'crypto';\nimport { Writable, PassThrough } from 'stream';\nimport type { Core } from '@strapi/types';\n\nimport type { TransferFlow, Step } from '../flows';\nimport type { TransferStage, IAsset, Protocol } from '../../../../types';\n\nimport { ProviderTransferError } from '../../../errors/providers';\nimport { createLocalStrapiDestinationProvider } from '../../providers';\nimport { createFlow, DEFAULT_TRANSFER_FLOW } from '../flows';\nimport { Handler } from './abstract';\nimport { handlerControllerFactory, isDataTransferMessage } from './utils';\n\nconst VALID_TRANSFER_ACTIONS = [\n 'bootstrap',\n 'close',\n 'rollback',\n 'beforeTransfer',\n 'getMetadata',\n 'getSchemas',\n] as const;\n\ntype PushTransferAction = (typeof VALID_TRANSFER_ACTIONS)[number];\n\nconst TRANSFER_KIND = 'push';\n\nexport interface PushHandler extends Handler {\n /**\n * Local Strapi Destination Provider used to write data to the current Strapi instance\n */\n provider?: ReturnType<typeof createLocalStrapiDestinationProvider>;\n\n /**\n * Holds all the stages' stream for the current transfer handler (one registry per connection)\n */\n streams?: { [stage in TransferStage]?: Writable };\n\n stats: {\n [stage in Exclude<TransferStage, 'schemas'>]: Protocol.Client.Stats;\n };\n\n /**\n * Holds all the transferred assets for the current transfer handler (one registry per connection)\n */\n assets: { [filepath: string]: IAsset & { stream: PassThrough } };\n\n /**\n * Ochestrate and manage the transfer messages' ordering\n */\n flow?: TransferFlow;\n\n /**\n * Checks that the given action is a valid push transfer action\n */\n assertValidTransferAction(action: string): asserts action is PushTransferAction;\n\n /**\n * Create a new writable stream for the given step in the handler's stream registry\n */\n createWritableStreamForStep(step: TransferStage): Promise<void>;\n\n /**\n * Simple override of the auth verification\n */\n verifyAuth(): Promise<void>;\n\n /**\n * Callback when receiving a regular transfer message\n */\n onTransferMessage(msg: Protocol.Client.TransferMessage): Promise<unknown> | unknown;\n\n /**\n * Callback when receiving a transfer action message\n */\n onTransferAction(msg: Protocol.Client.Action): Promise<unknown> | unknown;\n\n /**\n * Callback when receiving a transfer step message\n */\n onTransferStep(msg: Protocol.Client.TransferPushMessage): Promise<unknown> | unknown;\n\n /**\n * Start streaming an asset\n */\n streamAsset(\n this: PushHandler,\n payload: Protocol.Client.GetTransferPushStreamData<'assets'>\n ): Promise<void>;\n\n // Transfer Flow\n\n /**\n * Try to move to a specific transfer stage & lock the step\n */\n lockTransferStep(stage: TransferStage): void;\n\n /**\n * Try to move to unlock the current step\n */\n unlockTransferStep(stage: TransferStage): void;\n\n /**\n * Checks whether it's possible to stream a chunk for the given stage\n */\n assertValidStreamTransferStep(stage: TransferStage): void;\n}\n\nconst writeAsync = <T>(stream: Writable, data: T) => {\n return new Promise<void>((resolve, reject) => {\n stream.write(data, (error) => {\n if (error) {\n reject(error);\n }\n\n resolve();\n });\n });\n};\n\nexport const createPushController = handlerControllerFactory<Partial<PushHandler>>((proto) => ({\n isTransferStarted(this: PushHandler) {\n return proto.isTransferStarted.call(this) && this.provider !== undefined;\n },\n\n verifyAuth(this: PushHandler) {\n return proto.verifyAuth.call(this, TRANSFER_KIND);\n },\n onInfo(message) {\n this.diagnostics?.report({\n details: {\n message,\n origin: 'push-handler',\n createdAt: new Date(),\n },\n kind: 'info',\n });\n },\n onWarning(message) {\n this.diagnostics?.report({\n details: {\n message,\n createdAt: new Date(),\n origin: 'push-handler',\n },\n kind: 'warning',\n });\n },\n cleanup(this: PushHandler) {\n proto.cleanup.call(this);\n\n this.streams = {};\n this.assets = {};\n\n delete this.flow;\n delete this.provider;\n },\n\n teardown(this: PushHandler) {\n if (this.provider) {\n this.provider.rollback();\n }\n\n proto.teardown.call(this);\n },\n\n assertValidTransfer(this: PushHandler) {\n proto.assertValidTransfer.call(this);\n\n if (this.provider === undefined) {\n throw new Error('Invalid Transfer Process');\n }\n },\n\n assertValidTransferAction(this: PushHandler, action: PushTransferAction) {\n if (VALID_TRANSFER_ACTIONS.includes(action)) {\n return;\n }\n\n throw new ProviderTransferError(`Invalid action provided: \"${action}\"`, {\n action,\n validActions: Object.keys(VALID_TRANSFER_ACTIONS),\n });\n },\n\n assertValidStreamTransferStep(this: PushHandler, stage) {\n const currentStep = this.flow?.get();\n const nextStep: Step = { kind: 'transfer', stage };\n\n if (currentStep?.kind === 'transfer' && !currentStep.locked) {\n throw new ProviderTransferError(\n `You need to initialize the transfer stage (${nextStep}) before starting to stream data`\n );\n }\n\n if (this.flow?.cannot(nextStep)) {\n throw new ProviderTransferError(`Invalid stage (${nextStep}) provided for the current flow`, {\n step: nextStep,\n });\n }\n },\n\n async createWritableStreamForStep(this: PushHandler, step: Exclude<TransferStage, 'schemas'>) {\n const mapper = {\n entities: () => this.provider?.createEntitiesWriteStream(),\n links: () => this.provider?.createLinksWriteStream(),\n configuration: () => this.provider?.createConfigurationWriteStream(),\n assets: () => this.provider?.createAssetsWriteStream(),\n };\n\n if (!(step in mapper)) {\n throw new Error('Invalid transfer step, impossible to create a stream');\n }\n\n if (!this.streams) {\n throw new Error('Invalid transfer state');\n }\n\n this.streams[step] = await mapper[step]();\n },\n\n async onMessage(this: PushHandler, raw) {\n const msg = JSON.parse(raw.toString());\n\n if (!isDataTransferMessage(msg)) {\n return;\n }\n\n if (!msg.uuid) {\n await this.respond(undefined, new Error('Missing uuid in message'));\n }\n\n if (proto.hasUUID(msg.uuid)) {\n const previousResponse = proto.response;\n if (previousResponse?.uuid === msg.uuid) {\n await this.respond(previousResponse?.uuid, previousResponse.e, previousResponse.data);\n }\n return;\n }\n\n const { uuid, type } = msg;\n proto.addUUID(uuid);\n // Regular command message (init, end, status)\n if (type === 'command') {\n const { command } = msg;\n this.onInfo(`received command:${command} uuid:${uuid}`);\n await this.executeAndRespond(uuid, () => {\n this.assertValidTransferCommand(command);\n\n // The status command don't have params\n if (command === 'status') {\n return this.status();\n }\n return this[command](msg.params);\n });\n }\n\n // Transfer message (the transfer must be init first)\n else if (type === 'transfer') {\n this.onInfo(`received transfer action:${msg.action} step:${msg.kind} uuid:${uuid}`);\n await this.executeAndRespond(uuid, async () => {\n await this.verifyAuth();\n\n this.assertValidTransfer();\n\n return this.onTransferMessage(msg);\n });\n }\n\n // Invalid messages\n else {\n await this.respond(uuid, new Error('Bad Request'));\n }\n },\n\n async onTransferMessage(this: PushHandler, msg) {\n const { kind } = msg;\n\n if (kind === 'action') {\n return this.onTransferAction(msg);\n }\n\n if (kind === 'step') {\n return this.onTransferStep(msg as Protocol.Client.TransferPushMessage);\n }\n },\n\n lockTransferStep(stage: TransferStage) {\n const currentStep = this.flow?.get();\n const nextStep: Step = { kind: 'transfer', stage };\n\n if (currentStep?.kind === 'transfer' && currentStep.locked) {\n throw new ProviderTransferError(\n `It's not possible to start a new transfer stage (${stage}) while another one is in progress (${currentStep.stage})`\n );\n }\n\n if (this.flow?.cannot(nextStep)) {\n throw new ProviderTransferError(`Invalid stage (${stage}) provided for the current flow`, {\n step: nextStep,\n });\n }\n\n this.flow?.set({ ...nextStep, locked: true });\n },\n\n unlockTransferStep(stage: TransferStage) {\n const currentStep = this.flow?.get();\n const nextStep: Step = { kind: 'transfer', stage };\n\n // Cannot unlock if not locked (aka: started)\n if (currentStep?.kind === 'transfer' && !currentStep.locked) {\n throw new ProviderTransferError(\n `You need to initialize the transfer stage (${stage}) before ending it`\n );\n }\n\n // Cannot unlock if invalid step provided\n if (this.flow?.cannot(nextStep)) {\n throw new ProviderTransferError(`Invalid stage (${stage}) provided for the current flow`, {\n step: nextStep,\n });\n }\n\n this.flow?.set({ ...nextStep, locked: false });\n },\n\n async onTransferStep(this: PushHandler, msg) {\n const { step: stage } = msg;\n\n if (msg.action === 'start') {\n this.lockTransferStep(stage);\n\n if (this.streams?.[stage] instanceof Writable) {\n throw new Error('Stream already created, something went wrong');\n }\n\n await this.createWritableStreamForStep(stage);\n\n this.stats[stage] = { started: 0, finished: 0 };\n\n return { ok: true };\n }\n\n if (msg.action === 'stream') {\n this.assertValidStreamTransferStep(stage);\n\n // Stream operation on the current transfer stage\n const stream = this.streams?.[stage];\n\n if (!stream) {\n throw new Error('You need to init first');\n }\n\n // Assets are nested streams\n if (stage === 'assets') {\n return this.streamAsset(msg.data);\n }\n\n // For all other steps\n await Promise.all(\n msg.data.map(async (item) => {\n this.stats[stage].started += 1;\n await writeAsync(stream, item);\n this.stats[stage].finished += 1;\n })\n );\n }\n\n if (msg.action === 'end') {\n this.unlockTransferStep(stage);\n const stream = this.streams?.[stage];\n\n if (stream && !stream.closed) {\n await new Promise((resolve, reject) => {\n stream.on('close', resolve).on('error', reject).end();\n });\n }\n\n delete this.streams?.[stage];\n\n return { ok: true, stats: this.stats[stage] };\n }\n },\n\n async onTransferAction(this: PushHandler, msg) {\n const { action } = msg;\n\n this.assertValidTransferAction(action);\n\n const step: Step = { kind: 'action', action };\n const isStepRegistered = this.flow?.has(step);\n\n if (isStepRegistered) {\n if (this.flow?.cannot(step)) {\n throw new ProviderTransferError(`Invalid action \"${action}\" found for the current flow `, {\n action,\n });\n }\n\n this.flow?.set(step);\n }\n if (action === 'bootstrap') {\n return this.provider?.[action](this.diagnostics);\n }\n return this.provider?.[action]();\n },\n\n async streamAsset(this: PushHandler, payload) {\n const assetsStream = this.streams?.assets;\n\n // TODO: close the stream upon receiving an 'end' event instead\n if (payload === null) {\n this.streams?.assets?.end();\n return;\n }\n\n for (const item of payload) {\n const { action, assetID } = item;\n\n if (!assetsStream) {\n throw new Error('Stream not defined');\n }\n\n if (action === 'start') {\n this.stats.assets.started += 1;\n this.assets[assetID] = { ...item.data, stream: new PassThrough() };\n writeAsync(assetsStream, this.assets[assetID]);\n }\n\n if (action === 'stream') {\n // The buffer has gone through JSON operations and is now of shape { type: \"Buffer\"; data: UInt8Array }\n // We need to transform it back into a Buffer instance\n const rawBuffer = item.data as unknown as { type: 'Buffer'; data: Uint8Array };\n const chunk = Buffer.from(rawBuffer.data);\n await writeAsync(this.assets[assetID].stream, chunk);\n }\n\n if (action === 'end') {\n await new Promise<void>((resolve, reject) => {\n const { stream: assetStream } = this.assets[assetID];\n assetStream\n .on('close', () => {\n this.stats.assets.finished += 1;\n delete this.assets[assetID];\n resolve();\n })\n .on('error', reject)\n .end();\n });\n }\n }\n },\n\n onClose(this: Handler) {\n this.teardown();\n },\n\n onError(this: Handler, err) {\n this.teardown();\n strapi.log.error(err);\n },\n\n // Commands\n\n async init(\n this: PushHandler,\n params: Protocol.Client.GetCommandParams<'init'>\n ): Promise<Protocol.Server.Payload<Protocol.Server.InitMessage>> {\n if (this.transferID || this.provider) {\n throw new Error('Transfer already in progress');\n }\n\n await this.verifyAuth();\n\n this.transferID = randomUUID();\n this.startedAt = Date.now();\n\n this.assets = {};\n this.streams = {};\n this.stats = {\n assets: { started: 0, finished: 0 },\n configuration: { started: 0, finished: 0 },\n entities: { started: 0, finished: 0 },\n links: { started: 0, finished: 0 },\n };\n\n this.flow = createFlow(DEFAULT_TRANSFER_FLOW);\n\n this.provider = createLocalStrapiDestinationProvider({\n ...params.options,\n autoDestroy: false,\n getStrapi: () => strapi as Core.Strapi,\n });\n\n this.provider.onWarning = (message) => {\n this.onWarning(message);\n strapi.log.warn(message);\n };\n\n return { transferID: this.transferID };\n },\n\n async status(this: PushHandler) {\n const isStarted = this.isTransferStarted();\n\n if (isStarted) {\n const startedAt = this.startedAt as number;\n\n return {\n active: true,\n kind: TRANSFER_KIND,\n startedAt,\n elapsed: Date.now() - startedAt,\n };\n }\n\n return { active: false, kind: null, elapsed: null, startedAt: null };\n },\n\n async end(\n this: PushHandler,\n params: Protocol.Client.GetCommandParams<'end'>\n ): Promise<Protocol.Server.Payload<Protocol.Server.EndMessage>> {\n await this.verifyAuth();\n\n if (this.transferID !== params?.transferID) {\n throw new ProviderTransferError('Bad transfer ID provided');\n }\n\n this.cleanup();\n\n return { ok: true };\n },\n}));\n"],"names":["VALID_TRANSFER_ACTIONS","TRANSFER_KIND","writeAsync","stream","data","Promise","resolve","reject","write","error","createPushController","handlerControllerFactory","proto","isTransferStarted","call","provider","undefined","verifyAuth","onInfo","message","diagnostics","report","details","origin","createdAt","Date","kind","onWarning","cleanup","streams","assets","flow","teardown","rollback","assertValidTransfer","Error","assertValidTransferAction","action","includes","ProviderTransferError","validActions","Object","keys","assertValidStreamTransferStep","stage","currentStep","get","nextStep","locked","cannot","step","createWritableStreamForStep","mapper","entities","createEntitiesWriteStream","links","createLinksWriteStream","configuration","createConfigurationWriteStream","createAssetsWriteStream","onMessage","raw","msg","JSON","parse","toString","isDataTransferMessage","uuid","respond","hasUUID","previousResponse","response","e","type","addUUID","command","executeAndRespond","assertValidTransferCommand","status","params","onTransferMessage","onTransferAction","onTransferStep","lockTransferStep","set","unlockTransferStep","Writable","stats","started","finished","ok","streamAsset","all","map","item","closed","on","end","isStepRegistered","has","payload","assetsStream","assetID","PassThrough","rawBuffer","chunk","Buffer","from","assetStream","onClose","onError","err","strapi","log","init","transferID","randomUUID","startedAt","now","createFlow","DEFAULT_TRANSFER_FLOW","createLocalStrapiDestinationProvider","options","autoDestroy","getStrapi","warn","isStarted","active","elapsed"],"mappings":";;;;;;;;;;;;;;;AAaA,MAAMA,sBAAyB,GAAA;AAC7B,IAAA,WAAA;AACA,IAAA,OAAA;AACA,IAAA,UAAA;AAC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