Unverified Commit 9240ffad authored by Hong Minhee's avatar Hong Minhee
Browse files

Add cache to CLI

parent 0cfc76fb
Loading
Loading
Loading
Loading

cli/cache.ts

0 → 100644
+17 −0
Original line number Diff line number Diff line
import { dir } from "@cross/dir";
import { ensureDir } from "@std/fs";
import { join } from "@std/path";

export const DEFAULT_CACHE_DIR = join(await dir("cache", true), "fedify");

let currentCacheDir: string = DEFAULT_CACHE_DIR;

export async function getCacheDir(): Promise<string> {
  await ensureDir(currentCacheDir);
  return currentCacheDir;
}

export function setCacheDir(dir: string): Promise<void> {
  currentCacheDir = dir;
  return Promise.resolve();
}

cli/docloader.ts

0 → 100644
+17 −0
Original line number Diff line number Diff line
import {
  type DocumentLoader,
  fetchDocumentLoader,
  kvCache,
} from "@fedify/fedify";
import { DenoKvStore } from "@fedify/fedify/x/denokv";
import { join } from "@std/path";
import { getCacheDir } from "./cache.ts";

export async function getDocumentLoader(): Promise<DocumentLoader> {
  const path = join(await getCacheDir(), "kv");
  const kv = new DenoKvStore(await Deno.openKv(path));
  return kvCache({
    kv,
    loader: fetchDocumentLoader,
  });
}
+12 −6
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ import {
} from "@fedify/fedify";
import { highlight } from "cli-highlight";
import ora from "ora";
import { getDocumentLoader } from "./docloader.ts";
import { spawnTemporaryServer, type TemporaryServer } from "./tempserver.ts";

export const command = new Command()
@@ -33,7 +34,8 @@ export const command = new Command()
      discardStdin: false,
    }).start();
    let server: TemporaryServer | undefined = undefined;
    let loader: DocumentLoader | undefined = undefined;
    const documentLoader = await getDocumentLoader();
    let authLoader: DocumentLoader | undefined = undefined;
    if (options.authorizedFetch) {
      spinner.text = "Generating a one-time key pair...";
      const key = await generateCryptoKeyPair();
@@ -68,20 +70,24 @@ export const command = new Command()
            inbox: new URL("/inbox", server?.url),
            outbox: new URL("/outbox", server?.url),
          }),
          { documentLoader },
        );
      });
      loader = getAuthenticatedDocumentLoader({
      authLoader = getAuthenticatedDocumentLoader({
        keyId: new URL("#main-key", server.url),
        privateKey: key.privateKey,
      });
    }
    try {
      spinner.text = "Looking up the object...";
      const object = await lookupObject(url, { documentLoader: loader });
      const object = await lookupObject(
        url,
        { documentLoader: authLoader ?? documentLoader },
      );
      spinner.succeed();
      if (object == null) {
        console.error("Failed to fetch the object.");
        if (loader == null) {
        if (authLoader == null) {
          console.error(
            "It may be a private object.  Try with -a/--authorized-fetch.",
          );
@@ -89,9 +95,9 @@ export const command = new Command()
        Deno.exit(1);
      }
      if (options.compact) {
        printJson(await object.toJsonLd());
        printJson(await object.toJsonLd({ documentLoader }));
      } else if (options.expand) {
        printJson(await object.toJsonLd({ expand: true }));
        printJson(await object.toJsonLd({ expand: true, documentLoader }));
      } else {
        console.log(object);
      }
+7 −0
Original line number Diff line number Diff line
import { Command, HelpCommand } from "@cliffy/command";
import { configure, getConsoleSink } from "@logtape/logtape";
import metadata from "../deno.json" with { type: "json" };
import { DEFAULT_CACHE_DIR, setCacheDir } from "./cache.ts";
import { command as lookup } from "./lookup.ts";

const command = new Command()
@@ -26,6 +27,12 @@ const command = new Command()
      });
    },
  })
  .globalOption("-c, --cache-dir=<dir:file>", "Set the cache directory.", {
    default: DEFAULT_CACHE_DIR,
    async action(options) {
      await setCacheDir(options.cacheDir);
    },
  })
  .default("help")
  .command("lookup", lookup)
  .command("help", new HelpCommand().global());
+2 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
    "@cfworker/json-schema": "npm:@cfworker/json-schema@^1.12.8",
    "@cliffy/command": "https://deno.land/x/cliffy@v1.0.0-rc.4/command/mod.ts",
    "@cliffy/prompt": "https://deno.land/x/cliffy@v1.0.0-rc.4/prompt/mod.ts",
    "@cross/dir": "jsr:@cross/dir@^1.1.0",
    "@david/which-runtime": "jsr:@david/which-runtime@^0.2.0",
    "@deno/dnt": "jsr:@deno/dnt@^0.41.1",
    "@fedify/fedify": "./mod.ts",
@@ -71,7 +72,7 @@
    "cache": "deno task codegen && deno cache mod.ts",
    "check": "deno task codegen && deno fmt --check && deno lint && deno check */*.ts",
    "codegen": "deno run --allow-read --allow-write --check codegen/main.ts vocab/ ../runtime/ > vocab/vocab.ts && deno fmt vocab/vocab.ts && deno cache vocab/vocab.ts && deno check vocab/vocab.ts",
    "cli": "deno task codegen && deno run --allow-read --allow-net --allow-env --allow-run cli/mod.ts",
    "cli": "deno task codegen && deno run --allow-read --allow-write --allow-net --allow-env --allow-run cli/mod.ts",
    "test-without-codegen": "deno test --check --doc --allow-read --allow-write --unstable-kv --trace-leaks",
    "test": "deno task codegen && deno task test-without-codegen",
    "coverage": "rm -rf coverage/ && deno task test --coverage && deno coverage --html coverage",