@dbcube/core 4.1.13 → 4.1.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.d.mts CHANGED
@@ -47,13 +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
51
  private processQueue;
53
52
  private createNewConnection;
54
53
  private executeOnConnection;
55
- private sendOnExistingConnection;
56
- private createPersistentConnection;
57
54
  private createProcess;
58
55
  disconnect(): Promise<ResponseEngine>;
59
56
  }
package/dist/index.d.ts CHANGED
@@ -47,13 +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
51
  private processQueue;
53
52
  private createNewConnection;
54
53
  private executeOnConnection;
55
- private sendOnExistingConnection;
56
- private createPersistentConnection;
57
54
  private createProcess;
58
55
  disconnect(): Promise<ResponseEngine>;
59
56
  }
package/dist/index.js CHANGED
@@ -887,7 +887,7 @@ var QueryEngine = class {
887
887
  this.config = this.setConfig(name);
888
888
  this.arguments = this.setArguments();
889
889
  this.timeout = timeout;
890
- 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"}`;
891
891
  this.tcpPort = this.generatePort();
892
892
  }
893
893
  generatePort() {
@@ -983,30 +983,27 @@ var QueryEngine = class {
983
983
  return this.config;
984
984
  }
985
985
  async run(binary, args) {
986
- const isQueryEngine = binary === "query_engine";
987
- const isSchemaEngine = binary === "schema_engine";
988
986
  const actionIndex = args.findIndex((arg) => arg === "--action");
989
- const action = actionIndex !== -1 ? args[actionIndex + 1] : "";
990
- const isExecuteAction = action === "execute";
991
- if (isQueryEngine && isExecuteAction || isSchemaEngine) {
992
- return this.executeWithTcpServer(args, binary);
987
+ const isExecuteAction = actionIndex !== -1 && args[actionIndex + 1] === "execute";
988
+ const isQueryEngine = binary === "query_engine";
989
+ if (isQueryEngine && isExecuteAction) {
990
+ return this.executeWithTcpServer(args);
993
991
  } else {
994
992
  return this.createProcess(binary, args);
995
993
  }
996
994
  }
997
- async executeWithTcpServer(args, binary = "query_engine") {
998
- const serverConnectionId = `${this.connectionId}_${binary}`;
999
- const serverInfo = globalTcpServers.get(serverConnectionId);
995
+ async executeWithTcpServer(args) {
996
+ const serverInfo = globalTcpServers.get(this.connectionId);
1000
997
  if (!serverInfo || !serverInfo.process || serverInfo.process.killed) {
1001
- await this.startTcpServer(binary, serverConnectionId);
1002
- await this.waitForServerReady(serverConnectionId);
998
+ await this.startTcpServer();
999
+ await this.waitForServerReady();
1003
1000
  }
1004
- return this.sendTcpRequestFast(args, serverConnectionId);
1001
+ return this.sendTcpRequestFast(args);
1005
1002
  }
1006
- async waitForServerReady(serverConnectionId) {
1003
+ async waitForServerReady() {
1007
1004
  const maxRetries = 10;
1008
1005
  const retryDelay = 500;
1009
- const serverInfo = globalTcpServers.get(serverConnectionId);
1006
+ const serverInfo = globalTcpServers.get(this.connectionId);
1010
1007
  if (!serverInfo) {
1011
1008
  throw new Error("Server not found");
1012
1009
  }
@@ -1050,16 +1047,15 @@ var QueryEngine = class {
1050
1047
  }
1051
1048
  return parsed;
1052
1049
  }
1053
- async startTcpServer(binary, serverConnectionId) {
1050
+ async startTcpServer() {
1054
1051
  await this.initializeBinary();
1055
1052
  if (!this.binary) {
1056
1053
  throw new Error("Binary not initialized");
1057
1054
  }
1058
- const basePort = binary === "schema_engine" ? 9900 : 9944;
1059
- const tcpPort = await this.findAvailablePort(basePort);
1055
+ this.tcpPort = await this.findAvailablePort(this.tcpPort);
1060
1056
  return new Promise((resolve5, reject) => {
1061
- const serverArgs = [...this.arguments, "--action", "server", "--tcp-port", tcpPort.toString()];
1062
- const serverProcess = spawn2(this.binary[binary], serverArgs);
1057
+ const serverArgs = [...this.arguments, "--action", "server", "--tcp-port", this.tcpPort.toString()];
1058
+ const serverProcess = spawn2(this.binary["query_engine"], serverArgs);
1063
1059
  let started = false;
1064
1060
  const timeout = setTimeout(() => {
1065
1061
  if (!started) {
@@ -1073,8 +1069,8 @@ var QueryEngine = class {
1073
1069
  if (!started) {
1074
1070
  started = true;
1075
1071
  clearTimeout(timeout);
1076
- globalTcpServers.set(serverConnectionId, {
1077
- port: tcpPort,
1072
+ globalTcpServers.set(this.connectionId, {
1073
+ port: this.tcpPort,
1078
1074
  process: serverProcess
1079
1075
  });
1080
1076
  resolve5();
@@ -1082,10 +1078,10 @@ var QueryEngine = class {
1082
1078
  }
1083
1079
  });
1084
1080
  serverProcess.stderr.on("data", (data) => {
1085
- console.error(`[${binary} TCP Server Error] ${data.toString().trim()}`);
1081
+ console.error(`[query_engine TCP Server Error] ${data.toString().trim()}`);
1086
1082
  });
1087
1083
  serverProcess.on("close", (code) => {
1088
- globalTcpServers.delete(serverConnectionId);
1084
+ globalTcpServers.delete(this.connectionId);
1089
1085
  });
1090
1086
  serverProcess.on("error", (error) => {
1091
1087
  if (!started) {
@@ -1095,94 +1091,28 @@ var QueryEngine = class {
1095
1091
  });
1096
1092
  });
1097
1093
  }
1098
- async sendTcpRequest(args) {
1094
+ async sendTcpRequestFast(args) {
1099
1095
  return new Promise((resolve5, reject) => {
1100
- const client = new net.Socket();
1101
- let responseBuffer = "";
1102
- let isResolved = false;
1103
- const timeout = setTimeout(() => {
1104
- if (!isResolved) {
1105
- isResolved = true;
1106
- client.destroy();
1107
- reject(new Error("TCP request timeout"));
1108
- }
1109
- }, this.timeout * 2);
1110
- client.connect(this.tcpPort, "127.0.0.1", () => {
1111
- const dmlIndex = args.findIndex((arg) => arg === "--dml");
1112
- const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1113
- if (!dmlJson) {
1114
- if (!isResolved) {
1115
- isResolved = true;
1116
- clearTimeout(timeout);
1117
- client.destroy();
1118
- reject(new Error("No DML found in arguments"));
1119
- }
1120
- return;
1121
- }
1122
- const command = {
1123
- action: "execute",
1124
- dml: dmlJson
1125
- };
1126
- client.write(JSON.stringify(command));
1127
- });
1128
- client.on("data", (data) => {
1129
- responseBuffer += data.toString();
1130
- const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1131
- if (match && !isResolved) {
1132
- isResolved = true;
1133
- clearTimeout(timeout);
1134
- client.destroy();
1135
- try {
1136
- const response = JSON.parse(match[1]);
1137
- resolve5({
1138
- status: response.status,
1139
- message: response.message,
1140
- data: response.data
1141
- });
1142
- } catch (error) {
1143
- reject(new Error("Failed to parse TCP response"));
1144
- }
1145
- }
1146
- });
1147
- client.on("error", (error) => {
1148
- if (!isResolved) {
1149
- isResolved = true;
1150
- clearTimeout(timeout);
1151
- reject(error);
1152
- }
1153
- });
1154
- client.on("close", () => {
1155
- if (!isResolved) {
1156
- clearTimeout(timeout);
1157
- if (!responseBuffer.includes("PROCESS_RESPONSE:")) {
1158
- reject(new Error("Connection closed without valid response"));
1159
- }
1160
- }
1161
- });
1162
- });
1163
- }
1164
- async sendTcpRequestFast(args, serverConnectionId) {
1165
- return new Promise((resolve5, reject) => {
1166
- let queue = connectionQueues.get(serverConnectionId);
1096
+ let queue = connectionQueues.get(this.connectionId);
1167
1097
  if (!queue) {
1168
1098
  queue = [];
1169
- connectionQueues.set(serverConnectionId, queue);
1099
+ connectionQueues.set(this.connectionId, queue);
1170
1100
  }
1171
1101
  queue.push({ args, resolve: resolve5, reject });
1172
- this.processQueue(serverConnectionId);
1102
+ this.processQueue();
1173
1103
  });
1174
1104
  }
1175
- async processQueue(serverConnectionId) {
1176
- if (connectionProcessing.get(serverConnectionId)) {
1105
+ async processQueue() {
1106
+ if (connectionProcessing.get(this.connectionId)) {
1177
1107
  return;
1178
1108
  }
1179
- const queue = connectionQueues.get(serverConnectionId);
1109
+ const queue = connectionQueues.get(this.connectionId);
1180
1110
  if (!queue || queue.length === 0) {
1181
1111
  return;
1182
1112
  }
1183
- connectionProcessing.set(serverConnectionId, true);
1113
+ connectionProcessing.set(this.connectionId, true);
1184
1114
  try {
1185
- const serverInfo = globalTcpServers.get(serverConnectionId);
1115
+ const serverInfo = globalTcpServers.get(this.connectionId);
1186
1116
  if (!serverInfo) {
1187
1117
  throw new Error("Server not initialized");
1188
1118
  }
@@ -1190,20 +1120,20 @@ var QueryEngine = class {
1190
1120
  const request = queue.shift();
1191
1121
  if (!request) break;
1192
1122
  try {
1193
- let connection = globalTcpConnections.get(serverConnectionId);
1123
+ let connection = globalTcpConnections.get(this.connectionId);
1194
1124
  if (!connection || connection.destroyed || connection.readyState !== "open") {
1195
1125
  connection = await this.createNewConnection(serverInfo.port);
1196
- globalTcpConnections.set(serverConnectionId, connection);
1126
+ globalTcpConnections.set(this.connectionId, connection);
1197
1127
  }
1198
1128
  const result = await this.executeOnConnection(connection, request.args);
1199
1129
  request.resolve(result);
1200
1130
  } catch (error) {
1201
- globalTcpConnections.delete(serverConnectionId);
1131
+ globalTcpConnections.delete(this.connectionId);
1202
1132
  request.reject(error);
1203
1133
  }
1204
1134
  }
1205
1135
  } finally {
1206
- connectionProcessing.set(serverConnectionId, false);
1136
+ connectionProcessing.set(this.connectionId, false);
1207
1137
  }
1208
1138
  }
1209
1139
  async createNewConnection(port) {
@@ -1279,161 +1209,6 @@ var QueryEngine = class {
1279
1209
  connection.write(JSON.stringify(command));
1280
1210
  });
1281
1211
  }
1282
- async sendOnExistingConnection(connection, args) {
1283
- return new Promise((resolve5, reject) => {
1284
- const dmlIndex = args.findIndex((arg) => arg === "--dml");
1285
- const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1286
- if (!dmlJson) {
1287
- reject(new Error("No DML found in arguments"));
1288
- return;
1289
- }
1290
- let responseBuffer = "";
1291
- let isResolved = false;
1292
- const timeout = setTimeout(() => {
1293
- if (!isResolved) {
1294
- isResolved = true;
1295
- connection.off("data", onData);
1296
- connection.off("error", onError);
1297
- globalTcpConnections.delete(this.connectionId);
1298
- reject(new Error("TCP request timeout"));
1299
- }
1300
- }, this.timeout);
1301
- const onData = (data) => {
1302
- responseBuffer += data.toString();
1303
- const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1304
- if (match && !isResolved) {
1305
- isResolved = true;
1306
- clearTimeout(timeout);
1307
- connection.off("data", onData);
1308
- connection.off("error", onError);
1309
- try {
1310
- const response = JSON.parse(match[1]);
1311
- resolve5({
1312
- status: response.status,
1313
- message: response.message,
1314
- data: response.data
1315
- });
1316
- } catch (error) {
1317
- reject(new Error("Failed to parse TCP response"));
1318
- }
1319
- }
1320
- };
1321
- const onError = (error) => {
1322
- if (!isResolved) {
1323
- isResolved = true;
1324
- clearTimeout(timeout);
1325
- connection.off("data", onData);
1326
- connection.off("error", onError);
1327
- reject(error);
1328
- }
1329
- };
1330
- connection.on("data", onData);
1331
- connection.on("error", onError);
1332
- const command = {
1333
- action: "execute",
1334
- dml: dmlJson
1335
- };
1336
- connection.write(JSON.stringify(command));
1337
- });
1338
- }
1339
- async createPersistentConnection(args) {
1340
- return new Promise((resolve5, reject) => {
1341
- let client = globalTcpConnections.get(this.connectionId);
1342
- let isNewConnection = false;
1343
- if (!client || client.destroyed) {
1344
- client = new net.Socket();
1345
- isNewConnection = true;
1346
- client.setNoDelay(true);
1347
- client.setKeepAlive(true, 6e4);
1348
- client.setMaxListeners(20);
1349
- }
1350
- let responseBuffer = "";
1351
- let isResolved = false;
1352
- const timeout = setTimeout(() => {
1353
- if (!isResolved) {
1354
- isResolved = true;
1355
- client.removeListener("data", dataHandler);
1356
- globalTcpConnections.delete(this.connectionId);
1357
- reject(new Error("TCP connection timeout"));
1358
- }
1359
- }, this.timeout);
1360
- const executeQuery = () => {
1361
- const dmlIndex = args.findIndex((arg) => arg === "--dml");
1362
- const dmlJson = dmlIndex !== -1 ? args[dmlIndex + 1] : null;
1363
- if (!dmlJson) {
1364
- clearTimeout(timeout);
1365
- if (!isResolved) {
1366
- isResolved = true;
1367
- reject(new Error("No DML found in arguments"));
1368
- }
1369
- return;
1370
- }
1371
- const command = {
1372
- action: "execute",
1373
- dml: dmlJson
1374
- };
1375
- const commandStr = JSON.stringify(command);
1376
- const canWrite = client.write(commandStr);
1377
- if (!canWrite) {
1378
- client.once("drain", () => {
1379
- });
1380
- }
1381
- };
1382
- const dataHandler = (data) => {
1383
- responseBuffer += data.toString();
1384
- const match = responseBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
1385
- if (match && !isResolved) {
1386
- isResolved = true;
1387
- clearTimeout(timeout);
1388
- try {
1389
- const response = JSON.parse(match[1]);
1390
- responseBuffer = "";
1391
- client.removeListener("data", dataHandler);
1392
- resolve5({
1393
- status: response.status,
1394
- message: response.message,
1395
- data: response.data
1396
- });
1397
- } catch (error) {
1398
- client.removeListener("data", dataHandler);
1399
- reject(new Error("Failed to parse TCP response"));
1400
- }
1401
- }
1402
- };
1403
- const errorHandler = (error) => {
1404
- if (!isResolved) {
1405
- isResolved = true;
1406
- clearTimeout(timeout);
1407
- globalTcpConnections.delete(this.connectionId);
1408
- client.removeListener("data", dataHandler);
1409
- reject(error);
1410
- }
1411
- };
1412
- const closeHandler = () => {
1413
- globalTcpConnections.delete(this.connectionId);
1414
- if (!isResolved) {
1415
- isResolved = true;
1416
- clearTimeout(timeout);
1417
- client.removeListener("data", dataHandler);
1418
- reject(new Error("Connection closed unexpectedly"));
1419
- }
1420
- };
1421
- if (isNewConnection) {
1422
- client.connect(this.tcpPort, "127.0.0.1", () => {
1423
- clearTimeout(timeout);
1424
- globalTcpConnections.set(this.connectionId, client);
1425
- client.on("error", errorHandler);
1426
- client.on("close", closeHandler);
1427
- client.on("data", dataHandler);
1428
- executeQuery();
1429
- });
1430
- } else {
1431
- clearTimeout(timeout);
1432
- client.on("data", dataHandler);
1433
- executeQuery();
1434
- }
1435
- });
1436
- }
1437
1212
  async createProcess(binary, args) {
1438
1213
  await this.initializeBinary();
1439
1214
  if (!this.binary) {