@webiny/api-dynamodb-to-elasticsearch 6.0.0-beta.0 → 6.0.0-rc.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.
@@ -1,11 +1,4 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.NotEnoughRemainingTimeError = void 0;
7
- var _error = require("@webiny/error");
8
- class NotEnoughRemainingTimeError extends _error.WebinyError {}
9
- exports.NotEnoughRemainingTimeError = NotEnoughRemainingTimeError;
1
+ import { WebinyError } from "@webiny/error";
2
+ export class NotEnoughRemainingTimeError extends WebinyError {}
10
3
 
11
4
  //# sourceMappingURL=NotEnoughRemainingTimeError.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_error","require","NotEnoughRemainingTimeError","WebinyError","exports"],"sources":["NotEnoughRemainingTimeError.ts"],"sourcesContent":["import { WebinyError } from \"@webiny/error\";\n\nexport class NotEnoughRemainingTimeError extends WebinyError {}\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEO,MAAMC,2BAA2B,SAASC,kBAAW,CAAC;AAAEC,OAAA,CAAAF,2BAAA,GAAAA,2BAAA","ignoreList":[]}
1
+ {"version":3,"names":["WebinyError","NotEnoughRemainingTimeError"],"sources":["NotEnoughRemainingTimeError.ts"],"sourcesContent":["import { WebinyError } from \"@webiny/error\";\n\nexport class NotEnoughRemainingTimeError extends WebinyError {}\n"],"mappings":"AAAA,SAASA,WAAW,QAAQ,eAAe;AAE3C,OAAO,MAAMC,2BAA2B,SAASD,WAAW,CAAC","ignoreList":[]}
@@ -0,0 +1,16 @@
1
+ import type { GenericRecord } from "@webiny/api/types.js";
2
+ import type { IDeleteOperationParams, IInsertOperationParams, IModifyOperationParams, IOperations } from "./types.js";
3
+ export declare enum OperationType {
4
+ INSERT = "INSERT",
5
+ MODIFY = "MODIFY",
6
+ REMOVE = "REMOVE"
7
+ }
8
+ export declare class Operations implements IOperations {
9
+ private _items;
10
+ get items(): GenericRecord[];
11
+ get total(): number;
12
+ clear(): void;
13
+ insert(params: IInsertOperationParams): void;
14
+ modify(params: IModifyOperationParams): void;
15
+ delete(params: IDeleteOperationParams): void;
16
+ }
package/Operations.js ADDED
@@ -0,0 +1,39 @@
1
+ export let OperationType = /*#__PURE__*/function (OperationType) {
2
+ OperationType["INSERT"] = "INSERT";
3
+ OperationType["MODIFY"] = "MODIFY";
4
+ OperationType["REMOVE"] = "REMOVE";
5
+ return OperationType;
6
+ }({});
7
+ export class Operations {
8
+ _items = [];
9
+ get items() {
10
+ return this._items;
11
+ }
12
+ get total() {
13
+ return this.items.length;
14
+ }
15
+ clear() {
16
+ this._items = [];
17
+ }
18
+ insert(params) {
19
+ this.items.push({
20
+ index: {
21
+ _id: params.id,
22
+ _index: params.index
23
+ }
24
+ }, params.data);
25
+ }
26
+ modify(params) {
27
+ this.insert(params);
28
+ }
29
+ delete(params) {
30
+ this.items.push({
31
+ delete: {
32
+ _id: params.id,
33
+ _index: params.index
34
+ }
35
+ });
36
+ }
37
+ }
38
+
39
+ //# sourceMappingURL=Operations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["OperationType","Operations","_items","items","total","length","clear","insert","params","push","index","_id","id","_index","data","modify","delete"],"sources":["Operations.ts"],"sourcesContent":["import type { GenericRecord } from \"@webiny/api/types.js\";\nimport type {\n IDeleteOperationParams,\n IInsertOperationParams,\n IModifyOperationParams,\n IOperations\n} from \"~/types.js\";\n\nexport enum OperationType {\n INSERT = \"INSERT\",\n MODIFY = \"MODIFY\",\n REMOVE = \"REMOVE\"\n}\n\nexport class Operations implements IOperations {\n private _items: GenericRecord[] = [];\n\n public get items(): GenericRecord[] {\n return this._items;\n }\n\n public get total(): number {\n return this.items.length;\n }\n\n public clear() {\n this._items = [];\n }\n\n public insert(params: IInsertOperationParams): void {\n this.items.push(\n {\n index: {\n _id: params.id,\n _index: params.index\n }\n },\n params.data\n );\n }\n\n public modify(params: IModifyOperationParams): void {\n this.insert(params);\n }\n\n public delete(params: IDeleteOperationParams): void {\n this.items.push({\n delete: {\n _id: params.id,\n _index: params.index\n }\n });\n }\n}\n"],"mappings":"AAQA,WAAYA,aAAa,0BAAbA,aAAa;EAAbA,aAAa;EAAbA,aAAa;EAAbA,aAAa;EAAA,OAAbA,aAAa;AAAA;AAMzB,OAAO,MAAMC,UAAU,CAAwB;EACnCC,MAAM,GAAoB,EAAE;EAEpC,IAAWC,KAAKA,CAAA,EAAoB;IAChC,OAAO,IAAI,CAACD,MAAM;EACtB;EAEA,IAAWE,KAAKA,CAAA,EAAW;IACvB,OAAO,IAAI,CAACD,KAAK,CAACE,MAAM;EAC5B;EAEOC,KAAKA,CAAA,EAAG;IACX,IAAI,CAACJ,MAAM,GAAG,EAAE;EACpB;EAEOK,MAAMA,CAACC,MAA8B,EAAQ;IAChD,IAAI,CAACL,KAAK,CAACM,IAAI,CACX;MACIC,KAAK,EAAE;QACHC,GAAG,EAAEH,MAAM,CAACI,EAAE;QACdC,MAAM,EAAEL,MAAM,CAACE;MACnB;IACJ,CAAC,EACDF,MAAM,CAACM,IACX,CAAC;EACL;EAEOC,MAAMA,CAACP,MAA8B,EAAQ;IAChD,IAAI,CAACD,MAAM,CAACC,MAAM,CAAC;EACvB;EAEOQ,MAAMA,CAACR,MAA8B,EAAQ;IAChD,IAAI,CAACL,KAAK,CAACM,IAAI,CAAC;MACZO,MAAM,EAAE;QACJL,GAAG,EAAEH,MAAM,CAACI,EAAE;QACdC,MAAM,EAAEL,MAAM,CAACE;MACnB;IACJ,CAAC,CAAC;EACN;AACJ","ignoreList":[]}
@@ -0,0 +1,10 @@
1
+ import type { IOperations, IOperationsBuilder, IOperationsBuilderBuildParams } from "./types.js";
2
+ import type { ICompressor } from "@webiny/utils/compression/Compressor.js";
3
+ export interface IOperationsBuilderParams {
4
+ compressor: ICompressor;
5
+ }
6
+ export declare class OperationsBuilder implements IOperationsBuilder {
7
+ private readonly compressor;
8
+ constructor(params: IOperationsBuilderParams);
9
+ build(params: IOperationsBuilderBuildParams): Promise<IOperations>;
10
+ }
@@ -0,0 +1,94 @@
1
+ import { Operations, OperationType } from "./Operations.js";
2
+ import { unmarshall } from "./marshall.js";
3
+ export class OperationsBuilder {
4
+ constructor(params) {
5
+ this.compressor = params.compressor;
6
+ }
7
+ async build(params) {
8
+ const operations = new Operations();
9
+ for (const record of params.records) {
10
+ if (!record.dynamodb) {
11
+ continue;
12
+ } else if (!record.eventName) {
13
+ console.error(`Could not get operation from the record, skipping event "${record.eventID}".`);
14
+ continue;
15
+ }
16
+ const keys = unmarshall(record.dynamodb.Keys);
17
+ if (!keys?.PK || !keys.SK) {
18
+ console.error(`Could not get keys from the record, skipping event "${record.eventID}".`);
19
+ continue;
20
+ }
21
+ const id = `${keys.PK}:${keys.SK}`;
22
+
23
+ /**
24
+ * On operations other than REMOVE we decompress the data and store it into the Elasticsearch.
25
+ * No need to try to decompress if operation is REMOVE since there is no data sent into that operation.
26
+ */
27
+ if (record.eventName === OperationType.INSERT || record.eventName === OperationType.MODIFY) {
28
+ const newImage = unmarshall(record.dynamodb.NewImage);
29
+ /**
30
+ * If there is no newImage, silently continue to the next operation.
31
+ */
32
+ if (!newImage || typeof newImage !== "object" || Object.keys(newImage).length === 0) {
33
+ continue;
34
+ }
35
+ /**
36
+ * Note that with the `REMOVE` event, there is no `NewImage` property. Which means,
37
+ * if the `newImage` is `undefined`, we are dealing with a `REMOVE` event and we still
38
+ * need to process it.
39
+ */
40
+ //
41
+ else if (newImage.ignore === true) {
42
+ // Nothing to log here, we are skipping the record intentionally.
43
+ continue;
44
+ }
45
+ /**
46
+ * Also, possibly there is no index?
47
+ */
48
+ //
49
+ else if (!newImage.index) {
50
+ console.error(`Could not get index from the new image, skipping event "${record.eventID}".`);
51
+ console.log({
52
+ newImage
53
+ });
54
+ continue;
55
+ }
56
+ /**
57
+ * We must decompress the data that is going into the Elasticsearch.
58
+ */
59
+ const data = await this.compressor.decompress(newImage.data);
60
+ /**
61
+ * No point in writing null or undefined data into the Elasticsearch.
62
+ * This might happen on some error while decompressing. We will log it.
63
+ *
64
+ * Data should NEVER be null or undefined in the Elasticsearch DynamoDB table, unless it is a delete operations.
65
+ * If it is - it is a bug.
66
+ */
67
+ if (data === undefined || data === null) {
68
+ console.error(`Could not get decompressed data, skipping ES operation "${record.eventName}", ID ${id}. Skipping...`);
69
+ continue;
70
+ }
71
+ operations.insert({
72
+ id,
73
+ index: newImage.index,
74
+ data
75
+ });
76
+ } else if (record.eventName === OperationType.REMOVE) {
77
+ const oldImage = unmarshall(record.dynamodb.OldImage);
78
+ /**
79
+ * If there is no index found, silently continue to the next operation.
80
+ */
81
+ if (!oldImage?.index) {
82
+ continue;
83
+ }
84
+ operations.delete({
85
+ id,
86
+ index: oldImage.index
87
+ });
88
+ }
89
+ }
90
+ return operations;
91
+ }
92
+ }
93
+
94
+ //# sourceMappingURL=OperationsBuilder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["Operations","OperationType","unmarshall","OperationsBuilder","constructor","params","compressor","build","operations","record","records","dynamodb","eventName","console","error","eventID","keys","Keys","PK","SK","id","INSERT","MODIFY","newImage","NewImage","Object","length","ignore","index","log","data","decompress","undefined","insert","REMOVE","oldImage","OldImage","delete"],"sources":["OperationsBuilder.ts"],"sourcesContent":["import type { IOperations, IOperationsBuilder, IOperationsBuilderBuildParams } from \"./types.js\";\nimport { Operations, OperationType } from \"~/Operations.js\";\nimport { unmarshall } from \"~/marshall.js\";\nimport type { ICompressor } from \"@webiny/utils/compression/Compressor.js\";\n\ninterface RecordDynamoDbImage {\n data: {\n compression: string;\n value: string;\n };\n ignore?: boolean;\n index: string;\n}\n\ninterface RecordDynamoDbKeys {\n PK: string;\n SK: string;\n}\n\nexport interface IOperationsBuilderParams {\n compressor: ICompressor;\n}\n\nexport class OperationsBuilder implements IOperationsBuilder {\n private readonly compressor: ICompressor;\n\n public constructor(params: IOperationsBuilderParams) {\n this.compressor = params.compressor;\n }\n\n public async build(params: IOperationsBuilderBuildParams): Promise<IOperations> {\n const operations = new Operations();\n for (const record of params.records) {\n if (!record.dynamodb) {\n continue;\n } else if (!record.eventName) {\n console.error(\n `Could not get operation from the record, skipping event \"${record.eventID}\".`\n );\n continue;\n }\n\n const keys = unmarshall<RecordDynamoDbKeys>(record.dynamodb.Keys);\n if (!keys?.PK || !keys.SK) {\n console.error(\n `Could not get keys from the record, skipping event \"${record.eventID}\".`\n );\n continue;\n }\n\n const id = `${keys.PK}:${keys.SK}`;\n\n /**\n * On operations other than REMOVE we decompress the data and store it into the Elasticsearch.\n * No need to try to decompress if operation is REMOVE since there is no data sent into that operation.\n */\n if (\n record.eventName === OperationType.INSERT ||\n record.eventName === OperationType.MODIFY\n ) {\n const newImage = unmarshall<RecordDynamoDbImage>(record.dynamodb.NewImage);\n /**\n * If there is no newImage, silently continue to the next operation.\n */\n if (\n !newImage ||\n typeof newImage !== \"object\" ||\n Object.keys(newImage).length === 0\n ) {\n continue;\n }\n /**\n * Note that with the `REMOVE` event, there is no `NewImage` property. Which means,\n * if the `newImage` is `undefined`, we are dealing with a `REMOVE` event and we still\n * need to process it.\n */\n //\n else if (newImage.ignore === true) {\n // Nothing to log here, we are skipping the record intentionally.\n continue;\n }\n /**\n * Also, possibly there is no index?\n */\n //\n else if (!newImage.index) {\n console.error(\n `Could not get index from the new image, skipping event \"${record.eventID}\".`\n );\n console.log({ newImage });\n continue;\n }\n /**\n * We must decompress the data that is going into the Elasticsearch.\n */\n const data = await this.compressor.decompress(newImage.data);\n /**\n * No point in writing null or undefined data into the Elasticsearch.\n * This might happen on some error while decompressing. We will log it.\n *\n * Data should NEVER be null or undefined in the Elasticsearch DynamoDB table, unless it is a delete operations.\n * If it is - it is a bug.\n */\n if (data === undefined || data === null) {\n console.error(\n `Could not get decompressed data, skipping ES operation \"${record.eventName}\", ID ${id}. Skipping...`\n );\n continue;\n }\n\n operations.insert({\n id,\n index: newImage.index,\n data\n });\n } else if (record.eventName === OperationType.REMOVE) {\n const oldImage = unmarshall<RecordDynamoDbImage>(record.dynamodb.OldImage);\n /**\n * If there is no index found, silently continue to the next operation.\n */\n if (!oldImage?.index) {\n continue;\n }\n operations.delete({\n id,\n index: oldImage.index\n });\n }\n }\n return operations;\n }\n}\n"],"mappings":"AACA,SAASA,UAAU,EAAEC,aAAa;AAClC,SAASC,UAAU;AAqBnB,OAAO,MAAMC,iBAAiB,CAA+B;EAGlDC,WAAWA,CAACC,MAAgC,EAAE;IACjD,IAAI,CAACC,UAAU,GAAGD,MAAM,CAACC,UAAU;EACvC;EAEA,MAAaC,KAAKA,CAACF,MAAqC,EAAwB;IAC5E,MAAMG,UAAU,GAAG,IAAIR,UAAU,CAAC,CAAC;IACnC,KAAK,MAAMS,MAAM,IAAIJ,MAAM,CAACK,OAAO,EAAE;MACjC,IAAI,CAACD,MAAM,CAACE,QAAQ,EAAE;QAClB;MACJ,CAAC,MAAM,IAAI,CAACF,MAAM,CAACG,SAAS,EAAE;QAC1BC,OAAO,CAACC,KAAK,CACT,4DAA4DL,MAAM,CAACM,OAAO,IAC9E,CAAC;QACD;MACJ;MAEA,MAAMC,IAAI,GAAGd,UAAU,CAAqBO,MAAM,CAACE,QAAQ,CAACM,IAAI,CAAC;MACjE,IAAI,CAACD,IAAI,EAAEE,EAAE,IAAI,CAACF,IAAI,CAACG,EAAE,EAAE;QACvBN,OAAO,CAACC,KAAK,CACT,uDAAuDL,MAAM,CAACM,OAAO,IACzE,CAAC;QACD;MACJ;MAEA,MAAMK,EAAE,GAAG,GAAGJ,IAAI,CAACE,EAAE,IAAIF,IAAI,CAACG,EAAE,EAAE;;MAElC;AACZ;AACA;AACA;MACY,IACIV,MAAM,CAACG,SAAS,KAAKX,aAAa,CAACoB,MAAM,IACzCZ,MAAM,CAACG,SAAS,KAAKX,aAAa,CAACqB,MAAM,EAC3C;QACE,MAAMC,QAAQ,GAAGrB,UAAU,CAAsBO,MAAM,CAACE,QAAQ,CAACa,QAAQ,CAAC;QAC1E;AACh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AAOpB,UAAU;EACrB;AACJ","ignoreList":[]}
package/README.md CHANGED
@@ -1,9 +1,11 @@
1
1
  # @webiny/api-dynamodb-to-elasticsearch
2
- [![](https://img.shields.io/npm/dw/@webiny/api-file-manager.svg)](https://www.npmjs.com/package/@webiny/api-file-manager)
3
- [![](https://img.shields.io/npm/v/@webiny/api-file-manager.svg)](https://www.npmjs.com/package/@webiny/api-file-manager)
4
- [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
5
- [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
6
2
 
7
- NOTE: not to be used as a standalone package!
3
+ > [!NOTE]
4
+ > This package is part of the [Webiny](https://www.webiny.com) monorepo.
5
+ > It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
8
6
 
9
- This package provides a handler plugin for Webiny, which takes records from DynamoDB Stream and synchronizes them with the Elasticsearch domain configured for current request context.
7
+ 📘 **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
8
+
9
+ ---
10
+
11
+ _This README file is automatically generated during the publish process._
@@ -0,0 +1,24 @@
1
+ import type { Context, IDeleteOperationParams, IInsertOperationParams, IModifyOperationParams } from "./types.js";
2
+ import type { IExecuteWithRetryParams } from "./executeWithRetry.js";
3
+ import type { ITimer } from "@webiny/handler-aws";
4
+ export type ISynchronizationBuilderExecuteWithRetryParams = Omit<IExecuteWithRetryParams, "context" | "timer" | "maxRunningTime" | "operations">;
5
+ export interface ISynchronizationBuilder {
6
+ insert(params: IInsertOperationParams): void;
7
+ delete(params: IDeleteOperationParams): void;
8
+ build: () => (params?: ISynchronizationBuilderExecuteWithRetryParams) => Promise<void>;
9
+ }
10
+ export interface ISynchronizationBuilderParams {
11
+ timer: ITimer;
12
+ context: Pick<Context, "elasticsearch">;
13
+ }
14
+ export declare class SynchronizationBuilder implements ISynchronizationBuilder {
15
+ private readonly timer;
16
+ private readonly context;
17
+ private readonly operations;
18
+ constructor(params: ISynchronizationBuilderParams);
19
+ insert(params: IInsertOperationParams): void;
20
+ modify(params: IModifyOperationParams): void;
21
+ delete(params: IDeleteOperationParams): void;
22
+ build(): (params?: ISynchronizationBuilderExecuteWithRetryParams) => Promise<void>;
23
+ }
24
+ export declare const createSynchronizationBuilder: (params: ISynchronizationBuilderParams) => ISynchronizationBuilder;
@@ -0,0 +1,38 @@
1
+ import { Operations } from "./Operations.js";
2
+ import { executeWithRetry } from "./executeWithRetry.js";
3
+ export class SynchronizationBuilder {
4
+ constructor(params) {
5
+ this.timer = params.timer;
6
+ this.context = params.context;
7
+ this.operations = new Operations();
8
+ }
9
+ insert(params) {
10
+ return this.operations.insert(params);
11
+ }
12
+ modify(params) {
13
+ return this.operations.modify(params);
14
+ }
15
+ delete(params) {
16
+ return this.operations.delete(params);
17
+ }
18
+ build() {
19
+ return async params => {
20
+ if (this.operations.total === 0) {
21
+ return;
22
+ }
23
+ await executeWithRetry({
24
+ ...params,
25
+ maxRunningTime: this.timer.getRemainingMilliseconds(),
26
+ timer: this.timer,
27
+ context: this.context,
28
+ operations: this.operations
29
+ });
30
+ this.operations.clear();
31
+ };
32
+ }
33
+ }
34
+ export const createSynchronizationBuilder = params => {
35
+ return new SynchronizationBuilder(params);
36
+ };
37
+
38
+ //# sourceMappingURL=SynchronizationBuilder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["Operations","executeWithRetry","SynchronizationBuilder","constructor","params","timer","context","operations","insert","modify","delete","build","total","maxRunningTime","getRemainingMilliseconds","clear","createSynchronizationBuilder"],"sources":["SynchronizationBuilder.ts"],"sourcesContent":["import type {\n Context,\n IDeleteOperationParams,\n IInsertOperationParams,\n IModifyOperationParams,\n IOperations\n} from \"~/types.js\";\nimport { Operations } from \"~/Operations.js\";\nimport type { IExecuteWithRetryParams } from \"~/executeWithRetry.js\";\nimport { executeWithRetry } from \"~/executeWithRetry.js\";\nimport type { ITimer } from \"@webiny/handler-aws\";\n\nexport type ISynchronizationBuilderExecuteWithRetryParams = Omit<\n IExecuteWithRetryParams,\n \"context\" | \"timer\" | \"maxRunningTime\" | \"operations\"\n>;\n\nexport interface ISynchronizationBuilder {\n insert(params: IInsertOperationParams): void;\n delete(params: IDeleteOperationParams): void;\n build: () => (params?: ISynchronizationBuilderExecuteWithRetryParams) => Promise<void>;\n}\n\nexport interface ISynchronizationBuilderParams {\n timer: ITimer;\n context: Pick<Context, \"elasticsearch\">;\n}\n\nexport class SynchronizationBuilder implements ISynchronizationBuilder {\n private readonly timer: ITimer;\n private readonly context: Pick<Context, \"elasticsearch\">;\n private readonly operations: IOperations;\n\n public constructor(params: ISynchronizationBuilderParams) {\n this.timer = params.timer;\n this.context = params.context;\n this.operations = new Operations();\n }\n\n public insert(params: IInsertOperationParams): void {\n return this.operations.insert(params);\n }\n\n public modify(params: IModifyOperationParams): void {\n return this.operations.modify(params);\n }\n\n public delete(params: IDeleteOperationParams): void {\n return this.operations.delete(params);\n }\n\n public build() {\n return async (params?: ISynchronizationBuilderExecuteWithRetryParams) => {\n if (this.operations.total === 0) {\n return;\n }\n await executeWithRetry({\n ...params,\n maxRunningTime: this.timer.getRemainingMilliseconds(),\n timer: this.timer,\n context: this.context,\n operations: this.operations\n });\n this.operations.clear();\n };\n }\n}\n\nexport const createSynchronizationBuilder = (\n params: ISynchronizationBuilderParams\n): ISynchronizationBuilder => {\n return new SynchronizationBuilder(params);\n};\n"],"mappings":"AAOA,SAASA,UAAU;AAEnB,SAASC,gBAAgB;AAmBzB,OAAO,MAAMC,sBAAsB,CAAoC;EAK5DC,WAAWA,CAACC,MAAqC,EAAE;IACtD,IAAI,CAACC,KAAK,GAAGD,MAAM,CAACC,KAAK;IACzB,IAAI,CAACC,OAAO,GAAGF,MAAM,CAACE,OAAO;IAC7B,IAAI,CAACC,UAAU,GAAG,IAAIP,UAAU,CAAC,CAAC;EACtC;EAEOQ,MAAMA,CAACJ,MAA8B,EAAQ;IAChD,OAAO,IAAI,CAACG,UAAU,CAACC,MAAM,CAACJ,MAAM,CAAC;EACzC;EAEOK,MAAMA,CAACL,MAA8B,EAAQ;IAChD,OAAO,IAAI,CAACG,UAAU,CAACE,MAAM,CAACL,MAAM,CAAC;EACzC;EAEOM,MAAMA,CAACN,MAA8B,EAAQ;IAChD,OAAO,IAAI,CAACG,UAAU,CAACG,MAAM,CAACN,MAAM,CAAC;EACzC;EAEOO,KAAKA,CAAA,EAAG;IACX,OAAO,MAAOP,MAAsD,IAAK;MACrE,IAAI,IAAI,CAACG,UAAU,CAACK,KAAK,KAAK,CAAC,EAAE;QAC7B;MACJ;MACA,MAAMX,gBAAgB,CAAC;QACnB,GAAGG,MAAM;QACTS,cAAc,EAAE,IAAI,CAACR,KAAK,CAACS,wBAAwB,CAAC,CAAC;QACrDT,KAAK,EAAE,IAAI,CAACA,KAAK;QACjBC,OAAO,EAAE,IAAI,CAACA,OAAO;QACrBC,UAAU,EAAE,IAAI,CAACA;MACrB,CAAC,CAAC;MACF,IAAI,CAACA,UAAU,CAACQ,KAAK,CAAC,CAAC;IAC3B,CAAC;EACL;AACJ;AAEA,OAAO,MAAMC,4BAA4B,GACrCZ,MAAqC,IACX;EAC1B,OAAO,IAAIF,sBAAsB,CAACE,MAAM,CAAC;AAC7C,CAAC","ignoreList":[]}
@@ -0,0 +1 @@
1
+ export declare const createEventHandler: () => import("@webiny/handler-aws").DynamoDBEventHandler<unknown>;
@@ -0,0 +1,48 @@
1
+ import { createDynamoDBEventHandler, timerFactory } from "@webiny/handler-aws";
2
+ import { OperationsBuilder } from "./OperationsBuilder.js";
3
+ import { executeWithRetry } from "./executeWithRetry.js";
4
+
5
+ /**
6
+ * Also, we need to set the maximum running time for the Lambda Function.
7
+ * https://github.com/webiny/webiny-js/blob/f7352d418da2b5ae0b781376be46785aa7ac6ae0/packages/pulumi-aws/src/apps/core/CoreOpenSearch.ts#L232
8
+ * https://github.com/webiny/webiny-js/blob/f7352d418da2b5ae0b781376be46785aa7ac6ae0/packages/pulumi-aws/src/apps/core/CoreElasticSearch.ts#L218
9
+ */
10
+ const MAX_RUNNING_TIME = 900;
11
+ export const createEventHandler = () => {
12
+ return createDynamoDBEventHandler(async ({
13
+ event,
14
+ context: ctx,
15
+ lambdaContext
16
+ }) => {
17
+ const timer = timerFactory(lambdaContext);
18
+ const context = ctx;
19
+ if (!context.elasticsearch) {
20
+ console.error("Missing elasticsearch definition on context.");
21
+ return null;
22
+ }
23
+ const builder = new OperationsBuilder({
24
+ compressor: context.compressor
25
+ });
26
+ const operations = await builder.build({
27
+ records: event.Records
28
+ });
29
+ /**
30
+ * No need to do anything if there are no operations.
31
+ */
32
+ if (operations.total === 0) {
33
+ return null;
34
+ }
35
+ /**
36
+ * Execute the operations with retry.
37
+ */
38
+ await executeWithRetry({
39
+ timer,
40
+ maxRunningTime: MAX_RUNNING_TIME,
41
+ context,
42
+ operations
43
+ });
44
+ return null;
45
+ });
46
+ };
47
+
48
+ //# sourceMappingURL=eventHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["createDynamoDBEventHandler","timerFactory","OperationsBuilder","executeWithRetry","MAX_RUNNING_TIME","createEventHandler","event","context","ctx","lambdaContext","timer","elasticsearch","console","error","builder","compressor","operations","build","records","Records","total","maxRunningTime"],"sources":["eventHandler.ts"],"sourcesContent":["import { createDynamoDBEventHandler, timerFactory } from \"@webiny/handler-aws\";\nimport type { Context } from \"~/types.js\";\nimport { OperationsBuilder } from \"~/OperationsBuilder.js\";\nimport { executeWithRetry } from \"~/executeWithRetry.js\";\n\n/**\n * Also, we need to set the maximum running time for the Lambda Function.\n * https://github.com/webiny/webiny-js/blob/f7352d418da2b5ae0b781376be46785aa7ac6ae0/packages/pulumi-aws/src/apps/core/CoreOpenSearch.ts#L232\n * https://github.com/webiny/webiny-js/blob/f7352d418da2b5ae0b781376be46785aa7ac6ae0/packages/pulumi-aws/src/apps/core/CoreElasticSearch.ts#L218\n */\nconst MAX_RUNNING_TIME = 900;\n\nexport const createEventHandler = () => {\n return createDynamoDBEventHandler(async ({ event, context: ctx, lambdaContext }) => {\n const timer = timerFactory(lambdaContext);\n const context = ctx as unknown as Context;\n if (!context.elasticsearch) {\n console.error(\"Missing elasticsearch definition on context.\");\n return null;\n }\n\n const builder = new OperationsBuilder({\n compressor: context.compressor\n });\n\n const operations = await builder.build({\n records: event.Records\n });\n /**\n * No need to do anything if there are no operations.\n */\n if (operations.total === 0) {\n return null;\n }\n /**\n * Execute the operations with retry.\n */\n await executeWithRetry({\n timer,\n maxRunningTime: MAX_RUNNING_TIME,\n context,\n operations\n });\n\n return null;\n });\n};\n"],"mappings":"AAAA,SAASA,0BAA0B,EAAEC,YAAY,QAAQ,qBAAqB;AAE9E,SAASC,iBAAiB;AAC1B,SAASC,gBAAgB;;AAEzB;AACA;AACA;AACA;AACA;AACA,MAAMC,gBAAgB,GAAG,GAAG;AAE5B,OAAO,MAAMC,kBAAkB,GAAGA,CAAA,KAAM;EACpC,OAAOL,0BAA0B,CAAC,OAAO;IAAEM,KAAK;IAAEC,OAAO,EAAEC,GAAG;IAAEC;EAAc,CAAC,KAAK;IAChF,MAAMC,KAAK,GAAGT,YAAY,CAACQ,aAAa,CAAC;IACzC,MAAMF,OAAO,GAAGC,GAAyB;IACzC,IAAI,CAACD,OAAO,CAACI,aAAa,EAAE;MACxBC,OAAO,CAACC,KAAK,CAAC,8CAA8C,CAAC;MAC7D,OAAO,IAAI;IACf;IAEA,MAAMC,OAAO,GAAG,IAAIZ,iBAAiB,CAAC;MAClCa,UAAU,EAAER,OAAO,CAACQ;IACxB,CAAC,CAAC;IAEF,MAAMC,UAAU,GAAG,MAAMF,OAAO,CAACG,KAAK,CAAC;MACnCC,OAAO,EAAEZ,KAAK,CAACa;IACnB,CAAC,CAAC;IACF;AACR;AACA;IACQ,IAAIH,UAAU,CAACI,KAAK,KAAK,CAAC,EAAE;MACxB,OAAO,IAAI;IACf;IACA;AACR;AACA;IACQ,MAAMjB,gBAAgB,CAAC;MACnBO,KAAK;MACLW,cAAc,EAAEjB,gBAAgB;MAChCG,OAAO;MACPS;IACJ,CAAC,CAAC;IAEF,OAAO,IAAI;EACf,CAAC,CAAC;AACN,CAAC","ignoreList":[]}
package/execute.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ import type { ITimer } from "@webiny/handler-aws";
2
+ import type { Context, IOperations } from "./types.js";
3
+ export interface BulkOperationsResponseBodyItemIndexError {
4
+ reason?: string;
5
+ }
6
+ export interface BulkOperationsResponseBodyItemIndex {
7
+ error?: BulkOperationsResponseBodyItemIndexError;
8
+ }
9
+ export interface BulkOperationsResponseBodyItem {
10
+ index?: BulkOperationsResponseBodyItemIndex;
11
+ error?: string;
12
+ }
13
+ export interface BulkOperationsResponseBody {
14
+ items: BulkOperationsResponseBodyItem[];
15
+ }
16
+ export interface IExecuteParams {
17
+ timer: ITimer;
18
+ maxRunningTime: number;
19
+ maxProcessorPercent: number;
20
+ context: Pick<Context, "elasticsearch">;
21
+ operations: Pick<IOperations, "items" | "total">;
22
+ }
23
+ export declare const execute: (params: IExecuteParams) => () => Promise<void>;
package/execute.js ADDED
@@ -0,0 +1,137 @@
1
+ import { createWaitUntilHealthy, ElasticsearchCatClusterHealthStatus, UnhealthyClusterError, WaitingHealthyClusterAbortedError } from "@webiny/api-elasticsearch";
2
+ import { WebinyError } from "@webiny/error";
3
+ import { shouldShowLogs } from "./helpers/shouldShowLogs.js";
4
+ const getError = item => {
5
+ if (!item.index?.error?.reason) {
6
+ return null;
7
+ }
8
+ const reason = item.index.error.reason;
9
+ if (reason.match(/no such index \[([a-zA-Z0-9_-]+)\]/) !== null) {
10
+ return "index";
11
+ }
12
+ return reason;
13
+ };
14
+ const checkErrors = result => {
15
+ if (!result || !result.body || !result.body.items) {
16
+ return;
17
+ }
18
+ for (const item of result.body.items) {
19
+ const err = getError(item);
20
+ if (!err) {
21
+ continue;
22
+ } else if (err === "index") {
23
+ if (process.env.DEBUG === "true") {
24
+ console.error("Bulk response", JSON.stringify(result, null, 2));
25
+ }
26
+ continue;
27
+ }
28
+ console.error("Body item with error", item);
29
+ throw new WebinyError(err, "DYNAMODB_TO_OPENSEARCH_ERROR", item);
30
+ }
31
+ };
32
+ export const execute = params => {
33
+ return async () => {
34
+ const {
35
+ context,
36
+ timer,
37
+ maxRunningTime,
38
+ maxProcessorPercent,
39
+ operations
40
+ } = params;
41
+ if (operations.total === 0) {
42
+ return;
43
+ }
44
+ const remainingTime = timer.getRemainingSeconds();
45
+ const runningTime = maxRunningTime - remainingTime;
46
+ const maxWaitingTime = remainingTime - 90;
47
+ if (shouldShowLogs()) {
48
+ console.debug(`The Lambda is already running for ${runningTime}s. Setting Health Check max waiting time: ${maxWaitingTime}s`);
49
+ }
50
+ const healthCheck = createWaitUntilHealthy(context.elasticsearch, {
51
+ minClusterHealthStatus: ElasticsearchCatClusterHealthStatus.Yellow,
52
+ waitingTimeStep: 30,
53
+ maxProcessorPercent,
54
+ maxWaitingTime
55
+ });
56
+
57
+ // const log = context.logger.withSource("dynamodbToElasticsearch");
58
+ const log = {
59
+ notice: (...params) => {
60
+ console.log(...params);
61
+ },
62
+ debug: (...params) => {
63
+ console.debug(...params);
64
+ },
65
+ info: (...params) => {
66
+ console.info(...params);
67
+ },
68
+ warn: (...params) => {
69
+ console.warn(...params);
70
+ },
71
+ error: (...params) => {
72
+ console.error(...params);
73
+ }
74
+ };
75
+ try {
76
+ await healthCheck.wait({
77
+ async onUnhealthy({
78
+ startedAt,
79
+ runs,
80
+ mustEndAt,
81
+ waitingTimeStep,
82
+ waitingReason
83
+ }) {
84
+ console.debug(`Cluster is unhealthy on run #${runs}.`, {
85
+ startedAt,
86
+ mustEndAt,
87
+ waitingTimeStep,
88
+ waitingReason
89
+ });
90
+ },
91
+ async onTimeout({
92
+ startedAt,
93
+ runs,
94
+ waitingTimeStep,
95
+ mustEndAt,
96
+ waitingReason
97
+ }) {
98
+ console.error(`Cluster health check timeout on run #${runs}.`, {
99
+ startedAt,
100
+ mustEndAt,
101
+ waitingTimeStep,
102
+ waitingReason
103
+ });
104
+ }
105
+ });
106
+ } catch (ex) {
107
+ if (ex instanceof UnhealthyClusterError || ex instanceof WaitingHealthyClusterAbortedError) {
108
+ throw ex;
109
+ }
110
+ console.error(`Cluster health check failed.`, ex);
111
+ throw ex;
112
+ }
113
+ try {
114
+ const res = await context.elasticsearch.bulk({
115
+ body: operations.items
116
+ });
117
+ checkErrors(res);
118
+ } catch (error) {
119
+ log.error(error, {
120
+ tenant: "root"
121
+ });
122
+ if (shouldShowLogs() === false) {
123
+ throw error;
124
+ }
125
+ const meta = error?.meta || {};
126
+ delete meta["meta"];
127
+ console.error("Bulk error", JSON.stringify(error, null, 2));
128
+ throw error;
129
+ }
130
+ if (shouldShowLogs() === false) {
131
+ return;
132
+ }
133
+ console.info(`Transferred ${operations.total} record operations to Elasticsearch.`);
134
+ };
135
+ };
136
+
137
+ //# sourceMappingURL=execute.js.map
package/execute.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":["createWaitUntilHealthy","ElasticsearchCatClusterHealthStatus","UnhealthyClusterError","WaitingHealthyClusterAbortedError","WebinyError","shouldShowLogs","getError","item","index","error","reason","match","checkErrors","result","body","items","err","process","env","DEBUG","console","JSON","stringify","execute","params","context","timer","maxRunningTime","maxProcessorPercent","operations","total","remainingTime","getRemainingSeconds","runningTime","maxWaitingTime","debug","healthCheck","elasticsearch","minClusterHealthStatus","Yellow","waitingTimeStep","log","notice","info","warn","wait","onUnhealthy","startedAt","runs","mustEndAt","waitingReason","onTimeout","ex","res","bulk","tenant","meta"],"sources":["execute.ts"],"sourcesContent":["import {\n createWaitUntilHealthy,\n ElasticsearchCatClusterHealthStatus,\n UnhealthyClusterError,\n WaitingHealthyClusterAbortedError\n} from \"@webiny/api-elasticsearch\";\nimport type { ITimer } from \"@webiny/handler-aws\";\nimport type { ApiResponse } from \"@webiny/api-elasticsearch/types.js\";\nimport { WebinyError } from \"@webiny/error\";\nimport type { Context, IOperations } from \"./types.js\";\nimport { shouldShowLogs } from \"~/helpers/shouldShowLogs.js\";\n\nexport interface BulkOperationsResponseBodyItemIndexError {\n reason?: string;\n}\n\nexport interface BulkOperationsResponseBodyItemIndex {\n error?: BulkOperationsResponseBodyItemIndexError;\n}\n\nexport interface BulkOperationsResponseBodyItem {\n index?: BulkOperationsResponseBodyItemIndex;\n error?: string;\n}\n\nexport interface BulkOperationsResponseBody {\n items: BulkOperationsResponseBodyItem[];\n}\n\nexport interface IExecuteParams {\n timer: ITimer;\n maxRunningTime: number;\n maxProcessorPercent: number;\n context: Pick<Context, \"elasticsearch\">;\n operations: Pick<IOperations, \"items\" | \"total\">;\n}\n\nconst getError = (item: BulkOperationsResponseBodyItem): string | null => {\n if (!item.index?.error?.reason) {\n return null;\n }\n const reason = item.index.error.reason;\n if (reason.match(/no such index \\[([a-zA-Z0-9_-]+)\\]/) !== null) {\n return \"index\";\n }\n return reason;\n};\n\nconst checkErrors = (result?: ApiResponse<BulkOperationsResponseBody>): void => {\n if (!result || !result.body || !result.body.items) {\n return;\n }\n for (const item of result.body.items) {\n const err = getError(item);\n if (!err) {\n continue;\n } else if (err === \"index\") {\n if (process.env.DEBUG === \"true\") {\n console.error(\"Bulk response\", JSON.stringify(result, null, 2));\n }\n continue;\n }\n console.error(\"Body item with error\", item);\n throw new WebinyError(err, \"DYNAMODB_TO_OPENSEARCH_ERROR\", item);\n }\n};\n\nexport const execute = (params: IExecuteParams) => {\n return async (): Promise<void> => {\n const { context, timer, maxRunningTime, maxProcessorPercent, operations } = params;\n\n if (operations.total === 0) {\n return;\n }\n\n const remainingTime = timer.getRemainingSeconds();\n const runningTime = maxRunningTime - remainingTime;\n const maxWaitingTime = remainingTime - 90;\n\n if (shouldShowLogs()) {\n console.debug(\n `The Lambda is already running for ${runningTime}s. Setting Health Check max waiting time: ${maxWaitingTime}s`\n );\n }\n\n const healthCheck = createWaitUntilHealthy(context.elasticsearch, {\n minClusterHealthStatus: ElasticsearchCatClusterHealthStatus.Yellow,\n waitingTimeStep: 30,\n maxProcessorPercent,\n maxWaitingTime\n });\n\n // const log = context.logger.withSource(\"dynamodbToElasticsearch\");\n const log = {\n notice: (...params: any[]) => {\n console.log(...params);\n },\n debug: (...params: any[]) => {\n console.debug(...params);\n },\n info: (...params: any[]) => {\n console.info(...params);\n },\n warn: (...params: any[]) => {\n console.warn(...params);\n },\n error: (...params: any[]) => {\n console.error(...params);\n }\n };\n\n try {\n await healthCheck.wait({\n async onUnhealthy({ startedAt, runs, mustEndAt, waitingTimeStep, waitingReason }) {\n console.debug(`Cluster is unhealthy on run #${runs}.`, {\n startedAt,\n mustEndAt,\n waitingTimeStep,\n waitingReason\n });\n },\n async onTimeout({ startedAt, runs, waitingTimeStep, mustEndAt, waitingReason }) {\n console.error(`Cluster health check timeout on run #${runs}.`, {\n startedAt,\n mustEndAt,\n waitingTimeStep,\n waitingReason\n });\n }\n });\n } catch (ex) {\n if (\n ex instanceof UnhealthyClusterError ||\n ex instanceof WaitingHealthyClusterAbortedError\n ) {\n throw ex;\n }\n console.error(`Cluster health check failed.`, ex);\n throw ex;\n }\n\n try {\n const res = await context.elasticsearch.bulk<BulkOperationsResponseBody>({\n body: operations.items\n });\n checkErrors(res);\n } catch (error) {\n log.error(error, {\n tenant: \"root\"\n });\n\n if (shouldShowLogs() === false) {\n throw error;\n }\n const meta = error?.meta || {};\n delete meta[\"meta\"];\n console.error(\"Bulk error\", JSON.stringify(error, null, 2));\n throw error;\n }\n if (shouldShowLogs() === false) {\n return;\n }\n console.info(`Transferred ${operations.total} record operations to Elasticsearch.`);\n };\n};\n"],"mappings":"AAAA,SACIA,sBAAsB,EACtBC,mCAAmC,EACnCC,qBAAqB,EACrBC,iCAAiC,QAC9B,2BAA2B;AAGlC,SAASC,WAAW,QAAQ,eAAe;AAE3C,SAASC,cAAc;AA2BvB,MAAMC,QAAQ,GAAIC,IAAoC,IAAoB;EACtE,IAAI,CAACA,IAAI,CAACC,KAAK,EAAEC,KAAK,EAAEC,MAAM,EAAE;IAC5B,OAAO,IAAI;EAC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+1,9 @@
1
+ import type { IExecuteParams } from "./execute.js";
2
+ export interface IExecuteWithRetryParams extends Omit<IExecuteParams, "maxProcessorPercent"> {
3
+ maxRetryTime?: number;
4
+ retries?: number;
5
+ minTimeout?: number;
6
+ maxTimeout?: number;
7
+ maxProcessorPercent?: number;
8
+ }
9
+ export declare const executeWithRetry: (params: IExecuteWithRetryParams) => Promise<void>;
@@ -0,0 +1,47 @@
1
+ import { execute } from "./execute.js";
2
+ import { NotEnoughRemainingTimeError } from "./NotEnoughRemainingTimeError.js";
3
+ import pRetry from "p-retry";
4
+ import { getNumberEnvVariable } from "./helpers/getNumberEnvVariable.js";
5
+ const minRemainingSecondsToTimeout = 120;
6
+ const MAX_PROCESSOR_PERCENT = getNumberEnvVariable("MAX_ES_PROCESSOR", process.env.NODE_ENV === "test" ? 101 : 98);
7
+ export const executeWithRetry = async params => {
8
+ const maxRetryTime = getNumberEnvVariable("WEBINY_DYNAMODB_TO_OPENSEARCH_MAX_RETRY_TIME", params.maxRetryTime || 300000);
9
+ const retries = getNumberEnvVariable("WEBINY_DYNAMODB_TO_OPENSEARCH_RETRIES", params.retries || 20);
10
+ const minTimeout = getNumberEnvVariable("WEBINY_DYNAMODB_TO_OPENSEARCH_MIN_TIMEOUT", params.minTimeout || 1500);
11
+ const maxTimeout = getNumberEnvVariable("WEBINY_DYNAMODB_TO_OPENSEARCH_MAX_TIMEOUT", params.maxTimeout || 30000);
12
+ try {
13
+ await pRetry(execute({
14
+ timer: params.timer,
15
+ maxRunningTime: params.maxRunningTime,
16
+ maxProcessorPercent: params.maxProcessorPercent || MAX_PROCESSOR_PERCENT,
17
+ context: params.context,
18
+ operations: params.operations
19
+ }), {
20
+ maxRetryTime,
21
+ retries,
22
+ minTimeout,
23
+ maxTimeout,
24
+ onFailedAttempt: ({
25
+ error,
26
+ attemptNumber
27
+ }) => {
28
+ if (params.timer.getRemainingSeconds() < minRemainingSecondsToTimeout) {
29
+ throw new NotEnoughRemainingTimeError(error);
30
+ }
31
+ /**
32
+ * We will only log attempts which are after 3/4 of total attempts.
33
+ */
34
+ if (attemptNumber < retries * 0.75) {
35
+ return;
36
+ }
37
+ console.error(`Attempt #${attemptNumber} failed.`);
38
+ console.error(error);
39
+ }
40
+ });
41
+ } catch (ex) {
42
+ // TODO implement storing of failed operations
43
+ throw ex;
44
+ }
45
+ };
46
+
47
+ //# sourceMappingURL=executeWithRetry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["execute","NotEnoughRemainingTimeError","pRetry","getNumberEnvVariable","minRemainingSecondsToTimeout","MAX_PROCESSOR_PERCENT","process","env","NODE_ENV","executeWithRetry","params","maxRetryTime","retries","minTimeout","maxTimeout","timer","maxRunningTime","maxProcessorPercent","context","operations","onFailedAttempt","error","attemptNumber","getRemainingSeconds","console","ex"],"sources":["executeWithRetry.ts"],"sourcesContent":["import type { IExecuteParams } from \"~/execute.js\";\nimport { execute } from \"~/execute.js\";\nimport { NotEnoughRemainingTimeError } from \"~/NotEnoughRemainingTimeError.js\";\nimport pRetry from \"p-retry\";\nimport { getNumberEnvVariable } from \"./helpers/getNumberEnvVariable.js\";\n\nconst minRemainingSecondsToTimeout = 120;\n\nconst MAX_PROCESSOR_PERCENT = getNumberEnvVariable(\n \"MAX_ES_PROCESSOR\",\n process.env.NODE_ENV === \"test\" ? 101 : 98\n);\n\nexport interface IExecuteWithRetryParams extends Omit<IExecuteParams, \"maxProcessorPercent\"> {\n maxRetryTime?: number;\n retries?: number;\n minTimeout?: number;\n maxTimeout?: number;\n maxProcessorPercent?: number;\n}\n\nexport const executeWithRetry = async (params: IExecuteWithRetryParams) => {\n const maxRetryTime = getNumberEnvVariable(\n \"WEBINY_DYNAMODB_TO_OPENSEARCH_MAX_RETRY_TIME\",\n params.maxRetryTime || 300000\n );\n const retries = getNumberEnvVariable(\n \"WEBINY_DYNAMODB_TO_OPENSEARCH_RETRIES\",\n params.retries || 20\n );\n const minTimeout = getNumberEnvVariable(\n \"WEBINY_DYNAMODB_TO_OPENSEARCH_MIN_TIMEOUT\",\n params.minTimeout || 1500\n );\n const maxTimeout = getNumberEnvVariable(\n \"WEBINY_DYNAMODB_TO_OPENSEARCH_MAX_TIMEOUT\",\n params.maxTimeout || 30000\n );\n\n try {\n await pRetry(\n execute({\n timer: params.timer,\n maxRunningTime: params.maxRunningTime,\n maxProcessorPercent: params.maxProcessorPercent || MAX_PROCESSOR_PERCENT,\n context: params.context,\n operations: params.operations\n }),\n {\n maxRetryTime,\n retries,\n minTimeout,\n maxTimeout,\n onFailedAttempt: ({ error, attemptNumber }) => {\n if (params.timer.getRemainingSeconds() < minRemainingSecondsToTimeout) {\n throw new NotEnoughRemainingTimeError(error);\n }\n /**\n * We will only log attempts which are after 3/4 of total attempts.\n */\n if (attemptNumber < retries * 0.75) {\n return;\n }\n console.error(`Attempt #${attemptNumber} failed.`);\n console.error(error);\n }\n }\n );\n } catch (ex) {\n // TODO implement storing of failed operations\n throw ex;\n }\n};\n"],"mappings":"AACA,SAASA,OAAO;AAChB,SAASC,2BAA2B;AACpC,OAAOC,MAAM,MAAM,SAAS;AAC5B,SAASC,oBAAoB;AAE7B,MAAMC,4BAA4B,GAAG,GAAG;AAExC,MAAMC,qBAAqB,GAAGF,oBAAoB,CAC9C,kBAAkB,EAClBG,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,MAAM,GAAG,GAAG,GAAG,EAC5C,CAAC;AAUD,OAAO,MAAMC,gBAAgB,GAAG,MAAOC,MAA+B,IAAK;EACvE,MAAMC,YAAY,GAAGR,oBAAoB,CACrC,8CAA8C,EAC9CO,MAAM,CAACC,YAAY,IAAI,MAC3B,CAAC;EACD,MAAMC,OAAO,GAAGT,oBAAoB,CAChC,uCAAuC,EACvCO,MAAM,CAACE,OAAO,IAAI,EACtB,CAAC;EACD,MAAMC,UAAU,GAAGV,oBAAoB,CACnC,2CAA2C,EAC3CO,MAAM,CAACG,UAAU,IAAI,IACzB,CAAC;EACD,MAAMC,UAAU,GAAGX,oBAAoB,CACnC,2CAA2C,EAC3CO,MAAM,CAACI,UAAU,IAAI,KACzB,CAAC;EAED,IAAI;IACA,MAAMZ,MAAM,CACRF,OAAO,CAAC;MACJe,KAAK,EAAEL,MAAM,CAACK,KAAK;MACnBC,cAAc,EAAEN,MAAM,CAACM,cAAc;MACrCC,mBAAmB,EAAEP,MAAM,CAACO,mBAAmB,IAAIZ,qBAAqB;MACxEa,OAAO,EAAER,MAAM,CAACQ,OAAO;MACvBC,UAAU,EAAET,MAAM,CAACS;IACvB,CAAC,CAAC,EACF;MACIR,YAAY;MACZC,OAAO;MACPC,UAAU;MACVC,UAAU;MACVM,eAAe,EAAEA,CAAC;QAAEC,KAAK;QAAEC;MAAc,CAAC,KAAK;QAC3C,IAAIZ,MAAM,CAACK,KAAK,CAACQ,mBAAmB,CAAC,CAAC,GAAGnB,4BAA4B,EAAE;UACnE,MAAM,IAAIH,2BAA2B,CAACoB,KAAK,CAAC;QAChD;QACA;AACpB;AACA;QACoB,IAAIC,aAAa,GAAGV,OAAO,GAAG,IAAI,EAAE;UAChC;QACJ;QACAY,OAAO,CAACH,KAAK,CAAC,YAAYC,aAAa,UAAU,CAAC;QAClDE,OAAO,CAACH,KAAK,CAACA,KAAK,CAAC;MACxB;IACJ,CACJ,CAAC;EACL,CAAC,CAAC,OAAOI,EAAE,EAAE;IACT;IACA,MAAMA,EAAE;EACZ;AACJ,CAAC","ignoreList":[]}
@@ -0,0 +1 @@
1
+ export declare const getNumberEnvVariable: (name: string, def: number) => number;
@@ -0,0 +1,12 @@
1
+ export const getNumberEnvVariable = (name, def) => {
2
+ const input = process.env[name];
3
+ const value = Number(input);
4
+ if (isNaN(value)) {
5
+ return def;
6
+ } else if (value <= 0) {
7
+ return def;
8
+ }
9
+ return value;
10
+ };
11
+
12
+ //# sourceMappingURL=getNumberEnvVariable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["getNumberEnvVariable","name","def","input","process","env","value","Number","isNaN"],"sources":["getNumberEnvVariable.ts"],"sourcesContent":["export const getNumberEnvVariable = (name: string, def: number): number => {\n const input = process.env[name];\n const value = Number(input);\n if (isNaN(value)) {\n return def;\n } else if (value <= 0) {\n return def;\n }\n return value;\n};\n"],"mappings":"AAAA,OAAO,MAAMA,oBAAoB,GAAGA,CAACC,IAAY,EAAEC,GAAW,KAAa;EACvE,MAAMC,KAAK,GAAGC,OAAO,CAACC,GAAG,CAACJ,IAAI,CAAC;EAC/B,MAAMK,KAAK,GAAGC,MAAM,CAACJ,KAAK,CAAC;EAC3B,IAAIK,KAAK,CAACF,KAAK,CAAC,EAAE;IACd,OAAOJ,GAAG;EACd,CAAC,MAAM,IAAII,KAAK,IAAI,CAAC,EAAE;IACnB,OAAOJ,GAAG;EACd;EACA,OAAOI,KAAK;AAChB,CAAC","ignoreList":[]}
@@ -0,0 +1 @@
1
+ export declare const shouldShowLogs: () => boolean;
@@ -0,0 +1,11 @@
1
+ export const shouldShowLogs = () => {
2
+ /**
3
+ * Don't show logs during tests, really no point.
4
+ */
5
+ if (process.env.TESTING === "true") {
6
+ return false;
7
+ }
8
+ return process.env.DEBUG === "true";
9
+ };
10
+
11
+ //# sourceMappingURL=shouldShowLogs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["shouldShowLogs","process","env","TESTING","DEBUG"],"sources":["shouldShowLogs.ts"],"sourcesContent":["export const shouldShowLogs = (): boolean => {\n /**\n * Don't show logs during tests, really no point.\n */\n if (process.env.TESTING === \"true\") {\n return false;\n }\n return process.env.DEBUG === \"true\";\n};\n"],"mappings":"AAAA,OAAO,MAAMA,cAAc,GAAGA,CAAA,KAAe;EACzC;AACJ;AACA;EACI,IAAIC,OAAO,CAACC,GAAG,CAACC,OAAO,KAAK,MAAM,EAAE;IAChC,OAAO,KAAK;EAChB;EACA,OAAOF,OAAO,CAACC,GAAG,CAACE,KAAK,KAAK,MAAM;AACvC,CAAC","ignoreList":[]}
package/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
- export declare enum Operations {
2
- INSERT = "INSERT",
3
- MODIFY = "MODIFY",
4
- REMOVE = "REMOVE"
5
- }
6
- export declare const createEventHandler: () => import("@webiny/handler-aws").DynamoDBEventHandler<unknown>;
1
+ export * from "./eventHandler.js";
2
+ export * from "./execute.js";
3
+ export * from "./executeWithRetry.js";
4
+ export * from "./marshall.js";
5
+ export * from "./NotEnoughRemainingTimeError.js";
6
+ export * from "./Operations.js";
7
+ export * from "./OperationsBuilder.js";
8
+ export * from "./SynchronizationBuilder.js";
9
+ export type * from "./types.js";
package/index.js CHANGED
@@ -1,280 +1,10 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.createEventHandler = exports.Operations = void 0;
8
- var _error = _interopRequireDefault(require("@webiny/error"));
9
- var _clientDynamodb = require("@webiny/aws-sdk/client-dynamodb");
10
- var _apiElasticsearch = require("@webiny/api-elasticsearch");
11
- var _handlerAws = require("@webiny/handler-aws");
12
- var _types = require("@webiny/api-elasticsearch/operations/types");
13
- var _pRetry = _interopRequireDefault(require("p-retry"));
14
- var _NotEnoughRemainingTimeError = require("./NotEnoughRemainingTimeError");
15
- let Operations = exports.Operations = /*#__PURE__*/function (Operations) {
16
- Operations["INSERT"] = "INSERT";
17
- Operations["MODIFY"] = "MODIFY";
18
- Operations["REMOVE"] = "REMOVE";
19
- return Operations;
20
- }({});
21
- const getError = item => {
22
- if (!item.index?.error?.reason) {
23
- return null;
24
- }
25
- const reason = item.index.error.reason;
26
- if (reason.match(/no such index \[([a-zA-Z0-9_-]+)\]/) !== null) {
27
- return "index";
28
- }
29
- return reason;
30
- };
31
- const checkErrors = result => {
32
- if (!result || !result.body || !result.body.items) {
33
- return;
34
- }
35
- for (const item of result.body.items) {
36
- const err = getError(item);
37
- if (!err) {
38
- continue;
39
- } else if (err === "index") {
40
- if (process.env.DEBUG === "true") {
41
- console.error("Bulk response", JSON.stringify(result, null, 2));
42
- }
43
- continue;
44
- }
45
- console.error(item.error);
46
- throw new _error.default(err, "DYNAMODB_TO_ELASTICSEARCH_ERROR", item);
47
- }
48
- };
49
- const getNumberEnvVariable = (name, def) => {
50
- const input = process.env[name];
51
- const value = Number(input);
52
- if (isNaN(value)) {
53
- return def;
54
- } else if (value <= 0) {
55
- return def;
56
- }
57
- return value;
58
- };
59
- const MAX_PROCESSOR_PERCENT = getNumberEnvVariable("MAX_ES_PROCESSOR", process.env.NODE_ENV === "test" ? 101 : 98);
60
- const unmarshall = value => {
61
- if (!value) {
62
- return undefined;
63
- }
64
- return (0, _clientDynamodb.unmarshall)(value);
65
- };
66
- const minRemainingSecondsToTimeout = 120;
67
-
68
- /**
69
- * Also, we need to set the maximum running time for the Lambda Function.
70
- * https://github.com/webiny/webiny-js/blob/f7352d418da2b5ae0b781376be46785aa7ac6ae0/packages/pulumi-aws/src/apps/core/CoreOpenSearch.ts#L232
71
- * https://github.com/webiny/webiny-js/blob/f7352d418da2b5ae0b781376be46785aa7ac6ae0/packages/pulumi-aws/src/apps/core/CoreElasticSearch.ts#L218
72
- */
73
- const MAX_RUNNING_TIME = 900;
74
- const createEventHandler = () => {
75
- return (0, _handlerAws.createDynamoDBEventHandler)(async ({
76
- event,
77
- context: ctx,
78
- lambdaContext
79
- }) => {
80
- const timer = (0, _handlerAws.timerFactory)(lambdaContext);
81
- const context = ctx;
82
- if (!context.elasticsearch) {
83
- console.error("Missing elasticsearch definition on context.");
84
- return null;
85
- }
86
- const operations = [];
87
- const operationIdList = [];
88
- for (const record of event.Records) {
89
- const dynamodb = record.dynamodb;
90
- if (!dynamodb) {
91
- continue;
92
- }
93
- /**
94
- * TODO: figure out correct types
95
- */
96
- // @ts-expect-error
97
- const newImage = unmarshall(dynamodb.NewImage);
98
-
99
- // Note that with the `REMOVE` event, there is no `NewImage` property. Which means,
100
- // if the `newImage` is `undefined`, we are dealing with a `REMOVE` event and we still
101
- // need to process it.
102
- if (newImage && newImage.ignore === true) {
103
- continue;
104
- }
105
- /**
106
- * TODO: figure out correct types
107
- */
108
- // @ts-expect-error
109
- const keys = unmarshall(dynamodb.Keys);
110
- if (!keys?.PK || !keys.SK) {
111
- continue;
112
- }
113
- const _id = `${keys.PK}:${keys.SK}`;
114
- /**
115
- * TODO: figure out correct types
116
- */
117
- // @ts-expect-error
118
- const oldImage = unmarshall(dynamodb.OldImage);
119
- const operation = record.eventName;
120
-
121
- /**
122
- * On operations other than REMOVE we decompress the data and store it into the Elasticsearch.
123
- * No need to try to decompress if operation is REMOVE since there is no data sent into that operation.
124
- */
125
- let data = undefined;
126
- if (newImage && operation !== Operations.REMOVE) {
127
- /**
128
- * We must decompress the data that is going into the Elasticsearch.
129
- */
130
- data = await (0, _apiElasticsearch.decompress)(context.plugins, newImage.data);
131
- /**
132
- * No point in writing null or undefined data into the Elasticsearch.
133
- * This might happen on some error while decompressing. We will log it.
134
- *
135
- * Data should NEVER be null or undefined in the Elasticsearch DynamoDB table, unless it is a delete operations.
136
- * If it is - it is a bug.
137
- */
138
- if (data === undefined || data === null) {
139
- console.error(`Could not get decompressed data, skipping ES operation "${operation}", ID ${_id}`);
140
- continue;
141
- }
142
- }
143
- operationIdList.push(_id);
144
- switch (record.eventName) {
145
- case Operations.INSERT:
146
- case Operations.MODIFY:
147
- if (newImage) {
148
- operations.push({
149
- index: {
150
- _id,
151
- _index: newImage.index
152
- }
153
- }, data);
154
- }
155
- break;
156
- case Operations.REMOVE:
157
- operations.push({
158
- delete: {
159
- _id,
160
- _index: oldImage?.index || "unknown"
161
- }
162
- });
163
- break;
164
- default:
165
- break;
166
- }
167
- }
168
- /**
169
- * No need to do anything if there are no operations.
170
- */
171
- if (operations.length === 0) {
172
- return null;
173
- }
174
- /**
175
- * Wrap the code we need to run into the function, so it can be called within itself.
176
- */
177
- const execute = async () => {
178
- const remainingTime = timer.getRemainingSeconds();
179
- const runningTime = MAX_RUNNING_TIME - remainingTime;
180
- const maxWaitingTime = remainingTime - 90;
181
- if (process.env.DEBUG === "true") {
182
- console.debug(`The Lambda is already running for ${runningTime}s. Setting Health Check max waiting time: ${maxWaitingTime}s`);
183
- }
184
- const healthCheck = (0, _apiElasticsearch.createWaitUntilHealthy)(context.elasticsearch, {
185
- minClusterHealthStatus: _types.ElasticsearchCatClusterHealthStatus.Yellow,
186
- waitingTimeStep: 30,
187
- maxProcessorPercent: MAX_PROCESSOR_PERCENT,
188
- maxWaitingTime
189
- });
190
- try {
191
- await healthCheck.wait({
192
- async onUnhealthy({
193
- startedAt,
194
- runs,
195
- mustEndAt,
196
- waitingTimeStep,
197
- waitingReason
198
- }) {
199
- console.debug(`Cluster is unhealthy on run #${runs}.`, {
200
- startedAt,
201
- mustEndAt,
202
- waitingTimeStep,
203
- waitingReason
204
- });
205
- },
206
- async onTimeout({
207
- startedAt,
208
- runs,
209
- waitingTimeStep,
210
- mustEndAt,
211
- waitingReason
212
- }) {
213
- console.error(`Cluster health check timeout on run #${runs}.`, {
214
- startedAt,
215
- mustEndAt,
216
- waitingTimeStep,
217
- waitingReason
218
- });
219
- }
220
- });
221
- } catch (ex) {
222
- if (ex instanceof _apiElasticsearch.UnhealthyClusterError || ex instanceof _apiElasticsearch.WaitingHealthyClusterAbortedError) {
223
- throw ex;
224
- }
225
- console.error(`Cluster health check failed.`, ex);
226
- throw ex;
227
- }
228
- try {
229
- const res = await context.elasticsearch.bulk({
230
- body: operations
231
- });
232
- checkErrors(res);
233
- } catch (error) {
234
- if (process.env.DEBUG !== "true") {
235
- throw error;
236
- }
237
- const meta = error?.meta || {};
238
- delete meta["meta"];
239
- console.error("Bulk error", JSON.stringify(error, null, 2));
240
- throw error;
241
- }
242
- if (process.env.DEBUG !== "true") {
243
- return;
244
- }
245
- console.info(`Transferred ${operations.length / 2} record operations to Elasticsearch.`);
246
- };
247
- const maxRetryTime = getNumberEnvVariable("WEBINY_DYNAMODB_TO_ELASTICSEARCH_MAX_RETRY_TIME", 300000);
248
- const retries = getNumberEnvVariable("WEBINY_DYNAMODB_TO_ELASTICSEARCH_RETRIES", 20);
249
- const minTimeout = getNumberEnvVariable("WEBINY_DYNAMODB_TO_ELASTICSEARCH_MIN_TIMEOUT", 1500);
250
- const maxTimeout = getNumberEnvVariable("WEBINY_DYNAMODB_TO_ELASTICSEARCH_MAX_TIMEOUT", 30000);
251
- try {
252
- await (0, _pRetry.default)(execute, {
253
- maxRetryTime,
254
- retries,
255
- minTimeout,
256
- maxTimeout,
257
- onFailedAttempt: error => {
258
- if (timer.getRemainingSeconds() < minRemainingSecondsToTimeout) {
259
- throw new _NotEnoughRemainingTimeError.NotEnoughRemainingTimeError(error);
260
- }
261
- /**
262
- * We will only log attempts which are after 3/4 of total attempts.
263
- */
264
- if (error.attemptNumber < retries * 0.75) {
265
- return;
266
- }
267
- console.error(`Attempt #${error.attemptNumber} failed.`);
268
- console.error(error);
269
- }
270
- });
271
- } catch (ex) {
272
- // TODO implement storing of failed operations
273
- throw ex;
274
- }
275
- return null;
276
- });
277
- };
278
- exports.createEventHandler = createEventHandler;
1
+ export * from "./eventHandler.js";
2
+ export * from "./execute.js";
3
+ export * from "./executeWithRetry.js";
4
+ export * from "./marshall.js";
5
+ export * from "./NotEnoughRemainingTimeError.js";
6
+ export * from "./Operations.js";
7
+ export * from "./OperationsBuilder.js";
8
+ export * from "./SynchronizationBuilder.js";
279
9
 
280
10
  //# sourceMappingURL=index.js.map
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["_error","_interopRequireDefault","require","_clientDynamodb","_apiElasticsearch","_handlerAws","_types","_pRetry","_NotEnoughRemainingTimeError","Operations","exports","getError","item","index","error","reason","match","checkErrors","result","body","items","err","process","env","DEBUG","console","JSON","stringify","WebinyError","getNumberEnvVariable","name","def","input","value","Number","isNaN","MAX_PROCESSOR_PERCENT","NODE_ENV","unmarshall","undefined","baseUnmarshall","minRemainingSecondsToTimeout","MAX_RUNNING_TIME","createEventHandler","createDynamoDBEventHandler","event","context","ctx","lambdaContext","timer","timerFactory","elasticsearch","operations","operationIdList","record","Records","dynamodb","newImage","NewImage","ignore","keys","Keys","PK","SK","_id","oldImage","OldImage","operation","eventName","data","REMOVE","decompress","plugins","push","INSERT","MODIFY","_index","delete","length","execute","remainingTime","getRemainingSeconds","runningTime","maxWaitingTime","debug","healthCheck","createWaitUntilHealthy","minClusterHealthStatus","ElasticsearchCatClusterHealthStatus","Yellow","waitingTimeStep","maxProcessorPercent","wait","onUnhealthy","startedAt","runs","mustEndAt","waitingReason","onTimeout","ex","UnhealthyClusterError","WaitingHealthyClusterAbortedError","res","bulk","meta","info","maxRetryTime","retries","minTimeout","maxTimeout","pRetry","onFailedAttempt","NotEnoughRemainingTimeError","attemptNumber"],"sources":["index.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport { AttributeValue, unmarshall as baseUnmarshall } from \"@webiny/aws-sdk/client-dynamodb\";\nimport {\n createWaitUntilHealthy,\n decompress,\n UnhealthyClusterError,\n WaitingHealthyClusterAbortedError\n} from \"@webiny/api-elasticsearch\";\nimport { ApiResponse, ElasticsearchContext } from \"@webiny/api-elasticsearch/types\";\nimport { createDynamoDBEventHandler, timerFactory } from \"@webiny/handler-aws\";\nimport { ElasticsearchCatClusterHealthStatus } from \"@webiny/api-elasticsearch/operations/types\";\nimport pRetry from \"p-retry\";\nimport { NotEnoughRemainingTimeError } from \"./NotEnoughRemainingTimeError\";\n\nexport enum Operations {\n INSERT = \"INSERT\",\n MODIFY = \"MODIFY\",\n REMOVE = \"REMOVE\"\n}\n\ninterface BulkOperationsResponseBodyItemIndexError {\n reason?: string;\n}\n\ninterface BulkOperationsResponseBodyItemIndex {\n error?: BulkOperationsResponseBodyItemIndexError;\n}\n\ninterface BulkOperationsResponseBodyItem {\n index?: BulkOperationsResponseBodyItemIndex;\n error?: string;\n}\n\ninterface BulkOperationsResponseBody {\n items: BulkOperationsResponseBodyItem[];\n}\n\nconst getError = (item: BulkOperationsResponseBodyItem): string | null => {\n if (!item.index?.error?.reason) {\n return null;\n }\n const reason = item.index.error.reason;\n if (reason.match(/no such index \\[([a-zA-Z0-9_-]+)\\]/) !== null) {\n return \"index\";\n }\n return reason;\n};\n\nconst checkErrors = (result?: ApiResponse<BulkOperationsResponseBody>): void => {\n if (!result || !result.body || !result.body.items) {\n return;\n }\n for (const item of result.body.items) {\n const err = getError(item);\n if (!err) {\n continue;\n } else if (err === \"index\") {\n if (process.env.DEBUG === \"true\") {\n console.error(\"Bulk response\", JSON.stringify(result, null, 2));\n }\n continue;\n }\n console.error(item.error);\n throw new WebinyError(err, \"DYNAMODB_TO_ELASTICSEARCH_ERROR\", item);\n }\n};\n\nconst getNumberEnvVariable = (name: string, def: number): number => {\n const input = process.env[name];\n const value = Number(input);\n if (isNaN(value)) {\n return def;\n } else if (value <= 0) {\n return def;\n }\n return value;\n};\n\nconst MAX_PROCESSOR_PERCENT = getNumberEnvVariable(\n \"MAX_ES_PROCESSOR\",\n process.env.NODE_ENV === \"test\" ? 101 : 98\n);\n\ninterface RecordDynamoDbImage {\n data: Record<string, any>;\n ignore?: boolean;\n index: string;\n}\n\ninterface RecordDynamoDbKeys {\n PK: string;\n SK: string;\n}\n\nconst unmarshall = <T>(value?: Record<string, AttributeValue>): T | undefined => {\n if (!value) {\n return undefined;\n }\n return baseUnmarshall(value) as T;\n};\n\nconst minRemainingSecondsToTimeout = 120;\n\n/**\n * Also, we need to set the maximum running time for the Lambda Function.\n * https://github.com/webiny/webiny-js/blob/f7352d418da2b5ae0b781376be46785aa7ac6ae0/packages/pulumi-aws/src/apps/core/CoreOpenSearch.ts#L232\n * https://github.com/webiny/webiny-js/blob/f7352d418da2b5ae0b781376be46785aa7ac6ae0/packages/pulumi-aws/src/apps/core/CoreElasticSearch.ts#L218\n */\nconst MAX_RUNNING_TIME = 900;\n\nexport const createEventHandler = () => {\n return createDynamoDBEventHandler(async ({ event, context: ctx, lambdaContext }) => {\n const timer = timerFactory(lambdaContext);\n const context = ctx as unknown as ElasticsearchContext;\n if (!context.elasticsearch) {\n console.error(\"Missing elasticsearch definition on context.\");\n return null;\n }\n\n const operations: Record<string, any>[] = [];\n\n const operationIdList: string[] = [];\n\n for (const record of event.Records) {\n const dynamodb = record.dynamodb;\n if (!dynamodb) {\n continue;\n }\n /**\n * TODO: figure out correct types\n */\n // @ts-expect-error\n const newImage = unmarshall<RecordDynamoDbImage>(dynamodb.NewImage);\n\n // Note that with the `REMOVE` event, there is no `NewImage` property. Which means,\n // if the `newImage` is `undefined`, we are dealing with a `REMOVE` event and we still\n // need to process it.\n if (newImage && newImage.ignore === true) {\n continue;\n }\n /**\n * TODO: figure out correct types\n */\n // @ts-expect-error\n const keys = unmarshall<RecordDynamoDbKeys>(dynamodb.Keys);\n if (!keys?.PK || !keys.SK) {\n continue;\n }\n const _id = `${keys.PK}:${keys.SK}`;\n /**\n * TODO: figure out correct types\n */\n // @ts-expect-error\n const oldImage = unmarshall<RecordDynamoDbImage>(dynamodb.OldImage);\n const operation = record.eventName;\n\n /**\n * On operations other than REMOVE we decompress the data and store it into the Elasticsearch.\n * No need to try to decompress if operation is REMOVE since there is no data sent into that operation.\n */\n let data: any = undefined;\n if (newImage && operation !== Operations.REMOVE) {\n /**\n * We must decompress the data that is going into the Elasticsearch.\n */\n data = await decompress(context.plugins, newImage.data);\n /**\n * No point in writing null or undefined data into the Elasticsearch.\n * This might happen on some error while decompressing. We will log it.\n *\n * Data should NEVER be null or undefined in the Elasticsearch DynamoDB table, unless it is a delete operations.\n * If it is - it is a bug.\n */\n if (data === undefined || data === null) {\n console.error(\n `Could not get decompressed data, skipping ES operation \"${operation}\", ID ${_id}`\n );\n continue;\n }\n }\n\n operationIdList.push(_id);\n\n switch (record.eventName) {\n case Operations.INSERT:\n case Operations.MODIFY:\n if (newImage) {\n operations.push(\n {\n index: {\n _id,\n _index: newImage.index\n }\n },\n data\n );\n }\n break;\n case Operations.REMOVE:\n operations.push({\n delete: {\n _id,\n _index: oldImage?.index || \"unknown\"\n }\n });\n break;\n default:\n break;\n }\n }\n /**\n * No need to do anything if there are no operations.\n */\n if (operations.length === 0) {\n return null;\n }\n /**\n * Wrap the code we need to run into the function, so it can be called within itself.\n */\n const execute = async (): Promise<void> => {\n const remainingTime = timer.getRemainingSeconds();\n const runningTime = MAX_RUNNING_TIME - remainingTime;\n const maxWaitingTime = remainingTime - 90;\n\n if (process.env.DEBUG === \"true\") {\n console.debug(\n `The Lambda is already running for ${runningTime}s. Setting Health Check max waiting time: ${maxWaitingTime}s`\n );\n }\n\n const healthCheck = createWaitUntilHealthy(context.elasticsearch, {\n minClusterHealthStatus: ElasticsearchCatClusterHealthStatus.Yellow,\n waitingTimeStep: 30,\n maxProcessorPercent: MAX_PROCESSOR_PERCENT,\n maxWaitingTime\n });\n\n try {\n await healthCheck.wait({\n async onUnhealthy({\n startedAt,\n runs,\n mustEndAt,\n waitingTimeStep,\n waitingReason\n }) {\n console.debug(`Cluster is unhealthy on run #${runs}.`, {\n startedAt,\n mustEndAt,\n waitingTimeStep,\n waitingReason\n });\n },\n async onTimeout({\n startedAt,\n runs,\n waitingTimeStep,\n mustEndAt,\n waitingReason\n }) {\n console.error(`Cluster health check timeout on run #${runs}.`, {\n startedAt,\n mustEndAt,\n waitingTimeStep,\n waitingReason\n });\n }\n });\n } catch (ex) {\n if (\n ex instanceof UnhealthyClusterError ||\n ex instanceof WaitingHealthyClusterAbortedError\n ) {\n throw ex;\n }\n console.error(`Cluster health check failed.`, ex);\n throw ex;\n }\n\n try {\n const res = await context.elasticsearch.bulk<BulkOperationsResponseBody>({\n body: operations\n });\n checkErrors(res);\n } catch (error) {\n if (process.env.DEBUG !== \"true\") {\n throw error;\n }\n const meta = error?.meta || {};\n delete meta[\"meta\"];\n console.error(\"Bulk error\", JSON.stringify(error, null, 2));\n throw error;\n }\n if (process.env.DEBUG !== \"true\") {\n return;\n }\n console.info(\n `Transferred ${operations.length / 2} record operations to Elasticsearch.`\n );\n };\n\n const maxRetryTime = getNumberEnvVariable(\n \"WEBINY_DYNAMODB_TO_ELASTICSEARCH_MAX_RETRY_TIME\",\n 300000\n );\n const retries = getNumberEnvVariable(\"WEBINY_DYNAMODB_TO_ELASTICSEARCH_RETRIES\", 20);\n const minTimeout = getNumberEnvVariable(\n \"WEBINY_DYNAMODB_TO_ELASTICSEARCH_MIN_TIMEOUT\",\n 1500\n );\n const maxTimeout = getNumberEnvVariable(\n \"WEBINY_DYNAMODB_TO_ELASTICSEARCH_MAX_TIMEOUT\",\n 30000\n );\n\n try {\n await pRetry(execute, {\n maxRetryTime,\n retries,\n minTimeout,\n maxTimeout,\n onFailedAttempt: error => {\n if (timer.getRemainingSeconds() < minRemainingSecondsToTimeout) {\n throw new NotEnoughRemainingTimeError(error);\n }\n /**\n * We will only log attempts which are after 3/4 of total attempts.\n */\n if (error.attemptNumber < retries * 0.75) {\n return;\n }\n console.error(`Attempt #${error.attemptNumber} failed.`);\n console.error(error);\n }\n });\n } catch (ex) {\n // TODO implement storing of failed operations\n throw ex;\n }\n\n return null;\n });\n};\n"],"mappings":";;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,eAAA,GAAAD,OAAA;AACA,IAAAE,iBAAA,GAAAF,OAAA;AAOA,IAAAG,WAAA,GAAAH,OAAA;AACA,IAAAI,MAAA,GAAAJ,OAAA;AACA,IAAAK,OAAA,GAAAN,sBAAA,CAAAC,OAAA;AACA,IAAAM,4BAAA,GAAAN,OAAA;AAA4E,IAEhEO,UAAU,GAAAC,OAAA,CAAAD,UAAA,0BAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA;AAuBtB,MAAME,QAAQ,GAAIC,IAAoC,IAAoB;EACtE,IAAI,CAACA,IAAI,CAACC,KAAK,EAAEC,KAAK,EAAEC,MAAM,EAAE;IAC5B,OAAO,IAAI;EACf;EACA,MAAMA,MAAM,GAAGH,IAAI,CAACC,KAAK,CAACC,KAAK,CAACC,MAAM;EACtC,IAAIA,MAAM,CAACC,KAAK,CAAC,oCAAoC,CA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AAIuE,wDAA2B,CAAClG,KAAK,CAAC;UAChD;UACA;AACpB;AACA;UACoB,IAAIA,KAAK,CAACmG,aAAa,GAAGN,OAAO,GAAG,IAAI,EAAE;YACtC;UACJ;UACAlF,OAAO,CAACX,KAAK,CAAE,YAAWA,KAAK,CAACmG,aAAc,UAAS,CAAC;UACxDxF,OAAO,CAACX,KAAK,CAACA,KAAK,CAAC;QACxB;MACJ,CAAC,CAAC;IACN,CAAC,CAAC,OAAOqF,EAAE,EAAE;MACT;MACA,MAAMA,EAAE;IACZ;IAEA,OAAO,IAAI;EACf,CAAC,CAAC;AACN,CAAC;AAACzF,OAAA,CAAAiC,kBAAA,GAAAA,kBAAA","ignoreList":[]}
1
+ {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./eventHandler.js\";\nexport * from \"./execute.js\";\nexport * from \"./executeWithRetry.js\";\nexport * from \"./marshall.js\";\nexport * from \"./NotEnoughRemainingTimeError.js\";\nexport * from \"./Operations.js\";\nexport * from \"./OperationsBuilder.js\";\nexport * from \"./SynchronizationBuilder.js\";\nexport type * from \"./types.js\";\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","ignoreList":[]}
package/marshall.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import type { GenericRecord } from "@webiny/api/types.js";
2
+ import type { AttributeValue } from "@webiny/handler-aws/types.js";
3
+ export interface MarshalledValue {
4
+ [key: string]: AttributeValue;
5
+ }
6
+ export declare const marshall: (value: GenericRecord) => MarshalledValue;
7
+ export declare const unmarshall: <T>(value?: MarshalledValue) => T | undefined;
package/marshall.js ADDED
@@ -0,0 +1,19 @@
1
+ import { marshall as baseMarshall, unmarshall as baseUnmarshall } from "@webiny/aws-sdk/client-dynamodb/index.js";
2
+ export const marshall = value => {
3
+ if (!value) {
4
+ return value;
5
+ }
6
+ return baseMarshall(value);
7
+ };
8
+ export const unmarshall = value => {
9
+ if (!value) {
10
+ return undefined;
11
+ }
12
+ /**
13
+ * We can safely cast the return value to `T` because we are 100% positive that this is correct.
14
+ */
15
+ // @ts-expect-error
16
+ return baseUnmarshall(value);
17
+ };
18
+
19
+ //# sourceMappingURL=marshall.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["marshall","baseMarshall","unmarshall","baseUnmarshall","value","undefined"],"sources":["marshall.ts"],"sourcesContent":["import {\n marshall as baseMarshall,\n unmarshall as baseUnmarshall\n} from \"@webiny/aws-sdk/client-dynamodb/index.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\n\nimport type { AttributeValue } from \"@webiny/handler-aws/types.js\";\n\nexport interface MarshalledValue {\n [key: string]: AttributeValue;\n}\n\nexport const marshall = (value: GenericRecord): MarshalledValue => {\n if (!value) {\n return value;\n }\n return baseMarshall(value) as MarshalledValue;\n};\n\nexport const unmarshall = <T>(value?: MarshalledValue): T | undefined => {\n if (!value) {\n return undefined;\n }\n /**\n * We can safely cast the return value to `T` because we are 100% positive that this is correct.\n */\n // @ts-expect-error\n return baseUnmarshall(value) as T;\n};\n"],"mappings":"AAAA,SACIA,QAAQ,IAAIC,YAAY,EACxBC,UAAU,IAAIC,cAAc,QACzB,0CAA0C;AASjD,OAAO,MAAMH,QAAQ,GAAII,KAAoB,IAAsB;EAC/D,IAAI,CAACA,KAAK,EAAE;IACR,OAAOA,KAAK;EAChB;EACA,OAAOH,YAAY,CAACG,KAAK,CAAC;AAC9B,CAAC;AAED,OAAO,MAAMF,UAAU,GAAOE,KAAuB,IAAoB;EACrE,IAAI,CAACA,KAAK,EAAE;IACR,OAAOC,SAAS;EACpB;EACA;AACJ;AACA;EACI;EACA,OAAOF,cAAc,CAACC,KAAK,CAAC;AAChC,CAAC","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@webiny/api-dynamodb-to-elasticsearch",
3
- "version": "6.0.0-beta.0",
3
+ "version": "6.0.0-rc.1",
4
+ "type": "module",
4
5
  "main": "index.js",
5
6
  "repository": {
6
7
  "type": "git",
@@ -11,38 +12,27 @@
11
12
  "license": "MIT",
12
13
  "author": "Webiny Ltd.",
13
14
  "dependencies": {
14
- "@babel/runtime": "7.24.1",
15
- "@webiny/api-elasticsearch": "6.0.0-beta.0",
16
- "@webiny/aws-sdk": "6.0.0-beta.0",
17
- "@webiny/error": "6.0.0-beta.0",
18
- "@webiny/handler-aws": "6.0.0-beta.0",
19
- "p-retry": "4.6.2"
15
+ "@webiny/api": "6.0.0-rc.1",
16
+ "@webiny/api-elasticsearch": "6.0.0-rc.1",
17
+ "@webiny/aws-sdk": "6.0.0-rc.1",
18
+ "@webiny/error": "6.0.0-rc.1",
19
+ "@webiny/handler-aws": "6.0.0-rc.1",
20
+ "@webiny/utils": "6.0.0-rc.1",
21
+ "p-retry": "7.1.1"
20
22
  },
21
23
  "devDependencies": {
22
- "@babel/cli": "7.24.1",
23
- "@babel/core": "7.24.3",
24
- "@babel/plugin-proposal-object-rest-spread": "7.20.7",
25
- "@babel/plugin-transform-runtime": "7.24.3",
26
- "@babel/preset-env": "7.24.3",
27
- "@babel/preset-typescript": "7.24.1",
28
- "@types/aws-lambda": "8.10.136",
29
- "@webiny/cli": "6.0.0-beta.0",
30
- "@webiny/plugins": "6.0.0-beta.0",
31
- "@webiny/project-utils": "6.0.0-beta.0",
32
- "typescript": "4.7.4"
24
+ "@webiny/build-tools": "6.0.0-rc.1",
25
+ "@webiny/plugins": "6.0.0-rc.1",
26
+ "typescript": "5.9.3"
33
27
  },
34
28
  "publishConfig": {
35
29
  "access": "public",
36
30
  "directory": "dist"
37
31
  },
38
- "scripts": {
39
- "build": "yarn webiny run build",
40
- "watch": "yarn webiny run watch"
41
- },
42
32
  "adio": {
43
33
  "ignoreDirs": [
44
34
  "__tests__"
45
35
  ]
46
36
  },
47
- "gitHead": "aa8dbfbbd5ad13ec271adba6f2431e02991a300f"
37
+ "gitHead": "36d702721ff9ed39fb21d6f5fe7922a2a8716e63"
48
38
  }
package/types.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ import type { GenericRecord } from "@webiny/api/types.js";
2
+ import type { DynamoDBRecord } from "@webiny/handler-aws/types.js";
3
+ import type { ElasticsearchContext } from "@webiny/api-elasticsearch/types.js";
4
+ export interface IOperationsBuilderBuildParams {
5
+ records: DynamoDBRecord[];
6
+ }
7
+ export interface IOperationsBuilder {
8
+ build(params: IOperationsBuilderBuildParams): Promise<IOperations>;
9
+ }
10
+ export interface IInsertOperationParams {
11
+ id: string;
12
+ index: string;
13
+ data: GenericRecord;
14
+ }
15
+ export type IModifyOperationParams = IInsertOperationParams;
16
+ export interface IDeleteOperationParams {
17
+ id: string;
18
+ index: string;
19
+ }
20
+ export interface IOperations {
21
+ items: GenericRecord[];
22
+ total: number;
23
+ clear(): void;
24
+ insert(params: IInsertOperationParams): void;
25
+ modify(params: IModifyOperationParams): void;
26
+ delete(params: IDeleteOperationParams): void;
27
+ }
28
+ export interface Context extends ElasticsearchContext {
29
+ }
package/types.js ADDED
@@ -0,0 +1,3 @@
1
+ export {};
2
+
3
+ //# sourceMappingURL=types.js.map
package/types.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import type { GenericRecord } from \"@webiny/api/types.js\";\nimport type { DynamoDBRecord } from \"@webiny/handler-aws/types.js\";\nimport type { ElasticsearchContext } from \"@webiny/api-elasticsearch/types.js\";\n\nexport interface IOperationsBuilderBuildParams {\n records: DynamoDBRecord[];\n}\n\nexport interface IOperationsBuilder {\n build(params: IOperationsBuilderBuildParams): Promise<IOperations>;\n}\n\nexport interface IInsertOperationParams {\n id: string;\n index: string;\n data: GenericRecord;\n}\n\nexport type IModifyOperationParams = IInsertOperationParams;\n\nexport interface IDeleteOperationParams {\n id: string;\n index: string;\n}\n\nexport interface IOperations {\n items: GenericRecord[];\n total: number;\n clear(): void;\n insert(params: IInsertOperationParams): void;\n modify(params: IModifyOperationParams): void;\n delete(params: IDeleteOperationParams): void;\n}\n\nexport interface Context extends ElasticsearchContext {}\n"],"mappings":"","ignoreList":[]}