Skip to content

Commit b93fb31

Browse files
committed
Keep deprecated Router.route() a nullable probe
Callers using the deprecated `Router` from `@fedify/fedify/federation` could pass arbitrary strings, including absolute URLs or otherwise non-path inputs, and get `null` back when they did not match. After the switch to `@fedify/uri-template`, the `assertPath(url)` call made the same probing code throw `RouterError` instead. Since this class is still the deprecated compatibility surface and this change ships in a minor release, that is a compatibility regression rather than a migration nudge. Replace `assertPath(url)` with an `isPath(url)` guard so `route()` again returns `null` for non-path inputs, while the underlying `@fedify/uri-template` router stays strict. `add()` is left throwing because the old `Router.add()` also threw on invalid templates. #758 (comment) #418 Assisted-by: Claude Code:claude-opus-4-7
1 parent 391d861 commit b93fb31

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

packages/fedify/src/federation/router.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ test("Router.build()", () => {
108108
);
109109
});
110110

111+
test("Router.route() returns null for non-path inputs", () => {
112+
const router = setUp();
113+
// The old Fedify 2.x `Router` returned `null` (not threw) when probed
114+
// with non-path inputs such as absolute URLs.
115+
assertEquals(router.route("https://example.com/users/alice"), null);
116+
assertEquals(router.route("users/alice"), null);
117+
assertEquals(router.route("not a path"), null);
118+
// Valid paths that simply do not match still return null.
119+
assertEquals(router.route("/unknown"), null);
120+
});
121+
111122
test("Compatibility between RouterErrors", () => {
112123
const newError = new UriTemplateRouterError("boom");
113124
assert(newError instanceof UriTemplateRouterError);

packages/fedify/src/federation/router.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
} from "@fedify/uri-template";
55
import {
66
assertPath,
7+
isPath,
78
Router as _Router,
89
RouterError as _RouterError,
910
} from "@fedify/uri-template";
@@ -112,11 +113,14 @@ export class Router {
112113
* @param url The URL to resolve.
113114
* @returns The name of the path and its values, if any match. Otherwise,
114115
* `null`.
115-
* @deprecated Use `Router` from `@fedify/uri-template` instead.
116+
* @deprecated Use `Router` from `@fedify/uri-template` instead. Unlike the
117+
* stricter `@fedify/uri-template` router, this compatibility
118+
* method keeps the old Fedify 2.x contract of returning `null`
119+
* (rather than throwing) for inputs that are not router paths.
116120
*/
117121
route(url: string): RouterRouteResult | null {
118122
return convertRouterError(() => {
119-
assertPath(url);
123+
if (!isPath(url)) return null;
120124
return this.#router.route(url);
121125
});
122126
}

0 commit comments

Comments
 (0)