Skip to content
TypeScript

fix(server): usage hubs with a host-less URL no longer vanish from Limits (#11368)

shivam6002c5c68c63
diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts
new file mode 100644
index 00000000000..b8976fca81e
--- /dev/null
+++ b/apps/server/src/usage/UsageLimitSources.test.ts
@@ -0,0 +1,85 @@
+import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
+import { expect, it } from "@effect/vitest";
+import { ServerConfigStreamEvent, UsageLimitSourceId } from "@t3tools/contracts";
+import * as Effect from "effect/Effect";
+import * as Layer from "effect/Layer";
+import * as Schema from "effect/Schema";
+import { HttpClient } from "effect/http";
+
+import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
+import * as ServerSettings from "../serverSettings.ts";
+import * as UsageLimitSources from "./UsageLimitSources.ts";
+
+const encodeConfigEvent = Schema.encodeSync(ServerConfigStreamEvent);
+
+it.effect("keeps malformed source URLs encodable for config subscribers", () =>
+ Effect.gen(function* () {
+ const sources = yield* UsageLimitSources.make;
+ yield* sources.refresh;
+ const snapshots = yield* sources.current;
+ expect(snapshots.map((source) => source.label)).toEqual([
+ "local-hub",
+ "opaque-hub",
+ "Named hub",
+ "hub.test:8317",
+ "Usage limit source",
+ ]);
+ expect(() =>
+ encodeConfigEvent({
+ version: 1,
+ type: "usageLimitSourcesUpdated",
+ payload: { sources: snapshots },
+ }),
+ ).not.toThrow();
+ expect(snapshots.every((source) => source.error === "No management key configured.")).toBe(
+ true,
+ );
+ }).pipe(
+ Effect.provide([
+ ServerSettings.layerTest({
+ usageLimitSources: {
+ [UsageLimitSourceId.make("local-hub")]: {
+ kind: "cliproxy",
+ url: "localhost:8317",
+ managementKey: "",
+ enabled: true,
+ },
+ [UsageLimitSourceId.make("opaque-hub")]: {
+ kind: "cliproxy",
+ url: "file:///tmp/hub",
+ managementKey: "",
+ enabled: true,
+ },
+ [UsageLimitSourceId.make("named-hub")]: {
+ kind: "cliproxy",
+ url: "localhost:8317",
+ label: "Named hub",
+ managementKey: "",
+ enabled: true,
+ },
+ [UsageLimitSourceId.make("valid-hub")]: {
+ kind: "cliproxy",
+ url: "https://hub.test:8317",
+ managementKey: "",
+ enabled: true,
+ },
+ [UsageLimitSourceId.make(" ")]: {
+ kind: "cliproxy",
+ url: "localhost:8317",
+ managementKey: "",
+ enabled: true,
+ },
+ },
+ }),
+ Layer.mock(BackgroundPolicy.BackgroundPolicy)({
+ shouldRunScopeWork: () => Effect.succeed(false),
+ }),
+ Layer.succeed(
+ HttpClient.HttpClient,
+ HttpClient.make(() => Effect.die("Unexpected network request")),
+ ),
+ NodeCrypto.layer,
+ ]),
+ Effect.scoped,
+ ),
+);
diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts
index da0138ae2f5..453ab59160e 100644
--- a/apps/server/src/usage/UsageLimitSources.ts
+++ b/apps/server/src/usage/UsageLimitSources.ts
@@ -53,10 +53,11 @@ export class UsageLimitSources extends Context.Service<
function sourceLabel(id: string, config: UsageLimitSourceConfig): string {
if (config.label) return config.label;
+ const fallback = id.trim() || "Usage limit source";
try {
- return new URL(config.url).host;
+ return new URL(config.url).host || fallback;
} catch {
- return id;
+ return fallback;
}
}