Unverified Commit dbda4291 authored by Hong Minhee's avatar Hong Minhee
Browse files

AuthenticatedDocumentLoaderFactory type

parent ecb9175b
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -25,6 +25,9 @@ To be released.

     -  Added `Context.getDocumentLoader()` method.
     -  Added `getAuthenticatedDocumentLoader()` function.
     -  Added `AuthenticatedDocumentLoaderFactory` type.
     -  Added `authenticatedDocumentLoaderFactory` option to `new Federation()`
        constructor.

 -  Added singular accessors to `Object`'s `icon` and `image` properties.

+16 −0
Original line number Diff line number Diff line
@@ -76,6 +76,22 @@ See the
[*Getting a `DocumentLoader`* section](./context.md#getting-a-documentloader)
for details.

### `authenticatedDocumentLoaderFactory`

*This API is available since Fedify 0.4.0.*

A factory function that creates an authenticated document loader function.
The factory function takes the key pair of an actor and returns a document
loader function that loads remote JSON-LD documents as the actor.

Usually, you don't need to set this property because the default document
loader factory is sufficient for most cases.  The default document loader
factory intentionally doesn't cache the loaded documents in the key-value
store.

See the [*Getting an authenticated `DocumentLoader`*
section](./context.md#getting-an-authenticated-documentloader) for details.

### `treatHttps`

Whether to treat HTTP requests as HTTPS.  This is useful for testing and
+15 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ import { Temporal } from "@js-temporal/polyfill";
import { exportJwk, importJwk, validateCryptoKey } from "../httpsig/key.ts";
import { handleNodeInfo, handleNodeInfoJrd } from "../nodeinfo/handler.ts";
import {
  AuthenticatedDocumentLoaderFactory,
  DocumentLoader,
  fetchDocumentLoader,
  getAuthenticatedDocumentLoader,
@@ -52,6 +53,13 @@ export interface FederationParameters {
   */
  documentLoader?: DocumentLoader;

  /**
   * A factory function that creates an authenticated document loader for a
   * given identity.  This is used for fetching documents that require
   * authentication.
   */
  authenticatedDocumentLoaderFactory?: AuthenticatedDocumentLoaderFactory;

  /**
   * Whether to treat HTTP requests as HTTPS.  This is useful for testing and
   * local development.  However, it must be disabled in production.
@@ -103,6 +111,7 @@ export class Federation<TContextData> {
  >;
  #inboxErrorHandler?: InboxErrorHandler<TContextData>;
  #documentLoader: DocumentLoader;
  #authenticatedDocumentLoaderFactory: AuthenticatedDocumentLoaderFactory;
  #treatHttps: boolean;
  #backoffSchedule: number[];

@@ -115,6 +124,7 @@ export class Federation<TContextData> {
      kv,
      kvPrefixes,
      documentLoader,
      authenticatedDocumentLoaderFactory,
      treatHttps,
      backoffSchedule,
    }: FederationParameters,
@@ -136,6 +146,9 @@ export class Federation<TContextData> {
      kv: kv,
      prefix: this.#kvPrefixes.remoteDocument,
    });
    this.#authenticatedDocumentLoaderFactory =
      authenticatedDocumentLoaderFactory ??
        getAuthenticatedDocumentLoader;
    this.#treatHttps = treatHttps ?? false;
    if (backoffSchedule != null) {
      // TODO: Deno KV Queue's backoff schedule is too limited for our needs.
@@ -227,6 +240,8 @@ export class Federation<TContextData> {
        privateKey: keyPair.privateKey,
      };
    };
    const getAuthenticatedDocumentLoader =
      this.#authenticatedDocumentLoaderFactory;
    function getDocumentLoader(
      identity: { handle: string },
    ): Promise<DocumentLoader>;
+14 −0
Original line number Diff line number Diff line
@@ -14,9 +14,23 @@ export interface RemoteDocument {

/**
 * A JSON-LD document loader that fetches documents from the Web.
 * @param url The URL of the document to load.
 * @returns The loaded remote document.
 */
export type DocumentLoader = (url: string) => Promise<RemoteDocument>;

/**
 * A factory function that creates an authenticated {@link DocumentLoader} for
 * a given identity.  This is used for fetching documents that require
 * authentication.
 * @param identity The identity to create the document loader for.
 *                 The actor's key pair.
 * @returns The authenticated document loader.
 */
export type AuthenticatedDocumentLoaderFactory = (
  identity: { keyId: URL; privateKey: CryptoKey },
) => DocumentLoader;

/**
 * Error thrown when fetching a JSON-LD document failed.
 */