@fedify/fedify 2.5.0-dev.2687 → 2.5.0-dev.2726

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.
Files changed (244) hide show
  1. package/dist/federation/mod.cjs +1 -1
  2. package/dist/federation/mod.js +1 -1
  3. package/dist/{federation-pFFwPyoZ.cjs → federation-C0zalE9W.cjs} +8 -3
  4. package/dist/{http-T7h9GBAN.js → http-BC5Bd3-E.js} +1 -1
  5. package/dist/{http-CXX-QPzs.cjs → http-Dqzgc4Rj.cjs} +1 -1
  6. package/dist/{kv-cache-EFcb1kkU.js → kv-cache-CQ0u0Ry0.js} +1 -1
  7. package/dist/{kv-cache-C6XX_fwz.cjs → kv-cache-ihEHdx1v.cjs} +1 -1
  8. package/dist/{middleware-Bj91r7r2.js → middleware-Bgm3A90j.js} +8 -3
  9. package/dist/mod.cjs +4 -4
  10. package/dist/mod.js +4 -4
  11. package/dist/{portable-collection-Do5QA2oy.js → portable-collection-DZ_XqNUM.js} +1 -1
  12. package/dist/{portable-collection-BOl9oKrt.cjs → portable-collection-nvog8lS6.cjs} +1 -1
  13. package/dist/sig/mod.cjs +2 -2
  14. package/dist/sig/mod.js +2 -2
  15. package/dist/utils/mod.cjs +1 -1
  16. package/dist/utils/mod.js +1 -1
  17. package/package.json +22 -9
  18. package/skills/fedify/SKILL.md +35 -0
  19. package/dist/accept-CPVqki78.mjs +0 -145
  20. package/dist/activity-listener-CELFAodc.mjs +0 -44
  21. package/dist/assert-D1Eafmd_.mjs +0 -23
  22. package/dist/assert_equals-Z6IcEbWU.mjs +0 -987
  23. package/dist/assert_exists-cffSpJ7X.mjs +0 -28
  24. package/dist/assert_false-CPdRnc3E.mjs +0 -23
  25. package/dist/assert_greater_or_equal-BlafOF4-.mjs +0 -31
  26. package/dist/assert_instance_of-mrXWWNY-.mjs +0 -37
  27. package/dist/assert_not_equals-CpPK2t89.mjs +0 -30
  28. package/dist/assert_rejects-CTptdYck.mjs +0 -39
  29. package/dist/assert_strict_equals-CYvBmkpC.mjs +0 -42
  30. package/dist/assert_throws-BYITOvPO.mjs +0 -74
  31. package/dist/assertion_error-B-cx0fFA.mjs +0 -31
  32. package/dist/audience-CHQ-FlLD.mjs +0 -95
  33. package/dist/builder-D1FP_5nQ.mjs +0 -911
  34. package/dist/circuit-breaker-XJ53md4S.mjs +0 -544
  35. package/dist/circuit-breaker-test-utils-DW6YC_Pe.mjs +0 -14
  36. package/dist/client-5SIFzW3q.mjs +0 -441
  37. package/dist/codec-C6rTrpIp.mjs +0 -147
  38. package/dist/collection-CnnP1UAv.mjs +0 -44
  39. package/dist/compat/outgoing-jsonld.test.d.mts +0 -2
  40. package/dist/compat/outgoing-jsonld.test.mjs +0 -257
  41. package/dist/compat/public-audience.test.d.mts +0 -2
  42. package/dist/compat/public-audience.test.mjs +0 -187
  43. package/dist/compat/secured-document.test.d.mts +0 -2
  44. package/dist/compat/secured-document.test.mjs +0 -58
  45. package/dist/compat/transformers.test.d.mts +0 -2
  46. package/dist/compat/transformers.test.mjs +0 -62
  47. package/dist/compound-proof-DSHhnOEW.mjs +0 -1019
  48. package/dist/context-LgGFFSz7.mjs +0 -146
  49. package/dist/deno-C50ikGBU.mjs +0 -8
  50. package/dist/dist-_geWsuaI.mjs +0 -189
  51. package/dist/docloader-BY-SEID0.mjs +0 -136
  52. package/dist/esm-BHXXx4Wx.mjs +0 -1233
  53. package/dist/exporter-DVG68ozH.mjs +0 -263
  54. package/dist/federation/audience.test.d.mts +0 -2
  55. package/dist/federation/audience.test.mjs +0 -130
  56. package/dist/federation/builder.test.d.mts +0 -2
  57. package/dist/federation/builder.test.mjs +0 -396
  58. package/dist/federation/circuit-breaker.test.d.mts +0 -2
  59. package/dist/federation/circuit-breaker.test.mjs +0 -1277
  60. package/dist/federation/collection.test.d.mts +0 -2
  61. package/dist/federation/collection.test.mjs +0 -21
  62. package/dist/federation/compound-fanout.test.d.mts +0 -2
  63. package/dist/federation/compound-fanout.test.mjs +0 -231
  64. package/dist/federation/handler-compound-proof.test.d.mts +0 -2
  65. package/dist/federation/handler-compound-proof.test.mjs +0 -384
  66. package/dist/federation/handler.test.d.mts +0 -2
  67. package/dist/federation/handler.test.mjs +0 -5445
  68. package/dist/federation/hashlink-media.test.d.mts +0 -2
  69. package/dist/federation/hashlink-media.test.mjs +0 -422
  70. package/dist/federation/idempotency.test.d.mts +0 -2
  71. package/dist/federation/idempotency.test.mjs +0 -207
  72. package/dist/federation/inbox-actorless.test.d.mts +0 -2
  73. package/dist/federation/inbox-actorless.test.mjs +0 -522
  74. package/dist/federation/inbox-authentication.test.d.mts +0 -2
  75. package/dist/federation/inbox-authentication.test.mjs +0 -220
  76. package/dist/federation/inbox-report.test.d.mts +0 -2
  77. package/dist/federation/inbox-report.test.mjs +0 -719
  78. package/dist/federation/inbox-view.test.d.mts +0 -2
  79. package/dist/federation/inbox-view.test.mjs +0 -369
  80. package/dist/federation/inbox.test.d.mts +0 -2
  81. package/dist/federation/inbox.test.mjs +0 -139
  82. package/dist/federation/keycache.test.d.mts +0 -2
  83. package/dist/federation/keycache.test.mjs +0 -337
  84. package/dist/federation/kv.test.d.mts +0 -2
  85. package/dist/federation/kv.test.mjs +0 -88
  86. package/dist/federation/metrics.test.d.mts +0 -2
  87. package/dist/federation/metrics.test.mjs +0 -634
  88. package/dist/federation/middleware.test.d.mts +0 -2
  89. package/dist/federation/middleware.test.mjs +0 -9314
  90. package/dist/federation/mq.test.d.mts +0 -2
  91. package/dist/federation/mq.test.mjs +0 -591
  92. package/dist/federation/negotiation.test.d.mts +0 -2
  93. package/dist/federation/negotiation.test.mjs +0 -18
  94. package/dist/federation/outbound-body.test.d.mts +0 -2
  95. package/dist/federation/outbound-body.test.mjs +0 -149
  96. package/dist/federation/outbound-capture.test.d.mts +0 -2
  97. package/dist/federation/outbound-capture.test.mjs +0 -394
  98. package/dist/federation/outbound-dispatch.test.d.mts +0 -2
  99. package/dist/federation/outbound-dispatch.test.mjs +0 -368
  100. package/dist/federation/outbound-document.test.d.mts +0 -2
  101. package/dist/federation/outbound-document.test.mjs +0 -171
  102. package/dist/federation/outbound-provenance.test.d.mts +0 -2
  103. package/dist/federation/outbound-provenance.test.mjs +0 -543
  104. package/dist/federation/outbound-report.test.d.mts +0 -2
  105. package/dist/federation/outbound-report.test.mjs +0 -5475
  106. package/dist/federation/outbound-workers.test.d.mts +0 -2
  107. package/dist/federation/outbound-workers.test.mjs +0 -64
  108. package/dist/federation/outbound-workflow.test.d.mts +0 -2
  109. package/dist/federation/outbound-workflow.test.mjs +0 -154
  110. package/dist/federation/outgoing-proof.test.d.mts +0 -2
  111. package/dist/federation/outgoing-proof.test.mjs +0 -933
  112. package/dist/federation/portable-actor.test.d.mts +0 -2
  113. package/dist/federation/portable-actor.test.mjs +0 -496
  114. package/dist/federation/portable-chain.test.d.mts +0 -2
  115. package/dist/federation/portable-chain.test.mjs +0 -370
  116. package/dist/federation/portable-collection.test.d.mts +0 -2
  117. package/dist/federation/portable-collection.test.mjs +0 -832
  118. package/dist/federation/portable-delivery.test.d.mts +0 -2
  119. package/dist/federation/portable-delivery.test.mjs +0 -845
  120. package/dist/federation/portable-gateway-keys.test.d.mts +0 -2
  121. package/dist/federation/portable-gateway-keys.test.mjs +0 -692
  122. package/dist/federation/portable-inbox.test.d.mts +0 -2
  123. package/dist/federation/portable-inbox.test.mjs +0 -1960
  124. package/dist/federation/portable-parse-uri.test.d.mts +0 -2
  125. package/dist/federation/portable-parse-uri.test.mjs +0 -348
  126. package/dist/federation/portable-route-activity.test.d.mts +0 -2
  127. package/dist/federation/portable-route-activity.test.mjs +0 -259
  128. package/dist/federation/portable.test.d.mts +0 -2
  129. package/dist/federation/portable.test.mjs +0 -1072
  130. package/dist/federation/retry.test.d.mts +0 -2
  131. package/dist/federation/retry.test.mjs +0 -42
  132. package/dist/federation/router.test.d.mts +0 -2
  133. package/dist/federation/router.test.mjs +0 -123
  134. package/dist/federation/send.test.d.mts +0 -2
  135. package/dist/federation/send.test.mjs +0 -1421
  136. package/dist/federation/tasks/codec.test.d.mts +0 -2
  137. package/dist/federation/tasks/codec.test.mjs +0 -302
  138. package/dist/federation/tasks/enqueue.test.d.mts +0 -2
  139. package/dist/federation/tasks/enqueue.test.mjs +0 -1095
  140. package/dist/federation/tasks/tasks.test.d.mts +0 -2
  141. package/dist/federation/tasks/tasks.test.mjs +0 -1229
  142. package/dist/federation/temporal.test.d.mts +0 -2
  143. package/dist/federation/temporal.test.mjs +0 -71
  144. package/dist/federation/webfinger.test.d.mts +0 -2
  145. package/dist/federation/webfinger.test.mjs +0 -944
  146. package/dist/http-BXbQQZDM.mjs +0 -1449
  147. package/dist/inbox-authentication-CgonVRkg.mjs +0 -198
  148. package/dist/inbox-cS97XzOD.mjs +0 -399
  149. package/dist/inbox-view-D-d0Ar6o.mjs +0 -385
  150. package/dist/key-owner-evidence-APB6NpSk.mjs +0 -108
  151. package/dist/keycache-N2opu4-3.mjs +0 -221
  152. package/dist/keys-DIJNHW-Y.mjs +0 -120
  153. package/dist/kv-DBRWVx3Q.mjs +0 -104
  154. package/dist/kv-DCHkaJ9-.mjs +0 -92
  155. package/dist/kv-cache-fgbLHGVL.mjs +0 -121
  156. package/dist/ld-BpkhkDGj.mjs +0 -1752
  157. package/dist/map-local-context-conflict-DOYEEJ3u.mjs +0 -204
  158. package/dist/map-local-create-note-B0SeXJms.mjs +0 -214
  159. package/dist/metrics-Uqf8GomB.mjs +0 -818
  160. package/dist/middleware-DcXzz-YK.mjs +0 -9971
  161. package/dist/middleware-DgwZkvJR.mjs +0 -5
  162. package/dist/mq-eF96QxUT.mjs +0 -288
  163. package/dist/negotiation-BAKO9nih.mjs +0 -96
  164. package/dist/nodeinfo/client.test.d.mts +0 -2
  165. package/dist/nodeinfo/client.test.mjs +0 -1364
  166. package/dist/nodeinfo/handler.test.d.mts +0 -2
  167. package/dist/nodeinfo/handler.test.mjs +0 -94
  168. package/dist/nodeinfo/types.test.d.mts +0 -2
  169. package/dist/nodeinfo/types.test.mjs +0 -192
  170. package/dist/otel/exporter.test.d.mts +0 -2
  171. package/dist/otel/exporter.test.mjs +0 -887
  172. package/dist/outbound-body-CckPA5gc.mjs +0 -83
  173. package/dist/outbound-capture-DVgnRzlf.mjs +0 -184
  174. package/dist/outbound-document-BMgwLXTY.mjs +0 -179
  175. package/dist/outgoing-jsonld-4IJqPYos.mjs +0 -205
  176. package/dist/owner-BVenIFg1.mjs +0 -267
  177. package/dist/portable-collection-DIk9gpjL.mjs +0 -418
  178. package/dist/portable-key-C7pVaHNR.mjs +0 -549
  179. package/dist/portable-key-id-Ca30-hOa.mjs +0 -248
  180. package/dist/proof-BlfZ95--.mjs +0 -1420
  181. package/dist/public-audience--klBIOk1.mjs +0 -197
  182. package/dist/retry-zX5xw5e0.mjs +0 -34
  183. package/dist/router-u5O5KCQr.mjs +0 -156
  184. package/dist/secured-document-CxzZZ9nL.mjs +0 -35
  185. package/dist/send-DEGHPGFk.mjs +0 -710
  186. package/dist/sig/accept.test.d.mts +0 -2
  187. package/dist/sig/accept.test.mjs +0 -416
  188. package/dist/sig/compound-proof-discovery.test.d.mts +0 -2
  189. package/dist/sig/compound-proof-discovery.test.mjs +0 -331
  190. package/dist/sig/compound-proof-producer.test.d.mts +0 -2
  191. package/dist/sig/compound-proof-producer.test.mjs +0 -107
  192. package/dist/sig/compound-proof-verification.test.d.mts +0 -2
  193. package/dist/sig/compound-proof-verification.test.mjs +0 -1017
  194. package/dist/sig/compound-proof.test.d.mts +0 -2
  195. package/dist/sig/compound-proof.test.mjs +0 -411
  196. package/dist/sig/http.test.d.mts +0 -2
  197. package/dist/sig/http.test.mjs +0 -3328
  198. package/dist/sig/key-owner-evidence.test.d.mts +0 -2
  199. package/dist/sig/key-owner-evidence.test.mjs +0 -100
  200. package/dist/sig/key.test.d.mts +0 -2
  201. package/dist/sig/key.test.mjs +0 -1250
  202. package/dist/sig/ld.test.d.mts +0 -2
  203. package/dist/sig/ld.test.mjs +0 -1044
  204. package/dist/sig/mitra-receive-interop.test.d.mts +0 -2
  205. package/dist/sig/mitra-receive-interop.test.mjs +0 -260
  206. package/dist/sig/mitra-send-interop.test.d.mts +0 -2
  207. package/dist/sig/mitra-send-interop.test.mjs +0 -461
  208. package/dist/sig/owner.test.d.mts +0 -2
  209. package/dist/sig/owner.test.mjs +0 -202
  210. package/dist/sig/portable-collection.test.d.mts +0 -2
  211. package/dist/sig/portable-collection.test.mjs +0 -697
  212. package/dist/sig/portable-interop.test.d.mts +0 -2
  213. package/dist/sig/portable-interop.test.mjs +0 -156
  214. package/dist/sig/portable-key-id.test.d.mts +0 -2
  215. package/dist/sig/portable-key-id.test.mjs +0 -67
  216. package/dist/sig/portable-key.test.d.mts +0 -2
  217. package/dist/sig/portable-key.test.mjs +0 -1438
  218. package/dist/sig/proof.test.d.mts +0 -2
  219. package/dist/sig/proof.test.mjs +0 -2638
  220. package/dist/sig/tootik-request-interop.test.d.mts +0 -2
  221. package/dist/sig/tootik-request-interop.test.mjs +0 -190
  222. package/dist/sig/tootik-send-interop.test.d.mts +0 -2
  223. package/dist/sig/tootik-send-interop.test.mjs +0 -331
  224. package/dist/sig/verification.test.d.mts +0 -2
  225. package/dist/sig/verification.test.mjs +0 -310
  226. package/dist/std__assert-DvMDRPes.mjs +0 -8
  227. package/dist/tasks-BCUmQbbz.mjs +0 -77
  228. package/dist/temporal-B4oQ8HpW.mjs +0 -95
  229. package/dist/testing/context.test.d.mts +0 -2
  230. package/dist/testing/context.test.mjs +0 -19
  231. package/dist/testing/kv.test.d.mts +0 -2
  232. package/dist/testing/kv.test.mjs +0 -101
  233. package/dist/testing/mod.d.mts +0 -6099
  234. package/dist/testing/mod.mjs +0 -9
  235. package/dist/testing-vrnD5Uaa.mjs +0 -8
  236. package/dist/types-DE_LV1JB.mjs +0 -46
  237. package/dist/utils/docloader.test.d.mts +0 -2
  238. package/dist/utils/docloader.test.mjs +0 -1076
  239. package/dist/utils/kv-cache.test.d.mts +0 -2
  240. package/dist/utils/kv-cache.test.mjs +0 -261
  241. package/dist/verification-C3954hQh.mjs +0 -5
  242. package/dist/verification-Dpfcw8ml.mjs +0 -105
  243. package/dist/vocab/cjs.test.d.mts +0 -2
  244. package/dist/vocab/cjs.test.mjs +0 -27
@@ -1,221 +0,0 @@
1
- import { Temporal } from "temporal-polyfill";
2
- import "urlpattern-polyfill";
3
- globalThis.addEventListener = () => {};
4
- import { t as MAX_PORTABLE_KEY_TTL } from "./key-owner-evidence-APB6NpSk.mjs";
5
- import { CryptographicKey, Multikey } from "@fedify/vocab";
6
- import { FetchError, UrlError } from "@fedify/vocab-runtime";
7
- //#region src/federation/keycache.ts
8
- const KEY_CACHE_GENERATION = "3";
9
- const COMPATIBLE_KEY_SEGMENT = "__compatible";
10
- const MAX_OWNER_BYTES = 32 * 1024;
11
- function isCompatibleKeyEntry(value) {
12
- return value != null && typeof value === "object" && "key" in value && "expires" in value && typeof value.expires === "number";
13
- }
14
- function isJsonObject(value) {
15
- return value != null && typeof value === "object" && !Array.isArray(value);
16
- }
17
- var KvKeyCache = class {
18
- kv;
19
- prefix;
20
- options;
21
- unavailableKeyTtl;
22
- keyTtl;
23
- nullKeys;
24
- constructor(kv, prefix, options = {}) {
25
- this.kv = kv;
26
- this.prefix = prefix;
27
- this.options = options;
28
- this.unavailableKeyTtl = options.unavailableKeyTtl ?? Temporal.Duration.from({ minutes: 10 });
29
- this.keyTtl = options.keyTtl ?? Temporal.Duration.from({ days: 30 });
30
- this.nullKeys = /* @__PURE__ */ new Map();
31
- }
32
- #getFetchErrorKey(keyId) {
33
- return [
34
- ...this.prefix,
35
- "__fetchError",
36
- keyId.href
37
- ];
38
- }
39
- #entryKey(keyId) {
40
- return [
41
- ...this.prefix,
42
- KEY_CACHE_GENERATION,
43
- keyId.href
44
- ];
45
- }
46
- async get(keyId) {
47
- const negativeExpiration = this.nullKeys.get(keyId.href);
48
- if (negativeExpiration != null) {
49
- if (Temporal.Now.instant().until(negativeExpiration).sign >= 0) return null;
50
- this.nullKeys.delete(keyId.href);
51
- }
52
- const serialized = await this.kv.get(this.#entryKey(keyId));
53
- if (serialized === void 0) return void 0;
54
- if (serialized === null) {
55
- this.nullKeys.set(keyId.href, Temporal.Now.instant().add(this.unavailableKeyTtl));
56
- return null;
57
- }
58
- try {
59
- return await CryptographicKey.fromJsonLd(serialized, this.options);
60
- } catch {
61
- try {
62
- return await Multikey.fromJsonLd(serialized, this.options);
63
- } catch {
64
- await this.kv.delete(this.#entryKey(keyId));
65
- return;
66
- }
67
- }
68
- }
69
- async set(keyId, key) {
70
- if (key == null) {
71
- this.nullKeys.set(keyId.href, Temporal.Now.instant().add(this.unavailableKeyTtl));
72
- await this.kv.set(this.#entryKey(keyId), null, { ttl: this.unavailableKeyTtl });
73
- return;
74
- }
75
- this.nullKeys.delete(keyId.href);
76
- const serialized = await key.toJsonLd(this.options);
77
- await this.kv.set(this.#entryKey(keyId), serialized, { ttl: this.keyTtl });
78
- }
79
- /**
80
- * Returns the namespace of keys at FEP-ef61 compatible identifiers looked
81
- * up for the given purpose. Its entries carry the time they expire, which
82
- * is checked when they are read, so that an entry is never used after
83
- * the proof that vouched for its key expired, however late the underlying
84
- * store evicts it.
85
- * @param scope The purpose of the lookups.
86
- * @returns The namespace.
87
- * @internal
88
- */
89
- compatibleKeyScope(scope) {
90
- const now = this.options.now ?? (() => Temporal.Now.instant());
91
- const entryKey = (keyId) => [
92
- ...this.prefix,
93
- COMPATIBLE_KEY_SEGMENT,
94
- scope,
95
- keyId.href
96
- ];
97
- const isExpired = (entry) => entry.expires <= now().epochMilliseconds;
98
- const keyTtl = Math.min(this.keyTtl.total("millisecond"), MAX_PORTABLE_KEY_TTL.total("millisecond"));
99
- const unavailableKeyTtl = this.unavailableKeyTtl.total("millisecond");
100
- const getEntry = async (keyId) => {
101
- const entry = await this.kv.get(entryKey(keyId));
102
- if (entry === void 0) return void 0;
103
- if (!isCompatibleKeyEntry(entry) || isExpired(entry)) {
104
- await this.kv.delete(entryKey(keyId));
105
- return;
106
- }
107
- if (entry.key === null) return null;
108
- let expires;
109
- try {
110
- expires = Temporal.Instant.fromEpochMilliseconds(entry.expires);
111
- } catch (error) {
112
- if (!(error instanceof RangeError)) throw error;
113
- await this.kv.delete(entryKey(keyId));
114
- return;
115
- }
116
- let key;
117
- try {
118
- key = await CryptographicKey.fromJsonLd(entry.key, this.options);
119
- } catch {
120
- try {
121
- key = await Multikey.fromJsonLd(entry.key, this.options);
122
- } catch {
123
- await this.kv.delete(entryKey(keyId));
124
- return;
125
- }
126
- }
127
- if (isExpired(entry)) return void 0;
128
- return {
129
- key,
130
- ...isJsonObject(entry.owner) ? { owner: entry.owner } : {},
131
- expires
132
- };
133
- };
134
- return {
135
- get: async (keyId) => {
136
- const entry = await getEntry(keyId);
137
- return entry == null ? entry : entry.key;
138
- },
139
- getEntry,
140
- set: async (keyId, key, options) => {
141
- const serialized = key == null ? null : await key.toJsonLd(this.options);
142
- const current = now();
143
- let expires = current.epochMilliseconds + (key == null ? unavailableKeyTtl : keyTtl);
144
- if (options?.expires != null) expires = Math.min(expires, options.expires.epochMilliseconds);
145
- const ttl = expires - current.epochMilliseconds;
146
- if (ttl <= 0) {
147
- await this.kv.delete(entryKey(keyId));
148
- return;
149
- }
150
- const owner = key == null || options?.owner == null || new TextEncoder().encode(JSON.stringify(options.owner)).length > MAX_OWNER_BYTES ? void 0 : options.owner;
151
- await this.kv.set(entryKey(keyId), {
152
- key: serialized,
153
- expires,
154
- ...owner == null ? {} : { owner }
155
- }, { ttl: Temporal.Duration.from({ milliseconds: ttl }) });
156
- return key == null ? void 0 : Temporal.Instant.fromEpochMilliseconds(expires);
157
- },
158
- delete: async (keyId) => {
159
- await this.kv.delete(entryKey(keyId));
160
- }
161
- };
162
- }
163
- async getFetchError(keyId) {
164
- const cached = await this.kv.get(this.#getFetchErrorKey(keyId));
165
- if (cached == null || typeof cached !== "object") return void 0;
166
- if ("status" in cached && typeof cached.status === "number" && "statusText" in cached && typeof cached.statusText === "string" && "headers" in cached && Array.isArray(cached.headers) && "body" in cached && typeof cached.body === "string") return {
167
- status: cached.status,
168
- response: new Response(cached.body, {
169
- status: cached.status,
170
- statusText: cached.statusText,
171
- headers: cached.headers
172
- })
173
- };
174
- else if ("errorName" in cached && typeof cached.errorName === "string" && "errorMessage" in cached && typeof cached.errorMessage === "string") {
175
- let error;
176
- if ("errorType" in cached && cached.errorType === "FetchError" && "errorUrl" in cached && typeof cached.errorUrl === "string" && URL.canParse(cached.errorUrl)) {
177
- error = new FetchError(cached.errorUrl);
178
- error.message = cached.errorMessage;
179
- } else if ("errorType" in cached && cached.errorType === "UrlError" && "errorReason" in cached && (cached.errorReason === "dns" || cached.errorReason === "disallowed")) error = new UrlError(cached.errorMessage, { reason: cached.errorReason });
180
- else error = new Error(cached.errorMessage);
181
- error.name = cached.errorName;
182
- if ("errorCause" in cached && cached.errorCause != null && typeof cached.errorCause === "object" && "name" in cached.errorCause && typeof cached.errorCause.name === "string" && "message" in cached.errorCause && typeof cached.errorCause.message === "string") if ("isDomException" in cached.errorCause && cached.errorCause.isDomException === true) error.cause = new DOMException(cached.errorCause.message, cached.errorCause.name);
183
- else error.cause = Object.assign(new Error(cached.errorCause.message), { name: cached.errorCause.name });
184
- return { error };
185
- }
186
- }
187
- async setFetchError(keyId, error) {
188
- if (error == null) {
189
- await this.kv.delete(this.#getFetchErrorKey(keyId));
190
- return;
191
- }
192
- if ("status" in error) {
193
- await this.kv.set(this.#getFetchErrorKey(keyId), {
194
- status: error.status,
195
- statusText: error.response.statusText,
196
- headers: Array.from(error.response.headers.entries()),
197
- body: await error.response.clone().text()
198
- }, { ttl: this.unavailableKeyTtl });
199
- return;
200
- }
201
- const cause = error.error.cause;
202
- await this.kv.set(this.#getFetchErrorKey(keyId), {
203
- errorName: error.error.name,
204
- errorMessage: error.error.message,
205
- ...error.error instanceof FetchError ? {
206
- errorType: "FetchError",
207
- errorUrl: error.error.url.href
208
- } : error.error instanceof UrlError ? {
209
- errorType: "UrlError",
210
- errorReason: error.error.reason
211
- } : {},
212
- ...cause instanceof Error || cause instanceof DOMException ? { errorCause: {
213
- name: cause.name,
214
- message: cause.message,
215
- isDomException: cause instanceof DOMException
216
- } } : {}
217
- }, { ttl: this.unavailableKeyTtl });
218
- }
219
- };
220
- //#endregion
221
- export { KvKeyCache as t };
@@ -1,120 +0,0 @@
1
- import "temporal-polyfill";
2
- import "urlpattern-polyfill";
3
- globalThis.addEventListener = () => {};
4
- import { CryptographicKey, Multikey } from "@fedify/vocab";
5
- import { importSpki } from "@fedify/vocab-runtime";
6
- //#region src/testing/keys.ts
7
- const rsaPublicKey1 = new CryptographicKey({
8
- id: new URL("https://example.com/key"),
9
- owner: new URL("https://example.com/person"),
10
- publicKey: await crypto.subtle.importKey("jwk", {
11
- kty: "RSA",
12
- alg: "RS256",
13
- n: "yIB9rotX8G6r6_6toT-x24BUiQ_HaPH1Em9dOt4c94s-OPFoEdH7DY7Iym9A8LlH4JaGF8KD38bLHWe1S4x0jV3gHJKhK7veJfGZCKUENcQecBZ-YWUs5HWvUIX1vVB__0luHrg6BQKGOrSOE-WIAxyr0qsWCFfZzQrvSnUD2yvg1arJX2xhms14uxoRd5Kg9efKSCmmQaNEapicARUmFWrIEpGFa_nUUnqimssAGw1eZFqf3wA4TjhsuARBhGaJtv_3KEa016eMZxy3kDlOjZnXZTaTgWkXdodwUvy8563fes3Al6BlcS2iJ9qbtha8rSm0FHqoUKH73JsLPKQIwQ",
14
- e: "AQAB",
15
- key_ops: ["verify"],
16
- ext: true
17
- }, {
18
- name: "RSASSA-PKCS1-v1_5",
19
- hash: "SHA-256"
20
- }, true, ["verify"])
21
- });
22
- const rsaPrivateKey2 = await crypto.subtle.importKey("jwk", {
23
- kty: "RSA",
24
- alg: "RS256",
25
- n: "oRmBtnxbdFutoRd1GLGwwGTrsqlRRWUe11hHQaoRLGf5LwQ0tIc6I9q-dynliw-2kxYsLn9SH2je6HcTYOolgW7F_cOWXZQN04b-OiYcU1ConAhLjmn4k1uKawJ614y0ScPNd8PQ-Cljsnl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zkqJiz2WpW-AE",
31
- dq: "TCLoYcX0ywuNA9DSU6v94KmBh1e_IELEFVbJb5vvLKlAK-ycMK0rfzC1co9Hhkski1LskTnxnoqwZ5oF-7X10eZvy3Te_FHSl0IsTar8ST2-MRtGh2UjTdvP_nnygj4GcXvKfngjPEfthDzVfVMeR38oDhDxMFD5AaY_v9aMH_U",
32
- qi: "KC6gWhVM_x7iQgl-gEoSh_iM1Jf314ZLJKAAz1DsTHMi5yuCkCMmmY7h6jlkAJVngK3KIf5LPoAeUoGJ26E1kocbRU_nZBftMDVXHCYICz8qMQXR5euN_5SeJnu_VWXH-CY83MKhPYAorWSZ1-G9gh-C16LlRMzJwoE6h5QNeNo",
33
- "key_ops": ["sign"],
34
- "ext": true
35
- }, {
36
- name: "RSASSA-PKCS1-v1_5",
37
- hash: "SHA-256"
38
- }, true, ["sign"]);
39
- const rsaPublicKey2 = new CryptographicKey({
40
- id: new URL("https://example.com/key2"),
41
- publicKey: await crypto.subtle.importKey("jwk", {
42
- kty: "RSA",
43
- alg: "RS256",
44
- n: "oRmBtnxbdFutoRd1GLGwwGTrsqlRRWUe11hHQaoRLGf5LwQ0tIc6I9q-dynliw-2kxYsLn9SH2je6HcTYOolgW7F_cOWXZQN04b-OiYcU1ConAhLjmn4k1uKawJ614y0ScPNd8PQ-CljsnlPxbq9ofaCMe2BV3B6y09aCuGFJ0nxn1_ubjmIBIWWFTAznoz1J9BhJDGyt3IO3ABy3f9zDVlR32L_n5VIkXnxkjUKdzMAOzYb62kuKOp1iznRTPrV71SNtivJMwSh_LVgBrmZjtIn_oim-KyX_fdLU3tQ7VClyqmJzyAjccOH6Qj6nFTPh-vX07gqN8IlLT2uye4waw",
45
- e: "AQAB",
46
- key_ops: ["verify"],
47
- ext: true
48
- }, {
49
- "name": "RSASSA-PKCS1-v1_5",
50
- "hash": "SHA-256"
51
- }, true, ["verify"])
52
- });
53
- const rsaPrivateKey3 = await crypto.subtle.importKey("jwk", {
54
- kty: "RSA",
55
- alg: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082n3ZsULceH6w7j7v4w5StHVnkOAYsbeHxbzs10hbWv9RjBI0vUb1M8UdKK1sg9kiz6cy0SJ5QYYoMzrEkiqh0U-tOSvRUUsEmI0_g5kOts1VMZD2OGFQ8LkIqzwjRm9lqF114vnQqadKyLNJcudVkSYpeG8hU5aqHyr73VISdgQP2smKeiwt6lhNC8puyNS0AqL4CyNKuddFgA-KLFNTSF70y7vUYY8U47UsotXNdpAMrFzHjweJ3GAiAqyBh79dH-ufLpivX9wSWat-NWaLqrkJNHqLrRmtfWK1pxny9n-1c6XcN93V0mOw",
62
- dq: "n_-xA_eGT9uGZj_RDQiKOJT3vvOMxIuB60EXJs_4HaXerMuMn7B75hJLa2dQpEh-cTV6LsNm2i8LxNWf4q5GTurAk0ONWBoUcIlTHBDvJWfkAny-9yjf9N_xctvD1vucsqv7waeQKXcKv4cj5ZVbZXDZwJCodApYGyF4jFCh5O4HV9dllwpiWyE5nJihu-rELCYUSKUDaElGw7Ut9jRbdRME9ztH5LtFVcC_fzCXbZYm9i7jA6FEEQ7cQjdliq1N8AMprum-r_wqRssEafAFEcsnOsSJoIZpgS9gXsVbr7R1OMj95DBmKpzK6fV8TXfy3XrrcHOkOzMiqRPCRcIO2Q",
63
- qi: "LhkzXD7HOowopZpKmEklXoAZlZV-tPqZm9m_0fkh-dXoFaHfIj-eXUr-7Z8yJWx4-nnGITZR28Q10QHvnG5phqhvgUJ2nHixqaphXfYt80nictRfVcRW4bN_oHm-87zK0BS5OjJvLoMYKSREvdz1UqVJxA4jKsYURCIj6KSDJyZsd5ENAQWjxs0jEw73sKPT-J-ePCEz05V-euBp6RBSePnu3rphrtZ54MNnShyqhoimnymNqx7iXTFBdIP2OKRmzSyKMpwBLU54tctXIOlv_l89gN7V6F8_I0q359M9tdGmmqxjIzAKm9USP7jfcoejXXt3lpSglwKpEfQBfFF9yg",
64
- key_ops: ["sign"],
65
- ext: true
66
- }, {
67
- name: "RSASSA-PKCS1-v1_5",
68
- hash: "SHA-256"
69
- }, true, ["sign"]);
70
- const rsaPublicKey3 = new CryptographicKey({
71
- id: new URL("https://example.com/person2#key3"),
72
- owner: new URL("https://example.com/person2"),
73
- publicKey: await crypto.subtle.importKey("jwk", {
74
- kty: "RSA",
75
- alg: "RS256",
76
- n: "4GUqWgdiYlN3Su5Gr4l6i-xRS8gDDVKZ718vpGk6eIpvqs33q430nRbHIzbHRXRaAhc_1--rUBcK0V4_kjZlCSzVtRgGU6HMkmjcD-uE56a8XbTczfltbEDj7afoEuB2F3UhQEWrSz-QJ29DPXaLMIa1Yv61NR2vxGqNbdtoMjDORMBYtg77CYbcFkiJHw65PDa7-f_yjLxuCRPye5L7hncN0UZuuFoRJmHNRLSg5omBad9WTvQXmSyXEhEdk9fHwlI022AqAzlWbT79hldcDSKGGLLbQIs1c3JZIG8G5i6Uh5Vy0Z7tSNBcxbhqoI9i9je4f_x_OPIVc19f04BE1LgWuHsftZzRgW9Sdqz53W83XxVdxlyHeywXOnstSWT11f8dkLyQUcHKTH-E6urbH-aiPLiRpYK8W7D9KTQA9kZ5JXaEuveBd5vJX7wakhbzAn8pWJU7GYIHNY38YcokmivkU5pY8S2cKFMwY0b7ade3MComlir5P3ZYSjF-n6gRVsT96P-9mNfCu9gXt_f8XCyjKlH89kGwuJ7HhR8CuVdm0l-jYozVt6GsDy0hHYyn79NCCAEzP7ZbhBMR0T5Vrkl-TIGXoJH9WFiz4VxO-NnglF6dNQjDS5IzYLoFRXIK1f3cmQiEB4FZmL70l9HLrgwR-Xys83xia79OqFDRezM",
77
- e: "AQAB",
78
- key_ops: ["verify"],
79
- ext: true
80
- }, {
81
- "name": "RSASSA-PKCS1-v1_5",
82
- "hash": "SHA-256"
83
- }, true, ["verify"])
84
- });
85
- new Multikey({
86
- id: new URL("https://example.com/person2#key3"),
87
- controller: new URL("https://example.com/person2"),
88
- publicKey: rsaPublicKey3.publicKey
89
- });
90
- const rsaPublicKey5 = new CryptographicKey({
91
- id: new URL("https://remote.domain/users/bob#main-key"),
92
- owner: new URL("https://remote.domain/users/bob"),
93
- publicKey: await importSpki("-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqIAYvNFGbZ5g4iiK6feS\ndXD4bDStFM58A7tHycYXaYtzZQpIeHXAmaXuZzXIwtrP4N0gIk8JNwZvXj2UPS+S\n07t0V9wNK94he01LV5EMz/GN4eNnFmDL64HIEuKLvV8TvgjbUPRD6Y5X0UpKi2ZI\nFLSb96Q5w0Z/k7ntpVKV52y8kz5Fjr/O/0JuHryZe0yItzJh8kzFfeMf0EXzfSna\nKvT7P9jhgC6uTre+jXyvVZjiHDrnqvvucdI3I7DRfXo1OqARBrLjy+TdseUAjNYJ\n+OuPRI1URIWQI01DCHqcohVu9+Ar+BiCjFp3ua+XMuJvrvbD61d1Fvig/9nbBRR+\n8QIDAQAB\n-----END PUBLIC KEY-----\n")
94
- });
95
- const ed25519PrivateKey = await crypto.subtle.importKey("jwk", {
96
- crv: "Ed25519",
97
- ext: true,
98
- key_ops: ["sign"],
99
- kty: "OKP",
100
- d: "LledL195fP9TQGQrkE2l2Y2k48UvqCzYI9M1zXyh7zQ",
101
- x: "LR8epAGDe-cVq5p2Tx49CCfphpk1rNhkNoY9i-XEUfg"
102
- }, "Ed25519", true, ["sign"]);
103
- const ed25519PublicKey = new CryptographicKey({
104
- id: new URL("https://example.com/person2#key4"),
105
- owner: new URL("https://example.com/person2"),
106
- publicKey: await crypto.subtle.importKey("jwk", {
107
- crv: "Ed25519",
108
- ext: true,
109
- key_ops: ["verify"],
110
- kty: "OKP",
111
- x: "LR8epAGDe-cVq5p2Tx49CCfphpk1rNhkNoY9i-XEUfg"
112
- }, "Ed25519", true, ["verify"])
113
- });
114
- const ed25519Multikey = new Multikey({
115
- id: new URL("https://example.com/person2#key4"),
116
- controller: new URL("https://example.com/person2"),
117
- publicKey: ed25519PublicKey.publicKey
118
- });
119
- //#endregion
120
- export { rsaPrivateKey3 as a, rsaPublicKey3 as c, rsaPrivateKey2 as i, rsaPublicKey5 as l, ed25519PrivateKey as n, rsaPublicKey1 as o, ed25519PublicKey as r, rsaPublicKey2 as s, ed25519Multikey as t };
@@ -1,104 +0,0 @@
1
- import { Temporal } from "temporal-polyfill";
2
- import "urlpattern-polyfill";
3
- globalThis.addEventListener = () => {};
4
- import { t as MemoryKvStore } from "./kv-DCHkaJ9-.mjs";
5
- //#region src/testing/kv.ts
6
- /**
7
- * A {@link KvStore} decorator whose entry expiry is driven by a virtual clock
8
- * instead of wall-clock time.
9
- *
10
- * {@link MemoryKvStore} decides expiry by comparing timestamps rather than by
11
- * scheduling timers, so a test that wants to observe a TTL elapsing would
12
- * otherwise have to sleep for longer than the TTL and hope the runner keeps
13
- * up. That makes such tests both slow and load-sensitive.
14
- *
15
- * This decorator keeps the TTLs itself and never passes them to the store it
16
- * wraps, so nothing expires until {@link advance} moves the clock. Cache
17
- * hits, expiry, and repopulation therefore all become deterministic, without
18
- * patching any global and without a test-only entry point in the library
19
- * itself. The clock starts at the Unix epoch and only ever moves forward.
20
- */
21
- var ManualClockKvStore = class {
22
- #inner;
23
- #expirations = /* @__PURE__ */ new Map();
24
- #now = Temporal.Instant.fromEpochMilliseconds(0);
25
- /**
26
- * Present only when the wrapped store has its own `cas()`. Callers such as
27
- * the task deduplication planner test `kv.cas != null` to decide whether a
28
- * conditional write is available, so the decorator must not advertise a
29
- * capability the wrapped store lacks.
30
- */
31
- cas;
32
- constructor(inner = new MemoryKvStore()) {
33
- this.#inner = inner;
34
- const innerCas = inner.cas;
35
- if (innerCas != null) this.cas = async (key, expectedValue, newValue, options) => {
36
- await this.#evictIfExpired(key);
37
- const swapped = await innerCas.call(inner, key, expectedValue, newValue);
38
- if (swapped) this.#recordTtl(key, options);
39
- return swapped;
40
- };
41
- }
42
- /**
43
- * The current reading of the virtual clock.
44
- */
45
- get now() {
46
- return this.#now;
47
- }
48
- /**
49
- * Moves the virtual clock forward. Any entry whose TTL has elapsed by the
50
- * new time reads as missing from this point on, exactly as it would once a
51
- * real `KvStore` had let it expire.
52
- */
53
- advance(duration) {
54
- this.#now = this.#now.add(Temporal.Duration.from(duration).round({ largestUnit: "hour" }));
55
- }
56
- #encodeKey(key) {
57
- return JSON.stringify(key);
58
- }
59
- /**
60
- * Drops the entry if its TTL has elapsed. Mirrors {@link MemoryKvStore},
61
- * which treats an entry as live up to and including its expiration instant.
62
- */
63
- async #evictIfExpired(key) {
64
- const encodedKey = this.#encodeKey(key);
65
- const expiration = this.#expirations.get(encodedKey);
66
- if (expiration == null) return;
67
- if (this.#now.until(expiration).sign >= 0) return;
68
- this.#expirations.delete(encodedKey);
69
- await this.#inner.delete(key);
70
- }
71
- async #evictAllExpired() {
72
- for (const [encodedKey, expiration] of [...this.#expirations]) {
73
- if (this.#now.until(expiration).sign >= 0) continue;
74
- this.#expirations.delete(encodedKey);
75
- await this.#inner.delete(JSON.parse(encodedKey));
76
- }
77
- }
78
- #recordTtl(key, options) {
79
- const encodedKey = this.#encodeKey(key);
80
- if (options?.ttl == null) {
81
- this.#expirations.delete(encodedKey);
82
- return;
83
- }
84
- this.#expirations.set(encodedKey, this.#now.add(options.ttl.round({ largestUnit: "hour" })));
85
- }
86
- async get(key) {
87
- await this.#evictIfExpired(key);
88
- return await this.#inner.get(key);
89
- }
90
- async set(key, value, options) {
91
- this.#recordTtl(key, options);
92
- await this.#inner.set(key, value);
93
- }
94
- async delete(key) {
95
- this.#expirations.delete(this.#encodeKey(key));
96
- await this.#inner.delete(key);
97
- }
98
- async *list(prefix) {
99
- await this.#evictAllExpired();
100
- yield* this.#inner.list(prefix);
101
- }
102
- };
103
- //#endregion
104
- export { ManualClockKvStore as t };
@@ -1,92 +0,0 @@
1
- import { Temporal } from "temporal-polyfill";
2
- import "urlpattern-polyfill";
3
- globalThis.addEventListener = () => {};
4
- import { isEqual } from "es-toolkit";
5
- //#region src/federation/kv.ts
6
- /**
7
- * A key–value store that stores values in memory.
8
- * Do not use this in production as it does not persist values.
9
- *
10
- * @since 0.5.0
11
- */
12
- var MemoryKvStore = class {
13
- #values = {};
14
- #encodeKey(key) {
15
- return JSON.stringify(key);
16
- }
17
- /**
18
- * {@inheritDoc KvStore.get}
19
- */
20
- get(key) {
21
- const encodedKey = this.#encodeKey(key);
22
- const entry = this.#values[encodedKey];
23
- if (entry == null) return Promise.resolve(void 0);
24
- const [value, expiration] = entry;
25
- if (expiration != null && Temporal.Now.instant().until(expiration).sign < 0) {
26
- delete this.#values[encodedKey];
27
- return Promise.resolve(void 0);
28
- }
29
- return Promise.resolve(value);
30
- }
31
- /**
32
- * {@inheritDoc KvStore.set}
33
- */
34
- set(key, value, options) {
35
- const encodedKey = this.#encodeKey(key);
36
- const expiration = options?.ttl == null ? null : Temporal.Now.instant().add(options.ttl.round({ largestUnit: "hour" }));
37
- this.#values[encodedKey] = [value, expiration];
38
- return Promise.resolve();
39
- }
40
- /**
41
- * {@inheritDoc KvStore.delete}
42
- */
43
- delete(key) {
44
- const encodedKey = this.#encodeKey(key);
45
- delete this.#values[encodedKey];
46
- return Promise.resolve();
47
- }
48
- /**
49
- * {@inheritDoc KvStore.cas}
50
- */
51
- cas(key, expectedValue, newValue, options) {
52
- const encodedKey = this.#encodeKey(key);
53
- const entry = this.#values[encodedKey];
54
- let currentValue;
55
- if (entry == null) currentValue = void 0;
56
- else {
57
- const [value, expiration] = entry;
58
- if (expiration != null && Temporal.Now.instant().until(expiration).sign < 0) {
59
- delete this.#values[encodedKey];
60
- currentValue = void 0;
61
- } else currentValue = value;
62
- }
63
- if (!isEqual(currentValue, expectedValue)) return Promise.resolve(false);
64
- const expiration = options?.ttl == null ? null : Temporal.Now.instant().add(options.ttl.round({ largestUnit: "hour" }));
65
- this.#values[encodedKey] = [newValue, expiration];
66
- return Promise.resolve(true);
67
- }
68
- /**
69
- * {@inheritDoc KvStore.list}
70
- */
71
- async *list(prefix) {
72
- const now = Temporal.Now.instant();
73
- for (const [encodedKey, entry] of Object.entries(this.#values)) {
74
- const key = JSON.parse(encodedKey);
75
- if (prefix != null) {
76
- if (key.length < prefix.length) continue;
77
- if (!prefix.every((p, i) => key[i] === p)) continue;
78
- }
79
- const [value, expiration] = entry;
80
- if (expiration != null && now.until(expiration).sign < 0) {
81
- delete this.#values[encodedKey];
82
- continue;
83
- }
84
- yield {
85
- key,
86
- value
87
- };
88
- }
89
- }
90
- };
91
- //#endregion
92
- export { MemoryKvStore as t };
@@ -1,121 +0,0 @@
1
- import { Temporal } from "temporal-polyfill";
2
- import { URLPattern } from "urlpattern-polyfill";
3
- globalThis.addEventListener = () => {};
4
- import { p as recordDocumentCache } from "./metrics-Uqf8GomB.mjs";
5
- import { getLogger } from "@logtape/logtape";
6
- import { preloadedContexts } from "@fedify/vocab-runtime";
7
- //#region src/utils/kv-cache.ts
8
- const logger = getLogger([
9
- "fedify",
10
- "utils",
11
- "kv-cache"
12
- ]);
13
- /**
14
- * A mock implementation of a key–value store for testing purposes.
15
- */
16
- var MockKvStore = class {
17
- #values = {};
18
- get(key) {
19
- return Promise.resolve(this.#values[JSON.stringify(key)]);
20
- }
21
- set(key, value, _options) {
22
- this.#values[JSON.stringify(key)] = value;
23
- return Promise.resolve();
24
- }
25
- async delete(_) {}
26
- cas(..._) {
27
- return Promise.resolve(false);
28
- }
29
- async *list(prefix) {
30
- for (const [encodedKey, value] of Object.entries(this.#values)) {
31
- const key = JSON.parse(encodedKey);
32
- if (prefix != null) {
33
- if (key.length < prefix.length) continue;
34
- if (!prefix.every((p, i) => key[i] === p)) continue;
35
- }
36
- yield {
37
- key,
38
- value
39
- };
40
- }
41
- }
42
- };
43
- /**
44
- * Decorates a {@link DocumentLoader} with a cache backed by a {@link KvStore}.
45
- * @param parameters The parameters for the cache.
46
- * @returns The decorated document loader which is cache-enabled.
47
- */
48
- function kvCache({ loader, kv, prefix, rules, meterProvider, kind }) {
49
- const keyPrefix = prefix ?? ["_fedify", "remoteDocument"];
50
- rules ??= [[new URLPattern({}), Temporal.Duration.from({ minutes: 5 })]];
51
- 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()));
52
- const lookupKind = kind ?? "object";
53
- function emitCacheMetric(url, result) {
54
- if (meterProvider == null) return;
55
- let remoteUrl;
56
- try {
57
- remoteUrl = new URL(url);
58
- } catch {
59
- remoteUrl = void 0;
60
- }
61
- recordDocumentCache(meterProvider, {
62
- kind: lookupKind,
63
- result,
64
- remoteUrl
65
- });
66
- }
67
- return async (url, options) => {
68
- if (url in preloadedContexts) {
69
- logger.debug("Using preloaded context: {url}.", { url });
70
- return {
71
- contextUrl: null,
72
- document: preloadedContexts[url],
73
- documentUrl: url
74
- };
75
- }
76
- const match = matchRule(url, rules);
77
- if (match == null) return await loader(url, options);
78
- const key = [...keyPrefix, url];
79
- let cache = void 0;
80
- try {
81
- cache = await kv.get(key);
82
- } catch (error) {
83
- if (error instanceof Error) logger.warn("Failed to get the document of {url} from the KV cache: {error}", {
84
- url,
85
- error
86
- });
87
- }
88
- if (cache == null) {
89
- emitCacheMetric(url, "miss");
90
- const remoteDoc = await loader(url, options);
91
- try {
92
- await kv.set(key, remoteDoc, { ttl: match });
93
- } catch (error) {
94
- logger.warn("Failed to save the document of {url} to the KV cache: {error}", {
95
- url,
96
- error
97
- });
98
- }
99
- return remoteDoc;
100
- }
101
- emitCacheMetric(url, "hit");
102
- return cache;
103
- };
104
- }
105
- function matchRule(url, rules) {
106
- for (const [pattern, d] of rules) {
107
- const duration = 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
- //#endregion
121
- export { kvCache as n, MockKvStore as t };