@pi-r/mariadb 0.11.2 → 0.12.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/client/index.js +31 -36
- package/client/pool.js +6 -4
- package/package.json +3 -3
package/client/index.js
CHANGED
|
@@ -1,26 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const mariadb = require("mariadb");
|
|
9
|
-
const Db = require("@e-mc/db");
|
|
10
|
-
const types_1 = require("@e-mc/types");
|
|
11
|
-
const util_1 = require("@e-mc/db/util");
|
|
12
|
-
const DbPool = require("@pi-r/mariadb/client/pool");
|
|
2
|
+
|
|
3
|
+
const mariadb = require('mariadb');
|
|
4
|
+
const Db = require('@e-mc/db');
|
|
5
|
+
const { DB_TYPE, asFunction, errorMessage, isArray, isError, isErrorCode, isPlainObject, isString } = require('@e-mc/types');
|
|
6
|
+
const { parseConnectionString, parseServerAuth } = require('@e-mc/db/util');
|
|
7
|
+
const DbPool = require('@pi-r/mariadb/client/pool');
|
|
13
8
|
const POOL_STATE = {};
|
|
14
9
|
function isOkPacket(value) {
|
|
15
10
|
const result = Array.isArray(value) ? value[0] : value;
|
|
16
11
|
return !!result && !Array.isArray(result) && result.constructor.name === 'OkPacket';
|
|
17
12
|
}
|
|
18
13
|
function setAuthentication(credential) {
|
|
19
|
-
const auth =
|
|
14
|
+
const auth = parseServerAuth(credential);
|
|
20
15
|
credential.host ||= auth.hostname;
|
|
21
16
|
credential.user ||= auth.username;
|
|
22
17
|
credential.port ||= 3306;
|
|
23
|
-
if (
|
|
18
|
+
if (isPlainObject(credential.ssl)) {
|
|
24
19
|
this.readTLSConfig(credential.ssl);
|
|
25
20
|
}
|
|
26
21
|
}
|
|
@@ -32,7 +27,7 @@ function setCredential(item) {
|
|
|
32
27
|
else {
|
|
33
28
|
credential = {};
|
|
34
29
|
if (item.uri) {
|
|
35
|
-
const connection =
|
|
30
|
+
const connection = parseConnectionString(item.uri);
|
|
36
31
|
if (connection) {
|
|
37
32
|
const { username, password, hostname, port, database } = connection;
|
|
38
33
|
credential.host = hostname;
|
|
@@ -54,12 +49,12 @@ function setCredential(item) {
|
|
|
54
49
|
errors.push('user');
|
|
55
50
|
}
|
|
56
51
|
if (errors.length > 0) {
|
|
57
|
-
throw
|
|
52
|
+
throw errorMessage("mariadb", "Invalid credentials", errors.join(', '));
|
|
58
53
|
}
|
|
59
54
|
if (credential.host?.endsWith('.rds.amazonaws.com')) {
|
|
60
55
|
try {
|
|
61
56
|
const ssl = credential.ssl ||= {};
|
|
62
|
-
if (
|
|
57
|
+
if (isPlainObject(ssl) && !ssl.ca) {
|
|
63
58
|
const bundle = require('aws-ssl-profiles');
|
|
64
59
|
ssl.ca = bundle.ca;
|
|
65
60
|
}
|
|
@@ -76,7 +71,7 @@ function setCredential(item) {
|
|
|
76
71
|
return;
|
|
77
72
|
}
|
|
78
73
|
let username, password, pool;
|
|
79
|
-
if (
|
|
74
|
+
if (isString(usePool)) {
|
|
80
75
|
username = credential.user;
|
|
81
76
|
if (username) {
|
|
82
77
|
[password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
|
|
@@ -134,7 +129,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
134
129
|
return [];
|
|
135
130
|
}
|
|
136
131
|
let parallel, connectOnce, errorQuery, sessionKey, outCacheMiss;
|
|
137
|
-
if (
|
|
132
|
+
if (isPlainObject(options)) {
|
|
138
133
|
({ parallel, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
|
|
139
134
|
}
|
|
140
135
|
else {
|
|
@@ -177,20 +172,12 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
177
172
|
}
|
|
178
173
|
catch (err) {
|
|
179
174
|
let close;
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
case 'ER_DBACCESS_DENIED_ERROR':
|
|
183
|
-
case 'ER_SPECIFIC_ACCESS_DENIED_ERROR':
|
|
184
|
-
case 'ER_ACCESS_DENIED_NO_PASSWORD_ERROR':
|
|
185
|
-
close = 1;
|
|
186
|
-
break;
|
|
187
|
-
default:
|
|
188
|
-
close = pool.closeable;
|
|
189
|
-
break;
|
|
175
|
+
if (!isErrorCode(err, 'ER_ACCESS_DENIED_ERROR', 'ER_DBACCESS_DENIED_ERROR', 'ER_SPECIFIC_ACCESS_DENIED_ERROR', 'ER_ACCESS_DENIED_NO_PASSWORD_ERROR')) {
|
|
176
|
+
close = pool.closeable;
|
|
190
177
|
}
|
|
191
|
-
if (close) {
|
|
178
|
+
if (close !== false) {
|
|
192
179
|
await pool.detach(true);
|
|
193
|
-
if (close
|
|
180
|
+
if (!close) {
|
|
194
181
|
throw err;
|
|
195
182
|
}
|
|
196
183
|
}
|
|
@@ -215,7 +202,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
215
202
|
for (let i = 0, statement; i < length; ++i) {
|
|
216
203
|
const item = batch[i];
|
|
217
204
|
const { source, params, ignoreCache } = item;
|
|
218
|
-
const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("mariadb", 'options', item.credential) &&
|
|
205
|
+
const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("mariadb", 'options', item.credential) && asFunction(item.streamRow) : item.streamRow;
|
|
219
206
|
let credential = mariaDBCredential || onceCredential, query = item.query, prepared = false, error;
|
|
220
207
|
if (typeof item.preparedStatement === 'boolean') {
|
|
221
208
|
if (item.preparedStatement) {
|
|
@@ -235,7 +222,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
235
222
|
query = statement;
|
|
236
223
|
prepared = true;
|
|
237
224
|
}
|
|
238
|
-
if (!
|
|
225
|
+
if (!isPlainObject(credential = item.credential) && (error = errorMessage(source, "Invalid credentials")) || !query && (error = errorMessage(source, "Missing database query"))) {
|
|
239
226
|
if (this.handleFail(error, item, { errorQuery })) {
|
|
240
227
|
if (!parallel) {
|
|
241
228
|
tasks.length = 0;
|
|
@@ -301,7 +288,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
301
288
|
if (err === false) {
|
|
302
289
|
continue;
|
|
303
290
|
}
|
|
304
|
-
if (err
|
|
291
|
+
if (isError(err)) {
|
|
305
292
|
error = err;
|
|
306
293
|
continue;
|
|
307
294
|
}
|
|
@@ -323,7 +310,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
323
310
|
else {
|
|
324
311
|
const rows = await client[prepared && connectOnce && !parallel ? 'execute' : 'query'](query, params);
|
|
325
312
|
this.add(item, 4);
|
|
326
|
-
if (
|
|
313
|
+
if (isArray(rows)) {
|
|
327
314
|
const [row1, row2] = rows;
|
|
328
315
|
if (rows.length === 2 && Array.isArray(row1) && !Array.isArray(row2) && isOkPacket(row2)) {
|
|
329
316
|
resolve(row1);
|
|
@@ -371,5 +358,13 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
371
358
|
async function checkTimeout(value, limit = 0) {
|
|
372
359
|
return DbPool.checkTimeout(POOL_STATE, value, limit);
|
|
373
360
|
}
|
|
374
|
-
|
|
375
|
-
|
|
361
|
+
const DB_SOURCE_CLIENT = true;
|
|
362
|
+
const DB_SOURCE_TYPE = DB_TYPE.SQL;
|
|
363
|
+
|
|
364
|
+
exports.DB_SOURCE_CLIENT = DB_SOURCE_CLIENT;
|
|
365
|
+
exports.DB_SOURCE_TYPE = DB_SOURCE_TYPE;
|
|
366
|
+
exports.checkTimeout = checkTimeout;
|
|
367
|
+
exports.executeBatchQuery = executeBatchQuery;
|
|
368
|
+
exports.executeQuery = executeQuery;
|
|
369
|
+
exports.setAuthentication = setAuthentication;
|
|
370
|
+
exports.setCredential = setCredential;
|
package/client/pool.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
const { isArray, isObject, isString } = require('@e-mc/types');
|
|
3
4
|
const DbPool = require('@e-mc/db/pool');
|
|
4
5
|
const POOL_ACTIVE = new WeakSet();
|
|
5
6
|
class MariaDBPool extends DbPool {
|
|
@@ -20,18 +21,18 @@ class MariaDBPool extends DbPool {
|
|
|
20
21
|
}
|
|
21
22
|
const ssl = credential.ssl;
|
|
22
23
|
let result = super.sanitize(credential);
|
|
23
|
-
if (
|
|
24
|
+
if (isObject(ssl)) {
|
|
24
25
|
if (result === credential) {
|
|
25
26
|
result = { ...credential };
|
|
26
27
|
}
|
|
27
28
|
const cert = ssl.cert;
|
|
28
|
-
if (
|
|
29
|
+
if (isString(cert)) {
|
|
29
30
|
result.ssl = { cert };
|
|
30
31
|
}
|
|
31
32
|
else if (Buffer.isBuffer(cert)) {
|
|
32
33
|
result.ssl = { cert: cert.toString('utf8') };
|
|
33
34
|
}
|
|
34
|
-
else if (
|
|
35
|
+
else if (isArray(cert)) {
|
|
35
36
|
result.ssl = { cert: cert.reduce((a, b) => a + (Buffer.isBuffer(b) ? b.toString('utf8') : b), '') };
|
|
36
37
|
}
|
|
37
38
|
else {
|
|
@@ -57,4 +58,5 @@ class MariaDBPool extends DbPool {
|
|
|
57
58
|
return this.client.closed;
|
|
58
59
|
}
|
|
59
60
|
}
|
|
61
|
+
|
|
60
62
|
module.exports = MariaDBPool;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/mariadb",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "MariaDB client driver for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"types": "client/index.d.ts",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/db": "^0.
|
|
24
|
-
"@e-mc/types": "^0.
|
|
23
|
+
"@e-mc/db": "^0.14.0",
|
|
24
|
+
"@e-mc/types": "^0.14.0",
|
|
25
25
|
"mariadb": "^3.5.2"
|
|
26
26
|
}
|
|
27
27
|
}
|