@webiny/api-dynamodb-to-elasticsearch 5.33.4 → 5.33.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +112 -74
- package/index.js.map +1 -1
- package/package.json +10 -9
package/index.js
CHANGED
|
@@ -15,6 +15,8 @@ var _apiElasticsearch = require("@webiny/api-elasticsearch");
|
|
|
15
15
|
|
|
16
16
|
var _handlerAws = require("@webiny/handler-aws");
|
|
17
17
|
|
|
18
|
+
var _pRetry = _interopRequireDefault(require("p-retry"));
|
|
19
|
+
|
|
18
20
|
var Operations;
|
|
19
21
|
|
|
20
22
|
(function (Operations) {
|
|
@@ -37,6 +39,17 @@ const getError = item => {
|
|
|
37
39
|
return reason;
|
|
38
40
|
};
|
|
39
41
|
|
|
42
|
+
const getNumberEnvVariable = (name, def) => {
|
|
43
|
+
const input = process.env[name];
|
|
44
|
+
const value = Number(input);
|
|
45
|
+
|
|
46
|
+
if (value > 0) {
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return def;
|
|
51
|
+
};
|
|
52
|
+
|
|
40
53
|
const checkErrors = result => {
|
|
41
54
|
if (!result || !result.body || !result.body.items) {
|
|
42
55
|
return;
|
|
@@ -71,100 +84,125 @@ const createEventHandler = () => {
|
|
|
71
84
|
console.log("Missing elasticsearch definition on context.");
|
|
72
85
|
return null;
|
|
73
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* Wrap the code we need to run into the function, so it can be called within itself.
|
|
89
|
+
*/
|
|
74
90
|
|
|
75
|
-
const operations = [];
|
|
76
91
|
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
if (!dynamodb) {
|
|
81
|
-
continue;
|
|
82
|
-
}
|
|
92
|
+
const execute = async () => {
|
|
93
|
+
const operations = [];
|
|
83
94
|
|
|
84
|
-
const
|
|
95
|
+
for (const record of event.Records) {
|
|
96
|
+
const dynamodb = record.dynamodb;
|
|
85
97
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
98
|
+
if (!dynamodb) {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
89
101
|
|
|
90
|
-
|
|
102
|
+
const newImage = _dynamodb.Converter.unmarshall(dynamodb.NewImage);
|
|
91
103
|
|
|
92
|
-
|
|
104
|
+
if (newImage.ignore === true) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
93
107
|
|
|
94
|
-
|
|
95
|
-
const operation = record.eventName;
|
|
96
|
-
/**
|
|
97
|
-
* On operations other than REMOVE we decompress the data and store it into the Elasticsearch.
|
|
98
|
-
* No need to try to decompress if operation is REMOVE since there is no data sent into that operation.
|
|
99
|
-
*/
|
|
108
|
+
const oldImage = _dynamodb.Converter.unmarshall(dynamodb.OldImage);
|
|
100
109
|
|
|
101
|
-
|
|
110
|
+
const keys = _dynamodb.Converter.unmarshall(dynamodb.Keys);
|
|
102
111
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
* We must decompress the data that is going into the Elasticsearch.
|
|
106
|
-
*/
|
|
107
|
-
data = await (0, _apiElasticsearch.decompress)(context.plugins, newImage.data);
|
|
112
|
+
const _id = `${keys.PK}:${keys.SK}`;
|
|
113
|
+
const operation = record.eventName;
|
|
108
114
|
/**
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
* Data should NEVER be null or undefined in the Elasticsearch DynamoDB table, unless it is a delete operations.
|
|
113
|
-
* If it is - it is a bug.
|
|
115
|
+
* On operations other than REMOVE we decompress the data and store it into the Elasticsearch.
|
|
116
|
+
* No need to try to decompress if operation is REMOVE since there is no data sent into that operation.
|
|
114
117
|
*/
|
|
115
118
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
+
let data = undefined;
|
|
120
|
+
|
|
121
|
+
if (operation !== Operations.REMOVE) {
|
|
122
|
+
/**
|
|
123
|
+
* We must decompress the data that is going into the Elasticsearch.
|
|
124
|
+
*/
|
|
125
|
+
data = await (0, _apiElasticsearch.decompress)(context.plugins, newImage.data);
|
|
126
|
+
/**
|
|
127
|
+
* No point in writing null or undefined data into the Elasticsearch.
|
|
128
|
+
* This might happen on some error while decompressing. We will log it.
|
|
129
|
+
*
|
|
130
|
+
* Data should NEVER be null or undefined in the Elasticsearch DynamoDB table, unless it is a delete operations.
|
|
131
|
+
* If it is - it is a bug.
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
if (data === undefined || data === null) {
|
|
135
|
+
console.log(`Could not get decompressed data, skipping ES operation "${operation}", ID ${_id}`);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
119
138
|
}
|
|
120
|
-
}
|
|
121
139
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
140
|
+
switch (record.eventName) {
|
|
141
|
+
case Operations.INSERT:
|
|
142
|
+
case Operations.MODIFY:
|
|
143
|
+
operations.push({
|
|
144
|
+
index: {
|
|
145
|
+
_id,
|
|
146
|
+
_index: newImage.index
|
|
147
|
+
}
|
|
148
|
+
}, data);
|
|
149
|
+
break;
|
|
150
|
+
|
|
151
|
+
case Operations.REMOVE:
|
|
152
|
+
operations.push({
|
|
153
|
+
delete: {
|
|
154
|
+
_id,
|
|
155
|
+
_index: oldImage.index
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
break;
|
|
159
|
+
|
|
160
|
+
default:
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
144
163
|
}
|
|
145
|
-
}
|
|
146
164
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
165
|
+
if (!operations.length) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
150
168
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
169
|
+
try {
|
|
170
|
+
const res = await context.elasticsearch.bulk({
|
|
171
|
+
body: operations
|
|
172
|
+
});
|
|
173
|
+
checkErrors(res);
|
|
174
|
+
} catch (error) {
|
|
175
|
+
if (process.env.DEBUG === "true") {
|
|
176
|
+
const meta = (error === null || error === void 0 ? void 0 : error.meta) || {};
|
|
177
|
+
delete meta["meta"];
|
|
178
|
+
console.log("Bulk error", JSON.stringify(meta, null, 2));
|
|
179
|
+
}
|
|
156
180
|
|
|
157
|
-
|
|
158
|
-
console.log("Bulk response", JSON.stringify(res, null, 2));
|
|
181
|
+
throw error;
|
|
159
182
|
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const maxRetryTime = getNumberEnvVariable("WEBINY_DYNAMODB_TO_ELASTICSEARCH_MAX_RETRY_TIME", 300000);
|
|
186
|
+
const retries = getNumberEnvVariable("WEBINY_DYNAMODB_TO_ELASTICSEARCH_RETRIES", 20);
|
|
187
|
+
const minTimeout = getNumberEnvVariable("WEBINY_DYNAMODB_TO_ELASTICSEARCH_MIN_TIMEOUT", 1500);
|
|
188
|
+
const maxTimeout = getNumberEnvVariable("WEBINY_DYNAMODB_TO_ELASTICSEARCH_MAX_TIMEOUT", 30000);
|
|
189
|
+
await (0, _pRetry.default)(execute, {
|
|
190
|
+
maxRetryTime,
|
|
191
|
+
retries,
|
|
192
|
+
minTimeout,
|
|
193
|
+
maxTimeout,
|
|
194
|
+
onFailedAttempt: error => {
|
|
195
|
+
/**
|
|
196
|
+
* We will only log attempts which are after 3/4 of total attempts.
|
|
197
|
+
*/
|
|
198
|
+
if (error.attemptNumber < retries * 0.75) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
167
201
|
|
|
202
|
+
console.log(`Attempt #${error.attemptNumber} failed.`);
|
|
203
|
+
console.log(error.message);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
168
206
|
return null;
|
|
169
207
|
});
|
|
170
208
|
};
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Operations","getError","item","index","error","reason","match","checkErrors","result","body","items","err","process","env","DEBUG","console","log","JSON","stringify","WebinyError","createEventHandler","createDynamoDBEventHandler","event","context","ctx","elasticsearch","operations","record","Records","dynamodb","newImage","Converter","unmarshall","NewImage","ignore","oldImage","OldImage","keys","Keys","_id","PK","SK","operation","eventName","data","undefined","REMOVE","decompress","plugins","INSERT","MODIFY","push","_index","delete","length","res","bulk"],"sources":["index.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport { Converter } from \"aws-sdk/clients/dynamodb\";\nimport { decompress } from \"@webiny/api-elasticsearch\";\nimport { ApiResponse, ElasticsearchContext } from \"@webiny/api-elasticsearch/types\";\nimport { createDynamoDBEventHandler } from \"@webiny/handler-aws\";\nimport { StreamRecord } from \"aws-lambda/trigger/dynamodb-stream\";\n\nenum Operations {\n INSERT = \"INSERT\",\n MODIFY = \"MODIFY\",\n REMOVE = \"REMOVE\"\n}\n\ninterface BulkOperationsResponseBodyItemIndexError {\n reason?: string;\n}\ninterface BulkOperationsResponseBodyItemIndex {\n error?: BulkOperationsResponseBodyItemIndexError;\n}\ninterface BulkOperationsResponseBodyItem {\n index?: BulkOperationsResponseBodyItemIndex;\n error?: string;\n}\ninterface BulkOperationsResponseBody {\n items: BulkOperationsResponseBodyItem[];\n}\n\nconst getError = (item: BulkOperationsResponseBodyItem): string | null => {\n if (!item.index || !item.index.error || !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.log(\"Bulk response\", JSON.stringify(result, null, 2));\n }\n continue;\n }\n console.log(item.error);\n throw new WebinyError(err, \"DYNAMODB_TO_ELASTICSEARCH_ERROR\", item);\n }\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\nexport const createEventHandler = () => {\n return createDynamoDBEventHandler(async ({ event, context: ctx }) => {\n const context = ctx as unknown as ElasticsearchContext;\n if (!context.elasticsearch) {\n console.log(\"Missing elasticsearch definition on context.\");\n return null;\n }\n const operations = [];\n\n for (const record of event.Records) {\n const dynamodb = record.dynamodb as Required<StreamRecord>;\n if (!dynamodb) {\n continue;\n }\n const newImage = Converter.unmarshall(dynamodb.NewImage) as RecordDynamoDbImage;\n\n if (newImage.ignore === true) {\n continue;\n }\n\n const oldImage = Converter.unmarshall(dynamodb.OldImage) as RecordDynamoDbImage;\n const keys = Converter.unmarshall(dynamodb.Keys) as RecordDynamoDbKeys;\n const _id = `${keys.PK}:${keys.SK}`;\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 (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.log(\n `Could not get decompressed data, skipping ES operation \"${operation}\", ID ${_id}`\n );\n continue;\n }\n }\n\n switch (record.eventName) {\n case Operations.INSERT:\n case Operations.MODIFY:\n operations.push({ index: { _id, _index: newImage.index } }, data);\n break;\n case Operations.REMOVE:\n operations.push({ delete: { _id, _index: oldImage.index } });\n break;\n default:\n break;\n }\n }\n\n if (!operations.length) {\n return null;\n }\n\n try {\n const res = await context.elasticsearch.bulk<BulkOperationsResponseBody>({\n body: operations\n });\n checkErrors(res);\n if (process.env.DEBUG === \"true\") {\n console.log(\"Bulk response\", JSON.stringify(res, null, 2));\n }\n } catch (error) {\n if (process.env.DEBUG === \"true\") {\n console.log(\"Bulk error\", JSON.stringify(error, null, 2));\n }\n throw error;\n }\n\n return null;\n });\n};\n"],"mappings":";;;;;;;;;AAAA;;AACA;;AACA;;AAEA;;IAGKA,U;;WAAAA,U;EAAAA,U;EAAAA,U;EAAAA,U;GAAAA,U,KAAAA,U;;AAoBL,MAAMC,QAAQ,GAAIC,IAAD,IAAyD;EACtE,IAAI,CAACA,IAAI,CAACC,KAAN,IAAe,CAACD,IAAI,CAACC,KAAL,CAAWC,KAA3B,IAAoC,CAACF,IAAI,CAACC,KAAL,CAAWC,KAAX,CAAiBC,MAA1D,EAAkE;IAC9D,OAAO,IAAP;EACH;;EACD,MAAMA,MAAM,GAAGH,IAAI,CAACC,KAAL,CAAWC,KAAX,CAAiBC,MAAhC;;EACA,IAAIA,MAAM,CAACC,KAAP,CAAa,oCAAb,MAAuD,IAA3D,EAAiE;IAC7D,OAAO,OAAP;EACH;;EACD,OAAOD,MAAP;AACH,CATD;;AAWA,MAAME,WAAW,GAAIC,MAAD,IAA4D;EAC5E,IAAI,CAACA,MAAD,IAAW,CAACA,MAAM,CAACC,IAAnB,IAA2B,CAACD,MAAM,CAACC,IAAP,CAAYC,KAA5C,EAAmD;IAC/C;EACH;;EACD,KAAK,MAAMR,IAAX,IAAmBM,MAAM,CAACC,IAAP,CAAYC,KAA/B,EAAsC;IAClC,MAAMC,GAAG,GAAGV,QAAQ,CAACC,IAAD,CAApB;;IACA,IAAI,CAACS,GAAL,EAAU;MACN;IACH,CAFD,MAEO,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AAY,eAAZ,EAA6BC,IAAI,CAACC,SAAL,CAAeqC,GAAf,EAAoB,IAApB,EAA0B,CAA1B,CAA7B;MACH;IACJ,CARD,CAQE,OAAOnD,KAAP,EAAc;MACZ,IAAIQ,OAAO,CAACC,GAAR,CAAYC,KAAZ,KAAsB,MAA1B,EAAkC;QAC9BC,OAAO,CAACC,GAAR,CAAY,YAAZ,EAA0BC,IAAI,CAACC,SAAL,CAAed,KAAf,EAAsB,IAAtB,EAA4B,CAA5B,CAA1B;MACH;;MACD,MAAMA,KAAN;IACH;;IAED,OAAO,IAAP;EACH,CAlFM,CAAP;AAmFH,CApFM"}
|
|
1
|
+
{"version":3,"names":["Operations","getError","item","index","error","reason","match","getNumberEnvVariable","name","def","input","process","env","value","Number","checkErrors","result","body","items","err","DEBUG","console","log","JSON","stringify","WebinyError","createEventHandler","createDynamoDBEventHandler","event","context","ctx","elasticsearch","execute","operations","record","Records","dynamodb","newImage","Converter","unmarshall","NewImage","ignore","oldImage","OldImage","keys","Keys","_id","PK","SK","operation","eventName","data","undefined","REMOVE","decompress","plugins","INSERT","MODIFY","push","_index","delete","length","res","bulk","meta","maxRetryTime","retries","minTimeout","maxTimeout","pRetry","onFailedAttempt","attemptNumber","message"],"sources":["index.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport { Converter } from \"aws-sdk/clients/dynamodb\";\nimport { decompress } from \"@webiny/api-elasticsearch\";\nimport { ApiResponse, ElasticsearchContext } from \"@webiny/api-elasticsearch/types\";\nimport { createDynamoDBEventHandler } from \"@webiny/handler-aws\";\nimport { StreamRecord } from \"aws-lambda/trigger/dynamodb-stream\";\nimport pRetry from \"p-retry\";\n\nenum 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 || !item.index.error || !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 getNumberEnvVariable = (name: string, def: number): number => {\n const input = process.env[name];\n const value = Number(input);\n if (value > 0) {\n return value;\n }\n return def;\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.log(\"Bulk response\", JSON.stringify(result, null, 2));\n }\n continue;\n }\n console.log(item.error);\n throw new WebinyError(err, \"DYNAMODB_TO_ELASTICSEARCH_ERROR\", item);\n }\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\nexport const createEventHandler = () => {\n return createDynamoDBEventHandler(async ({ event, context: ctx }) => {\n const context = ctx as unknown as ElasticsearchContext;\n if (!context.elasticsearch) {\n console.log(\"Missing elasticsearch definition on context.\");\n return null;\n }\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 operations = [];\n\n for (const record of event.Records) {\n const dynamodb = record.dynamodb as Required<StreamRecord>;\n if (!dynamodb) {\n continue;\n }\n const newImage = Converter.unmarshall(dynamodb.NewImage) as RecordDynamoDbImage;\n\n if (newImage.ignore === true) {\n continue;\n }\n\n const oldImage = Converter.unmarshall(dynamodb.OldImage) as RecordDynamoDbImage;\n const keys = Converter.unmarshall(dynamodb.Keys) as RecordDynamoDbKeys;\n const _id = `${keys.PK}:${keys.SK}`;\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 (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.log(\n `Could not get decompressed data, skipping ES operation \"${operation}\", ID ${_id}`\n );\n continue;\n }\n }\n\n switch (record.eventName) {\n case Operations.INSERT:\n case Operations.MODIFY:\n operations.push({ index: { _id, _index: newImage.index } }, data);\n break;\n case Operations.REMOVE:\n operations.push({ delete: { _id, _index: oldImage.index } });\n break;\n default:\n break;\n }\n }\n\n if (!operations.length) {\n return;\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 const meta = error?.meta || {};\n delete meta[\"meta\"];\n console.log(\"Bulk error\", JSON.stringify(meta, null, 2));\n }\n throw error;\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 await pRetry(execute, {\n maxRetryTime,\n retries,\n minTimeout,\n maxTimeout,\n onFailedAttempt: error => {\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.log(`Attempt #${error.attemptNumber} failed.`);\n console.log(error.message);\n }\n });\n\n return null;\n });\n};\n"],"mappings":";;;;;;;;;AAAA;;AACA;;AACA;;AAEA;;AAEA;;IAEKA,U;;WAAAA,U;EAAAA,U;EAAAA,U;EAAAA,U;GAAAA,U,KAAAA,U;;AAuBL,MAAMC,QAAQ,GAAIC,IAAD,IAAyD;EACtE,IAAI,CAACA,IAAI,CAACC,KAAN,IAAe,CAACD,IAAI,CAACC,KAAL,CAAWC,KAA3B,IAAoC,CAACF,IAAI,CAACC,KAAL,CAAWC,KAAX,CAAiBC,MAA1D,EAAkE;IAC9D,OAAO,IAAP;EACH;;EACD,MAAMA,MAAM,GAAGH,IAAI,CAACC,KAAL,CAAWC,KAAX,CAAiBC,MAAhC;;EACA,IAAIA,MAAM,CAACC,KAAP,CAAa,oCAAb,MAAuD,IAA3D,EAAiE;IAC7D,OAAO,OAAP;EACH;;EACD,OAAOD,MAAP;AACH,CATD;;AAWA,MAAME,oBAAoB,GAAG,CAACC,IAAD,EAAeC,GAAf,KAAuC;EAChE,MAAMC,KAAK,GAAGC,OAAO,CAACC,GAAR,CAAYJ,IAAZ,CAAd;EACA,MAAMK,KAAK,GAAGC,MAAM,CAACJ,KAAD,CAApB;;EACA,IAAIG,KAAK,GAAG,CAAZ,EAAe;IACX,OAAOA,KAAP;EACH;;EACD,OAAOJ,GAAP;AACH,CAPD;;AASA,MAAMM,WAAW,GAAIC,MAAD,IAA4D;EAC5E,IAAI,CAACA,MAAD,IAAW,CAACA,MAAM,CAACC,IAAnB,IAA2B,CAACD,MAAM,CAACC,IAAP,CAAYC,KAA5C,EAAmD;IAC/C;EACH;;EACD,KAAK,MAAMhB,IAAX,IAAmBc,MAAM,CAACC,IAAP,CAAYC,KAA/B,EAAsC;IAClC,MAAMC,GAAG,GAAGlB,QAAQ,CAACC,IAAD,CAApB;;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
|
|
3
|
-
"version": "5.33.
|
|
3
|
+
"version": "5.33.5",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -12,11 +12,12 @@
|
|
|
12
12
|
"author": "Webiny Ltd.",
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@babel/runtime": "7.18.9",
|
|
15
|
-
"@webiny/api-elasticsearch": "5.33.
|
|
16
|
-
"@webiny/error": "5.33.
|
|
17
|
-
"@webiny/handler-aws": "5.33.
|
|
15
|
+
"@webiny/api-elasticsearch": "5.33.5",
|
|
16
|
+
"@webiny/error": "5.33.5",
|
|
17
|
+
"@webiny/handler-aws": "5.33.5",
|
|
18
18
|
"aws-lambda": "1.0.7",
|
|
19
|
-
"aws-sdk": "2.1188.0"
|
|
19
|
+
"aws-sdk": "2.1188.0",
|
|
20
|
+
"p-retry": "4.6.2"
|
|
20
21
|
},
|
|
21
22
|
"devDependencies": {
|
|
22
23
|
"@babel/cli": "^7.16.0",
|
|
@@ -25,9 +26,9 @@
|
|
|
25
26
|
"@babel/plugin-transform-runtime": "^7.16.4",
|
|
26
27
|
"@babel/preset-env": "^7.16.4",
|
|
27
28
|
"@babel/preset-typescript": "^7.16.0",
|
|
28
|
-
"@webiny/cli": "^5.33.
|
|
29
|
-
"@webiny/plugins": "^5.33.
|
|
30
|
-
"@webiny/project-utils": "^5.33.
|
|
29
|
+
"@webiny/cli": "^5.33.5",
|
|
30
|
+
"@webiny/plugins": "^5.33.5",
|
|
31
|
+
"@webiny/project-utils": "^5.33.5",
|
|
31
32
|
"typescript": "4.7.4"
|
|
32
33
|
},
|
|
33
34
|
"publishConfig": {
|
|
@@ -48,5 +49,5 @@
|
|
|
48
49
|
]
|
|
49
50
|
}
|
|
50
51
|
},
|
|
51
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "5063045cd2abdcdb360f97262f9b90b8858ec1df"
|
|
52
53
|
}
|