@fedify/fedify 2.1.30 → 2.1.32
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-DZ4JHJD8.mjs +111 -0
- package/dist/{builder-CTpfuuVp.mjs → builder--sc-rozj.mjs} +3 -3
- package/dist/callback-BARPujV7.d.mts +2388 -0
- package/dist/{client-CAM_bQXx.d.cts → client-9DPcy_Yx.d.ts} +16 -1
- package/dist/{client-CSddvgWN.d.ts → client-B3ruDgWg.d.cts} +16 -1
- package/dist/{client-CjDMsn4a.mjs → client-cpPaW7Ww.mjs} +189 -65
- package/dist/compat/attachment-arrays.test.d.mts +2 -0
- package/dist/compat/attachment-arrays.test.mjs +254 -0
- package/dist/compat/mod.d.cts +1 -1
- package/dist/compat/mod.d.ts +1 -1
- package/dist/compat/transformers.test.mjs +1 -1
- package/dist/{context-DLLaDau8.d.ts → context-DE2YrQwM.d.ts} +56 -13
- package/dist/{context-BNwv9Q1v.d.cts → context-y6ZUQCMf.d.cts} +56 -13
- package/dist/{deno-BATJ1hq6.mjs → deno-L23b3Hba.mjs} +1 -1
- package/dist/docloader-DUXr9k_O.mjs +124 -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 +280 -0
- package/dist/federation/handler.test.mjs +208 -16
- package/dist/federation/idempotency.test.mjs +2 -2
- package/dist/federation/inbox.test.mjs +1 -1
- package/dist/federation/middleware.test.mjs +1559 -26
- 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-CK3efjgM.js → http-BtbMAgTA.js} +176 -105
- package/dist/{http-Dl00HXr4.d.ts → http-C8qLHs_Z.d.cts} +3 -4
- package/dist/{http-BoR1M3W7.cjs → http-D5woFKA_.cjs} +177 -106
- package/dist/{http-DqZhyEso.d.cts → http-D8WmcRAF.d.ts} +3 -4
- package/dist/{http-Bjd6I92u.mjs → http-HX6wVyJh.mjs} +198 -98
- package/dist/{inbox-9MlBGgs9.mjs → inbox-CL7A0HXT.mjs} +1 -1
- package/dist/{key-ClQh9Qqh.mjs → key-Dai3f5yI.mjs} +3 -2
- package/dist/{kv-cache-BxSx_Iyp.cjs → kv-cache-B0V_e231.cjs} +59 -17
- package/dist/{kv-cache-ImApO425.js → kv-cache-BsDg-cT8.js} +60 -18
- package/dist/{ld-Crg0Xmwr.mjs → ld-DN6x1iZ8.mjs} +153 -21
- package/dist/ld-signature-DoZcbsZR.mjs +23 -0
- package/dist/{middleware-jL9p9V9G.cjs → middleware-Cl6k5Hrm.cjs} +1 -1
- package/dist/{middleware-CmD3lpy4.cjs → middleware-CrfRuKCf.cjs} +728 -143
- package/dist/{middleware-CE0b69gj.mjs → middleware-DiquMgtt.mjs} +444 -115
- package/dist/{middleware-C2AJFk8W.js → middleware-DunNtbo9.js} +727 -143
- package/dist/{middleware-Bi5D6DBT.mjs → middleware-SnQS8eIb.mjs} +1 -1
- package/dist/{mod-DiuFTYUt.d.cts → mod-CehHJOV4.d.cts} +30 -13
- package/dist/{mod-CWJNbyju.d.ts → mod-DLJpnqdw.d.ts} +1 -1
- package/dist/{mod-BLDHhuW0.d.ts → mod-Mh3oIuRK.d.ts} +30 -13
- package/dist/{mod-DNEgTI8M.d.ts → mod-REfljt0T.d.ts} +4 -9
- package/dist/{mod-Cn9GJ5ex.d.cts → mod-UvIGUHqW.d.cts} +4 -9
- package/dist/{mod-BmlfRBmU.d.cts → mod-sTa1Zzxk.d.cts} +1 -1
- 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 -19
- 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/{owner-BRoKojK1.mjs → owner-DwyDorZI.mjs} +2 -2
- package/dist/{proof-CHXVlsH_.cjs → proof-Be-4K4jn.cjs} +193 -46
- package/dist/{proof-DMIAezxz.mjs → proof-DYjmyrHh.mjs} +38 -13
- package/dist/{proof-7R4s10TJ.js → proof-Du4-5SHY.js} +187 -46
- package/dist/send-BGSfQkIK.mjs +416 -0
- package/dist/sig/http.test.mjs +506 -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 +95 -3
- package/dist/{temporal-DR0JahMu.mjs → temporal-CNCchWly.mjs} +3 -5
- package/dist/testing/mod.d.mts +1 -2177
- package/dist/{types-DRPjmHVV.cjs → types-B5wRhemi.cjs} +187 -63
- package/dist/{types-miAYfiGO.js → types-CXy2lRVU.js} +188 -64
- package/dist/utils/docloader.test.mjs +316 -32
- 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 +7 -7
- package/dist/docloader-OLWc7P2D.mjs +0 -82
- package/dist/send-BHIt_8ia.mjs +0 -260
- /package/dist/{collection-BD6-SZ6O.mjs → collection-DnoYNwBC.mjs} +0 -0
- /package/dist/{negotiation-BehA2uul.mjs → negotiation-xb0QR3u_.mjs} +0 -0
- /package/dist/{retry-B_E3V_Dx.mjs → retry-CI1flGDD.mjs} +0 -0
- /package/dist/{types-DCP0WLdt.mjs → types-s4mCTGkT.mjs} +0 -0
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
import "@js-temporal/polyfill";
|
|
2
|
+
import "urlpattern-polyfill";
|
|
3
|
+
globalThis.addEventListener = () => {};
|
|
4
|
+
import { n as version, t as name } from "./deno-L23b3Hba.mjs";
|
|
5
|
+
import { r as doubleKnockScoped } from "./http-HX6wVyJh.mjs";
|
|
6
|
+
import { SpanKind, SpanStatusCode, trace } from "@opentelemetry/api";
|
|
7
|
+
import { FetchError, UrlError } from "@fedify/vocab-runtime";
|
|
8
|
+
import { openDirectFetch, openPublicFetch } from "@fedify/vocab-runtime/internal";
|
|
9
|
+
import { getLogger } from "@logtape/logtape";
|
|
10
|
+
//#region src/federation/send.ts
|
|
11
|
+
/**
|
|
12
|
+
* Extracts the inbox URLs from recipients.
|
|
13
|
+
* @param parameters The parameters to extract the inboxes.
|
|
14
|
+
* See also {@link ExtractInboxesParameters}.
|
|
15
|
+
* @returns The inboxes as a map of inbox URL to actor URIs.
|
|
16
|
+
*/
|
|
17
|
+
function extractInboxes({ recipients, preferSharedInbox, excludeBaseUris }) {
|
|
18
|
+
const inboxes = {};
|
|
19
|
+
for (const recipient of recipients) {
|
|
20
|
+
let inbox;
|
|
21
|
+
let sharedInbox = false;
|
|
22
|
+
if (preferSharedInbox && recipient.endpoints?.sharedInbox != null) {
|
|
23
|
+
inbox = recipient.endpoints.sharedInbox;
|
|
24
|
+
sharedInbox = true;
|
|
25
|
+
} else inbox = recipient.inboxId;
|
|
26
|
+
if (inbox != null && recipient.id != null) {
|
|
27
|
+
if (excludeBaseUris != null && excludeBaseUris.some((u) => u.origin === inbox?.origin)) continue;
|
|
28
|
+
inboxes[inbox.href] ??= {
|
|
29
|
+
actorIds: /* @__PURE__ */ new Set(),
|
|
30
|
+
sharedInbox
|
|
31
|
+
};
|
|
32
|
+
inboxes[inbox.href].actorIds.add(recipient.id.href);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return inboxes;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Sends an {@link Activity} to an inbox.
|
|
39
|
+
*
|
|
40
|
+
* @param parameters The parameters for sending the activity.
|
|
41
|
+
* See also {@link SendActivityParameters}.
|
|
42
|
+
* @param deps The dependencies, for tests.
|
|
43
|
+
* @throws {FetchError} If the delivery times out before receiving a response.
|
|
44
|
+
* @throws {SendActivityError} If the inbox returns an unsuccessful response.
|
|
45
|
+
*/
|
|
46
|
+
function sendActivity(options, deps = {}) {
|
|
47
|
+
const tracerProvider = options.tracerProvider ?? trace.getTracerProvider();
|
|
48
|
+
return tracerProvider.getTracer(name, version).startActiveSpan("activitypub.send_activity", {
|
|
49
|
+
kind: SpanKind.CLIENT,
|
|
50
|
+
attributes: { "activitypub.shared_inbox": options.sharedInbox ?? false }
|
|
51
|
+
}, async (span) => {
|
|
52
|
+
if (options.activityId != null) span.setAttribute("activitypub.activity.id", options.activityId);
|
|
53
|
+
if (options.activityType != null) span.setAttribute("activitypub.activity.type", options.activityType);
|
|
54
|
+
try {
|
|
55
|
+
await sendActivityInternal({
|
|
56
|
+
...options,
|
|
57
|
+
tracerProvider
|
|
58
|
+
}, span, deps);
|
|
59
|
+
} catch (e) {
|
|
60
|
+
span.setStatus({
|
|
61
|
+
code: SpanStatusCode.ERROR,
|
|
62
|
+
message: String(e)
|
|
63
|
+
});
|
|
64
|
+
throw e;
|
|
65
|
+
} finally {
|
|
66
|
+
span.end();
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
const MAX_ERROR_RESPONSE_BODY_BYTES = 1024;
|
|
71
|
+
const SEND_ACTIVITY_TIMEOUT_MS = 3e4;
|
|
72
|
+
function raceWithAbort(promise, signal) {
|
|
73
|
+
if (signal.aborted) return Promise.reject(signal.reason);
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
const onAbort = () => {
|
|
76
|
+
signal.removeEventListener("abort", onAbort);
|
|
77
|
+
reject(signal.reason);
|
|
78
|
+
};
|
|
79
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
80
|
+
promise.then((value) => {
|
|
81
|
+
signal.removeEventListener("abort", onAbort);
|
|
82
|
+
resolve(value);
|
|
83
|
+
}, (error) => {
|
|
84
|
+
signal.removeEventListener("abort", onAbort);
|
|
85
|
+
reject(error);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
async function readLimitedResponseBody(response, maxBytes, signal) {
|
|
90
|
+
if (response.body == null) return "";
|
|
91
|
+
const reader = response.body.getReader();
|
|
92
|
+
const decoder = new TextDecoder();
|
|
93
|
+
const chunks = [];
|
|
94
|
+
let totalBytes = 0;
|
|
95
|
+
let truncated = false;
|
|
96
|
+
try {
|
|
97
|
+
while (true) {
|
|
98
|
+
signal.throwIfAborted();
|
|
99
|
+
const { done, value } = await raceWithAbort(reader.read(), signal);
|
|
100
|
+
if (done) break;
|
|
101
|
+
if (totalBytes + value.length > maxBytes) {
|
|
102
|
+
const remaining = maxBytes - totalBytes;
|
|
103
|
+
if (remaining > 0) chunks.push(decoder.decode(value.slice(0, remaining), { stream: true }));
|
|
104
|
+
truncated = true;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
chunks.push(decoder.decode(value, { stream: true }));
|
|
108
|
+
totalBytes += value.length;
|
|
109
|
+
}
|
|
110
|
+
} finally {
|
|
111
|
+
if (truncated || signal.aborted) reader.cancel().catch(() => {});
|
|
112
|
+
try {
|
|
113
|
+
reader.releaseLock();
|
|
114
|
+
} catch (_) {}
|
|
115
|
+
}
|
|
116
|
+
let result = chunks.join("");
|
|
117
|
+
if (truncated) result += "… (truncated)";
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
async function sendActivityInternal({ activity, activityId, keys, inbox, allowPrivateAddress, headers, specDeterminer, tracerProvider }, span, deps) {
|
|
121
|
+
const logger = getLogger([
|
|
122
|
+
"fedify",
|
|
123
|
+
"federation",
|
|
124
|
+
"outbox"
|
|
125
|
+
]);
|
|
126
|
+
const timeoutMs = deps.timeoutMs ?? SEND_ACTIVITY_TIMEOUT_MS;
|
|
127
|
+
const controller = new AbortController();
|
|
128
|
+
const { signal } = controller;
|
|
129
|
+
const openUnvalidatedHop = deps.openHop ?? (allowPrivateAddress ? openDirectFetch : (url) => openPublicFetch(url, { signal }));
|
|
130
|
+
let opened = false;
|
|
131
|
+
async function openHop(url) {
|
|
132
|
+
let session;
|
|
133
|
+
try {
|
|
134
|
+
signal.throwIfAborted();
|
|
135
|
+
const opening = openUnvalidatedHop(url);
|
|
136
|
+
opening.then((lateSession) => {
|
|
137
|
+
if (signal.aborted) closeAfterError(lateSession, logger);
|
|
138
|
+
}, () => {});
|
|
139
|
+
session = await raceWithAbort(opening, signal);
|
|
140
|
+
if (signal.aborted) {
|
|
141
|
+
closeAfterError(session, logger);
|
|
142
|
+
signal.throwIfAborted();
|
|
143
|
+
}
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if (error instanceof UrlError && error.reason === "dns") {
|
|
146
|
+
logger.error("DNS resolution failed for URL: {url}", {
|
|
147
|
+
url,
|
|
148
|
+
error
|
|
149
|
+
});
|
|
150
|
+
const failure = new FetchError(url, error.message);
|
|
151
|
+
failure.cause = error;
|
|
152
|
+
throw failure;
|
|
153
|
+
}
|
|
154
|
+
if (error instanceof UrlError) logger.error("Disallowed private URL: {url}", {
|
|
155
|
+
url,
|
|
156
|
+
error
|
|
157
|
+
});
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
opened = true;
|
|
161
|
+
return session;
|
|
162
|
+
}
|
|
163
|
+
headers = new Headers(headers);
|
|
164
|
+
headers.set("Content-Type", "application/activity+json");
|
|
165
|
+
const request = new Request(inbox, {
|
|
166
|
+
method: "POST",
|
|
167
|
+
headers,
|
|
168
|
+
body: JSON.stringify(activity),
|
|
169
|
+
redirect: "manual",
|
|
170
|
+
signal
|
|
171
|
+
});
|
|
172
|
+
let rsaKey = null;
|
|
173
|
+
for (const key of keys) if (key.privateKey.algorithm.name === "RSASSA-PKCS1-v1_5") {
|
|
174
|
+
rsaKey = key;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
if (rsaKey == null) logger.warn("No supported key found to sign the request to {inbox}. The request will be sent without a signature. In order to sign the request, at least one RSASSA-PKCS1-v1_5 key must be provided.", {
|
|
178
|
+
inbox: inbox.href,
|
|
179
|
+
keys: keys.map((pair) => ({
|
|
180
|
+
keyId: pair.keyId.href,
|
|
181
|
+
privateKey: pair.privateKey
|
|
182
|
+
}))
|
|
183
|
+
});
|
|
184
|
+
let outcome;
|
|
185
|
+
const boundedSpecDeterminer = specDeterminer == null ? void 0 : {
|
|
186
|
+
determineSpec(origin) {
|
|
187
|
+
signal.throwIfAborted();
|
|
188
|
+
return raceWithAbort(Promise.resolve(specDeterminer.determineSpec(origin)), signal);
|
|
189
|
+
},
|
|
190
|
+
rememberSpec(origin, spec) {
|
|
191
|
+
if (signal.aborted) return;
|
|
192
|
+
try {
|
|
193
|
+
Promise.resolve(specDeterminer.rememberSpec(origin, spec)).catch((error) => logger.debug("Failed to remember the signature spec for {origin}: {error}", {
|
|
194
|
+
origin,
|
|
195
|
+
error
|
|
196
|
+
}));
|
|
197
|
+
} catch (error) {
|
|
198
|
+
logger.debug("Failed to remember the signature spec for {origin}: {error}", {
|
|
199
|
+
origin,
|
|
200
|
+
error
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
const timer = setTimeout(() => controller.abort(new DOMException("The delivery timed out.", "TimeoutError")), timeoutMs);
|
|
206
|
+
try {
|
|
207
|
+
outcome = rsaKey == null ? await fetchWithValidatedRedirects(request, openHop, (response) => consumeSendActivityResponse(response, inbox, signal), signal) : await doubleKnockScoped(request, rsaKey, {
|
|
208
|
+
tracerProvider,
|
|
209
|
+
specDeterminer: boundedSpecDeterminer,
|
|
210
|
+
signal
|
|
211
|
+
}, openHop, (response) => consumeSendActivityResponse(response, inbox, signal));
|
|
212
|
+
} catch (error) {
|
|
213
|
+
let failureError = error;
|
|
214
|
+
if (signal.aborted && !(error instanceof SendActivityError)) {
|
|
215
|
+
const failure = new FetchError(inbox.href, `Timed out after ${timeoutMs} ms`);
|
|
216
|
+
failure.cause = signal.reason;
|
|
217
|
+
failureError = failure;
|
|
218
|
+
}
|
|
219
|
+
if (opened || signal.aborted) logger.error("Failed to send activity {activityId} to {inbox}:\n{error}", {
|
|
220
|
+
activityId,
|
|
221
|
+
inbox: inbox.href,
|
|
222
|
+
error: failureError
|
|
223
|
+
});
|
|
224
|
+
throw failureError;
|
|
225
|
+
} finally {
|
|
226
|
+
clearTimeout(timer);
|
|
227
|
+
}
|
|
228
|
+
if (!outcome.ok) {
|
|
229
|
+
const { status, statusText, error } = outcome;
|
|
230
|
+
logger.error("Failed to send activity {activityId} to {inbox} ({status} {statusText}):\n{error}", {
|
|
231
|
+
activityId,
|
|
232
|
+
inbox: inbox.href,
|
|
233
|
+
status,
|
|
234
|
+
statusText,
|
|
235
|
+
error
|
|
236
|
+
});
|
|
237
|
+
throw new SendActivityError(inbox, status, `Failed to send activity ${activityId} to ${inbox.href} (${status} ${statusText}):\n${error}`, error);
|
|
238
|
+
}
|
|
239
|
+
span.addEvent("activitypub.activity.sent", {
|
|
240
|
+
"activitypub.activity.json": JSON.stringify(activity),
|
|
241
|
+
"activitypub.inbox.url": inbox.href,
|
|
242
|
+
"activitypub.activity.id": activityId ?? ""
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Starts cancelling the body of a response without waiting for it to settle.
|
|
247
|
+
* The cancellation of a branch of a tee (e.g., a body cloned by a fetch mock)
|
|
248
|
+
* settles only after the other branch is cancelled too, per the Streams
|
|
249
|
+
* standard, so awaiting it could block forever. Failures are only logged.
|
|
250
|
+
*/
|
|
251
|
+
function cancelResponseBody(response, url) {
|
|
252
|
+
response.body?.cancel().catch((error) => {
|
|
253
|
+
getLogger([
|
|
254
|
+
"fedify",
|
|
255
|
+
"federation",
|
|
256
|
+
"outbox"
|
|
257
|
+
]).debug("Failed to cancel the response body from {url}: {error}", {
|
|
258
|
+
url,
|
|
259
|
+
error
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Reads what is needed from the final response of a delivery while its
|
|
265
|
+
* session is open: the body of a successful response is cancelled, and at
|
|
266
|
+
* most 1 KiB of an error response body is read.
|
|
267
|
+
*/
|
|
268
|
+
async function consumeSendActivityResponse(response, inbox, signal) {
|
|
269
|
+
if (response.ok) {
|
|
270
|
+
cancelResponseBody(response, response.url || inbox.href);
|
|
271
|
+
return { ok: true };
|
|
272
|
+
}
|
|
273
|
+
let error;
|
|
274
|
+
try {
|
|
275
|
+
error = await readLimitedResponseBody(response, MAX_ERROR_RESPONSE_BODY_BYTES, signal);
|
|
276
|
+
} catch (_) {
|
|
277
|
+
error = "";
|
|
278
|
+
}
|
|
279
|
+
if (signal.aborted) error = "(timed out reading response body)";
|
|
280
|
+
return {
|
|
281
|
+
ok: false,
|
|
282
|
+
status: response.status,
|
|
283
|
+
statusText: response.statusText,
|
|
284
|
+
error
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Sends a request without signing it, following redirects manually like the
|
|
289
|
+
* Fetch standard does. Every hop is opened with `openHop()`, which validates
|
|
290
|
+
* the destination, after the previous hop is closed. The final response is
|
|
291
|
+
* passed to `consume()`, and its session is closed after that.
|
|
292
|
+
* This is exported only for tests; it is not exported from any entry point.
|
|
293
|
+
* @param request The request to send.
|
|
294
|
+
* @param openHop The function to open a session for each hop.
|
|
295
|
+
* @param consume The function to consume the final response.
|
|
296
|
+
* @returns The value returned by `consume`.
|
|
297
|
+
*/
|
|
298
|
+
async function fetchWithValidatedRedirects(request, openHop, consume, signal) {
|
|
299
|
+
const logger = getLogger([
|
|
300
|
+
"fedify",
|
|
301
|
+
"federation",
|
|
302
|
+
"outbox"
|
|
303
|
+
]);
|
|
304
|
+
let body = await request.clone().text();
|
|
305
|
+
for (let redirects = 0;; redirects++) {
|
|
306
|
+
signal?.throwIfAborted();
|
|
307
|
+
const session = await openHop(request.url);
|
|
308
|
+
let response;
|
|
309
|
+
try {
|
|
310
|
+
response = await session.fetch(request, {
|
|
311
|
+
redirect: "manual",
|
|
312
|
+
signal
|
|
313
|
+
});
|
|
314
|
+
} catch (error) {
|
|
315
|
+
await closeAfterError(session, logger);
|
|
316
|
+
throw error;
|
|
317
|
+
}
|
|
318
|
+
const location = response.headers.get("Location");
|
|
319
|
+
if (![
|
|
320
|
+
301,
|
|
321
|
+
302,
|
|
322
|
+
303,
|
|
323
|
+
307,
|
|
324
|
+
308
|
|
325
|
+
].includes(response.status) || location == null) try {
|
|
326
|
+
return await consume(response);
|
|
327
|
+
} catch (error) {
|
|
328
|
+
logger.debug("Failed to consume the response from {url}; closing the session: {error}", {
|
|
329
|
+
url: request.url,
|
|
330
|
+
error
|
|
331
|
+
});
|
|
332
|
+
throw error;
|
|
333
|
+
} finally {
|
|
334
|
+
await session.close();
|
|
335
|
+
}
|
|
336
|
+
cancelResponseBody(response, request.url);
|
|
337
|
+
await session.close();
|
|
338
|
+
if (redirects >= 20) throw new TypeError("Too many redirects");
|
|
339
|
+
const url = new URL(location, request.url);
|
|
340
|
+
const headers = new Headers(request.headers);
|
|
341
|
+
if (url.origin !== new URL(request.url).origin) {
|
|
342
|
+
headers.delete("Authorization");
|
|
343
|
+
headers.delete("Proxy-Authorization");
|
|
344
|
+
headers.delete("Cookie");
|
|
345
|
+
headers.delete("Host");
|
|
346
|
+
}
|
|
347
|
+
let method = request.method;
|
|
348
|
+
if ((response.status === 301 || response.status === 302) && method === "POST" || response.status === 303 && method !== "GET" && method !== "HEAD") {
|
|
349
|
+
method = "GET";
|
|
350
|
+
body = void 0;
|
|
351
|
+
for (const name of [
|
|
352
|
+
"Content-Encoding",
|
|
353
|
+
"Content-Language",
|
|
354
|
+
"Content-Length",
|
|
355
|
+
"Content-Location",
|
|
356
|
+
"Content-Type"
|
|
357
|
+
]) headers.delete(name);
|
|
358
|
+
}
|
|
359
|
+
request = new Request(url, {
|
|
360
|
+
method,
|
|
361
|
+
headers,
|
|
362
|
+
body,
|
|
363
|
+
redirect: "manual",
|
|
364
|
+
signal: signal ?? request.signal
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
async function closeAfterError(session, logger) {
|
|
369
|
+
try {
|
|
370
|
+
await session.close();
|
|
371
|
+
} catch (error) {
|
|
372
|
+
logger.debug("Failed to close the session for {url}: {error}", {
|
|
373
|
+
url: session.url.href,
|
|
374
|
+
error
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* An error that is thrown when an activity fails to send to a remote inbox.
|
|
380
|
+
* It contains structured information about the failure, including the HTTP
|
|
381
|
+
* status code, the inbox URL, and the response body.
|
|
382
|
+
* @since 2.0.0
|
|
383
|
+
*/
|
|
384
|
+
var SendActivityError = class extends Error {
|
|
385
|
+
/**
|
|
386
|
+
* The inbox URL that the activity was being sent to.
|
|
387
|
+
*/
|
|
388
|
+
inbox;
|
|
389
|
+
/**
|
|
390
|
+
* The HTTP status code returned by the inbox.
|
|
391
|
+
*/
|
|
392
|
+
statusCode;
|
|
393
|
+
/**
|
|
394
|
+
* The response body from the inbox, if any. Note that this may be
|
|
395
|
+
* truncated to a maximum of 1 KiB to prevent excessive memory consumption
|
|
396
|
+
* when remote servers return large error pages (e.g., Cloudflare error pages).
|
|
397
|
+
* If truncated, the string will end with `"… (truncated)"`.
|
|
398
|
+
*/
|
|
399
|
+
responseBody;
|
|
400
|
+
/**
|
|
401
|
+
* Creates a new {@link SendActivityError}.
|
|
402
|
+
* @param inbox The inbox URL.
|
|
403
|
+
* @param statusCode The HTTP status code.
|
|
404
|
+
* @param message The error message.
|
|
405
|
+
* @param responseBody The response body.
|
|
406
|
+
*/
|
|
407
|
+
constructor(inbox, statusCode, message, responseBody) {
|
|
408
|
+
super(message);
|
|
409
|
+
this.name = "SendActivityError";
|
|
410
|
+
this.inbox = inbox;
|
|
411
|
+
this.statusCode = statusCode;
|
|
412
|
+
this.responseBody = responseBody;
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
//#endregion
|
|
416
|
+
export { sendActivity as i, extractInboxes as n, fetchWithValidatedRedirects as r, SendActivityError as t };
|