@dbcube/core 1.0.52 → 1.0.57

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 CHANGED
@@ -593,17 +593,31 @@ var Config = class {
593
593
  // src/lib/Engine.ts
594
594
  var import_child_process = require("child_process");
595
595
  var import_module = require("module");
596
- var Engine = class {
597
- name;
596
+ var Engine = class _Engine {
597
+ static instances = /* @__PURE__ */ new Map();
598
+ name = "";
598
599
  config;
599
600
  arguments;
600
601
  binary = null;
601
- timeout;
602
+ timeout = 3e4;
603
+ isDaemonConnected = false;
604
+ isConnecting = false;
602
605
  constructor(name, timeout = 3e4) {
606
+ if (_Engine.instances.has(name)) {
607
+ return _Engine.instances.get(name);
608
+ }
603
609
  this.name = name;
604
610
  this.config = this.setConfig(name);
605
611
  this.arguments = this.setArguments();
606
612
  this.timeout = timeout;
613
+ _Engine.instances.set(name, this);
614
+ this.setupProcessExitHandlers();
615
+ }
616
+ static getInstance(name, timeout = 3e4) {
617
+ if (_Engine.instances.has(name)) {
618
+ return _Engine.instances.get(name);
619
+ }
620
+ return new _Engine(name, timeout);
607
621
  }
608
622
  async initializeBinary() {
609
623
  if (!this.binary) {
@@ -675,6 +689,12 @@ var Engine = class {
675
689
  if (!this.binary) {
676
690
  throw new Error("Binary not initialized");
677
691
  }
692
+ const isConnectAction = args.includes("connect");
693
+ const isDisconnectAction = args.includes("disconnect");
694
+ const isExecuteAction = args.includes("execute");
695
+ if (isExecuteAction) {
696
+ await this.ensureDaemonConnected();
697
+ }
678
698
  return new Promise((resolve5, reject) => {
679
699
  const child = (0, import_child_process.spawn)(this.binary[binary], [...this.arguments, ...args]);
680
700
  let stdoutBuffer = "";
@@ -692,6 +712,12 @@ var Engine = class {
692
712
  isResolved = true;
693
713
  clearTimeout(timeoutId);
694
714
  resolve5(response);
715
+ if (isConnectAction && response.status === 200) {
716
+ this.isDaemonConnected = true;
717
+ this.isConnecting = false;
718
+ } else if (isDisconnectAction) {
719
+ this.isDaemonConnected = false;
720
+ }
695
721
  }
696
722
  };
697
723
  child.stdout.on("data", (data) => {
@@ -759,6 +785,67 @@ var Engine = class {
759
785
  child.unref();
760
786
  });
761
787
  }
788
+ async ensureDaemonConnected() {
789
+ if (this.isDaemonConnected) {
790
+ return;
791
+ }
792
+ if (this.isConnecting) {
793
+ while (this.isConnecting) {
794
+ await new Promise((resolve5) => setTimeout(resolve5, 50));
795
+ }
796
+ return;
797
+ }
798
+ this.isConnecting = true;
799
+ try {
800
+ const response = await this.run("query_engine", ["--action", "connect"]);
801
+ if (response.status !== 200) {
802
+ throw new Error(`Failed to connect daemon: ${response.message}`);
803
+ }
804
+ } catch (error) {
805
+ this.isConnecting = false;
806
+ throw error;
807
+ }
808
+ }
809
+ async disconnect() {
810
+ if (this.isDaemonConnected) {
811
+ await this.run("query_engine", ["--action", "disconnect"]);
812
+ }
813
+ }
814
+ setupProcessExitHandlers() {
815
+ const gracefulShutdown = async () => {
816
+ if (this.isDaemonConnected) {
817
+ console.log("\u{1F504} Disconnecting daemon before exit...");
818
+ try {
819
+ await this.disconnect();
820
+ console.log("\u2705 Daemon disconnected successfully");
821
+ } catch (error) {
822
+ console.error("\u274C Error disconnecting daemon:", error);
823
+ }
824
+ }
825
+ };
826
+ process.on("exit", () => {
827
+ });
828
+ process.on("SIGINT", async () => {
829
+ console.log("\n\u{1F6D1} Received SIGINT (Ctrl+C)");
830
+ await gracefulShutdown();
831
+ process.exit(0);
832
+ });
833
+ process.on("SIGTERM", async () => {
834
+ console.log("\u{1F6D1} Received SIGTERM");
835
+ await gracefulShutdown();
836
+ process.exit(0);
837
+ });
838
+ process.on("uncaughtException", async (error) => {
839
+ console.error("\u{1F4A5} Uncaught Exception:", error);
840
+ await gracefulShutdown();
841
+ process.exit(1);
842
+ });
843
+ process.on("unhandledRejection", async (reason, promise) => {
844
+ console.error("\u{1F4A5} Unhandled Rejection at:", promise, "reason:", reason);
845
+ await gracefulShutdown();
846
+ process.exit(1);
847
+ });
848
+ }
762
849
  };
763
850
 
764
851
  // src/lib/SqliteExecutor.ts