@e-mc/db 0.11.7 → 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/README.md +10 -8
- package/index.js +24 -24
- package/package.json +4 -4
- package/pool.js +34 -26
- package/util.d.ts +0 -2
- package/util.js +1 -2
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @e-mc/db
|
|
2
2
|
|
|
3
|
-
* NodeJS
|
|
4
|
-
*
|
|
3
|
+
* NodeJS 18
|
|
4
|
+
* ES2022
|
|
5
5
|
|
|
6
6
|
## General Usage
|
|
7
7
|
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
## Interface
|
|
11
11
|
|
|
12
|
-
* [View Source](https://www.unpkg.com/@e-mc/types@0.
|
|
12
|
+
* [View Source](https://www.unpkg.com/@e-mc/types@0.12.0/lib/index.d.ts)
|
|
13
13
|
|
|
14
14
|
```typescript
|
|
15
15
|
import type { DbDataSource } from "./squared";
|
|
@@ -88,7 +88,9 @@ interface IDbPool {
|
|
|
88
88
|
|
|
89
89
|
interface DbPoolConstructor {
|
|
90
90
|
CACHE_UNUSED: readonly string[];
|
|
91
|
+
CACHE_IGNORE: readonly string[];
|
|
91
92
|
asString(credential: unknown): string;
|
|
93
|
+
canCache(credential: unknown): boolean;
|
|
92
94
|
sanitize(credential: unknown): unknown;
|
|
93
95
|
removeUUIDKey(credential: unknown): unknown;
|
|
94
96
|
findKey(pools: Record<string, IDbPool>, uuidKey: unknown, poolKey: string | undefined, ...items: DbDataSource[]): Record<string, IDbPool> | null;
|
|
@@ -204,11 +206,11 @@ const [rows1, rows2] = await instance.executeBatchQuery([
|
|
|
204
206
|
|
|
205
207
|
## References
|
|
206
208
|
|
|
207
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
208
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
209
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
210
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
211
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
209
|
+
- https://www.unpkg.com/@e-mc/types@0.12.0/lib/squared.d.ts
|
|
210
|
+
- https://www.unpkg.com/@e-mc/types@0.12.0/lib/core.d.ts
|
|
211
|
+
- https://www.unpkg.com/@e-mc/types@0.12.0/lib/db.d.ts
|
|
212
|
+
- https://www.unpkg.com/@e-mc/types@0.12.0/lib/http.d.ts
|
|
213
|
+
- https://www.unpkg.com/@e-mc/types@0.12.0/lib/settings.d.ts
|
|
212
214
|
|
|
213
215
|
* https://www.npmjs.com/package/@types/node
|
|
214
216
|
|
package/index.js
CHANGED
|
@@ -4,6 +4,7 @@ const types_1 = require("@e-mc/types");
|
|
|
4
4
|
const core_1 = require("@e-mc/core");
|
|
5
5
|
const request_1 = require("@e-mc/request");
|
|
6
6
|
const util_1 = require("@e-mc/db/util");
|
|
7
|
+
const kDb = Symbol.for('db:constructor');
|
|
7
8
|
const DB_CLIENT = new Map();
|
|
8
9
|
const POOL_CONFIG = new Map();
|
|
9
10
|
function sanitizePoolConfig(value) {
|
|
@@ -14,23 +15,14 @@ function sanitizePoolConfig(value) {
|
|
|
14
15
|
value.queue_idle ??= -1;
|
|
15
16
|
value.purge ??= 0;
|
|
16
17
|
value.timeout ??= -1;
|
|
18
|
+
value.server_timeout ??= -1;
|
|
17
19
|
value.socket_timeout ??= -1;
|
|
18
20
|
return value;
|
|
19
21
|
}
|
|
20
|
-
function setCert(instance, items, cache) {
|
|
21
|
-
if (Array.isArray(items)) {
|
|
22
|
-
return items.map(cert => (0, types_1.isString)(cert) ? instance.readTLSCert(cert, cache) : Buffer.isBuffer(cert) ? cert : null).filter(cert => cert);
|
|
23
|
-
}
|
|
24
|
-
if ((0, types_1.isString)(items)) {
|
|
25
|
-
return instance.readTLSCert(items, cache);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
22
|
class Db extends core_1.ClientDb {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
this._threadable = true;
|
|
33
|
-
}
|
|
23
|
+
static [kDb] = true;
|
|
24
|
+
static STORE_RESULT_PARTITION_SIZE = 16;
|
|
25
|
+
static STORE_RESULT_PARTITION_MULT = 2;
|
|
34
26
|
static async purgeMemory(percent = 1, limit = 0, parent) {
|
|
35
27
|
let result = 0;
|
|
36
28
|
if (percent > 0 && percent <= 1) {
|
|
@@ -61,6 +53,8 @@ class Db extends core_1.ClientDb {
|
|
|
61
53
|
static getPoolConfig(source) {
|
|
62
54
|
return POOL_CONFIG.get(source);
|
|
63
55
|
}
|
|
56
|
+
_moduleName = 'db';
|
|
57
|
+
_threadable = true;
|
|
64
58
|
async setCredential(item) {
|
|
65
59
|
try {
|
|
66
60
|
return this.getClient(item.source).setCredential.call(this, item);
|
|
@@ -239,17 +233,17 @@ class Db extends core_1.ClientDb {
|
|
|
239
233
|
if ((0, types_1.isPlainObject)(options)) {
|
|
240
234
|
const { ca, cert, key } = options;
|
|
241
235
|
if (ca) {
|
|
242
|
-
options.ca =
|
|
236
|
+
options.ca = this.#checkCert(ca);
|
|
243
237
|
}
|
|
244
238
|
if (cert) {
|
|
245
|
-
options.cert =
|
|
239
|
+
options.cert = this.#checkCert(cert);
|
|
246
240
|
}
|
|
247
241
|
if (key) {
|
|
248
242
|
if ((0, types_1.isString)(key)) {
|
|
249
243
|
options.key = this.readTLSCert(key, cache);
|
|
250
244
|
}
|
|
251
245
|
else if ((0, types_1.isArray)(key)) {
|
|
252
|
-
options.key = key.map(item => (0, types_1.isString)(item) ? { pem: this.readTLSCert(item, cache) } : Buffer.isBuffer(item) ? { pem: item.toString('
|
|
246
|
+
options.key = key.map(item => (0, types_1.isString)(item) ? { pem: this.readTLSCert(item, cache) } : Buffer.isBuffer(item) ? { pem: item.toString('utf8') } : item).filter(item => item.pem);
|
|
253
247
|
}
|
|
254
248
|
}
|
|
255
249
|
}
|
|
@@ -284,12 +278,6 @@ class Db extends core_1.ClientDb {
|
|
|
284
278
|
}
|
|
285
279
|
return config;
|
|
286
280
|
}
|
|
287
|
-
get sourceType() {
|
|
288
|
-
return types_1.DB_TYPE;
|
|
289
|
-
}
|
|
290
|
-
get commandType() {
|
|
291
|
-
return util_1.SQL_COMMAND;
|
|
292
|
-
}
|
|
293
281
|
getClient(source) {
|
|
294
282
|
let client = DB_CLIENT.get(source);
|
|
295
283
|
if (client) {
|
|
@@ -307,9 +295,21 @@ class Db extends core_1.ClientDb {
|
|
|
307
295
|
}
|
|
308
296
|
throw (0, types_1.errorMessage)(source, "Database provider not found");
|
|
309
297
|
}
|
|
298
|
+
#checkCert(items, cache) {
|
|
299
|
+
if (Array.isArray(items)) {
|
|
300
|
+
return items.map(cert => (0, types_1.isString)(cert) ? this.readTLSCert(cert, cache) : Buffer.isBuffer(cert) ? cert : null).filter(cert => cert);
|
|
301
|
+
}
|
|
302
|
+
if ((0, types_1.isString)(items)) {
|
|
303
|
+
return this.readTLSCert(items, cache);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
get sourceType() {
|
|
307
|
+
return types_1.DB_TYPE;
|
|
308
|
+
}
|
|
309
|
+
get commandType() {
|
|
310
|
+
return util_1.SQL_COMMAND;
|
|
311
|
+
}
|
|
310
312
|
}
|
|
311
|
-
Db.STORE_RESULT_PARTITION_SIZE = 16;
|
|
312
|
-
Db.STORE_RESULT_PARTITION_MULT = 2;
|
|
313
313
|
Object.freeze(types_1.DB_TYPE);
|
|
314
314
|
Object.freeze(util_1.SQL_COMMAND);
|
|
315
315
|
module.exports = Db;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/db",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "DB modules for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"license": "BSD-3-Clause",
|
|
21
21
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/core": "0.
|
|
24
|
-
"@e-mc/request": "0.
|
|
25
|
-
"@e-mc/types": "0.
|
|
23
|
+
"@e-mc/core": "0.12.0",
|
|
24
|
+
"@e-mc/request": "0.12.0",
|
|
25
|
+
"@e-mc/types": "0.12.0"
|
|
26
26
|
}
|
|
27
27
|
}
|
package/pool.js
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var _a, _b, _c;
|
|
3
2
|
const crypto = require("node:crypto");
|
|
4
3
|
const types_1 = require("@e-mc/types");
|
|
5
4
|
const util_1 = require("@e-mc/db/util");
|
|
6
|
-
const kItems = Symbol('items');
|
|
7
|
-
const kParent = Symbol('parent');
|
|
8
|
-
const kIdlePrevious = Symbol('idlePrevious');
|
|
9
5
|
class DbPool {
|
|
6
|
+
client;
|
|
7
|
+
poolKey;
|
|
8
|
+
uuidKey;
|
|
9
|
+
static CACHE_UNUSED = [];
|
|
10
|
+
static CACHE_IGNORE = [];
|
|
10
11
|
static asString(credential) {
|
|
11
12
|
return crypto.randomUUID();
|
|
12
13
|
}
|
|
14
|
+
static canCache(credential) {
|
|
15
|
+
const properties = this.CACHE_IGNORE;
|
|
16
|
+
for (let i = 0, length = properties.length; i < length; ++i) {
|
|
17
|
+
if (credential[properties[i]] !== undefined) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
13
23
|
static sanitize(credential) {
|
|
14
24
|
const properties = this.CACHE_UNUSED;
|
|
15
25
|
let result;
|
|
@@ -80,19 +90,19 @@ class DbPool {
|
|
|
80
90
|
}
|
|
81
91
|
return result;
|
|
82
92
|
}
|
|
93
|
+
success = 0;
|
|
94
|
+
failed = 0;
|
|
95
|
+
lastAccessed = 0;
|
|
96
|
+
#parent = null;
|
|
97
|
+
#items = new WeakSet();
|
|
98
|
+
#idleStats = { success: -1, failed: -1, count: 0, error: 0 };
|
|
83
99
|
constructor(client, poolKey, uuidKey = null) {
|
|
84
100
|
this.client = client;
|
|
85
101
|
this.poolKey = poolKey;
|
|
86
102
|
this.uuidKey = uuidKey;
|
|
87
|
-
this.success = 0;
|
|
88
|
-
this.failed = 0;
|
|
89
|
-
this.lastAccessed = 0;
|
|
90
|
-
this[_a] = new WeakSet();
|
|
91
|
-
this[_b] = null;
|
|
92
|
-
this[_c] = { success: -1, failed: -1, count: 0, error: 0 };
|
|
93
103
|
}
|
|
94
104
|
add(item, uuidKey) {
|
|
95
|
-
this
|
|
105
|
+
this.#items.add(item);
|
|
96
106
|
if (uuidKey ||= this.uuidKey?.password) {
|
|
97
107
|
(0, util_1.setUUIDKey)(item, uuidKey);
|
|
98
108
|
}
|
|
@@ -100,10 +110,10 @@ class DbPool {
|
|
|
100
110
|
return this;
|
|
101
111
|
}
|
|
102
112
|
has(item) {
|
|
103
|
-
return this
|
|
113
|
+
return this.#items.has(item);
|
|
104
114
|
}
|
|
105
115
|
remove() {
|
|
106
|
-
const parent = this
|
|
116
|
+
const parent = this.#parent;
|
|
107
117
|
if (parent) {
|
|
108
118
|
delete parent[this.poolKey];
|
|
109
119
|
}
|
|
@@ -120,22 +130,22 @@ class DbPool {
|
|
|
120
130
|
return true;
|
|
121
131
|
}
|
|
122
132
|
const { success, failed } = this;
|
|
123
|
-
const
|
|
133
|
+
const stats = this.#idleStats;
|
|
124
134
|
let count = 0, error = 0;
|
|
125
|
-
if (success ===
|
|
126
|
-
count = ++
|
|
127
|
-
if (failed >
|
|
128
|
-
error = ++
|
|
135
|
+
if (success === stats.success && failed >= stats.failed) {
|
|
136
|
+
count = ++stats.count;
|
|
137
|
+
if (failed > stats.failed) {
|
|
138
|
+
error = ++stats.error;
|
|
129
139
|
}
|
|
130
140
|
}
|
|
131
141
|
else {
|
|
132
|
-
|
|
133
|
-
if (failed ===
|
|
134
|
-
|
|
142
|
+
stats.count = 0;
|
|
143
|
+
if (failed === stats.failed) {
|
|
144
|
+
stats.error = 0;
|
|
135
145
|
}
|
|
136
146
|
}
|
|
137
|
-
|
|
138
|
-
|
|
147
|
+
stats.success = success;
|
|
148
|
+
stats.failed = failed;
|
|
139
149
|
return Date.now() - timeout >= this.lastAccessed && (this.closeable || count >= (this.uuidKey ? 10 : 5)) || error >= (this.uuidKey ? 3 : 2);
|
|
140
150
|
}
|
|
141
151
|
get persist() {
|
|
@@ -155,9 +165,7 @@ class DbPool {
|
|
|
155
165
|
}
|
|
156
166
|
set parent(value) {
|
|
157
167
|
value[this.poolKey] = this;
|
|
158
|
-
this
|
|
168
|
+
this.#parent = value;
|
|
159
169
|
}
|
|
160
170
|
}
|
|
161
|
-
_a = kItems, _b = kParent, _c = kIdlePrevious;
|
|
162
|
-
DbPool.CACHE_UNUSED = [];
|
|
163
171
|
module.exports = DbPool;
|
package/util.d.ts
CHANGED
|
@@ -5,8 +5,6 @@ import type { AuthValue } from '@e-mc/types/lib/http';
|
|
|
5
5
|
|
|
6
6
|
declare namespace util {
|
|
7
7
|
const SQL_COMMAND: SQL_COMMAND;
|
|
8
|
-
/** @deprecated Types.IMPORT_MAP */
|
|
9
|
-
const IMPORTS: Record<string, string | undefined>;
|
|
10
8
|
function getBasicAuth(auth: AuthValue): string;
|
|
11
9
|
function getBasicAuth(username: unknown, password?: unknown): string;
|
|
12
10
|
function hasBasicAuth(value: string): boolean;
|
package/util.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
exports.hasBasicAuth = exports.getBasicAuth = exports.
|
|
2
|
+
exports.hasBasicAuth = exports.getBasicAuth = exports.SQL_COMMAND = void 0;
|
|
3
3
|
exports.parseServerAuth = parseServerAuth;
|
|
4
4
|
exports.parseConnectionString = parseConnectionString;
|
|
5
5
|
exports.checkEmpty = checkEmpty;
|
|
6
6
|
exports.setUUIDKey = setUUIDKey;
|
|
7
7
|
const types_1 = require("@e-mc/types");
|
|
8
|
-
Object.defineProperty(exports, "IMPORTS", { enumerable: true, get: function () { return types_1.IMPORT_MAP; } });
|
|
9
8
|
const util_1 = require("@e-mc/request/util");
|
|
10
9
|
Object.defineProperty(exports, "getBasicAuth", { enumerable: true, get: function () { return util_1.getBasicAuth; } });
|
|
11
10
|
Object.defineProperty(exports, "hasBasicAuth", { enumerable: true, get: function () { return util_1.hasBasicAuth; } });
|