@strapi/data-transfer 4.8.2 → 4.9.0-beta.1

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 (34) hide show
  1. package/lib/engine/index.d.ts +1 -0
  2. package/lib/engine/index.js +30 -4
  3. package/lib/strapi/providers/index.d.ts +1 -0
  4. package/lib/strapi/providers/index.js +1 -0
  5. package/lib/strapi/providers/remote-destination/index.d.ts +1 -1
  6. package/lib/strapi/providers/remote-destination/index.js +3 -3
  7. package/lib/strapi/providers/remote-source/index.d.ts +36 -0
  8. package/lib/strapi/providers/remote-source/index.js +228 -0
  9. package/lib/strapi/providers/{remote-destination/utils.d.ts → utils.d.ts} +3 -3
  10. package/lib/strapi/providers/{remote-destination/utils.js → utils.js} +1 -1
  11. package/lib/strapi/remote/constants.d.ts +3 -1
  12. package/lib/strapi/remote/constants.js +1 -1
  13. package/lib/strapi/remote/flows/index.d.ts +4 -4
  14. package/lib/strapi/remote/handlers/abstract.d.ts +62 -0
  15. package/lib/strapi/remote/handlers/abstract.js +3 -0
  16. package/lib/strapi/remote/handlers/constants.d.ts +2 -0
  17. package/lib/strapi/remote/handlers/constants.js +5 -0
  18. package/lib/strapi/remote/handlers/index.d.ts +3 -0
  19. package/lib/strapi/remote/handlers/index.js +10 -0
  20. package/lib/strapi/remote/handlers/pull.d.ts +22 -0
  21. package/lib/strapi/remote/handlers/pull.js +186 -0
  22. package/lib/strapi/remote/handlers/push.d.ts +75 -0
  23. package/lib/strapi/remote/handlers/push.js +297 -0
  24. package/lib/strapi/remote/handlers/utils.d.ts +25 -0
  25. package/lib/strapi/remote/handlers/utils.js +181 -0
  26. package/lib/strapi/remote/index.d.ts +0 -1
  27. package/lib/strapi/remote/index.js +1 -2
  28. package/package.json +4 -4
  29. package/lib/strapi/remote/controllers/index.d.ts +0 -1
  30. package/lib/strapi/remote/controllers/index.js +0 -18
  31. package/lib/strapi/remote/controllers/push.d.ts +0 -26
  32. package/lib/strapi/remote/controllers/push.js +0 -116
  33. package/lib/strapi/remote/handlers.d.ts +0 -10
  34. package/lib/strapi/remote/handlers.js +0 -285
@@ -1,285 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createTransferHandler = void 0;
7
- const crypto_1 = require("crypto");
8
- const ws_1 = require("ws");
9
- const push_1 = __importDefault(require("./controllers/push"));
10
- const providers_1 = require("../../errors/providers");
11
- const constants_1 = require("./constants");
12
- const flows_1 = require("./flows");
13
- const createTransferHandler = (options) => {
14
- const { verify, server: serverOptions } = options;
15
- // Create the websocket server
16
- const wss = new ws_1.WebSocket.Server({ ...serverOptions, noServer: true });
17
- return async (ctx) => {
18
- const verifyAuth = (scope) => verify(ctx, scope);
19
- const upgradeHeader = (ctx.request.headers.upgrade || '')
20
- .split(',')
21
- .map((s) => s.trim().toLowerCase());
22
- if (upgradeHeader.includes('websocket')) {
23
- wss.handleUpgrade(ctx.req, ctx.request.socket, Buffer.alloc(0), (ws) => {
24
- // Create a connection between the client & the server
25
- wss.emit('connection', ws, ctx.req);
26
- const state = {};
27
- let uuid;
28
- function assertValidTransfer(transferState) {
29
- const { transfer, controller } = transferState;
30
- if (!controller || !transfer) {
31
- throw new providers_1.ProviderTransferError('Invalid transfer process');
32
- }
33
- }
34
- /**
35
- * Format error & message to follow the remote transfer protocol
36
- */
37
- const callback = (e = null, data) => {
38
- return new Promise((resolve, reject) => {
39
- if (!uuid && !e) {
40
- reject(new Error('Missing uuid for this message'));
41
- return;
42
- }
43
- const payload = JSON.stringify({
44
- uuid,
45
- data: data ?? null,
46
- error: e
47
- ? {
48
- code: 'ERR',
49
- message: e?.message,
50
- }
51
- : null,
52
- });
53
- ws.send(payload, (error) => (error ? reject(error) : resolve()));
54
- });
55
- };
56
- /**
57
- * Wrap a function call to catch errors and answer the request with the correct format
58
- */
59
- const answer = async (fn) => {
60
- try {
61
- const response = await fn();
62
- callback(null, response);
63
- }
64
- catch (e) {
65
- if (e instanceof Error) {
66
- callback(e);
67
- }
68
- else if (typeof e === 'string') {
69
- callback(new providers_1.ProviderTransferError(e));
70
- }
71
- else {
72
- callback(new providers_1.ProviderTransferError('Unexpected error', {
73
- error: e,
74
- }));
75
- }
76
- }
77
- };
78
- const cleanup = () => {
79
- delete state.controller;
80
- delete state.transfer;
81
- };
82
- const teardown = async () => {
83
- if (state.controller) {
84
- await state.controller.actions.rollback();
85
- }
86
- cleanup();
87
- };
88
- const end = async (msg) => {
89
- await verifyAuth(state.transfer?.kind);
90
- if (msg.params.transferID !== state.transfer?.id) {
91
- throw new providers_1.ProviderTransferError('Bad transfer ID provided');
92
- }
93
- cleanup();
94
- return { ok: true };
95
- };
96
- const init = async (msg) => {
97
- // TODO: For push transfer, we'll probably have to trigger a
98
- // maintenance mode to prevent other transfer at the same time.
99
- if (state.transfer || state.controller) {
100
- throw new providers_1.ProviderInitializationError('Transfer already in progres');
101
- }
102
- const { transfer } = msg.params;
103
- await verifyAuth(transfer);
104
- // Push transfer
105
- if (transfer === 'push') {
106
- const { options: controllerOptions } = msg.params;
107
- state.controller = (0, push_1.default)({
108
- ...controllerOptions,
109
- autoDestroy: false,
110
- getStrapi: () => strapi,
111
- });
112
- }
113
- // Pull or any other string
114
- else {
115
- throw new providers_1.ProviderTransferError(`Transfer type not implemented: "${transfer}"`, {
116
- transfer,
117
- validTransfers: constants_1.TRANSFER_METHODS,
118
- });
119
- }
120
- state.transfer = {
121
- id: (0, crypto_1.randomUUID)(),
122
- kind: transfer,
123
- startedAt: Date.now(),
124
- flow: (0, flows_1.createFlow)(flows_1.DEFAULT_TRANSFER_FLOW),
125
- };
126
- return { transferID: state.transfer.id };
127
- };
128
- const status = () => {
129
- if (state.transfer) {
130
- const { transfer } = state;
131
- const elapsed = Date.now() - transfer.startedAt;
132
- return {
133
- active: true,
134
- kind: transfer.kind,
135
- startedAt: transfer.startedAt,
136
- elapsed,
137
- };
138
- }
139
- return { active: false, kind: null, elapsed: null, startedAt: null };
140
- };
141
- /**
142
- * On command message (init, end, status, ...)
143
- */
144
- const onCommand = async (msg) => {
145
- const { command } = msg;
146
- if (command === 'init') {
147
- await answer(() => init(msg));
148
- }
149
- if (command === 'end') {
150
- await answer(() => {
151
- assertValidTransfer(state);
152
- end(msg);
153
- });
154
- }
155
- if (command === 'status') {
156
- await answer(status);
157
- }
158
- };
159
- const onTransferCommand = async (msg) => {
160
- assertValidTransfer(state);
161
- const { transferID, kind } = msg;
162
- const { controller, transfer } = state;
163
- await verifyAuth(transfer.kind);
164
- // TODO: (re)move this check
165
- // It shouldn't be possible to start a pull transfer for now, so reaching
166
- // this code should be impossible too, but this has been added by security
167
- if (transfer.kind === 'pull') {
168
- return callback(new providers_1.ProviderTransferError('Pull transfer not implemented'));
169
- }
170
- if (!controller) {
171
- return callback(new providers_1.ProviderTransferError("The transfer hasn't been initialized"));
172
- }
173
- if (!transferID) {
174
- return callback(new providers_1.ProviderTransferError('Missing transfer ID'));
175
- }
176
- // Action
177
- if (kind === 'action') {
178
- const { action } = msg;
179
- if (!(action in controller.actions)) {
180
- return callback(new providers_1.ProviderTransferError(`Invalid action provided: "${action}"`, {
181
- action,
182
- validActions: Object.keys(controller.actions),
183
- }));
184
- }
185
- const step = { kind: 'action', action };
186
- const isStepRegistered = transfer.flow.has(step);
187
- if (isStepRegistered) {
188
- if (transfer.flow.cannot(step)) {
189
- return callback(new providers_1.ProviderTransferError(`Invalid action "${action}" found for the current flow `, {
190
- action,
191
- }));
192
- }
193
- transfer.flow.set(step);
194
- }
195
- await answer(() => controller.actions[action]());
196
- }
197
- // Transfer
198
- else if (kind === 'step') {
199
- // We can only have push transfer message for the moment
200
- const message = msg;
201
- const currentStep = transfer.flow.get();
202
- const step = { kind: 'transfer', stage: message.step };
203
- // Lock the current transfer stage
204
- if (message.action === 'start') {
205
- if (currentStep?.kind === 'transfer' && currentStep.locked) {
206
- return callback(new providers_1.ProviderTransferError(`It's not possible to start a new transfer stage (${message.step}) while another one is in progress (${currentStep.stage})`));
207
- }
208
- if (transfer.flow.cannot(step)) {
209
- return callback(new providers_1.ProviderTransferError(`Invalid stage (${message.step}) provided for the current flow`, { step }));
210
- }
211
- transfer?.flow.set({ ...step, locked: true });
212
- return callback(null, { ok: true });
213
- }
214
- // Stream operation on the current transfer stage
215
- if (message.action === 'stream') {
216
- if (currentStep?.kind === 'transfer' && !currentStep.locked) {
217
- return callback(new providers_1.ProviderTransferError(`You need to initialize the transfer stage (${message.step}) before starting to stream data`));
218
- }
219
- if (transfer?.flow.cannot(step)) {
220
- return callback(new providers_1.ProviderTransferError(`Invalid stage (${message.step}) provided for the current flow`, { step }));
221
- }
222
- await answer(() => controller.transfer[message.step]?.(message.data));
223
- }
224
- // Unlock the current transfer stage
225
- if (message.action === 'end') {
226
- // Cannot unlock if not locked (aka: started)
227
- if (currentStep?.kind === 'transfer' && !currentStep.locked) {
228
- return callback(new providers_1.ProviderTransferError(`You need to initialize the transfer stage (${message.step}) before ending it`));
229
- }
230
- // Cannot unlock if invalid step provided
231
- if (transfer?.flow.cannot(step)) {
232
- return callback(new providers_1.ProviderTransferError(`Invalid stage (${message.step}) provided for the current flow`, { step }));
233
- }
234
- transfer?.flow.set({ ...step, locked: false });
235
- return callback(null, { ok: true });
236
- }
237
- }
238
- };
239
- ws.on('close', async () => {
240
- await teardown();
241
- });
242
- ws.on('error', async (e) => {
243
- await teardown();
244
- strapi.log.error(e);
245
- });
246
- ws.on('message', async (raw) => {
247
- try {
248
- const msg = JSON.parse(raw.toString());
249
- if (!msg.uuid) {
250
- await callback(new providers_1.ProviderTransferError('Missing uuid in message'));
251
- return;
252
- }
253
- uuid = msg.uuid;
254
- // Regular command message (init, end, status)
255
- if (msg.type === 'command') {
256
- await onCommand(msg);
257
- }
258
- // Transfer message (the transfer must be initialized first)
259
- else if (msg.type === 'transfer') {
260
- await onTransferCommand(msg);
261
- }
262
- // Invalid messages
263
- else {
264
- await callback(new providers_1.ProviderTransferError('Bad request'));
265
- }
266
- }
267
- catch (e) {
268
- // Only known errors should be returned to client
269
- if (e instanceof providers_1.ProviderError || e instanceof SyntaxError) {
270
- await callback(e);
271
- }
272
- else {
273
- // TODO: log error to server?
274
- // Unknown errors should not be sent to client
275
- await callback(new providers_1.ProviderTransferError('Unknown transfer error'));
276
- }
277
- }
278
- });
279
- });
280
- ctx.respond = false;
281
- }
282
- };
283
- };
284
- exports.createTransferHandler = createTransferHandler;
285
- //# sourceMappingURL=handlers.js.map