@noego/sqlego 0.1.6-dev.0 → 0.1.6-dev.10
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/README.md +67 -10
- package/dist/client.d.ts +57 -9
- package/dist/index.js +488 -86
- package/dist/lease.d.ts +39 -0
- package/dist/legacy_lock.d.ts +20 -0
- package/dist/paths.d.ts +6 -0
- package/dist/protocol.d.ts +28 -2
- package/dist/version.d.ts +17 -3
- package/package.json +9 -8
- package/src/cli.ts +121 -2
- package/src/client.ts +188 -85
- package/src/lease.ts +265 -0
- package/src/legacy_lock.ts +123 -0
- package/src/paths.ts +4 -0
- package/src/protocol.ts +56 -6
- package/src/server.ts +355 -30
- package/src/version.ts +75 -7
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// src/client.ts
|
|
2
2
|
import { spawn } from "node:child_process";
|
|
3
|
-
import {
|
|
3
|
+
import { readdirSync as readdirSync3, readFileSync as readFileSync4, existsSync as existsSync3 } from "node:fs";
|
|
4
4
|
import { createRequire } from "node:module";
|
|
5
5
|
import net from "node:net";
|
|
6
|
-
import
|
|
6
|
+
import path5 from "node:path";
|
|
7
7
|
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
8
8
|
|
|
9
9
|
// node_modules/@noego/trust/dist/index.js
|
|
@@ -90,6 +90,16 @@ function locateServerBinary(lookup) {
|
|
|
90
90
|
});
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
// src/lease.ts
|
|
94
|
+
import { linkSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
95
|
+
import path3 from "node:path";
|
|
96
|
+
|
|
97
|
+
// src/paths.ts
|
|
98
|
+
import { createHash } from "node:crypto";
|
|
99
|
+
import { mkdirSync, realpathSync, existsSync } from "node:fs";
|
|
100
|
+
import { tmpdir } from "node:os";
|
|
101
|
+
import path2 from "node:path";
|
|
102
|
+
|
|
93
103
|
// src/protocol.ts
|
|
94
104
|
var PROTOCOL_VERSION = 3;
|
|
95
105
|
var INT64_MIN = -9223372036854775808n;
|
|
@@ -596,32 +606,52 @@ function normalizePragma(pragma) {
|
|
|
596
606
|
function samePragmas(a, b) {
|
|
597
607
|
return a.length === b.length && a.every((pragma, index) => normalizePragma(pragma) === normalizePragma(b[index]));
|
|
598
608
|
}
|
|
609
|
+
var DEFAULT_MAX_FRAME_LENGTH = 64 * 1024 * 1024;
|
|
610
|
+
|
|
599
611
|
class FrameReader {
|
|
612
|
+
maxFrameLength;
|
|
600
613
|
pieces = [];
|
|
614
|
+
held = 0;
|
|
615
|
+
overflowed = false;
|
|
601
616
|
decoder = null;
|
|
602
617
|
searched = 0;
|
|
618
|
+
constructor(maxFrameLength = Number.POSITIVE_INFINITY) {
|
|
619
|
+
this.maxFrameLength = maxFrameLength;
|
|
620
|
+
}
|
|
603
621
|
get scannedCharacters() {
|
|
604
622
|
return this.searched;
|
|
605
623
|
}
|
|
624
|
+
get oversized() {
|
|
625
|
+
return this.overflowed;
|
|
626
|
+
}
|
|
606
627
|
push(chunk) {
|
|
628
|
+
if (this.overflowed)
|
|
629
|
+
return [];
|
|
607
630
|
let newline = chunk.indexOf(`
|
|
608
631
|
`);
|
|
609
632
|
if (newline < 0) {
|
|
610
633
|
this.searched += chunk.length;
|
|
611
|
-
if (chunk.length > 0)
|
|
634
|
+
if (chunk.length > 0) {
|
|
635
|
+
this.held += chunk.length;
|
|
636
|
+
if (this.held > this.maxFrameLength)
|
|
637
|
+
return this.overflow([]);
|
|
612
638
|
this.pieces.push(chunk);
|
|
639
|
+
}
|
|
613
640
|
return [];
|
|
614
641
|
}
|
|
615
642
|
const frames = [];
|
|
616
643
|
let start = 0;
|
|
617
644
|
while (newline >= 0) {
|
|
618
645
|
this.searched += newline + 1 - start;
|
|
646
|
+
if (this.held + (newline - start) > this.maxFrameLength)
|
|
647
|
+
return this.overflow(frames);
|
|
619
648
|
let frame = chunk.slice(start, newline);
|
|
620
649
|
if (this.pieces.length > 0) {
|
|
621
650
|
this.pieces.push(frame);
|
|
622
651
|
frame = this.pieces.join("");
|
|
623
652
|
this.pieces = [];
|
|
624
653
|
}
|
|
654
|
+
this.held = 0;
|
|
625
655
|
if (frame.length > 0)
|
|
626
656
|
frames.push(frame);
|
|
627
657
|
start = newline + 1;
|
|
@@ -629,8 +659,12 @@ class FrameReader {
|
|
|
629
659
|
`, start);
|
|
630
660
|
}
|
|
631
661
|
this.searched += chunk.length - start;
|
|
632
|
-
if (start < chunk.length)
|
|
662
|
+
if (start < chunk.length) {
|
|
663
|
+
this.held = chunk.length - start;
|
|
664
|
+
if (this.held > this.maxFrameLength)
|
|
665
|
+
return this.overflow(frames);
|
|
633
666
|
this.pieces.push(chunk.slice(start));
|
|
667
|
+
}
|
|
634
668
|
return frames;
|
|
635
669
|
}
|
|
636
670
|
pushBytes(chunk) {
|
|
@@ -639,18 +673,22 @@ class FrameReader {
|
|
|
639
673
|
}
|
|
640
674
|
reset() {
|
|
641
675
|
this.pieces = [];
|
|
676
|
+
this.held = 0;
|
|
677
|
+
this.overflowed = false;
|
|
642
678
|
this.decoder = null;
|
|
643
679
|
}
|
|
680
|
+
overflow(frames) {
|
|
681
|
+
this.pieces = [];
|
|
682
|
+
this.held = 0;
|
|
683
|
+
this.overflowed = true;
|
|
684
|
+
return frames;
|
|
685
|
+
}
|
|
644
686
|
}
|
|
645
687
|
function replaceBigInt(_key, value) {
|
|
646
688
|
return typeof value === "bigint" ? Number(value) : value;
|
|
647
689
|
}
|
|
648
690
|
|
|
649
691
|
// src/paths.ts
|
|
650
|
-
import { createHash } from "node:crypto";
|
|
651
|
-
import { mkdirSync, realpathSync, existsSync } from "node:fs";
|
|
652
|
-
import { tmpdir } from "node:os";
|
|
653
|
-
import path2 from "node:path";
|
|
654
692
|
function resolveRuntimeDirectory() {
|
|
655
693
|
const configured = process.env.SQLEGO_RUNTIME_DIR;
|
|
656
694
|
const directory = configured && configured.length > 0 ? configured : path2.join(tmpdir(), "sqlego");
|
|
@@ -705,6 +743,7 @@ function resolvePaths(file) {
|
|
|
705
743
|
key,
|
|
706
744
|
socket,
|
|
707
745
|
lock: path2.join(directory.value, `${key}.lock`),
|
|
746
|
+
lease: path2.join(directory.value, `${key}.lease`),
|
|
708
747
|
meta: path2.join(directory.value, `${key}.json`)
|
|
709
748
|
});
|
|
710
749
|
return Result.ok(paths);
|
|
@@ -753,15 +792,248 @@ function isProcessAlive(pid) {
|
|
|
753
792
|
return unhandled;
|
|
754
793
|
}
|
|
755
794
|
|
|
795
|
+
// src/lease.ts
|
|
796
|
+
var leaseFailure = (cause) => ({ kind: "lease-failed", message: errorMessage(cause) });
|
|
797
|
+
var ACQUIRE_ATTEMPTS = 8;
|
|
798
|
+
function spawnLeaseBase(runtimeDirectory) {
|
|
799
|
+
return path3.join(runtimeDirectory, "spawn.lease");
|
|
800
|
+
}
|
|
801
|
+
function generationFile(base, generation) {
|
|
802
|
+
return `${base}.${generation}`;
|
|
803
|
+
}
|
|
804
|
+
var created = 0;
|
|
805
|
+
function newToken() {
|
|
806
|
+
created += 1;
|
|
807
|
+
return `${process.pid}.${Date.now().toString(36)}.${created}`;
|
|
808
|
+
}
|
|
809
|
+
function encodeLease(lease) {
|
|
810
|
+
return `${lease.generation}:${lease.token}`;
|
|
811
|
+
}
|
|
812
|
+
function generations(base) {
|
|
813
|
+
const prefix = `${path3.basename(base)}.`;
|
|
814
|
+
const names = Trust.try(() => readdirSync(path3.dirname(base)), leaseFailure);
|
|
815
|
+
if (!names.ok)
|
|
816
|
+
return names;
|
|
817
|
+
const found = [];
|
|
818
|
+
for (const name of names.value) {
|
|
819
|
+
if (!name.startsWith(prefix))
|
|
820
|
+
continue;
|
|
821
|
+
const suffix = name.slice(prefix.length);
|
|
822
|
+
if (/^\d+$/.test(suffix))
|
|
823
|
+
found.push(Number(suffix));
|
|
824
|
+
}
|
|
825
|
+
return Result.ok(found);
|
|
826
|
+
}
|
|
827
|
+
function leaseHead(base) {
|
|
828
|
+
const found = generations(base);
|
|
829
|
+
if (!found.ok)
|
|
830
|
+
return found;
|
|
831
|
+
return Result.ok(Math.max(0, ...found.value));
|
|
832
|
+
}
|
|
833
|
+
function readGeneration(base, generation) {
|
|
834
|
+
const text = Trust.try(() => readFileSync(generationFile(base, generation), "utf8"), (cause) => errnoCode(cause) === "ENOENT" ? { kind: "gone" } : leaseFailure(cause));
|
|
835
|
+
if (!text.ok)
|
|
836
|
+
return text;
|
|
837
|
+
const state = Trust.decide(fromJson(text.value), { invalid: () => null }).value;
|
|
838
|
+
if (typeof state !== "object" || state === null)
|
|
839
|
+
return Result.ok({ holder: null, token: null });
|
|
840
|
+
const token = "token" in state && typeof state.token === "string" ? state.token : null;
|
|
841
|
+
const holder = "holder" in state ? state.holder : null;
|
|
842
|
+
const live = typeof holder === "number" && Number.isInteger(holder) && holder > 0 && isProcessAlive(holder) ? holder : null;
|
|
843
|
+
return Result.ok({ holder: live, token });
|
|
844
|
+
}
|
|
845
|
+
function generationIs(base, generation, token) {
|
|
846
|
+
if (generation === 0)
|
|
847
|
+
return Result.ok(true);
|
|
848
|
+
const state = readGeneration(base, generation);
|
|
849
|
+
if (state.ok)
|
|
850
|
+
return Result.ok(state.value.token === token);
|
|
851
|
+
const failure = state.error;
|
|
852
|
+
if (failure.kind === "gone")
|
|
853
|
+
return Result.ok(false);
|
|
854
|
+
return Result.err(failure);
|
|
855
|
+
}
|
|
856
|
+
function advance(base, from, fromToken, holder) {
|
|
857
|
+
const before = generationIs(base, from, fromToken);
|
|
858
|
+
if (!before.ok)
|
|
859
|
+
return before;
|
|
860
|
+
if (!before.value)
|
|
861
|
+
return Result.ok(null);
|
|
862
|
+
const next = generationFile(base, from + 1);
|
|
863
|
+
const token = newToken();
|
|
864
|
+
const staged = `${next}.${token}.tmp`;
|
|
865
|
+
const linked = Trust.try(() => {
|
|
866
|
+
writeFileSync(staged, holder === null ? `{"free":true,"token":"${token}"}` : `{"holder":${holder},"token":"${token}"}`);
|
|
867
|
+
linkSync(staged, next);
|
|
868
|
+
}, (cause) => errnoCode(cause) === "EEXIST" ? { kind: "lost" } : leaseFailure(cause));
|
|
869
|
+
const unstaged = Trust.try(() => rmSync(staged, { force: true }), leaseFailure);
|
|
870
|
+
if (!unstaged.ok && unstaged.error.kind === "lease-failed") {}
|
|
871
|
+
if (!linked.ok) {
|
|
872
|
+
const failure = linked.error;
|
|
873
|
+
if (failure.kind === "lost")
|
|
874
|
+
return Result.ok(null);
|
|
875
|
+
return Result.err(failure);
|
|
876
|
+
}
|
|
877
|
+
const found = generations(base);
|
|
878
|
+
if (!found.ok)
|
|
879
|
+
return found;
|
|
880
|
+
if (found.value.some((generation) => generation > from + 1))
|
|
881
|
+
return Result.ok(null);
|
|
882
|
+
const after = generationIs(base, from, fromToken);
|
|
883
|
+
if (!after.ok || !after.value) {
|
|
884
|
+
withdraw(base, from + 1, token);
|
|
885
|
+
if (!after.ok)
|
|
886
|
+
return after;
|
|
887
|
+
return Result.ok(null);
|
|
888
|
+
}
|
|
889
|
+
prune(base, found.value.filter((generation) => generation <= from));
|
|
890
|
+
return Result.ok(token);
|
|
891
|
+
}
|
|
892
|
+
function withdraw(base, generation, token) {
|
|
893
|
+
if (!Trust.decide(generationIs(base, generation, token), { "lease-failed": () => false }).value)
|
|
894
|
+
return;
|
|
895
|
+
const removed = Trust.try(() => rmSync(generationFile(base, generation), { force: true }), leaseFailure);
|
|
896
|
+
if (!removed.ok && removed.error.kind === "lease-failed") {}
|
|
897
|
+
}
|
|
898
|
+
function prune(base, superseded) {
|
|
899
|
+
for (const generation of superseded) {
|
|
900
|
+
const removed = Trust.try(() => rmSync(generationFile(base, generation), { force: true }), leaseFailure);
|
|
901
|
+
if (!removed.ok && removed.error.kind === "lease-failed") {}
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
function acquireLease(base) {
|
|
905
|
+
for (let attempt = 0;attempt < ACQUIRE_ATTEMPTS; attempt += 1) {
|
|
906
|
+
const head = leaseHead(base);
|
|
907
|
+
if (!head.ok)
|
|
908
|
+
return head;
|
|
909
|
+
let headToken = null;
|
|
910
|
+
if (head.value > 0) {
|
|
911
|
+
const state = readGeneration(base, head.value);
|
|
912
|
+
if (!state.ok) {
|
|
913
|
+
const failure = state.error;
|
|
914
|
+
if (failure.kind === "gone")
|
|
915
|
+
continue;
|
|
916
|
+
return Result.err(failure);
|
|
917
|
+
}
|
|
918
|
+
if (state.value.holder !== null)
|
|
919
|
+
return Result.ok(null);
|
|
920
|
+
headToken = state.value.token;
|
|
921
|
+
}
|
|
922
|
+
const won = advance(base, head.value, headToken, process.pid);
|
|
923
|
+
if (!won.ok)
|
|
924
|
+
return won;
|
|
925
|
+
if (won.value !== null)
|
|
926
|
+
return Result.ok({ base, generation: head.value + 1, token: won.value });
|
|
927
|
+
}
|
|
928
|
+
return Result.ok(null);
|
|
929
|
+
}
|
|
930
|
+
function handOverLease(lease, holder) {
|
|
931
|
+
const moved = advance(lease.base, lease.generation, lease.token, holder);
|
|
932
|
+
if (!moved.ok)
|
|
933
|
+
return moved;
|
|
934
|
+
return Result.ok(moved.value === null ? null : { base: lease.base, generation: lease.generation + 1, token: moved.value });
|
|
935
|
+
}
|
|
936
|
+
function releaseLease(lease) {
|
|
937
|
+
const released = advance(lease.base, lease.generation, lease.token, null);
|
|
938
|
+
if (!released.ok)
|
|
939
|
+
return released;
|
|
940
|
+
return Result.ok(released.value !== null);
|
|
941
|
+
}
|
|
942
|
+
function leaseHolder(base) {
|
|
943
|
+
for (;; ) {
|
|
944
|
+
const head = leaseHead(base);
|
|
945
|
+
if (!head.ok)
|
|
946
|
+
return head;
|
|
947
|
+
if (head.value === 0)
|
|
948
|
+
return Result.ok(null);
|
|
949
|
+
const state = readGeneration(base, head.value);
|
|
950
|
+
if (state.ok)
|
|
951
|
+
return Result.ok(state.value.holder);
|
|
952
|
+
const failure = state.error;
|
|
953
|
+
if (failure.kind !== "gone")
|
|
954
|
+
return Result.err(failure);
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
// src/legacy_lock.ts
|
|
959
|
+
import { linkSync as linkSync2, readFileSync as readFileSync2, renameSync, rmSync as rmSync2, writeFileSync as writeFileSync2 } from "node:fs";
|
|
960
|
+
var legacyFailure = (cause) => ({ kind: "legacy-lock-failed", message: errorMessage(cause) });
|
|
961
|
+
var staged = 0;
|
|
962
|
+
function stage(lock, pid) {
|
|
963
|
+
staged += 1;
|
|
964
|
+
const file = `${lock}.${process.pid}.${staged}.tmp`;
|
|
965
|
+
return Trust.try(() => {
|
|
966
|
+
writeFileSync2(file, String(pid));
|
|
967
|
+
return file;
|
|
968
|
+
}, legacyFailure);
|
|
969
|
+
}
|
|
970
|
+
function unstage(file) {
|
|
971
|
+
const removed = Trust.try(() => rmSync2(file, { force: true }), legacyFailure);
|
|
972
|
+
if (!removed.ok && removed.error.kind === "legacy-lock-failed") {}
|
|
973
|
+
}
|
|
974
|
+
function holderOf(lock) {
|
|
975
|
+
const text = Trust.try(() => readFileSync2(lock, "utf8"), (cause) => errnoCode(cause) === "ENOENT" ? { kind: "absent" } : legacyFailure(cause));
|
|
976
|
+
if (!text.ok) {
|
|
977
|
+
const failure = text.error;
|
|
978
|
+
if (failure.kind === "absent")
|
|
979
|
+
return Result.ok(null);
|
|
980
|
+
return Result.err(failure);
|
|
981
|
+
}
|
|
982
|
+
const pid = Number(text.value.trim());
|
|
983
|
+
return Result.ok(Number.isInteger(pid) && pid > 0 ? pid : 0);
|
|
984
|
+
}
|
|
985
|
+
function create(lock, pid) {
|
|
986
|
+
const file = stage(lock, pid);
|
|
987
|
+
if (!file.ok)
|
|
988
|
+
return file;
|
|
989
|
+
const linked = Trust.try(() => linkSync2(file.value, lock), (cause) => errnoCode(cause) === "EEXIST" ? { kind: "exists" } : legacyFailure(cause));
|
|
990
|
+
unstage(file.value);
|
|
991
|
+
if (linked.ok)
|
|
992
|
+
return Result.ok(true);
|
|
993
|
+
const failure = linked.error;
|
|
994
|
+
if (failure.kind === "exists")
|
|
995
|
+
return Result.ok(false);
|
|
996
|
+
return Result.err(failure);
|
|
997
|
+
}
|
|
998
|
+
function takeLegacyLock(lock, pid) {
|
|
999
|
+
for (let attempt = 0;attempt < 2; attempt += 1) {
|
|
1000
|
+
const created = create(lock, pid);
|
|
1001
|
+
if (!created.ok || created.value)
|
|
1002
|
+
return created;
|
|
1003
|
+
const holder = holderOf(lock);
|
|
1004
|
+
if (!holder.ok)
|
|
1005
|
+
return holder;
|
|
1006
|
+
if (holder.value === pid)
|
|
1007
|
+
return Result.ok(true);
|
|
1008
|
+
if (holder.value !== null && holder.value > 0 && isProcessAlive(holder.value))
|
|
1009
|
+
return Result.ok(false);
|
|
1010
|
+
const removed = Trust.try(() => rmSync2(lock, { force: true }), legacyFailure);
|
|
1011
|
+
if (!removed.ok)
|
|
1012
|
+
return removed;
|
|
1013
|
+
}
|
|
1014
|
+
return Result.ok(false);
|
|
1015
|
+
}
|
|
1016
|
+
function dropLegacyLock(lock, pids) {
|
|
1017
|
+
const holder = holderOf(lock);
|
|
1018
|
+
if (!holder.ok)
|
|
1019
|
+
return holder;
|
|
1020
|
+
if (holder.value === null || !pids.includes(holder.value))
|
|
1021
|
+
return Result.ok(false);
|
|
1022
|
+
const removed = Trust.try(() => rmSync2(lock, { force: true }), legacyFailure);
|
|
1023
|
+
if (!removed.ok)
|
|
1024
|
+
return removed;
|
|
1025
|
+
return Result.ok(true);
|
|
1026
|
+
}
|
|
1027
|
+
|
|
756
1028
|
// src/version.ts
|
|
757
1029
|
import { createHash as createHash2 } from "node:crypto";
|
|
758
|
-
import { existsSync as existsSync2, readdirSync, readFileSync } from "node:fs";
|
|
759
|
-
import
|
|
1030
|
+
import { existsSync as existsSync2, readdirSync as readdirSync2, readFileSync as readFileSync3 } from "node:fs";
|
|
1031
|
+
import path4 from "node:path";
|
|
760
1032
|
import { fileURLToPath } from "node:url";
|
|
761
1033
|
// package.json
|
|
762
1034
|
var package_default = {
|
|
763
1035
|
name: "@noego/sqlego",
|
|
764
|
-
version: "0.1.6-dev.
|
|
1036
|
+
version: "0.1.6-dev.10",
|
|
765
1037
|
description: "One owner per SQLite file, many files per owner process. Clients (CLI, Node, Bun) pass SQL over a local socket (Unix) or named pipe (Windows); the owner starts on demand and exits when idle.",
|
|
766
1038
|
type: "module",
|
|
767
1039
|
license: "ISC",
|
|
@@ -792,7 +1064,7 @@ var package_default = {
|
|
|
792
1064
|
"README.md"
|
|
793
1065
|
],
|
|
794
1066
|
scripts: {
|
|
795
|
-
prepare: "bun scripts/build.ts
|
|
1067
|
+
prepare: "bun scripts/build.ts lib",
|
|
796
1068
|
build: "bun scripts/build.ts all",
|
|
797
1069
|
"build:all": "bun scripts/build.ts all:platforms",
|
|
798
1070
|
"build:lib": "bun scripts/build.ts lib",
|
|
@@ -812,31 +1084,75 @@ var package_default = {
|
|
|
812
1084
|
"@noego/trust": "^0.5.1"
|
|
813
1085
|
},
|
|
814
1086
|
optionalDependencies: {
|
|
815
|
-
"@noego/sqlego-darwin-arm64": "0.1.6-dev.
|
|
816
|
-
"@noego/sqlego-darwin-x64": "0.1.6-dev.
|
|
817
|
-
"@noego/sqlego-linux-x64": "0.1.6-dev.
|
|
818
|
-
"@noego/sqlego-linux-arm64": "0.1.6-dev.
|
|
819
|
-
"@noego/sqlego-win32-x64": "0.1.6-dev.
|
|
820
|
-
}
|
|
1087
|
+
"@noego/sqlego-darwin-arm64": "0.1.6-dev.10",
|
|
1088
|
+
"@noego/sqlego-darwin-x64": "0.1.6-dev.10",
|
|
1089
|
+
"@noego/sqlego-linux-x64": "0.1.6-dev.10",
|
|
1090
|
+
"@noego/sqlego-linux-arm64": "0.1.6-dev.10",
|
|
1091
|
+
"@noego/sqlego-win32-x64": "0.1.6-dev.10"
|
|
1092
|
+
},
|
|
1093
|
+
sqlegoBuild: "247f5e669b76"
|
|
821
1094
|
};
|
|
822
1095
|
|
|
823
1096
|
// src/version.ts
|
|
1097
|
+
var RUST_BUILD_FILES = ["rust/Cargo.toml", "rust/Cargo.lock", "rust/build.rs"];
|
|
824
1098
|
function sourceBuildId(sourceDirectory) {
|
|
825
1099
|
if (!existsSync2(sourceDirectory))
|
|
826
1100
|
return Result.ok("source");
|
|
827
1101
|
return Trust.try(() => {
|
|
828
1102
|
const hash = createHash2("sha1");
|
|
829
|
-
for (const name of
|
|
1103
|
+
for (const name of readdirSync2(sourceDirectory).filter((entry) => entry.endsWith(".ts")).sort()) {
|
|
830
1104
|
hash.update(name);
|
|
831
|
-
hash.update(
|
|
1105
|
+
hash.update(readFileSync3(path4.join(sourceDirectory, name)));
|
|
1106
|
+
}
|
|
1107
|
+
const root = path4.dirname(sourceDirectory);
|
|
1108
|
+
if (existsSync2(path4.join(root, "rust", "Cargo.toml"))) {
|
|
1109
|
+
const files = RUST_BUILD_FILES.filter((file) => existsSync2(path4.join(root, file)));
|
|
1110
|
+
const rustSources = path4.join(root, "rust", "src");
|
|
1111
|
+
if (existsSync2(rustSources)) {
|
|
1112
|
+
for (const entry of readdirSync2(rustSources, { recursive: true, withFileTypes: true })) {
|
|
1113
|
+
if (!entry.isFile())
|
|
1114
|
+
continue;
|
|
1115
|
+
const relative = path4.relative(root, path4.join(entry.parentPath, entry.name));
|
|
1116
|
+
files.push(relative.split(path4.sep).join("/"));
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
for (const file of files.sort()) {
|
|
1120
|
+
hash.update(file);
|
|
1121
|
+
hash.update(readFileSync3(path4.join(root, file)));
|
|
1122
|
+
}
|
|
832
1123
|
}
|
|
833
1124
|
return hash.digest("hex").slice(0, 12);
|
|
834
1125
|
}, (cause) => ({ kind: "source-unreadable", message: `cannot hash sources in ${sourceDirectory}: ${errorMessage(cause)}` }));
|
|
835
1126
|
}
|
|
836
|
-
function
|
|
837
|
-
if ("
|
|
838
|
-
return
|
|
839
|
-
|
|
1127
|
+
function chooseBuildId(defined, stamped, rustInTree, fromSource) {
|
|
1128
|
+
if (typeof defined === "string" && defined.length > 0)
|
|
1129
|
+
return defined;
|
|
1130
|
+
if (!rustInTree && typeof stamped === "string" && stamped.length > 0)
|
|
1131
|
+
return stamped;
|
|
1132
|
+
return fromSource();
|
|
1133
|
+
}
|
|
1134
|
+
var stampedBuild = package_default.sqlegoBuild;
|
|
1135
|
+
function moduleDirectory() {
|
|
1136
|
+
return Trust.try(() => path4.dirname(fileURLToPath(import.meta.url)), (cause) => ({ kind: "module-path", message: errorMessage(cause) }));
|
|
1137
|
+
}
|
|
1138
|
+
function rustInTree() {
|
|
1139
|
+
const directory = moduleDirectory();
|
|
1140
|
+
if (!directory.ok)
|
|
1141
|
+
return decideNoTree(directory.error.kind);
|
|
1142
|
+
return existsSync2(path4.join(directory.value, "..", "rust", "Cargo.toml"));
|
|
1143
|
+
}
|
|
1144
|
+
function decideNoTree(kind) {
|
|
1145
|
+
switch (kind) {
|
|
1146
|
+
case "module-path":
|
|
1147
|
+
return false;
|
|
1148
|
+
default: {
|
|
1149
|
+
const unhandled = kind;
|
|
1150
|
+
return unhandled;
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
function buildIdFromSource() {
|
|
1155
|
+
const directory = moduleDirectory();
|
|
840
1156
|
if (!directory.ok)
|
|
841
1157
|
return decideUnknownBuild(directory.error.kind);
|
|
842
1158
|
const id = sourceBuildId(directory.value);
|
|
@@ -844,6 +1160,9 @@ function resolveBuildId() {
|
|
|
844
1160
|
return decideUnknownBuild(id.error.kind);
|
|
845
1161
|
return id.value;
|
|
846
1162
|
}
|
|
1163
|
+
function resolveBuildId() {
|
|
1164
|
+
return chooseBuildId("247f5e669b76", stampedBuild, rustInTree(), buildIdFromSource);
|
|
1165
|
+
}
|
|
847
1166
|
function decideUnknownBuild(kind) {
|
|
848
1167
|
switch (kind) {
|
|
849
1168
|
case "module-path":
|
|
@@ -895,7 +1214,8 @@ class SqlegoClient {
|
|
|
895
1214
|
hello = null;
|
|
896
1215
|
mismatches = [];
|
|
897
1216
|
spawnFailure = null;
|
|
898
|
-
|
|
1217
|
+
claim = null;
|
|
1218
|
+
spawnLease = null;
|
|
899
1219
|
options;
|
|
900
1220
|
constructor(paths, options) {
|
|
901
1221
|
this.paths = paths;
|
|
@@ -1110,55 +1430,122 @@ class SqlegoClient {
|
|
|
1110
1430
|
if (this.options.spawn === false) {
|
|
1111
1431
|
return Result.err({ kind: "not-running", message: `no sqlego server for ${this.paths.file}` });
|
|
1112
1432
|
}
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1433
|
+
for (;; ) {
|
|
1434
|
+
const claimed = this.claimLock();
|
|
1435
|
+
if (!claimed.ok)
|
|
1436
|
+
return claimed;
|
|
1437
|
+
if (claimed.value) {
|
|
1438
|
+
const placed = await this.placeFile(deadline);
|
|
1439
|
+
if (!placed.ok) {
|
|
1440
|
+
this.releaseSpawnLock();
|
|
1441
|
+
const released = this.releaseLock();
|
|
1442
|
+
const suffix = !released.ok && released.error.kind === "lock-failed" ? `; lock not released: ${released.error.message}` : "";
|
|
1443
|
+
return Result.err({ kind: placed.error.kind, message: `${placed.error.message}${suffix}` });
|
|
1444
|
+
}
|
|
1122
1445
|
}
|
|
1446
|
+
const waited = await this.waitForSocket(deadline, !claimed.value);
|
|
1447
|
+
if (!waited.ok)
|
|
1448
|
+
return waited;
|
|
1449
|
+
if (waited.value)
|
|
1450
|
+
return Result.ok(waited.value);
|
|
1123
1451
|
}
|
|
1452
|
+
}
|
|
1453
|
+
static CLAIM_RECHECK_MS = 100;
|
|
1454
|
+
async waitForSocket(deadline, watchClaim) {
|
|
1455
|
+
let rechecked = Date.now();
|
|
1124
1456
|
for (let delayMs = 2;Date.now() < deadline; delayMs = Math.min(25, delayMs * 2)) {
|
|
1125
1457
|
const socket = await tryConnect(this.paths.socket);
|
|
1126
1458
|
if (socket) {
|
|
1127
|
-
this.
|
|
1459
|
+
this.settleLeases();
|
|
1128
1460
|
return Result.ok(socket);
|
|
1129
1461
|
}
|
|
1462
|
+
if (watchClaim && Date.now() - rechecked >= SqlegoClient.CLAIM_RECHECK_MS) {
|
|
1463
|
+
rechecked = Date.now();
|
|
1464
|
+
if (Trust.decide(leaseHolder(this.paths.lease), { "lease-failed": () => -1 }).value === null)
|
|
1465
|
+
return Result.ok(null);
|
|
1466
|
+
}
|
|
1130
1467
|
await sleep(delayMs);
|
|
1131
1468
|
}
|
|
1132
|
-
this.
|
|
1469
|
+
this.settleLeases();
|
|
1133
1470
|
const cause = this.spawnFailure ? ` (${this.spawnFailure})` : "";
|
|
1134
1471
|
return Result.err({ kind: "connect-timeout", message: `no sqlego server became reachable for ${this.paths.file}${cause}` });
|
|
1135
1472
|
}
|
|
1136
1473
|
claimLock() {
|
|
1137
|
-
|
|
1474
|
+
const taken = this.takeClaim();
|
|
1475
|
+
if (!taken.ok)
|
|
1476
|
+
return taken;
|
|
1477
|
+
this.claim = taken.value;
|
|
1478
|
+
return Result.ok(taken.value !== null);
|
|
1479
|
+
}
|
|
1480
|
+
takeClaim() {
|
|
1481
|
+
const acquired = acquireLease(this.paths.lease);
|
|
1482
|
+
if (!acquired.ok)
|
|
1483
|
+
return Result.err({ kind: "lock-failed", message: `cannot claim ${this.paths.file}: ${acquired.error.message}` });
|
|
1484
|
+
const lease = acquired.value;
|
|
1485
|
+
if (!lease)
|
|
1486
|
+
return Result.ok(null);
|
|
1487
|
+
const legacy = takeLegacyLock(this.paths.lock, process.pid);
|
|
1488
|
+
if (legacy.ok && legacy.value)
|
|
1489
|
+
return Result.ok(lease);
|
|
1490
|
+
const released = releaseLease(lease);
|
|
1491
|
+
if (!released.ok && released.error.kind === "lease-failed") {}
|
|
1492
|
+
if (!legacy.ok)
|
|
1493
|
+
return Result.err({ kind: "lock-failed", message: `cannot claim ${this.paths.file}: ${legacy.error.message}` });
|
|
1494
|
+
return Result.ok(null);
|
|
1495
|
+
}
|
|
1496
|
+
followClaim() {
|
|
1497
|
+
const claim = this.claim;
|
|
1498
|
+
if (!claim)
|
|
1499
|
+
return Result.ok("own");
|
|
1500
|
+
if (Trust.decide(leaseHead(claim.base), { "lease-failed": () => 0 }).value <= claim.generation)
|
|
1501
|
+
return Result.ok("own");
|
|
1502
|
+
const holder = leaseHolder(claim.base);
|
|
1503
|
+
if (!holder.ok)
|
|
1504
|
+
return Result.err({ kind: "lock-failed", message: `cannot read the claim on ${this.paths.file}: ${holder.error.message}` });
|
|
1505
|
+
if (holder.value !== null) {
|
|
1506
|
+
this.claim = null;
|
|
1507
|
+
return Result.ok("placed");
|
|
1508
|
+
}
|
|
1509
|
+
const again = this.takeClaim();
|
|
1510
|
+
if (!again.ok)
|
|
1511
|
+
return Result.err({ kind: "lock-failed", message: `cannot claim ${this.paths.file} again: ${again.error.message}` });
|
|
1512
|
+
this.claim = again.value;
|
|
1513
|
+
return Result.ok(again.value === null ? "placed" : "own");
|
|
1138
1514
|
}
|
|
1139
1515
|
ownerBudget() {
|
|
1140
1516
|
const configured = this.options.maxOwners ?? Number(process.env.SQLEGO_MAX_OWNERS ?? DEFAULT_MAX_OWNERS);
|
|
1141
1517
|
return Number.isFinite(configured) && configured >= 0 ? Math.floor(configured) : DEFAULT_MAX_OWNERS;
|
|
1142
1518
|
}
|
|
1143
|
-
spawnLockPath() {
|
|
1144
|
-
return path4.join(path4.dirname(this.paths.lock), "spawn.lock");
|
|
1145
|
-
}
|
|
1146
1519
|
async placeFile(deadline) {
|
|
1147
1520
|
const budget = this.ownerBudget();
|
|
1148
1521
|
let lastRefusal = null;
|
|
1522
|
+
const refusal = () => lastRefusal ? `: ${lastRefusal}` : "";
|
|
1149
1523
|
while (Date.now() < deadline) {
|
|
1524
|
+
const standing = this.followClaim();
|
|
1525
|
+
if (!standing.ok)
|
|
1526
|
+
return Result.err({ kind: standing.error.kind, message: `${standing.error.message}${refusal()}` });
|
|
1527
|
+
if (standing.value === "placed")
|
|
1528
|
+
return Result.ok(undefined);
|
|
1150
1529
|
const owners = this.currentOwners();
|
|
1151
1530
|
if (!owners.ok)
|
|
1152
1531
|
return owners;
|
|
1153
1532
|
if (budget === 0 || owners.value.length < budget) {
|
|
1154
1533
|
if (budget === 0)
|
|
1155
1534
|
return this.spawnServer();
|
|
1156
|
-
const claimed =
|
|
1535
|
+
const claimed = acquireLease(spawnLeaseBase(path5.dirname(this.paths.lease)));
|
|
1157
1536
|
if (!claimed.ok)
|
|
1158
|
-
return claimed;
|
|
1537
|
+
return Result.err({ kind: "lock-failed", message: `cannot reserve a spawn: ${claimed.error.message}` });
|
|
1159
1538
|
if (claimed.value) {
|
|
1160
|
-
this.
|
|
1161
|
-
|
|
1539
|
+
this.spawnLease = claimed.value;
|
|
1540
|
+
const settled = this.currentOwners();
|
|
1541
|
+
if (!settled.ok) {
|
|
1542
|
+
this.releaseSpawnLock();
|
|
1543
|
+
return settled;
|
|
1544
|
+
}
|
|
1545
|
+
if (settled.value.length < budget)
|
|
1546
|
+
return this.spawnServer();
|
|
1547
|
+
this.releaseSpawnLock();
|
|
1548
|
+
continue;
|
|
1162
1549
|
}
|
|
1163
1550
|
await sleep(25);
|
|
1164
1551
|
continue;
|
|
@@ -1171,28 +1558,33 @@ class SqlegoClient {
|
|
|
1171
1558
|
op: "adopt",
|
|
1172
1559
|
file: this.paths.file,
|
|
1173
1560
|
pragmas: this.options.serverPragmas,
|
|
1174
|
-
idleTimeoutMs: this.options.idleTimeoutMs ?? 30000
|
|
1561
|
+
idleTimeoutMs: this.options.idleTimeoutMs ?? 30000,
|
|
1562
|
+
claim: this.claim ? encodeLease(this.claim) : undefined
|
|
1175
1563
|
}, parseAdoptResult, Math.min(ADOPT_TIMEOUT_MS, remaining));
|
|
1176
1564
|
if (adopted.ok)
|
|
1177
1565
|
return Result.ok(undefined);
|
|
1178
1566
|
if (adopted.error.kind === "probe-failed")
|
|
1179
1567
|
lastRefusal = `owner ${owner.pid}: ${adopted.error.message}`;
|
|
1568
|
+
const after = this.followClaim();
|
|
1569
|
+
if (!after.ok)
|
|
1570
|
+
return Result.err({ kind: after.error.kind, message: `${after.error.message}${refusal()}` });
|
|
1571
|
+
if (after.value === "placed")
|
|
1572
|
+
return Result.ok(undefined);
|
|
1180
1573
|
}
|
|
1181
1574
|
await sleep(25);
|
|
1182
1575
|
}
|
|
1183
|
-
|
|
1184
|
-
return Result.err({ kind: "connect-timeout", message: `no sqlego owner took on ${this.paths.file}${why}` });
|
|
1576
|
+
return Result.err({ kind: "connect-timeout", message: `no sqlego owner took on ${this.paths.file}${refusal()}` });
|
|
1185
1577
|
}
|
|
1186
1578
|
currentOwners() {
|
|
1187
|
-
const directory =
|
|
1188
|
-
const names = Trust.try(() =>
|
|
1579
|
+
const directory = path5.dirname(this.paths.lock);
|
|
1580
|
+
const names = Trust.try(() => readdirSync3(directory), (cause) => ({ kind: "runtime-directory", message: `cannot list sqlego owners in ${directory}: ${errorMessage(cause)}` }));
|
|
1189
1581
|
if (!names.ok)
|
|
1190
1582
|
return names;
|
|
1191
1583
|
const byPid = new Map;
|
|
1192
1584
|
for (const name of names.value) {
|
|
1193
1585
|
if (!name.endsWith(".json"))
|
|
1194
1586
|
continue;
|
|
1195
|
-
const text = Trust.try(() =>
|
|
1587
|
+
const text = Trust.try(() => readFileSync4(path5.join(directory, name), "utf8"), () => ({ kind: "invalid", message: "unreadable" }));
|
|
1196
1588
|
const json = text.ok ? fromJson(text.value) : text;
|
|
1197
1589
|
const meta = json.ok ? parseServerMeta(json.value) : json;
|
|
1198
1590
|
if (meta.ok) {
|
|
@@ -1211,13 +1603,20 @@ class SqlegoClient {
|
|
|
1211
1603
|
return Result.ok(owners);
|
|
1212
1604
|
}
|
|
1213
1605
|
releaseSpawnLock() {
|
|
1214
|
-
|
|
1606
|
+
const lease = this.spawnLease;
|
|
1607
|
+
this.spawnLease = null;
|
|
1608
|
+
if (!lease)
|
|
1215
1609
|
return;
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
if (!released.ok && released.error.kind === "lock-failed")
|
|
1610
|
+
const released = releaseLease(lease);
|
|
1611
|
+
if (!released.ok && released.error.kind === "lease-failed")
|
|
1219
1612
|
this.spawnFailure = `spawn lock not released: ${released.error.message}`;
|
|
1220
1613
|
}
|
|
1614
|
+
settleLeases() {
|
|
1615
|
+
this.releaseSpawnLock();
|
|
1616
|
+
const released = this.releaseLock();
|
|
1617
|
+
if (!released.ok && released.error.kind === "lock-failed")
|
|
1618
|
+
this.spawnFailure = `claim not released: ${released.error.message}`;
|
|
1619
|
+
}
|
|
1221
1620
|
async waitForRelease(deadline) {
|
|
1222
1621
|
for (let delayMs = 2;Date.now() < deadline; delayMs = Math.min(25, delayMs * 2)) {
|
|
1223
1622
|
const socket = await tryConnect(this.paths.socket);
|
|
@@ -1233,12 +1632,19 @@ class SqlegoClient {
|
|
|
1233
1632
|
return resolved;
|
|
1234
1633
|
const [command, ...args] = resolved.value;
|
|
1235
1634
|
const pragmaArgs = (this.options.serverPragmas ?? []).flatMap((pragma) => ["--pragma", pragma]);
|
|
1236
|
-
|
|
1635
|
+
const env = { ...process.env };
|
|
1636
|
+
delete env.SQLEGO_CLAIM_LEASE;
|
|
1637
|
+
delete env.SQLEGO_SPAWN_LEASE;
|
|
1638
|
+
if (this.claim)
|
|
1639
|
+
env.SQLEGO_CLAIM_LEASE = encodeLease(this.claim);
|
|
1640
|
+
if (this.spawnLease)
|
|
1641
|
+
env.SQLEGO_SPAWN_LEASE = encodeLease(this.spawnLease);
|
|
1642
|
+
const spawned = Trust.try(() => {
|
|
1237
1643
|
const child = spawn(command, [...args, "serve", this.paths.file, "--idle-ms", String(this.options.idleTimeoutMs ?? 30000), ...pragmaArgs], {
|
|
1238
1644
|
detached: true,
|
|
1239
1645
|
windowsHide: true,
|
|
1240
1646
|
stdio: ["ignore", "ignore", "pipe"],
|
|
1241
|
-
env
|
|
1647
|
+
env
|
|
1242
1648
|
});
|
|
1243
1649
|
let stderr = "";
|
|
1244
1650
|
child.stderr?.on("data", (chunk) => {
|
|
@@ -1255,16 +1661,36 @@ class SqlegoClient {
|
|
|
1255
1661
|
this.spawnFailure = `server exited before listening (code ${code}, signal ${signal})${stderr ? `: ${stderr.trim()}` : ""}`;
|
|
1256
1662
|
});
|
|
1257
1663
|
child.unref();
|
|
1664
|
+
return child.pid;
|
|
1258
1665
|
}, (cause) => ({ kind: "spawn-failed", message: `spawn ${command} failed: ${errorMessage(cause)}` }));
|
|
1666
|
+
if (!spawned.ok)
|
|
1667
|
+
return spawned;
|
|
1668
|
+
return Result.ok(undefined);
|
|
1259
1669
|
}
|
|
1260
1670
|
onSpawnError(command, error) {
|
|
1671
|
+
this.releaseSpawnLock();
|
|
1261
1672
|
this.spawnFailure = `spawn ${command} failed: ${error.message}`;
|
|
1262
1673
|
const released = this.releaseLock();
|
|
1263
1674
|
if (!released.ok && released.error.kind === "lock-failed")
|
|
1264
1675
|
this.spawnFailure += `; lock not released: ${released.error.message}`;
|
|
1265
1676
|
}
|
|
1266
1677
|
releaseLock() {
|
|
1267
|
-
|
|
1678
|
+
const claim = this.claim;
|
|
1679
|
+
this.claim = null;
|
|
1680
|
+
if (!claim)
|
|
1681
|
+
return Result.ok(undefined);
|
|
1682
|
+
const releasing = handOverLease(claim, process.pid);
|
|
1683
|
+
if (!releasing.ok)
|
|
1684
|
+
return Result.err({ kind: "lock-failed", message: releasing.error.message });
|
|
1685
|
+
if (!releasing.value)
|
|
1686
|
+
return Result.ok(undefined);
|
|
1687
|
+
const dropped = dropLegacyLock(this.paths.lock, [process.pid]);
|
|
1688
|
+
const released = releaseLease(releasing.value);
|
|
1689
|
+
if (!dropped.ok)
|
|
1690
|
+
return Result.err({ kind: "lock-failed", message: dropped.error.message });
|
|
1691
|
+
if (!released.ok)
|
|
1692
|
+
return Result.err({ kind: "lock-failed", message: released.error.message });
|
|
1693
|
+
return Result.ok(undefined);
|
|
1268
1694
|
}
|
|
1269
1695
|
attach(socket) {
|
|
1270
1696
|
this.socket = socket;
|
|
@@ -1347,30 +1773,6 @@ class SqlegoClient {
|
|
|
1347
1773
|
this.pending.clear();
|
|
1348
1774
|
}
|
|
1349
1775
|
}
|
|
1350
|
-
function claimLockAt(lock) {
|
|
1351
|
-
for (let attempt = 0;attempt < 2; attempt += 1) {
|
|
1352
|
-
const created = Trust.try(() => {
|
|
1353
|
-
const fd = openSync(lock, "wx");
|
|
1354
|
-
writeSync(fd, String(process.pid));
|
|
1355
|
-
closeSync(fd);
|
|
1356
|
-
}, (cause) => errnoCode(cause) === "EEXIST" ? { kind: "lock-held", message: `${lock} exists` } : { kind: "lock-failed", message: `cannot create ${lock}: ${errorMessage(cause)}` });
|
|
1357
|
-
if (created.ok)
|
|
1358
|
-
return Result.ok(true);
|
|
1359
|
-
if (created.error.kind === "lock-failed")
|
|
1360
|
-
return Result.err({ kind: "lock-failed", message: created.error.message });
|
|
1361
|
-
if (created.error.kind === "lock-held") {
|
|
1362
|
-
const holder = Trust.try(() => Number(readFileSync2(lock, "utf8").trim()), (cause) => ({ kind: "lock-unreadable", message: errorMessage(cause) }));
|
|
1363
|
-
if (holder.ok) {
|
|
1364
|
-
if (Number.isFinite(holder.value) && holder.value > 0 && isProcessAlive(holder.value))
|
|
1365
|
-
return Result.ok(false);
|
|
1366
|
-
} else if (holder.error.kind === "lock-unreadable") {}
|
|
1367
|
-
const removed = Trust.try(() => rmSync(lock, { force: true }), (cause) => ({ kind: "lock-failed", message: `cannot remove stale ${lock}: ${errorMessage(cause)}` }));
|
|
1368
|
-
if (!removed.ok)
|
|
1369
|
-
return removed;
|
|
1370
|
-
}
|
|
1371
|
-
}
|
|
1372
|
-
return Result.ok(false);
|
|
1373
|
-
}
|
|
1374
1776
|
|
|
1375
1777
|
class SqlegoTransaction {
|
|
1376
1778
|
id;
|
|
@@ -1426,7 +1828,7 @@ function resolveServerCommand() {
|
|
|
1426
1828
|
const configured = process.env.SQLEGO_BIN;
|
|
1427
1829
|
if (configured && configured.length > 0)
|
|
1428
1830
|
return Result.ok([configured]);
|
|
1429
|
-
const located = Trust.try(() =>
|
|
1831
|
+
const located = Trust.try(() => path5.dirname(fileURLToPath2(import.meta.url)), (cause) => ({ kind: "module-path", message: errorMessage(cause) }));
|
|
1430
1832
|
let here = null;
|
|
1431
1833
|
if (located.ok)
|
|
1432
1834
|
here = located.value;
|
|
@@ -1434,12 +1836,12 @@ function resolveServerCommand() {
|
|
|
1434
1836
|
here = null;
|
|
1435
1837
|
if (process.versions.bun) {
|
|
1436
1838
|
const entry = process.argv[1] ?? "";
|
|
1437
|
-
if (
|
|
1839
|
+
if (path5.basename(process.execPath).startsWith("sqlego"))
|
|
1438
1840
|
return Result.ok([process.execPath]);
|
|
1439
1841
|
if (entry.endsWith("cli.ts"))
|
|
1440
1842
|
return Result.ok([process.execPath, entry]);
|
|
1441
1843
|
if (entry.endsWith(".ts") && here !== null) {
|
|
1442
|
-
const cli =
|
|
1844
|
+
const cli = path5.join(here, "cli.ts");
|
|
1443
1845
|
if (existsSync3(cli))
|
|
1444
1846
|
return Result.ok([process.execPath, cli]);
|
|
1445
1847
|
}
|
|
@@ -1450,7 +1852,7 @@ function resolveServerCommand() {
|
|
|
1450
1852
|
configured: undefined,
|
|
1451
1853
|
resolvePackageJson: (name) => resolvePackageJson(name, here),
|
|
1452
1854
|
exists: existsSync3,
|
|
1453
|
-
localBinDirectory: here === null ? null :
|
|
1855
|
+
localBinDirectory: here === null ? null : path5.join(here, "..", "bin")
|
|
1454
1856
|
});
|
|
1455
1857
|
if (!binary.ok)
|
|
1456
1858
|
return binary;
|
|
@@ -1465,7 +1867,7 @@ function defaultServerCommand() {
|
|
|
1465
1867
|
function resolvePackageJson(name, here) {
|
|
1466
1868
|
if (here === null)
|
|
1467
1869
|
return null;
|
|
1468
|
-
const resolved = Trust.try(() => createRequire(
|
|
1870
|
+
const resolved = Trust.try(() => createRequire(path5.join(here, "index.js")).resolve(`${name}/package.json`), (cause) => ({ kind: "not-installed", message: errorMessage(cause) }));
|
|
1469
1871
|
if (resolved.ok)
|
|
1470
1872
|
return typeof resolved.value === "string" ? resolved.value : null;
|
|
1471
1873
|
if (resolved.error.kind === "not-installed")
|