@noego/sqlego 0.0.0-stage → 0.0.1-dev.1
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 +128 -2
- package/bin/sqlego +4 -0
- package/dist/client.d.ts +125 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +870 -0
- package/dist/paths.d.ts +70 -0
- package/dist/protocol.d.ts +287 -0
- package/dist/version.d.ts +23 -0
- package/package.json +43 -4
- package/src/cli.ts +314 -0
- package/src/client.ts +621 -0
- package/src/index.ts +18 -0
- package/src/paths.ts +154 -0
- package/src/protocol.ts +477 -0
- package/src/server.ts +723 -0
- package/src/version.ts +78 -0
package/src/version.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { Result, Trust } from '@noego/trust';
|
|
6
|
+
import { errorMessage } from './protocol.ts';
|
|
7
|
+
import pkg from '../package.json' with { type: 'json' };
|
|
8
|
+
|
|
9
|
+
/** Stamped by `scripts/build.ts` (`--define`) into dist and the compiled binary; absent when running from source. */
|
|
10
|
+
declare const __SQLEGO_BUILD__: string | undefined;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Hash of every `src/*.ts` file, so the two artifacts built from one tree
|
|
14
|
+
* (Node client in dist/, Bun server in bin/) and a source run agree, while any
|
|
15
|
+
* edit yields a new id. `source` when the tree is not there.
|
|
16
|
+
*/
|
|
17
|
+
export function sourceBuildId(sourceDirectory: string) {
|
|
18
|
+
if (!existsSync(sourceDirectory)) return Result.ok('source');
|
|
19
|
+
return Trust.try(
|
|
20
|
+
() => {
|
|
21
|
+
const hash = createHash('sha1');
|
|
22
|
+
for (const name of readdirSync(sourceDirectory).filter((entry) => entry.endsWith('.ts')).sort()) {
|
|
23
|
+
hash.update(name);
|
|
24
|
+
hash.update(readFileSync(path.join(sourceDirectory, name)));
|
|
25
|
+
}
|
|
26
|
+
return hash.digest('hex').slice(0, 12);
|
|
27
|
+
},
|
|
28
|
+
(cause) => ({ kind: 'source-unreadable', message: `cannot hash sources in ${sourceDirectory}: ${errorMessage(cause)}` }),
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Exception-based form of `sourceBuildId` for the build script, which must stop on an unreadable tree. */
|
|
33
|
+
export function computeSourceBuildId(sourceDirectory: string): string {
|
|
34
|
+
const id = sourceBuildId(sourceDirectory);
|
|
35
|
+
if (id.ok) return id.value;
|
|
36
|
+
//trust:accept build script contract: an unhashable source tree must fail the build
|
|
37
|
+
throw new Error(id.error.message);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function resolveBuildId() {
|
|
41
|
+
if (typeof __SQLEGO_BUILD__ === 'string' && __SQLEGO_BUILD__.length > 0) return __SQLEGO_BUILD__;
|
|
42
|
+
const directory = Trust.try(
|
|
43
|
+
() => path.dirname(fileURLToPath(import.meta.url)),
|
|
44
|
+
(cause) => ({ kind: 'module-path', message: errorMessage(cause) }),
|
|
45
|
+
);
|
|
46
|
+
if (!directory.ok) return decideUnknownBuild(directory.error.kind);
|
|
47
|
+
const id = sourceBuildId(directory.value);
|
|
48
|
+
if (!id.ok) return decideUnknownBuild(id.error.kind);
|
|
49
|
+
return id.value;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A source run whose tree cannot be located or read has no build id; it
|
|
54
|
+
* identifies as `source`, the same as a run with no tree at all.
|
|
55
|
+
*/
|
|
56
|
+
function decideUnknownBuild(kind: 'module-path' | 'source-unreadable') {
|
|
57
|
+
switch (kind) {
|
|
58
|
+
case 'module-path': return 'source';
|
|
59
|
+
case 'source-unreadable': return 'source';
|
|
60
|
+
default: {
|
|
61
|
+
const unhandled: never = kind;
|
|
62
|
+
return unhandled;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Build identity of this artifact: the source hash it was built from. */
|
|
68
|
+
export const BUILD_ID: string = resolveBuildId();
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* What the client/server handshake compares: `<package version>+<build id>`.
|
|
72
|
+
* A rebuilt binary or dist — even at an unchanged package version — therefore
|
|
73
|
+
* retires owners started from the previous build instead of leaving stale
|
|
74
|
+
* code serving (2026-09-04: a framing fix shipped under the same version and
|
|
75
|
+
* the old owners kept running until stopped by hand).
|
|
76
|
+
* `SQLEGO_VERSION_OVERRIDE` lets a test start a deliberately stale server.
|
|
77
|
+
*/
|
|
78
|
+
export const VERSION: string = process.env.SQLEGO_VERSION_OVERRIDE || `${pkg.version}+${BUILD_ID}`;
|