@crowdin/app-project-module 0.20.4 → 0.20.6

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.
@@ -13,6 +13,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.MySQLStorage = void 0;
16
+ const util_1 = require("../util");
16
17
  class MySQLStorage {
17
18
  constructor(config) {
18
19
  this.mysql = require('mysql2/promise');
@@ -24,19 +25,26 @@ class MySQLStorage {
24
25
  }
25
26
  executeQuery(command) {
26
27
  return __awaiter(this, void 0, void 0, function* () {
27
- let connection;
28
- try {
29
- connection = yield this.mysql.createConnection(this.config);
30
- const res = yield command(connection);
31
- yield connection.end();
32
- return res;
33
- }
34
- catch (e) {
35
- if (connection) {
28
+ return (0, util_1.executeWithRetry)(() => __awaiter(this, void 0, void 0, function* () {
29
+ let connection;
30
+ try {
31
+ connection = yield this.mysql.createConnection(this.config);
32
+ const res = yield command(connection);
36
33
  yield connection.end();
34
+ return res;
37
35
  }
38
- throw e;
39
- }
36
+ catch (error) {
37
+ if (connection) {
38
+ try {
39
+ yield connection.end();
40
+ }
41
+ catch (nestedError) {
42
+ throw nestedError;
43
+ }
44
+ }
45
+ throw error;
46
+ }
47
+ }));
40
48
  });
41
49
  }
42
50
  migrate() {
@@ -12,6 +12,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  exports.PostgreStorage = void 0;
14
14
  const pg_1 = require("pg");
15
+ const util_1 = require("../util");
15
16
  class PostgreStorage {
16
17
  constructor(config) {
17
18
  this.dbPromise = new Promise((res, rej) => {
@@ -22,17 +23,24 @@ class PostgreStorage {
22
23
  }
23
24
  executeQuery(command) {
24
25
  return __awaiter(this, void 0, void 0, function* () {
25
- const client = new pg_1.Client(this.config);
26
- try {
27
- yield client.connect();
28
- const res = yield command(client);
29
- yield client.end();
30
- return res;
31
- }
32
- catch (e) {
33
- yield client.end();
34
- throw e;
35
- }
26
+ return (0, util_1.executeWithRetry)(() => __awaiter(this, void 0, void 0, function* () {
27
+ const client = new pg_1.Client(this.config);
28
+ try {
29
+ yield client.connect();
30
+ const res = yield command(client);
31
+ yield client.end();
32
+ return res;
33
+ }
34
+ catch (error) {
35
+ try {
36
+ yield client.end();
37
+ throw error;
38
+ }
39
+ catch (nestedError) {
40
+ throw nestedError;
41
+ }
42
+ }
43
+ }));
36
44
  });
37
45
  }
38
46
  migrate() {
@@ -9,3 +9,4 @@ export declare function getMessage(err: any): any;
9
9
  export declare function runAsyncWrapper(callback: Function, onError?: (e: any) => void): (req: Request, res: Response, next: Function) => void;
10
10
  export declare function encryptData(config: Config, data: string): string;
11
11
  export declare function decryptData(config: Config, data: string): string;
12
+ export declare function executeWithRetry<T>(func: () => Promise<T>, numOfRetries?: number): Promise<T>;
package/out/util/index.js CHANGED
@@ -28,7 +28,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
28
28
  });
29
29
  };
30
30
  Object.defineProperty(exports, "__esModule", { value: true });
31
- exports.decryptData = exports.encryptData = exports.runAsyncWrapper = exports.getMessage = exports.log = exports.CodeError = void 0;
31
+ exports.executeWithRetry = exports.decryptData = exports.encryptData = exports.runAsyncWrapper = exports.getMessage = exports.log = exports.CodeError = void 0;
32
32
  const crypto = __importStar(require("crypto-js"));
33
33
  const storage_1 = require("../storage");
34
34
  class CodeError extends Error {
@@ -103,3 +103,20 @@ function decryptData(config, data) {
103
103
  return crypto.AES.decrypt(data, config.cryptoSecret || config.clientSecret).toString(crypto.enc.Utf8);
104
104
  }
105
105
  exports.decryptData = decryptData;
106
+ function executeWithRetry(func, numOfRetries = 2) {
107
+ return __awaiter(this, void 0, void 0, function* () {
108
+ for (let i = 0; i <= numOfRetries; i++) {
109
+ try {
110
+ const result = yield func();
111
+ return result;
112
+ }
113
+ catch (error) {
114
+ if (i === numOfRetries) {
115
+ throw error;
116
+ }
117
+ }
118
+ }
119
+ throw new Error('Failed to process request with retry');
120
+ });
121
+ }
122
+ exports.executeWithRetry = executeWithRetry;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crowdin/app-project-module",
3
- "version": "0.20.4",
3
+ "version": "0.20.6",
4
4
  "description": "Module that generates for you all common endpoints for serving standalone Crowdin App",
5
5
  "main": "out/index.js",
6
6
  "types": "out/index.d.ts",