@noego/sqlego 0.1.6-dev.6 → 0.1.6-dev.7
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 +9 -64
- package/dist/client.d.ts +9 -47
- package/dist/index.js +75 -414
- package/dist/paths.d.ts +0 -6
- package/dist/protocol.d.ts +2 -28
- package/package.json +7 -7
- package/src/cli.ts +2 -121
- package/src/client.ts +74 -152
- package/src/paths.ts +0 -4
- package/src/protocol.ts +6 -56
- package/src/server.ts +30 -355
- package/dist/lease.d.ts +0 -39
- package/dist/legacy_lock.d.ts +0 -20
- package/src/lease.ts +0 -265
- package/src/legacy_lock.ts +0 -123
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// src/client.ts
|
|
2
2
|
import { spawn } from "node:child_process";
|
|
3
|
-
import { readdirSync as
|
|
3
|
+
import { closeSync, openSync, readdirSync as readdirSync2, readFileSync as readFileSync2, rmSync, writeSync, existsSync as existsSync3 } from "node:fs";
|
|
4
4
|
import { createRequire } from "node:module";
|
|
5
5
|
import net from "node:net";
|
|
6
|
-
import
|
|
6
|
+
import path4 from "node:path";
|
|
7
7
|
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
8
8
|
|
|
9
9
|
// node_modules/@noego/trust/dist/index.js
|
|
@@ -90,16 +90,6 @@ 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
|
-
|
|
103
93
|
// src/protocol.ts
|
|
104
94
|
var PROTOCOL_VERSION = 3;
|
|
105
95
|
var INT64_MIN = -9223372036854775808n;
|
|
@@ -606,52 +596,32 @@ function normalizePragma(pragma) {
|
|
|
606
596
|
function samePragmas(a, b) {
|
|
607
597
|
return a.length === b.length && a.every((pragma, index) => normalizePragma(pragma) === normalizePragma(b[index]));
|
|
608
598
|
}
|
|
609
|
-
var DEFAULT_MAX_FRAME_LENGTH = 64 * 1024 * 1024;
|
|
610
|
-
|
|
611
599
|
class FrameReader {
|
|
612
|
-
maxFrameLength;
|
|
613
600
|
pieces = [];
|
|
614
|
-
held = 0;
|
|
615
|
-
overflowed = false;
|
|
616
601
|
decoder = null;
|
|
617
602
|
searched = 0;
|
|
618
|
-
constructor(maxFrameLength = Number.POSITIVE_INFINITY) {
|
|
619
|
-
this.maxFrameLength = maxFrameLength;
|
|
620
|
-
}
|
|
621
603
|
get scannedCharacters() {
|
|
622
604
|
return this.searched;
|
|
623
605
|
}
|
|
624
|
-
get oversized() {
|
|
625
|
-
return this.overflowed;
|
|
626
|
-
}
|
|
627
606
|
push(chunk) {
|
|
628
|
-
if (this.overflowed)
|
|
629
|
-
return [];
|
|
630
607
|
let newline = chunk.indexOf(`
|
|
631
608
|
`);
|
|
632
609
|
if (newline < 0) {
|
|
633
610
|
this.searched += chunk.length;
|
|
634
|
-
if (chunk.length > 0)
|
|
635
|
-
this.held += chunk.length;
|
|
636
|
-
if (this.held > this.maxFrameLength)
|
|
637
|
-
return this.overflow([]);
|
|
611
|
+
if (chunk.length > 0)
|
|
638
612
|
this.pieces.push(chunk);
|
|
639
|
-
}
|
|
640
613
|
return [];
|
|
641
614
|
}
|
|
642
615
|
const frames = [];
|
|
643
616
|
let start = 0;
|
|
644
617
|
while (newline >= 0) {
|
|
645
618
|
this.searched += newline + 1 - start;
|
|
646
|
-
if (this.held + (newline - start) > this.maxFrameLength)
|
|
647
|
-
return this.overflow(frames);
|
|
648
619
|
let frame = chunk.slice(start, newline);
|
|
649
620
|
if (this.pieces.length > 0) {
|
|
650
621
|
this.pieces.push(frame);
|
|
651
622
|
frame = this.pieces.join("");
|
|
652
623
|
this.pieces = [];
|
|
653
624
|
}
|
|
654
|
-
this.held = 0;
|
|
655
625
|
if (frame.length > 0)
|
|
656
626
|
frames.push(frame);
|
|
657
627
|
start = newline + 1;
|
|
@@ -659,12 +629,8 @@ class FrameReader {
|
|
|
659
629
|
`, start);
|
|
660
630
|
}
|
|
661
631
|
this.searched += chunk.length - start;
|
|
662
|
-
if (start < chunk.length)
|
|
663
|
-
this.held = chunk.length - start;
|
|
664
|
-
if (this.held > this.maxFrameLength)
|
|
665
|
-
return this.overflow(frames);
|
|
632
|
+
if (start < chunk.length)
|
|
666
633
|
this.pieces.push(chunk.slice(start));
|
|
667
|
-
}
|
|
668
634
|
return frames;
|
|
669
635
|
}
|
|
670
636
|
pushBytes(chunk) {
|
|
@@ -673,22 +639,18 @@ class FrameReader {
|
|
|
673
639
|
}
|
|
674
640
|
reset() {
|
|
675
641
|
this.pieces = [];
|
|
676
|
-
this.held = 0;
|
|
677
|
-
this.overflowed = false;
|
|
678
642
|
this.decoder = null;
|
|
679
643
|
}
|
|
680
|
-
overflow(frames) {
|
|
681
|
-
this.pieces = [];
|
|
682
|
-
this.held = 0;
|
|
683
|
-
this.overflowed = true;
|
|
684
|
-
return frames;
|
|
685
|
-
}
|
|
686
644
|
}
|
|
687
645
|
function replaceBigInt(_key, value) {
|
|
688
646
|
return typeof value === "bigint" ? Number(value) : value;
|
|
689
647
|
}
|
|
690
648
|
|
|
691
649
|
// 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";
|
|
692
654
|
function resolveRuntimeDirectory() {
|
|
693
655
|
const configured = process.env.SQLEGO_RUNTIME_DIR;
|
|
694
656
|
const directory = configured && configured.length > 0 ? configured : path2.join(tmpdir(), "sqlego");
|
|
@@ -743,7 +705,6 @@ function resolvePaths(file) {
|
|
|
743
705
|
key,
|
|
744
706
|
socket,
|
|
745
707
|
lock: path2.join(directory.value, `${key}.lock`),
|
|
746
|
-
lease: path2.join(directory.value, `${key}.lease`),
|
|
747
708
|
meta: path2.join(directory.value, `${key}.json`)
|
|
748
709
|
});
|
|
749
710
|
return Result.ok(paths);
|
|
@@ -792,248 +753,15 @@ function isProcessAlive(pid) {
|
|
|
792
753
|
return unhandled;
|
|
793
754
|
}
|
|
794
755
|
|
|
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
|
-
|
|
1028
756
|
// src/version.ts
|
|
1029
757
|
import { createHash as createHash2 } from "node:crypto";
|
|
1030
|
-
import { existsSync as existsSync2, readdirSync
|
|
1031
|
-
import
|
|
758
|
+
import { existsSync as existsSync2, readdirSync, readFileSync } from "node:fs";
|
|
759
|
+
import path3 from "node:path";
|
|
1032
760
|
import { fileURLToPath } from "node:url";
|
|
1033
761
|
// package.json
|
|
1034
762
|
var package_default = {
|
|
1035
763
|
name: "@noego/sqlego",
|
|
1036
|
-
version: "0.1.6-dev.
|
|
764
|
+
version: "0.1.6-dev.7",
|
|
1037
765
|
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.",
|
|
1038
766
|
type: "module",
|
|
1039
767
|
license: "ISC",
|
|
@@ -1064,7 +792,7 @@ var package_default = {
|
|
|
1064
792
|
"README.md"
|
|
1065
793
|
],
|
|
1066
794
|
scripts: {
|
|
1067
|
-
prepare: "bun scripts/build.ts
|
|
795
|
+
prepare: "bun scripts/build.ts lib",
|
|
1068
796
|
build: "bun scripts/build.ts all",
|
|
1069
797
|
"build:all": "bun scripts/build.ts all:platforms",
|
|
1070
798
|
"build:lib": "bun scripts/build.ts lib",
|
|
@@ -1084,11 +812,11 @@ var package_default = {
|
|
|
1084
812
|
"@noego/trust": "^0.5.1"
|
|
1085
813
|
},
|
|
1086
814
|
optionalDependencies: {
|
|
1087
|
-
"@noego/sqlego-darwin-arm64": "0.1.6-dev.
|
|
1088
|
-
"@noego/sqlego-darwin-x64": "0.1.6-dev.
|
|
1089
|
-
"@noego/sqlego-linux-x64": "0.1.6-dev.
|
|
1090
|
-
"@noego/sqlego-linux-arm64": "0.1.6-dev.
|
|
1091
|
-
"@noego/sqlego-win32-x64": "0.1.6-dev.
|
|
815
|
+
"@noego/sqlego-darwin-arm64": "0.1.6-dev.7",
|
|
816
|
+
"@noego/sqlego-darwin-x64": "0.1.6-dev.7",
|
|
817
|
+
"@noego/sqlego-linux-x64": "0.1.6-dev.7",
|
|
818
|
+
"@noego/sqlego-linux-arm64": "0.1.6-dev.7",
|
|
819
|
+
"@noego/sqlego-win32-x64": "0.1.6-dev.7"
|
|
1092
820
|
}
|
|
1093
821
|
};
|
|
1094
822
|
|
|
@@ -1098,17 +826,17 @@ function sourceBuildId(sourceDirectory) {
|
|
|
1098
826
|
return Result.ok("source");
|
|
1099
827
|
return Trust.try(() => {
|
|
1100
828
|
const hash = createHash2("sha1");
|
|
1101
|
-
for (const name of
|
|
829
|
+
for (const name of readdirSync(sourceDirectory).filter((entry) => entry.endsWith(".ts")).sort()) {
|
|
1102
830
|
hash.update(name);
|
|
1103
|
-
hash.update(
|
|
831
|
+
hash.update(readFileSync(path3.join(sourceDirectory, name)));
|
|
1104
832
|
}
|
|
1105
833
|
return hash.digest("hex").slice(0, 12);
|
|
1106
834
|
}, (cause) => ({ kind: "source-unreadable", message: `cannot hash sources in ${sourceDirectory}: ${errorMessage(cause)}` }));
|
|
1107
835
|
}
|
|
1108
836
|
function resolveBuildId() {
|
|
1109
|
-
if ("
|
|
1110
|
-
return "
|
|
1111
|
-
const directory = Trust.try(() =>
|
|
837
|
+
if ("51daed1bd424".length > 0)
|
|
838
|
+
return "51daed1bd424";
|
|
839
|
+
const directory = Trust.try(() => path3.dirname(fileURLToPath(import.meta.url)), (cause) => ({ kind: "module-path", message: errorMessage(cause) }));
|
|
1112
840
|
if (!directory.ok)
|
|
1113
841
|
return decideUnknownBuild(directory.error.kind);
|
|
1114
842
|
const id = sourceBuildId(directory.value);
|
|
@@ -1167,8 +895,7 @@ class SqlegoClient {
|
|
|
1167
895
|
hello = null;
|
|
1168
896
|
mismatches = [];
|
|
1169
897
|
spawnFailure = null;
|
|
1170
|
-
|
|
1171
|
-
spawnLease = null;
|
|
898
|
+
holdsSpawnLock = false;
|
|
1172
899
|
options;
|
|
1173
900
|
constructor(paths, options) {
|
|
1174
901
|
this.paths = paths;
|
|
@@ -1389,7 +1116,6 @@ class SqlegoClient {
|
|
|
1389
1116
|
if (claimed.value) {
|
|
1390
1117
|
const placed = await this.placeFile(deadline);
|
|
1391
1118
|
if (!placed.ok) {
|
|
1392
|
-
this.releaseSpawnLock();
|
|
1393
1119
|
const released = this.releaseLock();
|
|
1394
1120
|
const suffix = !released.ok && released.error.kind === "lock-failed" ? `; lock not released: ${released.error.message}` : "";
|
|
1395
1121
|
return Result.err({ kind: placed.error.kind, message: `${placed.error.message}${suffix}` });
|
|
@@ -1398,91 +1124,41 @@ class SqlegoClient {
|
|
|
1398
1124
|
for (let delayMs = 2;Date.now() < deadline; delayMs = Math.min(25, delayMs * 2)) {
|
|
1399
1125
|
const socket = await tryConnect(this.paths.socket);
|
|
1400
1126
|
if (socket) {
|
|
1401
|
-
this.
|
|
1127
|
+
this.releaseSpawnLock();
|
|
1402
1128
|
return Result.ok(socket);
|
|
1403
1129
|
}
|
|
1404
1130
|
await sleep(delayMs);
|
|
1405
1131
|
}
|
|
1406
|
-
this.
|
|
1132
|
+
this.releaseSpawnLock();
|
|
1407
1133
|
const cause = this.spawnFailure ? ` (${this.spawnFailure})` : "";
|
|
1408
1134
|
return Result.err({ kind: "connect-timeout", message: `no sqlego server became reachable for ${this.paths.file}${cause}` });
|
|
1409
1135
|
}
|
|
1410
1136
|
claimLock() {
|
|
1411
|
-
|
|
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");
|
|
1137
|
+
return claimLockAt(this.paths.lock);
|
|
1451
1138
|
}
|
|
1452
1139
|
ownerBudget() {
|
|
1453
1140
|
const configured = this.options.maxOwners ?? Number(process.env.SQLEGO_MAX_OWNERS ?? DEFAULT_MAX_OWNERS);
|
|
1454
1141
|
return Number.isFinite(configured) && configured >= 0 ? Math.floor(configured) : DEFAULT_MAX_OWNERS;
|
|
1455
1142
|
}
|
|
1143
|
+
spawnLockPath() {
|
|
1144
|
+
return path4.join(path4.dirname(this.paths.lock), "spawn.lock");
|
|
1145
|
+
}
|
|
1456
1146
|
async placeFile(deadline) {
|
|
1457
1147
|
const budget = this.ownerBudget();
|
|
1458
1148
|
let lastRefusal = null;
|
|
1459
|
-
const refusal = () => lastRefusal ? `: ${lastRefusal}` : "";
|
|
1460
1149
|
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);
|
|
1466
1150
|
const owners = this.currentOwners();
|
|
1467
1151
|
if (!owners.ok)
|
|
1468
1152
|
return owners;
|
|
1469
1153
|
if (budget === 0 || owners.value.length < budget) {
|
|
1470
1154
|
if (budget === 0)
|
|
1471
1155
|
return this.spawnServer();
|
|
1472
|
-
const claimed =
|
|
1156
|
+
const claimed = claimLockAt(this.spawnLockPath());
|
|
1473
1157
|
if (!claimed.ok)
|
|
1474
|
-
return
|
|
1158
|
+
return claimed;
|
|
1475
1159
|
if (claimed.value) {
|
|
1476
|
-
this.
|
|
1477
|
-
|
|
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;
|
|
1160
|
+
this.holdsSpawnLock = true;
|
|
1161
|
+
return this.spawnServer();
|
|
1486
1162
|
}
|
|
1487
1163
|
await sleep(25);
|
|
1488
1164
|
continue;
|
|
@@ -1495,33 +1171,28 @@ class SqlegoClient {
|
|
|
1495
1171
|
op: "adopt",
|
|
1496
1172
|
file: this.paths.file,
|
|
1497
1173
|
pragmas: this.options.serverPragmas,
|
|
1498
|
-
idleTimeoutMs: this.options.idleTimeoutMs ?? 30000
|
|
1499
|
-
claim: this.claim ? encodeLease(this.claim) : undefined
|
|
1174
|
+
idleTimeoutMs: this.options.idleTimeoutMs ?? 30000
|
|
1500
1175
|
}, parseAdoptResult, Math.min(ADOPT_TIMEOUT_MS, remaining));
|
|
1501
1176
|
if (adopted.ok)
|
|
1502
1177
|
return Result.ok(undefined);
|
|
1503
1178
|
if (adopted.error.kind === "probe-failed")
|
|
1504
1179
|
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);
|
|
1510
1180
|
}
|
|
1511
1181
|
await sleep(25);
|
|
1512
1182
|
}
|
|
1513
|
-
|
|
1183
|
+
const why = lastRefusal ? `: ${lastRefusal}` : "";
|
|
1184
|
+
return Result.err({ kind: "connect-timeout", message: `no sqlego owner took on ${this.paths.file}${why}` });
|
|
1514
1185
|
}
|
|
1515
1186
|
currentOwners() {
|
|
1516
|
-
const directory =
|
|
1517
|
-
const names = Trust.try(() =>
|
|
1187
|
+
const directory = path4.dirname(this.paths.lock);
|
|
1188
|
+
const names = Trust.try(() => readdirSync2(directory), (cause) => ({ kind: "runtime-directory", message: `cannot list sqlego owners in ${directory}: ${errorMessage(cause)}` }));
|
|
1518
1189
|
if (!names.ok)
|
|
1519
1190
|
return names;
|
|
1520
1191
|
const byPid = new Map;
|
|
1521
1192
|
for (const name of names.value) {
|
|
1522
1193
|
if (!name.endsWith(".json"))
|
|
1523
1194
|
continue;
|
|
1524
|
-
const text = Trust.try(() =>
|
|
1195
|
+
const text = Trust.try(() => readFileSync2(path4.join(directory, name), "utf8"), () => ({ kind: "invalid", message: "unreadable" }));
|
|
1525
1196
|
const json = text.ok ? fromJson(text.value) : text;
|
|
1526
1197
|
const meta = json.ok ? parseServerMeta(json.value) : json;
|
|
1527
1198
|
if (meta.ok) {
|
|
@@ -1540,19 +1211,12 @@ class SqlegoClient {
|
|
|
1540
1211
|
return Result.ok(owners);
|
|
1541
1212
|
}
|
|
1542
1213
|
releaseSpawnLock() {
|
|
1543
|
-
|
|
1544
|
-
this.spawnLease = null;
|
|
1545
|
-
if (!lease)
|
|
1214
|
+
if (!this.holdsSpawnLock)
|
|
1546
1215
|
return;
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
this.spawnFailure = `spawn lock not released: ${released.error.message}`;
|
|
1550
|
-
}
|
|
1551
|
-
settleLeases() {
|
|
1552
|
-
this.releaseSpawnLock();
|
|
1553
|
-
const released = this.releaseLock();
|
|
1216
|
+
this.holdsSpawnLock = false;
|
|
1217
|
+
const released = Trust.try(() => rmSync(this.spawnLockPath(), { force: true }), (cause) => ({ kind: "lock-failed", message: errorMessage(cause) }));
|
|
1554
1218
|
if (!released.ok && released.error.kind === "lock-failed")
|
|
1555
|
-
this.spawnFailure = `
|
|
1219
|
+
this.spawnFailure = `spawn lock not released: ${released.error.message}`;
|
|
1556
1220
|
}
|
|
1557
1221
|
async waitForRelease(deadline) {
|
|
1558
1222
|
for (let delayMs = 2;Date.now() < deadline; delayMs = Math.min(25, delayMs * 2)) {
|
|
@@ -1569,19 +1233,12 @@ class SqlegoClient {
|
|
|
1569
1233
|
return resolved;
|
|
1570
1234
|
const [command, ...args] = resolved.value;
|
|
1571
1235
|
const pragmaArgs = (this.options.serverPragmas ?? []).flatMap((pragma) => ["--pragma", pragma]);
|
|
1572
|
-
|
|
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(() => {
|
|
1236
|
+
return Trust.try(() => {
|
|
1580
1237
|
const child = spawn(command, [...args, "serve", this.paths.file, "--idle-ms", String(this.options.idleTimeoutMs ?? 30000), ...pragmaArgs], {
|
|
1581
1238
|
detached: true,
|
|
1582
1239
|
windowsHide: true,
|
|
1583
1240
|
stdio: ["ignore", "ignore", "pipe"],
|
|
1584
|
-
env
|
|
1241
|
+
env: { ...process.env, SQLEGO_CLAIMED_BY: String(process.pid) }
|
|
1585
1242
|
});
|
|
1586
1243
|
let stderr = "";
|
|
1587
1244
|
child.stderr?.on("data", (chunk) => {
|
|
@@ -1598,36 +1255,16 @@ class SqlegoClient {
|
|
|
1598
1255
|
this.spawnFailure = `server exited before listening (code ${code}, signal ${signal})${stderr ? `: ${stderr.trim()}` : ""}`;
|
|
1599
1256
|
});
|
|
1600
1257
|
child.unref();
|
|
1601
|
-
return child.pid;
|
|
1602
1258
|
}, (cause) => ({ kind: "spawn-failed", message: `spawn ${command} failed: ${errorMessage(cause)}` }));
|
|
1603
|
-
if (!spawned.ok)
|
|
1604
|
-
return spawned;
|
|
1605
|
-
return Result.ok(undefined);
|
|
1606
1259
|
}
|
|
1607
1260
|
onSpawnError(command, error) {
|
|
1608
|
-
this.releaseSpawnLock();
|
|
1609
1261
|
this.spawnFailure = `spawn ${command} failed: ${error.message}`;
|
|
1610
1262
|
const released = this.releaseLock();
|
|
1611
1263
|
if (!released.ok && released.error.kind === "lock-failed")
|
|
1612
1264
|
this.spawnFailure += `; lock not released: ${released.error.message}`;
|
|
1613
1265
|
}
|
|
1614
1266
|
releaseLock() {
|
|
1615
|
-
|
|
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);
|
|
1267
|
+
return Trust.try(() => rmSync(this.paths.lock, { force: true }), (cause) => ({ kind: "lock-failed", message: errorMessage(cause) }));
|
|
1631
1268
|
}
|
|
1632
1269
|
attach(socket) {
|
|
1633
1270
|
this.socket = socket;
|
|
@@ -1710,6 +1347,30 @@ class SqlegoClient {
|
|
|
1710
1347
|
this.pending.clear();
|
|
1711
1348
|
}
|
|
1712
1349
|
}
|
|
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
|
+
}
|
|
1713
1374
|
|
|
1714
1375
|
class SqlegoTransaction {
|
|
1715
1376
|
id;
|
|
@@ -1765,7 +1426,7 @@ function resolveServerCommand() {
|
|
|
1765
1426
|
const configured = process.env.SQLEGO_BIN;
|
|
1766
1427
|
if (configured && configured.length > 0)
|
|
1767
1428
|
return Result.ok([configured]);
|
|
1768
|
-
const located = Trust.try(() =>
|
|
1429
|
+
const located = Trust.try(() => path4.dirname(fileURLToPath2(import.meta.url)), (cause) => ({ kind: "module-path", message: errorMessage(cause) }));
|
|
1769
1430
|
let here = null;
|
|
1770
1431
|
if (located.ok)
|
|
1771
1432
|
here = located.value;
|
|
@@ -1773,12 +1434,12 @@ function resolveServerCommand() {
|
|
|
1773
1434
|
here = null;
|
|
1774
1435
|
if (process.versions.bun) {
|
|
1775
1436
|
const entry = process.argv[1] ?? "";
|
|
1776
|
-
if (
|
|
1437
|
+
if (path4.basename(process.execPath).startsWith("sqlego"))
|
|
1777
1438
|
return Result.ok([process.execPath]);
|
|
1778
1439
|
if (entry.endsWith("cli.ts"))
|
|
1779
1440
|
return Result.ok([process.execPath, entry]);
|
|
1780
1441
|
if (entry.endsWith(".ts") && here !== null) {
|
|
1781
|
-
const cli =
|
|
1442
|
+
const cli = path4.join(here, "cli.ts");
|
|
1782
1443
|
if (existsSync3(cli))
|
|
1783
1444
|
return Result.ok([process.execPath, cli]);
|
|
1784
1445
|
}
|
|
@@ -1789,7 +1450,7 @@ function resolveServerCommand() {
|
|
|
1789
1450
|
configured: undefined,
|
|
1790
1451
|
resolvePackageJson: (name) => resolvePackageJson(name, here),
|
|
1791
1452
|
exists: existsSync3,
|
|
1792
|
-
localBinDirectory: here === null ? null :
|
|
1453
|
+
localBinDirectory: here === null ? null : path4.join(here, "..", "bin")
|
|
1793
1454
|
});
|
|
1794
1455
|
if (!binary.ok)
|
|
1795
1456
|
return binary;
|
|
@@ -1804,7 +1465,7 @@ function defaultServerCommand() {
|
|
|
1804
1465
|
function resolvePackageJson(name, here) {
|
|
1805
1466
|
if (here === null)
|
|
1806
1467
|
return null;
|
|
1807
|
-
const resolved = Trust.try(() => createRequire(
|
|
1468
|
+
const resolved = Trust.try(() => createRequire(path4.join(here, "index.js")).resolve(`${name}/package.json`), (cause) => ({ kind: "not-installed", message: errorMessage(cause) }));
|
|
1808
1469
|
if (resolved.ok)
|
|
1809
1470
|
return typeof resolved.value === "string" ? resolved.value : null;
|
|
1810
1471
|
if (resolved.error.kind === "not-installed")
|