@autofleet/sequelize-utils 5.0.11 → 5.1.0

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/dist/index.js CHANGED
@@ -41,43 +41,27 @@ exports.default = (sequelize) => {
41
41
  });
42
42
  // eslint-disable-next-line @typescript-eslint/ban-types
43
43
  const httpBasedTransaction = (req, cb) => {
44
- if (req.aborted) {
44
+ if (req.socket.destroyed) {
45
45
  log(abortErrorText);
46
46
  throw new Error(abortErrorText);
47
47
  }
48
48
  return sequelize.transaction((transaction) => __awaiter(void 0, void 0, void 0, function* () {
49
49
  // https://github.com/nodejs/node/blob/e46c680bf2b211bbd52cf959ca17ee98c7f657f5/test/parallel/test-http-aborted.js#L9
50
- if (req.aborted) {
50
+ const rollback = () => __awaiter(void 0, void 0, void 0, function* () {
51
51
  log(abortErrorText);
52
- console.log('aborted: on pre');
53
52
  yield transaction.rollback();
54
- return null;
55
- }
56
- req.on('aborted', () => __awaiter(void 0, void 0, void 0, function* () {
57
- console.log('aborted: on callback');
58
- log(abortErrorText);
59
- yield transaction.rollback();
60
- }));
61
- if (req.aborted) {
62
- log(abortErrorText);
63
- console.log('aborted: on pre');
64
- yield transaction.rollback();
65
- return null;
66
- }
53
+ });
54
+ req.socket.on('close', rollback);
67
55
  try {
68
56
  const response = yield cb(transaction);
69
- if (req.aborted) {
70
- log(abortErrorText);
71
- console.log('aborted: on post');
72
- yield transaction.rollback();
73
- return null;
74
- }
57
+ req.socket.removeListener('close', rollback);
75
58
  return response;
76
59
  }
77
60
  catch (e) {
78
61
  if (e.message.includes(rollbackErrorText)) {
79
62
  throw new Error(abortErrorText);
80
63
  }
64
+ req.socket.on('close', rollback);
81
65
  throw e;
82
66
  }
83
67
  }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sequelize-utils",
3
- "version": "5.0.11",
3
+ "version": "5.1.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -13,9 +13,13 @@
13
13
  "test-local": "jest --forceExit",
14
14
  "coverage": "jest --coverage --forceExit --runInBand && rm -rf ./coverage"
15
15
  },
16
+ "jest": {
17
+ "testEnvironment": "node"
18
+ },
16
19
  "author": "",
17
20
  "license": "ISC",
18
21
  "dependencies": {
22
+ "@autofleet/network": "^1.3.7",
19
23
  "debug": "^4.3.2",
20
24
  "jest": "^27.0.6",
21
25
  "sequelize": "^5.22.3"
@@ -29,6 +33,7 @@
29
33
  "@typescript-eslint/eslint-plugin": "^4.28.3",
30
34
  "@typescript-eslint/parser": "^4.28.3",
31
35
  "axios": "^0.26.1",
36
+ "bluebird": "^3.7.2",
32
37
  "eslint": "^7.31.0",
33
38
  "eslint-config-airbnb-base": "^14.2.1",
34
39
  "eslint-plugin-import": "^2.23.4",
package/src/index.ts CHANGED
@@ -33,45 +33,27 @@ export default (sequelize: Sequelize): {
33
33
 
34
34
  // eslint-disable-next-line @typescript-eslint/ban-types
35
35
  const httpBasedTransaction = (req: any, cb: Function) => {
36
- if (req.aborted) {
36
+ if (req.socket.destroyed) {
37
37
  log(abortErrorText);
38
38
  throw new Error(abortErrorText);
39
39
  }
40
40
  return sequelize.transaction(async (transaction: Transaction) => {
41
41
  // https://github.com/nodejs/node/blob/e46c680bf2b211bbd52cf959ca17ee98c7f657f5/test/parallel/test-http-aborted.js#L9
42
- if (req.aborted) {
42
+ const rollback = async () => {
43
43
  log(abortErrorText);
44
- console.log('aborted: on pre');
45
44
  await transaction.rollback();
46
- return null;
47
- }
48
-
49
- req.on('aborted', async () => {
50
- console.log('aborted: on callback');
51
- log(abortErrorText);
52
- await transaction.rollback();
53
- });
54
-
55
- if (req.aborted) {
56
- log(abortErrorText);
57
- console.log('aborted: on pre');
58
- await transaction.rollback();
59
- return null;
60
- }
45
+ };
46
+ req.socket.on('close', rollback);
61
47
 
62
48
  try {
63
49
  const response = await cb(transaction);
64
- if (req.aborted) {
65
- log(abortErrorText);
66
- console.log('aborted: on post');
67
- await transaction.rollback();
68
- return null;
69
- }
50
+ req.socket.removeListener('close', rollback);
70
51
  return response;
71
52
  } catch (e) {
72
53
  if (e.message.includes(rollbackErrorText)) {
73
54
  throw new Error(abortErrorText);
74
55
  }
56
+ req.socket.on('close', rollback);
75
57
  throw e;
76
58
  }
77
59
  });
package/tsconfig.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "module": "commonjs",
5
5
  "declaration": true,
6
6
  "outDir": "./dist",
7
- "strict": true,
7
+ "strict": false,
8
8
  "esModuleInterop": true
9
9
  }
10
10
  }
@@ -1,6 +0,0 @@
1
- import { Sequelize } from 'sequelize';
2
- declare const _default: (sequelize: Sequelize) => {
3
- transactionWithRetry: any;
4
- httpBasedTransaction: any;
5
- };
6
- export default _default;
package/dist/src/index.js DELETED
@@ -1,89 +0,0 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- /* eslint-disable import/prefer-default-export */
16
- const sequelize_1 = require("sequelize");
17
- const debug_1 = __importDefault(require("debug"));
18
- const log = debug_1.default('sequelize-utils');
19
- const rollbackErrorText = 'rollback has been called on this transaction';
20
- const abortErrorText = 'Transaction cancelled due to request cancellation';
21
- exports.default = (sequelize) => {
22
- const transactionWithRetry = (funcToRun, retriesCount = 2) => __awaiter(void 0, void 0, void 0, function* () {
23
- try {
24
- const transValue = yield sequelize.transaction((transaction) => __awaiter(void 0, void 0, void 0, function* () {
25
- const funcValue = yield funcToRun(transaction);
26
- return funcValue;
27
- }));
28
- return transValue;
29
- }
30
- catch (e) {
31
- if (e instanceof sequelize_1.DatabaseError) {
32
- if (retriesCount === 0) {
33
- log('error inside transactionWithRetry - will stop retry', e);
34
- throw e;
35
- }
36
- log(`error inside transactionWithRetry - will retry times ${retriesCount - 1}`, e);
37
- return transactionWithRetry(funcToRun, retriesCount - 1);
38
- }
39
- throw e;
40
- }
41
- });
42
- // eslint-disable-next-line @typescript-eslint/ban-types
43
- const httpBasedTransaction = (req, cb) => {
44
- if (req.aborted) {
45
- log(abortErrorText);
46
- throw new Error(abortErrorText);
47
- }
48
- return sequelize.transaction((transaction) => __awaiter(void 0, void 0, void 0, function* () {
49
- // https://github.com/nodejs/node/blob/e46c680bf2b211bbd52cf959ca17ee98c7f657f5/test/parallel/test-http-aborted.js#L9
50
- if (req.aborted) {
51
- log(abortErrorText);
52
- console.log('aborted: on pre');
53
- yield transaction.rollback();
54
- return null;
55
- }
56
- req.on('aborted', () => __awaiter(void 0, void 0, void 0, function* () {
57
- console.log('aborted: on callback');
58
- log(abortErrorText);
59
- yield transaction.rollback();
60
- }));
61
- if (req.aborted) {
62
- log(abortErrorText);
63
- console.log('aborted: on pre');
64
- yield transaction.rollback();
65
- return null;
66
- }
67
- try {
68
- const response = yield cb(transaction);
69
- if (req.aborted) {
70
- log(abortErrorText);
71
- console.log('aborted: on post');
72
- yield transaction.rollback();
73
- return null;
74
- }
75
- return response;
76
- }
77
- catch (e) {
78
- if (e.message.includes(rollbackErrorText)) {
79
- throw new Error(abortErrorText);
80
- }
81
- throw e;
82
- }
83
- }));
84
- };
85
- return {
86
- httpBasedTransaction,
87
- transactionWithRetry,
88
- };
89
- };