@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.d.mts CHANGED
@@ -7,19 +7,26 @@ interface ResponseEngine {
7
7
  }
8
8
 
9
9
  declare class Engine {
10
+ private static instances;
10
11
  private name;
11
12
  private config;
12
13
  private arguments;
13
14
  private binary;
14
15
  private timeout;
16
+ private isDaemonConnected;
17
+ private isConnecting;
15
18
  constructor(name: string, timeout?: number);
19
+ static getInstance(name: string, timeout?: number): Engine;
16
20
  initializeBinary(): Promise<void>;
17
21
  setArguments(): any[];
18
22
  setConfig(name: string): {
19
23
  [x: string]: any;
20
24
  } | null;
21
25
  getConfig(): any;
22
- run(binary: string, args: []): Promise<ResponseEngine>;
26
+ run(binary: string, args: string[]): Promise<ResponseEngine>;
27
+ ensureDaemonConnected(): Promise<void>;
28
+ disconnect(): Promise<void>;
29
+ private setupProcessExitHandlers;
23
30
  }
24
31
 
25
32
  interface SystemInfo {
package/dist/index.d.ts CHANGED
@@ -7,19 +7,26 @@ interface ResponseEngine {
7
7
  }
8
8
 
9
9
  declare class Engine {
10
+ private static instances;
10
11
  private name;
11
12
  private config;
12
13
  private arguments;
13
14
  private binary;
14
15
  private timeout;
16
+ private isDaemonConnected;
17
+ private isConnecting;
15
18
  constructor(name: string, timeout?: number);
19
+ static getInstance(name: string, timeout?: number): Engine;
16
20
  initializeBinary(): Promise<void>;
17
21
  setArguments(): any[];
18
22
  setConfig(name: string): {
19
23
  [x: string]: any;
20
24
  } | null;
21
25
  getConfig(): any;
22
- run(binary: string, args: []): Promise<ResponseEngine>;
26
+ run(binary: string, args: string[]): Promise<ResponseEngine>;
27
+ ensureDaemonConnected(): Promise<void>;
28
+ disconnect(): Promise<void>;
29
+ private setupProcessExitHandlers;
23
30
  }
24
31
 
25
32
  interface SystemInfo {
package/dist/index.js CHANGED
@@ -549,17 +549,31 @@ var Config = class {
549
549
  // src/lib/Engine.ts
550
550
  import { spawn } from "child_process";
551
551
  import { createRequire } from "module";
552
- var Engine = class {
553
- name;
552
+ var Engine = class _Engine {
553
+ static instances = /* @__PURE__ */ new Map();
554
+ name = "";
554
555
  config;
555
556
  arguments;
556
557
  binary = null;
557
- timeout;
558
+ timeout = 3e4;
559
+ isDaemonConnected = false;
560
+ isConnecting = false;
558
561
  constructor(name, timeout = 3e4) {
562
+ if (_Engine.instances.has(name)) {
563
+ return _Engine.instances.get(name);
564
+ }
559
565
  this.name = name;
560
566
  this.config = this.setConfig(name);
561
567
  this.arguments = this.setArguments();
562
568
  this.timeout = timeout;
569
+ _Engine.instances.set(name, this);
570
+ this.setupProcessExitHandlers();
571
+ }
572
+ static getInstance(name, timeout = 3e4) {
573
+ if (_Engine.instances.has(name)) {
574
+ return _Engine.instances.get(name);
575
+ }
576
+ return new _Engine(name, timeout);
563
577
  }
564
578
  async initializeBinary() {
565
579
  if (!this.binary) {
@@ -631,6 +645,12 @@ var Engine = class {
631
645
  if (!this.binary) {
632
646
  throw new Error("Binary not initialized");
633
647
  }
648
+ const isConnectAction = args.includes("connect");
649
+ const isDisconnectAction = args.includes("disconnect");
650
+ const isExecuteAction = args.includes("execute");
651
+ if (isExecuteAction) {
652
+ await this.ensureDaemonConnected();
653
+ }
634
654
  return new Promise((resolve5, reject) => {
635
655
  const child = spawn(this.binary[binary], [...this.arguments, ...args]);
636
656
  let stdoutBuffer = "";
@@ -648,6 +668,12 @@ var Engine = class {
648
668
  isResolved = true;
649
669
  clearTimeout(timeoutId);
650
670
  resolve5(response);
671
+ if (isConnectAction && response.status === 200) {
672
+ this.isDaemonConnected = true;
673
+ this.isConnecting = false;
674
+ } else if (isDisconnectAction) {
675
+ this.isDaemonConnected = false;
676
+ }
651
677
  }
652
678
  };
653
679
  child.stdout.on("data", (data) => {
@@ -715,6 +741,67 @@ var Engine = class {
715
741
  child.unref();
716
742
  });
717
743
  }
744
+ async ensureDaemonConnected() {
745
+ if (this.isDaemonConnected) {
746
+ return;
747
+ }
748
+ if (this.isConnecting) {
749
+ while (this.isConnecting) {
750
+ await new Promise((resolve5) => setTimeout(resolve5, 50));
751
+ }
752
+ return;
753
+ }
754
+ this.isConnecting = true;
755
+ try {
756
+ const response = await this.run("query_engine", ["--action", "connect"]);
757
+ if (response.status !== 200) {
758
+ throw new Error(`Failed to connect daemon: ${response.message}`);
759
+ }
760
+ } catch (error) {
761
+ this.isConnecting = false;
762
+ throw error;
763
+ }
764
+ }
765
+ async disconnect() {
766
+ if (this.isDaemonConnected) {
767
+ await this.run("query_engine", ["--action", "disconnect"]);
768
+ }
769
+ }
770
+ setupProcessExitHandlers() {
771
+ const gracefulShutdown = async () => {
772
+ if (this.isDaemonConnected) {
773
+ console.log("\u{1F504} Disconnecting daemon before exit...");
774
+ try {
775
+ await this.disconnect();
776
+ console.log("\u2705 Daemon disconnected successfully");
777
+ } catch (error) {
778
+ console.error("\u274C Error disconnecting daemon:", error);
779
+ }
780
+ }
781
+ };
782
+ process.on("exit", () => {
783
+ });
784
+ process.on("SIGINT", async () => {
785
+ console.log("\n\u{1F6D1} Received SIGINT (Ctrl+C)");
786
+ await gracefulShutdown();
787
+ process.exit(0);
788
+ });
789
+ process.on("SIGTERM", async () => {
790
+ console.log("\u{1F6D1} Received SIGTERM");
791
+ await gracefulShutdown();
792
+ process.exit(0);
793
+ });
794
+ process.on("uncaughtException", async (error) => {
795
+ console.error("\u{1F4A5} Uncaught Exception:", error);
796
+ await gracefulShutdown();
797
+ process.exit(1);
798
+ });
799
+ process.on("unhandledRejection", async (reason, promise) => {
800
+ console.error("\u{1F4A5} Unhandled Rejection at:", promise, "reason:", reason);
801
+ await gracefulShutdown();
802
+ process.exit(1);
803
+ });
804
+ }
718
805
  };
719
806
 
720
807
  // src/lib/SqliteExecutor.ts