forked from fedify-dev/fedify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocloader.ts
More file actions
87 lines (82 loc) · 2.12 KB
/
Copy pathdocloader.ts
File metadata and controls
87 lines (82 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { kvCache } from "@fedify/fedify";
import {
type DocumentLoader,
getDocumentLoader as getDefaultDocumentLoader,
} from "@fedify/vocab-runtime";
import { getKvStore } from "#kv";
const documentLoaders: Record<string, DocumentLoader> = {};
export interface DocumentLoaderOptions {
userAgent?: string;
allowPrivateAddress?: boolean;
}
/**
* Returns a cache prefix that separates document-loader entries by user agent
* and private-address policy.
*/
export function getDocumentLoaderCachePrefix(
userAgent: string | undefined,
allowPrivateAddress: boolean,
): readonly [string, ...string[]] {
return [
"_fedify",
"remoteDocument",
"cli",
userAgent ?? "",
allowPrivateAddress ? "allow-private" : "deny-private",
];
}
export async function getDocumentLoader(
{ userAgent, allowPrivateAddress = false }: DocumentLoaderOptions = {},
): Promise<DocumentLoader> {
const cacheKey = `${userAgent ?? ""}:${allowPrivateAddress}`;
if (documentLoaders[cacheKey]) return documentLoaders[cacheKey];
const kv = await getKvStore();
return documentLoaders[cacheKey] = kvCache({
kv,
prefix: getDocumentLoaderCachePrefix(userAgent, allowPrivateAddress),
rules: [
[
new URLPattern({
protocol: "http{s}?",
hostname: "localhost",
port: "*",
pathname: "/*",
search: "*",
hash: "*",
}),
{ seconds: 0 },
],
[
new URLPattern({
protocol: "http{s}?",
hostname: "127.0.0.1",
port: "*",
pathname: "/*",
search: "*",
hash: "*",
}),
{ seconds: 0 },
],
[
new URLPattern({
protocol: "http{s}?",
hostname: "\\[\\:\\:1\\]",
port: "*",
pathname: "/*",
search: "*",
hash: "*",
}),
{ seconds: 0 },
],
],
loader: getDefaultDocumentLoader({
allowPrivateAddress,
userAgent,
}),
});
}
export function getContextLoader(
options: DocumentLoaderOptions = {},
): Promise<DocumentLoader> {
return getDocumentLoader(options);
}