@fedify/webfinger 2.5.0-dev.2844 → 2.5.0-dev.2860
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/README.md +4 -1
- package/dist/mod.cjs +114 -11
- package/dist/mod.d.cts +5 -2
- package/dist/mod.d.ts +5 -2
- package/dist/mod.js +114 -11
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -94,11 +94,14 @@ const result = await lookupWebFinger("acct:alice@example.com", {
|
|
|
94
94
|
// Maximum redirects to follow (default: 5)
|
|
95
95
|
maxRedirection: 3,
|
|
96
96
|
|
|
97
|
-
//
|
|
97
|
+
// Cancel sooner than the default 10-second lookup deadline
|
|
98
98
|
signal: AbortSignal.timeout(5000),
|
|
99
99
|
});
|
|
100
100
|
~~~~
|
|
101
101
|
|
|
102
|
+
Each lookup has a 10-second deadline across DNS resolution, redirects, and
|
|
103
|
+
response reading. A supplied signal can end it sooner but cannot extend it.
|
|
104
|
+
|
|
102
105
|
|
|
103
106
|
API
|
|
104
107
|
---
|
package/dist/mod.cjs
CHANGED
|
@@ -5,7 +5,7 @@ let _fedify_vocab_runtime_internal = require("@fedify/vocab-runtime/internal");
|
|
|
5
5
|
let _logtape_logtape = require("@logtape/logtape");
|
|
6
6
|
//#region deno.json
|
|
7
7
|
var name = "@fedify/webfinger";
|
|
8
|
-
var version = "2.5.0-dev.
|
|
8
|
+
var version = "2.5.0-dev.2860+54e6d1c2";
|
|
9
9
|
//#endregion
|
|
10
10
|
//#region src/body.ts
|
|
11
11
|
/** Decoded JSON body limit: 16 MiB. @internal */
|
|
@@ -28,13 +28,18 @@ function validateBodySizeLimit(maxBytes) {
|
|
|
28
28
|
* @param message The response or request to read.
|
|
29
29
|
* @param maxBytes The maximum number of decoded bytes to read.
|
|
30
30
|
* @param url The URL to include in errors and logs.
|
|
31
|
+
* @param signal The signal that cancels an in-progress body read.
|
|
31
32
|
* @returns The decoded body text.
|
|
32
33
|
* @throws {BodyTooLargeError} If the body exceeds the limit.
|
|
33
34
|
* @internal
|
|
34
35
|
*/
|
|
35
|
-
async function readBoundedText(message, maxBytes, url) {
|
|
36
|
+
async function readBoundedText(message, maxBytes, url, signal) {
|
|
36
37
|
validateBodySizeLimit(maxBytes);
|
|
37
38
|
const reader = message.body?.getReader();
|
|
39
|
+
const cancelOnAbort = () => {
|
|
40
|
+
if (reader != null) reader.cancel(signal?.reason).catch(() => {});
|
|
41
|
+
};
|
|
42
|
+
signal?.addEventListener("abort", cancelOnAbort, { once: true });
|
|
38
43
|
const tooLarge = () => {
|
|
39
44
|
(0, _logtape_logtape.getLogger)([
|
|
40
45
|
"fedify",
|
|
@@ -47,6 +52,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
47
52
|
throw new BodyTooLargeError(url, maxBytes);
|
|
48
53
|
};
|
|
49
54
|
try {
|
|
55
|
+
signal?.throwIfAborted();
|
|
50
56
|
const length = message.headers.get("Content-Length");
|
|
51
57
|
const encoding = message.headers.get("Content-Encoding");
|
|
52
58
|
if ((encoding == null || encoding.toLowerCase() === "identity") && length != null && /^\d+$/.test(length) && Number(length) > maxBytes) tooLarge();
|
|
@@ -54,6 +60,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
54
60
|
let size = 0;
|
|
55
61
|
const textReader = new ReadableStream({ async pull(controller) {
|
|
56
62
|
const { done, value } = await reader.read();
|
|
63
|
+
signal?.throwIfAborted();
|
|
57
64
|
if (done) {
|
|
58
65
|
controller.close();
|
|
59
66
|
return;
|
|
@@ -66,9 +73,11 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
66
73
|
const chunks = [];
|
|
67
74
|
while (true) {
|
|
68
75
|
const { done, value } = await textReader.read();
|
|
76
|
+
signal?.throwIfAborted();
|
|
69
77
|
if (done) break;
|
|
70
78
|
chunks.push(value);
|
|
71
79
|
}
|
|
80
|
+
signal?.throwIfAborted();
|
|
72
81
|
return chunks.join("");
|
|
73
82
|
} finally {
|
|
74
83
|
textReader.releaseLock();
|
|
@@ -77,6 +86,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
77
86
|
if (reader != null) reader.cancel(error).catch(() => {});
|
|
78
87
|
throw error;
|
|
79
88
|
} finally {
|
|
89
|
+
signal?.removeEventListener("abort", cancelOnAbort);
|
|
80
90
|
reader?.releaseLock();
|
|
81
91
|
}
|
|
82
92
|
}
|
|
@@ -88,6 +98,7 @@ const logger = (0, _logtape_logtape.getLogger)([
|
|
|
88
98
|
"lookup"
|
|
89
99
|
]);
|
|
90
100
|
const DEFAULT_MAX_REDIRECTION = 5;
|
|
101
|
+
const DEFAULT_TIMEOUT_MS = 1e4;
|
|
91
102
|
/**
|
|
92
103
|
* Looks up a WebFinger resource, opening a fetch session per hop. This is
|
|
93
104
|
* the implementation of {@link lookupWebFinger}, which is not exported from
|
|
@@ -121,14 +132,67 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
121
132
|
protocol = resource.protocol;
|
|
122
133
|
server = resource.host;
|
|
123
134
|
}
|
|
124
|
-
|
|
135
|
+
const url = new URL(`${protocol}//${server}/.well-known/webfinger`);
|
|
125
136
|
url.searchParams.set("resource", resource.href);
|
|
126
|
-
const
|
|
137
|
+
const controller = new AbortController();
|
|
138
|
+
const signal = controller.signal;
|
|
139
|
+
let timedOut = false;
|
|
140
|
+
const timeoutMs = deps.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
141
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 2147483647) throw new RangeError("The WebFinger timeout must be a positive integer of at most 2147483647 ms.");
|
|
142
|
+
const timeout = setTimeout(() => {
|
|
143
|
+
timedOut = true;
|
|
144
|
+
controller.abort(new DOMException("WebFinger lookup timed out", "TimeoutError"));
|
|
145
|
+
}, timeoutMs);
|
|
146
|
+
const abortFromCaller = () => controller.abort(options.signal?.reason);
|
|
147
|
+
if (options.signal?.aborted) abortFromCaller();
|
|
148
|
+
else options.signal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
149
|
+
let stopWaiting = () => {};
|
|
150
|
+
const cancelledOutcome = () => ({
|
|
151
|
+
resource: null,
|
|
152
|
+
result: "network_error",
|
|
153
|
+
remoteHost: url.host
|
|
154
|
+
});
|
|
155
|
+
const aborted = new Promise((resolve) => {
|
|
156
|
+
if (signal.aborted) resolve(cancelledOutcome());
|
|
157
|
+
else {
|
|
158
|
+
stopWaiting = () => signal.removeEventListener("abort", onAbort);
|
|
159
|
+
const onAbort = () => resolve(cancelledOutcome());
|
|
160
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
const work = lookupHops(url, options, deps.openHop ?? ((hopUrl, hopSignal) => options.allowPrivateAddress === true ? (0, _fedify_vocab_runtime_internal.openDirectFetch)(hopUrl) : (0, _fedify_vocab_runtime_internal.openPublicFetch)(hopUrl, { signal: hopSignal })), signal);
|
|
164
|
+
try {
|
|
165
|
+
const result = await Promise.race([work, aborted]);
|
|
166
|
+
if (signal.aborted) {
|
|
167
|
+
if (timedOut) logger.warn("WebFinger lookup timed out for {url} after {timeoutMs} ms.", {
|
|
168
|
+
url: url.href,
|
|
169
|
+
timeoutMs
|
|
170
|
+
});
|
|
171
|
+
return cancelledOutcome();
|
|
172
|
+
}
|
|
173
|
+
return result;
|
|
174
|
+
} catch (error) {
|
|
175
|
+
if (signal.aborted) return cancelledOutcome();
|
|
176
|
+
throw error;
|
|
177
|
+
} finally {
|
|
178
|
+
clearTimeout(timeout);
|
|
179
|
+
stopWaiting();
|
|
180
|
+
options.signal?.removeEventListener("abort", abortFromCaller);
|
|
181
|
+
work.catch(() => {});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
async function lookupHops(initialUrl, options, openHop, signal) {
|
|
185
|
+
let url = initialUrl;
|
|
127
186
|
let redirected = 0;
|
|
128
187
|
while (true) {
|
|
188
|
+
if (signal.aborted) return {
|
|
189
|
+
resource: null,
|
|
190
|
+
result: "network_error",
|
|
191
|
+
remoteHost: url.host
|
|
192
|
+
};
|
|
129
193
|
logger.debug("Fetching WebFinger resource descriptor from {url}...", { url: url.href });
|
|
130
194
|
const remoteHost = url.host;
|
|
131
|
-
const session = await openSession(url, openHop,
|
|
195
|
+
const session = await openSession(url, openHop, signal);
|
|
132
196
|
if (session == null) return {
|
|
133
197
|
resource: null,
|
|
134
198
|
result: "network_error",
|
|
@@ -136,10 +200,26 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
136
200
|
};
|
|
137
201
|
let hop;
|
|
138
202
|
try {
|
|
139
|
-
|
|
203
|
+
if (signal.aborted) return {
|
|
204
|
+
resource: null,
|
|
205
|
+
result: "network_error",
|
|
206
|
+
remoteHost
|
|
207
|
+
};
|
|
208
|
+
hop = await fetchHopUntilAbort(session, url, options, signal);
|
|
140
209
|
} finally {
|
|
141
|
-
|
|
210
|
+
if (signal.aborted) session.close().catch((error) => {
|
|
211
|
+
logger.debug("Failed to close WebFinger session for {url}: {error}", {
|
|
212
|
+
url: url.href,
|
|
213
|
+
error
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
else await session.close();
|
|
142
217
|
}
|
|
218
|
+
if (signal.aborted) return {
|
|
219
|
+
resource: null,
|
|
220
|
+
result: "network_error",
|
|
221
|
+
remoteHost
|
|
222
|
+
};
|
|
143
223
|
if ("outcome" in hop) return hop.outcome;
|
|
144
224
|
redirected++;
|
|
145
225
|
const maxRedirection = options.maxRedirection ?? DEFAULT_MAX_REDIRECTION;
|
|
@@ -182,13 +262,36 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
182
262
|
url = redirectedUrl;
|
|
183
263
|
}
|
|
184
264
|
}
|
|
265
|
+
/** Lets the session close even when a custom fetch or body ignores abort. */
|
|
266
|
+
async function fetchHopUntilAbort(session, url, options, signal) {
|
|
267
|
+
const fetching = fetchHop(session, url, options, signal);
|
|
268
|
+
let onAbort;
|
|
269
|
+
const aborted = new Promise((resolve) => {
|
|
270
|
+
const outcome = {
|
|
271
|
+
resource: null,
|
|
272
|
+
result: "network_error",
|
|
273
|
+
remoteHost: url.host
|
|
274
|
+
};
|
|
275
|
+
if (signal.aborted) resolve({ outcome });
|
|
276
|
+
else {
|
|
277
|
+
onAbort = () => resolve({ outcome });
|
|
278
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
try {
|
|
282
|
+
return await Promise.race([fetching, aborted]);
|
|
283
|
+
} finally {
|
|
284
|
+
if (onAbort != null) signal.removeEventListener("abort", onAbort);
|
|
285
|
+
fetching.catch(() => {});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
185
288
|
/**
|
|
186
289
|
* Opens a fetch session for a hop. A disallowed URL, a failed DNS lookup,
|
|
187
290
|
* or an aborted lookup yields `null` instead of an error.
|
|
188
291
|
*/
|
|
189
292
|
async function openSession(url, openHop, signal) {
|
|
190
293
|
try {
|
|
191
|
-
return await openHop(url.href);
|
|
294
|
+
return await openHop(url.href, signal);
|
|
192
295
|
} catch (e) {
|
|
193
296
|
if (e instanceof _fedify_vocab_runtime.UrlError) {
|
|
194
297
|
if (e.reason === "dns") logger.debug("DNS lookup failed for {url}", {
|
|
@@ -212,7 +315,7 @@ async function openSession(url, openHop, signal) {
|
|
|
212
315
|
* Fetches a hop with the given session, and consumes the response body or
|
|
213
316
|
* starts cancelling it before returning. The caller closes the session.
|
|
214
317
|
*/
|
|
215
|
-
async function fetchHop(session, url, options) {
|
|
318
|
+
async function fetchHop(session, url, options, signal) {
|
|
216
319
|
let response;
|
|
217
320
|
try {
|
|
218
321
|
response = await session.fetch(url, {
|
|
@@ -221,7 +324,7 @@ async function fetchHop(session, url, options) {
|
|
|
221
324
|
"User-Agent": typeof options.userAgent === "string" ? options.userAgent : (0, _fedify_vocab_runtime.getUserAgent)(options.userAgent)
|
|
222
325
|
},
|
|
223
326
|
redirect: "manual",
|
|
224
|
-
signal
|
|
327
|
+
signal
|
|
225
328
|
});
|
|
226
329
|
} catch (error) {
|
|
227
330
|
logger.debug("Failed to fetch WebFinger resource descriptor: {error}", {
|
|
@@ -256,7 +359,7 @@ async function fetchHop(session, url, options) {
|
|
|
256
359
|
} };
|
|
257
360
|
}
|
|
258
361
|
try {
|
|
259
|
-
const parsed = JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, url));
|
|
362
|
+
const parsed = JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, url, signal));
|
|
260
363
|
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return { outcome: {
|
|
261
364
|
resource: null,
|
|
262
365
|
result: "invalid",
|
package/dist/mod.d.cts
CHANGED
|
@@ -72,7 +72,9 @@ interface LookupWebFingerDependencies {
|
|
|
72
72
|
* Opens a fetch session for a single hop. Defaults to `openDirectFetch()`
|
|
73
73
|
* if `allowPrivateAddress` is turned on, or `openPublicFetch()` otherwise.
|
|
74
74
|
*/
|
|
75
|
-
readonly openHop?: (url: string) => Promise<PublicFetchSession>;
|
|
75
|
+
readonly openHop?: (url: string, signal?: AbortSignal) => Promise<PublicFetchSession>;
|
|
76
|
+
/** Overrides the lookup deadline for internal tests. */
|
|
77
|
+
readonly timeoutMs?: number;
|
|
76
78
|
}
|
|
77
79
|
//#endregion
|
|
78
80
|
//#region src/lookup.d.ts
|
|
@@ -166,7 +168,8 @@ interface LookupWebFingerOptions {
|
|
|
166
168
|
*/
|
|
167
169
|
meterProvider?: MeterProvider;
|
|
168
170
|
/**
|
|
169
|
-
* AbortSignal for cancelling the
|
|
171
|
+
* `AbortSignal` for cancelling the lookup sooner than its default 10-second
|
|
172
|
+
* deadline. A signal cannot extend that deadline.
|
|
170
173
|
* @since 1.8.0
|
|
171
174
|
*/
|
|
172
175
|
signal?: AbortSignal;
|
package/dist/mod.d.ts
CHANGED
|
@@ -72,7 +72,9 @@ interface LookupWebFingerDependencies {
|
|
|
72
72
|
* Opens a fetch session for a single hop. Defaults to `openDirectFetch()`
|
|
73
73
|
* if `allowPrivateAddress` is turned on, or `openPublicFetch()` otherwise.
|
|
74
74
|
*/
|
|
75
|
-
readonly openHop?: (url: string) => Promise<PublicFetchSession>;
|
|
75
|
+
readonly openHop?: (url: string, signal?: AbortSignal) => Promise<PublicFetchSession>;
|
|
76
|
+
/** Overrides the lookup deadline for internal tests. */
|
|
77
|
+
readonly timeoutMs?: number;
|
|
76
78
|
}
|
|
77
79
|
//#endregion
|
|
78
80
|
//#region src/lookup.d.ts
|
|
@@ -166,7 +168,8 @@ interface LookupWebFingerOptions {
|
|
|
166
168
|
*/
|
|
167
169
|
meterProvider?: MeterProvider;
|
|
168
170
|
/**
|
|
169
|
-
* AbortSignal for cancelling the
|
|
171
|
+
* `AbortSignal` for cancelling the lookup sooner than its default 10-second
|
|
172
|
+
* deadline. A signal cannot extend that deadline.
|
|
170
173
|
* @since 1.8.0
|
|
171
174
|
*/
|
|
172
175
|
signal?: AbortSignal;
|
package/dist/mod.js
CHANGED
|
@@ -4,7 +4,7 @@ import { openDirectFetch, openPublicFetch } from "@fedify/vocab-runtime/internal
|
|
|
4
4
|
import { getLogger } from "@logtape/logtape";
|
|
5
5
|
//#region deno.json
|
|
6
6
|
var name = "@fedify/webfinger";
|
|
7
|
-
var version = "2.5.0-dev.
|
|
7
|
+
var version = "2.5.0-dev.2860+54e6d1c2";
|
|
8
8
|
//#endregion
|
|
9
9
|
//#region src/body.ts
|
|
10
10
|
/** Decoded JSON body limit: 16 MiB. @internal */
|
|
@@ -27,13 +27,18 @@ function validateBodySizeLimit(maxBytes) {
|
|
|
27
27
|
* @param message The response or request to read.
|
|
28
28
|
* @param maxBytes The maximum number of decoded bytes to read.
|
|
29
29
|
* @param url The URL to include in errors and logs.
|
|
30
|
+
* @param signal The signal that cancels an in-progress body read.
|
|
30
31
|
* @returns The decoded body text.
|
|
31
32
|
* @throws {BodyTooLargeError} If the body exceeds the limit.
|
|
32
33
|
* @internal
|
|
33
34
|
*/
|
|
34
|
-
async function readBoundedText(message, maxBytes, url) {
|
|
35
|
+
async function readBoundedText(message, maxBytes, url, signal) {
|
|
35
36
|
validateBodySizeLimit(maxBytes);
|
|
36
37
|
const reader = message.body?.getReader();
|
|
38
|
+
const cancelOnAbort = () => {
|
|
39
|
+
if (reader != null) reader.cancel(signal?.reason).catch(() => {});
|
|
40
|
+
};
|
|
41
|
+
signal?.addEventListener("abort", cancelOnAbort, { once: true });
|
|
37
42
|
const tooLarge = () => {
|
|
38
43
|
getLogger([
|
|
39
44
|
"fedify",
|
|
@@ -46,6 +51,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
46
51
|
throw new BodyTooLargeError(url, maxBytes);
|
|
47
52
|
};
|
|
48
53
|
try {
|
|
54
|
+
signal?.throwIfAborted();
|
|
49
55
|
const length = message.headers.get("Content-Length");
|
|
50
56
|
const encoding = message.headers.get("Content-Encoding");
|
|
51
57
|
if ((encoding == null || encoding.toLowerCase() === "identity") && length != null && /^\d+$/.test(length) && Number(length) > maxBytes) tooLarge();
|
|
@@ -53,6 +59,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
53
59
|
let size = 0;
|
|
54
60
|
const textReader = new ReadableStream({ async pull(controller) {
|
|
55
61
|
const { done, value } = await reader.read();
|
|
62
|
+
signal?.throwIfAborted();
|
|
56
63
|
if (done) {
|
|
57
64
|
controller.close();
|
|
58
65
|
return;
|
|
@@ -65,9 +72,11 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
65
72
|
const chunks = [];
|
|
66
73
|
while (true) {
|
|
67
74
|
const { done, value } = await textReader.read();
|
|
75
|
+
signal?.throwIfAborted();
|
|
68
76
|
if (done) break;
|
|
69
77
|
chunks.push(value);
|
|
70
78
|
}
|
|
79
|
+
signal?.throwIfAborted();
|
|
71
80
|
return chunks.join("");
|
|
72
81
|
} finally {
|
|
73
82
|
textReader.releaseLock();
|
|
@@ -76,6 +85,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
76
85
|
if (reader != null) reader.cancel(error).catch(() => {});
|
|
77
86
|
throw error;
|
|
78
87
|
} finally {
|
|
88
|
+
signal?.removeEventListener("abort", cancelOnAbort);
|
|
79
89
|
reader?.releaseLock();
|
|
80
90
|
}
|
|
81
91
|
}
|
|
@@ -87,6 +97,7 @@ const logger = getLogger([
|
|
|
87
97
|
"lookup"
|
|
88
98
|
]);
|
|
89
99
|
const DEFAULT_MAX_REDIRECTION = 5;
|
|
100
|
+
const DEFAULT_TIMEOUT_MS = 1e4;
|
|
90
101
|
/**
|
|
91
102
|
* Looks up a WebFinger resource, opening a fetch session per hop. This is
|
|
92
103
|
* the implementation of {@link lookupWebFinger}, which is not exported from
|
|
@@ -120,14 +131,67 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
120
131
|
protocol = resource.protocol;
|
|
121
132
|
server = resource.host;
|
|
122
133
|
}
|
|
123
|
-
|
|
134
|
+
const url = new URL(`${protocol}//${server}/.well-known/webfinger`);
|
|
124
135
|
url.searchParams.set("resource", resource.href);
|
|
125
|
-
const
|
|
136
|
+
const controller = new AbortController();
|
|
137
|
+
const signal = controller.signal;
|
|
138
|
+
let timedOut = false;
|
|
139
|
+
const timeoutMs = deps.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
140
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 2147483647) throw new RangeError("The WebFinger timeout must be a positive integer of at most 2147483647 ms.");
|
|
141
|
+
const timeout = setTimeout(() => {
|
|
142
|
+
timedOut = true;
|
|
143
|
+
controller.abort(new DOMException("WebFinger lookup timed out", "TimeoutError"));
|
|
144
|
+
}, timeoutMs);
|
|
145
|
+
const abortFromCaller = () => controller.abort(options.signal?.reason);
|
|
146
|
+
if (options.signal?.aborted) abortFromCaller();
|
|
147
|
+
else options.signal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
148
|
+
let stopWaiting = () => {};
|
|
149
|
+
const cancelledOutcome = () => ({
|
|
150
|
+
resource: null,
|
|
151
|
+
result: "network_error",
|
|
152
|
+
remoteHost: url.host
|
|
153
|
+
});
|
|
154
|
+
const aborted = new Promise((resolve) => {
|
|
155
|
+
if (signal.aborted) resolve(cancelledOutcome());
|
|
156
|
+
else {
|
|
157
|
+
stopWaiting = () => signal.removeEventListener("abort", onAbort);
|
|
158
|
+
const onAbort = () => resolve(cancelledOutcome());
|
|
159
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
const work = lookupHops(url, options, deps.openHop ?? ((hopUrl, hopSignal) => options.allowPrivateAddress === true ? openDirectFetch(hopUrl) : openPublicFetch(hopUrl, { signal: hopSignal })), signal);
|
|
163
|
+
try {
|
|
164
|
+
const result = await Promise.race([work, aborted]);
|
|
165
|
+
if (signal.aborted) {
|
|
166
|
+
if (timedOut) logger.warn("WebFinger lookup timed out for {url} after {timeoutMs} ms.", {
|
|
167
|
+
url: url.href,
|
|
168
|
+
timeoutMs
|
|
169
|
+
});
|
|
170
|
+
return cancelledOutcome();
|
|
171
|
+
}
|
|
172
|
+
return result;
|
|
173
|
+
} catch (error) {
|
|
174
|
+
if (signal.aborted) return cancelledOutcome();
|
|
175
|
+
throw error;
|
|
176
|
+
} finally {
|
|
177
|
+
clearTimeout(timeout);
|
|
178
|
+
stopWaiting();
|
|
179
|
+
options.signal?.removeEventListener("abort", abortFromCaller);
|
|
180
|
+
work.catch(() => {});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
async function lookupHops(initialUrl, options, openHop, signal) {
|
|
184
|
+
let url = initialUrl;
|
|
126
185
|
let redirected = 0;
|
|
127
186
|
while (true) {
|
|
187
|
+
if (signal.aborted) return {
|
|
188
|
+
resource: null,
|
|
189
|
+
result: "network_error",
|
|
190
|
+
remoteHost: url.host
|
|
191
|
+
};
|
|
128
192
|
logger.debug("Fetching WebFinger resource descriptor from {url}...", { url: url.href });
|
|
129
193
|
const remoteHost = url.host;
|
|
130
|
-
const session = await openSession(url, openHop,
|
|
194
|
+
const session = await openSession(url, openHop, signal);
|
|
131
195
|
if (session == null) return {
|
|
132
196
|
resource: null,
|
|
133
197
|
result: "network_error",
|
|
@@ -135,10 +199,26 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
135
199
|
};
|
|
136
200
|
let hop;
|
|
137
201
|
try {
|
|
138
|
-
|
|
202
|
+
if (signal.aborted) return {
|
|
203
|
+
resource: null,
|
|
204
|
+
result: "network_error",
|
|
205
|
+
remoteHost
|
|
206
|
+
};
|
|
207
|
+
hop = await fetchHopUntilAbort(session, url, options, signal);
|
|
139
208
|
} finally {
|
|
140
|
-
|
|
209
|
+
if (signal.aborted) session.close().catch((error) => {
|
|
210
|
+
logger.debug("Failed to close WebFinger session for {url}: {error}", {
|
|
211
|
+
url: url.href,
|
|
212
|
+
error
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
else await session.close();
|
|
141
216
|
}
|
|
217
|
+
if (signal.aborted) return {
|
|
218
|
+
resource: null,
|
|
219
|
+
result: "network_error",
|
|
220
|
+
remoteHost
|
|
221
|
+
};
|
|
142
222
|
if ("outcome" in hop) return hop.outcome;
|
|
143
223
|
redirected++;
|
|
144
224
|
const maxRedirection = options.maxRedirection ?? DEFAULT_MAX_REDIRECTION;
|
|
@@ -181,13 +261,36 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
181
261
|
url = redirectedUrl;
|
|
182
262
|
}
|
|
183
263
|
}
|
|
264
|
+
/** Lets the session close even when a custom fetch or body ignores abort. */
|
|
265
|
+
async function fetchHopUntilAbort(session, url, options, signal) {
|
|
266
|
+
const fetching = fetchHop(session, url, options, signal);
|
|
267
|
+
let onAbort;
|
|
268
|
+
const aborted = new Promise((resolve) => {
|
|
269
|
+
const outcome = {
|
|
270
|
+
resource: null,
|
|
271
|
+
result: "network_error",
|
|
272
|
+
remoteHost: url.host
|
|
273
|
+
};
|
|
274
|
+
if (signal.aborted) resolve({ outcome });
|
|
275
|
+
else {
|
|
276
|
+
onAbort = () => resolve({ outcome });
|
|
277
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
try {
|
|
281
|
+
return await Promise.race([fetching, aborted]);
|
|
282
|
+
} finally {
|
|
283
|
+
if (onAbort != null) signal.removeEventListener("abort", onAbort);
|
|
284
|
+
fetching.catch(() => {});
|
|
285
|
+
}
|
|
286
|
+
}
|
|
184
287
|
/**
|
|
185
288
|
* Opens a fetch session for a hop. A disallowed URL, a failed DNS lookup,
|
|
186
289
|
* or an aborted lookup yields `null` instead of an error.
|
|
187
290
|
*/
|
|
188
291
|
async function openSession(url, openHop, signal) {
|
|
189
292
|
try {
|
|
190
|
-
return await openHop(url.href);
|
|
293
|
+
return await openHop(url.href, signal);
|
|
191
294
|
} catch (e) {
|
|
192
295
|
if (e instanceof UrlError) {
|
|
193
296
|
if (e.reason === "dns") logger.debug("DNS lookup failed for {url}", {
|
|
@@ -211,7 +314,7 @@ async function openSession(url, openHop, signal) {
|
|
|
211
314
|
* Fetches a hop with the given session, and consumes the response body or
|
|
212
315
|
* starts cancelling it before returning. The caller closes the session.
|
|
213
316
|
*/
|
|
214
|
-
async function fetchHop(session, url, options) {
|
|
317
|
+
async function fetchHop(session, url, options, signal) {
|
|
215
318
|
let response;
|
|
216
319
|
try {
|
|
217
320
|
response = await session.fetch(url, {
|
|
@@ -220,7 +323,7 @@ async function fetchHop(session, url, options) {
|
|
|
220
323
|
"User-Agent": typeof options.userAgent === "string" ? options.userAgent : getUserAgent(options.userAgent)
|
|
221
324
|
},
|
|
222
325
|
redirect: "manual",
|
|
223
|
-
signal
|
|
326
|
+
signal
|
|
224
327
|
});
|
|
225
328
|
} catch (error) {
|
|
226
329
|
logger.debug("Failed to fetch WebFinger resource descriptor: {error}", {
|
|
@@ -255,7 +358,7 @@ async function fetchHop(session, url, options) {
|
|
|
255
358
|
} };
|
|
256
359
|
}
|
|
257
360
|
try {
|
|
258
|
-
const parsed = JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, url));
|
|
361
|
+
const parsed = JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, url, signal));
|
|
259
362
|
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return { outcome: {
|
|
260
363
|
resource: null,
|
|
261
364
|
result: "invalid",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fedify/webfinger",
|
|
3
|
-
"version": "2.5.0-dev.
|
|
3
|
+
"version": "2.5.0-dev.2860+54e6d1c2",
|
|
4
4
|
"homepage": "https://fedify.dev/",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"@logtape/logtape": "2.4.2",
|
|
75
75
|
"@opentelemetry/api": "~1.9.1",
|
|
76
76
|
"es-toolkit": "1.46.1",
|
|
77
|
-
"@fedify/vocab-runtime": "2.5.0-dev.
|
|
77
|
+
"@fedify/vocab-runtime": "2.5.0-dev.2860+54e6d1c2"
|
|
78
78
|
},
|
|
79
79
|
"scripts": {
|
|
80
80
|
"build:self": "tsdown",
|