@fedify/relay 2.2.11 → 2.2.13

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-PS54uoNv.js";
4
+ import { n as exportSpki, r as getDocumentLoader, t as isRelayFollowerData } from "./types-BFzM7PGw.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-PS54uoNv.js";
4
+ import { n as exportSpki, r as getDocumentLoader, t as isRelayFollowerData } from "./types-BFzM7PGw.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";
@@ -25712,7 +25712,147 @@ const preloadedContexts = {
25712
25712
  "https://purl.archive.org/miscellany": miscellany_default
25713
25713
  };
25714
25714
  var name = "@fedify/vocab-runtime";
25715
- var version = "2.2.11";
25715
+ var version = "2.2.13";
25716
+ /**
25717
+ * Error thrown when fetching a JSON-LD document failed.
25718
+ */
25719
+ var FetchError = class extends Error {
25720
+ /**
25721
+ * The URL that failed to fetch.
25722
+ */
25723
+ url;
25724
+ /**
25725
+ * The HTTP response that failed, if available.
25726
+ */
25727
+ response;
25728
+ /**
25729
+ * Constructs a new `FetchError`.
25730
+ *
25731
+ * @param url The URL that failed to fetch.
25732
+ * @param message Error message.
25733
+ * @param response The failed HTTP response, if available.
25734
+ */
25735
+ constructor(url, message, response) {
25736
+ super(message == null ? url.toString() : `${url}: ${message}`);
25737
+ this.name = "FetchError";
25738
+ this.url = typeof url === "string" ? new URL(url) : url;
25739
+ this.response = response;
25740
+ }
25741
+ };
25742
+ /**
25743
+ * Creates a request for the given URL.
25744
+ * @param url The URL to create the request for.
25745
+ * @param options The options for the request.
25746
+ * @returns The created request.
25747
+ * @internal
25748
+ */
25749
+ function createActivityPubRequest(url, options = {}) {
25750
+ return new Request(url, {
25751
+ headers: {
25752
+ Accept: "application/activity+json, application/ld+json",
25753
+ "User-Agent": typeof options.userAgent === "string" ? options.userAgent : getUserAgent(options.userAgent)
25754
+ },
25755
+ redirect: "manual"
25756
+ });
25757
+ }
25758
+ /**
25759
+ * Gets the user agent string for the given application and URL.
25760
+ * @param options The options for making the user agent string.
25761
+ * @returns The user agent string.
25762
+ * @since 1.3.0
25763
+ */
25764
+ function getUserAgent({ software, url } = {}) {
25765
+ const fedify = `Fedify/${version}`;
25766
+ const runtime = globalThis.Deno?.version?.deno != null ? `Deno/${Deno.version.deno}` : globalThis.process?.versions?.bun != null ? `Bun/${process$1.versions.bun}` : "navigator" in globalThis && navigator.userAgent === "Cloudflare-Workers" ? navigator.userAgent : globalThis.process?.versions?.node != null ? `Node.js/${process$1.versions.node}` : null;
25767
+ const userAgent = software == null ? [fedify] : [software, fedify];
25768
+ if (runtime != null) userAgent.push(runtime);
25769
+ if (url != null) userAgent.push(`+${url.toString()}`);
25770
+ return `${userAgent.shift()} (${userAgent.join("; ")})`;
25771
+ }
25772
+ /**
25773
+ * Logs the request.
25774
+ * @param request The request to log.
25775
+ * @internal
25776
+ */
25777
+ function logRequest(logger, request) {
25778
+ logger.debug("Fetching document: {method} {url} {headers}", {
25779
+ method: request.method,
25780
+ url: request.url,
25781
+ headers: Object.fromEntries(request.headers.entries())
25782
+ });
25783
+ }
25784
+ /** Decoded JSON body limit: 16 MiB. @internal */
25785
+ const MAX_BODY_SIZE = 16 * 1024 * 1024;
25786
+ /** A body exceeded the byte limit. @internal */
25787
+ var BodyTooLargeError = class extends FetchError {
25788
+ /** Creates an error for a body exceeding the given limit. */
25789
+ constructor(url, maxBytes) {
25790
+ super(url, `Body exceeds the limit of ${maxBytes} bytes`);
25791
+ this.name = "BodyTooLargeError";
25792
+ }
25793
+ };
25794
+ /** Validates a finite, positive byte limit. @internal */
25795
+ function validateBodySizeLimit(maxBytes) {
25796
+ if (!Number.isSafeInteger(maxBytes) || maxBytes <= 0) throw new RangeError("The body size limit must be a positive safe integer.");
25797
+ }
25798
+ /**
25799
+ * Reads UTF-8 text while limiting bytes received from the body stream.
25800
+ * Fetch responses are already decompressed, so the stream count is authoritative.
25801
+ * @param message The response or request to read.
25802
+ * @param maxBytes The maximum number of decoded bytes to read.
25803
+ * @param url The URL to include in errors and logs.
25804
+ * @returns The decoded body text.
25805
+ * @throws {BodyTooLargeError} If the body exceeds the limit.
25806
+ * @internal
25807
+ */
25808
+ async function readBoundedText(message, maxBytes, url) {
25809
+ validateBodySizeLimit(maxBytes);
25810
+ const reader = message.body?.getReader();
25811
+ const tooLarge = () => {
25812
+ getLogger([
25813
+ "fedify",
25814
+ "runtime",
25815
+ "body"
25816
+ ]).warn("Body from {url} exceeds the limit of {maxBytes} bytes.", {
25817
+ url: url.toString(),
25818
+ maxBytes
25819
+ });
25820
+ throw new BodyTooLargeError(url, maxBytes);
25821
+ };
25822
+ try {
25823
+ const length = message.headers.get("Content-Length");
25824
+ const encoding = message.headers.get("Content-Encoding");
25825
+ if ((encoding == null || encoding.toLowerCase() === "identity") && length != null && /^\d+$/.test(length) && Number(length) > maxBytes) tooLarge();
25826
+ if (reader == null) return "";
25827
+ let size = 0;
25828
+ const textReader = new ReadableStream({ async pull(controller) {
25829
+ const { done, value } = await reader.read();
25830
+ if (done) {
25831
+ controller.close();
25832
+ return;
25833
+ }
25834
+ size += value.byteLength;
25835
+ if (size > maxBytes) tooLarge();
25836
+ controller.enqueue(value);
25837
+ } }, { highWaterMark: 0 }).pipeThrough(new TextDecoderStream()).getReader();
25838
+ try {
25839
+ const chunks = [];
25840
+ while (true) {
25841
+ const { done, value } = await textReader.read();
25842
+ if (done) break;
25843
+ chunks.push(value);
25844
+ }
25845
+ return chunks.join("");
25846
+ } finally {
25847
+ textReader.releaseLock();
25848
+ }
25849
+ } catch (error) {
25850
+ if (reader != null) reader.cancel(error).catch(() => {});
25851
+ throw error;
25852
+ } finally {
25853
+ reader?.releaseLock();
25854
+ }
25855
+ }
25716
25856
  const parametersNeedLowerCase = ["rel", "type"];
25717
25857
  const regexpLinkWhitespace = /[\n\r\s\t]/;
25718
25858
  function validateURI(uri) {
@@ -25892,74 +26032,6 @@ var HttpHeaderLink = class HttpHeaderLink {
25892
26032
  return new this(...inputs).toString();
25893
26033
  }
25894
26034
  };
25895
- /**
25896
- * Error thrown when fetching a JSON-LD document failed.
25897
- */
25898
- var FetchError = class extends Error {
25899
- /**
25900
- * The URL that failed to fetch.
25901
- */
25902
- url;
25903
- /**
25904
- * The HTTP response that failed, if available.
25905
- */
25906
- response;
25907
- /**
25908
- * Constructs a new `FetchError`.
25909
- *
25910
- * @param url The URL that failed to fetch.
25911
- * @param message Error message.
25912
- * @param response The failed HTTP response, if available.
25913
- */
25914
- constructor(url, message, response) {
25915
- super(message == null ? url.toString() : `${url}: ${message}`);
25916
- this.name = "FetchError";
25917
- this.url = typeof url === "string" ? new URL(url) : url;
25918
- this.response = response;
25919
- }
25920
- };
25921
- /**
25922
- * Creates a request for the given URL.
25923
- * @param url The URL to create the request for.
25924
- * @param options The options for the request.
25925
- * @returns The created request.
25926
- * @internal
25927
- */
25928
- function createActivityPubRequest(url, options = {}) {
25929
- return new Request(url, {
25930
- headers: {
25931
- Accept: "application/activity+json, application/ld+json",
25932
- "User-Agent": typeof options.userAgent === "string" ? options.userAgent : getUserAgent(options.userAgent)
25933
- },
25934
- redirect: "manual"
25935
- });
25936
- }
25937
- /**
25938
- * Gets the user agent string for the given application and URL.
25939
- * @param options The options for making the user agent string.
25940
- * @returns The user agent string.
25941
- * @since 1.3.0
25942
- */
25943
- function getUserAgent({ software, url } = {}) {
25944
- const fedify = `Fedify/${version}`;
25945
- const runtime = globalThis.Deno?.version?.deno != null ? `Deno/${Deno.version.deno}` : globalThis.process?.versions?.bun != null ? `Bun/${process$1.versions.bun}` : "navigator" in globalThis && navigator.userAgent === "Cloudflare-Workers" ? navigator.userAgent : globalThis.process?.versions?.node != null ? `Node.js/${process$1.versions.node}` : null;
25946
- const userAgent = software == null ? [fedify] : [software, fedify];
25947
- if (runtime != null) userAgent.push(runtime);
25948
- if (url != null) userAgent.push(`+${url.toString()}`);
25949
- return `${userAgent.shift()} (${userAgent.join("; ")})`;
25950
- }
25951
- /**
25952
- * Logs the request.
25953
- * @param request The request to log.
25954
- * @internal
25955
- */
25956
- function logRequest(logger, request) {
25957
- logger.debug("Fetching document: {method} {url} {headers}", {
25958
- method: request.method,
25959
- url: request.url,
25960
- headers: Object.fromEntries(request.headers.entries())
25961
- });
25962
- }
25963
26035
  var UrlError = class extends Error {
25964
26036
  constructor(message, options) {
25965
26037
  super(message, options);
@@ -26218,38 +26290,29 @@ async function getRemoteDocument(url, response, fetch) {
26218
26290
  }
26219
26291
  let document;
26220
26292
  if (!jsonLd && (contentType === "text/html" || contentType?.startsWith("text/html;") || contentType === "application/xhtml+xml" || contentType?.startsWith("application/xhtml+xml;"))) {
26221
- const MAX_HTML_SIZE = 1024 * 1024;
26222
- const html = await response.text();
26223
- if (html.length > MAX_HTML_SIZE) {
26224
- logger.warn("HTML response too large, skipping alternate link discovery: {url}", {
26225
- url: documentUrl,
26226
- size: html.length
26227
- });
26228
- document = JSON.parse(html);
26229
- } else {
26230
- const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
26231
- const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
26232
- let tagMatch;
26233
- while ((tagMatch = tagPattern.exec(html)) !== null) {
26234
- const tagContent = tagMatch[2];
26235
- let attrMatch;
26236
- const attribs = {};
26237
- attrPattern.lastIndex = 0;
26238
- while ((attrMatch = attrPattern.exec(tagContent)) !== null) {
26239
- const key = attrMatch[1].toLowerCase();
26240
- attribs[key] = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4] ?? "";
26241
- }
26242
- if (attribs.rel === "alternate" && "type" in attribs && (attribs.type === "application/activity+json" || attribs.type === "application/ld+json" || attribs.type.startsWith("application/ld+json;")) && "href" in attribs && new URL(attribs.href, docUrl).href !== docUrl.href) {
26243
- logger.debug("Found alternate document: {alternateUrl} from {url}", {
26244
- alternateUrl: attribs.href,
26245
- url: documentUrl
26246
- });
26247
- return await fetch(new URL(attribs.href, docUrl).href);
26248
- }
26293
+ const html = await readBoundedText(response, 1024 * 1024, documentUrl);
26294
+ const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
26295
+ const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
26296
+ let tagMatch;
26297
+ while ((tagMatch = tagPattern.exec(html)) !== null) {
26298
+ const tagContent = tagMatch[2];
26299
+ let attrMatch;
26300
+ const attribs = {};
26301
+ attrPattern.lastIndex = 0;
26302
+ while ((attrMatch = attrPattern.exec(tagContent)) !== null) {
26303
+ const key = attrMatch[1].toLowerCase();
26304
+ attribs[key] = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4] ?? "";
26305
+ }
26306
+ if (attribs.rel === "alternate" && "type" in attribs && (attribs.type === "application/activity+json" || attribs.type === "application/ld+json" || attribs.type.startsWith("application/ld+json;")) && "href" in attribs && new URL(attribs.href, docUrl).href !== docUrl.href) {
26307
+ logger.debug("Found alternate document: {alternateUrl} from {url}", {
26308
+ alternateUrl: attribs.href,
26309
+ url: documentUrl
26310
+ });
26311
+ return await fetch(new URL(attribs.href, docUrl).href);
26249
26312
  }
26250
- document = JSON.parse(html);
26251
26313
  }
26252
- } else document = await response.json();
26314
+ document = JSON.parse(html);
26315
+ } else document = JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, documentUrl));
26253
26316
  logger.debug("Fetched document: {status} {url} {headers}", {
26254
26317
  status: response.status,
26255
26318
  url: documentUrl,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/relay",
3
- "version": "2.2.11",
3
+ "version": "2.2.13",
4
4
  "description": "ActivityPub relay support for Fedify",
5
5
  "keywords": [
6
6
  "Fedify",
@@ -50,14 +50,14 @@
50
50
  "dependencies": {
51
51
  "@js-temporal/polyfill": "^0.5.1",
52
52
  "@logtape/logtape": "^2.0.5",
53
- "@fedify/fedify": "^2.2.11",
54
- "@fedify/vocab": "2.2.11"
53
+ "@fedify/fedify": "^2.2.13",
54
+ "@fedify/vocab": "2.2.13"
55
55
  },
56
56
  "devDependencies": {
57
57
  "tsdown": "^0.21.6",
58
58
  "typescript": "^6.0.0",
59
59
  "urlpattern-polyfill": "^10.1.0",
60
- "@fedify/vocab-runtime": "^2.2.11"
60
+ "@fedify/vocab-runtime": "^2.2.13"
61
61
  },
62
62
  "scripts": {
63
63
  "build:self": "tsdown",