@autofleet/sequelize-utils 5.1.9 → 5.2.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 +26 -3
- package/package.json +1 -1
- package/src/index.ts +27 -3
package/dist/index.js
CHANGED
|
@@ -32,28 +32,51 @@ exports.default = (sequelize) => {
|
|
|
32
32
|
}
|
|
33
33
|
};
|
|
34
34
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
35
|
-
const httpBasedTransaction = (req, cb) => {
|
|
35
|
+
const httpBasedTransaction = (req, res, cb) => {
|
|
36
|
+
let aborted = false;
|
|
36
37
|
if (req.socket.destroyed) {
|
|
37
38
|
log(abortErrorText);
|
|
38
39
|
throw new Error(abortErrorText);
|
|
39
40
|
}
|
|
40
41
|
return sequelize.transaction(async (transaction) => {
|
|
41
42
|
// https://github.com/nodejs/node/blob/e46c680bf2b211bbd52cf959ca17ee98c7f657f5/test/parallel/test-http-aborted.js#L9
|
|
43
|
+
// 2023-04-13: Node.js 16.0.0
|
|
44
|
+
// Added also the res.writableFinished check, becurse it seems that the socket is not destroyed
|
|
45
|
+
// when the request is aborted, but the response is not finished.
|
|
46
|
+
// https://github.com/Jimbly/http-proxy-node16/commit/ba0c414cd03799e357c5d867c11dea06a9c34ec8#diff-b2d1e3b7c5f3b424a0af7971c582c822fd25a5ea279040ef6dc93fc4b2cf619dR151
|
|
42
47
|
const rollback = async () => {
|
|
43
48
|
log(abortErrorText);
|
|
49
|
+
if (aborted)
|
|
50
|
+
return;
|
|
51
|
+
aborted = true;
|
|
44
52
|
await transaction.rollback();
|
|
45
53
|
};
|
|
54
|
+
const resRollback = async () => {
|
|
55
|
+
const didNotFinishWrite = !res.writableFinished;
|
|
56
|
+
if (didNotFinishWrite) {
|
|
57
|
+
log(abortErrorText);
|
|
58
|
+
if (aborted)
|
|
59
|
+
return;
|
|
60
|
+
aborted = true;
|
|
61
|
+
await transaction.rollback();
|
|
62
|
+
}
|
|
63
|
+
};
|
|
46
64
|
req.socket.once('close', rollback);
|
|
65
|
+
res.once('close', resRollback);
|
|
66
|
+
const removeListeners = () => {
|
|
67
|
+
req.socket.removeListener('close', rollback);
|
|
68
|
+
res.removeListener('close', resRollback);
|
|
69
|
+
};
|
|
47
70
|
try {
|
|
48
71
|
const response = await cb(transaction);
|
|
49
|
-
|
|
72
|
+
removeListeners();
|
|
50
73
|
return response;
|
|
51
74
|
}
|
|
52
75
|
catch (e) {
|
|
76
|
+
removeListeners();
|
|
53
77
|
if (e.message.includes(rollbackErrorText)) {
|
|
54
78
|
throw new Error(abortErrorText);
|
|
55
79
|
}
|
|
56
|
-
req.socket.removeListener('close', rollback);
|
|
57
80
|
throw e;
|
|
58
81
|
}
|
|
59
82
|
});
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -41,28 +41,52 @@ export default (sequelize: Sequelize): {
|
|
|
41
41
|
};
|
|
42
42
|
|
|
43
43
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
44
|
-
const httpBasedTransaction = (req: any, cb: Function) => {
|
|
44
|
+
const httpBasedTransaction = (req: any, res: any, cb: Function) => {
|
|
45
|
+
let aborted = false;
|
|
45
46
|
if (req.socket.destroyed) {
|
|
46
47
|
log(abortErrorText);
|
|
47
48
|
throw new Error(abortErrorText);
|
|
48
49
|
}
|
|
49
50
|
return sequelize.transaction(async (transaction: Transaction) => {
|
|
50
51
|
// https://github.com/nodejs/node/blob/e46c680bf2b211bbd52cf959ca17ee98c7f657f5/test/parallel/test-http-aborted.js#L9
|
|
52
|
+
|
|
53
|
+
// 2023-04-13: Node.js 16.0.0
|
|
54
|
+
// Added also the res.writableFinished check, becurse it seems that the socket is not destroyed
|
|
55
|
+
// when the request is aborted, but the response is not finished.
|
|
56
|
+
// https://github.com/Jimbly/http-proxy-node16/commit/ba0c414cd03799e357c5d867c11dea06a9c34ec8#diff-b2d1e3b7c5f3b424a0af7971c582c822fd25a5ea279040ef6dc93fc4b2cf619dR151
|
|
57
|
+
|
|
51
58
|
const rollback = async () => {
|
|
52
59
|
log(abortErrorText);
|
|
60
|
+
if (aborted) return;
|
|
61
|
+
aborted = true;
|
|
53
62
|
await transaction.rollback();
|
|
54
63
|
};
|
|
64
|
+
const resRollback = async () => {
|
|
65
|
+
const didNotFinishWrite = !res.writableFinished;
|
|
66
|
+
if (didNotFinishWrite) {
|
|
67
|
+
log(abortErrorText);
|
|
68
|
+
if (aborted) return;
|
|
69
|
+
aborted = true;
|
|
70
|
+
await transaction.rollback();
|
|
71
|
+
}
|
|
72
|
+
};
|
|
55
73
|
req.socket.once('close', rollback);
|
|
74
|
+
res.once('close', resRollback);
|
|
75
|
+
|
|
76
|
+
const removeListeners = () => {
|
|
77
|
+
req.socket.removeListener('close', rollback);
|
|
78
|
+
res.removeListener('close', resRollback);
|
|
79
|
+
};
|
|
56
80
|
|
|
57
81
|
try {
|
|
58
82
|
const response = await cb(transaction);
|
|
59
|
-
|
|
83
|
+
removeListeners();
|
|
60
84
|
return response;
|
|
61
85
|
} catch (e) {
|
|
86
|
+
removeListeners();
|
|
62
87
|
if (e.message.includes(rollbackErrorText)) {
|
|
63
88
|
throw new Error(abortErrorText);
|
|
64
89
|
}
|
|
65
|
-
req.socket.removeListener('close', rollback);
|
|
66
90
|
throw e;
|
|
67
91
|
}
|
|
68
92
|
});
|