Skip to content

Commit 324a644

Browse files
committed
fix(server): keep malformed usage sources from disconnecting clients
1 parent 20363c3 commit 324a644

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { expect, it } from "@effect/vitest";
2+
import { ServerConfigStreamEvent, UsageLimitSourceId } from "@t3tools/contracts";
3+
import * as Effect from "effect/Effect";
4+
import * as Layer from "effect/Layer";
5+
import * as Schema from "effect/Schema";
6+
import { HttpClient } from "effect/unstable/http";
7+
8+
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
9+
import * as ServerSettings from "../serverSettings.ts";
10+
import * as UsageLimitSources from "./UsageLimitSources.ts";
11+
12+
const encodeConfigEvent = Schema.encodeSync(ServerConfigStreamEvent);
13+
14+
it.effect("keeps malformed source URLs encodable for config subscribers", () =>
15+
Effect.gen(function* () {
16+
const sources = yield* UsageLimitSources.make;
17+
yield* sources.refresh;
18+
const snapshots = yield* sources.current;
19+
expect(snapshots.map((source) => source.label)).toEqual([
20+
"local-hub",
21+
"opaque-hub",
22+
"Named hub",
23+
"hub.test:8317",
24+
"Usage limit source",
25+
]);
26+
expect(() =>
27+
encodeConfigEvent({
28+
version: 1,
29+
type: "usageLimitSourcesUpdated",
30+
payload: { sources: snapshots },
31+
}),
32+
).not.toThrow();
33+
expect(snapshots.every((source) => source.error === "No management key configured.")).toBe(
34+
true,
35+
);
36+
}).pipe(
37+
Effect.provide([
38+
ServerSettings.layerTest({
39+
usageLimitSources: {
40+
[UsageLimitSourceId.make("local-hub")]: {
41+
kind: "cliproxy",
42+
url: "localhost:8317",
43+
managementKey: "",
44+
enabled: true,
45+
},
46+
[UsageLimitSourceId.make("opaque-hub")]: {
47+
kind: "cliproxy",
48+
url: "file:///tmp/hub",
49+
managementKey: "",
50+
enabled: true,
51+
},
52+
[UsageLimitSourceId.make("named-hub")]: {
53+
kind: "cliproxy",
54+
url: "localhost:8317",
55+
label: "Named hub",
56+
managementKey: "",
57+
enabled: true,
58+
},
59+
[UsageLimitSourceId.make("valid-hub")]: {
60+
kind: "cliproxy",
61+
url: "https://hub.test:8317",
62+
managementKey: "",
63+
enabled: true,
64+
},
65+
[UsageLimitSourceId.make(" ")]: {
66+
kind: "cliproxy",
67+
url: "localhost:8317",
68+
managementKey: "",
69+
enabled: true,
70+
},
71+
},
72+
}),
73+
Layer.mock(BackgroundPolicy.BackgroundPolicy)({
74+
shouldRunScopeWork: () => Effect.succeed(false),
75+
}),
76+
Layer.succeed(
77+
HttpClient.HttpClient,
78+
HttpClient.make(() => Effect.die("Unexpected network request")),
79+
),
80+
]),
81+
Effect.scoped,
82+
),
83+
);

‎apps/server/src/usage/UsageLimitSources.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ export class UsageLimitSources extends Context.Service<
5353

5454
function sourceLabel(id: string, config: UsageLimitSourceConfig): string {
5555
if (config.label) return config.label;
56+
const fallback = id.trim() || "Usage limit source";
5657
try {
57-
return new URL(config.url).host;
58+
return new URL(config.url).host || fallback;
5859
} catch {
59-
return id;
60+
return fallback;
6061
}
6162
}
6263

0 commit comments

Comments
 (0)