@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.cjs CHANGED
@@ -600,6 +600,10 @@ var Engine = class {
600
600
  arguments;
601
601
  binary = null;
602
602
  timeout;
603
+ childProcess = null;
604
+ pendingRequests = /* @__PURE__ */ new Map();
605
+ requestCounter = 0;
606
+ isProcessStarting = false;
603
607
  constructor(name, timeout = 3e4) {
604
608
  this.name = name;
605
609
  this.config = this.setConfig(name);
@@ -622,7 +626,9 @@ var Engine = class {
622
626
  "--database",
623
627
  this.config.config.DATABASE + ".db",
624
628
  "--motor",
625
- this.config.type
629
+ this.config.type,
630
+ "--persistent"
631
+ // Agregar flag para modo persistente si el binario lo soporta
626
632
  ];
627
633
  } else {
628
634
  args = [
@@ -641,7 +647,9 @@ var Engine = class {
641
647
  "--password",
642
648
  this.config.config.PASSWORD,
643
649
  "--motor",
644
- this.config.type
650
+ this.config.type,
651
+ "--persistent"
652
+ // Agregar flag para modo persistente si el binario lo soporta
645
653
  ];
646
654
  }
647
655
  return args;
@@ -671,93 +679,152 @@ var Engine = class {
671
679
  getConfig() {
672
680
  return this.config;
673
681
  }
674
- async run(binary, args) {
675
- await this.initializeBinary();
676
- if (!this.binary) {
677
- throw new Error("Binary not initialized");
682
+ async ensureProcess(binary) {
683
+ if (this.childProcess && !this.childProcess.killed) {
684
+ return;
678
685
  }
679
- return new Promise((resolve5, reject) => {
680
- const child = (0, import_child_process.spawn)(this.binary[binary], [...this.arguments, ...args]);
681
- let stdoutBuffer = "";
682
- let stderrBuffer = "";
683
- let isResolved = false;
684
- const timeoutId = setTimeout(() => {
685
- if (!isResolved) {
686
- isResolved = true;
687
- child.kill();
688
- reject(new Error("Process timeout"));
689
- }
690
- }, this.timeout);
691
- const resolveOnce = (response) => {
692
- if (!isResolved) {
693
- isResolved = true;
694
- clearTimeout(timeoutId);
695
- resolve5(response);
696
- }
697
- };
698
- child.stdout.on("data", (data) => {
699
- stdoutBuffer += data.toString();
700
- const match = stdoutBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
701
- if (match) {
702
- try {
703
- const response = JSON.parse(match[1]);
704
- resolveOnce({
705
- status: response.status,
706
- message: response.message,
707
- data: response.data
708
- });
709
- } catch (error) {
710
- resolveOnce({
711
- status: 500,
712
- message: "Failed to parse response JSON",
713
- data: null
714
- });
686
+ if (this.isProcessStarting) {
687
+ return new Promise((resolve5) => {
688
+ const checkProcess = () => {
689
+ if (!this.isProcessStarting) {
690
+ resolve5();
691
+ } else {
692
+ setTimeout(checkProcess, 10);
715
693
  }
716
- }
694
+ };
695
+ checkProcess();
717
696
  });
718
- child.stderr.on("data", (data) => {
719
- stderrBuffer += data.toString();
720
- const match = stderrBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
721
- if (match) {
722
- try {
723
- const response = JSON.parse(match[1]);
724
- resolveOnce({
725
- status: response.status,
726
- message: response.message,
727
- data: response.data
728
- });
729
- } catch (error) {
730
- resolveOnce({
731
- status: 500,
732
- message: "Failed to parse response JSON",
733
- data: null
734
- });
735
- }
736
- }
697
+ }
698
+ this.isProcessStarting = true;
699
+ try {
700
+ await this.initializeBinary();
701
+ if (!this.binary) {
702
+ throw new Error("Binary not initialized");
703
+ }
704
+ this.childProcess = (0, import_child_process.spawn)(this.binary[binary], this.arguments, {
705
+ stdio: ["pipe", "pipe", "pipe"]
737
706
  });
738
- child.on("close", (code) => {
739
- clearTimeout(timeoutId);
740
- if (!isResolved) {
741
- resolveOnce({
742
- status: code === 0 ? 200 : 500,
743
- message: code === 0 ? "Process completed" : `Process exited with code ${code}`,
744
- data: null
707
+ this.setupProcessHandlers();
708
+ } finally {
709
+ this.isProcessStarting = false;
710
+ }
711
+ }
712
+ setupProcessHandlers() {
713
+ if (!this.childProcess) return;
714
+ let stdoutBuffer = "";
715
+ let stderrBuffer = "";
716
+ this.childProcess.stdout?.on("data", (data) => {
717
+ stdoutBuffer += data.toString();
718
+ this.processBuffer(stdoutBuffer, "stdout");
719
+ });
720
+ this.childProcess.stderr?.on("data", (data) => {
721
+ stderrBuffer += data.toString();
722
+ this.processBuffer(stderrBuffer, "stderr");
723
+ });
724
+ this.childProcess.on("close", (code) => {
725
+ this.handleProcessClose(code);
726
+ });
727
+ this.childProcess.on("error", (error) => {
728
+ this.handleProcessError(error);
729
+ });
730
+ this.childProcess.unref();
731
+ }
732
+ processBuffer(buffer, source) {
733
+ const responsePattern = /PROCESS_RESPONSE:(\{[^}]*\})/g;
734
+ let match;
735
+ while ((match = responsePattern.exec(buffer)) !== null) {
736
+ try {
737
+ const response = JSON.parse(match[1]);
738
+ const requestId = response.requestId || "unknown";
739
+ const pendingRequest = this.pendingRequests.get(requestId);
740
+ if (pendingRequest) {
741
+ clearTimeout(pendingRequest.timeoutId);
742
+ this.pendingRequests.delete(requestId);
743
+ pendingRequest.resolve({
744
+ status: response.status,
745
+ message: response.message,
746
+ data: response.data
745
747
  });
746
748
  }
747
- });
748
- child.on("error", (error) => {
749
- clearTimeout(timeoutId);
750
- if (!isResolved) {
751
- resolveOnce({
752
- status: 500,
753
- message: `Process error: ${error.message}`,
754
- data: null
755
- });
749
+ } catch (error) {
750
+ console.error("Failed to parse response JSON:", error);
751
+ }
752
+ }
753
+ }
754
+ handleProcessClose(code) {
755
+ this.childProcess = null;
756
+ for (const [requestId, request] of this.pendingRequests) {
757
+ clearTimeout(request.timeoutId);
758
+ request.reject(new Error(`Process closed with code ${code}`));
759
+ }
760
+ this.pendingRequests.clear();
761
+ }
762
+ handleProcessError(error) {
763
+ this.childProcess = null;
764
+ for (const [requestId, request] of this.pendingRequests) {
765
+ clearTimeout(request.timeoutId);
766
+ request.reject(error);
767
+ }
768
+ this.pendingRequests.clear();
769
+ }
770
+ async run(binary, args) {
771
+ await this.ensureProcess(binary);
772
+ if (!this.childProcess) {
773
+ throw new Error("Failed to start process");
774
+ }
775
+ return new Promise((resolve5, reject) => {
776
+ const requestId = `req_${++this.requestCounter}_${Date.now()}`;
777
+ const timeoutId = setTimeout(() => {
778
+ const request = this.pendingRequests.get(requestId);
779
+ if (request) {
780
+ this.pendingRequests.delete(requestId);
781
+ reject(new Error("Request timeout"));
756
782
  }
783
+ }, this.timeout);
784
+ this.pendingRequests.set(requestId, {
785
+ resolve: resolve5,
786
+ reject,
787
+ timeoutId,
788
+ requestId
757
789
  });
758
- child.unref();
790
+ const command = {
791
+ requestId,
792
+ args
793
+ };
794
+ try {
795
+ this.childProcess.stdin?.write(JSON.stringify(command) + "\n");
796
+ } catch (error) {
797
+ clearTimeout(timeoutId);
798
+ this.pendingRequests.delete(requestId);
799
+ reject(error);
800
+ }
759
801
  });
760
802
  }
803
+ // Método para cerrar el proceso manualmente
804
+ async close() {
805
+ if (this.childProcess && !this.childProcess.killed) {
806
+ return new Promise((resolve5) => {
807
+ this.childProcess.once("close", () => {
808
+ resolve5();
809
+ });
810
+ this.childProcess.stdin?.end();
811
+ setTimeout(() => {
812
+ if (this.childProcess && !this.childProcess.killed) {
813
+ this.childProcess.kill("SIGTERM");
814
+ setTimeout(() => {
815
+ if (this.childProcess && !this.childProcess.killed) {
816
+ this.childProcess.kill("SIGKILL");
817
+ }
818
+ }, 2e3);
819
+ }
820
+ }, 5e3);
821
+ });
822
+ }
823
+ }
824
+ // Verificar si el proceso está activo
825
+ isProcessAlive() {
826
+ return this.childProcess !== null && !this.childProcess.killed;
827
+ }
761
828
  };
762
829
 
763
830
  // src/lib/SqliteExecutor.ts