@webiny/api-elasticsearch 0.0.0-mt-1

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 (65) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/client.d.ts +5 -0
  4. package/client.js +47 -0
  5. package/compression.d.ts +6 -0
  6. package/compression.js +68 -0
  7. package/cursors.d.ts +9 -0
  8. package/cursors.js +49 -0
  9. package/index.d.ts +9 -0
  10. package/index.js +38 -0
  11. package/limit.d.ts +1 -0
  12. package/limit.js +45 -0
  13. package/normalize.d.ts +5 -0
  14. package/normalize.js +27 -0
  15. package/operators.d.ts +18 -0
  16. package/operators.js +42 -0
  17. package/package.json +49 -0
  18. package/plugins/GzipCompression.d.ts +19 -0
  19. package/plugins/GzipCompression.js +75 -0
  20. package/plugins/definition/CompressionPlugin.d.ts +20 -0
  21. package/plugins/definition/CompressionPlugin.js +17 -0
  22. package/plugins/definition/ElasticsearchBodyModifierPlugin.d.ts +13 -0
  23. package/plugins/definition/ElasticsearchBodyModifierPlugin.js +35 -0
  24. package/plugins/definition/ElasticsearchFieldPlugin.d.ts +87 -0
  25. package/plugins/definition/ElasticsearchFieldPlugin.js +128 -0
  26. package/plugins/definition/ElasticsearchQueryBuilderOperatorPlugin.d.ts +10 -0
  27. package/plugins/definition/ElasticsearchQueryBuilderOperatorPlugin.js +20 -0
  28. package/plugins/definition/ElasticsearchQueryModifierPlugin.d.ts +14 -0
  29. package/plugins/definition/ElasticsearchQueryModifierPlugin.js +35 -0
  30. package/plugins/definition/ElasticsearchSortModifierPlugin.d.ts +13 -0
  31. package/plugins/definition/ElasticsearchSortModifierPlugin.js +35 -0
  32. package/plugins/operator/andIn.d.ts +7 -0
  33. package/plugins/operator/andIn.js +56 -0
  34. package/plugins/operator/between.d.ts +7 -0
  35. package/plugins/operator/between.js +51 -0
  36. package/plugins/operator/contains.d.ts +7 -0
  37. package/plugins/operator/contains.js +43 -0
  38. package/plugins/operator/equal.d.ts +7 -0
  39. package/plugins/operator/equal.js +45 -0
  40. package/plugins/operator/gt.d.ts +7 -0
  41. package/plugins/operator/gt.js +40 -0
  42. package/plugins/operator/gte.d.ts +7 -0
  43. package/plugins/operator/gte.js +40 -0
  44. package/plugins/operator/in.d.ts +7 -0
  45. package/plugins/operator/in.js +54 -0
  46. package/plugins/operator/index.d.ts +13 -0
  47. package/plugins/operator/index.js +174 -0
  48. package/plugins/operator/lt.d.ts +7 -0
  49. package/plugins/operator/lt.js +40 -0
  50. package/plugins/operator/lte.d.ts +7 -0
  51. package/plugins/operator/lte.js +40 -0
  52. package/plugins/operator/not.d.ts +7 -0
  53. package/plugins/operator/not.js +40 -0
  54. package/plugins/operator/notBetween.d.ts +7 -0
  55. package/plugins/operator/notBetween.js +51 -0
  56. package/plugins/operator/notContains.d.ts +7 -0
  57. package/plugins/operator/notContains.js +43 -0
  58. package/plugins/operator/notIn.d.ts +7 -0
  59. package/plugins/operator/notIn.js +46 -0
  60. package/sort.d.ts +12 -0
  61. package/sort.js +67 -0
  62. package/types.d.ts +49 -0
  63. package/types.js +18 -0
  64. package/where.d.ts +17 -0
  65. package/where.js +106 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Webiny
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @webiny/api-elasticsearch
package/client.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { Client, ClientOptions } from "@elastic/elasticsearch";
2
+ export interface ElasticsearchClientOptions extends ClientOptions {
3
+ endpoint?: string;
4
+ }
5
+ export declare const createElasticsearchClient: (options: ElasticsearchClientOptions) => Client;
package/client.js ADDED
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.createElasticsearchClient = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
13
+
14
+ var _elasticsearch = require("@elastic/elasticsearch");
15
+
16
+ var _awsSdk = _interopRequireDefault(require("aws-sdk"));
17
+
18
+ var _awsElasticsearchConnector = _interopRequireDefault(require("aws-elasticsearch-connector"));
19
+
20
+ const _excluded = ["endpoint", "node"];
21
+
22
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
23
+
24
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
25
+
26
+ const createElasticsearchClient = options => {
27
+ const {
28
+ endpoint,
29
+ node
30
+ } = options,
31
+ rest = (0, _objectWithoutProperties2.default)(options, _excluded);
32
+
33
+ const clientOptions = _objectSpread({
34
+ node: endpoint || node
35
+ }, rest);
36
+
37
+ if (!clientOptions.auth) {
38
+ /**
39
+ * If no `auth` configuration is present, we setup AWS connector.
40
+ */
41
+ Object.assign(clientOptions, (0, _awsElasticsearchConnector.default)(_awsSdk.default.config));
42
+ }
43
+
44
+ return new _elasticsearch.Client(clientOptions);
45
+ };
46
+
47
+ exports.createElasticsearchClient = createElasticsearchClient;
@@ -0,0 +1,6 @@
1
+ import { PluginsContainer } from "@webiny/plugins";
2
+ /**
3
+ * Method to compress the elasticsearch data that is going to be stored into the DynamoDB table that is meant for elasticsearch.
4
+ */
5
+ export declare const compress: (pluginsContainer: PluginsContainer, data: Record<string, any>) => Promise<Record<string, any>>;
6
+ export declare const decompress: (pluginsContainer: PluginsContainer, data: Record<string, any>) => Promise<Record<string, any>>;
package/compression.js ADDED
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.decompress = exports.compress = void 0;
7
+
8
+ var _CompressionPlugin = require("./plugins/definition/CompressionPlugin");
9
+
10
+ /**
11
+ * Get the compression plugins, in reverse order, because we want to use the last one added - first.
12
+ */
13
+ const getCompressionPlugins = plugins => {
14
+ return plugins.byType(_CompressionPlugin.CompressionPlugin.type).reverse();
15
+ };
16
+ /**
17
+ * Method to compress the elasticsearch data that is going to be stored into the DynamoDB table that is meant for elasticsearch.
18
+ */
19
+
20
+
21
+ const compress = async (pluginsContainer, data) => {
22
+ const plugins = getCompressionPlugins(pluginsContainer);
23
+
24
+ if (plugins.length === 0) {
25
+ console.log("No compression plugins");
26
+ return data;
27
+ }
28
+
29
+ for (const plugin of plugins) {
30
+ if (plugin.canCompress(data) === false) {
31
+ continue;
32
+ }
33
+
34
+ return await plugin.compress(data);
35
+ }
36
+ /**
37
+ * Possibly no plugins that can compress, just return the data.
38
+ */
39
+
40
+
41
+ return data;
42
+ };
43
+
44
+ exports.compress = compress;
45
+
46
+ const decompress = async (pluginsContainer, data) => {
47
+ const plugins = getCompressionPlugins(pluginsContainer);
48
+
49
+ if (plugins.length === 0) {
50
+ return data;
51
+ }
52
+
53
+ for (const plugin of plugins) {
54
+ if (plugin.canDecompress(data) === false) {
55
+ continue;
56
+ }
57
+
58
+ return await plugin.decompress(data);
59
+ }
60
+ /**
61
+ * Possibly no plugins that can decompress, just return the data.
62
+ */
63
+
64
+
65
+ return data;
66
+ };
67
+
68
+ exports.decompress = decompress;
package/cursors.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Encode a received cursor value into something that can be passed on to the user.
3
+ */
4
+ export declare const encodeCursor: (cursor?: string | string[]) => string | undefined;
5
+ /**
6
+ * Decode a received value into a Elasticsearch cursor.
7
+ * If no value is received or is not decodable, return undefined.
8
+ */
9
+ export declare const decodeCursor: (cursor?: string) => string[] | string | undefined;
package/cursors.js ADDED
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.encodeCursor = exports.decodeCursor = void 0;
7
+
8
+ /**
9
+ * Encode a received cursor value into something that can be passed on to the user.
10
+ */
11
+ const encodeCursor = cursor => {
12
+ if (!cursor) {
13
+ return undefined;
14
+ }
15
+
16
+ cursor = Array.isArray(cursor) ? cursor.map(encodeURIComponent) : encodeURIComponent(cursor);
17
+
18
+ try {
19
+ return Buffer.from(JSON.stringify(cursor)).toString("base64");
20
+ } catch (ex) {
21
+ console.error(ex.message);
22
+ }
23
+
24
+ return undefined;
25
+ };
26
+ /**
27
+ * Decode a received value into a Elasticsearch cursor.
28
+ * If no value is received or is not decodable, return undefined.
29
+ */
30
+
31
+
32
+ exports.encodeCursor = encodeCursor;
33
+
34
+ const decodeCursor = cursor => {
35
+ if (!cursor) {
36
+ return undefined;
37
+ }
38
+
39
+ try {
40
+ const value = JSON.parse(Buffer.from(cursor, "base64").toString("ascii"));
41
+ return Array.isArray(value) ? value.map(decodeURIComponent) : decodeURIComponent(value);
42
+ } catch (ex) {
43
+ console.error(ex.message);
44
+ }
45
+
46
+ return undefined;
47
+ };
48
+
49
+ exports.decodeCursor = decodeCursor;
package/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { ElasticsearchContext } from "./types";
2
+ import { ContextPlugin } from "@webiny/handler/plugins/ContextPlugin";
3
+ import { ElasticsearchClientOptions } from "./client";
4
+ import { Client } from "@elastic/elasticsearch";
5
+ declare const _default: (params: ElasticsearchClientOptions | Client) => ContextPlugin<ElasticsearchContext>;
6
+ /**
7
+ * We must accept either Elasticsearch client or options that create the client.
8
+ */
9
+ export default _default;
package/index.js ADDED
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+
10
+ var _ContextPlugin = require("@webiny/handler/plugins/ContextPlugin");
11
+
12
+ var _error = _interopRequireDefault(require("@webiny/error"));
13
+
14
+ var _client = require("./client");
15
+
16
+ var _operators = require("./operators");
17
+
18
+ var _elasticsearch = require("@elastic/elasticsearch");
19
+
20
+ /**
21
+ * We must accept either Elasticsearch client or options that create the client.
22
+ */
23
+ var _default = params => {
24
+ return new _ContextPlugin.ContextPlugin(context => {
25
+ if (context.elasticsearch) {
26
+ throw new _error.default("Elasticsearch client is already initialized, no need to define it again. Check your code for duplicate initializations.", "ELASTICSEARCH_ALREADY_INITIALIZED");
27
+ }
28
+ /**
29
+ * Initialize the Elasticsearch client.
30
+ */
31
+
32
+
33
+ context.elasticsearch = params instanceof _elasticsearch.Client ? params : (0, _client.createElasticsearchClient)(params);
34
+ context.plugins.register((0, _operators.getElasticsearchOperators)());
35
+ });
36
+ };
37
+
38
+ exports.default = _default;
package/limit.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const createLimit: (limit?: number, defaultValue?: number) => number;
package/limit.js ADDED
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createLimit = void 0;
7
+
8
+ /**
9
+ * This is the max limit of the Elasticsearch.
10
+ * Change here if it changes (and if it is necessary).
11
+ */
12
+ const ES_LIMIT_MAX = 10000;
13
+ /**
14
+ * Our system default limit.
15
+ */
16
+
17
+ const ES_LIMIT_DEFAULT = 50;
18
+
19
+ const createLimit = (limit, defaultValue = ES_LIMIT_DEFAULT) => {
20
+ /**
21
+ * Limit can possibly be null/undefined or less than 1.
22
+ * In that case return the defaults.
23
+ */
24
+ if (!limit || limit < 1) {
25
+ return defaultValue;
26
+ }
27
+ /**
28
+ * Users input limit cannot be greater than the Elasticsearch one.
29
+ * Elasticsearch query breaks because of that.
30
+ */
31
+
32
+
33
+ if (limit < ES_LIMIT_MAX) {
34
+ return limit;
35
+ }
36
+ /**
37
+ * Always reduce by 1 because we check if there are more items by adding 1 to the limit
38
+ * and then remove that last one loaded.
39
+ */
40
+
41
+
42
+ return ES_LIMIT_MAX - 1;
43
+ };
44
+
45
+ exports.createLimit = createLimit;
package/normalize.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Before performing the query, we need to escape all special characters.
3
+ * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters
4
+ */
5
+ export declare const normalizeValue: (value: string) => string;
package/normalize.js ADDED
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.normalizeValue = void 0;
7
+
8
+ /**
9
+ * Before performing the query, we need to escape all special characters.
10
+ * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters
11
+ */
12
+ const RESERVED_CHARACTERS = {
13
+ // These characters need to be escaped with backslash ("\").
14
+ escape: ["\\\\", "\\/", "\\+", "\\-", "\\=", "\\&\\&", "\\|\\|", "\\!", "\\(", "\\)", "\\{", "\\}", "\\[", "\\]", "\\^", '\\"', "\\~", "\\*", "\\?", "\\:", "\\>", "\\<"]
15
+ };
16
+
17
+ const normalizeValue = value => {
18
+ let result = value;
19
+
20
+ for (const character of RESERVED_CHARACTERS.escape) {
21
+ result = result.replace(new RegExp(`${character}`, "g"), ` `);
22
+ }
23
+
24
+ return result ? `*${result}*` : "";
25
+ };
26
+
27
+ exports.normalizeValue = normalizeValue;
package/operators.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { ElasticsearchQueryBuilderOperatorBetweenPlugin } from "./plugins/operator/between";
2
+ import { ElasticsearchQueryBuilderOperatorNotBetweenPlugin } from "./plugins/operator/notBetween";
3
+ import { ElasticsearchQueryBuilderOperatorContainsPlugin } from "./plugins/operator/contains";
4
+ import { ElasticsearchQueryBuilderOperatorNotContainsPlugin } from "./plugins/operator/notContains";
5
+ import { ElasticsearchQueryBuilderOperatorEqualPlugin } from "./plugins/operator/equal";
6
+ import { ElasticsearchQueryBuilderOperatorNotPlugin } from "./plugins/operator/not";
7
+ import { ElasticsearchQueryBuilderOperatorGreaterThanPlugin } from "./plugins/operator/gt";
8
+ import { ElasticsearchQueryBuilderOperatorGreaterThanOrEqualToPlugin } from "./plugins/operator/gte";
9
+ import { ElasticsearchQueryBuilderOperatorLesserThanPlugin } from "./plugins/operator/lt";
10
+ import { ElasticsearchQueryBuilderOperatorLesserThanOrEqualToPlugin } from "./plugins/operator/lte";
11
+ import { ElasticsearchQueryBuilderOperatorInPlugin } from "./plugins/operator/in";
12
+ import { ElasticsearchQueryBuilderOperatorAndInPlugin } from "./plugins/operator/andIn";
13
+ import { ElasticsearchQueryBuilderOperatorNotInPlugin } from "./plugins/operator/notIn";
14
+ /**
15
+ * We export as a function because there might be something to be sent to the operators at some point.
16
+ * This way, we make it easier to upgrade.
17
+ */
18
+ export declare const getElasticsearchOperators: () => (ElasticsearchQueryBuilderOperatorBetweenPlugin | ElasticsearchQueryBuilderOperatorNotBetweenPlugin | ElasticsearchQueryBuilderOperatorContainsPlugin | ElasticsearchQueryBuilderOperatorNotContainsPlugin | ElasticsearchQueryBuilderOperatorEqualPlugin | ElasticsearchQueryBuilderOperatorNotPlugin | ElasticsearchQueryBuilderOperatorGreaterThanPlugin | ElasticsearchQueryBuilderOperatorGreaterThanOrEqualToPlugin | ElasticsearchQueryBuilderOperatorLesserThanPlugin | ElasticsearchQueryBuilderOperatorLesserThanOrEqualToPlugin | ElasticsearchQueryBuilderOperatorInPlugin | ElasticsearchQueryBuilderOperatorAndInPlugin | ElasticsearchQueryBuilderOperatorNotInPlugin)[];
package/operators.js ADDED
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getElasticsearchOperators = void 0;
7
+
8
+ var _between = require("./plugins/operator/between");
9
+
10
+ var _notBetween = require("./plugins/operator/notBetween");
11
+
12
+ var _contains = require("./plugins/operator/contains");
13
+
14
+ var _notContains = require("./plugins/operator/notContains");
15
+
16
+ var _equal = require("./plugins/operator/equal");
17
+
18
+ var _not = require("./plugins/operator/not");
19
+
20
+ var _gt = require("./plugins/operator/gt");
21
+
22
+ var _gte = require("./plugins/operator/gte");
23
+
24
+ var _lt = require("./plugins/operator/lt");
25
+
26
+ var _lte = require("./plugins/operator/lte");
27
+
28
+ var _in = require("./plugins/operator/in");
29
+
30
+ var _andIn = require("./plugins/operator/andIn");
31
+
32
+ var _notIn = require("./plugins/operator/notIn");
33
+
34
+ const operators = [new _between.ElasticsearchQueryBuilderOperatorBetweenPlugin(), new _notBetween.ElasticsearchQueryBuilderOperatorNotBetweenPlugin(), new _contains.ElasticsearchQueryBuilderOperatorContainsPlugin(), new _notContains.ElasticsearchQueryBuilderOperatorNotContainsPlugin(), new _equal.ElasticsearchQueryBuilderOperatorEqualPlugin(), new _not.ElasticsearchQueryBuilderOperatorNotPlugin(), new _gt.ElasticsearchQueryBuilderOperatorGreaterThanPlugin(), new _gte.ElasticsearchQueryBuilderOperatorGreaterThanOrEqualToPlugin(), new _lt.ElasticsearchQueryBuilderOperatorLesserThanPlugin(), new _lte.ElasticsearchQueryBuilderOperatorLesserThanOrEqualToPlugin(), new _in.ElasticsearchQueryBuilderOperatorInPlugin(), new _andIn.ElasticsearchQueryBuilderOperatorAndInPlugin(), new _notIn.ElasticsearchQueryBuilderOperatorNotInPlugin()];
35
+ /**
36
+ * We export as a function because there might be something to be sent to the operators at some point.
37
+ * This way, we make it easier to upgrade.
38
+ */
39
+
40
+ const getElasticsearchOperators = () => operators;
41
+
42
+ exports.getElasticsearchOperators = getElasticsearchOperators;
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@webiny/api-elasticsearch",
3
+ "version": "0.0.0-mt-1",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/webiny/webiny-js.git",
7
+ "directory": "packages/api-elasticsearch"
8
+ },
9
+ "description": "A set of plugins to work with Elasticsearch.",
10
+ "contributors": [
11
+ "Pavel Denisjuk <pavel@webiny.com>"
12
+ ],
13
+ "license": "MIT",
14
+ "dependencies": {
15
+ "@elastic/elasticsearch": "7.12.0",
16
+ "@webiny/error": "0.0.0-mt-1",
17
+ "@webiny/handler": "0.0.0-mt-1",
18
+ "@webiny/plugins": "0.0.0-mt-1",
19
+ "@webiny/utils": "0.0.0-mt-1",
20
+ "aws-elasticsearch-connector": "9.0.3",
21
+ "aws-sdk": "2.1026.0",
22
+ "elastic-ts": "0.7.0"
23
+ },
24
+ "devDependencies": {
25
+ "@babel/cli": "^7.5.5",
26
+ "@babel/core": "^7.5.5",
27
+ "@webiny/cli": "^0.0.0-mt-1",
28
+ "@webiny/project-utils": "^0.0.0-mt-1",
29
+ "rimraf": "^3.0.2",
30
+ "ttypescript": "^1.5.12",
31
+ "typescript": "^4.1.3"
32
+ },
33
+ "publishConfig": {
34
+ "access": "public",
35
+ "directory": "dist"
36
+ },
37
+ "scripts": {
38
+ "build": "yarn webiny run build",
39
+ "watch": "yarn webiny run watch"
40
+ },
41
+ "adio": {
42
+ "ignore": {
43
+ "src": [
44
+ "aws-sdk"
45
+ ]
46
+ }
47
+ },
48
+ "gitHead": "37736d8456a6ecb342a6c3645060bd9a3f2d4bb0"
49
+ }
@@ -0,0 +1,19 @@
1
+ import { CompressionPlugin } from "./definition/CompressionPlugin";
2
+ export interface CompressedData {
3
+ compression: string;
4
+ value: string;
5
+ }
6
+ interface OriginalData {
7
+ [key: string]: any;
8
+ }
9
+ declare class GzipCompression extends CompressionPlugin {
10
+ canCompress(data: any): boolean;
11
+ compress(data: any): Promise<{
12
+ compression: string;
13
+ value: string;
14
+ }>;
15
+ canDecompress(data: CompressedData | Record<string, any>): boolean;
16
+ decompress(data: CompressedData): Promise<OriginalData | null>;
17
+ }
18
+ declare const _default: () => GzipCompression;
19
+ export default _default;
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _CompressionPlugin = require("./definition/CompressionPlugin");
9
+
10
+ var _gzip = require("@webiny/utils/compression/gzip");
11
+
12
+ const GZIP = "gzip";
13
+ const TO_STORAGE_ENCODING = "base64";
14
+ const FROM_STORAGE_ENCODING = "utf8";
15
+
16
+ const convertToBuffer = value => {
17
+ if (typeof value === "string") {
18
+ return Buffer.from(value, TO_STORAGE_ENCODING);
19
+ }
20
+
21
+ return value;
22
+ };
23
+
24
+ class GzipCompression extends _CompressionPlugin.CompressionPlugin {
25
+ canCompress(data) {
26
+ /**
27
+ * If already compressed, skip this.
28
+ */
29
+ if (data.compression) {
30
+ if (data.compression !== "GZIP") {
31
+ console.log(`Data is already compressed with "${data.compression}".`);
32
+ }
33
+
34
+ return false;
35
+ }
36
+
37
+ return true;
38
+ }
39
+
40
+ async compress(data) {
41
+ const value = await (0, _gzip.compress)(JSON.stringify(data));
42
+ return {
43
+ compression: GZIP,
44
+ value: value.toString(TO_STORAGE_ENCODING)
45
+ };
46
+ }
47
+
48
+ canDecompress(data) {
49
+ if (!data || !data.compression) {
50
+ return false;
51
+ } else if (data.compression !== GZIP) {
52
+ console.log(`Could not decompress given data since its compression is not "${GZIP}". It is "${data.compression}".`);
53
+ return false;
54
+ }
55
+
56
+ return true;
57
+ }
58
+
59
+ async decompress(data) {
60
+ try {
61
+ const buf = await (0, _gzip.decompress)(convertToBuffer(data.value));
62
+ const value = buf.toString(FROM_STORAGE_ENCODING);
63
+ return JSON.parse(value);
64
+ } catch (ex) {
65
+ return null;
66
+ }
67
+ }
68
+
69
+ }
70
+
71
+ var _default = () => {
72
+ return new GzipCompression();
73
+ };
74
+
75
+ exports.default = _default;
@@ -0,0 +1,20 @@
1
+ import { Plugin } from "@webiny/plugins";
2
+ export declare abstract class CompressionPlugin extends Plugin {
3
+ static readonly type = "elasticsearch.compression";
4
+ /**
5
+ * Check if data can be compressed.
6
+ */
7
+ abstract canCompress(data: any): boolean;
8
+ /**
9
+ * Pass the data to get the compressed one back.
10
+ */
11
+ abstract compress(data: any): Promise<any>;
12
+ /**
13
+ * Check if data can be decompressed.
14
+ */
15
+ abstract canDecompress(data: any): boolean;
16
+ /**
17
+ * Passed the compressed data to get the original data back.
18
+ */
19
+ abstract decompress(data: any): Promise<any>;
20
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.CompressionPlugin = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _plugins = require("@webiny/plugins");
13
+
14
+ class CompressionPlugin extends _plugins.Plugin {}
15
+
16
+ exports.CompressionPlugin = CompressionPlugin;
17
+ (0, _defineProperty2.default)(CompressionPlugin, "type", "elasticsearch.compression");
@@ -0,0 +1,13 @@
1
+ import { Plugin } from "@webiny/plugins";
2
+ import { SearchBody } from "elastic-ts";
3
+ export interface ModifyBodyParams {
4
+ body: SearchBody;
5
+ }
6
+ export interface ModifyBodyCallable<T extends ModifyBodyParams> {
7
+ (params: T): void;
8
+ }
9
+ export declare abstract class ElasticsearchBodyModifierPlugin<T extends ModifyBodyParams = ModifyBodyParams> extends Plugin {
10
+ private readonly callable?;
11
+ constructor(callable?: ModifyBodyCallable<T>);
12
+ modifyBody(params: T): void;
13
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.ElasticsearchBodyModifierPlugin = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _error = _interopRequireDefault(require("@webiny/error"));
13
+
14
+ var _plugins = require("@webiny/plugins");
15
+
16
+ class ElasticsearchBodyModifierPlugin extends _plugins.Plugin {
17
+ constructor(callable) {
18
+ super();
19
+ (0, _defineProperty2.default)(this, "callable", void 0);
20
+ this.callable = callable;
21
+ }
22
+
23
+ modifyBody(params) {
24
+ if (typeof this.callable !== "function") {
25
+ throw new _error.default(`Missing modification for the body.`, "BODY_MODIFICATION_MISSING", {
26
+ params
27
+ });
28
+ }
29
+
30
+ this.callable(params);
31
+ }
32
+
33
+ }
34
+
35
+ exports.ElasticsearchBodyModifierPlugin = ElasticsearchBodyModifierPlugin;