mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
fix(config): treat an empty environment variable as unset
docker-compose.yml forwards its whitelist as "${NAME:-}", so a variable
the operator never set reaches the process as the empty string. Reads
written as a nullish fallback kept that string: the weather client built
an empty base URL and every geocode failed with ERR_INVALID_URL, and the
IP geo lookup requested a relative path.
Reads with a fallback now go through src/lib/env.ts (envValue, envOr,
envFlag), which treats empty and whitespace as unset. IP_GEO_LOOKUP_DISABLED
accepts 1, true, yes and on instead of only "1". A structural test refuses
a ?? fallback written directly against process.env anywhere in src/, and
the compose whitelist guard counts the helper's call shape as a read.
This commit is contained in:
1 parent
4af6b87a2a
commit
3c3e5ceab7
25 files changed
+337
-37
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
/**
|
||||
* Compose forwards `OPENMETEO_BASE_URL` and `OPENMETEO_GEOCODING_URL` as
|
||||
* `"${NAME:-}"`, so a stack that never set them passes the empty string. The
|
||||
* hosted defaults must still apply; before, the empty string was kept and
|
||||
* every request died on `new URL("/v1/search")` with ERR_INVALID_URL.
|
||||
*/
|
||||
beforeEach(() => {
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe("Open-Meteo base URLs under compose-style empty values", () => {
|
||||
it("uses the hosted geocoding endpoint when the variable is empty", async () => {
|
||||
vi.stubEnv("OPENMETEO_GEOCODING_URL", "");
|
||||
vi.stubEnv("OPENMETEO_BASE_URL", "");
|
||||
const fetchSpy = vi
|
||||
.fn()
|
||||
.mockResolvedValue(Response.json({ results: [] }, { status: 200 }));
|
||||
vi.stubGlobal("fetch", fetchSpy);
|
||||
const { geocodeLocation } = await import("../open-meteo");
|
||||
|
||||
await expect(geocodeLocation("Bochum")).resolves.toEqual([]);
|
||||
const url = String(fetchSpy.mock.calls[0]?.[0]);
|
||||
expect(
|
||||
url.startsWith("https://geocoding-api.open-meteo.com/v1/search?"),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("still honours a configured self-hosted endpoint", async () => {
|
||||
vi.stubEnv("OPENMETEO_GEOCODING_URL", "https://meteo.example.test/");
|
||||
const fetchSpy = vi
|
||||
.fn()
|
||||
.mockResolvedValue(Response.json({ results: [] }, { status: 200 }));
|
||||
vi.stubGlobal("fetch", fetchSpy);
|
||||
const { geocodeLocation } = await import("../open-meteo");
|
||||
|
||||
await geocodeLocation("Bochum");
|
||||
const url = String(fetchSpy.mock.calls[0]?.[0]);
|
||||
expect(url.startsWith("https://meteo.example.test/v1/search?")).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -18,17 +18,20 @@
|
||||
* settling), so callers fetch with a lookback window rather than "yesterday
|
||||
* only" — see the nightly job and the backfill route.
|
||||
*/
|
||||
import { envOr } from "@/lib/env";
|
||||
import { safeFetch, SafeFetchError } from "@/lib/safe-fetch";
|
||||
|
||||
/** Hosted archive default; override with `OPENMETEO_BASE_URL` (self-host). */
|
||||
const ARCHIVE_BASE_URL =
|
||||
process.env.OPENMETEO_BASE_URL?.replace(/\/$/, "") ??
|
||||
"https://archive-api.open-meteo.com";
|
||||
const ARCHIVE_BASE_URL = envOr(
|
||||
"OPENMETEO_BASE_URL",
|
||||
"https://archive-api.open-meteo.com",
|
||||
).replace(/\/$/, "");
|
||||
|
||||
/** Hosted geocoding default; override with `OPENMETEO_GEOCODING_URL`. */
|
||||
const GEOCODING_BASE_URL =
|
||||
process.env.OPENMETEO_GEOCODING_URL?.replace(/\/$/, "") ??
|
||||
"https://geocoding-api.open-meteo.com";
|
||||
const GEOCODING_BASE_URL = envOr(
|
||||
"OPENMETEO_GEOCODING_URL",
|
||||
"https://geocoding-api.open-meteo.com",
|
||||
).replace(/\/$/, "");
|
||||
|
||||
const FETCH_TIMEOUT_MS = 15_000;
|
||||
|
||||
|
||||
Reference in new issue
Block a user