@dbcube/core 3.0.21 → 3.0.22
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.cjs +23 -186
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +0 -2
- package/dist/index.d.ts +0 -2
- package/dist/index.js +23 -193
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
-
}) : x)(function(x) {
|
|
4
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
-
});
|
|
7
|
-
|
|
8
1
|
// src/lib/Engine.ts
|
|
9
2
|
import path3 from "path";
|
|
10
3
|
|
|
@@ -728,166 +721,11 @@ import path4 from "path";
|
|
|
728
721
|
import { createRequire as createRequire2 } from "module";
|
|
729
722
|
import * as net from "net";
|
|
730
723
|
import { spawn as spawn2 } from "child_process";
|
|
731
|
-
var MAX_CONNECTIONS_PER_DB = 10;
|
|
732
|
-
var MAX_CACHE_ENTRIES = 1e3;
|
|
733
|
-
var CACHE_CLEANUP_THRESHOLD = 0.9;
|
|
734
|
-
var CONNECTION_IDLE_TIMEOUT = 3e5;
|
|
735
|
-
var CONNECTION_MAX_AGE = 36e5;
|
|
736
|
-
var LRUCache = class {
|
|
737
|
-
cache;
|
|
738
|
-
maxSize;
|
|
739
|
-
cleanupThreshold;
|
|
740
|
-
constructor(maxSize, cleanupThreshold = 0.9) {
|
|
741
|
-
this.cache = /* @__PURE__ */ new Map();
|
|
742
|
-
this.maxSize = maxSize;
|
|
743
|
-
this.cleanupThreshold = cleanupThreshold;
|
|
744
|
-
}
|
|
745
|
-
get(key) {
|
|
746
|
-
const entry = this.cache.get(key);
|
|
747
|
-
if (!entry) return void 0;
|
|
748
|
-
entry.lastAccessed = Date.now();
|
|
749
|
-
entry.accessCount++;
|
|
750
|
-
return entry.value;
|
|
751
|
-
}
|
|
752
|
-
set(key, value) {
|
|
753
|
-
if (this.cache.size >= this.maxSize * this.cleanupThreshold) {
|
|
754
|
-
this.evictLRU();
|
|
755
|
-
}
|
|
756
|
-
this.cache.set(key, {
|
|
757
|
-
value,
|
|
758
|
-
lastAccessed: Date.now(),
|
|
759
|
-
accessCount: 1
|
|
760
|
-
});
|
|
761
|
-
}
|
|
762
|
-
evictLRU() {
|
|
763
|
-
const entries = Array.from(this.cache.entries()).sort((a, b) => {
|
|
764
|
-
const scoreDiff = this.calculateScore(a[1]) - this.calculateScore(b[1]);
|
|
765
|
-
if (scoreDiff !== 0) return scoreDiff;
|
|
766
|
-
return a[1].lastAccessed - b[1].lastAccessed;
|
|
767
|
-
});
|
|
768
|
-
const toRemove = Math.floor(this.maxSize * 0.2);
|
|
769
|
-
for (let i = 0; i < toRemove && i < entries.length; i++) {
|
|
770
|
-
this.cache.delete(entries[i][0]);
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
|
-
calculateScore(entry) {
|
|
774
|
-
const now = Date.now();
|
|
775
|
-
const age = now - entry.lastAccessed;
|
|
776
|
-
const recencyScore = 1 / (age + 1);
|
|
777
|
-
const frequencyScore = entry.accessCount;
|
|
778
|
-
return recencyScore * 0.3 + frequencyScore * 0.7;
|
|
779
|
-
}
|
|
780
|
-
clear() {
|
|
781
|
-
this.cache.clear();
|
|
782
|
-
}
|
|
783
|
-
size() {
|
|
784
|
-
return this.cache.size;
|
|
785
|
-
}
|
|
786
|
-
};
|
|
787
|
-
var ConnectionPool = class {
|
|
788
|
-
pools;
|
|
789
|
-
maxConnectionsPerDb;
|
|
790
|
-
constructor(maxConnectionsPerDb = MAX_CONNECTIONS_PER_DB) {
|
|
791
|
-
this.pools = /* @__PURE__ */ new Map();
|
|
792
|
-
this.maxConnectionsPerDb = maxConnectionsPerDb;
|
|
793
|
-
setInterval(() => this.cleanupIdleConnections(), 6e4);
|
|
794
|
-
}
|
|
795
|
-
getConnection(connectionId) {
|
|
796
|
-
const pool = this.pools.get(connectionId);
|
|
797
|
-
if (!pool || pool.length === 0) return null;
|
|
798
|
-
for (let i = 0; i < pool.length; i++) {
|
|
799
|
-
const conn = pool[i];
|
|
800
|
-
if (this.isConnectionValid(conn)) {
|
|
801
|
-
conn.lastUsed = Date.now();
|
|
802
|
-
conn.requestCount++;
|
|
803
|
-
return conn.socket;
|
|
804
|
-
} else {
|
|
805
|
-
conn.socket.destroy();
|
|
806
|
-
pool.splice(i, 1);
|
|
807
|
-
i--;
|
|
808
|
-
}
|
|
809
|
-
}
|
|
810
|
-
return null;
|
|
811
|
-
}
|
|
812
|
-
addConnection(connectionId, socket) {
|
|
813
|
-
if (!this.pools.has(connectionId)) {
|
|
814
|
-
this.pools.set(connectionId, []);
|
|
815
|
-
}
|
|
816
|
-
const pool = this.pools.get(connectionId);
|
|
817
|
-
if (pool.length >= this.maxConnectionsPerDb) {
|
|
818
|
-
const oldest = pool.shift();
|
|
819
|
-
if (oldest) {
|
|
820
|
-
oldest.socket.destroy();
|
|
821
|
-
}
|
|
822
|
-
}
|
|
823
|
-
pool.push({
|
|
824
|
-
socket,
|
|
825
|
-
lastUsed: Date.now(),
|
|
826
|
-
createdAt: Date.now(),
|
|
827
|
-
requestCount: 0
|
|
828
|
-
});
|
|
829
|
-
}
|
|
830
|
-
removeConnection(connectionId, socket) {
|
|
831
|
-
const pool = this.pools.get(connectionId);
|
|
832
|
-
if (!pool) return;
|
|
833
|
-
const index = pool.findIndex((c) => c.socket === socket);
|
|
834
|
-
if (index !== -1) {
|
|
835
|
-
pool[index].socket.destroy();
|
|
836
|
-
pool.splice(index, 1);
|
|
837
|
-
}
|
|
838
|
-
if (pool.length === 0) {
|
|
839
|
-
this.pools.delete(connectionId);
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
closeAll(connectionId) {
|
|
843
|
-
if (connectionId) {
|
|
844
|
-
const pool = this.pools.get(connectionId);
|
|
845
|
-
if (pool) {
|
|
846
|
-
pool.forEach((conn) => conn.socket.destroy());
|
|
847
|
-
this.pools.delete(connectionId);
|
|
848
|
-
}
|
|
849
|
-
} else {
|
|
850
|
-
this.pools.forEach((pool) => {
|
|
851
|
-
pool.forEach((conn) => conn.socket.destroy());
|
|
852
|
-
});
|
|
853
|
-
this.pools.clear();
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
isConnectionValid(conn) {
|
|
857
|
-
const now = Date.now();
|
|
858
|
-
const age = now - conn.createdAt;
|
|
859
|
-
return conn.socket.readyState === "open" && age < CONNECTION_MAX_AGE;
|
|
860
|
-
}
|
|
861
|
-
cleanupIdleConnections() {
|
|
862
|
-
const now = Date.now();
|
|
863
|
-
this.pools.forEach((pool, connectionId) => {
|
|
864
|
-
for (let i = pool.length - 1; i >= 0; i--) {
|
|
865
|
-
const conn = pool[i];
|
|
866
|
-
const idleTime = now - conn.lastUsed;
|
|
867
|
-
if (idleTime > CONNECTION_IDLE_TIMEOUT || !this.isConnectionValid(conn)) {
|
|
868
|
-
conn.socket.destroy();
|
|
869
|
-
pool.splice(i, 1);
|
|
870
|
-
}
|
|
871
|
-
}
|
|
872
|
-
if (pool.length === 0) {
|
|
873
|
-
this.pools.delete(connectionId);
|
|
874
|
-
}
|
|
875
|
-
});
|
|
876
|
-
}
|
|
877
|
-
getStats() {
|
|
878
|
-
const stats = {};
|
|
879
|
-
this.pools.forEach((pool, id) => {
|
|
880
|
-
stats[id] = {
|
|
881
|
-
activeConnections: pool.length,
|
|
882
|
-
totalRequests: pool.reduce((sum, c) => sum + c.requestCount, 0)
|
|
883
|
-
};
|
|
884
|
-
});
|
|
885
|
-
return stats;
|
|
886
|
-
}
|
|
887
|
-
};
|
|
888
724
|
var globalTcpServers = /* @__PURE__ */ new Map();
|
|
889
|
-
var
|
|
890
|
-
var queryCache = new
|
|
725
|
+
var globalTcpConnections = /* @__PURE__ */ new Map();
|
|
726
|
+
var queryCache = /* @__PURE__ */ new Map();
|
|
727
|
+
var cacheSize = 0;
|
|
728
|
+
var MAX_CACHE_SIZE = 500;
|
|
891
729
|
var QueryEngine = class {
|
|
892
730
|
name;
|
|
893
731
|
config;
|
|
@@ -917,8 +755,7 @@ var QueryEngine = class {
|
|
|
917
755
|
}
|
|
918
756
|
isPortAvailable(port) {
|
|
919
757
|
return new Promise((resolve5) => {
|
|
920
|
-
const
|
|
921
|
-
const tester = net2.createServer();
|
|
758
|
+
const tester = net.createServer();
|
|
922
759
|
tester.once("error", () => resolve5(false));
|
|
923
760
|
tester.once("listening", () => {
|
|
924
761
|
tester.close();
|
|
@@ -1042,12 +879,14 @@ var QueryEngine = class {
|
|
|
1042
879
|
return new Promise((resolve5) => setTimeout(resolve5, ms));
|
|
1043
880
|
}
|
|
1044
881
|
getCachedDML(dmlJson) {
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
return cached;
|
|
882
|
+
if (queryCache.has(dmlJson)) {
|
|
883
|
+
return queryCache.get(dmlJson);
|
|
1048
884
|
}
|
|
1049
885
|
const parsed = JSON.parse(dmlJson);
|
|
1050
|
-
|
|
886
|
+
if (cacheSize < MAX_CACHE_SIZE) {
|
|
887
|
+
queryCache.set(dmlJson, parsed);
|
|
888
|
+
cacheSize++;
|
|
889
|
+
}
|
|
1051
890
|
return parsed;
|
|
1052
891
|
}
|
|
1053
892
|
async startTcpServer() {
|
|
@@ -1161,12 +1000,13 @@ var QueryEngine = class {
|
|
|
1161
1000
|
});
|
|
1162
1001
|
}
|
|
1163
1002
|
async sendTcpRequestFast(args) {
|
|
1164
|
-
const existingConnection =
|
|
1003
|
+
const existingConnection = globalTcpConnections.get(this.connectionId);
|
|
1165
1004
|
if (existingConnection && existingConnection.readyState === "open") {
|
|
1166
1005
|
try {
|
|
1167
1006
|
return await this.sendOnExistingConnection(existingConnection, args);
|
|
1168
1007
|
} catch (error) {
|
|
1169
|
-
|
|
1008
|
+
globalTcpConnections.delete(this.connectionId);
|
|
1009
|
+
existingConnection.destroy();
|
|
1170
1010
|
}
|
|
1171
1011
|
}
|
|
1172
1012
|
return await this.createPersistentConnection(args);
|
|
@@ -1251,7 +1091,7 @@ var QueryEngine = class {
|
|
|
1251
1091
|
}
|
|
1252
1092
|
return;
|
|
1253
1093
|
}
|
|
1254
|
-
|
|
1094
|
+
globalTcpConnections.set(this.connectionId, client);
|
|
1255
1095
|
const command = {
|
|
1256
1096
|
action: "execute",
|
|
1257
1097
|
dml: dmlJson
|
|
@@ -1278,12 +1118,12 @@ var QueryEngine = class {
|
|
|
1278
1118
|
client.on("error", (error) => {
|
|
1279
1119
|
if (!isResolved) {
|
|
1280
1120
|
isResolved = true;
|
|
1281
|
-
|
|
1121
|
+
globalTcpConnections.delete(this.connectionId);
|
|
1282
1122
|
reject(error);
|
|
1283
1123
|
}
|
|
1284
1124
|
});
|
|
1285
1125
|
client.on("close", () => {
|
|
1286
|
-
|
|
1126
|
+
globalTcpConnections.delete(this.connectionId);
|
|
1287
1127
|
if (!isResolved && !responseBuffer.includes("PROCESS_RESPONSE:")) {
|
|
1288
1128
|
reject(new Error("Connection closed without valid response"));
|
|
1289
1129
|
}
|
|
@@ -1378,7 +1218,12 @@ var QueryEngine = class {
|
|
|
1378
1218
|
});
|
|
1379
1219
|
}
|
|
1380
1220
|
async disconnect() {
|
|
1381
|
-
|
|
1221
|
+
const connection = globalTcpConnections.get(this.connectionId);
|
|
1222
|
+
if (connection && connection.readyState === "open") {
|
|
1223
|
+
connection.write(JSON.stringify({ action: "disconnect" }));
|
|
1224
|
+
connection.destroy();
|
|
1225
|
+
globalTcpConnections.delete(this.connectionId);
|
|
1226
|
+
}
|
|
1382
1227
|
const serverInfo = globalTcpServers.get(this.connectionId);
|
|
1383
1228
|
if (serverInfo && serverInfo.process && !serverInfo.process.killed) {
|
|
1384
1229
|
serverInfo.process.kill();
|
|
@@ -1395,21 +1240,6 @@ var QueryEngine = class {
|
|
|
1395
1240
|
data: null
|
|
1396
1241
|
};
|
|
1397
1242
|
}
|
|
1398
|
-
// Método para obtener estadísticas del pool y cache
|
|
1399
|
-
static getStats() {
|
|
1400
|
-
return {
|
|
1401
|
-
connectionPool: connectionPool.getStats(),
|
|
1402
|
-
cache: {
|
|
1403
|
-
size: queryCache.size(),
|
|
1404
|
-
maxSize: MAX_CACHE_ENTRIES
|
|
1405
|
-
}
|
|
1406
|
-
};
|
|
1407
|
-
}
|
|
1408
|
-
// Método para limpiar todo el cache y conexiones
|
|
1409
|
-
static clearAll() {
|
|
1410
|
-
queryCache.clear();
|
|
1411
|
-
connectionPool.closeAll();
|
|
1412
|
-
}
|
|
1413
1243
|
};
|
|
1414
1244
|
|
|
1415
1245
|
// src/lib/SqliteExecutor.ts
|