@fedify/relay 2.4.0-dev.1528 → 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-Dx-fpm5w.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-Dx-fpm5w.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";
@@ -26104,7 +26104,7 @@ const preloadedContexts = {
26104
26104
  } }
26105
26105
  };
26106
26106
  var name = "@fedify/vocab-runtime";
26107
- var version = "2.4.0-dev.1528+fea670ad";
26107
+ var version = "2.4.0-dev.1531+0896bc51";
26108
26108
  const parametersNeedLowerCase = ["rel", "type"];
26109
26109
  const regexpLinkWhitespace = /[\n\r\s\t]/;
26110
26110
  function validateURI(uri) {
@@ -26358,6 +26358,66 @@ const logger = getLogger([
26358
26358
  "docloader"
26359
26359
  ]);
26360
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
+ }
26361
26421
  /**
26362
26422
  * Gets a {@link RemoteDocument} from the given response.
26363
26423
  * @param url The URL of the document to load.
@@ -26412,19 +26472,19 @@ async function getRemoteDocument(url, response, fetch) {
26412
26472
  }
26413
26473
  let document;
26414
26474
  if (!jsonLd && (contentType === "text/html" || contentType?.startsWith("text/html;") || contentType === "application/xhtml+xml" || contentType?.startsWith("application/xhtml+xml;"))) {
26415
- const MAX_HTML_SIZE = 1024 * 1024;
26416
- const html = await response.text();
26417
- if (html.length > MAX_HTML_SIZE) {
26475
+ const errorResponse = createResponseMetadata(response);
26476
+ const html = await readBoundedText(response, MAX_HTML_SIZE);
26477
+ if (html.tooLarge) {
26418
26478
  logger.warn("HTML response too large, skipping alternate link discovery: {url}", {
26419
26479
  url: documentUrl,
26420
- size: html.length
26480
+ size: html.size
26421
26481
  });
26422
- 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);
26423
26483
  } else {
26424
26484
  const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
26425
26485
  const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
26426
26486
  let tagMatch;
26427
- while ((tagMatch = tagPattern.exec(html)) !== null) {
26487
+ while ((tagMatch = tagPattern.exec(html.text)) !== null) {
26428
26488
  const tagContent = tagMatch[2];
26429
26489
  let attrMatch;
26430
26490
  const attribs = {};
@@ -26441,7 +26501,12 @@ async function getRemoteDocument(url, response, fetch) {
26441
26501
  return await fetch(new URL(attribs.href, docUrl).href);
26442
26502
  }
26443
26503
  }
26444
- 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
+ }
26445
26510
  }
26446
26511
  } else document = await response.json();
26447
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.1528+fea670ad",
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/vocab": "2.4.0-dev.1528+fea670ad",
51
- "@fedify/fedify": "^2.4.0-dev.1528+fea670ad"
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.1528+fea670ad"
57
+ "@fedify/vocab-runtime": "^2.4.0-dev.1531+0896bc51"
58
58
  },
59
59
  "scripts": {
60
60
  "build:self": "tsdown",