@factorearth/recordmiddleware-errorhandler 0.0.1-y.24 → 0.0.1-y.26

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.
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.determineRetryableDynamo = void 0;
4
+ /**
5
+ * Given the error from the dynamodb operation, dertermines if we can retry it, based on the information from the dynamodb sdk docs
6
+ * {@link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.Errors.html}
7
+ * @author Eric Webb <ewebb@factorearth.com>
8
+ * @param err The error thrown by the dynamodb operation
9
+ * @returns Whether or not we can retry the operation
10
+ */
11
+ function determineRetryableDynamo(err) {
12
+ if (!(err === null || err === void 0 ? void 0 : err.name))
13
+ return false;
14
+ // The errors from the link I provided that are marked as retryable
15
+ var retryableErrors = new Set(["ItemCollectionSizeLimitExceededException", "LimitExceededException", "ProvisionedThroughputExceededException", "RequestLimitExceeded", "ThrottlingException"]);
16
+ return retryableErrors.has(err.name) || (err.$metadata && err.$metadata.httpStatusCode && err.$metadata.httpStatusCode >= 500);
17
+ }
18
+ exports.determineRetryableDynamo = determineRetryableDynamo;
19
+ //# sourceMappingURL=determineRetryableDynamo.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"determineRetryableDynamo.js","sourceRoot":"","sources":["../../../lib/aws-services/dynamodb/determineRetryableDynamo.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,SAAgB,wBAAwB,CAAC,GAAQ;IAChD,IAAI,CAAC,CAAA,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,IAAI,CAAA;QAAE,OAAO,KAAK,CAAC;IAC7B,mEAAmE;IACnE,IAAM,eAAe,GAAgB,IAAI,GAAG,CAAC,CAAC,0CAA0C,EAAE,wBAAwB,EAAE,wCAAwC,EAAE,sBAAsB,EAAE,qBAAqB,CAAC,CAAC,CAAC;IAC9M,OAAO,eAAe,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,IAAI,GAAG,CAAC,SAAS,CAAC,cAAc,IAAI,GAAG,CAAC,SAAS,CAAC,cAAc,IAAI,GAAG,CAAC,CAAC;AAChI,CAAC;AALD,4DAKC"}
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.backoff = void 0;
40
+ var delay_1 = require("./delay");
41
+ // Default values
42
+ var DELAY = 100;
43
+ var NUMBER_OF_ATTEMPTS = 10;
44
+ var MAX_DELAY = Infinity;
45
+ var TIME_MULTIPLE = 2;
46
+ /**
47
+ * Given a task, attempts to execute it a designated number of times, returning the eventual response
48
+ * @author Eric Webb <ewebb@factorearth.com>
49
+ * @param task The task we want to retry potentially
50
+ * @param evaluator A function to evaluate if the task executed successfully, or if it needs to be retried. Needs to return boolean
51
+ * @param options The configurable options for the module
52
+ * @returns The result of the function
53
+ */
54
+ function backoff(task, evaluator, options) {
55
+ var taskArguments = [];
56
+ for (var _i = 3; _i < arguments.length; _i++) {
57
+ taskArguments[_i - 3] = arguments[_i];
58
+ }
59
+ return __awaiter(this, void 0, void 0, function () {
60
+ var attemptNumber, maxAttempts, initialDelay, maxDelay, timeMultiple, calculatedDelay, delayTime, taskResult, evaluatorResult, err_1;
61
+ return __generator(this, function (_a) {
62
+ switch (_a.label) {
63
+ case 0:
64
+ attemptNumber = 0;
65
+ maxAttempts = (options === null || options === void 0 ? void 0 : options.numberOfAttempts) || NUMBER_OF_ATTEMPTS;
66
+ initialDelay = (options === null || options === void 0 ? void 0 : options.delay) || DELAY;
67
+ maxDelay = (options === null || options === void 0 ? void 0 : options.maxDelay) || MAX_DELAY;
68
+ timeMultiple = (options === null || options === void 0 ? void 0 : options.timeMultiple) || TIME_MULTIPLE;
69
+ _a.label = 1;
70
+ case 1:
71
+ if (!(attemptNumber < maxAttempts)) return [3 /*break*/, 7];
72
+ calculatedDelay = initialDelay * Math.pow(timeMultiple, attemptNumber);
73
+ delayTime = Math.min(calculatedDelay, maxDelay);
74
+ return [4 /*yield*/, (0, delay_1.delay)(delayTime)];
75
+ case 2:
76
+ _a.sent();
77
+ _a.label = 3;
78
+ case 3:
79
+ _a.trys.push([3, 5, , 6]);
80
+ return [4 /*yield*/, task.apply(void 0, taskArguments)];
81
+ case 4:
82
+ taskResult = _a.sent();
83
+ evaluatorResult = evaluator(taskResult);
84
+ if (evaluatorResult || !(attemptNumber + 1 < maxAttempts))
85
+ return [2 /*return*/, taskResult];
86
+ return [3 /*break*/, 6];
87
+ case 5:
88
+ err_1 = _a.sent();
89
+ return [3 /*break*/, 6];
90
+ case 6:
91
+ attemptNumber++;
92
+ return [3 /*break*/, 1];
93
+ case 7: return [2 /*return*/];
94
+ }
95
+ });
96
+ });
97
+ }
98
+ exports.backoff = backoff;
99
+ //# sourceMappingURL=backOff.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"backOff.js","sourceRoot":"","sources":["../lib/backOff.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAAgC;AAqBhC,iBAAiB;AACjB,IAAM,KAAK,GAAG,GAAG,CAAC;AAClB,IAAM,kBAAkB,GAAG,EAAE,CAAC;AAC9B,IAAM,SAAS,GAAG,QAAQ,CAAC;AAC3B,IAAM,aAAa,GAAG,CAAC,CAAC;AAExB;;;;;;;GAOG;AACH,SAAsB,OAAO,CAA0C,IAAqD,EAAE,SAA8C,EAAE,OAAwB;IAAE,uBAA+B;SAA/B,UAA+B,EAA/B,qBAA+B,EAA/B,IAA+B;QAA/B,sCAA+B;;;;;;;oBAClO,aAAa,GAAG,CAAC,CAAC;oBAChB,WAAW,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,gBAAgB,KAAI,kBAAkB,CAAC;oBAC9D,YAAY,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,KAAI,KAAK,CAAC;oBACvC,QAAQ,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,KAAI,SAAS,CAAC;oBAC1C,YAAY,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,YAAY,KAAI,aAAa,CAAC;;;yBACrD,CAAA,aAAa,GAAG,WAAW,CAAA;oBAC3B,eAAe,GAAG,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC;oBACvE,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,eAAe,EAAE,QAAQ,CAAC,CAAC;oBACtD,qBAAM,IAAA,aAAK,EAAC,SAAS,CAAC,EAAA;;oBAAtB,SAAsB,CAAC;;;;oBAES,qBAAM,IAAI,eAAI,aAAa,GAAC;;oBAArD,UAAU,GAAe,SAA4B;oBACrD,eAAe,GAAG,SAAS,CAAC,UAAU,CAAC,CAAC;oBAC9C,IAAI,eAAe,IAAI,CAAC,CAAC,aAAa,GAAG,CAAC,GAAG,WAAW,CAAC;wBAAE,sBAAO,UAAU,EAAC;;;;;;oBAI9E,aAAa,EAAE,CAAC;;;;;;CAEjB;AAnBD,0BAmBC"}
package/dist/delay.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.delay = void 0;
4
+ /**
5
+ * Creates a promise that resolvers after a provided number of ms
6
+ * @author Eric Webb <ewebb@factorearth.com>
7
+ * @param millis The number of ms you want to sleep
8
+ * @returns A void promise
9
+ */
10
+ function delay(millis) {
11
+ return new Promise(function (resolve, _reject) { return setTimeout(resolve, millis); });
12
+ }
13
+ exports.delay = delay;
14
+ //# sourceMappingURL=delay.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"delay.js","sourceRoot":"","sources":["../lib/delay.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACH,SAAgB,KAAK,CAAC,MAAc;IACnC,OAAO,IAAI,OAAO,CAAC,UAAC,OAAO,EAAE,OAAO,IAAK,OAAA,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,EAA3B,CAA2B,CAAC,CAAC;AACvE,CAAC;AAFD,sBAEC"}
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
40
+ if (ar || !(i in from)) {
41
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
42
+ ar[i] = from[i];
43
+ }
44
+ }
45
+ return to.concat(ar || Array.prototype.slice.call(from));
46
+ };
47
+ Object.defineProperty(exports, "__esModule", { value: true });
48
+ exports.handler = void 0;
49
+ var determineRetryableDynamo_1 = require("./aws-services/dynamodb/determineRetryableDynamo");
50
+ var backOff_1 = require("./backOff");
51
+ /**
52
+ * Takes a given error, determines if we can bother retrying the operation, and re-attempts the operation
53
+ * @author Eric Webb <ewebb@factorearth.com>
54
+ * @param err The error thrown by the dynamodb sdk
55
+ * @param task The task that we need to try and perform, if it can be retried
56
+ * @param evaluator The function that evaluates the success of the task
57
+ * @param taskArguments The arguments we need to pass into the task
58
+ * @returns The result of the task, or the error that prevented it from executing
59
+ */
60
+ function handler(err, task, evaluator) {
61
+ var taskArguments = [];
62
+ for (var _i = 3; _i < arguments.length; _i++) {
63
+ taskArguments[_i - 3] = arguments[_i];
64
+ }
65
+ return __awaiter(this, void 0, void 0, function () {
66
+ var canBeRetried, result;
67
+ return __generator(this, function (_a) {
68
+ switch (_a.label) {
69
+ case 0:
70
+ canBeRetried = (0, determineRetryableDynamo_1.determineRetryableDynamo)(err);
71
+ if (!canBeRetried) return [3 /*break*/, 2];
72
+ return [4 /*yield*/, backOff_1.backoff.apply(void 0, __spreadArray([task, evaluator, undefined], taskArguments, false))];
73
+ case 1:
74
+ result = _a.sent();
75
+ return [2 /*return*/, result];
76
+ case 2: return [2 /*return*/, err];
77
+ }
78
+ });
79
+ });
80
+ }
81
+ exports.handler = handler;
82
+ //# sourceMappingURL=handler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handler.js","sourceRoot":"","sources":["../lib/handler.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6FAA4F;AAC5F,qCAAoC;AAEpC;;;;;;;;GAQG;AACH,SAAsB,OAAO,CAA0C,GAAY,EAAE,IAAuD,EAAE,SAA0C;IAAE,uBAA+B;SAA/B,UAA+B,EAA/B,qBAA+B,EAA/B,IAA+B;QAA/B,sCAA+B;;;;;;;oBAClN,YAAY,GAAG,IAAA,mDAAwB,EAAC,GAAG,CAAC,CAAC;yBAC/C,YAAY,EAAZ,wBAAY;oBACA,qBAAM,iBAAO,8BAAC,IAAI,EAAE,SAAS,EAAE,SAAS,GAAK,aAAa,WAAC;;oBAApE,MAAM,GAAG,SAA2D;oBAC1E,sBAAO,MAAM,EAAC;wBAEf,sBAAO,GAAG,EAAC;;;;CACX;AAPD,0BAOC"}
package/dist/index.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./aws-services/dynamodb/determineRetryableDynamo"), exports);
18
+ __exportStar(require("./backOff"), exports);
19
+ __exportStar(require("./handler"), exports);
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mFAAiE;AACjE,4CAA0B;AAC1B,4CAA0B"}
package/package.json CHANGED
@@ -1,22 +1,26 @@
1
1
  {
2
2
  "name": "@factorearth/recordmiddleware-errorhandler",
3
- "version": "0.0.1-y.24",
3
+ "version": "0.0.1-y.26",
4
4
  "description": "A module for handling CRUD errors in lambda",
5
5
  "author": "madtrx <marlin.makori@gmail.com>",
6
6
  "homepage": "https://github.com/FactorEarth/RecordMiddleware#readme",
7
7
  "license": "ISC",
8
- "main": "dist/index.js",
9
- "types": "dist/index.d.ts",
8
+ "main": "dist/index",
9
+ "types": "dist/index",
10
10
  "files": [
11
11
  "dist"
12
12
  ],
13
+ "scripts": {
14
+ "build": "npm run clean && npm run compile",
15
+ "clean": "rm -rf ./dist && rm -rf tsconfig.build.tsbuildinfo",
16
+ "compile": "tsc -b tsconfig.build.json",
17
+ "test": "node ./__tests__/errorHandler.test.js",
18
+ "prepublishOnly": "npm run build"
19
+ },
13
20
  "repository": {
14
21
  "type": "git",
15
22
  "url": "git+https://github.com/FactorEarth/RecordMiddleware.git"
16
23
  },
17
- "scripts": {
18
- "test": "node ./__tests__/errorHandler.test.js"
19
- },
20
24
  "bugs": {
21
25
  "url": "https://github.com/FactorEarth/RecordMiddleware/issues"
22
26
  },
@@ -24,5 +28,5 @@
24
28
  "access": "public",
25
29
  "registry": "https://registry.npmjs.org/"
26
30
  },
27
- "gitHead": "49e25c03c2ba02f2613958c90e18e82173d5865a"
31
+ "gitHead": "df6f3f954d6db402b0dfe78dbfc386ba9684f824"
28
32
  }