@fedify/vocab-runtime 2.2.19 → 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 -253
  7. package/dist/mod.d.cts +5 -28
  8. package/dist/mod.d.ts +6 -28
  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-BOnyCa1y.mjs → body-B9_hb-GO.mjs} +1 -1
  15. package/dist/tests/{body-DhbRZ6Ps.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-BjyFsnAA.mjs → docloader-Bdlp7FkK.mjs} +102 -37
  21. package/dist/tests/{docloader-BhSiTajy.cjs → docloader-Ck3VfY29.cjs} +106 -35
  22. package/dist/tests/docloader.test.cjs +523 -1273
  23. package/dist/tests/docloader.test.mjs +469 -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-DGtx5MkG.cjs → request-B3NUKbyk.cjs} +1 -1
  74. package/dist/tests/{request-yCis9VCB.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 +700 -32
  85. package/src/docloader.ts +174 -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
@@ -1,17 +1,50 @@
1
1
  import { configure, type LogRecord, reset } from "@logtape/logtape";
2
2
  import fetchMock from "fetch-mock";
3
3
  import { deepStrictEqual, ok, rejects } from "node:assert";
4
- import dns from "node:dns/promises";
4
+ import type { LookupAddress } from "node:dns";
5
5
  import { test } from "@fedify/fixture";
6
6
  import { createServer } from "node:http";
7
7
  import { gzipSync } from "node:zlib";
8
8
  import preloadedContexts from "./contexts.ts";
9
9
  import cidV1Context from "./contexts/cid-v1.json" with { type: "json" };
10
- import { getDocumentLoader, getRemoteDocument } from "./docloader.ts";
10
+ import {
11
+ createDocumentLoader,
12
+ getDocumentLoader,
13
+ getRemoteDocument,
14
+ type RemoteDocument,
15
+ } from "./docloader.ts";
11
16
  import jsonld from "./jsonld.ts";
17
+ import type { ConnectionPinner } from "./pinners/mod.ts";
18
+ import { createPublicFetch, type PublicFetchSession } from "./public-fetch.ts";
12
19
  import { FetchError } from "./request.ts";
13
20
  import { UrlError } from "./url.ts";
14
21
 
22
+ // Validates addresses like openPublicFetch() but sends requests through the
23
+ // global fetch() without pinning, so that fetch-mock's hostname routes match
24
+ // on every runtime (the Bun pinner would substitute IP addresses). The lookup
25
+ // is always injected, so tests do not depend on the network's DNS.
26
+ const unpinnedPublicFetch = createPublicFetch({
27
+ pinner: () => Promise.resolve(null),
28
+ lookup: () => Promise.reject(new Error("Unexpected default lookup")),
29
+ });
30
+
31
+ const PUBLIC_ADDRESS: LookupAddress = { address: "93.184.215.14", family: 4 };
32
+ const PRIVATE_ADDRESS: LookupAddress = { address: "10.0.0.1", family: 4 };
33
+
34
+ function lookupTestAddresses(
35
+ hostname: string,
36
+ ): Promise<readonly LookupAddress[]> {
37
+ return Promise.resolve([
38
+ hostname === "private.example" ? PRIVATE_ADDRESS : PUBLIC_ADDRESS,
39
+ ]);
40
+ }
41
+
42
+ function openUnpinnedHop(url: string): Promise<PublicFetchSession> {
43
+ return unpinnedPublicFetch.openPublicFetch(url, {
44
+ lookup: lookupTestAddresses,
45
+ });
46
+ }
47
+
15
48
  test("new FetchError()", () => {
16
49
  const e = new FetchError("https://example.com/", "An error message.");
17
50
  deepStrictEqual(e.name, "FetchError");
@@ -32,7 +65,9 @@ test("new FetchError()", () => {
32
65
  });
33
66
 
34
67
  test("getDocumentLoader()", async (t) => {
35
- const fetchDocumentLoader = getDocumentLoader();
68
+ const fetchDocumentLoader = createDocumentLoader({}, {
69
+ openHop: openUnpinnedHop,
70
+ });
36
71
 
37
72
  fetchMock.spyGlobal();
38
73
 
@@ -557,6 +592,19 @@ test("getDocumentLoader()", async (t) => {
557
592
  },
558
593
  });
559
594
 
595
+ fetchMock.get("https://example.com/private-redirect", {
596
+ status: 302,
597
+ headers: { Location: "https://private.example/object" },
598
+ });
599
+
600
+ fetchMock.get("https://private.example/object", {
601
+ body: {
602
+ "@context": "https://www.w3.org/ns/activitystreams",
603
+ id: "https://private.example/object",
604
+ type: "Object",
605
+ },
606
+ });
607
+
560
608
  await t.step("allowPrivateAddress: false", async () => {
561
609
  await rejects(
562
610
  () => fetchDocumentLoader("https://localhost/object"),
@@ -570,6 +618,26 @@ test("getDocumentLoader()", async (t) => {
570
618
  () => fetchDocumentLoader("https://example.com/localhost-link"),
571
619
  UrlError,
572
620
  );
621
+ await rejects(
622
+ () => fetchDocumentLoader("https://private.example/object"),
623
+ UrlError,
624
+ );
625
+ await rejects(
626
+ () => fetchDocumentLoader("https://example.com/private-redirect"),
627
+ UrlError,
628
+ );
629
+ deepStrictEqual(
630
+ fetchMock.callHistory.calls("https://private.example/object").length,
631
+ 0,
632
+ );
633
+ // The default loader rejects these without resolving any hostname:
634
+ const defaultLoader = getDocumentLoader();
635
+ await rejects(() => defaultLoader("https://localhost/object"), UrlError);
636
+ await rejects(() => defaultLoader("https://127.0.0.1/object"), UrlError);
637
+ deepStrictEqual(
638
+ fetchMock.callHistory.calls("https://localhost/object").length,
639
+ 0,
640
+ );
573
641
  });
574
642
 
575
643
  const fetchDocumentLoader2 = getDocumentLoader({ allowPrivateAddress: true });
@@ -623,7 +691,10 @@ test("getDocumentLoader()", async (t) => {
623
691
 
624
692
  await t.step("custom max redirection", async () => {
625
693
  redirectAttempts = 0;
626
- const loader = getDocumentLoader({ maxRedirection: 1 });
694
+ const loader = createDocumentLoader(
695
+ { maxRedirection: 1 },
696
+ { openHop: openUnpinnedHop },
697
+ );
627
698
  await rejects(
628
699
  () => loader("https://example.com/too-many-redirects/0"),
629
700
  FetchError,
@@ -830,33 +901,24 @@ test("getRemoteDocument() bounds JSON by default", async () => {
830
901
  });
831
902
 
832
903
  test("getDocumentLoader() logs DNS failures as such", async (t) => {
833
- // The validator skips DNS when Deno has no network permission. Checked
834
- // here rather than with top-level await, which the CommonJS build rejects.
835
- if (
836
- "Deno" in globalThis &&
837
- (await Deno.permissions.query({ name: "net" })).state !== "granted"
838
- ) {
839
- throw new Error(
840
- "This DNS test requires Deno network permission (--allow-net).",
841
- );
842
- }
843
- const loader = getDocumentLoader();
844
- for (const result of ["throws", "empty", "private"] as const) {
904
+ for (const result of ["throws", "empty", "private", "default"] as const) {
845
905
  await t.step(result, async () => {
846
- // Stubbing works only because url.ts uses the default node:dns/promises
847
- // import; see the FIXME there.
848
- const originalLookup = dns.lookup;
849
- dns.lookup = (() =>
906
+ const lookup = (): Promise<readonly LookupAddress[]> =>
850
907
  result === "throws"
851
908
  ? Promise.reject(new Error("Resolver unavailable"))
852
- : Promise.resolve(
853
- result === "empty" ? [] : [{ address: "127.0.0.1", family: 4 }],
854
- )) as typeof dns.lookup;
909
+ : Promise.resolve(result === "empty" ? [] : [PRIVATE_ADDRESS]);
910
+ // The default loader rejects IP literals without resolving them, so
911
+ // the "default" step checks that its logging matches the injected one:
912
+ const loader = result === "default"
913
+ ? getDocumentLoader()
914
+ : createDocumentLoader({}, {
915
+ openHop: (url) =>
916
+ unpinnedPublicFetch.openPublicFetch(url, { lookup }),
917
+ });
855
918
  const records: LogRecord[] = [];
856
919
  await configure({
857
920
  sinks: {
858
- buffer: (record) =>
859
- records.push(record),
921
+ buffer: (record) => records.push(record),
860
922
  },
861
923
  loggers: [
862
924
  { category: "fedify", sinks: ["buffer"], lowestLevel: "debug" },
@@ -865,21 +927,21 @@ test("getDocumentLoader() logs DNS failures as such", async (t) => {
865
927
  reset: true,
866
928
  });
867
929
  try {
868
- const url = "https://dns-failure.invalid/object";
930
+ const url = result === "default"
931
+ ? "https://10.0.0.1/object"
932
+ : "https://dns-failure.invalid/object";
869
933
  let error: unknown;
870
934
  await rejects(() => loader(url), (e) => {
871
935
  error = e;
872
936
  return true;
873
937
  });
874
938
  ok(error instanceof UrlError);
875
- deepStrictEqual(
876
- error.reason,
877
- result === "private" ? "disallowed" : "dns",
878
- );
939
+ const disallowed = result === "private" || result === "default";
940
+ deepStrictEqual(error.reason, disallowed ? "disallowed" : "dns");
879
941
  deepStrictEqual(
880
942
  records.map((r) => [r.level, r.rawMessage, r.properties.url]),
881
943
  [
882
- result === "private"
944
+ disallowed
883
945
  ? ["error", "Disallowed private URL: {url}", url]
884
946
  : ["debug", "DNS lookup failed for {url}", url],
885
947
  ],
@@ -887,7 +949,6 @@ test("getDocumentLoader() logs DNS failures as such", async (t) => {
887
949
  ok(records[0].properties.error === error);
888
950
  } finally {
889
951
  await reset();
890
- dns.lookup = originalLookup;
891
952
  }
892
953
  });
893
954
  }
@@ -1277,3 +1338,610 @@ for (const url of unsupportedUrls) {
1277
1338
  );
1278
1339
  }
1279
1340
  }
1341
+
1342
+ interface RecordedResponseInit {
1343
+ readonly status?: number;
1344
+ readonly headers?: Record<string, string>;
1345
+ readonly body?: string;
1346
+ /** Whether cancelling the body fails. */
1347
+ readonly failCancel?: boolean;
1348
+ /** Whether cancelling the body never settles, like a tee branch's. */
1349
+ readonly hangCancel?: boolean;
1350
+ /** The `Response.url` to report, like an IP address URL on Bun. */
1351
+ readonly responseUrl?: string;
1352
+ }
1353
+
1354
+ /**
1355
+ * Records the life cycle of fetch sessions and their responses as events:
1356
+ * `open <url>`, `fetch <url> #n`, `cancel #n`, `consumed #n`, and
1357
+ * `close <url>`.
1358
+ */
1359
+ interface HopRecorder {
1360
+ readonly events: readonly string[];
1361
+ /** Makes a response whose body records being consumed or cancelled. */
1362
+ respond(url: string): Response;
1363
+ /** Records opening and closing the given session. */
1364
+ record(url: string, session: PublicFetchSession): PublicFetchSession;
1365
+ /** Opens a session that sends requests to {@link HopRecorder.respond}. */
1366
+ openHop(url: string): Promise<PublicFetchSession>;
1367
+ /**
1368
+ * Checks that every opened session is closed, and that every response is
1369
+ * consumed or cancelled before its session is closed.
1370
+ */
1371
+ assertInvariants(): void;
1372
+ }
1373
+
1374
+ function createHopRecorder(
1375
+ responder: (url: string) => RecordedResponseInit,
1376
+ ): HopRecorder {
1377
+ const events: string[] = [];
1378
+ const states = new Map<Response, { readonly n: number; settled: boolean }>();
1379
+ const violations: string[] = [];
1380
+ let responses = 0;
1381
+ let opens = 0;
1382
+ let closes = 0;
1383
+
1384
+ function respond(url: string): Response {
1385
+ const n = ++responses;
1386
+ events.push(`fetch ${url} #${n}`);
1387
+ const {
1388
+ status = 200,
1389
+ headers,
1390
+ body = "",
1391
+ failCancel,
1392
+ hangCancel,
1393
+ responseUrl,
1394
+ } = responder(url);
1395
+ const state = { n, settled: false };
1396
+ let sent = false;
1397
+ const stream = new ReadableStream<Uint8Array>({
1398
+ pull(controller) {
1399
+ if (!sent && body !== "") {
1400
+ sent = true;
1401
+ controller.enqueue(new TextEncoder().encode(body));
1402
+ return;
1403
+ }
1404
+ controller.close();
1405
+ state.settled = true;
1406
+ events.push(`consumed #${n}`);
1407
+ },
1408
+ cancel() {
1409
+ state.settled = true;
1410
+ events.push(`cancel #${n}`);
1411
+ if (failCancel) throw new Error(`Failed to cancel #${n}`);
1412
+ if (hangCancel) return new Promise<void>(() => {});
1413
+ },
1414
+ }, { highWaterMark: 0 });
1415
+ const response = new Response(stream, { status, headers });
1416
+ if (responseUrl != null) {
1417
+ Object.defineProperty(response, "url", { value: responseUrl });
1418
+ }
1419
+ states.set(response, state);
1420
+ return response;
1421
+ }
1422
+
1423
+ function record(
1424
+ url: string,
1425
+ session: PublicFetchSession,
1426
+ ): PublicFetchSession {
1427
+ opens++;
1428
+ events.push(`open ${url}`);
1429
+ const fetched: Response[] = [];
1430
+ let closed = false;
1431
+ return {
1432
+ get url() {
1433
+ return session.url;
1434
+ },
1435
+ async fetch(input, init) {
1436
+ const response = await session.fetch(input, init);
1437
+ fetched.push(response);
1438
+ return response;
1439
+ },
1440
+ async close() {
1441
+ if (closed) return;
1442
+ closed = true;
1443
+ for (const response of fetched) {
1444
+ const state = states.get(response);
1445
+ if (state != null && !state.settled) {
1446
+ violations.push(`#${state.n} is still open when closing ${url}`);
1447
+ }
1448
+ }
1449
+ closes++;
1450
+ events.push(`close ${url}`);
1451
+ await session.close();
1452
+ },
1453
+ };
1454
+ }
1455
+
1456
+ function openHop(url: string): Promise<PublicFetchSession> {
1457
+ const origin = new URL(url).origin;
1458
+ return Promise.resolve(record(url, {
1459
+ url: new URL(url),
1460
+ fetch(input) {
1461
+ const target = input instanceof Request ? input.url : String(input);
1462
+ ok(new URL(target).origin === origin, `${target} is not on ${origin}`);
1463
+ return Promise.resolve(respond(target));
1464
+ },
1465
+ close: () => Promise.resolve(),
1466
+ }));
1467
+ }
1468
+
1469
+ return {
1470
+ events,
1471
+ respond,
1472
+ record,
1473
+ openHop,
1474
+ assertInvariants() {
1475
+ deepStrictEqual(violations, []);
1476
+ deepStrictEqual(opens, closes);
1477
+ },
1478
+ };
1479
+ }
1480
+
1481
+ const lifecycleDocument = {
1482
+ "@context": "https://www.w3.org/ns/activitystreams",
1483
+ id: "https://b.example/object",
1484
+ type: "Object",
1485
+ };
1486
+
1487
+ test("getDocumentLoader() closes a redirect hop before the next hop", async () => {
1488
+ const recorder = createHopRecorder((url) =>
1489
+ url === "https://a.example/start"
1490
+ ? { status: 302, headers: { Location: "https://b.example/object" } }
1491
+ : { body: JSON.stringify(lifecycleDocument) }
1492
+ );
1493
+ const loader = createDocumentLoader({}, { openHop: recorder.openHop });
1494
+ deepStrictEqual(await loader("https://a.example/start"), {
1495
+ contextUrl: null,
1496
+ documentUrl: "https://b.example/object",
1497
+ document: lifecycleDocument,
1498
+ });
1499
+ deepStrictEqual(recorder.events, [
1500
+ "open https://a.example/start",
1501
+ "fetch https://a.example/start #1",
1502
+ "cancel #1",
1503
+ "close https://a.example/start",
1504
+ "open https://b.example/object",
1505
+ "fetch https://b.example/object #2",
1506
+ "consumed #2",
1507
+ "close https://b.example/object",
1508
+ ]);
1509
+ recorder.assertInvariants();
1510
+ });
1511
+
1512
+ test("getDocumentLoader() cancels the body before a Link header alternate", async () => {
1513
+ const recorder = createHopRecorder((url) =>
1514
+ url === "https://a.example/start"
1515
+ ? {
1516
+ headers: {
1517
+ "Content-Type": "text/plain",
1518
+ Link: '<https://b.example/object>; rel="alternate"; ' +
1519
+ 'type="application/activity+json"',
1520
+ },
1521
+ body: "not JSON",
1522
+ }
1523
+ : { body: JSON.stringify(lifecycleDocument) }
1524
+ );
1525
+ const loader = createDocumentLoader({}, { openHop: recorder.openHop });
1526
+ deepStrictEqual(
1527
+ (await loader("https://a.example/start")).documentUrl,
1528
+ "https://b.example/object",
1529
+ );
1530
+ deepStrictEqual(recorder.events, [
1531
+ "open https://a.example/start",
1532
+ "fetch https://a.example/start #1",
1533
+ "cancel #1",
1534
+ "close https://a.example/start",
1535
+ "open https://b.example/object",
1536
+ "fetch https://b.example/object #2",
1537
+ "consumed #2",
1538
+ "close https://b.example/object",
1539
+ ]);
1540
+ recorder.assertInvariants();
1541
+ });
1542
+
1543
+ test("getDocumentLoader() reads the body before an HTML alternate", async () => {
1544
+ const recorder = createHopRecorder((url) =>
1545
+ url === "https://a.example/start"
1546
+ ? {
1547
+ headers: { "Content-Type": "text/html" },
1548
+ body: '<link rel="alternate" type="application/activity+json" ' +
1549
+ 'href="https://b.example/object">',
1550
+ }
1551
+ : { body: JSON.stringify(lifecycleDocument) }
1552
+ );
1553
+ const loader = createDocumentLoader({}, { openHop: recorder.openHop });
1554
+ deepStrictEqual(
1555
+ (await loader("https://a.example/start")).documentUrl,
1556
+ "https://b.example/object",
1557
+ );
1558
+ deepStrictEqual(recorder.events, [
1559
+ "open https://a.example/start",
1560
+ "fetch https://a.example/start #1",
1561
+ "consumed #1",
1562
+ "close https://a.example/start",
1563
+ "open https://b.example/object",
1564
+ "fetch https://b.example/object #2",
1565
+ "consumed #2",
1566
+ "close https://b.example/object",
1567
+ ]);
1568
+ recorder.assertInvariants();
1569
+ });
1570
+
1571
+ /**
1572
+ * Fails instead of blocking the test if the given promise does not settle
1573
+ * within the given time.
1574
+ */
1575
+ async function settlesWithin<T>(
1576
+ promise: Promise<T>,
1577
+ milliseconds = 1000,
1578
+ ): Promise<T> {
1579
+ let timer: ReturnType<typeof setTimeout> | undefined;
1580
+ try {
1581
+ return await Promise.race([
1582
+ promise,
1583
+ new Promise<never>((_, reject) => {
1584
+ timer = setTimeout(
1585
+ () => reject(new Error(`Did not settle in ${milliseconds} ms.`)),
1586
+ milliseconds,
1587
+ );
1588
+ }),
1589
+ ]);
1590
+ } finally {
1591
+ clearTimeout(timer);
1592
+ }
1593
+ }
1594
+
1595
+ /**
1596
+ * Runs the given function while capturing the document loader's logs, and
1597
+ * waits until the rejections of body cancellations started without being
1598
+ * awaited have been logged.
1599
+ */
1600
+ async function captureDocumentLoaderLogs<T>(
1601
+ fn: () => Promise<T>,
1602
+ ): Promise<{ result: T; records: LogRecord[] }> {
1603
+ const records: LogRecord[] = [];
1604
+ await configure({
1605
+ sinks: { buffer: (record) => records.push(record) },
1606
+ loggers: [
1607
+ {
1608
+ category: ["fedify", "runtime", "docloader"],
1609
+ sinks: ["buffer"],
1610
+ lowestLevel: "debug",
1611
+ },
1612
+ { category: ["logtape", "meta"], sinks: [] },
1613
+ ],
1614
+ reset: true,
1615
+ });
1616
+ try {
1617
+ const result = await fn();
1618
+ await new Promise((resolve) => setTimeout(resolve, 0));
1619
+ return { result, records };
1620
+ } finally {
1621
+ await reset();
1622
+ }
1623
+ }
1624
+
1625
+ function cancelFailureLogs(
1626
+ records: readonly LogRecord[],
1627
+ ): [string, unknown, string][] {
1628
+ return records
1629
+ .filter((record) =>
1630
+ record.rawMessage ===
1631
+ "Failed to cancel the response body from {url}: {error}"
1632
+ )
1633
+ .map((record) => [
1634
+ record.level,
1635
+ record.properties.url,
1636
+ String(record.properties.error),
1637
+ ]);
1638
+ }
1639
+
1640
+ function linkHeaderAlternate(next: string): RecordedResponseInit {
1641
+ return {
1642
+ headers: {
1643
+ "Content-Type": "text/plain",
1644
+ Link: `<${next}>; rel="alternate"; type="application/activity+json"`,
1645
+ },
1646
+ body: "not JSON",
1647
+ };
1648
+ }
1649
+
1650
+ test("getDocumentLoader() continues if an intermediate body fails to cancel", async (t) => {
1651
+ for (const kind of ["redirect", "Link header alternate"] as const) {
1652
+ await t.step(kind, async () => {
1653
+ const recorder = createHopRecorder((url) =>
1654
+ url === "https://a.example/start"
1655
+ ? {
1656
+ ...(kind === "redirect"
1657
+ ? {
1658
+ status: 302,
1659
+ headers: { Location: "https://b.example/object" },
1660
+ }
1661
+ : linkHeaderAlternate("https://b.example/object")),
1662
+ failCancel: true,
1663
+ }
1664
+ : { body: JSON.stringify(lifecycleDocument) }
1665
+ );
1666
+ const loader = createDocumentLoader({}, { openHop: recorder.openHop });
1667
+ const { result, records } = await captureDocumentLoaderLogs(() =>
1668
+ loader("https://a.example/start")
1669
+ );
1670
+ deepStrictEqual(result, {
1671
+ contextUrl: null,
1672
+ documentUrl: "https://b.example/object",
1673
+ document: lifecycleDocument,
1674
+ });
1675
+ deepStrictEqual(recorder.events, [
1676
+ "open https://a.example/start",
1677
+ "fetch https://a.example/start #1",
1678
+ "cancel #1",
1679
+ "close https://a.example/start",
1680
+ "open https://b.example/object",
1681
+ "fetch https://b.example/object #2",
1682
+ "consumed #2",
1683
+ "close https://b.example/object",
1684
+ ]);
1685
+ recorder.assertInvariants();
1686
+ // The failure is only logged:
1687
+ deepStrictEqual(cancelFailureLogs(records), [[
1688
+ "debug",
1689
+ "https://a.example/start",
1690
+ "Error: Failed to cancel #1",
1691
+ ]]);
1692
+ });
1693
+ }
1694
+ });
1695
+
1696
+ test("getDocumentLoader() does not wait for intermediate bodies to be cancelled", async (t) => {
1697
+ for (const kind of ["redirect", "Link header alternate"] as const) {
1698
+ await t.step(kind, async () => {
1699
+ const recorder = createHopRecorder((url) =>
1700
+ url === "https://a.example/start"
1701
+ ? {
1702
+ ...(kind === "redirect"
1703
+ ? {
1704
+ status: 302,
1705
+ headers: { Location: "https://b.example/object" },
1706
+ }
1707
+ : linkHeaderAlternate("https://b.example/object")),
1708
+ hangCancel: true,
1709
+ }
1710
+ : { body: JSON.stringify(lifecycleDocument) }
1711
+ );
1712
+ const loader = createDocumentLoader({}, { openHop: recorder.openHop });
1713
+ deepStrictEqual(
1714
+ (await settlesWithin(loader("https://a.example/start"))).documentUrl,
1715
+ "https://b.example/object",
1716
+ );
1717
+ deepStrictEqual(recorder.events, [
1718
+ "open https://a.example/start",
1719
+ "fetch https://a.example/start #1",
1720
+ "cancel #1",
1721
+ "close https://a.example/start",
1722
+ "open https://b.example/object",
1723
+ "fetch https://b.example/object #2",
1724
+ "consumed #2",
1725
+ "close https://b.example/object",
1726
+ ]);
1727
+ recorder.assertInvariants();
1728
+ });
1729
+ }
1730
+
1731
+ await t.step("public session with a never-settling cancel", async () => {
1732
+ // A real session must not wait for a body it cancels on close() either:
1733
+ let cancelled = 0;
1734
+ const pinner: ConnectionPinner = {
1735
+ supports: () => true,
1736
+ fetch: () =>
1737
+ Promise.resolve(
1738
+ new Response(
1739
+ new ReadableStream({
1740
+ cancel() {
1741
+ cancelled++;
1742
+ return new Promise<void>(() => {});
1743
+ },
1744
+ }),
1745
+ { status: 404 },
1746
+ ),
1747
+ ),
1748
+ release() {},
1749
+ };
1750
+ const publicFetch = createPublicFetch({
1751
+ pinner: () => Promise.resolve(pinner),
1752
+ lookup: () => Promise.reject(new Error("Unexpected default lookup")),
1753
+ });
1754
+ const loader = createDocumentLoader({}, {
1755
+ openHop: (url) =>
1756
+ publicFetch.openPublicFetch(url, { lookup: lookupTestAddresses }),
1757
+ });
1758
+ await settlesWithin(
1759
+ rejects(loader("https://a.example/missing"), (error) => {
1760
+ ok(error instanceof FetchError);
1761
+ // The error retains a response clone for the caller. Cancel that tee
1762
+ // branch too, so the underlying stream's cancellation can start.
1763
+ error.response?.body?.cancel().catch(() => {});
1764
+ return true;
1765
+ }),
1766
+ );
1767
+ deepStrictEqual(cancelled, 1);
1768
+ });
1769
+ });
1770
+
1771
+ test("getDocumentLoader() completes with tee'd fetch-mock responses", async (t) => {
1772
+ // fetch-mock returns a clone (a tee branch) of a Response instance that a
1773
+ // route returns, and the cancellation of a tee branch settles only after
1774
+ // the other branch is cancelled too. That never happens for a string body
1775
+ // on Node.js, and for a stream body on any runtime:
1776
+ const url = "https://example.com/teed";
1777
+ function stream(text: string): ReadableStream<Uint8Array> {
1778
+ return new ReadableStream({
1779
+ start(controller) {
1780
+ controller.enqueue(new TextEncoder().encode(text));
1781
+ controller.close();
1782
+ },
1783
+ });
1784
+ }
1785
+ const loaders = {
1786
+ unpinned: createDocumentLoader({}, { openHop: openUnpinnedHop }),
1787
+ "private addresses allowed": getDocumentLoader({
1788
+ allowPrivateAddress: true,
1789
+ }),
1790
+ };
1791
+ for (const [name, loader] of Object.entries(loaders)) {
1792
+ for (const body of ["string", "stream"] as const) {
1793
+ await t.step(`${name}: ${body} bodies`, async () => {
1794
+ const make = (text: string) => body === "string" ? text : stream(text);
1795
+ fetchMock.mockGlobal();
1796
+ try {
1797
+ fetchMock.get(
1798
+ `${url}/object`,
1799
+ () =>
1800
+ new Response(make(JSON.stringify(lifecycleDocument)), {
1801
+ headers: { "Content-Type": "application/activity+json" },
1802
+ }),
1803
+ );
1804
+ fetchMock.get(
1805
+ `${url}/redirect`,
1806
+ () =>
1807
+ new Response(make("redirect"), {
1808
+ status: 302,
1809
+ headers: { Location: `${url}/object` },
1810
+ }),
1811
+ );
1812
+ fetchMock.get(
1813
+ `${url}/alternate`,
1814
+ () =>
1815
+ new Response(make("not JSON"), {
1816
+ headers: {
1817
+ "Content-Type": "text/plain",
1818
+ Link: `<${url}/object>; rel="alternate"; ` +
1819
+ 'type="application/activity+json"',
1820
+ },
1821
+ }),
1822
+ );
1823
+ fetchMock.get(
1824
+ `${url}/missing`,
1825
+ () => new Response(make("not found"), { status: 404 }),
1826
+ );
1827
+ for (const path of ["redirect", "alternate"]) {
1828
+ deepStrictEqual(
1829
+ (await settlesWithin(loader(`${url}/${path}`))).document,
1830
+ lifecycleDocument,
1831
+ );
1832
+ }
1833
+ await settlesWithin(rejects(loader(`${url}/missing`), FetchError));
1834
+ deepStrictEqual(
1835
+ fetchMock.callHistory.calls().map((call) => call.url),
1836
+ [
1837
+ `${url}/redirect`,
1838
+ `${url}/object`,
1839
+ `${url}/alternate`,
1840
+ `${url}/object`,
1841
+ `${url}/missing`,
1842
+ ],
1843
+ );
1844
+ } finally {
1845
+ fetchMock.hardReset();
1846
+ }
1847
+ });
1848
+ }
1849
+ }
1850
+ });
1851
+
1852
+ test("getDocumentLoader() resolves each hop once", async () => {
1853
+ const lookups: string[] = [];
1854
+ const recorder = createHopRecorder((url) => {
1855
+ // Pinned responses report the IP address URL on Bun; the request URL must
1856
+ // still be used as the document URL and as the base of Location:
1857
+ const responseUrl = `https://${PUBLIC_ADDRESS.address}${
1858
+ new URL(url).pathname
1859
+ }`;
1860
+ if (url === "https://a.example/start") {
1861
+ return {
1862
+ status: 302,
1863
+ headers: { Location: "https://b.example/next" },
1864
+ responseUrl,
1865
+ };
1866
+ } else if (url === "https://b.example/next") {
1867
+ return { status: 301, headers: { Location: "/object" }, responseUrl };
1868
+ }
1869
+ return { body: JSON.stringify(lifecycleDocument), responseUrl };
1870
+ });
1871
+ const pinner: ConnectionPinner = {
1872
+ supports: () => true,
1873
+ fetch: (_grant, request) => Promise.resolve(recorder.respond(request.url)),
1874
+ release() {},
1875
+ };
1876
+ const publicFetch = createPublicFetch({
1877
+ pinner: () => Promise.resolve(pinner),
1878
+ lookup: () => Promise.reject(new Error("Unexpected default lookup")),
1879
+ });
1880
+ const loader = createDocumentLoader({}, {
1881
+ openHop: async (url) =>
1882
+ recorder.record(
1883
+ url,
1884
+ await publicFetch.openPublicFetch(url, {
1885
+ lookup(hostname) {
1886
+ lookups.push(hostname);
1887
+ return Promise.resolve([PUBLIC_ADDRESS]);
1888
+ },
1889
+ }),
1890
+ ),
1891
+ });
1892
+ deepStrictEqual(await loader("https://a.example/start"), {
1893
+ contextUrl: null,
1894
+ documentUrl: "https://b.example/object",
1895
+ document: lifecycleDocument,
1896
+ });
1897
+ deepStrictEqual(lookups, ["a.example", "b.example", "b.example"]);
1898
+ deepStrictEqual(recorder.events, [
1899
+ "open https://a.example/start",
1900
+ "fetch https://a.example/start #1",
1901
+ "cancel #1",
1902
+ "close https://a.example/start",
1903
+ "open https://b.example/next",
1904
+ "fetch https://b.example/next #2",
1905
+ "cancel #2",
1906
+ "close https://b.example/next",
1907
+ "open https://b.example/object",
1908
+ "fetch https://b.example/object #3",
1909
+ "consumed #3",
1910
+ "close https://b.example/object",
1911
+ ]);
1912
+ recorder.assertInvariants();
1913
+ });
1914
+
1915
+ test("getRemoteDocument() cancels the body before loading a Link header alternate", async () => {
1916
+ const recorder = createHopRecorder(() => ({
1917
+ headers: {
1918
+ "Content-Type": "text/html",
1919
+ Link: '<https://b.example/object>; rel="alternate"; ' +
1920
+ 'type="application/activity+json"',
1921
+ },
1922
+ body: "<html></html>",
1923
+ }));
1924
+ const response = recorder.respond("https://a.example/start");
1925
+ const placeholder: RemoteDocument = {
1926
+ contextUrl: null,
1927
+ document: {},
1928
+ documentUrl: "https://b.example/object",
1929
+ };
1930
+ const alternates: string[] = [];
1931
+ const result = await getRemoteDocument(
1932
+ "https://a.example/start",
1933
+ response,
1934
+ (url) => {
1935
+ alternates.push(url);
1936
+ deepStrictEqual(recorder.events.at(-1), "cancel #1");
1937
+ ok(response.bodyUsed);
1938
+ return Promise.resolve(placeholder);
1939
+ },
1940
+ );
1941
+ ok(result === placeholder);
1942
+ deepStrictEqual(alternates, ["https://b.example/object"]);
1943
+ deepStrictEqual(recorder.events, [
1944
+ "fetch https://a.example/start #1",
1945
+ "cancel #1",
1946
+ ]);
1947
+ });