@fedify/webfinger 2.5.0-dev.2687 → 2.5.0-dev.2726
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/mod.cjs +1 -1
- package/dist/mod.js +1 -1
- package/package.json +20 -3
- package/deno.json +0 -32
- package/dist/lookup.test.cjs +0 -2504
- package/dist/lookup.test.d.cts +0 -1
- package/dist/lookup.test.d.ts +0 -1
- package/dist/lookup.test.js +0 -2504
- package/src/body.ts +0 -101
- package/src/jrd.ts +0 -67
- package/src/lookup-impl.ts +0 -342
- package/src/lookup.test.ts +0 -1589
- package/src/lookup.ts +0 -332
- package/src/mod.ts +0 -7
- package/tsconfig.check.json +0 -5
- package/tsdown.config.ts +0 -33
package/src/lookup.ts
DELETED
|
@@ -1,332 +0,0 @@
|
|
|
1
|
-
import type { GetUserAgentOptions } from "@fedify/vocab-runtime";
|
|
2
|
-
import {
|
|
3
|
-
type Attributes,
|
|
4
|
-
type Counter,
|
|
5
|
-
type Histogram,
|
|
6
|
-
type MeterProvider,
|
|
7
|
-
SpanKind,
|
|
8
|
-
SpanStatusCode,
|
|
9
|
-
trace,
|
|
10
|
-
type TracerProvider,
|
|
11
|
-
} from "@opentelemetry/api";
|
|
12
|
-
import metadata from "../deno.json" with { type: "json" };
|
|
13
|
-
import type { ResourceDescriptor } from "./jrd.ts";
|
|
14
|
-
|
|
15
|
-
import {
|
|
16
|
-
type LookupWebFingerDependencies,
|
|
17
|
-
lookupWebFingerInternal,
|
|
18
|
-
} from "./lookup-impl.ts";
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* The terminal classification of an outgoing {@link lookupWebFinger} call,
|
|
22
|
-
* recorded as the `webfinger.lookup.result` attribute on the
|
|
23
|
-
* `webfinger.lookup` counter and `webfinger.lookup.duration` histogram.
|
|
24
|
-
*
|
|
25
|
-
* - `found`: the lookup returned a {@link ResourceDescriptor}.
|
|
26
|
-
* - `not_found`: the remote responded with HTTP `404 Not Found` or
|
|
27
|
-
* `410 Gone`. Recorded together with `http.response.status_code`.
|
|
28
|
-
* - `invalid`: the remote responded with content Fedify could not parse
|
|
29
|
-
* into a {@link ResourceDescriptor} (JSON parse failure or a body over
|
|
30
|
-
* the size limit), the redirect chain exceeded `maxRedirection`, the
|
|
31
|
-
* remote redirected to a different protocol, or the queried `acct:`
|
|
32
|
-
* resource itself was malformed.
|
|
33
|
-
* - `network_error`: no HTTP response was received (the URL was
|
|
34
|
-
* rejected as a private address, `fetch()` threw, or an `AbortError`
|
|
35
|
-
* cancelled the request).
|
|
36
|
-
* - `error`: the remote responded with a non-2xx HTTP response that is
|
|
37
|
-
* neither `404` nor `410`, or any other unexpected failure bubbled up
|
|
38
|
-
* from the lookup.
|
|
39
|
-
* @since 2.3.0
|
|
40
|
-
*/
|
|
41
|
-
export type WebFingerLookupResult =
|
|
42
|
-
| "found"
|
|
43
|
-
| "not_found"
|
|
44
|
-
| "invalid"
|
|
45
|
-
| "network_error"
|
|
46
|
-
| "error";
|
|
47
|
-
|
|
48
|
-
interface WebFingerInstruments {
|
|
49
|
-
lookup: Counter;
|
|
50
|
-
lookupDuration: Histogram;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const WEBFINGER_HISTOGRAM_BUCKETS: ReadonlyArray<number> = [
|
|
54
|
-
5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000,
|
|
55
|
-
];
|
|
56
|
-
|
|
57
|
-
const webFingerInstruments = new WeakMap<MeterProvider, WebFingerInstruments>();
|
|
58
|
-
|
|
59
|
-
function getWebFingerInstruments(
|
|
60
|
-
meterProvider: MeterProvider,
|
|
61
|
-
): WebFingerInstruments {
|
|
62
|
-
let instruments = webFingerInstruments.get(meterProvider);
|
|
63
|
-
if (instruments == null) {
|
|
64
|
-
const meter = meterProvider.getMeter(metadata.name, metadata.version);
|
|
65
|
-
instruments = {
|
|
66
|
-
lookup: meter.createCounter("webfinger.lookup", {
|
|
67
|
-
description: "Outgoing WebFinger lookup attempts.",
|
|
68
|
-
unit: "{lookup}",
|
|
69
|
-
}),
|
|
70
|
-
lookupDuration: meter.createHistogram("webfinger.lookup.duration", {
|
|
71
|
-
description: "Duration of outgoing WebFinger lookups.",
|
|
72
|
-
unit: "ms",
|
|
73
|
-
advice: { explicitBucketBoundaries: [...WEBFINGER_HISTOGRAM_BUCKETS] },
|
|
74
|
-
}),
|
|
75
|
-
};
|
|
76
|
-
webFingerInstruments.set(meterProvider, instruments);
|
|
77
|
-
}
|
|
78
|
-
return instruments;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function getResourceScheme(resource: URL | string): string {
|
|
82
|
-
if (typeof resource === "string") {
|
|
83
|
-
const colon = resource.indexOf(":");
|
|
84
|
-
return colon > 0 ? resource.substring(0, colon).toLowerCase() : "";
|
|
85
|
-
}
|
|
86
|
-
return resource.protocol.replace(/:$/, "").toLowerCase();
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// The scheme attribute is recorded on the `webfinger.lookup` metric. Even
|
|
90
|
-
// though most call sites pass scheme-controlled resources (Fedify code and
|
|
91
|
-
// library users), `lookupObject()` accepts user-supplied identifiers that
|
|
92
|
-
// flow into here, so the metric attribute is bucketed to the schemes
|
|
93
|
-
// WebFinger / fediverse clients legitimately use (RFC 7565 +
|
|
94
|
-
// ActivityPub). Anything else is bucketed as `other`, keeping metric
|
|
95
|
-
// cardinality bounded even when a remote returns redirects whose target
|
|
96
|
-
// scheme is unusual.
|
|
97
|
-
const WEBFINGER_LOOKUP_SCHEME_WHITELIST: ReadonlySet<string> = new Set([
|
|
98
|
-
"acct",
|
|
99
|
-
"http",
|
|
100
|
-
"https",
|
|
101
|
-
"mailto",
|
|
102
|
-
]);
|
|
103
|
-
|
|
104
|
-
function getMetricResourceScheme(scheme: string): string {
|
|
105
|
-
return WEBFINGER_LOOKUP_SCHEME_WHITELIST.has(scheme) ? scheme : "other";
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* The outcome of a {@link lookupWebFinger} call, returned in place of the
|
|
110
|
-
* plain {@link ResourceDescriptor} when `options.withDiagnostics` is `true`.
|
|
111
|
-
* @since 2.5.0
|
|
112
|
-
*/
|
|
113
|
-
export interface WebFingerLookupOutcome {
|
|
114
|
-
/**
|
|
115
|
-
* The resource descriptor fetched from the remote WebFinger endpoint, or
|
|
116
|
-
* `null` if the lookup did not succeed.
|
|
117
|
-
*/
|
|
118
|
-
resource: ResourceDescriptor | null;
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* The terminal classification of the lookup. See
|
|
122
|
-
* {@link WebFingerLookupResult} for what each value means.
|
|
123
|
-
*/
|
|
124
|
-
result: WebFingerLookupResult;
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* The HTTP status code of the final response, if a response was received
|
|
128
|
-
* from the remote server.
|
|
129
|
-
*/
|
|
130
|
-
statusCode?: number;
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* The host of the final URL the lookup attempted to reach.
|
|
134
|
-
*/
|
|
135
|
-
remoteHost?: string;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Options for {@link lookupWebFinger}.
|
|
140
|
-
* @since 1.3.0
|
|
141
|
-
*/
|
|
142
|
-
export interface LookupWebFingerOptions {
|
|
143
|
-
/**
|
|
144
|
-
* The options for making `User-Agent` header.
|
|
145
|
-
* If a string is given, it is used as the `User-Agent` header value.
|
|
146
|
-
* If an object is given, it is passed to {@link getUserAgent} to generate
|
|
147
|
-
* the `User-Agent` header value.
|
|
148
|
-
*/
|
|
149
|
-
userAgent?: GetUserAgentOptions | string;
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Whether to allow private IP addresses in the URL.
|
|
153
|
-
*
|
|
154
|
-
* Mostly useful for testing purposes. *Do not use this in production.*
|
|
155
|
-
*
|
|
156
|
-
* Turned off by default.
|
|
157
|
-
* @since 1.4.0
|
|
158
|
-
*/
|
|
159
|
-
allowPrivateAddress?: boolean;
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* The maximum number of redirections to follow.
|
|
163
|
-
* @default `5`
|
|
164
|
-
* @since 1.8.0
|
|
165
|
-
*/
|
|
166
|
-
maxRedirection?: number;
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* The OpenTelemetry tracer provider. If omitted, the global tracer provider
|
|
170
|
-
* is used.
|
|
171
|
-
*/
|
|
172
|
-
tracerProvider?: TracerProvider;
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* The OpenTelemetry meter provider used to record the `webfinger.lookup`
|
|
176
|
-
* counter and `webfinger.lookup.duration` histogram. If omitted, no
|
|
177
|
-
* metric measurements are emitted (the helper is opt-in to avoid
|
|
178
|
-
* touching the global meter provider for callers that do not use
|
|
179
|
-
* OpenTelemetry).
|
|
180
|
-
* @since 2.3.0
|
|
181
|
-
*/
|
|
182
|
-
meterProvider?: MeterProvider;
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* AbortSignal for cancelling the request.
|
|
186
|
-
* @since 1.8.0
|
|
187
|
-
*/
|
|
188
|
-
signal?: AbortSignal;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Looks up a WebFinger resource.
|
|
193
|
-
* @param resource The resource URL to look up.
|
|
194
|
-
* @param options Extra options for looking up the resource.
|
|
195
|
-
* @returns The resource descriptor, or `null` if not found.
|
|
196
|
-
* @since 0.2.0
|
|
197
|
-
*/
|
|
198
|
-
export async function lookupWebFinger(
|
|
199
|
-
resource: URL | string,
|
|
200
|
-
options?: LookupWebFingerOptions & {
|
|
201
|
-
/**
|
|
202
|
-
* Whether to return a {@link WebFingerLookupOutcome} describing why
|
|
203
|
-
* the lookup failed, instead of collapsing every failure into `null`.
|
|
204
|
-
* Turned off by default.
|
|
205
|
-
* @since 2.5.0
|
|
206
|
-
*/
|
|
207
|
-
withDiagnostics?: false;
|
|
208
|
-
},
|
|
209
|
-
): Promise<ResourceDescriptor | null>;
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Looks up a WebFinger resource, returning diagnostics that explain why the
|
|
213
|
-
* lookup failed instead of collapsing every failure into `null`.
|
|
214
|
-
* @param resource The resource URL to look up.
|
|
215
|
-
* @param options Extra options for looking up the resource. Must set
|
|
216
|
-
* `withDiagnostics: true`.
|
|
217
|
-
* @returns The outcome of the lookup.
|
|
218
|
-
* @since 2.5.0
|
|
219
|
-
*/
|
|
220
|
-
export async function lookupWebFinger(
|
|
221
|
-
resource: URL | string,
|
|
222
|
-
options: LookupWebFingerOptions & {
|
|
223
|
-
/**
|
|
224
|
-
* Returns a {@link WebFingerLookupOutcome} instead of a plain
|
|
225
|
-
* {@link ResourceDescriptor}.
|
|
226
|
-
* @since 2.5.0
|
|
227
|
-
*/
|
|
228
|
-
withDiagnostics: true;
|
|
229
|
-
},
|
|
230
|
-
): Promise<WebFingerLookupOutcome>;
|
|
231
|
-
|
|
232
|
-
export async function lookupWebFinger(
|
|
233
|
-
resource: URL | string,
|
|
234
|
-
options: LookupWebFingerOptions & { withDiagnostics?: boolean } = {},
|
|
235
|
-
): Promise<ResourceDescriptor | null | WebFingerLookupOutcome> {
|
|
236
|
-
return await lookupWebFingerWithDependencies(resource, options);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* The lookup implementation with injectable hop sessions for tests.
|
|
241
|
-
* Not exported from the package entry point.
|
|
242
|
-
* @internal
|
|
243
|
-
*/
|
|
244
|
-
export function lookupWebFingerWithDependencies(
|
|
245
|
-
resource: URL | string,
|
|
246
|
-
options?: LookupWebFingerOptions & { withDiagnostics?: false },
|
|
247
|
-
deps?: LookupWebFingerDependencies,
|
|
248
|
-
): Promise<ResourceDescriptor | null>;
|
|
249
|
-
export function lookupWebFingerWithDependencies(
|
|
250
|
-
resource: URL | string,
|
|
251
|
-
options: LookupWebFingerOptions & { withDiagnostics: true },
|
|
252
|
-
deps?: LookupWebFingerDependencies,
|
|
253
|
-
): Promise<WebFingerLookupOutcome>;
|
|
254
|
-
export function lookupWebFingerWithDependencies(
|
|
255
|
-
resource: URL | string,
|
|
256
|
-
options: LookupWebFingerOptions & { withDiagnostics?: boolean },
|
|
257
|
-
deps?: LookupWebFingerDependencies,
|
|
258
|
-
): Promise<ResourceDescriptor | null | WebFingerLookupOutcome>;
|
|
259
|
-
export async function lookupWebFingerWithDependencies(
|
|
260
|
-
resource: URL | string,
|
|
261
|
-
options: LookupWebFingerOptions & { withDiagnostics?: boolean } = {},
|
|
262
|
-
deps: LookupWebFingerDependencies = {},
|
|
263
|
-
): Promise<ResourceDescriptor | null | WebFingerLookupOutcome> {
|
|
264
|
-
const tracerProvider = options.tracerProvider ?? trace.getTracerProvider();
|
|
265
|
-
const tracer = tracerProvider.getTracer(metadata.name, metadata.version);
|
|
266
|
-
const scheme = getResourceScheme(resource);
|
|
267
|
-
return await tracer.startActiveSpan(
|
|
268
|
-
"webfinger.lookup",
|
|
269
|
-
{
|
|
270
|
-
kind: SpanKind.CLIENT,
|
|
271
|
-
attributes: {
|
|
272
|
-
"webfinger.resource": resource.toString(),
|
|
273
|
-
"webfinger.resource.scheme": scheme,
|
|
274
|
-
},
|
|
275
|
-
},
|
|
276
|
-
async (span) => {
|
|
277
|
-
const meterProvider = options.meterProvider;
|
|
278
|
-
const start = meterProvider == null ? 0 : performance.now();
|
|
279
|
-
// Initialise the outcome with the `error` shape that the `finally`
|
|
280
|
-
// block records when `lookupWebFingerInternal()` itself rejects;
|
|
281
|
-
// the `try` body reassigns this to the actual outcome before any
|
|
282
|
-
// other statement runs, so the `catch` does not need to reassign.
|
|
283
|
-
let outcome: WebFingerLookupOutcome = {
|
|
284
|
-
resource: null,
|
|
285
|
-
result: "error",
|
|
286
|
-
};
|
|
287
|
-
try {
|
|
288
|
-
outcome = await lookupWebFingerInternal(resource, options, deps);
|
|
289
|
-
span.setStatus({
|
|
290
|
-
code:
|
|
291
|
-
outcome.resource === null
|
|
292
|
-
? SpanStatusCode.ERROR
|
|
293
|
-
: SpanStatusCode.OK,
|
|
294
|
-
});
|
|
295
|
-
return options.withDiagnostics ? outcome : outcome.resource;
|
|
296
|
-
} catch (error) {
|
|
297
|
-
span.setStatus({
|
|
298
|
-
code: SpanStatusCode.ERROR,
|
|
299
|
-
message: String(error),
|
|
300
|
-
});
|
|
301
|
-
throw error;
|
|
302
|
-
} finally {
|
|
303
|
-
if (meterProvider != null) {
|
|
304
|
-
const durationMs = Math.max(0, performance.now() - start);
|
|
305
|
-
recordWebFingerLookup(meterProvider, durationMs, scheme, outcome);
|
|
306
|
-
}
|
|
307
|
-
span.end();
|
|
308
|
-
}
|
|
309
|
-
},
|
|
310
|
-
);
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
function recordWebFingerLookup(
|
|
314
|
-
meterProvider: MeterProvider,
|
|
315
|
-
durationMs: number,
|
|
316
|
-
scheme: string,
|
|
317
|
-
outcome: WebFingerLookupOutcome,
|
|
318
|
-
): void {
|
|
319
|
-
const attributes: Attributes = {
|
|
320
|
-
"webfinger.lookup.result": outcome.result,
|
|
321
|
-
"webfinger.resource.scheme": getMetricResourceScheme(scheme),
|
|
322
|
-
};
|
|
323
|
-
if (outcome.remoteHost != null) {
|
|
324
|
-
attributes["activitypub.remote.host"] = outcome.remoteHost;
|
|
325
|
-
}
|
|
326
|
-
if (outcome.statusCode != null) {
|
|
327
|
-
attributes["http.response.status_code"] = outcome.statusCode;
|
|
328
|
-
}
|
|
329
|
-
const instruments = getWebFingerInstruments(meterProvider);
|
|
330
|
-
instruments.lookup.add(1, attributes);
|
|
331
|
-
instruments.lookupDuration.record(durationMs, attributes);
|
|
332
|
-
}
|
package/src/mod.ts
DELETED
package/tsconfig.check.json
DELETED
package/tsdown.config.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { glob } from "node:fs/promises";
|
|
2
|
-
import { sep } from "node:path";
|
|
3
|
-
import { defineConfig } from "tsdown";
|
|
4
|
-
|
|
5
|
-
export default [
|
|
6
|
-
defineConfig({
|
|
7
|
-
entry: ["src/mod.ts"],
|
|
8
|
-
dts: { compilerOptions: { isolatedDeclarations: true, declaration: true } },
|
|
9
|
-
format: ["esm", "cjs"],
|
|
10
|
-
platform: "node",
|
|
11
|
-
outExtensions({ format }) {
|
|
12
|
-
return {
|
|
13
|
-
js: format === "cjs" ? ".cjs" : ".js",
|
|
14
|
-
dts: format === "cjs" ? ".d.cts" : ".d.ts",
|
|
15
|
-
};
|
|
16
|
-
},
|
|
17
|
-
deps: { neverBundle: [/^node:/] },
|
|
18
|
-
}),
|
|
19
|
-
defineConfig({
|
|
20
|
-
entry: (await Array.fromAsync(glob(`src/**/*.test.ts`))).map((f) =>
|
|
21
|
-
f.replace(sep, "/"),
|
|
22
|
-
),
|
|
23
|
-
format: ["esm", "cjs"],
|
|
24
|
-
platform: "node",
|
|
25
|
-
outExtensions({ format }) {
|
|
26
|
-
return {
|
|
27
|
-
js: format === "cjs" ? ".cjs" : ".js",
|
|
28
|
-
dts: format === "cjs" ? ".d.cts" : ".d.ts",
|
|
29
|
-
};
|
|
30
|
-
},
|
|
31
|
-
deps: { neverBundle: [/^node:/, "@fedify/fixture"] },
|
|
32
|
-
}),
|
|
33
|
-
];
|