@fedify/vocab-runtime 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.
Files changed (50) hide show
  1. package/deno.json +1 -1
  2. package/dist/mod.cjs +166 -101
  3. package/dist/mod.js +166 -101
  4. package/dist/tests/body-UDpZTF6c.mjs +77 -0
  5. package/dist/tests/body-Vx0QSf6C.cjs +95 -0
  6. package/dist/tests/body.test.cjs +90 -0
  7. package/dist/tests/body.test.d.cts +1 -0
  8. package/dist/tests/body.test.d.mts +1 -0
  9. package/dist/tests/body.test.mjs +90 -0
  10. package/dist/tests/decimal.test.cjs +6 -6
  11. package/dist/tests/decimal.test.mjs +6 -6
  12. package/dist/tests/{docloader-ekzTbc9R.cjs → docloader-B5V6GD-7.cjs} +31 -33
  13. package/dist/tests/{docloader-Nug7_hIl.mjs → docloader-Cld9tcx4.mjs} +26 -34
  14. package/dist/tests/docloader.test.cjs +83 -4
  15. package/dist/tests/docloader.test.mjs +83 -4
  16. package/dist/tests/internal/multicodec.test.cjs +2 -2
  17. package/dist/tests/internal/multicodec.test.mjs +2 -2
  18. package/dist/tests/{key-BeTHFQJK.mjs → key-BDIsC6hV.mjs} +2 -2
  19. package/dist/tests/{key-DTTIntwb.cjs → key-BarKGLQz.cjs} +2 -2
  20. package/dist/tests/key.test.cjs +3 -3
  21. package/dist/tests/key.test.mjs +3 -3
  22. package/dist/tests/langstr.test.cjs +2 -2
  23. package/dist/tests/langstr.test.mjs +2 -2
  24. package/dist/tests/link.test.cjs +2 -2
  25. package/dist/tests/link.test.mjs +2 -2
  26. package/dist/tests/multibase/multibase.test.cjs +2 -2
  27. package/dist/tests/multibase/multibase.test.mjs +2 -2
  28. package/dist/tests/{request-CMtb-99N.mjs → request-B2ac0drD.mjs} +1 -1
  29. package/dist/tests/{request-HcA2nRAs.cjs → request-DqvIx6-9.cjs} +1 -1
  30. package/dist/tests/request.test.cjs +2 -2
  31. package/dist/tests/request.test.mjs +2 -2
  32. package/dist/tests/temporal.test.cjs +1 -1
  33. package/dist/tests/temporal.test.mjs +1 -1
  34. package/dist/tests/url.test.cjs +2 -2
  35. package/dist/tests/url.test.mjs +2 -2
  36. package/package.json +1 -1
  37. package/src/body.test.ts +125 -0
  38. package/src/body.ts +95 -0
  39. package/src/docloader.test.ts +124 -1
  40. package/src/docloader.ts +41 -43
  41. /package/dist/tests/{langstr-CbAxaeEZ.cjs → langstr-BOQHfJDr.cjs} +0 -0
  42. /package/dist/tests/{langstr-Di5AvKpB.mjs → langstr-DDLc4833.mjs} +0 -0
  43. /package/dist/tests/{link-NUUWCdnK.mjs → link-CYszIegt.mjs} +0 -0
  44. /package/dist/tests/{link-FguCydMA.cjs → link-qDTmjPw_.cjs} +0 -0
  45. /package/dist/tests/{multibase-F7LtMMsK.cjs → multibase-8zhYPa7o.cjs} +0 -0
  46. /package/dist/tests/{multibase-BgU9XRf7.mjs → multibase-Ds75BvXs.mjs} +0 -0
  47. /package/dist/tests/{multicodec-CyFp54fI.mjs → multicodec-BBPZjRfg.mjs} +0 -0
  48. /package/dist/tests/{multicodec-CxGVGa91.cjs → multicodec-ByS9XLsw.cjs} +0 -0
  49. /package/dist/tests/{url-o0e-aXQY.mjs → url-BBfduh6o.mjs} +0 -0
  50. /package/dist/tests/{url-CWU5HT3Y.cjs → url-CnyF489g.cjs} +0 -0
@@ -0,0 +1,95 @@
1
+ require("./chunk-Do9eywBl.cjs");
2
+ const require_request = require("./request-DqvIx6-9.cjs");
3
+ let _logtape_logtape = require("@logtape/logtape");
4
+ //#region src/body.ts
5
+ /** Decoded JSON body limit: 16 MiB. @internal */
6
+ const MAX_BODY_SIZE = 16 * 1024 * 1024;
7
+ /** A body exceeded the byte limit. @internal */
8
+ var BodyTooLargeError = class extends require_request.FetchError {
9
+ /** Creates an error for a body exceeding the given limit. */
10
+ constructor(url, maxBytes) {
11
+ super(url, `Body exceeds the limit of ${maxBytes} bytes`);
12
+ this.name = "BodyTooLargeError";
13
+ }
14
+ };
15
+ /** Validates a finite, positive byte limit. @internal */
16
+ function validateBodySizeLimit(maxBytes) {
17
+ if (!Number.isSafeInteger(maxBytes) || maxBytes <= 0) throw new RangeError("The body size limit must be a positive safe integer.");
18
+ }
19
+ /**
20
+ * Reads UTF-8 text while limiting bytes received from the body stream.
21
+ * Fetch responses are already decompressed, so the stream count is authoritative.
22
+ * @param message The response or request to read.
23
+ * @param maxBytes The maximum number of decoded bytes to read.
24
+ * @param url The URL to include in errors and logs.
25
+ * @returns The decoded body text.
26
+ * @throws {BodyTooLargeError} If the body exceeds the limit.
27
+ * @internal
28
+ */
29
+ async function readBoundedText(message, maxBytes, url) {
30
+ validateBodySizeLimit(maxBytes);
31
+ const reader = message.body?.getReader();
32
+ const tooLarge = () => {
33
+ (0, _logtape_logtape.getLogger)([
34
+ "fedify",
35
+ "runtime",
36
+ "body"
37
+ ]).warn("Body from {url} exceeds the limit of {maxBytes} bytes.", {
38
+ url: url.toString(),
39
+ maxBytes
40
+ });
41
+ throw new BodyTooLargeError(url, maxBytes);
42
+ };
43
+ try {
44
+ const length = message.headers.get("Content-Length");
45
+ const encoding = message.headers.get("Content-Encoding");
46
+ if ((encoding == null || encoding.toLowerCase() === "identity") && length != null && /^\d+$/.test(length) && Number(length) > maxBytes) tooLarge();
47
+ if (reader == null) return "";
48
+ let size = 0;
49
+ const textReader = new ReadableStream({ async pull(controller) {
50
+ const { done, value } = await reader.read();
51
+ if (done) {
52
+ controller.close();
53
+ return;
54
+ }
55
+ size += value.byteLength;
56
+ if (size > maxBytes) tooLarge();
57
+ controller.enqueue(value);
58
+ } }, { highWaterMark: 0 }).pipeThrough(new TextDecoderStream()).getReader();
59
+ try {
60
+ const chunks = [];
61
+ while (true) {
62
+ const { done, value } = await textReader.read();
63
+ if (done) break;
64
+ chunks.push(value);
65
+ }
66
+ return chunks.join("");
67
+ } finally {
68
+ textReader.releaseLock();
69
+ }
70
+ } catch (error) {
71
+ if (reader != null) reader.cancel(error).catch(() => {});
72
+ throw error;
73
+ } finally {
74
+ reader?.releaseLock();
75
+ }
76
+ }
77
+ //#endregion
78
+ Object.defineProperty(exports, "BodyTooLargeError", {
79
+ enumerable: true,
80
+ get: function() {
81
+ return BodyTooLargeError;
82
+ }
83
+ });
84
+ Object.defineProperty(exports, "MAX_BODY_SIZE", {
85
+ enumerable: true,
86
+ get: function() {
87
+ return MAX_BODY_SIZE;
88
+ }
89
+ });
90
+ Object.defineProperty(exports, "readBoundedText", {
91
+ enumerable: true,
92
+ get: function() {
93
+ return readBoundedText;
94
+ }
95
+ });
@@ -0,0 +1,90 @@
1
+ require("./chunk-Do9eywBl.cjs");
2
+ const require_body = require("./body-Vx0QSf6C.cjs");
3
+ let node_assert_strict = require("node:assert/strict");
4
+ let node_test = require("node:test");
5
+ let node_zlib = require("node:zlib");
6
+ //#region src/body.test.ts
7
+ const url = "https://example.com/document";
8
+ (0, node_test.test)("readBoundedText handles empty bodies and flushes incomplete UTF-8", async () => {
9
+ (0, node_assert_strict.deepStrictEqual)(await require_body.readBoundedText(new Response(null), 1, url), "");
10
+ (0, node_assert_strict.deepStrictEqual)(await require_body.readBoundedText(new Response(""), 1, url), "");
11
+ (0, node_assert_strict.deepStrictEqual)(await require_body.readBoundedText(new Response(new Uint8Array([226, 130])), 2, url), "�");
12
+ });
13
+ (0, node_test.test)("readBoundedText counts bytes and preserves split UTF-8", async () => {
14
+ const bytes = new TextEncoder().encode("\"안녕\"");
15
+ const stream = new ReadableStream({ start(controller) {
16
+ for (const byte of bytes) controller.enqueue(new Uint8Array([byte]));
17
+ controller.close();
18
+ } });
19
+ (0, node_assert_strict.deepStrictEqual)(await require_body.readBoundedText(new Response(stream), bytes.length, url), "\"안녕\"");
20
+ await (0, node_assert_strict.rejects)(require_body.readBoundedText(new Response(bytes), bytes.length - 1, url), require_body.BodyTooLargeError);
21
+ });
22
+ (0, node_test.test)("readBoundedText rejects a declared oversized body without pulling", async () => {
23
+ let pulled = false;
24
+ let canceled = false;
25
+ const response = new Response(new ReadableStream({
26
+ pull() {
27
+ pulled = true;
28
+ },
29
+ cancel() {
30
+ canceled = true;
31
+ }
32
+ }, { highWaterMark: 0 }), { headers: { "Content-Length": "100" } });
33
+ await (0, node_assert_strict.rejects)(require_body.readBoundedText(response, 10, url), require_body.BodyTooLargeError);
34
+ (0, node_assert_strict.deepStrictEqual)(pulled, false);
35
+ (0, node_assert_strict.deepStrictEqual)(canceled, true);
36
+ (0, node_assert_strict.deepStrictEqual)(response.body?.locked, false);
37
+ });
38
+ (0, node_test.test)("readBoundedText cancels chunked bodies despite absent or false lengths", async () => {
39
+ for (const length of [
40
+ void 0,
41
+ "1",
42
+ "invalid"
43
+ ]) {
44
+ let pulls = 0;
45
+ let canceled = false;
46
+ const stream = new ReadableStream({
47
+ pull(controller) {
48
+ pulls++;
49
+ controller.enqueue(new Uint8Array(4));
50
+ },
51
+ cancel() {
52
+ canceled = true;
53
+ }
54
+ }, { highWaterMark: 0 });
55
+ const response = new Response(stream, { headers: length == null ? {} : { "Content-Length": length } });
56
+ await (0, node_assert_strict.rejects)(require_body.readBoundedText(response, 8, url), require_body.BodyTooLargeError);
57
+ (0, node_assert_strict.deepStrictEqual)(pulls, 3);
58
+ (0, node_assert_strict.deepStrictEqual)(canceled, true);
59
+ (0, node_assert_strict.deepStrictEqual)(response.body?.locked, false);
60
+ }
61
+ });
62
+ (0, node_test.test)("readBoundedText limits decoded gzip bytes, not the compressed length", async () => {
63
+ const compressed = (0, node_zlib.gzipSync)("\"" + "a".repeat(4096) + "\"");
64
+ (0, node_assert_strict.ok)(compressed.length < 128);
65
+ const decoded = (0, node_zlib.gunzipSync)(compressed);
66
+ await (0, node_assert_strict.rejects)(require_body.readBoundedText(new Response(decoded, { headers: {
67
+ "Content-Encoding": "gzip",
68
+ "Content-Length": String(compressed.length)
69
+ } }), 128, url), require_body.BodyTooLargeError);
70
+ (0, node_assert_strict.deepStrictEqual)(await require_body.readBoundedText(new Response("{}", { headers: {
71
+ "Content-Encoding": "gzip",
72
+ "Content-Length": "22"
73
+ } }), 2, url), "{}");
74
+ });
75
+ (0, node_test.test)("readBoundedText rejects invalid limits and releases failed streams", async () => {
76
+ for (const limit of [
77
+ 0,
78
+ -1,
79
+ NaN,
80
+ Infinity,
81
+ 1.5
82
+ ]) await (0, node_assert_strict.rejects)(require_body.readBoundedText(new Response("{}"), limit, url), RangeError);
83
+ const failure = /* @__PURE__ */ new Error("broken stream");
84
+ const response = new Response(new ReadableStream({ pull(controller) {
85
+ controller.error(failure);
86
+ } }));
87
+ await (0, node_assert_strict.rejects)(require_body.readBoundedText(response, 10, url), (error) => error === failure);
88
+ (0, node_assert_strict.deepStrictEqual)(response.body?.locked, false);
89
+ });
90
+ //#endregion
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,90 @@
1
+ import { r as readBoundedText, t as BodyTooLargeError } from "./body-UDpZTF6c.mjs";
2
+ import { deepStrictEqual, ok, rejects } from "node:assert/strict";
3
+ import { test } from "node:test";
4
+ import { gunzipSync, gzipSync } from "node:zlib";
5
+ //#region src/body.test.ts
6
+ const url = "https://example.com/document";
7
+ test("readBoundedText handles empty bodies and flushes incomplete UTF-8", async () => {
8
+ deepStrictEqual(await readBoundedText(new Response(null), 1, url), "");
9
+ deepStrictEqual(await readBoundedText(new Response(""), 1, url), "");
10
+ deepStrictEqual(await readBoundedText(new Response(new Uint8Array([226, 130])), 2, url), "�");
11
+ });
12
+ test("readBoundedText counts bytes and preserves split UTF-8", async () => {
13
+ const bytes = new TextEncoder().encode("\"안녕\"");
14
+ const stream = new ReadableStream({ start(controller) {
15
+ for (const byte of bytes) controller.enqueue(new Uint8Array([byte]));
16
+ controller.close();
17
+ } });
18
+ deepStrictEqual(await readBoundedText(new Response(stream), bytes.length, url), "\"안녕\"");
19
+ await rejects(readBoundedText(new Response(bytes), bytes.length - 1, url), BodyTooLargeError);
20
+ });
21
+ test("readBoundedText rejects a declared oversized body without pulling", async () => {
22
+ let pulled = false;
23
+ let canceled = false;
24
+ const response = new Response(new ReadableStream({
25
+ pull() {
26
+ pulled = true;
27
+ },
28
+ cancel() {
29
+ canceled = true;
30
+ }
31
+ }, { highWaterMark: 0 }), { headers: { "Content-Length": "100" } });
32
+ await rejects(readBoundedText(response, 10, url), BodyTooLargeError);
33
+ deepStrictEqual(pulled, false);
34
+ deepStrictEqual(canceled, true);
35
+ deepStrictEqual(response.body?.locked, false);
36
+ });
37
+ test("readBoundedText cancels chunked bodies despite absent or false lengths", async () => {
38
+ for (const length of [
39
+ void 0,
40
+ "1",
41
+ "invalid"
42
+ ]) {
43
+ let pulls = 0;
44
+ let canceled = false;
45
+ const stream = new ReadableStream({
46
+ pull(controller) {
47
+ pulls++;
48
+ controller.enqueue(new Uint8Array(4));
49
+ },
50
+ cancel() {
51
+ canceled = true;
52
+ }
53
+ }, { highWaterMark: 0 });
54
+ const response = new Response(stream, { headers: length == null ? {} : { "Content-Length": length } });
55
+ await rejects(readBoundedText(response, 8, url), BodyTooLargeError);
56
+ deepStrictEqual(pulls, 3);
57
+ deepStrictEqual(canceled, true);
58
+ deepStrictEqual(response.body?.locked, false);
59
+ }
60
+ });
61
+ test("readBoundedText limits decoded gzip bytes, not the compressed length", async () => {
62
+ const compressed = gzipSync("\"" + "a".repeat(4096) + "\"");
63
+ ok(compressed.length < 128);
64
+ const decoded = gunzipSync(compressed);
65
+ await rejects(readBoundedText(new Response(decoded, { headers: {
66
+ "Content-Encoding": "gzip",
67
+ "Content-Length": String(compressed.length)
68
+ } }), 128, url), BodyTooLargeError);
69
+ deepStrictEqual(await readBoundedText(new Response("{}", { headers: {
70
+ "Content-Encoding": "gzip",
71
+ "Content-Length": "22"
72
+ } }), 2, url), "{}");
73
+ });
74
+ test("readBoundedText rejects invalid limits and releases failed streams", async () => {
75
+ for (const limit of [
76
+ 0,
77
+ -1,
78
+ NaN,
79
+ Infinity,
80
+ 1.5
81
+ ]) await rejects(readBoundedText(new Response("{}"), limit, url), RangeError);
82
+ const failure = /* @__PURE__ */ new Error("broken stream");
83
+ const response = new Response(new ReadableStream({ pull(controller) {
84
+ controller.error(failure);
85
+ } }));
86
+ await rejects(readBoundedText(response, 10, url), (error) => error === failure);
87
+ deepStrictEqual(response.body?.locked, false);
88
+ });
89
+ //#endregion
90
+ export {};
@@ -1,11 +1,11 @@
1
1
  require("./chunk-Do9eywBl.cjs");
2
- require("./docloader-ekzTbc9R.cjs");
3
- require("./url-CWU5HT3Y.cjs");
4
- require("./key-DTTIntwb.cjs");
5
- require("./multibase-F7LtMMsK.cjs");
6
- require("./langstr-CbAxaeEZ.cjs");
7
- let node_assert = require("node:assert");
2
+ require("./docloader-B5V6GD-7.cjs");
3
+ require("./url-CnyF489g.cjs");
4
+ require("./key-BarKGLQz.cjs");
5
+ require("./multibase-8zhYPa7o.cjs");
6
+ require("./langstr-BOQHfJDr.cjs");
8
7
  let node_test = require("node:test");
8
+ let node_assert = require("node:assert");
9
9
  //#region src/decimal.ts
10
10
  const DECIMAL_PATTERN = /^(\+|-)?([0-9]+(\.[0-9]*)?|\.[0-9]+)$/;
11
11
  const XML_SCHEMA_WHITESPACE_PATTERN = /[\t\n\r ]+/g;
@@ -1,10 +1,10 @@
1
- import "./docloader-Nug7_hIl.mjs";
2
- import "./url-o0e-aXQY.mjs";
3
- import "./key-BeTHFQJK.mjs";
4
- import "./multibase-BgU9XRf7.mjs";
5
- import "./langstr-Di5AvKpB.mjs";
6
- import { deepStrictEqual, throws } from "node:assert";
1
+ import "./docloader-Cld9tcx4.mjs";
2
+ import "./url-BBfduh6o.mjs";
3
+ import "./key-BDIsC6hV.mjs";
4
+ import "./multibase-Ds75BvXs.mjs";
5
+ import "./langstr-DDLc4833.mjs";
7
6
  import { test } from "node:test";
7
+ import { deepStrictEqual, throws } from "node:assert";
8
8
  //#region src/decimal.ts
9
9
  const DECIMAL_PATTERN = /^(\+|-)?([0-9]+(\.[0-9]*)?|\.[0-9]+)$/;
10
10
  const XML_SCHEMA_WHITESPACE_PATTERN = /[\t\n\r ]+/g;
@@ -1,7 +1,8 @@
1
1
  require("./chunk-Do9eywBl.cjs");
2
- const require_request = require("./request-HcA2nRAs.cjs");
3
- const require_link = require("./link-FguCydMA.cjs");
4
- const require_url = require("./url-CWU5HT3Y.cjs");
2
+ const require_request = require("./request-DqvIx6-9.cjs");
3
+ const require_body = require("./body-Vx0QSf6C.cjs");
4
+ const require_link = require("./link-qDTmjPw_.cjs");
5
+ const require_url = require("./url-CnyF489g.cjs");
5
6
  let _logtape_logtape = require("@logtape/logtape");
6
7
  let _opentelemetry_api = require("@opentelemetry/api");
7
8
  //#region src/contexts/activitystreams.json
@@ -4572,38 +4573,29 @@ async function getRemoteDocument(url, response, fetch) {
4572
4573
  }
4573
4574
  let document;
4574
4575
  if (!jsonLd && (contentType === "text/html" || contentType?.startsWith("text/html;") || contentType === "application/xhtml+xml" || contentType?.startsWith("application/xhtml+xml;"))) {
4575
- const MAX_HTML_SIZE = 1024 * 1024;
4576
- const html = await response.text();
4577
- if (html.length > MAX_HTML_SIZE) {
4578
- logger.warn("HTML response too large, skipping alternate link discovery: {url}", {
4579
- url: documentUrl,
4580
- size: html.length
4581
- });
4582
- document = JSON.parse(html);
4583
- } else {
4584
- const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
4585
- const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
4586
- let tagMatch;
4587
- while ((tagMatch = tagPattern.exec(html)) !== null) {
4588
- const tagContent = tagMatch[2];
4589
- let attrMatch;
4590
- const attribs = {};
4591
- attrPattern.lastIndex = 0;
4592
- while ((attrMatch = attrPattern.exec(tagContent)) !== null) {
4593
- const key = attrMatch[1].toLowerCase();
4594
- attribs[key] = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4] ?? "";
4595
- }
4596
- 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) {
4597
- logger.debug("Found alternate document: {alternateUrl} from {url}", {
4598
- alternateUrl: attribs.href,
4599
- url: documentUrl
4600
- });
4601
- return await fetch(new URL(attribs.href, docUrl).href);
4602
- }
4576
+ const html = await require_body.readBoundedText(response, 1024 * 1024, documentUrl);
4577
+ const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
4578
+ const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
4579
+ let tagMatch;
4580
+ while ((tagMatch = tagPattern.exec(html)) !== null) {
4581
+ const tagContent = tagMatch[2];
4582
+ let attrMatch;
4583
+ const attribs = {};
4584
+ attrPattern.lastIndex = 0;
4585
+ while ((attrMatch = attrPattern.exec(tagContent)) !== null) {
4586
+ const key = attrMatch[1].toLowerCase();
4587
+ attribs[key] = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4] ?? "";
4588
+ }
4589
+ 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) {
4590
+ logger.debug("Found alternate document: {alternateUrl} from {url}", {
4591
+ alternateUrl: attribs.href,
4592
+ url: documentUrl
4593
+ });
4594
+ return await fetch(new URL(attribs.href, docUrl).href);
4603
4595
  }
4604
- document = JSON.parse(html);
4605
4596
  }
4606
- } else document = await response.json();
4597
+ document = JSON.parse(html);
4598
+ } else document = JSON.parse(await require_body.readBoundedText(response, require_body.MAX_BODY_SIZE, documentUrl));
4607
4599
  logger.debug("Fetched document: {status} {url} {headers}", {
4608
4600
  status: response.status,
4609
4601
  url: documentUrl,
@@ -4720,6 +4712,12 @@ Object.defineProperty(exports, "getDocumentLoader", {
4720
4712
  return getDocumentLoader;
4721
4713
  }
4722
4714
  });
4715
+ Object.defineProperty(exports, "getRemoteDocument", {
4716
+ enumerable: true,
4717
+ get: function() {
4718
+ return getRemoteDocument;
4719
+ }
4720
+ });
4723
4721
  Object.defineProperty(exports, "preloadedContexts", {
4724
4722
  enumerable: true,
4725
4723
  get: function() {
@@ -1,6 +1,7 @@
1
- import { a as name, i as logRequest, n as createActivityPubRequest, o as version, t as FetchError } from "./request-CMtb-99N.mjs";
2
- import { t as HttpHeaderLink } from "./link-NUUWCdnK.mjs";
3
- import { o as validatePublicUrl, t as UrlError } from "./url-o0e-aXQY.mjs";
1
+ import { a as name, i as logRequest, n as createActivityPubRequest, o as version, t as FetchError } from "./request-B2ac0drD.mjs";
2
+ import { n as MAX_BODY_SIZE, r as readBoundedText } from "./body-UDpZTF6c.mjs";
3
+ import { t as HttpHeaderLink } from "./link-CYszIegt.mjs";
4
+ import { o as validatePublicUrl, t as UrlError } from "./url-BBfduh6o.mjs";
4
5
  import { getLogger } from "@logtape/logtape";
5
6
  import { SpanKind, SpanStatusCode, trace } from "@opentelemetry/api";
6
7
  //#region src/contexts/activitystreams.json
@@ -4571,38 +4572,29 @@ async function getRemoteDocument(url, response, fetch) {
4571
4572
  }
4572
4573
  let document;
4573
4574
  if (!jsonLd && (contentType === "text/html" || contentType?.startsWith("text/html;") || contentType === "application/xhtml+xml" || contentType?.startsWith("application/xhtml+xml;"))) {
4574
- const MAX_HTML_SIZE = 1024 * 1024;
4575
- const html = await response.text();
4576
- if (html.length > MAX_HTML_SIZE) {
4577
- logger.warn("HTML response too large, skipping alternate link discovery: {url}", {
4578
- url: documentUrl,
4579
- size: html.length
4580
- });
4581
- document = JSON.parse(html);
4582
- } else {
4583
- const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
4584
- const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
4585
- let tagMatch;
4586
- while ((tagMatch = tagPattern.exec(html)) !== null) {
4587
- const tagContent = tagMatch[2];
4588
- let attrMatch;
4589
- const attribs = {};
4590
- attrPattern.lastIndex = 0;
4591
- while ((attrMatch = attrPattern.exec(tagContent)) !== null) {
4592
- const key = attrMatch[1].toLowerCase();
4593
- attribs[key] = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4] ?? "";
4594
- }
4595
- 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) {
4596
- logger.debug("Found alternate document: {alternateUrl} from {url}", {
4597
- alternateUrl: attribs.href,
4598
- url: documentUrl
4599
- });
4600
- return await fetch(new URL(attribs.href, docUrl).href);
4601
- }
4575
+ const html = await readBoundedText(response, 1024 * 1024, documentUrl);
4576
+ const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
4577
+ const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
4578
+ let tagMatch;
4579
+ while ((tagMatch = tagPattern.exec(html)) !== null) {
4580
+ const tagContent = tagMatch[2];
4581
+ let attrMatch;
4582
+ const attribs = {};
4583
+ attrPattern.lastIndex = 0;
4584
+ while ((attrMatch = attrPattern.exec(tagContent)) !== null) {
4585
+ const key = attrMatch[1].toLowerCase();
4586
+ attribs[key] = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4] ?? "";
4587
+ }
4588
+ 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) {
4589
+ logger.debug("Found alternate document: {alternateUrl} from {url}", {
4590
+ alternateUrl: attribs.href,
4591
+ url: documentUrl
4592
+ });
4593
+ return await fetch(new URL(attribs.href, docUrl).href);
4602
4594
  }
4603
- document = JSON.parse(html);
4604
4595
  }
4605
- } else document = await response.json();
4596
+ document = JSON.parse(html);
4597
+ } else document = JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, documentUrl));
4606
4598
  logger.debug("Fetched document: {status} {url} {headers}", {
4607
4599
  status: response.status,
4608
4600
  url: documentUrl,
@@ -4707,4 +4699,4 @@ function getDocumentLoader({ allowPrivateAddress, maxRedirection, skipPreloadedC
4707
4699
  return load;
4708
4700
  }
4709
4701
  //#endregion
4710
- export { preloadedContexts as n, cid_v1_default as r, getDocumentLoader as t };
4702
+ export { cid_v1_default as i, getRemoteDocument as n, preloadedContexts as r, getDocumentLoader as t };
@@ -1,9 +1,11 @@
1
1
  const require_chunk = require("./chunk-Do9eywBl.cjs");
2
- const require_docloader = require("./docloader-ekzTbc9R.cjs");
3
- const require_request = require("./request-HcA2nRAs.cjs");
4
- const require_url = require("./url-CWU5HT3Y.cjs");
5
- let node_assert = require("node:assert");
2
+ const require_request = require("./request-DqvIx6-9.cjs");
3
+ const require_docloader = require("./docloader-B5V6GD-7.cjs");
4
+ const require_url = require("./url-CnyF489g.cjs");
6
5
  let node_test = require("node:test");
6
+ let node_zlib = require("node:zlib");
7
+ let node_assert = require("node:assert");
8
+ let node_http = require("node:http");
7
9
  //#region ../../node_modules/.pnpm/glob-to-regexp@0.4.1/node_modules/glob-to-regexp/index.js
8
10
  var require_glob_to_regexp = /* @__PURE__ */ require_chunk.__commonJSMin(((exports, module) => {
9
11
  module.exports = function(glob, opts) {
@@ -1567,4 +1569,81 @@ var esm_default = new class FetchMock {
1567
1569
  });
1568
1570
  esm_default.hardReset();
1569
1571
  });
1572
+ (0, node_test.test)("getDocumentLoader() bounds JSON, HTML, and alternate documents", async () => {
1573
+ const url = "https://example.com/bounded";
1574
+ esm_default.mockGlobal();
1575
+ let oversized = true;
1576
+ try {
1577
+ esm_default.get(url, () => new Response("{\"name\":\"hello\"}", { headers: {
1578
+ "Content-Type": "application/activity+json",
1579
+ ...oversized ? { "Content-Length": String(16777217) } : {}
1580
+ } }));
1581
+ const loader = require_docloader.getDocumentLoader({ allowPrivateAddress: true });
1582
+ await (0, node_assert.rejects)(loader(url), require_request.FetchError);
1583
+ oversized = false;
1584
+ (0, node_assert.deepStrictEqual)((await loader(url)).document, { name: "hello" });
1585
+ oversized = true;
1586
+ esm_default.get(`${url}/html`, () => new Response(" ".repeat(1048577), { headers: { "Content-Type": "text/html" } }));
1587
+ await (0, node_assert.rejects)(require_docloader.getDocumentLoader({ allowPrivateAddress: true })(`${url}/html`), require_request.FetchError);
1588
+ esm_default.get(`${url}/alternate`, () => new Response("", { headers: {
1589
+ "Content-Type": "text/html",
1590
+ Link: `<${url}>; rel="alternate"; type="application/activity+json"`
1591
+ } }));
1592
+ await (0, node_assert.rejects)(loader(`${url}/alternate`), require_request.FetchError);
1593
+ esm_default.get(`${url}/redirect`, {
1594
+ status: 302,
1595
+ headers: { Location: url }
1596
+ });
1597
+ await (0, node_assert.rejects)(loader(`${url}/redirect`), require_request.FetchError);
1598
+ } finally {
1599
+ esm_default.hardReset();
1600
+ }
1601
+ });
1602
+ (0, node_test.test)("getDocumentLoader() rejects oversized gzip responses from fetch", async () => {
1603
+ const document = { name: "a".repeat(16 * 1024 * 1024) };
1604
+ const compressed = (0, node_zlib.gzipSync)(JSON.stringify(document));
1605
+ (0, node_assert.ok)(compressed.byteLength < 64 * 1024);
1606
+ const server = (0, node_http.createServer)((request, response) => {
1607
+ const body = request.url === "/small" ? (0, node_zlib.gzipSync)("{\"name\":\"hello\"}") : compressed;
1608
+ response.writeHead(200, {
1609
+ "Content-Type": "application/activity+json",
1610
+ "Content-Encoding": "gzip",
1611
+ "Content-Length": body.byteLength
1612
+ });
1613
+ response.end(body);
1614
+ });
1615
+ await new Promise((resolve, reject) => {
1616
+ server.once("error", reject);
1617
+ server.listen(0, "127.0.0.1", resolve);
1618
+ });
1619
+ try {
1620
+ const address = server.address();
1621
+ (0, node_assert.ok)(address != null && typeof address !== "string");
1622
+ const url = `http://127.0.0.1:${address.port}/document`;
1623
+ await (0, node_assert.rejects)(require_docloader.getDocumentLoader({ allowPrivateAddress: true })(url), require_request.FetchError);
1624
+ (0, node_assert.deepStrictEqual)((await require_docloader.getDocumentLoader({ allowPrivateAddress: true })(new URL("/small", url).href)).document, { name: "hello" });
1625
+ } finally {
1626
+ await new Promise((resolve, reject) => {
1627
+ server.close((error) => error == null ? resolve() : reject(error));
1628
+ server.closeAllConnections();
1629
+ });
1630
+ }
1631
+ });
1632
+ (0, node_test.test)("getRemoteDocument() bounds JSON by default", async () => {
1633
+ let canceled = false;
1634
+ let pulls = 0;
1635
+ await (0, node_assert.rejects)(require_docloader.getRemoteDocument("https://example.com/document", new Response(new ReadableStream({
1636
+ pull(controller) {
1637
+ pulls++;
1638
+ controller.enqueue(new Uint8Array(64 * 1024).fill(32));
1639
+ },
1640
+ cancel() {
1641
+ canceled = true;
1642
+ }
1643
+ }, { highWaterMark: 0 })), () => {
1644
+ throw new Error("Unexpected alternate document");
1645
+ }), require_request.FetchError);
1646
+ (0, node_assert.deepStrictEqual)(canceled, true);
1647
+ (0, node_assert.deepStrictEqual)(pulls, 257);
1648
+ });
1570
1649
  //#endregion