@strapi/data-transfer 4.9.0 → 4.10.0-beta.0
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/jest.config.js +14 -0
- package/lib/engine/diagnostic.d.ts +40 -0
- package/lib/engine/diagnostic.js +50 -0
- package/lib/engine/errors.d.ts +28 -0
- package/lib/engine/errors.js +29 -0
- package/lib/engine/index.d.ts +44 -0
- package/lib/engine/index.js +506 -0
- package/lib/engine/validation/index.d.ts +2 -0
- package/lib/engine/validation/index.js +19 -0
- package/lib/engine/validation/provider.d.ts +3 -0
- package/lib/engine/validation/provider.js +18 -0
- package/lib/engine/validation/schemas/index.d.ts +7 -0
- package/lib/engine/validation/schemas/index.js +53 -0
- package/lib/errors/base.d.ts +8 -0
- package/lib/errors/base.js +13 -0
- package/lib/errors/constants.d.ts +3 -0
- package/lib/errors/constants.js +9 -0
- package/lib/errors/index.d.ts +2 -0
- package/lib/errors/index.js +19 -0
- package/lib/errors/providers.d.ts +21 -0
- package/lib/errors/providers.js +32 -0
- package/lib/file/index.d.ts +1 -0
- package/lib/file/index.js +28 -0
- package/lib/file/providers/destination/index.d.ts +45 -0
- package/lib/file/providers/destination/index.js +174 -0
- package/lib/file/providers/destination/utils.d.ts +9 -0
- package/lib/file/providers/destination/utils.js +61 -0
- package/lib/file/providers/index.d.ts +2 -0
- package/lib/file/providers/index.js +19 -0
- package/lib/file/providers/source/index.d.ts +40 -0
- package/lib/file/providers/source/index.js +220 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +31 -0
- package/lib/strapi/index.d.ts +3 -0
- package/lib/strapi/index.js +30 -0
- package/lib/strapi/providers/index.d.ts +3 -0
- package/lib/strapi/providers/index.js +22 -0
- package/lib/strapi/providers/local-destination/index.d.ts +34 -0
- package/lib/strapi/providers/local-destination/index.js +220 -0
- package/lib/strapi/providers/local-destination/strategies/index.d.ts +1 -0
- package/lib/strapi/providers/local-destination/strategies/index.js +28 -0
- package/lib/strapi/providers/local-destination/strategies/restore/configuration.d.ts +5 -0
- package/lib/strapi/providers/local-destination/strategies/restore/configuration.js +51 -0
- package/lib/strapi/providers/local-destination/strategies/restore/entities.d.ts +11 -0
- package/lib/strapi/providers/local-destination/strategies/restore/entities.js +103 -0
- package/lib/strapi/providers/local-destination/strategies/restore/index.d.ts +32 -0
- package/lib/strapi/providers/local-destination/strategies/restore/index.js +107 -0
- package/lib/strapi/providers/local-destination/strategies/restore/links.d.ts +4 -0
- package/lib/strapi/providers/local-destination/strategies/restore/links.js +32 -0
- package/lib/strapi/providers/local-source/assets.d.ts +5 -0
- package/lib/strapi/providers/local-source/assets.js +31 -0
- package/lib/strapi/providers/local-source/configuration.d.ts +5 -0
- package/lib/strapi/providers/local-source/configuration.js +36 -0
- package/lib/strapi/providers/local-source/entities.d.ts +10 -0
- package/lib/strapi/providers/local-source/entities.js +85 -0
- package/lib/strapi/providers/local-source/index.d.ts +27 -0
- package/lib/strapi/providers/local-source/index.js +98 -0
- package/lib/strapi/providers/local-source/links.d.ts +5 -0
- package/lib/strapi/providers/local-source/links.js +23 -0
- package/lib/strapi/providers/remote-destination/index.d.ts +37 -0
- package/lib/strapi/providers/remote-destination/index.js +284 -0
- package/lib/strapi/providers/remote-destination/utils.d.ts +31 -0
- package/lib/strapi/providers/remote-destination/utils.js +72 -0
- package/lib/strapi/queries/entity.d.ts +19 -0
- package/lib/strapi/queries/entity.js +130 -0
- package/lib/strapi/queries/index.d.ts +2 -0
- package/lib/strapi/queries/index.js +29 -0
- package/lib/strapi/queries/link.d.ts +6 -0
- package/lib/strapi/queries/link.js +215 -0
- package/lib/strapi/remote/constants.d.ts +2 -0
- package/lib/strapi/remote/constants.js +6 -0
- package/lib/strapi/remote/controllers/index.d.ts +1 -0
- package/lib/strapi/remote/controllers/index.js +18 -0
- package/lib/strapi/remote/controllers/push.d.ts +26 -0
- package/lib/strapi/remote/controllers/push.js +116 -0
- package/lib/strapi/remote/flows/default.d.ts +3 -0
- package/lib/strapi/remote/flows/default.js +41 -0
- package/lib/strapi/remote/flows/index.d.ts +18 -0
- package/lib/strapi/remote/flows/index.js +59 -0
- package/lib/strapi/remote/handlers.d.ts +10 -0
- package/lib/strapi/remote/handlers.js +285 -0
- package/lib/strapi/remote/index.d.ts +3 -0
- package/lib/strapi/remote/index.js +30 -0
- package/lib/utils/encryption/decrypt.d.ts +11 -0
- package/lib/utils/encryption/decrypt.js +47 -0
- package/lib/utils/encryption/encrypt.d.ts +11 -0
- package/lib/utils/encryption/encrypt.js +47 -0
- package/lib/utils/encryption/index.d.ts +2 -0
- package/lib/utils/encryption/index.js +19 -0
- package/lib/utils/index.d.ts +5 -0
- package/lib/utils/index.js +32 -0
- package/lib/utils/json.d.ts +30 -0
- package/lib/utils/json.js +68 -0
- package/lib/utils/providers.d.ts +2 -0
- package/lib/utils/providers.js +11 -0
- package/lib/utils/schema.d.ts +7 -0
- package/lib/utils/schema.js +29 -0
- package/lib/utils/stream.d.ts +27 -0
- package/lib/utils/stream.js +59 -0
- package/lib/utils/transaction.d.ts +3 -0
- package/lib/utils/transaction.js +88 -0
- package/package.json +16 -17
- package/dist/engine/diagnostic.js.map +0 -1
- package/dist/engine/errors.js.map +0 -1
- package/dist/engine/index.js.map +0 -1
- package/dist/engine/validation/index.js.map +0 -1
- package/dist/engine/validation/provider.js.map +0 -1
- package/dist/engine/validation/schemas/index.js.map +0 -1
- package/dist/errors/base.js.map +0 -1
- package/dist/errors/constants.js.map +0 -1
- package/dist/errors/index.js.map +0 -1
- package/dist/errors/providers.js.map +0 -1
- package/dist/file/index.js.map +0 -1
- package/dist/file/providers/destination/index.js.map +0 -1
- package/dist/file/providers/destination/utils.js.map +0 -1
- package/dist/file/providers/index.js.map +0 -1
- package/dist/file/providers/source/index.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/strapi/index.js.map +0 -1
- package/dist/strapi/providers/index.js.map +0 -1
- package/dist/strapi/providers/local-destination/index.js.map +0 -1
- package/dist/strapi/providers/local-destination/strategies/index.js.map +0 -1
- package/dist/strapi/providers/local-destination/strategies/restore/configuration.js.map +0 -1
- package/dist/strapi/providers/local-destination/strategies/restore/entities.js.map +0 -1
- package/dist/strapi/providers/local-destination/strategies/restore/index.js.map +0 -1
- package/dist/strapi/providers/local-destination/strategies/restore/links.js.map +0 -1
- package/dist/strapi/providers/local-source/assets.js.map +0 -1
- package/dist/strapi/providers/local-source/configuration.js.map +0 -1
- package/dist/strapi/providers/local-source/entities.js.map +0 -1
- package/dist/strapi/providers/local-source/index.js.map +0 -1
- package/dist/strapi/providers/local-source/links.js.map +0 -1
- package/dist/strapi/providers/remote-destination/index.js.map +0 -1
- package/dist/strapi/providers/remote-source/index.js.map +0 -1
- package/dist/strapi/providers/utils.js.map +0 -1
- package/dist/strapi/queries/entity.js.map +0 -1
- package/dist/strapi/queries/index.js.map +0 -1
- package/dist/strapi/queries/link.js.map +0 -1
- package/dist/strapi/remote/constants.js.map +0 -1
- package/dist/strapi/remote/flows/default.js.map +0 -1
- package/dist/strapi/remote/flows/index.js.map +0 -1
- package/dist/strapi/remote/handlers/abstract.js.map +0 -1
- package/dist/strapi/remote/handlers/constants.js.map +0 -1
- package/dist/strapi/remote/handlers/index.js.map +0 -1
- package/dist/strapi/remote/handlers/pull.js.map +0 -1
- package/dist/strapi/remote/handlers/push.js.map +0 -1
- package/dist/strapi/remote/handlers/utils.js.map +0 -1
- package/dist/strapi/remote/index.js.map +0 -1
- package/dist/utils/encryption/decrypt.js.map +0 -1
- package/dist/utils/encryption/encrypt.js.map +0 -1
- package/dist/utils/encryption/index.js.map +0 -1
- package/dist/utils/index.js.map +0 -1
- package/dist/utils/json.js.map +0 -1
- package/dist/utils/providers.js.map +0 -1
- package/dist/utils/schema.js.map +0 -1
- package/dist/utils/stream.js.map +0 -1
- package/dist/utils/transaction.js.map +0 -1
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createLinkQuery = void 0;
|
|
4
|
+
const fp_1 = require("lodash/fp");
|
|
5
|
+
// TODO: Remove any types when we'll have types for DB metadata
|
|
6
|
+
const createLinkQuery = (strapi, trx) => {
|
|
7
|
+
const query = () => {
|
|
8
|
+
const { connection } = strapi.db;
|
|
9
|
+
async function* generateAllForAttribute(uid, fieldName) {
|
|
10
|
+
const metadata = strapi.db.metadata.get(uid);
|
|
11
|
+
if (!metadata) {
|
|
12
|
+
throw new Error(`No metadata found for ${uid}`);
|
|
13
|
+
}
|
|
14
|
+
const attributes = filterValidRelationalAttributes(metadata.attributes);
|
|
15
|
+
if (!(fieldName in attributes)) {
|
|
16
|
+
throw new Error(`${fieldName} is not a valid relational attribute name`);
|
|
17
|
+
}
|
|
18
|
+
const attribute = attributes[fieldName];
|
|
19
|
+
const kind = getLinkKind(attribute, uid);
|
|
20
|
+
const { relation, target } = attribute;
|
|
21
|
+
// The relation is stored in the same table
|
|
22
|
+
// TODO: handle manyToOne joinColumn
|
|
23
|
+
if (attribute.joinColumn) {
|
|
24
|
+
const joinColumnName = attribute.joinColumn.name;
|
|
25
|
+
const qb = connection.queryBuilder().select('id', joinColumnName).from(metadata.tableName);
|
|
26
|
+
if (trx) {
|
|
27
|
+
qb.transacting(trx);
|
|
28
|
+
}
|
|
29
|
+
// TODO: stream the query to improve performances
|
|
30
|
+
const entries = await qb;
|
|
31
|
+
for (const entry of entries) {
|
|
32
|
+
const ref = entry[joinColumnName];
|
|
33
|
+
if (ref !== null) {
|
|
34
|
+
yield {
|
|
35
|
+
kind,
|
|
36
|
+
relation,
|
|
37
|
+
left: { type: uid, ref: entry.id, field: fieldName },
|
|
38
|
+
right: { type: target, ref },
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// The relation uses a join table
|
|
44
|
+
if (attribute.joinTable) {
|
|
45
|
+
const { name, joinColumn, inverseJoinColumn, orderColumnName, morphColumn, inverseOrderColumnName, } = attribute.joinTable;
|
|
46
|
+
const qb = connection.queryBuilder().from(name);
|
|
47
|
+
const columns = {
|
|
48
|
+
left: { ref: null },
|
|
49
|
+
right: { ref: null },
|
|
50
|
+
};
|
|
51
|
+
const left = { type: uid, field: fieldName };
|
|
52
|
+
const right = {};
|
|
53
|
+
if (kind === 'relation.basic' || kind === 'relation.circular') {
|
|
54
|
+
right.type = attribute.target;
|
|
55
|
+
right.field = attribute.inversedBy;
|
|
56
|
+
columns.left.ref = joinColumn.name;
|
|
57
|
+
columns.right.ref = inverseJoinColumn.name;
|
|
58
|
+
if (orderColumnName) {
|
|
59
|
+
columns.left.order = orderColumnName;
|
|
60
|
+
}
|
|
61
|
+
if (inverseOrderColumnName) {
|
|
62
|
+
columns.right.order = inverseOrderColumnName;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (kind === 'relation.morph') {
|
|
66
|
+
columns.left.ref = joinColumn.name;
|
|
67
|
+
columns.right.ref = morphColumn.idColumn.name;
|
|
68
|
+
columns.right.type = morphColumn.typeColumn.name;
|
|
69
|
+
columns.right.field = 'field';
|
|
70
|
+
columns.right.order = 'order';
|
|
71
|
+
}
|
|
72
|
+
const validColumns = [
|
|
73
|
+
// Left
|
|
74
|
+
columns.left.ref,
|
|
75
|
+
columns.left.order,
|
|
76
|
+
// Right
|
|
77
|
+
columns.right.ref,
|
|
78
|
+
columns.right.type,
|
|
79
|
+
columns.right.field,
|
|
80
|
+
columns.right.order,
|
|
81
|
+
].filter((column) => !(0, fp_1.isNil)(column));
|
|
82
|
+
qb.select(validColumns);
|
|
83
|
+
if (trx) {
|
|
84
|
+
qb.transacting(trx);
|
|
85
|
+
}
|
|
86
|
+
// TODO: stream the query to improve performances
|
|
87
|
+
const entries = await qb;
|
|
88
|
+
for (const entry of entries) {
|
|
89
|
+
if (columns.left.ref) {
|
|
90
|
+
left.ref = entry[columns.left.ref];
|
|
91
|
+
}
|
|
92
|
+
if (columns.right.ref) {
|
|
93
|
+
right.ref = entry[columns.right.ref];
|
|
94
|
+
}
|
|
95
|
+
if (columns.left.order) {
|
|
96
|
+
left.pos = entry[columns.left.order];
|
|
97
|
+
}
|
|
98
|
+
if (columns.right.order) {
|
|
99
|
+
right.pos = entry[columns.right.order];
|
|
100
|
+
}
|
|
101
|
+
if (columns.right.type) {
|
|
102
|
+
right.type = entry[columns.right.type];
|
|
103
|
+
}
|
|
104
|
+
if (columns.right.field) {
|
|
105
|
+
right.field = entry[columns.right.field];
|
|
106
|
+
}
|
|
107
|
+
const link = {
|
|
108
|
+
kind,
|
|
109
|
+
relation,
|
|
110
|
+
left: (0, fp_1.clone)(left),
|
|
111
|
+
right: (0, fp_1.clone)(right),
|
|
112
|
+
};
|
|
113
|
+
yield link;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
async function* generateAll(uid) {
|
|
118
|
+
const metadata = strapi.db.metadata.get(uid);
|
|
119
|
+
if (!metadata) {
|
|
120
|
+
throw new Error(`No metadata found for ${uid}`);
|
|
121
|
+
}
|
|
122
|
+
const attributes = filterValidRelationalAttributes(metadata.attributes);
|
|
123
|
+
for (const fieldName of Object.keys(attributes)) {
|
|
124
|
+
for await (const link of generateAllForAttribute(uid, fieldName)) {
|
|
125
|
+
yield link;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const insert = async (link) => {
|
|
130
|
+
const { kind, left, right } = link;
|
|
131
|
+
const metadata = strapi.db.metadata.get(left.type);
|
|
132
|
+
const attribute = metadata.attributes[left.field];
|
|
133
|
+
const payload = {};
|
|
134
|
+
if (attribute.joinColumn) {
|
|
135
|
+
const joinColumnName = attribute.joinColumn.name;
|
|
136
|
+
const qb = connection(metadata.tableName)
|
|
137
|
+
.where('id', left.ref)
|
|
138
|
+
.update({ [joinColumnName]: right.ref });
|
|
139
|
+
if (trx) {
|
|
140
|
+
qb.transacting(trx);
|
|
141
|
+
}
|
|
142
|
+
await qb;
|
|
143
|
+
}
|
|
144
|
+
if (attribute.joinTable) {
|
|
145
|
+
const { name, joinColumn, inverseJoinColumn, orderColumnName, inverseOrderColumnName, morphColumn, } = attribute.joinTable;
|
|
146
|
+
if (joinColumn) {
|
|
147
|
+
Object.assign(payload, { [joinColumn.name]: left.ref });
|
|
148
|
+
}
|
|
149
|
+
const assignInverseColumn = () => {
|
|
150
|
+
if (inverseJoinColumn) {
|
|
151
|
+
Object.assign(payload, {
|
|
152
|
+
[inverseJoinColumn.name]: right.ref,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
const assignOrderColumns = () => {
|
|
157
|
+
if (orderColumnName) {
|
|
158
|
+
Object.assign(payload, { [orderColumnName]: left.pos ?? null });
|
|
159
|
+
}
|
|
160
|
+
if (inverseOrderColumnName) {
|
|
161
|
+
Object.assign(payload, { [inverseOrderColumnName]: right.pos ?? null });
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
const assignMorphColumns = () => {
|
|
165
|
+
const { idColumn, typeColumn } = morphColumn ?? {};
|
|
166
|
+
if (idColumn) {
|
|
167
|
+
Object.assign(payload, { [idColumn.name]: right.ref });
|
|
168
|
+
}
|
|
169
|
+
if (typeColumn) {
|
|
170
|
+
Object.assign(payload, { [typeColumn.name]: right.type });
|
|
171
|
+
}
|
|
172
|
+
Object.assign(payload, { order: right.pos ?? null, field: right.field ?? null });
|
|
173
|
+
};
|
|
174
|
+
if (kind === 'relation.basic' || kind === 'relation.circular') {
|
|
175
|
+
assignInverseColumn();
|
|
176
|
+
}
|
|
177
|
+
if (kind === 'relation.morph') {
|
|
178
|
+
assignMorphColumns();
|
|
179
|
+
}
|
|
180
|
+
assignOrderColumns();
|
|
181
|
+
const qb = connection.insert(payload).into(name);
|
|
182
|
+
if (trx) {
|
|
183
|
+
qb.transacting(trx);
|
|
184
|
+
}
|
|
185
|
+
await qb;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
return { generateAll, generateAllForAttribute, insert };
|
|
189
|
+
};
|
|
190
|
+
return query;
|
|
191
|
+
};
|
|
192
|
+
exports.createLinkQuery = createLinkQuery;
|
|
193
|
+
const filterValidRelationalAttributes = (attributes) => {
|
|
194
|
+
const isOwner = (attribute) => {
|
|
195
|
+
return attribute.owner || (!attribute.mappedBy && !attribute.morphBy);
|
|
196
|
+
};
|
|
197
|
+
const isComponentLike = (attribute) => {
|
|
198
|
+
return attribute.component || attribute.components;
|
|
199
|
+
};
|
|
200
|
+
return Object.entries(attributes)
|
|
201
|
+
.filter(([, attribute]) => {
|
|
202
|
+
return attribute.type === 'relation' && isOwner(attribute) && !isComponentLike(attribute);
|
|
203
|
+
})
|
|
204
|
+
.reduce((acc, [key, attribute]) => ({ ...acc, [key]: attribute }), {});
|
|
205
|
+
};
|
|
206
|
+
const getLinkKind = (attribute, uid) => {
|
|
207
|
+
if (attribute.relation.startsWith('morph')) {
|
|
208
|
+
return 'relation.morph';
|
|
209
|
+
}
|
|
210
|
+
if (attribute.target === uid) {
|
|
211
|
+
return 'relation.circular';
|
|
212
|
+
}
|
|
213
|
+
return 'relation.basic';
|
|
214
|
+
};
|
|
215
|
+
//# sourceMappingURL=link.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TRANSFER_METHODS = exports.TRANSFER_PATH = void 0;
|
|
4
|
+
exports.TRANSFER_PATH = '/transfer/runner/connect';
|
|
5
|
+
exports.TRANSFER_METHODS = ['push', 'pull'];
|
|
6
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './push';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./push"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Writable } from 'stream-chain';
|
|
3
|
+
import type { IMetadata } from '../../../../types';
|
|
4
|
+
import type { TransferPushMessage, TransferPushStep } from '../../../../types/remote/protocol/client';
|
|
5
|
+
import { ILocalStrapiDestinationProviderOptions } from '../../providers';
|
|
6
|
+
export interface IPushController {
|
|
7
|
+
streams: {
|
|
8
|
+
[stage in TransferPushStep]?: Writable;
|
|
9
|
+
};
|
|
10
|
+
actions: {
|
|
11
|
+
getMetadata(): Promise<IMetadata>;
|
|
12
|
+
getSchemas(): Strapi.Schemas;
|
|
13
|
+
bootstrap(): Promise<void>;
|
|
14
|
+
close(): Promise<void>;
|
|
15
|
+
beforeTransfer(): Promise<void>;
|
|
16
|
+
rollback(): Promise<void>;
|
|
17
|
+
};
|
|
18
|
+
transfer: {
|
|
19
|
+
[key in TransferPushStep]: <T extends TransferPushMessage>(value: T extends {
|
|
20
|
+
step: key;
|
|
21
|
+
data: infer U;
|
|
22
|
+
} ? U : never) => Promise<void>;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
declare const createPushController: (options: ILocalStrapiDestinationProviderOptions) => IPushController;
|
|
26
|
+
export default createPushController;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const stream_chain_1 = require("stream-chain");
|
|
4
|
+
const providers_1 = require("../../providers");
|
|
5
|
+
const createPushController = (options) => {
|
|
6
|
+
const provider = (0, providers_1.createLocalStrapiDestinationProvider)(options);
|
|
7
|
+
const streams = {};
|
|
8
|
+
const assets = {};
|
|
9
|
+
const writeAsync = (stream, data) => {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
stream.write(data, (error) => {
|
|
12
|
+
if (error) {
|
|
13
|
+
reject(error);
|
|
14
|
+
}
|
|
15
|
+
resolve();
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
return {
|
|
20
|
+
streams,
|
|
21
|
+
actions: {
|
|
22
|
+
async getSchemas() {
|
|
23
|
+
return provider.getSchemas();
|
|
24
|
+
},
|
|
25
|
+
async getMetadata() {
|
|
26
|
+
return provider.getMetadata();
|
|
27
|
+
},
|
|
28
|
+
async bootstrap() {
|
|
29
|
+
return provider.bootstrap();
|
|
30
|
+
},
|
|
31
|
+
async close() {
|
|
32
|
+
return provider.close();
|
|
33
|
+
},
|
|
34
|
+
async beforeTransfer() {
|
|
35
|
+
return provider.beforeTransfer();
|
|
36
|
+
},
|
|
37
|
+
async rollback() {
|
|
38
|
+
await provider.rollback();
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
transfer: {
|
|
42
|
+
async entities(entities) {
|
|
43
|
+
if (!streams.entities) {
|
|
44
|
+
streams.entities = provider.createEntitiesWriteStream();
|
|
45
|
+
}
|
|
46
|
+
for (const entity of entities) {
|
|
47
|
+
if (streams.entities) {
|
|
48
|
+
await writeAsync(streams.entities, entity);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
async links(links) {
|
|
53
|
+
if (!streams.links) {
|
|
54
|
+
streams.links = await provider.createLinksWriteStream();
|
|
55
|
+
}
|
|
56
|
+
for (const link of links) {
|
|
57
|
+
if (streams.links) {
|
|
58
|
+
await writeAsync(streams.links, link);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
async configuration(configs) {
|
|
63
|
+
if (!streams.configuration) {
|
|
64
|
+
streams.configuration = await provider.createConfigurationWriteStream();
|
|
65
|
+
}
|
|
66
|
+
for (const config of configs) {
|
|
67
|
+
if (streams.configuration) {
|
|
68
|
+
await writeAsync(streams.configuration, config);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
async assets(payloads) {
|
|
73
|
+
if (payloads === null) {
|
|
74
|
+
streams.assets?.end();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (!streams.assets) {
|
|
78
|
+
streams.assets = await provider.createAssetsWriteStream();
|
|
79
|
+
}
|
|
80
|
+
for (const payload of payloads) {
|
|
81
|
+
if (streams.assets.closed) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const { action, assetID } = payload;
|
|
85
|
+
if (action === 'start' && streams.assets) {
|
|
86
|
+
assets[assetID] = { ...payload.data, stream: new stream_chain_1.PassThrough() };
|
|
87
|
+
writeAsync(streams.assets, assets[assetID]);
|
|
88
|
+
}
|
|
89
|
+
if (action === 'stream') {
|
|
90
|
+
// The buffer has gone through JSON operations and is now of shape { type: "Buffer"; data: UInt8Array }
|
|
91
|
+
// We need to transform it back into a Buffer instance
|
|
92
|
+
const rawBuffer = payload.data;
|
|
93
|
+
const chunk = Buffer.from(rawBuffer.data);
|
|
94
|
+
await writeAsync(assets[assetID].stream, chunk);
|
|
95
|
+
}
|
|
96
|
+
if (action === 'end') {
|
|
97
|
+
await new Promise((resolve, reject) => {
|
|
98
|
+
const { stream } = assets[assetID];
|
|
99
|
+
stream
|
|
100
|
+
.on('close', () => {
|
|
101
|
+
delete assets[assetID];
|
|
102
|
+
resolve();
|
|
103
|
+
})
|
|
104
|
+
.on('error', (e) => {
|
|
105
|
+
reject(e);
|
|
106
|
+
})
|
|
107
|
+
.end();
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
exports.default = createPushController;
|
|
116
|
+
//# sourceMappingURL=push.js.map
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = [
|
|
4
|
+
{
|
|
5
|
+
kind: 'action',
|
|
6
|
+
action: 'bootstrap',
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
kind: 'action',
|
|
10
|
+
action: 'init',
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
kind: 'action',
|
|
14
|
+
action: 'beforeTransfer',
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
kind: 'transfer',
|
|
18
|
+
stage: 'schemas',
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
kind: 'transfer',
|
|
22
|
+
stage: 'entities',
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
kind: 'transfer',
|
|
26
|
+
stage: 'assets',
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
kind: 'transfer',
|
|
30
|
+
stage: 'links',
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
kind: 'transfer',
|
|
34
|
+
stage: 'configuration',
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
kind: 'action',
|
|
38
|
+
action: 'close',
|
|
39
|
+
},
|
|
40
|
+
];
|
|
41
|
+
//# sourceMappingURL=default.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { TransferStage } from '../../../../types';
|
|
2
|
+
export declare type Step = {
|
|
3
|
+
kind: 'action';
|
|
4
|
+
action: string;
|
|
5
|
+
} | {
|
|
6
|
+
kind: 'transfer';
|
|
7
|
+
stage: TransferStage;
|
|
8
|
+
locked?: boolean;
|
|
9
|
+
};
|
|
10
|
+
export { default as DEFAULT_TRANSFER_FLOW } from './default';
|
|
11
|
+
export declare const createFlow: (flow: readonly Step[]) => {
|
|
12
|
+
has(step: Step): boolean;
|
|
13
|
+
can(step: Step): boolean;
|
|
14
|
+
cannot(step: Step): boolean;
|
|
15
|
+
set(step: Step): any;
|
|
16
|
+
get(): Step | null;
|
|
17
|
+
};
|
|
18
|
+
export declare type TransferFlow = ReturnType<typeof createFlow>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createFlow = exports.DEFAULT_TRANSFER_FLOW = void 0;
|
|
7
|
+
var default_1 = require("./default");
|
|
8
|
+
Object.defineProperty(exports, "DEFAULT_TRANSFER_FLOW", { enumerable: true, get: function () { return __importDefault(default_1).default; } });
|
|
9
|
+
const createFlow = (flow) => {
|
|
10
|
+
const state = { step: null };
|
|
11
|
+
/**
|
|
12
|
+
* Equality check between two steps
|
|
13
|
+
*/
|
|
14
|
+
const stepEqual = (stepA, stepB) => {
|
|
15
|
+
if (stepA.kind === 'action' && stepB.kind === 'action') {
|
|
16
|
+
return stepA.action === stepB.action;
|
|
17
|
+
}
|
|
18
|
+
if (stepA.kind === 'transfer' && stepB.kind === 'transfer') {
|
|
19
|
+
return stepA.stage === stepB.stage;
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Find the index for a given step
|
|
25
|
+
*/
|
|
26
|
+
const findStepIndex = (step) => flow.findIndex((flowStep) => stepEqual(step, flowStep));
|
|
27
|
+
return {
|
|
28
|
+
has(step) {
|
|
29
|
+
return findStepIndex(step) !== -1;
|
|
30
|
+
},
|
|
31
|
+
can(step) {
|
|
32
|
+
if (state.step === null) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
const indexesDifference = findStepIndex(step) - findStepIndex(state.step);
|
|
36
|
+
// It's possible to send multiple time the same transfer step in a row
|
|
37
|
+
if (indexesDifference === 0 && step.kind === 'transfer') {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
return indexesDifference > 0;
|
|
41
|
+
},
|
|
42
|
+
cannot(step) {
|
|
43
|
+
return !this.can(step);
|
|
44
|
+
},
|
|
45
|
+
set(step) {
|
|
46
|
+
const canSwitch = this.can(step);
|
|
47
|
+
if (!canSwitch) {
|
|
48
|
+
throw new Error('Impossible to proceed to the given step');
|
|
49
|
+
}
|
|
50
|
+
state.step = step;
|
|
51
|
+
return this;
|
|
52
|
+
},
|
|
53
|
+
get() {
|
|
54
|
+
return state.step;
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
exports.createFlow = createFlow;
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Context } from 'koa';
|
|
2
|
+
import type { ServerOptions } from 'ws';
|
|
3
|
+
import { TRANSFER_METHODS } from './constants';
|
|
4
|
+
declare type TransferMethod = (typeof TRANSFER_METHODS)[number];
|
|
5
|
+
interface IHandlerOptions {
|
|
6
|
+
verify: (ctx: Context, scope?: TransferMethod) => Promise<void>;
|
|
7
|
+
server?: ServerOptions;
|
|
8
|
+
}
|
|
9
|
+
export declare const createTransferHandler: (options: IHandlerOptions) => (ctx: Context) => Promise<void>;
|
|
10
|
+
export {};
|