@strapi/data-transfer 4.9.0-alpha.0 → 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.
- package/lib/engine/index.d.ts +2 -1
- package/lib/engine/index.js +67 -7
- package/lib/errors/constants.d.ts +1 -1
- 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/index.d.ts +1 -0
- package/lib/strapi/providers/index.js +1 -0
- 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 +5 -8
- package/lib/strapi/providers/remote-destination/index.js +157 -59
- package/lib/strapi/providers/remote-source/index.d.ts +36 -0
- package/lib/strapi/providers/remote-source/index.js +228 -0
- package/lib/strapi/providers/{remote-destination/utils.d.ts → utils.d.ts} +3 -3
- package/lib/strapi/providers/{remote-destination/utils.js → utils.js} +2 -2
- package/lib/strapi/remote/constants.d.ts +4 -2
- package/lib/strapi/remote/constants.js +1 -1
- 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/abstract.d.ts +62 -0
- package/lib/strapi/remote/handlers/abstract.js +3 -0
- package/lib/strapi/remote/handlers/constants.d.ts +2 -0
- package/lib/strapi/remote/handlers/constants.js +5 -0
- package/lib/strapi/remote/handlers/index.d.ts +3 -0
- package/lib/strapi/remote/handlers/index.js +10 -0
- package/lib/strapi/remote/handlers/pull.d.ts +22 -0
- package/lib/strapi/remote/handlers/pull.js +186 -0
- package/lib/strapi/remote/handlers/push.d.ts +75 -0
- package/lib/strapi/remote/handlers/push.js +297 -0
- package/lib/strapi/remote/handlers/utils.d.ts +25 -0
- package/lib/strapi/remote/handlers/utils.js +181 -0
- package/lib/strapi/remote/index.d.ts +1 -2
- package/lib/strapi/remote/index.js +2 -3
- package/lib/utils/transaction.js +21 -3
- package/package.json +8 -7
- package/lib/strapi/register.d.ts +0 -7
- package/lib/strapi/register.js +0 -13
- package/lib/strapi/remote/controllers/index.d.ts +0 -1
- package/lib/strapi/remote/controllers/index.js +0 -18
- package/lib/strapi/remote/controllers/push.d.ts +0 -25
- package/lib/strapi/remote/controllers/push.js +0 -95
- package/lib/strapi/remote/handlers.d.ts +0 -3
- package/lib/strapi/remote/handlers.js +0 -193
- package/lib/strapi/remote/routes.d.ts +0 -21
- package/lib/strapi/remote/routes.js +0 -22
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPullController = void 0;
|
|
4
|
+
const stream_1 = require("stream");
|
|
5
|
+
const crypto_1 = require("crypto");
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
const providers_1 = require("../../providers");
|
|
8
|
+
const providers_2 = require("../../../errors/providers");
|
|
9
|
+
const TRANSFER_KIND = 'pull';
|
|
10
|
+
const VALID_TRANSFER_ACTIONS = ['bootstrap', 'close', 'getMetadata', 'getSchemas'];
|
|
11
|
+
exports.createPullController = (0, utils_1.handlerControllerFactory)((proto) => ({
|
|
12
|
+
isTransferStarted() {
|
|
13
|
+
return proto.isTransferStarted.call(this) && this.provider !== undefined;
|
|
14
|
+
},
|
|
15
|
+
verifyAuth() {
|
|
16
|
+
return proto.verifyAuth.call(this, TRANSFER_KIND);
|
|
17
|
+
},
|
|
18
|
+
cleanup() {
|
|
19
|
+
proto.cleanup.call(this);
|
|
20
|
+
this.streams = {};
|
|
21
|
+
delete this.provider;
|
|
22
|
+
},
|
|
23
|
+
assertValidTransferAction(action) {
|
|
24
|
+
// Abstract the constant to string[] to allow looser check on the given action
|
|
25
|
+
const validActions = VALID_TRANSFER_ACTIONS;
|
|
26
|
+
if (validActions.includes(action)) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
throw new providers_2.ProviderTransferError(`Invalid action provided: "${action}"`, {
|
|
30
|
+
action,
|
|
31
|
+
validActions: Object.keys(VALID_TRANSFER_ACTIONS),
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
async onMessage(raw) {
|
|
35
|
+
const msg = JSON.parse(raw.toString());
|
|
36
|
+
if (!(0, utils_1.isDataTransferMessage)(msg)) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (!msg.uuid) {
|
|
40
|
+
await this.respond(undefined, new Error('Missing uuid in message'));
|
|
41
|
+
}
|
|
42
|
+
const { uuid, type } = msg;
|
|
43
|
+
// Regular command message (init, end, status)
|
|
44
|
+
if (type === 'command') {
|
|
45
|
+
const { command } = msg;
|
|
46
|
+
await this.executeAndRespond(uuid, () => {
|
|
47
|
+
this.assertValidTransferCommand(command);
|
|
48
|
+
// The status command don't have params
|
|
49
|
+
if (command === 'status') {
|
|
50
|
+
return this.status();
|
|
51
|
+
}
|
|
52
|
+
return this[command](msg.params);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
// Transfer message (the transfer must be init first)
|
|
56
|
+
else if (type === 'transfer') {
|
|
57
|
+
await this.executeAndRespond(uuid, async () => {
|
|
58
|
+
await this.verifyAuth();
|
|
59
|
+
this.assertValidTransfer();
|
|
60
|
+
return this.onTransferMessage(msg);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
// Invalid messages
|
|
64
|
+
else {
|
|
65
|
+
await this.respond(uuid, new Error('Bad Request'));
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
async onTransferMessage(msg) {
|
|
69
|
+
const { kind } = msg;
|
|
70
|
+
if (kind === 'action') {
|
|
71
|
+
return this.onTransferAction(msg);
|
|
72
|
+
}
|
|
73
|
+
if (kind === 'step') {
|
|
74
|
+
return this.onTransferStep(msg);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
async onTransferAction(msg) {
|
|
78
|
+
const { action } = msg;
|
|
79
|
+
this.assertValidTransferAction(action);
|
|
80
|
+
return this.provider?.[action]();
|
|
81
|
+
},
|
|
82
|
+
// TODO: Optimize performances (batching, client packets reconstruction, etc...)
|
|
83
|
+
async flush(stage, id) {
|
|
84
|
+
const stream = this.streams?.[stage];
|
|
85
|
+
if (!stream) {
|
|
86
|
+
throw new providers_2.ProviderTransferError(`No available stream found for ${stage}`);
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
for await (const chunk of stream) {
|
|
90
|
+
await this.confirm({ type: 'transfer', data: chunk, ended: false, error: null, id });
|
|
91
|
+
}
|
|
92
|
+
await this.confirm({ type: 'transfer', data: null, ended: true, error: null, id });
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
await this.confirm({ type: 'transfer', data: null, ended: true, error: e, id });
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
async onTransferStep(msg) {
|
|
99
|
+
const { step, action } = msg;
|
|
100
|
+
if (action === 'start') {
|
|
101
|
+
if (this.streams?.[step] instanceof stream_1.Readable) {
|
|
102
|
+
throw new Error('Stream already created, something went wrong');
|
|
103
|
+
}
|
|
104
|
+
const flushUUID = (0, crypto_1.randomUUID)();
|
|
105
|
+
await this.createReadableStreamForStep(step);
|
|
106
|
+
this.flush(step, flushUUID);
|
|
107
|
+
return { ok: true, id: flushUUID };
|
|
108
|
+
}
|
|
109
|
+
if (action === 'end') {
|
|
110
|
+
const stream = this.streams?.[step];
|
|
111
|
+
if (stream?.readableEnded === false) {
|
|
112
|
+
await new Promise((resolve) => {
|
|
113
|
+
stream?.on('close', resolve).destroy();
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
delete this.streams?.[step];
|
|
117
|
+
return { ok: true };
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
async createReadableStreamForStep(step) {
|
|
121
|
+
const mapper = {
|
|
122
|
+
entities: () => this.provider?.createEntitiesReadStream(),
|
|
123
|
+
links: () => this.provider?.createLinksReadStream(),
|
|
124
|
+
configuration: () => this.provider?.createConfigurationReadStream(),
|
|
125
|
+
assets: () => {
|
|
126
|
+
const assets = this.provider?.createAssetsReadStream();
|
|
127
|
+
if (!assets) {
|
|
128
|
+
throw new Error('bad');
|
|
129
|
+
}
|
|
130
|
+
async function* generator(stream) {
|
|
131
|
+
for await (const chunk of stream) {
|
|
132
|
+
const { stream: assetStream, ...rest } = chunk;
|
|
133
|
+
for await (const assetChunk of assetStream) {
|
|
134
|
+
yield { ...rest, chunk: assetChunk };
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return stream_1.Readable.from(generator(assets));
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
if (!(step in mapper)) {
|
|
142
|
+
throw new Error('Invalid transfer step, impossible to create a stream');
|
|
143
|
+
}
|
|
144
|
+
if (!this.streams) {
|
|
145
|
+
throw new Error('Invalid transfer state');
|
|
146
|
+
}
|
|
147
|
+
this.streams[step] = await mapper[step]();
|
|
148
|
+
},
|
|
149
|
+
// Commands
|
|
150
|
+
async init() {
|
|
151
|
+
if (this.transferID || this.provider) {
|
|
152
|
+
throw new Error('Transfer already in progress');
|
|
153
|
+
}
|
|
154
|
+
await this.verifyAuth();
|
|
155
|
+
this.transferID = (0, crypto_1.randomUUID)();
|
|
156
|
+
this.startedAt = Date.now();
|
|
157
|
+
this.streams = {};
|
|
158
|
+
this.provider = (0, providers_1.createLocalStrapiSourceProvider)({
|
|
159
|
+
autoDestroy: false,
|
|
160
|
+
getStrapi: () => strapi,
|
|
161
|
+
});
|
|
162
|
+
return { transferID: this.transferID };
|
|
163
|
+
},
|
|
164
|
+
async end(params) {
|
|
165
|
+
await this.verifyAuth();
|
|
166
|
+
if (this.transferID !== params.transferID) {
|
|
167
|
+
throw new providers_2.ProviderTransferError('Bad transfer ID provided');
|
|
168
|
+
}
|
|
169
|
+
this.cleanup();
|
|
170
|
+
return { ok: true };
|
|
171
|
+
},
|
|
172
|
+
async status() {
|
|
173
|
+
const isStarted = this.isTransferStarted();
|
|
174
|
+
if (!isStarted) {
|
|
175
|
+
const startedAt = this.startedAt;
|
|
176
|
+
return {
|
|
177
|
+
active: true,
|
|
178
|
+
kind: TRANSFER_KIND,
|
|
179
|
+
startedAt,
|
|
180
|
+
elapsed: Date.now() - startedAt,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
return { active: false, kind: null, elapsed: null, startedAt: null };
|
|
184
|
+
},
|
|
185
|
+
}));
|
|
186
|
+
//# sourceMappingURL=pull.js.map
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="koa" />
|
|
3
|
+
import { Writable, PassThrough } from 'stream';
|
|
4
|
+
import type { TransferFlow } from '../flows';
|
|
5
|
+
import type { TransferStage, IAsset, Protocol } from '../../../../types';
|
|
6
|
+
import { createLocalStrapiDestinationProvider } from '../../providers';
|
|
7
|
+
import { Handler } from './abstract';
|
|
8
|
+
declare const VALID_TRANSFER_ACTIONS: readonly ["bootstrap", "close", "rollback", "beforeTransfer", "getMetadata", "getSchemas"];
|
|
9
|
+
declare type PushTransferAction = (typeof VALID_TRANSFER_ACTIONS)[number];
|
|
10
|
+
export interface PushHandler extends Handler {
|
|
11
|
+
/**
|
|
12
|
+
* Local Strapi Destination Provider used to write data to the current Strapi instance
|
|
13
|
+
*/
|
|
14
|
+
provider?: ReturnType<typeof createLocalStrapiDestinationProvider>;
|
|
15
|
+
/**
|
|
16
|
+
* Holds all the stages' stream for the current transfer handler (one registry per connection)
|
|
17
|
+
*/
|
|
18
|
+
streams?: {
|
|
19
|
+
[stage in TransferStage]?: Writable;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Holds all the transferred assets for the current transfer handler (one registry per connection)
|
|
23
|
+
*/
|
|
24
|
+
assets: {
|
|
25
|
+
[filepath: string]: IAsset & {
|
|
26
|
+
stream: PassThrough;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Ochestrate and manage the transfer messages' ordering
|
|
31
|
+
*/
|
|
32
|
+
flow?: TransferFlow;
|
|
33
|
+
/**
|
|
34
|
+
* Checks that the given action is a valid push transfer action
|
|
35
|
+
*/
|
|
36
|
+
assertValidTransferAction(action: string): asserts action is PushTransferAction;
|
|
37
|
+
/**
|
|
38
|
+
* Create a new writable stream for the given step in the handler's stream registry
|
|
39
|
+
*/
|
|
40
|
+
createWritableStreamForStep(step: TransferStage): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Simple override of the auth verification
|
|
43
|
+
*/
|
|
44
|
+
verifyAuth(): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Callback when receiving a regular transfer message
|
|
47
|
+
*/
|
|
48
|
+
onTransferMessage(msg: Protocol.client.TransferMessage): Promise<unknown> | unknown;
|
|
49
|
+
/**
|
|
50
|
+
* Callback when receiving a transfer action message
|
|
51
|
+
*/
|
|
52
|
+
onTransferAction(msg: Protocol.client.Action): Promise<unknown> | unknown;
|
|
53
|
+
/**
|
|
54
|
+
* Callback when receiving a transfer step message
|
|
55
|
+
*/
|
|
56
|
+
onTransferStep(msg: Protocol.client.TransferPushMessage): Promise<unknown> | unknown;
|
|
57
|
+
/**
|
|
58
|
+
* Start streaming an asset
|
|
59
|
+
*/
|
|
60
|
+
streamAsset(this: PushHandler, payload: Protocol.client.GetTransferPushStreamData<'assets'>): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Try to move to a specific transfer stage & lock the step
|
|
63
|
+
*/
|
|
64
|
+
lockTransferStep(stage: TransferStage): void;
|
|
65
|
+
/**
|
|
66
|
+
* Try to move to unlock the current step
|
|
67
|
+
*/
|
|
68
|
+
unlockTransferStep(stage: TransferStage): void;
|
|
69
|
+
/**
|
|
70
|
+
* Checks whether it's possible to stream a chunk for the given stage
|
|
71
|
+
*/
|
|
72
|
+
assertValidStreamTransferStep(stage: TransferStage): void;
|
|
73
|
+
}
|
|
74
|
+
export declare const createPushController: (options: import("./utils").HandlerOptions) => (ctx: import("koa").Context) => Promise<void>;
|
|
75
|
+
export {};
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPushController = void 0;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
5
|
+
const stream_1 = require("stream");
|
|
6
|
+
const providers_1 = require("../../../errors/providers");
|
|
7
|
+
const providers_2 = require("../../providers");
|
|
8
|
+
const flows_1 = require("../flows");
|
|
9
|
+
const utils_1 = require("./utils");
|
|
10
|
+
const VALID_TRANSFER_ACTIONS = [
|
|
11
|
+
'bootstrap',
|
|
12
|
+
'close',
|
|
13
|
+
'rollback',
|
|
14
|
+
'beforeTransfer',
|
|
15
|
+
'getMetadata',
|
|
16
|
+
'getSchemas',
|
|
17
|
+
];
|
|
18
|
+
const TRANSFER_KIND = 'push';
|
|
19
|
+
const writeAsync = (stream, data) => {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
stream.write(data, (error) => {
|
|
22
|
+
if (error) {
|
|
23
|
+
reject(error);
|
|
24
|
+
}
|
|
25
|
+
resolve();
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
exports.createPushController = (0, utils_1.handlerControllerFactory)((proto) => ({
|
|
30
|
+
isTransferStarted() {
|
|
31
|
+
return proto.isTransferStarted.call(this) && this.provider !== undefined;
|
|
32
|
+
},
|
|
33
|
+
verifyAuth() {
|
|
34
|
+
return proto.verifyAuth.call(this, TRANSFER_KIND);
|
|
35
|
+
},
|
|
36
|
+
cleanup() {
|
|
37
|
+
proto.cleanup.call(this);
|
|
38
|
+
this.streams = {};
|
|
39
|
+
this.assets = {};
|
|
40
|
+
delete this.flow;
|
|
41
|
+
delete this.provider;
|
|
42
|
+
},
|
|
43
|
+
teardown() {
|
|
44
|
+
if (this.provider) {
|
|
45
|
+
this.provider.rollback();
|
|
46
|
+
}
|
|
47
|
+
proto.teardown.call(this);
|
|
48
|
+
},
|
|
49
|
+
assertValidTransfer() {
|
|
50
|
+
proto.assertValidTransfer.call(this);
|
|
51
|
+
if (this.provider === undefined) {
|
|
52
|
+
throw new Error('Invalid Transfer Process');
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
assertValidTransferAction(action) {
|
|
56
|
+
if (VALID_TRANSFER_ACTIONS.includes(action)) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
throw new providers_1.ProviderTransferError(`Invalid action provided: "${action}"`, {
|
|
60
|
+
action,
|
|
61
|
+
validActions: Object.keys(VALID_TRANSFER_ACTIONS),
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
assertValidStreamTransferStep(stage) {
|
|
65
|
+
const currentStep = this.flow?.get();
|
|
66
|
+
const nextStep = { kind: 'transfer', stage };
|
|
67
|
+
if (currentStep?.kind === 'transfer' && !currentStep.locked) {
|
|
68
|
+
throw new providers_1.ProviderTransferError(`You need to initialize the transfer stage (${nextStep}) before starting to stream data`);
|
|
69
|
+
}
|
|
70
|
+
if (this.flow?.cannot(nextStep)) {
|
|
71
|
+
throw new providers_1.ProviderTransferError(`Invalid stage (${nextStep}) provided for the current flow`, {
|
|
72
|
+
step: nextStep,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
async createWritableStreamForStep(step) {
|
|
77
|
+
const mapper = {
|
|
78
|
+
entities: () => this.provider?.createEntitiesWriteStream(),
|
|
79
|
+
links: () => this.provider?.createLinksWriteStream(),
|
|
80
|
+
configuration: () => this.provider?.createConfigurationWriteStream(),
|
|
81
|
+
assets: () => this.provider?.createAssetsWriteStream(),
|
|
82
|
+
};
|
|
83
|
+
if (!(step in mapper)) {
|
|
84
|
+
throw new Error('Invalid transfer step, impossible to create a stream');
|
|
85
|
+
}
|
|
86
|
+
if (!this.streams) {
|
|
87
|
+
throw new Error('Invalid transfer state');
|
|
88
|
+
}
|
|
89
|
+
this.streams[step] = await mapper[step]();
|
|
90
|
+
},
|
|
91
|
+
async onMessage(raw) {
|
|
92
|
+
const msg = JSON.parse(raw.toString());
|
|
93
|
+
if (!(0, utils_1.isDataTransferMessage)(msg)) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (!msg.uuid) {
|
|
97
|
+
await this.respond(undefined, new Error('Missing uuid in message'));
|
|
98
|
+
}
|
|
99
|
+
const { uuid, type } = msg;
|
|
100
|
+
// Regular command message (init, end, status)
|
|
101
|
+
if (type === 'command') {
|
|
102
|
+
const { command } = msg;
|
|
103
|
+
await this.executeAndRespond(uuid, () => {
|
|
104
|
+
this.assertValidTransferCommand(command);
|
|
105
|
+
// The status command don't have params
|
|
106
|
+
if (command === 'status') {
|
|
107
|
+
return this.status();
|
|
108
|
+
}
|
|
109
|
+
return this[command](msg.params);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
// Transfer message (the transfer must be init first)
|
|
113
|
+
else if (type === 'transfer') {
|
|
114
|
+
await this.executeAndRespond(uuid, async () => {
|
|
115
|
+
await this.verifyAuth();
|
|
116
|
+
this.assertValidTransfer();
|
|
117
|
+
return this.onTransferMessage(msg);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
// Invalid messages
|
|
121
|
+
else {
|
|
122
|
+
await this.respond(uuid, new Error('Bad Request'));
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
async onTransferMessage(msg) {
|
|
126
|
+
const { kind } = msg;
|
|
127
|
+
if (kind === 'action') {
|
|
128
|
+
return this.onTransferAction(msg);
|
|
129
|
+
}
|
|
130
|
+
if (kind === 'step') {
|
|
131
|
+
return this.onTransferStep(msg);
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
lockTransferStep(stage) {
|
|
135
|
+
const currentStep = this.flow?.get();
|
|
136
|
+
const nextStep = { kind: 'transfer', stage };
|
|
137
|
+
if (currentStep?.kind === 'transfer' && currentStep.locked) {
|
|
138
|
+
throw new providers_1.ProviderTransferError(`It's not possible to start a new transfer stage (${stage}) while another one is in progress (${currentStep.stage})`);
|
|
139
|
+
}
|
|
140
|
+
if (this.flow?.cannot(nextStep)) {
|
|
141
|
+
throw new providers_1.ProviderTransferError(`Invalid stage (${stage}) provided for the current flow`, {
|
|
142
|
+
step: nextStep,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
this.flow?.set({ ...nextStep, locked: true });
|
|
146
|
+
},
|
|
147
|
+
unlockTransferStep(stage) {
|
|
148
|
+
const currentStep = this.flow?.get();
|
|
149
|
+
const nextStep = { kind: 'transfer', stage };
|
|
150
|
+
// Cannot unlock if not locked (aka: started)
|
|
151
|
+
if (currentStep?.kind === 'transfer' && !currentStep.locked) {
|
|
152
|
+
throw new providers_1.ProviderTransferError(`You need to initialize the transfer stage (${stage}) before ending it`);
|
|
153
|
+
}
|
|
154
|
+
// Cannot unlock if invalid step provided
|
|
155
|
+
if (this.flow?.cannot(nextStep)) {
|
|
156
|
+
throw new providers_1.ProviderTransferError(`Invalid stage (${stage}) provided for the current flow`, {
|
|
157
|
+
step: nextStep,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
this.flow?.set({ ...nextStep, locked: false });
|
|
161
|
+
},
|
|
162
|
+
async onTransferStep(msg) {
|
|
163
|
+
const { step: stage } = msg;
|
|
164
|
+
if (msg.action === 'start') {
|
|
165
|
+
this.lockTransferStep(stage);
|
|
166
|
+
if (this.streams?.[stage] instanceof stream_1.Writable) {
|
|
167
|
+
throw new Error('Stream already created, something went wrong');
|
|
168
|
+
}
|
|
169
|
+
await this.createWritableStreamForStep(stage);
|
|
170
|
+
return { ok: true };
|
|
171
|
+
}
|
|
172
|
+
if (msg.action === 'stream') {
|
|
173
|
+
this.assertValidStreamTransferStep(stage);
|
|
174
|
+
// Stream operation on the current transfer stage
|
|
175
|
+
const stream = this.streams?.[stage];
|
|
176
|
+
if (!stream) {
|
|
177
|
+
throw new Error('You need to init first');
|
|
178
|
+
}
|
|
179
|
+
// Assets are nested streams
|
|
180
|
+
if (stage === 'assets') {
|
|
181
|
+
return this.streamAsset(msg.data);
|
|
182
|
+
}
|
|
183
|
+
// For all other steps
|
|
184
|
+
await Promise.all(msg.data.map((item) => writeAsync(stream, item)));
|
|
185
|
+
}
|
|
186
|
+
if (msg.action === 'end') {
|
|
187
|
+
this.unlockTransferStep(stage);
|
|
188
|
+
const stream = this.streams?.[stage];
|
|
189
|
+
if (stream && !stream.closed) {
|
|
190
|
+
await new Promise((resolve, reject) => {
|
|
191
|
+
stream.on('close', resolve).on('error', reject).end();
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
delete this.streams?.[stage];
|
|
195
|
+
return { ok: true };
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
async onTransferAction(msg) {
|
|
199
|
+
const { action } = msg;
|
|
200
|
+
this.assertValidTransferAction(action);
|
|
201
|
+
const step = { kind: 'action', action };
|
|
202
|
+
const isStepRegistered = this.flow?.has(step);
|
|
203
|
+
if (isStepRegistered) {
|
|
204
|
+
if (this.flow?.cannot(step)) {
|
|
205
|
+
throw new providers_1.ProviderTransferError(`Invalid action "${action}" found for the current flow `, {
|
|
206
|
+
action,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
this.flow?.set(step);
|
|
210
|
+
}
|
|
211
|
+
return this.provider?.[action]();
|
|
212
|
+
},
|
|
213
|
+
async streamAsset(payload) {
|
|
214
|
+
const assetsStream = this.streams?.assets;
|
|
215
|
+
// TODO: close the stream upong receiving an 'end' event instead
|
|
216
|
+
if (payload === null) {
|
|
217
|
+
this.streams?.assets?.end();
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
for (const item of payload) {
|
|
221
|
+
const { action, assetID } = item;
|
|
222
|
+
if (!assetsStream) {
|
|
223
|
+
throw new Error('Stream not defined');
|
|
224
|
+
}
|
|
225
|
+
if (action === 'start') {
|
|
226
|
+
this.assets[assetID] = { ...item.data, stream: new stream_1.PassThrough() };
|
|
227
|
+
writeAsync(assetsStream, this.assets[assetID]);
|
|
228
|
+
}
|
|
229
|
+
if (action === 'stream') {
|
|
230
|
+
// The buffer has gone through JSON operations and is now of shape { type: "Buffer"; data: UInt8Array }
|
|
231
|
+
// We need to transform it back into a Buffer instance
|
|
232
|
+
const rawBuffer = item.data;
|
|
233
|
+
const chunk = Buffer.from(rawBuffer.data);
|
|
234
|
+
await writeAsync(this.assets[assetID].stream, chunk);
|
|
235
|
+
}
|
|
236
|
+
if (action === 'end') {
|
|
237
|
+
await new Promise((resolve, reject) => {
|
|
238
|
+
const { stream: assetStream } = this.assets[assetID];
|
|
239
|
+
assetStream
|
|
240
|
+
.on('close', () => {
|
|
241
|
+
delete this.assets[assetID];
|
|
242
|
+
resolve();
|
|
243
|
+
})
|
|
244
|
+
.on('error', reject)
|
|
245
|
+
.end();
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
onClose() {
|
|
251
|
+
this.teardown();
|
|
252
|
+
},
|
|
253
|
+
onError(err) {
|
|
254
|
+
this.teardown();
|
|
255
|
+
strapi.log.error(err);
|
|
256
|
+
},
|
|
257
|
+
// Commands
|
|
258
|
+
async init(params) {
|
|
259
|
+
if (this.transferID || this.provider) {
|
|
260
|
+
throw new Error('Transfer already in progress');
|
|
261
|
+
}
|
|
262
|
+
await this.verifyAuth();
|
|
263
|
+
this.transferID = (0, crypto_1.randomUUID)();
|
|
264
|
+
this.startedAt = Date.now();
|
|
265
|
+
this.assets = {};
|
|
266
|
+
this.streams = {};
|
|
267
|
+
this.flow = (0, flows_1.createFlow)(flows_1.DEFAULT_TRANSFER_FLOW);
|
|
268
|
+
this.provider = (0, providers_2.createLocalStrapiDestinationProvider)({
|
|
269
|
+
...params,
|
|
270
|
+
autoDestroy: false,
|
|
271
|
+
getStrapi: () => strapi,
|
|
272
|
+
});
|
|
273
|
+
return { transferID: this.transferID };
|
|
274
|
+
},
|
|
275
|
+
async status() {
|
|
276
|
+
const isStarted = this.isTransferStarted();
|
|
277
|
+
if (isStarted) {
|
|
278
|
+
const startedAt = this.startedAt;
|
|
279
|
+
return {
|
|
280
|
+
active: true,
|
|
281
|
+
kind: TRANSFER_KIND,
|
|
282
|
+
startedAt,
|
|
283
|
+
elapsed: Date.now() - startedAt,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
return { active: false, kind: null, elapsed: null, startedAt: null };
|
|
287
|
+
},
|
|
288
|
+
async end(params) {
|
|
289
|
+
await this.verifyAuth();
|
|
290
|
+
if (this.transferID !== params.transferID) {
|
|
291
|
+
throw new providers_1.ProviderTransferError('Bad transfer ID provided');
|
|
292
|
+
}
|
|
293
|
+
this.cleanup();
|
|
294
|
+
return { ok: true };
|
|
295
|
+
},
|
|
296
|
+
}));
|
|
297
|
+
//# sourceMappingURL=push.js.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Context } from 'koa';
|
|
3
|
+
import type { IncomingMessage } from 'http';
|
|
4
|
+
import type { ServerOptions } from 'ws';
|
|
5
|
+
import { WebSocket, WebSocketServer } from 'ws';
|
|
6
|
+
import type { Handler } from './abstract';
|
|
7
|
+
import type { Protocol } from '../../../../types';
|
|
8
|
+
import { TransferMethod } from '../constants';
|
|
9
|
+
declare type WSCallback = (client: WebSocket, request: IncomingMessage) => void;
|
|
10
|
+
export interface HandlerOptions {
|
|
11
|
+
verify: (ctx: Context, scope?: TransferMethod) => Promise<void>;
|
|
12
|
+
server?: ServerOptions;
|
|
13
|
+
}
|
|
14
|
+
export declare const transformUpgradeHeader: (header?: string) => string[];
|
|
15
|
+
/**
|
|
16
|
+
* Make sure that the upgrade header is a valid websocket one
|
|
17
|
+
*/
|
|
18
|
+
export declare const assertValidHeader: (ctx: Context) => void;
|
|
19
|
+
export declare const isDataTransferMessage: (message: unknown) => message is Protocol.client.Message;
|
|
20
|
+
/**
|
|
21
|
+
* Handle the upgrade to ws connection
|
|
22
|
+
*/
|
|
23
|
+
export declare const handleWSUpgrade: (wss: WebSocketServer, ctx: Context, callback: WSCallback) => void;
|
|
24
|
+
export declare const handlerControllerFactory: <T extends Partial<Handler>>(implementation: (proto: Handler) => T) => (options: HandlerOptions) => (ctx: Context) => Promise<void>;
|
|
25
|
+
export {};
|