@dbcube/core 4.1.12 → 4.1.14

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.d.mts CHANGED
@@ -47,10 +47,10 @@ declare class QueryEngine {
47
47
  private sleep;
48
48
  private getCachedDML;
49
49
  private startTcpServer;
50
- private sendTcpRequest;
51
50
  private sendTcpRequestFast;
52
- private sendOnExistingConnection;
53
- private createPersistentConnection;
51
+ private processQueue;
52
+ private createNewConnection;
53
+ private executeOnConnection;
54
54
  private createProcess;
55
55
  disconnect(): Promise<ResponseEngine>;
56
56
  }
package/dist/index.d.ts CHANGED
@@ -47,10 +47,10 @@ declare class QueryEngine {
47
47
  private sleep;
48
48
  private getCachedDML;
49
49
  private startTcpServer;
50
- private sendTcpRequest;
51
50
  private sendTcpRequestFast;
52
- private sendOnExistingConnection;
53
- private createPersistentConnection;
51
+ private processQueue;
52
+ private createNewConnection;
53
+ private executeOnConnection;
54
54
  private createProcess;
55
55
  disconnect(): Promise<ResponseEngine>;
56
56
  }
package/dist/index.js CHANGED
@@ -869,6 +869,8 @@ import * as net from "net";
869
869
  import { spawn as spawn2 } from "child_process";
870
870
  var globalTcpServers = /* @__PURE__ */ new Map();
871
871
  var globalTcpConnections = /* @__PURE__ */ new Map();
872
+ var connectionQueues = /* @__PURE__ */ new Map();
873
+ var connectionProcessing = /* @__PURE__ */ new Map();
872
874
  var queryCache = /* @__PURE__ */ new Map();
873
875
  var cacheSize = 0;
874
876
  var MAX_CACHE_SIZE = 500;
@@ -885,7 +887,7 @@ var QueryEngine = class {
885
887
  this.config = this.setConfig(name);
886
888
  this.arguments = this.setArguments();
887
889
  this.timeout = timeout;
888
- this.connectionId = `${name}_${this.config.type}_${this.config.config.DATABASE}_${this.config.config.HOST || "localhost"}`;
890
+ this.connectionId = `${name}_query_engine_${this.config.type}_${this.config.config.DATABASE}_${this.config.config.HOST || "localhost"}`;
889
891
  this.tcpPort = this.generatePort();
890
892
  }
891
893
  generatePort() {
@@ -983,7 +985,8 @@ var QueryEngine = class {
983
985
  async run(binary, args) {
984
986
  const actionIndex = args.findIndex((arg) => arg === "--action");
985
987
  const isExecuteAction = actionIndex !== -1 && args[actionIndex + 1] === "execute";
986
- if (isExecuteAction) {
988
+ const isQueryEngine = binary === "query_engine";
989
+ if (isQueryEngine && isExecuteAction) {
987
990
  return this.executeWithTcpServer(args);
988
991
  } else {
989
992
  return this.createProcess(binary, args);
@@ -1000,22 +1003,26 @@ var QueryEngine = class {
1000
1003
  async waitForServerReady() {
1001
1004
  const maxRetries = 10;
1002
1005
  const retryDelay = 500;
1006
+ const serverInfo = globalTcpServers.get(this.connectionId);
1007
+ if (!serverInfo) {
1008
+ throw new Error("Server not found");
1009
+ }
1003
1010
  for (let i = 0; i < maxRetries; i++) {
1004
- if (await this.isServerResponding()) {
1011
+ if (await this.isServerResponding(serverInfo.port)) {
1005
1012
  return;
1006
1013
  }
1007
1014
  await this.sleep(retryDelay);
1008
1015
  }
1009
1016
  throw new Error("TCP server failed to become ready within timeout");
1010
1017
  }
1011
- async isServerResponding() {
1018
+ async isServerResponding(port) {
1012
1019
  return new Promise((resolve5) => {
1013
1020
  const client = new net.Socket();
1014
1021
  const timeout = setTimeout(() => {
1015
1022
  client.destroy();
1016
1023
  resolve5(false);
1017
1024
  }, 1e3);
1018
- client.connect(this.tcpPort, "127.0.0.1", () => {
1025
+ client.connect(port, "127.0.0.1", () => {
1019
1026
  clearTimeout(timeout);
1020
1027
  client.destroy();
1021
1028
  resolve5(true);
@@ -1071,7 +1078,7 @@ var QueryEngine = class {
1071
1078
  }
1072
1079
  });
1073
1080
  serverProcess.stderr.on("data", (data) => {
1074
- console.error(`[TCP Server Error] ${data.toString().trim()}`);
1081
+ console.error(`[query_engine TCP Server Error] ${data.toString().trim()}`);
1075
1082
  });
1076
1083
  serverProcess.on("close", (code) => {
1077
1084
  globalTcpServers.delete(this.connectionId);
@@ -1084,92 +1091,88 @@ var QueryEngine = class {
1084
1091
  });
1085
1092
  });
1086
1093
  }
1087
- async sendTcpRequest(args) {
1094
+ async sendTcpRequestFast(args) {
1088
1095
  return new Promise((resolve5, reject) => {
1089
- const client = new net.Socket();
1090
- let responseBuffer = "";
1091
- let isResolved = false;
1092
- const timeout = setTimeout(() => {
1093
- if (!isResolved) {
1094
- isResolved = true;
1095
- client.destroy();
1096
- reject(new Error("TCP request timeout"));
1097
- }
1098
- }, this.timeout * 2);
1099
- client.connect(this.tcpPort, "127.0.0.1", () => {
1100
- const dmlIndex = args.findIndex((arg) => arg === "--dml");
1101
- const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1102
- if (!dmlJson) {
1103
- if (!isResolved) {
1104
- isResolved = true;
1105
- clearTimeout(timeout);
1106
- client.destroy();
1107
- reject(new Error("No DML found in arguments"));
1108
- }
1109
- return;
1110
- }
1111
- const command = {
1112
- action: "execute",
1113
- dml: dmlJson
1114
- };
1115
- client.write(JSON.stringify(command));
1116
- });
1117
- client.on("data", (data) => {
1118
- responseBuffer += data.toString();
1119
- const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1120
- if (match && !isResolved) {
1121
- isResolved = true;
1122
- clearTimeout(timeout);
1123
- client.destroy();
1124
- try {
1125
- const response = JSON.parse(match[1]);
1126
- resolve5({
1127
- status: response.status,
1128
- message: response.message,
1129
- data: response.data
1130
- });
1131
- } catch (error) {
1132
- reject(new Error("Failed to parse TCP response"));
1096
+ let queue = connectionQueues.get(this.connectionId);
1097
+ if (!queue) {
1098
+ queue = [];
1099
+ connectionQueues.set(this.connectionId, queue);
1100
+ }
1101
+ queue.push({ args, resolve: resolve5, reject });
1102
+ this.processQueue();
1103
+ });
1104
+ }
1105
+ async processQueue() {
1106
+ if (connectionProcessing.get(this.connectionId)) {
1107
+ return;
1108
+ }
1109
+ const queue = connectionQueues.get(this.connectionId);
1110
+ if (!queue || queue.length === 0) {
1111
+ return;
1112
+ }
1113
+ connectionProcessing.set(this.connectionId, true);
1114
+ try {
1115
+ const serverInfo = globalTcpServers.get(this.connectionId);
1116
+ if (!serverInfo) {
1117
+ throw new Error("Server not initialized");
1118
+ }
1119
+ while (queue.length > 0) {
1120
+ const request = queue.shift();
1121
+ if (!request) break;
1122
+ try {
1123
+ let connection = globalTcpConnections.get(this.connectionId);
1124
+ if (!connection || connection.destroyed || connection.readyState !== "open") {
1125
+ connection = await this.createNewConnection(serverInfo.port);
1126
+ globalTcpConnections.set(this.connectionId, connection);
1133
1127
  }
1128
+ const result = await this.executeOnConnection(connection, request.args);
1129
+ request.resolve(result);
1130
+ } catch (error) {
1131
+ globalTcpConnections.delete(this.connectionId);
1132
+ request.reject(error);
1134
1133
  }
1134
+ }
1135
+ } finally {
1136
+ connectionProcessing.set(this.connectionId, false);
1137
+ }
1138
+ }
1139
+ async createNewConnection(port) {
1140
+ return new Promise((resolve5, reject) => {
1141
+ const client = new net.Socket();
1142
+ client.setNoDelay(true);
1143
+ client.setKeepAlive(true, 6e4);
1144
+ const timeout = setTimeout(() => {
1145
+ client.destroy();
1146
+ reject(new Error("Connection timeout"));
1147
+ }, 5e3);
1148
+ client.connect(port, "127.0.0.1", () => {
1149
+ clearTimeout(timeout);
1150
+ resolve5(client);
1135
1151
  });
1136
1152
  client.on("error", (error) => {
1137
- if (!isResolved) {
1138
- isResolved = true;
1139
- clearTimeout(timeout);
1140
- reject(error);
1141
- }
1142
- });
1143
- client.on("close", () => {
1144
- if (!isResolved) {
1145
- clearTimeout(timeout);
1146
- if (!responseBuffer.includes("PROCESS_RESPONSE:")) {
1147
- reject(new Error("Connection closed without valid response"));
1148
- }
1149
- }
1153
+ clearTimeout(timeout);
1154
+ reject(error);
1150
1155
  });
1151
1156
  });
1152
1157
  }
1153
- async sendTcpRequestFast(args) {
1154
- return await this.sendTcpRequest(args);
1155
- }
1156
- async sendOnExistingConnection(connection, args) {
1158
+ async executeOnConnection(connection, args) {
1157
1159
  return new Promise((resolve5, reject) => {
1158
- const dmlIndex = args.findIndex((arg) => arg === "--dml");
1159
- const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1160
- if (!dmlJson) {
1161
- reject(new Error("No DML found in arguments"));
1162
- return;
1160
+ const command = {};
1161
+ for (let i = 0; i < args.length; i += 2) {
1162
+ if (args[i].startsWith("--")) {
1163
+ const key = args[i].substring(2);
1164
+ const value = args[i + 1];
1165
+ command[key] = value;
1166
+ }
1163
1167
  }
1164
1168
  let responseBuffer = "";
1165
1169
  let isResolved = false;
1166
1170
  const timeout = setTimeout(() => {
1167
1171
  if (!isResolved) {
1168
1172
  isResolved = true;
1169
- connection.off("data", onData);
1170
- connection.off("error", onError);
1171
- globalTcpConnections.delete(this.connectionId);
1172
- reject(new Error("TCP request timeout"));
1173
+ connection.removeListener("data", onData);
1174
+ connection.removeListener("error", onError);
1175
+ reject(new Error("Request timeout"));
1173
1176
  }
1174
1177
  }, this.timeout);
1175
1178
  const onData = (data) => {
@@ -1178,8 +1181,8 @@ var QueryEngine = class {
1178
1181
  if (match && !isResolved) {
1179
1182
  isResolved = true;
1180
1183
  clearTimeout(timeout);
1181
- connection.off("data", onData);
1182
- connection.off("error", onError);
1184
+ connection.removeListener("data", onData);
1185
+ connection.removeListener("error", onError);
1183
1186
  try {
1184
1187
  const response = JSON.parse(match[1]);
1185
1188
  resolve5({
@@ -1188,7 +1191,7 @@ var QueryEngine = class {
1188
1191
  data: response.data
1189
1192
  });
1190
1193
  } catch (error) {
1191
- reject(new Error("Failed to parse TCP response"));
1194
+ reject(new Error("Failed to parse response"));
1192
1195
  }
1193
1196
  }
1194
1197
  };
@@ -1196,118 +1199,16 @@ var QueryEngine = class {
1196
1199
  if (!isResolved) {
1197
1200
  isResolved = true;
1198
1201
  clearTimeout(timeout);
1199
- connection.off("data", onData);
1200
- connection.off("error", onError);
1202
+ connection.removeListener("data", onData);
1203
+ connection.removeListener("error", onError);
1201
1204
  reject(error);
1202
1205
  }
1203
1206
  };
1204
1207
  connection.on("data", onData);
1205
1208
  connection.on("error", onError);
1206
- const command = {
1207
- action: "execute",
1208
- dml: dmlJson
1209
- };
1210
1209
  connection.write(JSON.stringify(command));
1211
1210
  });
1212
1211
  }
1213
- async createPersistentConnection(args) {
1214
- return new Promise((resolve5, reject) => {
1215
- let client = globalTcpConnections.get(this.connectionId);
1216
- let isNewConnection = false;
1217
- if (!client || client.destroyed) {
1218
- client = new net.Socket();
1219
- isNewConnection = true;
1220
- client.setNoDelay(true);
1221
- client.setKeepAlive(true, 6e4);
1222
- client.setMaxListeners(20);
1223
- }
1224
- let responseBuffer = "";
1225
- let isResolved = false;
1226
- const timeout = setTimeout(() => {
1227
- if (!isResolved) {
1228
- isResolved = true;
1229
- client.removeListener("data", dataHandler);
1230
- globalTcpConnections.delete(this.connectionId);
1231
- reject(new Error("TCP connection timeout"));
1232
- }
1233
- }, this.timeout);
1234
- const executeQuery = () => {
1235
- const dmlIndex = args.findIndex((arg) => arg === "--dml");
1236
- const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1237
- if (!dmlJson) {
1238
- clearTimeout(timeout);
1239
- if (!isResolved) {
1240
- isResolved = true;
1241
- reject(new Error("No DML found in arguments"));
1242
- }
1243
- return;
1244
- }
1245
- const command = {
1246
- action: "execute",
1247
- dml: dmlJson
1248
- };
1249
- const commandStr = JSON.stringify(command);
1250
- const canWrite = client.write(commandStr);
1251
- if (!canWrite) {
1252
- client.once("drain", () => {
1253
- });
1254
- }
1255
- };
1256
- const dataHandler = (data) => {
1257
- responseBuffer += data.toString();
1258
- const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1259
- if (match && !isResolved) {
1260
- isResolved = true;
1261
- clearTimeout(timeout);
1262
- try {
1263
- const response = JSON.parse(match[1]);
1264
- responseBuffer = "";
1265
- client.removeListener("data", dataHandler);
1266
- resolve5({
1267
- status: response.status,
1268
- message: response.message,
1269
- data: response.data
1270
- });
1271
- } catch (error) {
1272
- client.removeListener("data", dataHandler);
1273
- reject(new Error("Failed to parse TCP response"));
1274
- }
1275
- }
1276
- };
1277
- const errorHandler = (error) => {
1278
- if (!isResolved) {
1279
- isResolved = true;
1280
- clearTimeout(timeout);
1281
- globalTcpConnections.delete(this.connectionId);
1282
- client.removeListener("data", dataHandler);
1283
- reject(error);
1284
- }
1285
- };
1286
- const closeHandler = () => {
1287
- globalTcpConnections.delete(this.connectionId);
1288
- if (!isResolved) {
1289
- isResolved = true;
1290
- clearTimeout(timeout);
1291
- client.removeListener("data", dataHandler);
1292
- reject(new Error("Connection closed unexpectedly"));
1293
- }
1294
- };
1295
- if (isNewConnection) {
1296
- client.connect(this.tcpPort, "127.0.0.1", () => {
1297
- clearTimeout(timeout);
1298
- globalTcpConnections.set(this.connectionId, client);
1299
- client.on("error", errorHandler);
1300
- client.on("close", closeHandler);
1301
- client.on("data", dataHandler);
1302
- executeQuery();
1303
- });
1304
- } else {
1305
- clearTimeout(timeout);
1306
- client.on("data", dataHandler);
1307
- executeQuery();
1308
- }
1309
- });
1310
- }
1311
1212
  async createProcess(binary, args) {
1312
1213
  await this.initializeBinary();
1313
1214
  if (!this.binary) {