@strapi/data-transfer 4.6.0-beta.1 → 4.6.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.
Files changed (113) hide show
  1. package/lib/engine/diagnostic.d.ts +40 -0
  2. package/lib/engine/diagnostic.js +50 -0
  3. package/lib/engine/errors.d.ts +28 -0
  4. package/lib/engine/errors.js +29 -0
  5. package/{dist → lib}/engine/index.d.ts +18 -4
  6. package/{dist → lib}/engine/index.js +211 -45
  7. package/lib/engine/validation/index.d.ts +2 -0
  8. package/{dist → lib/engine/validation}/index.js +2 -2
  9. package/lib/engine/validation/provider.d.ts +3 -0
  10. package/lib/engine/validation/provider.js +18 -0
  11. package/{dist/strategies → lib/engine/validation/schemas}/index.d.ts +2 -2
  12. package/{dist/strategies → lib/engine/validation/schemas}/index.js +3 -2
  13. package/lib/errors/base.d.ts +8 -0
  14. package/lib/errors/base.js +13 -0
  15. package/lib/errors/constants.d.ts +3 -0
  16. package/lib/errors/constants.js +9 -0
  17. package/lib/errors/index.d.ts +2 -0
  18. package/{dist/providers → lib/errors}/index.js +2 -6
  19. package/lib/errors/providers.d.ts +21 -0
  20. package/lib/errors/providers.js +32 -0
  21. package/lib/file/index.d.ts +1 -0
  22. package/{dist/providers/shared → lib/file}/index.js +2 -2
  23. package/{dist/providers/local-file-destination-provider → lib/file/providers/destination}/index.d.ts +6 -6
  24. package/{dist/providers/local-file-destination-provider → lib/file/providers/destination}/index.js +7 -7
  25. package/{dist/providers/local-file-destination-provider → lib/file/providers/destination}/utils.d.ts +0 -0
  26. package/{dist/providers/local-file-destination-provider → lib/file/providers/destination}/utils.js +0 -0
  27. package/lib/file/providers/index.d.ts +2 -0
  28. package/lib/file/providers/index.js +19 -0
  29. package/{dist/providers/local-file-source-provider → lib/file/providers/source}/index.d.ts +9 -8
  30. package/{dist/providers/local-file-source-provider → lib/file/providers/source}/index.js +42 -56
  31. package/lib/index.d.ts +4 -0
  32. package/lib/index.js +31 -0
  33. package/lib/strapi/index.d.ts +4 -0
  34. package/lib/strapi/index.js +35 -0
  35. package/lib/strapi/providers/index.d.ts +3 -0
  36. package/lib/strapi/providers/index.js +22 -0
  37. package/{dist/providers/local-strapi-destination-provider → lib/strapi/providers/local-destination}/index.d.ts +10 -7
  38. package/{dist/providers/local-strapi-destination-provider → lib/strapi/providers/local-destination}/index.js +65 -37
  39. package/{dist/providers/local-strapi-destination-provider → lib/strapi/providers/local-destination}/strategies/index.d.ts +0 -0
  40. package/{dist/providers/local-strapi-destination-provider → lib/strapi/providers/local-destination}/strategies/index.js +0 -0
  41. package/{dist/providers/local-strapi-destination-provider → lib/strapi/providers/local-destination}/strategies/restore/configuration.d.ts +2 -2
  42. package/{dist/providers/local-strapi-destination-provider → lib/strapi/providers/local-destination}/strategies/restore/configuration.js +17 -10
  43. package/{dist/providers/local-strapi-destination-provider → lib/strapi/providers/local-destination}/strategies/restore/entities.d.ts +2 -0
  44. package/lib/strapi/providers/local-destination/strategies/restore/entities.js +103 -0
  45. package/{dist/providers/local-strapi-destination-provider → lib/strapi/providers/local-destination}/strategies/restore/index.d.ts +0 -0
  46. package/{dist/providers/local-strapi-destination-provider → lib/strapi/providers/local-destination}/strategies/restore/index.js +4 -3
  47. package/lib/strapi/providers/local-destination/strategies/restore/links.d.ts +4 -0
  48. package/lib/strapi/providers/local-destination/strategies/restore/links.js +32 -0
  49. package/{dist/providers/local-strapi-source-provider → lib/strapi/providers/local-source}/assets.d.ts +0 -0
  50. package/{dist/providers/local-strapi-source-provider → lib/strapi/providers/local-source}/assets.js +0 -0
  51. package/{dist/providers/local-strapi-source-provider → lib/strapi/providers/local-source}/configuration.d.ts +0 -0
  52. package/{dist/providers/local-strapi-source-provider → lib/strapi/providers/local-source}/configuration.js +12 -13
  53. package/{dist/providers/local-strapi-source-provider → lib/strapi/providers/local-source}/entities.d.ts +0 -0
  54. package/{dist/providers/local-strapi-source-provider → lib/strapi/providers/local-source}/entities.js +11 -4
  55. package/{dist/providers/local-strapi-source-provider → lib/strapi/providers/local-source}/index.d.ts +6 -6
  56. package/{dist/providers/local-strapi-source-provider → lib/strapi/providers/local-source}/index.js +12 -27
  57. package/{dist/providers/local-strapi-source-provider → lib/strapi/providers/local-source}/links.d.ts +0 -0
  58. package/{dist/providers/local-strapi-source-provider → lib/strapi/providers/local-source}/links.js +1 -1
  59. package/lib/strapi/providers/remote-destination/index.d.ts +40 -0
  60. package/lib/strapi/providers/remote-destination/index.js +186 -0
  61. package/lib/strapi/providers/remote-destination/utils.d.ts +31 -0
  62. package/lib/strapi/providers/remote-destination/utils.js +72 -0
  63. package/{dist/providers/shared/strapi → lib/strapi/queries}/entity.d.ts +0 -0
  64. package/{dist/providers/shared/strapi → lib/strapi/queries}/entity.js +0 -0
  65. package/{dist/providers/shared/strapi → lib/strapi/queries}/index.d.ts +0 -0
  66. package/{dist/providers/shared/strapi → lib/strapi/queries}/index.js +0 -0
  67. package/{dist/providers/shared/strapi → lib/strapi/queries}/link.d.ts +2 -2
  68. package/{dist/providers/shared/strapi → lib/strapi/queries}/link.js +17 -3
  69. package/lib/strapi/register.d.ts +7 -0
  70. package/lib/strapi/register.js +13 -0
  71. package/lib/strapi/remote/constants.d.ts +2 -0
  72. package/lib/strapi/remote/constants.js +6 -0
  73. package/lib/strapi/remote/controllers/index.d.ts +1 -0
  74. package/lib/strapi/remote/controllers/index.js +18 -0
  75. package/lib/strapi/remote/controllers/push.d.ts +25 -0
  76. package/lib/strapi/remote/controllers/push.js +95 -0
  77. package/lib/strapi/remote/handlers.d.ts +3 -0
  78. package/lib/strapi/remote/handlers.js +193 -0
  79. package/lib/strapi/remote/index.d.ts +3 -0
  80. package/lib/strapi/remote/index.js +30 -0
  81. package/lib/strapi/remote/routes.d.ts +21 -0
  82. package/lib/strapi/remote/routes.js +22 -0
  83. package/lib/utils/encryption/decrypt.d.ts +11 -0
  84. package/{dist → lib/utils}/encryption/decrypt.js +8 -0
  85. package/lib/utils/encryption/encrypt.d.ts +11 -0
  86. package/{dist → lib/utils}/encryption/encrypt.js +8 -0
  87. package/{dist → lib/utils}/encryption/index.d.ts +0 -0
  88. package/{dist → lib/utils}/encryption/index.js +0 -0
  89. package/{dist → lib}/utils/index.d.ts +2 -0
  90. package/{dist → lib}/utils/index.js +3 -1
  91. package/{dist → lib}/utils/json.d.ts +7 -0
  92. package/{dist → lib}/utils/json.js +8 -1
  93. package/lib/utils/providers.d.ts +2 -0
  94. package/lib/utils/providers.js +11 -0
  95. package/{dist → lib}/utils/schema.d.ts +4 -0
  96. package/{dist → lib}/utils/schema.js +9 -2
  97. package/lib/utils/stream.d.ts +27 -0
  98. package/lib/utils/stream.js +59 -0
  99. package/lib/utils/transaction.d.ts +3 -0
  100. package/lib/utils/transaction.js +70 -0
  101. package/package.json +20 -12
  102. package/dist/encryption/decrypt.d.ts +0 -3
  103. package/dist/encryption/encrypt.d.ts +0 -3
  104. package/dist/index.d.ts +0 -2
  105. package/dist/providers/index.d.ts +0 -4
  106. package/dist/providers/local-strapi-destination-provider/strategies/restore/entities.js +0 -88
  107. package/dist/providers/local-strapi-destination-provider/strategies/restore/links.d.ts +0 -3
  108. package/dist/providers/local-strapi-destination-provider/strategies/restore/links.js +0 -29
  109. package/dist/providers/shared/index.d.ts +0 -1
  110. package/dist/providers/test-utils/index.d.ts +0 -111
  111. package/dist/providers/test-utils/index.js +0 -64
  112. package/dist/utils/stream.d.ts +0 -10
  113. package/dist/utils/stream.js +0 -39
@@ -0,0 +1,193 @@
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.createTransferHandler = void 0;
7
+ const crypto_1 = require("crypto");
8
+ const ws_1 = require("ws");
9
+ const push_1 = __importDefault(require("./controllers/push"));
10
+ const providers_1 = require("../../errors/providers");
11
+ const constants_1 = require("./constants");
12
+ const createTransferHandler = (options = {}) => async (ctx) => {
13
+ const upgradeHeader = (ctx.request.headers.upgrade || '')
14
+ .split(',')
15
+ .map((s) => s.trim().toLowerCase());
16
+ // Create the websocket server
17
+ const wss = new ws_1.WebSocket.Server({ ...options, noServer: true });
18
+ if (upgradeHeader.includes('websocket')) {
19
+ wss.handleUpgrade(ctx.req, ctx.request.socket, Buffer.alloc(0), (ws) => {
20
+ // Create a connection between the client & the server
21
+ wss.emit('connection', ws, ctx.req);
22
+ const state = {};
23
+ let uuid;
24
+ /**
25
+ * Format error & message to follow the remote transfer protocol
26
+ */
27
+ const callback = (e = null, data) => {
28
+ return new Promise((resolve, reject) => {
29
+ if (!uuid) {
30
+ reject(new Error('Missing uuid for this message'));
31
+ return;
32
+ }
33
+ const payload = JSON.stringify({
34
+ uuid,
35
+ data: data ?? null,
36
+ error: e
37
+ ? {
38
+ code: 'ERR',
39
+ message: e?.message,
40
+ }
41
+ : null,
42
+ });
43
+ ws.send(payload, (error) => (error ? reject(error) : resolve()));
44
+ });
45
+ };
46
+ /**
47
+ * Wrap a function call to catch errors and answer the request with the correct format
48
+ */
49
+ const answer = async (fn) => {
50
+ try {
51
+ const response = await fn();
52
+ callback(null, response);
53
+ }
54
+ catch (e) {
55
+ if (e instanceof Error) {
56
+ callback(e);
57
+ }
58
+ else if (typeof e === 'string') {
59
+ callback(new providers_1.ProviderTransferError(e));
60
+ }
61
+ else {
62
+ callback(new providers_1.ProviderTransferError('Unexpected error', {
63
+ error: e,
64
+ }));
65
+ }
66
+ }
67
+ };
68
+ const teardown = () => {
69
+ delete state.controller;
70
+ delete state.transfer;
71
+ return { ok: true };
72
+ };
73
+ const init = (msg) => {
74
+ // TODO: this only checks for this instance of node: we should consider a database lock
75
+ if (state.controller) {
76
+ throw new providers_1.ProviderInitializationError('Transfer already in progres');
77
+ }
78
+ const { transfer } = msg.params;
79
+ // Push transfer
80
+ if (transfer === 'push') {
81
+ const { options: controllerOptions } = msg.params;
82
+ state.controller = (0, push_1.default)({
83
+ ...controllerOptions,
84
+ autoDestroy: false,
85
+ getStrapi: () => strapi,
86
+ });
87
+ }
88
+ // Pull or any other string
89
+ else {
90
+ throw new providers_1.ProviderTransferError(`Transfer type not implemented: "${transfer}"`, {
91
+ transfer,
92
+ validTransfers: constants_1.TRANSFER_METHODS,
93
+ });
94
+ }
95
+ state.transfer = { id: (0, crypto_1.randomUUID)(), kind: transfer };
96
+ return { transferID: state.transfer.id };
97
+ };
98
+ /**
99
+ * On command message (init, end, status, ...)
100
+ */
101
+ const onCommand = async (msg) => {
102
+ const { command } = msg;
103
+ if (command === 'init') {
104
+ await answer(() => init(msg));
105
+ }
106
+ if (command === 'end') {
107
+ await answer(teardown);
108
+ }
109
+ if (command === 'status') {
110
+ await callback(new providers_1.ProviderTransferError('Command not implemented: "status"', {
111
+ command,
112
+ validCommands: ['init', 'end', 'status'],
113
+ }));
114
+ }
115
+ };
116
+ const onTransferCommand = async (msg) => {
117
+ const { transferID, kind } = msg;
118
+ const { controller } = state;
119
+ // TODO: (re)move this check
120
+ // It shouldn't be possible to start a pull transfer for now, so reaching
121
+ // this code should be impossible too, but this has been added by security
122
+ if (state.transfer?.kind === 'pull') {
123
+ return callback(new providers_1.ProviderTransferError('Pull transfer not implemented'));
124
+ }
125
+ if (!controller) {
126
+ return callback(new providers_1.ProviderTransferError("The transfer hasn't been initialized"));
127
+ }
128
+ if (!transferID) {
129
+ return callback(new providers_1.ProviderTransferError('Missing transfer ID'));
130
+ }
131
+ // Action
132
+ if (kind === 'action') {
133
+ const { action } = msg;
134
+ if (!(action in controller.actions)) {
135
+ return callback(new providers_1.ProviderTransferError(`Invalid action provided: "${action}"`, {
136
+ action,
137
+ validActions: Object.keys(controller.actions),
138
+ }));
139
+ }
140
+ await answer(() => controller.actions[action]());
141
+ }
142
+ // Transfer
143
+ else if (kind === 'step') {
144
+ // We can only have push transfer message for the moment
145
+ const message = msg;
146
+ // TODO: lock transfer process
147
+ if (message.action === 'start') {
148
+ // console.log('Starting transfer for ', message.step);
149
+ }
150
+ // Stream step
151
+ else if (message.action === 'stream') {
152
+ await answer(() => controller.transfer[message.step]?.(message.data));
153
+ }
154
+ // TODO: unlock transfer process
155
+ else if (message.action === 'end') {
156
+ // console.log('Ending transfer for ', message.step);
157
+ }
158
+ }
159
+ };
160
+ ws.on('close', () => {
161
+ teardown();
162
+ });
163
+ ws.on('error', (e) => {
164
+ teardown();
165
+ // TODO: is logging a console error to the running instance of Strapi ok to do? Should we check for an existing strapi.logger to use?
166
+ console.error(e);
167
+ });
168
+ ws.on('message', async (raw) => {
169
+ const msg = JSON.parse(raw.toString());
170
+ if (!msg.uuid) {
171
+ await callback(new providers_1.ProviderTransferError('Missing uuid in message'));
172
+ return;
173
+ }
174
+ uuid = msg.uuid;
175
+ // Regular command message (init, end, status)
176
+ if (msg.type === 'command') {
177
+ await onCommand(msg);
178
+ }
179
+ // Transfer message (the transfer must be initialized first)
180
+ else if (msg.type === 'transfer') {
181
+ await onTransferCommand(msg);
182
+ }
183
+ // Invalid messages
184
+ else {
185
+ await callback(new providers_1.ProviderTransferError('Bad request'));
186
+ }
187
+ });
188
+ });
189
+ ctx.respond = false;
190
+ }
191
+ };
192
+ exports.createTransferHandler = createTransferHandler;
193
+ //# sourceMappingURL=handlers.js.map
@@ -0,0 +1,3 @@
1
+ export * as controllers from './controllers';
2
+ export * as routes from './routes';
3
+ export * as constants from './constants';
@@ -0,0 +1,30 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.constants = exports.routes = exports.controllers = void 0;
27
+ exports.controllers = __importStar(require("./controllers"));
28
+ exports.routes = __importStar(require("./routes"));
29
+ exports.constants = __importStar(require("./constants"));
30
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,21 @@
1
+ import type { Context } from 'koa';
2
+ declare module '@strapi/strapi' {
3
+ interface Strapi {
4
+ admin: {
5
+ routes: {
6
+ method: string;
7
+ path: string;
8
+ handler: (ctx: Context) => Promise<void>;
9
+ config: unknown;
10
+ }[];
11
+ };
12
+ }
13
+ }
14
+ /**
15
+ * Register a transfer route in the Strapi admin router.
16
+ *
17
+ * It exposes a WS server that can be used to run and manage transfer processes.
18
+ *
19
+ * @param strapi - A Strapi instance
20
+ */
21
+ export declare const registerAdminTransferRoute: (strapi: Strapi.Strapi) => void;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registerAdminTransferRoute = void 0;
4
+ const constants_1 = require("./constants");
5
+ const handlers_1 = require("./handlers");
6
+ /**
7
+ * Register a transfer route in the Strapi admin router.
8
+ *
9
+ * It exposes a WS server that can be used to run and manage transfer processes.
10
+ *
11
+ * @param strapi - A Strapi instance
12
+ */
13
+ const registerAdminTransferRoute = (strapi) => {
14
+ strapi.admin.routes.push({
15
+ method: 'GET',
16
+ path: constants_1.TRANSFER_PATH,
17
+ handler: (0, handlers_1.createTransferHandler)(),
18
+ config: { auth: false },
19
+ });
20
+ };
21
+ exports.registerAdminTransferRoute = registerAdminTransferRoute;
22
+ //# sourceMappingURL=routes.js.map
@@ -0,0 +1,11 @@
1
+ import { Cipher } from 'crypto';
2
+ import { Algorithm } from '../../../types';
3
+ /**
4
+ * It creates a cipher instance used for decryption
5
+ *
6
+ * @param key - The decryption key
7
+ * @param algorithm - The algorithm to use to create the Cipher
8
+ *
9
+ * @returns A {@link Cipher} instance created with the given key & algorithm
10
+ */
11
+ export declare const createDecryptionCipher: (key: string, algorithm?: Algorithm) => Cipher;
@@ -32,6 +32,14 @@ const getDecryptionStrategy = (algorithm) => {
32
32
  };
33
33
  return strategies[algorithm];
34
34
  };
35
+ /**
36
+ * It creates a cipher instance used for decryption
37
+ *
38
+ * @param key - The decryption key
39
+ * @param algorithm - The algorithm to use to create the Cipher
40
+ *
41
+ * @returns A {@link Cipher} instance created with the given key & algorithm
42
+ */
35
43
  const createDecryptionCipher = (key, algorithm = 'aes-128-ecb') => {
36
44
  return getDecryptionStrategy(algorithm)(key);
37
45
  };
@@ -0,0 +1,11 @@
1
+ import { Cipher } from 'crypto';
2
+ import { Algorithm } from '../../../types';
3
+ /**
4
+ * It creates a cipher instance used for encryption
5
+ *
6
+ * @param key - The encryption key
7
+ * @param algorithm - The algorithm to use to create the Cipher
8
+ *
9
+ * @returns A {@link Cipher} instance created with the given key & algorithm
10
+ */
11
+ export declare const createEncryptionCipher: (key: string, algorithm?: Algorithm) => Cipher;
@@ -32,6 +32,14 @@ const getEncryptionStrategy = (algorithm) => {
32
32
  };
33
33
  return strategies[algorithm];
34
34
  };
35
+ /**
36
+ * It creates a cipher instance used for encryption
37
+ *
38
+ * @param key - The encryption key
39
+ * @param algorithm - The algorithm to use to create the Cipher
40
+ *
41
+ * @returns A {@link Cipher} instance created with the given key & algorithm
42
+ */
35
43
  const createEncryptionCipher = (key, algorithm = 'aes-128-ecb') => {
36
44
  return getEncryptionStrategy(algorithm)(key);
37
45
  };
File without changes
File without changes
@@ -1,3 +1,5 @@
1
+ export * as encryption from './encryption';
1
2
  export * as stream from './stream';
2
3
  export * as json from './json';
3
4
  export * as schema from './schema';
5
+ export * as transaction from './transaction';
@@ -23,8 +23,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.schema = exports.json = exports.stream = void 0;
26
+ exports.transaction = exports.schema = exports.json = exports.stream = exports.encryption = void 0;
27
+ exports.encryption = __importStar(require("./encryption"));
27
28
  exports.stream = __importStar(require("./stream"));
28
29
  exports.json = __importStar(require("./json"));
29
30
  exports.schema = __importStar(require("./schema"));
31
+ exports.transaction = __importStar(require("./transaction"));
30
32
  //# sourceMappingURL=index.js.map
@@ -1,3 +1,10 @@
1
+ /**
2
+ * Compute differences between two JSON objects and returns them
3
+ *
4
+ * @param a - First object
5
+ * @param b - Second object
6
+ * @param ctx - Context used to keep track of the current path during recursion
7
+ */
1
8
  export declare const diff: (a: unknown, b: unknown, ctx?: Context) => Diff[];
2
9
  export interface AddedDiff<T = unknown> {
3
10
  kind: 'added';
@@ -3,6 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.diff = void 0;
4
4
  const fp_1 = require("lodash/fp");
5
5
  const createContext = () => ({ path: [] });
6
+ /**
7
+ * Compute differences between two JSON objects and returns them
8
+ *
9
+ * @param a - First object
10
+ * @param b - Second object
11
+ * @param ctx - Context used to keep track of the current path during recursion
12
+ */
6
13
  const diff = (a, b, ctx = createContext()) => {
7
14
  const diffs = [];
8
15
  const { path } = ctx;
@@ -53,7 +60,7 @@ const diff = (a, b, ctx = createContext()) => {
53
60
  return diffs;
54
61
  }
55
62
  if (!(0, fp_1.isEqual)(a, b)) {
56
- modified();
63
+ return modified();
57
64
  }
58
65
  return diffs;
59
66
  };
@@ -0,0 +1,2 @@
1
+ export declare type ValidStrapiAssertion = (strapi: unknown, msg?: string) => asserts strapi is Strapi.Strapi;
2
+ export declare const assertValidStrapi: ValidStrapiAssertion;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertValidStrapi = void 0;
4
+ const providers_1 = require("../errors/providers");
5
+ const assertValidStrapi = (strapi, msg = '') => {
6
+ if (!strapi) {
7
+ throw new providers_1.ProviderInitializationError(`${msg}. Strapi instance not found.`);
8
+ }
9
+ };
10
+ exports.assertValidStrapi = assertValidStrapi;
11
+ //# sourceMappingURL=providers.js.map
@@ -1,3 +1,7 @@
1
1
  /// <reference types="lodash" />
2
2
  import type { Schema } from '@strapi/strapi';
3
+ /**
4
+ * Sanitize a schemas dictionnary by omiting unwanted properties
5
+ * The list of allowed properties can be found here: {@link VALID_SCHEMA_PROPERTIES}
6
+ */
3
7
  export declare const mapSchemasValues: (schemas: Record<string, Schema>) => import("lodash").Dictionary<Partial<Schema>>;
@@ -2,7 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mapSchemasValues = void 0;
4
4
  const fp_1 = require("lodash/fp");
5
- const schemaSelectedKeys = [
5
+ /**
6
+ * List of schema properties that should be kept when sanitizing schemas
7
+ */
8
+ const VALID_SCHEMA_PROPERTIES = [
6
9
  'collectionName',
7
10
  'info',
8
11
  'options',
@@ -15,8 +18,12 @@ const schemaSelectedKeys = [
15
18
  'plugin',
16
19
  'globalId',
17
20
  ];
21
+ /**
22
+ * Sanitize a schemas dictionnary by omiting unwanted properties
23
+ * The list of allowed properties can be found here: {@link VALID_SCHEMA_PROPERTIES}
24
+ */
18
25
  const mapSchemasValues = (schemas) => {
19
- return (0, fp_1.mapValues)((0, fp_1.pick)(schemaSelectedKeys), schemas);
26
+ return (0, fp_1.mapValues)((0, fp_1.pick)(VALID_SCHEMA_PROPERTIES), schemas);
20
27
  };
21
28
  exports.mapSchemasValues = mapSchemasValues;
22
29
  //# sourceMappingURL=schema.js.map
@@ -0,0 +1,27 @@
1
+ /// <reference types="node" />
2
+ import { Transform, Readable } from 'stream';
3
+ declare type TransformOptions = ConstructorParameters<typeof Transform>[0];
4
+ /**
5
+ * Create a filter stream that discard chunks which doesn't satisfies the given predicate
6
+ *
7
+ * @param predicate - A filter predicate, takes a stream data chunk as parameter and returns a boolean value
8
+ * @param options - Transform stream options
9
+ */
10
+ export declare const filter: <T>(predicate: (value: T) => boolean | Promise<boolean>, options?: TransformOptions) => Transform;
11
+ /**
12
+ * Create a map stream that transform chunks using the given predicate
13
+ *
14
+ * @param predicate - A map predicate, takes a stream data chunk as parameter and returns a mapped value
15
+ * @param options - Transform stream options
16
+ */
17
+ export declare const map: <T, U = T>(predicate: (value: T) => U | Promise<U>, options?: TransformOptions) => Transform;
18
+ /**
19
+ * Collect every chunks from a Readable stream.
20
+ *
21
+ * @param stream - The redable stream to collect data from
22
+ * @param options.destroy - If set to true, it automatically calls `destroy()` on the given stream upon receiving the 'end' event
23
+ */
24
+ export declare const collect: <T = unknown>(stream: Readable, options?: {
25
+ destroy: boolean;
26
+ }) => Promise<T[]>;
27
+ export {};
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.collect = exports.map = exports.filter = void 0;
4
+ const stream_1 = require("stream");
5
+ /**
6
+ * Create a filter stream that discard chunks which doesn't satisfies the given predicate
7
+ *
8
+ * @param predicate - A filter predicate, takes a stream data chunk as parameter and returns a boolean value
9
+ * @param options - Transform stream options
10
+ */
11
+ const filter = (predicate, options = { objectMode: true }) => {
12
+ return new stream_1.Transform({
13
+ ...options,
14
+ async transform(chunk, _encoding, callback) {
15
+ const keep = await predicate(chunk);
16
+ callback(null, keep ? chunk : undefined);
17
+ },
18
+ });
19
+ };
20
+ exports.filter = filter;
21
+ /**
22
+ * Create a map stream that transform chunks using the given predicate
23
+ *
24
+ * @param predicate - A map predicate, takes a stream data chunk as parameter and returns a mapped value
25
+ * @param options - Transform stream options
26
+ */
27
+ const map = (predicate, options = { objectMode: true }) => {
28
+ return new stream_1.Transform({
29
+ ...options,
30
+ async transform(chunk, _encoding, callback) {
31
+ const mappedValue = await predicate(chunk);
32
+ callback(null, mappedValue);
33
+ },
34
+ });
35
+ };
36
+ exports.map = map;
37
+ /**
38
+ * Collect every chunks from a Readable stream.
39
+ *
40
+ * @param stream - The redable stream to collect data from
41
+ * @param options.destroy - If set to true, it automatically calls `destroy()` on the given stream upon receiving the 'end' event
42
+ */
43
+ const collect = (stream, options = { destroy: true }) => {
44
+ const chunks = [];
45
+ return new Promise((resolve, reject) => {
46
+ stream
47
+ .on('close', () => resolve(chunks))
48
+ .on('error', reject)
49
+ .on('data', (chunk) => chunks.push(chunk))
50
+ .on('end', () => {
51
+ if (options.destroy) {
52
+ stream.destroy();
53
+ }
54
+ resolve(chunks);
55
+ });
56
+ });
57
+ };
58
+ exports.collect = collect;
59
+ //# sourceMappingURL=stream.js.map
@@ -0,0 +1,3 @@
1
+ import { Strapi } from '@strapi/strapi';
2
+ import { Transaction } from '../../types/utils';
3
+ export declare const createTransaction: (strapi: Strapi) => Transaction;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTransaction = void 0;
4
+ const events_1 = require("events");
5
+ const crypto_1 = require("crypto");
6
+ const createTransaction = (strapi) => {
7
+ const fns = [];
8
+ let done = false;
9
+ let resume = null;
10
+ const e = new events_1.EventEmitter();
11
+ e.on('spawn', (uuid, cb) => {
12
+ fns.push({ fn: cb, uuid });
13
+ resume?.();
14
+ });
15
+ e.on('close', () => {
16
+ done = true;
17
+ resume?.();
18
+ });
19
+ strapi.db.transaction(async ({ trx, rollback }) => {
20
+ e.on('rollback', async () => {
21
+ await rollback();
22
+ });
23
+ while (!done) {
24
+ while (fns.length) {
25
+ const item = fns.shift();
26
+ if (item) {
27
+ const { fn, uuid } = item;
28
+ try {
29
+ const res = await fn(trx);
30
+ e.emit(uuid, { data: res });
31
+ }
32
+ catch (error) {
33
+ e.emit(uuid, { error });
34
+ }
35
+ }
36
+ }
37
+ if (!done && !fns.length) {
38
+ // eslint-disable-next-line @typescript-eslint/no-loop-func
39
+ await new Promise((resolve) => {
40
+ resume = resolve;
41
+ });
42
+ }
43
+ }
44
+ });
45
+ return {
46
+ async attach(callback) {
47
+ const uuid = (0, crypto_1.randomUUID)();
48
+ e.emit('spawn', uuid, callback);
49
+ return new Promise((resolve, reject) => {
50
+ e.on(uuid, ({ data, error }) => {
51
+ if (data) {
52
+ resolve(data);
53
+ }
54
+ if (error) {
55
+ reject(error);
56
+ }
57
+ resolve(undefined);
58
+ });
59
+ });
60
+ },
61
+ end() {
62
+ return e.emit('close');
63
+ },
64
+ rollback() {
65
+ return e.emit('rollback');
66
+ },
67
+ };
68
+ };
69
+ exports.createTransaction = createTransaction;
70
+ //# sourceMappingURL=transaction.js.map