@fedify/vocab-runtime 2.2.18 → 2.2.20

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 (114) 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 -252
  7. package/dist/mod.d.cts +5 -28
  8. package/dist/mod.d.ts +6 -28
  9. package/dist/mod.js +99 -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-CbvYxP3V.mjs → body-B9_hb-GO.mjs} +1 -1
  15. package/dist/tests/{body-CaJBg0tr.cjs → body-pOSG2RFb.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/decimal.test.cjs +6 -6
  19. package/dist/tests/decimal.test.mjs +6 -6
  20. package/dist/tests/{docloader-jQMjHIRI.mjs → docloader-Bdlp7FkK.mjs} +103 -37
  21. package/dist/tests/{docloader-k2T5kUlV.cjs → docloader-Ck3VfY29.cjs} +107 -35
  22. package/dist/tests/docloader.test.cjs +569 -1273
  23. package/dist/tests/docloader.test.mjs +515 -1238
  24. package/dist/tests/esm-CnLt0wAF.mjs +1225 -0
  25. package/dist/tests/esm-CxbqfkLP.cjs +1208 -0
  26. package/dist/tests/internal/multicodec.test.cjs +1 -1
  27. package/dist/tests/internal/multicodec.test.mjs +1 -1
  28. package/dist/tests/{key-BDIsC6hV.mjs → key-GEdpxA4M.mjs} +2 -2
  29. package/dist/tests/{key-BarKGLQz.cjs → key-L6ReMuUb.cjs} +2 -2
  30. package/dist/tests/key.test.cjs +2 -2
  31. package/dist/tests/key.test.mjs +2 -2
  32. package/dist/tests/{langstr-BsqFJupv.cjs → langstr-CZpWmpo5.cjs} +1 -1
  33. package/dist/tests/{langstr-ABdAOKlF.mjs → langstr-DTB4sl0a.mjs} +1 -1
  34. package/dist/tests/langstr.test.cjs +1 -1
  35. package/dist/tests/langstr.test.mjs +1 -1
  36. package/dist/tests/langtag.test.cjs +1 -1
  37. package/dist/tests/langtag.test.mjs +1 -1
  38. package/dist/tests/link.test.mjs +1 -1
  39. package/dist/tests/multibase/multibase.test.cjs +1 -1
  40. package/dist/tests/multibase/multibase.test.mjs +1 -1
  41. package/dist/tests/node-C3bjFjL1.mjs +84 -0
  42. package/dist/tests/node-DWD76vPo.cjs +90 -0
  43. package/dist/tests/pin-grant-CU-o367k.cjs +70 -0
  44. package/dist/tests/pin-grant-HW3B6SKv.mjs +53 -0
  45. package/dist/tests/pin-grant.test.cjs +88 -0
  46. package/dist/tests/pin-grant.test.d.cts +1 -0
  47. package/dist/tests/pin-grant.test.d.mts +1 -0
  48. package/dist/tests/pin-grant.test.mjs +88 -0
  49. package/dist/tests/pinners/bun.test.cjs +167 -0
  50. package/dist/tests/pinners/bun.test.d.cts +1 -0
  51. package/dist/tests/pinners/bun.test.d.mts +1 -0
  52. package/dist/tests/pinners/bun.test.mjs +167 -0
  53. package/dist/tests/pinners/node.test.cjs +269 -0
  54. package/dist/tests/pinners/node.test.d.cts +1 -0
  55. package/dist/tests/pinners/node.test.d.mts +1 -0
  56. package/dist/tests/pinners/node.test.mjs +268 -0
  57. package/dist/tests/pinners/pinners.test.cjs +197 -0
  58. package/dist/tests/pinners/pinners.test.d.cts +1 -0
  59. package/dist/tests/pinners/pinners.test.d.mts +1 -0
  60. package/dist/tests/pinners/pinners.test.mjs +197 -0
  61. package/dist/tests/pinners/testdata/node-child.cjs +135 -0
  62. package/dist/tests/pinners/testdata/node-child.d.cts +1 -0
  63. package/dist/tests/pinners/testdata/node-child.d.mts +1 -0
  64. package/dist/tests/pinners/testdata/node-child.mjs +134 -0
  65. package/dist/tests/pinners-BjE-YJLL.cjs +221 -0
  66. package/dist/tests/pinners-ZgZDx9y6.mjs +203 -0
  67. package/dist/tests/public-fetch-Ddjfxbwc.cjs +187 -0
  68. package/dist/tests/public-fetch-Yi7Vks9s.mjs +170 -0
  69. package/dist/tests/public-fetch.test.cjs +1074 -0
  70. package/dist/tests/public-fetch.test.d.cts +1 -0
  71. package/dist/tests/public-fetch.test.d.mts +1 -0
  72. package/dist/tests/public-fetch.test.mjs +1074 -0
  73. package/dist/tests/{request-BngWcP0t.cjs → request-B3NUKbyk.cjs} +1 -1
  74. package/dist/tests/{request-BE7XIrGw.mjs → request-BSg3IQzf.mjs} +1 -1
  75. package/dist/tests/request.test.cjs +1 -1
  76. package/dist/tests/request.test.mjs +1 -1
  77. package/dist/tests/support-D5-KVjWZ.cjs +363 -0
  78. package/dist/tests/support-M5ClN-no.mjs +272 -0
  79. package/dist/tests/{url-vZTgkIZg.cjs → url-DLuSj57U.cjs} +88 -12
  80. package/dist/tests/{url-3fyJuqC0.mjs → url-DQWrMGhu.mjs} +65 -13
  81. package/dist/tests/url.test.cjs +4 -1
  82. package/dist/tests/url.test.mjs +4 -1
  83. package/package.json +14 -3
  84. package/src/docloader.test.ts +784 -32
  85. package/src/docloader.ts +179 -67
  86. package/src/internal.ts +13 -0
  87. package/src/pin-grant.test.ts +118 -0
  88. package/src/pin-grant.ts +99 -0
  89. package/src/pinners/bun.test.ts +278 -0
  90. package/src/pinners/bun.ts +223 -0
  91. package/src/pinners/mod.ts +59 -0
  92. package/src/pinners/node.test.ts +415 -0
  93. package/src/pinners/node.ts +144 -0
  94. package/src/pinners/pinners.test.ts +316 -0
  95. package/src/pinners/testdata/node-child.ts +147 -0
  96. package/src/pinners/testdata/proxy-child.ts +82 -0
  97. package/src/pinners/testdata/support.ts +343 -0
  98. package/src/pinners/testdata/tls-cert.pem +12 -0
  99. package/src/pinners/testdata/tls-child.ts +88 -0
  100. package/src/pinners/testdata/tls-key.pem +5 -0
  101. package/src/pinners/types.ts +48 -0
  102. package/src/public-fetch.test.ts +1286 -0
  103. package/src/public-fetch.ts +338 -0
  104. package/src/testdata/public-fetch-deno-worker.ts +74 -0
  105. package/src/url.test.ts +11 -0
  106. package/src/url.ts +113 -22
  107. package/tsdown.config.ts +12 -3
  108. /package/dist/tests/{langtag-LDBqlxHv.mjs → langtag-D8FWxjv8.mjs} +0 -0
  109. /package/dist/tests/{langtag-DgySESmk.cjs → langtag-a2-s9uyi.cjs} +0 -0
  110. /package/dist/tests/{link-CYszIegt.mjs → link-CHMtH2-k.mjs} +0 -0
  111. /package/dist/tests/{multibase-8zhYPa7o.cjs → multibase-BmWaK56H.cjs} +0 -0
  112. /package/dist/tests/{multibase-Ds75BvXs.mjs → multibase-D642Uv3S.mjs} +0 -0
  113. /package/dist/tests/{multicodec-ByS9XLsw.cjs → multicodec-B-_CSt1M.cjs} +0 -0
  114. /package/dist/tests/{multicodec-BBPZjRfg.mjs → multicodec-caWdG98t.mjs} +0 -0
package/deno.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@fedify/vocab-runtime",
3
- "version": "2.2.18",
3
+ "version": "2.2.20",
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");
@@ -4593,7 +4591,7 @@ const preloadedContexts = {
4593
4591
  //#endregion
4594
4592
  //#region deno.json
4595
4593
  var name = "@fedify/vocab-runtime";
4596
- var version = "2.2.18";
4594
+ var version = "2.2.20";
4597
4595
  //#endregion
4598
4596
  //#region src/request.ts
4599
4597
  /**
@@ -4920,216 +4918,6 @@ var HttpHeaderLink = class HttpHeaderLink {
4920
4918
  }
4921
4919
  };
4922
4920
  //#endregion
4923
- //#region src/url.ts
4924
- var UrlError = class extends Error {
4925
- /**
4926
- * The failure category. Defaults to `"disallowed"`.
4927
- * `"dns"` includes lookup errors and results with no usable IP addresses.
4928
- * @since 2.0.29
4929
- */
4930
- reason;
4931
- constructor(message, options) {
4932
- super(message, options);
4933
- this.name = "UrlError";
4934
- this.reason = options?.reason ?? "disallowed";
4935
- }
4936
- };
4937
- /**
4938
- * Validates a URL to prevent SSRF attacks.
4939
- */
4940
- async function validatePublicUrl(url) {
4941
- const parsed = new URL(url);
4942
- if (parsed.protocol !== "http:" && parsed.protocol !== "https:") throw new UrlError(`Unsupported protocol: ${parsed.protocol}`);
4943
- let hostname = parsed.hostname;
4944
- if (hostname.startsWith("[") && hostname.endsWith("]")) hostname = hostname.slice(1, -1);
4945
- if (hostname === "localhost") throw new UrlError("Localhost is not allowed");
4946
- const hostnameFamily = (0, node_net.isIP)(hostname);
4947
- if (hostnameFamily !== 0) {
4948
- validatePublicIpAddress(hostname, hostnameFamily);
4949
- return;
4950
- }
4951
- if ("Deno" in globalThis && !(0, node_net.isIP)(hostname)) {
4952
- if ((await Deno.permissions.query({ name: "net" })).state !== "granted") return;
4953
- }
4954
- if ("Bun" in globalThis) {
4955
- if (hostname === "example.com" || hostname.endsWith(".example.com")) return;
4956
- else if (hostname === "fedify-test.internal") throw new UrlError("Invalid or private address: fedify-test.internal");
4957
- }
4958
- let addresses;
4959
- try {
4960
- addresses = await node_dns_promises.default.lookup(hostname, { all: true });
4961
- } catch (error) {
4962
- throw new UrlError("DNS lookup failed", {
4963
- cause: error,
4964
- reason: "dns"
4965
- });
4966
- }
4967
- validateLookupAddresses(addresses);
4968
- }
4969
- /**
4970
- * Validates the IP addresses returned by `node:dns.lookup()`.
4971
- *
4972
- * Cloudflare Workers' `node:dns` implementation currently maps every record
4973
- * in a DNS-over-HTTPS `Answer` array—including CNAME records—to a
4974
- * `LookupAddress`, even though Node.js specifies that the `address` field must
4975
- * contain an IPv4 or IPv6 literal. See:
4976
- * https://github.com/cloudflare/workerd/issues/6886
4977
- *
4978
- * Work around that bug by ignoring non-IP entries only when the lookup also
4979
- * returns at least one actual IP address. This remains fail-closed: a result
4980
- * containing no IP addresses is rejected, and every returned IP address is
4981
- * still validated and must be public. This workaround can be revisited once
4982
- * the Workerd issue is fixed in supported Cloudflare Workers runtimes.
4983
- *
4984
- * @internal
4985
- */
4986
- function validateLookupAddresses(addresses) {
4987
- let ipAddressCount = 0;
4988
- for (const { address } of addresses) {
4989
- const family = (0, node_net.isIP)(address);
4990
- if (family === 0) continue;
4991
- ipAddressCount++;
4992
- validatePublicIpAddress(address, family);
4993
- }
4994
- if (ipAddressCount === 0) throw new UrlError("DNS lookup did not return any IP address", { reason: "dns" });
4995
- }
4996
- function validatePublicIpAddress(address, family) {
4997
- if (family === 4 && isValidPublicIPv4Address(address) || family === 6 && isValidPublicIPv6Address(address)) return;
4998
- throw new UrlError(`Invalid or private address: ${address}`);
4999
- }
5000
- function isValidPublicIPv4Address(address) {
5001
- const parts = parseIPv4Address(address);
5002
- if (parts == null) return false;
5003
- const value = ipv4PartsToNumber(parts);
5004
- return !nonPublicIPv4Prefixes.some(({ base, prefix }) => matchesIPv4Prefix(value, base, prefix));
5005
- }
5006
- function isValidPublicIPv6Address(address) {
5007
- const words = parseIPv6Address(address);
5008
- if (words == null) return false;
5009
- if (nonPublicIPv6Prefixes.some(({ words: prefixWords, prefix }) => matchesIPv6Prefix(words, prefixWords, prefix))) return false;
5010
- for (const { extractIPv4, prefix, words: prefixWords } of ipv6WithIPv4Prefixes) {
5011
- if (!matchesIPv6Prefix(words, prefixWords, prefix)) continue;
5012
- const ipv4Address = extractIPv4(words);
5013
- if (ipv4Address != null && !isValidPublicIPv4Address(ipv4Address)) return false;
5014
- }
5015
- return true;
5016
- }
5017
- function expandIPv6Address(address) {
5018
- address = address.toLowerCase();
5019
- const ipv4Delimiter = address.lastIndexOf(":");
5020
- if (address.includes(".") && ipv4Delimiter >= 0) {
5021
- const ipv4Parts = parseIPv4Address(address.substring(ipv4Delimiter + 1));
5022
- if (ipv4Parts == null) return address;
5023
- const high = (ipv4Parts[0] << 8) + ipv4Parts[1];
5024
- const low = (ipv4Parts[2] << 8) + ipv4Parts[3];
5025
- address = address.substring(0, ipv4Delimiter + 1) + high.toString(16) + ":" + low.toString(16);
5026
- }
5027
- if (address === "::") return "0000:0000:0000:0000:0000:0000:0000:0000";
5028
- if (address.startsWith("::")) address = "0000" + address;
5029
- if (address.endsWith("::")) address = address + "0000";
5030
- address = address.replace("::", ":0000".repeat(8 - (address.match(/:/g) || []).length) + ":");
5031
- return address.split(":").map((part) => part.padStart(4, "0")).join(":");
5032
- }
5033
- const nonPublicIPv4Prefixes = [
5034
- ipv4Prefix("0.0.0.0/8", "RFC 6890"),
5035
- ipv4Prefix("10.0.0.0/8", "RFC 1918"),
5036
- ipv4Prefix("100.64.0.0/10", "RFC 6598"),
5037
- ipv4Prefix("127.0.0.0/8", "RFC 1122"),
5038
- ipv4Prefix("169.254.0.0/16", "RFC 3927"),
5039
- ipv4Prefix("172.16.0.0/12", "RFC 1918"),
5040
- ipv4Prefix("192.0.0.0/24", "RFC 6890"),
5041
- ipv4Prefix("192.0.2.0/24", "RFC 5737"),
5042
- ipv4Prefix("192.88.99.0/24", "RFC 7526"),
5043
- ipv4Prefix("192.168.0.0/16", "RFC 1918"),
5044
- ipv4Prefix("198.18.0.0/15", "RFC 2544"),
5045
- ipv4Prefix("198.51.100.0/24", "RFC 5737"),
5046
- ipv4Prefix("203.0.113.0/24", "RFC 5737"),
5047
- ipv4Prefix("224.0.0.0/4", "RFC 5771"),
5048
- ipv4Prefix("240.0.0.0/4", "RFC 1112")
5049
- ];
5050
- const nonPublicIPv6Prefixes = [
5051
- ipv6Prefix("::/16", "RFC 4291"),
5052
- ipv6Prefix("2001::/32", "RFC 4380"),
5053
- ipv6Prefix("2002::/16", "RFC 3056"),
5054
- ipv6Prefix("64:ff9b:1::/48", "RFC 8215"),
5055
- ipv6Prefix("fc00::/7", "RFC 4193"),
5056
- ipv6Prefix("fe80::/10", "RFC 4291"),
5057
- ipv6Prefix("ff00::/8", "RFC 4291")
5058
- ];
5059
- const ipv6WithIPv4Prefixes = [{
5060
- ...ipv6Prefix("64:ff9b::/96", "RFC 6052"),
5061
- extractIPv4: (words) => ipv4FromWords(words[6], words[7])
5062
- }];
5063
- function ipv4Prefix(cidr, rfc) {
5064
- const [address, prefixText] = cidr.split("/");
5065
- const prefix = parseInt(prefixText, 10);
5066
- const parts = parseIPv4Address(address);
5067
- if (parts == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 32) throw new Error(`Invalid IPv4 prefix: ${cidr}`);
5068
- return {
5069
- cidr,
5070
- base: ipv4PartsToNumber(parts),
5071
- prefix,
5072
- rfc
5073
- };
5074
- }
5075
- function ipv6Prefix(cidr, rfc) {
5076
- const [address, prefixText] = cidr.split("/");
5077
- const prefix = parseInt(prefixText, 10);
5078
- const words = parseIPv6Address(address);
5079
- if (words == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 128) throw new Error(`Invalid IPv6 prefix: ${cidr}`);
5080
- return {
5081
- cidr,
5082
- words,
5083
- prefix,
5084
- rfc
5085
- };
5086
- }
5087
- function parseIPv4Address(address) {
5088
- const parts = address.split(".").map((part) => {
5089
- if (!/^\d+$/.test(part)) return NaN;
5090
- return parseInt(part, 10);
5091
- });
5092
- if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) return null;
5093
- return parts;
5094
- }
5095
- function parseIPv6Address(address) {
5096
- const parts = expandIPv6Address(address).split(":");
5097
- if (parts.length !== 8) return null;
5098
- const words = parts.map((part) => {
5099
- if (!/^[0-9a-f]{1,4}$/i.test(part)) return NaN;
5100
- return parseInt(part, 16);
5101
- });
5102
- if (words.some((word) => !Number.isInteger(word) || word < 0 || word > 65535)) return null;
5103
- return words;
5104
- }
5105
- function ipv4PartsToNumber(parts) {
5106
- return parts[0] * 2 ** 24 + parts[1] * 2 ** 16 + parts[2] * 2 ** 8 + parts[3];
5107
- }
5108
- function ipv4FromWords(highWord, lowWord) {
5109
- return [
5110
- highWord >> 8,
5111
- highWord & 255,
5112
- lowWord >> 8,
5113
- lowWord & 255
5114
- ].join(".");
5115
- }
5116
- function matchesIPv4Prefix(address, prefixBase, prefixLength) {
5117
- const blockSize = 2 ** (32 - prefixLength);
5118
- return Math.floor(address / blockSize) === Math.floor(prefixBase / blockSize);
5119
- }
5120
- function matchesIPv6Prefix(address, prefixWords, prefixLength) {
5121
- let remaining = prefixLength;
5122
- for (let i = 0; i < 8 && remaining > 0; i++) if (remaining >= 16) {
5123
- if (address[i] !== prefixWords[i]) return false;
5124
- remaining -= 16;
5125
- } else {
5126
- const mask = 65535 << 16 - remaining & 65535;
5127
- if ((address[i] & mask) !== (prefixWords[i] & mask)) return false;
5128
- remaining = 0;
5129
- }
5130
- return true;
5131
- }
5132
- //#endregion
5133
4921
  //#region src/docloader.ts
5134
4922
  const logger = (0, _logtape_logtape.getLogger)([
5135
4923
  "fedify",
@@ -5141,13 +4929,47 @@ const DEFAULT_MAX_REDIRECTION = 20;
5141
4929
  * Gets a {@link RemoteDocument} from the given response.
5142
4930
  * @param url The URL of the document to load.
5143
4931
  * @param response The response to get the document from.
5144
- * @param fetch The function to fetch the document.
4932
+ * @param fetch The function to fetch the document. It is called with the URL
4933
+ * of an alternate document, if any, after the response body has
4934
+ * been either read or its cancellation has been started.
5145
4935
  * @returns The loaded remote document.
5146
4936
  * @throws {FetchError} If the response is not OK.
5147
4937
  * @internal
5148
4938
  */
5149
4939
  async function getRemoteDocument(url, response, fetch) {
5150
- const documentUrl = response.url === "" ? url : response.url;
4940
+ const result = await parseRemoteDocument(url, response);
4941
+ if ("alternate" in result) return await fetch(result.alternate);
4942
+ return result.document;
4943
+ }
4944
+ /**
4945
+ * Starts cancelling the body of a response without waiting for it to settle.
4946
+ * The cancellation of a branch of a tee (e.g., a body cloned by a fetch mock)
4947
+ * settles only after the other branch is cancelled too, per the Streams
4948
+ * standard, so awaiting it could block forever. Failures are only logged.
4949
+ * @param response The response whose body is cancelled.
4950
+ * @param url The URL of the response, for logging.
4951
+ */
4952
+ function cancelResponseBody(response, url) {
4953
+ response.body?.cancel().catch((error) => {
4954
+ logger.debug("Failed to cancel the response body from {url}: {error}", {
4955
+ url,
4956
+ error
4957
+ });
4958
+ });
4959
+ }
4960
+ /**
4961
+ * Gets a {@link RemoteDocument} from the given response, or the URL of an
4962
+ * alternate document to load instead. When an alternate document is found,
4963
+ * the response body has been either read or its cancellation has been started
4964
+ * (without waiting for it), so that the alternate document can be loaded after
4965
+ * the response's session is closed.
4966
+ * @param url The URL of the document to load.
4967
+ * @param response The response to get the document from.
4968
+ * @returns The loaded remote document, or the alternate document URL.
4969
+ * @throws {FetchError} If the response is not OK.
4970
+ */
4971
+ async function parseRemoteDocument(url, response) {
4972
+ const documentUrl = url;
5151
4973
  const docUrl = new URL(documentUrl);
5152
4974
  if (!response.ok) {
5153
4975
  logger.error("Failed to fetch document: {status} {url} {headers}", {
@@ -5184,7 +5006,8 @@ async function getRemoteDocument(url, response, fetch) {
5184
5006
  alternateUrl: altUri.href,
5185
5007
  url: documentUrl
5186
5008
  });
5187
- return await fetch(altUri.href);
5009
+ cancelResponseBody(response, documentUrl);
5010
+ return { alternate: altUri.href };
5188
5011
  }
5189
5012
  }
5190
5013
  }
@@ -5209,7 +5032,7 @@ async function getRemoteDocument(url, response, fetch) {
5209
5032
  alternateUrl: attribs.href,
5210
5033
  url: documentUrl
5211
5034
  });
5212
- return await fetch(new URL(attribs.href, docUrl).href);
5035
+ return { alternate: new URL(attribs.href, docUrl).href };
5213
5036
  }
5214
5037
  }
5215
5038
  document = JSON.parse(html);
@@ -5219,11 +5042,11 @@ async function getRemoteDocument(url, response, fetch) {
5219
5042
  url: documentUrl,
5220
5043
  headers: Object.fromEntries(response.headers.entries())
5221
5044
  });
5222
- return {
5045
+ return { document: {
5223
5046
  contextUrl,
5224
5047
  document,
5225
5048
  documentUrl
5226
- };
5049
+ } };
5227
5050
  }
5228
5051
  /**
5229
5052
  * Creates a JSON-LD document loader that utilizes the browser's `fetch` API.
@@ -5249,13 +5072,59 @@ async function getRemoteDocument(url, response, fetch) {
5249
5072
  * @returns The document loader.
5250
5073
  * @since 1.3.0
5251
5074
  */
5252
- function getDocumentLoader({ allowPrivateAddress, maxRedirection, skipPreloadedContexts, userAgent } = {}) {
5075
+ function getDocumentLoader(options = {}) {
5076
+ return createDocumentLoader(options);
5077
+ }
5078
+ /**
5079
+ * The implementation of {@link getDocumentLoader}, with injectable
5080
+ * dependencies. This is not exported from any entry point.
5081
+ * @param options Options for the document loader.
5082
+ * @param deps The dependencies.
5083
+ * @returns The document loader.
5084
+ */
5085
+ function createDocumentLoader({ allowPrivateAddress, maxRedirection, skipPreloadedContexts, userAgent } = {}, deps = {}) {
5253
5086
  const tracer = _opentelemetry_api.trace.getTracerProvider().getTracer(name, version);
5254
5087
  const maximumRedirection = maxRedirection ?? DEFAULT_MAX_REDIRECTION;
5088
+ async function openHop(url, signal) {
5089
+ try {
5090
+ if (deps.openHop != null) return await deps.openHop(url);
5091
+ return allowPrivateAddress ? await require_public_fetch.openDirectFetch(url) : await require_public_fetch.openPublicFetch(url, { signal });
5092
+ } catch (error) {
5093
+ if (error instanceof require_public_fetch.UrlError) if (error.reason === "dns") logger.debug("DNS lookup failed for {url}", {
5094
+ url,
5095
+ error
5096
+ });
5097
+ else logger.error("Disallowed private URL: {url}", {
5098
+ url,
5099
+ error
5100
+ });
5101
+ throw error;
5102
+ }
5103
+ }
5104
+ async function fetchHop(session, url, span, options) {
5105
+ try {
5106
+ const request = createActivityPubRequest(url, { userAgent });
5107
+ logRequest(logger, request);
5108
+ const response = await session.fetch(request, {
5109
+ redirect: "manual",
5110
+ signal: options?.signal
5111
+ });
5112
+ span.setAttribute("http.response.status_code", response.status);
5113
+ if (response.status >= 300 && response.status < 400 && response.headers.has("Location")) {
5114
+ const location = response.headers.get("Location");
5115
+ cancelResponseBody(response, url);
5116
+ return { location };
5117
+ }
5118
+ return await parseRemoteDocument(url, response);
5119
+ } finally {
5120
+ await session.close();
5121
+ }
5122
+ }
5255
5123
  async function load(url, options, redirected = 0, visited = /* @__PURE__ */ new Set()) {
5256
5124
  options?.signal?.throwIfAborted();
5257
5125
  const parsedUrl = new URL(url);
5258
- if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") throw new UrlError(`Unsupported protocol: ${parsedUrl.protocol}`);
5126
+ if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") throw new require_public_fetch.UrlError(`Unsupported protocol: ${parsedUrl.protocol}`);
5127
+ if (parsedUrl.username !== "" || parsedUrl.password !== "") throw new require_public_fetch.UrlError("URLs with credentials are not allowed");
5259
5128
  const currentUrl = parsedUrl.href;
5260
5129
  if (!skipPreloadedContexts && currentUrl in preloadedContexts) {
5261
5130
  logger.debug("Using preloaded context: {url}.", { url: currentUrl });
@@ -5265,33 +5134,15 @@ function getDocumentLoader({ allowPrivateAddress, maxRedirection, skipPreloadedC
5265
5134
  documentUrl: currentUrl
5266
5135
  };
5267
5136
  }
5268
- if (!allowPrivateAddress) try {
5269
- await validatePublicUrl(currentUrl);
5270
- } catch (error) {
5271
- if (error instanceof UrlError) if (error.reason === "dns") logger.debug("DNS lookup failed for {url}", {
5272
- url: currentUrl,
5273
- error
5274
- });
5275
- else logger.error("Disallowed private URL: {url}", {
5276
- url: currentUrl,
5277
- error
5278
- });
5279
- throw error;
5280
- }
5137
+ const session = await openHop(currentUrl, options?.signal);
5281
5138
  visited.add(currentUrl);
5282
5139
  return await tracer.startActiveSpan("activitypub.fetch_document", {
5283
5140
  kind: _opentelemetry_api.SpanKind.CLIENT,
5284
5141
  attributes: { "url.full": currentUrl }
5285
5142
  }, async (span) => {
5286
5143
  try {
5287
- const request = createActivityPubRequest(currentUrl, { userAgent });
5288
- logRequest(logger, request);
5289
- const response = await fetch(request, {
5290
- redirect: "manual",
5291
- signal: options?.signal
5292
- });
5293
- span.setAttribute("http.response.status_code", response.status);
5294
- if (response.status >= 300 && response.status < 400 && response.headers.has("Location")) {
5144
+ const outcome = await fetchHop(session, currentUrl, span, options);
5145
+ if ("location" in outcome) {
5295
5146
  if (redirected >= maximumRedirection) {
5296
5147
  logger.error("Too many redirections ({redirections}) while fetching document.", {
5297
5148
  redirections: redirected + 1,
@@ -5299,7 +5150,7 @@ function getDocumentLoader({ allowPrivateAddress, maxRedirection, skipPreloadedC
5299
5150
  });
5300
5151
  throw new FetchError(currentUrl, `Too many redirections (${redirected + 1})`);
5301
5152
  }
5302
- const redirectUrl = new URL(response.headers.get("Location"), response.url === "" ? currentUrl : response.url).href;
5153
+ const redirectUrl = new URL(outcome.location, currentUrl).href;
5303
5154
  span.setAttribute("http.redirect.url", redirectUrl);
5304
5155
  if (visited.has(redirectUrl)) {
5305
5156
  logger.error("Detected a redirect loop while fetching document: {url} -> {redirectUrl}", {
@@ -5310,12 +5161,14 @@ function getDocumentLoader({ allowPrivateAddress, maxRedirection, skipPreloadedC
5310
5161
  }
5311
5162
  return await load(redirectUrl, options, redirected + 1, visited);
5312
5163
  }
5313
- const result = await getRemoteDocument(currentUrl, response, async (alternateUrl) => {
5164
+ let result;
5165
+ if ("alternate" in outcome) {
5166
+ const alternateUrl = outcome.alternate;
5314
5167
  options?.signal?.throwIfAborted();
5315
5168
  if (redirected >= DEFAULT_MAX_REDIRECTION) throw new FetchError(currentUrl, `Too many redirections (${redirected + 1})`);
5316
5169
  if (visited.has(alternateUrl)) throw new FetchError(currentUrl, `Redirect loop detected: ${alternateUrl}`);
5317
- return await load(alternateUrl, options, redirected + 1, visited);
5318
- });
5170
+ result = await load(alternateUrl, options, redirected + 1, visited);
5171
+ } else result = outcome.document;
5319
5172
  span.setAttribute("docloader.document_url", result.documentUrl);
5320
5173
  if (result.contextUrl != null) span.setAttribute("docloader.context_url", result.contextUrl);
5321
5174
  return result;
@@ -5979,13 +5832,13 @@ LanguageString.prototype[Symbol.for("nodejs.util.inspect.custom")] = function(_d
5979
5832
  //#endregion
5980
5833
  exports.FetchError = FetchError;
5981
5834
  exports.LanguageString = LanguageString;
5982
- exports.UrlError = UrlError;
5835
+ exports.UrlError = require_public_fetch.UrlError;
5983
5836
  exports.canParseDecimal = canParseDecimal;
5984
5837
  exports.createActivityPubRequest = createActivityPubRequest;
5985
5838
  exports.decodeMultibase = decodeMultibase;
5986
5839
  exports.encodeMultibase = encodeMultibase;
5987
5840
  exports.encodingFromBaseData = encodingFromBaseData;
5988
- exports.expandIPv6Address = expandIPv6Address;
5841
+ exports.expandIPv6Address = require_public_fetch.expandIPv6Address;
5989
5842
  exports.exportMultibaseKey = exportMultibaseKey;
5990
5843
  exports.exportSpki = exportSpki;
5991
5844
  exports.getDocumentLoader = getDocumentLoader;
@@ -5996,10 +5849,10 @@ exports.importPem = importPem;
5996
5849
  exports.importPkcs1 = importPkcs1;
5997
5850
  exports.importSpki = importSpki;
5998
5851
  exports.isDecimal = isDecimal;
5999
- exports.isValidPublicIPv4Address = isValidPublicIPv4Address;
6000
- exports.isValidPublicIPv6Address = isValidPublicIPv6Address;
5852
+ exports.isValidPublicIPv4Address = require_public_fetch.isValidPublicIPv4Address;
5853
+ exports.isValidPublicIPv6Address = require_public_fetch.isValidPublicIPv6Address;
6001
5854
  exports.logRequest = logRequest;
6002
5855
  exports.normalizeLanguageTag = normalizeLanguageTag;
6003
5856
  exports.parseDecimal = parseDecimal;
6004
5857
  exports.preloadedContexts = preloadedContexts;
6005
- exports.validatePublicUrl = validatePublicUrl;
5858
+ 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
@@ -162,7 +163,9 @@ type AuthenticatedDocumentLoaderFactory = (identity: {
162
163
  * Gets a {@link RemoteDocument} from the given response.
163
164
  * @param url The URL of the document to load.
164
165
  * @param response The response to get the document from.
165
- * @param fetch The function to fetch the document.
166
+ * @param fetch The function to fetch the document. It is called with the URL
167
+ * of an alternate document, if any, after the response body has
168
+ * been either read or its cancellation has been started.
166
169
  * @returns The loaded remote document.
167
170
  * @throws {FetchError} If the response is not OK.
168
171
  * @internal
@@ -202,12 +205,7 @@ interface GetDocumentLoaderOptions extends DocumentLoaderFactoryOptions {
202
205
  * @returns The document loader.
203
206
  * @since 1.3.0
204
207
  */
205
- declare function getDocumentLoader({
206
- allowPrivateAddress,
207
- maxRedirection,
208
- skipPreloadedContexts,
209
- userAgent
210
- }?: GetDocumentLoaderOptions): DocumentLoader;
208
+ declare function getDocumentLoader(options?: GetDocumentLoaderOptions): DocumentLoader;
211
209
  //#endregion
212
210
  //#region src/key.d.ts
213
211
  /**
@@ -455,25 +453,4 @@ declare function decodeMultibase(data: Uint8Array | string): Uint8Array;
455
453
  */
456
454
  declare function encodingFromBaseData(data: string | Uint8Array): Base;
457
455
  //#endregion
458
- //#region src/url.d.ts
459
- declare class UrlError extends Error {
460
- /**
461
- * The failure category. Defaults to `"disallowed"`.
462
- * `"dns"` includes lookup errors and results with no usable IP addresses.
463
- * @since 2.0.29
464
- */
465
- readonly reason: "dns" | "disallowed";
466
- constructor(message: string, options?: {
467
- cause?: unknown;
468
- reason?: "dns" | "disallowed";
469
- });
470
- }
471
- /**
472
- * Validates a URL to prevent SSRF attacks.
473
- */
474
- declare function validatePublicUrl(url: string): Promise<void>;
475
- declare function isValidPublicIPv4Address(address: string): boolean;
476
- declare function isValidPublicIPv6Address(address: string): boolean;
477
- declare function expandIPv6Address(address: string): string;
478
- //#endregion
479
456
  export { type AuthenticatedDocumentLoaderFactory, type CreateRequestOptions, type Decimal, type DocumentLoader, type DocumentLoaderFactory, type DocumentLoaderFactoryOptions, type DocumentLoaderOptions, FetchError, type GetDocumentLoaderOptions, type GetUserAgentOptions, LanguageString, type RemoteDocument, UrlError, canParseDecimal, createActivityPubRequest, decodeMultibase, encodeMultibase, encodingFromBaseData, expandIPv6Address, exportMultibaseKey, exportSpki, getDocumentLoader, getRemoteDocument, getUserAgent, importMultibaseKey, importPem, importPkcs1, importSpki, isDecimal, isValidPublicIPv4Address, isValidPublicIPv6Address, logRequest, normalizeLanguageTag, parseDecimal, 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
@@ -161,7 +163,9 @@ type AuthenticatedDocumentLoaderFactory = (identity: {
161
163
  * Gets a {@link RemoteDocument} from the given response.
162
164
  * @param url The URL of the document to load.
163
165
  * @param response The response to get the document from.
164
- * @param fetch The function to fetch the document.
166
+ * @param fetch The function to fetch the document. It is called with the URL
167
+ * of an alternate document, if any, after the response body has
168
+ * been either read or its cancellation has been started.
165
169
  * @returns The loaded remote document.
166
170
  * @throws {FetchError} If the response is not OK.
167
171
  * @internal
@@ -201,12 +205,7 @@ interface GetDocumentLoaderOptions extends DocumentLoaderFactoryOptions {
201
205
  * @returns The document loader.
202
206
  * @since 1.3.0
203
207
  */
204
- declare function getDocumentLoader({
205
- allowPrivateAddress,
206
- maxRedirection,
207
- skipPreloadedContexts,
208
- userAgent
209
- }?: GetDocumentLoaderOptions): DocumentLoader;
208
+ declare function getDocumentLoader(options?: GetDocumentLoaderOptions): DocumentLoader;
210
209
  //#endregion
211
210
  //#region src/key.d.ts
212
211
  /**
@@ -454,25 +453,4 @@ declare function decodeMultibase(data: Uint8Array | string): Uint8Array;
454
453
  */
455
454
  declare function encodingFromBaseData(data: string | Uint8Array): Base;
456
455
  //#endregion
457
- //#region src/url.d.ts
458
- declare class UrlError extends Error {
459
- /**
460
- * The failure category. Defaults to `"disallowed"`.
461
- * `"dns"` includes lookup errors and results with no usable IP addresses.
462
- * @since 2.0.29
463
- */
464
- readonly reason: "dns" | "disallowed";
465
- constructor(message: string, options?: {
466
- cause?: unknown;
467
- reason?: "dns" | "disallowed";
468
- });
469
- }
470
- /**
471
- * Validates a URL to prevent SSRF attacks.
472
- */
473
- declare function validatePublicUrl(url: string): Promise<void>;
474
- declare function isValidPublicIPv4Address(address: string): boolean;
475
- declare function isValidPublicIPv6Address(address: string): boolean;
476
- declare function expandIPv6Address(address: string): string;
477
- //#endregion
478
456
  export { type AuthenticatedDocumentLoaderFactory, type CreateRequestOptions, type Decimal, type DocumentLoader, type DocumentLoaderFactory, type DocumentLoaderFactoryOptions, type DocumentLoaderOptions, FetchError, type GetDocumentLoaderOptions, type GetUserAgentOptions, LanguageString, type RemoteDocument, UrlError, canParseDecimal, createActivityPubRequest, decodeMultibase, encodeMultibase, encodingFromBaseData, expandIPv6Address, exportMultibaseKey, exportSpki, getDocumentLoader, getRemoteDocument, getUserAgent, importMultibaseKey, importPem, importPkcs1, importSpki, isDecimal, isValidPublicIPv4Address, isValidPublicIPv6Address, logRequest, normalizeLanguageTag, parseDecimal, preloadedContexts, validatePublicUrl };