Unverified Commit 4751d9a7 authored by Hong Minhee's avatar Hong Minhee
Browse files

Separate some role of a doc loader to a ctx loader

parent 67c3a767
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -46,6 +46,26 @@ To be released.
        parameter became `CollectionDispatcher<Recipient, TContextData, URL>`
        (was `CollectionDispatcher<Actor | URL, TContextData>`).

 -  Some of the responsibility of a document loader was separated to a context
    loader and a document loader.

     -  Added `contextLoader` option to constructors, `fromJsonLd()` static
        methods, `clone()` methods, and all non-scalar accessors (`get*()`) of
        Activity Vocabulary classes.
     -  Renamed `documentLoader` option to `contextLoader` in `toJsonLd()`
        methods of Activity Vocabulary objects.
     -  Added `contextLoader` option to `LookupObjectOptions` interface.
     -  Added `contextLoader` property to `Context` interface.
     -  Added `contextLoader` option to `FederationParameters` interface.
     -  Renamed `documentLoader` option to `contextLoader` in
        `RespondWithObjectOptions` interface.
     -  Added `GetKeyOwnerOptions` interface.
     -  The type of the second parameter of `getKeyOwner()` function became
        `GetKeyOwnerOptions` (was `DocumentLoader`).
     -  Added `DoesActorOwnKeyOptions` interface.
     -  The type of the third parameter of `doesActorOwnKey()` function became
        `DoesActorOwnKeyOptions` (was `DocumentLoader`).

 -  Removed the dependency on *@js-temporal/polyfill* on Deno, and Fedify now
    requires `--unstable-temporal` flag.  On other runtime, it still depends
    on *@js-temporal/polyfill*.
+8 −1
Original line number Diff line number Diff line
@@ -7,11 +7,18 @@ import { DenoKvStore } from "@fedify/fedify/x/denokv";
import { join } from "@std/path";
import { getCacheDir } from "./cache.ts";

let documentLoader: DocumentLoader | undefined = undefined;

export async function getDocumentLoader(): Promise<DocumentLoader> {
  if (documentLoader) return documentLoader;
  const path = join(await getCacheDir(), "kv");
  const kv = new DenoKvStore(await Deno.openKv(path));
  return kvCache({
  return documentLoader = kvCache({
    kv,
    loader: fetchDocumentLoader,
  });
}

export function getContextLoader(): Promise<DocumentLoader> {
  return getDocumentLoader();
}
+3 −3
Original line number Diff line number Diff line
import type { Activity } from "@fedify/fedify";
import { getStatusText } from "@poppanator/http-constants";
import { getDocumentLoader } from "../docloader.ts";
import { getContextLoader, getDocumentLoader } from "../docloader.ts";

export async function renderRequest(request: Request): Promise<string> {
  request = request.clone();
@@ -43,8 +43,8 @@ export async function renderActivity(
  activity: Activity,
  expand: boolean = false,
): Promise<string> {
  const documentLoader = await getDocumentLoader();
  const jsonLd = await activity.toJsonLd({ documentLoader, expand });
  const contextLoader = await getContextLoader();
  const jsonLd = await activity.toJsonLd({ contextLoader, expand });
  return JSON.stringify(jsonLd, null, 2);
}

+6 −5
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ import {
} from "@fedify/fedify";
import { highlight } from "cli-highlight";
import ora from "ora";
import { getDocumentLoader } from "./docloader.ts";
import { getContextLoader, getDocumentLoader } from "./docloader.ts";
import { spawnTemporaryServer, type TemporaryServer } from "./tempserver.ts";

export const command = new Command()
@@ -35,6 +35,7 @@ export const command = new Command()
    }).start();
    let server: TemporaryServer | undefined = undefined;
    const documentLoader = await getDocumentLoader();
    const contextLoader = await getContextLoader();
    let authLoader: DocumentLoader | undefined = undefined;
    if (options.authorizedFetch) {
      spinner.text = "Generating a one-time key pair...";
@@ -71,7 +72,7 @@ export const command = new Command()
            inbox: new URL("/inbox", serverUrl),
            outbox: new URL("/outbox", serverUrl),
          }),
          { documentLoader },
          { contextLoader },
        );
      });
      authLoader = getAuthenticatedDocumentLoader({
@@ -83,7 +84,7 @@ export const command = new Command()
      spinner.text = "Looking up the object...";
      const object = await lookupObject(
        url,
        { documentLoader: authLoader ?? documentLoader },
        { documentLoader: authLoader ?? documentLoader, contextLoader },
      );
      spinner.succeed();
      if (object == null) {
@@ -96,9 +97,9 @@ export const command = new Command()
        Deno.exit(1);
      }
      if (options.compact) {
        printJson(await object.toJsonLd({ documentLoader }));
        printJson(await object.toJsonLd({ contextLoader }));
      } else if (options.expand) {
        printJson(await object.toJsonLd({ expand: true, documentLoader }));
        printJson(await object.toJsonLd({ expand: true, contextLoader }));
      } else {
        console.log(object);
      }
+2221 −633

File changed.

Preview size limit exceeded, changes collapsed.

Loading