@strapi/data-transfer 5.12.1 → 5.12.3
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,310 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
2
|
+
import { WebSocket } from 'ws';
|
|
3
|
+
import { ProviderError, ProviderTransferError } from '../../../errors/providers.mjs';
|
|
4
|
+
import { VALID_TRANSFER_COMMANDS } from './constants.mjs';
|
|
5
|
+
import { createDiagnosticReporter } from '../../../utils/diagnostic.mjs';
|
|
6
|
+
|
|
7
|
+
const transformUpgradeHeader = (header = '')=>{
|
|
8
|
+
return header.split(',').map((s)=>s.trim().toLowerCase());
|
|
9
|
+
};
|
|
10
|
+
let timeouts;
|
|
11
|
+
const hasHttpServer = ()=>{
|
|
12
|
+
// during server restarts, strapi may not have ever been defined at all, so we have to check it first
|
|
13
|
+
return typeof strapi !== 'undefined' && !!strapi?.server?.httpServer;
|
|
14
|
+
};
|
|
15
|
+
// temporarily disable server timeouts while transfer is running
|
|
16
|
+
const disableTimeouts = ()=>{
|
|
17
|
+
if (!hasHttpServer()) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const { httpServer } = strapi.server;
|
|
21
|
+
// save the original timeouts to restore after
|
|
22
|
+
if (!timeouts) {
|
|
23
|
+
timeouts = {
|
|
24
|
+
headersTimeout: httpServer.headersTimeout,
|
|
25
|
+
requestTimeout: httpServer.requestTimeout
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
httpServer.headersTimeout = 0;
|
|
29
|
+
httpServer.requestTimeout = 0;
|
|
30
|
+
strapi.log.info('[Data transfer] Disabling http timeouts');
|
|
31
|
+
};
|
|
32
|
+
const resetTimeouts = ()=>{
|
|
33
|
+
if (!hasHttpServer() || !timeouts) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const { httpServer } = strapi.server;
|
|
37
|
+
strapi.log.info('[Data transfer] Restoring http timeouts');
|
|
38
|
+
httpServer.headersTimeout = timeouts.headersTimeout;
|
|
39
|
+
httpServer.requestTimeout = timeouts.requestTimeout;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Make sure that the upgrade header is a valid websocket one
|
|
43
|
+
*/ const assertValidHeader = (ctx)=>{
|
|
44
|
+
// if it's exactly what we expect, it's fine
|
|
45
|
+
if (ctx.headers.upgrade === 'websocket') {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// check if it could be an array that still includes websocket
|
|
49
|
+
const upgradeHeader = transformUpgradeHeader(ctx.headers.upgrade);
|
|
50
|
+
// Sanitize user input before writing it to our logs
|
|
51
|
+
const logSafeUpgradeHeader = JSON.stringify(ctx.headers.upgrade)?.replace(/[^a-z0-9\s.,|]/gi, '').substring(0, 50);
|
|
52
|
+
if (!upgradeHeader.includes('websocket')) {
|
|
53
|
+
throw new Error(`Transfer Upgrade header expected 'websocket', found '${logSafeUpgradeHeader}'. Please ensure that your server or proxy is not modifying the Upgrade header.`);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* If there's more than expected but it still includes websocket, in theory it could still work
|
|
57
|
+
* and could be necessary for their certain configurations, so we'll allow it to proceed but
|
|
58
|
+
* log the unexpected behaviour in case it helps debug an issue
|
|
59
|
+
* */ strapi.log.info(`Transfer Upgrade header expected only 'websocket', found unexpected values: ${logSafeUpgradeHeader}`);
|
|
60
|
+
};
|
|
61
|
+
const isDataTransferMessage = (message)=>{
|
|
62
|
+
if (!message || typeof message !== 'object') {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
const { uuid, type } = message;
|
|
66
|
+
if (typeof uuid !== 'string' || typeof type !== 'string') {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
if (![
|
|
70
|
+
'command',
|
|
71
|
+
'transfer'
|
|
72
|
+
].includes(type)) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
return true;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Handle the upgrade to ws connection
|
|
79
|
+
*/ const handleWSUpgrade = (wss, ctx, callback)=>{
|
|
80
|
+
assertValidHeader(ctx);
|
|
81
|
+
wss.handleUpgrade(ctx.req, ctx.request.socket, Buffer.alloc(0), (client, request)=>{
|
|
82
|
+
if (!client) {
|
|
83
|
+
// If the WebSocket upgrade failed, destroy the socket to avoid hanging
|
|
84
|
+
ctx.request.socket.destroy();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
disableTimeouts();
|
|
88
|
+
strapi.db.lifecycles.disable();
|
|
89
|
+
strapi.log.info('[Data transfer] Disabling lifecycle hooks');
|
|
90
|
+
// Create a connection between the client & the server
|
|
91
|
+
wss.emit('connection', client, ctx.req);
|
|
92
|
+
// Invoke the ws callback
|
|
93
|
+
callback(client, request);
|
|
94
|
+
});
|
|
95
|
+
ctx.respond = false;
|
|
96
|
+
};
|
|
97
|
+
// Protocol related functions
|
|
98
|
+
const handlerControllerFactory = (implementation)=>(options)=>{
|
|
99
|
+
const { verify, server: serverOptions } = options ?? {};
|
|
100
|
+
const wss = new WebSocket.Server({
|
|
101
|
+
...serverOptions,
|
|
102
|
+
noServer: true
|
|
103
|
+
});
|
|
104
|
+
return async (ctx)=>{
|
|
105
|
+
const cb = (ws)=>{
|
|
106
|
+
const state = {
|
|
107
|
+
id: undefined
|
|
108
|
+
};
|
|
109
|
+
const messageUUIDs = new Set();
|
|
110
|
+
const diagnostics = createDiagnosticReporter();
|
|
111
|
+
const cannotRespondHandler = (err)=>{
|
|
112
|
+
strapi?.log?.error('[Data transfer] Cannot send error response to client, closing connection');
|
|
113
|
+
strapi?.log?.error(err);
|
|
114
|
+
try {
|
|
115
|
+
ws.terminate();
|
|
116
|
+
ctx.req.socket.destroy();
|
|
117
|
+
} catch (err) {
|
|
118
|
+
strapi?.log?.error('[Data transfer] Failed to close socket on error');
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
const prototype = {
|
|
122
|
+
// Transfer ID
|
|
123
|
+
get transferID () {
|
|
124
|
+
return state.id;
|
|
125
|
+
},
|
|
126
|
+
set transferID (id){
|
|
127
|
+
state.id = id;
|
|
128
|
+
},
|
|
129
|
+
// Started at
|
|
130
|
+
get startedAt () {
|
|
131
|
+
return state.startedAt;
|
|
132
|
+
},
|
|
133
|
+
set startedAt (timestamp){
|
|
134
|
+
state.startedAt = timestamp;
|
|
135
|
+
},
|
|
136
|
+
get response () {
|
|
137
|
+
return state.response;
|
|
138
|
+
},
|
|
139
|
+
set response (response){
|
|
140
|
+
state.response = response;
|
|
141
|
+
},
|
|
142
|
+
get diagnostics () {
|
|
143
|
+
return diagnostics;
|
|
144
|
+
},
|
|
145
|
+
addUUID (uuid) {
|
|
146
|
+
messageUUIDs.add(uuid);
|
|
147
|
+
},
|
|
148
|
+
hasUUID (uuid) {
|
|
149
|
+
return messageUUIDs.has(uuid);
|
|
150
|
+
},
|
|
151
|
+
isTransferStarted () {
|
|
152
|
+
return this.transferID !== undefined && this.startedAt !== undefined;
|
|
153
|
+
},
|
|
154
|
+
assertValidTransfer () {
|
|
155
|
+
const isStarted = this.isTransferStarted();
|
|
156
|
+
if (!isStarted) {
|
|
157
|
+
throw new Error('Invalid Transfer Process');
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
assertValidTransferCommand (command) {
|
|
161
|
+
const isDefined = typeof this[command] === 'function';
|
|
162
|
+
const isValidTransferCommand = VALID_TRANSFER_COMMANDS.includes(command);
|
|
163
|
+
if (!isDefined || !isValidTransferCommand) {
|
|
164
|
+
throw new Error('Invalid transfer command');
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
async respond (uuid, e, data) {
|
|
168
|
+
let details = {};
|
|
169
|
+
return new Promise((resolve, reject)=>{
|
|
170
|
+
if (!uuid && !e) {
|
|
171
|
+
reject(new Error('Missing uuid for this message'));
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
this.response = {
|
|
175
|
+
uuid,
|
|
176
|
+
data,
|
|
177
|
+
e
|
|
178
|
+
};
|
|
179
|
+
if (e instanceof ProviderError) {
|
|
180
|
+
details = e.details;
|
|
181
|
+
}
|
|
182
|
+
const payload = JSON.stringify({
|
|
183
|
+
uuid,
|
|
184
|
+
data: data ?? null,
|
|
185
|
+
error: e ? {
|
|
186
|
+
code: e?.name ?? 'ERR',
|
|
187
|
+
message: e?.message,
|
|
188
|
+
details
|
|
189
|
+
} : null
|
|
190
|
+
});
|
|
191
|
+
this.send(payload, (error)=>error ? reject(error) : resolve());
|
|
192
|
+
});
|
|
193
|
+
},
|
|
194
|
+
send (message, cb) {
|
|
195
|
+
ws.send(message, cb);
|
|
196
|
+
},
|
|
197
|
+
confirm (message) {
|
|
198
|
+
return new Promise((resolve, reject)=>{
|
|
199
|
+
const uuid = randomUUID();
|
|
200
|
+
const payload = JSON.stringify({
|
|
201
|
+
uuid,
|
|
202
|
+
data: message
|
|
203
|
+
});
|
|
204
|
+
this.send(payload, (error)=>{
|
|
205
|
+
if (error) {
|
|
206
|
+
reject(error);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
const onResponse = (raw)=>{
|
|
210
|
+
const response1 = JSON.parse(raw.toString());
|
|
211
|
+
if (response1.uuid === uuid) {
|
|
212
|
+
resolve(response1.data ?? null);
|
|
213
|
+
} else {
|
|
214
|
+
ws.once('message', onResponse);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
ws.once('message', onResponse);
|
|
218
|
+
});
|
|
219
|
+
},
|
|
220
|
+
async executeAndRespond (uuid, fn) {
|
|
221
|
+
try {
|
|
222
|
+
const response1 = await fn();
|
|
223
|
+
await this.respond(uuid, null, response1);
|
|
224
|
+
} catch (e) {
|
|
225
|
+
if (e instanceof Error) {
|
|
226
|
+
await this.respond(uuid, e).catch(cannotRespondHandler);
|
|
227
|
+
} else if (typeof e === 'string') {
|
|
228
|
+
await this.respond(uuid, new ProviderTransferError(e)).catch(cannotRespondHandler);
|
|
229
|
+
} else {
|
|
230
|
+
await this.respond(uuid, new ProviderTransferError('Unexpected error', {
|
|
231
|
+
error: e
|
|
232
|
+
})).catch(cannotRespondHandler);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
cleanup () {
|
|
237
|
+
this.transferID = undefined;
|
|
238
|
+
this.startedAt = undefined;
|
|
239
|
+
this.response = undefined;
|
|
240
|
+
},
|
|
241
|
+
teardown () {
|
|
242
|
+
this.cleanup();
|
|
243
|
+
},
|
|
244
|
+
verifyAuth (scope) {
|
|
245
|
+
return verify(ctx, scope);
|
|
246
|
+
},
|
|
247
|
+
// Transfer commands
|
|
248
|
+
init () {},
|
|
249
|
+
end () {},
|
|
250
|
+
status () {},
|
|
251
|
+
// Default prototype implementation for events
|
|
252
|
+
onMessage () {},
|
|
253
|
+
onError () {},
|
|
254
|
+
onClose () {},
|
|
255
|
+
onInfo () {},
|
|
256
|
+
onWarning () {}
|
|
257
|
+
};
|
|
258
|
+
const handler = Object.assign(Object.create(prototype), implementation(prototype));
|
|
259
|
+
// Bind ws events to handler methods
|
|
260
|
+
ws.on('close', async (...args)=>{
|
|
261
|
+
try {
|
|
262
|
+
await handler.onClose(...args);
|
|
263
|
+
} catch (err) {
|
|
264
|
+
strapi?.log?.error('[Data transfer] Uncaught error closing connection');
|
|
265
|
+
strapi?.log?.error(err);
|
|
266
|
+
cannotRespondHandler(err);
|
|
267
|
+
} finally{
|
|
268
|
+
resetTimeouts();
|
|
269
|
+
strapi.db.lifecycles.enable();
|
|
270
|
+
strapi.log.info('[Data transfer] Restoring lifecycle hooks');
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
ws.on('error', async (...args)=>{
|
|
274
|
+
try {
|
|
275
|
+
await handler.onError(...args);
|
|
276
|
+
} catch (err) {
|
|
277
|
+
strapi?.log?.error('[Data transfer] Uncaught error in error handling');
|
|
278
|
+
strapi?.log?.error(err);
|
|
279
|
+
cannotRespondHandler(err);
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
ws.on('message', async (...args)=>{
|
|
283
|
+
try {
|
|
284
|
+
await handler.onMessage(...args);
|
|
285
|
+
} catch (err) {
|
|
286
|
+
strapi?.log?.error('[Data transfer] Uncaught error in message handling');
|
|
287
|
+
strapi?.log?.error(err);
|
|
288
|
+
cannotRespondHandler(err);
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
diagnostics.onDiagnostic((diagnostic)=>{
|
|
292
|
+
const uuid = randomUUID();
|
|
293
|
+
const payload = JSON.stringify({
|
|
294
|
+
diagnostic,
|
|
295
|
+
uuid
|
|
296
|
+
});
|
|
297
|
+
handler.send(payload);
|
|
298
|
+
});
|
|
299
|
+
};
|
|
300
|
+
try {
|
|
301
|
+
handleWSUpgrade(wss, ctx, cb);
|
|
302
|
+
} catch (err) {
|
|
303
|
+
strapi?.log?.error('[Data transfer] Error in websocket upgrade request');
|
|
304
|
+
strapi?.log?.error(err);
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
export { assertValidHeader, handleWSUpgrade, handlerControllerFactory, isDataTransferMessage, transformUpgradeHeader };
|
|
310
|
+
//# sourceMappingURL=utils.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.mjs","sources":["../../../../src/strapi/remote/handlers/utils.ts"],"sourcesContent":["import type { IncomingMessage } from 'node:http';\nimport { randomUUID } from 'crypto';\nimport type { Context } from 'koa';\nimport type { RawData, ServerOptions } from 'ws';\nimport { WebSocket, WebSocketServer } from 'ws';\n\nimport type { Handler, TransferState } from './abstract';\nimport type { Protocol } from '../../../../types';\nimport { ProviderError, ProviderTransferError } from '../../../errors/providers';\nimport { VALID_TRANSFER_COMMANDS, ValidTransferCommand } from './constants';\nimport { TransferMethod } from '../constants';\nimport { createDiagnosticReporter } from '../../../utils/diagnostic';\n\ntype WSCallback = (client: WebSocket, request: IncomingMessage) => void;\n\nexport interface HandlerOptions {\n verify: (ctx: Context, scope?: TransferMethod) => Promise<void>;\n server?: ServerOptions;\n}\n\nexport const transformUpgradeHeader = (header = '') => {\n return header.split(',').map((s) => s.trim().toLowerCase());\n};\n\nlet timeouts: Record<string, number> | undefined;\n\nconst hasHttpServer = () => {\n // during server restarts, strapi may not have ever been defined at all, so we have to check it first\n return typeof strapi !== 'undefined' && !!strapi?.server?.httpServer;\n};\n\n// temporarily disable server timeouts while transfer is running\nconst disableTimeouts = () => {\n if (!hasHttpServer()) {\n return;\n }\n\n const { httpServer } = strapi.server;\n\n // save the original timeouts to restore after\n if (!timeouts) {\n timeouts = {\n headersTimeout: httpServer.headersTimeout,\n requestTimeout: httpServer.requestTimeout,\n };\n }\n\n httpServer.headersTimeout = 0;\n httpServer.requestTimeout = 0;\n\n strapi.log.info('[Data transfer] Disabling http timeouts');\n};\nconst resetTimeouts = () => {\n if (!hasHttpServer() || !timeouts) {\n return;\n }\n\n const { httpServer } = strapi.server;\n\n strapi.log.info('[Data transfer] Restoring http timeouts');\n httpServer.headersTimeout = timeouts.headersTimeout;\n httpServer.requestTimeout = timeouts.requestTimeout;\n};\n/**\n * Make sure that the upgrade header is a valid websocket one\n */\nexport const assertValidHeader = (ctx: Context) => {\n // if it's exactly what we expect, it's fine\n if (ctx.headers.upgrade === 'websocket') {\n return;\n }\n\n // check if it could be an array that still includes websocket\n const upgradeHeader = transformUpgradeHeader(ctx.headers.upgrade);\n\n // Sanitize user input before writing it to our logs\n const logSafeUpgradeHeader = JSON.stringify(ctx.headers.upgrade)\n ?.replace(/[^a-z0-9\\s.,|]/gi, '')\n .substring(0, 50);\n\n if (!upgradeHeader.includes('websocket')) {\n throw new Error(\n `Transfer Upgrade header expected 'websocket', found '${logSafeUpgradeHeader}'. Please ensure that your server or proxy is not modifying the Upgrade header.`\n );\n }\n\n /**\n * If there's more than expected but it still includes websocket, in theory it could still work\n * and could be necessary for their certain configurations, so we'll allow it to proceed but\n * log the unexpected behaviour in case it helps debug an issue\n * */\n strapi.log.info(\n `Transfer Upgrade header expected only 'websocket', found unexpected values: ${logSafeUpgradeHeader}`\n );\n};\n\nexport const isDataTransferMessage = (message: unknown): message is Protocol.Client.Message => {\n if (!message || typeof message !== 'object') {\n return false;\n }\n\n const { uuid, type } = message as Record<string, unknown>;\n\n if (typeof uuid !== 'string' || typeof type !== 'string') {\n return false;\n }\n\n if (!['command', 'transfer'].includes(type)) {\n return false;\n }\n\n return true;\n};\n\n/**\n * Handle the upgrade to ws connection\n */\nexport const handleWSUpgrade = (wss: WebSocketServer, ctx: Context, callback: WSCallback) => {\n assertValidHeader(ctx);\n\n wss.handleUpgrade(ctx.req, ctx.request.socket, Buffer.alloc(0), (client, request) => {\n if (!client) {\n // If the WebSocket upgrade failed, destroy the socket to avoid hanging\n ctx.request.socket.destroy();\n return;\n }\n\n disableTimeouts();\n strapi.db.lifecycles.disable();\n strapi.log.info('[Data transfer] Disabling lifecycle hooks');\n\n // Create a connection between the client & the server\n wss.emit('connection', client, ctx.req);\n\n // Invoke the ws callback\n callback(client, request);\n });\n\n ctx.respond = false;\n};\n\n// Protocol related functions\n\nexport const handlerControllerFactory =\n <T extends Partial<Handler>>(implementation: (proto: Handler) => T) =>\n (options: HandlerOptions) => {\n const { verify, server: serverOptions } = options ?? {};\n\n const wss = new WebSocket.Server({ ...serverOptions, noServer: true });\n\n return async (ctx: Context) => {\n const cb: WSCallback = (ws) => {\n const state: TransferState = { id: undefined };\n const messageUUIDs = new Set<string>();\n const diagnostics = createDiagnosticReporter();\n\n const cannotRespondHandler = (err: unknown) => {\n strapi?.log?.error(\n '[Data transfer] Cannot send error response to client, closing connection'\n );\n strapi?.log?.error(err);\n try {\n ws.terminate();\n ctx.req.socket.destroy();\n } catch (err) {\n strapi?.log?.error('[Data transfer] Failed to close socket on error');\n }\n };\n\n const prototype: Handler = {\n // Transfer ID\n get transferID() {\n return state.id;\n },\n\n set transferID(id) {\n state.id = id;\n },\n\n // Started at\n get startedAt() {\n return state.startedAt;\n },\n\n set startedAt(timestamp) {\n state.startedAt = timestamp;\n },\n\n get response() {\n return state.response;\n },\n\n set response(response) {\n state.response = response;\n },\n\n get diagnostics() {\n return diagnostics;\n },\n\n addUUID(uuid) {\n messageUUIDs.add(uuid);\n },\n\n hasUUID(uuid) {\n return messageUUIDs.has(uuid);\n },\n\n isTransferStarted() {\n return this.transferID !== undefined && this.startedAt !== undefined;\n },\n\n assertValidTransfer() {\n const isStarted = this.isTransferStarted();\n\n if (!isStarted) {\n throw new Error('Invalid Transfer Process');\n }\n },\n\n assertValidTransferCommand(command: ValidTransferCommand) {\n const isDefined = typeof this[command] === 'function';\n const isValidTransferCommand = VALID_TRANSFER_COMMANDS.includes(command);\n\n if (!isDefined || !isValidTransferCommand) {\n throw new Error('Invalid transfer command');\n }\n },\n\n async respond(uuid, e, data) {\n let details = {};\n return new Promise<void>((resolve, reject) => {\n if (!uuid && !e) {\n reject(new Error('Missing uuid for this message'));\n return;\n }\n\n this.response = {\n uuid,\n data,\n e,\n };\n\n if (e instanceof ProviderError) {\n details = e.details;\n }\n\n const payload = JSON.stringify({\n uuid,\n data: data ?? null,\n error: e\n ? {\n code: e?.name ?? 'ERR',\n message: e?.message,\n details,\n }\n : null,\n });\n\n this.send(payload, (error) => (error ? reject(error) : resolve()));\n });\n },\n\n send(message, cb) {\n ws.send(message, cb);\n },\n confirm(message) {\n return new Promise((resolve, reject) => {\n const uuid = randomUUID();\n\n const payload = JSON.stringify({ uuid, data: message });\n\n this.send(payload, (error) => {\n if (error) {\n reject(error);\n }\n });\n\n const onResponse = (raw: RawData) => {\n const response = JSON.parse(raw.toString());\n\n if (response.uuid === uuid) {\n resolve(response.data ?? null);\n } else {\n ws.once('message', onResponse);\n }\n };\n\n ws.once('message', onResponse);\n });\n },\n\n async executeAndRespond(uuid, fn) {\n try {\n const response = await fn();\n await this.respond(uuid, null, response);\n } catch (e) {\n if (e instanceof Error) {\n await this.respond(uuid, e).catch(cannotRespondHandler);\n } else if (typeof e === 'string') {\n await this.respond(uuid, new ProviderTransferError(e)).catch(cannotRespondHandler);\n } else {\n await this.respond(\n uuid,\n new ProviderTransferError('Unexpected error', {\n error: e,\n })\n ).catch(cannotRespondHandler);\n }\n }\n },\n\n cleanup() {\n this.transferID = undefined;\n this.startedAt = undefined;\n this.response = undefined;\n },\n\n teardown() {\n this.cleanup();\n },\n\n verifyAuth(scope?: TransferMethod) {\n return verify(ctx, scope);\n },\n\n // Transfer commands\n init() {},\n end() {},\n status() {},\n\n // Default prototype implementation for events\n onMessage() {},\n onError() {},\n onClose() {},\n onInfo() {},\n onWarning() {},\n };\n\n const handler: Handler = Object.assign(Object.create(prototype), implementation(prototype));\n\n // Bind ws events to handler methods\n ws.on('close', async (...args) => {\n try {\n await handler.onClose(...args);\n } catch (err) {\n strapi?.log?.error('[Data transfer] Uncaught error closing connection');\n strapi?.log?.error(err);\n cannotRespondHandler(err);\n } finally {\n resetTimeouts();\n strapi.db.lifecycles.enable();\n strapi.log.info('[Data transfer] Restoring lifecycle hooks');\n }\n });\n ws.on('error', async (...args) => {\n try {\n await handler.onError(...args);\n } catch (err) {\n strapi?.log?.error('[Data transfer] Uncaught error in error handling');\n strapi?.log?.error(err);\n cannotRespondHandler(err);\n }\n });\n ws.on('message', async (...args) => {\n try {\n await handler.onMessage(...args);\n } catch (err) {\n strapi?.log?.error('[Data transfer] Uncaught error in message handling');\n strapi?.log?.error(err);\n cannotRespondHandler(err);\n }\n });\n\n diagnostics.onDiagnostic((diagnostic) => {\n const uuid = randomUUID();\n const payload = JSON.stringify({\n diagnostic,\n uuid,\n });\n\n handler.send(payload);\n });\n };\n\n try {\n handleWSUpgrade(wss, ctx, cb);\n } catch (err) {\n strapi?.log?.error('[Data transfer] Error in websocket upgrade request');\n strapi?.log?.error(err);\n }\n };\n };\n"],"names":["transformUpgradeHeader","header","split","map","s","trim","toLowerCase","timeouts","hasHttpServer","strapi","server","httpServer","disableTimeouts","headersTimeout","requestTimeout","log","info","resetTimeouts","assertValidHeader","ctx","headers","upgrade","upgradeHeader","logSafeUpgradeHeader","JSON","stringify","replace","substring","includes","Error","isDataTransferMessage","message","uuid","type","handleWSUpgrade","wss","callback","handleUpgrade","req","request","socket","Buffer","alloc","client","destroy","db","lifecycles","disable","emit","respond","handlerControllerFactory","implementation","options","verify","serverOptions","WebSocket","Server","noServer","cb","ws","state","id","undefined","messageUUIDs","Set","diagnostics","createDiagnosticReporter","cannotRespondHandler","err","error","terminate","prototype","transferID","startedAt","timestamp","response","addUUID","add","hasUUID","has","isTransferStarted","assertValidTransfer","isStarted","assertValidTransferCommand","command","isDefined","isValidTransferCommand","VALID_TRANSFER_COMMANDS","e","data","details","Promise","resolve","reject","ProviderError","payload","code","name","send","confirm","randomUUID","onResponse","raw","parse","toString","once","executeAndRespond","fn","catch","ProviderTransferError","cleanup","teardown","verifyAuth","scope","init","end","status","onMessage","onError","onClose","onInfo","onWarning","handler","Object","assign","create","on","args","enable","onDiagnostic","diagnostic"],"mappings":";;;;;;AAoBaA,MAAAA,sBAAAA,GAAyB,CAACC,MAAAA,GAAS,EAAE,GAAA;IAChD,OAAOA,MAAAA,CAAOC,KAAK,CAAC,GAAKC,CAAAA,CAAAA,GAAG,CAAC,CAACC,CAAMA,GAAAA,CAAAA,CAAEC,IAAI,EAAA,CAAGC,WAAW,EAAA,CAAA;AAC1D;AAEA,IAAIC,QAAAA;AAEJ,MAAMC,aAAgB,GAAA,IAAA;;AAEpB,IAAA,OAAO,OAAOC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],"names":[],"mappings":";;;;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('lodash');
|
|
4
|
+
var fp = require('lodash/fp');
|
|
5
|
+
var utils = require('@strapi/utils');
|
|
6
|
+
|
|
7
|
+
const isDialectMySQL = ()=>strapi.db?.dialect.client === 'mysql';
|
|
8
|
+
function omitComponentData(contentType, data) {
|
|
9
|
+
const { attributes } = contentType;
|
|
10
|
+
const componentAttributes = Object.keys(attributes).filter((attributeName)=>utils.contentTypes.isComponentAttribute(attributes[attributeName]));
|
|
11
|
+
return fp.omit(componentAttributes, data);
|
|
12
|
+
}
|
|
13
|
+
// NOTE: we could generalize the logic to allow CRUD of relation directly in the DB layer
|
|
14
|
+
const createComponents = async (uid, data)=>{
|
|
15
|
+
const { attributes = {} } = strapi.getModel(uid);
|
|
16
|
+
const componentBody = {};
|
|
17
|
+
const attributeNames = Object.keys(attributes);
|
|
18
|
+
for (const attributeName of attributeNames){
|
|
19
|
+
const attribute = attributes[attributeName];
|
|
20
|
+
if (!fp.has(attributeName, data) || !utils.contentTypes.isComponentAttribute(attribute)) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (attribute.type === 'component') {
|
|
24
|
+
const { component: componentUID, repeatable = false } = attribute;
|
|
25
|
+
const componentValue = data[attributeName];
|
|
26
|
+
if (componentValue === null) {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (repeatable === true) {
|
|
30
|
+
if (!Array.isArray(componentValue)) {
|
|
31
|
+
throw new Error('Expected an array to create repeatable component');
|
|
32
|
+
}
|
|
33
|
+
// MySQL/MariaDB can cause deadlocks here if concurrency higher than 1
|
|
34
|
+
const components = await utils.async.map(componentValue, (value)=>createComponent(componentUID, value), {
|
|
35
|
+
concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity
|
|
36
|
+
});
|
|
37
|
+
componentBody[attributeName] = components.map(({ id })=>{
|
|
38
|
+
return {
|
|
39
|
+
id,
|
|
40
|
+
__pivot: {
|
|
41
|
+
field: attributeName,
|
|
42
|
+
component_type: componentUID
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
} else {
|
|
47
|
+
const component = await createComponent(componentUID, componentValue);
|
|
48
|
+
componentBody[attributeName] = {
|
|
49
|
+
id: component.id,
|
|
50
|
+
__pivot: {
|
|
51
|
+
field: attributeName,
|
|
52
|
+
component_type: componentUID
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (attribute.type === 'dynamiczone') {
|
|
59
|
+
const dynamiczoneValues = data[attributeName];
|
|
60
|
+
if (!Array.isArray(dynamiczoneValues)) {
|
|
61
|
+
throw new Error('Expected an array to create repeatable component');
|
|
62
|
+
}
|
|
63
|
+
const createDynamicZoneComponents = async (value)=>{
|
|
64
|
+
const { id } = await createComponent(value.__component, value);
|
|
65
|
+
return {
|
|
66
|
+
id,
|
|
67
|
+
__component: value.__component,
|
|
68
|
+
__pivot: {
|
|
69
|
+
field: attributeName
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
// MySQL/MariaDB can cause deadlocks here if concurrency higher than 1
|
|
74
|
+
componentBody[attributeName] = await utils.async.map(dynamiczoneValues, createDynamicZoneComponents, {
|
|
75
|
+
concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity
|
|
76
|
+
});
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return componentBody;
|
|
81
|
+
};
|
|
82
|
+
const getComponents = async (uid, entity)=>{
|
|
83
|
+
const componentAttributes = utils.contentTypes.getComponentAttributes(strapi.getModel(uid));
|
|
84
|
+
if (_.isEmpty(componentAttributes)) {
|
|
85
|
+
return {};
|
|
86
|
+
}
|
|
87
|
+
return strapi.db.query(uid).load(entity, componentAttributes);
|
|
88
|
+
};
|
|
89
|
+
const deleteComponents = async (uid, entityToDelete, { loadComponents = true } = {})=>{
|
|
90
|
+
const { attributes = {} } = strapi.getModel(uid);
|
|
91
|
+
const attributeNames = Object.keys(attributes);
|
|
92
|
+
for (const attributeName of attributeNames){
|
|
93
|
+
const attribute = attributes[attributeName];
|
|
94
|
+
if (attribute.type === 'component' || attribute.type === 'dynamiczone') {
|
|
95
|
+
let value;
|
|
96
|
+
if (loadComponents) {
|
|
97
|
+
value = await strapi.db.query(uid).load(entityToDelete, attributeName);
|
|
98
|
+
} else {
|
|
99
|
+
value = entityToDelete[attributeName];
|
|
100
|
+
}
|
|
101
|
+
if (!value) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (attribute.type === 'component') {
|
|
105
|
+
const { component: componentUID } = attribute;
|
|
106
|
+
// MySQL/MariaDB can cause deadlocks here if concurrency higher than 1
|
|
107
|
+
await utils.async.map(_.castArray(value), (subValue)=>deleteComponent(componentUID, subValue), {
|
|
108
|
+
concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity
|
|
109
|
+
});
|
|
110
|
+
} else {
|
|
111
|
+
// delete dynamic zone components
|
|
112
|
+
// MySQL/MariaDB can cause deadlocks here if concurrency higher than 1
|
|
113
|
+
await utils.async.map(_.castArray(value), (subValue)=>deleteComponent(subValue.__component, subValue), {
|
|
114
|
+
concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
/** *************************
|
|
122
|
+
Component queries
|
|
123
|
+
************************** */ // components can have nested compos so this must be recursive
|
|
124
|
+
const createComponent = async (uid, data)=>{
|
|
125
|
+
const model = strapi.getModel(uid);
|
|
126
|
+
const componentData = await createComponents(uid, data);
|
|
127
|
+
const transform = fp.pipe(// Make sure we don't save the component with a pre-defined ID
|
|
128
|
+
fp.omit('id'), // Remove the component data from the original data object ...
|
|
129
|
+
(payload)=>omitComponentData(model, payload), // ... and assign the newly created component instead
|
|
130
|
+
fp.assign(componentData));
|
|
131
|
+
return strapi.db.query(uid).create({
|
|
132
|
+
data: transform(data)
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
const deleteComponent = async (uid, componentToDelete)=>{
|
|
136
|
+
await deleteComponents(uid, componentToDelete);
|
|
137
|
+
await strapi.db.query(uid).delete({
|
|
138
|
+
where: {
|
|
139
|
+
id: componentToDelete.id
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* Resolve the component UID of an entity's attribute based
|
|
145
|
+
* on a given path (components & dynamic zones only)
|
|
146
|
+
*/ const resolveComponentUID = ({ paths, strapi: strapi1, data, contentType })=>{
|
|
147
|
+
let value = data;
|
|
148
|
+
let cType = contentType;
|
|
149
|
+
for (const path of paths){
|
|
150
|
+
value = fp.get(path, value);
|
|
151
|
+
// Needed when the value of cType should be computed
|
|
152
|
+
// based on the next value (eg: dynamic zones)
|
|
153
|
+
if (typeof cType === 'function') {
|
|
154
|
+
cType = cType(value);
|
|
155
|
+
}
|
|
156
|
+
if (path in cType.attributes) {
|
|
157
|
+
const attribute = cType.attributes[path];
|
|
158
|
+
if (attribute.type === 'component') {
|
|
159
|
+
cType = strapi1.getModel(attribute.component);
|
|
160
|
+
}
|
|
161
|
+
if (attribute.type === 'dynamiczone') {
|
|
162
|
+
cType = ({ __component })=>strapi1.getModel(__component);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if ('uid' in cType) {
|
|
167
|
+
return cType.uid;
|
|
168
|
+
}
|
|
169
|
+
return undefined;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
exports.createComponents = createComponents;
|
|
173
|
+
exports.deleteComponent = deleteComponent;
|
|
174
|
+
exports.deleteComponents = deleteComponents;
|
|
175
|
+
exports.getComponents = getComponents;
|
|
176
|
+
exports.omitComponentData = omitComponentData;
|
|
177
|
+
exports.resolveComponentUID = resolveComponentUID;
|
|
178
|
+
//# sourceMappingURL=components.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"components.js","sources":["../../src/utils/components.ts"],"sourcesContent":["import _ from 'lodash';\nimport { get, has, omit, pipe, assign } from 'lodash/fp';\n\nimport { contentTypes as contentTypesUtils, async, errors } from '@strapi/utils';\nimport type { Modules, UID, Data, Utils, Schema, Core } from '@strapi/types';\n\ntype LoadedComponents<TUID extends UID.Schema> = Data.Entity<\n TUID,\n Schema.AttributeNamesByType<TUID, 'component' | 'dynamiczone'>\n>;\n\ntype ComponentValue = Schema.Attribute.Value<\n Schema.Attribute.Component<UID.Component, false> | Schema.Attribute.Component<UID.Component, true>\n>;\n\ntype ComponentBody = {\n [key: string]: Schema.Attribute.Value<\n | Schema.Attribute.Component<UID.Component, false>\n | Schema.Attribute.Component<UID.Component, true>\n | Schema.Attribute.DynamicZone\n >;\n};\n\nconst isDialectMySQL = () => strapi.db?.dialect.client === 'mysql';\n\nfunction omitComponentData(\n contentType: Schema.ContentType,\n data: Modules.EntityService.Params.Data.Input<Schema.ContentType['uid']>\n): Partial<Modules.EntityService.Params.Data.Input<Schema.ContentType['uid']>>;\nfunction omitComponentData(\n contentType: Schema.Component,\n data: Modules.EntityService.Params.Data.Input<Schema.Component['uid']>\n): Partial<Modules.EntityService.Params.Data.Input<Schema.Component['uid']>>;\nfunction omitComponentData(\n contentType: Schema.ContentType | Schema.Component,\n data: Modules.EntityService.Params.Data.Input<Schema.ContentType['uid'] | Schema.Component['uid']>\n): Partial<\n Modules.EntityService.Params.Data.Input<Schema.ContentType['uid'] | Schema.Component['uid']>\n> {\n const { attributes } = contentType;\n const componentAttributes = Object.keys(attributes).filter((attributeName) =>\n contentTypesUtils.isComponentAttribute(attributes[attributeName])\n );\n\n return omit(componentAttributes, data);\n}\n\n// NOTE: we could generalize the logic to allow CRUD of relation directly in the DB layer\nconst createComponents = async <\n TUID extends UID.Schema,\n TData extends Modules.EntityService.Params.Data.Input<TUID>,\n>(\n uid: TUID,\n data: TData\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const componentBody: ComponentBody = {};\n\n const attributeNames = Object.keys(attributes);\n\n for (const attributeName of attributeNames) {\n const attribute = attributes[attributeName];\n\n if (!has(attributeName, data) || !contentTypesUtils.isComponentAttribute(attribute)) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID, repeatable = false } = attribute;\n\n const componentValue = data[attributeName as keyof TData];\n\n if (componentValue === null) {\n continue;\n }\n\n if (repeatable === true) {\n if (!Array.isArray(componentValue)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n const components = (await async.map(\n componentValue,\n (value: any) => createComponent(componentUID, value),\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n )) as Schema.Attribute.Value<Schema.Attribute.Component<UID.Component, true>>;\n\n componentBody[attributeName] = components.map(({ id }) => {\n return {\n id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n });\n } else {\n const component = await createComponent(\n componentUID,\n componentValue as Modules.EntityService.Params.Data.Input<UID.Component>\n );\n componentBody[attributeName] = {\n id: component.id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n }\n\n continue;\n }\n\n if (attribute.type === 'dynamiczone') {\n const dynamiczoneValues = data[\n attributeName as keyof TData\n ] as Modules.EntityService.Params.Attribute.GetValue<Schema.Attribute.DynamicZone>;\n\n if (!Array.isArray(dynamiczoneValues)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n const createDynamicZoneComponents = async (\n value: Utils.Array.Values<typeof dynamiczoneValues>\n ) => {\n const { id } = await createComponent(value.__component, value);\n return {\n id,\n __component: value.__component,\n __pivot: {\n field: attributeName,\n },\n };\n };\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n componentBody[attributeName] = await async.map(\n dynamiczoneValues,\n createDynamicZoneComponents,\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n );\n\n continue;\n }\n }\n\n return componentBody;\n};\n\nconst getComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n entity: { id: Modules.EntityService.Params.Attribute.ID }\n): Promise<LoadedComponents<TUID>> => {\n const componentAttributes = contentTypesUtils.getComponentAttributes(strapi.getModel(uid));\n\n if (_.isEmpty(componentAttributes)) {\n return {} as LoadedComponents<TUID>;\n }\n\n return strapi.db.query(uid).load(entity, componentAttributes) as Promise<LoadedComponents<TUID>>;\n};\n\n/*\n delete old components\n create or update\n*/\nconst updateComponents = async <\n TUID extends UID.Schema,\n TData extends Partial<Modules.EntityService.Params.Data.Input<TUID>>,\n>(\n uid: TUID,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n data: TData\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const componentBody: ComponentBody = {};\n\n for (const attributeName of Object.keys(attributes)) {\n const attribute = attributes[attributeName];\n\n if (!has(attributeName, data)) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID, repeatable = false } = attribute;\n\n const componentValue = data[\n attributeName as keyof TData\n ] as Schema.Attribute.Value<Schema.Attribute.Component>;\n\n await deleteOldComponents(uid, componentUID, entityToUpdate, attributeName, componentValue);\n\n if (repeatable === true) {\n if (!Array.isArray(componentValue)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n const components = (await async.map(\n componentValue,\n (value: any) => updateOrCreateComponent(componentUID, value),\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n )) as Schema.Attribute.Value<Schema.Attribute.Component<UID.Component, true>>;\n\n componentBody[attributeName] = components.filter(_.negate(_.isNil)).map(({ id }) => {\n return {\n id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n });\n } else {\n const component = await updateOrCreateComponent(componentUID, componentValue);\n componentBody[attributeName] = component && {\n id: component.id,\n __pivot: {\n field: attributeName,\n component_type: componentUID,\n },\n };\n }\n\n continue;\n }\n\n if (attribute.type === 'dynamiczone') {\n const dynamiczoneValues = data[\n attributeName as keyof TData\n ] as Schema.Attribute.Value<Schema.Attribute.DynamicZone>;\n\n await deleteOldDZComponents(uid, entityToUpdate, attributeName, dynamiczoneValues);\n\n if (!Array.isArray(dynamiczoneValues)) {\n throw new Error('Expected an array to create repeatable component');\n }\n\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n componentBody[attributeName] = await async.map(\n dynamiczoneValues,\n async (value: any) => {\n const { id } = await updateOrCreateComponent(value.__component, value);\n\n return {\n id,\n __component: value.__component,\n __pivot: {\n field: attributeName,\n },\n };\n },\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n );\n\n continue;\n }\n }\n\n return componentBody;\n};\n\nconst pickStringifiedId = ({\n id,\n}: {\n id: Modules.EntityService.Params.Attribute.ID;\n}): Modules.EntityService.Params.Attribute.ID & string => {\n if (typeof id === 'string') {\n return id;\n }\n\n return `${id}`;\n};\n\nconst deleteOldComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n componentUID: UID.Component,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n attributeName: string,\n componentValue: Schema.Attribute.Value<Schema.Attribute.Component>\n) => {\n const previousValue = (await strapi.db\n .query(uid)\n .load(entityToUpdate, attributeName)) as ComponentValue;\n\n const idsToKeep = _.castArray(componentValue).filter(has('id')).map(pickStringifiedId);\n const allIds = _.castArray(previousValue).filter(has('id')).map(pickStringifiedId);\n\n idsToKeep.forEach((id) => {\n if (!allIds.includes(id)) {\n throw new errors.ApplicationError(\n `Some of the provided components in ${attributeName} are not related to the entity`\n );\n }\n });\n\n const idsToDelete = _.difference(allIds, idsToKeep);\n\n if (idsToDelete.length > 0) {\n for (const idToDelete of idsToDelete) {\n await deleteComponent(componentUID, { id: idToDelete });\n }\n }\n};\n\nconst deleteOldDZComponents = async <TUID extends UID.Schema>(\n uid: TUID,\n entityToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n attributeName: string,\n dynamiczoneValues: Schema.Attribute.Value<Schema.Attribute.DynamicZone>\n) => {\n const previousValue = (await strapi.db\n .query(uid)\n .load(entityToUpdate, attributeName)) as Schema.Attribute.Value<Schema.Attribute.DynamicZone>;\n\n const idsToKeep = _.castArray(dynamiczoneValues)\n .filter(has('id'))\n .map((v) => ({\n id: pickStringifiedId(v),\n __component: v.__component,\n }));\n\n const allIds = _.castArray(previousValue)\n .filter(has('id'))\n .map((v) => ({\n id: pickStringifiedId(v),\n __component: v.__component,\n }));\n\n idsToKeep.forEach(({ id, __component }) => {\n if (!allIds.find((el) => el.id === id && el.__component === __component)) {\n const err = new Error(\n `Some of the provided components in ${attributeName} are not related to the entity`\n );\n\n Object.assign(err, { status: 400 });\n throw err;\n }\n });\n\n type IdsToDelete = Schema.Attribute.Value<Schema.Attribute.DynamicZone>;\n\n const idsToDelete = allIds.reduce((acc, { id, __component }) => {\n if (!idsToKeep.find((el) => el.id === id && el.__component === __component)) {\n acc.push({ id, __component });\n }\n\n return acc;\n }, [] as IdsToDelete);\n\n if (idsToDelete.length > 0) {\n for (const idToDelete of idsToDelete) {\n const { id, __component } = idToDelete;\n await deleteComponent(__component, { id });\n }\n }\n};\n\nconst deleteComponents = async <TUID extends UID.Schema, TEntity extends Data.Entity<TUID>>(\n uid: TUID,\n entityToDelete: TEntity,\n { loadComponents = true } = {}\n) => {\n const { attributes = {} } = strapi.getModel(uid);\n\n const attributeNames = Object.keys(attributes);\n\n for (const attributeName of attributeNames) {\n const attribute = attributes[attributeName];\n\n if (attribute.type === 'component' || attribute.type === 'dynamiczone') {\n let value;\n if (loadComponents) {\n value = await strapi.db.query(uid).load(entityToDelete, attributeName);\n } else {\n value = entityToDelete[attributeName as keyof TEntity];\n }\n\n if (!value) {\n continue;\n }\n\n if (attribute.type === 'component') {\n const { component: componentUID } = attribute;\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n await async.map(\n _.castArray(value),\n (subValue: any) => deleteComponent(componentUID, subValue),\n {\n concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity,\n }\n );\n } else {\n // delete dynamic zone components\n // MySQL/MariaDB can cause deadlocks here if concurrency higher than 1\n await async.map(\n _.castArray(value),\n (subValue: any) => deleteComponent(subValue.__component, subValue),\n { concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }\n );\n }\n\n continue;\n }\n }\n};\n\n/** *************************\n Component queries\n************************** */\n\n// components can have nested compos so this must be recursive\nconst createComponent = async <TUID extends UID.Component = UID.Component>(\n uid: TUID,\n data: Modules.EntityService.Params.Data.Input<TUID>\n) => {\n const model = strapi.getModel(uid) as Schema.Component;\n\n const componentData = await createComponents(uid, data);\n const transform = pipe(\n // Make sure we don't save the component with a pre-defined ID\n omit('id'),\n // Remove the component data from the original data object ...\n (payload) => omitComponentData(model, payload),\n // ... and assign the newly created component instead\n assign(componentData)\n );\n\n return strapi.db.query(uid).create({ data: transform(data) });\n};\n\n// components can have nested compos so this must be recursive\nconst updateComponent = async <TUID extends UID.Component>(\n uid: TUID,\n componentToUpdate: { id: Modules.EntityService.Params.Attribute.ID },\n data: Modules.EntityService.Params.Data.Input<TUID>\n) => {\n const model = strapi.getModel(uid) as Schema.Component;\n\n const componentData = await updateComponents(uid, componentToUpdate, data);\n\n return strapi.db.query(uid).update({\n where: {\n id: componentToUpdate.id,\n },\n data: Object.assign(omitComponentData(model, data), componentData),\n });\n};\n\nconst updateOrCreateComponent = <TUID extends UID.Component>(\n componentUID: TUID,\n value: Modules.EntityService.Params.Data.Input<TUID>\n) => {\n if (value === null) {\n return null;\n }\n\n // update\n if ('id' in value && typeof value.id !== 'undefined') {\n // TODO: verify the compo is associated with the entity\n return updateComponent(componentUID, { id: value.id }, value);\n }\n\n // create\n return createComponent(componentUID, value);\n};\n\nconst deleteComponent = async <TUID extends UID.Component>(\n uid: TUID,\n componentToDelete: Data.Component<TUID>\n) => {\n await deleteComponents(uid, componentToDelete);\n await strapi.db.query(uid).delete({ where: { id: componentToDelete.id } });\n};\n\n/**\n * Resolve the component UID of an entity's attribute based\n * on a given path (components & dynamic zones only)\n */\nconst resolveComponentUID = ({\n paths,\n strapi,\n data,\n contentType,\n}: {\n paths: string[];\n strapi: Core.Strapi;\n data: any;\n contentType: Schema.ContentType;\n}): UID.Schema | undefined => {\n let value: unknown = data;\n let cType:\n | Schema.ContentType\n | Schema.Component\n | ((...opts: any[]) => Schema.ContentType | Schema.Component) = contentType;\n for (const path of paths) {\n value = get(path, value);\n\n // Needed when the value of cType should be computed\n // based on the next value (eg: dynamic zones)\n if (typeof cType === 'function') {\n cType = cType(value);\n }\n\n if (path in cType.attributes) {\n const attribute: Schema.Attribute.AnyAttribute = cType.attributes[path];\n\n if (attribute.type === 'component') {\n cType = strapi.getModel(attribute.component);\n }\n\n if (attribute.type === 'dynamiczone') {\n cType = ({ __component }: { __component: UID.Component }) => strapi.getModel(__component);\n }\n }\n }\n\n if ('uid' in cType) {\n return cType.uid;\n }\n\n return undefined;\n};\n\nexport {\n omitComponentData,\n getComponents,\n createComponents,\n updateComponents,\n deleteComponents,\n deleteComponent,\n resolveComponentUID,\n};\n"],"names":["isDialectMySQL","strapi","db","dialect","client","omitComponentData","contentType","data","attributes","componentAttributes","Object","keys","filter","attributeName","contentTypesUtils","isComponentAttribute","omit","createComponents","uid","getModel","componentBody","attributeNames","attribute","has","type","component","componentUID","repeatable","componentValue","Array","isArray","Error","components","async","map","value","createComponent","concurrency","inTransaction","Infinity","id","__pivot","field","component_type","dynamiczoneValues","createDynamicZoneComponents","__component","getComponents","entity","getComponentAttributes","_","isEmpty","query","load","deleteComponents","entityToDelete","loadComponents","castArray","subValue","deleteComponent","model","componentData","transform","pipe","payload","assign","create","componentToDelete","delete","where","resolveComponentUID","paths","cType","path","get","undefined"],"mappings":";;;;;;AAuBA,MAAMA,iBAAiB,IAAMC,MAAAA,CAAOC,EAAE,EAAEC,QAAQC,MAAW,KAAA,OAAA;AAU3D,SAASC,iBAAAA,CACPC,WAAkD,EAClDC,IAAkG,EAAA;IAIlG,MAAM,EAAEC,UAAU,EAAE,GAAGF,WAAAA;AACvB,IAAA,MAAMG,mBAAsBC,GAAAA,MAAAA,CAAOC,IAAI,CAACH,YAAYI,MAAM,CAAC,CAACC,aAAAA,GAC1DC,kBAAkBC,CAAAA,oBAAoB,CAACP,UAAU,CAACK,aAAc,CAAA,CAAA,CAAA;AAGlE,IAAA,OAAOG,QAAKP,mBAAqBF,EAAAA,IAAAA,CAAAA;AACnC;AAEA;AACMU,MAAAA,gBAAAA,GAAmB,OAIvBC,GACAX,EAAAA,IAAAA,GAAAA;IAEA,MAAM,EAAEC,aAAa,EAAE,EAAE,GAAGP,MAAAA,CAAOkB,QAAQ,CAACD,GAAAA,CAAAA;AAE5C,IAAA,MAAME,gBAA+B,EAAC;IAEtC,MAAMC,cAAAA,GAAiBX,MAAOC,CAAAA,IAAI,CAACH,UAAAA,CAAAA;IAEnC,KAAK,MAAMK,iBAAiBQ,cAAgB,CAAA;QAC1C,MAAMC,SAAAA,GAAYd,UAAU,CAACK,aAAc,CAAA;QAE3C,IAAI,CAACU,OAAIV,aAAeN,EAAAA,IAAAA,CAAAA,IAAS,CAACO,kBAAkBC,CAAAA,oBAAoB,CAACO,SAAY,CAAA,EAAA;AACnF,YAAA;AACF;QAEA,IAAIA,SAAAA,CAAUE,IAAI,KAAK,WAAa,EAAA;AAClC,YAAA,MAAM,EAAEC,SAAWC,EAAAA,YAAY,EAAEC,UAAa,GAAA,KAAK,EAAE,GAAGL,SAAAA;YAExD,MAAMM,cAAAA,GAAiBrB,IAAI,CAACM,aAA6B,CAAA;AAEzD,YAAA,IAAIe,mBAAmB,IAAM,EAAA;AA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