Unverified Commit 86cf0ae4 authored by Hong Minhee's avatar Hong Minhee
Browse files

`Context.federation` property

parent 95a0d541
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -11,6 +11,9 @@ To be released. Note that 1.6.0 was skipped due to a mistake in the versioning.
 -  Added `Context.lookupWebFinger()` method to make WebFinger lookups
    accessible from the context.  [[#227]]

 -  Added `Context.federation` property to access the `Federation`
    object from the context.  [[#235]]

 -  Introduced `FederationBuilder` for creating a federation instance with
    a builder pattern.

@@ -39,6 +42,7 @@ To be released. Note that 1.6.0 was skipped due to a mistake in the versioning.
[RFC 9421]: https://www.rfc-editor.org/rfc/rfc9421
[#208]: https://github.com/fedify-dev/fedify/issues/208
[#227]: https://github.com/fedify-dev/fedify/issues/227
[#235]: https://github.com/fedify-dev/fedify/pull/235


Version 1.5.3
+23 −0
Original line number Diff line number Diff line
@@ -420,6 +420,29 @@ export const federation = await builder.build({
});
~~~~

If you want to access the `Federation` object inside dispatchers or listeners
before the `FederationBuilder` instantiates it, you can use
the `Context.federation` property.  The `Context.federation` property refers
to the `Federation` object that is to be instantiated by the `FederationBuilder`
and is available in the `Context` object passed to the dispatchers and
listeners.  For example, you can access the `Federation` object like this:

~~~~ typescript twoslash
import { type FederationBuilder, Person } from "@fedify/fedify";
const builder = null as unknown as FederationBuilder<void>;
// ---cut-before---
builder.setActorDispatcher(
  "/users/{handle}",
  async (ctx, handle) => {
    const federation = ctx.federation; // Access the `Federation` object
    // Omitted for brevity
// ---cut-start---
    return new Person({});
// ---cut-end---
  }
);
~~~~


The `~Federation.fetch()` API
-----------------------------
+7 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ import type {
} from "../vocab/vocab.ts";
import type { ResourceDescriptor } from "../webfinger/jrd.ts";
import type { LookupWebFingerOptions } from "../webfinger/lookup.ts";
import type { Federation } from "./federation.ts";
import type { SenderKeyPair } from "./send.ts";

/**
@@ -77,6 +78,12 @@ export interface Context<TContextData> {
   */
  readonly contextLoader: DocumentLoader;

  /**
   * The federation object that this context belongs to.
   * @since 1.6.0
   */
  readonly federation: Federation<TContextData>;

  /**
   * Builds the URI of the NodeInfo document.
   * @returns The NodeInfo URI.
+11 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import {
  respondWithObjectIfAcceptable,
} from "./handler.ts";
import { MemoryKvStore } from "./kv.ts";
import { createFederation } from "./middleware.ts";

test("acceptsJsonLd()", () => {
  assert(acceptsJsonLd(
@@ -68,7 +69,9 @@ test("acceptsJsonLd()", () => {
});

test("handleActor()", async () => {
  const federation = createFederation<void>({ kv: new MemoryKvStore() });
  let context = createRequestContext<void>({
    federation,
    data: undefined,
    url: new URL("https://example.com/"),
    getActorUri(identifier) {
@@ -332,7 +335,9 @@ test("handleActor()", async () => {
});

test("handleObject()", async () => {
  const federation = createFederation<void>({ kv: new MemoryKvStore() });
  let context = createRequestContext<void>({
    federation,
    data: undefined,
    url: new URL("https://example.com/"),
    getObjectUri(_cls, values) {
@@ -591,7 +596,9 @@ test("handleObject()", async () => {
});

test("handleCollection()", async () => {
  const federation = createFederation<void>({ kv: new MemoryKvStore() });
  let context = createRequestContext<void>({
    federation,
    data: undefined,
    url: new URL("https://example.com/"),
    getActorUri(identifier) {
@@ -1142,7 +1149,9 @@ test("handleInbox()", async () => {
    method: "POST",
    body: JSON.stringify(await activity.toJsonLd()),
  });
  const federation = createFederation<void>({ kv: new MemoryKvStore() });
  const unsignedContext = createRequestContext({
    federation,
    request: unsignedRequest,
    url: new URL(unsignedRequest.url),
    data: undefined,
@@ -1221,6 +1230,7 @@ test("handleInbox()", async () => {
    rsaPublicKey3.id!,
  );
  const signedContext = createRequestContext({
    federation,
    request: signedRequest,
    url: new URL(signedRequest.url),
    data: undefined,
@@ -1291,6 +1301,7 @@ test("handleInbox()", async () => {
    rsaPublicKey3.id!,
  );
  const signedInvalidContext = createRequestContext({
    federation,
    request: signedInvalidRequest,
    url: new URL(signedInvalidRequest.url),
    data: undefined,
+1 −0
Original line number Diff line number Diff line
@@ -235,6 +235,7 @@ test("Federation.createContext()", async (t) => {
    assertEquals(ctx.hostname, "example.com");
    assertStrictEquals(ctx.documentLoader, documentLoader);
    assertStrictEquals(ctx.contextLoader, mockDocumentLoader);
    assertStrictEquals(ctx.federation, federation);
    assertThrows(() => ctx.getNodeInfoUri(), RouterError);
    assertThrows(() => ctx.getActorUri("handle"), RouterError);
    assertThrows(
Loading