@fedify/fedify 2.0.0-pr.451.1731 → 2.0.0-pr.457.1754
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/{actor-GwnlqPRv.js → actor--EIM8WJq.js} +1 -1
- package/dist/{actor-CRQwUlsr.js → actor-BkClSt23.js} +185 -186
- package/dist/{actor-Ci-5y3ml.cjs → actor-C1V-Vbaa.cjs} +186 -187
- package/dist/{actor-Cd2CREO0.d.ts → actor-DqFajh9s.d.ts} +2 -2
- package/dist/{actor-DDhZuMGl.d.cts → actor-f2NtjyCg.d.cts} +2 -2
- package/dist/{builder-BflZmL4k.js → builder-Drp8J0wC.js} +3 -3
- package/dist/{client-Cle5j1T1.d.cts → client-94iWEfQa.d.cts} +1 -1
- package/dist/{client-Bkaw0_PR.d.ts → client-BsGzbnV-.d.ts} +1 -1
- package/dist/{client-DylapB-t.js → client-pY7-3icS.js} +1 -1
- package/dist/compat/mod.d.cts +11 -12
- package/dist/compat/mod.d.ts +11 -12
- package/dist/compat/transformers.test.js +16 -16
- package/dist/{context-DqFifMwj.d.ts → context-DG0huGW-.d.ts} +26 -56
- package/dist/{context-BOYzcFSe.d.cts → context-DJ8aSy2Q.d.cts} +26 -56
- package/dist/{docloader-BpcE9hBI.js → docloader-BhDPeZk3.js} +4 -5
- package/dist/{esm-DjbC-dsH.js → esm-D2byLwNf.js} +1 -1
- package/dist/federation/builder.test.js +4 -4
- package/dist/federation/collection.test.js +2 -2
- package/dist/federation/handler.test.js +17 -17
- package/dist/federation/idempotency.test.js +17 -17
- package/dist/federation/inbox.test.js +3 -3
- package/dist/federation/keycache.test.js +3 -3
- package/dist/federation/kv.test.js +2 -2
- package/dist/federation/middleware.test.js +18 -18
- package/dist/federation/mod.cjs +9 -10
- package/dist/federation/mod.d.cts +11 -12
- package/dist/federation/mod.d.ts +11 -12
- package/dist/federation/mod.js +9 -10
- package/dist/federation/mq.test.js +2 -2
- package/dist/federation/negotiation.test.js +2 -2
- package/dist/federation/retry.test.js +2 -2
- package/dist/federation/router.test.js +2 -2
- package/dist/federation/send.test.js +9 -9
- package/dist/{http-BE7aFuqn.d.ts → http-BbO0ejuk.d.ts} +1 -1
- package/dist/{http-CCUJdi0H.js → http-CQraRgim.js} +2 -2
- package/dist/{http-BzsavjR2.js → http-ChVy_Zs1.js} +2 -2
- package/dist/{http-DtUGyvkU.cjs → http-Dc-3AhVQ.cjs} +5 -5
- package/dist/{http-B6SKO6NJ.d.cts → http-M8k5mKc0.d.cts} +1 -1
- package/dist/{inbox-DizLjfhC.js → inbox-c2HespXf.js} +1 -1
- package/dist/{key-De1MJH3k.js → key-DfjYEehd.js} +2 -2
- package/dist/{keycache-D2HiBv4F.js → keycache-Bqod49Je.js} +1 -1
- package/dist/{keys-B4ZjS1Ot.js → keys-DpfNqWw3.js} +1 -1
- package/dist/kv-cache--YUzf-BE.cjs +134 -0
- package/dist/kv-cache-BHyEN0Z7.js +122 -0
- package/dist/kv-cache-DN9pfMBe.js +94 -0
- package/dist/{ld-Dux3_Cw2.js → ld-DxH5pJEy.js} +2 -2
- package/dist/{lookup-Bep-EOgr.js → lookup-C5pO70ss.js} +188 -298
- package/dist/lookup-CHu82YNt.cjs +272 -0
- package/dist/{lookup-Bql--V-C.js → lookup-UlFMEz4S.js} +128 -60
- package/dist/middleware-BPWWS7Li.js +15 -0
- package/dist/middleware-BZRfF01Z.cjs +15 -0
- package/dist/{middleware-DrpnHeGO.cjs → middleware-BjvG3FSg.cjs} +18 -19
- package/dist/{middleware-D7tP9Tq6.js → middleware-CnVcyowb.js} +13 -13
- package/dist/middleware-d26uv3fL.js +26 -0
- package/dist/{middleware-BBTmHKGh.js → middleware-f2BqmeAn.js} +8 -9
- package/dist/{mod-CLKu6Uo_.d.cts → mod-0noXw66R.d.cts} +4 -4
- package/dist/{mod-DLIidI_j.d.ts → mod-B2iOw50L.d.ts} +2 -2
- package/dist/{mod-Cm97bAiT.d.ts → mod-B9M-8jm2.d.ts} +4 -4
- package/dist/{mod-CRENK2dd.d.ts → mod-BlVovdcy.d.ts} +2 -3
- package/dist/{mod-CaWbCg0N.d.ts → mod-BoHnwOCs.d.ts} +2 -2
- package/dist/{mod--K7l84wp.d.cts → mod-BxRCHTz-.d.cts} +2 -3
- package/dist/{mod-DuclhZjh.d.cts → mod-C58MZ7Wx.d.cts} +1 -1
- package/dist/{mod-D9XZsft2.d.cts → mod-DJcZDvjA.d.cts} +2 -2
- package/dist/{mod-COlOrmr9.d.cts → mod-DgdBYYa0.d.cts} +2 -2
- package/dist/{mod-CB80AlIA.d.ts → mod-Ds0mpFZU.d.ts} +1 -1
- package/dist/mod.cjs +13 -15
- package/dist/mod.d.cts +15 -16
- package/dist/mod.d.ts +16 -17
- package/dist/mod.js +14 -15
- package/dist/nodeinfo/client.test.js +4 -4
- package/dist/nodeinfo/handler.test.js +16 -16
- package/dist/nodeinfo/mod.cjs +2 -3
- package/dist/nodeinfo/mod.d.cts +2 -3
- package/dist/nodeinfo/mod.d.ts +2 -3
- package/dist/nodeinfo/mod.js +2 -3
- package/dist/nodeinfo/types.test.js +2 -2
- package/dist/{owner-dkg5OpSC.d.cts → owner-B4HbyP8s.d.cts} +2 -2
- package/dist/{owner-Zq8du9WO.js → owner-BqWkxhC_.js} +2 -2
- package/dist/{owner-CktUdA0h.d.ts → owner-kQRGVXG1.d.ts} +2 -2
- package/dist/{proof-0PY1LDZY.cjs → proof-DHEZDAfy.cjs} +7 -7
- package/dist/{proof-aIlK5BTc.js → proof-DKHqWkww.js} +3 -3
- package/dist/{proof-C5S_3hdJ.js → proof-DjnaYscE.js} +2 -2
- package/dist/{send-BbVnlsQO.js → send-DG2jH3Yb.js} +2 -2
- package/dist/sig/http.test.js +7 -7
- package/dist/sig/key.test.js +5 -5
- package/dist/sig/ld.test.js +6 -6
- package/dist/sig/mod.cjs +5 -6
- package/dist/sig/mod.d.cts +5 -6
- package/dist/sig/mod.d.ts +5 -6
- package/dist/sig/mod.js +5 -6
- package/dist/sig/owner.test.js +6 -6
- package/dist/sig/proof.test.js +6 -6
- package/dist/testing/docloader.test.js +2 -2
- package/dist/testing/mod.d.ts +16 -72
- package/dist/testing/mod.js +2 -2
- package/dist/{testing-BkidWE0N.js → testing-CUOkRXmb.js} +1 -1
- package/dist/{types-CXnyjUdK.js → types-BtUjyi5y.js} +1 -1
- package/dist/{types-BDNYgodm.cjs → types-CWgzGaqk.cjs} +3 -3
- package/dist/utils/docloader.test.js +9 -8
- package/dist/utils/kv-cache.test.js +4 -3
- package/dist/utils/mod.cjs +5 -7
- package/dist/utils/mod.d.cts +5 -6
- package/dist/utils/mod.d.ts +5 -6
- package/dist/utils/mod.js +6 -7
- package/dist/vocab/actor.test.js +4 -4
- package/dist/vocab/lookup.test.js +3 -3
- package/dist/vocab/mod.cjs +3 -4
- package/dist/vocab/mod.d.cts +3 -4
- package/dist/vocab/mod.d.ts +3 -4
- package/dist/vocab/mod.js +3 -4
- package/dist/vocab/type.test.js +2 -2
- package/dist/vocab/vocab.test.js +3 -3
- package/dist/{vocab-Dapemq3v.cjs → vocab-DWCgeSMU.cjs} +3 -4
- package/dist/{vocab-BiKK7m2H.js → vocab-HZjpFucb.js} +2 -3
- package/dist/webfinger/handler.test.js +16 -16
- package/dist/webfinger/lookup.test.js +3 -3
- package/dist/webfinger/mod.cjs +2 -3
- package/dist/webfinger/mod.d.cts +1 -2
- package/dist/webfinger/mod.d.ts +1 -2
- package/dist/webfinger/mod.js +2 -3
- package/dist/x/cfworkers.d.cts +2 -2
- package/dist/x/cfworkers.d.ts +2 -2
- package/dist/x/cfworkers.test.js +2 -2
- package/dist/x/hono.d.cts +10 -11
- package/dist/x/hono.d.ts +10 -11
- package/dist/x/sveltekit.d.cts +10 -11
- package/dist/x/sveltekit.d.ts +10 -11
- package/package.json +4 -5
- package/dist/kv-cache-3VK2-wq_.cjs +0 -4279
- package/dist/kv-cache-DNvS-egZ.js +0 -4236
- package/dist/kv-cache-rR-Cnl95.js +0 -4267
- package/dist/lookup-BmGuy-Ps.cjs +0 -210
- package/dist/middleware-BUZSGsQH.js +0 -16
- package/dist/middleware-BusTq7Js.cjs +0 -16
- package/dist/middleware-tZdLtCWz.js +0 -26
- package/dist/request-BbHkedIU.d.cts +0 -35
- package/dist/request-Bc5CHBqO.cjs +0 -208
- package/dist/request-C0Ags1gQ.js +0 -184
- package/dist/request-U1t6zZtk.d.ts +0 -39
- package/dist/utils/request.test.d.ts +0 -3
- package/dist/utils/request.test.js +0 -48
- package/dist/utils/url.test.d.ts +0 -3
- package/dist/utils/url.test.js +0 -41
- /package/dist/{federation-DOuZpcAw.cjs → federation-CRpdnOMS.cjs} +0 -0
- /package/dist/{federation-pZJsapBn.js → federation-jcR8-ZxP.js} +0 -0
- /package/dist/{kv-Dt06smFt.d.ts → kv-BKNZ-Tb-.d.ts} +0 -0
- /package/dist/{kv-BMKIFXNW.d.cts → kv-Bxr0Q87_.d.cts} +0 -0
- /package/dist/{mod-FZd39qVq.d.cts → mod-B-hUPT2N.d.cts} +0 -0
- /package/dist/{mod-B-ZQqbGe.d.ts → mod-CVgZgliM.d.ts} +0 -0
- /package/dist/{mod-CjsiSl7_.d.ts → mod-xIj-IT58.d.ts} +0 -0
- /package/dist/{mq-CPRJk4mZ.d.ts → mq-CUKlBw08.d.ts} +0 -0
- /package/dist/{mq-B7R1Q-M5.d.cts → mq-DcJPkXD5.d.cts} +0 -0
- /package/dist/{nodeinfo-B0UefMXO.js → nodeinfo-BnthBobC.js} +0 -0
- /package/dist/{nodeinfo-ByTd7HpT.cjs → nodeinfo-CdN0rEnZ.cjs} +0 -0
- /package/dist/{sig-Dx59zAgn.js → sig-C34-oHBl.js} +0 -0
- /package/dist/{sig-DE_NC_VQ.cjs → sig-YYj5tCnr.cjs} +0 -0
- /package/dist/{utils-BXMGcmqK.js → utils-D-Va7aXC.js} +0 -0
- /package/dist/{utils-B6yXfxAQ.cjs → utils-DyRU1gdZ.cjs} +0 -0
- /package/dist/{vocab-ivDKb439.d.ts → vocab-BCWe1Ih5.d.ts} +0 -0
- /package/dist/{vocab-DCBw44JZ.d.cts → vocab-CeDBzu-f.d.cts} +0 -0
- /package/dist/{webfinger-qG8R5jc3.js → webfinger-C72Y8lrh.js} +0 -0
- /package/dist/{webfinger-C949bxcD.cjs → webfinger-vAtLmxOF.cjs} +0 -0
@@ -0,0 +1,134 @@
|
|
1
|
+
|
2
|
+
const { Temporal } = require("@js-temporal/polyfill");
|
3
|
+
const { URLPattern } = require("urlpattern-polyfill");
|
4
|
+
|
5
|
+
const require_chunk = require('./chunk-DqRYRqnO.cjs');
|
6
|
+
const require_http = require('./http-Dc-3AhVQ.cjs');
|
7
|
+
const __logtape_logtape = require_chunk.__toESM(require("@logtape/logtape"));
|
8
|
+
const __fedify_vocab_runtime = require_chunk.__toESM(require("@fedify/vocab-runtime"));
|
9
|
+
const es_toolkit = require_chunk.__toESM(require("es-toolkit"));
|
10
|
+
|
11
|
+
//#region src/utils/docloader.ts
|
12
|
+
const logger$1 = (0, __logtape_logtape.getLogger)([
|
13
|
+
"fedify",
|
14
|
+
"utils",
|
15
|
+
"docloader"
|
16
|
+
]);
|
17
|
+
/**
|
18
|
+
* Gets an authenticated {@link DocumentLoader} for the given identity.
|
19
|
+
* Note that an authenticated document loader intentionally does not cache
|
20
|
+
* the fetched documents.
|
21
|
+
* @param identity The identity to get the document loader for.
|
22
|
+
* The actor's key pair.
|
23
|
+
* @param options The options for the document loader.
|
24
|
+
* @returns The authenticated document loader.
|
25
|
+
* @throws {TypeError} If the key is invalid or unsupported.
|
26
|
+
* @since 0.4.0
|
27
|
+
*/
|
28
|
+
function getAuthenticatedDocumentLoader(identity, { allowPrivateAddress, userAgent, specDeterminer, tracerProvider } = {}) {
|
29
|
+
require_http.validateCryptoKey(identity.privateKey);
|
30
|
+
async function load(url, options) {
|
31
|
+
if (!allowPrivateAddress) try {
|
32
|
+
await (0, __fedify_vocab_runtime.validatePublicUrl)(url);
|
33
|
+
} catch (error) {
|
34
|
+
if (error instanceof __fedify_vocab_runtime.UrlError) logger$1.error("Disallowed private URL: {url}", {
|
35
|
+
url,
|
36
|
+
error
|
37
|
+
});
|
38
|
+
throw error;
|
39
|
+
}
|
40
|
+
const originalRequest = (0, __fedify_vocab_runtime.createActivityPubRequest)(url, { userAgent });
|
41
|
+
const response = await require_http.doubleKnock(originalRequest, identity, {
|
42
|
+
specDeterminer,
|
43
|
+
log: (0, es_toolkit.curry)(__fedify_vocab_runtime.logRequest)(logger$1),
|
44
|
+
tracerProvider,
|
45
|
+
signal: options?.signal
|
46
|
+
});
|
47
|
+
return (0, __fedify_vocab_runtime.getRemoteDocument)(url, response, load);
|
48
|
+
}
|
49
|
+
return load;
|
50
|
+
}
|
51
|
+
const _fetchDocumentLoader = (0, __fedify_vocab_runtime.getDocumentLoader)();
|
52
|
+
const _fetchDocumentLoader_allowPrivateAddress = (0, __fedify_vocab_runtime.getDocumentLoader)({ allowPrivateAddress: true });
|
53
|
+
|
54
|
+
//#endregion
|
55
|
+
//#region src/utils/kv-cache.ts
|
56
|
+
const logger = (0, __logtape_logtape.getLogger)([
|
57
|
+
"fedify",
|
58
|
+
"utils",
|
59
|
+
"kv-cache"
|
60
|
+
]);
|
61
|
+
/**
|
62
|
+
* Decorates a {@link DocumentLoader} with a cache backed by a {@link Deno.Kv}.
|
63
|
+
* @param parameters The parameters for the cache.
|
64
|
+
* @returns The decorated document loader which is cache-enabled.
|
65
|
+
*/
|
66
|
+
function kvCache({ loader, kv, prefix, rules }) {
|
67
|
+
const keyPrefix = prefix ?? ["_fedify", "remoteDocument"];
|
68
|
+
rules ??= [[new URLPattern({}), Temporal.Duration.from({ minutes: 5 })]];
|
69
|
+
for (const [p, duration] of rules) if (Temporal.Duration.compare(duration, { days: 30 }) > 0) throw new TypeError("The maximum cache duration is 30 days: " + (p instanceof URLPattern ? `${p.protocol}://${p.username}:${p.password}@${p.hostname}:${p.port}/${p.pathname}?${p.search}#${p.hash}` : p.toString()));
|
70
|
+
return async (url, options) => {
|
71
|
+
if (url in __fedify_vocab_runtime.preloadedContexts) {
|
72
|
+
logger.debug("Using preloaded context: {url}.", { url });
|
73
|
+
return {
|
74
|
+
contextUrl: null,
|
75
|
+
document: __fedify_vocab_runtime.preloadedContexts[url],
|
76
|
+
documentUrl: url
|
77
|
+
};
|
78
|
+
}
|
79
|
+
const match = matchRule(url, rules);
|
80
|
+
if (match == null) return await loader(url, options);
|
81
|
+
const key = [...keyPrefix, url];
|
82
|
+
let cache = void 0;
|
83
|
+
try {
|
84
|
+
cache = await kv.get(key);
|
85
|
+
} catch (error) {
|
86
|
+
if (error instanceof Error) logger.warn("Failed to get the document of {url} from the KV cache: {error}", {
|
87
|
+
url,
|
88
|
+
error
|
89
|
+
});
|
90
|
+
}
|
91
|
+
if (cache == null) {
|
92
|
+
const remoteDoc = await loader(url, options);
|
93
|
+
try {
|
94
|
+
await kv.set(key, remoteDoc, { ttl: match });
|
95
|
+
} catch (error) {
|
96
|
+
logger.warn("Failed to save the document of {url} to the KV cache: {error}", {
|
97
|
+
url,
|
98
|
+
error
|
99
|
+
});
|
100
|
+
}
|
101
|
+
return remoteDoc;
|
102
|
+
}
|
103
|
+
return cache;
|
104
|
+
};
|
105
|
+
}
|
106
|
+
function matchRule(url, rules) {
|
107
|
+
for (const [pattern, d] of rules) {
|
108
|
+
const duration = d instanceof Temporal.Duration ? d : Temporal.Duration.from(d);
|
109
|
+
if (typeof pattern === "string") {
|
110
|
+
if (url === pattern) return duration;
|
111
|
+
continue;
|
112
|
+
}
|
113
|
+
if (pattern instanceof URL) {
|
114
|
+
if (pattern.href == url) return duration;
|
115
|
+
continue;
|
116
|
+
}
|
117
|
+
if (pattern.test(url)) return duration;
|
118
|
+
}
|
119
|
+
return null;
|
120
|
+
}
|
121
|
+
|
122
|
+
//#endregion
|
123
|
+
Object.defineProperty(exports, 'getAuthenticatedDocumentLoader', {
|
124
|
+
enumerable: true,
|
125
|
+
get: function () {
|
126
|
+
return getAuthenticatedDocumentLoader;
|
127
|
+
}
|
128
|
+
});
|
129
|
+
Object.defineProperty(exports, 'kvCache', {
|
130
|
+
enumerable: true,
|
131
|
+
get: function () {
|
132
|
+
return kvCache;
|
133
|
+
}
|
134
|
+
});
|
@@ -0,0 +1,122 @@
|
|
1
|
+
|
2
|
+
import { Temporal } from "@js-temporal/polyfill";
|
3
|
+
import { URLPattern } from "urlpattern-polyfill";
|
4
|
+
|
5
|
+
import { doubleKnock, validateCryptoKey } from "./http-ChVy_Zs1.js";
|
6
|
+
import { getLogger } from "@logtape/logtape";
|
7
|
+
import { UrlError, createActivityPubRequest, getDocumentLoader, getRemoteDocument, logRequest, preloadedContexts, validatePublicUrl } from "@fedify/vocab-runtime";
|
8
|
+
import { curry } from "es-toolkit";
|
9
|
+
|
10
|
+
//#region src/utils/docloader.ts
|
11
|
+
const logger$1 = getLogger([
|
12
|
+
"fedify",
|
13
|
+
"utils",
|
14
|
+
"docloader"
|
15
|
+
]);
|
16
|
+
/**
|
17
|
+
* Gets an authenticated {@link DocumentLoader} for the given identity.
|
18
|
+
* Note that an authenticated document loader intentionally does not cache
|
19
|
+
* the fetched documents.
|
20
|
+
* @param identity The identity to get the document loader for.
|
21
|
+
* The actor's key pair.
|
22
|
+
* @param options The options for the document loader.
|
23
|
+
* @returns The authenticated document loader.
|
24
|
+
* @throws {TypeError} If the key is invalid or unsupported.
|
25
|
+
* @since 0.4.0
|
26
|
+
*/
|
27
|
+
function getAuthenticatedDocumentLoader(identity, { allowPrivateAddress, userAgent, specDeterminer, tracerProvider } = {}) {
|
28
|
+
validateCryptoKey(identity.privateKey);
|
29
|
+
async function load(url, options) {
|
30
|
+
if (!allowPrivateAddress) try {
|
31
|
+
await validatePublicUrl(url);
|
32
|
+
} catch (error) {
|
33
|
+
if (error instanceof UrlError) logger$1.error("Disallowed private URL: {url}", {
|
34
|
+
url,
|
35
|
+
error
|
36
|
+
});
|
37
|
+
throw error;
|
38
|
+
}
|
39
|
+
const originalRequest = createActivityPubRequest(url, { userAgent });
|
40
|
+
const response = await doubleKnock(originalRequest, identity, {
|
41
|
+
specDeterminer,
|
42
|
+
log: curry(logRequest)(logger$1),
|
43
|
+
tracerProvider,
|
44
|
+
signal: options?.signal
|
45
|
+
});
|
46
|
+
return getRemoteDocument(url, response, load);
|
47
|
+
}
|
48
|
+
return load;
|
49
|
+
}
|
50
|
+
const _fetchDocumentLoader = getDocumentLoader();
|
51
|
+
const _fetchDocumentLoader_allowPrivateAddress = getDocumentLoader({ allowPrivateAddress: true });
|
52
|
+
|
53
|
+
//#endregion
|
54
|
+
//#region src/utils/kv-cache.ts
|
55
|
+
const logger = getLogger([
|
56
|
+
"fedify",
|
57
|
+
"utils",
|
58
|
+
"kv-cache"
|
59
|
+
]);
|
60
|
+
/**
|
61
|
+
* Decorates a {@link DocumentLoader} with a cache backed by a {@link Deno.Kv}.
|
62
|
+
* @param parameters The parameters for the cache.
|
63
|
+
* @returns The decorated document loader which is cache-enabled.
|
64
|
+
*/
|
65
|
+
function kvCache({ loader, kv, prefix, rules }) {
|
66
|
+
const keyPrefix = prefix ?? ["_fedify", "remoteDocument"];
|
67
|
+
rules ??= [[new URLPattern({}), Temporal.Duration.from({ minutes: 5 })]];
|
68
|
+
for (const [p, duration] of rules) if (Temporal.Duration.compare(duration, { days: 30 }) > 0) throw new TypeError("The maximum cache duration is 30 days: " + (p instanceof URLPattern ? `${p.protocol}://${p.username}:${p.password}@${p.hostname}:${p.port}/${p.pathname}?${p.search}#${p.hash}` : p.toString()));
|
69
|
+
return async (url, options) => {
|
70
|
+
if (url in preloadedContexts) {
|
71
|
+
logger.debug("Using preloaded context: {url}.", { url });
|
72
|
+
return {
|
73
|
+
contextUrl: null,
|
74
|
+
document: preloadedContexts[url],
|
75
|
+
documentUrl: url
|
76
|
+
};
|
77
|
+
}
|
78
|
+
const match = matchRule(url, rules);
|
79
|
+
if (match == null) return await loader(url, options);
|
80
|
+
const key = [...keyPrefix, url];
|
81
|
+
let cache = void 0;
|
82
|
+
try {
|
83
|
+
cache = await kv.get(key);
|
84
|
+
} catch (error) {
|
85
|
+
if (error instanceof Error) logger.warn("Failed to get the document of {url} from the KV cache: {error}", {
|
86
|
+
url,
|
87
|
+
error
|
88
|
+
});
|
89
|
+
}
|
90
|
+
if (cache == null) {
|
91
|
+
const remoteDoc = await loader(url, options);
|
92
|
+
try {
|
93
|
+
await kv.set(key, remoteDoc, { ttl: match });
|
94
|
+
} catch (error) {
|
95
|
+
logger.warn("Failed to save the document of {url} to the KV cache: {error}", {
|
96
|
+
url,
|
97
|
+
error
|
98
|
+
});
|
99
|
+
}
|
100
|
+
return remoteDoc;
|
101
|
+
}
|
102
|
+
return cache;
|
103
|
+
};
|
104
|
+
}
|
105
|
+
function matchRule(url, rules) {
|
106
|
+
for (const [pattern, d] of rules) {
|
107
|
+
const duration = d instanceof Temporal.Duration ? d : Temporal.Duration.from(d);
|
108
|
+
if (typeof pattern === "string") {
|
109
|
+
if (url === pattern) return duration;
|
110
|
+
continue;
|
111
|
+
}
|
112
|
+
if (pattern instanceof URL) {
|
113
|
+
if (pattern.href == url) return duration;
|
114
|
+
continue;
|
115
|
+
}
|
116
|
+
if (pattern.test(url)) return duration;
|
117
|
+
}
|
118
|
+
return null;
|
119
|
+
}
|
120
|
+
|
121
|
+
//#endregion
|
122
|
+
export { getAuthenticatedDocumentLoader, kvCache };
|
@@ -0,0 +1,94 @@
|
|
1
|
+
|
2
|
+
import { Temporal } from "@js-temporal/polyfill";
|
3
|
+
import { URLPattern } from "urlpattern-polyfill";
|
4
|
+
globalThis.addEventListener = () => {};
|
5
|
+
|
6
|
+
import { preloadedContexts } from "@fedify/vocab-runtime";
|
7
|
+
import { getLogger } from "@logtape/logtape";
|
8
|
+
|
9
|
+
//#region src/utils/kv-cache.ts
|
10
|
+
const logger = getLogger([
|
11
|
+
"fedify",
|
12
|
+
"utils",
|
13
|
+
"kv-cache"
|
14
|
+
]);
|
15
|
+
/**
|
16
|
+
* A mock implementation of a key–value store for testing purposes.
|
17
|
+
*/
|
18
|
+
var MockKvStore = class {
|
19
|
+
#values = {};
|
20
|
+
get(key) {
|
21
|
+
return Promise.resolve(this.#values[JSON.stringify(key)]);
|
22
|
+
}
|
23
|
+
set(key, value, _options) {
|
24
|
+
this.#values[JSON.stringify(key)] = value;
|
25
|
+
return Promise.resolve();
|
26
|
+
}
|
27
|
+
async delete(_) {}
|
28
|
+
cas(..._) {
|
29
|
+
return Promise.resolve(false);
|
30
|
+
}
|
31
|
+
};
|
32
|
+
/**
|
33
|
+
* Decorates a {@link DocumentLoader} with a cache backed by a {@link Deno.Kv}.
|
34
|
+
* @param parameters The parameters for the cache.
|
35
|
+
* @returns The decorated document loader which is cache-enabled.
|
36
|
+
*/
|
37
|
+
function kvCache({ loader, kv, prefix, rules }) {
|
38
|
+
const keyPrefix = prefix ?? ["_fedify", "remoteDocument"];
|
39
|
+
rules ??= [[new URLPattern({}), Temporal.Duration.from({ minutes: 5 })]];
|
40
|
+
for (const [p, duration] of rules) if (Temporal.Duration.compare(duration, { days: 30 }) > 0) throw new TypeError("The maximum cache duration is 30 days: " + (p instanceof URLPattern ? `${p.protocol}://${p.username}:${p.password}@${p.hostname}:${p.port}/${p.pathname}?${p.search}#${p.hash}` : p.toString()));
|
41
|
+
return async (url, options) => {
|
42
|
+
if (url in preloadedContexts) {
|
43
|
+
logger.debug("Using preloaded context: {url}.", { url });
|
44
|
+
return {
|
45
|
+
contextUrl: null,
|
46
|
+
document: preloadedContexts[url],
|
47
|
+
documentUrl: url
|
48
|
+
};
|
49
|
+
}
|
50
|
+
const match = matchRule(url, rules);
|
51
|
+
if (match == null) return await loader(url, options);
|
52
|
+
const key = [...keyPrefix, url];
|
53
|
+
let cache = void 0;
|
54
|
+
try {
|
55
|
+
cache = await kv.get(key);
|
56
|
+
} catch (error) {
|
57
|
+
if (error instanceof Error) logger.warn("Failed to get the document of {url} from the KV cache: {error}", {
|
58
|
+
url,
|
59
|
+
error
|
60
|
+
});
|
61
|
+
}
|
62
|
+
if (cache == null) {
|
63
|
+
const remoteDoc = await loader(url, options);
|
64
|
+
try {
|
65
|
+
await kv.set(key, remoteDoc, { ttl: match });
|
66
|
+
} catch (error) {
|
67
|
+
logger.warn("Failed to save the document of {url} to the KV cache: {error}", {
|
68
|
+
url,
|
69
|
+
error
|
70
|
+
});
|
71
|
+
}
|
72
|
+
return remoteDoc;
|
73
|
+
}
|
74
|
+
return cache;
|
75
|
+
};
|
76
|
+
}
|
77
|
+
function matchRule(url, rules) {
|
78
|
+
for (const [pattern, d] of rules) {
|
79
|
+
const duration = d instanceof Temporal.Duration ? d : Temporal.Duration.from(d);
|
80
|
+
if (typeof pattern === "string") {
|
81
|
+
if (url === pattern) return duration;
|
82
|
+
continue;
|
83
|
+
}
|
84
|
+
if (pattern instanceof URL) {
|
85
|
+
if (pattern.href == url) return duration;
|
86
|
+
continue;
|
87
|
+
}
|
88
|
+
if (pattern.test(url)) return duration;
|
89
|
+
}
|
90
|
+
return null;
|
91
|
+
}
|
92
|
+
|
93
|
+
//#endregion
|
94
|
+
export { MockKvStore, kvCache };
|
@@ -3,8 +3,8 @@
|
|
3
3
|
import { URLPattern } from "urlpattern-polyfill";
|
4
4
|
globalThis.addEventListener = () => {};
|
5
5
|
|
6
|
-
import { Activity, CryptographicKey, Object as Object$1, deno_default, getTypeId } from "./lookup-
|
7
|
-
import { fetchKey, validateCryptoKey } from "./key-
|
6
|
+
import { Activity, CryptographicKey, Object as Object$1, deno_default, getTypeId } from "./lookup-C5pO70ss.js";
|
7
|
+
import { fetchKey, validateCryptoKey } from "./key-DfjYEehd.js";
|
8
8
|
import { getDocumentLoader } from "@fedify/vocab-runtime";
|
9
9
|
import { getLogger } from "@logtape/logtape";
|
10
10
|
import { SpanStatusCode, trace } from "@opentelemetry/api";
|