@fedify/fedify 2.0.0-dev.1961 → 2.0.0-dev.85

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/README.md +3 -0
  2. package/dist/{actor-DhgrrgXz.cjs → actor-Bpgsv6Ii.cjs} +185 -185
  3. package/dist/{actor-DiKHxw_H.js → actor-D3v8T9kQ.js} +185 -185
  4. package/dist/{actor-Kqyoic-M.js → actor-DrvDW-tK.js} +1 -1
  5. package/dist/{builder-BrqBKkVi.js → builder-D2trc6p7.js} +3 -3
  6. package/dist/compat/mod.d.cts +3 -3
  7. package/dist/compat/mod.d.ts +3 -3
  8. package/dist/compat/transformers.test.js +16 -16
  9. package/dist/{context-PxGADCsD.d.cts → context-DBQ7nMCw.d.cts} +1 -1
  10. package/dist/{context-V-XS2_6O.d.ts → context-mM0Nu5yo.d.ts} +1 -1
  11. package/dist/{docloader-HB61Uc88.js → docloader-DlSrn4Io.js} +2 -2
  12. package/dist/{esm-DaZiDvFW.js → esm-DQKBkSAc.js} +1 -1
  13. package/dist/federation/builder.test.js +5 -5
  14. package/dist/federation/collection.test.js +2 -2
  15. package/dist/federation/handler.test.js +104 -17
  16. package/dist/federation/idempotency.test.js +17 -17
  17. package/dist/federation/inbox.test.js +3 -3
  18. package/dist/federation/keycache.test.js +4 -4
  19. package/dist/federation/kv.test.js +57 -3
  20. package/dist/federation/middleware.test.js +18 -18
  21. package/dist/federation/mod.cjs +8 -8
  22. package/dist/federation/mod.d.cts +3 -3
  23. package/dist/federation/mod.d.ts +3 -3
  24. package/dist/federation/mod.js +8 -8
  25. package/dist/federation/mq.test.js +2 -2
  26. package/dist/federation/negotiation.test.js +2 -2
  27. package/dist/federation/retry.test.js +2 -2
  28. package/dist/federation/router.test.js +2 -2
  29. package/dist/federation/send.test.js +52 -9
  30. package/dist/{federation-CRpdnOMS.cjs → federation-CoW-KDKv.cjs} +22 -0
  31. package/dist/{federation-jcR8-ZxP.js → federation-D0hkM4T7.js} +22 -0
  32. package/dist/{http-B2bao4EB.js → http-BNxhNjYm.js} +2 -2
  33. package/dist/{http-CUsGQiwU.js → http-Bz4YpW-D.js} +2 -2
  34. package/dist/{http-BxgYWxTY.cjs → http-Dr7V4DY7.cjs} +2 -2
  35. package/dist/{inbox-CAojlSWc.js → inbox-CXOoDyEH.js} +1 -1
  36. package/dist/{key-Dz-KWCap.js → key-C_b1t1Na.js} +2 -2
  37. package/dist/{keycache-Czcf33al.js → keycache-zfG6F1PW.js} +1 -1
  38. package/dist/{keys-BN4nelft.js → keys-C01rMN22.js} +1 -1
  39. package/dist/{kv-BKNZ-Tb-.d.ts → kv-BpJND1Hr.d.ts} +30 -1
  40. package/dist/{kv-CRZrzyXm.js → kv-QzKcOQgP.js} +22 -0
  41. package/dist/{kv-Bxr0Q87_.d.cts → kv-cD_d4hg-.d.cts} +30 -1
  42. package/dist/{kv-cache-Bq6kUUoG.js → kv-cache-4I_O-pli.js} +1 -1
  43. package/dist/{kv-cache-5j5Pb-V6.cjs → kv-cache-CpjdzlKh.cjs} +1 -1
  44. package/dist/{kv-cache-DN9pfMBe.js → kv-cache-D7LFlByV.js} +13 -0
  45. package/dist/{ld-CEJHYq8u.js → ld-Ba0jIf7k.js} +2 -2
  46. package/dist/{lookup-gMu_9ZKY.js → lookup-BUqdDUac.js} +6 -5
  47. package/dist/{lookup-_Hap2IXS.cjs → lookup-UA4SKewV.cjs} +6 -5
  48. package/dist/{lookup-D8hvtZHY.js → lookup-kl47RWf3.js} +195 -189
  49. package/dist/middleware-B64_UxVQ.js +26 -0
  50. package/dist/middleware-BK7fptEJ.cjs +15 -0
  51. package/dist/{middleware-DW3rJHWy.cjs → middleware-BYqpBCFp.cjs} +21 -9
  52. package/dist/{middleware-DgrceAHB.js → middleware-CwpDL0KB.js} +19 -12
  53. package/dist/{middleware-Df9OWALM.js → middleware-Cx0KwuzQ.js} +21 -9
  54. package/dist/middleware-_DVIaAFk.js +15 -0
  55. package/dist/{mod-aAE2wOWV.d.ts → mod-CAdoBu0x.d.ts} +1 -1
  56. package/dist/{mod-BoRKfJPE.d.cts → mod-Dy2fJtmN.d.cts} +1 -1
  57. package/dist/{mod-Cdo6SYlJ.d.ts → mod-GetHzY6F.d.ts} +1 -1
  58. package/dist/{mod-DMpuiKXi.d.cts → mod-fxr25Gv7.d.cts} +1 -1
  59. package/dist/mod.cjs +8 -8
  60. package/dist/mod.d.cts +5 -5
  61. package/dist/mod.d.ts +5 -5
  62. package/dist/mod.js +8 -8
  63. package/dist/nodeinfo/client.test.js +3 -3
  64. package/dist/nodeinfo/handler.test.js +16 -16
  65. package/dist/nodeinfo/types.test.js +2 -2
  66. package/dist/otel/exporter.test.d.ts +3 -0
  67. package/dist/otel/exporter.test.js +900 -0
  68. package/dist/otel/mod.cjs +262 -0
  69. package/dist/otel/mod.d.cts +230 -0
  70. package/dist/otel/mod.d.ts +232 -0
  71. package/dist/otel/mod.js +261 -0
  72. package/dist/otel-Bfe_BgOV.js +63 -0
  73. package/dist/{owner-JwI-WzF-.js → owner-CgznkTZ6.js} +44 -8
  74. package/dist/{proof-CObJuclI.cjs → proof-AEFUz3SS.cjs} +44 -8
  75. package/dist/{proof-xH0V1mdD.js → proof-BrsOl9bB.js} +45 -9
  76. package/dist/{proof-Dhqndmfg.js → proof-rFeNfcVE.js} +2 -2
  77. package/dist/{send-1o7D-GGL.js → send-w_IyjZ0S.js} +9 -4
  78. package/dist/sig/http.test.js +7 -7
  79. package/dist/sig/key.test.js +5 -5
  80. package/dist/sig/ld.test.js +6 -6
  81. package/dist/sig/mod.cjs +4 -4
  82. package/dist/sig/mod.js +4 -4
  83. package/dist/sig/owner.test.js +30 -6
  84. package/dist/sig/proof.test.js +6 -6
  85. package/dist/testing/docloader.test.js +2 -2
  86. package/dist/testing/mod.js +2 -2
  87. package/dist/{testing-DpRy1YTU.js → testing-fIO3Z258.js} +1 -1
  88. package/dist/utils/docloader.test.js +8 -8
  89. package/dist/utils/kv-cache.test.js +6 -3
  90. package/dist/utils/mod.cjs +4 -4
  91. package/dist/utils/mod.d.cts +2 -2
  92. package/dist/utils/mod.d.ts +2 -2
  93. package/dist/utils/mod.js +4 -4
  94. package/dist/vocab/actor.test.js +4 -4
  95. package/dist/vocab/lookup.test.js +26 -4
  96. package/dist/vocab/mod.cjs +3 -3
  97. package/dist/vocab/mod.js +3 -3
  98. package/dist/vocab/type.test.js +2 -2
  99. package/dist/vocab/vocab.test.js +3 -3
  100. package/dist/{vocab-CAwj263k.js → vocab-BtqMcZvq.js} +7 -2
  101. package/dist/{vocab-DgHGCFcw.cjs → vocab-COVHviQ5.cjs} +7 -2
  102. package/dist/webfinger/handler.test.js +16 -16
  103. package/dist/webfinger/lookup.test.js +3 -3
  104. package/dist/webfinger/mod.cjs +1 -1
  105. package/dist/webfinger/mod.js +1 -1
  106. package/package.json +19 -9
  107. package/dist/middleware-19QFZj7b.js +0 -26
  108. package/dist/middleware-BbbcXY4w.cjs +0 -15
  109. package/dist/middleware-CSTK543z.js +0 -15
@@ -0,0 +1,232 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { URLPattern } from "urlpattern-polyfill";
3
+ import { KvKey, KvStore } from "../kv-BpJND1Hr.js";
4
+ import { ExportResultCode } from "@opentelemetry/core";
5
+ import { ReadableSpan, SpanExporter } from "@opentelemetry/sdk-trace-base";
6
+
7
+ //#region src/otel/exporter.d.ts
8
+ /**
9
+ * The direction of an activity in the trace.
10
+ *
11
+ * @since 1.10.0
12
+ */
13
+ type ActivityDirection = "inbound" | "outbound";
14
+ /**
15
+ * Signature verification details for an inbound activity.
16
+ *
17
+ * @since 1.10.0
18
+ */
19
+ interface SignatureVerificationDetails {
20
+ /**
21
+ * Whether HTTP Signatures were verified.
22
+ */
23
+ httpSignaturesVerified: boolean;
24
+ /**
25
+ * The key ID used for HTTP signature verification, if available.
26
+ */
27
+ httpSignaturesKeyId?: string;
28
+ /**
29
+ * Whether Linked Data Signatures were verified.
30
+ */
31
+ ldSignaturesVerified: boolean;
32
+ }
33
+ /**
34
+ * A record of an activity captured from a trace span.
35
+ * This interface stores the activity data along with trace context
36
+ * for distributed tracing support.
37
+ *
38
+ * @since 1.10.0
39
+ */
40
+ interface TraceActivityRecord {
41
+ /**
42
+ * The trace ID from OpenTelemetry.
43
+ */
44
+ traceId: string;
45
+ /**
46
+ * The span ID from OpenTelemetry.
47
+ */
48
+ spanId: string;
49
+ /**
50
+ * The parent span ID, if any.
51
+ */
52
+ parentSpanId?: string;
53
+ /**
54
+ * Whether this is an inbound or outbound activity.
55
+ */
56
+ direction: ActivityDirection;
57
+ /**
58
+ * The ActivityPub activity type (e.g., "Create", "Follow", "Like").
59
+ */
60
+ activityType: string;
61
+ /**
62
+ * The activity's ID URL, if present.
63
+ */
64
+ activityId?: string;
65
+ /**
66
+ * The actor ID URL (sender of the activity).
67
+ */
68
+ actorId?: string;
69
+ /**
70
+ * The full JSON representation of the activity.
71
+ */
72
+ activityJson: string;
73
+ /**
74
+ * Whether the activity was verified (for inbound activities).
75
+ */
76
+ verified?: boolean;
77
+ /**
78
+ * Detailed signature verification information (for inbound activities).
79
+ */
80
+ signatureDetails?: SignatureVerificationDetails;
81
+ /**
82
+ * The timestamp when this record was created (ISO 8601 format).
83
+ */
84
+ timestamp: string;
85
+ /**
86
+ * The target inbox URL (for outbound activities).
87
+ */
88
+ inboxUrl?: string;
89
+ }
90
+ /**
91
+ * Summary information about a trace.
92
+ *
93
+ * @since 1.10.0
94
+ */
95
+ interface TraceSummary {
96
+ /**
97
+ * The trace ID.
98
+ */
99
+ traceId: string;
100
+ /**
101
+ * The timestamp of the first activity in the trace.
102
+ */
103
+ timestamp: string;
104
+ /**
105
+ * The number of activities in the trace.
106
+ */
107
+ activityCount: number;
108
+ /**
109
+ * Activity types present in this trace.
110
+ */
111
+ activityTypes: string[];
112
+ }
113
+ /**
114
+ * Options for configuring the {@link FedifySpanExporter}.
115
+ *
116
+ * @since 1.10.0
117
+ */
118
+ interface FedifySpanExporterOptions {
119
+ /**
120
+ * The time-to-live for stored trace data.
121
+ * If not specified, data will be stored indefinitely
122
+ * (or until manually deleted).
123
+ */
124
+ ttl?: Temporal.Duration;
125
+ /**
126
+ * The key prefix for storing trace data in the KvStore.
127
+ * Defaults to `["fedify", "traces"]`.
128
+ */
129
+ keyPrefix?: KvKey;
130
+ }
131
+ /**
132
+ * Options for the {@link FedifySpanExporter.getRecentTraces} method.
133
+ *
134
+ * @since 1.10.0
135
+ */
136
+ interface GetRecentTracesOptions {
137
+ /**
138
+ * Maximum number of traces to return.
139
+ * If not specified, returns all available traces.
140
+ */
141
+ limit?: number;
142
+ }
143
+ /**
144
+ * A SpanExporter that persists ActivityPub activity traces to a
145
+ * {@link KvStore}. This enables distributed tracing across multiple
146
+ * nodes in a Fedify deployment.
147
+ *
148
+ * The exporter captures activity data from OpenTelemetry span events
149
+ * (`activitypub.activity.received` and `activitypub.activity.sent`)
150
+ * and stores them in the KvStore with trace context preserved.
151
+ *
152
+ * @example Basic usage with MemoryKvStore
153
+ * ```typescript ignore
154
+ * import { MemoryKvStore } from "@fedify/fedify";
155
+ * import { FedifySpanExporter } from "@fedify/fedify/otel";
156
+ * import {
157
+ * BasicTracerProvider,
158
+ * SimpleSpanProcessor,
159
+ * } from "@opentelemetry/sdk-trace-base";
160
+ *
161
+ * const kv = new MemoryKvStore();
162
+ * const exporter = new FedifySpanExporter(kv, {
163
+ * ttl: Temporal.Duration.from({ hours: 1 }),
164
+ * });
165
+ *
166
+ * const provider = new BasicTracerProvider({
167
+ * spanProcessors: [new SimpleSpanProcessor(exporter)],
168
+ * });
169
+ * ```
170
+ *
171
+ * @example Querying stored traces
172
+ * ```typescript ignore
173
+ * import { MemoryKvStore } from "@fedify/fedify";
174
+ * import { FedifySpanExporter } from "@fedify/fedify/otel";
175
+ *
176
+ * const kv = new MemoryKvStore();
177
+ * const exporter = new FedifySpanExporter(kv);
178
+ * const traceId = "abc123";
179
+ *
180
+ * // Get all activities for a specific trace
181
+ * const activities = await exporter.getActivitiesByTraceId(traceId);
182
+ *
183
+ * // Get recent traces
184
+ * const recentTraces = await exporter.getRecentTraces({ limit: 100 });
185
+ * ```
186
+ *
187
+ * @since 1.10.0
188
+ */
189
+ declare class FedifySpanExporter implements SpanExporter {
190
+ #private;
191
+ /**
192
+ * Creates a new FedifySpanExporter.
193
+ *
194
+ * @param kv The KvStore to persist trace data to.
195
+ * @param options Configuration options.
196
+ */
197
+ constructor(kv: KvStore, options?: FedifySpanExporterOptions);
198
+ /**
199
+ * Exports spans to the KvStore.
200
+ *
201
+ * @param spans The spans to export.
202
+ * @param resultCallback Callback to invoke with the export result.
203
+ */
204
+ export(spans: ReadableSpan[], resultCallback: (result: {
205
+ code: ExportResultCode;
206
+ }) => void): void;
207
+ /**
208
+ * Gets all activity records for a specific trace ID.
209
+ *
210
+ * @param traceId The trace ID to query.
211
+ * @returns An array of activity records belonging to the trace.
212
+ */
213
+ getActivitiesByTraceId(traceId: string): Promise<TraceActivityRecord[]>;
214
+ /**
215
+ * Gets recent traces with summary information.
216
+ *
217
+ * @param options Options for the query.
218
+ * @returns An array of trace summaries.
219
+ */
220
+ getRecentTraces(options?: GetRecentTracesOptions): Promise<TraceSummary[]>;
221
+ /**
222
+ * Forces the exporter to flush any buffered data.
223
+ * This is a no-op because we write directly to the KvStore without buffering.
224
+ */
225
+ forceFlush(): Promise<void>;
226
+ /**
227
+ * Shuts down the exporter.
228
+ */
229
+ shutdown(): Promise<void>;
230
+ }
231
+ //#endregion
232
+ export { ActivityDirection, FedifySpanExporter, FedifySpanExporterOptions, GetRecentTracesOptions, SignatureVerificationDetails, TraceActivityRecord, TraceSummary };
@@ -0,0 +1,261 @@
1
+
2
+ import { Temporal } from "@js-temporal/polyfill";
3
+ import { URLPattern } from "urlpattern-polyfill";
4
+
5
+ import { getLogger } from "@logtape/logtape";
6
+ import { ExportResultCode } from "@opentelemetry/core";
7
+
8
+ //#region src/otel/exporter.ts
9
+ /**
10
+ * A SpanExporter that persists ActivityPub activity traces to a
11
+ * {@link KvStore}. This enables distributed tracing across multiple
12
+ * nodes in a Fedify deployment.
13
+ *
14
+ * The exporter captures activity data from OpenTelemetry span events
15
+ * (`activitypub.activity.received` and `activitypub.activity.sent`)
16
+ * and stores them in the KvStore with trace context preserved.
17
+ *
18
+ * @example Basic usage with MemoryKvStore
19
+ * ```typescript ignore
20
+ * import { MemoryKvStore } from "@fedify/fedify";
21
+ * import { FedifySpanExporter } from "@fedify/fedify/otel";
22
+ * import {
23
+ * BasicTracerProvider,
24
+ * SimpleSpanProcessor,
25
+ * } from "@opentelemetry/sdk-trace-base";
26
+ *
27
+ * const kv = new MemoryKvStore();
28
+ * const exporter = new FedifySpanExporter(kv, {
29
+ * ttl: Temporal.Duration.from({ hours: 1 }),
30
+ * });
31
+ *
32
+ * const provider = new BasicTracerProvider({
33
+ * spanProcessors: [new SimpleSpanProcessor(exporter)],
34
+ * });
35
+ * ```
36
+ *
37
+ * @example Querying stored traces
38
+ * ```typescript ignore
39
+ * import { MemoryKvStore } from "@fedify/fedify";
40
+ * import { FedifySpanExporter } from "@fedify/fedify/otel";
41
+ *
42
+ * const kv = new MemoryKvStore();
43
+ * const exporter = new FedifySpanExporter(kv);
44
+ * const traceId = "abc123";
45
+ *
46
+ * // Get all activities for a specific trace
47
+ * const activities = await exporter.getActivitiesByTraceId(traceId);
48
+ *
49
+ * // Get recent traces
50
+ * const recentTraces = await exporter.getRecentTraces({ limit: 100 });
51
+ * ```
52
+ *
53
+ * @since 1.10.0
54
+ */
55
+ var FedifySpanExporter = class {
56
+ #kv;
57
+ #ttl;
58
+ #keyPrefix;
59
+ /**
60
+ * Creates a new FedifySpanExporter.
61
+ *
62
+ * @param kv The KvStore to persist trace data to.
63
+ * @param options Configuration options.
64
+ */
65
+ constructor(kv, options) {
66
+ this.#kv = kv;
67
+ this.#ttl = options?.ttl;
68
+ this.#keyPrefix = options?.keyPrefix ?? ["fedify", "traces"];
69
+ }
70
+ /**
71
+ * Exports spans to the KvStore.
72
+ *
73
+ * @param spans The spans to export.
74
+ * @param resultCallback Callback to invoke with the export result.
75
+ */
76
+ export(spans, resultCallback) {
77
+ this.#exportAsync(spans).then(() => resultCallback({ code: ExportResultCode.SUCCESS })).catch((error) => {
78
+ getLogger([
79
+ "fedify",
80
+ "otel",
81
+ "exporter"
82
+ ]).error("Failed to export spans to KvStore: {error}", { error });
83
+ resultCallback({ code: ExportResultCode.FAILED });
84
+ });
85
+ }
86
+ async #exportAsync(spans) {
87
+ const storeOperations = [];
88
+ for (const span of spans) {
89
+ const records = this.#extractRecords(span);
90
+ for (const record of records) storeOperations.push(this.#storeRecord(record));
91
+ }
92
+ const results = await Promise.allSettled(storeOperations);
93
+ const rejected = results.filter((r) => r.status === "rejected");
94
+ if (rejected.length > 0) throw new AggregateError(rejected.map((r) => r.reason), "Failed to store one or more trace activity records.");
95
+ }
96
+ #extractRecords(span) {
97
+ const records = [];
98
+ const spanContext = span.spanContext();
99
+ const traceId = spanContext.traceId;
100
+ const spanId = spanContext.spanId;
101
+ const parentSpanId = span.parentSpanContext?.spanId;
102
+ for (const event of span.events) if (event.name === "activitypub.activity.received") {
103
+ const record = this.#extractInboundRecord(event, traceId, spanId, parentSpanId);
104
+ if (record != null) records.push(record);
105
+ } else if (event.name === "activitypub.activity.sent") {
106
+ const record = this.#extractOutboundRecord(event, traceId, spanId, parentSpanId);
107
+ if (record != null) records.push(record);
108
+ }
109
+ return records;
110
+ }
111
+ #extractInboundRecord(event, traceId, spanId, parentSpanId) {
112
+ const attrs = event.attributes;
113
+ if (attrs == null) return null;
114
+ const activityJson = attrs["activitypub.activity.json"];
115
+ if (typeof activityJson !== "string") return null;
116
+ let activityType = "Unknown";
117
+ let activityId;
118
+ let actorId;
119
+ try {
120
+ const activity = JSON.parse(activityJson);
121
+ activityType = activity.type ?? "Unknown";
122
+ activityId = activity.id;
123
+ if (typeof activity.actor === "string") actorId = activity.actor;
124
+ else if (activity.actor != null && typeof activity.actor.id === "string") actorId = activity.actor.id;
125
+ } catch {}
126
+ const verified = attrs["activitypub.activity.verified"];
127
+ const httpSigVerified = attrs["http_signatures.verified"];
128
+ const httpSigKeyId = attrs["http_signatures.key_id"];
129
+ const ldSigVerified = attrs["ld_signatures.verified"];
130
+ let signatureDetails;
131
+ if (typeof httpSigVerified === "boolean" || typeof ldSigVerified === "boolean") signatureDetails = {
132
+ httpSignaturesVerified: httpSigVerified === true,
133
+ httpSignaturesKeyId: typeof httpSigKeyId === "string" && httpSigKeyId !== "" ? httpSigKeyId : void 0,
134
+ ldSignaturesVerified: ldSigVerified === true
135
+ };
136
+ return {
137
+ traceId,
138
+ spanId,
139
+ parentSpanId,
140
+ direction: "inbound",
141
+ activityType,
142
+ activityId,
143
+ actorId,
144
+ activityJson,
145
+ verified: typeof verified === "boolean" ? verified : void 0,
146
+ signatureDetails,
147
+ timestamp: (/* @__PURE__ */ new Date(event.time[0] * 1e3 + event.time[1] / 1e6)).toISOString()
148
+ };
149
+ }
150
+ #extractOutboundRecord(event, traceId, spanId, parentSpanId) {
151
+ const attrs = event.attributes;
152
+ if (attrs == null) return null;
153
+ const activityJson = attrs["activitypub.activity.json"];
154
+ if (typeof activityJson !== "string") return null;
155
+ let activityType = "Unknown";
156
+ let activityId;
157
+ let actorId;
158
+ try {
159
+ const activity = JSON.parse(activityJson);
160
+ activityType = activity.type ?? "Unknown";
161
+ activityId = activity.id;
162
+ if (typeof activity.actor === "string") actorId = activity.actor;
163
+ else if (activity.actor != null && typeof activity.actor.id === "string") actorId = activity.actor.id;
164
+ } catch {}
165
+ const inboxUrl = attrs["activitypub.inbox.url"];
166
+ const explicitActivityId = attrs["activitypub.activity.id"];
167
+ return {
168
+ traceId,
169
+ spanId,
170
+ parentSpanId,
171
+ direction: "outbound",
172
+ activityType,
173
+ activityId: activityId ?? (typeof explicitActivityId === "string" && explicitActivityId !== "" ? explicitActivityId : void 0),
174
+ actorId,
175
+ activityJson,
176
+ timestamp: (/* @__PURE__ */ new Date(event.time[0] * 1e3 + event.time[1] / 1e6)).toISOString(),
177
+ inboxUrl: typeof inboxUrl === "string" ? inboxUrl : void 0
178
+ };
179
+ }
180
+ async #storeRecord(record) {
181
+ const options = this.#ttl != null ? { ttl: this.#ttl } : void 0;
182
+ const key = [
183
+ ...this.#keyPrefix,
184
+ record.traceId,
185
+ record.spanId
186
+ ];
187
+ await this.#kv.set(key, record, options);
188
+ await this.#updateTraceSummary(record, options);
189
+ }
190
+ async #setWithCasRetry(key, transform, options) {
191
+ if (this.#kv.cas != null) for (let attempt = 0; attempt < 3; attempt++) {
192
+ const existing$1 = await this.#kv.get(key);
193
+ const newValue$1 = transform(existing$1);
194
+ if (await this.#kv.cas(key, existing$1, newValue$1, options)) return;
195
+ }
196
+ const existing = await this.#kv.get(key);
197
+ const newValue = transform(existing);
198
+ await this.#kv.set(key, newValue, options);
199
+ }
200
+ async #updateTraceSummary(record, options) {
201
+ const summaryKey = [
202
+ ...this.#keyPrefix,
203
+ "_summaries",
204
+ record.traceId
205
+ ];
206
+ await this.#setWithCasRetry(summaryKey, (existing) => {
207
+ const summary = existing != null ? {
208
+ traceId: existing.traceId,
209
+ timestamp: existing.timestamp,
210
+ activityCount: existing.activityCount,
211
+ activityTypes: [...existing.activityTypes]
212
+ } : {
213
+ traceId: record.traceId,
214
+ timestamp: record.timestamp,
215
+ activityCount: 0,
216
+ activityTypes: []
217
+ };
218
+ summary.activityCount += 1;
219
+ if (!summary.activityTypes.includes(record.activityType)) summary.activityTypes.push(record.activityType);
220
+ return summary;
221
+ }, options);
222
+ }
223
+ /**
224
+ * Gets all activity records for a specific trace ID.
225
+ *
226
+ * @param traceId The trace ID to query.
227
+ * @returns An array of activity records belonging to the trace.
228
+ */
229
+ async getActivitiesByTraceId(traceId) {
230
+ const prefix = [...this.#keyPrefix, traceId];
231
+ const records = [];
232
+ for await (const entry of this.#kv.list(prefix)) records.push(entry.value);
233
+ return records;
234
+ }
235
+ /**
236
+ * Gets recent traces with summary information.
237
+ *
238
+ * @param options Options for the query.
239
+ * @returns An array of trace summaries.
240
+ */
241
+ async getRecentTraces(options) {
242
+ const summaryPrefix = [...this.#keyPrefix, "_summaries"];
243
+ const summaries = [];
244
+ for await (const entry of this.#kv.list(summaryPrefix)) summaries.push(entry.value);
245
+ summaries.sort((a, b) => b.timestamp.localeCompare(a.timestamp));
246
+ if (options?.limit != null) return summaries.slice(0, options.limit);
247
+ return summaries;
248
+ }
249
+ /**
250
+ * Forces the exporter to flush any buffered data.
251
+ * This is a no-op because we write directly to the KvStore without buffering.
252
+ */
253
+ async forceFlush() {}
254
+ /**
255
+ * Shuts down the exporter.
256
+ */
257
+ async shutdown() {}
258
+ };
259
+
260
+ //#endregion
261
+ export { FedifySpanExporter };
@@ -0,0 +1,63 @@
1
+
2
+ import { Temporal } from "@js-temporal/polyfill";
3
+ import { URLPattern } from "urlpattern-polyfill";
4
+ globalThis.addEventListener = () => {};
5
+
6
+ import { BasicTracerProvider, SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base";
7
+ import { ExportResultCode } from "@opentelemetry/core";
8
+
9
+ //#region src/testing/otel.ts
10
+ /**
11
+ * A test spy for OpenTelemetry spans that captures all spans and events.
12
+ */
13
+ var TestSpanExporter = class {
14
+ spans = [];
15
+ export(spans, resultCallback) {
16
+ this.spans.push(...spans);
17
+ resultCallback({ code: ExportResultCode.SUCCESS });
18
+ }
19
+ async forceFlush() {}
20
+ shutdown() {
21
+ this.spans = [];
22
+ return Promise.resolve();
23
+ }
24
+ /**
25
+ * Gets all spans with the given name.
26
+ */
27
+ getSpans(name) {
28
+ return this.spans.filter((span) => span.name === name);
29
+ }
30
+ /**
31
+ * Gets the first span with the given name.
32
+ */
33
+ getSpan(name) {
34
+ return this.spans.find((span) => span.name === name);
35
+ }
36
+ /**
37
+ * Gets all events from spans with the given name.
38
+ */
39
+ getEvents(spanName, eventName) {
40
+ const spans = this.getSpans(spanName);
41
+ const events = spans.flatMap((span) => span.events);
42
+ if (eventName) return events.filter((event) => event.name === eventName);
43
+ return events;
44
+ }
45
+ /**
46
+ * Clears all captured spans.
47
+ */
48
+ clear() {
49
+ this.spans = [];
50
+ }
51
+ };
52
+ /**
53
+ * Creates a test tracer provider with a test exporter.
54
+ * @returns A tuple of [tracerProvider, testExporter]
55
+ */
56
+ function createTestTracerProvider() {
57
+ const exporter = new TestSpanExporter();
58
+ const provider = new BasicTracerProvider({ spanProcessors: [new SimpleSpanProcessor(exporter)] });
59
+ return [provider, exporter];
60
+ }
61
+
62
+ //#endregion
63
+ export { createTestTracerProvider };
@@ -3,10 +3,10 @@
3
3
  import { URLPattern } from "urlpattern-polyfill";
4
4
  globalThis.addEventListener = () => {};
5
5
 
6
- import { CryptographicKey, Object as Object$1 } from "./lookup-D8hvtZHY.js";
7
- import { isActor } from "./actor-Kqyoic-M.js";
6
+ import { CryptographicKey, Object as Object$1, deno_default } from "./lookup-kl47RWf3.js";
7
+ import { isActor } from "./actor-DrvDW-tK.js";
8
8
  import { getDocumentLoader } from "@fedify/vocab-runtime";
9
- import { trace } from "@opentelemetry/api";
9
+ import { SpanKind, SpanStatusCode, trace } from "@opentelemetry/api";
10
10
 
11
11
  //#region src/sig/owner.ts
12
12
  /**
@@ -17,11 +17,47 @@ import { trace } from "@opentelemetry/api";
17
17
  * @returns Whether the actor is the owner of the key.
18
18
  */
19
19
  async function doesActorOwnKey(activity, key, options) {
20
- if (key.ownerId != null) return key.ownerId.href === activity.actorId?.href;
21
- const actor = await activity.getActor(options);
22
- if (actor == null || !isActor(actor)) return false;
23
- for (const publicKeyId of actor.publicKeyIds) if (key.id != null && publicKeyId.href === key.id.href) return true;
24
- return false;
20
+ const tracerProvider = options.tracerProvider ?? trace.getTracerProvider();
21
+ const tracer = tracerProvider.getTracer(deno_default.name, deno_default.version);
22
+ return await tracer.startActiveSpan("activitypub.verify_key_ownership", {
23
+ kind: SpanKind.INTERNAL,
24
+ attributes: {
25
+ "activitypub.actor.id": activity.actorId?.href ?? "",
26
+ "activitypub.key.id": key.id?.href ?? ""
27
+ }
28
+ }, async (span) => {
29
+ try {
30
+ if (key.ownerId != null) {
31
+ const owns = key.ownerId.href === activity.actorId?.href;
32
+ span.setAttribute("activitypub.key_ownership.verified", owns);
33
+ span.setAttribute("activitypub.key_ownership.method", "owner_id");
34
+ return owns;
35
+ }
36
+ const actor = await activity.getActor(options);
37
+ if (actor == null || !isActor(actor)) {
38
+ span.setAttribute("activitypub.key_ownership.verified", false);
39
+ span.setAttribute("activitypub.key_ownership.method", "actor_fetch");
40
+ return false;
41
+ }
42
+ for (const publicKeyId of actor.publicKeyIds) if (key.id != null && publicKeyId.href === key.id.href) {
43
+ span.setAttribute("activitypub.key_ownership.verified", true);
44
+ span.setAttribute("activitypub.key_ownership.method", "actor_fetch");
45
+ return true;
46
+ }
47
+ span.setAttribute("activitypub.key_ownership.verified", false);
48
+ span.setAttribute("activitypub.key_ownership.method", "actor_fetch");
49
+ return false;
50
+ } catch (error) {
51
+ span.recordException(error);
52
+ span.setStatus({
53
+ code: SpanStatusCode.ERROR,
54
+ message: String(error)
55
+ });
56
+ throw error;
57
+ } finally {
58
+ span.end();
59
+ }
60
+ });
25
61
  }
26
62
  /**
27
63
  * Gets the actor that owns the specified key. Returns `null` if the key has no
@@ -3,9 +3,9 @@
3
3
  const { URLPattern } = require("urlpattern-polyfill");
4
4
 
5
5
  const require_chunk = require('./chunk-DqRYRqnO.cjs');
6
- const require_lookup = require('./lookup-_Hap2IXS.cjs');
7
- const require_actor = require('./actor-DhgrrgXz.cjs');
8
- const require_http = require('./http-BxgYWxTY.cjs');
6
+ const require_lookup = require('./lookup-UA4SKewV.cjs');
7
+ const require_actor = require('./actor-Bpgsv6Ii.cjs');
8
+ const require_http = require('./http-Dr7V4DY7.cjs');
9
9
  const __logtape_logtape = require_chunk.__toESM(require("@logtape/logtape"));
10
10
  const __opentelemetry_api = require_chunk.__toESM(require("@opentelemetry/api"));
11
11
  const jsonld = require_chunk.__toESM(require("jsonld"));
@@ -277,11 +277,47 @@ async function hashJsonLd(jsonLd, contextLoader) {
277
277
  * @returns Whether the actor is the owner of the key.
278
278
  */
279
279
  async function doesActorOwnKey(activity, key, options) {
280
- if (key.ownerId != null) return key.ownerId.href === activity.actorId?.href;
281
- const actor = await activity.getActor(options);
282
- if (actor == null || !require_actor.isActor(actor)) return false;
283
- for (const publicKeyId of actor.publicKeyIds) if (key.id != null && publicKeyId.href === key.id.href) return true;
284
- return false;
280
+ const tracerProvider = options.tracerProvider ?? __opentelemetry_api.trace.getTracerProvider();
281
+ const tracer = tracerProvider.getTracer(require_lookup.deno_default.name, require_lookup.deno_default.version);
282
+ return await tracer.startActiveSpan("activitypub.verify_key_ownership", {
283
+ kind: __opentelemetry_api.SpanKind.INTERNAL,
284
+ attributes: {
285
+ "activitypub.actor.id": activity.actorId?.href ?? "",
286
+ "activitypub.key.id": key.id?.href ?? ""
287
+ }
288
+ }, async (span) => {
289
+ try {
290
+ if (key.ownerId != null) {
291
+ const owns = key.ownerId.href === activity.actorId?.href;
292
+ span.setAttribute("activitypub.key_ownership.verified", owns);
293
+ span.setAttribute("activitypub.key_ownership.method", "owner_id");
294
+ return owns;
295
+ }
296
+ const actor = await activity.getActor(options);
297
+ if (actor == null || !require_actor.isActor(actor)) {
298
+ span.setAttribute("activitypub.key_ownership.verified", false);
299
+ span.setAttribute("activitypub.key_ownership.method", "actor_fetch");
300
+ return false;
301
+ }
302
+ for (const publicKeyId of actor.publicKeyIds) if (key.id != null && publicKeyId.href === key.id.href) {
303
+ span.setAttribute("activitypub.key_ownership.verified", true);
304
+ span.setAttribute("activitypub.key_ownership.method", "actor_fetch");
305
+ return true;
306
+ }
307
+ span.setAttribute("activitypub.key_ownership.verified", false);
308
+ span.setAttribute("activitypub.key_ownership.method", "actor_fetch");
309
+ return false;
310
+ } catch (error) {
311
+ span.recordException(error);
312
+ span.setStatus({
313
+ code: __opentelemetry_api.SpanStatusCode.ERROR,
314
+ message: String(error)
315
+ });
316
+ throw error;
317
+ } finally {
318
+ span.end();
319
+ }
320
+ });
285
321
  }
286
322
  /**
287
323
  * Gets the actor that owns the specified key. Returns `null` if the key has no