@strapi/data-transfer 4.6.0-alpha.0 → 4.6.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/jest.config.js +3 -1
  2. package/{dist → lib}/engine/index.d.ts +17 -10
  3. package/lib/engine/index.js +359 -0
  4. package/lib/engine/validation/index.d.ts +1 -0
  5. package/lib/engine/validation/index.js +28 -0
  6. package/lib/engine/validation/schemas/index.d.ts +7 -0
  7. package/lib/engine/validation/schemas/index.js +53 -0
  8. package/lib/file/index.d.ts +1 -0
  9. package/lib/file/index.js +28 -0
  10. package/{dist/providers/local-file-destination-provider.d.ts → lib/file/providers/destination/index.d.ts} +10 -6
  11. package/{dist/providers/local-file-destination-provider.js → lib/file/providers/destination/index.js} +59 -87
  12. package/lib/file/providers/destination/utils.d.ts +9 -0
  13. package/lib/file/providers/destination/utils.js +61 -0
  14. package/lib/file/providers/index.d.ts +2 -0
  15. package/{dist → lib/file}/providers/index.js +2 -6
  16. package/lib/file/providers/source/index.d.ts +40 -0
  17. package/{dist/providers/local-file-source-provider.js → lib/file/providers/source/index.js} +94 -51
  18. package/lib/index.d.ts +4 -0
  19. package/lib/index.js +31 -0
  20. package/lib/strapi/index.d.ts +4 -0
  21. package/lib/strapi/index.js +35 -0
  22. package/lib/strapi/providers/index.d.ts +3 -0
  23. package/lib/strapi/providers/index.js +22 -0
  24. package/lib/strapi/providers/local-destination/index.d.ts +32 -0
  25. package/lib/strapi/providers/local-destination/index.js +195 -0
  26. package/lib/strapi/providers/local-destination/strategies/index.d.ts +1 -0
  27. package/lib/strapi/providers/local-destination/strategies/index.js +28 -0
  28. package/lib/strapi/providers/local-destination/strategies/restore/configuration.d.ts +5 -0
  29. package/lib/strapi/providers/local-destination/strategies/restore/configuration.js +44 -0
  30. package/lib/strapi/providers/local-destination/strategies/restore/entities.d.ts +9 -0
  31. package/lib/strapi/providers/local-destination/strategies/restore/entities.js +100 -0
  32. package/lib/strapi/providers/local-destination/strategies/restore/index.d.ts +32 -0
  33. package/lib/strapi/providers/local-destination/strategies/restore/index.js +106 -0
  34. package/lib/strapi/providers/local-destination/strategies/restore/links.d.ts +3 -0
  35. package/lib/strapi/providers/local-destination/strategies/restore/links.js +29 -0
  36. package/lib/strapi/providers/local-source/assets.d.ts +5 -0
  37. package/lib/strapi/providers/local-source/assets.js +31 -0
  38. package/{dist/providers/local-strapi-source-provider → lib/strapi/providers/local-source}/configuration.d.ts +0 -0
  39. package/{dist/providers/local-strapi-source-provider → lib/strapi/providers/local-source}/configuration.js +13 -14
  40. package/{dist/providers/local-strapi-source-provider → lib/strapi/providers/local-source}/entities.d.ts +0 -0
  41. package/lib/strapi/providers/local-source/entities.js +85 -0
  42. package/{dist/providers/local-strapi-source-provider → lib/strapi/providers/local-source}/index.d.ts +9 -8
  43. package/{dist/providers/local-strapi-source-provider → lib/strapi/providers/local-source}/index.js +36 -6
  44. package/{dist/providers/local-strapi-source-provider/links/index.d.ts → lib/strapi/providers/local-source/links.d.ts} +1 -1
  45. package/lib/strapi/providers/local-source/links.js +23 -0
  46. package/lib/strapi/providers/remote-destination/index.d.ts +40 -0
  47. package/lib/strapi/providers/remote-destination/index.js +171 -0
  48. package/lib/strapi/providers/remote-destination/utils.d.ts +31 -0
  49. package/lib/strapi/providers/remote-destination/utils.js +72 -0
  50. package/lib/strapi/queries/entity.d.ts +19 -0
  51. package/lib/strapi/queries/entity.js +130 -0
  52. package/lib/strapi/queries/index.d.ts +2 -0
  53. package/lib/strapi/queries/index.js +29 -0
  54. package/lib/strapi/queries/link.d.ts +6 -0
  55. package/lib/strapi/queries/link.js +201 -0
  56. package/lib/strapi/register.d.ts +7 -0
  57. package/lib/strapi/register.js +13 -0
  58. package/lib/strapi/remote/constants.d.ts +1 -0
  59. package/lib/strapi/remote/constants.js +5 -0
  60. package/lib/strapi/remote/controllers/index.d.ts +1 -0
  61. package/{dist → lib/strapi/remote/controllers}/index.js +1 -2
  62. package/lib/strapi/remote/controllers/push.d.ts +25 -0
  63. package/lib/strapi/remote/controllers/push.js +95 -0
  64. package/lib/strapi/remote/handlers.d.ts +3 -0
  65. package/lib/strapi/remote/handlers.js +177 -0
  66. package/lib/strapi/remote/index.d.ts +3 -0
  67. package/lib/strapi/remote/index.js +30 -0
  68. package/lib/strapi/remote/routes.d.ts +21 -0
  69. package/lib/strapi/remote/routes.js +22 -0
  70. package/lib/utils/encryption/decrypt.d.ts +11 -0
  71. package/{dist → lib/utils}/encryption/decrypt.js +12 -4
  72. package/lib/utils/encryption/encrypt.d.ts +11 -0
  73. package/{dist → lib/utils}/encryption/encrypt.js +12 -4
  74. package/{dist → lib/utils}/encryption/index.d.ts +0 -0
  75. package/{dist → lib/utils}/encryption/index.js +0 -0
  76. package/lib/utils/index.d.ts +4 -0
  77. package/lib/utils/index.js +31 -0
  78. package/lib/utils/json.d.ts +30 -0
  79. package/{dist/utils.js → lib/utils/json.js} +16 -38
  80. package/lib/utils/schema.d.ts +7 -0
  81. package/lib/utils/schema.js +29 -0
  82. package/lib/utils/stream.d.ts +27 -0
  83. package/lib/utils/stream.js +59 -0
  84. package/package.json +19 -12
  85. package/dist/encryption/decrypt.d.ts +0 -3
  86. package/dist/encryption/encrypt.d.ts +0 -3
  87. package/dist/engine/index.js +0 -324
  88. package/dist/index.d.ts +0 -2
  89. package/dist/providers/index.d.ts +0 -4
  90. package/dist/providers/local-file-source-provider.d.ts +0 -43
  91. package/dist/providers/local-strapi-destination-provider.d.ts +0 -22
  92. package/dist/providers/local-strapi-destination-provider.js +0 -78
  93. package/dist/providers/local-strapi-source-provider/entities.js +0 -58
  94. package/dist/providers/local-strapi-source-provider/links/index.js +0 -37
  95. package/dist/providers/local-strapi-source-provider/links/utils.d.ts +0 -27
  96. package/dist/providers/local-strapi-source-provider/links/utils.js +0 -155
  97. package/dist/strategies/index.d.ts +0 -7
  98. package/dist/strategies/index.js +0 -29
  99. package/dist/utils.d.ts +0 -10
@@ -0,0 +1,177 @@
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 createTransferHandler = (options = {}) => async (ctx) => {
11
+ const upgradeHeader = (ctx.request.headers.upgrade || '')
12
+ .split(',')
13
+ .map((s) => s.trim().toLowerCase());
14
+ // Create the websocket server
15
+ const wss = new ws_1.WebSocket.Server({ ...options, noServer: true });
16
+ if (upgradeHeader.includes('websocket')) {
17
+ wss.handleUpgrade(ctx.req, ctx.request.socket, Buffer.alloc(0), (ws) => {
18
+ // Create a connection between the client & the server
19
+ wss.emit('connection', ws, ctx.req);
20
+ const state = {};
21
+ let uuid;
22
+ /**
23
+ * Format error & message to follow the remote transfer protocol
24
+ */
25
+ const callback = (e = null, data) => {
26
+ return new Promise((resolve, reject) => {
27
+ if (!uuid) {
28
+ reject(new Error('Missing uuid for this message'));
29
+ return;
30
+ }
31
+ const payload = JSON.stringify({
32
+ uuid,
33
+ data: data ?? null,
34
+ error: e
35
+ ? {
36
+ code: 'ERR',
37
+ message: e?.message,
38
+ }
39
+ : null,
40
+ });
41
+ ws.send(payload, (error) => (error ? reject(error) : resolve()));
42
+ });
43
+ };
44
+ /**
45
+ * Wrap a function call to catch errors and answer the request with the correct format
46
+ */
47
+ const answer = async (fn) => {
48
+ try {
49
+ const response = await fn();
50
+ callback(null, response);
51
+ }
52
+ catch (e) {
53
+ if (e instanceof Error) {
54
+ callback(e);
55
+ }
56
+ else if (typeof e === 'string') {
57
+ callback(new Error(e));
58
+ }
59
+ else {
60
+ callback(new Error('Unexpected error'));
61
+ }
62
+ }
63
+ };
64
+ const teardown = () => {
65
+ delete state.controller;
66
+ delete state.transfer;
67
+ return { ok: true };
68
+ };
69
+ const init = (msg) => {
70
+ if (state.controller) {
71
+ throw new Error('Transfer already in progres');
72
+ }
73
+ const { transfer } = msg.params;
74
+ // Push transfer
75
+ if (transfer === 'push') {
76
+ const { options: controllerOptions } = msg.params;
77
+ state.controller = (0, push_1.default)({
78
+ ...controllerOptions,
79
+ autoDestroy: false,
80
+ getStrapi: () => strapi,
81
+ });
82
+ }
83
+ // Pull or any other string
84
+ else {
85
+ throw new Error(`Transfer not implemented: "${transfer}"`);
86
+ }
87
+ state.transfer = { id: (0, crypto_1.randomUUID)(), kind: transfer };
88
+ return { transferID: state.transfer.id };
89
+ };
90
+ /**
91
+ * On command message (init, end, status, ...)
92
+ */
93
+ const onCommand = async (msg) => {
94
+ const { command } = msg;
95
+ if (command === 'init') {
96
+ await answer(() => init(msg));
97
+ }
98
+ if (command === 'end') {
99
+ await answer(teardown);
100
+ }
101
+ if (command === 'status') {
102
+ await callback(new Error('Command not implemented: "status"'));
103
+ }
104
+ };
105
+ const onTransferCommand = async (msg) => {
106
+ const { transferID, kind } = msg;
107
+ const { controller } = state;
108
+ // TODO: (re)move this check
109
+ // It shouldn't be possible to strart a pull transfer for now, so reaching
110
+ // this code should be impossible too, but this has been added by security
111
+ if (state.transfer?.kind === 'pull') {
112
+ return callback(new Error('Pull transfer not implemented'));
113
+ }
114
+ if (!controller) {
115
+ return callback(new Error("The transfer hasn't been initialized"));
116
+ }
117
+ if (!transferID) {
118
+ return callback(new Error('Missing transfer ID'));
119
+ }
120
+ // Action
121
+ if (kind === 'action') {
122
+ const { action } = msg;
123
+ if (!(action in controller.actions)) {
124
+ return callback(new Error(`Invalid action provided: "${action}"`));
125
+ }
126
+ await answer(() => controller.actions[action]());
127
+ }
128
+ // Transfer
129
+ else if (kind === 'step') {
130
+ // We can only have push transfer message for the moment
131
+ const message = msg;
132
+ // TODO: lock transfer process
133
+ if (message.action === 'start') {
134
+ // console.log('Starting transfer for ', message.step);
135
+ }
136
+ // Stream step
137
+ else if (message.action === 'stream') {
138
+ await answer(() => controller.transfer[message.step]?.(message.data));
139
+ }
140
+ // TODO: unlock transfer process
141
+ else if (message.action === 'end') {
142
+ // console.log('Ending transfer for ', message.step);
143
+ }
144
+ }
145
+ };
146
+ ws.on('close', () => {
147
+ teardown();
148
+ });
149
+ ws.on('error', (e) => {
150
+ teardown();
151
+ console.error(e);
152
+ });
153
+ ws.on('message', async (raw) => {
154
+ const msg = JSON.parse(raw.toString());
155
+ if (!msg.uuid) {
156
+ throw new Error('Missing uuid in message');
157
+ }
158
+ uuid = msg.uuid;
159
+ // Regular command message (init, end, status)
160
+ if (msg.type === 'command') {
161
+ await onCommand(msg);
162
+ }
163
+ // Transfer message (the transfer must be initialized first)
164
+ else if (msg.type === 'transfer') {
165
+ await onTransferCommand(msg);
166
+ }
167
+ // Invalid messages
168
+ else {
169
+ await callback(new Error('Bad request'));
170
+ }
171
+ });
172
+ });
173
+ ctx.respond = false;
174
+ }
175
+ };
176
+ exports.createTransferHandler = createTransferHandler;
177
+ //# 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;
@@ -5,25 +5,25 @@ const crypto_1 = require("crypto");
5
5
  // different key values depending on algorithm chosen
6
6
  const getDecryptionStrategy = (algorithm) => {
7
7
  const strategies = {
8
- 'aes-128-ecb': (key) => {
8
+ 'aes-128-ecb'(key) {
9
9
  const hashedKey = (0, crypto_1.scryptSync)(key, '', 16);
10
10
  const initVector = null;
11
11
  const securityKey = hashedKey;
12
12
  return (0, crypto_1.createDecipheriv)(algorithm, securityKey, initVector);
13
13
  },
14
- aes128: (key) => {
14
+ aes128(key) {
15
15
  const hashedKey = (0, crypto_1.scryptSync)(key, '', 32);
16
16
  const initVector = hashedKey.slice(16);
17
17
  const securityKey = hashedKey.slice(0, 16);
18
18
  return (0, crypto_1.createDecipheriv)(algorithm, securityKey, initVector);
19
19
  },
20
- aes192: (key) => {
20
+ aes192(key) {
21
21
  const hashedKey = (0, crypto_1.scryptSync)(key, '', 40);
22
22
  const initVector = hashedKey.slice(24);
23
23
  const securityKey = hashedKey.slice(0, 24);
24
24
  return (0, crypto_1.createDecipheriv)(algorithm, securityKey, initVector);
25
25
  },
26
- aes256: (key) => {
26
+ aes256(key) {
27
27
  const hashedKey = (0, crypto_1.scryptSync)(key, '', 48);
28
28
  const initVector = hashedKey.slice(32);
29
29
  const securityKey = hashedKey.slice(0, 32);
@@ -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;
@@ -5,25 +5,25 @@ const crypto_1 = require("crypto");
5
5
  // different key values depending on algorithm chosen
6
6
  const getEncryptionStrategy = (algorithm) => {
7
7
  const strategies = {
8
- 'aes-128-ecb': (key) => {
8
+ 'aes-128-ecb'(key) {
9
9
  const hashedKey = (0, crypto_1.scryptSync)(key, '', 16);
10
10
  const initVector = null;
11
11
  const securityKey = hashedKey;
12
12
  return (0, crypto_1.createCipheriv)(algorithm, securityKey, initVector);
13
13
  },
14
- aes128: (key) => {
14
+ aes128(key) {
15
15
  const hashedKey = (0, crypto_1.scryptSync)(key, '', 32);
16
16
  const initVector = hashedKey.slice(16);
17
17
  const securityKey = hashedKey.slice(0, 16);
18
18
  return (0, crypto_1.createCipheriv)(algorithm, securityKey, initVector);
19
19
  },
20
- aes192: (key) => {
20
+ aes192(key) {
21
21
  const hashedKey = (0, crypto_1.scryptSync)(key, '', 40);
22
22
  const initVector = hashedKey.slice(24);
23
23
  const securityKey = hashedKey.slice(0, 24);
24
24
  return (0, crypto_1.createCipheriv)(algorithm, securityKey, initVector);
25
25
  },
26
- aes256: (key) => {
26
+ aes256(key) {
27
27
  const hashedKey = (0, crypto_1.scryptSync)(key, '', 48);
28
28
  const initVector = hashedKey.slice(32);
29
29
  const securityKey = hashedKey.slice(0, 32);
@@ -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
@@ -0,0 +1,4 @@
1
+ export * as encryption from './encryption';
2
+ export * as stream from './stream';
3
+ export * as json from './json';
4
+ export * as schema from './schema';
@@ -0,0 +1,31 @@
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.schema = exports.json = exports.stream = exports.encryption = void 0;
27
+ exports.encryption = __importStar(require("./encryption"));
28
+ exports.stream = __importStar(require("./stream"));
29
+ exports.json = __importStar(require("./json"));
30
+ exports.schema = __importStar(require("./schema"));
31
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,30 @@
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
+ */
8
+ export declare const diff: (a: unknown, b: unknown, ctx?: Context) => Diff[];
9
+ export interface AddedDiff<T = unknown> {
10
+ kind: 'added';
11
+ path: string[];
12
+ type: string;
13
+ value: T;
14
+ }
15
+ export interface ModifiedDiff<T = unknown, P = unknown> {
16
+ kind: 'modified';
17
+ path: string[];
18
+ types: [string, string];
19
+ values: [T, P];
20
+ }
21
+ export interface DeletedDiff<T = unknown> {
22
+ kind: 'deleted';
23
+ path: string[];
24
+ type: string;
25
+ value: T;
26
+ }
27
+ export declare type Diff = AddedDiff | ModifiedDiff | DeletedDiff;
28
+ export interface Context {
29
+ path: string[];
30
+ }
@@ -1,25 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mapSchemasValues = exports.jsonDiffs = exports.collect = void 0;
3
+ exports.diff = void 0;
4
4
  const fp_1 = require("lodash/fp");
5
+ const createContext = () => ({ path: [] });
5
6
  /**
6
- * Collect every entity in a Readable stream
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
7
12
  */
8
- const collect = (stream) => {
9
- const chunks = [];
10
- return new Promise((resolve) => {
11
- stream.on('data', (chunk) => chunks.push(chunk));
12
- stream.on('end', () => {
13
- stream.destroy();
14
- resolve(chunks);
15
- });
16
- });
17
- };
18
- exports.collect = collect;
19
- const createContext = () => ({ path: [] });
20
- const jsonDiffs = (a, b, ctx = createContext()) => {
13
+ const diff = (a, b, ctx = createContext()) => {
21
14
  const diffs = [];
22
15
  const { path } = ctx;
16
+ const aType = typeof a;
17
+ const bType = typeof b;
23
18
  // Define helpers
24
19
  const added = () => {
25
20
  diffs.push({ kind: 'added', path, type: bType, value: b });
@@ -38,8 +33,6 @@ const jsonDiffs = (a, b, ctx = createContext()) => {
38
33
  });
39
34
  return diffs;
40
35
  };
41
- const aType = typeof a;
42
- const bType = typeof b;
43
36
  if (aType === 'undefined') {
44
37
  return added();
45
38
  }
@@ -50,9 +43,9 @@ const jsonDiffs = (a, b, ctx = createContext()) => {
50
43
  let k = 0;
51
44
  for (const [aItem, bItem] of (0, fp_1.zip)(a, b)) {
52
45
  const kCtx = { path: [...path, k.toString()] };
53
- const kDiffs = (0, exports.jsonDiffs)(aItem, bItem, kCtx);
46
+ const kDiffs = (0, exports.diff)(aItem, bItem, kCtx);
54
47
  diffs.push(...kDiffs);
55
- k++;
48
+ k += 1;
56
49
  }
57
50
  return diffs;
58
51
  }
@@ -61,30 +54,15 @@ const jsonDiffs = (a, b, ctx = createContext()) => {
61
54
  for (const key of keys) {
62
55
  const aValue = a[key];
63
56
  const bValue = b[key];
64
- const nestedDiffs = (0, exports.jsonDiffs)(aValue, bValue, { path: [...path, key] });
57
+ const nestedDiffs = (0, exports.diff)(aValue, bValue, { path: [...path, key] });
65
58
  diffs.push(...nestedDiffs);
66
59
  }
67
60
  return diffs;
68
61
  }
69
62
  if (!(0, fp_1.isEqual)(a, b)) {
70
- modified();
63
+ return modified();
71
64
  }
72
65
  return diffs;
73
66
  };
74
- exports.jsonDiffs = jsonDiffs;
75
- const schemaSelectedKeys = [
76
- 'collectionName',
77
- 'info',
78
- 'options',
79
- 'pluginOptions',
80
- 'attributes',
81
- 'kind',
82
- 'modelType',
83
- 'modelName',
84
- 'uid',
85
- 'plugin',
86
- 'globalId',
87
- ];
88
- const mapSchemasValues = (schemas) => (0, fp_1.mapValues)((0, fp_1.pick)(schemaSelectedKeys), schemas);
89
- exports.mapSchemasValues = mapSchemasValues;
90
- //# sourceMappingURL=utils.js.map
67
+ exports.diff = diff;
68
+ //# sourceMappingURL=json.js.map
@@ -0,0 +1,7 @@
1
+ /// <reference types="lodash" />
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
+ */
7
+ export declare const mapSchemasValues: (schemas: Record<string, Schema>) => import("lodash").Dictionary<Partial<Schema>>;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mapSchemasValues = void 0;
4
+ const fp_1 = require("lodash/fp");
5
+ /**
6
+ * List of schema properties that should be kept when sanitizing schemas
7
+ */
8
+ const VALID_SCHEMA_PROPERTIES = [
9
+ 'collectionName',
10
+ 'info',
11
+ 'options',
12
+ 'pluginOptions',
13
+ 'attributes',
14
+ 'kind',
15
+ 'modelType',
16
+ 'modelName',
17
+ 'uid',
18
+ 'plugin',
19
+ 'globalId',
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
+ */
25
+ const mapSchemasValues = (schemas) => {
26
+ return (0, fp_1.mapValues)((0, fp_1.pick)(VALID_SCHEMA_PROPERTIES), schemas);
27
+ };
28
+ exports.mapSchemasValues = mapSchemasValues;
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