@fedify/vocab-runtime 2.4.0-dev.1964 → 2.4.0-dev.1995

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 (42) hide show
  1. package/deno.json +1 -1
  2. package/dist/mod.cjs +176 -152
  3. package/dist/mod.js +176 -152
  4. package/dist/tests/body-B0an0K5j.cjs +94 -0
  5. package/dist/tests/body-DTu3lmhg.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-B3auVdRI.cjs → docloader-BiGP4BSX.cjs} +33 -82
  15. package/dist/tests/{docloader-BexVAGvr.mjs → docloader-DmFInJaI.mjs} +33 -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-C8BUtPj4.mjs → request-BPFUWEsX.mjs} +1 -1
  31. package/dist/tests/{request-CRO9h2AO.cjs → request-CAmmk2G7.cjs} +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/docloader.test.ts +128 -3
  42. package/src/docloader.ts +52 -93
package/dist/mod.js CHANGED
@@ -4517,7 +4517,151 @@ const preloadedContexts = {
4517
4517
  //#endregion
4518
4518
  //#region deno.json
4519
4519
  var name = "@fedify/vocab-runtime";
4520
- var version = "2.4.0-dev.1964+1d28dc03";
4520
+ var version = "2.4.0-dev.1995+618fd4e7";
4521
+ //#endregion
4522
+ //#region src/request.ts
4523
+ /**
4524
+ * Error thrown when fetching a JSON-LD document failed.
4525
+ */
4526
+ var FetchError = class extends Error {
4527
+ /**
4528
+ * The URL that failed to fetch.
4529
+ */
4530
+ url;
4531
+ /**
4532
+ * The HTTP response that failed, if available.
4533
+ */
4534
+ response;
4535
+ /**
4536
+ * Constructs a new `FetchError`.
4537
+ *
4538
+ * @param url The URL that failed to fetch.
4539
+ * @param message Error message.
4540
+ * @param response The failed HTTP response, if available.
4541
+ */
4542
+ constructor(url, message, response) {
4543
+ super(message == null ? url.toString() : `${url}: ${message}`);
4544
+ this.name = "FetchError";
4545
+ this.url = typeof url === "string" ? new URL(url) : url;
4546
+ this.response = response;
4547
+ }
4548
+ };
4549
+ /**
4550
+ * Creates a request for the given URL.
4551
+ * @param url The URL to create the request for.
4552
+ * @param options The options for the request.
4553
+ * @returns The created request.
4554
+ * @internal
4555
+ */
4556
+ function createActivityPubRequest(url, options = {}) {
4557
+ return new Request(url, {
4558
+ headers: {
4559
+ Accept: "application/activity+json, application/ld+json",
4560
+ "User-Agent": typeof options.userAgent === "string" ? options.userAgent : getUserAgent(options.userAgent)
4561
+ },
4562
+ redirect: "manual"
4563
+ });
4564
+ }
4565
+ /**
4566
+ * Gets the user agent string for the given application and URL.
4567
+ * @param options The options for making the user agent string.
4568
+ * @returns The user agent string.
4569
+ * @since 1.3.0
4570
+ */
4571
+ function getUserAgent({ software, url } = {}) {
4572
+ const fedify = `Fedify/${version}`;
4573
+ const runtime = globalThis.Deno?.version?.deno != null ? `Deno/${Deno.version.deno}` : globalThis.process?.versions?.bun != null ? `Bun/${process.versions.bun}` : "navigator" in globalThis && navigator.userAgent === "Cloudflare-Workers" ? navigator.userAgent : globalThis.process?.versions?.node != null ? `Node.js/${process.versions.node}` : null;
4574
+ const userAgent = software == null ? [fedify] : [software, fedify];
4575
+ if (runtime != null) userAgent.push(runtime);
4576
+ if (url != null) userAgent.push(`+${url.toString()}`);
4577
+ return `${userAgent.shift()} (${userAgent.join("; ")})`;
4578
+ }
4579
+ /**
4580
+ * Logs the request.
4581
+ * @param request The request to log.
4582
+ * @internal
4583
+ */
4584
+ function logRequest(logger, request) {
4585
+ logger.debug("Fetching document: {method} {url} {headers}", {
4586
+ method: request.method,
4587
+ url: request.url,
4588
+ headers: Object.fromEntries(request.headers.entries())
4589
+ });
4590
+ }
4591
+ //#endregion
4592
+ //#region src/body.ts
4593
+ /** Decoded JSON body limit: 16 MiB. @internal */
4594
+ const MAX_BODY_SIZE = 16 * 1024 * 1024;
4595
+ /** A body exceeded the byte limit. @internal */
4596
+ var BodyTooLargeError = class extends FetchError {
4597
+ /** Creates an error for a body exceeding the given limit. */
4598
+ constructor(url, maxBytes) {
4599
+ super(url, `Body exceeds the limit of ${maxBytes} bytes`);
4600
+ this.name = "BodyTooLargeError";
4601
+ }
4602
+ };
4603
+ /** Validates a finite, positive byte limit. @internal */
4604
+ function validateBodySizeLimit(maxBytes) {
4605
+ if (!Number.isSafeInteger(maxBytes) || maxBytes <= 0) throw new RangeError("The body size limit must be a positive safe integer.");
4606
+ }
4607
+ /**
4608
+ * Reads UTF-8 text while limiting bytes received from the body stream.
4609
+ * Fetch responses are already decompressed, so the stream count is authoritative.
4610
+ * @param message The response or request to read.
4611
+ * @param maxBytes The maximum number of decoded bytes to read.
4612
+ * @param url The URL to include in errors and logs.
4613
+ * @returns The decoded body text.
4614
+ * @throws {BodyTooLargeError} If the body exceeds the limit.
4615
+ * @internal
4616
+ */
4617
+ async function readBoundedText(message, maxBytes, url) {
4618
+ validateBodySizeLimit(maxBytes);
4619
+ const reader = message.body?.getReader();
4620
+ const tooLarge = () => {
4621
+ getLogger([
4622
+ "fedify",
4623
+ "runtime",
4624
+ "body"
4625
+ ]).warn("Body from {url} exceeds the limit of {maxBytes} bytes.", {
4626
+ url: url.toString(),
4627
+ maxBytes
4628
+ });
4629
+ throw new BodyTooLargeError(url, maxBytes);
4630
+ };
4631
+ try {
4632
+ const length = message.headers.get("Content-Length");
4633
+ const encoding = message.headers.get("Content-Encoding");
4634
+ if ((encoding == null || encoding.toLowerCase() === "identity") && length != null && /^\d+$/.test(length) && Number(length) > maxBytes) tooLarge();
4635
+ if (reader == null) return "";
4636
+ let size = 0;
4637
+ const textReader = new ReadableStream({ async pull(controller) {
4638
+ const { done, value } = await reader.read();
4639
+ if (done) {
4640
+ controller.close();
4641
+ return;
4642
+ }
4643
+ size += value.byteLength;
4644
+ if (size > maxBytes) tooLarge();
4645
+ controller.enqueue(value);
4646
+ } }, { highWaterMark: 0 }).pipeThrough(new TextDecoderStream()).getReader();
4647
+ try {
4648
+ const chunks = [];
4649
+ while (true) {
4650
+ const { done, value } = await textReader.read();
4651
+ if (done) break;
4652
+ chunks.push(value);
4653
+ }
4654
+ return chunks.join("");
4655
+ } finally {
4656
+ textReader.releaseLock();
4657
+ }
4658
+ } catch (error) {
4659
+ if (reader != null) reader.cancel(error).catch(() => {});
4660
+ throw error;
4661
+ } finally {
4662
+ reader?.releaseLock();
4663
+ }
4664
+ }
4521
4665
  //#endregion
4522
4666
  //#region src/link.ts
4523
4667
  const parametersNeedLowerCase = ["rel", "type"];
@@ -4700,76 +4844,6 @@ var HttpHeaderLink = class HttpHeaderLink {
4700
4844
  }
4701
4845
  };
4702
4846
  //#endregion
4703
- //#region src/request.ts
4704
- /**
4705
- * Error thrown when fetching a JSON-LD document failed.
4706
- */
4707
- var FetchError = class extends Error {
4708
- /**
4709
- * The URL that failed to fetch.
4710
- */
4711
- url;
4712
- /**
4713
- * The HTTP response that failed, if available.
4714
- */
4715
- response;
4716
- /**
4717
- * Constructs a new `FetchError`.
4718
- *
4719
- * @param url The URL that failed to fetch.
4720
- * @param message Error message.
4721
- * @param response The failed HTTP response, if available.
4722
- */
4723
- constructor(url, message, response) {
4724
- super(message == null ? url.toString() : `${url}: ${message}`);
4725
- this.name = "FetchError";
4726
- this.url = typeof url === "string" ? new URL(url) : url;
4727
- this.response = response;
4728
- }
4729
- };
4730
- /**
4731
- * Creates a request for the given URL.
4732
- * @param url The URL to create the request for.
4733
- * @param options The options for the request.
4734
- * @returns The created request.
4735
- * @internal
4736
- */
4737
- function createActivityPubRequest(url, options = {}) {
4738
- return new Request(url, {
4739
- headers: {
4740
- Accept: "application/activity+json, application/ld+json",
4741
- "User-Agent": typeof options.userAgent === "string" ? options.userAgent : getUserAgent(options.userAgent)
4742
- },
4743
- redirect: "manual"
4744
- });
4745
- }
4746
- /**
4747
- * Gets the user agent string for the given application and URL.
4748
- * @param options The options for making the user agent string.
4749
- * @returns The user agent string.
4750
- * @since 1.3.0
4751
- */
4752
- function getUserAgent({ software, url } = {}) {
4753
- const fedify = `Fedify/${version}`;
4754
- const runtime = globalThis.Deno?.version?.deno != null ? `Deno/${Deno.version.deno}` : globalThis.process?.versions?.bun != null ? `Bun/${process.versions.bun}` : "navigator" in globalThis && navigator.userAgent === "Cloudflare-Workers" ? navigator.userAgent : globalThis.process?.versions?.node != null ? `Node.js/${process.versions.node}` : null;
4755
- const userAgent = software == null ? [fedify] : [software, fedify];
4756
- if (runtime != null) userAgent.push(runtime);
4757
- if (url != null) userAgent.push(`+${url.toString()}`);
4758
- return `${userAgent.shift()} (${userAgent.join("; ")})`;
4759
- }
4760
- /**
4761
- * Logs the request.
4762
- * @param request The request to log.
4763
- * @internal
4764
- */
4765
- function logRequest(logger, request) {
4766
- logger.debug("Fetching document: {method} {url} {headers}", {
4767
- method: request.method,
4768
- url: request.url,
4769
- headers: Object.fromEntries(request.headers.entries())
4770
- });
4771
- }
4772
- //#endregion
4773
4847
  //#region src/docloader.ts
4774
4848
  const logger = getLogger([
4775
4849
  "fedify",
@@ -4785,58 +4859,6 @@ function createResponseMetadata(response) {
4785
4859
  statusText: response.statusText
4786
4860
  });
4787
4861
  }
4788
- async function cancelResponseBody(response) {
4789
- if (response.body != null) await response.body.cancel();
4790
- }
4791
- async function readBoundedText(response, maxBytes) {
4792
- const contentLength = response.headers.get("Content-Length");
4793
- if (contentLength != null) {
4794
- const size = Number(contentLength);
4795
- if (size > maxBytes) {
4796
- await cancelResponseBody(response);
4797
- return {
4798
- text: "",
4799
- size,
4800
- tooLarge: true
4801
- };
4802
- }
4803
- }
4804
- if (response.body == null) return {
4805
- text: "",
4806
- size: 0,
4807
- tooLarge: false
4808
- };
4809
- const reader = response.body.getReader();
4810
- const decoder = new TextDecoder();
4811
- let text = "";
4812
- let size = 0;
4813
- try {
4814
- while (true) {
4815
- const result = await reader.read();
4816
- if (result.done) break;
4817
- const chunkSize = result.value.byteLength;
4818
- if (size + chunkSize > maxBytes) {
4819
- size += chunkSize;
4820
- await reader.cancel();
4821
- return {
4822
- text: "",
4823
- size,
4824
- tooLarge: true
4825
- };
4826
- }
4827
- size += chunkSize;
4828
- text += decoder.decode(result.value, { stream: true });
4829
- }
4830
- text += decoder.decode();
4831
- return {
4832
- text,
4833
- size,
4834
- tooLarge: false
4835
- };
4836
- } finally {
4837
- reader.releaseLock();
4838
- }
4839
- }
4840
4862
  /**
4841
4863
  * Gets a {@link RemoteDocument} from the given response.
4842
4864
  * @param url The URL of the document to load.
@@ -4892,42 +4914,44 @@ async function getRemoteDocument(url, response, fetch) {
4892
4914
  let document;
4893
4915
  if (!jsonLd && (contentType === "text/html" || contentType?.startsWith("text/html;") || contentType === "application/xhtml+xml" || contentType?.startsWith("application/xhtml+xml;"))) {
4894
4916
  const errorResponse = createResponseMetadata(response);
4895
- const html = await readBoundedText(response, MAX_HTML_SIZE);
4896
- if (html.tooLarge) {
4917
+ let html;
4918
+ try {
4919
+ html = await readBoundedText(response, MAX_HTML_SIZE, documentUrl);
4920
+ } catch (error) {
4921
+ if (!(error instanceof BodyTooLargeError)) throw error;
4897
4922
  logger.warn("HTML response too large, skipping alternate link discovery: {url}", {
4898
4923
  url: documentUrl,
4899
- size: html.size
4924
+ maxBytes: MAX_HTML_SIZE
4900
4925
  });
4901
4926
  throw new FetchError(documentUrl, `HTML document is too large to scan for an ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
4902
- } else {
4903
- const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
4904
- const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
4905
- let tagMatch;
4906
- while ((tagMatch = tagPattern.exec(html.text)) !== null) {
4907
- const tagContent = tagMatch[2];
4908
- let attrMatch;
4909
- const attribs = {};
4910
- attrPattern.lastIndex = 0;
4911
- while ((attrMatch = attrPattern.exec(tagContent)) !== null) {
4912
- const key = attrMatch[1].toLowerCase();
4913
- attribs[key] = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4] ?? "";
4914
- }
4915
- 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) {
4916
- logger.debug("Found alternate document: {alternateUrl} from {url}", {
4917
- alternateUrl: attribs.href,
4918
- url: documentUrl
4919
- });
4920
- return await fetch(new URL(attribs.href, docUrl).href);
4921
- }
4927
+ }
4928
+ const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
4929
+ const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
4930
+ let tagMatch;
4931
+ while ((tagMatch = tagPattern.exec(html)) !== null) {
4932
+ const tagContent = tagMatch[2];
4933
+ let attrMatch;
4934
+ const attribs = {};
4935
+ attrPattern.lastIndex = 0;
4936
+ while ((attrMatch = attrPattern.exec(tagContent)) !== null) {
4937
+ const key = attrMatch[1].toLowerCase();
4938
+ attribs[key] = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4] ?? "";
4922
4939
  }
4923
- try {
4924
- document = JSON.parse(html.text);
4925
- } catch (error) {
4926
- if (!(error instanceof SyntaxError)) throw error;
4927
- throw new FetchError(documentUrl, `HTML document has no ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
4940
+ 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) {
4941
+ logger.debug("Found alternate document: {alternateUrl} from {url}", {
4942
+ alternateUrl: attribs.href,
4943
+ url: documentUrl
4944
+ });
4945
+ return await fetch(new URL(attribs.href, docUrl).href);
4928
4946
  }
4929
4947
  }
4930
- } else document = await response.json();
4948
+ try {
4949
+ document = JSON.parse(html);
4950
+ } catch (error) {
4951
+ if (!(error instanceof SyntaxError)) throw error;
4952
+ throw new FetchError(documentUrl, `HTML document has no ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
4953
+ }
4954
+ } else document = JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, documentUrl));
4931
4955
  logger.debug("Fetched document: {status} {url} {headers}", {
4932
4956
  status: response.status,
4933
4957
  url: documentUrl,
@@ -0,0 +1,94 @@
1
+ const require_request = require("./request-CAmmk2G7.cjs");
2
+ let _logtape_logtape = require("@logtape/logtape");
3
+ //#region src/body.ts
4
+ /** Decoded JSON body limit: 16 MiB. @internal */
5
+ const MAX_BODY_SIZE = 16 * 1024 * 1024;
6
+ /** A body exceeded the byte limit. @internal */
7
+ var BodyTooLargeError = class extends require_request.FetchError {
8
+ /** Creates an error for a body exceeding the given limit. */
9
+ constructor(url, maxBytes) {
10
+ super(url, `Body exceeds the limit of ${maxBytes} bytes`);
11
+ this.name = "BodyTooLargeError";
12
+ }
13
+ };
14
+ /** Validates a finite, positive byte limit. @internal */
15
+ function validateBodySizeLimit(maxBytes) {
16
+ if (!Number.isSafeInteger(maxBytes) || maxBytes <= 0) throw new RangeError("The body size limit must be a positive safe integer.");
17
+ }
18
+ /**
19
+ * Reads UTF-8 text while limiting bytes received from the body stream.
20
+ * Fetch responses are already decompressed, so the stream count is authoritative.
21
+ * @param message The response or request to read.
22
+ * @param maxBytes The maximum number of decoded bytes to read.
23
+ * @param url The URL to include in errors and logs.
24
+ * @returns The decoded body text.
25
+ * @throws {BodyTooLargeError} If the body exceeds the limit.
26
+ * @internal
27
+ */
28
+ async function readBoundedText(message, maxBytes, url) {
29
+ validateBodySizeLimit(maxBytes);
30
+ const reader = message.body?.getReader();
31
+ const tooLarge = () => {
32
+ (0, _logtape_logtape.getLogger)([
33
+ "fedify",
34
+ "runtime",
35
+ "body"
36
+ ]).warn("Body from {url} exceeds the limit of {maxBytes} bytes.", {
37
+ url: url.toString(),
38
+ maxBytes
39
+ });
40
+ throw new BodyTooLargeError(url, maxBytes);
41
+ };
42
+ try {
43
+ const length = message.headers.get("Content-Length");
44
+ const encoding = message.headers.get("Content-Encoding");
45
+ if ((encoding == null || encoding.toLowerCase() === "identity") && length != null && /^\d+$/.test(length) && Number(length) > maxBytes) tooLarge();
46
+ if (reader == null) return "";
47
+ let size = 0;
48
+ const textReader = new ReadableStream({ async pull(controller) {
49
+ const { done, value } = await reader.read();
50
+ if (done) {
51
+ controller.close();
52
+ return;
53
+ }
54
+ size += value.byteLength;
55
+ if (size > maxBytes) tooLarge();
56
+ controller.enqueue(value);
57
+ } }, { highWaterMark: 0 }).pipeThrough(new TextDecoderStream()).getReader();
58
+ try {
59
+ const chunks = [];
60
+ while (true) {
61
+ const { done, value } = await textReader.read();
62
+ if (done) break;
63
+ chunks.push(value);
64
+ }
65
+ return chunks.join("");
66
+ } finally {
67
+ textReader.releaseLock();
68
+ }
69
+ } catch (error) {
70
+ if (reader != null) reader.cancel(error).catch(() => {});
71
+ throw error;
72
+ } finally {
73
+ reader?.releaseLock();
74
+ }
75
+ }
76
+ //#endregion
77
+ Object.defineProperty(exports, "BodyTooLargeError", {
78
+ enumerable: true,
79
+ get: function() {
80
+ return BodyTooLargeError;
81
+ }
82
+ });
83
+ Object.defineProperty(exports, "MAX_BODY_SIZE", {
84
+ enumerable: true,
85
+ get: function() {
86
+ return MAX_BODY_SIZE;
87
+ }
88
+ });
89
+ Object.defineProperty(exports, "readBoundedText", {
90
+ enumerable: true,
91
+ get: function() {
92
+ return readBoundedText;
93
+ }
94
+ });
@@ -0,0 +1,77 @@
1
+ import { t as FetchError } from "./request-BPFUWEsX.mjs";
2
+ import { getLogger } from "@logtape/logtape";
3
+ //#region src/body.ts
4
+ /** Decoded JSON body limit: 16 MiB. @internal */
5
+ const MAX_BODY_SIZE = 16 * 1024 * 1024;
6
+ /** A body exceeded the byte limit. @internal */
7
+ var BodyTooLargeError = class extends FetchError {
8
+ /** Creates an error for a body exceeding the given limit. */
9
+ constructor(url, maxBytes) {
10
+ super(url, `Body exceeds the limit of ${maxBytes} bytes`);
11
+ this.name = "BodyTooLargeError";
12
+ }
13
+ };
14
+ /** Validates a finite, positive byte limit. @internal */
15
+ function validateBodySizeLimit(maxBytes) {
16
+ if (!Number.isSafeInteger(maxBytes) || maxBytes <= 0) throw new RangeError("The body size limit must be a positive safe integer.");
17
+ }
18
+ /**
19
+ * Reads UTF-8 text while limiting bytes received from the body stream.
20
+ * Fetch responses are already decompressed, so the stream count is authoritative.
21
+ * @param message The response or request to read.
22
+ * @param maxBytes The maximum number of decoded bytes to read.
23
+ * @param url The URL to include in errors and logs.
24
+ * @returns The decoded body text.
25
+ * @throws {BodyTooLargeError} If the body exceeds the limit.
26
+ * @internal
27
+ */
28
+ async function readBoundedText(message, maxBytes, url) {
29
+ validateBodySizeLimit(maxBytes);
30
+ const reader = message.body?.getReader();
31
+ const tooLarge = () => {
32
+ getLogger([
33
+ "fedify",
34
+ "runtime",
35
+ "body"
36
+ ]).warn("Body from {url} exceeds the limit of {maxBytes} bytes.", {
37
+ url: url.toString(),
38
+ maxBytes
39
+ });
40
+ throw new BodyTooLargeError(url, maxBytes);
41
+ };
42
+ try {
43
+ const length = message.headers.get("Content-Length");
44
+ const encoding = message.headers.get("Content-Encoding");
45
+ if ((encoding == null || encoding.toLowerCase() === "identity") && length != null && /^\d+$/.test(length) && Number(length) > maxBytes) tooLarge();
46
+ if (reader == null) return "";
47
+ let size = 0;
48
+ const textReader = new ReadableStream({ async pull(controller) {
49
+ const { done, value } = await reader.read();
50
+ if (done) {
51
+ controller.close();
52
+ return;
53
+ }
54
+ size += value.byteLength;
55
+ if (size > maxBytes) tooLarge();
56
+ controller.enqueue(value);
57
+ } }, { highWaterMark: 0 }).pipeThrough(new TextDecoderStream()).getReader();
58
+ try {
59
+ const chunks = [];
60
+ while (true) {
61
+ const { done, value } = await textReader.read();
62
+ if (done) break;
63
+ chunks.push(value);
64
+ }
65
+ return chunks.join("");
66
+ } finally {
67
+ textReader.releaseLock();
68
+ }
69
+ } catch (error) {
70
+ if (reader != null) reader.cancel(error).catch(() => {});
71
+ throw error;
72
+ } finally {
73
+ reader?.releaseLock();
74
+ }
75
+ }
76
+ //#endregion
77
+ export { MAX_BODY_SIZE as n, readBoundedText as r, BodyTooLargeError as t };
@@ -0,0 +1,89 @@
1
+ const require_body = require("./body-B0an0K5j.cjs");
2
+ let node_assert_strict = require("node:assert/strict");
3
+ let node_test = require("node:test");
4
+ let node_zlib = require("node:zlib");
5
+ //#region src/body.test.ts
6
+ const url = "https://example.com/document";
7
+ (0, node_test.test)("readBoundedText handles empty bodies and flushes incomplete UTF-8", async () => {
8
+ (0, node_assert_strict.deepStrictEqual)(await require_body.readBoundedText(new Response(null), 1, url), "");
9
+ (0, node_assert_strict.deepStrictEqual)(await require_body.readBoundedText(new Response(""), 1, url), "");
10
+ (0, node_assert_strict.deepStrictEqual)(await require_body.readBoundedText(new Response(new Uint8Array([226, 130])), 2, url), "�");
11
+ });
12
+ (0, node_test.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
+ (0, node_assert_strict.deepStrictEqual)(await require_body.readBoundedText(new Response(stream), bytes.length, url), "\"안녕\"");
19
+ await (0, node_assert_strict.rejects)(require_body.readBoundedText(new Response(bytes), bytes.length - 1, url), require_body.BodyTooLargeError);
20
+ });
21
+ (0, node_test.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 (0, node_assert_strict.rejects)(require_body.readBoundedText(response, 10, url), require_body.BodyTooLargeError);
33
+ (0, node_assert_strict.deepStrictEqual)(pulled, false);
34
+ (0, node_assert_strict.deepStrictEqual)(canceled, true);
35
+ (0, node_assert_strict.deepStrictEqual)(response.body?.locked, false);
36
+ });
37
+ (0, node_test.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 (0, node_assert_strict.rejects)(require_body.readBoundedText(response, 8, url), require_body.BodyTooLargeError);
56
+ (0, node_assert_strict.deepStrictEqual)(pulls, 3);
57
+ (0, node_assert_strict.deepStrictEqual)(canceled, true);
58
+ (0, node_assert_strict.deepStrictEqual)(response.body?.locked, false);
59
+ }
60
+ });
61
+ (0, node_test.test)("readBoundedText limits decoded gzip bytes, not the compressed length", async () => {
62
+ const compressed = (0, node_zlib.gzipSync)("\"" + "a".repeat(4096) + "\"");
63
+ (0, node_assert_strict.ok)(compressed.length < 128);
64
+ const decoded = (0, node_zlib.gunzipSync)(compressed);
65
+ await (0, node_assert_strict.rejects)(require_body.readBoundedText(new Response(decoded, { headers: {
66
+ "Content-Encoding": "gzip",
67
+ "Content-Length": String(compressed.length)
68
+ } }), 128, url), require_body.BodyTooLargeError);
69
+ (0, node_assert_strict.deepStrictEqual)(await require_body.readBoundedText(new Response("{}", { headers: {
70
+ "Content-Encoding": "gzip",
71
+ "Content-Length": "22"
72
+ } }), 2, url), "{}");
73
+ });
74
+ (0, node_test.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 (0, node_assert_strict.rejects)(require_body.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 (0, node_assert_strict.rejects)(require_body.readBoundedText(response, 10, url), (error) => error === failure);
87
+ (0, node_assert_strict.deepStrictEqual)(response.body?.locked, false);
88
+ });
89
+ //#endregion
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1 @@
1
+ export { };