@noego/sqlego 0.1.6-dev.4 → 0.1.6-dev.6
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 +64 -9
- package/dist/client.d.ts +47 -9
- package/dist/index.js +414 -75
- 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/package.json +7 -7
- package/src/cli.ts +121 -2
- package/src/client.ts +152 -74
- 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/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.6",
|
|
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 all",
|
|
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,11 +1084,11 @@ 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.
|
|
1087
|
+
"@noego/sqlego-darwin-arm64": "0.1.6-dev.6",
|
|
1088
|
+
"@noego/sqlego-darwin-x64": "0.1.6-dev.6",
|
|
1089
|
+
"@noego/sqlego-linux-x64": "0.1.6-dev.6",
|
|
1090
|
+
"@noego/sqlego-linux-arm64": "0.1.6-dev.6",
|
|
1091
|
+
"@noego/sqlego-win32-x64": "0.1.6-dev.6"
|
|
820
1092
|
}
|
|
821
1093
|
};
|
|
822
1094
|
|
|
@@ -826,17 +1098,17 @@ function sourceBuildId(sourceDirectory) {
|
|
|
826
1098
|
return Result.ok("source");
|
|
827
1099
|
return Trust.try(() => {
|
|
828
1100
|
const hash = createHash2("sha1");
|
|
829
|
-
for (const name of
|
|
1101
|
+
for (const name of readdirSync2(sourceDirectory).filter((entry) => entry.endsWith(".ts")).sort()) {
|
|
830
1102
|
hash.update(name);
|
|
831
|
-
hash.update(
|
|
1103
|
+
hash.update(readFileSync3(path4.join(sourceDirectory, name)));
|
|
832
1104
|
}
|
|
833
1105
|
return hash.digest("hex").slice(0, 12);
|
|
834
1106
|
}, (cause) => ({ kind: "source-unreadable", message: `cannot hash sources in ${sourceDirectory}: ${errorMessage(cause)}` }));
|
|
835
1107
|
}
|
|
836
1108
|
function resolveBuildId() {
|
|
837
|
-
if ("
|
|
838
|
-
return "
|
|
839
|
-
const directory = Trust.try(() =>
|
|
1109
|
+
if ("d3f7347e657e".length > 0)
|
|
1110
|
+
return "d3f7347e657e";
|
|
1111
|
+
const directory = Trust.try(() => path4.dirname(fileURLToPath(import.meta.url)), (cause) => ({ kind: "module-path", message: errorMessage(cause) }));
|
|
840
1112
|
if (!directory.ok)
|
|
841
1113
|
return decideUnknownBuild(directory.error.kind);
|
|
842
1114
|
const id = sourceBuildId(directory.value);
|
|
@@ -895,7 +1167,8 @@ class SqlegoClient {
|
|
|
895
1167
|
hello = null;
|
|
896
1168
|
mismatches = [];
|
|
897
1169
|
spawnFailure = null;
|
|
898
|
-
|
|
1170
|
+
claim = null;
|
|
1171
|
+
spawnLease = null;
|
|
899
1172
|
options;
|
|
900
1173
|
constructor(paths, options) {
|
|
901
1174
|
this.paths = paths;
|
|
@@ -1116,6 +1389,7 @@ class SqlegoClient {
|
|
|
1116
1389
|
if (claimed.value) {
|
|
1117
1390
|
const placed = await this.placeFile(deadline);
|
|
1118
1391
|
if (!placed.ok) {
|
|
1392
|
+
this.releaseSpawnLock();
|
|
1119
1393
|
const released = this.releaseLock();
|
|
1120
1394
|
const suffix = !released.ok && released.error.kind === "lock-failed" ? `; lock not released: ${released.error.message}` : "";
|
|
1121
1395
|
return Result.err({ kind: placed.error.kind, message: `${placed.error.message}${suffix}` });
|
|
@@ -1124,41 +1398,91 @@ class SqlegoClient {
|
|
|
1124
1398
|
for (let delayMs = 2;Date.now() < deadline; delayMs = Math.min(25, delayMs * 2)) {
|
|
1125
1399
|
const socket = await tryConnect(this.paths.socket);
|
|
1126
1400
|
if (socket) {
|
|
1127
|
-
this.
|
|
1401
|
+
this.settleLeases();
|
|
1128
1402
|
return Result.ok(socket);
|
|
1129
1403
|
}
|
|
1130
1404
|
await sleep(delayMs);
|
|
1131
1405
|
}
|
|
1132
|
-
this.
|
|
1406
|
+
this.settleLeases();
|
|
1133
1407
|
const cause = this.spawnFailure ? ` (${this.spawnFailure})` : "";
|
|
1134
1408
|
return Result.err({ kind: "connect-timeout", message: `no sqlego server became reachable for ${this.paths.file}${cause}` });
|
|
1135
1409
|
}
|
|
1136
1410
|
claimLock() {
|
|
1137
|
-
|
|
1411
|
+
const taken = this.takeClaim();
|
|
1412
|
+
if (!taken.ok)
|
|
1413
|
+
return taken;
|
|
1414
|
+
this.claim = taken.value;
|
|
1415
|
+
return Result.ok(taken.value !== null);
|
|
1416
|
+
}
|
|
1417
|
+
takeClaim() {
|
|
1418
|
+
const acquired = acquireLease(this.paths.lease);
|
|
1419
|
+
if (!acquired.ok)
|
|
1420
|
+
return Result.err({ kind: "lock-failed", message: `cannot claim ${this.paths.file}: ${acquired.error.message}` });
|
|
1421
|
+
const lease = acquired.value;
|
|
1422
|
+
if (!lease)
|
|
1423
|
+
return Result.ok(null);
|
|
1424
|
+
const legacy = takeLegacyLock(this.paths.lock, process.pid);
|
|
1425
|
+
if (legacy.ok && legacy.value)
|
|
1426
|
+
return Result.ok(lease);
|
|
1427
|
+
const released = releaseLease(lease);
|
|
1428
|
+
if (!released.ok && released.error.kind === "lease-failed") {}
|
|
1429
|
+
if (!legacy.ok)
|
|
1430
|
+
return Result.err({ kind: "lock-failed", message: `cannot claim ${this.paths.file}: ${legacy.error.message}` });
|
|
1431
|
+
return Result.ok(null);
|
|
1432
|
+
}
|
|
1433
|
+
followClaim() {
|
|
1434
|
+
const claim = this.claim;
|
|
1435
|
+
if (!claim)
|
|
1436
|
+
return Result.ok("own");
|
|
1437
|
+
if (Trust.decide(leaseHead(claim.base), { "lease-failed": () => 0 }).value <= claim.generation)
|
|
1438
|
+
return Result.ok("own");
|
|
1439
|
+
const holder = leaseHolder(claim.base);
|
|
1440
|
+
if (!holder.ok)
|
|
1441
|
+
return Result.err({ kind: "lock-failed", message: `cannot read the claim on ${this.paths.file}: ${holder.error.message}` });
|
|
1442
|
+
if (holder.value !== null) {
|
|
1443
|
+
this.claim = null;
|
|
1444
|
+
return Result.ok("placed");
|
|
1445
|
+
}
|
|
1446
|
+
const again = this.takeClaim();
|
|
1447
|
+
if (!again.ok)
|
|
1448
|
+
return Result.err({ kind: "lock-failed", message: `cannot claim ${this.paths.file} again: ${again.error.message}` });
|
|
1449
|
+
this.claim = again.value;
|
|
1450
|
+
return Result.ok(again.value === null ? "placed" : "own");
|
|
1138
1451
|
}
|
|
1139
1452
|
ownerBudget() {
|
|
1140
1453
|
const configured = this.options.maxOwners ?? Number(process.env.SQLEGO_MAX_OWNERS ?? DEFAULT_MAX_OWNERS);
|
|
1141
1454
|
return Number.isFinite(configured) && configured >= 0 ? Math.floor(configured) : DEFAULT_MAX_OWNERS;
|
|
1142
1455
|
}
|
|
1143
|
-
spawnLockPath() {
|
|
1144
|
-
return path4.join(path4.dirname(this.paths.lock), "spawn.lock");
|
|
1145
|
-
}
|
|
1146
1456
|
async placeFile(deadline) {
|
|
1147
1457
|
const budget = this.ownerBudget();
|
|
1148
1458
|
let lastRefusal = null;
|
|
1459
|
+
const refusal = () => lastRefusal ? `: ${lastRefusal}` : "";
|
|
1149
1460
|
while (Date.now() < deadline) {
|
|
1461
|
+
const standing = this.followClaim();
|
|
1462
|
+
if (!standing.ok)
|
|
1463
|
+
return Result.err({ kind: standing.error.kind, message: `${standing.error.message}${refusal()}` });
|
|
1464
|
+
if (standing.value === "placed")
|
|
1465
|
+
return Result.ok(undefined);
|
|
1150
1466
|
const owners = this.currentOwners();
|
|
1151
1467
|
if (!owners.ok)
|
|
1152
1468
|
return owners;
|
|
1153
1469
|
if (budget === 0 || owners.value.length < budget) {
|
|
1154
1470
|
if (budget === 0)
|
|
1155
1471
|
return this.spawnServer();
|
|
1156
|
-
const claimed =
|
|
1472
|
+
const claimed = acquireLease(spawnLeaseBase(path5.dirname(this.paths.lease)));
|
|
1157
1473
|
if (!claimed.ok)
|
|
1158
|
-
return claimed;
|
|
1474
|
+
return Result.err({ kind: "lock-failed", message: `cannot reserve a spawn: ${claimed.error.message}` });
|
|
1159
1475
|
if (claimed.value) {
|
|
1160
|
-
this.
|
|
1161
|
-
|
|
1476
|
+
this.spawnLease = claimed.value;
|
|
1477
|
+
const settled = this.currentOwners();
|
|
1478
|
+
if (!settled.ok) {
|
|
1479
|
+
this.releaseSpawnLock();
|
|
1480
|
+
return settled;
|
|
1481
|
+
}
|
|
1482
|
+
if (settled.value.length < budget)
|
|
1483
|
+
return this.spawnServer();
|
|
1484
|
+
this.releaseSpawnLock();
|
|
1485
|
+
continue;
|
|
1162
1486
|
}
|
|
1163
1487
|
await sleep(25);
|
|
1164
1488
|
continue;
|
|
@@ -1171,28 +1495,33 @@ class SqlegoClient {
|
|
|
1171
1495
|
op: "adopt",
|
|
1172
1496
|
file: this.paths.file,
|
|
1173
1497
|
pragmas: this.options.serverPragmas,
|
|
1174
|
-
idleTimeoutMs: this.options.idleTimeoutMs ?? 30000
|
|
1498
|
+
idleTimeoutMs: this.options.idleTimeoutMs ?? 30000,
|
|
1499
|
+
claim: this.claim ? encodeLease(this.claim) : undefined
|
|
1175
1500
|
}, parseAdoptResult, Math.min(ADOPT_TIMEOUT_MS, remaining));
|
|
1176
1501
|
if (adopted.ok)
|
|
1177
1502
|
return Result.ok(undefined);
|
|
1178
1503
|
if (adopted.error.kind === "probe-failed")
|
|
1179
1504
|
lastRefusal = `owner ${owner.pid}: ${adopted.error.message}`;
|
|
1505
|
+
const after = this.followClaim();
|
|
1506
|
+
if (!after.ok)
|
|
1507
|
+
return Result.err({ kind: after.error.kind, message: `${after.error.message}${refusal()}` });
|
|
1508
|
+
if (after.value === "placed")
|
|
1509
|
+
return Result.ok(undefined);
|
|
1180
1510
|
}
|
|
1181
1511
|
await sleep(25);
|
|
1182
1512
|
}
|
|
1183
|
-
|
|
1184
|
-
return Result.err({ kind: "connect-timeout", message: `no sqlego owner took on ${this.paths.file}${why}` });
|
|
1513
|
+
return Result.err({ kind: "connect-timeout", message: `no sqlego owner took on ${this.paths.file}${refusal()}` });
|
|
1185
1514
|
}
|
|
1186
1515
|
currentOwners() {
|
|
1187
|
-
const directory =
|
|
1188
|
-
const names = Trust.try(() =>
|
|
1516
|
+
const directory = path5.dirname(this.paths.lock);
|
|
1517
|
+
const names = Trust.try(() => readdirSync3(directory), (cause) => ({ kind: "runtime-directory", message: `cannot list sqlego owners in ${directory}: ${errorMessage(cause)}` }));
|
|
1189
1518
|
if (!names.ok)
|
|
1190
1519
|
return names;
|
|
1191
1520
|
const byPid = new Map;
|
|
1192
1521
|
for (const name of names.value) {
|
|
1193
1522
|
if (!name.endsWith(".json"))
|
|
1194
1523
|
continue;
|
|
1195
|
-
const text = Trust.try(() =>
|
|
1524
|
+
const text = Trust.try(() => readFileSync4(path5.join(directory, name), "utf8"), () => ({ kind: "invalid", message: "unreadable" }));
|
|
1196
1525
|
const json = text.ok ? fromJson(text.value) : text;
|
|
1197
1526
|
const meta = json.ok ? parseServerMeta(json.value) : json;
|
|
1198
1527
|
if (meta.ok) {
|
|
@@ -1211,13 +1540,20 @@ class SqlegoClient {
|
|
|
1211
1540
|
return Result.ok(owners);
|
|
1212
1541
|
}
|
|
1213
1542
|
releaseSpawnLock() {
|
|
1214
|
-
|
|
1543
|
+
const lease = this.spawnLease;
|
|
1544
|
+
this.spawnLease = null;
|
|
1545
|
+
if (!lease)
|
|
1215
1546
|
return;
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
if (!released.ok && released.error.kind === "lock-failed")
|
|
1547
|
+
const released = releaseLease(lease);
|
|
1548
|
+
if (!released.ok && released.error.kind === "lease-failed")
|
|
1219
1549
|
this.spawnFailure = `spawn lock not released: ${released.error.message}`;
|
|
1220
1550
|
}
|
|
1551
|
+
settleLeases() {
|
|
1552
|
+
this.releaseSpawnLock();
|
|
1553
|
+
const released = this.releaseLock();
|
|
1554
|
+
if (!released.ok && released.error.kind === "lock-failed")
|
|
1555
|
+
this.spawnFailure = `claim not released: ${released.error.message}`;
|
|
1556
|
+
}
|
|
1221
1557
|
async waitForRelease(deadline) {
|
|
1222
1558
|
for (let delayMs = 2;Date.now() < deadline; delayMs = Math.min(25, delayMs * 2)) {
|
|
1223
1559
|
const socket = await tryConnect(this.paths.socket);
|
|
@@ -1233,12 +1569,19 @@ class SqlegoClient {
|
|
|
1233
1569
|
return resolved;
|
|
1234
1570
|
const [command, ...args] = resolved.value;
|
|
1235
1571
|
const pragmaArgs = (this.options.serverPragmas ?? []).flatMap((pragma) => ["--pragma", pragma]);
|
|
1236
|
-
|
|
1572
|
+
const env = { ...process.env };
|
|
1573
|
+
delete env.SQLEGO_CLAIM_LEASE;
|
|
1574
|
+
delete env.SQLEGO_SPAWN_LEASE;
|
|
1575
|
+
if (this.claim)
|
|
1576
|
+
env.SQLEGO_CLAIM_LEASE = encodeLease(this.claim);
|
|
1577
|
+
if (this.spawnLease)
|
|
1578
|
+
env.SQLEGO_SPAWN_LEASE = encodeLease(this.spawnLease);
|
|
1579
|
+
const spawned = Trust.try(() => {
|
|
1237
1580
|
const child = spawn(command, [...args, "serve", this.paths.file, "--idle-ms", String(this.options.idleTimeoutMs ?? 30000), ...pragmaArgs], {
|
|
1238
1581
|
detached: true,
|
|
1239
1582
|
windowsHide: true,
|
|
1240
1583
|
stdio: ["ignore", "ignore", "pipe"],
|
|
1241
|
-
env
|
|
1584
|
+
env
|
|
1242
1585
|
});
|
|
1243
1586
|
let stderr = "";
|
|
1244
1587
|
child.stderr?.on("data", (chunk) => {
|
|
@@ -1255,16 +1598,36 @@ class SqlegoClient {
|
|
|
1255
1598
|
this.spawnFailure = `server exited before listening (code ${code}, signal ${signal})${stderr ? `: ${stderr.trim()}` : ""}`;
|
|
1256
1599
|
});
|
|
1257
1600
|
child.unref();
|
|
1601
|
+
return child.pid;
|
|
1258
1602
|
}, (cause) => ({ kind: "spawn-failed", message: `spawn ${command} failed: ${errorMessage(cause)}` }));
|
|
1603
|
+
if (!spawned.ok)
|
|
1604
|
+
return spawned;
|
|
1605
|
+
return Result.ok(undefined);
|
|
1259
1606
|
}
|
|
1260
1607
|
onSpawnError(command, error) {
|
|
1608
|
+
this.releaseSpawnLock();
|
|
1261
1609
|
this.spawnFailure = `spawn ${command} failed: ${error.message}`;
|
|
1262
1610
|
const released = this.releaseLock();
|
|
1263
1611
|
if (!released.ok && released.error.kind === "lock-failed")
|
|
1264
1612
|
this.spawnFailure += `; lock not released: ${released.error.message}`;
|
|
1265
1613
|
}
|
|
1266
1614
|
releaseLock() {
|
|
1267
|
-
|
|
1615
|
+
const claim = this.claim;
|
|
1616
|
+
this.claim = null;
|
|
1617
|
+
if (!claim)
|
|
1618
|
+
return Result.ok(undefined);
|
|
1619
|
+
const releasing = handOverLease(claim, process.pid);
|
|
1620
|
+
if (!releasing.ok)
|
|
1621
|
+
return Result.err({ kind: "lock-failed", message: releasing.error.message });
|
|
1622
|
+
if (!releasing.value)
|
|
1623
|
+
return Result.ok(undefined);
|
|
1624
|
+
const dropped = dropLegacyLock(this.paths.lock, [process.pid]);
|
|
1625
|
+
const released = releaseLease(releasing.value);
|
|
1626
|
+
if (!dropped.ok)
|
|
1627
|
+
return Result.err({ kind: "lock-failed", message: dropped.error.message });
|
|
1628
|
+
if (!released.ok)
|
|
1629
|
+
return Result.err({ kind: "lock-failed", message: released.error.message });
|
|
1630
|
+
return Result.ok(undefined);
|
|
1268
1631
|
}
|
|
1269
1632
|
attach(socket) {
|
|
1270
1633
|
this.socket = socket;
|
|
@@ -1347,30 +1710,6 @@ class SqlegoClient {
|
|
|
1347
1710
|
this.pending.clear();
|
|
1348
1711
|
}
|
|
1349
1712
|
}
|
|
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
1713
|
|
|
1375
1714
|
class SqlegoTransaction {
|
|
1376
1715
|
id;
|
|
@@ -1426,7 +1765,7 @@ function resolveServerCommand() {
|
|
|
1426
1765
|
const configured = process.env.SQLEGO_BIN;
|
|
1427
1766
|
if (configured && configured.length > 0)
|
|
1428
1767
|
return Result.ok([configured]);
|
|
1429
|
-
const located = Trust.try(() =>
|
|
1768
|
+
const located = Trust.try(() => path5.dirname(fileURLToPath2(import.meta.url)), (cause) => ({ kind: "module-path", message: errorMessage(cause) }));
|
|
1430
1769
|
let here = null;
|
|
1431
1770
|
if (located.ok)
|
|
1432
1771
|
here = located.value;
|
|
@@ -1434,12 +1773,12 @@ function resolveServerCommand() {
|
|
|
1434
1773
|
here = null;
|
|
1435
1774
|
if (process.versions.bun) {
|
|
1436
1775
|
const entry = process.argv[1] ?? "";
|
|
1437
|
-
if (
|
|
1776
|
+
if (path5.basename(process.execPath).startsWith("sqlego"))
|
|
1438
1777
|
return Result.ok([process.execPath]);
|
|
1439
1778
|
if (entry.endsWith("cli.ts"))
|
|
1440
1779
|
return Result.ok([process.execPath, entry]);
|
|
1441
1780
|
if (entry.endsWith(".ts") && here !== null) {
|
|
1442
|
-
const cli =
|
|
1781
|
+
const cli = path5.join(here, "cli.ts");
|
|
1443
1782
|
if (existsSync3(cli))
|
|
1444
1783
|
return Result.ok([process.execPath, cli]);
|
|
1445
1784
|
}
|
|
@@ -1450,7 +1789,7 @@ function resolveServerCommand() {
|
|
|
1450
1789
|
configured: undefined,
|
|
1451
1790
|
resolvePackageJson: (name) => resolvePackageJson(name, here),
|
|
1452
1791
|
exists: existsSync3,
|
|
1453
|
-
localBinDirectory: here === null ? null :
|
|
1792
|
+
localBinDirectory: here === null ? null : path5.join(here, "..", "bin")
|
|
1454
1793
|
});
|
|
1455
1794
|
if (!binary.ok)
|
|
1456
1795
|
return binary;
|
|
@@ -1465,7 +1804,7 @@ function defaultServerCommand() {
|
|
|
1465
1804
|
function resolvePackageJson(name, here) {
|
|
1466
1805
|
if (here === null)
|
|
1467
1806
|
return null;
|
|
1468
|
-
const resolved = Trust.try(() => createRequire(
|
|
1807
|
+
const resolved = Trust.try(() => createRequire(path5.join(here, "index.js")).resolve(`${name}/package.json`), (cause) => ({ kind: "not-installed", message: errorMessage(cause) }));
|
|
1469
1808
|
if (resolved.ok)
|
|
1470
1809
|
return typeof resolved.value === "string" ? resolved.value : null;
|
|
1471
1810
|
if (resolved.error.kind === "not-installed")
|