fix(health): resolve app version in Nix bundle at runtime

The health endpoint was returning version 0.0.0 because readPackageVersion()
tried import.meta.require on a JSON path that Bun cannot bundle and the
package.json never shipped into the Nix store (installPhase only copied
dist/, home.html, schema.sql).

- flake.nix installPhase now copies package.json into $out/share/teleuploader/
- readPackageVersion() reads import.meta.dir/../package.json (bundle) or
  process.cwd()/package.json (dev) via readFileSync at runtime

Verified local resolution to 1.2.1; after this fixes deploy it will expose
the live semver on GET /health.
This commit is contained in:
asepharyana
2026-09-06 01:13:48 +07:00
parent 2ca1e67435
commit 6797dbada2
2 changed files with 20 additions and 18 deletions
+3
View File
@@ -61,6 +61,9 @@
cp -r dist $out/share/teleuploader/dist cp -r dist $out/share/teleuploader/dist
cp src/home.html $out/share/teleuploader/ 2>/dev/null || true cp src/home.html $out/share/teleuploader/ 2>/dev/null || true
cp schema.sql $out/share/teleuploader/ 2>/dev/null || true cp schema.sql $out/share/teleuploader/ 2>/dev/null || true
# Carry package.json into the store so runtime version lookup in
# env.ts readPackageVersion() resolves (dist/../package.json).
cp package.json $out/share/teleuploader/package.json
# Wrap with bun from Nix store (dependency sharing!) # Wrap with bun from Nix store (dependency sharing!)
# Note: NO --chdir — systemd WorkingDirectory controls this # Note: NO --chdir — systemd WorkingDirectory controls this
+17 -18
View File
@@ -1,3 +1,4 @@
import { readFileSync } from 'node:fs';
import logger from './shared/logger/index'; import logger from './shared/logger/index';
import { TELEGRAM_CHUNK_SIZE_MAX_BYTES } from './shared/utils/validation'; import { TELEGRAM_CHUNK_SIZE_MAX_BYTES } from './shared/utils/validation';
@@ -143,25 +144,23 @@ if (telegramChunkSizeBytes > TELEGRAM_CHUNK_SIZE_MAX_BYTES) {
* value always matches the deployed build. * value always matches the deployed build.
*/ */
const readPackageVersion = (): string => { const readPackageVersion = (): string => {
try { // Candidates in priority order. In the Nix bundle the app lives at
const candidates = [ // $out/share/teleuploader/dist/index.js, so the package.json that was
// node_modules resolution is only available when installed as a dep // copied alongside it is one level up from import.meta.dir. In a dev
import.meta.dir ? `${import.meta.dir}/../package.json` : undefined, // checkout it sits in the repo root (process.cwd()). Bun bundles JSON
`${process.cwd()}/package.json`, // imports statically, so we read the file at runtime instead.
]; const candidates = [
for (const candidate of candidates) { import.meta.dir ? `${import.meta.dir}/../package.json` : undefined,
if (!candidate) continue; `${process.cwd()}/package.json`,
try { ];
const pkg = JSON.parse(import.meta.require(candidate).default ?? '{}') as { for (const candidate of candidates) {
version?: string; if (!candidate) continue;
}; try {
if (pkg.version) return pkg.version; const parsed = JSON.parse(readFileSync(candidate, 'utf8')) as { version?: string };
} catch { if (parsed.version) return parsed.version;
// try next candidate } catch {
} // try next candidate
} }
} catch {
// fall through
} }
return '0.0.0'; return '0.0.0';
}; };