@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
@@ -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");
@@ -24,7 +57,9 @@ test("new FetchError()", () => {
24
57
  });
25
58
 
26
59
  test("getDocumentLoader()", async (t) => {
27
- const fetchDocumentLoader = getDocumentLoader();
60
+ const fetchDocumentLoader = createDocumentLoader({}, {
61
+ openHop: openUnpinnedHop,
62
+ });
28
63
 
29
64
  fetchMock.spyGlobal();
30
65
 
@@ -549,6 +584,19 @@ test("getDocumentLoader()", async (t) => {
549
584
  },
550
585
  });
551
586
 
587
+ fetchMock.get("https://example.com/private-redirect", {
588
+ status: 302,
589
+ headers: { Location: "https://private.example/object" },
590
+ });
591
+
592
+ fetchMock.get("https://private.example/object", {
593
+ body: {
594
+ "@context": "https://www.w3.org/ns/activitystreams",
595
+ id: "https://private.example/object",
596
+ type: "Object",
597
+ },
598
+ });
599
+
552
600
  await t.step("allowPrivateAddress: false", async () => {
553
601
  await rejects(
554
602
  () => fetchDocumentLoader("https://localhost/object"),
@@ -562,6 +610,26 @@ test("getDocumentLoader()", async (t) => {
562
610
  () => fetchDocumentLoader("https://example.com/localhost-link"),
563
611
  UrlError,
564
612
  );
613
+ await rejects(
614
+ () => fetchDocumentLoader("https://private.example/object"),
615
+ UrlError,
616
+ );
617
+ await rejects(
618
+ () => fetchDocumentLoader("https://example.com/private-redirect"),
619
+ UrlError,
620
+ );
621
+ deepStrictEqual(
622
+ fetchMock.callHistory.calls("https://private.example/object").length,
623
+ 0,
624
+ );
625
+ // The default loader rejects these without resolving any hostname:
626
+ const defaultLoader = getDocumentLoader();
627
+ await rejects(() => defaultLoader("https://localhost/object"), UrlError);
628
+ await rejects(() => defaultLoader("https://127.0.0.1/object"), UrlError);
629
+ deepStrictEqual(
630
+ fetchMock.callHistory.calls("https://localhost/object").length,
631
+ 0,
632
+ );
565
633
  });
566
634
 
567
635
  const fetchDocumentLoader2 = getDocumentLoader({ allowPrivateAddress: true });
@@ -811,33 +879,24 @@ test("getRemoteDocument() bounds JSON by default", async () => {
811
879
  });
812
880
 
813
881
  test("getDocumentLoader() logs DNS failures as such", async (t) => {
814
- // The validator skips DNS when Deno has no network permission. Checked
815
- // here rather than with top-level await, which the CommonJS build rejects.
816
- if (
817
- "Deno" in globalThis &&
818
- (await Deno.permissions.query({ name: "net" })).state !== "granted"
819
- ) {
820
- throw new Error(
821
- "This DNS test requires Deno network permission (--allow-net).",
822
- );
823
- }
824
- const loader = getDocumentLoader();
825
- for (const result of ["throws", "empty", "private"] as const) {
882
+ for (const result of ["throws", "empty", "private", "default"] as const) {
826
883
  await t.step(result, async () => {
827
- // Stubbing works only because url.ts uses the default node:dns/promises
828
- // import; see the FIXME there.
829
- const originalLookup = dns.lookup;
830
- dns.lookup = (() =>
884
+ const lookup = (): Promise<readonly LookupAddress[]> =>
831
885
  result === "throws"
832
886
  ? Promise.reject(new Error("Resolver unavailable"))
833
- : Promise.resolve(
834
- result === "empty" ? [] : [{ address: "127.0.0.1", family: 4 }],
835
- )) as typeof dns.lookup;
887
+ : Promise.resolve(result === "empty" ? [] : [PRIVATE_ADDRESS]);
888
+ // The default loader rejects IP literals without resolving them, so
889
+ // the "default" step checks that its logging matches the injected one:
890
+ const loader = result === "default"
891
+ ? getDocumentLoader()
892
+ : createDocumentLoader({}, {
893
+ openHop: (url) =>
894
+ unpinnedPublicFetch.openPublicFetch(url, { lookup }),
895
+ });
836
896
  const records: LogRecord[] = [];
837
897
  await configure({
838
898
  sinks: {
839
- buffer: (record) =>
840
- records.push(record),
899
+ buffer: (record) => records.push(record),
841
900
  },
842
901
  loggers: [
843
902
  { category: "fedify", sinks: ["buffer"], lowestLevel: "debug" },
@@ -846,21 +905,21 @@ test("getDocumentLoader() logs DNS failures as such", async (t) => {
846
905
  reset: true,
847
906
  });
848
907
  try {
849
- const url = "https://dns-failure.invalid/object";
908
+ const url = result === "default"
909
+ ? "https://10.0.0.1/object"
910
+ : "https://dns-failure.invalid/object";
850
911
  let error: unknown;
851
912
  await rejects(() => loader(url), (e) => {
852
913
  error = e;
853
914
  return true;
854
915
  });
855
916
  ok(error instanceof UrlError);
856
- deepStrictEqual(
857
- error.reason,
858
- result === "private" ? "disallowed" : "dns",
859
- );
917
+ const disallowed = result === "private" || result === "default";
918
+ deepStrictEqual(error.reason, disallowed ? "disallowed" : "dns");
860
919
  deepStrictEqual(
861
920
  records.map((r) => [r.level, r.rawMessage, r.properties.url]),
862
921
  [
863
- result === "private"
922
+ disallowed
864
923
  ? ["error", "Disallowed private URL: {url}", url]
865
924
  : ["debug", "DNS lookup failed for {url}", url],
866
925
  ],
@@ -868,7 +927,6 @@ test("getDocumentLoader() logs DNS failures as such", async (t) => {
868
927
  ok(records[0].properties.error === error);
869
928
  } finally {
870
929
  await reset();
871
- dns.lookup = originalLookup;
872
930
  }
873
931
  });
874
932
  }
@@ -1258,3 +1316,604 @@ for (const url of unsupportedUrls) {
1258
1316
  );
1259
1317
  }
1260
1318
  }
1319
+
1320
+ interface RecordedResponseInit {
1321
+ readonly status?: number;
1322
+ readonly headers?: Record<string, string>;
1323
+ readonly body?: string;
1324
+ /** Whether cancelling the body fails. */
1325
+ readonly failCancel?: boolean;
1326
+ /** Whether cancelling the body never settles, like a tee branch's. */
1327
+ readonly hangCancel?: boolean;
1328
+ /** The `Response.url` to report, like an IP address URL on Bun. */
1329
+ readonly responseUrl?: string;
1330
+ }
1331
+
1332
+ /**
1333
+ * Records the life cycle of fetch sessions and their responses as events:
1334
+ * `open <url>`, `fetch <url> #n`, `cancel #n`, `consumed #n`, and
1335
+ * `close <url>`.
1336
+ */
1337
+ interface HopRecorder {
1338
+ readonly events: readonly string[];
1339
+ /** Makes a response whose body records being consumed or cancelled. */
1340
+ respond(url: string): Response;
1341
+ /** Records opening and closing the given session. */
1342
+ record(url: string, session: PublicFetchSession): PublicFetchSession;
1343
+ /** Opens a session that sends requests to {@link HopRecorder.respond}. */
1344
+ openHop(url: string): Promise<PublicFetchSession>;
1345
+ /**
1346
+ * Checks that every opened session is closed, and that every response is
1347
+ * consumed or cancelled before its session is closed.
1348
+ */
1349
+ assertInvariants(): void;
1350
+ }
1351
+
1352
+ function createHopRecorder(
1353
+ responder: (url: string) => RecordedResponseInit,
1354
+ ): HopRecorder {
1355
+ const events: string[] = [];
1356
+ const states = new Map<Response, { readonly n: number; settled: boolean }>();
1357
+ const violations: string[] = [];
1358
+ let responses = 0;
1359
+ let opens = 0;
1360
+ let closes = 0;
1361
+
1362
+ function respond(url: string): Response {
1363
+ const n = ++responses;
1364
+ events.push(`fetch ${url} #${n}`);
1365
+ const {
1366
+ status = 200,
1367
+ headers,
1368
+ body = "",
1369
+ failCancel,
1370
+ hangCancel,
1371
+ responseUrl,
1372
+ } = responder(url);
1373
+ const state = { n, settled: false };
1374
+ let sent = false;
1375
+ const stream = new ReadableStream<Uint8Array>({
1376
+ pull(controller) {
1377
+ if (!sent && body !== "") {
1378
+ sent = true;
1379
+ controller.enqueue(new TextEncoder().encode(body));
1380
+ return;
1381
+ }
1382
+ controller.close();
1383
+ state.settled = true;
1384
+ events.push(`consumed #${n}`);
1385
+ },
1386
+ cancel() {
1387
+ state.settled = true;
1388
+ events.push(`cancel #${n}`);
1389
+ if (failCancel) throw new Error(`Failed to cancel #${n}`);
1390
+ if (hangCancel) return new Promise<void>(() => {});
1391
+ },
1392
+ }, { highWaterMark: 0 });
1393
+ const response = new Response(stream, { status, headers });
1394
+ if (responseUrl != null) {
1395
+ Object.defineProperty(response, "url", { value: responseUrl });
1396
+ }
1397
+ states.set(response, state);
1398
+ return response;
1399
+ }
1400
+
1401
+ function record(
1402
+ url: string,
1403
+ session: PublicFetchSession,
1404
+ ): PublicFetchSession {
1405
+ opens++;
1406
+ events.push(`open ${url}`);
1407
+ const fetched: Response[] = [];
1408
+ let closed = false;
1409
+ return {
1410
+ get url() {
1411
+ return session.url;
1412
+ },
1413
+ async fetch(input, init) {
1414
+ const response = await session.fetch(input, init);
1415
+ fetched.push(response);
1416
+ return response;
1417
+ },
1418
+ async close() {
1419
+ if (closed) return;
1420
+ closed = true;
1421
+ for (const response of fetched) {
1422
+ const state = states.get(response);
1423
+ if (state != null && !state.settled) {
1424
+ violations.push(`#${state.n} is still open when closing ${url}`);
1425
+ }
1426
+ }
1427
+ closes++;
1428
+ events.push(`close ${url}`);
1429
+ await session.close();
1430
+ },
1431
+ };
1432
+ }
1433
+
1434
+ function openHop(url: string): Promise<PublicFetchSession> {
1435
+ const origin = new URL(url).origin;
1436
+ return Promise.resolve(record(url, {
1437
+ url: new URL(url),
1438
+ fetch(input) {
1439
+ const target = input instanceof Request ? input.url : String(input);
1440
+ ok(new URL(target).origin === origin, `${target} is not on ${origin}`);
1441
+ return Promise.resolve(respond(target));
1442
+ },
1443
+ close: () => Promise.resolve(),
1444
+ }));
1445
+ }
1446
+
1447
+ return {
1448
+ events,
1449
+ respond,
1450
+ record,
1451
+ openHop,
1452
+ assertInvariants() {
1453
+ deepStrictEqual(violations, []);
1454
+ deepStrictEqual(opens, closes);
1455
+ },
1456
+ };
1457
+ }
1458
+
1459
+ const lifecycleDocument = {
1460
+ "@context": "https://www.w3.org/ns/activitystreams",
1461
+ id: "https://b.example/object",
1462
+ type: "Object",
1463
+ };
1464
+
1465
+ test("getDocumentLoader() closes a redirect hop before the next hop", async () => {
1466
+ const recorder = createHopRecorder((url) =>
1467
+ url === "https://a.example/start"
1468
+ ? { status: 302, headers: { Location: "https://b.example/object" } }
1469
+ : { body: JSON.stringify(lifecycleDocument) }
1470
+ );
1471
+ const loader = createDocumentLoader({}, { openHop: recorder.openHop });
1472
+ deepStrictEqual(await loader("https://a.example/start"), {
1473
+ contextUrl: null,
1474
+ documentUrl: "https://b.example/object",
1475
+ document: lifecycleDocument,
1476
+ });
1477
+ deepStrictEqual(recorder.events, [
1478
+ "open https://a.example/start",
1479
+ "fetch https://a.example/start #1",
1480
+ "cancel #1",
1481
+ "close https://a.example/start",
1482
+ "open https://b.example/object",
1483
+ "fetch https://b.example/object #2",
1484
+ "consumed #2",
1485
+ "close https://b.example/object",
1486
+ ]);
1487
+ recorder.assertInvariants();
1488
+ });
1489
+
1490
+ test("getDocumentLoader() cancels the body before a Link header alternate", async () => {
1491
+ const recorder = createHopRecorder((url) =>
1492
+ url === "https://a.example/start"
1493
+ ? {
1494
+ headers: {
1495
+ "Content-Type": "text/plain",
1496
+ Link: '<https://b.example/object>; rel="alternate"; ' +
1497
+ 'type="application/activity+json"',
1498
+ },
1499
+ body: "not JSON",
1500
+ }
1501
+ : { body: JSON.stringify(lifecycleDocument) }
1502
+ );
1503
+ const loader = createDocumentLoader({}, { openHop: recorder.openHop });
1504
+ deepStrictEqual(
1505
+ (await loader("https://a.example/start")).documentUrl,
1506
+ "https://b.example/object",
1507
+ );
1508
+ deepStrictEqual(recorder.events, [
1509
+ "open https://a.example/start",
1510
+ "fetch https://a.example/start #1",
1511
+ "cancel #1",
1512
+ "close https://a.example/start",
1513
+ "open https://b.example/object",
1514
+ "fetch https://b.example/object #2",
1515
+ "consumed #2",
1516
+ "close https://b.example/object",
1517
+ ]);
1518
+ recorder.assertInvariants();
1519
+ });
1520
+
1521
+ test("getDocumentLoader() reads the body before an HTML alternate", async () => {
1522
+ const recorder = createHopRecorder((url) =>
1523
+ url === "https://a.example/start"
1524
+ ? {
1525
+ headers: { "Content-Type": "text/html" },
1526
+ body: '<link rel="alternate" type="application/activity+json" ' +
1527
+ 'href="https://b.example/object">',
1528
+ }
1529
+ : { body: JSON.stringify(lifecycleDocument) }
1530
+ );
1531
+ const loader = createDocumentLoader({}, { openHop: recorder.openHop });
1532
+ deepStrictEqual(
1533
+ (await loader("https://a.example/start")).documentUrl,
1534
+ "https://b.example/object",
1535
+ );
1536
+ deepStrictEqual(recorder.events, [
1537
+ "open https://a.example/start",
1538
+ "fetch https://a.example/start #1",
1539
+ "consumed #1",
1540
+ "close https://a.example/start",
1541
+ "open https://b.example/object",
1542
+ "fetch https://b.example/object #2",
1543
+ "consumed #2",
1544
+ "close https://b.example/object",
1545
+ ]);
1546
+ recorder.assertInvariants();
1547
+ });
1548
+
1549
+ /**
1550
+ * Fails instead of blocking the test if the given promise does not settle
1551
+ * within the given time.
1552
+ */
1553
+ async function settlesWithin<T>(
1554
+ promise: Promise<T>,
1555
+ milliseconds = 1000,
1556
+ ): Promise<T> {
1557
+ let timer: ReturnType<typeof setTimeout> | undefined;
1558
+ try {
1559
+ return await Promise.race([
1560
+ promise,
1561
+ new Promise<never>((_, reject) => {
1562
+ timer = setTimeout(
1563
+ () => reject(new Error(`Did not settle in ${milliseconds} ms.`)),
1564
+ milliseconds,
1565
+ );
1566
+ }),
1567
+ ]);
1568
+ } finally {
1569
+ clearTimeout(timer);
1570
+ }
1571
+ }
1572
+
1573
+ /**
1574
+ * Runs the given function while capturing the document loader's logs, and
1575
+ * waits until the rejections of body cancellations started without being
1576
+ * awaited have been logged.
1577
+ */
1578
+ async function captureDocumentLoaderLogs<T>(
1579
+ fn: () => Promise<T>,
1580
+ ): Promise<{ result: T; records: LogRecord[] }> {
1581
+ const records: LogRecord[] = [];
1582
+ await configure({
1583
+ sinks: { buffer: (record) => records.push(record) },
1584
+ loggers: [
1585
+ {
1586
+ category: ["fedify", "runtime", "docloader"],
1587
+ sinks: ["buffer"],
1588
+ lowestLevel: "debug",
1589
+ },
1590
+ { category: ["logtape", "meta"], sinks: [] },
1591
+ ],
1592
+ reset: true,
1593
+ });
1594
+ try {
1595
+ const result = await fn();
1596
+ await new Promise((resolve) => setTimeout(resolve, 0));
1597
+ return { result, records };
1598
+ } finally {
1599
+ await reset();
1600
+ }
1601
+ }
1602
+
1603
+ function cancelFailureLogs(
1604
+ records: readonly LogRecord[],
1605
+ ): [string, unknown, string][] {
1606
+ return records
1607
+ .filter((record) =>
1608
+ record.rawMessage ===
1609
+ "Failed to cancel the response body from {url}: {error}"
1610
+ )
1611
+ .map((record) => [
1612
+ record.level,
1613
+ record.properties.url,
1614
+ String(record.properties.error),
1615
+ ]);
1616
+ }
1617
+
1618
+ function linkHeaderAlternate(next: string): RecordedResponseInit {
1619
+ return {
1620
+ headers: {
1621
+ "Content-Type": "text/plain",
1622
+ Link: `<${next}>; rel="alternate"; type="application/activity+json"`,
1623
+ },
1624
+ body: "not JSON",
1625
+ };
1626
+ }
1627
+
1628
+ test("getDocumentLoader() continues if an intermediate body fails to cancel", async (t) => {
1629
+ for (const kind of ["redirect", "Link header alternate"] as const) {
1630
+ await t.step(kind, async () => {
1631
+ const recorder = createHopRecorder((url) =>
1632
+ url === "https://a.example/start"
1633
+ ? {
1634
+ ...(kind === "redirect"
1635
+ ? {
1636
+ status: 302,
1637
+ headers: { Location: "https://b.example/object" },
1638
+ }
1639
+ : linkHeaderAlternate("https://b.example/object")),
1640
+ failCancel: true,
1641
+ }
1642
+ : { body: JSON.stringify(lifecycleDocument) }
1643
+ );
1644
+ const loader = createDocumentLoader({}, { openHop: recorder.openHop });
1645
+ const { result, records } = await captureDocumentLoaderLogs(() =>
1646
+ loader("https://a.example/start")
1647
+ );
1648
+ deepStrictEqual(result, {
1649
+ contextUrl: null,
1650
+ documentUrl: "https://b.example/object",
1651
+ document: lifecycleDocument,
1652
+ });
1653
+ deepStrictEqual(recorder.events, [
1654
+ "open https://a.example/start",
1655
+ "fetch https://a.example/start #1",
1656
+ "cancel #1",
1657
+ "close https://a.example/start",
1658
+ "open https://b.example/object",
1659
+ "fetch https://b.example/object #2",
1660
+ "consumed #2",
1661
+ "close https://b.example/object",
1662
+ ]);
1663
+ recorder.assertInvariants();
1664
+ // The failure is only logged:
1665
+ deepStrictEqual(cancelFailureLogs(records), [[
1666
+ "debug",
1667
+ "https://a.example/start",
1668
+ "Error: Failed to cancel #1",
1669
+ ]]);
1670
+ });
1671
+ }
1672
+ });
1673
+
1674
+ test("getDocumentLoader() does not wait for intermediate bodies to be cancelled", async (t) => {
1675
+ for (const kind of ["redirect", "Link header alternate"] as const) {
1676
+ await t.step(kind, async () => {
1677
+ const recorder = createHopRecorder((url) =>
1678
+ url === "https://a.example/start"
1679
+ ? {
1680
+ ...(kind === "redirect"
1681
+ ? {
1682
+ status: 302,
1683
+ headers: { Location: "https://b.example/object" },
1684
+ }
1685
+ : linkHeaderAlternate("https://b.example/object")),
1686
+ hangCancel: true,
1687
+ }
1688
+ : { body: JSON.stringify(lifecycleDocument) }
1689
+ );
1690
+ const loader = createDocumentLoader({}, { openHop: recorder.openHop });
1691
+ deepStrictEqual(
1692
+ (await settlesWithin(loader("https://a.example/start"))).documentUrl,
1693
+ "https://b.example/object",
1694
+ );
1695
+ deepStrictEqual(recorder.events, [
1696
+ "open https://a.example/start",
1697
+ "fetch https://a.example/start #1",
1698
+ "cancel #1",
1699
+ "close https://a.example/start",
1700
+ "open https://b.example/object",
1701
+ "fetch https://b.example/object #2",
1702
+ "consumed #2",
1703
+ "close https://b.example/object",
1704
+ ]);
1705
+ recorder.assertInvariants();
1706
+ });
1707
+ }
1708
+
1709
+ await t.step("public session with a never-settling cancel", async () => {
1710
+ // A real session must not wait for a body it cancels on close() either:
1711
+ let cancelled = 0;
1712
+ const pinner: ConnectionPinner = {
1713
+ supports: () => true,
1714
+ fetch: () =>
1715
+ Promise.resolve(
1716
+ new Response(
1717
+ new ReadableStream({
1718
+ cancel() {
1719
+ cancelled++;
1720
+ return new Promise<void>(() => {});
1721
+ },
1722
+ }),
1723
+ { status: 404 },
1724
+ ),
1725
+ ),
1726
+ release() {},
1727
+ };
1728
+ const publicFetch = createPublicFetch({
1729
+ pinner: () => Promise.resolve(pinner),
1730
+ lookup: () => Promise.reject(new Error("Unexpected default lookup")),
1731
+ });
1732
+ const loader = createDocumentLoader({}, {
1733
+ openHop: (url) =>
1734
+ publicFetch.openPublicFetch(url, { lookup: lookupTestAddresses }),
1735
+ });
1736
+ await settlesWithin(
1737
+ rejects(loader("https://a.example/missing"), FetchError),
1738
+ );
1739
+ deepStrictEqual(cancelled, 1);
1740
+ });
1741
+ });
1742
+
1743
+ test("getDocumentLoader() completes with tee'd fetch-mock responses", async (t) => {
1744
+ // fetch-mock returns a clone (a tee branch) of a Response instance that a
1745
+ // route returns, and the cancellation of a tee branch settles only after
1746
+ // the other branch is cancelled too. That never happens for a string body
1747
+ // on Node.js, and for a stream body on any runtime:
1748
+ const url = "https://example.com/teed";
1749
+ function stream(text: string): ReadableStream<Uint8Array> {
1750
+ return new ReadableStream({
1751
+ start(controller) {
1752
+ controller.enqueue(new TextEncoder().encode(text));
1753
+ controller.close();
1754
+ },
1755
+ });
1756
+ }
1757
+ const loaders = {
1758
+ unpinned: createDocumentLoader({}, { openHop: openUnpinnedHop }),
1759
+ "private addresses allowed": getDocumentLoader({
1760
+ allowPrivateAddress: true,
1761
+ }),
1762
+ };
1763
+ for (const [name, loader] of Object.entries(loaders)) {
1764
+ for (const body of ["string", "stream"] as const) {
1765
+ await t.step(`${name}: ${body} bodies`, async () => {
1766
+ const make = (text: string) => body === "string" ? text : stream(text);
1767
+ fetchMock.mockGlobal();
1768
+ try {
1769
+ fetchMock.get(
1770
+ `${url}/object`,
1771
+ () =>
1772
+ new Response(make(JSON.stringify(lifecycleDocument)), {
1773
+ headers: { "Content-Type": "application/activity+json" },
1774
+ }),
1775
+ );
1776
+ fetchMock.get(
1777
+ `${url}/redirect`,
1778
+ () =>
1779
+ new Response(make("redirect"), {
1780
+ status: 302,
1781
+ headers: { Location: `${url}/object` },
1782
+ }),
1783
+ );
1784
+ fetchMock.get(
1785
+ `${url}/alternate`,
1786
+ () =>
1787
+ new Response(make("not JSON"), {
1788
+ headers: {
1789
+ "Content-Type": "text/plain",
1790
+ Link: `<${url}/object>; rel="alternate"; ` +
1791
+ 'type="application/activity+json"',
1792
+ },
1793
+ }),
1794
+ );
1795
+ fetchMock.get(
1796
+ `${url}/missing`,
1797
+ () => new Response(make("not found"), { status: 404 }),
1798
+ );
1799
+ for (const path of ["redirect", "alternate"]) {
1800
+ deepStrictEqual(
1801
+ (await settlesWithin(loader(`${url}/${path}`))).document,
1802
+ lifecycleDocument,
1803
+ );
1804
+ }
1805
+ await settlesWithin(rejects(loader(`${url}/missing`), FetchError));
1806
+ deepStrictEqual(
1807
+ fetchMock.callHistory.calls().map((call) => call.url),
1808
+ [
1809
+ `${url}/redirect`,
1810
+ `${url}/object`,
1811
+ `${url}/alternate`,
1812
+ `${url}/object`,
1813
+ `${url}/missing`,
1814
+ ],
1815
+ );
1816
+ } finally {
1817
+ fetchMock.hardReset();
1818
+ }
1819
+ });
1820
+ }
1821
+ }
1822
+ });
1823
+
1824
+ test("getDocumentLoader() resolves each hop once", async () => {
1825
+ const lookups: string[] = [];
1826
+ const recorder = createHopRecorder((url) => {
1827
+ // Pinned responses report the IP address URL on Bun; the request URL must
1828
+ // still be used as the document URL and as the base of Location:
1829
+ const responseUrl = `https://${PUBLIC_ADDRESS.address}${
1830
+ new URL(url).pathname
1831
+ }`;
1832
+ if (url === "https://a.example/start") {
1833
+ return {
1834
+ status: 302,
1835
+ headers: { Location: "https://b.example/next" },
1836
+ responseUrl,
1837
+ };
1838
+ } else if (url === "https://b.example/next") {
1839
+ return { status: 301, headers: { Location: "/object" }, responseUrl };
1840
+ }
1841
+ return { body: JSON.stringify(lifecycleDocument), responseUrl };
1842
+ });
1843
+ const pinner: ConnectionPinner = {
1844
+ supports: () => true,
1845
+ fetch: (_grant, request) => Promise.resolve(recorder.respond(request.url)),
1846
+ release() {},
1847
+ };
1848
+ const publicFetch = createPublicFetch({
1849
+ pinner: () => Promise.resolve(pinner),
1850
+ lookup: () => Promise.reject(new Error("Unexpected default lookup")),
1851
+ });
1852
+ const loader = createDocumentLoader({}, {
1853
+ openHop: async (url) =>
1854
+ recorder.record(
1855
+ url,
1856
+ await publicFetch.openPublicFetch(url, {
1857
+ lookup(hostname) {
1858
+ lookups.push(hostname);
1859
+ return Promise.resolve([PUBLIC_ADDRESS]);
1860
+ },
1861
+ }),
1862
+ ),
1863
+ });
1864
+ deepStrictEqual(await loader("https://a.example/start"), {
1865
+ contextUrl: null,
1866
+ documentUrl: "https://b.example/object",
1867
+ document: lifecycleDocument,
1868
+ });
1869
+ deepStrictEqual(lookups, ["a.example", "b.example", "b.example"]);
1870
+ deepStrictEqual(recorder.events, [
1871
+ "open https://a.example/start",
1872
+ "fetch https://a.example/start #1",
1873
+ "cancel #1",
1874
+ "close https://a.example/start",
1875
+ "open https://b.example/next",
1876
+ "fetch https://b.example/next #2",
1877
+ "cancel #2",
1878
+ "close https://b.example/next",
1879
+ "open https://b.example/object",
1880
+ "fetch https://b.example/object #3",
1881
+ "consumed #3",
1882
+ "close https://b.example/object",
1883
+ ]);
1884
+ recorder.assertInvariants();
1885
+ });
1886
+
1887
+ test("getRemoteDocument() cancels the body before loading a Link header alternate", async () => {
1888
+ const recorder = createHopRecorder(() => ({
1889
+ headers: {
1890
+ "Content-Type": "text/html",
1891
+ Link: '<https://b.example/object>; rel="alternate"; ' +
1892
+ 'type="application/activity+json"',
1893
+ },
1894
+ body: "<html></html>",
1895
+ }));
1896
+ const response = recorder.respond("https://a.example/start");
1897
+ const placeholder: RemoteDocument = {
1898
+ contextUrl: null,
1899
+ document: {},
1900
+ documentUrl: "https://b.example/object",
1901
+ };
1902
+ const alternates: string[] = [];
1903
+ const result = await getRemoteDocument(
1904
+ "https://a.example/start",
1905
+ response,
1906
+ (url) => {
1907
+ alternates.push(url);
1908
+ deepStrictEqual(recorder.events.at(-1), "cancel #1");
1909
+ ok(response.bodyUsed);
1910
+ return Promise.resolve(placeholder);
1911
+ },
1912
+ );
1913
+ ok(result === placeholder);
1914
+ deepStrictEqual(alternates, ["https://b.example/object"]);
1915
+ deepStrictEqual(recorder.events, [
1916
+ "fetch https://a.example/start #1",
1917
+ "cancel #1",
1918
+ ]);
1919
+ });