@fedify/fedify 2.2.19 → 2.2.21
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.
- package/dist/attachment-arrays-CalAATqD.mjs +16 -0
- package/dist/{builder-UmydCxtK.mjs → builder-4ImvtjG4.mjs} +3 -3
- package/dist/callback-0KrwzutD.d.mts +2607 -0
- package/dist/{client-CSddvgWN.d.ts → client-9DPcy_Yx.d.ts} +16 -1
- package/dist/{client-CAM_bQXx.d.cts → client-B3ruDgWg.d.cts} +16 -1
- package/dist/{client-9F_u23dc.mjs → client-XYMC0Mmk.mjs} +189 -65
- package/dist/compat/attachment-arrays.test.d.mts +2 -0
- package/dist/compat/attachment-arrays.test.mjs +255 -0
- package/dist/compat/mod.d.cts +1 -1
- package/dist/compat/mod.d.ts +1 -1
- package/dist/compat/outgoing-jsonld.test.mjs +1 -1
- package/dist/compat/public-audience.test.mjs +1 -1
- package/dist/compat/transformers.test.mjs +1 -1
- package/dist/{context-tJbL_Hkw.d.cts → context-CjWuov0g.d.cts} +57 -14
- package/dist/{context-OkwEpc3-.d.ts → context-DZqI2LJD.d.ts} +57 -14
- package/dist/{deno-CD-8UlUi.mjs → deno-AWk3S2Ug.mjs} +1 -1
- package/dist/docloader-Bogpjg4d.mjs +125 -0
- package/dist/federation/attachment-responses.test.d.mts +2 -0
- package/dist/federation/attachment-responses.test.mjs +182 -0
- package/dist/federation/builder.test.mjs +1 -1
- package/dist/federation/collection.test.mjs +1 -1
- package/dist/federation/delivery-attachments.test.d.mts +2 -0
- package/dist/federation/delivery-attachments.test.mjs +288 -0
- package/dist/federation/handler.test.mjs +208 -16
- package/dist/federation/idempotency.test.mjs +2 -2
- package/dist/federation/inbox.test.mjs +2 -2
- package/dist/federation/middleware.test.mjs +1567 -30
- package/dist/federation/mod.cjs +1 -1
- package/dist/federation/mod.d.cts +2 -2
- package/dist/federation/mod.d.ts +2 -2
- package/dist/federation/mod.js +1 -1
- package/dist/federation/negotiation.test.mjs +1 -1
- package/dist/federation/retry.test.mjs +1 -1
- package/dist/federation/send.test.mjs +723 -68
- package/dist/federation/temporal.test.mjs +25 -1
- package/dist/federation/webfinger.test.mjs +1 -1
- package/dist/{http-BBBUbwKh.mjs → http-B3wCvgu8.mjs} +199 -99
- package/dist/{http-DrUG4fPS.cjs → http-BNaPJVnF.cjs} +178 -107
- package/dist/{http-DJK2udgX.js → http-BpdLaGy8.js} +177 -106
- package/dist/{http-Dl00HXr4.d.ts → http-C8qLHs_Z.d.cts} +3 -4
- package/dist/{http-DqZhyEso.d.cts → http-D8WmcRAF.d.ts} +3 -4
- package/dist/{inbox-Dca_0fwc.mjs → inbox-DLy6WBXU.mjs} +1 -1
- package/dist/{key-DolXUaLH.mjs → key-Bhc8cQC7.mjs} +3 -2
- package/dist/{kv-cache-Bk3V1Tiu.cjs → kv-cache-Bj3CY9Jo.cjs} +59 -17
- package/dist/{kv-cache-DDm80t1K.js → kv-cache-CvjLUQTf.js} +60 -18
- package/dist/{ld-rTdIfZ63.mjs → ld-DTeykI0e.mjs} +153 -21
- package/dist/ld-signature-Dgz_b89j.mjs +23 -0
- package/dist/{middleware-BWOwEGpy.cjs → middleware-BSQP4d9p.cjs} +642 -149
- package/dist/{middleware-B4-Rzdi3.cjs → middleware-CWOzxb3o.cjs} +1 -1
- package/dist/{middleware-DYK_res8.mjs → middleware-DkMsC5mQ.mjs} +1 -1
- package/dist/{middleware-BimXKBcP.js → middleware-Dx1iwFk8.js} +641 -149
- package/dist/{middleware-CpXoRHs3.mjs → middleware-dlGvSrXl.mjs} +452 -122
- package/dist/{mod-D2JyWtW3.d.cts → mod-BmxqcEnU.d.ts} +30 -13
- package/dist/{mod-a6kmB8xD.d.cts → mod-DWEx2D4q.d.cts} +1 -1
- package/dist/{mod-DQGjRJUG.d.ts → mod-DnPmz3-S.d.ts} +1 -1
- package/dist/{mod-ej_rLx8h.d.ts → mod-INjDw-m0.d.cts} +30 -13
- package/dist/{mod-B6LmugJ4.d.ts → mod-REfljt0T.d.ts} +4 -10
- package/dist/{mod-V2vDnYNu.d.cts → mod-UvIGUHqW.d.cts} +4 -10
- package/dist/mod.cjs +5 -5
- package/dist/mod.d.cts +8 -8
- package/dist/mod.d.ts +8 -8
- package/dist/mod.js +5 -5
- package/dist/nodeinfo/client.test.mjs +764 -18
- package/dist/nodeinfo/handler.test.mjs +1 -1
- package/dist/nodeinfo/mod.cjs +1 -1
- package/dist/nodeinfo/mod.d.cts +1 -1
- package/dist/nodeinfo/mod.d.ts +1 -1
- package/dist/nodeinfo/mod.js +1 -1
- package/dist/nodeinfo/types.test.mjs +1 -1
- package/dist/otel/exporter.test.mjs +292 -30
- package/dist/otel/mod.cjs +36 -30
- package/dist/otel/mod.d.cts +13 -0
- package/dist/otel/mod.d.ts +13 -0
- package/dist/otel/mod.js +36 -30
- package/dist/{outgoing-jsonld-amccRMrV.mjs → outgoing-jsonld-DHf_DYXp.mjs} +38 -8
- package/dist/{owner-CVfzyM5V.mjs → owner-BsA7qMHB.mjs} +2 -2
- package/dist/{proof-gOGM-sJS.mjs → proof-C63Hqt9N.mjs} +40 -15
- package/dist/{proof-DhaTTn-P.cjs → proof-Dbz6ApNP.cjs} +1190 -1007
- package/dist/{proof-CaQ4KG6y.js → proof-QV_o5ZqS.js} +1174 -1003
- package/dist/send-CAdpwbFX.mjs +416 -0
- package/dist/sig/http.test.mjs +528 -2
- package/dist/sig/key-types.test.d.mts +218 -0
- package/dist/sig/key-types.test.mjs +8 -0
- package/dist/sig/key.test.mjs +2 -2
- package/dist/sig/ld.test.mjs +573 -8
- package/dist/sig/mod.cjs +2 -2
- package/dist/sig/mod.d.cts +3 -3
- package/dist/sig/mod.d.ts +3 -3
- package/dist/sig/mod.js +2 -2
- package/dist/sig/owner.test.mjs +1 -1
- package/dist/sig/proof.test.mjs +96 -4
- package/dist/{temporal-CA4Y9hBJ.mjs → temporal-B3JGkVgk.mjs} +3 -5
- package/dist/testing/mod.d.mts +1 -2396
- package/dist/{types-DRPjmHVV.cjs → types-BWx7zw3Y.cjs} +187 -63
- package/dist/{types-miAYfiGO.js → types-Dx_w2JSM.js} +188 -64
- package/dist/utils/docloader.test.mjs +318 -34
- package/dist/utils/mod.cjs +1 -1
- package/dist/utils/mod.d.cts +1 -1
- package/dist/utils/mod.d.ts +1 -1
- package/dist/utils/mod.js +1 -1
- package/package.json +6 -6
- package/dist/docloader-D1ghurDJ.mjs +0 -83
- package/dist/send-D35Va72B.mjs +0 -260
- /package/dist/{activity-listener-ell7W1s9.mjs → activity-listener-tztVvlNb.mjs} +0 -0
- /package/dist/{collection-D-HqUuA2.mjs → collection-CA3V5zyK.mjs} +0 -0
- /package/dist/{negotiation-SQvQgUqe.mjs → negotiation-VnHNB0Q5.mjs} +0 -0
- /package/dist/{public-audience-DpJqzPuh.mjs → public-audience-4g3_Ze4y.mjs} +0 -0
- /package/dist/{retry-v_sGLH1d.mjs → retry-CXg_MBI-.mjs} +0 -0
- /package/dist/{types-J53Kw7so.mjs → types-BFowWFTT.mjs} +0 -0
|
@@ -5,12 +5,25 @@ import { t as esm_default } from "../esm-DVILvP5e.mjs";
|
|
|
5
5
|
import { t as assertEquals } from "../assert_equals-Ew3jOFa3.mjs";
|
|
6
6
|
import "../std__assert-DNZ4Liil.mjs";
|
|
7
7
|
import { n as assertInstanceOf } from "../assert-BhQd8QHP.mjs";
|
|
8
|
-
import {
|
|
8
|
+
import { t as assertRejects } from "../assert_rejects-WyYSTDDX.mjs";
|
|
9
|
+
import { t as assertStrictEquals } from "../assert_strict_equals-Dmjbg-bA.mjs";
|
|
10
|
+
import { a as parseOutboundService, c as parseSoftware, i as parseNodeInfo, l as parseUsage, n as getNodeInfoInternal, o as parseProtocol, r as parseInboundService, s as parseServices, t as getNodeInfo } from "../client-XYMC0Mmk.mjs";
|
|
9
11
|
import { test } from "@fedify/fixture";
|
|
10
12
|
import { UrlError } from "@fedify/vocab-runtime";
|
|
13
|
+
import { detectRuntime, openDirectFetch, openPublicFetch } from "@fedify/vocab-runtime/internal";
|
|
11
14
|
import { configure, reset } from "@logtape/logtape";
|
|
12
15
|
import dns from "node:dns/promises";
|
|
16
|
+
import { createServer } from "node:http";
|
|
13
17
|
//#region src/nodeinfo/client.test.ts
|
|
18
|
+
function getNodeInfoDirect(url, options) {
|
|
19
|
+
return getNodeInfoInternal(url, options, { openHop: openDirectFetch });
|
|
20
|
+
}
|
|
21
|
+
function getNodeInfoValidated(url, options) {
|
|
22
|
+
return getNodeInfoInternal(url, options, { openHop: (hopUrl) => new URL(hopUrl).hostname === "example.com" ? openDirectFetch(hopUrl) : openPublicFetch(hopUrl, { lookup: () => Promise.resolve([{
|
|
23
|
+
address: "93.184.215.14",
|
|
24
|
+
family: 4
|
|
25
|
+
}]) }) });
|
|
26
|
+
}
|
|
14
27
|
test("getNodeInfo()", async (t) => {
|
|
15
28
|
esm_default.spyGlobal();
|
|
16
29
|
esm_default.get("https://example.com/.well-known/nodeinfo", { body: { links: [{
|
|
@@ -44,25 +57,25 @@ test("getNodeInfo()", async (t) => {
|
|
|
44
57
|
}
|
|
45
58
|
};
|
|
46
59
|
await t.step("indirect", async () => {
|
|
47
|
-
assertEquals(await
|
|
48
|
-
assertEquals(await
|
|
60
|
+
assertEquals(await getNodeInfoDirect("https://example.com/"), expected);
|
|
61
|
+
assertEquals(await getNodeInfoDirect("https://example.com/", { parse: "none" }), rawExpected);
|
|
49
62
|
});
|
|
50
63
|
await t.step("direct", async () => {
|
|
51
|
-
assertEquals(await
|
|
64
|
+
assertEquals(await getNodeInfoDirect("https://example.com/nodeinfo/2.1", { direct: true }), expected);
|
|
52
65
|
});
|
|
53
66
|
esm_default.removeRoutes();
|
|
54
67
|
esm_default.get("https://example.com/.well-known/nodeinfo", { body: { links: [] } });
|
|
55
68
|
await t.step("indirect: no links", async () => {
|
|
56
|
-
assertEquals(await
|
|
69
|
+
assertEquals(await getNodeInfoDirect("https://example.com/"), void 0);
|
|
57
70
|
});
|
|
58
71
|
esm_default.removeRoutes();
|
|
59
72
|
esm_default.get("https://example.com/.well-known/nodeinfo", { status: 404 });
|
|
60
73
|
await t.step("indirect: 404", async () => {
|
|
61
|
-
assertEquals(await
|
|
74
|
+
assertEquals(await getNodeInfoDirect("https://example.com/"), void 0);
|
|
62
75
|
});
|
|
63
76
|
await t.step("direct: 404", async () => {
|
|
64
|
-
assertEquals(await
|
|
65
|
-
assertEquals(await
|
|
77
|
+
assertEquals(await getNodeInfoDirect("https://example.com/nodeinfo/2.0", { direct: true }), void 0);
|
|
78
|
+
assertEquals(await getNodeInfoDirect("https://example.com/404", { direct: true }), void 0);
|
|
66
79
|
});
|
|
67
80
|
esm_default.hardReset();
|
|
68
81
|
});
|
|
@@ -79,7 +92,7 @@ test("getNodeInfo() blocks SSRF", async (t) => {
|
|
|
79
92
|
internalFetched = true;
|
|
80
93
|
return { body: { secret: "INTERNAL_DATA_LEAKED" } };
|
|
81
94
|
});
|
|
82
|
-
assertEquals(await
|
|
95
|
+
assertEquals(await getNodeInfoValidated("https://example.com/", { parse: "none" }), void 0);
|
|
83
96
|
assertEquals(internalFetched, false);
|
|
84
97
|
});
|
|
85
98
|
await t.step("link href using a data: URL", async () => {
|
|
@@ -88,7 +101,7 @@ test("getNodeInfo() blocks SSRF", async (t) => {
|
|
|
88
101
|
rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
|
|
89
102
|
href: "data:application/json,{\"software\":{\"name\":\"x\"}}"
|
|
90
103
|
}] } });
|
|
91
|
-
assertEquals(await
|
|
104
|
+
assertEquals(await getNodeInfoValidated("https://example.com/", { parse: "none" }), void 0);
|
|
92
105
|
});
|
|
93
106
|
await t.step("redirect to a private address", async () => {
|
|
94
107
|
esm_default.removeRoutes();
|
|
@@ -105,7 +118,7 @@ test("getNodeInfo() blocks SSRF", async (t) => {
|
|
|
105
118
|
internalFetched = true;
|
|
106
119
|
return { body: { secret: "INTERNAL_DATA_LEAKED" } };
|
|
107
120
|
});
|
|
108
|
-
assertEquals(await
|
|
121
|
+
assertEquals(await getNodeInfoValidated("https://example.com/", { parse: "none" }), void 0);
|
|
109
122
|
assertEquals(internalFetched, false);
|
|
110
123
|
});
|
|
111
124
|
await t.step("direct fetch of a private address", async () => {
|
|
@@ -118,7 +131,7 @@ test("getNodeInfo() blocks SSRF", async (t) => {
|
|
|
118
131
|
version: "1.0.0"
|
|
119
132
|
} } };
|
|
120
133
|
});
|
|
121
|
-
assertEquals(await
|
|
134
|
+
assertEquals(await getNodeInfoValidated("http://127.0.0.1:8080/nodeinfo", {
|
|
122
135
|
direct: true,
|
|
123
136
|
parse: "none"
|
|
124
137
|
}), void 0);
|
|
@@ -998,17 +1011,16 @@ test("getNodeInfo() bounds both discovery and document responses", async () => {
|
|
|
998
1011
|
esm_default.hardReset();
|
|
999
1012
|
}
|
|
1000
1013
|
});
|
|
1001
|
-
test("getNodeInfo() logs DNS failures as such",
|
|
1014
|
+
test("getNodeInfo() logs DNS failures as such", async (t) => {
|
|
1002
1015
|
for (const result of [
|
|
1003
1016
|
"throws",
|
|
1004
1017
|
"empty",
|
|
1005
1018
|
"private"
|
|
1006
1019
|
]) await t.step(result, async () => {
|
|
1007
|
-
const
|
|
1008
|
-
dns.lookup = (() => result === "throws" ? Promise.reject(/* @__PURE__ */ new Error("Resolver unavailable")) : Promise.resolve(result === "empty" ? [] : [{
|
|
1020
|
+
const lookup = () => result === "throws" ? Promise.reject(/* @__PURE__ */ new Error("Resolver unavailable")) : Promise.resolve(result === "empty" ? [] : [{
|
|
1009
1021
|
address: "127.0.0.1",
|
|
1010
1022
|
family: 4
|
|
1011
|
-
}])
|
|
1023
|
+
}]);
|
|
1012
1024
|
const records = [];
|
|
1013
1025
|
await configure({
|
|
1014
1026
|
sinks: { buffer: (record) => records.push(record) },
|
|
@@ -1023,7 +1035,7 @@ test("getNodeInfo() logs DNS failures as such", { ignore: "Deno" in globalThis &
|
|
|
1023
1035
|
reset: true
|
|
1024
1036
|
});
|
|
1025
1037
|
try {
|
|
1026
|
-
assertEquals(await
|
|
1038
|
+
assertEquals(await getNodeInfoInternal("https://dns-failure.invalid/", {}, { openHop: (url) => openPublicFetch(url, { lookup }) }), void 0);
|
|
1027
1039
|
assertEquals(records.map((r) => [
|
|
1028
1040
|
r.level,
|
|
1029
1041
|
r.rawMessage,
|
|
@@ -1038,9 +1050,743 @@ test("getNodeInfo() logs DNS failures as such", { ignore: "Deno" in globalThis &
|
|
|
1038
1050
|
assertEquals(error.reason, result === "private" ? "disallowed" : "dns");
|
|
1039
1051
|
} finally {
|
|
1040
1052
|
await reset();
|
|
1041
|
-
dns.lookup = originalLookup;
|
|
1042
1053
|
}
|
|
1043
1054
|
});
|
|
1044
1055
|
});
|
|
1056
|
+
test("getNodeInfo() bounds and cancels default Node.js DNS lookups", { ignore: detectRuntime() !== "node" }, async () => {
|
|
1057
|
+
const originalLookup = dns.lookup;
|
|
1058
|
+
const pending = [];
|
|
1059
|
+
const hostnames = [];
|
|
1060
|
+
const twoStarted = deferred();
|
|
1061
|
+
const records = [];
|
|
1062
|
+
dns.lookup = ((hostname) => {
|
|
1063
|
+
hostnames.push(hostname);
|
|
1064
|
+
const lookup = deferred();
|
|
1065
|
+
pending.push(lookup);
|
|
1066
|
+
if (hostnames.length === 2) twoStarted.resolve();
|
|
1067
|
+
return lookup.promise;
|
|
1068
|
+
});
|
|
1069
|
+
try {
|
|
1070
|
+
await configure({
|
|
1071
|
+
sinks: { buffer: (record) => records.push(record) },
|
|
1072
|
+
loggers: [{
|
|
1073
|
+
category: "fedify",
|
|
1074
|
+
sinks: ["buffer"],
|
|
1075
|
+
lowestLevel: "debug"
|
|
1076
|
+
}, {
|
|
1077
|
+
category: ["logtape", "meta"],
|
|
1078
|
+
sinks: []
|
|
1079
|
+
}],
|
|
1080
|
+
reset: true
|
|
1081
|
+
});
|
|
1082
|
+
const first = getNodeInfo("https://first.example/nodeinfo", {
|
|
1083
|
+
direct: true,
|
|
1084
|
+
timeout: 50
|
|
1085
|
+
});
|
|
1086
|
+
const second = getNodeInfo("https://second.example/nodeinfo", {
|
|
1087
|
+
direct: true,
|
|
1088
|
+
timeout: 50
|
|
1089
|
+
});
|
|
1090
|
+
await settleWithin(twoStarted.promise, 1e3);
|
|
1091
|
+
assertEquals(await settleWithin(getNodeInfo("https://third.example/nodeinfo", {
|
|
1092
|
+
direct: true,
|
|
1093
|
+
timeout: 20
|
|
1094
|
+
}), 1e3), void 0);
|
|
1095
|
+
assertEquals(await settleWithin(first, 1e3), void 0);
|
|
1096
|
+
assertEquals(await settleWithin(second, 1e3), void 0);
|
|
1097
|
+
assertEquals(hostnames, ["first.example", "second.example"]);
|
|
1098
|
+
for (const lookup of pending) lookup.resolve([{
|
|
1099
|
+
address: "93.184.215.14",
|
|
1100
|
+
family: 4
|
|
1101
|
+
}]);
|
|
1102
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1103
|
+
assertEquals(hostnames, ["first.example", "second.example"]);
|
|
1104
|
+
assertEquals(records.some((record) => String(record.rawMessage).includes("DNS lookup failed")), false);
|
|
1105
|
+
} finally {
|
|
1106
|
+
dns.lookup = originalLookup;
|
|
1107
|
+
for (const lookup of pending) lookup.resolve([{
|
|
1108
|
+
address: "93.184.215.14",
|
|
1109
|
+
family: 4
|
|
1110
|
+
}]);
|
|
1111
|
+
await reset();
|
|
1112
|
+
}
|
|
1113
|
+
});
|
|
1114
|
+
function recordHops(respond) {
|
|
1115
|
+
const events = [];
|
|
1116
|
+
const unsettled = [];
|
|
1117
|
+
let opens = 0;
|
|
1118
|
+
let closes = 0;
|
|
1119
|
+
let responses = 0;
|
|
1120
|
+
return {
|
|
1121
|
+
events,
|
|
1122
|
+
async openHop(url) {
|
|
1123
|
+
opens++;
|
|
1124
|
+
events.push(`open ${url}`);
|
|
1125
|
+
const states = [];
|
|
1126
|
+
let closed = false;
|
|
1127
|
+
return {
|
|
1128
|
+
url: new URL(url),
|
|
1129
|
+
async fetch(input) {
|
|
1130
|
+
if (closed) throw new TypeError("The fetch session is closed.");
|
|
1131
|
+
const target = new URL(input instanceof Request ? input.url : input);
|
|
1132
|
+
const id = ++responses;
|
|
1133
|
+
events.push(`fetch ${target.href} #${id}`);
|
|
1134
|
+
const fake = respond(target);
|
|
1135
|
+
const state = {
|
|
1136
|
+
id,
|
|
1137
|
+
settled: false
|
|
1138
|
+
};
|
|
1139
|
+
states.push(state);
|
|
1140
|
+
let sent = false;
|
|
1141
|
+
const body = new ReadableStream({
|
|
1142
|
+
pull(controller) {
|
|
1143
|
+
if (!sent && fake.body != null && fake.body !== "") {
|
|
1144
|
+
sent = true;
|
|
1145
|
+
controller.enqueue(new TextEncoder().encode(fake.body));
|
|
1146
|
+
return;
|
|
1147
|
+
}
|
|
1148
|
+
state.settled = true;
|
|
1149
|
+
events.push(`consumed #${id}`);
|
|
1150
|
+
controller.close();
|
|
1151
|
+
},
|
|
1152
|
+
cancel() {
|
|
1153
|
+
state.settled = true;
|
|
1154
|
+
events.push(`cancel #${id}`);
|
|
1155
|
+
if (fake.cancelFails) throw new Error("Cancellation failed");
|
|
1156
|
+
if (fake.cancelNeverSettles) return new Promise(() => {});
|
|
1157
|
+
}
|
|
1158
|
+
}, { highWaterMark: 0 });
|
|
1159
|
+
const response = new Response(body, {
|
|
1160
|
+
status: fake.status ?? 200,
|
|
1161
|
+
headers: fake.headers
|
|
1162
|
+
});
|
|
1163
|
+
if (fake.url != null) Object.defineProperty(response, "url", { value: fake.url });
|
|
1164
|
+
return response;
|
|
1165
|
+
},
|
|
1166
|
+
async close() {
|
|
1167
|
+
if (closed) return;
|
|
1168
|
+
closed = true;
|
|
1169
|
+
closes++;
|
|
1170
|
+
for (const state of states) if (!state.settled) unsettled.push(`#${state.id}`);
|
|
1171
|
+
events.push(`close ${url}`);
|
|
1172
|
+
}
|
|
1173
|
+
};
|
|
1174
|
+
},
|
|
1175
|
+
assertInvariants() {
|
|
1176
|
+
assertEquals(closes, opens);
|
|
1177
|
+
assertEquals(unsettled, []);
|
|
1178
|
+
}
|
|
1179
|
+
};
|
|
1180
|
+
}
|
|
1181
|
+
function deferred() {
|
|
1182
|
+
let resolve;
|
|
1183
|
+
return {
|
|
1184
|
+
promise: new Promise((done) => resolve = done),
|
|
1185
|
+
resolve
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
function waitForAbort(signal) {
|
|
1189
|
+
return new Promise((_, reject) => {
|
|
1190
|
+
if (signal?.aborted) {
|
|
1191
|
+
reject(signal.reason);
|
|
1192
|
+
return;
|
|
1193
|
+
}
|
|
1194
|
+
signal?.addEventListener("abort", () => reject(signal.reason), { once: true });
|
|
1195
|
+
});
|
|
1196
|
+
}
|
|
1197
|
+
test("getNodeInfo() times out while waiting for response headers", async () => {
|
|
1198
|
+
const closed = deferred();
|
|
1199
|
+
let fetches = 0;
|
|
1200
|
+
let closes = 0;
|
|
1201
|
+
let signal;
|
|
1202
|
+
const records = [];
|
|
1203
|
+
await configure({
|
|
1204
|
+
sinks: { buffer: (record) => records.push(record) },
|
|
1205
|
+
loggers: [{
|
|
1206
|
+
category: "fedify",
|
|
1207
|
+
sinks: ["buffer"],
|
|
1208
|
+
lowestLevel: "debug"
|
|
1209
|
+
}, {
|
|
1210
|
+
category: ["logtape", "meta"],
|
|
1211
|
+
sinks: []
|
|
1212
|
+
}],
|
|
1213
|
+
reset: true
|
|
1214
|
+
});
|
|
1215
|
+
try {
|
|
1216
|
+
assertEquals(await settleWithin(getNodeInfoInternal("https://example.com/nodeinfo", {
|
|
1217
|
+
direct: true,
|
|
1218
|
+
timeout: 30
|
|
1219
|
+
}, { openHop: async (url, hopSignal) => {
|
|
1220
|
+
signal = hopSignal;
|
|
1221
|
+
return {
|
|
1222
|
+
url: new URL(url),
|
|
1223
|
+
fetch(_input, init) {
|
|
1224
|
+
fetches++;
|
|
1225
|
+
assertEquals(init?.signal, hopSignal);
|
|
1226
|
+
return waitForAbort(init?.signal);
|
|
1227
|
+
},
|
|
1228
|
+
async close() {
|
|
1229
|
+
closes++;
|
|
1230
|
+
closed.resolve();
|
|
1231
|
+
}
|
|
1232
|
+
};
|
|
1233
|
+
} }), 1e3), void 0);
|
|
1234
|
+
await settleWithin(closed.promise, 1e3);
|
|
1235
|
+
assertEquals(fetches, 1);
|
|
1236
|
+
assertEquals(closes, 1);
|
|
1237
|
+
assertEquals(signal?.aborted, true);
|
|
1238
|
+
assertEquals(records.filter((record) => record.level === "error").length, 1);
|
|
1239
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1240
|
+
assertEquals(records.filter((record) => record.level === "error").length, 1);
|
|
1241
|
+
} finally {
|
|
1242
|
+
await reset();
|
|
1243
|
+
}
|
|
1244
|
+
});
|
|
1245
|
+
test("getNodeInfo() times out while a small response body stalls", async () => {
|
|
1246
|
+
const closed = deferred();
|
|
1247
|
+
let bodyCancelled = false;
|
|
1248
|
+
let signal;
|
|
1249
|
+
assertEquals(await settleWithin(getNodeInfoInternal("https://example.com/nodeinfo", {
|
|
1250
|
+
direct: true,
|
|
1251
|
+
timeout: 30
|
|
1252
|
+
}, { async openHop(url, hopSignal) {
|
|
1253
|
+
signal = hopSignal;
|
|
1254
|
+
return {
|
|
1255
|
+
url: new URL(url),
|
|
1256
|
+
async fetch(_input, init) {
|
|
1257
|
+
assertEquals(init?.signal, hopSignal);
|
|
1258
|
+
return new Response(new ReadableStream({
|
|
1259
|
+
start(controller) {
|
|
1260
|
+
controller.enqueue(new TextEncoder().encode("{\"software\":"));
|
|
1261
|
+
},
|
|
1262
|
+
cancel() {
|
|
1263
|
+
bodyCancelled = true;
|
|
1264
|
+
}
|
|
1265
|
+
}));
|
|
1266
|
+
},
|
|
1267
|
+
async close() {
|
|
1268
|
+
closed.resolve();
|
|
1269
|
+
}
|
|
1270
|
+
};
|
|
1271
|
+
} }), 1e3), void 0);
|
|
1272
|
+
await settleWithin(closed.promise, 1e3);
|
|
1273
|
+
assertEquals(signal?.aborted, true);
|
|
1274
|
+
assertEquals(bodyCancelled, true);
|
|
1275
|
+
});
|
|
1276
|
+
test({
|
|
1277
|
+
name: "getNodeInfo() aborts stalled HTTP responses",
|
|
1278
|
+
ignore: "navigator" in globalThis && navigator.userAgent === "Cloudflare-Workers" || "Deno" in globalThis && (await Deno.permissions.query({ name: "net" })).state !== "granted",
|
|
1279
|
+
async fn(t) {
|
|
1280
|
+
for (const stage of ["headers", "body"]) await t.step(stage, async () => {
|
|
1281
|
+
const disconnected = deferred();
|
|
1282
|
+
const server = createServer((request, response) => {
|
|
1283
|
+
response.on("close", () => disconnected.resolve());
|
|
1284
|
+
request.socket.on("close", () => disconnected.resolve());
|
|
1285
|
+
if (stage === "body") {
|
|
1286
|
+
response.writeHead(200, { "Content-Type": "application/json" });
|
|
1287
|
+
response.write("{\"software\":");
|
|
1288
|
+
}
|
|
1289
|
+
});
|
|
1290
|
+
try {
|
|
1291
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
1292
|
+
const address = server.address();
|
|
1293
|
+
if (address == null || typeof address === "string") throw new Error("Expected a TCP server address.");
|
|
1294
|
+
assertEquals(await settleWithin(getNodeInfoInternal(`http://127.0.0.1:${address.port}/nodeinfo`, {
|
|
1295
|
+
direct: true,
|
|
1296
|
+
timeout: 200
|
|
1297
|
+
}, { openHop: openDirectFetch }), 2e3).catch((error) => {
|
|
1298
|
+
throw new Error(`${stage} lookup did not finish`, { cause: error });
|
|
1299
|
+
}), void 0);
|
|
1300
|
+
await settleWithin(disconnected.promise, 2e3).catch((error) => {
|
|
1301
|
+
throw new Error(`${stage} connection did not close`, { cause: error });
|
|
1302
|
+
});
|
|
1303
|
+
} finally {
|
|
1304
|
+
server.closeAllConnections();
|
|
1305
|
+
await new Promise((resolve) => server.close(() => resolve()));
|
|
1306
|
+
}
|
|
1307
|
+
});
|
|
1308
|
+
}
|
|
1309
|
+
});
|
|
1310
|
+
test("getNodeInfo() shares one timeout across discovery and document", async () => {
|
|
1311
|
+
const documentRequested = deferred();
|
|
1312
|
+
const documentClosed = deferred();
|
|
1313
|
+
let discoverySignal;
|
|
1314
|
+
let documentSignal;
|
|
1315
|
+
const descriptor = JSON.stringify({ links: [{
|
|
1316
|
+
rel: "http://nodeinfo.diaspora.software/ns/schema/2.1",
|
|
1317
|
+
href: "https://example.com/nodeinfo/2.1"
|
|
1318
|
+
}] });
|
|
1319
|
+
const result = getNodeInfoInternal("https://example.com/", { timeout: 200 }, { async openHop(url) {
|
|
1320
|
+
const isDocument = url.endsWith("/nodeinfo/2.1");
|
|
1321
|
+
return {
|
|
1322
|
+
url: new URL(url),
|
|
1323
|
+
async fetch(_input, init) {
|
|
1324
|
+
if (isDocument) {
|
|
1325
|
+
documentSignal = init?.signal ?? void 0;
|
|
1326
|
+
documentRequested.resolve();
|
|
1327
|
+
return await waitForAbort(init?.signal);
|
|
1328
|
+
}
|
|
1329
|
+
discoverySignal = init?.signal ?? void 0;
|
|
1330
|
+
return new Response(descriptor);
|
|
1331
|
+
},
|
|
1332
|
+
async close() {
|
|
1333
|
+
if (isDocument) documentClosed.resolve();
|
|
1334
|
+
}
|
|
1335
|
+
};
|
|
1336
|
+
} });
|
|
1337
|
+
await settleWithin(documentRequested.promise, 1e3);
|
|
1338
|
+
assertStrictEquals(documentSignal, discoverySignal);
|
|
1339
|
+
assertEquals(await settleWithin(result, 1e3), void 0);
|
|
1340
|
+
await settleWithin(documentClosed.promise, 1e3);
|
|
1341
|
+
assertEquals(discoverySignal?.aborted, true);
|
|
1342
|
+
});
|
|
1343
|
+
test("getNodeInfo() honors caller cancellation without a timeout", async () => {
|
|
1344
|
+
const caller = new AbortController();
|
|
1345
|
+
const closed = deferred();
|
|
1346
|
+
let requestSignal;
|
|
1347
|
+
assertEquals(await settleWithin(getNodeInfoInternal("https://example.com/nodeinfo", {
|
|
1348
|
+
direct: true,
|
|
1349
|
+
timeout: null,
|
|
1350
|
+
signal: caller.signal
|
|
1351
|
+
}, { async openHop(url, signal) {
|
|
1352
|
+
requestSignal = signal;
|
|
1353
|
+
return {
|
|
1354
|
+
url: new URL(url),
|
|
1355
|
+
fetch(_input, init) {
|
|
1356
|
+
assertStrictEquals(init?.signal, signal);
|
|
1357
|
+
caller.abort(/* @__PURE__ */ new Error("Caller stopped waiting"));
|
|
1358
|
+
return waitForAbort(init?.signal);
|
|
1359
|
+
},
|
|
1360
|
+
async close() {
|
|
1361
|
+
closed.resolve();
|
|
1362
|
+
}
|
|
1363
|
+
};
|
|
1364
|
+
} }), 1e3), void 0);
|
|
1365
|
+
await settleWithin(closed.promise, 1e3);
|
|
1366
|
+
assertEquals(requestSignal?.aborted, true);
|
|
1367
|
+
});
|
|
1368
|
+
test("getNodeInfo() stops before fetching when caller already aborted", async () => {
|
|
1369
|
+
const caller = new AbortController();
|
|
1370
|
+
caller.abort();
|
|
1371
|
+
let opened = false;
|
|
1372
|
+
assertEquals(await getNodeInfoInternal("https://example.com/nodeinfo", {
|
|
1373
|
+
direct: true,
|
|
1374
|
+
signal: caller.signal
|
|
1375
|
+
}, { async openHop() {
|
|
1376
|
+
opened = true;
|
|
1377
|
+
throw new Error("Unexpected fetch");
|
|
1378
|
+
} }), void 0);
|
|
1379
|
+
assertEquals(opened, false);
|
|
1380
|
+
});
|
|
1381
|
+
test("getNodeInfo() closes a session opened just after its timeout", async () => {
|
|
1382
|
+
const closed = deferred();
|
|
1383
|
+
let fetches = 0;
|
|
1384
|
+
let closes = 0;
|
|
1385
|
+
assertEquals(await settleWithin(getNodeInfoInternal("https://example.com/nodeinfo", {
|
|
1386
|
+
direct: true,
|
|
1387
|
+
timeout: 25
|
|
1388
|
+
}, { async openHop(url) {
|
|
1389
|
+
await new Promise((resolve) => setTimeout(resolve, 55));
|
|
1390
|
+
return {
|
|
1391
|
+
url: new URL(url),
|
|
1392
|
+
async fetch() {
|
|
1393
|
+
fetches++;
|
|
1394
|
+
return Response.json({ software: {
|
|
1395
|
+
name: "x",
|
|
1396
|
+
version: "1"
|
|
1397
|
+
} });
|
|
1398
|
+
},
|
|
1399
|
+
async close() {
|
|
1400
|
+
closes++;
|
|
1401
|
+
closed.resolve();
|
|
1402
|
+
}
|
|
1403
|
+
};
|
|
1404
|
+
} }), 1e3), void 0);
|
|
1405
|
+
await settleWithin(closed.promise, 1e3);
|
|
1406
|
+
assertEquals(fetches, 0);
|
|
1407
|
+
assertEquals(closes, 1);
|
|
1408
|
+
});
|
|
1409
|
+
test("getNodeInfo() does not log a late DNS failure as another error", async () => {
|
|
1410
|
+
const records = [];
|
|
1411
|
+
const finished = deferred();
|
|
1412
|
+
await configure({
|
|
1413
|
+
sinks: { buffer: (record) => records.push(record) },
|
|
1414
|
+
loggers: [{
|
|
1415
|
+
category: "fedify",
|
|
1416
|
+
sinks: ["buffer"],
|
|
1417
|
+
lowestLevel: "debug"
|
|
1418
|
+
}, {
|
|
1419
|
+
category: ["logtape", "meta"],
|
|
1420
|
+
sinks: []
|
|
1421
|
+
}],
|
|
1422
|
+
reset: true
|
|
1423
|
+
});
|
|
1424
|
+
try {
|
|
1425
|
+
assertEquals(await settleWithin(getNodeInfoInternal("https://example.com/nodeinfo", {
|
|
1426
|
+
direct: true,
|
|
1427
|
+
timeout: 25
|
|
1428
|
+
}, { async openHop() {
|
|
1429
|
+
try {
|
|
1430
|
+
await new Promise((resolve) => setTimeout(resolve, 55));
|
|
1431
|
+
throw new UrlError("DNS lookup failed", { reason: "dns" });
|
|
1432
|
+
} finally {
|
|
1433
|
+
finished.resolve();
|
|
1434
|
+
}
|
|
1435
|
+
} }), 1e3), void 0);
|
|
1436
|
+
await settleWithin(finished.promise, 1e3);
|
|
1437
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1438
|
+
assertEquals(records.filter((record) => record.level === "error").length, 1);
|
|
1439
|
+
} finally {
|
|
1440
|
+
await reset();
|
|
1441
|
+
}
|
|
1442
|
+
});
|
|
1443
|
+
test("getNodeInfo() cancels a response returned after timeout", async () => {
|
|
1444
|
+
const closed = deferred();
|
|
1445
|
+
let bodyCancelled = false;
|
|
1446
|
+
let closes = 0;
|
|
1447
|
+
assertEquals(await settleWithin(getNodeInfoInternal("https://example.com/nodeinfo", {
|
|
1448
|
+
direct: true,
|
|
1449
|
+
timeout: 25
|
|
1450
|
+
}, { async openHop(url) {
|
|
1451
|
+
return {
|
|
1452
|
+
url: new URL(url),
|
|
1453
|
+
async fetch() {
|
|
1454
|
+
await new Promise((resolve) => setTimeout(resolve, 55));
|
|
1455
|
+
return new Response(new ReadableStream({ cancel() {
|
|
1456
|
+
bodyCancelled = true;
|
|
1457
|
+
} }));
|
|
1458
|
+
},
|
|
1459
|
+
async close() {
|
|
1460
|
+
closes++;
|
|
1461
|
+
closed.resolve();
|
|
1462
|
+
}
|
|
1463
|
+
};
|
|
1464
|
+
} }), 1e3), void 0);
|
|
1465
|
+
await settleWithin(closed.promise, 1e3);
|
|
1466
|
+
assertEquals(closes, 1);
|
|
1467
|
+
assertEquals(bodyCancelled, true);
|
|
1468
|
+
});
|
|
1469
|
+
test("getNodeInfo() validates its timeout", async () => {
|
|
1470
|
+
for (const timeout of [
|
|
1471
|
+
0,
|
|
1472
|
+
-1,
|
|
1473
|
+
NaN,
|
|
1474
|
+
Infinity,
|
|
1475
|
+
2147483648
|
|
1476
|
+
]) await assertRejects(() => getNodeInfoInternal("https://example.com/", { timeout }), RangeError);
|
|
1477
|
+
});
|
|
1478
|
+
test("getNodeInfo() can disable its timeout", async () => {
|
|
1479
|
+
const value = { software: {
|
|
1480
|
+
name: "x",
|
|
1481
|
+
version: "1"
|
|
1482
|
+
} };
|
|
1483
|
+
assertEquals(await getNodeInfoInternal("https://example.com/nodeinfo", {
|
|
1484
|
+
direct: true,
|
|
1485
|
+
parse: "none",
|
|
1486
|
+
timeout: null
|
|
1487
|
+
}, { async openHop(url, signal) {
|
|
1488
|
+
assertEquals(signal, void 0);
|
|
1489
|
+
return {
|
|
1490
|
+
url: new URL(url),
|
|
1491
|
+
async fetch(_input, init) {
|
|
1492
|
+
assertEquals(init?.signal, void 0);
|
|
1493
|
+
return Response.json(value);
|
|
1494
|
+
},
|
|
1495
|
+
async close() {}
|
|
1496
|
+
};
|
|
1497
|
+
} }), value);
|
|
1498
|
+
});
|
|
1499
|
+
test("getNodeInfo() clears its timer after a successful fetch", async () => {
|
|
1500
|
+
let signal;
|
|
1501
|
+
const value = { software: {
|
|
1502
|
+
name: "x",
|
|
1503
|
+
version: "1"
|
|
1504
|
+
} };
|
|
1505
|
+
assertEquals(await getNodeInfoInternal("https://example.com/nodeinfo", {
|
|
1506
|
+
direct: true,
|
|
1507
|
+
parse: "none",
|
|
1508
|
+
timeout: 100
|
|
1509
|
+
}, { async openHop(url, hopSignal) {
|
|
1510
|
+
signal = hopSignal;
|
|
1511
|
+
return {
|
|
1512
|
+
url: new URL(url),
|
|
1513
|
+
async fetch() {
|
|
1514
|
+
return Response.json(value);
|
|
1515
|
+
},
|
|
1516
|
+
async close() {}
|
|
1517
|
+
};
|
|
1518
|
+
} }), value);
|
|
1519
|
+
await new Promise((resolve) => setTimeout(resolve, 120));
|
|
1520
|
+
assertEquals(signal?.aborted, false);
|
|
1521
|
+
});
|
|
1522
|
+
test("getNodeInfo() closes each hop after cleaning up its body", async (t) => {
|
|
1523
|
+
const wellKnown = "https://example.com/.well-known/nodeinfo";
|
|
1524
|
+
const wellKnown2 = "https://example.com/.well-known/nodeinfo2";
|
|
1525
|
+
const documentUrl = "https://example.com/nodeinfo/2.1";
|
|
1526
|
+
const rawDocument = {
|
|
1527
|
+
software: {
|
|
1528
|
+
name: "foo",
|
|
1529
|
+
version: "1.2.3"
|
|
1530
|
+
},
|
|
1531
|
+
protocols: ["activitypub"],
|
|
1532
|
+
usage: {
|
|
1533
|
+
users: {},
|
|
1534
|
+
localPosts: 0,
|
|
1535
|
+
localComments: 0
|
|
1536
|
+
}
|
|
1537
|
+
};
|
|
1538
|
+
const descriptor = JSON.stringify({ links: [{
|
|
1539
|
+
rel: "http://nodeinfo.diaspora.software/ns/schema/2.1",
|
|
1540
|
+
href: documentUrl
|
|
1541
|
+
}] });
|
|
1542
|
+
await t.step("redirect", async () => {
|
|
1543
|
+
const recorder = recordHops((url) => {
|
|
1544
|
+
switch (url.href) {
|
|
1545
|
+
case wellKnown: return {
|
|
1546
|
+
status: 302,
|
|
1547
|
+
headers: { Location: "/.well-known/nodeinfo2" },
|
|
1548
|
+
body: "redirecting",
|
|
1549
|
+
url: "https://93.184.215.14/.well-known/nodeinfo"
|
|
1550
|
+
};
|
|
1551
|
+
case wellKnown2: return { body: descriptor };
|
|
1552
|
+
default: return { body: JSON.stringify(rawDocument) };
|
|
1553
|
+
}
|
|
1554
|
+
});
|
|
1555
|
+
assertEquals(await getNodeInfoInternal("https://example.com/", { parse: "none" }, { openHop: recorder.openHop }), rawDocument);
|
|
1556
|
+
assertEquals(recorder.events, [
|
|
1557
|
+
`open ${wellKnown}`,
|
|
1558
|
+
`fetch ${wellKnown} #1`,
|
|
1559
|
+
"cancel #1",
|
|
1560
|
+
`close ${wellKnown}`,
|
|
1561
|
+
`open ${wellKnown2}`,
|
|
1562
|
+
`fetch ${wellKnown2} #2`,
|
|
1563
|
+
"consumed #2",
|
|
1564
|
+
`close ${wellKnown2}`,
|
|
1565
|
+
`open ${documentUrl}`,
|
|
1566
|
+
`fetch ${documentUrl} #3`,
|
|
1567
|
+
"consumed #3",
|
|
1568
|
+
`close ${documentUrl}`
|
|
1569
|
+
]);
|
|
1570
|
+
recorder.assertInvariants();
|
|
1571
|
+
});
|
|
1572
|
+
await t.step("non-2xx responses", async () => {
|
|
1573
|
+
const recorder = recordHops(() => ({
|
|
1574
|
+
status: 404,
|
|
1575
|
+
body: "not found"
|
|
1576
|
+
}));
|
|
1577
|
+
assertEquals(await getNodeInfoInternal("https://example.com/", {}, { openHop: recorder.openHop }), void 0);
|
|
1578
|
+
assertEquals(await getNodeInfoInternal(documentUrl, { direct: true }, { openHop: recorder.openHop }), void 0);
|
|
1579
|
+
assertEquals(recorder.events, [
|
|
1580
|
+
`open ${wellKnown}`,
|
|
1581
|
+
`fetch ${wellKnown} #1`,
|
|
1582
|
+
"cancel #1",
|
|
1583
|
+
`close ${wellKnown}`,
|
|
1584
|
+
`open ${documentUrl}`,
|
|
1585
|
+
`fetch ${documentUrl} #2`,
|
|
1586
|
+
"cancel #2",
|
|
1587
|
+
`close ${documentUrl}`
|
|
1588
|
+
]);
|
|
1589
|
+
recorder.assertInvariants();
|
|
1590
|
+
});
|
|
1591
|
+
await t.step("too many redirections", async () => {
|
|
1592
|
+
const recorder = recordHops((url) => ({
|
|
1593
|
+
status: 302,
|
|
1594
|
+
headers: { Location: `?hop=${Number(url.searchParams.get("hop") ?? 0) + 1}` },
|
|
1595
|
+
body: "redirecting"
|
|
1596
|
+
}));
|
|
1597
|
+
assertEquals(await getNodeInfoInternal("https://example.com/", {}, { openHop: recorder.openHop }), void 0);
|
|
1598
|
+
assertEquals(recorder.events.filter((e) => e.startsWith("open ")).length, 5);
|
|
1599
|
+
assertEquals(recorder.events.filter((e) => e.startsWith("cancel ")).length, 5);
|
|
1600
|
+
recorder.assertInvariants();
|
|
1601
|
+
});
|
|
1602
|
+
await t.step("self-redirect cycle", async () => {
|
|
1603
|
+
const recorder = recordHops(() => ({
|
|
1604
|
+
status: 302,
|
|
1605
|
+
headers: { Location: "/.well-known/nodeinfo#another-fragment" },
|
|
1606
|
+
body: "redirecting"
|
|
1607
|
+
}));
|
|
1608
|
+
assertEquals(await getNodeInfoInternal("https://example.com/", {}, { openHop: recorder.openHop }), void 0);
|
|
1609
|
+
assertEquals(recorder.events, [
|
|
1610
|
+
`open ${wellKnown}`,
|
|
1611
|
+
`fetch ${wellKnown} #1`,
|
|
1612
|
+
"cancel #1",
|
|
1613
|
+
`close ${wellKnown}`
|
|
1614
|
+
]);
|
|
1615
|
+
recorder.assertInvariants();
|
|
1616
|
+
});
|
|
1617
|
+
await t.step("two-URL redirect cycle", async () => {
|
|
1618
|
+
const recorder = recordHops((url) => ({
|
|
1619
|
+
status: 302,
|
|
1620
|
+
headers: { Location: url.href === wellKnown ? "/.well-known/nodeinfo2" : "/.well-known/nodeinfo" },
|
|
1621
|
+
body: "redirecting"
|
|
1622
|
+
}));
|
|
1623
|
+
assertEquals(await getNodeInfoInternal("https://example.com/", {}, { openHop: recorder.openHop }), void 0);
|
|
1624
|
+
assertEquals(recorder.events.filter((e) => e.startsWith("open ")), [`open ${wellKnown}`, `open ${wellKnown2}`]);
|
|
1625
|
+
recorder.assertInvariants();
|
|
1626
|
+
});
|
|
1627
|
+
await t.step("failed redirect body cancellation", async () => {
|
|
1628
|
+
const recorder = recordHops((url) => {
|
|
1629
|
+
switch (url.href) {
|
|
1630
|
+
case wellKnown: return {
|
|
1631
|
+
status: 302,
|
|
1632
|
+
headers: { Location: "/.well-known/nodeinfo2" },
|
|
1633
|
+
body: "redirecting",
|
|
1634
|
+
cancelFails: true
|
|
1635
|
+
};
|
|
1636
|
+
case wellKnown2: return { body: descriptor };
|
|
1637
|
+
default: return {
|
|
1638
|
+
status: 404,
|
|
1639
|
+
body: "not found",
|
|
1640
|
+
cancelFails: true
|
|
1641
|
+
};
|
|
1642
|
+
}
|
|
1643
|
+
});
|
|
1644
|
+
const records = [];
|
|
1645
|
+
await configure({
|
|
1646
|
+
sinks: { buffer: (record) => records.push(record) },
|
|
1647
|
+
loggers: [{
|
|
1648
|
+
category: "fedify",
|
|
1649
|
+
sinks: ["buffer"],
|
|
1650
|
+
lowestLevel: "debug"
|
|
1651
|
+
}, {
|
|
1652
|
+
category: ["logtape", "meta"],
|
|
1653
|
+
sinks: []
|
|
1654
|
+
}],
|
|
1655
|
+
reset: true
|
|
1656
|
+
});
|
|
1657
|
+
try {
|
|
1658
|
+
assertEquals(await getNodeInfoInternal("https://example.com/", {}, { openHop: recorder.openHop }), void 0);
|
|
1659
|
+
const failures = records.filter((r) => r.rawMessage === "Failed to cancel the response body from {url}: {error}");
|
|
1660
|
+
assertEquals(failures.map((r) => [r.level, r.properties.url]), [["debug", wellKnown], ["debug", documentUrl]]);
|
|
1661
|
+
for (const { properties: { error } } of failures) {
|
|
1662
|
+
assertInstanceOf(error, Error);
|
|
1663
|
+
assertEquals(error.message, "Cancellation failed");
|
|
1664
|
+
}
|
|
1665
|
+
} finally {
|
|
1666
|
+
await reset();
|
|
1667
|
+
}
|
|
1668
|
+
assertEquals(recorder.events, [
|
|
1669
|
+
`open ${wellKnown}`,
|
|
1670
|
+
`fetch ${wellKnown} #1`,
|
|
1671
|
+
"cancel #1",
|
|
1672
|
+
`close ${wellKnown}`,
|
|
1673
|
+
`open ${wellKnown2}`,
|
|
1674
|
+
`fetch ${wellKnown2} #2`,
|
|
1675
|
+
"consumed #2",
|
|
1676
|
+
`close ${wellKnown2}`,
|
|
1677
|
+
`open ${documentUrl}`,
|
|
1678
|
+
`fetch ${documentUrl} #3`,
|
|
1679
|
+
"cancel #3",
|
|
1680
|
+
`close ${documentUrl}`
|
|
1681
|
+
]);
|
|
1682
|
+
recorder.assertInvariants();
|
|
1683
|
+
});
|
|
1684
|
+
await t.step("never-settling body cancellation", async () => {
|
|
1685
|
+
const recorder = recordHops((url) => {
|
|
1686
|
+
switch (url.href) {
|
|
1687
|
+
case wellKnown: return {
|
|
1688
|
+
status: 302,
|
|
1689
|
+
headers: { Location: "/.well-known/nodeinfo2" },
|
|
1690
|
+
body: "redirecting",
|
|
1691
|
+
cancelNeverSettles: true
|
|
1692
|
+
};
|
|
1693
|
+
case wellKnown2: return { body: descriptor };
|
|
1694
|
+
default: return {
|
|
1695
|
+
status: 404,
|
|
1696
|
+
body: "not found",
|
|
1697
|
+
cancelNeverSettles: true
|
|
1698
|
+
};
|
|
1699
|
+
}
|
|
1700
|
+
});
|
|
1701
|
+
assertEquals(await settleWithin(getNodeInfoInternal("https://example.com/", {}, { openHop: recorder.openHop })), void 0);
|
|
1702
|
+
assertEquals(recorder.events, [
|
|
1703
|
+
`open ${wellKnown}`,
|
|
1704
|
+
`fetch ${wellKnown} #1`,
|
|
1705
|
+
"cancel #1",
|
|
1706
|
+
`close ${wellKnown}`,
|
|
1707
|
+
`open ${wellKnown2}`,
|
|
1708
|
+
`fetch ${wellKnown2} #2`,
|
|
1709
|
+
"consumed #2",
|
|
1710
|
+
`close ${wellKnown2}`,
|
|
1711
|
+
`open ${documentUrl}`,
|
|
1712
|
+
`fetch ${documentUrl} #3`,
|
|
1713
|
+
"cancel #3",
|
|
1714
|
+
`close ${documentUrl}`
|
|
1715
|
+
]);
|
|
1716
|
+
recorder.assertInvariants();
|
|
1717
|
+
});
|
|
1718
|
+
await t.step("failure while consuming", async () => {
|
|
1719
|
+
const recorder = recordHops(() => ({ body: "not json" }));
|
|
1720
|
+
assertEquals(await getNodeInfoInternal("https://example.com/", {}, { openHop: recorder.openHop }), void 0);
|
|
1721
|
+
assertEquals(recorder.events, [
|
|
1722
|
+
`open ${wellKnown}`,
|
|
1723
|
+
`fetch ${wellKnown} #1`,
|
|
1724
|
+
"consumed #1",
|
|
1725
|
+
`close ${wellKnown}`
|
|
1726
|
+
]);
|
|
1727
|
+
recorder.assertInvariants();
|
|
1728
|
+
});
|
|
1729
|
+
});
|
|
1730
|
+
/**
|
|
1731
|
+
* Rejects if the given promise does not settle within the timeout, so that
|
|
1732
|
+
* a hang fails the test instead of blocking it.
|
|
1733
|
+
*/
|
|
1734
|
+
async function settleWithin(promise, ms = 5e3) {
|
|
1735
|
+
let timer;
|
|
1736
|
+
try {
|
|
1737
|
+
return await Promise.race([promise, new Promise((_, reject) => {
|
|
1738
|
+
timer = setTimeout(() => reject(/* @__PURE__ */ new Error(`Did not settle within ${ms} ms.`)), ms);
|
|
1739
|
+
})]);
|
|
1740
|
+
} finally {
|
|
1741
|
+
clearTimeout(timer);
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
function neverCancellableResponse(init) {
|
|
1745
|
+
return new Response(new ReadableStream({
|
|
1746
|
+
start(controller) {
|
|
1747
|
+
controller.enqueue(new TextEncoder().encode("never cancelled"));
|
|
1748
|
+
},
|
|
1749
|
+
cancel: () => new Promise(() => {})
|
|
1750
|
+
}), init);
|
|
1751
|
+
}
|
|
1752
|
+
test("getNodeInfo() does not wait for cancelling response bodies", async (t) => {
|
|
1753
|
+
const rawDocument = {
|
|
1754
|
+
software: {
|
|
1755
|
+
name: "foo",
|
|
1756
|
+
version: "1.2.3"
|
|
1757
|
+
},
|
|
1758
|
+
protocols: ["activitypub"],
|
|
1759
|
+
usage: {
|
|
1760
|
+
users: {},
|
|
1761
|
+
localPosts: 0,
|
|
1762
|
+
localComments: 0
|
|
1763
|
+
}
|
|
1764
|
+
};
|
|
1765
|
+
esm_default.mockGlobal();
|
|
1766
|
+
try {
|
|
1767
|
+
await t.step("redirect", async () => {
|
|
1768
|
+
esm_default.removeRoutes();
|
|
1769
|
+
esm_default.get("https://example.com/.well-known/nodeinfo", () => neverCancellableResponse({
|
|
1770
|
+
status: 302,
|
|
1771
|
+
headers: { Location: "/.well-known/nodeinfo2" }
|
|
1772
|
+
}));
|
|
1773
|
+
esm_default.get("https://example.com/.well-known/nodeinfo2", () => Response.json({ links: [{
|
|
1774
|
+
rel: "http://nodeinfo.diaspora.software/ns/schema/2.1",
|
|
1775
|
+
href: "https://example.com/nodeinfo/2.1"
|
|
1776
|
+
}] }));
|
|
1777
|
+
esm_default.get("https://example.com/nodeinfo/2.1", () => Response.json(rawDocument));
|
|
1778
|
+
assertEquals(await settleWithin(getNodeInfoDirect("https://example.com/", { parse: "none" })), rawDocument);
|
|
1779
|
+
});
|
|
1780
|
+
await t.step("non-2xx responses", async () => {
|
|
1781
|
+
esm_default.removeRoutes();
|
|
1782
|
+
esm_default.get("https://example.com/.well-known/nodeinfo", () => neverCancellableResponse({ status: 404 }));
|
|
1783
|
+
esm_default.get("https://example.com/nodeinfo/2.1", () => neverCancellableResponse({ status: 404 }));
|
|
1784
|
+
assertEquals(await settleWithin(getNodeInfoDirect("https://example.com/")), void 0);
|
|
1785
|
+
assertEquals(await settleWithin(getNodeInfoDirect("https://example.com/nodeinfo/2.1", { direct: true })), void 0);
|
|
1786
|
+
});
|
|
1787
|
+
} finally {
|
|
1788
|
+
esm_default.hardReset();
|
|
1789
|
+
}
|
|
1790
|
+
});
|
|
1045
1791
|
//#endregion
|
|
1046
1792
|
export {};
|