@fedify/fedify 2.2.20 → 2.2.21

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 (102) hide show
  1. package/dist/attachment-arrays-CalAATqD.mjs +16 -0
  2. package/dist/{builder-BolQ56ab.mjs → builder-4ImvtjG4.mjs} +3 -3
  3. package/dist/callback-0KrwzutD.d.mts +2607 -0
  4. package/dist/{client-jE7oGvRh.d.ts → client-9DPcy_Yx.d.ts} +16 -0
  5. package/dist/{client-BIBzqEIp.d.cts → client-B3ruDgWg.d.cts} +16 -0
  6. package/dist/{client-DhxaVgOv.mjs → client-XYMC0Mmk.mjs} +86 -11
  7. package/dist/compat/attachment-arrays.test.d.mts +2 -0
  8. package/dist/compat/attachment-arrays.test.mjs +255 -0
  9. package/dist/compat/mod.d.cts +1 -1
  10. package/dist/compat/mod.d.ts +1 -1
  11. package/dist/compat/outgoing-jsonld.test.mjs +1 -1
  12. package/dist/compat/public-audience.test.mjs +1 -1
  13. package/dist/compat/transformers.test.mjs +1 -1
  14. package/dist/{context-CFQGaq5x.d.cts → context-CjWuov0g.d.cts} +31 -14
  15. package/dist/{context-DdbLVV_k.d.ts → context-DZqI2LJD.d.ts} +31 -14
  16. package/dist/{deno-BUj4roJc.mjs → deno-AWk3S2Ug.mjs} +1 -1
  17. package/dist/{docloader-CbtF_0cD.mjs → docloader-Bogpjg4d.mjs} +2 -2
  18. package/dist/federation/attachment-responses.test.d.mts +2 -0
  19. package/dist/federation/attachment-responses.test.mjs +182 -0
  20. package/dist/federation/builder.test.mjs +1 -1
  21. package/dist/federation/collection.test.mjs +1 -1
  22. package/dist/federation/delivery-attachments.test.d.mts +2 -0
  23. package/dist/federation/delivery-attachments.test.mjs +288 -0
  24. package/dist/federation/handler.test.mjs +208 -16
  25. package/dist/federation/idempotency.test.mjs +2 -2
  26. package/dist/federation/inbox.test.mjs +2 -2
  27. package/dist/federation/middleware.test.mjs +1184 -8
  28. package/dist/federation/mod.cjs +1 -1
  29. package/dist/federation/mod.d.cts +2 -2
  30. package/dist/federation/mod.d.ts +2 -2
  31. package/dist/federation/mod.js +1 -1
  32. package/dist/federation/negotiation.test.mjs +1 -1
  33. package/dist/federation/retry.test.mjs +1 -1
  34. package/dist/federation/send.test.mjs +283 -3
  35. package/dist/federation/temporal.test.mjs +25 -1
  36. package/dist/federation/webfinger.test.mjs +1 -1
  37. package/dist/{http-CQMVisM0.mjs → http-B3wCvgu8.mjs} +6 -4
  38. package/dist/{http-D8I7LBfx.cjs → http-BNaPJVnF.cjs} +6 -4
  39. package/dist/{http-C5g_VrA_.js → http-BpdLaGy8.js} +6 -4
  40. package/dist/{http-9IFUxdkp.d.cts → http-C8qLHs_Z.d.cts} +3 -3
  41. package/dist/{http-SbfezERn.d.ts → http-D8WmcRAF.d.ts} +3 -3
  42. package/dist/{inbox-CD8IDILN.mjs → inbox-DLy6WBXU.mjs} +1 -1
  43. package/dist/{key-C3o3DGap.mjs → key-Bhc8cQC7.mjs} +3 -2
  44. package/dist/{kv-cache-DC9LcfoC.cjs → kv-cache-Bj3CY9Jo.cjs} +1 -1
  45. package/dist/{kv-cache-CSKXXleR.js → kv-cache-CvjLUQTf.js} +1 -1
  46. package/dist/{ld-ijSr-0AB.mjs → ld-DTeykI0e.mjs} +153 -21
  47. package/dist/ld-signature-Dgz_b89j.mjs +23 -0
  48. package/dist/{middleware-DNueR0GD.cjs → middleware-BSQP4d9p.cjs} +475 -107
  49. package/dist/{middleware-DmcY2TWS.cjs → middleware-CWOzxb3o.cjs} +1 -1
  50. package/dist/{middleware-yqqfI7oJ.mjs → middleware-DkMsC5mQ.mjs} +1 -1
  51. package/dist/{middleware-sjhz30sm.js → middleware-Dx1iwFk8.js} +474 -107
  52. package/dist/{middleware-CYAaUTi5.mjs → middleware-dlGvSrXl.mjs} +386 -96
  53. package/dist/{mod-BYWp77-z.d.cts → mod-BmxqcEnU.d.ts} +30 -13
  54. package/dist/{mod-CVSKw-5H.d.cts → mod-DWEx2D4q.d.cts} +1 -1
  55. package/dist/{mod-BuRRdsfD.d.ts → mod-DnPmz3-S.d.ts} +1 -1
  56. package/dist/{mod-CM5_J_cx.d.ts → mod-INjDw-m0.d.cts} +30 -13
  57. package/dist/{mod-BRnwh2oy.d.ts → mod-REfljt0T.d.ts} +3 -3
  58. package/dist/{mod-Co7LEK9X.d.cts → mod-UvIGUHqW.d.cts} +3 -3
  59. package/dist/mod.cjs +5 -5
  60. package/dist/mod.d.cts +8 -8
  61. package/dist/mod.d.ts +8 -8
  62. package/dist/mod.js +5 -5
  63. package/dist/nodeinfo/client.test.mjs +432 -4
  64. package/dist/nodeinfo/handler.test.mjs +1 -1
  65. package/dist/nodeinfo/mod.cjs +1 -1
  66. package/dist/nodeinfo/mod.d.cts +1 -1
  67. package/dist/nodeinfo/mod.d.ts +1 -1
  68. package/dist/nodeinfo/mod.js +1 -1
  69. package/dist/nodeinfo/types.test.mjs +1 -1
  70. package/dist/{outgoing-jsonld-amccRMrV.mjs → outgoing-jsonld-DHf_DYXp.mjs} +38 -8
  71. package/dist/{owner-B8kXzD0q.mjs → owner-BsA7qMHB.mjs} +2 -2
  72. package/dist/{proof-CBQOjOgA.mjs → proof-C63Hqt9N.mjs} +40 -15
  73. package/dist/{proof-qiuGJCD7.cjs → proof-Dbz6ApNP.cjs} +1190 -1007
  74. package/dist/{proof-DKX6nASL.js → proof-QV_o5ZqS.js} +1174 -1003
  75. package/dist/{send-DRpisX_f.mjs → send-CAdpwbFX.mjs} +92 -21
  76. package/dist/sig/http.test.mjs +2 -2
  77. package/dist/sig/key-types.test.d.mts +218 -0
  78. package/dist/sig/key-types.test.mjs +8 -0
  79. package/dist/sig/key.test.mjs +2 -2
  80. package/dist/sig/ld.test.mjs +573 -8
  81. package/dist/sig/mod.cjs +2 -2
  82. package/dist/sig/mod.d.cts +3 -3
  83. package/dist/sig/mod.d.ts +3 -3
  84. package/dist/sig/mod.js +2 -2
  85. package/dist/sig/owner.test.mjs +1 -1
  86. package/dist/sig/proof.test.mjs +96 -4
  87. package/dist/{temporal-BQGEINad.mjs → temporal-B3JGkVgk.mjs} +3 -5
  88. package/dist/testing/mod.d.mts +2 -2422
  89. package/dist/{types-DOvyymM4.cjs → types-BWx7zw3Y.cjs} +85 -10
  90. package/dist/{types-B1_pRFE_.js → types-Dx_w2JSM.js} +86 -11
  91. package/dist/utils/docloader.test.mjs +2 -2
  92. package/dist/utils/mod.cjs +1 -1
  93. package/dist/utils/mod.d.cts +1 -1
  94. package/dist/utils/mod.d.ts +1 -1
  95. package/dist/utils/mod.js +1 -1
  96. package/package.json +5 -5
  97. /package/dist/{activity-listener-ell7W1s9.mjs → activity-listener-tztVvlNb.mjs} +0 -0
  98. /package/dist/{collection-D-HqUuA2.mjs → collection-CA3V5zyK.mjs} +0 -0
  99. /package/dist/{negotiation-SQvQgUqe.mjs → negotiation-VnHNB0Q5.mjs} +0 -0
  100. /package/dist/{public-audience-DpJqzPuh.mjs → public-audience-4g3_Ze4y.mjs} +0 -0
  101. /package/dist/{retry-v_sGLH1d.mjs → retry-CXg_MBI-.mjs} +0 -0
  102. /package/dist/{types-J53Kw7so.mjs → types-BFowWFTT.mjs} +0 -0
@@ -25,13 +25,18 @@ function validateBodySizeLimit(maxBytes) {
25
25
  * @param message The response or request to read.
26
26
  * @param maxBytes The maximum number of decoded bytes to read.
27
27
  * @param url The URL to include in errors and logs.
28
+ * @param signal An optional signal for cancelling the body read.
28
29
  * @returns The decoded body text.
29
30
  * @throws {BodyTooLargeError} If the body exceeds the limit.
30
31
  * @internal
31
32
  */
32
- async function readBoundedText(message, maxBytes, url) {
33
+ async function readBoundedText(message, maxBytes, url, signal) {
33
34
  validateBodySizeLimit(maxBytes);
34
35
  const reader = message.body?.getReader();
36
+ const onAbort = () => {
37
+ if (reader != null) reader.cancel(signal?.reason).catch(() => {});
38
+ };
39
+ signal?.addEventListener("abort", onAbort, { once: true });
35
40
  const tooLarge = () => {
36
41
  (0, _logtape_logtape.getLogger)([
37
42
  "fedify",
@@ -44,6 +49,7 @@ async function readBoundedText(message, maxBytes, url) {
44
49
  throw new BodyTooLargeError(url, maxBytes);
45
50
  };
46
51
  try {
52
+ signal?.throwIfAborted();
47
53
  const length = message.headers.get("Content-Length");
48
54
  const encoding = message.headers.get("Content-Encoding");
49
55
  if ((encoding == null || encoding.toLowerCase() === "identity") && length != null && /^\d+$/.test(length) && Number(length) > maxBytes) tooLarge();
@@ -51,6 +57,7 @@ async function readBoundedText(message, maxBytes, url) {
51
57
  let size = 0;
52
58
  const textReader = new ReadableStream({ async pull(controller) {
53
59
  const { done, value } = await reader.read();
60
+ signal?.throwIfAborted();
54
61
  if (done) {
55
62
  controller.close();
56
63
  return;
@@ -63,6 +70,7 @@ async function readBoundedText(message, maxBytes, url) {
63
70
  const chunks = [];
64
71
  while (true) {
65
72
  const { done, value } = await textReader.read();
73
+ signal?.throwIfAborted();
66
74
  if (done) break;
67
75
  chunks.push(value);
68
76
  }
@@ -74,6 +82,7 @@ async function readBoundedText(message, maxBytes, url) {
74
82
  if (reader != null) reader.cancel(error).catch(() => {});
75
83
  throw error;
76
84
  } finally {
85
+ signal?.removeEventListener("abort", onAbort);
77
86
  reader?.releaseLock();
78
87
  }
79
88
  }
@@ -85,6 +94,8 @@ const logger = (0, _logtape_logtape.getLogger)([
85
94
  "client"
86
95
  ]);
87
96
  const DEFAULT_MAX_REDIRECTION = 5;
97
+ const DEFAULT_TIMEOUT = 1e4;
98
+ const MAX_TIMEOUT = 2147483647;
88
99
  /**
89
100
  * Fetches a URL while guarding against SSRF. Every request—including each
90
101
  * redirect hop—is made in its own session opened with `openPublicFetch()`,
@@ -101,20 +112,27 @@ const DEFAULT_MAX_REDIRECTION = 5;
101
112
  * @param headers The request headers.
102
113
  * @param consume The function to consume the final response with.
103
114
  * @param deps The dependencies.
115
+ * @param signal The signal for the whole NodeInfo lookup.
104
116
  * @returns The value returned by `consume`, or `null` if the URL is
105
117
  * disallowed, its hostname cannot be resolved, or there are too many
106
118
  * redirects.
107
119
  * @internal
108
120
  */
109
- async function fetchPublicUrl(url, headers, consume, deps = {}) {
110
- const openHop = deps.openHop ?? ((hopUrl) => (0, _fedify_vocab_runtime_internal.openPublicFetch)(hopUrl));
121
+ async function fetchPublicUrl(url, headers, consume, deps = {}, signal) {
122
+ const openHop = deps.openHop ?? ((hopUrl, signal) => (0, _fedify_vocab_runtime_internal.openPublicFetch)(hopUrl, (0, _fedify_vocab_runtime_internal.detectRuntime)() === "node" ? {
123
+ signal,
124
+ lookup: (hostname) => (0, _fedify_vocab_runtime_internal.lookupAllAddressesBounded)(hostname, signal)
125
+ } : { signal }));
111
126
  let current = url instanceof URL ? url : new URL(url);
112
127
  let redirected = 0;
128
+ const visited = new Set([redirectKey(current)]);
113
129
  while (true) {
130
+ signal?.throwIfAborted();
114
131
  let session;
115
132
  try {
116
- session = await openHop(current.href);
133
+ session = await openHop(current.href, signal);
117
134
  } catch (e) {
135
+ signal?.throwIfAborted();
118
136
  if (e instanceof _fedify_vocab_runtime.UrlError) {
119
137
  if (e.reason === "dns") logger.error("DNS lookup failed for {url}: {error}", {
120
138
  url: current.href,
@@ -130,10 +148,16 @@ async function fetchPublicUrl(url, headers, consume, deps = {}) {
130
148
  }
131
149
  let location;
132
150
  try {
151
+ signal?.throwIfAborted();
133
152
  const response = await session.fetch(current, {
134
153
  headers,
135
- redirect: "manual"
154
+ redirect: "manual",
155
+ signal
136
156
  });
157
+ if (signal?.aborted) {
158
+ cancelBody(response, current);
159
+ signal.throwIfAborted();
160
+ }
137
161
  if (response.status < 300 || response.status >= 400 || !response.headers.has("Location")) return await consume(response);
138
162
  location = response.headers.get("Location");
139
163
  cancelBody(response, current);
@@ -156,9 +180,20 @@ async function fetchPublicUrl(url, headers, consume, deps = {}) {
156
180
  });
157
181
  return null;
158
182
  }
183
+ const key = redirectKey(next);
184
+ if (visited.has(key)) {
185
+ logger.error("Redirect cycle while fetching NodeInfo: {url}.", { url: next.href });
186
+ return null;
187
+ }
188
+ visited.add(key);
159
189
  current = next;
160
190
  }
161
191
  }
192
+ function redirectKey(url) {
193
+ const withoutFragment = new URL(url);
194
+ withoutFragment.hash = "";
195
+ return withoutFragment.href;
196
+ }
162
197
  /**
163
198
  * Starts cancelling a response body that is not read, without waiting for it
164
199
  * to settle. The cancellation of a branch of a tee (e.g., a body cloned by
@@ -189,6 +224,46 @@ async function getNodeInfo(url, options = {}) {
189
224
  * @internal
190
225
  */
191
226
  async function getNodeInfoInternal(url, options = {}, deps = {}) {
227
+ const timeout = resolveTimeout(options.timeout);
228
+ if (timeout == null && options.signal == null) return await getNodeInfoWithSignal(url, options, deps);
229
+ const controller = new AbortController();
230
+ const timeoutReason = timeout == null ? void 0 : new DOMException(`The NodeInfo lookup timed out after ${timeout} ms.`, "TimeoutError");
231
+ const timer = timeout == null ? void 0 : setTimeout(() => controller.abort(timeoutReason), timeout);
232
+ const onCallerAbort = () => controller.abort(options.signal?.reason);
233
+ options.signal?.addEventListener("abort", onCallerAbort, { once: true });
234
+ if (options.signal?.aborted) onCallerAbort();
235
+ let onAbort;
236
+ const aborted = new Promise((_, reject) => {
237
+ onAbort = () => reject(controller.signal.reason);
238
+ controller.signal.addEventListener("abort", onAbort, { once: true });
239
+ });
240
+ const loading = getNodeInfoWithSignal(url, options, deps, controller.signal);
241
+ loading.catch(() => {});
242
+ try {
243
+ return await Promise.race([loading, aborted]);
244
+ } catch (error) {
245
+ if (!controller.signal.aborted) throw error;
246
+ if (controller.signal.reason === timeoutReason) logger.error("Timed out after {timeout} ms while fetching NodeInfo: {url}", {
247
+ timeout,
248
+ url: url.toString()
249
+ });
250
+ else logger.debug("Cancelled NodeInfo lookup for {url}.", { url: url.toString() });
251
+ return;
252
+ } finally {
253
+ clearTimeout(timer);
254
+ options.signal?.removeEventListener("abort", onCallerAbort);
255
+ if (onAbort != null) controller.signal.removeEventListener("abort", onAbort);
256
+ }
257
+ }
258
+ function resolveTimeout(timeout) {
259
+ if (timeout === void 0) return DEFAULT_TIMEOUT;
260
+ if (timeout === null) return null;
261
+ if (!Number.isFinite(timeout) || timeout <= 0) throw new RangeError(`The NodeInfo timeout must be a positive finite number of milliseconds, but got ${String(timeout)}.`);
262
+ const ms = Math.ceil(timeout);
263
+ if (ms > MAX_TIMEOUT) throw new RangeError(`The NodeInfo timeout must not be greater than ${MAX_TIMEOUT} milliseconds, but got ${timeout}.`);
264
+ return ms;
265
+ }
266
+ async function getNodeInfoWithSignal(url, options, deps, signal) {
192
267
  try {
193
268
  const headers = {
194
269
  Accept: "application/json",
@@ -207,8 +282,8 @@ async function getNodeInfoInternal(url, options = {}, deps = {}) {
207
282
  cancelBody(response, wellKnownUrl);
208
283
  return;
209
284
  }
210
- return { rd: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, wellKnownUrl)) };
211
- }, deps);
285
+ return { rd: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, wellKnownUrl, signal)) };
286
+ }, deps, signal);
212
287
  if (wellKnown == null) return void 0;
213
288
  const wellKnownRd = wellKnown.rd;
214
289
  const link = wellKnownRd?.links?.find((link) => link != null && "rel" in link && (link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.0" || link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.1") && "href" in link && link.href != null);
@@ -231,14 +306,14 @@ async function getNodeInfoInternal(url, options = {}, deps = {}) {
231
306
  cancelBody(response, nodeInfoUrl);
232
307
  return;
233
308
  }
234
- return { data: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, nodeInfoUrl)) };
235
- }, deps);
309
+ return { data: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, nodeInfoUrl, signal)) };
310
+ }, deps, signal);
236
311
  if (document == null) return void 0;
237
312
  const { data } = document;
238
313
  if (options.parse === "none") return data;
239
314
  return parseNodeInfo(data, { tryBestEffort: options.parse === "best-effort" }) ?? void 0;
240
315
  } catch (error) {
241
- logger.error("Failed to fetch NodeInfo document from {url}: {error}", {
316
+ logger[signal?.aborted ? "debug" : "error"]("Failed to fetch NodeInfo document from {url}: {error}", {
242
317
  url: url.toString(),
243
318
  error
244
319
  });
@@ -2,7 +2,7 @@ import { Temporal } from "@js-temporal/polyfill";
2
2
  import { URLPattern } from "urlpattern-polyfill";
3
3
  import { getLogger } from "@logtape/logtape";
4
4
  import { FetchError, UrlError, getUserAgent } from "@fedify/vocab-runtime";
5
- import { openPublicFetch } from "@fedify/vocab-runtime/internal";
5
+ import { detectRuntime, lookupAllAddressesBounded, openPublicFetch } from "@fedify/vocab-runtime/internal";
6
6
  //#region src/utils/body.ts
7
7
  /** Decoded JSON body limit: 16 MiB. @internal */
8
8
  const MAX_BODY_SIZE = 16 * 1024 * 1024;
@@ -24,13 +24,18 @@ function validateBodySizeLimit(maxBytes) {
24
24
  * @param message The response or request to read.
25
25
  * @param maxBytes The maximum number of decoded bytes to read.
26
26
  * @param url The URL to include in errors and logs.
27
+ * @param signal An optional signal for cancelling the body read.
27
28
  * @returns The decoded body text.
28
29
  * @throws {BodyTooLargeError} If the body exceeds the limit.
29
30
  * @internal
30
31
  */
31
- async function readBoundedText(message, maxBytes, url) {
32
+ async function readBoundedText(message, maxBytes, url, signal) {
32
33
  validateBodySizeLimit(maxBytes);
33
34
  const reader = message.body?.getReader();
35
+ const onAbort = () => {
36
+ if (reader != null) reader.cancel(signal?.reason).catch(() => {});
37
+ };
38
+ signal?.addEventListener("abort", onAbort, { once: true });
34
39
  const tooLarge = () => {
35
40
  getLogger([
36
41
  "fedify",
@@ -43,6 +48,7 @@ async function readBoundedText(message, maxBytes, url) {
43
48
  throw new BodyTooLargeError(url, maxBytes);
44
49
  };
45
50
  try {
51
+ signal?.throwIfAborted();
46
52
  const length = message.headers.get("Content-Length");
47
53
  const encoding = message.headers.get("Content-Encoding");
48
54
  if ((encoding == null || encoding.toLowerCase() === "identity") && length != null && /^\d+$/.test(length) && Number(length) > maxBytes) tooLarge();
@@ -50,6 +56,7 @@ async function readBoundedText(message, maxBytes, url) {
50
56
  let size = 0;
51
57
  const textReader = new ReadableStream({ async pull(controller) {
52
58
  const { done, value } = await reader.read();
59
+ signal?.throwIfAborted();
53
60
  if (done) {
54
61
  controller.close();
55
62
  return;
@@ -62,6 +69,7 @@ async function readBoundedText(message, maxBytes, url) {
62
69
  const chunks = [];
63
70
  while (true) {
64
71
  const { done, value } = await textReader.read();
72
+ signal?.throwIfAborted();
65
73
  if (done) break;
66
74
  chunks.push(value);
67
75
  }
@@ -73,6 +81,7 @@ async function readBoundedText(message, maxBytes, url) {
73
81
  if (reader != null) reader.cancel(error).catch(() => {});
74
82
  throw error;
75
83
  } finally {
84
+ signal?.removeEventListener("abort", onAbort);
76
85
  reader?.releaseLock();
77
86
  }
78
87
  }
@@ -84,6 +93,8 @@ const logger = getLogger([
84
93
  "client"
85
94
  ]);
86
95
  const DEFAULT_MAX_REDIRECTION = 5;
96
+ const DEFAULT_TIMEOUT = 1e4;
97
+ const MAX_TIMEOUT = 2147483647;
87
98
  /**
88
99
  * Fetches a URL while guarding against SSRF. Every request—including each
89
100
  * redirect hop—is made in its own session opened with `openPublicFetch()`,
@@ -100,20 +111,27 @@ const DEFAULT_MAX_REDIRECTION = 5;
100
111
  * @param headers The request headers.
101
112
  * @param consume The function to consume the final response with.
102
113
  * @param deps The dependencies.
114
+ * @param signal The signal for the whole NodeInfo lookup.
103
115
  * @returns The value returned by `consume`, or `null` if the URL is
104
116
  * disallowed, its hostname cannot be resolved, or there are too many
105
117
  * redirects.
106
118
  * @internal
107
119
  */
108
- async function fetchPublicUrl(url, headers, consume, deps = {}) {
109
- const openHop = deps.openHop ?? ((hopUrl) => openPublicFetch(hopUrl));
120
+ async function fetchPublicUrl(url, headers, consume, deps = {}, signal) {
121
+ const openHop = deps.openHop ?? ((hopUrl, signal) => openPublicFetch(hopUrl, detectRuntime() === "node" ? {
122
+ signal,
123
+ lookup: (hostname) => lookupAllAddressesBounded(hostname, signal)
124
+ } : { signal }));
110
125
  let current = url instanceof URL ? url : new URL(url);
111
126
  let redirected = 0;
127
+ const visited = new Set([redirectKey(current)]);
112
128
  while (true) {
129
+ signal?.throwIfAborted();
113
130
  let session;
114
131
  try {
115
- session = await openHop(current.href);
132
+ session = await openHop(current.href, signal);
116
133
  } catch (e) {
134
+ signal?.throwIfAborted();
117
135
  if (e instanceof UrlError) {
118
136
  if (e.reason === "dns") logger.error("DNS lookup failed for {url}: {error}", {
119
137
  url: current.href,
@@ -129,10 +147,16 @@ async function fetchPublicUrl(url, headers, consume, deps = {}) {
129
147
  }
130
148
  let location;
131
149
  try {
150
+ signal?.throwIfAborted();
132
151
  const response = await session.fetch(current, {
133
152
  headers,
134
- redirect: "manual"
153
+ redirect: "manual",
154
+ signal
135
155
  });
156
+ if (signal?.aborted) {
157
+ cancelBody(response, current);
158
+ signal.throwIfAborted();
159
+ }
136
160
  if (response.status < 300 || response.status >= 400 || !response.headers.has("Location")) return await consume(response);
137
161
  location = response.headers.get("Location");
138
162
  cancelBody(response, current);
@@ -155,9 +179,20 @@ async function fetchPublicUrl(url, headers, consume, deps = {}) {
155
179
  });
156
180
  return null;
157
181
  }
182
+ const key = redirectKey(next);
183
+ if (visited.has(key)) {
184
+ logger.error("Redirect cycle while fetching NodeInfo: {url}.", { url: next.href });
185
+ return null;
186
+ }
187
+ visited.add(key);
158
188
  current = next;
159
189
  }
160
190
  }
191
+ function redirectKey(url) {
192
+ const withoutFragment = new URL(url);
193
+ withoutFragment.hash = "";
194
+ return withoutFragment.href;
195
+ }
161
196
  /**
162
197
  * Starts cancelling a response body that is not read, without waiting for it
163
198
  * to settle. The cancellation of a branch of a tee (e.g., a body cloned by
@@ -188,6 +223,46 @@ async function getNodeInfo(url, options = {}) {
188
223
  * @internal
189
224
  */
190
225
  async function getNodeInfoInternal(url, options = {}, deps = {}) {
226
+ const timeout = resolveTimeout(options.timeout);
227
+ if (timeout == null && options.signal == null) return await getNodeInfoWithSignal(url, options, deps);
228
+ const controller = new AbortController();
229
+ const timeoutReason = timeout == null ? void 0 : new DOMException(`The NodeInfo lookup timed out after ${timeout} ms.`, "TimeoutError");
230
+ const timer = timeout == null ? void 0 : setTimeout(() => controller.abort(timeoutReason), timeout);
231
+ const onCallerAbort = () => controller.abort(options.signal?.reason);
232
+ options.signal?.addEventListener("abort", onCallerAbort, { once: true });
233
+ if (options.signal?.aborted) onCallerAbort();
234
+ let onAbort;
235
+ const aborted = new Promise((_, reject) => {
236
+ onAbort = () => reject(controller.signal.reason);
237
+ controller.signal.addEventListener("abort", onAbort, { once: true });
238
+ });
239
+ const loading = getNodeInfoWithSignal(url, options, deps, controller.signal);
240
+ loading.catch(() => {});
241
+ try {
242
+ return await Promise.race([loading, aborted]);
243
+ } catch (error) {
244
+ if (!controller.signal.aborted) throw error;
245
+ if (controller.signal.reason === timeoutReason) logger.error("Timed out after {timeout} ms while fetching NodeInfo: {url}", {
246
+ timeout,
247
+ url: url.toString()
248
+ });
249
+ else logger.debug("Cancelled NodeInfo lookup for {url}.", { url: url.toString() });
250
+ return;
251
+ } finally {
252
+ clearTimeout(timer);
253
+ options.signal?.removeEventListener("abort", onCallerAbort);
254
+ if (onAbort != null) controller.signal.removeEventListener("abort", onAbort);
255
+ }
256
+ }
257
+ function resolveTimeout(timeout) {
258
+ if (timeout === void 0) return DEFAULT_TIMEOUT;
259
+ if (timeout === null) return null;
260
+ if (!Number.isFinite(timeout) || timeout <= 0) throw new RangeError(`The NodeInfo timeout must be a positive finite number of milliseconds, but got ${String(timeout)}.`);
261
+ const ms = Math.ceil(timeout);
262
+ if (ms > MAX_TIMEOUT) throw new RangeError(`The NodeInfo timeout must not be greater than ${MAX_TIMEOUT} milliseconds, but got ${timeout}.`);
263
+ return ms;
264
+ }
265
+ async function getNodeInfoWithSignal(url, options, deps, signal) {
191
266
  try {
192
267
  const headers = {
193
268
  Accept: "application/json",
@@ -206,8 +281,8 @@ async function getNodeInfoInternal(url, options = {}, deps = {}) {
206
281
  cancelBody(response, wellKnownUrl);
207
282
  return;
208
283
  }
209
- return { rd: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, wellKnownUrl)) };
210
- }, deps);
284
+ return { rd: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, wellKnownUrl, signal)) };
285
+ }, deps, signal);
211
286
  if (wellKnown == null) return void 0;
212
287
  const wellKnownRd = wellKnown.rd;
213
288
  const link = wellKnownRd?.links?.find((link) => link != null && "rel" in link && (link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.0" || link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.1") && "href" in link && link.href != null);
@@ -230,14 +305,14 @@ async function getNodeInfoInternal(url, options = {}, deps = {}) {
230
305
  cancelBody(response, nodeInfoUrl);
231
306
  return;
232
307
  }
233
- return { data: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, nodeInfoUrl)) };
234
- }, deps);
308
+ return { data: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, nodeInfoUrl, signal)) };
309
+ }, deps, signal);
235
310
  if (document == null) return void 0;
236
311
  const { data } = document;
237
312
  if (options.parse === "none") return data;
238
313
  return parseNodeInfo(data, { tryBestEffort: options.parse === "best-effort" }) ?? void 0;
239
314
  } catch (error) {
240
- logger.error("Failed to fetch NodeInfo document from {url}: {error}", {
315
+ logger[signal?.aborted ? "debug" : "error"]("Failed to fetch NodeInfo document from {url}: {error}", {
241
316
  url: url.toString(),
242
317
  error
243
318
  });
@@ -5,9 +5,9 @@ import { t as esm_default } from "../esm-DVILvP5e.mjs";
5
5
  import { t as assertEquals } from "../assert_equals-Ew3jOFa3.mjs";
6
6
  import "../std__assert-DNZ4Liil.mjs";
7
7
  import { t as assertRejects } from "../assert_rejects-WyYSTDDX.mjs";
8
- import { u as verifyRequest } from "../http-CQMVisM0.mjs";
8
+ import { u as verifyRequest } from "../http-B3wCvgu8.mjs";
9
9
  import { i as rsaPrivateKey2 } from "../keys-BAK-tUlf.mjs";
10
- import { n as getAuthenticatedDocumentLoader, t as createAuthenticatedDocumentLoader } from "../docloader-CbtF_0cD.mjs";
10
+ import { n as getAuthenticatedDocumentLoader, t as createAuthenticatedDocumentLoader } from "../docloader-Bogpjg4d.mjs";
11
11
  import { mockDocumentLoader, test } from "@fedify/fixture";
12
12
  import { FetchError, UrlError } from "@fedify/vocab-runtime";
13
13
  import { openDirectFetch, openPublicFetch } from "@fedify/vocab-runtime/internal";
@@ -1,6 +1,6 @@
1
1
  const { Temporal } = require("@js-temporal/polyfill");
2
2
  const { URLPattern } = require("urlpattern-polyfill");
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
4
- const require_kv_cache = require("../kv-cache-DC9LcfoC.cjs");
4
+ const require_kv_cache = require("../kv-cache-Bj3CY9Jo.cjs");
5
5
  exports.getAuthenticatedDocumentLoader = require_kv_cache.getAuthenticatedDocumentLoader;
6
6
  exports.kvCache = require_kv_cache.kvCache;
@@ -1,3 +1,3 @@
1
1
  /// <reference lib="esnext.temporal" />
2
- import { n as getAuthenticatedDocumentLoader, t as kvCache } from "../mod-Co7LEK9X.cjs";
2
+ import { n as getAuthenticatedDocumentLoader, t as kvCache } from "../mod-UvIGUHqW.cjs";
3
3
  export { getAuthenticatedDocumentLoader, kvCache };
@@ -1,3 +1,3 @@
1
1
  /// <reference lib="esnext.temporal" />
2
- import { n as getAuthenticatedDocumentLoader, t as kvCache } from "../mod-BRnwh2oy.js";
2
+ import { n as getAuthenticatedDocumentLoader, t as kvCache } from "../mod-REfljt0T.js";
3
3
  export { getAuthenticatedDocumentLoader, kvCache };
package/dist/utils/mod.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
2
  import { URLPattern } from "urlpattern-polyfill";
3
- import { n as getAuthenticatedDocumentLoader, t as kvCache } from "../kv-cache-CSKXXleR.js";
3
+ import { n as getAuthenticatedDocumentLoader, t as kvCache } from "../kv-cache-CvjLUQTf.js";
4
4
  export { getAuthenticatedDocumentLoader, kvCache };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/fedify",
3
- "version": "2.2.20",
3
+ "version": "2.2.21",
4
4
  "description": "An ActivityPub server framework",
5
5
  "keywords": [
6
6
  "ActivityPub",
@@ -153,9 +153,9 @@
153
153
  "uri-template-router": "^1.0.0",
154
154
  "url-template": "^3.1.1",
155
155
  "urlpattern-polyfill": "^10.1.0",
156
- "@fedify/vocab": "2.2.20",
157
- "@fedify/webfinger": "2.2.20",
158
- "@fedify/vocab-runtime": "2.2.20"
156
+ "@fedify/vocab-runtime": "2.2.21",
157
+ "@fedify/webfinger": "2.2.21",
158
+ "@fedify/vocab": "2.2.21"
159
159
  },
160
160
  "devDependencies": {
161
161
  "@std/assert": "npm:@jsr/std__assert@^0.226.0",
@@ -168,7 +168,7 @@
168
168
  "typescript": "^6.0.0",
169
169
  "wrangler": "^4.17.0",
170
170
  "@fedify/fixture": "2.0.0",
171
- "@fedify/vocab-tools": "^2.2.20"
171
+ "@fedify/vocab-tools": "^2.2.21"
172
172
  },
173
173
  "scripts": {
174
174
  "build:self": "tsdown",
File without changes
File without changes