@dbcube/core 3.0.14 → 3.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/dist/index.cjs +27 -159
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +0 -4
- package/dist/index.d.ts +0 -4
- package/dist/index.js +27 -159
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -45,14 +45,10 @@ declare class QueryEngine {
|
|
|
45
45
|
private waitForServerReady;
|
|
46
46
|
private isServerResponding;
|
|
47
47
|
private sleep;
|
|
48
|
-
private initializeConnectionPool;
|
|
49
48
|
private getCachedDML;
|
|
50
49
|
private startTcpServer;
|
|
51
50
|
private sendTcpRequest;
|
|
52
51
|
private sendTcpRequestFast;
|
|
53
|
-
private sendUltraFast;
|
|
54
|
-
private createAndPoolConnection;
|
|
55
|
-
private createPooledConnection;
|
|
56
52
|
private sendOnExistingConnection;
|
|
57
53
|
private createPersistentConnection;
|
|
58
54
|
private createProcess;
|
package/dist/index.d.ts
CHANGED
|
@@ -45,14 +45,10 @@ declare class QueryEngine {
|
|
|
45
45
|
private waitForServerReady;
|
|
46
46
|
private isServerResponding;
|
|
47
47
|
private sleep;
|
|
48
|
-
private initializeConnectionPool;
|
|
49
48
|
private getCachedDML;
|
|
50
49
|
private startTcpServer;
|
|
51
50
|
private sendTcpRequest;
|
|
52
51
|
private sendTcpRequestFast;
|
|
53
|
-
private sendUltraFast;
|
|
54
|
-
private createAndPoolConnection;
|
|
55
|
-
private createPooledConnection;
|
|
56
52
|
private sendOnExistingConnection;
|
|
57
53
|
private createPersistentConnection;
|
|
58
54
|
private createProcess;
|
package/dist/index.js
CHANGED
|
@@ -730,39 +730,9 @@ import * as net from "net";
|
|
|
730
730
|
import { spawn as spawn2 } from "child_process";
|
|
731
731
|
var globalTcpServers = /* @__PURE__ */ new Map();
|
|
732
732
|
var globalTcpConnections = /* @__PURE__ */ new Map();
|
|
733
|
-
var
|
|
734
|
-
var
|
|
735
|
-
var
|
|
736
|
-
cache = /* @__PURE__ */ new Map();
|
|
737
|
-
maxSize;
|
|
738
|
-
constructor(maxSize) {
|
|
739
|
-
this.maxSize = maxSize;
|
|
740
|
-
}
|
|
741
|
-
get(key) {
|
|
742
|
-
if (this.cache.has(key)) {
|
|
743
|
-
const value = this.cache.get(key);
|
|
744
|
-
this.cache.delete(key);
|
|
745
|
-
this.cache.set(key, value);
|
|
746
|
-
return value;
|
|
747
|
-
}
|
|
748
|
-
return void 0;
|
|
749
|
-
}
|
|
750
|
-
set(key, value) {
|
|
751
|
-
if (this.cache.has(key)) {
|
|
752
|
-
this.cache.delete(key);
|
|
753
|
-
} else if (this.cache.size >= this.maxSize) {
|
|
754
|
-
const firstKey = this.cache.keys().next().value;
|
|
755
|
-
if (firstKey !== void 0) {
|
|
756
|
-
this.cache.delete(firstKey);
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
this.cache.set(key, value);
|
|
760
|
-
}
|
|
761
|
-
size() {
|
|
762
|
-
return this.cache.size;
|
|
763
|
-
}
|
|
764
|
-
};
|
|
765
|
-
var queryCache = new LRUCache(500);
|
|
733
|
+
var queryCache = /* @__PURE__ */ new Map();
|
|
734
|
+
var cacheSize = 0;
|
|
735
|
+
var MAX_CACHE_SIZE = 500;
|
|
766
736
|
var QueryEngine = class {
|
|
767
737
|
name;
|
|
768
738
|
config;
|
|
@@ -881,7 +851,6 @@ var QueryEngine = class {
|
|
|
881
851
|
if (!serverInfo || !serverInfo.process || serverInfo.process.killed) {
|
|
882
852
|
await this.startTcpServer();
|
|
883
853
|
await this.waitForServerReady();
|
|
884
|
-
await this.initializeConnectionPool();
|
|
885
854
|
}
|
|
886
855
|
return this.sendTcpRequestFast(args);
|
|
887
856
|
}
|
|
@@ -917,30 +886,15 @@ var QueryEngine = class {
|
|
|
917
886
|
sleep(ms) {
|
|
918
887
|
return new Promise((resolve5) => setTimeout(resolve5, ms));
|
|
919
888
|
}
|
|
920
|
-
async initializeConnectionPool() {
|
|
921
|
-
console.log("\u{1F3CA} Initializing connection pool...");
|
|
922
|
-
if (!connectionPools.has(this.connectionId)) {
|
|
923
|
-
connectionPools.set(this.connectionId, []);
|
|
924
|
-
}
|
|
925
|
-
const pool = connectionPools.get(this.connectionId);
|
|
926
|
-
for (let i = 0; i < MAX_POOL_SIZE; i++) {
|
|
927
|
-
try {
|
|
928
|
-
const connection = await this.createPooledConnection();
|
|
929
|
-
pool.push(connection);
|
|
930
|
-
console.log(`\u2705 Pool connection ${i + 1}/${MAX_POOL_SIZE} created`);
|
|
931
|
-
} catch (error) {
|
|
932
|
-
console.error(`\u274C Failed to create pool connection ${i + 1}:`, error);
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
console.log(`\u{1F680} Connection pool ready with ${pool.length} connections`);
|
|
936
|
-
}
|
|
937
889
|
getCachedDML(dmlJson) {
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
return cached;
|
|
890
|
+
if (queryCache.has(dmlJson)) {
|
|
891
|
+
return queryCache.get(dmlJson);
|
|
941
892
|
}
|
|
942
893
|
const parsed = JSON.parse(dmlJson);
|
|
943
|
-
|
|
894
|
+
if (cacheSize < MAX_CACHE_SIZE) {
|
|
895
|
+
queryCache.set(dmlJson, parsed);
|
|
896
|
+
cacheSize++;
|
|
897
|
+
}
|
|
944
898
|
return parsed;
|
|
945
899
|
}
|
|
946
900
|
async startTcpServer() {
|
|
@@ -964,6 +918,7 @@ var QueryEngine = class {
|
|
|
964
918
|
if (output.includes("TCP Server listening on")) {
|
|
965
919
|
if (!started) {
|
|
966
920
|
started = true;
|
|
921
|
+
clearTimeout(timeout);
|
|
967
922
|
globalTcpServers.set(this.connectionId, {
|
|
968
923
|
port: this.tcpPort,
|
|
969
924
|
process: serverProcess
|
|
@@ -980,6 +935,7 @@ var QueryEngine = class {
|
|
|
980
935
|
});
|
|
981
936
|
serverProcess.on("error", (error) => {
|
|
982
937
|
if (!started) {
|
|
938
|
+
clearTimeout(timeout);
|
|
983
939
|
reject(error);
|
|
984
940
|
}
|
|
985
941
|
});
|
|
@@ -1003,6 +959,7 @@ var QueryEngine = class {
|
|
|
1003
959
|
if (!dmlJson) {
|
|
1004
960
|
if (!isResolved) {
|
|
1005
961
|
isResolved = true;
|
|
962
|
+
clearTimeout(timeout);
|
|
1006
963
|
client.destroy();
|
|
1007
964
|
reject(new Error("No DML found in arguments"));
|
|
1008
965
|
}
|
|
@@ -1019,6 +976,7 @@ var QueryEngine = class {
|
|
|
1019
976
|
const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
|
|
1020
977
|
if (match && !isResolved) {
|
|
1021
978
|
isResolved = true;
|
|
979
|
+
clearTimeout(timeout);
|
|
1022
980
|
client.destroy();
|
|
1023
981
|
try {
|
|
1024
982
|
const response = JSON.parse(match[1]);
|
|
@@ -1035,11 +993,13 @@ var QueryEngine = class {
|
|
|
1035
993
|
client.on("error", (error) => {
|
|
1036
994
|
if (!isResolved) {
|
|
1037
995
|
isResolved = true;
|
|
996
|
+
clearTimeout(timeout);
|
|
1038
997
|
reject(error);
|
|
1039
998
|
}
|
|
1040
999
|
});
|
|
1041
1000
|
client.on("close", () => {
|
|
1042
1001
|
if (!isResolved) {
|
|
1002
|
+
clearTimeout(timeout);
|
|
1043
1003
|
if (!responseBuffer.includes("PROCESS_RESPONSE:")) {
|
|
1044
1004
|
reject(new Error("Connection closed without valid response"));
|
|
1045
1005
|
}
|
|
@@ -1048,24 +1008,18 @@ var QueryEngine = class {
|
|
|
1048
1008
|
});
|
|
1049
1009
|
}
|
|
1050
1010
|
async sendTcpRequestFast(args) {
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
}
|
|
1054
|
-
const pool = connectionPools.get(this.connectionId);
|
|
1055
|
-
if (pool.length > 0) {
|
|
1056
|
-
const connection = pool.pop();
|
|
1011
|
+
const existingConnection = globalTcpConnections.get(this.connectionId);
|
|
1012
|
+
if (existingConnection && existingConnection.readyState === "open") {
|
|
1057
1013
|
try {
|
|
1058
|
-
|
|
1059
|
-
pool.push(connection);
|
|
1060
|
-
return result;
|
|
1014
|
+
return await this.sendOnExistingConnection(existingConnection, args);
|
|
1061
1015
|
} catch (error) {
|
|
1062
|
-
|
|
1063
|
-
|
|
1016
|
+
globalTcpConnections.delete(this.connectionId);
|
|
1017
|
+
existingConnection.destroy();
|
|
1064
1018
|
}
|
|
1065
1019
|
}
|
|
1066
|
-
return await this.
|
|
1020
|
+
return await this.createPersistentConnection(args);
|
|
1067
1021
|
}
|
|
1068
|
-
async
|
|
1022
|
+
async sendOnExistingConnection(connection, args) {
|
|
1069
1023
|
return new Promise((resolve5, reject) => {
|
|
1070
1024
|
const dmlIndex = args.findIndex((arg) => arg === "--dml");
|
|
1071
1025
|
const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
|
|
@@ -1075,96 +1029,20 @@ var QueryEngine = class {
|
|
|
1075
1029
|
}
|
|
1076
1030
|
let responseBuffer = "";
|
|
1077
1031
|
let isResolved = false;
|
|
1078
|
-
const
|
|
1079
|
-
responseBuffer += data.toString();
|
|
1080
|
-
const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
|
|
1081
|
-
if (match && !isResolved) {
|
|
1082
|
-
isResolved = true;
|
|
1083
|
-
connection.off("data", onData);
|
|
1084
|
-
connection.off("error", onError);
|
|
1085
|
-
connection.off("close", onClose);
|
|
1086
|
-
try {
|
|
1087
|
-
const response = JSON.parse(match[1]);
|
|
1088
|
-
resolve5({
|
|
1089
|
-
status: response.status,
|
|
1090
|
-
message: response.message,
|
|
1091
|
-
data: response.data
|
|
1092
|
-
});
|
|
1093
|
-
} catch (error) {
|
|
1094
|
-
reject(new Error("Failed to parse TCP response"));
|
|
1095
|
-
}
|
|
1096
|
-
}
|
|
1097
|
-
};
|
|
1098
|
-
const onError = (error) => {
|
|
1099
|
-
if (!isResolved) {
|
|
1100
|
-
isResolved = true;
|
|
1101
|
-
connection.off("data", onData);
|
|
1102
|
-
connection.off("error", onError);
|
|
1103
|
-
connection.off("close", onClose);
|
|
1104
|
-
reject(error);
|
|
1105
|
-
}
|
|
1106
|
-
};
|
|
1107
|
-
const onClose = () => {
|
|
1032
|
+
const timeout = setTimeout(() => {
|
|
1108
1033
|
if (!isResolved) {
|
|
1109
1034
|
isResolved = true;
|
|
1110
|
-
|
|
1111
|
-
connection.off("error", onError);
|
|
1112
|
-
connection.off("close", onClose);
|
|
1113
|
-
connection.off("close", onClose);
|
|
1114
|
-
reject(new Error("Connection closed unexpectedly"));
|
|
1035
|
+
reject(new Error("TCP request timeout"));
|
|
1115
1036
|
}
|
|
1116
|
-
};
|
|
1117
|
-
connection.on("data", onData);
|
|
1118
|
-
connection.on("error", onError);
|
|
1119
|
-
connection.on("close", onClose);
|
|
1120
|
-
const command = `{"action":"execute","dml":${dmlJson}}`;
|
|
1121
|
-
connection.write(command);
|
|
1122
|
-
});
|
|
1123
|
-
}
|
|
1124
|
-
async createAndPoolConnection(args) {
|
|
1125
|
-
const connection = await this.createPooledConnection();
|
|
1126
|
-
if (!connectionPools.has(this.connectionId)) {
|
|
1127
|
-
connectionPools.set(this.connectionId, []);
|
|
1128
|
-
}
|
|
1129
|
-
const result = await this.sendUltraFast(connection, args);
|
|
1130
|
-
const pool = connectionPools.get(this.connectionId);
|
|
1131
|
-
if (pool.length < MAX_POOL_SIZE) {
|
|
1132
|
-
pool.push(connection);
|
|
1133
|
-
} else {
|
|
1134
|
-
connection.destroy();
|
|
1135
|
-
}
|
|
1136
|
-
return result;
|
|
1137
|
-
}
|
|
1138
|
-
async createPooledConnection() {
|
|
1139
|
-
return new Promise((resolve5, reject) => {
|
|
1140
|
-
const client = new net.Socket();
|
|
1141
|
-
client.setNoDelay(true);
|
|
1142
|
-
client.setKeepAlive(true, 0);
|
|
1143
|
-
client.connect(this.tcpPort, "127.0.0.1", () => {
|
|
1144
|
-
resolve5(client);
|
|
1145
|
-
});
|
|
1146
|
-
client.on("error", reject);
|
|
1147
|
-
});
|
|
1148
|
-
}
|
|
1149
|
-
async sendOnExistingConnection(connection, args) {
|
|
1150
|
-
return new Promise((resolve5, reject) => {
|
|
1151
|
-
const dmlIndex = args.findIndex((arg) => arg === "--dml");
|
|
1152
|
-
const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
|
|
1153
|
-
if (!dmlJson) {
|
|
1154
|
-
reject(new Error("No DML found in arguments"));
|
|
1155
|
-
return;
|
|
1156
|
-
}
|
|
1157
|
-
const dmlParsed = this.getCachedDML(dmlJson);
|
|
1158
|
-
let responseBuffer = "";
|
|
1159
|
-
let isResolved = false;
|
|
1037
|
+
}, 100);
|
|
1160
1038
|
const onData = (data) => {
|
|
1161
1039
|
responseBuffer += data.toString();
|
|
1162
1040
|
const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
|
|
1163
1041
|
if (match && !isResolved) {
|
|
1164
1042
|
isResolved = true;
|
|
1043
|
+
clearTimeout(timeout);
|
|
1165
1044
|
connection.off("data", onData);
|
|
1166
1045
|
connection.off("error", onError);
|
|
1167
|
-
connection.off("close", onClose);
|
|
1168
1046
|
try {
|
|
1169
1047
|
const response = JSON.parse(match[1]);
|
|
1170
1048
|
resolve5({
|
|
@@ -1180,24 +1058,14 @@ var QueryEngine = class {
|
|
|
1180
1058
|
const onError = (error) => {
|
|
1181
1059
|
if (!isResolved) {
|
|
1182
1060
|
isResolved = true;
|
|
1061
|
+
clearTimeout(timeout);
|
|
1183
1062
|
connection.off("data", onData);
|
|
1184
1063
|
connection.off("error", onError);
|
|
1185
|
-
connection.off("close", onClose);
|
|
1186
1064
|
reject(error);
|
|
1187
1065
|
}
|
|
1188
1066
|
};
|
|
1189
|
-
const onClose = () => {
|
|
1190
|
-
if (!isResolved) {
|
|
1191
|
-
isResolved = true;
|
|
1192
|
-
connection.off("data", onData);
|
|
1193
|
-
connection.off("error", onError);
|
|
1194
|
-
connection.off("close", onClose);
|
|
1195
|
-
reject(new Error("Connection closed unexpectedly"));
|
|
1196
|
-
}
|
|
1197
|
-
};
|
|
1198
1067
|
connection.on("data", onData);
|
|
1199
1068
|
connection.on("error", onError);
|
|
1200
|
-
connection.on("close", onClose);
|
|
1201
1069
|
const command = {
|
|
1202
1070
|
action: "execute",
|
|
1203
1071
|
dml: dmlJson
|