@fedify/vocab-runtime 2.0.35-dev.2480 → 2.0.35

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 (108) hide show
  1. package/deno.json +5 -2
  2. package/dist/internal.cjs +6 -0
  3. package/dist/internal.d.cts +3 -0
  4. package/dist/internal.d.ts +3 -0
  5. package/dist/internal.js +3 -0
  6. package/dist/mod.cjs +105 -253
  7. package/dist/mod.d.cts +5 -27
  8. package/dist/mod.d.ts +6 -27
  9. package/dist/mod.js +98 -245
  10. package/dist/public-fetch-BC-nWJNM.d.ts +143 -0
  11. package/dist/public-fetch-CFnW7XMn.cjs +821 -0
  12. package/dist/public-fetch-DBpt3xxf.d.cts +143 -0
  13. package/dist/public-fetch-Dfmc_Kfy.js +772 -0
  14. package/dist/tests/{body-s65I5CCa.mjs → body-ByUnLtuw.mjs} +1 -1
  15. package/dist/tests/{body-moFua5l6.cjs → body-Cul4-oB8.cjs} +1 -1
  16. package/dist/tests/body.test.cjs +1 -1
  17. package/dist/tests/body.test.mjs +1 -1
  18. package/dist/tests/docloader.test.cjs +616 -1306
  19. package/dist/tests/docloader.test.mjs +562 -1271
  20. package/dist/tests/esm-4DuocLvD.cjs +1208 -0
  21. package/dist/tests/esm-CnLt0wAF.mjs +1225 -0
  22. package/dist/tests/internal/multicodec.test.cjs +1 -1
  23. package/dist/tests/internal/multicodec.test.mjs +1 -1
  24. package/dist/tests/key.test.cjs +2 -2
  25. package/dist/tests/key.test.mjs +2 -2
  26. package/dist/tests/langstr.test.cjs +1 -1
  27. package/dist/tests/langstr.test.mjs +1 -1
  28. package/dist/tests/langtag.test.cjs +1 -1
  29. package/dist/tests/langtag.test.mjs +1 -1
  30. package/dist/tests/link.test.cjs +1 -1
  31. package/dist/tests/link.test.mjs +1 -1
  32. package/dist/tests/multibase/multibase.test.cjs +1 -1
  33. package/dist/tests/multibase/multibase.test.mjs +1 -1
  34. package/dist/tests/node-BTPYk2h-.mjs +84 -0
  35. package/dist/tests/node-DWD76vPo.cjs +90 -0
  36. package/dist/tests/pin-grant-BAlKqSph.mjs +53 -0
  37. package/dist/tests/pin-grant-CU-o367k.cjs +70 -0
  38. package/dist/tests/pin-grant.test.cjs +88 -0
  39. package/dist/tests/pin-grant.test.d.cts +1 -0
  40. package/dist/tests/pin-grant.test.d.mts +1 -0
  41. package/dist/tests/pin-grant.test.mjs +88 -0
  42. package/dist/tests/pinners/bun.test.cjs +167 -0
  43. package/dist/tests/pinners/bun.test.d.cts +1 -0
  44. package/dist/tests/pinners/bun.test.d.mts +1 -0
  45. package/dist/tests/pinners/bun.test.mjs +167 -0
  46. package/dist/tests/pinners/node.test.cjs +269 -0
  47. package/dist/tests/pinners/node.test.d.cts +1 -0
  48. package/dist/tests/pinners/node.test.d.mts +1 -0
  49. package/dist/tests/pinners/node.test.mjs +268 -0
  50. package/dist/tests/pinners/pinners.test.cjs +197 -0
  51. package/dist/tests/pinners/pinners.test.d.cts +1 -0
  52. package/dist/tests/pinners/pinners.test.d.mts +1 -0
  53. package/dist/tests/pinners/pinners.test.mjs +197 -0
  54. package/dist/tests/pinners/testdata/node-child.cjs +135 -0
  55. package/dist/tests/pinners/testdata/node-child.d.cts +1 -0
  56. package/dist/tests/pinners/testdata/node-child.d.mts +1 -0
  57. package/dist/tests/pinners/testdata/node-child.mjs +134 -0
  58. package/dist/tests/pinners-BjE-YJLL.cjs +221 -0
  59. package/dist/tests/pinners-C0SoXz-S.mjs +203 -0
  60. package/dist/tests/public-fetch-BUSPlh1C.mjs +170 -0
  61. package/dist/tests/public-fetch-Ddjfxbwc.cjs +187 -0
  62. package/dist/tests/public-fetch.test.cjs +1074 -0
  63. package/dist/tests/public-fetch.test.d.cts +1 -0
  64. package/dist/tests/public-fetch.test.d.mts +1 -0
  65. package/dist/tests/public-fetch.test.mjs +1074 -0
  66. package/dist/tests/{request-BnwgFF6c.cjs → request-B7Iyskql.cjs} +1 -1
  67. package/dist/tests/{request-CZWDeBC-.mjs → request-pGToUa6Q.mjs} +1 -1
  68. package/dist/tests/request.test.cjs +1 -1
  69. package/dist/tests/request.test.mjs +1 -1
  70. package/dist/tests/support-EdBgVdNy.cjs +363 -0
  71. package/dist/tests/support-SnCB9BSR.mjs +272 -0
  72. package/dist/tests/{url-Qk1svKZU.cjs → url-DLuSj57U.cjs} +88 -12
  73. package/dist/tests/{url-DzlMS01z.mjs → url-DjsVF44Q.mjs} +65 -13
  74. package/dist/tests/url.test.cjs +4 -1
  75. package/dist/tests/url.test.mjs +4 -1
  76. package/package.json +13 -2
  77. package/src/docloader.test.ts +690 -31
  78. package/src/docloader.ts +174 -67
  79. package/src/internal.ts +13 -0
  80. package/src/pin-grant.test.ts +118 -0
  81. package/src/pin-grant.ts +99 -0
  82. package/src/pinners/bun.test.ts +278 -0
  83. package/src/pinners/bun.ts +223 -0
  84. package/src/pinners/mod.ts +59 -0
  85. package/src/pinners/node.test.ts +415 -0
  86. package/src/pinners/node.ts +144 -0
  87. package/src/pinners/pinners.test.ts +316 -0
  88. package/src/pinners/testdata/node-child.ts +147 -0
  89. package/src/pinners/testdata/proxy-child.ts +82 -0
  90. package/src/pinners/testdata/support.ts +343 -0
  91. package/src/pinners/testdata/tls-cert.pem +12 -0
  92. package/src/pinners/testdata/tls-child.ts +88 -0
  93. package/src/pinners/testdata/tls-key.pem +5 -0
  94. package/src/pinners/types.ts +48 -0
  95. package/src/public-fetch.test.ts +1286 -0
  96. package/src/public-fetch.ts +338 -0
  97. package/src/testdata/public-fetch-deno-worker.ts +74 -0
  98. package/src/url.test.ts +11 -0
  99. package/src/url.ts +113 -22
  100. package/tsdown.config.ts +12 -3
  101. /package/dist/tests/{langtag-Bm8oKhIO.cjs → langtag-B_fXk5oL.cjs} +0 -0
  102. /package/dist/tests/{langtag-QZZgy903.mjs → langtag-CwDxJ10m.mjs} +0 -0
  103. /package/dist/tests/{link-NUUWCdnK.mjs → link-CYszIegt.mjs} +0 -0
  104. /package/dist/tests/{link-FguCydMA.cjs → link-qDTmjPw_.cjs} +0 -0
  105. /package/dist/tests/{multibase-CnmModw0.cjs → multibase-BKJat02S.cjs} +0 -0
  106. /package/dist/tests/{multibase-DLBufb99.mjs → multibase-CNswrMoP.mjs} +0 -0
  107. /package/dist/tests/{multicodec-CxGVGa91.cjs → multicodec-B-_CSt1M.cjs} +0 -0
  108. /package/dist/tests/{multicodec-CyFp54fI.mjs → multicodec-CDEzQTyO.mjs} +0 -0
package/deno.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@fedify/vocab-runtime",
3
- "version": "2.0.35-dev.2480+2330a2e7",
3
+ "version": "2.0.35",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": "./src/mod.ts",
7
+ "./internal": "./src/internal.ts",
7
8
  "./jsonld": "./src/jsonld.ts",
8
9
  "./temporal": "./src/temporal.ts"
9
10
  },
@@ -20,7 +21,8 @@
20
21
  "byte-encodings": "npm:byte-encodings@^1.0.11",
21
22
  "fetch-mock": "npm:fetch-mock@^12.5.4",
22
23
  "jsonld": "npm:jsonld@^9.0.0",
23
- "pkijs": "npm:pkijs@^3.2.5"
24
+ "pkijs": "npm:pkijs@^3.2.5",
25
+ "undici": "npm:undici@^7.30.1"
24
26
  },
25
27
  "exclude": [
26
28
  "dist",
@@ -29,6 +31,7 @@
29
31
  "publish": {
30
32
  "exclude": [
31
33
  "**/*.test.ts",
34
+ "src/**/testdata/",
32
35
  "tsdown.config.ts"
33
36
  ]
34
37
  },
@@ -0,0 +1,6 @@
1
+
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const require_public_fetch = require("./public-fetch-CFnW7XMn.cjs");
4
+ exports.openDirectFetch = require_public_fetch.openDirectFetch;
5
+ exports.openPublicFetch = require_public_fetch.openPublicFetch;
6
+ exports.withPublicFetch = require_public_fetch.withPublicFetch;
@@ -0,0 +1,3 @@
1
+ /// <reference lib="esnext.temporal" />
2
+ import { a as withPublicFetch, i as openPublicFetch, n as PublicFetchSession, r as openDirectFetch, t as PublicFetchOptions } from "./public-fetch-DBpt3xxf.cjs";
3
+ export { type PublicFetchOptions, type PublicFetchSession, openDirectFetch, openPublicFetch, withPublicFetch };
@@ -0,0 +1,3 @@
1
+ /// <reference lib="esnext.temporal" />
2
+ import { a as withPublicFetch, i as openPublicFetch, n as PublicFetchSession, r as openDirectFetch, t as PublicFetchOptions } from "./public-fetch-BC-nWJNM.js";
3
+ export { type PublicFetchOptions, type PublicFetchSession, openDirectFetch, openPublicFetch, withPublicFetch };
@@ -0,0 +1,3 @@
1
+
2
+ import { n as openPublicFetch, r as withPublicFetch, t as openDirectFetch } from "./public-fetch-Dfmc_Kfy.js";
3
+ export { openDirectFetch, openPublicFetch, withPublicFetch };
package/dist/mod.cjs CHANGED
@@ -1,13 +1,11 @@
1
1
 
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const require_chunk = require("./chunk-M78iaK0I.cjs");
4
+ const require_public_fetch = require("./public-fetch-CFnW7XMn.cjs");
4
5
  let _logtape_logtape = require("@logtape/logtape");
5
6
  let _opentelemetry_api = require("@opentelemetry/api");
6
7
  let node_process = require("node:process");
7
8
  node_process = require_chunk.__toESM(node_process);
8
- let node_dns_promises = require("node:dns/promises");
9
- node_dns_promises = require_chunk.__toESM(node_dns_promises);
10
- let node_net = require("node:net");
11
9
  let asn1js = require("asn1js");
12
10
  let byte_encodings_base64 = require("byte-encodings/base64");
13
11
  let byte_encodings_base64url = require("byte-encodings/base64url");
@@ -4504,7 +4502,7 @@ const preloadedContexts = {
4504
4502
  //#endregion
4505
4503
  //#region deno.json
4506
4504
  var name = "@fedify/vocab-runtime";
4507
- var version = "2.0.35-dev.2480+2330a2e7";
4505
+ var version = "2.0.35";
4508
4506
  //#endregion
4509
4507
  //#region src/request.ts
4510
4508
  /**
@@ -4825,216 +4823,6 @@ var HttpHeaderLink = class HttpHeaderLink {
4825
4823
  }
4826
4824
  };
4827
4825
  //#endregion
4828
- //#region src/url.ts
4829
- var UrlError = class extends Error {
4830
- /**
4831
- * The failure category. Defaults to `"disallowed"`.
4832
- * `"dns"` includes lookup errors and results with no usable IP addresses.
4833
- * @since 2.0.29
4834
- */
4835
- reason;
4836
- constructor(message, options) {
4837
- super(message, options);
4838
- this.name = "UrlError";
4839
- this.reason = options?.reason ?? "disallowed";
4840
- }
4841
- };
4842
- /**
4843
- * Validates a URL to prevent SSRF attacks.
4844
- */
4845
- async function validatePublicUrl(url) {
4846
- const parsed = new URL(url);
4847
- if (parsed.protocol !== "http:" && parsed.protocol !== "https:") throw new UrlError(`Unsupported protocol: ${parsed.protocol}`);
4848
- let hostname = parsed.hostname;
4849
- if (hostname.startsWith("[") && hostname.endsWith("]")) hostname = hostname.slice(1, -1);
4850
- if (hostname === "localhost") throw new UrlError("Localhost is not allowed");
4851
- const hostnameFamily = (0, node_net.isIP)(hostname);
4852
- if (hostnameFamily !== 0) {
4853
- validatePublicIpAddress(hostname, hostnameFamily);
4854
- return;
4855
- }
4856
- if ("Deno" in globalThis && !(0, node_net.isIP)(hostname)) {
4857
- if ((await Deno.permissions.query({ name: "net" })).state !== "granted") return;
4858
- }
4859
- if ("Bun" in globalThis) {
4860
- if (hostname === "example.com" || hostname.endsWith(".example.com")) return;
4861
- else if (hostname === "fedify-test.internal") throw new UrlError("Invalid or private address: fedify-test.internal");
4862
- }
4863
- let addresses;
4864
- try {
4865
- addresses = await node_dns_promises.default.lookup(hostname, { all: true });
4866
- } catch (error) {
4867
- throw new UrlError("DNS lookup failed", {
4868
- cause: error,
4869
- reason: "dns"
4870
- });
4871
- }
4872
- validateLookupAddresses(addresses);
4873
- }
4874
- /**
4875
- * Validates the IP addresses returned by `node:dns.lookup()`.
4876
- *
4877
- * Cloudflare Workers' `node:dns` implementation currently maps every record
4878
- * in a DNS-over-HTTPS `Answer` array—including CNAME records—to a
4879
- * `LookupAddress`, even though Node.js specifies that the `address` field must
4880
- * contain an IPv4 or IPv6 literal. See:
4881
- * https://github.com/cloudflare/workerd/issues/6886
4882
- *
4883
- * Work around that bug by ignoring non-IP entries only when the lookup also
4884
- * returns at least one actual IP address. This remains fail-closed: a result
4885
- * containing no IP addresses is rejected, and every returned IP address is
4886
- * still validated and must be public. This workaround can be revisited once
4887
- * the Workerd issue is fixed in supported Cloudflare Workers runtimes.
4888
- *
4889
- * @internal
4890
- */
4891
- function validateLookupAddresses(addresses) {
4892
- let ipAddressCount = 0;
4893
- for (const { address } of addresses) {
4894
- const family = (0, node_net.isIP)(address);
4895
- if (family === 0) continue;
4896
- ipAddressCount++;
4897
- validatePublicIpAddress(address, family);
4898
- }
4899
- if (ipAddressCount === 0) throw new UrlError("DNS lookup did not return any IP address", { reason: "dns" });
4900
- }
4901
- function validatePublicIpAddress(address, family) {
4902
- if (family === 4 && isValidPublicIPv4Address(address) || family === 6 && isValidPublicIPv6Address(address)) return;
4903
- throw new UrlError(`Invalid or private address: ${address}`);
4904
- }
4905
- function isValidPublicIPv4Address(address) {
4906
- const parts = parseIPv4Address(address);
4907
- if (parts == null) return false;
4908
- const value = ipv4PartsToNumber(parts);
4909
- return !nonPublicIPv4Prefixes.some(({ base, prefix }) => matchesIPv4Prefix(value, base, prefix));
4910
- }
4911
- function isValidPublicIPv6Address(address) {
4912
- const words = parseIPv6Address(address);
4913
- if (words == null) return false;
4914
- if (nonPublicIPv6Prefixes.some(({ words: prefixWords, prefix }) => matchesIPv6Prefix(words, prefixWords, prefix))) return false;
4915
- for (const { extractIPv4, prefix, words: prefixWords } of ipv6WithIPv4Prefixes) {
4916
- if (!matchesIPv6Prefix(words, prefixWords, prefix)) continue;
4917
- const ipv4Address = extractIPv4(words);
4918
- if (ipv4Address != null && !isValidPublicIPv4Address(ipv4Address)) return false;
4919
- }
4920
- return true;
4921
- }
4922
- function expandIPv6Address(address) {
4923
- address = address.toLowerCase();
4924
- const ipv4Delimiter = address.lastIndexOf(":");
4925
- if (address.includes(".") && ipv4Delimiter >= 0) {
4926
- const ipv4Parts = parseIPv4Address(address.substring(ipv4Delimiter + 1));
4927
- if (ipv4Parts == null) return address;
4928
- const high = (ipv4Parts[0] << 8) + ipv4Parts[1];
4929
- const low = (ipv4Parts[2] << 8) + ipv4Parts[3];
4930
- address = address.substring(0, ipv4Delimiter + 1) + high.toString(16) + ":" + low.toString(16);
4931
- }
4932
- if (address === "::") return "0000:0000:0000:0000:0000:0000:0000:0000";
4933
- if (address.startsWith("::")) address = "0000" + address;
4934
- if (address.endsWith("::")) address = address + "0000";
4935
- address = address.replace("::", ":0000".repeat(8 - (address.match(/:/g) || []).length) + ":");
4936
- return address.split(":").map((part) => part.padStart(4, "0")).join(":");
4937
- }
4938
- const nonPublicIPv4Prefixes = [
4939
- ipv4Prefix("0.0.0.0/8", "RFC 6890"),
4940
- ipv4Prefix("10.0.0.0/8", "RFC 1918"),
4941
- ipv4Prefix("100.64.0.0/10", "RFC 6598"),
4942
- ipv4Prefix("127.0.0.0/8", "RFC 1122"),
4943
- ipv4Prefix("169.254.0.0/16", "RFC 3927"),
4944
- ipv4Prefix("172.16.0.0/12", "RFC 1918"),
4945
- ipv4Prefix("192.0.0.0/24", "RFC 6890"),
4946
- ipv4Prefix("192.0.2.0/24", "RFC 5737"),
4947
- ipv4Prefix("192.88.99.0/24", "RFC 7526"),
4948
- ipv4Prefix("192.168.0.0/16", "RFC 1918"),
4949
- ipv4Prefix("198.18.0.0/15", "RFC 2544"),
4950
- ipv4Prefix("198.51.100.0/24", "RFC 5737"),
4951
- ipv4Prefix("203.0.113.0/24", "RFC 5737"),
4952
- ipv4Prefix("224.0.0.0/4", "RFC 5771"),
4953
- ipv4Prefix("240.0.0.0/4", "RFC 1112")
4954
- ];
4955
- const nonPublicIPv6Prefixes = [
4956
- ipv6Prefix("::/16", "RFC 4291"),
4957
- ipv6Prefix("2001::/32", "RFC 4380"),
4958
- ipv6Prefix("2002::/16", "RFC 3056"),
4959
- ipv6Prefix("64:ff9b:1::/48", "RFC 8215"),
4960
- ipv6Prefix("fc00::/7", "RFC 4193"),
4961
- ipv6Prefix("fe80::/10", "RFC 4291"),
4962
- ipv6Prefix("ff00::/8", "RFC 4291")
4963
- ];
4964
- const ipv6WithIPv4Prefixes = [{
4965
- ...ipv6Prefix("64:ff9b::/96", "RFC 6052"),
4966
- extractIPv4: (words) => ipv4FromWords(words[6], words[7])
4967
- }];
4968
- function ipv4Prefix(cidr, rfc) {
4969
- const [address, prefixText] = cidr.split("/");
4970
- const prefix = parseInt(prefixText, 10);
4971
- const parts = parseIPv4Address(address);
4972
- if (parts == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 32) throw new Error(`Invalid IPv4 prefix: ${cidr}`);
4973
- return {
4974
- cidr,
4975
- base: ipv4PartsToNumber(parts),
4976
- prefix,
4977
- rfc
4978
- };
4979
- }
4980
- function ipv6Prefix(cidr, rfc) {
4981
- const [address, prefixText] = cidr.split("/");
4982
- const prefix = parseInt(prefixText, 10);
4983
- const words = parseIPv6Address(address);
4984
- if (words == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 128) throw new Error(`Invalid IPv6 prefix: ${cidr}`);
4985
- return {
4986
- cidr,
4987
- words,
4988
- prefix,
4989
- rfc
4990
- };
4991
- }
4992
- function parseIPv4Address(address) {
4993
- const parts = address.split(".").map((part) => {
4994
- if (!/^\d+$/.test(part)) return NaN;
4995
- return parseInt(part, 10);
4996
- });
4997
- if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) return null;
4998
- return parts;
4999
- }
5000
- function parseIPv6Address(address) {
5001
- const parts = expandIPv6Address(address).split(":");
5002
- if (parts.length !== 8) return null;
5003
- const words = parts.map((part) => {
5004
- if (!/^[0-9a-f]{1,4}$/i.test(part)) return NaN;
5005
- return parseInt(part, 16);
5006
- });
5007
- if (words.some((word) => !Number.isInteger(word) || word < 0 || word > 65535)) return null;
5008
- return words;
5009
- }
5010
- function ipv4PartsToNumber(parts) {
5011
- return parts[0] * 2 ** 24 + parts[1] * 2 ** 16 + parts[2] * 2 ** 8 + parts[3];
5012
- }
5013
- function ipv4FromWords(highWord, lowWord) {
5014
- return [
5015
- highWord >> 8,
5016
- highWord & 255,
5017
- lowWord >> 8,
5018
- lowWord & 255
5019
- ].join(".");
5020
- }
5021
- function matchesIPv4Prefix(address, prefixBase, prefixLength) {
5022
- const blockSize = 2 ** (32 - prefixLength);
5023
- return Math.floor(address / blockSize) === Math.floor(prefixBase / blockSize);
5024
- }
5025
- function matchesIPv6Prefix(address, prefixWords, prefixLength) {
5026
- let remaining = prefixLength;
5027
- for (let i = 0; i < 8 && remaining > 0; i++) if (remaining >= 16) {
5028
- if (address[i] !== prefixWords[i]) return false;
5029
- remaining -= 16;
5030
- } else {
5031
- const mask = 65535 << 16 - remaining & 65535;
5032
- if ((address[i] & mask) !== (prefixWords[i] & mask)) return false;
5033
- remaining = 0;
5034
- }
5035
- return true;
5036
- }
5037
- //#endregion
5038
4826
  //#region src/docloader.ts
5039
4827
  const logger = (0, _logtape_logtape.getLogger)([
5040
4828
  "fedify",
@@ -5046,13 +4834,47 @@ const DEFAULT_MAX_REDIRECTION = 20;
5046
4834
  * Gets a {@link RemoteDocument} from the given response.
5047
4835
  * @param url The URL of the document to load.
5048
4836
  * @param response The response to get the document from.
5049
- * @param fetch The function to fetch the document.
4837
+ * @param fetch The function to fetch the document. It is called with the URL
4838
+ * of an alternate document, if any, after the response body has
4839
+ * been either read or its cancellation has been started.
5050
4840
  * @returns The loaded remote document.
5051
4841
  * @throws {FetchError} If the response is not OK.
5052
4842
  * @internal
5053
4843
  */
5054
4844
  async function getRemoteDocument(url, response, fetch) {
5055
- const documentUrl = response.url === "" ? url : response.url;
4845
+ const result = await parseRemoteDocument(url, response);
4846
+ if ("alternate" in result) return await fetch(result.alternate);
4847
+ return result.document;
4848
+ }
4849
+ /**
4850
+ * Starts cancelling the body of a response without waiting for it to settle.
4851
+ * The cancellation of a branch of a tee (e.g., a body cloned by a fetch mock)
4852
+ * settles only after the other branch is cancelled too, per the Streams
4853
+ * standard, so awaiting it could block forever. Failures are only logged.
4854
+ * @param response The response whose body is cancelled.
4855
+ * @param url The URL of the response, for logging.
4856
+ */
4857
+ function cancelResponseBody(response, url) {
4858
+ response.body?.cancel().catch((error) => {
4859
+ logger.debug("Failed to cancel the response body from {url}: {error}", {
4860
+ url,
4861
+ error
4862
+ });
4863
+ });
4864
+ }
4865
+ /**
4866
+ * Gets a {@link RemoteDocument} from the given response, or the URL of an
4867
+ * alternate document to load instead. When an alternate document is found,
4868
+ * the response body has been either read or its cancellation has been started
4869
+ * (without waiting for it), so that the alternate document can be loaded after
4870
+ * the response's session is closed.
4871
+ * @param url The URL of the document to load.
4872
+ * @param response The response to get the document from.
4873
+ * @returns The loaded remote document, or the alternate document URL.
4874
+ * @throws {FetchError} If the response is not OK.
4875
+ */
4876
+ async function parseRemoteDocument(url, response) {
4877
+ const documentUrl = url;
5056
4878
  const docUrl = new URL(documentUrl);
5057
4879
  if (!response.ok) {
5058
4880
  logger.error("Failed to fetch document: {status} {url} {headers}", {
@@ -5089,7 +4911,8 @@ async function getRemoteDocument(url, response, fetch) {
5089
4911
  alternateUrl: altUri.href,
5090
4912
  url: documentUrl
5091
4913
  });
5092
- return await fetch(altUri.href);
4914
+ cancelResponseBody(response, documentUrl);
4915
+ return { alternate: altUri.href };
5093
4916
  }
5094
4917
  }
5095
4918
  }
@@ -5114,7 +4937,7 @@ async function getRemoteDocument(url, response, fetch) {
5114
4937
  alternateUrl: attribs.href,
5115
4938
  url: documentUrl
5116
4939
  });
5117
- return await fetch(new URL(attribs.href, docUrl).href);
4940
+ return { alternate: new URL(attribs.href, docUrl).href };
5118
4941
  }
5119
4942
  }
5120
4943
  document = JSON.parse(html);
@@ -5124,11 +4947,11 @@ async function getRemoteDocument(url, response, fetch) {
5124
4947
  url: documentUrl,
5125
4948
  headers: Object.fromEntries(response.headers.entries())
5126
4949
  });
5127
- return {
4950
+ return { document: {
5128
4951
  contextUrl,
5129
4952
  document,
5130
4953
  documentUrl
5131
- };
4954
+ } };
5132
4955
  }
5133
4956
  /**
5134
4957
  * Creates a JSON-LD document loader that utilizes the browser's `fetch` API.
@@ -5154,13 +4977,58 @@ async function getRemoteDocument(url, response, fetch) {
5154
4977
  * @returns The document loader.
5155
4978
  * @since 1.3.0
5156
4979
  */
5157
- function getDocumentLoader({ allowPrivateAddress, skipPreloadedContexts, userAgent } = {}) {
4980
+ function getDocumentLoader(options = {}) {
4981
+ return createDocumentLoader(options);
4982
+ }
4983
+ /**
4984
+ * The implementation of {@link getDocumentLoader}, with injectable
4985
+ * dependencies. This is not exported from any entry point.
4986
+ * @param options Options for the document loader.
4987
+ * @param deps The dependencies.
4988
+ * @returns The document loader.
4989
+ */
4990
+ function createDocumentLoader({ allowPrivateAddress, skipPreloadedContexts, userAgent } = {}, deps = {}) {
5158
4991
  const tracer = _opentelemetry_api.trace.getTracerProvider().getTracer(name, version);
4992
+ async function openHop(url, signal) {
4993
+ try {
4994
+ if (deps.openHop != null) return await deps.openHop(url);
4995
+ return allowPrivateAddress ? await require_public_fetch.openDirectFetch(url) : await require_public_fetch.openPublicFetch(url, { signal });
4996
+ } catch (error) {
4997
+ if (error instanceof require_public_fetch.UrlError) if (error.reason === "dns") logger.debug("DNS lookup failed for {url}", {
4998
+ url,
4999
+ error
5000
+ });
5001
+ else logger.error("Disallowed private URL: {url}", {
5002
+ url,
5003
+ error
5004
+ });
5005
+ throw error;
5006
+ }
5007
+ }
5008
+ async function fetchHop(session, url, span, options) {
5009
+ try {
5010
+ const request = createActivityPubRequest(url, { userAgent });
5011
+ logRequest(logger, request);
5012
+ const response = await session.fetch(request, {
5013
+ redirect: "manual",
5014
+ signal: options?.signal
5015
+ });
5016
+ span.setAttribute("http.response.status_code", response.status);
5017
+ if (response.status >= 300 && response.status < 400 && response.headers.has("Location")) {
5018
+ const location = response.headers.get("Location");
5019
+ cancelResponseBody(response, url);
5020
+ return { location };
5021
+ }
5022
+ return await parseRemoteDocument(url, response);
5023
+ } finally {
5024
+ await session.close();
5025
+ }
5026
+ }
5159
5027
  async function load(url, options, redirected = 0, visited = /* @__PURE__ */ new Set()) {
5160
5028
  options?.signal?.throwIfAborted();
5161
5029
  const parsedUrl = new URL(url);
5162
- if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") throw new UrlError(`Unsupported protocol: ${parsedUrl.protocol}`);
5163
- if (parsedUrl.username !== "" || parsedUrl.password !== "") throw new UrlError("URLs with credentials are not allowed");
5030
+ if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") throw new require_public_fetch.UrlError(`Unsupported protocol: ${parsedUrl.protocol}`);
5031
+ if (parsedUrl.username !== "" || parsedUrl.password !== "") throw new require_public_fetch.UrlError("URLs with credentials are not allowed");
5164
5032
  const currentUrl = parsedUrl.href;
5165
5033
  if (!skipPreloadedContexts && currentUrl in preloadedContexts) {
5166
5034
  logger.debug("Using preloaded context: {url}.", { url: currentUrl });
@@ -5170,33 +5038,15 @@ function getDocumentLoader({ allowPrivateAddress, skipPreloadedContexts, userAge
5170
5038
  documentUrl: currentUrl
5171
5039
  };
5172
5040
  }
5173
- if (!allowPrivateAddress) try {
5174
- await validatePublicUrl(currentUrl);
5175
- } catch (error) {
5176
- if (error instanceof UrlError) if (error.reason === "dns") logger.debug("DNS lookup failed for {url}", {
5177
- url: currentUrl,
5178
- error
5179
- });
5180
- else logger.error("Disallowed private URL: {url}", {
5181
- url: currentUrl,
5182
- error
5183
- });
5184
- throw error;
5185
- }
5041
+ const session = await openHop(currentUrl, options?.signal);
5186
5042
  visited.add(currentUrl);
5187
5043
  return await tracer.startActiveSpan("activitypub.fetch_document", {
5188
5044
  kind: _opentelemetry_api.SpanKind.CLIENT,
5189
5045
  attributes: { "url.full": currentUrl }
5190
5046
  }, async (span) => {
5191
5047
  try {
5192
- const request = createActivityPubRequest(currentUrl, { userAgent });
5193
- logRequest(logger, request);
5194
- const response = await fetch(request, {
5195
- redirect: "manual",
5196
- signal: options?.signal
5197
- });
5198
- span.setAttribute("http.response.status_code", response.status);
5199
- if (response.status >= 300 && response.status < 400 && response.headers.has("Location")) {
5048
+ const outcome = await fetchHop(session, currentUrl, span, options);
5049
+ if ("location" in outcome) {
5200
5050
  if (redirected >= DEFAULT_MAX_REDIRECTION) {
5201
5051
  logger.error("Too many redirections ({redirections}) while fetching document.", {
5202
5052
  redirections: redirected + 1,
@@ -5204,7 +5054,7 @@ function getDocumentLoader({ allowPrivateAddress, skipPreloadedContexts, userAge
5204
5054
  });
5205
5055
  throw new FetchError(currentUrl, `Too many redirections (${redirected + 1})`);
5206
5056
  }
5207
- const redirectUrl = new URL(response.headers.get("Location"), response.url === "" ? currentUrl : response.url).href;
5057
+ const redirectUrl = new URL(outcome.location, currentUrl).href;
5208
5058
  span.setAttribute("http.redirect.url", redirectUrl);
5209
5059
  if (visited.has(redirectUrl)) {
5210
5060
  logger.error("Detected a redirect loop while fetching document: {url} -> {redirectUrl}", {
@@ -5215,12 +5065,14 @@ function getDocumentLoader({ allowPrivateAddress, skipPreloadedContexts, userAge
5215
5065
  }
5216
5066
  return await load(redirectUrl, options, redirected + 1, visited);
5217
5067
  }
5218
- const result = await getRemoteDocument(currentUrl, response, async (alternateUrl) => {
5068
+ let result;
5069
+ if ("alternate" in outcome) {
5070
+ const alternateUrl = outcome.alternate;
5219
5071
  options?.signal?.throwIfAborted();
5220
5072
  if (redirected >= DEFAULT_MAX_REDIRECTION) throw new FetchError(currentUrl, `Too many redirections (${redirected + 1})`);
5221
5073
  if (visited.has(alternateUrl)) throw new FetchError(currentUrl, `Redirect loop detected: ${alternateUrl}`);
5222
- return await load(alternateUrl, options, redirected + 1, visited);
5223
- });
5074
+ result = await load(alternateUrl, options, redirected + 1, visited);
5075
+ } else result = outcome.document;
5224
5076
  span.setAttribute("docloader.document_url", result.documentUrl);
5225
5077
  if (result.contextUrl != null) span.setAttribute("docloader.context_url", result.contextUrl);
5226
5078
  return result;
@@ -5809,12 +5661,12 @@ LanguageString.prototype[Symbol.for("nodejs.util.inspect.custom")] = function(_d
5809
5661
  //#endregion
5810
5662
  exports.FetchError = FetchError;
5811
5663
  exports.LanguageString = LanguageString;
5812
- exports.UrlError = UrlError;
5664
+ exports.UrlError = require_public_fetch.UrlError;
5813
5665
  exports.createActivityPubRequest = createActivityPubRequest;
5814
5666
  exports.decodeMultibase = decodeMultibase;
5815
5667
  exports.encodeMultibase = encodeMultibase;
5816
5668
  exports.encodingFromBaseData = encodingFromBaseData;
5817
- exports.expandIPv6Address = expandIPv6Address;
5669
+ exports.expandIPv6Address = require_public_fetch.expandIPv6Address;
5818
5670
  exports.exportMultibaseKey = exportMultibaseKey;
5819
5671
  exports.exportSpki = exportSpki;
5820
5672
  exports.getDocumentLoader = getDocumentLoader;
@@ -5824,9 +5676,9 @@ exports.importMultibaseKey = importMultibaseKey;
5824
5676
  exports.importPem = importPem;
5825
5677
  exports.importPkcs1 = importPkcs1;
5826
5678
  exports.importSpki = importSpki;
5827
- exports.isValidPublicIPv4Address = isValidPublicIPv4Address;
5828
- exports.isValidPublicIPv6Address = isValidPublicIPv6Address;
5679
+ exports.isValidPublicIPv4Address = require_public_fetch.isValidPublicIPv4Address;
5680
+ exports.isValidPublicIPv6Address = require_public_fetch.isValidPublicIPv6Address;
5829
5681
  exports.logRequest = logRequest;
5830
5682
  exports.normalizeLanguageTag = normalizeLanguageTag;
5831
5683
  exports.preloadedContexts = preloadedContexts;
5832
- exports.validatePublicUrl = validatePublicUrl;
5684
+ exports.validatePublicUrl = require_public_fetch.validatePublicUrl;
package/dist/mod.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  /// <reference lib="esnext.temporal" />
2
+ import { c as isValidPublicIPv4Address, l as isValidPublicIPv6Address, o as UrlError, s as expandIPv6Address, u as validatePublicUrl } from "./public-fetch-DBpt3xxf.cjs";
2
3
  import { Logger } from "@logtape/logtape";
3
4
 
4
5
  //#region src/contexts.d.ts
@@ -151,7 +152,9 @@ type AuthenticatedDocumentLoaderFactory = (identity: {
151
152
  * Gets a {@link RemoteDocument} from the given response.
152
153
  * @param url The URL of the document to load.
153
154
  * @param response The response to get the document from.
154
- * @param fetch The function to fetch the document.
155
+ * @param fetch The function to fetch the document. It is called with the URL
156
+ * of an alternate document, if any, after the response body has
157
+ * been either read or its cancellation has been started.
155
158
  * @returns The loaded remote document.
156
159
  * @throws {FetchError} If the response is not OK.
157
160
  * @internal
@@ -191,11 +194,7 @@ interface GetDocumentLoaderOptions extends DocumentLoaderFactoryOptions {
191
194
  * @returns The document loader.
192
195
  * @since 1.3.0
193
196
  */
194
- declare function getDocumentLoader({
195
- allowPrivateAddress,
196
- skipPreloadedContexts,
197
- userAgent
198
- }?: GetDocumentLoaderOptions): DocumentLoader;
197
+ declare function getDocumentLoader(options?: GetDocumentLoaderOptions): DocumentLoader;
199
198
  //#endregion
200
199
  //#region src/key.d.ts
201
200
  /**
@@ -349,25 +348,4 @@ declare function decodeMultibase(data: Uint8Array | string): Uint8Array;
349
348
  */
350
349
  declare function encodingFromBaseData(data: string | Uint8Array): Base;
351
350
  //#endregion
352
- //#region src/url.d.ts
353
- declare class UrlError extends Error {
354
- /**
355
- * The failure category. Defaults to `"disallowed"`.
356
- * `"dns"` includes lookup errors and results with no usable IP addresses.
357
- * @since 2.0.29
358
- */
359
- readonly reason: "dns" | "disallowed";
360
- constructor(message: string, options?: {
361
- cause?: unknown;
362
- reason?: "dns" | "disallowed";
363
- });
364
- }
365
- /**
366
- * Validates a URL to prevent SSRF attacks.
367
- */
368
- declare function validatePublicUrl(url: string): Promise<void>;
369
- declare function isValidPublicIPv4Address(address: string): boolean;
370
- declare function isValidPublicIPv6Address(address: string): boolean;
371
- declare function expandIPv6Address(address: string): string;
372
- //#endregion
373
351
  export { type AuthenticatedDocumentLoaderFactory, type CreateRequestOptions, type DocumentLoader, type DocumentLoaderFactory, type DocumentLoaderFactoryOptions, type DocumentLoaderOptions, FetchError, type GetDocumentLoaderOptions, type GetUserAgentOptions, LanguageString, type RemoteDocument, UrlError, createActivityPubRequest, decodeMultibase, encodeMultibase, encodingFromBaseData, expandIPv6Address, exportMultibaseKey, exportSpki, getDocumentLoader, getRemoteDocument, getUserAgent, importMultibaseKey, importPem, importPkcs1, importSpki, isValidPublicIPv4Address, isValidPublicIPv6Address, logRequest, normalizeLanguageTag, preloadedContexts, validatePublicUrl };
package/dist/mod.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  /// <reference lib="esnext.temporal" />
2
+ import { c as isValidPublicIPv4Address, l as isValidPublicIPv6Address, o as UrlError, s as expandIPv6Address, u as validatePublicUrl } from "./public-fetch-BC-nWJNM.js";
2
3
  import { Logger } from "@logtape/logtape";
4
+
3
5
  //#region src/contexts.d.ts
4
6
  declare const preloadedContexts: Record<string, unknown>;
5
7
  //#endregion
@@ -150,7 +152,9 @@ type AuthenticatedDocumentLoaderFactory = (identity: {
150
152
  * Gets a {@link RemoteDocument} from the given response.
151
153
  * @param url The URL of the document to load.
152
154
  * @param response The response to get the document from.
153
- * @param fetch The function to fetch the document.
155
+ * @param fetch The function to fetch the document. It is called with the URL
156
+ * of an alternate document, if any, after the response body has
157
+ * been either read or its cancellation has been started.
154
158
  * @returns The loaded remote document.
155
159
  * @throws {FetchError} If the response is not OK.
156
160
  * @internal
@@ -190,11 +194,7 @@ interface GetDocumentLoaderOptions extends DocumentLoaderFactoryOptions {
190
194
  * @returns The document loader.
191
195
  * @since 1.3.0
192
196
  */
193
- declare function getDocumentLoader({
194
- allowPrivateAddress,
195
- skipPreloadedContexts,
196
- userAgent
197
- }?: GetDocumentLoaderOptions): DocumentLoader;
197
+ declare function getDocumentLoader(options?: GetDocumentLoaderOptions): DocumentLoader;
198
198
  //#endregion
199
199
  //#region src/key.d.ts
200
200
  /**
@@ -348,25 +348,4 @@ declare function decodeMultibase(data: Uint8Array | string): Uint8Array;
348
348
  */
349
349
  declare function encodingFromBaseData(data: string | Uint8Array): Base;
350
350
  //#endregion
351
- //#region src/url.d.ts
352
- declare class UrlError extends Error {
353
- /**
354
- * The failure category. Defaults to `"disallowed"`.
355
- * `"dns"` includes lookup errors and results with no usable IP addresses.
356
- * @since 2.0.29
357
- */
358
- readonly reason: "dns" | "disallowed";
359
- constructor(message: string, options?: {
360
- cause?: unknown;
361
- reason?: "dns" | "disallowed";
362
- });
363
- }
364
- /**
365
- * Validates a URL to prevent SSRF attacks.
366
- */
367
- declare function validatePublicUrl(url: string): Promise<void>;
368
- declare function isValidPublicIPv4Address(address: string): boolean;
369
- declare function isValidPublicIPv6Address(address: string): boolean;
370
- declare function expandIPv6Address(address: string): string;
371
- //#endregion
372
351
  export { type AuthenticatedDocumentLoaderFactory, type CreateRequestOptions, type DocumentLoader, type DocumentLoaderFactory, type DocumentLoaderFactoryOptions, type DocumentLoaderOptions, FetchError, type GetDocumentLoaderOptions, type GetUserAgentOptions, LanguageString, type RemoteDocument, UrlError, createActivityPubRequest, decodeMultibase, encodeMultibase, encodingFromBaseData, expandIPv6Address, exportMultibaseKey, exportSpki, getDocumentLoader, getRemoteDocument, getUserAgent, importMultibaseKey, importPem, importPkcs1, importSpki, isValidPublicIPv4Address, isValidPublicIPv6Address, logRequest, normalizeLanguageTag, preloadedContexts, validatePublicUrl };