@noego/sqlego 0.1.6-dev.1 → 0.1.6-dev.11
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 +72 -15
- package/dist/client.d.ts +66 -9
- package/dist/index.js +529 -116
- 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 +11 -8
- package/src/cli.ts +121 -2
- package/src/client.ts +240 -117
- 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,12 +1,12 @@
|
|
|
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
|
-
// node_modules/@noego/trust/dist/index.js
|
|
9
|
+
// node_modules/.pnpm/@noego+trust@0.5.1_typescript@5.9.3/node_modules/@noego/trust/dist/index.js
|
|
10
10
|
function ok(value) {
|
|
11
11
|
return { ok: true, value };
|
|
12
12
|
}
|
|
@@ -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.11",
|
|
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",
|
|
@@ -799,44 +1071,90 @@ var package_default = {
|
|
|
799
1071
|
"build:bin": "bun scripts/build.ts bin",
|
|
800
1072
|
"build:bin:all": "bun scripts/build.ts bin:all",
|
|
801
1073
|
release: "bun scripts/release.ts",
|
|
802
|
-
typecheck: "
|
|
1074
|
+
typecheck: "tsc --noEmit",
|
|
803
1075
|
trust: "trust check src",
|
|
804
1076
|
test: "bun test"
|
|
805
1077
|
},
|
|
806
1078
|
devDependencies: {
|
|
807
1079
|
"@types/bun": "^1.2.0",
|
|
808
1080
|
"@types/node": "^22.0.0",
|
|
1081
|
+
"bun-types": "^1.4.4",
|
|
809
1082
|
typescript: "^5.6.0"
|
|
810
1083
|
},
|
|
811
1084
|
dependencies: {
|
|
812
1085
|
"@noego/trust": "^0.5.1"
|
|
813
1086
|
},
|
|
814
1087
|
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
|
-
}
|
|
1088
|
+
"@noego/sqlego-darwin-arm64": "0.1.6-dev.11",
|
|
1089
|
+
"@noego/sqlego-darwin-x64": "0.1.6-dev.11",
|
|
1090
|
+
"@noego/sqlego-linux-x64": "0.1.6-dev.11",
|
|
1091
|
+
"@noego/sqlego-linux-arm64": "0.1.6-dev.11",
|
|
1092
|
+
"@noego/sqlego-win32-x64": "0.1.6-dev.11"
|
|
1093
|
+
},
|
|
1094
|
+
packageManager: "pnpm@10.34.9",
|
|
1095
|
+
sqlegoBuild: "382c13027eae"
|
|
821
1096
|
};
|
|
822
1097
|
|
|
823
1098
|
// src/version.ts
|
|
1099
|
+
var RUST_BUILD_FILES = ["rust/Cargo.toml", "rust/Cargo.lock", "rust/build.rs"];
|
|
824
1100
|
function sourceBuildId(sourceDirectory) {
|
|
825
1101
|
if (!existsSync2(sourceDirectory))
|
|
826
1102
|
return Result.ok("source");
|
|
827
1103
|
return Trust.try(() => {
|
|
828
1104
|
const hash = createHash2("sha1");
|
|
829
|
-
for (const name of
|
|
1105
|
+
for (const name of readdirSync2(sourceDirectory).filter((entry) => entry.endsWith(".ts")).sort()) {
|
|
830
1106
|
hash.update(name);
|
|
831
|
-
hash.update(
|
|
1107
|
+
hash.update(readFileSync3(path4.join(sourceDirectory, name)));
|
|
1108
|
+
}
|
|
1109
|
+
const root = path4.dirname(sourceDirectory);
|
|
1110
|
+
if (existsSync2(path4.join(root, "rust", "Cargo.toml"))) {
|
|
1111
|
+
const files = RUST_BUILD_FILES.filter((file) => existsSync2(path4.join(root, file)));
|
|
1112
|
+
const rustSources = path4.join(root, "rust", "src");
|
|
1113
|
+
if (existsSync2(rustSources)) {
|
|
1114
|
+
for (const entry of readdirSync2(rustSources, { recursive: true, withFileTypes: true })) {
|
|
1115
|
+
if (!entry.isFile())
|
|
1116
|
+
continue;
|
|
1117
|
+
const relative = path4.relative(root, path4.join(entry.parentPath, entry.name));
|
|
1118
|
+
files.push(relative.split(path4.sep).join("/"));
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
for (const file of files.sort()) {
|
|
1122
|
+
hash.update(file);
|
|
1123
|
+
hash.update(readFileSync3(path4.join(root, file)));
|
|
1124
|
+
}
|
|
832
1125
|
}
|
|
833
1126
|
return hash.digest("hex").slice(0, 12);
|
|
834
1127
|
}, (cause) => ({ kind: "source-unreadable", message: `cannot hash sources in ${sourceDirectory}: ${errorMessage(cause)}` }));
|
|
835
1128
|
}
|
|
836
|
-
function
|
|
837
|
-
if ("
|
|
838
|
-
return
|
|
839
|
-
|
|
1129
|
+
function chooseBuildId(defined, stamped, rustInTree, fromSource) {
|
|
1130
|
+
if (typeof defined === "string" && defined.length > 0)
|
|
1131
|
+
return defined;
|
|
1132
|
+
if (!rustInTree && typeof stamped === "string" && stamped.length > 0)
|
|
1133
|
+
return stamped;
|
|
1134
|
+
return fromSource();
|
|
1135
|
+
}
|
|
1136
|
+
var stampedBuild = package_default.sqlegoBuild;
|
|
1137
|
+
function moduleDirectory() {
|
|
1138
|
+
return Trust.try(() => path4.dirname(fileURLToPath(import.meta.url)), (cause) => ({ kind: "module-path", message: errorMessage(cause) }));
|
|
1139
|
+
}
|
|
1140
|
+
function rustInTree() {
|
|
1141
|
+
const directory = moduleDirectory();
|
|
1142
|
+
if (!directory.ok)
|
|
1143
|
+
return decideNoTree(directory.error.kind);
|
|
1144
|
+
return existsSync2(path4.join(directory.value, "..", "rust", "Cargo.toml"));
|
|
1145
|
+
}
|
|
1146
|
+
function decideNoTree(kind) {
|
|
1147
|
+
switch (kind) {
|
|
1148
|
+
case "module-path":
|
|
1149
|
+
return false;
|
|
1150
|
+
default: {
|
|
1151
|
+
const unhandled = kind;
|
|
1152
|
+
return unhandled;
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
function buildIdFromSource() {
|
|
1157
|
+
const directory = moduleDirectory();
|
|
840
1158
|
if (!directory.ok)
|
|
841
1159
|
return decideUnknownBuild(directory.error.kind);
|
|
842
1160
|
const id = sourceBuildId(directory.value);
|
|
@@ -844,6 +1162,9 @@ function resolveBuildId() {
|
|
|
844
1162
|
return decideUnknownBuild(id.error.kind);
|
|
845
1163
|
return id.value;
|
|
846
1164
|
}
|
|
1165
|
+
function resolveBuildId() {
|
|
1166
|
+
return chooseBuildId("382c13027eae", stampedBuild, rustInTree(), buildIdFromSource);
|
|
1167
|
+
}
|
|
847
1168
|
function decideUnknownBuild(kind) {
|
|
848
1169
|
switch (kind) {
|
|
849
1170
|
case "module-path":
|
|
@@ -895,7 +1216,8 @@ class SqlegoClient {
|
|
|
895
1216
|
hello = null;
|
|
896
1217
|
mismatches = [];
|
|
897
1218
|
spawnFailure = null;
|
|
898
|
-
|
|
1219
|
+
claim = null;
|
|
1220
|
+
spawnLease = null;
|
|
899
1221
|
options;
|
|
900
1222
|
constructor(paths, options) {
|
|
901
1223
|
this.paths = paths;
|
|
@@ -932,22 +1254,20 @@ class SqlegoClient {
|
|
|
932
1254
|
}, parseHelloResult));
|
|
933
1255
|
return this.hello.capabilities.includes("detached-plain");
|
|
934
1256
|
}
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
return callerRows(result.rows);
|
|
1257
|
+
query(sql, params) {
|
|
1258
|
+
return this.call({ op: "query", sql, params }, parseQueryResult).then((reply) => callerRows(unwrap(reply).rows));
|
|
938
1259
|
}
|
|
939
|
-
|
|
940
|
-
return
|
|
1260
|
+
run(sql, params) {
|
|
1261
|
+
return this.call({ op: "run", sql, params }, parseRunResult).then((reply) => unwrap(reply));
|
|
941
1262
|
}
|
|
942
|
-
|
|
943
|
-
return
|
|
1263
|
+
statement(sql, params) {
|
|
1264
|
+
return this.call({ op: "statement", sql, params }, parseStatementResult).then((reply) => callerStatement(unwrap(reply)));
|
|
944
1265
|
}
|
|
945
|
-
|
|
946
|
-
return
|
|
1266
|
+
exec(sql) {
|
|
1267
|
+
return this.call({ op: "exec", sql }, parseExecResult).then((reply) => unwrap(reply));
|
|
947
1268
|
}
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
return result.results;
|
|
1269
|
+
batch(statements) {
|
|
1270
|
+
return this.call({ op: "batch", statements }, parseBatchResult).then((reply) => unwrap(reply).results);
|
|
951
1271
|
}
|
|
952
1272
|
async begin(options = {}) {
|
|
953
1273
|
const opened = unwrap(await this.call({ op: "begin", mode: options.mode, leaseMs: options.leaseMs }, parseBeginResult));
|
|
@@ -965,15 +1285,14 @@ class SqlegoClient {
|
|
|
965
1285
|
});
|
|
966
1286
|
throw outcome.error.cause;
|
|
967
1287
|
}
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
return callerRows(result.rows);
|
|
1288
|
+
transactionQuery(txId, sql, params) {
|
|
1289
|
+
return this.call({ op: "query", sql, params, txId }, parseQueryResult).then((reply) => callerRows(unwrap(reply).rows));
|
|
971
1290
|
}
|
|
972
|
-
|
|
973
|
-
return
|
|
1291
|
+
transactionRun(txId, sql, params) {
|
|
1292
|
+
return this.call({ op: "run", sql, params, txId }, parseRunResult).then((reply) => unwrap(reply));
|
|
974
1293
|
}
|
|
975
|
-
|
|
976
|
-
return
|
|
1294
|
+
transactionStatement(txId, sql, params) {
|
|
1295
|
+
return this.call({ op: "statement", sql, params, txId }, parseStatementResult).then((reply) => callerStatement(unwrap(reply)));
|
|
977
1296
|
}
|
|
978
1297
|
async transactionExec(txId, sql) {
|
|
979
1298
|
return unwrap(await this.call({ op: "exec", sql, txId }, parseExecResult));
|
|
@@ -1110,55 +1429,122 @@ class SqlegoClient {
|
|
|
1110
1429
|
if (this.options.spawn === false) {
|
|
1111
1430
|
return Result.err({ kind: "not-running", message: `no sqlego server for ${this.paths.file}` });
|
|
1112
1431
|
}
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1432
|
+
for (;; ) {
|
|
1433
|
+
const claimed = this.claimLock();
|
|
1434
|
+
if (!claimed.ok)
|
|
1435
|
+
return claimed;
|
|
1436
|
+
if (claimed.value) {
|
|
1437
|
+
const placed = await this.placeFile(deadline);
|
|
1438
|
+
if (!placed.ok) {
|
|
1439
|
+
this.releaseSpawnLock();
|
|
1440
|
+
const released = this.releaseLock();
|
|
1441
|
+
const suffix = !released.ok && released.error.kind === "lock-failed" ? `; lock not released: ${released.error.message}` : "";
|
|
1442
|
+
return Result.err({ kind: placed.error.kind, message: `${placed.error.message}${suffix}` });
|
|
1443
|
+
}
|
|
1122
1444
|
}
|
|
1445
|
+
const waited = await this.waitForSocket(deadline, !claimed.value);
|
|
1446
|
+
if (!waited.ok)
|
|
1447
|
+
return waited;
|
|
1448
|
+
if (waited.value)
|
|
1449
|
+
return Result.ok(waited.value);
|
|
1123
1450
|
}
|
|
1451
|
+
}
|
|
1452
|
+
static CLAIM_RECHECK_MS = 100;
|
|
1453
|
+
async waitForSocket(deadline, watchClaim) {
|
|
1454
|
+
let rechecked = Date.now();
|
|
1124
1455
|
for (let delayMs = 2;Date.now() < deadline; delayMs = Math.min(25, delayMs * 2)) {
|
|
1125
1456
|
const socket = await tryConnect(this.paths.socket);
|
|
1126
1457
|
if (socket) {
|
|
1127
|
-
this.
|
|
1458
|
+
this.settleLeases();
|
|
1128
1459
|
return Result.ok(socket);
|
|
1129
1460
|
}
|
|
1461
|
+
if (watchClaim && Date.now() - rechecked >= SqlegoClient.CLAIM_RECHECK_MS) {
|
|
1462
|
+
rechecked = Date.now();
|
|
1463
|
+
if (Trust.decide(leaseHolder(this.paths.lease), { "lease-failed": () => -1 }).value === null)
|
|
1464
|
+
return Result.ok(null);
|
|
1465
|
+
}
|
|
1130
1466
|
await sleep(delayMs);
|
|
1131
1467
|
}
|
|
1132
|
-
this.
|
|
1468
|
+
this.settleLeases();
|
|
1133
1469
|
const cause = this.spawnFailure ? ` (${this.spawnFailure})` : "";
|
|
1134
1470
|
return Result.err({ kind: "connect-timeout", message: `no sqlego server became reachable for ${this.paths.file}${cause}` });
|
|
1135
1471
|
}
|
|
1136
1472
|
claimLock() {
|
|
1137
|
-
|
|
1473
|
+
const taken = this.takeClaim();
|
|
1474
|
+
if (!taken.ok)
|
|
1475
|
+
return taken;
|
|
1476
|
+
this.claim = taken.value;
|
|
1477
|
+
return Result.ok(taken.value !== null);
|
|
1478
|
+
}
|
|
1479
|
+
takeClaim() {
|
|
1480
|
+
const acquired = acquireLease(this.paths.lease);
|
|
1481
|
+
if (!acquired.ok)
|
|
1482
|
+
return Result.err({ kind: "lock-failed", message: `cannot claim ${this.paths.file}: ${acquired.error.message}` });
|
|
1483
|
+
const lease = acquired.value;
|
|
1484
|
+
if (!lease)
|
|
1485
|
+
return Result.ok(null);
|
|
1486
|
+
const legacy = takeLegacyLock(this.paths.lock, process.pid);
|
|
1487
|
+
if (legacy.ok && legacy.value)
|
|
1488
|
+
return Result.ok(lease);
|
|
1489
|
+
const released = releaseLease(lease);
|
|
1490
|
+
if (!released.ok && released.error.kind === "lease-failed") {}
|
|
1491
|
+
if (!legacy.ok)
|
|
1492
|
+
return Result.err({ kind: "lock-failed", message: `cannot claim ${this.paths.file}: ${legacy.error.message}` });
|
|
1493
|
+
return Result.ok(null);
|
|
1494
|
+
}
|
|
1495
|
+
followClaim() {
|
|
1496
|
+
const claim = this.claim;
|
|
1497
|
+
if (!claim)
|
|
1498
|
+
return Result.ok("own");
|
|
1499
|
+
if (Trust.decide(leaseHead(claim.base), { "lease-failed": () => 0 }).value <= claim.generation)
|
|
1500
|
+
return Result.ok("own");
|
|
1501
|
+
const holder = leaseHolder(claim.base);
|
|
1502
|
+
if (!holder.ok)
|
|
1503
|
+
return Result.err({ kind: "lock-failed", message: `cannot read the claim on ${this.paths.file}: ${holder.error.message}` });
|
|
1504
|
+
if (holder.value !== null) {
|
|
1505
|
+
this.claim = null;
|
|
1506
|
+
return Result.ok("placed");
|
|
1507
|
+
}
|
|
1508
|
+
const again = this.takeClaim();
|
|
1509
|
+
if (!again.ok)
|
|
1510
|
+
return Result.err({ kind: "lock-failed", message: `cannot claim ${this.paths.file} again: ${again.error.message}` });
|
|
1511
|
+
this.claim = again.value;
|
|
1512
|
+
return Result.ok(again.value === null ? "placed" : "own");
|
|
1138
1513
|
}
|
|
1139
1514
|
ownerBudget() {
|
|
1140
1515
|
const configured = this.options.maxOwners ?? Number(process.env.SQLEGO_MAX_OWNERS ?? DEFAULT_MAX_OWNERS);
|
|
1141
1516
|
return Number.isFinite(configured) && configured >= 0 ? Math.floor(configured) : DEFAULT_MAX_OWNERS;
|
|
1142
1517
|
}
|
|
1143
|
-
spawnLockPath() {
|
|
1144
|
-
return path4.join(path4.dirname(this.paths.lock), "spawn.lock");
|
|
1145
|
-
}
|
|
1146
1518
|
async placeFile(deadline) {
|
|
1147
1519
|
const budget = this.ownerBudget();
|
|
1148
1520
|
let lastRefusal = null;
|
|
1521
|
+
const refusal = () => lastRefusal ? `: ${lastRefusal}` : "";
|
|
1149
1522
|
while (Date.now() < deadline) {
|
|
1523
|
+
const standing = this.followClaim();
|
|
1524
|
+
if (!standing.ok)
|
|
1525
|
+
return Result.err({ kind: standing.error.kind, message: `${standing.error.message}${refusal()}` });
|
|
1526
|
+
if (standing.value === "placed")
|
|
1527
|
+
return Result.ok(undefined);
|
|
1150
1528
|
const owners = this.currentOwners();
|
|
1151
1529
|
if (!owners.ok)
|
|
1152
1530
|
return owners;
|
|
1153
1531
|
if (budget === 0 || owners.value.length < budget) {
|
|
1154
1532
|
if (budget === 0)
|
|
1155
1533
|
return this.spawnServer();
|
|
1156
|
-
const claimed =
|
|
1534
|
+
const claimed = acquireLease(spawnLeaseBase(path5.dirname(this.paths.lease)));
|
|
1157
1535
|
if (!claimed.ok)
|
|
1158
|
-
return claimed;
|
|
1536
|
+
return Result.err({ kind: "lock-failed", message: `cannot reserve a spawn: ${claimed.error.message}` });
|
|
1159
1537
|
if (claimed.value) {
|
|
1160
|
-
this.
|
|
1161
|
-
|
|
1538
|
+
this.spawnLease = claimed.value;
|
|
1539
|
+
const settled = this.currentOwners();
|
|
1540
|
+
if (!settled.ok) {
|
|
1541
|
+
this.releaseSpawnLock();
|
|
1542
|
+
return settled;
|
|
1543
|
+
}
|
|
1544
|
+
if (settled.value.length < budget)
|
|
1545
|
+
return this.spawnServer();
|
|
1546
|
+
this.releaseSpawnLock();
|
|
1547
|
+
continue;
|
|
1162
1548
|
}
|
|
1163
1549
|
await sleep(25);
|
|
1164
1550
|
continue;
|
|
@@ -1171,28 +1557,33 @@ class SqlegoClient {
|
|
|
1171
1557
|
op: "adopt",
|
|
1172
1558
|
file: this.paths.file,
|
|
1173
1559
|
pragmas: this.options.serverPragmas,
|
|
1174
|
-
idleTimeoutMs: this.options.idleTimeoutMs ?? 30000
|
|
1560
|
+
idleTimeoutMs: this.options.idleTimeoutMs ?? 30000,
|
|
1561
|
+
claim: this.claim ? encodeLease(this.claim) : undefined
|
|
1175
1562
|
}, parseAdoptResult, Math.min(ADOPT_TIMEOUT_MS, remaining));
|
|
1176
1563
|
if (adopted.ok)
|
|
1177
1564
|
return Result.ok(undefined);
|
|
1178
1565
|
if (adopted.error.kind === "probe-failed")
|
|
1179
1566
|
lastRefusal = `owner ${owner.pid}: ${adopted.error.message}`;
|
|
1567
|
+
const after = this.followClaim();
|
|
1568
|
+
if (!after.ok)
|
|
1569
|
+
return Result.err({ kind: after.error.kind, message: `${after.error.message}${refusal()}` });
|
|
1570
|
+
if (after.value === "placed")
|
|
1571
|
+
return Result.ok(undefined);
|
|
1180
1572
|
}
|
|
1181
1573
|
await sleep(25);
|
|
1182
1574
|
}
|
|
1183
|
-
|
|
1184
|
-
return Result.err({ kind: "connect-timeout", message: `no sqlego owner took on ${this.paths.file}${why}` });
|
|
1575
|
+
return Result.err({ kind: "connect-timeout", message: `no sqlego owner took on ${this.paths.file}${refusal()}` });
|
|
1185
1576
|
}
|
|
1186
1577
|
currentOwners() {
|
|
1187
|
-
const directory =
|
|
1188
|
-
const names = Trust.try(() =>
|
|
1578
|
+
const directory = path5.dirname(this.paths.lock);
|
|
1579
|
+
const names = Trust.try(() => readdirSync3(directory), (cause) => ({ kind: "runtime-directory", message: `cannot list sqlego owners in ${directory}: ${errorMessage(cause)}` }));
|
|
1189
1580
|
if (!names.ok)
|
|
1190
1581
|
return names;
|
|
1191
1582
|
const byPid = new Map;
|
|
1192
1583
|
for (const name of names.value) {
|
|
1193
1584
|
if (!name.endsWith(".json"))
|
|
1194
1585
|
continue;
|
|
1195
|
-
const text = Trust.try(() =>
|
|
1586
|
+
const text = Trust.try(() => readFileSync4(path5.join(directory, name), "utf8"), () => ({ kind: "invalid", message: "unreadable" }));
|
|
1196
1587
|
const json = text.ok ? fromJson(text.value) : text;
|
|
1197
1588
|
const meta = json.ok ? parseServerMeta(json.value) : json;
|
|
1198
1589
|
if (meta.ok) {
|
|
@@ -1211,13 +1602,20 @@ class SqlegoClient {
|
|
|
1211
1602
|
return Result.ok(owners);
|
|
1212
1603
|
}
|
|
1213
1604
|
releaseSpawnLock() {
|
|
1214
|
-
|
|
1605
|
+
const lease = this.spawnLease;
|
|
1606
|
+
this.spawnLease = null;
|
|
1607
|
+
if (!lease)
|
|
1215
1608
|
return;
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
if (!released.ok && released.error.kind === "lock-failed")
|
|
1609
|
+
const released = releaseLease(lease);
|
|
1610
|
+
if (!released.ok && released.error.kind === "lease-failed")
|
|
1219
1611
|
this.spawnFailure = `spawn lock not released: ${released.error.message}`;
|
|
1220
1612
|
}
|
|
1613
|
+
settleLeases() {
|
|
1614
|
+
this.releaseSpawnLock();
|
|
1615
|
+
const released = this.releaseLock();
|
|
1616
|
+
if (!released.ok && released.error.kind === "lock-failed")
|
|
1617
|
+
this.spawnFailure = `claim not released: ${released.error.message}`;
|
|
1618
|
+
}
|
|
1221
1619
|
async waitForRelease(deadline) {
|
|
1222
1620
|
for (let delayMs = 2;Date.now() < deadline; delayMs = Math.min(25, delayMs * 2)) {
|
|
1223
1621
|
const socket = await tryConnect(this.paths.socket);
|
|
@@ -1233,12 +1631,19 @@ class SqlegoClient {
|
|
|
1233
1631
|
return resolved;
|
|
1234
1632
|
const [command, ...args] = resolved.value;
|
|
1235
1633
|
const pragmaArgs = (this.options.serverPragmas ?? []).flatMap((pragma) => ["--pragma", pragma]);
|
|
1236
|
-
|
|
1634
|
+
const env = { ...process.env };
|
|
1635
|
+
delete env.SQLEGO_CLAIM_LEASE;
|
|
1636
|
+
delete env.SQLEGO_SPAWN_LEASE;
|
|
1637
|
+
if (this.claim)
|
|
1638
|
+
env.SQLEGO_CLAIM_LEASE = encodeLease(this.claim);
|
|
1639
|
+
if (this.spawnLease)
|
|
1640
|
+
env.SQLEGO_SPAWN_LEASE = encodeLease(this.spawnLease);
|
|
1641
|
+
const spawned = Trust.try(() => {
|
|
1237
1642
|
const child = spawn(command, [...args, "serve", this.paths.file, "--idle-ms", String(this.options.idleTimeoutMs ?? 30000), ...pragmaArgs], {
|
|
1238
1643
|
detached: true,
|
|
1239
1644
|
windowsHide: true,
|
|
1240
1645
|
stdio: ["ignore", "ignore", "pipe"],
|
|
1241
|
-
env
|
|
1646
|
+
env
|
|
1242
1647
|
});
|
|
1243
1648
|
let stderr = "";
|
|
1244
1649
|
child.stderr?.on("data", (chunk) => {
|
|
@@ -1255,16 +1660,36 @@ class SqlegoClient {
|
|
|
1255
1660
|
this.spawnFailure = `server exited before listening (code ${code}, signal ${signal})${stderr ? `: ${stderr.trim()}` : ""}`;
|
|
1256
1661
|
});
|
|
1257
1662
|
child.unref();
|
|
1663
|
+
return child.pid;
|
|
1258
1664
|
}, (cause) => ({ kind: "spawn-failed", message: `spawn ${command} failed: ${errorMessage(cause)}` }));
|
|
1665
|
+
if (!spawned.ok)
|
|
1666
|
+
return spawned;
|
|
1667
|
+
return Result.ok(undefined);
|
|
1259
1668
|
}
|
|
1260
1669
|
onSpawnError(command, error) {
|
|
1670
|
+
this.releaseSpawnLock();
|
|
1261
1671
|
this.spawnFailure = `spawn ${command} failed: ${error.message}`;
|
|
1262
1672
|
const released = this.releaseLock();
|
|
1263
1673
|
if (!released.ok && released.error.kind === "lock-failed")
|
|
1264
1674
|
this.spawnFailure += `; lock not released: ${released.error.message}`;
|
|
1265
1675
|
}
|
|
1266
1676
|
releaseLock() {
|
|
1267
|
-
|
|
1677
|
+
const claim = this.claim;
|
|
1678
|
+
this.claim = null;
|
|
1679
|
+
if (!claim)
|
|
1680
|
+
return Result.ok(undefined);
|
|
1681
|
+
const releasing = handOverLease(claim, process.pid);
|
|
1682
|
+
if (!releasing.ok)
|
|
1683
|
+
return Result.err({ kind: "lock-failed", message: releasing.error.message });
|
|
1684
|
+
if (!releasing.value)
|
|
1685
|
+
return Result.ok(undefined);
|
|
1686
|
+
const dropped = dropLegacyLock(this.paths.lock, [process.pid]);
|
|
1687
|
+
const released = releaseLease(releasing.value);
|
|
1688
|
+
if (!dropped.ok)
|
|
1689
|
+
return Result.err({ kind: "lock-failed", message: dropped.error.message });
|
|
1690
|
+
if (!released.ok)
|
|
1691
|
+
return Result.err({ kind: "lock-failed", message: released.error.message });
|
|
1692
|
+
return Result.ok(undefined);
|
|
1268
1693
|
}
|
|
1269
1694
|
attach(socket) {
|
|
1270
1695
|
this.socket = socket;
|
|
@@ -1303,15 +1728,18 @@ class SqlegoClient {
|
|
|
1303
1728
|
waiter.settle(response.ok ? Result.ok(response.result) : Result.err({ kind: response.error.code, message: response.error.message }));
|
|
1304
1729
|
}
|
|
1305
1730
|
}
|
|
1306
|
-
|
|
1307
|
-
const
|
|
1731
|
+
call(body, parse, timeoutMs) {
|
|
1732
|
+
const op = body.op;
|
|
1733
|
+
return this.request(body, timeoutMs).then((reply) => this.parseReply(op, reply, parse));
|
|
1734
|
+
}
|
|
1735
|
+
parseReply(op, reply, parse) {
|
|
1308
1736
|
if (!reply.ok)
|
|
1309
1737
|
return reply;
|
|
1310
1738
|
const parsed = parse(reply.value);
|
|
1311
1739
|
if (parsed.ok)
|
|
1312
1740
|
return parsed;
|
|
1313
1741
|
if (parsed.error.kind === "invalid") {
|
|
1314
|
-
return Result.err({ kind: "protocol", message: `unexpected ${
|
|
1742
|
+
return Result.err({ kind: "protocol", message: `unexpected ${op} reply: ${parsed.error.message}` });
|
|
1315
1743
|
}
|
|
1316
1744
|
const unhandled = parsed.error.kind;
|
|
1317
1745
|
return unhandled;
|
|
@@ -1325,20 +1753,29 @@ class SqlegoClient {
|
|
|
1325
1753
|
const frame = encodeRequestFrame({ id, ...body });
|
|
1326
1754
|
if (!frame.ok)
|
|
1327
1755
|
return Promise.resolve(frame);
|
|
1756
|
+
const reply = this.register(id, body.op, timeoutMs);
|
|
1757
|
+
const written = Trust.try(() => socket.write(frame.value), (cause) => ({ kind: "socket", message: errorMessage(cause) }));
|
|
1758
|
+
if (!written.ok) {
|
|
1759
|
+
this.forget(id);
|
|
1760
|
+
return Promise.resolve(written);
|
|
1761
|
+
}
|
|
1762
|
+
return reply;
|
|
1763
|
+
}
|
|
1764
|
+
register(id, op, timeoutMs) {
|
|
1328
1765
|
return new Promise((settle) => {
|
|
1329
1766
|
const timer = setTimeout(() => {
|
|
1330
1767
|
this.pending.delete(id);
|
|
1331
|
-
settle(Result.err({ kind: "request-timeout", message: `request ${id} (${
|
|
1768
|
+
settle(Result.err({ kind: "request-timeout", message: `request ${id} (${op}) timed out` }));
|
|
1332
1769
|
}, timeoutMs);
|
|
1333
1770
|
this.pending.set(id, { settle, timer });
|
|
1334
|
-
const written = Trust.try(() => socket.write(frame.value), (cause) => ({ kind: "socket", message: errorMessage(cause) }));
|
|
1335
|
-
if (!written.ok) {
|
|
1336
|
-
this.pending.delete(id);
|
|
1337
|
-
clearTimeout(timer);
|
|
1338
|
-
settle(written);
|
|
1339
|
-
}
|
|
1340
1771
|
});
|
|
1341
1772
|
}
|
|
1773
|
+
forget(id) {
|
|
1774
|
+
const waiter = this.pending.get(id);
|
|
1775
|
+
if (waiter)
|
|
1776
|
+
clearTimeout(waiter.timer);
|
|
1777
|
+
this.pending.delete(id);
|
|
1778
|
+
}
|
|
1342
1779
|
settleAll(failure) {
|
|
1343
1780
|
for (const waiter of this.pending.values()) {
|
|
1344
1781
|
clearTimeout(waiter.timer);
|
|
@@ -1347,30 +1784,6 @@ class SqlegoClient {
|
|
|
1347
1784
|
this.pending.clear();
|
|
1348
1785
|
}
|
|
1349
1786
|
}
|
|
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
1787
|
|
|
1375
1788
|
class SqlegoTransaction {
|
|
1376
1789
|
id;
|
|
@@ -1426,7 +1839,7 @@ function resolveServerCommand() {
|
|
|
1426
1839
|
const configured = process.env.SQLEGO_BIN;
|
|
1427
1840
|
if (configured && configured.length > 0)
|
|
1428
1841
|
return Result.ok([configured]);
|
|
1429
|
-
const located = Trust.try(() =>
|
|
1842
|
+
const located = Trust.try(() => path5.dirname(fileURLToPath2(import.meta.url)), (cause) => ({ kind: "module-path", message: errorMessage(cause) }));
|
|
1430
1843
|
let here = null;
|
|
1431
1844
|
if (located.ok)
|
|
1432
1845
|
here = located.value;
|
|
@@ -1434,12 +1847,12 @@ function resolveServerCommand() {
|
|
|
1434
1847
|
here = null;
|
|
1435
1848
|
if (process.versions.bun) {
|
|
1436
1849
|
const entry = process.argv[1] ?? "";
|
|
1437
|
-
if (
|
|
1850
|
+
if (path5.basename(process.execPath).startsWith("sqlego"))
|
|
1438
1851
|
return Result.ok([process.execPath]);
|
|
1439
1852
|
if (entry.endsWith("cli.ts"))
|
|
1440
1853
|
return Result.ok([process.execPath, entry]);
|
|
1441
1854
|
if (entry.endsWith(".ts") && here !== null) {
|
|
1442
|
-
const cli =
|
|
1855
|
+
const cli = path5.join(here, "cli.ts");
|
|
1443
1856
|
if (existsSync3(cli))
|
|
1444
1857
|
return Result.ok([process.execPath, cli]);
|
|
1445
1858
|
}
|
|
@@ -1450,7 +1863,7 @@ function resolveServerCommand() {
|
|
|
1450
1863
|
configured: undefined,
|
|
1451
1864
|
resolvePackageJson: (name) => resolvePackageJson(name, here),
|
|
1452
1865
|
exists: existsSync3,
|
|
1453
|
-
localBinDirectory: here === null ? null :
|
|
1866
|
+
localBinDirectory: here === null ? null : path5.join(here, "..", "bin")
|
|
1454
1867
|
});
|
|
1455
1868
|
if (!binary.ok)
|
|
1456
1869
|
return binary;
|
|
@@ -1465,7 +1878,7 @@ function defaultServerCommand() {
|
|
|
1465
1878
|
function resolvePackageJson(name, here) {
|
|
1466
1879
|
if (here === null)
|
|
1467
1880
|
return null;
|
|
1468
|
-
const resolved = Trust.try(() => createRequire(
|
|
1881
|
+
const resolved = Trust.try(() => createRequire(path5.join(here, "index.js")).resolve(`${name}/package.json`), (cause) => ({ kind: "not-installed", message: errorMessage(cause) }));
|
|
1469
1882
|
if (resolved.ok)
|
|
1470
1883
|
return typeof resolved.value === "string" ? resolved.value : null;
|
|
1471
1884
|
if (resolved.error.kind === "not-installed")
|