@dbcube/core 3.0.2 → 3.0.4

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
@@ -12,6 +12,10 @@ declare class Engine {
12
12
  private arguments;
13
13
  private binary;
14
14
  private timeout;
15
+ private childProcess;
16
+ private pendingRequests;
17
+ private requestCounter;
18
+ private isProcessStarting;
15
19
  constructor(name: string, timeout?: number);
16
20
  initializeBinary(): Promise<void>;
17
21
  setArguments(): any[];
@@ -19,7 +23,14 @@ declare class Engine {
19
23
  [x: string]: any;
20
24
  } | null;
21
25
  getConfig(): any;
22
- run(binary: string, args: []): Promise<ResponseEngine>;
26
+ private ensureProcess;
27
+ private setupProcessHandlers;
28
+ private processBuffer;
29
+ private handleProcessClose;
30
+ private handleProcessError;
31
+ run(binary: string, args: string[]): Promise<ResponseEngine>;
32
+ close(): Promise<void>;
33
+ isProcessAlive(): boolean;
23
34
  }
24
35
 
25
36
  interface SystemInfo {
package/dist/index.d.ts CHANGED
@@ -12,6 +12,10 @@ declare class Engine {
12
12
  private arguments;
13
13
  private binary;
14
14
  private timeout;
15
+ private childProcess;
16
+ private pendingRequests;
17
+ private requestCounter;
18
+ private isProcessStarting;
15
19
  constructor(name: string, timeout?: number);
16
20
  initializeBinary(): Promise<void>;
17
21
  setArguments(): any[];
@@ -19,7 +23,14 @@ declare class Engine {
19
23
  [x: string]: any;
20
24
  } | null;
21
25
  getConfig(): any;
22
- run(binary: string, args: []): Promise<ResponseEngine>;
26
+ private ensureProcess;
27
+ private setupProcessHandlers;
28
+ private processBuffer;
29
+ private handleProcessClose;
30
+ private handleProcessError;
31
+ run(binary: string, args: string[]): Promise<ResponseEngine>;
32
+ close(): Promise<void>;
33
+ isProcessAlive(): boolean;
23
34
  }
24
35
 
25
36
  interface SystemInfo {
package/dist/index.js CHANGED
@@ -556,6 +556,10 @@ var Engine = class {
556
556
  arguments;
557
557
  binary = null;
558
558
  timeout;
559
+ childProcess = null;
560
+ pendingRequests = /* @__PURE__ */ new Map();
561
+ requestCounter = 0;
562
+ isProcessStarting = false;
559
563
  constructor(name, timeout = 3e4) {
560
564
  this.name = name;
561
565
  this.config = this.setConfig(name);
@@ -578,7 +582,9 @@ var Engine = class {
578
582
  "--database",
579
583
  this.config.config.DATABASE + ".db",
580
584
  "--motor",
581
- this.config.type
585
+ this.config.type,
586
+ "--persistent"
587
+ // Agregar flag para modo persistente si el binario lo soporta
582
588
  ];
583
589
  } else {
584
590
  args = [
@@ -597,7 +603,9 @@ var Engine = class {
597
603
  "--password",
598
604
  this.config.config.PASSWORD,
599
605
  "--motor",
600
- this.config.type
606
+ this.config.type,
607
+ "--persistent"
608
+ // Agregar flag para modo persistente si el binario lo soporta
601
609
  ];
602
610
  }
603
611
  return args;
@@ -627,93 +635,152 @@ var Engine = class {
627
635
  getConfig() {
628
636
  return this.config;
629
637
  }
630
- async run(binary, args) {
631
- await this.initializeBinary();
632
- if (!this.binary) {
633
- throw new Error("Binary not initialized");
638
+ async ensureProcess(binary) {
639
+ if (this.childProcess && !this.childProcess.killed) {
640
+ return;
634
641
  }
635
- return new Promise((resolve5, reject) => {
636
- const child = spawn(this.binary[binary], [...this.arguments, ...args]);
637
- let stdoutBuffer = "";
638
- let stderrBuffer = "";
639
- let isResolved = false;
640
- const timeoutId = setTimeout(() => {
641
- if (!isResolved) {
642
- isResolved = true;
643
- child.kill();
644
- reject(new Error("Process timeout"));
645
- }
646
- }, this.timeout);
647
- const resolveOnce = (response) => {
648
- if (!isResolved) {
649
- isResolved = true;
650
- clearTimeout(timeoutId);
651
- resolve5(response);
652
- }
653
- };
654
- child.stdout.on("data", (data) => {
655
- stdoutBuffer += data.toString();
656
- const match = stdoutBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
657
- if (match) {
658
- try {
659
- const response = JSON.parse(match[1]);
660
- resolveOnce({
661
- status: response.status,
662
- message: response.message,
663
- data: response.data
664
- });
665
- } catch (error) {
666
- resolveOnce({
667
- status: 500,
668
- message: "Failed to parse response JSON",
669
- data: null
670
- });
642
+ if (this.isProcessStarting) {
643
+ return new Promise((resolve5) => {
644
+ const checkProcess = () => {
645
+ if (!this.isProcessStarting) {
646
+ resolve5();
647
+ } else {
648
+ setTimeout(checkProcess, 10);
671
649
  }
672
- }
650
+ };
651
+ checkProcess();
673
652
  });
674
- child.stderr.on("data", (data) => {
675
- stderrBuffer += data.toString();
676
- const match = stderrBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
677
- if (match) {
678
- try {
679
- const response = JSON.parse(match[1]);
680
- resolveOnce({
681
- status: response.status,
682
- message: response.message,
683
- data: response.data
684
- });
685
- } catch (error) {
686
- resolveOnce({
687
- status: 500,
688
- message: "Failed to parse response JSON",
689
- data: null
690
- });
691
- }
692
- }
653
+ }
654
+ this.isProcessStarting = true;
655
+ try {
656
+ await this.initializeBinary();
657
+ if (!this.binary) {
658
+ throw new Error("Binary not initialized");
659
+ }
660
+ this.childProcess = spawn(this.binary[binary], this.arguments, {
661
+ stdio: ["pipe", "pipe", "pipe"]
693
662
  });
694
- child.on("close", (code) => {
695
- clearTimeout(timeoutId);
696
- if (!isResolved) {
697
- resolveOnce({
698
- status: code === 0 ? 200 : 500,
699
- message: code === 0 ? "Process completed" : `Process exited with code ${code}`,
700
- data: null
663
+ this.setupProcessHandlers();
664
+ } finally {
665
+ this.isProcessStarting = false;
666
+ }
667
+ }
668
+ setupProcessHandlers() {
669
+ if (!this.childProcess) return;
670
+ let stdoutBuffer = "";
671
+ let stderrBuffer = "";
672
+ this.childProcess.stdout?.on("data", (data) => {
673
+ stdoutBuffer += data.toString();
674
+ this.processBuffer(stdoutBuffer, "stdout");
675
+ });
676
+ this.childProcess.stderr?.on("data", (data) => {
677
+ stderrBuffer += data.toString();
678
+ this.processBuffer(stderrBuffer, "stderr");
679
+ });
680
+ this.childProcess.on("close", (code) => {
681
+ this.handleProcessClose(code);
682
+ });
683
+ this.childProcess.on("error", (error) => {
684
+ this.handleProcessError(error);
685
+ });
686
+ this.childProcess.unref();
687
+ }
688
+ processBuffer(buffer, source) {
689
+ const responsePattern = /PROCESS_RESPONSE:(\{[^}]*\})/g;
690
+ let match;
691
+ while ((match = responsePattern.exec(buffer)) !== null) {
692
+ try {
693
+ const response = JSON.parse(match[1]);
694
+ const requestId = response.requestId || "unknown";
695
+ const pendingRequest = this.pendingRequests.get(requestId);
696
+ if (pendingRequest) {
697
+ clearTimeout(pendingRequest.timeoutId);
698
+ this.pendingRequests.delete(requestId);
699
+ pendingRequest.resolve({
700
+ status: response.status,
701
+ message: response.message,
702
+ data: response.data
701
703
  });
702
704
  }
703
- });
704
- child.on("error", (error) => {
705
- clearTimeout(timeoutId);
706
- if (!isResolved) {
707
- resolveOnce({
708
- status: 500,
709
- message: `Process error: ${error.message}`,
710
- data: null
711
- });
705
+ } catch (error) {
706
+ console.error("Failed to parse response JSON:", error);
707
+ }
708
+ }
709
+ }
710
+ handleProcessClose(code) {
711
+ this.childProcess = null;
712
+ for (const [requestId, request] of this.pendingRequests) {
713
+ clearTimeout(request.timeoutId);
714
+ request.reject(new Error(`Process closed with code ${code}`));
715
+ }
716
+ this.pendingRequests.clear();
717
+ }
718
+ handleProcessError(error) {
719
+ this.childProcess = null;
720
+ for (const [requestId, request] of this.pendingRequests) {
721
+ clearTimeout(request.timeoutId);
722
+ request.reject(error);
723
+ }
724
+ this.pendingRequests.clear();
725
+ }
726
+ async run(binary, args) {
727
+ await this.ensureProcess(binary);
728
+ if (!this.childProcess) {
729
+ throw new Error("Failed to start process");
730
+ }
731
+ return new Promise((resolve5, reject) => {
732
+ const requestId = `req_${++this.requestCounter}_${Date.now()}`;
733
+ const timeoutId = setTimeout(() => {
734
+ const request = this.pendingRequests.get(requestId);
735
+ if (request) {
736
+ this.pendingRequests.delete(requestId);
737
+ reject(new Error("Request timeout"));
712
738
  }
739
+ }, this.timeout);
740
+ this.pendingRequests.set(requestId, {
741
+ resolve: resolve5,
742
+ reject,
743
+ timeoutId,
744
+ requestId
713
745
  });
714
- child.unref();
746
+ const command = {
747
+ requestId,
748
+ args
749
+ };
750
+ try {
751
+ this.childProcess.stdin?.write(JSON.stringify(command) + "\n");
752
+ } catch (error) {
753
+ clearTimeout(timeoutId);
754
+ this.pendingRequests.delete(requestId);
755
+ reject(error);
756
+ }
715
757
  });
716
758
  }
759
+ // Método para cerrar el proceso manualmente
760
+ async close() {
761
+ if (this.childProcess && !this.childProcess.killed) {
762
+ return new Promise((resolve5) => {
763
+ this.childProcess.once("close", () => {
764
+ resolve5();
765
+ });
766
+ this.childProcess.stdin?.end();
767
+ setTimeout(() => {
768
+ if (this.childProcess && !this.childProcess.killed) {
769
+ this.childProcess.kill("SIGTERM");
770
+ setTimeout(() => {
771
+ if (this.childProcess && !this.childProcess.killed) {
772
+ this.childProcess.kill("SIGKILL");
773
+ }
774
+ }, 2e3);
775
+ }
776
+ }, 5e3);
777
+ });
778
+ }
779
+ }
780
+ // Verificar si el proceso está activo
781
+ isProcessAlive() {
782
+ return this.childProcess !== null && !this.childProcess.killed;
783
+ }
717
784
  };
718
785
 
719
786
  // src/lib/SqliteExecutor.ts