@fedify/relay 2.4.0-dev.1508 → 2.4.0-dev.1531

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.
@@ -1,7 +1,7 @@
1
1
  import "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
- import { n as exportSpki, r as getDocumentLoader, t as isRelayFollowerData } from "./types-_nsodelz.js";
4
+ import { n as exportSpki, r as getDocumentLoader, t as isRelayFollowerData } from "./types-BSpAepbI.js";
5
5
  import { MemoryKvStore, signRequest } from "@fedify/fedify";
6
6
  import { createRelay } from "@fedify/relay";
7
7
  import { Accept, Announce, Create, Delete, Follow, Move, Note, Person, Undo, Update } from "@fedify/vocab";
@@ -1,7 +1,7 @@
1
1
  import "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
- import { n as exportSpki, r as getDocumentLoader, t as isRelayFollowerData } from "./types-_nsodelz.js";
4
+ import { n as exportSpki, r as getDocumentLoader, t as isRelayFollowerData } from "./types-BSpAepbI.js";
5
5
  import { MemoryKvStore, signRequest } from "@fedify/fedify";
6
6
  import { createRelay } from "@fedify/relay";
7
7
  import { Create, Delete, Follow, Move, Note, Person, Undo, Update } from "@fedify/vocab";
package/dist/mod.d.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  /// <reference lib="esnext.temporal" />
2
2
  import { Context, KvStore, MessageQueue } from "@fedify/fedify";
3
3
  import { Actor } from "@fedify/vocab";
4
- //#region ../vocab-runtime/dist/mod.d.ts
4
+ //#region ../vocab-runtime/dist/docloader-xRGn1azD.d.ts
5
5
  /**
6
6
  * Options for making `User-Agent` string.
7
7
  * @see {@link getUserAgent}
package/dist/mod.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /// <reference lib="esnext.temporal" />
2
2
  import { Context, KvStore, MessageQueue } from "@fedify/fedify";
3
3
  import { Actor } from "@fedify/vocab";
4
- //#region ../vocab-runtime/dist/mod.d.ts
4
+ //#region ../vocab-runtime/dist/docloader-xRGn1azD.d.ts
5
5
  /**
6
6
  * Options for making `User-Agent` string.
7
7
  * @see {@link getUserAgent}
@@ -2,10 +2,10 @@ import "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
4
  import { createRequire } from "node:module";
5
- import { getLogger } from "@logtape/logtape";
6
- import process$1 from "node:process";
7
5
  import { lookup } from "node:dns/promises";
8
6
  import { isIP } from "node:net";
7
+ import { getLogger } from "@logtape/logtape";
8
+ import process$1 from "node:process";
9
9
  //#region \0rolldown/runtime.js
10
10
  var __create = Object.create;
11
11
  var __defProp = Object.defineProperty;
@@ -30,6 +30,179 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
30
  }) : target, mod));
31
31
  var __require = /* @__PURE__ */ createRequire(import.meta.url);
32
32
  //#endregion
33
+ //#region ../vocab-runtime/dist/url-BuxPHxK2.js
34
+ var UrlError = class extends Error {
35
+ constructor(message) {
36
+ super(message);
37
+ this.name = "UrlError";
38
+ }
39
+ };
40
+ /**
41
+ * Validates a URL to prevent SSRF attacks.
42
+ */
43
+ async function validatePublicUrl(url) {
44
+ const parsed = new URL(url);
45
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") throw new UrlError(`Unsupported protocol: ${parsed.protocol}`);
46
+ let hostname = parsed.hostname;
47
+ if (hostname.startsWith("[") && hostname.endsWith("]")) hostname = hostname.slice(1, -1);
48
+ if (hostname === "localhost") throw new UrlError("Localhost is not allowed");
49
+ const hostnameFamily = isIP(hostname);
50
+ if (hostnameFamily !== 0) {
51
+ validatePublicIpAddress(hostname, hostnameFamily);
52
+ return;
53
+ }
54
+ if ("Deno" in globalThis && !isIP(hostname)) {
55
+ if ((await Deno.permissions.query({ name: "net" })).state !== "granted") return;
56
+ }
57
+ if ("Bun" in globalThis) {
58
+ if (hostname === "example.com" || hostname.endsWith(".example.com")) return;
59
+ else if (hostname === "fedify-test.internal") throw new UrlError("Invalid or private address: fedify-test.internal");
60
+ }
61
+ let addresses;
62
+ try {
63
+ addresses = await lookup(hostname, { all: true });
64
+ } catch {
65
+ addresses = [];
66
+ }
67
+ for (const { address, family } of addresses) validatePublicIpAddress(address, family);
68
+ }
69
+ function validatePublicIpAddress(address, family) {
70
+ if (family === 4 && isValidPublicIPv4Address(address) || family === 6 && isValidPublicIPv6Address(address)) return;
71
+ throw new UrlError(`Invalid or private address: ${address}`);
72
+ }
73
+ function isValidPublicIPv4Address(address) {
74
+ const parts = parseIPv4Address(address);
75
+ if (parts == null) return false;
76
+ const value = ipv4PartsToNumber(parts);
77
+ return !nonPublicIPv4Prefixes.some(({ base, prefix }) => matchesIPv4Prefix(value, base, prefix));
78
+ }
79
+ function isValidPublicIPv6Address(address) {
80
+ const words = parseIPv6Address(address);
81
+ if (words == null) return false;
82
+ if (nonPublicIPv6Prefixes.some(({ words: prefixWords, prefix }) => matchesIPv6Prefix(words, prefixWords, prefix))) return false;
83
+ for (const { extractIPv4, prefix, words: prefixWords } of ipv6WithIPv4Prefixes) {
84
+ if (!matchesIPv6Prefix(words, prefixWords, prefix)) continue;
85
+ const ipv4Address = extractIPv4(words);
86
+ if (ipv4Address != null && !isValidPublicIPv4Address(ipv4Address)) return false;
87
+ }
88
+ return true;
89
+ }
90
+ function expandIPv6Address(address) {
91
+ address = address.toLowerCase();
92
+ const ipv4Delimiter = address.lastIndexOf(":");
93
+ if (address.includes(".") && ipv4Delimiter >= 0) {
94
+ const ipv4Parts = parseIPv4Address(address.substring(ipv4Delimiter + 1));
95
+ if (ipv4Parts == null) return address;
96
+ const high = (ipv4Parts[0] << 8) + ipv4Parts[1];
97
+ const low = (ipv4Parts[2] << 8) + ipv4Parts[3];
98
+ address = address.substring(0, ipv4Delimiter + 1) + high.toString(16) + ":" + low.toString(16);
99
+ }
100
+ if (address === "::") return "0000:0000:0000:0000:0000:0000:0000:0000";
101
+ if (address.startsWith("::")) address = "0000" + address;
102
+ if (address.endsWith("::")) address = address + "0000";
103
+ address = address.replace("::", ":0000".repeat(8 - (address.match(/:/g) || []).length) + ":");
104
+ return address.split(":").map((part) => part.padStart(4, "0")).join(":");
105
+ }
106
+ const nonPublicIPv4Prefixes = [
107
+ ipv4Prefix("0.0.0.0/8", "RFC 6890"),
108
+ ipv4Prefix("10.0.0.0/8", "RFC 1918"),
109
+ ipv4Prefix("100.64.0.0/10", "RFC 6598"),
110
+ ipv4Prefix("127.0.0.0/8", "RFC 1122"),
111
+ ipv4Prefix("169.254.0.0/16", "RFC 3927"),
112
+ ipv4Prefix("172.16.0.0/12", "RFC 1918"),
113
+ ipv4Prefix("192.0.0.0/24", "RFC 6890"),
114
+ ipv4Prefix("192.0.2.0/24", "RFC 5737"),
115
+ ipv4Prefix("192.88.99.0/24", "RFC 7526"),
116
+ ipv4Prefix("192.168.0.0/16", "RFC 1918"),
117
+ ipv4Prefix("198.18.0.0/15", "RFC 2544"),
118
+ ipv4Prefix("198.51.100.0/24", "RFC 5737"),
119
+ ipv4Prefix("203.0.113.0/24", "RFC 5737"),
120
+ ipv4Prefix("224.0.0.0/4", "RFC 5771"),
121
+ ipv4Prefix("240.0.0.0/4", "RFC 1112")
122
+ ];
123
+ const nonPublicIPv6Prefixes = [
124
+ ipv6Prefix("::/16", "RFC 4291"),
125
+ ipv6Prefix("2001::/32", "RFC 4380"),
126
+ ipv6Prefix("2002::/16", "RFC 3056"),
127
+ ipv6Prefix("64:ff9b:1::/48", "RFC 8215"),
128
+ ipv6Prefix("fc00::/7", "RFC 4193"),
129
+ ipv6Prefix("fe80::/10", "RFC 4291"),
130
+ ipv6Prefix("ff00::/8", "RFC 4291")
131
+ ];
132
+ const ipv6WithIPv4Prefixes = [{
133
+ ...ipv6Prefix("64:ff9b::/96", "RFC 6052"),
134
+ extractIPv4: (words) => ipv4FromWords(words[6], words[7])
135
+ }];
136
+ function ipv4Prefix(cidr, rfc) {
137
+ const [address, prefixText] = cidr.split("/");
138
+ const prefix = parseInt(prefixText, 10);
139
+ const parts = parseIPv4Address(address);
140
+ if (parts == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 32) throw new Error(`Invalid IPv4 prefix: ${cidr}`);
141
+ return {
142
+ cidr,
143
+ base: ipv4PartsToNumber(parts),
144
+ prefix,
145
+ rfc
146
+ };
147
+ }
148
+ function ipv6Prefix(cidr, rfc) {
149
+ const [address, prefixText] = cidr.split("/");
150
+ const prefix = parseInt(prefixText, 10);
151
+ const words = parseIPv6Address(address);
152
+ if (words == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 128) throw new Error(`Invalid IPv6 prefix: ${cidr}`);
153
+ return {
154
+ cidr,
155
+ words,
156
+ prefix,
157
+ rfc
158
+ };
159
+ }
160
+ function parseIPv4Address(address) {
161
+ const parts = address.split(".").map((part) => {
162
+ if (!/^\d+$/.test(part)) return NaN;
163
+ return parseInt(part, 10);
164
+ });
165
+ if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) return null;
166
+ return parts;
167
+ }
168
+ function parseIPv6Address(address) {
169
+ const parts = expandIPv6Address(address).split(":");
170
+ if (parts.length !== 8) return null;
171
+ const words = parts.map((part) => {
172
+ if (!/^[0-9a-f]{1,4}$/i.test(part)) return NaN;
173
+ return parseInt(part, 16);
174
+ });
175
+ if (words.some((word) => !Number.isInteger(word) || word < 0 || word > 65535)) return null;
176
+ return words;
177
+ }
178
+ function ipv4PartsToNumber(parts) {
179
+ return parts[0] * 2 ** 24 + parts[1] * 2 ** 16 + parts[2] * 2 ** 8 + parts[3];
180
+ }
181
+ function ipv4FromWords(highWord, lowWord) {
182
+ return [
183
+ highWord >> 8,
184
+ highWord & 255,
185
+ lowWord >> 8,
186
+ lowWord & 255
187
+ ].join(".");
188
+ }
189
+ function matchesIPv4Prefix(address, prefixBase, prefixLength) {
190
+ const blockSize = 2 ** (32 - prefixLength);
191
+ return Math.floor(address / blockSize) === Math.floor(prefixBase / blockSize);
192
+ }
193
+ function matchesIPv6Prefix(address, prefixWords, prefixLength) {
194
+ let remaining = prefixLength;
195
+ for (let i = 0; i < 8 && remaining > 0; i++) if (remaining >= 16) {
196
+ if (address[i] !== prefixWords[i]) return false;
197
+ remaining -= 16;
198
+ } else {
199
+ const mask = 65535 << 16 - remaining & 65535;
200
+ if ((address[i] & mask) !== (prefixWords[i] & mask)) return false;
201
+ remaining = 0;
202
+ }
203
+ return true;
204
+ }
205
+ //#endregion
33
206
  //#region ../../node_modules/.pnpm/@opentelemetry+api@1.9.1/node_modules/@opentelemetry/api/build/esm/version.js
34
207
  const VERSION = "1.9.1";
35
208
  //#endregion
@@ -25931,7 +26104,7 @@ const preloadedContexts = {
25931
26104
  } }
25932
26105
  };
25933
26106
  var name = "@fedify/vocab-runtime";
25934
- var version = "2.4.0-dev.1508+68519bf4";
26107
+ var version = "2.4.0-dev.1531+0896bc51";
25935
26108
  const parametersNeedLowerCase = ["rel", "type"];
25936
26109
  const regexpLinkWhitespace = /[\n\r\s\t]/;
25937
26110
  function validateURI(uri) {
@@ -26179,183 +26352,72 @@ function logRequest(logger, request) {
26179
26352
  headers: Object.fromEntries(request.headers.entries())
26180
26353
  });
26181
26354
  }
26182
- var UrlError = class extends Error {
26183
- constructor(message) {
26184
- super(message);
26185
- this.name = "UrlError";
26186
- }
26187
- };
26188
- /**
26189
- * Validates a URL to prevent SSRF attacks.
26190
- */
26191
- async function validatePublicUrl(url) {
26192
- const parsed = new URL(url);
26193
- if (parsed.protocol !== "http:" && parsed.protocol !== "https:") throw new UrlError(`Unsupported protocol: ${parsed.protocol}`);
26194
- let hostname = parsed.hostname;
26195
- if (hostname.startsWith("[") && hostname.endsWith("]")) hostname = hostname.slice(1, -1);
26196
- if (hostname === "localhost") throw new UrlError("Localhost is not allowed");
26197
- const hostnameFamily = isIP(hostname);
26198
- if (hostnameFamily !== 0) {
26199
- validatePublicIpAddress(hostname, hostnameFamily);
26200
- return;
26201
- }
26202
- if ("Deno" in globalThis && !isIP(hostname)) {
26203
- if ((await Deno.permissions.query({ name: "net" })).state !== "granted") return;
26204
- }
26205
- if ("Bun" in globalThis) {
26206
- if (hostname === "example.com" || hostname.endsWith(".example.com")) return;
26207
- else if (hostname === "fedify-test.internal") throw new UrlError("Invalid or private address: fedify-test.internal");
26208
- }
26209
- let addresses;
26210
- try {
26211
- addresses = await lookup(hostname, { all: true });
26212
- } catch {
26213
- addresses = [];
26214
- }
26215
- for (const { address, family } of addresses) validatePublicIpAddress(address, family);
26216
- }
26217
- function validatePublicIpAddress(address, family) {
26218
- if (family === 4 && isValidPublicIPv4Address(address) || family === 6 && isValidPublicIPv6Address(address)) return;
26219
- throw new UrlError(`Invalid or private address: ${address}`);
26220
- }
26221
- function isValidPublicIPv4Address(address) {
26222
- const parts = parseIPv4Address(address);
26223
- if (parts == null) return false;
26224
- const value = ipv4PartsToNumber(parts);
26225
- return !nonPublicIPv4Prefixes.some(({ base, prefix }) => matchesIPv4Prefix(value, base, prefix));
26226
- }
26227
- function isValidPublicIPv6Address(address) {
26228
- const words = parseIPv6Address(address);
26229
- if (words == null) return false;
26230
- if (nonPublicIPv6Prefixes.some(({ words: prefixWords, prefix }) => matchesIPv6Prefix(words, prefixWords, prefix))) return false;
26231
- for (const { extractIPv4, prefix, words: prefixWords } of ipv6WithIPv4Prefixes) {
26232
- if (!matchesIPv6Prefix(words, prefixWords, prefix)) continue;
26233
- const ipv4Address = extractIPv4(words);
26234
- if (ipv4Address != null && !isValidPublicIPv4Address(ipv4Address)) return false;
26235
- }
26236
- return true;
26237
- }
26238
- function expandIPv6Address(address) {
26239
- address = address.toLowerCase();
26240
- const ipv4Delimiter = address.lastIndexOf(":");
26241
- if (address.includes(".") && ipv4Delimiter >= 0) {
26242
- const ipv4Parts = parseIPv4Address(address.substring(ipv4Delimiter + 1));
26243
- if (ipv4Parts == null) return address;
26244
- const high = (ipv4Parts[0] << 8) + ipv4Parts[1];
26245
- const low = (ipv4Parts[2] << 8) + ipv4Parts[3];
26246
- address = address.substring(0, ipv4Delimiter + 1) + high.toString(16) + ":" + low.toString(16);
26247
- }
26248
- if (address === "::") return "0000:0000:0000:0000:0000:0000:0000:0000";
26249
- if (address.startsWith("::")) address = "0000" + address;
26250
- if (address.endsWith("::")) address = address + "0000";
26251
- address = address.replace("::", ":0000".repeat(8 - (address.match(/:/g) || []).length) + ":");
26252
- return address.split(":").map((part) => part.padStart(4, "0")).join(":");
26253
- }
26254
- const nonPublicIPv4Prefixes = [
26255
- ipv4Prefix("0.0.0.0/8", "RFC 6890"),
26256
- ipv4Prefix("10.0.0.0/8", "RFC 1918"),
26257
- ipv4Prefix("100.64.0.0/10", "RFC 6598"),
26258
- ipv4Prefix("127.0.0.0/8", "RFC 1122"),
26259
- ipv4Prefix("169.254.0.0/16", "RFC 3927"),
26260
- ipv4Prefix("172.16.0.0/12", "RFC 1918"),
26261
- ipv4Prefix("192.0.0.0/24", "RFC 6890"),
26262
- ipv4Prefix("192.0.2.0/24", "RFC 5737"),
26263
- ipv4Prefix("192.88.99.0/24", "RFC 7526"),
26264
- ipv4Prefix("192.168.0.0/16", "RFC 1918"),
26265
- ipv4Prefix("198.18.0.0/15", "RFC 2544"),
26266
- ipv4Prefix("198.51.100.0/24", "RFC 5737"),
26267
- ipv4Prefix("203.0.113.0/24", "RFC 5737"),
26268
- ipv4Prefix("224.0.0.0/4", "RFC 5771"),
26269
- ipv4Prefix("240.0.0.0/4", "RFC 1112")
26270
- ];
26271
- const nonPublicIPv6Prefixes = [
26272
- ipv6Prefix("::/16", "RFC 4291"),
26273
- ipv6Prefix("2001::/32", "RFC 4380"),
26274
- ipv6Prefix("2002::/16", "RFC 3056"),
26275
- ipv6Prefix("64:ff9b:1::/48", "RFC 8215"),
26276
- ipv6Prefix("fc00::/7", "RFC 4193"),
26277
- ipv6Prefix("fe80::/10", "RFC 4291"),
26278
- ipv6Prefix("ff00::/8", "RFC 4291")
26279
- ];
26280
- const ipv6WithIPv4Prefixes = [{
26281
- ...ipv6Prefix("64:ff9b::/96", "RFC 6052"),
26282
- extractIPv4: (words) => ipv4FromWords(words[6], words[7])
26283
- }];
26284
- function ipv4Prefix(cidr, rfc) {
26285
- const [address, prefixText] = cidr.split("/");
26286
- const prefix = parseInt(prefixText, 10);
26287
- const parts = parseIPv4Address(address);
26288
- if (parts == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 32) throw new Error(`Invalid IPv4 prefix: ${cidr}`);
26289
- return {
26290
- cidr,
26291
- base: ipv4PartsToNumber(parts),
26292
- prefix,
26293
- rfc
26294
- };
26295
- }
26296
- function ipv6Prefix(cidr, rfc) {
26297
- const [address, prefixText] = cidr.split("/");
26298
- const prefix = parseInt(prefixText, 10);
26299
- const words = parseIPv6Address(address);
26300
- if (words == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 128) throw new Error(`Invalid IPv6 prefix: ${cidr}`);
26301
- return {
26302
- cidr,
26303
- words,
26304
- prefix,
26305
- rfc
26306
- };
26307
- }
26308
- function parseIPv4Address(address) {
26309
- const parts = address.split(".").map((part) => {
26310
- if (!/^\d+$/.test(part)) return NaN;
26311
- return parseInt(part, 10);
26312
- });
26313
- if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) return null;
26314
- return parts;
26315
- }
26316
- function parseIPv6Address(address) {
26317
- const parts = expandIPv6Address(address).split(":");
26318
- if (parts.length !== 8) return null;
26319
- const words = parts.map((part) => {
26320
- if (!/^[0-9a-f]{1,4}$/i.test(part)) return NaN;
26321
- return parseInt(part, 16);
26322
- });
26323
- if (words.some((word) => !Number.isInteger(word) || word < 0 || word > 65535)) return null;
26324
- return words;
26325
- }
26326
- function ipv4PartsToNumber(parts) {
26327
- return parts[0] * 2 ** 24 + parts[1] * 2 ** 16 + parts[2] * 2 ** 8 + parts[3];
26328
- }
26329
- function ipv4FromWords(highWord, lowWord) {
26330
- return [
26331
- highWord >> 8,
26332
- highWord & 255,
26333
- lowWord >> 8,
26334
- lowWord & 255
26335
- ].join(".");
26336
- }
26337
- function matchesIPv4Prefix(address, prefixBase, prefixLength) {
26338
- const blockSize = 2 ** (32 - prefixLength);
26339
- return Math.floor(address / blockSize) === Math.floor(prefixBase / blockSize);
26340
- }
26341
- function matchesIPv6Prefix(address, prefixWords, prefixLength) {
26342
- let remaining = prefixLength;
26343
- for (let i = 0; i < 8 && remaining > 0; i++) if (remaining >= 16) {
26344
- if (address[i] !== prefixWords[i]) return false;
26345
- remaining -= 16;
26346
- } else {
26347
- const mask = 65535 << 16 - remaining & 65535;
26348
- if ((address[i] & mask) !== (prefixWords[i] & mask)) return false;
26349
- remaining = 0;
26350
- }
26351
- return true;
26352
- }
26353
26355
  const logger = getLogger([
26354
26356
  "fedify",
26355
26357
  "runtime",
26356
26358
  "docloader"
26357
26359
  ]);
26358
26360
  const DEFAULT_MAX_REDIRECTION = 20;
26361
+ const MAX_HTML_SIZE = 1024 * 1024;
26362
+ function createResponseMetadata(response) {
26363
+ return new Response(null, {
26364
+ headers: response.headers,
26365
+ status: response.status,
26366
+ statusText: response.statusText
26367
+ });
26368
+ }
26369
+ async function cancelResponseBody(response) {
26370
+ if (response.body != null) await response.body.cancel();
26371
+ }
26372
+ async function readBoundedText(response, maxBytes) {
26373
+ const contentLength = response.headers.get("Content-Length");
26374
+ if (contentLength != null) {
26375
+ const size = Number(contentLength);
26376
+ if (size > maxBytes) {
26377
+ await cancelResponseBody(response);
26378
+ return {
26379
+ text: "",
26380
+ size,
26381
+ tooLarge: true
26382
+ };
26383
+ }
26384
+ }
26385
+ if (response.body == null) return {
26386
+ text: "",
26387
+ size: 0,
26388
+ tooLarge: false
26389
+ };
26390
+ const reader = response.body.getReader();
26391
+ const decoder = new TextDecoder();
26392
+ let text = "";
26393
+ let size = 0;
26394
+ try {
26395
+ while (true) {
26396
+ const result = await reader.read();
26397
+ if (result.done) break;
26398
+ const chunkSize = result.value.byteLength;
26399
+ if (size + chunkSize > maxBytes) {
26400
+ size += chunkSize;
26401
+ await reader.cancel();
26402
+ return {
26403
+ text: "",
26404
+ size,
26405
+ tooLarge: true
26406
+ };
26407
+ }
26408
+ size += chunkSize;
26409
+ text += decoder.decode(result.value, { stream: true });
26410
+ }
26411
+ text += decoder.decode();
26412
+ return {
26413
+ text,
26414
+ size,
26415
+ tooLarge: false
26416
+ };
26417
+ } finally {
26418
+ reader.releaseLock();
26419
+ }
26420
+ }
26359
26421
  /**
26360
26422
  * Gets a {@link RemoteDocument} from the given response.
26361
26423
  * @param url The URL of the document to load.
@@ -26410,19 +26472,19 @@ async function getRemoteDocument(url, response, fetch) {
26410
26472
  }
26411
26473
  let document;
26412
26474
  if (!jsonLd && (contentType === "text/html" || contentType?.startsWith("text/html;") || contentType === "application/xhtml+xml" || contentType?.startsWith("application/xhtml+xml;"))) {
26413
- const MAX_HTML_SIZE = 1024 * 1024;
26414
- const html = await response.text();
26415
- if (html.length > MAX_HTML_SIZE) {
26475
+ const errorResponse = createResponseMetadata(response);
26476
+ const html = await readBoundedText(response, MAX_HTML_SIZE);
26477
+ if (html.tooLarge) {
26416
26478
  logger.warn("HTML response too large, skipping alternate link discovery: {url}", {
26417
26479
  url: documentUrl,
26418
- size: html.length
26480
+ size: html.size
26419
26481
  });
26420
- document = JSON.parse(html);
26482
+ throw new FetchError(documentUrl, `HTML document is too large to scan for an ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
26421
26483
  } else {
26422
26484
  const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
26423
26485
  const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
26424
26486
  let tagMatch;
26425
- while ((tagMatch = tagPattern.exec(html)) !== null) {
26487
+ while ((tagMatch = tagPattern.exec(html.text)) !== null) {
26426
26488
  const tagContent = tagMatch[2];
26427
26489
  let attrMatch;
26428
26490
  const attribs = {};
@@ -26439,7 +26501,12 @@ async function getRemoteDocument(url, response, fetch) {
26439
26501
  return await fetch(new URL(attribs.href, docUrl).href);
26440
26502
  }
26441
26503
  }
26442
- document = JSON.parse(html);
26504
+ try {
26505
+ document = JSON.parse(html.text);
26506
+ } catch (error) {
26507
+ if (!(error instanceof SyntaxError)) throw error;
26508
+ throw new FetchError(documentUrl, `HTML document has no ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
26509
+ }
26443
26510
  }
26444
26511
  } else document = await response.json();
26445
26512
  logger.debug("Fetched document: {status} {url} {headers}", {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/relay",
3
- "version": "2.4.0-dev.1508+68519bf4",
3
+ "version": "2.4.0-dev.1531+0896bc51",
4
4
  "description": "ActivityPub relay support for Fedify",
5
5
  "keywords": [
6
6
  "Fedify",
@@ -47,14 +47,14 @@
47
47
  "dependencies": {
48
48
  "@js-temporal/polyfill": "^0.5.1",
49
49
  "@logtape/logtape": "^2.2.0",
50
- "@fedify/fedify": "^2.4.0-dev.1508+68519bf4",
51
- "@fedify/vocab": "2.4.0-dev.1508+68519bf4"
50
+ "@fedify/fedify": "^2.4.0-dev.1531+0896bc51",
51
+ "@fedify/vocab": "2.4.0-dev.1531+0896bc51"
52
52
  },
53
53
  "devDependencies": {
54
54
  "tsdown": "^0.22.0",
55
55
  "typescript": "^6.0.0",
56
56
  "urlpattern-polyfill": "^10.1.0",
57
- "@fedify/vocab-runtime": "^2.4.0-dev.1508+68519bf4"
57
+ "@fedify/vocab-runtime": "^2.4.0-dev.1531+0896bc51"
58
58
  },
59
59
  "scripts": {
60
60
  "build:self": "tsdown",