Skip to content

Commit

Permalink
Negotiate after determining if the resource exists
Browse files Browse the repository at this point in the history
Fix #34
  • Loading branch information
dahlia committed Apr 5, 2024
1 parent d62ee37 commit 7dcf4b2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ Version 0.5.1

To be released.

- Fixed a bug of `Federation` that its actor/collection dispatchers had done
content negotiation before determining if the resource exists or not.
It also fixed a bug that `integrateHandler()` from `@fedify/fedify/x/fresh`
had responded with `406 Not Acceptable` instead of `404 Not Found` when
the resource does not exist in the web browser. [[#34]]

[#34]: https://github.com/dahlia/fedify/issues/34


Version 0.5.0
-------------
Expand Down
30 changes: 30 additions & 0 deletions federation/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ Deno.test("handleActor()", async () => {
assertEquals(onNotAcceptableCalled, context.request);

onNotAcceptableCalled = null;
response = await handleActor(
context.request,
{
context,
handle: "no-one",
actorDispatcher,
onNotFound,
onNotAcceptable,
},
);
assertEquals(response.status, 404);
assertEquals(onNotFoundCalled, context.request);
assertEquals(onNotAcceptableCalled, null);

onNotFoundCalled = null;
context = createRequestContext<void>({
...context,
request: new Request(context.url, {
Expand Down Expand Up @@ -235,6 +250,21 @@ Deno.test("handleCollection()", async () => {
assertEquals(onNotAcceptableCalled, context.request);

onNotAcceptableCalled = null;
response = await handleCollection(
context.request,
{
context,
handle: "no-one",
collectionCallbacks: { dispatcher },
onNotFound,
onNotAcceptable,
},
);
assertEquals(response.status, 404);
assertEquals(onNotFoundCalled, context.request);
assertEquals(onNotAcceptableCalled, null);

onNotFoundCalled = null;
context = createRequestContext<void>({
...context,
request: new Request(context.url, {
Expand Down
16 changes: 8 additions & 8 deletions federation/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ export async function handleActor<TContextData>(
const response = onNotFound(request);
return response instanceof Promise ? await response : response;
}
if (!acceptsJsonLd(request)) {
const response = onNotAcceptable(request);
return response instanceof Promise ? await response : response;
}
const key = await context.getActorKey(handle);
const actor = await actorDispatcher(context, handle, key);
if (actor == null) {
const response = onNotFound(request);
return response instanceof Promise ? await response : response;
}
if (!acceptsJsonLd(request)) {
const response = onNotAcceptable(request);
return response instanceof Promise ? await response : response;
}
const jsonLd = await actor.toJsonLd(context);
return new Response(JSON.stringify(jsonLd), {
headers: {
Expand Down Expand Up @@ -122,10 +122,6 @@ export async function handleCollection<
const response = onNotFound(request);
return response instanceof Promise ? await response : response;
}
if (!acceptsJsonLd(request)) {
const response = onNotAcceptable(request);
return response instanceof Promise ? await response : response;
}
const url = new URL(request.url);
const cursor = url.searchParams.get("cursor");
let collection: OrderedCollection | OrderedCollectionPage;
Expand Down Expand Up @@ -200,6 +196,10 @@ export async function handleCollection<
partOf.searchParams.delete("cursor");
collection = new OrderedCollectionPage({ prev, next, items, partOf });
}
if (!acceptsJsonLd(request)) {
const response = onNotAcceptable(request);
return response instanceof Promise ? await response : response;
}
const jsonLd = await collection.toJsonLd(context);
return new Response(JSON.stringify(jsonLd), {
headers: {
Expand Down

0 comments on commit 7dcf4b2

Please sign in to comment.