@fedify/vocab-runtime 2.4.0-dev.1962 → 2.4.0-dev.1988

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 (44) hide show
  1. package/deno.json +1 -1
  2. package/dist/mod.cjs +204 -152
  3. package/dist/mod.js +204 -152
  4. package/dist/tests/body-Cg5W_IdS.cjs +94 -0
  5. package/dist/tests/body-DRVriaJO.mjs +77 -0
  6. package/dist/tests/body.test.cjs +89 -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 +2 -2
  11. package/dist/tests/decimal.test.mjs +2 -2
  12. package/dist/tests/digest.test.cjs +1 -1
  13. package/dist/tests/digest.test.mjs +1 -1
  14. package/dist/tests/{docloader-cQ5_cV6R.mjs → docloader-ZSduXDDg.mjs} +61 -82
  15. package/dist/tests/{docloader-CbeAECB4.cjs → docloader-j_N6ZEZx.cjs} +61 -82
  16. package/dist/tests/docloader.test.cjs +84 -5
  17. package/dist/tests/docloader.test.mjs +84 -5
  18. package/dist/tests/internal/multicodec.test.cjs +1 -1
  19. package/dist/tests/internal/multicodec.test.mjs +1 -1
  20. package/dist/tests/jsonld-cache.test.cjs +1 -1
  21. package/dist/tests/jsonld-cache.test.mjs +1 -1
  22. package/dist/tests/key.test.cjs +1 -1
  23. package/dist/tests/key.test.mjs +1 -1
  24. package/dist/tests/langstr.test.cjs +1 -1
  25. package/dist/tests/langstr.test.mjs +1 -1
  26. package/dist/tests/link.test.cjs +1 -1
  27. package/dist/tests/link.test.mjs +1 -1
  28. package/dist/tests/multibase/multibase.test.cjs +1 -1
  29. package/dist/tests/multibase/multibase.test.mjs +1 -1
  30. package/dist/tests/{request-me46Cb_a.cjs → request-B3YkikPO.cjs} +1 -1
  31. package/dist/tests/{request-CywSvOoa.mjs → request-Bo1iqGDW.mjs} +1 -1
  32. package/dist/tests/request.test.cjs +2 -2
  33. package/dist/tests/request.test.mjs +2 -2
  34. package/dist/tests/temporal.test.cjs +1 -1
  35. package/dist/tests/temporal.test.mjs +1 -1
  36. package/dist/tests/url.test.cjs +1 -1
  37. package/dist/tests/url.test.mjs +1 -1
  38. package/package.json +1 -1
  39. package/src/body.test.ts +125 -0
  40. package/src/body.ts +95 -0
  41. package/src/contexts/fep-22cd.json +21 -0
  42. package/src/contexts.ts +2 -0
  43. package/src/docloader.test.ts +128 -3
  44. package/src/docloader.ts +52 -93
@@ -0,0 +1,90 @@
1
+ import { r as readBoundedText, t as BodyTooLargeError } from "./body-DRVriaJO.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(/* @__PURE__ */ 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
- require("./docloader-CbeAECB4.cjs");
1
+ require("./docloader-j_N6ZEZx.cjs");
2
2
  require("./url-CPfMatUL.cjs");
3
3
  require("./key-8Qg2SbZi.cjs");
4
4
  require("./multibase-B5Mea7Ip.cjs");
5
5
  require("./digest-3FeH2Y-Q.cjs");
6
6
  require("./langstr-C4Fl80ae.cjs");
7
- let node_assert = require("node:assert");
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,11 +1,11 @@
1
- import "./docloader-cQ5_cV6R.mjs";
1
+ import "./docloader-ZSduXDDg.mjs";
2
2
  import "./url-Basm1L_c.mjs";
3
3
  import "./key-C7MNhlkO.mjs";
4
4
  import "./multibase-BPnF_L4e.mjs";
5
5
  import "./digest-COC7xDiQ.mjs";
6
6
  import "./langstr-CQ26J_L7.mjs";
7
- import { deepStrictEqual, throws } from "node:assert";
8
7
  import { test } from "node:test";
8
+ import { deepStrictEqual, throws } from "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,8 +1,8 @@
1
1
  const require_multicodec = require("./multicodec-CLRPeW4N.cjs");
2
2
  const require_multibase = require("./multibase-B5Mea7Ip.cjs");
3
3
  const require_digest = require("./digest-3FeH2Y-Q.cjs");
4
- let node_test = require("node:test");
5
4
  let node_assert_strict = require("node:assert/strict");
5
+ let node_test = require("node:test");
6
6
  //#region src/digest.test.ts
7
7
  const encoder = new TextEncoder();
8
8
  const decoder = new TextDecoder();
@@ -1,8 +1,8 @@
1
1
  import { t as addMulticodecPrefix } from "./multicodec-CRIj_05H.mjs";
2
2
  import { n as encodeMultibase } from "./multibase-BPnF_L4e.mjs";
3
3
  import { a as verifyDigestMultibase, i as parseHashlink, n as createHashlink, o as verifyHashlink, r as parseDigestMultibase, t as computeDigestMultibase } from "./digest-COC7xDiQ.mjs";
4
- import { test } from "node:test";
5
4
  import { deepStrictEqual, equal, rejects, throws } from "node:assert/strict";
5
+ import { test } from "node:test";
6
6
  //#region src/digest.test.ts
7
7
  const encoder = new TextEncoder();
8
8
  const decoder = new TextDecoder();
@@ -1,4 +1,5 @@
1
- import { a as name, i as logRequest, n as createActivityPubRequest, o as version, t as FetchError } from "./request-CywSvOoa.mjs";
1
+ import { a as name, i as logRequest, n as createActivityPubRequest, o as version, t as FetchError } from "./request-Bo1iqGDW.mjs";
2
+ import { n as MAX_BODY_SIZE, r as readBoundedText, t as BodyTooLargeError } from "./body-DRVriaJO.mjs";
2
3
  import { t as HttpHeaderLink } from "./link-Cevmc87v.mjs";
3
4
  import { g as validatePublicUrl, t as UrlError } from "./url-Basm1L_c.mjs";
4
5
  import { getLogger } from "@logtape/logtape";
@@ -4354,6 +4355,34 @@ const preloadedContexts = {
4354
4355
  "@type": "@id"
4355
4356
  }
4356
4357
  } },
4358
+ "https://w3id.org/fep/22cd": { "@context": {
4359
+ "fep-22cd": "https://w3id.org/fep/22cd#",
4360
+ "schema": "https://schema.org/",
4361
+ "xsd": "http://www.w3.org/2001/XMLSchema#",
4362
+ "translations": {
4363
+ "@id": "fep-22cd:translations",
4364
+ "@container": "@set"
4365
+ },
4366
+ "Translation": "fep-22cd:Translation",
4367
+ "sourceUpdated": {
4368
+ "@id": "fep-22cd:sourceUpdated",
4369
+ "@type": "xsd:dateTime"
4370
+ },
4371
+ "translator": {
4372
+ "@id": "schema:translator",
4373
+ "@type": "@id",
4374
+ "@container": "@set"
4375
+ },
4376
+ "inLanguage": "schema:inLanguage",
4377
+ "translationOfWork": {
4378
+ "@id": "schema:translationOfWork",
4379
+ "@type": "@id"
4380
+ },
4381
+ "isBasedOn": {
4382
+ "@id": "schema:isBasedOn",
4383
+ "@type": "@id"
4384
+ }
4385
+ } },
4357
4386
  "https://w3id.org/fep/5711": { "@context": {
4358
4387
  "likesOf": {
4359
4388
  "@id": "https://w3id.org/fep/5711#likesOf",
@@ -4501,58 +4530,6 @@ function createResponseMetadata(response) {
4501
4530
  statusText: response.statusText
4502
4531
  });
4503
4532
  }
4504
- async function cancelResponseBody(response) {
4505
- if (response.body != null) await response.body.cancel();
4506
- }
4507
- async function readBoundedText(response, maxBytes) {
4508
- const contentLength = response.headers.get("Content-Length");
4509
- if (contentLength != null) {
4510
- const size = Number(contentLength);
4511
- if (size > maxBytes) {
4512
- await cancelResponseBody(response);
4513
- return {
4514
- text: "",
4515
- size,
4516
- tooLarge: true
4517
- };
4518
- }
4519
- }
4520
- if (response.body == null) return {
4521
- text: "",
4522
- size: 0,
4523
- tooLarge: false
4524
- };
4525
- const reader = response.body.getReader();
4526
- const decoder = new TextDecoder();
4527
- let text = "";
4528
- let size = 0;
4529
- try {
4530
- while (true) {
4531
- const result = await reader.read();
4532
- if (result.done) break;
4533
- const chunkSize = result.value.byteLength;
4534
- if (size + chunkSize > maxBytes) {
4535
- size += chunkSize;
4536
- await reader.cancel();
4537
- return {
4538
- text: "",
4539
- size,
4540
- tooLarge: true
4541
- };
4542
- }
4543
- size += chunkSize;
4544
- text += decoder.decode(result.value, { stream: true });
4545
- }
4546
- text += decoder.decode();
4547
- return {
4548
- text,
4549
- size,
4550
- tooLarge: false
4551
- };
4552
- } finally {
4553
- reader.releaseLock();
4554
- }
4555
- }
4556
4533
  /**
4557
4534
  * Gets a {@link RemoteDocument} from the given response.
4558
4535
  * @param url The URL of the document to load.
@@ -4608,42 +4585,44 @@ async function getRemoteDocument(url, response, fetch) {
4608
4585
  let document;
4609
4586
  if (!jsonLd && (contentType === "text/html" || contentType?.startsWith("text/html;") || contentType === "application/xhtml+xml" || contentType?.startsWith("application/xhtml+xml;"))) {
4610
4587
  const errorResponse = createResponseMetadata(response);
4611
- const html = await readBoundedText(response, MAX_HTML_SIZE);
4612
- if (html.tooLarge) {
4588
+ let html;
4589
+ try {
4590
+ html = await readBoundedText(response, MAX_HTML_SIZE, documentUrl);
4591
+ } catch (error) {
4592
+ if (!(error instanceof BodyTooLargeError)) throw error;
4613
4593
  logger.warn("HTML response too large, skipping alternate link discovery: {url}", {
4614
4594
  url: documentUrl,
4615
- size: html.size
4595
+ maxBytes: MAX_HTML_SIZE
4616
4596
  });
4617
4597
  throw new FetchError(documentUrl, `HTML document is too large to scan for an ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
4618
- } else {
4619
- const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
4620
- const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
4621
- let tagMatch;
4622
- while ((tagMatch = tagPattern.exec(html.text)) !== null) {
4623
- const tagContent = tagMatch[2];
4624
- let attrMatch;
4625
- const attribs = {};
4626
- attrPattern.lastIndex = 0;
4627
- while ((attrMatch = attrPattern.exec(tagContent)) !== null) {
4628
- const key = attrMatch[1].toLowerCase();
4629
- attribs[key] = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4] ?? "";
4630
- }
4631
- 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) {
4632
- logger.debug("Found alternate document: {alternateUrl} from {url}", {
4633
- alternateUrl: attribs.href,
4634
- url: documentUrl
4635
- });
4636
- return await fetch(new URL(attribs.href, docUrl).href);
4637
- }
4598
+ }
4599
+ const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
4600
+ const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
4601
+ let tagMatch;
4602
+ while ((tagMatch = tagPattern.exec(html)) !== null) {
4603
+ const tagContent = tagMatch[2];
4604
+ let attrMatch;
4605
+ const attribs = {};
4606
+ attrPattern.lastIndex = 0;
4607
+ while ((attrMatch = attrPattern.exec(tagContent)) !== null) {
4608
+ const key = attrMatch[1].toLowerCase();
4609
+ attribs[key] = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4] ?? "";
4638
4610
  }
4639
- try {
4640
- document = JSON.parse(html.text);
4641
- } catch (error) {
4642
- if (!(error instanceof SyntaxError)) throw error;
4643
- throw new FetchError(documentUrl, `HTML document has no ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
4611
+ 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) {
4612
+ logger.debug("Found alternate document: {alternateUrl} from {url}", {
4613
+ alternateUrl: attribs.href,
4614
+ url: documentUrl
4615
+ });
4616
+ return await fetch(new URL(attribs.href, docUrl).href);
4644
4617
  }
4645
4618
  }
4646
- } else document = await response.json();
4619
+ try {
4620
+ document = JSON.parse(html);
4621
+ } catch (error) {
4622
+ if (!(error instanceof SyntaxError)) throw error;
4623
+ throw new FetchError(documentUrl, `HTML document has no ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
4624
+ }
4625
+ } else document = JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, documentUrl));
4647
4626
  logger.debug("Fetched document: {status} {url} {headers}", {
4648
4627
  status: response.status,
4649
4628
  url: documentUrl,
@@ -1,4 +1,5 @@
1
- const require_request = require("./request-me46Cb_a.cjs");
1
+ const require_request = require("./request-B3YkikPO.cjs");
2
+ const require_body = require("./body-Cg5W_IdS.cjs");
2
3
  const require_link = require("./link-DlKm8bEr.cjs");
3
4
  const require_url = require("./url-CPfMatUL.cjs");
4
5
  let _logtape_logtape = require("@logtape/logtape");
@@ -4354,6 +4355,34 @@ const preloadedContexts = {
4354
4355
  "@type": "@id"
4355
4356
  }
4356
4357
  } },
4358
+ "https://w3id.org/fep/22cd": { "@context": {
4359
+ "fep-22cd": "https://w3id.org/fep/22cd#",
4360
+ "schema": "https://schema.org/",
4361
+ "xsd": "http://www.w3.org/2001/XMLSchema#",
4362
+ "translations": {
4363
+ "@id": "fep-22cd:translations",
4364
+ "@container": "@set"
4365
+ },
4366
+ "Translation": "fep-22cd:Translation",
4367
+ "sourceUpdated": {
4368
+ "@id": "fep-22cd:sourceUpdated",
4369
+ "@type": "xsd:dateTime"
4370
+ },
4371
+ "translator": {
4372
+ "@id": "schema:translator",
4373
+ "@type": "@id",
4374
+ "@container": "@set"
4375
+ },
4376
+ "inLanguage": "schema:inLanguage",
4377
+ "translationOfWork": {
4378
+ "@id": "schema:translationOfWork",
4379
+ "@type": "@id"
4380
+ },
4381
+ "isBasedOn": {
4382
+ "@id": "schema:isBasedOn",
4383
+ "@type": "@id"
4384
+ }
4385
+ } },
4357
4386
  "https://w3id.org/fep/5711": { "@context": {
4358
4387
  "likesOf": {
4359
4388
  "@id": "https://w3id.org/fep/5711#likesOf",
@@ -4501,58 +4530,6 @@ function createResponseMetadata(response) {
4501
4530
  statusText: response.statusText
4502
4531
  });
4503
4532
  }
4504
- async function cancelResponseBody(response) {
4505
- if (response.body != null) await response.body.cancel();
4506
- }
4507
- async function readBoundedText(response, maxBytes) {
4508
- const contentLength = response.headers.get("Content-Length");
4509
- if (contentLength != null) {
4510
- const size = Number(contentLength);
4511
- if (size > maxBytes) {
4512
- await cancelResponseBody(response);
4513
- return {
4514
- text: "",
4515
- size,
4516
- tooLarge: true
4517
- };
4518
- }
4519
- }
4520
- if (response.body == null) return {
4521
- text: "",
4522
- size: 0,
4523
- tooLarge: false
4524
- };
4525
- const reader = response.body.getReader();
4526
- const decoder = new TextDecoder();
4527
- let text = "";
4528
- let size = 0;
4529
- try {
4530
- while (true) {
4531
- const result = await reader.read();
4532
- if (result.done) break;
4533
- const chunkSize = result.value.byteLength;
4534
- if (size + chunkSize > maxBytes) {
4535
- size += chunkSize;
4536
- await reader.cancel();
4537
- return {
4538
- text: "",
4539
- size,
4540
- tooLarge: true
4541
- };
4542
- }
4543
- size += chunkSize;
4544
- text += decoder.decode(result.value, { stream: true });
4545
- }
4546
- text += decoder.decode();
4547
- return {
4548
- text,
4549
- size,
4550
- tooLarge: false
4551
- };
4552
- } finally {
4553
- reader.releaseLock();
4554
- }
4555
- }
4556
4533
  /**
4557
4534
  * Gets a {@link RemoteDocument} from the given response.
4558
4535
  * @param url The URL of the document to load.
@@ -4608,42 +4585,44 @@ async function getRemoteDocument(url, response, fetch) {
4608
4585
  let document;
4609
4586
  if (!jsonLd && (contentType === "text/html" || contentType?.startsWith("text/html;") || contentType === "application/xhtml+xml" || contentType?.startsWith("application/xhtml+xml;"))) {
4610
4587
  const errorResponse = createResponseMetadata(response);
4611
- const html = await readBoundedText(response, MAX_HTML_SIZE);
4612
- if (html.tooLarge) {
4588
+ let html;
4589
+ try {
4590
+ html = await require_body.readBoundedText(response, MAX_HTML_SIZE, documentUrl);
4591
+ } catch (error) {
4592
+ if (!(error instanceof require_body.BodyTooLargeError)) throw error;
4613
4593
  logger.warn("HTML response too large, skipping alternate link discovery: {url}", {
4614
4594
  url: documentUrl,
4615
- size: html.size
4595
+ maxBytes: MAX_HTML_SIZE
4616
4596
  });
4617
4597
  throw new require_request.FetchError(documentUrl, `HTML document is too large to scan for an ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
4618
- } else {
4619
- const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
4620
- const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
4621
- let tagMatch;
4622
- while ((tagMatch = tagPattern.exec(html.text)) !== null) {
4623
- const tagContent = tagMatch[2];
4624
- let attrMatch;
4625
- const attribs = {};
4626
- attrPattern.lastIndex = 0;
4627
- while ((attrMatch = attrPattern.exec(tagContent)) !== null) {
4628
- const key = attrMatch[1].toLowerCase();
4629
- attribs[key] = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4] ?? "";
4630
- }
4631
- 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) {
4632
- logger.debug("Found alternate document: {alternateUrl} from {url}", {
4633
- alternateUrl: attribs.href,
4634
- url: documentUrl
4635
- });
4636
- return await fetch(new URL(attribs.href, docUrl).href);
4637
- }
4598
+ }
4599
+ const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
4600
+ const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
4601
+ let tagMatch;
4602
+ while ((tagMatch = tagPattern.exec(html)) !== null) {
4603
+ const tagContent = tagMatch[2];
4604
+ let attrMatch;
4605
+ const attribs = {};
4606
+ attrPattern.lastIndex = 0;
4607
+ while ((attrMatch = attrPattern.exec(tagContent)) !== null) {
4608
+ const key = attrMatch[1].toLowerCase();
4609
+ attribs[key] = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4] ?? "";
4638
4610
  }
4639
- try {
4640
- document = JSON.parse(html.text);
4641
- } catch (error) {
4642
- if (!(error instanceof SyntaxError)) throw error;
4643
- throw new require_request.FetchError(documentUrl, `HTML document has no ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
4611
+ 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) {
4612
+ logger.debug("Found alternate document: {alternateUrl} from {url}", {
4613
+ alternateUrl: attribs.href,
4614
+ url: documentUrl
4615
+ });
4616
+ return await fetch(new URL(attribs.href, docUrl).href);
4644
4617
  }
4645
4618
  }
4646
- } else document = await response.json();
4619
+ try {
4620
+ document = JSON.parse(html);
4621
+ } catch (error) {
4622
+ if (!(error instanceof SyntaxError)) throw error;
4623
+ throw new require_request.FetchError(documentUrl, `HTML document has no ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
4624
+ }
4625
+ } else document = JSON.parse(await require_body.readBoundedText(response, require_body.MAX_BODY_SIZE, documentUrl));
4647
4626
  logger.debug("Fetched document: {status} {url} {headers}", {
4648
4627
  status: response.status,
4649
4628
  url: documentUrl,
@@ -1,9 +1,11 @@
1
1
  const require_rolldown_runtime = require("./rolldown-runtime-emK7D4bc.cjs");
2
- const require_docloader = require("./docloader-CbeAECB4.cjs");
3
- const require_request = require("./request-me46Cb_a.cjs");
2
+ const require_request = require("./request-B3YkikPO.cjs");
3
+ const require_docloader = require("./docloader-j_N6ZEZx.cjs");
4
4
  const require_url = require("./url-CPfMatUL.cjs");
5
- let node_assert = require("node:assert");
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_rolldown_runtime.__commonJSMin(((exports, module) => {
9
11
  module.exports = function(glob, opts) {
@@ -1471,9 +1473,9 @@ var esm_default = new class FetchMock {
1471
1473
  "Content-Length": String(1048577),
1472
1474
  "Content-Type": "text/html; charset=utf-8"
1473
1475
  } });
1474
- Object.defineProperty(response, "body", { value: { cancel: () => {
1476
+ Object.defineProperty(response, "body", { value: new ReadableStream({ cancel() {
1475
1477
  canceled = true;
1476
- } } });
1478
+ } }) });
1477
1479
  await (0, node_assert.rejects)(() => require_docloader.getRemoteDocument("https://example.com/large-html-cancel", response, () => {
1478
1480
  throw new Error("unexpected alternate fetch");
1479
1481
  }), require_request.FetchError);
@@ -1626,4 +1628,81 @@ var esm_default = new class FetchMock {
1626
1628
  });
1627
1629
  esm_default.hardReset();
1628
1630
  });
1631
+ (0, node_test.test)("getDocumentLoader() bounds JSON, HTML, and alternate documents", async () => {
1632
+ const url = "https://example.com/bounded";
1633
+ esm_default.mockGlobal();
1634
+ let oversized = true;
1635
+ try {
1636
+ esm_default.get(url, () => new Response("{\"name\":\"hello\"}", { headers: {
1637
+ "Content-Type": "application/activity+json",
1638
+ ...oversized ? { "Content-Length": String(16777217) } : {}
1639
+ } }));
1640
+ const loader = require_docloader.getDocumentLoader({ allowPrivateAddress: true });
1641
+ await (0, node_assert.rejects)(loader(url), require_request.FetchError);
1642
+ oversized = false;
1643
+ (0, node_assert.deepStrictEqual)((await loader(url)).document, { name: "hello" });
1644
+ oversized = true;
1645
+ esm_default.get(`${url}/html`, () => new Response(" ".repeat(1048577), { headers: { "Content-Type": "text/html" } }));
1646
+ await (0, node_assert.rejects)(require_docloader.getDocumentLoader({ allowPrivateAddress: true })(`${url}/html`), require_request.FetchError);
1647
+ esm_default.get(`${url}/alternate`, () => new Response("", { headers: {
1648
+ "Content-Type": "text/html",
1649
+ Link: `<${url}>; rel="alternate"; type="application/activity+json"`
1650
+ } }));
1651
+ await (0, node_assert.rejects)(loader(`${url}/alternate`), require_request.FetchError);
1652
+ esm_default.get(`${url}/redirect`, {
1653
+ status: 302,
1654
+ headers: { Location: url }
1655
+ });
1656
+ await (0, node_assert.rejects)(loader(`${url}/redirect`), require_request.FetchError);
1657
+ } finally {
1658
+ esm_default.hardReset();
1659
+ }
1660
+ });
1661
+ (0, node_test.test)("getDocumentLoader() rejects oversized gzip responses from fetch", async () => {
1662
+ const document = { name: "a".repeat(16 * 1024 * 1024) };
1663
+ const compressed = (0, node_zlib.gzipSync)(JSON.stringify(document));
1664
+ (0, node_assert.ok)(compressed.byteLength < 64 * 1024);
1665
+ const server = (0, node_http.createServer)((request, response) => {
1666
+ const body = request.url === "/small" ? (0, node_zlib.gzipSync)("{\"name\":\"hello\"}") : compressed;
1667
+ response.writeHead(200, {
1668
+ "Content-Type": "application/activity+json",
1669
+ "Content-Encoding": "gzip",
1670
+ "Content-Length": body.byteLength
1671
+ });
1672
+ response.end(body);
1673
+ });
1674
+ await new Promise((resolve, reject) => {
1675
+ server.once("error", reject);
1676
+ server.listen(0, "127.0.0.1", resolve);
1677
+ });
1678
+ try {
1679
+ const address = server.address();
1680
+ (0, node_assert.ok)(address != null && typeof address !== "string");
1681
+ const url = `http://127.0.0.1:${address.port}/document`;
1682
+ await (0, node_assert.rejects)(require_docloader.getDocumentLoader({ allowPrivateAddress: true })(url), require_request.FetchError);
1683
+ (0, node_assert.deepStrictEqual)((await require_docloader.getDocumentLoader({ allowPrivateAddress: true })(new URL("/small", url).href)).document, { name: "hello" });
1684
+ } finally {
1685
+ await new Promise((resolve, reject) => {
1686
+ server.close((error) => error == null ? resolve() : reject(error));
1687
+ server.closeAllConnections();
1688
+ });
1689
+ }
1690
+ });
1691
+ (0, node_test.test)("getRemoteDocument() bounds JSON by default", async () => {
1692
+ let canceled = false;
1693
+ let pulls = 0;
1694
+ await (0, node_assert.rejects)(require_docloader.getRemoteDocument("https://example.com/document", new Response(new ReadableStream({
1695
+ pull(controller) {
1696
+ pulls++;
1697
+ controller.enqueue(new Uint8Array(64 * 1024).fill(32));
1698
+ },
1699
+ cancel() {
1700
+ canceled = true;
1701
+ }
1702
+ }, { highWaterMark: 0 })), () => {
1703
+ throw new Error("Unexpected alternate document");
1704
+ }), require_request.FetchError);
1705
+ (0, node_assert.deepStrictEqual)(canceled, true);
1706
+ (0, node_assert.deepStrictEqual)(pulls, 257);
1707
+ });
1629
1708
  //#endregion