Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): Update context find logic in context-registry #943

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wet-pumas-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"counterfact": patch
---

Update context-registry find logic for bug with mixed path casing
15 changes: 14 additions & 1 deletion src/server/context-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,26 @@ export class ContextRegistry {
this.add("/", {});
}

private getContextIgnoreCase(map: Map<string, Context>, key: string) {
const lowerCaseKey = key.toLowerCase();
for (const currentKey of map.keys()) {
if (currentKey.toLowerCase() === lowerCaseKey) {
return map.get(currentKey);
}
}
return undefined;
}

public add(path: string, context: Context): void {
this.entries.set(path, context);
this.cache.set(path, structuredClone(context));
}

public find(path: string): Context {
return this.entries.get(path) ?? this.find(parentPath(path));
return (
this.getContextIgnoreCase(this.entries, path) ??
this.find(parentPath(path))
);
}

// eslint-disable-next-line max-statements
Expand Down
10 changes: 10 additions & 0 deletions test/server/context-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ describe("a context registry", () => {
expect(registry.find("/hello/world")).toBe(helloContext);
});

it("finds a context at a parent path with alternate casing", () => {
const helloContext = { name: "hello" };

const registry = new ContextRegistry();

registry.add("/hello", helloContext);

expect(registry.find("/Hello/world")).toBe(helloContext);
});

it("returns an empty object when there is no matching context", () => {
const helloContext = { name: "hello" };

Expand Down
Loading