delore-crm-core 1.0.13 → 1.0.15
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/db/sequelize_db.js +46 -8
- package/index.js +19 -14
- package/package.json +1 -1
- package/utils/utilCripto.js +58 -0
package/db/sequelize_db.js
CHANGED
|
@@ -9,6 +9,11 @@ const uri = process.env.MONGODB_URI;
|
|
|
9
9
|
const client = new MongoClient(uri);
|
|
10
10
|
var db = null;
|
|
11
11
|
|
|
12
|
+
function envInt(name, defaultValue) {
|
|
13
|
+
const value = Number.parseInt(process.env[name], 10);
|
|
14
|
+
return Number.isNaN(value) ? defaultValue : value;
|
|
15
|
+
}
|
|
16
|
+
|
|
12
17
|
async function connectMongoDB() {
|
|
13
18
|
try {
|
|
14
19
|
await client.connect();
|
|
@@ -31,9 +36,10 @@ module.exports = {
|
|
|
31
36
|
},
|
|
32
37
|
init(onConnect) {
|
|
33
38
|
let isolationLevel =
|
|
34
|
-
process.env.DB_ISOLATION === "
|
|
35
|
-
? Sequelize.Transaction.ISOLATION_LEVELS.
|
|
36
|
-
: Sequelize.Transaction.ISOLATION_LEVELS.
|
|
39
|
+
process.env.DB_ISOLATION === "REPEATABLE_READ"
|
|
40
|
+
? Sequelize.Transaction.ISOLATION_LEVELS.REPEATABLE_READ
|
|
41
|
+
: Sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED;
|
|
42
|
+
const lockWaitTimeout = envInt("DB_LOCK_WAIT_TIMEOUT", 15);
|
|
37
43
|
|
|
38
44
|
logger.info("init - - > Iniciando conexão com o MySQL..." + isolationLevel);
|
|
39
45
|
|
|
@@ -45,16 +51,48 @@ module.exports = {
|
|
|
45
51
|
dialect: "mysql",
|
|
46
52
|
host: process.env.DB_HOST,
|
|
47
53
|
pool: {
|
|
48
|
-
max:
|
|
49
|
-
min:
|
|
50
|
-
idle:
|
|
51
|
-
acquire:
|
|
52
|
-
evict:
|
|
54
|
+
max: envInt("DB_POOL_MAX", 30),
|
|
55
|
+
min: envInt("DB_POOL_MIN", 0),
|
|
56
|
+
idle: envInt("DB_POOL_IDLE", 10000),
|
|
57
|
+
acquire: envInt("DB_POOL_ACQUIRE", 30000),
|
|
58
|
+
evict: envInt("DB_POOL_EVICT", 10000),
|
|
53
59
|
},
|
|
54
60
|
timezone: "-03:00",
|
|
55
61
|
dialectOptions: {
|
|
56
62
|
multipleStatements: true,
|
|
57
63
|
},
|
|
64
|
+
retry: {
|
|
65
|
+
max: envInt("DB_RETRY_MAX", 3),
|
|
66
|
+
match: [
|
|
67
|
+
/Deadlock/i,
|
|
68
|
+
/ER_LOCK_DEADLOCK/,
|
|
69
|
+
/ER_LOCK_WAIT_TIMEOUT/,
|
|
70
|
+
/Lock wait timeout exceeded/i,
|
|
71
|
+
],
|
|
72
|
+
backoffBase: envInt("DB_RETRY_BACKOFF_BASE", 100),
|
|
73
|
+
backoffExponent: 1.5,
|
|
74
|
+
},
|
|
75
|
+
hooks: {
|
|
76
|
+
afterConnect: async (connection) => {
|
|
77
|
+
const query = (sql) =>
|
|
78
|
+
new Promise((resolve, reject) => {
|
|
79
|
+
connection.query(sql, (error) => {
|
|
80
|
+
if (error) reject(error);
|
|
81
|
+
else resolve();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
await query(
|
|
86
|
+
`SET SESSION TRANSACTION ISOLATION LEVEL ${isolationLevel.replace(
|
|
87
|
+
"_",
|
|
88
|
+
" ",
|
|
89
|
+
)}`,
|
|
90
|
+
);
|
|
91
|
+
await query(
|
|
92
|
+
`SET SESSION innodb_lock_wait_timeout = ${lockWaitTimeout}`,
|
|
93
|
+
);
|
|
94
|
+
},
|
|
95
|
+
},
|
|
58
96
|
define: {
|
|
59
97
|
underscored: false,
|
|
60
98
|
freezeTableName: true,
|
package/index.js
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
|
-
const access = require(
|
|
4
|
-
const util = require(
|
|
5
|
-
const logger = require(
|
|
6
|
-
const memory = require(
|
|
7
|
-
const print = require(
|
|
8
|
-
const authControl = require(
|
|
9
|
-
const ret = require(
|
|
10
|
-
const validRequest = require(
|
|
11
|
-
const syncAccess = require(
|
|
12
|
-
const sequelizeDB = require(
|
|
13
|
-
const
|
|
3
|
+
const access = require("./utils/access");
|
|
4
|
+
const util = require("./utils/util");
|
|
5
|
+
const logger = require("./utils/logger");
|
|
6
|
+
const memory = require("./utils/memory");
|
|
7
|
+
const print = require("./print");
|
|
8
|
+
const authControl = require("./auth/auth.control");
|
|
9
|
+
const ret = require("./auth/ret");
|
|
10
|
+
const validRequest = require("./auth/valid.request");
|
|
11
|
+
const syncAccess = require("./sync/sync.access");
|
|
12
|
+
const sequelizeDB = require("./db/sequelize_db");
|
|
13
|
+
const utilCripto = require("./utils/utilCripto");
|
|
14
|
+
const models = require("./db/models");
|
|
14
15
|
|
|
15
|
-
const exported = Object.assign(
|
|
16
|
+
const exported = Object.assign(
|
|
17
|
+
{},
|
|
18
|
+
{
|
|
16
19
|
access,
|
|
17
20
|
util,
|
|
18
21
|
logger,
|
|
@@ -24,6 +27,8 @@ const exported = Object.assign({}, {
|
|
|
24
27
|
syncAccess,
|
|
25
28
|
sequelizeDB,
|
|
26
29
|
models,
|
|
27
|
-
|
|
30
|
+
utilCripto,
|
|
31
|
+
},
|
|
32
|
+
);
|
|
28
33
|
|
|
29
34
|
module.exports = exported;
|
package/package.json
CHANGED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const crypto = require("crypto");
|
|
2
|
+
const logger = require("./logger");
|
|
3
|
+
|
|
4
|
+
const ALGORITHM = "aes-256-gcm";
|
|
5
|
+
const SEPARATOR = ":";
|
|
6
|
+
|
|
7
|
+
function encrypt(text, secretKey) {
|
|
8
|
+
const SECRET_KEY = crypto.createHash("sha256").update(secretKey).digest(); // 32 bytes
|
|
9
|
+
|
|
10
|
+
const iv = crypto.randomBytes(12);
|
|
11
|
+
|
|
12
|
+
const cipher = crypto.createCipheriv(ALGORITHM, SECRET_KEY, iv);
|
|
13
|
+
|
|
14
|
+
const encrypted = Buffer.concat([
|
|
15
|
+
cipher.update(text, "utf8"),
|
|
16
|
+
cipher.final(),
|
|
17
|
+
]);
|
|
18
|
+
|
|
19
|
+
const authTag = cipher.getAuthTag();
|
|
20
|
+
|
|
21
|
+
return [
|
|
22
|
+
iv.toString("hex"),
|
|
23
|
+
authTag.toString("hex"),
|
|
24
|
+
encrypted.toString("hex"),
|
|
25
|
+
].join(SEPARATOR);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function decrypt(encryptedData, secretKey) {
|
|
29
|
+
try {
|
|
30
|
+
const SECRET_KEY = crypto.createHash("sha256").update(secretKey).digest(); // 32 bytes
|
|
31
|
+
|
|
32
|
+
const [ivHex, tagHex, contentHex] = encryptedData.split(SEPARATOR);
|
|
33
|
+
|
|
34
|
+
if (!ivHex || !tagHex || !contentHex) {
|
|
35
|
+
throw new Error("Formato de dado criptografado inválido");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const decipher = crypto.createDecipheriv(
|
|
39
|
+
ALGORITHM,
|
|
40
|
+
SECRET_KEY,
|
|
41
|
+
Buffer.from(ivHex, "hex"),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
decipher.setAuthTag(Buffer.from(tagHex, "hex"));
|
|
45
|
+
|
|
46
|
+
const decrypted = Buffer.concat([
|
|
47
|
+
decipher.update(Buffer.from(contentHex, "hex")),
|
|
48
|
+
decipher.final(),
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
return decrypted.toString("utf8");
|
|
52
|
+
} catch (error) {
|
|
53
|
+
logger.error("Error decrypt " + error.message);
|
|
54
|
+
throw new Error("Error decrypt " + error.message);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = { encrypt, decrypt };
|