@strapi/data-transfer 5.12.1 → 5.12.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/engine/errors.js +39 -0
- package/dist/engine/errors.js.map +1 -0
- package/dist/engine/errors.mjs +34 -0
- package/dist/engine/errors.mjs.map +1 -0
- package/dist/engine/index.js +797 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/engine/index.mjs +792 -0
- package/dist/engine/index.mjs.map +1 -0
- package/dist/engine/validation/provider.js +19 -0
- package/dist/engine/validation/provider.js.map +1 -0
- package/dist/engine/validation/provider.mjs +17 -0
- package/dist/engine/validation/provider.mjs.map +1 -0
- package/dist/engine/validation/schemas/index.js +57 -0
- package/dist/engine/validation/schemas/index.js.map +1 -0
- package/dist/engine/validation/schemas/index.mjs +55 -0
- package/dist/engine/validation/schemas/index.mjs.map +1 -0
- package/dist/errors/base.js +13 -0
- package/dist/errors/base.js.map +1 -0
- package/dist/errors/base.mjs +11 -0
- package/dist/errors/base.mjs.map +1 -0
- package/dist/errors/constants.js +10 -0
- package/dist/errors/constants.js.map +1 -0
- package/dist/errors/constants.mjs +8 -0
- package/dist/errors/constants.mjs.map +1 -0
- package/dist/errors/providers.js +41 -0
- package/dist/errors/providers.js.map +1 -0
- package/dist/errors/providers.mjs +36 -0
- package/dist/errors/providers.mjs.map +1 -0
- package/dist/file/index.js +8 -0
- package/dist/file/index.js.map +1 -0
- package/dist/file/index.mjs +3 -0
- package/dist/file/index.mjs.map +1 -0
- package/dist/file/providers/destination/index.js +248 -0
- package/dist/file/providers/destination/index.js.map +1 -0
- package/dist/file/providers/destination/index.mjs +246 -0
- package/dist/file/providers/destination/index.mjs.map +1 -0
- package/dist/file/providers/destination/utils.js +63 -0
- package/dist/file/providers/destination/utils.js.map +1 -0
- package/dist/file/providers/destination/utils.mjs +60 -0
- package/dist/file/providers/destination/utils.mjs.map +1 -0
- package/dist/file/providers/index.js +10 -0
- package/dist/file/providers/index.js.map +1 -0
- package/dist/file/providers/index.mjs +3 -0
- package/dist/file/providers/index.mjs.map +1 -0
- package/dist/file/providers/source/index.js +288 -0
- package/dist/file/providers/source/index.js.map +1 -0
- package/dist/file/providers/source/index.mjs +286 -0
- package/dist/file/providers/source/index.mjs.map +1 -0
- package/dist/file/providers/source/utils.js +56 -0
- package/dist/file/providers/source/utils.js.map +1 -0
- package/dist/file/providers/source/utils.mjs +52 -0
- package/dist/file/providers/source/utils.mjs.map +1 -0
- package/dist/index.js +8 -5692
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +8 -5674
- package/dist/index.mjs.map +1 -1
- package/dist/strapi/index.js +12 -0
- package/dist/strapi/index.js.map +1 -0
- package/dist/strapi/index.mjs +7 -0
- package/dist/strapi/index.mjs.map +1 -0
- package/dist/strapi/providers/index.js +16 -0
- package/dist/strapi/providers/index.js.map +1 -0
- package/dist/strapi/providers/index.mjs +7 -0
- package/dist/strapi/providers/index.mjs.map +1 -0
- package/dist/strapi/providers/local-destination/index.js +419 -0
- package/dist/strapi/providers/local-destination/index.js.map +1 -0
- package/dist/strapi/providers/local-destination/index.mjs +396 -0
- package/dist/strapi/providers/local-destination/index.mjs.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/configuration.js +52 -0
- package/dist/strapi/providers/local-destination/strategies/restore/configuration.js.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/configuration.mjs +49 -0
- package/dist/strapi/providers/local-destination/strategies/restore/configuration.mjs.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/entities.js +64 -0
- package/dist/strapi/providers/local-destination/strategies/restore/entities.js.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/entities.mjs +62 -0
- package/dist/strapi/providers/local-destination/strategies/restore/entities.mjs.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/index.js +126 -0
- package/dist/strapi/providers/local-destination/strategies/restore/index.js.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/index.mjs +122 -0
- package/dist/strapi/providers/local-destination/strategies/restore/index.mjs.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/links.js +60 -0
- package/dist/strapi/providers/local-destination/strategies/restore/links.js.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/links.mjs +58 -0
- package/dist/strapi/providers/local-destination/strategies/restore/links.mjs.map +1 -0
- package/dist/strapi/providers/local-source/assets.js +123 -0
- package/dist/strapi/providers/local-source/assets.js.map +1 -0
- package/dist/strapi/providers/local-source/assets.mjs +121 -0
- package/dist/strapi/providers/local-source/assets.mjs.map +1 -0
- package/dist/strapi/providers/local-source/configuration.js +39 -0
- package/dist/strapi/providers/local-source/configuration.js.map +1 -0
- package/dist/strapi/providers/local-source/configuration.mjs +37 -0
- package/dist/strapi/providers/local-source/configuration.mjs.map +1 -0
- package/dist/strapi/providers/local-source/entities.js +62 -0
- package/dist/strapi/providers/local-source/entities.js.map +1 -0
- package/dist/strapi/providers/local-source/entities.mjs +59 -0
- package/dist/strapi/providers/local-source/entities.mjs.map +1 -0
- package/dist/strapi/providers/local-source/index.js +154 -0
- package/dist/strapi/providers/local-source/index.js.map +1 -0
- package/dist/strapi/providers/local-source/index.mjs +152 -0
- package/dist/strapi/providers/local-source/index.mjs.map +1 -0
- package/dist/strapi/providers/local-source/links.js +26 -0
- package/dist/strapi/providers/local-source/links.js.map +1 -0
- package/dist/strapi/providers/local-source/links.mjs +24 -0
- package/dist/strapi/providers/local-source/links.mjs.map +1 -0
- package/dist/strapi/providers/remote-destination/index.js +392 -0
- package/dist/strapi/providers/remote-destination/index.js.map +1 -0
- package/dist/strapi/providers/remote-destination/index.mjs +390 -0
- package/dist/strapi/providers/remote-destination/index.mjs.map +1 -0
- package/dist/strapi/providers/remote-source/index.js +405 -0
- package/dist/strapi/providers/remote-source/index.js.map +1 -0
- package/dist/strapi/providers/remote-source/index.mjs +403 -0
- package/dist/strapi/providers/remote-source/index.mjs.map +1 -0
- package/dist/strapi/providers/utils.js +173 -0
- package/dist/strapi/providers/utils.js.map +1 -0
- package/dist/strapi/providers/utils.mjs +169 -0
- package/dist/strapi/providers/utils.mjs.map +1 -0
- package/dist/strapi/queries/entity.js +125 -0
- package/dist/strapi/queries/entity.js.map +1 -0
- package/dist/strapi/queries/entity.mjs +123 -0
- package/dist/strapi/queries/entity.mjs.map +1 -0
- package/dist/strapi/queries/index.js +10 -0
- package/dist/strapi/queries/index.js.map +1 -0
- package/dist/strapi/queries/index.mjs +5 -0
- package/dist/strapi/queries/index.mjs.map +1 -0
- package/dist/strapi/queries/link.js +298 -0
- package/dist/strapi/queries/link.js.map +1 -0
- package/dist/strapi/queries/link.mjs +295 -0
- package/dist/strapi/queries/link.mjs.map +1 -0
- package/dist/strapi/remote/constants.js +11 -0
- package/dist/strapi/remote/constants.js.map +1 -0
- package/dist/strapi/remote/constants.mjs +8 -0
- package/dist/strapi/remote/constants.mjs.map +1 -0
- package/dist/strapi/remote/flows/default.js +43 -0
- package/dist/strapi/remote/flows/default.js.map +1 -0
- package/dist/strapi/remote/flows/default.mjs +41 -0
- package/dist/strapi/remote/flows/default.mjs.map +1 -0
- package/dist/strapi/remote/flows/index.js +54 -0
- package/dist/strapi/remote/flows/index.js.map +1 -0
- package/dist/strapi/remote/flows/index.mjs +52 -0
- package/dist/strapi/remote/flows/index.mjs.map +1 -0
- package/dist/strapi/remote/handlers/constants.js +10 -0
- package/dist/strapi/remote/handlers/constants.js.map +1 -0
- package/dist/strapi/remote/handlers/constants.mjs +8 -0
- package/dist/strapi/remote/handlers/constants.mjs.map +1 -0
- package/dist/strapi/remote/handlers/index.js +12 -0
- package/dist/strapi/remote/handlers/index.js.map +1 -0
- package/dist/strapi/remote/handlers/index.mjs +4 -0
- package/dist/strapi/remote/handlers/index.mjs.map +1 -0
- package/dist/strapi/remote/handlers/pull.js +348 -0
- package/dist/strapi/remote/handlers/pull.js.map +1 -0
- package/dist/strapi/remote/handlers/pull.mjs +346 -0
- package/dist/strapi/remote/handlers/pull.mjs.map +1 -0
- package/dist/strapi/remote/handlers/push.js +400 -0
- package/dist/strapi/remote/handlers/push.js.map +1 -0
- package/dist/strapi/remote/handlers/push.mjs +398 -0
- package/dist/strapi/remote/handlers/push.mjs.map +1 -0
- package/dist/strapi/remote/handlers/utils.js +316 -0
- package/dist/strapi/remote/handlers/utils.js.map +1 -0
- package/dist/strapi/remote/handlers/utils.mjs +310 -0
- package/dist/strapi/remote/handlers/utils.mjs.map +1 -0
- package/dist/strapi/remote/index.js +10 -0
- package/dist/strapi/remote/index.js.map +1 -0
- package/dist/strapi/remote/index.mjs +5 -0
- package/dist/strapi/remote/index.mjs.map +1 -0
- package/dist/utils/components.js +178 -0
- package/dist/utils/components.js.map +1 -0
- package/dist/utils/components.mjs +171 -0
- package/dist/utils/components.mjs.map +1 -0
- package/dist/utils/diagnostic.js +51 -0
- package/dist/utils/diagnostic.js.map +1 -0
- package/dist/utils/diagnostic.mjs +49 -0
- package/dist/utils/diagnostic.mjs.map +1 -0
- package/dist/utils/encryption/decrypt.js +47 -0
- package/dist/utils/encryption/decrypt.js.map +1 -0
- package/dist/utils/encryption/decrypt.mjs +45 -0
- package/dist/utils/encryption/decrypt.mjs.map +1 -0
- package/dist/utils/encryption/encrypt.js +47 -0
- package/dist/utils/encryption/encrypt.js.map +1 -0
- package/dist/utils/encryption/encrypt.mjs +45 -0
- package/dist/utils/encryption/encrypt.mjs.map +1 -0
- package/dist/utils/encryption/index.js +10 -0
- package/dist/utils/encryption/index.js.map +1 -0
- package/dist/utils/encryption/index.mjs +3 -0
- package/dist/utils/encryption/index.mjs.map +1 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/index.mjs +15 -0
- package/dist/utils/index.mjs.map +1 -0
- package/dist/utils/json.js +96 -0
- package/dist/utils/json.js.map +1 -0
- package/dist/utils/json.mjs +94 -0
- package/dist/utils/json.mjs.map +1 -0
- package/dist/utils/middleware.js +14 -0
- package/dist/utils/middleware.js.map +1 -0
- package/dist/utils/middleware.mjs +12 -0
- package/dist/utils/middleware.mjs.map +1 -0
- package/dist/utils/providers.js +12 -0
- package/dist/utils/providers.js.map +1 -0
- package/dist/utils/providers.mjs +10 -0
- package/dist/utils/providers.mjs.map +1 -0
- package/dist/utils/schema.js +32 -0
- package/dist/utils/schema.js.map +1 -0
- package/dist/utils/schema.mjs +29 -0
- package/dist/utils/schema.mjs.map +1 -0
- package/dist/utils/stream.js +59 -0
- package/dist/utils/stream.js.map +1 -0
- package/dist/utils/stream.mjs +55 -0
- package/dist/utils/stream.mjs.map +1 -0
- package/dist/utils/transaction.js +93 -0
- package/dist/utils/transaction.js.map +1 -0
- package/dist/utils/transaction.mjs +91 -0
- package/dist/utils/transaction.mjs.map +1 -0
- package/package.json +5 -5
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import { PassThrough } from 'stream';
|
|
2
|
+
import { castArray } from 'lodash/fp';
|
|
3
|
+
import { ProviderValidationError, ProviderTransferError } from '../../../errors/providers.mjs';
|
|
4
|
+
import { TRANSFER_PATH } from '../../remote/constants.mjs';
|
|
5
|
+
import { trimTrailingSlash, connectToWebsocket, createDispatcher } from '../utils.mjs';
|
|
6
|
+
|
|
7
|
+
function _class_private_field_loose_base(receiver, privateKey) {
|
|
8
|
+
if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {
|
|
9
|
+
throw new TypeError("attempted to use private field on non-instance");
|
|
10
|
+
}
|
|
11
|
+
return receiver;
|
|
12
|
+
}
|
|
13
|
+
var id = 0;
|
|
14
|
+
function _class_private_field_loose_key(name) {
|
|
15
|
+
return "__private_" + id++ + "_" + name;
|
|
16
|
+
}
|
|
17
|
+
var _diagnostics = /*#__PURE__*/ _class_private_field_loose_key("_diagnostics"), _createStageReadStream = /*#__PURE__*/ _class_private_field_loose_key("_createStageReadStream"), _reportInfo = /*#__PURE__*/ _class_private_field_loose_key("_reportInfo"), _startStep = /*#__PURE__*/ _class_private_field_loose_key("_startStep"), _respond = /*#__PURE__*/ _class_private_field_loose_key("_respond"), _endStep = /*#__PURE__*/ _class_private_field_loose_key("_endStep");
|
|
18
|
+
class RemoteStrapiSourceProvider {
|
|
19
|
+
createEntitiesReadStream() {
|
|
20
|
+
return _class_private_field_loose_base(this, _createStageReadStream)[_createStageReadStream]('entities');
|
|
21
|
+
}
|
|
22
|
+
createLinksReadStream() {
|
|
23
|
+
return _class_private_field_loose_base(this, _createStageReadStream)[_createStageReadStream]('links');
|
|
24
|
+
}
|
|
25
|
+
async createAssetsReadStream() {
|
|
26
|
+
// Create the streams used to transfer the assets
|
|
27
|
+
const stream = await _class_private_field_loose_base(this, _createStageReadStream)[_createStageReadStream]('assets');
|
|
28
|
+
const pass = new PassThrough({
|
|
29
|
+
objectMode: true
|
|
30
|
+
});
|
|
31
|
+
// Init the asset map
|
|
32
|
+
const assets = {};
|
|
33
|
+
// Watch for stalled assets; if we don't receive a chunk within timeout, abort transfer
|
|
34
|
+
const resetTimeout = (assetID)=>{
|
|
35
|
+
if (assets[assetID].timeout) {
|
|
36
|
+
clearTimeout(assets[assetID].timeout);
|
|
37
|
+
}
|
|
38
|
+
assets[assetID].timeout = setTimeout(()=>{
|
|
39
|
+
_class_private_field_loose_base(this, _reportInfo)[_reportInfo](`Asset ${assetID} transfer stalled, aborting.`);
|
|
40
|
+
assets[assetID].status = 'errored';
|
|
41
|
+
assets[assetID].stream.destroy(new Error(`Asset ${assetID} transfer timed out`));
|
|
42
|
+
}, this.options.streamTimeout);
|
|
43
|
+
};
|
|
44
|
+
stream/**
|
|
45
|
+
* Process a payload of many transfer assets and performs the following tasks:
|
|
46
|
+
* - Start: creates a stream for new assets.
|
|
47
|
+
* - Stream: writes asset chunks to the asset's stream.
|
|
48
|
+
* - End: closes the stream after the asset s transferred and cleanup related resources.
|
|
49
|
+
*/ .on('data', async (payload)=>{
|
|
50
|
+
for (const item of payload){
|
|
51
|
+
const { action, assetID } = item;
|
|
52
|
+
// Creates the stream to send the incoming asset through
|
|
53
|
+
if (action === 'start') {
|
|
54
|
+
// if a transfer has already been started for the same asset ID, something is wrong
|
|
55
|
+
if (assets[assetID]) {
|
|
56
|
+
throw new Error(`Asset ${assetID} already started`);
|
|
57
|
+
}
|
|
58
|
+
_class_private_field_loose_base(this, _reportInfo)[_reportInfo](`Asset ${assetID} starting`);
|
|
59
|
+
// Register the asset
|
|
60
|
+
assets[assetID] = {
|
|
61
|
+
...item.data,
|
|
62
|
+
stream: new PassThrough(),
|
|
63
|
+
status: 'ok',
|
|
64
|
+
queue: []
|
|
65
|
+
};
|
|
66
|
+
resetTimeout(assetID);
|
|
67
|
+
// Connect the individual asset stream to the main asset stage stream
|
|
68
|
+
// Note: nothing is transferred until data chunks are fed to the asset stream
|
|
69
|
+
await this.writeAsync(pass, assets[assetID]);
|
|
70
|
+
} else if (action === 'stream' || action === 'end') {
|
|
71
|
+
// If the asset hasn't been registered, or if it's been closed already, something is wrong
|
|
72
|
+
if (!assets[assetID]) {
|
|
73
|
+
throw new Error(`No id matching ${assetID} for stream action`);
|
|
74
|
+
}
|
|
75
|
+
// On every action, reset the timeout timer
|
|
76
|
+
if (action === 'stream') {
|
|
77
|
+
resetTimeout(assetID);
|
|
78
|
+
} else {
|
|
79
|
+
clearTimeout(assets[assetID].timeout);
|
|
80
|
+
}
|
|
81
|
+
if (assets[assetID].status === 'closed') {
|
|
82
|
+
throw new Error(`Asset ${assetID} is closed`);
|
|
83
|
+
}
|
|
84
|
+
assets[assetID].queue.push(item);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// each new payload will start new processQueue calls, which may cause some extra calls
|
|
88
|
+
// it's essentially saying "start processing this asset again, I added more data to the queue"
|
|
89
|
+
for(const assetID in assets){
|
|
90
|
+
if (Object.prototype.hasOwnProperty.call(assets, assetID)) {
|
|
91
|
+
const asset = assets[assetID];
|
|
92
|
+
if (asset.queue?.length > 0) {
|
|
93
|
+
await processQueue(assetID);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}).on('close', ()=>{
|
|
98
|
+
pass.end();
|
|
99
|
+
});
|
|
100
|
+
/**
|
|
101
|
+
* Start processing the queue for a given assetID
|
|
102
|
+
*
|
|
103
|
+
* Even though this is a loop that attempts to process the entire queue, it is safe to call this more than once
|
|
104
|
+
* for the same asset id because the queue is shared globally, the items are shifted off, and immediately written
|
|
105
|
+
*/ const processQueue = async (id)=>{
|
|
106
|
+
if (!assets[id]) {
|
|
107
|
+
throw new Error(`Failed to write asset chunk for "${id}". Asset not found.`);
|
|
108
|
+
}
|
|
109
|
+
const asset = assets[id];
|
|
110
|
+
const { status: currentStatus } = asset;
|
|
111
|
+
if ([
|
|
112
|
+
'closed',
|
|
113
|
+
'errored'
|
|
114
|
+
].includes(currentStatus)) {
|
|
115
|
+
throw new Error(`Failed to write asset chunk for "${id}". The asset is currently "${currentStatus}"`);
|
|
116
|
+
}
|
|
117
|
+
while(asset.queue.length > 0){
|
|
118
|
+
const data = asset.queue.shift();
|
|
119
|
+
if (!data) {
|
|
120
|
+
throw new Error(`Invalid chunk found for ${id}`);
|
|
121
|
+
}
|
|
122
|
+
try {
|
|
123
|
+
// if this is an end chunk, close the asset stream
|
|
124
|
+
if (data.action === 'end') {
|
|
125
|
+
_class_private_field_loose_base(this, _reportInfo)[_reportInfo](`Ending asset stream for ${id}`);
|
|
126
|
+
await closeAssetStream(id);
|
|
127
|
+
break; // Exit the loop after closing the stream
|
|
128
|
+
}
|
|
129
|
+
// Save the current chunk
|
|
130
|
+
await writeChunkToStream(id, data);
|
|
131
|
+
} catch {
|
|
132
|
+
if (!assets[id]) {
|
|
133
|
+
throw new Error(`No id matching ${id} for writeAssetChunk`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Writes a chunk of data to the asset's stream.
|
|
140
|
+
*
|
|
141
|
+
* Only check if the targeted asset exists, no other validation is done.
|
|
142
|
+
*/ const writeChunkToStream = async (id, data)=>{
|
|
143
|
+
const asset = assets[id];
|
|
144
|
+
if (!asset) {
|
|
145
|
+
throw new Error(`Failed to write asset chunk for "${id}". Asset not found.`);
|
|
146
|
+
}
|
|
147
|
+
const rawBuffer = data;
|
|
148
|
+
const chunk = Buffer.from(rawBuffer.data);
|
|
149
|
+
await this.writeAsync(asset.stream, chunk);
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* Closes the asset stream associated with the given ID.
|
|
153
|
+
*
|
|
154
|
+
* It deletes the stream for the asset upon successful closure.
|
|
155
|
+
*/ const closeAssetStream = async (id)=>{
|
|
156
|
+
if (!assets[id]) {
|
|
157
|
+
throw new Error(`Failed to close asset "${id}". Asset not found.`);
|
|
158
|
+
}
|
|
159
|
+
assets[id].status = 'closed';
|
|
160
|
+
await new Promise((resolve, reject)=>{
|
|
161
|
+
const { stream } = assets[id];
|
|
162
|
+
stream.on('close', ()=>{
|
|
163
|
+
resolve();
|
|
164
|
+
}).on('error', (e)=>{
|
|
165
|
+
assets[id].status = 'errored';
|
|
166
|
+
reject(new Error(`Failed to close asset "${id}". Asset stream error: ${e.toString()}`));
|
|
167
|
+
}).end();
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
return pass;
|
|
171
|
+
}
|
|
172
|
+
createConfigurationReadStream() {
|
|
173
|
+
return _class_private_field_loose_base(this, _createStageReadStream)[_createStageReadStream]('configuration');
|
|
174
|
+
}
|
|
175
|
+
async getMetadata() {
|
|
176
|
+
const metadata = await this.dispatcher?.dispatchTransferAction('getMetadata');
|
|
177
|
+
return metadata ?? null;
|
|
178
|
+
}
|
|
179
|
+
assertValidProtocol(url) {
|
|
180
|
+
const validProtocols = [
|
|
181
|
+
'https:',
|
|
182
|
+
'http:'
|
|
183
|
+
];
|
|
184
|
+
if (!validProtocols.includes(url.protocol)) {
|
|
185
|
+
throw new ProviderValidationError(`Invalid protocol "${url.protocol}"`, {
|
|
186
|
+
check: 'url',
|
|
187
|
+
details: {
|
|
188
|
+
protocol: url.protocol,
|
|
189
|
+
validProtocols
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
async initTransfer() {
|
|
195
|
+
const query = this.dispatcher?.dispatchCommand({
|
|
196
|
+
command: 'init'
|
|
197
|
+
});
|
|
198
|
+
const res = await query;
|
|
199
|
+
if (!res?.transferID) {
|
|
200
|
+
throw new ProviderTransferError('Init failed, invalid response from the server');
|
|
201
|
+
}
|
|
202
|
+
return res.transferID;
|
|
203
|
+
}
|
|
204
|
+
async bootstrap(diagnostics) {
|
|
205
|
+
_class_private_field_loose_base(this, _diagnostics)[_diagnostics] = diagnostics;
|
|
206
|
+
const { url, auth } = this.options;
|
|
207
|
+
let ws;
|
|
208
|
+
this.assertValidProtocol(url);
|
|
209
|
+
const wsProtocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
210
|
+
const wsUrl = `${wsProtocol}//${url.host}${trimTrailingSlash(url.pathname)}${TRANSFER_PATH}/pull`;
|
|
211
|
+
_class_private_field_loose_base(this, _reportInfo)[_reportInfo]('establishing websocket connection');
|
|
212
|
+
// No auth defined, trying public access for transfer
|
|
213
|
+
if (!auth) {
|
|
214
|
+
ws = await connectToWebsocket(wsUrl, undefined, _class_private_field_loose_base(this, _diagnostics)[_diagnostics]);
|
|
215
|
+
} else if (auth.type === 'token') {
|
|
216
|
+
const headers = {
|
|
217
|
+
Authorization: `Bearer ${auth.token}`
|
|
218
|
+
};
|
|
219
|
+
ws = await connectToWebsocket(wsUrl, {
|
|
220
|
+
headers
|
|
221
|
+
}, _class_private_field_loose_base(this, _diagnostics)[_diagnostics]);
|
|
222
|
+
} else {
|
|
223
|
+
throw new ProviderValidationError('Auth method not available', {
|
|
224
|
+
check: 'auth.type',
|
|
225
|
+
details: {
|
|
226
|
+
auth: auth.type
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
_class_private_field_loose_base(this, _reportInfo)[_reportInfo]('established websocket connection');
|
|
231
|
+
this.ws = ws;
|
|
232
|
+
const { retryMessageOptions } = this.options;
|
|
233
|
+
_class_private_field_loose_base(this, _reportInfo)[_reportInfo]('creating dispatcher');
|
|
234
|
+
this.dispatcher = createDispatcher(this.ws, retryMessageOptions, (message)=>_class_private_field_loose_base(this, _reportInfo)[_reportInfo](message));
|
|
235
|
+
_class_private_field_loose_base(this, _reportInfo)[_reportInfo]('creating dispatcher');
|
|
236
|
+
_class_private_field_loose_base(this, _reportInfo)[_reportInfo]('initialize transfer');
|
|
237
|
+
const transferID = await this.initTransfer();
|
|
238
|
+
_class_private_field_loose_base(this, _reportInfo)[_reportInfo](`initialized transfer ${transferID}`);
|
|
239
|
+
this.dispatcher.setTransferProperties({
|
|
240
|
+
id: transferID,
|
|
241
|
+
kind: 'pull'
|
|
242
|
+
});
|
|
243
|
+
await this.dispatcher.dispatchTransferAction('bootstrap');
|
|
244
|
+
}
|
|
245
|
+
async close() {
|
|
246
|
+
await this.dispatcher?.dispatchTransferAction('close');
|
|
247
|
+
await new Promise((resolve)=>{
|
|
248
|
+
const { ws } = this;
|
|
249
|
+
if (!ws || ws.CLOSED) {
|
|
250
|
+
resolve();
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
ws.on('close', ()=>resolve()).close();
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
async getSchemas() {
|
|
257
|
+
const schemas = await this.dispatcher?.dispatchTransferAction('getSchemas');
|
|
258
|
+
return schemas ?? null;
|
|
259
|
+
}
|
|
260
|
+
constructor(options){
|
|
261
|
+
Object.defineProperty(this, _createStageReadStream, {
|
|
262
|
+
value: createStageReadStream
|
|
263
|
+
});
|
|
264
|
+
Object.defineProperty(this, _reportInfo, {
|
|
265
|
+
value: reportInfo
|
|
266
|
+
});
|
|
267
|
+
Object.defineProperty(this, _startStep, {
|
|
268
|
+
value: startStep
|
|
269
|
+
});
|
|
270
|
+
Object.defineProperty(this, _respond, {
|
|
271
|
+
value: respond
|
|
272
|
+
});
|
|
273
|
+
Object.defineProperty(this, _endStep, {
|
|
274
|
+
value: endStep
|
|
275
|
+
});
|
|
276
|
+
Object.defineProperty(this, _diagnostics, {
|
|
277
|
+
writable: true,
|
|
278
|
+
value: void 0
|
|
279
|
+
});
|
|
280
|
+
this.name = 'source::remote-strapi';
|
|
281
|
+
this.type = 'source';
|
|
282
|
+
this.defaultOptions = {
|
|
283
|
+
streamTimeout: 15000
|
|
284
|
+
};
|
|
285
|
+
this.writeAsync = (stream, data)=>{
|
|
286
|
+
return new Promise((resolve, reject)=>{
|
|
287
|
+
stream.write(data, (error)=>{
|
|
288
|
+
if (error) {
|
|
289
|
+
reject(error);
|
|
290
|
+
}
|
|
291
|
+
resolve();
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
};
|
|
295
|
+
this.options = {
|
|
296
|
+
...this.defaultOptions,
|
|
297
|
+
...options
|
|
298
|
+
};
|
|
299
|
+
this.ws = null;
|
|
300
|
+
this.dispatcher = null;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
async function createStageReadStream(stage) {
|
|
304
|
+
const startResult = await _class_private_field_loose_base(this, _startStep)[_startStep](stage);
|
|
305
|
+
if (startResult instanceof Error) {
|
|
306
|
+
throw startResult;
|
|
307
|
+
}
|
|
308
|
+
const { id: processID } = startResult;
|
|
309
|
+
const stream = new PassThrough({
|
|
310
|
+
objectMode: true
|
|
311
|
+
});
|
|
312
|
+
const listener = async (raw)=>{
|
|
313
|
+
const parsed = JSON.parse(raw.toString());
|
|
314
|
+
// If not a message related to our transfer process, ignore it
|
|
315
|
+
if (!parsed.uuid || parsed?.data?.type !== 'transfer' || parsed?.data?.id !== processID) {
|
|
316
|
+
this.ws?.once('message', listener);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const { uuid, data: message } = parsed;
|
|
320
|
+
const { ended, error, data } = message;
|
|
321
|
+
if (error) {
|
|
322
|
+
await _class_private_field_loose_base(this, _respond)[_respond](uuid);
|
|
323
|
+
stream.destroy(error);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
if (ended) {
|
|
327
|
+
await _class_private_field_loose_base(this, _respond)[_respond](uuid);
|
|
328
|
+
await _class_private_field_loose_base(this, _endStep)[_endStep](stage);
|
|
329
|
+
stream.end();
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
// if we get a single items instead of a batch
|
|
333
|
+
for (const item of castArray(data)){
|
|
334
|
+
stream.push(item);
|
|
335
|
+
}
|
|
336
|
+
this.ws?.once('message', listener);
|
|
337
|
+
await _class_private_field_loose_base(this, _respond)[_respond](uuid);
|
|
338
|
+
};
|
|
339
|
+
this.ws?.once('message', listener);
|
|
340
|
+
return stream;
|
|
341
|
+
}
|
|
342
|
+
function reportInfo(message) {
|
|
343
|
+
_class_private_field_loose_base(this, _diagnostics)[_diagnostics]?.report({
|
|
344
|
+
details: {
|
|
345
|
+
createdAt: new Date(),
|
|
346
|
+
message,
|
|
347
|
+
origin: 'remote-source-provider'
|
|
348
|
+
},
|
|
349
|
+
kind: 'info'
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
async function startStep(step) {
|
|
353
|
+
try {
|
|
354
|
+
return await this.dispatcher?.dispatchTransferStep({
|
|
355
|
+
action: 'start',
|
|
356
|
+
step
|
|
357
|
+
});
|
|
358
|
+
} catch (e) {
|
|
359
|
+
if (e instanceof Error) {
|
|
360
|
+
return e;
|
|
361
|
+
}
|
|
362
|
+
if (typeof e === 'string') {
|
|
363
|
+
return new ProviderTransferError(e);
|
|
364
|
+
}
|
|
365
|
+
return new ProviderTransferError('Unexpected error');
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
async function respond(uuid) {
|
|
369
|
+
return new Promise((resolve, reject)=>{
|
|
370
|
+
this.ws?.send(JSON.stringify({
|
|
371
|
+
uuid
|
|
372
|
+
}), (e)=>{
|
|
373
|
+
if (e) {
|
|
374
|
+
reject(e);
|
|
375
|
+
} else {
|
|
376
|
+
resolve(e);
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
async function endStep(step) {
|
|
382
|
+
try {
|
|
383
|
+
await this.dispatcher?.dispatchTransferStep({
|
|
384
|
+
action: 'end',
|
|
385
|
+
step
|
|
386
|
+
});
|
|
387
|
+
} catch (e) {
|
|
388
|
+
if (e instanceof Error) {
|
|
389
|
+
return e;
|
|
390
|
+
}
|
|
391
|
+
if (typeof e === 'string') {
|
|
392
|
+
return new ProviderTransferError(e);
|
|
393
|
+
}
|
|
394
|
+
return new ProviderTransferError('Unexpected error');
|
|
395
|
+
}
|
|
396
|
+
return null;
|
|
397
|
+
}
|
|
398
|
+
const createRemoteStrapiSourceProvider = (options)=>{
|
|
399
|
+
return new RemoteStrapiSourceProvider(options);
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
export { createRemoteStrapiSourceProvider };
|
|
403
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../../../src/strapi/providers/remote-source/index.ts"],"sourcesContent":["import { PassThrough, Readable, Writable } from 'stream';\nimport type { Struct, Utils } from '@strapi/types';\nimport { WebSocket } from 'ws';\nimport { castArray } from 'lodash/fp';\n\nimport type {\n IAsset,\n IMetadata,\n ISourceProvider,\n ISourceProviderTransferResults,\n MaybePromise,\n Protocol,\n ProviderType,\n TransferStage,\n} from '../../../../types';\nimport type { IDiagnosticReporter } from '../../../utils/diagnostic';\nimport { Client, Server, Auth } from '../../../../types/remote/protocol';\nimport { ProviderTransferError, ProviderValidationError } from '../../../errors/providers';\nimport { TRANSFER_PATH } from '../../remote/constants';\nimport { ILocalStrapiSourceProviderOptions } from '../local-source';\nimport { createDispatcher, connectToWebsocket, trimTrailingSlash } from '../utils';\n\nexport interface IRemoteStrapiSourceProviderOptions extends ILocalStrapiSourceProviderOptions {\n url: URL; // the url of the remote Strapi admin\n auth?: Auth.ITransferTokenAuth;\n retryMessageOptions?: {\n retryMessageTimeout: number; // milliseconds to wait for a response from a message\n retryMessageMaxRetries: number; // max number of retries for a message before aborting transfer\n };\n streamTimeout?: number; // milliseconds to wait between chunks of an asset before aborting the transfer\n}\n\ntype QueueableAction = Protocol.Client.TransferAssetFlow &\n ({ action: 'stream' } | { action: 'end' });\n\nclass RemoteStrapiSourceProvider implements ISourceProvider {\n name = 'source::remote-strapi';\n\n type: ProviderType = 'source';\n\n options: IRemoteStrapiSourceProviderOptions;\n\n ws: WebSocket | null;\n\n dispatcher: ReturnType<typeof createDispatcher> | null;\n\n defaultOptions: Partial<IRemoteStrapiSourceProviderOptions> = {\n streamTimeout: 15000,\n };\n\n constructor(options: IRemoteStrapiSourceProviderOptions) {\n this.options = {\n ...this.defaultOptions,\n ...options,\n };\n\n this.ws = null;\n this.dispatcher = null;\n }\n\n results?: ISourceProviderTransferResults | undefined;\n\n #diagnostics?: IDiagnosticReporter;\n\n async #createStageReadStream(stage: Exclude<TransferStage, 'schemas'>) {\n const startResult = await this.#startStep(stage);\n\n if (startResult instanceof Error) {\n throw startResult;\n }\n\n const { id: processID } = startResult as { id: string };\n\n const stream = new PassThrough({ objectMode: true });\n\n const listener = async (raw: Buffer) => {\n const parsed = JSON.parse(raw.toString());\n // If not a message related to our transfer process, ignore it\n if (!parsed.uuid || parsed?.data?.type !== 'transfer' || parsed?.data?.id !== processID) {\n this.ws?.once('message', listener);\n return;\n }\n\n const { uuid, data: message } = parsed;\n const { ended, error, data } = message;\n\n if (error) {\n await this.#respond(uuid);\n stream.destroy(error);\n return;\n }\n\n if (ended) {\n await this.#respond(uuid);\n await this.#endStep(stage);\n\n stream.end();\n return;\n }\n\n // if we get a single items instead of a batch\n for (const item of castArray(data)) {\n stream.push(item);\n }\n\n this.ws?.once('message', listener);\n\n await this.#respond(uuid);\n };\n\n this.ws?.once('message', listener);\n\n return stream;\n }\n\n createEntitiesReadStream(): MaybePromise<Readable> {\n return this.#createStageReadStream('entities');\n }\n\n createLinksReadStream(): MaybePromise<Readable> {\n return this.#createStageReadStream('links');\n }\n\n 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\n async createAssetsReadStream(): Promise<Readable> {\n // Create the streams used to transfer the assets\n const stream = await this.#createStageReadStream('assets');\n const pass = new PassThrough({ objectMode: true });\n\n // Init the asset map\n const assets: {\n // TODO: could we include filename in this for improved logging?\n [assetID: string]: IAsset & {\n stream: PassThrough;\n queue: Array<QueueableAction>;\n status: 'ok' | 'closed' | 'errored';\n timeout?: NodeJS.Timeout;\n };\n } = {};\n\n // Watch for stalled assets; if we don't receive a chunk within timeout, abort transfer\n const resetTimeout = (assetID: string) => {\n if (assets[assetID].timeout) {\n clearTimeout(assets[assetID].timeout);\n }\n assets[assetID].timeout = setTimeout(() => {\n this.#reportInfo(`Asset ${assetID} transfer stalled, aborting.`);\n assets[assetID].status = 'errored';\n assets[assetID].stream.destroy(new Error(`Asset ${assetID} transfer timed out`));\n }, this.options.streamTimeout);\n };\n\n stream\n /**\n * Process a payload of many transfer assets and performs the following tasks:\n * - Start: creates a stream for new assets.\n * - Stream: writes asset chunks to the asset's stream.\n * - End: closes the stream after the asset s transferred and cleanup related resources.\n */\n .on('data', async (payload: Protocol.Client.TransferAssetFlow[]) => {\n for (const item of payload) {\n const { action, assetID } = item;\n\n // Creates the stream to send the incoming asset through\n if (action === 'start') {\n // if a transfer has already been started for the same asset ID, something is wrong\n if (assets[assetID]) {\n throw new Error(`Asset ${assetID} already started`);\n }\n\n this.#reportInfo(`Asset ${assetID} starting`);\n // Register the asset\n assets[assetID] = {\n ...item.data,\n stream: new PassThrough(),\n status: 'ok',\n queue: [],\n };\n\n resetTimeout(assetID);\n\n // Connect the individual asset stream to the main asset stage stream\n // Note: nothing is transferred until data chunks are fed to the asset stream\n await this.writeAsync(pass, assets[assetID]);\n }\n\n // Writes the asset's data chunks to their corresponding stream\n // \"end\" is considered a chunk, but it's not a data chunk, it's a control message\n // That is done so that we don't complicate the already complicated async processing of the queue\n else if (action === 'stream' || action === 'end') {\n // If the asset hasn't been registered, or if it's been closed already, something is wrong\n if (!assets[assetID]) {\n throw new Error(`No id matching ${assetID} for stream action`);\n }\n\n // On every action, reset the timeout timer\n if (action === 'stream') {\n resetTimeout(assetID);\n } else {\n clearTimeout(assets[assetID].timeout);\n }\n\n if (assets[assetID].status === 'closed') {\n throw new Error(`Asset ${assetID} is closed`);\n }\n\n assets[assetID].queue.push(item);\n }\n }\n\n // each new payload will start new processQueue calls, which may cause some extra calls\n // it's essentially saying \"start processing this asset again, I added more data to the queue\"\n for (const assetID in assets) {\n if (Object.prototype.hasOwnProperty.call(assets, assetID)) {\n const asset = assets[assetID];\n if (asset.queue?.length > 0) {\n await processQueue(assetID);\n }\n }\n }\n })\n .on('close', () => {\n pass.end();\n });\n\n /**\n * Start processing the queue for a given assetID\n *\n * Even though this is a loop that attempts to process the entire queue, it is safe to call this more than once\n * for the same asset id because the queue is shared globally, the items are shifted off, and immediately written\n */\n const processQueue = async (id: string) => {\n if (!assets[id]) {\n throw new Error(`Failed to write asset chunk for \"${id}\". Asset not found.`);\n }\n\n const asset = assets[id];\n const { status: currentStatus } = asset;\n\n if (['closed', 'errored'].includes(currentStatus)) {\n throw new Error(\n `Failed to write asset chunk for \"${id}\". The asset is currently \"${currentStatus}\"`\n );\n }\n\n while (asset.queue.length > 0) {\n const data = asset.queue.shift();\n\n if (!data) {\n throw new Error(`Invalid chunk found for ${id}`);\n }\n\n try {\n // if this is an end chunk, close the asset stream\n if (data.action === 'end') {\n this.#reportInfo(`Ending asset stream for ${id}`);\n await closeAssetStream(id);\n break; // Exit the loop after closing the stream\n }\n\n // Save the current chunk\n await writeChunkToStream(id, data);\n } catch {\n if (!assets[id]) {\n throw new Error(`No id matching ${id} for writeAssetChunk`);\n }\n }\n }\n };\n\n /**\n * Writes a chunk of data to the asset's stream.\n *\n * Only check if the targeted asset exists, no other validation is done.\n */\n const writeChunkToStream = async (id: string, data: unknown) => {\n const asset = assets[id];\n\n if (!asset) {\n throw new Error(`Failed to write asset chunk for \"${id}\". Asset not found.`);\n }\n\n const rawBuffer = data as { type: 'Buffer'; data: Uint8Array };\n const chunk = Buffer.from(rawBuffer.data);\n\n await this.writeAsync(asset.stream, chunk);\n };\n\n /**\n * Closes the asset stream associated with the given ID.\n *\n * It deletes the stream for the asset upon successful closure.\n */\n const closeAssetStream = async (id: string) => {\n if (!assets[id]) {\n throw new Error(`Failed to close asset \"${id}\". Asset not found.`);\n }\n\n assets[id].status = 'closed';\n\n await new Promise<void>((resolve, reject) => {\n const { stream } = assets[id];\n\n stream\n .on('close', () => {\n resolve();\n })\n .on('error', (e) => {\n assets[id].status = 'errored';\n reject(new Error(`Failed to close asset \"${id}\". Asset stream error: ${e.toString()}`));\n })\n .end();\n });\n };\n\n return pass;\n }\n\n createConfigurationReadStream(): MaybePromise<Readable> {\n return this.#createStageReadStream('configuration');\n }\n\n async getMetadata(): Promise<IMetadata | null> {\n const metadata = await this.dispatcher?.dispatchTransferAction<IMetadata>('getMetadata');\n\n return metadata ?? null;\n }\n\n assertValidProtocol(url: URL) {\n const validProtocols = ['https:', 'http:'];\n\n if (!validProtocols.includes(url.protocol)) {\n throw new ProviderValidationError(`Invalid protocol \"${url.protocol}\"`, {\n check: 'url',\n details: {\n protocol: url.protocol,\n validProtocols,\n },\n });\n }\n }\n\n async initTransfer(): Promise<string> {\n const query = this.dispatcher?.dispatchCommand({\n command: 'init',\n });\n\n const res = (await query) as Server.Payload<Server.InitMessage>;\n\n if (!res?.transferID) {\n throw new ProviderTransferError('Init failed, invalid response from the server');\n }\n\n return res.transferID;\n }\n\n #reportInfo(message: string) {\n this.#diagnostics?.report({\n details: {\n createdAt: new Date(),\n message,\n origin: 'remote-source-provider',\n },\n kind: 'info',\n });\n }\n\n async bootstrap(diagnostics?: IDiagnosticReporter): Promise<void> {\n this.#diagnostics = diagnostics;\n const { url, auth } = this.options;\n let ws: WebSocket;\n this.assertValidProtocol(url);\n const wsProtocol = url.protocol === 'https:' ? 'wss:' : 'ws:';\n const wsUrl = `${wsProtocol}//${url.host}${trimTrailingSlash(\n url.pathname\n )}${TRANSFER_PATH}/pull`;\n\n this.#reportInfo('establishing websocket connection');\n // No auth defined, trying public access for transfer\n if (!auth) {\n ws = await connectToWebsocket(wsUrl, undefined, this.#diagnostics);\n }\n\n // Common token auth, this should be the main auth method\n else if (auth.type === 'token') {\n const headers = { Authorization: `Bearer ${auth.token}` };\n ws = await connectToWebsocket(wsUrl, { headers }, this.#diagnostics);\n }\n\n // Invalid auth method provided\n else {\n throw new ProviderValidationError('Auth method not available', {\n check: 'auth.type',\n details: {\n auth: auth.type,\n },\n });\n }\n\n this.#reportInfo('established websocket connection');\n this.ws = ws;\n const { retryMessageOptions } = this.options;\n\n this.#reportInfo('creating dispatcher');\n this.dispatcher = createDispatcher(this.ws, retryMessageOptions, (message: string) =>\n this.#reportInfo(message)\n );\n this.#reportInfo('creating dispatcher');\n\n this.#reportInfo('initialize transfer');\n const transferID = await this.initTransfer();\n this.#reportInfo(`initialized transfer ${transferID}`);\n\n this.dispatcher.setTransferProperties({ id: transferID, kind: 'pull' });\n await this.dispatcher.dispatchTransferAction('bootstrap');\n }\n\n async close() {\n await this.dispatcher?.dispatchTransferAction('close');\n\n await new Promise<void>((resolve) => {\n const { ws } = this;\n\n if (!ws || ws.CLOSED) {\n resolve();\n return;\n }\n\n ws.on('close', () => resolve()).close();\n });\n }\n\n async getSchemas() {\n const schemas =\n await this.dispatcher?.dispatchTransferAction<Utils.String.Dict<Struct.Schema>>('getSchemas');\n\n return schemas ?? null;\n }\n\n async #startStep<T extends Client.TransferPullStep>(step: T) {\n try {\n return await this.dispatcher?.dispatchTransferStep({ action: 'start', step });\n } catch (e) {\n if (e instanceof Error) {\n return e;\n }\n\n if (typeof e === 'string') {\n return new ProviderTransferError(e);\n }\n\n return new ProviderTransferError('Unexpected error');\n }\n }\n\n async #respond(uuid: string) {\n return new Promise((resolve, reject) => {\n this.ws?.send(JSON.stringify({ uuid }), (e) => {\n if (e) {\n reject(e);\n } else {\n resolve(e);\n }\n });\n });\n }\n\n async #endStep<T extends Client.TransferPullStep>(step: T) {\n try {\n await this.dispatcher?.dispatchTransferStep({ action: 'end', step });\n } catch (e) {\n if (e instanceof Error) {\n return e;\n }\n\n if (typeof e === 'string') {\n return new ProviderTransferError(e);\n }\n\n return new ProviderTransferError('Unexpected error');\n }\n\n return null;\n }\n}\n\nexport const createRemoteStrapiSourceProvider = (options: IRemoteStrapiSourceProviderOptions) => {\n return new RemoteStrapiSourceProvider(options);\n};\n"],"names":["RemoteStrapiSourceProvider","createEntitiesReadStream","createStageReadStream","createLinksReadStream","createAssetsReadStream","stream","pass","PassThrough","objectMode","assets","resetTimeout","assetID","timeout","clearTimeout","setTimeout","reportInfo","status","destroy","Error","options","streamTimeout","on","payload","item","action","data","queue","writeAsync","push","Object","prototype","hasOwnProperty","call","asset","length","processQueue","end","id","currentStatus","includes","shift","closeAssetStream","writeChunkToStream","rawBuffer","chunk","Buffer","from","Promise","resolve","reject","e","toString","createConfigurationReadStream","getMetadata","metadata","dispatcher","dispatchTransferAction","assertValidProtocol","url","validProtocols","protocol","ProviderValidationError","check","details","initTransfer","query","dispatchCommand","command","res","transferID","ProviderTransferError","bootstrap","diagnostics","auth","ws","wsProtocol","wsUrl","host","trimTrailingSlash","pathname","TRANSFER_PATH","connectToWebsocket","undefined","type","headers","Authorization","token","retryMessageOptions","createDispatcher","message","setTransferProperties","kind","close","CLOSED","getSchemas","schemas","constructor","name","defaultOptions","write","error","stage","startResult","startStep","processID","listener","raw","parsed","JSON","parse","uuid","once","ended","respond","endStep","castArray","report","createdAt","Date","origin","step","dispatchTransferStep","send","stringify","createRemoteStrapiSourceProvider"],"mappings":";;;;;;;;;;;;;;;;IA8DE,YAEM,iBAAA,8BAAA,CAAA,cAAA,CAAA,EAAA,sBAAA,iBAAA,8BAAA,CAAA,wBAAA,CAAA,EA+SN,WAmFM,iBA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
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var crypto = require('crypto');
|
|
4
|
+
var ws = require('ws');
|
|
5
|
+
var providers = require('../../errors/providers.js');
|
|
6
|
+
|
|
7
|
+
const createDispatcher = (ws, retryMessageOptions = {
|
|
8
|
+
retryMessageMaxRetries: 5,
|
|
9
|
+
retryMessageTimeout: 30000
|
|
10
|
+
}, reportInfo)=>{
|
|
11
|
+
const state = {};
|
|
12
|
+
const dispatch = async (message, options = {})=>{
|
|
13
|
+
if (!ws) {
|
|
14
|
+
throw new Error('No websocket connection found');
|
|
15
|
+
}
|
|
16
|
+
return new Promise((resolve, reject)=>{
|
|
17
|
+
const uuid = crypto.randomUUID();
|
|
18
|
+
const payload = {
|
|
19
|
+
...message,
|
|
20
|
+
uuid
|
|
21
|
+
};
|
|
22
|
+
let numberOfTimesMessageWasSent = 0;
|
|
23
|
+
if (options.attachTransfer) {
|
|
24
|
+
Object.assign(payload, {
|
|
25
|
+
transferID: state.transfer?.id
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
if (message.type === 'command') {
|
|
29
|
+
reportInfo?.(`dispatching message command:${message.command} uuid:${uuid} sent:${numberOfTimesMessageWasSent}`);
|
|
30
|
+
} else if (message.type === 'transfer') {
|
|
31
|
+
const messageToSend = message;
|
|
32
|
+
reportInfo?.(`dispatching message action:${messageToSend.action} ${messageToSend.kind === 'step' ? `step:${messageToSend.step}` : ''} uuid:${uuid} sent:${numberOfTimesMessageWasSent}`);
|
|
33
|
+
}
|
|
34
|
+
const stringifiedPayload = JSON.stringify(payload);
|
|
35
|
+
ws.send(stringifiedPayload, (error)=>{
|
|
36
|
+
if (error) {
|
|
37
|
+
reject(error);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
const { retryMessageMaxRetries, retryMessageTimeout } = retryMessageOptions;
|
|
41
|
+
const sendPeriodically = ()=>{
|
|
42
|
+
if (numberOfTimesMessageWasSent <= retryMessageMaxRetries) {
|
|
43
|
+
numberOfTimesMessageWasSent += 1;
|
|
44
|
+
ws.send(stringifiedPayload, (error)=>{
|
|
45
|
+
if (error) {
|
|
46
|
+
reject(error);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
} else {
|
|
50
|
+
reject(new providers.ProviderError('error', 'Request timed out'));
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const interval = setInterval(sendPeriodically, retryMessageTimeout);
|
|
54
|
+
const onResponse = (raw)=>{
|
|
55
|
+
const response = JSON.parse(raw.toString());
|
|
56
|
+
if (message.type === 'command') {
|
|
57
|
+
reportInfo?.(`received response to message command: ${message.command} uuid: ${uuid} sent: ${numberOfTimesMessageWasSent}`);
|
|
58
|
+
} else if (message.type === 'transfer') {
|
|
59
|
+
const messageToSend = message;
|
|
60
|
+
reportInfo?.(`received response to message action:${messageToSend.action} ${messageToSend.kind === 'step' ? `step:${messageToSend.step}` : ''} uuid:${uuid} sent:${numberOfTimesMessageWasSent}`);
|
|
61
|
+
}
|
|
62
|
+
if (response.uuid === uuid) {
|
|
63
|
+
clearInterval(interval);
|
|
64
|
+
if (response.error) {
|
|
65
|
+
const message = response.error.message;
|
|
66
|
+
const details = response.error.details?.details;
|
|
67
|
+
const step = response.error.details?.step;
|
|
68
|
+
let error = new providers.ProviderError('error', message, details);
|
|
69
|
+
if (step === 'transfer') {
|
|
70
|
+
error = new providers.ProviderTransferError(message, details);
|
|
71
|
+
} else if (step === 'validation') {
|
|
72
|
+
error = new providers.ProviderValidationError(message, details);
|
|
73
|
+
} else if (step === 'initialization') {
|
|
74
|
+
error = new providers.ProviderInitializationError(message);
|
|
75
|
+
}
|
|
76
|
+
return reject(error);
|
|
77
|
+
}
|
|
78
|
+
resolve(response.data ?? null);
|
|
79
|
+
} else {
|
|
80
|
+
ws.once('message', onResponse);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
ws.once('message', onResponse);
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
const dispatchCommand = (payload)=>{
|
|
87
|
+
return dispatch({
|
|
88
|
+
type: 'command',
|
|
89
|
+
...payload
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
const dispatchTransferAction = async (action)=>{
|
|
93
|
+
const payload = {
|
|
94
|
+
type: 'transfer',
|
|
95
|
+
kind: 'action',
|
|
96
|
+
action
|
|
97
|
+
};
|
|
98
|
+
return dispatch(payload, {
|
|
99
|
+
attachTransfer: true
|
|
100
|
+
}) ?? Promise.resolve(null);
|
|
101
|
+
};
|
|
102
|
+
const dispatchTransferStep = async (payload)=>{
|
|
103
|
+
const message = {
|
|
104
|
+
type: 'transfer',
|
|
105
|
+
kind: 'step',
|
|
106
|
+
...payload
|
|
107
|
+
};
|
|
108
|
+
return dispatch(message, {
|
|
109
|
+
attachTransfer: true
|
|
110
|
+
}) ?? Promise.resolve(null);
|
|
111
|
+
};
|
|
112
|
+
const setTransferProperties = (properties)=>{
|
|
113
|
+
state.transfer = {
|
|
114
|
+
...properties
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
return {
|
|
118
|
+
get transferID () {
|
|
119
|
+
return state.transfer?.id;
|
|
120
|
+
},
|
|
121
|
+
get transferKind () {
|
|
122
|
+
return state.transfer?.kind;
|
|
123
|
+
},
|
|
124
|
+
setTransferProperties,
|
|
125
|
+
dispatch,
|
|
126
|
+
dispatchCommand,
|
|
127
|
+
dispatchTransferAction,
|
|
128
|
+
dispatchTransferStep
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
const connectToWebsocket = (address, options, diagnostics)=>{
|
|
132
|
+
return new Promise((resolve, reject)=>{
|
|
133
|
+
const server = new ws.WebSocket(address, options);
|
|
134
|
+
server.once('open', ()=>{
|
|
135
|
+
resolve(server);
|
|
136
|
+
});
|
|
137
|
+
server.on('unexpected-response', (_req, res)=>{
|
|
138
|
+
if (res.statusCode === 401) {
|
|
139
|
+
return reject(new providers.ProviderInitializationError('Failed to initialize the connection: Authentication Error'));
|
|
140
|
+
}
|
|
141
|
+
if (res.statusCode === 403) {
|
|
142
|
+
return reject(new providers.ProviderInitializationError('Failed to initialize the connection: Authorization Error'));
|
|
143
|
+
}
|
|
144
|
+
if (res.statusCode === 404) {
|
|
145
|
+
return reject(new providers.ProviderInitializationError('Failed to initialize the connection: Data transfer is not enabled on the remote host'));
|
|
146
|
+
}
|
|
147
|
+
return reject(new providers.ProviderInitializationError(`Failed to initialize the connection: Unexpected server response ${res.statusCode}`));
|
|
148
|
+
});
|
|
149
|
+
server.on('message', (raw)=>{
|
|
150
|
+
const response = JSON.parse(raw.toString());
|
|
151
|
+
if (response.diagnostic) {
|
|
152
|
+
diagnostics?.report({
|
|
153
|
+
...response.diagnostic
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
server.once('error', (err)=>{
|
|
158
|
+
reject(new providers.ProviderTransferError(err.message, {
|
|
159
|
+
details: {
|
|
160
|
+
error: err.message
|
|
161
|
+
}
|
|
162
|
+
}));
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
};
|
|
166
|
+
const trimTrailingSlash = (input)=>{
|
|
167
|
+
return input.replace(/\/$/, '');
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
exports.connectToWebsocket = connectToWebsocket;
|
|
171
|
+
exports.createDispatcher = createDispatcher;
|
|
172
|
+
exports.trimTrailingSlash = trimTrailingSlash;
|
|
173
|
+
//# sourceMappingURL=utils.js.map
|