Skip to content

Commit

Permalink
Merge pull request #943 from pmcelhaney/server-context-registry-find-fix
Browse files Browse the repository at this point in the history
fix(server): Update context find logic in context-registry
  • Loading branch information
pmcelhaney authored Jun 13, 2024
2 parents 02960d9 + 2ff9fdf commit e5602fb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
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

0 comments on commit e5602fb

Please sign in to comment.