@strapi/data-transfer 4.6.2 → 4.7.0-exp.117579f4c13806c2cd518e7d7d2f9d0c8a20107d
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.
- package/lib/engine/index.js +13 -3
- package/lib/file/providers/destination/index.js +7 -0
- package/lib/strapi/index.d.ts +0 -1
- package/lib/strapi/index.js +1 -6
- package/lib/strapi/providers/local-destination/index.d.ts +1 -1
- package/lib/strapi/providers/local-destination/index.js +9 -4
- package/lib/strapi/providers/remote-destination/index.d.ts +4 -7
- package/lib/strapi/providers/remote-destination/index.js +140 -57
- package/lib/strapi/providers/remote-destination/utils.js +2 -2
- package/lib/strapi/remote/constants.d.ts +2 -2
- package/lib/strapi/remote/constants.js +1 -1
- package/lib/strapi/remote/controllers/push.d.ts +1 -0
- package/lib/strapi/remote/controllers/push.js +53 -32
- package/lib/strapi/remote/flows/default.d.ts +3 -0
- package/lib/strapi/remote/flows/default.js +41 -0
- package/lib/strapi/remote/flows/index.d.ts +18 -0
- package/lib/strapi/remote/flows/index.js +59 -0
- package/lib/strapi/remote/handlers.d.ts +8 -1
- package/lib/strapi/remote/handlers.js +263 -171
- package/lib/strapi/remote/index.d.ts +1 -1
- package/lib/strapi/remote/index.js +2 -2
- package/lib/utils/transaction.js +21 -3
- package/package.json +4 -4
- package/lib/strapi/register.d.ts +0 -7
- package/lib/strapi/register.js +0 -13
- package/lib/strapi/remote/routes.d.ts +0 -21
- package/lib/strapi/remote/routes.js +0 -22
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = [
|
|
4
|
+
{
|
|
5
|
+
kind: 'action',
|
|
6
|
+
action: 'bootstrap',
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
kind: 'action',
|
|
10
|
+
action: 'init',
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
kind: 'action',
|
|
14
|
+
action: 'beforeTransfer',
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
kind: 'transfer',
|
|
18
|
+
stage: 'schemas',
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
kind: 'transfer',
|
|
22
|
+
stage: 'entities',
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
kind: 'transfer',
|
|
26
|
+
stage: 'assets',
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
kind: 'transfer',
|
|
30
|
+
stage: 'links',
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
kind: 'transfer',
|
|
34
|
+
stage: 'configuration',
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
kind: 'action',
|
|
38
|
+
action: 'close',
|
|
39
|
+
},
|
|
40
|
+
];
|
|
41
|
+
//# sourceMappingURL=default.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { TransferStage } from '../../../../types';
|
|
2
|
+
export declare type Step = {
|
|
3
|
+
kind: 'action';
|
|
4
|
+
action: string;
|
|
5
|
+
} | {
|
|
6
|
+
kind: 'transfer';
|
|
7
|
+
stage: TransferStage;
|
|
8
|
+
locked?: boolean;
|
|
9
|
+
};
|
|
10
|
+
export { default as DEFAULT_TRANSFER_FLOW } from './default';
|
|
11
|
+
export declare const createFlow: (flow: readonly Step[]) => {
|
|
12
|
+
has(step: Step): boolean;
|
|
13
|
+
can(step: Step): boolean;
|
|
14
|
+
cannot(step: Step): boolean;
|
|
15
|
+
set(step: Step): any;
|
|
16
|
+
get(): Step | null;
|
|
17
|
+
};
|
|
18
|
+
export declare type TransferFlow = ReturnType<typeof createFlow>;
|
|
@@ -0,0 +1,59 @@
|
|
|
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.createFlow = exports.DEFAULT_TRANSFER_FLOW = void 0;
|
|
7
|
+
var default_1 = require("./default");
|
|
8
|
+
Object.defineProperty(exports, "DEFAULT_TRANSFER_FLOW", { enumerable: true, get: function () { return __importDefault(default_1).default; } });
|
|
9
|
+
const createFlow = (flow) => {
|
|
10
|
+
const state = { step: null };
|
|
11
|
+
/**
|
|
12
|
+
* Equality check between two steps
|
|
13
|
+
*/
|
|
14
|
+
const stepEqual = (stepA, stepB) => {
|
|
15
|
+
if (stepA.kind === 'action' && stepB.kind === 'action') {
|
|
16
|
+
return stepA.action === stepB.action;
|
|
17
|
+
}
|
|
18
|
+
if (stepA.kind === 'transfer' && stepB.kind === 'transfer') {
|
|
19
|
+
return stepA.stage === stepB.stage;
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Find the index for a given step
|
|
25
|
+
*/
|
|
26
|
+
const findStepIndex = (step) => flow.findIndex((flowStep) => stepEqual(step, flowStep));
|
|
27
|
+
return {
|
|
28
|
+
has(step) {
|
|
29
|
+
return findStepIndex(step) !== -1;
|
|
30
|
+
},
|
|
31
|
+
can(step) {
|
|
32
|
+
if (state.step === null) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
const indexesDifference = findStepIndex(step) - findStepIndex(state.step);
|
|
36
|
+
// It's possible to send multiple time the same transfer step in a row
|
|
37
|
+
if (indexesDifference === 0 && step.kind === 'transfer') {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
return indexesDifference > 0;
|
|
41
|
+
},
|
|
42
|
+
cannot(step) {
|
|
43
|
+
return !this.can(step);
|
|
44
|
+
},
|
|
45
|
+
set(step) {
|
|
46
|
+
const canSwitch = this.can(step);
|
|
47
|
+
if (!canSwitch) {
|
|
48
|
+
throw new Error('Impossible to proceed to the given step');
|
|
49
|
+
}
|
|
50
|
+
state.step = step;
|
|
51
|
+
return this;
|
|
52
|
+
},
|
|
53
|
+
get() {
|
|
54
|
+
return state.step;
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
exports.createFlow = createFlow;
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1,3 +1,10 @@
|
|
|
1
1
|
import type { Context } from 'koa';
|
|
2
2
|
import type { ServerOptions } from 'ws';
|
|
3
|
-
|
|
3
|
+
import { TRANSFER_METHODS } from './constants';
|
|
4
|
+
declare type TransferMethod = (typeof TRANSFER_METHODS)[number];
|
|
5
|
+
interface IHandlerOptions {
|
|
6
|
+
verify: (ctx: Context, scope?: TransferMethod) => Promise<void>;
|
|
7
|
+
server?: ServerOptions;
|
|
8
|
+
}
|
|
9
|
+
export declare const createTransferHandler: (options: IHandlerOptions) => (ctx: Context) => Promise<void>;
|
|
10
|
+
export {};
|
|
@@ -9,185 +9,277 @@ const ws_1 = require("ws");
|
|
|
9
9
|
const push_1 = __importDefault(require("./controllers/push"));
|
|
10
10
|
const providers_1 = require("../../errors/providers");
|
|
11
11
|
const constants_1 = require("./constants");
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
.map((s) => s.trim().toLowerCase());
|
|
12
|
+
const flows_1 = require("./flows");
|
|
13
|
+
const createTransferHandler = (options) => {
|
|
14
|
+
const { verify, server: serverOptions } = options;
|
|
16
15
|
// Create the websocket server
|
|
17
|
-
const wss = new ws_1.WebSocket.Server({ ...
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
uuid,
|
|
35
|
-
data: data ?? null,
|
|
36
|
-
error: e
|
|
37
|
-
? {
|
|
38
|
-
code: 'ERR',
|
|
39
|
-
message: e?.message,
|
|
40
|
-
}
|
|
41
|
-
: null,
|
|
42
|
-
});
|
|
43
|
-
ws.send(payload, (error) => (error ? reject(error) : resolve()));
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Wrap a function call to catch errors and answer the request with the correct format
|
|
48
|
-
*/
|
|
49
|
-
const answer = async (fn) => {
|
|
50
|
-
try {
|
|
51
|
-
const response = await fn();
|
|
52
|
-
callback(null, response);
|
|
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
|
+
}
|
|
53
33
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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) {
|
|
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();
|
|
57
85
|
}
|
|
58
|
-
|
|
59
|
-
|
|
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');
|
|
60
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
|
|
61
114
|
else {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
115
|
+
throw new providers_1.ProviderTransferError(`Transfer type not implemented: "${transfer}"`, {
|
|
116
|
+
transfer,
|
|
117
|
+
validTransfers: constants_1.TRANSFER_METHODS,
|
|
118
|
+
});
|
|
65
119
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
autoDestroy: false,
|
|
85
|
-
getStrapi: () => strapi,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
// Pull or any other string
|
|
89
|
-
else {
|
|
90
|
-
throw new providers_1.ProviderTransferError(`Transfer type not implemented: "${transfer}"`, {
|
|
91
|
-
transfer,
|
|
92
|
-
validTransfers: constants_1.TRANSFER_METHODS,
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
state.transfer = { id: (0, crypto_1.randomUUID)(), kind: transfer };
|
|
96
|
-
return { transferID: state.transfer.id };
|
|
97
|
-
};
|
|
98
|
-
/**
|
|
99
|
-
* On command message (init, end, status, ...)
|
|
100
|
-
*/
|
|
101
|
-
const onCommand = async (msg) => {
|
|
102
|
-
const { command } = msg;
|
|
103
|
-
if (command === 'init') {
|
|
104
|
-
await answer(() => init(msg));
|
|
105
|
-
}
|
|
106
|
-
if (command === 'end') {
|
|
107
|
-
await answer(teardown);
|
|
108
|
-
}
|
|
109
|
-
if (command === 'status') {
|
|
110
|
-
await callback(new providers_1.ProviderTransferError('Command not implemented: "status"', {
|
|
111
|
-
command,
|
|
112
|
-
validCommands: ['init', 'end', 'status'],
|
|
113
|
-
}));
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
const onTransferCommand = async (msg) => {
|
|
117
|
-
const { transferID, kind } = msg;
|
|
118
|
-
const { controller } = state;
|
|
119
|
-
// TODO: (re)move this check
|
|
120
|
-
// It shouldn't be possible to start a pull transfer for now, so reaching
|
|
121
|
-
// this code should be impossible too, but this has been added by security
|
|
122
|
-
if (state.transfer?.kind === 'pull') {
|
|
123
|
-
return callback(new providers_1.ProviderTransferError('Pull transfer not implemented'));
|
|
124
|
-
}
|
|
125
|
-
if (!controller) {
|
|
126
|
-
return callback(new providers_1.ProviderTransferError("The transfer hasn't been initialized"));
|
|
127
|
-
}
|
|
128
|
-
if (!transferID) {
|
|
129
|
-
return callback(new providers_1.ProviderTransferError('Missing transfer ID'));
|
|
130
|
-
}
|
|
131
|
-
// Action
|
|
132
|
-
if (kind === 'action') {
|
|
133
|
-
const { action } = msg;
|
|
134
|
-
if (!(action in controller.actions)) {
|
|
135
|
-
return callback(new providers_1.ProviderTransferError(`Invalid action provided: "${action}"`, {
|
|
136
|
-
action,
|
|
137
|
-
validActions: Object.keys(controller.actions),
|
|
138
|
-
}));
|
|
139
|
-
}
|
|
140
|
-
await answer(() => controller.actions[action]());
|
|
141
|
-
}
|
|
142
|
-
// Transfer
|
|
143
|
-
else if (kind === 'step') {
|
|
144
|
-
// We can only have push transfer message for the moment
|
|
145
|
-
const message = msg;
|
|
146
|
-
// TODO: lock transfer process
|
|
147
|
-
if (message.action === 'start') {
|
|
148
|
-
// console.log('Starting transfer for ', message.step);
|
|
149
|
-
}
|
|
150
|
-
// Stream step
|
|
151
|
-
else if (message.action === 'stream') {
|
|
152
|
-
await answer(() => controller.transfer[message.step]?.(message.data));
|
|
153
|
-
}
|
|
154
|
-
// TODO: unlock transfer process
|
|
155
|
-
else if (message.action === 'end') {
|
|
156
|
-
// console.log('Ending transfer for ', message.step);
|
|
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
|
+
};
|
|
157
138
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
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
|
+
});
|
|
187
279
|
});
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}
|
|
280
|
+
ctx.respond = false;
|
|
281
|
+
}
|
|
282
|
+
};
|
|
191
283
|
};
|
|
192
284
|
exports.createTransferHandler = createTransferHandler;
|
|
193
285
|
//# sourceMappingURL=handlers.js.map
|
|
@@ -23,8 +23,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
26
|
+
exports.handlers = exports.constants = exports.controllers = void 0;
|
|
27
27
|
exports.controllers = __importStar(require("./controllers"));
|
|
28
|
-
exports.routes = __importStar(require("./routes"));
|
|
29
28
|
exports.constants = __importStar(require("./constants"));
|
|
29
|
+
exports.handlers = __importStar(require("./handlers"));
|
|
30
30
|
//# sourceMappingURL=index.js.map
|
package/lib/utils/transaction.js
CHANGED
|
@@ -13,12 +13,26 @@ const createTransaction = (strapi) => {
|
|
|
13
13
|
resume?.();
|
|
14
14
|
});
|
|
15
15
|
e.on('close', () => {
|
|
16
|
+
e.removeAllListeners('rollback');
|
|
17
|
+
e.removeAllListeners('spawn');
|
|
16
18
|
done = true;
|
|
17
19
|
resume?.();
|
|
18
20
|
});
|
|
19
21
|
strapi.db.transaction(async ({ trx, rollback }) => {
|
|
20
|
-
e.
|
|
21
|
-
|
|
22
|
+
e.once('rollback', async () => {
|
|
23
|
+
e.removeAllListeners('close');
|
|
24
|
+
e.removeAllListeners('spawn');
|
|
25
|
+
try {
|
|
26
|
+
await rollback();
|
|
27
|
+
e.emit('rollback_completed');
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
e.emit('rollback_failed');
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
done = true;
|
|
34
|
+
resume?.();
|
|
35
|
+
}
|
|
22
36
|
});
|
|
23
37
|
while (!done) {
|
|
24
38
|
while (fns.length) {
|
|
@@ -62,7 +76,11 @@ const createTransaction = (strapi) => {
|
|
|
62
76
|
return e.emit('close');
|
|
63
77
|
},
|
|
64
78
|
rollback() {
|
|
65
|
-
return
|
|
79
|
+
return new Promise((resolve) => {
|
|
80
|
+
e.emit('rollback');
|
|
81
|
+
e.once('rollback_failed', () => resolve(false));
|
|
82
|
+
e.once('rollback_completed', () => resolve(true));
|
|
83
|
+
});
|
|
66
84
|
},
|
|
67
85
|
};
|
|
68
86
|
};
|