Skip to content

Commit 4751d9a

Browse files
committed
Separate some role of a doc loader to a ctx loader
1 parent 67c3a76 commit 4751d9a

25 files changed

+2565
-750
lines changed

Diff for: CHANGES.md

+20
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ To be released.
4646
parameter became `CollectionDispatcher<Recipient, TContextData, URL>`
4747
(was `CollectionDispatcher<Actor | URL, TContextData>`).
4848

49+
- Some of the responsibility of a document loader was separated to a context
50+
loader and a document loader.
51+
52+
- Added `contextLoader` option to constructors, `fromJsonLd()` static
53+
methods, `clone()` methods, and all non-scalar accessors (`get*()`) of
54+
Activity Vocabulary classes.
55+
- Renamed `documentLoader` option to `contextLoader` in `toJsonLd()`
56+
methods of Activity Vocabulary objects.
57+
- Added `contextLoader` option to `LookupObjectOptions` interface.
58+
- Added `contextLoader` property to `Context` interface.
59+
- Added `contextLoader` option to `FederationParameters` interface.
60+
- Renamed `documentLoader` option to `contextLoader` in
61+
`RespondWithObjectOptions` interface.
62+
- Added `GetKeyOwnerOptions` interface.
63+
- The type of the second parameter of `getKeyOwner()` function became
64+
`GetKeyOwnerOptions` (was `DocumentLoader`).
65+
- Added `DoesActorOwnKeyOptions` interface.
66+
- The type of the third parameter of `doesActorOwnKey()` function became
67+
`DoesActorOwnKeyOptions` (was `DocumentLoader`).
68+
4969
- Removed the dependency on *@js-temporal/polyfill* on Deno, and Fedify now
5070
requires `--unstable-temporal` flag. On other runtime, it still depends
5171
on *@js-temporal/polyfill*.

Diff for: cli/docloader.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@ import { DenoKvStore } from "@fedify/fedify/x/denokv";
77
import { join } from "@std/path";
88
import { getCacheDir } from "./cache.ts";
99

10+
let documentLoader: DocumentLoader | undefined = undefined;
11+
1012
export async function getDocumentLoader(): Promise<DocumentLoader> {
13+
if (documentLoader) return documentLoader;
1114
const path = join(await getCacheDir(), "kv");
1215
const kv = new DenoKvStore(await Deno.openKv(path));
13-
return kvCache({
16+
return documentLoader = kvCache({
1417
kv,
1518
loader: fetchDocumentLoader,
1619
});
1720
}
21+
22+
export function getContextLoader(): Promise<DocumentLoader> {
23+
return getDocumentLoader();
24+
}

Diff for: cli/inbox/rendercode.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Activity } from "@fedify/fedify";
22
import { getStatusText } from "@poppanator/http-constants";
3-
import { getDocumentLoader } from "../docloader.ts";
3+
import { getContextLoader, getDocumentLoader } from "../docloader.ts";
44

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

Diff for: cli/lookup.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from "@fedify/fedify";
1212
import { highlight } from "cli-highlight";
1313
import ora from "ora";
14-
import { getDocumentLoader } from "./docloader.ts";
14+
import { getContextLoader, getDocumentLoader } from "./docloader.ts";
1515
import { spawnTemporaryServer, type TemporaryServer } from "./tempserver.ts";
1616

1717
export const command = new Command()
@@ -35,6 +35,7 @@ export const command = new Command()
3535
}).start();
3636
let server: TemporaryServer | undefined = undefined;
3737
const documentLoader = await getDocumentLoader();
38+
const contextLoader = await getContextLoader();
3839
let authLoader: DocumentLoader | undefined = undefined;
3940
if (options.authorizedFetch) {
4041
spinner.text = "Generating a one-time key pair...";
@@ -71,7 +72,7 @@ export const command = new Command()
7172
inbox: new URL("/inbox", serverUrl),
7273
outbox: new URL("/outbox", serverUrl),
7374
}),
74-
{ documentLoader },
75+
{ contextLoader },
7576
);
7677
});
7778
authLoader = getAuthenticatedDocumentLoader({
@@ -83,7 +84,7 @@ export const command = new Command()
8384
spinner.text = "Looking up the object...";
8485
const object = await lookupObject(
8586
url,
86-
{ documentLoader: authLoader ?? documentLoader },
87+
{ documentLoader: authLoader ?? documentLoader, contextLoader },
8788
);
8889
spinner.succeed();
8990
if (object == null) {
@@ -96,9 +97,9 @@ export const command = new Command()
9697
Deno.exit(1);
9798
}
9899
if (options.compact) {
99-
printJson(await object.toJsonLd({ documentLoader }));
100+
printJson(await object.toJsonLd({ contextLoader }));
100101
} else if (options.expand) {
101-
printJson(await object.toJsonLd({ expand: true, documentLoader }));
102+
printJson(await object.toJsonLd({ expand: true, contextLoader }));
102103
} else {
103104
console.log(object);
104105
}

0 commit comments

Comments
 (0)