@noego/sqlego 0.1.6-dev.6 → 0.1.6-dev.8
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 +3 -1
- package/dist/client.d.ts +10 -0
- package/dist/index.js +85 -22
- package/dist/version.d.ts +17 -3
- package/package.json +9 -8
- package/src/client.ts +38 -13
- package/src/version.ts +75 -7
package/README.md
CHANGED
|
@@ -421,7 +421,9 @@ bun scripts/release.ts --channel dev --pack ./out # build + stage + check all
|
|
|
421
421
|
|
|
422
422
|
`bun run build` runs the fresh binary with `--version` and fails unless it
|
|
423
423
|
reports `<package.json version>+<build id>`, so a stale binary cannot be
|
|
424
|
-
mistaken for the current one.
|
|
424
|
+
mistaken for the current one. The build id hashes `src/*.ts` and the Rust
|
|
425
|
+
owner's inputs (`rust/Cargo.toml`, `rust/Cargo.lock`, `rust/build.rs`, every
|
|
426
|
+
file under `rust/src/`), so a change on either side is a new build.
|
|
425
427
|
|
|
426
428
|
### Releasing
|
|
427
429
|
|
package/dist/client.d.ts
CHANGED
|
@@ -203,6 +203,16 @@ export declare class SqlegoClient {
|
|
|
203
203
|
*/
|
|
204
204
|
private mismatchesOf;
|
|
205
205
|
private connectOrSpawn;
|
|
206
|
+
/** How often a client waiting on another process's claim checks whether that claim was freed. */
|
|
207
|
+
private static readonly CLAIM_RECHECK_MS;
|
|
208
|
+
/**
|
|
209
|
+
* Waits for the file's socket until `deadline`. A client that found the
|
|
210
|
+
* claim held by another process (`watchClaim`) also watches that claim:
|
|
211
|
+
* an owner that is stopping removes its socket and meta while it still
|
|
212
|
+
* holds the claim and frees it last, so once the claim is free nothing
|
|
213
|
+
* will listen unless this client places the file itself (ok, null).
|
|
214
|
+
*/
|
|
215
|
+
private waitForSocket;
|
|
206
216
|
/**
|
|
207
217
|
* The file's claim: whoever holds it places the file with an owner, which
|
|
208
218
|
* then holds it while it serves the file. See `takeClaim`.
|
package/dist/index.js
CHANGED
|
@@ -1033,7 +1033,7 @@ import { fileURLToPath } from "node:url";
|
|
|
1033
1033
|
// package.json
|
|
1034
1034
|
var package_default = {
|
|
1035
1035
|
name: "@noego/sqlego",
|
|
1036
|
-
version: "0.1.6-dev.
|
|
1036
|
+
version: "0.1.6-dev.8",
|
|
1037
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.",
|
|
1038
1038
|
type: "module",
|
|
1039
1039
|
license: "ISC",
|
|
@@ -1064,7 +1064,7 @@ var package_default = {
|
|
|
1064
1064
|
"README.md"
|
|
1065
1065
|
],
|
|
1066
1066
|
scripts: {
|
|
1067
|
-
prepare: "bun scripts/build.ts
|
|
1067
|
+
prepare: "bun scripts/build.ts lib",
|
|
1068
1068
|
build: "bun scripts/build.ts all",
|
|
1069
1069
|
"build:all": "bun scripts/build.ts all:platforms",
|
|
1070
1070
|
"build:lib": "bun scripts/build.ts lib",
|
|
@@ -1084,15 +1084,17 @@ var package_default = {
|
|
|
1084
1084
|
"@noego/trust": "^0.5.1"
|
|
1085
1085
|
},
|
|
1086
1086
|
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.
|
|
1092
|
-
}
|
|
1087
|
+
"@noego/sqlego-darwin-arm64": "0.1.6-dev.8",
|
|
1088
|
+
"@noego/sqlego-darwin-x64": "0.1.6-dev.8",
|
|
1089
|
+
"@noego/sqlego-linux-x64": "0.1.6-dev.8",
|
|
1090
|
+
"@noego/sqlego-linux-arm64": "0.1.6-dev.8",
|
|
1091
|
+
"@noego/sqlego-win32-x64": "0.1.6-dev.8"
|
|
1092
|
+
},
|
|
1093
|
+
sqlegoBuild: "06f30a893f9f"
|
|
1093
1094
|
};
|
|
1094
1095
|
|
|
1095
1096
|
// src/version.ts
|
|
1097
|
+
var RUST_BUILD_FILES = ["rust/Cargo.toml", "rust/Cargo.lock", "rust/build.rs"];
|
|
1096
1098
|
function sourceBuildId(sourceDirectory) {
|
|
1097
1099
|
if (!existsSync2(sourceDirectory))
|
|
1098
1100
|
return Result.ok("source");
|
|
@@ -1102,13 +1104,55 @@ function sourceBuildId(sourceDirectory) {
|
|
|
1102
1104
|
hash.update(name);
|
|
1103
1105
|
hash.update(readFileSync3(path4.join(sourceDirectory, name)));
|
|
1104
1106
|
}
|
|
1107
|
+
const root = path4.dirname(sourceDirectory);
|
|
1108
|
+
if (existsSync2(path4.join(root, "rust", "Cargo.toml"))) {
|
|
1109
|
+
const files = RUST_BUILD_FILES.filter((file) => existsSync2(path4.join(root, file)));
|
|
1110
|
+
const rustSources = path4.join(root, "rust", "src");
|
|
1111
|
+
if (existsSync2(rustSources)) {
|
|
1112
|
+
for (const entry of readdirSync2(rustSources, { recursive: true, withFileTypes: true })) {
|
|
1113
|
+
if (!entry.isFile())
|
|
1114
|
+
continue;
|
|
1115
|
+
const relative = path4.relative(root, path4.join(entry.parentPath, entry.name));
|
|
1116
|
+
files.push(relative.split(path4.sep).join("/"));
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
for (const file of files.sort()) {
|
|
1120
|
+
hash.update(file);
|
|
1121
|
+
hash.update(readFileSync3(path4.join(root, file)));
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1105
1124
|
return hash.digest("hex").slice(0, 12);
|
|
1106
1125
|
}, (cause) => ({ kind: "source-unreadable", message: `cannot hash sources in ${sourceDirectory}: ${errorMessage(cause)}` }));
|
|
1107
1126
|
}
|
|
1108
|
-
function
|
|
1109
|
-
if ("
|
|
1110
|
-
return
|
|
1111
|
-
|
|
1127
|
+
function chooseBuildId(defined, stamped, rustInTree, fromSource) {
|
|
1128
|
+
if (typeof defined === "string" && defined.length > 0)
|
|
1129
|
+
return defined;
|
|
1130
|
+
if (!rustInTree && typeof stamped === "string" && stamped.length > 0)
|
|
1131
|
+
return stamped;
|
|
1132
|
+
return fromSource();
|
|
1133
|
+
}
|
|
1134
|
+
var stampedBuild = package_default.sqlegoBuild;
|
|
1135
|
+
function moduleDirectory() {
|
|
1136
|
+
return Trust.try(() => path4.dirname(fileURLToPath(import.meta.url)), (cause) => ({ kind: "module-path", message: errorMessage(cause) }));
|
|
1137
|
+
}
|
|
1138
|
+
function rustInTree() {
|
|
1139
|
+
const directory = moduleDirectory();
|
|
1140
|
+
if (!directory.ok)
|
|
1141
|
+
return decideNoTree(directory.error.kind);
|
|
1142
|
+
return existsSync2(path4.join(directory.value, "..", "rust", "Cargo.toml"));
|
|
1143
|
+
}
|
|
1144
|
+
function decideNoTree(kind) {
|
|
1145
|
+
switch (kind) {
|
|
1146
|
+
case "module-path":
|
|
1147
|
+
return false;
|
|
1148
|
+
default: {
|
|
1149
|
+
const unhandled = kind;
|
|
1150
|
+
return unhandled;
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
function buildIdFromSource() {
|
|
1155
|
+
const directory = moduleDirectory();
|
|
1112
1156
|
if (!directory.ok)
|
|
1113
1157
|
return decideUnknownBuild(directory.error.kind);
|
|
1114
1158
|
const id = sourceBuildId(directory.value);
|
|
@@ -1116,6 +1160,9 @@ function resolveBuildId() {
|
|
|
1116
1160
|
return decideUnknownBuild(id.error.kind);
|
|
1117
1161
|
return id.value;
|
|
1118
1162
|
}
|
|
1163
|
+
function resolveBuildId() {
|
|
1164
|
+
return chooseBuildId("06f30a893f9f", stampedBuild, rustInTree(), buildIdFromSource);
|
|
1165
|
+
}
|
|
1119
1166
|
function decideUnknownBuild(kind) {
|
|
1120
1167
|
switch (kind) {
|
|
1121
1168
|
case "module-path":
|
|
@@ -1383,24 +1430,40 @@ class SqlegoClient {
|
|
|
1383
1430
|
if (this.options.spawn === false) {
|
|
1384
1431
|
return Result.err({ kind: "not-running", message: `no sqlego server for ${this.paths.file}` });
|
|
1385
1432
|
}
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1433
|
+
for (;; ) {
|
|
1434
|
+
const claimed = this.claimLock();
|
|
1435
|
+
if (!claimed.ok)
|
|
1436
|
+
return claimed;
|
|
1437
|
+
if (claimed.value) {
|
|
1438
|
+
const placed = await this.placeFile(deadline);
|
|
1439
|
+
if (!placed.ok) {
|
|
1440
|
+
this.releaseSpawnLock();
|
|
1441
|
+
const released = this.releaseLock();
|
|
1442
|
+
const suffix = !released.ok && released.error.kind === "lock-failed" ? `; lock not released: ${released.error.message}` : "";
|
|
1443
|
+
return Result.err({ kind: placed.error.kind, message: `${placed.error.message}${suffix}` });
|
|
1444
|
+
}
|
|
1396
1445
|
}
|
|
1446
|
+
const waited = await this.waitForSocket(deadline, !claimed.value);
|
|
1447
|
+
if (!waited.ok)
|
|
1448
|
+
return waited;
|
|
1449
|
+
if (waited.value)
|
|
1450
|
+
return Result.ok(waited.value);
|
|
1397
1451
|
}
|
|
1452
|
+
}
|
|
1453
|
+
static CLAIM_RECHECK_MS = 100;
|
|
1454
|
+
async waitForSocket(deadline, watchClaim) {
|
|
1455
|
+
let rechecked = Date.now();
|
|
1398
1456
|
for (let delayMs = 2;Date.now() < deadline; delayMs = Math.min(25, delayMs * 2)) {
|
|
1399
1457
|
const socket = await tryConnect(this.paths.socket);
|
|
1400
1458
|
if (socket) {
|
|
1401
1459
|
this.settleLeases();
|
|
1402
1460
|
return Result.ok(socket);
|
|
1403
1461
|
}
|
|
1462
|
+
if (watchClaim && Date.now() - rechecked >= SqlegoClient.CLAIM_RECHECK_MS) {
|
|
1463
|
+
rechecked = Date.now();
|
|
1464
|
+
if (Trust.decide(leaseHolder(this.paths.lease), { "lease-failed": () => -1 }).value === null)
|
|
1465
|
+
return Result.ok(null);
|
|
1466
|
+
}
|
|
1404
1467
|
await sleep(delayMs);
|
|
1405
1468
|
}
|
|
1406
1469
|
this.settleLeases();
|
package/dist/version.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { Result } from '@noego/trust';
|
|
2
2
|
/**
|
|
3
|
-
* Hash of every `src/*.ts` file,
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* Hash of every `src/*.ts` file (name, bytes), then, in a tree with
|
|
4
|
+
* `rust/Cargo.toml`, of the Rust owner's inputs (`/`-separated path, bytes,
|
|
5
|
+
* sorted): `RUST_BUILD_FILES` that exist and every file under `rust/src/`.
|
|
6
|
+
* So the artifacts built from one tree (Node client in dist/, the owner
|
|
7
|
+
* binary in bin/) and a source run agree, while any edit on either side
|
|
8
|
+
* yields a new id. `rust/build.rs` computes the same for the binary.
|
|
9
|
+
* `source` when the tree is not there.
|
|
6
10
|
*/
|
|
7
11
|
export declare function sourceBuildId(sourceDirectory: string): Result<string, {
|
|
8
12
|
kind: "source-unreadable";
|
|
@@ -10,6 +14,16 @@ export declare function sourceBuildId(sourceDirectory: string): Result<string, {
|
|
|
10
14
|
}>;
|
|
11
15
|
/** Exception-based form of `sourceBuildId` for the build script, which must stop on an unreadable tree. */
|
|
12
16
|
export declare function computeSourceBuildId(sourceDirectory: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Which build id an artifact reports. `defined` (stamped into dist by the
|
|
19
|
+
* build) always wins. Otherwise a tree holding the Rust sources
|
|
20
|
+
* (`rustInTree`: a checkout) hashes itself, exactly as the build does. A
|
|
21
|
+
* tree without them is the published package run from `src/` by Bun, whose
|
|
22
|
+
* hash would leave out the Rust inputs the owner binary was built with: it
|
|
23
|
+
* takes the id the release stamped into its package.json (`stamped`), and
|
|
24
|
+
* hashes only when there is none.
|
|
25
|
+
*/
|
|
26
|
+
export declare function chooseBuildId(defined: unknown, stamped: unknown, rustInTree: boolean, fromSource: () => string): string;
|
|
13
27
|
/** Build identity of this artifact: the source hash it was built from. */
|
|
14
28
|
export declare const BUILD_ID: string;
|
|
15
29
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noego/sqlego",
|
|
3
|
-
"version": "0.1.6-dev.
|
|
3
|
+
"version": "0.1.6-dev.8",
|
|
4
4
|
"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.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "ISC",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"README.md"
|
|
32
32
|
],
|
|
33
33
|
"scripts": {
|
|
34
|
-
"prepare": "bun scripts/build.ts
|
|
34
|
+
"prepare": "bun scripts/build.ts lib",
|
|
35
35
|
"build": "bun scripts/build.ts all",
|
|
36
36
|
"build:all": "bun scripts/build.ts all:platforms",
|
|
37
37
|
"build:lib": "bun scripts/build.ts lib",
|
|
@@ -51,10 +51,11 @@
|
|
|
51
51
|
"@noego/trust": "^0.5.1"
|
|
52
52
|
},
|
|
53
53
|
"optionalDependencies": {
|
|
54
|
-
"@noego/sqlego-darwin-arm64": "0.1.6-dev.
|
|
55
|
-
"@noego/sqlego-darwin-x64": "0.1.6-dev.
|
|
56
|
-
"@noego/sqlego-linux-x64": "0.1.6-dev.
|
|
57
|
-
"@noego/sqlego-linux-arm64": "0.1.6-dev.
|
|
58
|
-
"@noego/sqlego-win32-x64": "0.1.6-dev.
|
|
59
|
-
}
|
|
54
|
+
"@noego/sqlego-darwin-arm64": "0.1.6-dev.8",
|
|
55
|
+
"@noego/sqlego-darwin-x64": "0.1.6-dev.8",
|
|
56
|
+
"@noego/sqlego-linux-x64": "0.1.6-dev.8",
|
|
57
|
+
"@noego/sqlego-linux-arm64": "0.1.6-dev.8",
|
|
58
|
+
"@noego/sqlego-win32-x64": "0.1.6-dev.8"
|
|
59
|
+
},
|
|
60
|
+
"sqlegoBuild": "06f30a893f9f"
|
|
60
61
|
}
|
package/src/client.ts
CHANGED
|
@@ -514,30 +514,55 @@ export class SqlegoClient {
|
|
|
514
514
|
if (this.options.spawn === false) {
|
|
515
515
|
return Result.err({ kind: 'not-running', message: `no sqlego server for ${this.paths.file}` });
|
|
516
516
|
}
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
517
|
+
for (;;) {
|
|
518
|
+
const claimed = this.claimLock();
|
|
519
|
+
if (!claimed.ok) return claimed;
|
|
520
|
+
if (claimed.value) {
|
|
521
|
+
const placed = await this.placeFile(deadline);
|
|
522
|
+
if (!placed.ok) {
|
|
523
|
+
// Nothing will serve the file: the spawn lock (when this client took it) and the claim must go so the next client can try.
|
|
524
|
+
this.releaseSpawnLock();
|
|
525
|
+
const released = this.releaseLock();
|
|
526
|
+
const suffix = !released.ok && released.error.kind === 'lock-failed' ? `; lock not released: ${released.error.message}` : '';
|
|
527
|
+
return Result.err({ kind: placed.error.kind, message: `${placed.error.message}${suffix}` });
|
|
528
|
+
}
|
|
527
529
|
}
|
|
530
|
+
// Claimer or not, the socket appears when the winner is listening.
|
|
531
|
+
const waited = await this.waitForSocket(deadline, !claimed.value);
|
|
532
|
+
if (!waited.ok) return waited;
|
|
533
|
+
if (waited.value) return Result.ok(waited.value);
|
|
534
|
+
// The claim it waited on was freed with nothing listening: place the file now.
|
|
528
535
|
}
|
|
529
|
-
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/** How often a client waiting on another process's claim checks whether that claim was freed. */
|
|
539
|
+
private static readonly CLAIM_RECHECK_MS = 100;
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Waits for the file's socket until `deadline`. A client that found the
|
|
543
|
+
* claim held by another process (`watchClaim`) also watches that claim:
|
|
544
|
+
* an owner that is stopping removes its socket and meta while it still
|
|
545
|
+
* holds the claim and frees it last, so once the claim is free nothing
|
|
546
|
+
* will listen unless this client places the file itself (ok, null).
|
|
547
|
+
*/
|
|
548
|
+
private async waitForSocket(deadline: number, watchClaim: boolean) {
|
|
549
|
+
let rechecked = Date.now();
|
|
530
550
|
for (let delayMs = 2; Date.now() < deadline; delayMs = Math.min(25, delayMs * 2)) {
|
|
531
551
|
const socket = await tryConnect(this.paths.socket);
|
|
532
552
|
if (socket) {
|
|
533
553
|
this.settleLeases();
|
|
534
|
-
return Result.ok(socket);
|
|
554
|
+
return Result.ok<typeof socket | null>(socket);
|
|
555
|
+
}
|
|
556
|
+
if (watchClaim && Date.now() - rechecked >= SqlegoClient.CLAIM_RECHECK_MS) {
|
|
557
|
+
rechecked = Date.now();
|
|
558
|
+
// A claim that cannot be read is not known to be free.
|
|
559
|
+
if (Trust.decide(leaseHolder(this.paths.lease), { 'lease-failed': () => -1 }).value === null) return Result.ok(null);
|
|
535
560
|
}
|
|
536
561
|
await sleep(delayMs);
|
|
537
562
|
}
|
|
538
563
|
this.settleLeases();
|
|
539
564
|
const cause = this.spawnFailure ? ` (${this.spawnFailure})` : '';
|
|
540
|
-
return Result.err({ kind: 'connect-timeout', message: `no sqlego server became reachable for ${this.paths.file}${cause}` });
|
|
565
|
+
return Result.err({ kind: 'connect-timeout' as const, message: `no sqlego server became reachable for ${this.paths.file}${cause}` });
|
|
541
566
|
}
|
|
542
567
|
|
|
543
568
|
/**
|
package/src/version.ts
CHANGED
|
@@ -9,10 +9,17 @@ import pkg from '../package.json' with { type: 'json' };
|
|
|
9
9
|
/** Stamped by `scripts/build.ts` (`--define`) into dist and the compiled binary; absent when running from source. */
|
|
10
10
|
declare const __SQLEGO_BUILD__: string | undefined;
|
|
11
11
|
|
|
12
|
+
/** The Rust owner's build inputs besides `rust/src/`, relative to the package root. */
|
|
13
|
+
const RUST_BUILD_FILES = ['rust/Cargo.toml', 'rust/Cargo.lock', 'rust/build.rs'] as const;
|
|
14
|
+
|
|
12
15
|
/**
|
|
13
|
-
* Hash of every `src/*.ts` file,
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
+
* Hash of every `src/*.ts` file (name, bytes), then, in a tree with
|
|
17
|
+
* `rust/Cargo.toml`, of the Rust owner's inputs (`/`-separated path, bytes,
|
|
18
|
+
* sorted): `RUST_BUILD_FILES` that exist and every file under `rust/src/`.
|
|
19
|
+
* So the artifacts built from one tree (Node client in dist/, the owner
|
|
20
|
+
* binary in bin/) and a source run agree, while any edit on either side
|
|
21
|
+
* yields a new id. `rust/build.rs` computes the same for the binary.
|
|
22
|
+
* `source` when the tree is not there.
|
|
16
23
|
*/
|
|
17
24
|
export function sourceBuildId(sourceDirectory: string) {
|
|
18
25
|
if (!existsSync(sourceDirectory)) return Result.ok('source');
|
|
@@ -23,6 +30,24 @@ export function sourceBuildId(sourceDirectory: string) {
|
|
|
23
30
|
hash.update(name);
|
|
24
31
|
hash.update(readFileSync(path.join(sourceDirectory, name)));
|
|
25
32
|
}
|
|
33
|
+
const root = path.dirname(sourceDirectory);
|
|
34
|
+
if (existsSync(path.join(root, 'rust', 'Cargo.toml'))) {
|
|
35
|
+
const files: string[] = RUST_BUILD_FILES.filter((file) => existsSync(path.join(root, file)));
|
|
36
|
+
// Every regular file under rust/src, as a `/`-separated path from the root; links are
|
|
37
|
+
// neither hashed nor followed, as build.rs's walk does not.
|
|
38
|
+
const rustSources = path.join(root, 'rust', 'src');
|
|
39
|
+
if (existsSync(rustSources)) {
|
|
40
|
+
for (const entry of readdirSync(rustSources, { recursive: true, withFileTypes: true })) {
|
|
41
|
+
if (!entry.isFile()) continue;
|
|
42
|
+
const relative = path.relative(root, path.join(entry.parentPath, entry.name));
|
|
43
|
+
files.push(relative.split(path.sep).join('/'));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
for (const file of files.sort()) {
|
|
47
|
+
hash.update(file);
|
|
48
|
+
hash.update(readFileSync(path.join(root, file)));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
26
51
|
return hash.digest('hex').slice(0, 12);
|
|
27
52
|
},
|
|
28
53
|
(cause) => ({ kind: 'source-unreadable', message: `cannot hash sources in ${sourceDirectory}: ${errorMessage(cause)}` }),
|
|
@@ -37,18 +62,61 @@ export function computeSourceBuildId(sourceDirectory: string): string {
|
|
|
37
62
|
throw new Error(id.error.message);
|
|
38
63
|
}
|
|
39
64
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Which build id an artifact reports. `defined` (stamped into dist by the
|
|
67
|
+
* build) always wins. Otherwise a tree holding the Rust sources
|
|
68
|
+
* (`rustInTree`: a checkout) hashes itself, exactly as the build does. A
|
|
69
|
+
* tree without them is the published package run from `src/` by Bun, whose
|
|
70
|
+
* hash would leave out the Rust inputs the owner binary was built with: it
|
|
71
|
+
* takes the id the release stamped into its package.json (`stamped`), and
|
|
72
|
+
* hashes only when there is none.
|
|
73
|
+
*/
|
|
74
|
+
export function chooseBuildId(defined: unknown, stamped: unknown, rustInTree: boolean, fromSource: () => string): string {
|
|
75
|
+
if (typeof defined === 'string' && defined.length > 0) return defined;
|
|
76
|
+
if (!rustInTree && typeof stamped === 'string' && stamped.length > 0) return stamped;
|
|
77
|
+
return fromSource();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** `sqlegoBuild` in package.json: written by `scripts/release.ts` into the package it publishes, never committed. */
|
|
81
|
+
const stampedBuild: unknown = (pkg as Readonly<Record<string, unknown>>).sqlegoBuild;
|
|
82
|
+
|
|
83
|
+
function moduleDirectory() {
|
|
84
|
+
return Trust.try(
|
|
43
85
|
() => path.dirname(fileURLToPath(import.meta.url)),
|
|
44
|
-
(cause) => ({ kind: 'module-path', message: errorMessage(cause) }),
|
|
86
|
+
(cause) => ({ kind: 'module-path' as const, message: errorMessage(cause) }),
|
|
45
87
|
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Whether this module sits in a checkout: `rust/Cargo.toml` beside its `src/`. */
|
|
91
|
+
function rustInTree(): boolean {
|
|
92
|
+
const directory = moduleDirectory();
|
|
93
|
+
if (!directory.ok) return decideNoTree(directory.error.kind);
|
|
94
|
+
return existsSync(path.join(directory.value, '..', 'rust', 'Cargo.toml'));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** A module whose path cannot be resolved is not known to sit in a checkout. */
|
|
98
|
+
function decideNoTree(kind: 'module-path') {
|
|
99
|
+
switch (kind) {
|
|
100
|
+
case 'module-path': return false;
|
|
101
|
+
default: {
|
|
102
|
+
const unhandled: never = kind;
|
|
103
|
+
return unhandled;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function buildIdFromSource() {
|
|
109
|
+
const directory = moduleDirectory();
|
|
46
110
|
if (!directory.ok) return decideUnknownBuild(directory.error.kind);
|
|
47
111
|
const id = sourceBuildId(directory.value);
|
|
48
112
|
if (!id.ok) return decideUnknownBuild(id.error.kind);
|
|
49
113
|
return id.value;
|
|
50
114
|
}
|
|
51
115
|
|
|
116
|
+
function resolveBuildId() {
|
|
117
|
+
return chooseBuildId(typeof __SQLEGO_BUILD__ === 'string' ? __SQLEGO_BUILD__ : undefined, stampedBuild, rustInTree(), buildIdFromSource);
|
|
118
|
+
}
|
|
119
|
+
|
|
52
120
|
/**
|
|
53
121
|
* A source run whose tree cannot be located or read has no build id; it
|
|
54
122
|
* identifies as `source`, the same as a run with no tree at all.
|