@noego/sqlego 0.1.6-dev.7 → 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/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, 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
+ * 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
- function resolveBuildId() {
41
- if (typeof __SQLEGO_BUILD__ === 'string' && __SQLEGO_BUILD__.length > 0) return __SQLEGO_BUILD__;
42
- const directory = Trust.try(
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.