@fedify/fedify 2.0.0-dev.1726 → 2.0.0-dev.1736

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 (193) hide show
  1. package/dist/{actor-dO7jHa6A.js → actor-BLq4KEGR.js} +1 -1
  2. package/dist/{actor-Bn2RCpws.cjs → actor-C3oFUgfc.cjs} +921 -1451
  3. package/dist/{actor-T6RyhRgk.d.ts → actor-DqFajh9s.d.ts} +2 -2
  4. package/dist/{actor-menZmmiI.js → actor-btAA7QSA.js} +189 -677
  5. package/dist/{actor-D6K058Tb.d.cts → actor-f2NtjyCg.d.cts} +2 -2
  6. package/dist/{assert_rejects-DiIiJbZn.js → assert_rejects-Ce45JcFg.js} +1 -1
  7. package/dist/{assert_is_error-BPGph1Jx.js → assert_throws-BNXdRGWP.js} +31 -1
  8. package/dist/{builder-CGF1vv7G.js → builder-C4gbKvM5.js} +3 -4
  9. package/dist/{client-CegPX0Rn.d.cts → client-94iWEfQa.d.cts} +1 -1
  10. package/dist/{client-bgSdkFa2.d.ts → client-BsGzbnV-.d.ts} +1 -1
  11. package/dist/{client-BfzV3u96.js → client-pY7-3icS.js} +1 -1
  12. package/dist/compat/mod.d.cts +11 -12
  13. package/dist/compat/mod.d.ts +11 -12
  14. package/dist/compat/transformers.test.js +20 -20
  15. package/dist/{context-DYCJXr7J.d.cts → context-BvYz3eOb.d.cts} +10 -10
  16. package/dist/{context-B1X8-X33.d.ts → context-myrGWbL_.d.ts} +10 -10
  17. package/dist/{authdocloader-DevmXsTo.js → docloader-DtYIz2Pb.js} +17 -8
  18. package/dist/{esm-BODOj_Oi.js → esm-CQ9nlHuI.js} +11 -11
  19. package/dist/federation/builder.test.js +8 -10
  20. package/dist/federation/collection.test.js +7 -9
  21. package/dist/federation/handler.test.js +25 -26
  22. package/dist/federation/idempotency.test.js +25 -26
  23. package/dist/federation/inbox.test.js +4 -6
  24. package/dist/federation/keycache.test.js +3 -4
  25. package/dist/federation/kv.test.js +6 -8
  26. package/dist/federation/middleware.test.js +26 -27
  27. package/dist/federation/mod.cjs +9 -11
  28. package/dist/federation/mod.d.cts +11 -12
  29. package/dist/federation/mod.d.ts +11 -12
  30. package/dist/federation/mod.js +9 -11
  31. package/dist/federation/mq.test.js +8 -10
  32. package/dist/federation/negotiation.test.js +7 -9
  33. package/dist/federation/retry.test.js +4 -5
  34. package/dist/federation/router.test.js +6 -8
  35. package/dist/federation/send.test.js +13 -15
  36. package/dist/{http-Danw_xTY.js → http-B9npZ3Md.js} +2 -2
  37. package/dist/{http-D6Uj2x2y.d.ts → http-BbO0ejuk.d.ts} +2 -2
  38. package/dist/{http-gHyCrTrX.js → http-D7qwMpca.js} +256 -6
  39. package/dist/{http-D-e6AFwR.d.cts → http-M8k5mKc0.d.cts} +2 -2
  40. package/dist/{http-BmbMWfmh.cjs → http-ngJvMHez.cjs} +289 -9
  41. package/dist/{inbox-Blp5F74G.js → inbox-CY8fblhz.js} +1 -1
  42. package/dist/{key-CIIkeide.js → key-CgKiloJr.js} +3 -2
  43. package/dist/{keycache-Covv8lvK.js → keycache-B6O-bhMs.js} +1 -1
  44. package/dist/{keys-DZ1bqhJz.js → keys-B3O7b5eB.js} +2 -1
  45. package/dist/kv-cache-BrjWZnGU.cjs +133 -0
  46. package/dist/kv-cache-DkW_lqUw.js +121 -0
  47. package/dist/kv-cache-z0lgwFi_.js +93 -0
  48. package/dist/{ld-C7KL1MK2.js → ld-B7TO6JH2.js} +3 -2
  49. package/dist/lookup-Cc3ZAAis.cjs +273 -0
  50. package/dist/{type-D_8QzkEn.js → lookup-NXjB7xUj.js} +503 -5327
  51. package/dist/lookup-fEH6e7ZX.js +261 -0
  52. package/dist/{middleware-HKKY6sAX.js → middleware-BMPvaTpX.js} +18 -17
  53. package/dist/{middleware-C60xXvWx.cjs → middleware-C8w5YYxK.cjs} +30 -31
  54. package/dist/middleware-D8ENcqxG.js +26 -0
  55. package/dist/{middleware-DXOZ6_ST.js → middleware-D_VyRsDx.js} +10 -11
  56. package/dist/middleware-Yk-MuxWc.js +15 -0
  57. package/dist/middleware-glwCut0b.cjs +15 -0
  58. package/dist/{mod-BhUKmBJD.d.ts → mod-B2iOw50L.d.ts} +3 -3
  59. package/dist/mod-BYynOiJG.d.ts +109 -0
  60. package/dist/{mod-DlU8ISoa.d.ts → mod-BlVovdcy.d.ts} +2 -2
  61. package/dist/{mod-CxkWO3Mg.d.cts → mod-BxRCHTz-.d.cts} +2 -2
  62. package/dist/{mod-jQ4OODsl.d.cts → mod-C58MZ7Wx.d.cts} +1 -1
  63. package/dist/{mod-twdvV2hR.d.cts → mod-DbFNTF7m.d.cts} +2 -2
  64. package/dist/{mod-Djzcw2ry.d.cts → mod-DgdBYYa0.d.cts} +3 -3
  65. package/dist/{mod-DBzN0aCM.d.ts → mod-Ds0mpFZU.d.ts} +1 -1
  66. package/dist/mod-VastcQsk.d.cts +107 -0
  67. package/dist/{mod-DcKxhFQ8.d.ts → mod-k0Dk3fGk.d.ts} +2 -2
  68. package/dist/mod.cjs +19 -32
  69. package/dist/mod.d.cts +15 -16
  70. package/dist/mod.d.ts +16 -17
  71. package/dist/mod.js +14 -16
  72. package/dist/nodeinfo/client.test.js +8 -10
  73. package/dist/nodeinfo/handler.test.js +24 -25
  74. package/dist/nodeinfo/mod.cjs +2 -3
  75. package/dist/nodeinfo/mod.d.cts +2 -4
  76. package/dist/nodeinfo/mod.d.ts +2 -4
  77. package/dist/nodeinfo/mod.js +2 -3
  78. package/dist/nodeinfo/types.test.js +7 -9
  79. package/dist/{owner-BN_tO3cY.d.cts → owner-B4HbyP8s.d.cts} +3 -3
  80. package/dist/{owner-COcyel6J.js → owner-CSktF-2s.js} +3 -2
  81. package/dist/{owner-hd9lvQcP.d.ts → owner-kQRGVXG1.d.ts} +3 -3
  82. package/dist/{proof-D6pbnNx2.js → proof-BT65yR1Q.js} +5 -4
  83. package/dist/{proof-Cpk853lc.js → proof-Cs5Mcy_U.js} +2 -2
  84. package/dist/{proof-CMJcrQoM.cjs → proof-feEDocLH.cjs} +17 -16
  85. package/dist/{send-C7A7_big.js → send-vPZ03xUr.js} +2 -2
  86. package/dist/sig/http.test.js +12 -13
  87. package/dist/sig/key.test.js +9 -11
  88. package/dist/sig/ld.test.js +8 -10
  89. package/dist/sig/mod.cjs +9 -11
  90. package/dist/sig/mod.d.cts +5 -7
  91. package/dist/sig/mod.d.ts +5 -7
  92. package/dist/sig/mod.js +5 -7
  93. package/dist/sig/owner.test.js +10 -12
  94. package/dist/sig/proof.test.js +13 -14
  95. package/dist/testing/docloader.test.js +6 -8
  96. package/dist/testing/mod.d.ts +1 -86
  97. package/dist/testing/mod.js +2 -3
  98. package/dist/{testing-e8Kpb1uV.js → testing-u2cUPQLp.js} +1 -2
  99. package/dist/{types-Db4ukgX8.js → types-BtUjyi5y.js} +1 -1
  100. package/dist/{types-LjTL4QMx.cjs → types-CWgzGaqk.cjs} +3 -3
  101. package/dist/{runtime/authdocloader.test.js → utils/docloader.test.js} +14 -15
  102. package/dist/utils/kv-cache.test.js +209 -0
  103. package/dist/utils/mod.cjs +12 -0
  104. package/dist/utils/mod.d.cts +5 -0
  105. package/dist/utils/mod.d.ts +7 -0
  106. package/dist/utils/mod.js +11 -0
  107. package/dist/vocab/actor.test.js +8 -10
  108. package/dist/vocab/lookup.test.js +7 -9
  109. package/dist/vocab/mod.cjs +3 -4
  110. package/dist/vocab/mod.d.cts +3 -5
  111. package/dist/vocab/mod.d.ts +3 -5
  112. package/dist/vocab/mod.js +3 -4
  113. package/dist/vocab/type.test.js +2 -3
  114. package/dist/vocab/vocab.test.js +9 -10
  115. package/dist/{vocab-BI0Ak5lL.d.ts → vocab-BCWe1Ih5.d.ts} +2 -21
  116. package/dist/{vocab-Dw1-yVGg.d.cts → vocab-CeDBzu-f.d.cts} +2 -21
  117. package/dist/{vocab-DLHpZwOW.cjs → vocab-NvDV-exu.cjs} +5 -5
  118. package/dist/{vocab-CJHF7Q71.js → vocab-wADjlIH4.js} +3 -3
  119. package/dist/webfinger/handler.test.js +24 -25
  120. package/dist/webfinger/lookup.test.js +7 -9
  121. package/dist/webfinger/mod.cjs +2 -3
  122. package/dist/webfinger/mod.d.cts +1 -3
  123. package/dist/webfinger/mod.d.ts +1 -3
  124. package/dist/webfinger/mod.js +2 -3
  125. package/dist/x/cfworkers.d.cts +2 -2
  126. package/dist/x/cfworkers.d.ts +2 -2
  127. package/dist/x/cfworkers.test.js +6 -8
  128. package/dist/x/hono.d.cts +10 -11
  129. package/dist/x/hono.d.ts +10 -11
  130. package/dist/x/sveltekit.d.cts +10 -11
  131. package/dist/x/sveltekit.d.ts +10 -11
  132. package/package.json +15 -14
  133. package/dist/assert_throws-BOO88avQ.js +0 -39
  134. package/dist/authdocloader-CEqdZdT7.cjs +0 -58
  135. package/dist/authdocloader-CZsCIRLV.js +0 -52
  136. package/dist/docloader-Buh0Ah9G.cjs +0 -4861
  137. package/dist/docloader-CxWcuWqQ.d.ts +0 -221
  138. package/dist/docloader-D-MrRyHl.d.cts +0 -219
  139. package/dist/docloader-DOmINJ1U.js +0 -4795
  140. package/dist/key-B2cp5-JF.js +0 -10
  141. package/dist/key-BR1W25rw.cjs +0 -290
  142. package/dist/key-BnjofJ4e.js +0 -260
  143. package/dist/key-DhflzsBJ.cjs +0 -10
  144. package/dist/key-WES6wszI.js +0 -10
  145. package/dist/lookup-CDOxr8vw.cjs +0 -137
  146. package/dist/lookup-D8tCnUj2.js +0 -131
  147. package/dist/lookup-VSVPBU3J.js +0 -331
  148. package/dist/middleware-9KSLASn7.js +0 -17
  149. package/dist/middleware-CH0PNtaj.cjs +0 -17
  150. package/dist/middleware-CrzCnpdt.js +0 -26
  151. package/dist/mod-CerN_Sza.d.ts +0 -104
  152. package/dist/mod-Cj1tHXBR.d.cts +0 -102
  153. package/dist/runtime/docloader.test.js +0 -522
  154. package/dist/runtime/key.test.d.ts +0 -3
  155. package/dist/runtime/key.test.js +0 -103
  156. package/dist/runtime/langstr.test.d.ts +0 -3
  157. package/dist/runtime/langstr.test.js +0 -39
  158. package/dist/runtime/link.test.d.ts +0 -3
  159. package/dist/runtime/link.test.js +0 -61
  160. package/dist/runtime/mod.cjs +0 -25
  161. package/dist/runtime/mod.d.cts +0 -6
  162. package/dist/runtime/mod.d.ts +0 -8
  163. package/dist/runtime/mod.js +0 -13
  164. package/dist/runtime/multibase/multibase.test.d.ts +0 -3
  165. package/dist/runtime/multibase/multibase.test.js +0 -358
  166. package/dist/runtime/url.test.d.ts +0 -3
  167. package/dist/runtime/url.test.js +0 -45
  168. /package/dist/{assert_not_equals-f3m3epl3.js → assert_not_equals-C80BG-_5.js} +0 -0
  169. /package/dist/{collection-CcnIw1qY.js → collection-BzWsN9pB.js} +0 -0
  170. /package/dist/{denokv-Bv33Xxea.js → denokv-CCssOzMJ.js} +0 -0
  171. /package/dist/{federation-H2_En3j5.cjs → federation-CRpdnOMS.cjs} +0 -0
  172. /package/dist/{federation-D1U8YY9t.js → federation-jcR8-ZxP.js} +0 -0
  173. /package/dist/{kv-C7sopW2E.d.ts → kv-BKNZ-Tb-.d.ts} +0 -0
  174. /package/dist/{kv-63Cil1MD.d.cts → kv-Bxr0Q87_.d.cts} +0 -0
  175. /package/dist/{mod-FZd39qVq.d.cts → mod-B-hUPT2N.d.cts} +0 -0
  176. /package/dist/{mod-1pDWKvUL.d.ts → mod-CVgZgliM.d.ts} +0 -0
  177. /package/dist/{mod-g0xFzAP9.d.ts → mod-xIj-IT58.d.ts} +0 -0
  178. /package/dist/{mq-CRGm1e_F.d.ts → mq-CUKlBw08.d.ts} +0 -0
  179. /package/dist/{mq-B7R1Q-M5.d.cts → mq-DcJPkXD5.d.cts} +0 -0
  180. /package/dist/{negotiation-5NPJL6zp.js → negotiation-C4nFufNk.js} +0 -0
  181. /package/dist/{nodeinfo-DfycQ8Wf.js → nodeinfo-BnthBobC.js} +0 -0
  182. /package/dist/{nodeinfo-Co9lJrWl.cjs → nodeinfo-CdN0rEnZ.cjs} +0 -0
  183. /package/dist/{retry-D4GJ670a.js → retry-CfF8Gn4d.js} +0 -0
  184. /package/dist/{runtime-DPYEDf-o.js → sig-C34-oHBl.js} +0 -0
  185. /package/dist/{runtime-C58AJWSv.cjs → sig-YYj5tCnr.cjs} +0 -0
  186. /package/dist/{std__assert-X-_kMxKM.js → std__assert-DWivtrGR.js} +0 -0
  187. /package/dist/{types-BSuWJsOm.js → types-C2XVl6gj.js} +0 -0
  188. /package/dist/{runtime → utils}/docloader.test.d.ts +0 -0
  189. /package/dist/{runtime/authdocloader.test.d.ts → utils/kv-cache.test.d.ts} +0 -0
  190. /package/dist/{sig-Cj3tk-ig.js → utils-D-Va7aXC.js} +0 -0
  191. /package/dist/{sig-ByHXzqUi.cjs → utils-DyRU1gdZ.cjs} +0 -0
  192. /package/dist/{webfinger-De_bU0iE.js → webfinger-C72Y8lrh.js} +0 -0
  193. /package/dist/{webfinger-BjOEdFPs.cjs → webfinger-vAtLmxOF.cjs} +0 -0
@@ -0,0 +1,133 @@
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-ngJvMHez.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, duration] of rules) {
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
+ Object.defineProperty(exports, 'getAuthenticatedDocumentLoader', {
123
+ enumerable: true,
124
+ get: function () {
125
+ return getAuthenticatedDocumentLoader;
126
+ }
127
+ });
128
+ Object.defineProperty(exports, 'kvCache', {
129
+ enumerable: true,
130
+ get: function () {
131
+ return kvCache;
132
+ }
133
+ });
@@ -0,0 +1,121 @@
1
+
2
+ import { Temporal } from "@js-temporal/polyfill";
3
+ import { URLPattern } from "urlpattern-polyfill";
4
+
5
+ import { doubleKnock, validateCryptoKey } from "./http-D7qwMpca.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, duration] of rules) {
107
+ if (typeof pattern === "string") {
108
+ if (url === pattern) return duration;
109
+ continue;
110
+ }
111
+ if (pattern instanceof URL) {
112
+ if (pattern.href == url) return duration;
113
+ continue;
114
+ }
115
+ if (pattern.test(url)) return duration;
116
+ }
117
+ return null;
118
+ }
119
+
120
+ //#endregion
121
+ export { getAuthenticatedDocumentLoader, kvCache };
@@ -0,0 +1,93 @@
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, duration] of rules) {
79
+ if (typeof pattern === "string") {
80
+ if (url === pattern) return duration;
81
+ continue;
82
+ }
83
+ if (pattern instanceof URL) {
84
+ if (pattern.href == url) return duration;
85
+ continue;
86
+ }
87
+ if (pattern.test(url)) return duration;
88
+ }
89
+ return null;
90
+ }
91
+
92
+ //#endregion
93
+ export { MockKvStore, kvCache };
@@ -3,8 +3,9 @@
3
3
  import { URLPattern } from "urlpattern-polyfill";
4
4
  globalThis.addEventListener = () => {};
5
5
 
6
- import { Activity, CryptographicKey, Object as Object$1, deno_default, getDocumentLoader, getTypeId } from "./type-D_8QzkEn.js";
7
- import { fetchKey, validateCryptoKey } from "./key-CIIkeide.js";
6
+ import { Activity, CryptographicKey, Object as Object$1, deno_default, getTypeId } from "./lookup-NXjB7xUj.js";
7
+ import { fetchKey, validateCryptoKey } from "./key-CgKiloJr.js";
8
+ import { getDocumentLoader } from "@fedify/vocab-runtime";
8
9
  import { getLogger } from "@logtape/logtape";
9
10
  import { SpanStatusCode, trace } from "@opentelemetry/api";
10
11
  import { decodeBase64, encodeBase64 } from "byte-encodings/base64";
@@ -0,0 +1,273 @@
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 __logtape_logtape = require_chunk.__toESM(require("@logtape/logtape"));
7
+ const __opentelemetry_api = require_chunk.__toESM(require("@opentelemetry/api"));
8
+ const __fedify_vocab_runtime = require_chunk.__toESM(require("@fedify/vocab-runtime"));
9
+
10
+ //#region deno.json
11
+ var name = "@fedify/fedify";
12
+ var version = "2.0.0-dev.1736+ea8e853e";
13
+ var license = "MIT";
14
+ var exports$1 = {
15
+ ".": "./src/mod.ts",
16
+ "./compat": "./src/compat/mod.ts",
17
+ "./federation": "./src/federation/mod.ts",
18
+ "./nodeinfo": "./src/nodeinfo/mod.ts",
19
+ "./sig": "./src/sig/mod.ts",
20
+ "./testing": "./src/testing/mod.ts",
21
+ "./vocab": "./src/vocab/mod.ts",
22
+ "./utils": "./src/utils/mod.ts",
23
+ "./webfinger": "./src/webfinger/mod.ts",
24
+ "./x/cfworkers": "./src/x/cfworkers.ts",
25
+ "./x/denokv": "./src/x/denokv.ts",
26
+ "./x/fresh": "./src/x/fresh.ts",
27
+ "./x/hono": "./src/x/hono.ts",
28
+ "./x/sveltekit": "./src/x/sveltekit.ts"
29
+ };
30
+ var imports = {
31
+ "@cfworker/json-schema": "npm:@cfworker/json-schema@^4.1.1",
32
+ "@multiformats/base-x": "npm:@multiformats/base-x@^4.0.1",
33
+ "@opentelemetry/api": "npm:@opentelemetry/api@^1.9.0",
34
+ "@opentelemetry/semantic-conventions": "npm:@opentelemetry/semantic-conventions@^1.27.0",
35
+ "@std/assert": "jsr:@std/assert@^0.226.0",
36
+ "@std/testing": "jsr:@std/testing@^0.224.0",
37
+ "@std/url": "jsr:@std/url@^0.225.1",
38
+ "asn1js": "npm:asn1js@^3.0.5",
39
+ "byte-encodings": "npm:byte-encodings@^1.0.11",
40
+ "es-toolkit": "jsr:@es-toolkit/es-toolkit@^1.39.5",
41
+ "fast-check": "npm:fast-check@^3.22.0",
42
+ "fetch-mock": "npm:fetch-mock@^12.5.2",
43
+ "json-canon": "npm:json-canon@^1.0.1",
44
+ "jsonld": "npm:jsonld@^8.3.2",
45
+ "multicodec": "npm:multicodec@^3.2.1",
46
+ "pkijs": "npm:pkijs@^3.2.4",
47
+ "structured-field-values": "npm:structured-field-values@^2.0.4",
48
+ "uri-template-router": "npm:uri-template-router@^0.0.17",
49
+ "url-template": "npm:url-template@^3.1.1"
50
+ };
51
+ var include = ["src/vocab/vocab.ts"];
52
+ var exclude = [
53
+ ".test-report.xml",
54
+ "apidoc/",
55
+ "dist/",
56
+ "node_modules/",
57
+ "npm/",
58
+ "pnpm-lock.yaml",
59
+ "src/cfworkers/dist/",
60
+ "src/cfworkers/fixtures/",
61
+ "src/cfworkers/imports.ts",
62
+ "src/cfworkers/README.md",
63
+ "src/cfworkers/server.ts",
64
+ "src/cfworkers/server.js",
65
+ "src/cfworkers/server.js.map",
66
+ "src/codegen/schema.yaml",
67
+ "src/vocab/*.yaml",
68
+ "!src/vocab/vocab.ts"
69
+ ];
70
+ var tasks = {
71
+ "codegen": "GENPATH=vocab-$(deno eval \"console.log(crypto.randomUUID());\").ts && deno run --allow-read --allow-write --check src/codegen/main.ts src/vocab/ src/vocab/$GENPATH && deno fmt src/vocab/$GENPATH && mv src/vocab/$GENPATH src/vocab/vocab.ts && deno cache src/vocab/vocab.ts && deno check src/vocab/vocab.ts",
72
+ "cache": {
73
+ "command": "deno cache src/mod.ts",
74
+ "dependencies": ["codegen"]
75
+ },
76
+ "check": {
77
+ "command": "deno fmt --check && deno lint && deno check src/**/*.ts",
78
+ "dependencies": ["codegen"]
79
+ },
80
+ "test": {
81
+ "command": "deno test --check --doc --allow-read --allow-write --allow-env --unstable-kv --trace-leaks --parallel",
82
+ "dependencies": ["codegen"]
83
+ },
84
+ "coverage": "deno task test --clean --coverage && deno coverage --html coverage",
85
+ "bench": {
86
+ "command": "deno bench --allow-read --allow-write --allow-net --allow-env --allow-run --unstable-kv",
87
+ "dependencies": ["codegen"]
88
+ },
89
+ "apidoc": {
90
+ "command": "deno doc --html --name=Fedify --output=apidoc/ src/mod.ts",
91
+ "dependencies": ["codegen"]
92
+ },
93
+ "publish": {
94
+ "command": "deno publish",
95
+ "dependencies": ["codegen"]
96
+ },
97
+ "pnpm:install": "pnpm install --silent",
98
+ "pnpm:build": {
99
+ "command": "pnpm exec tsdown",
100
+ "dependencies": [
101
+ "codegen",
102
+ "pnpm:install",
103
+ "pnpm:build-vocab"
104
+ ]
105
+ },
106
+ "pnpm:build-vocab-runtime": { "command": "cd ../vocab-runtime && pnpm build" },
107
+ "pnpm:build-vocab": { "dependencies": ["pnpm:build-vocab-runtime"] },
108
+ "test:node": {
109
+ "command": "cd dist/ && node --test",
110
+ "dependencies": ["pnpm:build"]
111
+ },
112
+ "test:bun": {
113
+ "command": "cd dist/ && bun test --timeout 60000",
114
+ "dependencies": ["pnpm:build-vocab", "pnpm:build"]
115
+ },
116
+ "test:cfworkers": {
117
+ "command": "pnpm exec wrangler deploy --dry-run --outdir src/cfworkers && node --import=tsx src/cfworkers/client.ts",
118
+ "dependencies": ["pnpm:build-vocab", "pnpm:build"]
119
+ },
120
+ "test-all": { "dependencies": [
121
+ "check",
122
+ "test",
123
+ "test:node",
124
+ "test:bun",
125
+ "test:cfworkers"
126
+ ] }
127
+ };
128
+ var deno_default = {
129
+ name,
130
+ version,
131
+ license,
132
+ exports: exports$1,
133
+ imports,
134
+ include,
135
+ exclude,
136
+ tasks
137
+ };
138
+
139
+ //#endregion
140
+ //#region src/webfinger/lookup.ts
141
+ const logger = (0, __logtape_logtape.getLogger)([
142
+ "fedify",
143
+ "webfinger",
144
+ "lookup"
145
+ ]);
146
+ const DEFAULT_MAX_REDIRECTION = 5;
147
+ /**
148
+ * Looks up a WebFinger resource.
149
+ * @param resource The resource URL to look up.
150
+ * @param options Extra options for looking up the resource.
151
+ * @returns The resource descriptor, or `null` if not found.
152
+ * @since 0.2.0
153
+ */
154
+ async function lookupWebFinger(resource, options = {}) {
155
+ const tracerProvider = options.tracerProvider ?? __opentelemetry_api.trace.getTracerProvider();
156
+ const tracer = tracerProvider.getTracer(deno_default.name, deno_default.version);
157
+ return await tracer.startActiveSpan("webfinger.lookup", {
158
+ kind: __opentelemetry_api.SpanKind.CLIENT,
159
+ attributes: {
160
+ "webfinger.resource": resource.toString(),
161
+ "webfinger.resource.scheme": typeof resource === "string" ? resource.replace(/:.*$/, "") : resource.protocol.replace(/:$/, "")
162
+ }
163
+ }, async (span) => {
164
+ try {
165
+ const result = await lookupWebFingerInternal(resource, options);
166
+ span.setStatus({ code: result === null ? __opentelemetry_api.SpanStatusCode.ERROR : __opentelemetry_api.SpanStatusCode.OK });
167
+ return result;
168
+ } catch (error) {
169
+ span.setStatus({
170
+ code: __opentelemetry_api.SpanStatusCode.ERROR,
171
+ message: String(error)
172
+ });
173
+ throw error;
174
+ } finally {
175
+ span.end();
176
+ }
177
+ });
178
+ }
179
+ async function lookupWebFingerInternal(resource, options = {}) {
180
+ if (typeof resource === "string") resource = new URL(resource);
181
+ let protocol = "https:";
182
+ let server;
183
+ if (resource.protocol === "acct:") {
184
+ const atPos = resource.pathname.lastIndexOf("@");
185
+ if (atPos < 0) return null;
186
+ server = resource.pathname.substring(atPos + 1);
187
+ if (server === "") return null;
188
+ } else {
189
+ protocol = resource.protocol;
190
+ server = resource.host;
191
+ }
192
+ let url = new URL(`${protocol}//${server}/.well-known/webfinger`);
193
+ url.searchParams.set("resource", resource.href);
194
+ let redirected = 0;
195
+ while (true) {
196
+ logger.debug("Fetching WebFinger resource descriptor from {url}...", { url: url.href });
197
+ let response;
198
+ if (options.allowPrivateAddress !== true) try {
199
+ await (0, __fedify_vocab_runtime.validatePublicUrl)(url.href);
200
+ } catch (e) {
201
+ if (e instanceof __fedify_vocab_runtime.UrlError) {
202
+ logger.error("Invalid URL for WebFinger resource descriptor: {error}", { error: e });
203
+ return null;
204
+ }
205
+ throw e;
206
+ }
207
+ try {
208
+ response = await fetch(url, {
209
+ headers: {
210
+ Accept: "application/jrd+json",
211
+ "User-Agent": typeof options.userAgent === "string" ? options.userAgent : (0, __fedify_vocab_runtime.getUserAgent)(options.userAgent)
212
+ },
213
+ redirect: "manual",
214
+ signal: options.signal
215
+ });
216
+ } catch (error) {
217
+ logger.debug("Failed to fetch WebFinger resource descriptor: {error}", {
218
+ url: url.href,
219
+ error
220
+ });
221
+ return null;
222
+ }
223
+ if (response.status >= 300 && response.status < 400 && response.headers.has("Location")) {
224
+ redirected++;
225
+ const maxRedirection = options.maxRedirection ?? DEFAULT_MAX_REDIRECTION;
226
+ if (redirected >= maxRedirection) {
227
+ logger.error("Too many redirections ({redirections}) while fetching WebFinger resource descriptor.", { redirections: redirected });
228
+ return null;
229
+ }
230
+ const redirectedUrl = new URL(response.headers.get("Location"), response.url == null || response.url === "" ? url : response.url);
231
+ if (redirectedUrl.protocol !== url.protocol) {
232
+ logger.error("Redirected to a different protocol ({protocol} to {redirectedProtocol}) while fetching WebFinger resource descriptor.", {
233
+ protocol: url.protocol,
234
+ redirectedProtocol: redirectedUrl.protocol
235
+ });
236
+ return null;
237
+ }
238
+ url = redirectedUrl;
239
+ continue;
240
+ }
241
+ if (!response.ok) {
242
+ logger.debug("Failed to fetch WebFinger resource descriptor: {status} {statusText}.", {
243
+ url: url.href,
244
+ status: response.status,
245
+ statusText: response.statusText
246
+ });
247
+ return null;
248
+ }
249
+ try {
250
+ return await response.json();
251
+ } catch (e) {
252
+ if (e instanceof SyntaxError) {
253
+ logger.debug("Failed to parse WebFinger resource descriptor as JSON: {error}", { error: e });
254
+ return null;
255
+ }
256
+ throw e;
257
+ }
258
+ }
259
+ }
260
+
261
+ //#endregion
262
+ Object.defineProperty(exports, 'deno_default', {
263
+ enumerable: true,
264
+ get: function () {
265
+ return deno_default;
266
+ }
267
+ });
268
+ Object.defineProperty(exports, 'lookupWebFinger', {
269
+ enumerable: true,
270
+ get: function () {
271
+ return lookupWebFinger;
272
+ }
273
+ });