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

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 (266) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +66 -36
  3. package/dist/{builder-BrqBKkVi.js → builder-CBUaUcr5.js} +5 -3
  4. package/dist/{client-BsGzbnV-.d.ts → client-CUTUGgvJ.d.ts} +18 -18
  5. package/dist/{client-pY7-3icS.js → client-Dg7OfUDA.js} +28 -23
  6. package/dist/{client-94iWEfQa.d.cts → client-by-PEGAJ.d.cts} +18 -18
  7. package/dist/compat/mod.cjs +1 -1
  8. package/dist/compat/mod.d.cts +6 -10
  9. package/dist/compat/mod.d.ts +6 -10
  10. package/dist/compat/mod.js +1 -1
  11. package/dist/compat/transformers.test.js +22 -21
  12. package/dist/context-Bns6uTJq.js +109 -0
  13. package/dist/{context-V-XS2_6O.d.ts → context-C7vzWilY.d.ts} +63 -65
  14. package/dist/{context-PxGADCsD.d.cts → context-CrB9RFy5.d.cts} +63 -65
  15. package/dist/deno-Ldk6cZ5y.js +117 -0
  16. package/dist/{testing-DpRy1YTU.js → dist-B5f6a8Tt.js} +90 -110
  17. package/dist/{docloader-HB61Uc88.js → docloader-CX_8i5G9.js} +3 -3
  18. package/dist/{esm-DaZiDvFW.js → esm-DGl7uK1r.js} +1 -1
  19. package/dist/federation/builder.test.js +7 -5
  20. package/dist/federation/collection.test.js +2 -3
  21. package/dist/federation/handler.test.js +110 -22
  22. package/dist/federation/idempotency.test.js +23 -22
  23. package/dist/federation/inbox.test.js +4 -3
  24. package/dist/federation/keycache.test.js +4 -4
  25. package/dist/federation/kv.test.js +56 -3
  26. package/dist/federation/middleware.test.js +95 -93
  27. package/dist/federation/mod.cjs +8 -10
  28. package/dist/federation/mod.d.cts +7 -11
  29. package/dist/federation/mod.d.ts +7 -11
  30. package/dist/federation/mod.js +8 -11
  31. package/dist/federation/mq.test.js +5 -6
  32. package/dist/federation/negotiation.test.js +2 -3
  33. package/dist/federation/retry.test.js +2 -3
  34. package/dist/federation/router.test.js +2 -2
  35. package/dist/federation/send.test.js +51 -9
  36. package/dist/{webfinger/handler.test.js → federation/webfinger.test.js} +24 -22
  37. package/dist/{federation-CRpdnOMS.cjs → federation-B431K2gm.cjs} +22 -0
  38. package/dist/{federation-jcR8-ZxP.js → federation-BbZwNNWj.js} +28 -6
  39. package/dist/{http-M8k5mKc0.d.cts → http-ClB3pLcL.d.cts} +1 -1
  40. package/dist/{http-CUsGQiwU.js → http-CnZ1IHUS.js} +119 -9
  41. package/dist/{http-BbO0ejuk.d.ts → http-DLBDPal9.d.ts} +1 -1
  42. package/dist/{http-B2bao4EB.js → http-DNpKjQw7.js} +3 -2
  43. package/dist/{http-BxgYWxTY.cjs → http-dzRhLC1t.cjs} +132 -16
  44. package/dist/{inbox-CAojlSWc.js → inbox-DR-Gk02p.js} +2 -1
  45. package/dist/{key-Dz-KWCap.js → key-C1rxalIW.js} +3 -3
  46. package/dist/{keycache-Czcf33al.js → keycache-DRxpZ5r9.js} +1 -1
  47. package/dist/{keys-BN4nelft.js → keys-ZbcByPg9.js} +1 -1
  48. package/dist/{kv-Bxr0Q87_.d.cts → kv-B4vFhIYL.d.cts} +30 -1
  49. package/dist/{kv-BKNZ-Tb-.d.ts → kv-CYySNrsn.d.ts} +30 -1
  50. package/dist/{kv-CRZrzyXm.js → kv-QzKcOQgP.js} +22 -0
  51. package/dist/{kv-cache-DN9pfMBe.js → kv-cache-BEeqyGER.js} +14 -1
  52. package/dist/{kv-cache-5j5Pb-V6.cjs → kv-cache-BlS1sett.cjs} +2 -2
  53. package/dist/{kv-cache-Bq6kUUoG.js → kv-cache-DpIq7Isv.js} +2 -2
  54. package/dist/{ld-CEJHYq8u.js → ld-Cm6SNnYj.js} +4 -3
  55. package/dist/middleware-BJ396MAL.cjs +12 -0
  56. package/dist/{middleware-DW3rJHWy.cjs → middleware-CBBhOrXl.cjs} +210 -194
  57. package/dist/middleware-CRk4rARo.js +12 -0
  58. package/dist/middleware-DaemJ_4r.js +26 -0
  59. package/dist/{middleware-DgrceAHB.js → middleware-DmcjHVg8.js} +162 -165
  60. package/dist/{middleware-Df9OWALM.js → middleware-JPuzpoIQ.js} +164 -154
  61. package/dist/{mod-DMpuiKXi.d.cts → mod-0p9zUdzg.d.cts} +6 -6
  62. package/dist/mod-0qnPv4EC.d.cts +62 -0
  63. package/dist/{mod-DgxG-byT.d.cts → mod-BrS8tiad.d.cts} +2 -2
  64. package/dist/mod-C3SOvTD1.d.ts +64 -0
  65. package/dist/{mod-BoRKfJPE.d.cts → mod-D6pS5_xJ.d.cts} +4 -4
  66. package/dist/{mod-D5Z2tISD.d.ts → mod-jOa7W503.d.ts} +2 -2
  67. package/dist/{mod-Cdo6SYlJ.d.ts → mod-waqu-BL_.d.ts} +4 -4
  68. package/dist/{mod-aAE2wOWV.d.ts → mod-xc20HhMD.d.ts} +6 -6
  69. package/dist/mod.cjs +11 -93
  70. package/dist/mod.d.cts +11 -15
  71. package/dist/mod.d.ts +11 -15
  72. package/dist/mod.js +11 -15
  73. package/dist/nodeinfo/client.test.js +3 -4
  74. package/dist/nodeinfo/handler.test.js +22 -21
  75. package/dist/nodeinfo/mod.cjs +2 -2
  76. package/dist/nodeinfo/mod.d.cts +2 -2
  77. package/dist/nodeinfo/mod.d.ts +2 -2
  78. package/dist/nodeinfo/mod.js +2 -2
  79. package/dist/nodeinfo/types.test.js +2 -3
  80. package/dist/otel/exporter.test.js +893 -0
  81. package/dist/otel/mod.cjs +256 -0
  82. package/dist/otel/mod.d.cts +230 -0
  83. package/dist/otel/mod.d.ts +232 -0
  84. package/dist/otel/mod.js +255 -0
  85. package/dist/{owner-kQRGVXG1.d.ts → owner-BgI8C-VY.d.ts} +1 -2
  86. package/dist/{owner-B4HbyP8s.d.cts → owner-C-zfmVAD.d.cts} +1 -2
  87. package/dist/{owner-JwI-WzF-.js → owner-D-ZwLwaI.js} +44 -8
  88. package/dist/{proof-Dhqndmfg.js → proof-B-5l6HNZ.js} +3 -2
  89. package/dist/{proof-CObJuclI.cjs → proof-C2oMTHcU.cjs} +63 -28
  90. package/dist/{proof-xH0V1mdD.js → proof-lN5nVUla.js} +46 -11
  91. package/dist/router-D9eI0s4b.js +118 -0
  92. package/dist/{send-1o7D-GGL.js → send-D0AlkVEU.js} +9 -4
  93. package/dist/sig/http.test.js +6 -7
  94. package/dist/sig/key.test.js +5 -5
  95. package/dist/sig/ld.test.js +6 -6
  96. package/dist/sig/mod.cjs +3 -5
  97. package/dist/sig/mod.d.cts +3 -5
  98. package/dist/sig/mod.d.ts +3 -5
  99. package/dist/sig/mod.js +3 -5
  100. package/dist/sig/owner.test.js +29 -6
  101. package/dist/sig/proof.test.js +6 -6
  102. package/dist/testing/mod.d.ts +70 -6999
  103. package/dist/testing/mod.js +4 -3
  104. package/dist/{transformers-CoBS-oFG.cjs → transformers-BjBg6Lag.cjs} +2 -2
  105. package/dist/{transformers-BFT6d7J5.js → transformers-N_ip_y4P.js} +2 -2
  106. package/dist/{types-BtUjyi5y.js → types-8l28uC8o.js} +30 -25
  107. package/dist/{types-CWgzGaqk.cjs → types-B6z6CqIz.cjs} +30 -25
  108. package/dist/{types-C2XVl6gj.js → types-CPz01LGH.js} +3 -3
  109. package/dist/utils/docloader.test.js +7 -8
  110. package/dist/utils/kv-cache.test.js +5 -3
  111. package/dist/utils/mod.cjs +3 -5
  112. package/dist/utils/mod.d.cts +3 -4
  113. package/dist/utils/mod.d.ts +3 -4
  114. package/dist/utils/mod.js +3 -5
  115. package/package.json +24 -35
  116. package/dist/actor-DhgrrgXz.cjs +0 -42079
  117. package/dist/actor-DiKHxw_H.js +0 -41647
  118. package/dist/actor-DqFajh9s.d.ts +0 -130
  119. package/dist/actor-Kqyoic-M.js +0 -146
  120. package/dist/actor-f2NtjyCg.d.cts +0 -128
  121. package/dist/fixtures/activitypub.academy/users/brauca_darradiul.json +0 -83
  122. package/dist/fixtures/example.com/announce.json +0 -6
  123. package/dist/fixtures/example.com/collection.json +0 -19
  124. package/dist/fixtures/example.com/create.json +0 -6
  125. package/dist/fixtures/example.com/cross-origin-actor.json +0 -6
  126. package/dist/fixtures/example.com/hong-gildong.json +0 -11
  127. package/dist/fixtures/example.com/invite.json +0 -7
  128. package/dist/fixtures/example.com/key.json +0 -7
  129. package/dist/fixtures/example.com/key2.json +0 -6
  130. package/dist/fixtures/example.com/object.json +0 -6
  131. package/dist/fixtures/example.com/orderedcollectionpage.json +0 -24
  132. package/dist/fixtures/example.com/paged/a.json +0 -13
  133. package/dist/fixtures/example.com/paged/b.json +0 -16
  134. package/dist/fixtures/example.com/paged-collection.json +0 -6
  135. package/dist/fixtures/example.com/person.json +0 -22
  136. package/dist/fixtures/example.com/person2.json +0 -40
  137. package/dist/fixtures/example.com/test.json +0 -5
  138. package/dist/fixtures/example.com/users/handle.json +0 -16
  139. package/dist/fixtures/example.com/wrong-type.json +0 -3
  140. package/dist/fixtures/media.example.com/avatars/test-avatar.jpg.json +0 -6
  141. package/dist/fixtures/oeee.cafe/ap/users/3609fd4e-d51d-4db8-9f04-4189815864dd.json +0 -24
  142. package/dist/fixtures/remote.domain/users/bob.json +0 -20
  143. package/dist/fixtures/server.example/users/alice.json +0 -20
  144. package/dist/fixtures/w3id.org/identity/v1.json +0 -152
  145. package/dist/fixtures/w3id.org/security/data-integrity/v1.json +0 -74
  146. package/dist/fixtures/w3id.org/security/multikey/v1.json +0 -35
  147. package/dist/fixtures/w3id.org/security/v1.json +0 -50
  148. package/dist/fixtures/wizard.casa/users/hongminhee.json +0 -69
  149. package/dist/fixtures/www.w3.org/ns/activitystreams.json +0 -379
  150. package/dist/fixtures/www.w3.org/ns/did/v1.json +0 -58
  151. package/dist/lookup-D8hvtZHY.js +0 -42178
  152. package/dist/lookup-_Hap2IXS.cjs +0 -265
  153. package/dist/lookup-gMu_9ZKY.js +0 -253
  154. package/dist/middleware-19QFZj7b.js +0 -26
  155. package/dist/middleware-BbbcXY4w.cjs +0 -15
  156. package/dist/middleware-CSTK543z.js +0 -15
  157. package/dist/mod-BlVovdcy.d.ts +0 -309
  158. package/dist/mod-BxRCHTz-.d.cts +0 -307
  159. package/dist/mod-C58MZ7Wx.d.cts +0 -113
  160. package/dist/mod-CcDPcLJW.d.cts +0 -1
  161. package/dist/mod-Ds0mpFZU.d.ts +0 -115
  162. package/dist/mod-bjzj5QIb.d.ts +0 -2
  163. package/dist/src/vocab/accept.yaml +0 -15
  164. package/dist/src/vocab/activity.yaml +0 -98
  165. package/dist/src/vocab/add.yaml +0 -16
  166. package/dist/src/vocab/announce.yaml +0 -30
  167. package/dist/src/vocab/application.yaml +0 -324
  168. package/dist/src/vocab/arrive.yaml +0 -15
  169. package/dist/src/vocab/article.yaml +0 -46
  170. package/dist/src/vocab/audio.yaml +0 -11
  171. package/dist/src/vocab/block.yaml +0 -16
  172. package/dist/src/vocab/chatmessage.yaml +0 -50
  173. package/dist/src/vocab/collection.yaml +0 -154
  174. package/dist/src/vocab/collectionpage.yaml +0 -55
  175. package/dist/src/vocab/create.yaml +0 -28
  176. package/dist/src/vocab/dataintegrityproof.yaml +0 -56
  177. package/dist/src/vocab/delete.yaml +0 -27
  178. package/dist/src/vocab/didservice.yaml +0 -22
  179. package/dist/src/vocab/dislike.yaml +0 -14
  180. package/dist/src/vocab/document.yaml +0 -31
  181. package/dist/src/vocab/emoji.yaml +0 -12
  182. package/dist/src/vocab/emojireact.yaml +0 -17
  183. package/dist/src/vocab/endpoints.yaml +0 -85
  184. package/dist/src/vocab/event.yaml +0 -11
  185. package/dist/src/vocab/export.yaml +0 -9
  186. package/dist/src/vocab/flag.yaml +0 -15
  187. package/dist/src/vocab/follow.yaml +0 -19
  188. package/dist/src/vocab/group.yaml +0 -324
  189. package/dist/src/vocab/hashtag.yaml +0 -14
  190. package/dist/src/vocab/ignore.yaml +0 -14
  191. package/dist/src/vocab/image.yaml +0 -9
  192. package/dist/src/vocab/intransitiveactivity.yaml +0 -15
  193. package/dist/src/vocab/invite.yaml +0 -14
  194. package/dist/src/vocab/join.yaml +0 -14
  195. package/dist/src/vocab/key.yaml +0 -28
  196. package/dist/src/vocab/leave.yaml +0 -14
  197. package/dist/src/vocab/like.yaml +0 -16
  198. package/dist/src/vocab/link.yaml +0 -101
  199. package/dist/src/vocab/listen.yaml +0 -12
  200. package/dist/src/vocab/mention.yaml +0 -9
  201. package/dist/src/vocab/move.yaml +0 -15
  202. package/dist/src/vocab/multikey.yaml +0 -36
  203. package/dist/src/vocab/note.yaml +0 -48
  204. package/dist/src/vocab/object.yaml +0 -404
  205. package/dist/src/vocab/offer.yaml +0 -15
  206. package/dist/src/vocab/orderedcollection.yaml +0 -39
  207. package/dist/src/vocab/orderedcollectionpage.yaml +0 -50
  208. package/dist/src/vocab/organization.yaml +0 -324
  209. package/dist/src/vocab/page.yaml +0 -11
  210. package/dist/src/vocab/person.yaml +0 -324
  211. package/dist/src/vocab/place.yaml +0 -75
  212. package/dist/src/vocab/profile.yaml +0 -26
  213. package/dist/src/vocab/propertyvalue.yaml +0 -32
  214. package/dist/src/vocab/question.yaml +0 -103
  215. package/dist/src/vocab/read.yaml +0 -13
  216. package/dist/src/vocab/reject.yaml +0 -14
  217. package/dist/src/vocab/relationship.yaml +0 -52
  218. package/dist/src/vocab/remove.yaml +0 -14
  219. package/dist/src/vocab/service.yaml +0 -324
  220. package/dist/src/vocab/source.yaml +0 -26
  221. package/dist/src/vocab/tentativeaccept.yaml +0 -14
  222. package/dist/src/vocab/tentativereject.yaml +0 -14
  223. package/dist/src/vocab/tombstone.yaml +0 -24
  224. package/dist/src/vocab/travel.yaml +0 -16
  225. package/dist/src/vocab/undo.yaml +0 -26
  226. package/dist/src/vocab/update.yaml +0 -58
  227. package/dist/src/vocab/video.yaml +0 -11
  228. package/dist/src/vocab/view.yaml +0 -13
  229. package/dist/testing/docloader.test.js +0 -22
  230. package/dist/vocab/actor.test.js +0 -5963
  231. package/dist/vocab/lookup.test.d.ts +0 -3
  232. package/dist/vocab/lookup.test.js +0 -454
  233. package/dist/vocab/mod.cjs +0 -86
  234. package/dist/vocab/mod.d.cts +0 -4
  235. package/dist/vocab/mod.d.ts +0 -6
  236. package/dist/vocab/mod.js +0 -9
  237. package/dist/vocab/type.test.d.ts +0 -3
  238. package/dist/vocab/type.test.js +0 -24
  239. package/dist/vocab/vocab.test.d.ts +0 -3
  240. package/dist/vocab/vocab.test.js +0 -9397
  241. package/dist/vocab-BCWe1Ih5.d.ts +0 -14905
  242. package/dist/vocab-CAwj263k.js +0 -255
  243. package/dist/vocab-CeDBzu-f.d.cts +0 -14903
  244. package/dist/vocab-DgHGCFcw.cjs +0 -291
  245. package/dist/webfinger/handler.test.d.ts +0 -3
  246. package/dist/webfinger/lookup.test.d.ts +0 -3
  247. package/dist/webfinger/lookup.test.js +0 -193
  248. package/dist/webfinger/mod.cjs +0 -8
  249. package/dist/webfinger/mod.d.cts +0 -2
  250. package/dist/webfinger/mod.d.ts +0 -4
  251. package/dist/webfinger/mod.js +0 -8
  252. package/dist/webfinger-C72Y8lrh.js +0 -4
  253. package/dist/webfinger-vAtLmxOF.cjs +0 -4
  254. /package/dist/{collection-BzWsN9pB.js → collection-CcnIw1qY.js} +0 -0
  255. /package/dist/{testing/docloader.test.d.ts → federation/webfinger.test.d.ts} +0 -0
  256. /package/dist/{mod-CVgZgliM.d.ts → mod-1E3W847c.d.ts} +0 -0
  257. /package/dist/{mod-B-hUPT2N.d.cts → mod-C81L6_lQ.d.cts} +0 -0
  258. /package/dist/{negotiation-C4nFufNk.js → negotiation-5NPJL6zp.js} +0 -0
  259. /package/dist/{nodeinfo-BnthBobC.js → nodeinfo-BlLsRSiT.js} +0 -0
  260. /package/dist/{nodeinfo-CdN0rEnZ.cjs → nodeinfo-DuMYTpbZ.cjs} +0 -0
  261. /package/dist/{vocab/actor.test.d.ts → otel/exporter.test.d.ts} +0 -0
  262. /package/dist/{retry-CfF8Gn4d.js → retry-D4GJ670a.js} +0 -0
  263. /package/dist/{sig-C34-oHBl.js → sig-CwuONEzF.js} +0 -0
  264. /package/dist/{sig-YYj5tCnr.cjs → sig-DeXX2xnj.cjs} +0 -0
  265. /package/dist/{utils-DyRU1gdZ.cjs → utils-Db0ZmjcD.cjs} +0 -0
  266. /package/dist/{utils-D-Va7aXC.js → utils-Wranxuoe.js} +0 -0
@@ -0,0 +1,893 @@
1
+
2
+ import { Temporal } from "@js-temporal/polyfill";
3
+ import { URLPattern } from "urlpattern-polyfill";
4
+ globalThis.addEventListener = () => {};
5
+
6
+ import { test } from "../dist-B5f6a8Tt.js";
7
+ import { assertEquals } from "../assert_equals-DSbWqCm3.js";
8
+ import "../assert-MZs1qjMx.js";
9
+ import "../assert_instance_of-DHz7EHNU.js";
10
+ import { MemoryKvStore } from "../kv-QzKcOQgP.js";
11
+ import "../std__assert-DWivtrGR.js";
12
+ import "../assert_rejects-Ce45JcFg.js";
13
+ import "../assert_throws-BNXdRGWP.js";
14
+ import "../assert_not_equals-C80BG-_5.js";
15
+ import { getLogger } from "@logtape/logtape";
16
+ import { ExportResultCode } from "@opentelemetry/core";
17
+ import { SpanKind, SpanStatusCode, TraceFlags } from "@opentelemetry/api";
18
+
19
+ //#region src/otel/exporter.ts
20
+ /**
21
+ * A SpanExporter that persists ActivityPub activity traces to a
22
+ * {@link KvStore}. This enables distributed tracing across multiple
23
+ * nodes in a Fedify deployment.
24
+ *
25
+ * The exporter captures activity data from OpenTelemetry span events
26
+ * (`activitypub.activity.received` and `activitypub.activity.sent`)
27
+ * and stores them in the KvStore with trace context preserved.
28
+ *
29
+ * @example Basic usage with MemoryKvStore
30
+ * ```typescript ignore
31
+ * import { MemoryKvStore } from "@fedify/fedify";
32
+ * import { FedifySpanExporter } from "@fedify/fedify/otel";
33
+ * import {
34
+ * BasicTracerProvider,
35
+ * SimpleSpanProcessor,
36
+ * } from "@opentelemetry/sdk-trace-base";
37
+ *
38
+ * const kv = new MemoryKvStore();
39
+ * const exporter = new FedifySpanExporter(kv, {
40
+ * ttl: Temporal.Duration.from({ hours: 1 }),
41
+ * });
42
+ *
43
+ * const provider = new BasicTracerProvider({
44
+ * spanProcessors: [new SimpleSpanProcessor(exporter)],
45
+ * });
46
+ * ```
47
+ *
48
+ * @example Querying stored traces
49
+ * ```typescript ignore
50
+ * import { MemoryKvStore } from "@fedify/fedify";
51
+ * import { FedifySpanExporter } from "@fedify/fedify/otel";
52
+ *
53
+ * const kv = new MemoryKvStore();
54
+ * const exporter = new FedifySpanExporter(kv);
55
+ * const traceId = "abc123";
56
+ *
57
+ * // Get all activities for a specific trace
58
+ * const activities = await exporter.getActivitiesByTraceId(traceId);
59
+ *
60
+ * // Get recent traces
61
+ * const recentTraces = await exporter.getRecentTraces({ limit: 100 });
62
+ * ```
63
+ *
64
+ * @since 1.10.0
65
+ */
66
+ var FedifySpanExporter = class {
67
+ #kv;
68
+ #ttl;
69
+ #keyPrefix;
70
+ /**
71
+ * Creates a new FedifySpanExporter.
72
+ *
73
+ * @param kv The KvStore to persist trace data to.
74
+ * @param options Configuration options.
75
+ */
76
+ constructor(kv, options) {
77
+ this.#kv = kv;
78
+ this.#ttl = options?.ttl;
79
+ this.#keyPrefix = options?.keyPrefix ?? ["fedify", "traces"];
80
+ }
81
+ /**
82
+ * Exports spans to the KvStore.
83
+ *
84
+ * @param spans The spans to export.
85
+ * @param resultCallback Callback to invoke with the export result.
86
+ */
87
+ export(spans, resultCallback) {
88
+ this.#exportAsync(spans).then(() => resultCallback({ code: ExportResultCode.SUCCESS })).catch((error) => {
89
+ getLogger([
90
+ "fedify",
91
+ "otel",
92
+ "exporter"
93
+ ]).error("Failed to export spans to KvStore: {error}", { error });
94
+ resultCallback({ code: ExportResultCode.FAILED });
95
+ });
96
+ }
97
+ async #exportAsync(spans) {
98
+ const storeOperations = [];
99
+ for (const span of spans) {
100
+ const records = this.#extractRecords(span);
101
+ for (const record of records) storeOperations.push(this.#storeRecord(record));
102
+ }
103
+ const results = await Promise.allSettled(storeOperations);
104
+ const rejected = results.filter((r) => r.status === "rejected");
105
+ if (rejected.length > 0) throw new AggregateError(rejected.map((r) => r.reason), "Failed to store one or more trace activity records.");
106
+ }
107
+ #extractRecords(span) {
108
+ const records = [];
109
+ const spanContext = span.spanContext();
110
+ const traceId = spanContext.traceId;
111
+ const spanId = spanContext.spanId;
112
+ const parentSpanId = span.parentSpanContext?.spanId;
113
+ for (const event of span.events) if (event.name === "activitypub.activity.received") {
114
+ const record = this.#extractInboundRecord(event, traceId, spanId, parentSpanId);
115
+ if (record != null) records.push(record);
116
+ } else if (event.name === "activitypub.activity.sent") {
117
+ const record = this.#extractOutboundRecord(event, traceId, spanId, parentSpanId);
118
+ if (record != null) records.push(record);
119
+ }
120
+ return records;
121
+ }
122
+ #extractInboundRecord(event, traceId, spanId, parentSpanId) {
123
+ const attrs = event.attributes;
124
+ if (attrs == null) return null;
125
+ const activityJson = attrs["activitypub.activity.json"];
126
+ if (typeof activityJson !== "string") return null;
127
+ let activityType = "Unknown";
128
+ let activityId;
129
+ let actorId;
130
+ try {
131
+ const activity = JSON.parse(activityJson);
132
+ activityType = activity.type ?? "Unknown";
133
+ activityId = activity.id;
134
+ if (typeof activity.actor === "string") actorId = activity.actor;
135
+ else if (activity.actor != null && typeof activity.actor.id === "string") actorId = activity.actor.id;
136
+ } catch {}
137
+ const verified = attrs["activitypub.activity.verified"];
138
+ const httpSigVerified = attrs["http_signatures.verified"];
139
+ const httpSigKeyId = attrs["http_signatures.key_id"];
140
+ const ldSigVerified = attrs["ld_signatures.verified"];
141
+ let signatureDetails;
142
+ if (typeof httpSigVerified === "boolean" || typeof ldSigVerified === "boolean") signatureDetails = {
143
+ httpSignaturesVerified: httpSigVerified === true,
144
+ httpSignaturesKeyId: typeof httpSigKeyId === "string" && httpSigKeyId !== "" ? httpSigKeyId : void 0,
145
+ ldSignaturesVerified: ldSigVerified === true
146
+ };
147
+ return {
148
+ traceId,
149
+ spanId,
150
+ parentSpanId,
151
+ direction: "inbound",
152
+ activityType,
153
+ activityId,
154
+ actorId,
155
+ activityJson,
156
+ verified: typeof verified === "boolean" ? verified : void 0,
157
+ signatureDetails,
158
+ timestamp: (/* @__PURE__ */ new Date(event.time[0] * 1e3 + event.time[1] / 1e6)).toISOString()
159
+ };
160
+ }
161
+ #extractOutboundRecord(event, traceId, spanId, parentSpanId) {
162
+ const attrs = event.attributes;
163
+ if (attrs == null) return null;
164
+ const activityJson = attrs["activitypub.activity.json"];
165
+ if (typeof activityJson !== "string") return null;
166
+ let activityType = "Unknown";
167
+ let activityId;
168
+ let actorId;
169
+ try {
170
+ const activity = JSON.parse(activityJson);
171
+ activityType = activity.type ?? "Unknown";
172
+ activityId = activity.id;
173
+ if (typeof activity.actor === "string") actorId = activity.actor;
174
+ else if (activity.actor != null && typeof activity.actor.id === "string") actorId = activity.actor.id;
175
+ } catch {}
176
+ const inboxUrl = attrs["activitypub.inbox.url"];
177
+ const explicitActivityId = attrs["activitypub.activity.id"];
178
+ return {
179
+ traceId,
180
+ spanId,
181
+ parentSpanId,
182
+ direction: "outbound",
183
+ activityType,
184
+ activityId: activityId ?? (typeof explicitActivityId === "string" && explicitActivityId !== "" ? explicitActivityId : void 0),
185
+ actorId,
186
+ activityJson,
187
+ timestamp: (/* @__PURE__ */ new Date(event.time[0] * 1e3 + event.time[1] / 1e6)).toISOString(),
188
+ inboxUrl: typeof inboxUrl === "string" ? inboxUrl : void 0
189
+ };
190
+ }
191
+ async #storeRecord(record) {
192
+ const options = this.#ttl != null ? { ttl: this.#ttl } : void 0;
193
+ const key = [
194
+ ...this.#keyPrefix,
195
+ record.traceId,
196
+ record.spanId
197
+ ];
198
+ await this.#kv.set(key, record, options);
199
+ await this.#updateTraceSummary(record, options);
200
+ }
201
+ async #setWithCasRetry(key, transform, options) {
202
+ if (this.#kv.cas != null) for (let attempt = 0; attempt < 3; attempt++) {
203
+ const existing$1 = await this.#kv.get(key);
204
+ const newValue$1 = transform(existing$1);
205
+ if (await this.#kv.cas(key, existing$1, newValue$1, options)) return;
206
+ }
207
+ const existing = await this.#kv.get(key);
208
+ const newValue = transform(existing);
209
+ await this.#kv.set(key, newValue, options);
210
+ }
211
+ async #updateTraceSummary(record, options) {
212
+ const summaryKey = [
213
+ ...this.#keyPrefix,
214
+ "_summaries",
215
+ record.traceId
216
+ ];
217
+ await this.#setWithCasRetry(summaryKey, (existing) => {
218
+ const activityCount = existing != null ? existing.activityCount + 1 : 1;
219
+ const activityTypes = existing != null ? existing.activityTypes.includes(record.activityType) ? existing.activityTypes : [...existing.activityTypes, record.activityType] : [record.activityType];
220
+ return {
221
+ traceId: existing?.traceId ?? record.traceId,
222
+ timestamp: existing?.timestamp ?? record.timestamp,
223
+ activityCount,
224
+ activityTypes
225
+ };
226
+ }, options);
227
+ }
228
+ /**
229
+ * Gets all activity records for a specific trace ID.
230
+ *
231
+ * @param traceId The trace ID to query.
232
+ * @returns An array of activity records belonging to the trace.
233
+ */
234
+ async getActivitiesByTraceId(traceId) {
235
+ const prefix = [...this.#keyPrefix, traceId];
236
+ const records = [];
237
+ for await (const entry of this.#kv.list(prefix)) records.push(entry.value);
238
+ return records;
239
+ }
240
+ /**
241
+ * Gets recent traces with summary information.
242
+ *
243
+ * @param options Options for the query.
244
+ * @returns An array of trace summaries.
245
+ */
246
+ async getRecentTraces(options) {
247
+ const summaryPrefix = [...this.#keyPrefix, "_summaries"];
248
+ const summaries = [];
249
+ for await (const entry of this.#kv.list(summaryPrefix)) summaries.push(entry.value);
250
+ summaries.sort((a, b) => b.timestamp.localeCompare(a.timestamp));
251
+ if (options?.limit != null) return summaries.slice(0, options.limit);
252
+ return summaries;
253
+ }
254
+ /**
255
+ * Forces the exporter to flush any buffered data.
256
+ * This is a no-op because we write directly to the KvStore without buffering.
257
+ */
258
+ async forceFlush() {}
259
+ /**
260
+ * Shuts down the exporter.
261
+ */
262
+ async shutdown() {}
263
+ };
264
+
265
+ //#endregion
266
+ //#region src/otel/exporter.test.ts
267
+ function createMockSpan(options) {
268
+ const traceId = options.traceId ?? "0123456789abcdef0123456789abcdef";
269
+ const spanId = options.spanId ?? "0123456789abcdef";
270
+ const spanContext = {
271
+ traceId,
272
+ spanId,
273
+ traceFlags: TraceFlags.SAMPLED
274
+ };
275
+ const parentSpanContext = options.parentSpanId ? {
276
+ traceId,
277
+ spanId: options.parentSpanId,
278
+ traceFlags: TraceFlags.SAMPLED
279
+ } : void 0;
280
+ return {
281
+ name: options.name ?? "test-span",
282
+ kind: SpanKind.INTERNAL,
283
+ spanContext: () => spanContext,
284
+ parentSpanContext,
285
+ startTime: [17e8, 0],
286
+ endTime: [1700000001, 0],
287
+ status: { code: SpanStatusCode.OK },
288
+ attributes: {},
289
+ links: [],
290
+ events: options.events ?? [],
291
+ duration: [1, 0],
292
+ ended: true,
293
+ resource: {
294
+ attributes: {},
295
+ getRawAttributes: () => [],
296
+ merge: () => ({
297
+ attributes: {},
298
+ getRawAttributes: () => [],
299
+ merge: () => null
300
+ })
301
+ },
302
+ instrumentationScope: { name: "test" },
303
+ droppedAttributesCount: 0,
304
+ droppedEventsCount: 0,
305
+ droppedLinksCount: 0
306
+ };
307
+ }
308
+ function createActivityReceivedEvent(options) {
309
+ return {
310
+ name: "activitypub.activity.received",
311
+ time: [17e8, 5e8],
312
+ attributes: {
313
+ "activitypub.activity.json": options.activityJson,
314
+ "activitypub.activity.verified": options.verified ?? true,
315
+ "ld_signatures.verified": options.ldSigVerified ?? false,
316
+ "http_signatures.verified": options.httpSigVerified ?? true,
317
+ "http_signatures.key_id": options.httpSigKeyId ?? ""
318
+ }
319
+ };
320
+ }
321
+ function createActivitySentEvent(options) {
322
+ return {
323
+ name: "activitypub.activity.sent",
324
+ time: [17e8, 5e8],
325
+ attributes: {
326
+ "activitypub.activity.json": options.activityJson,
327
+ "activitypub.inbox.url": options.inboxUrl,
328
+ "activitypub.activity.id": options.activityId ?? ""
329
+ }
330
+ };
331
+ }
332
+ test("FedifySpanExporter", async (t) => {
333
+ await t.step("constructor accepts KvStore with list()", () => {
334
+ const kv = new MemoryKvStore();
335
+ const exporter = new FedifySpanExporter(kv);
336
+ assertEquals(exporter instanceof FedifySpanExporter, true);
337
+ });
338
+ await t.step("export() stores inbound activity from span event", async () => {
339
+ const kv = new MemoryKvStore();
340
+ const exporter = new FedifySpanExporter(kv);
341
+ const traceId = "trace123";
342
+ const spanId = "span456";
343
+ const activity = {
344
+ "@context": "https://www.w3.org/ns/activitystreams",
345
+ type: "Create",
346
+ id: "https://example.com/activities/123",
347
+ actor: "https://example.com/users/alice",
348
+ object: {
349
+ type: "Note",
350
+ content: "Hello!"
351
+ }
352
+ };
353
+ const activityJson = JSON.stringify(activity);
354
+ const span = createMockSpan({
355
+ traceId,
356
+ spanId,
357
+ name: "activitypub.inbox",
358
+ events: [createActivityReceivedEvent({
359
+ activityJson,
360
+ verified: true,
361
+ httpSigVerified: true
362
+ })]
363
+ });
364
+ await new Promise((resolve) => {
365
+ exporter.export([span], (result) => {
366
+ assertEquals(result.code, 0);
367
+ resolve();
368
+ });
369
+ });
370
+ const activities = await exporter.getActivitiesByTraceId(traceId);
371
+ assertEquals(activities.length, 1);
372
+ assertEquals(activities[0].traceId, traceId);
373
+ assertEquals(activities[0].spanId, spanId);
374
+ assertEquals(activities[0].direction, "inbound");
375
+ assertEquals(activities[0].activityType, activity.type);
376
+ assertEquals(activities[0].activityId, activity.id);
377
+ assertEquals(activities[0].activityJson, activityJson);
378
+ assertEquals(activities[0].verified, true);
379
+ });
380
+ await t.step("export() stores outbound activity from span event", async () => {
381
+ const kv = new MemoryKvStore();
382
+ const exporter = new FedifySpanExporter(kv);
383
+ const traceId = "trace789";
384
+ const spanId = "span012";
385
+ const inboxUrl = "https://example.com/users/alice/inbox";
386
+ const activity = {
387
+ "@context": "https://www.w3.org/ns/activitystreams",
388
+ type: "Follow",
389
+ id: "https://myserver.com/activities/789",
390
+ actor: "https://myserver.com/users/bob",
391
+ object: "https://example.com/users/alice"
392
+ };
393
+ const activityJson = JSON.stringify(activity);
394
+ const span = createMockSpan({
395
+ traceId,
396
+ spanId,
397
+ name: "activitypub.send_activity",
398
+ events: [createActivitySentEvent({
399
+ activityJson,
400
+ inboxUrl,
401
+ activityId: activity.id
402
+ })]
403
+ });
404
+ await new Promise((resolve) => {
405
+ exporter.export([span], (result) => {
406
+ assertEquals(result.code, 0);
407
+ resolve();
408
+ });
409
+ });
410
+ const activities = await exporter.getActivitiesByTraceId(traceId);
411
+ assertEquals(activities.length, 1);
412
+ assertEquals(activities[0].traceId, traceId);
413
+ assertEquals(activities[0].spanId, spanId);
414
+ assertEquals(activities[0].direction, "outbound");
415
+ assertEquals(activities[0].activityType, activity.type);
416
+ assertEquals(activities[0].activityId, activity.id);
417
+ assertEquals(activities[0].inboxUrl, inboxUrl);
418
+ });
419
+ await t.step("export() ignores spans without activity events", async () => {
420
+ const kv = new MemoryKvStore();
421
+ const exporter = new FedifySpanExporter(kv);
422
+ const span = createMockSpan({
423
+ traceId: "trace999",
424
+ spanId: "span999",
425
+ name: "some-other-span",
426
+ events: []
427
+ });
428
+ await new Promise((resolve) => {
429
+ exporter.export([span], (result) => {
430
+ assertEquals(result.code, 0);
431
+ resolve();
432
+ });
433
+ });
434
+ const activities = await exporter.getActivitiesByTraceId("trace999");
435
+ assertEquals(activities.length, 0);
436
+ });
437
+ await t.step("export() stores multiple activities from same trace", async () => {
438
+ const kv = new MemoryKvStore();
439
+ const exporter = new FedifySpanExporter(kv);
440
+ const inboundActivity = JSON.stringify({
441
+ "@context": "https://www.w3.org/ns/activitystreams",
442
+ type: "Create",
443
+ id: "https://example.com/activities/1"
444
+ });
445
+ const outboundActivity = JSON.stringify({
446
+ "@context": "https://www.w3.org/ns/activitystreams",
447
+ type: "Accept",
448
+ id: "https://myserver.com/activities/2"
449
+ });
450
+ const span1 = createMockSpan({
451
+ traceId: "multitrace",
452
+ spanId: "span1",
453
+ name: "activitypub.inbox",
454
+ events: [createActivityReceivedEvent({ activityJson: inboundActivity })]
455
+ });
456
+ const span2 = createMockSpan({
457
+ traceId: "multitrace",
458
+ spanId: "span2",
459
+ parentSpanId: "span1",
460
+ name: "activitypub.send_activity",
461
+ events: [createActivitySentEvent({
462
+ activityJson: outboundActivity,
463
+ inboxUrl: "https://example.com/inbox"
464
+ })]
465
+ });
466
+ await new Promise((resolve) => {
467
+ exporter.export([span1, span2], (result) => {
468
+ assertEquals(result.code, 0);
469
+ resolve();
470
+ });
471
+ });
472
+ const activities = await exporter.getActivitiesByTraceId("multitrace");
473
+ assertEquals(activities.length, 2);
474
+ const inbound = activities.find((a) => a.direction === "inbound");
475
+ const outbound = activities.find((a) => a.direction === "outbound");
476
+ assertEquals(inbound?.activityType, "Create");
477
+ assertEquals(outbound?.activityType, "Accept");
478
+ assertEquals(outbound?.parentSpanId, "span1");
479
+ });
480
+ await t.step("getRecentTraces() returns recent traces", async () => {
481
+ const kv = new MemoryKvStore();
482
+ const exporter = new FedifySpanExporter(kv);
483
+ for (let i = 0; i < 5; i++) {
484
+ const activityJson = JSON.stringify({
485
+ "@context": "https://www.w3.org/ns/activitystreams",
486
+ type: "Create",
487
+ id: `https://example.com/activities/${i}`
488
+ });
489
+ const span = createMockSpan({
490
+ traceId: `trace-${i}`,
491
+ spanId: `span-${i}`,
492
+ name: "activitypub.inbox",
493
+ events: [createActivityReceivedEvent({ activityJson })]
494
+ });
495
+ await new Promise((resolve) => {
496
+ exporter.export([span], () => resolve());
497
+ });
498
+ }
499
+ const traces = await exporter.getRecentTraces({ limit: 3 });
500
+ assertEquals(traces.length, 3);
501
+ });
502
+ await t.step("getRecentTraces() returns all traces when limit not specified", async () => {
503
+ const kv = new MemoryKvStore();
504
+ const exporter = new FedifySpanExporter(kv);
505
+ for (let i = 0; i < 3; i++) {
506
+ const activityJson = JSON.stringify({
507
+ "@context": "https://www.w3.org/ns/activitystreams",
508
+ type: "Create",
509
+ id: `https://example.com/activities/${i}`
510
+ });
511
+ const span = createMockSpan({
512
+ traceId: `all-trace-${i}`,
513
+ spanId: `span-${i}`,
514
+ name: "activitypub.inbox",
515
+ events: [createActivityReceivedEvent({ activityJson })]
516
+ });
517
+ await new Promise((resolve) => {
518
+ exporter.export([span], () => resolve());
519
+ });
520
+ }
521
+ const traces = await exporter.getRecentTraces();
522
+ assertEquals(traces.length >= 3, true);
523
+ });
524
+ await t.step("forceFlush() returns resolved promise", async () => {
525
+ const kv = new MemoryKvStore();
526
+ const exporter = new FedifySpanExporter(kv);
527
+ await exporter.forceFlush();
528
+ });
529
+ await t.step("shutdown() completes successfully", async () => {
530
+ const kv = new MemoryKvStore();
531
+ const exporter = new FedifySpanExporter(kv);
532
+ await exporter.shutdown();
533
+ });
534
+ await t.step("works with custom KvStore implementation", async () => {
535
+ const storedData = {};
536
+ const kv = {
537
+ get: (key) => {
538
+ const k = JSON.stringify(key);
539
+ return Promise.resolve(storedData[k]);
540
+ },
541
+ set: (key, value) => {
542
+ const k = JSON.stringify(key);
543
+ storedData[k] = value;
544
+ return Promise.resolve();
545
+ },
546
+ delete: (key) => {
547
+ const k = JSON.stringify(key);
548
+ delete storedData[k];
549
+ return Promise.resolve();
550
+ },
551
+ async *list(prefix) {
552
+ for (const [encodedKey, value] of Object.entries(storedData)) {
553
+ const key = JSON.parse(encodedKey);
554
+ if (prefix != null) {
555
+ if (key.length < prefix.length) continue;
556
+ if (!prefix.every((p, i) => key[i] === p)) continue;
557
+ }
558
+ yield {
559
+ key,
560
+ value
561
+ };
562
+ }
563
+ }
564
+ };
565
+ const exporter = new FedifySpanExporter(kv);
566
+ const activityJson = JSON.stringify({
567
+ "@context": "https://www.w3.org/ns/activitystreams",
568
+ type: "Like",
569
+ id: "https://example.com/activities/like"
570
+ });
571
+ const span = createMockSpan({
572
+ traceId: "cas-trace",
573
+ spanId: "cas-span",
574
+ name: "activitypub.inbox",
575
+ events: [createActivityReceivedEvent({ activityJson })]
576
+ });
577
+ await new Promise((resolve) => {
578
+ exporter.export([span], (result) => {
579
+ assertEquals(result.code, 0);
580
+ resolve();
581
+ });
582
+ });
583
+ const activities = await exporter.getActivitiesByTraceId("cas-trace");
584
+ assertEquals(activities.length, 1);
585
+ assertEquals(activities[0].activityType, "Like");
586
+ });
587
+ await t.step("TTL option is respected", async () => {
588
+ const kv = new MemoryKvStore();
589
+ const exporter = new FedifySpanExporter(kv, { ttl: Temporal.Duration.from({ hours: 1 }) });
590
+ const activityJson = JSON.stringify({
591
+ "@context": "https://www.w3.org/ns/activitystreams",
592
+ type: "Create"
593
+ });
594
+ const span = createMockSpan({
595
+ traceId: "ttl-trace",
596
+ spanId: "ttl-span",
597
+ name: "activitypub.inbox",
598
+ events: [createActivityReceivedEvent({ activityJson })]
599
+ });
600
+ await new Promise((resolve) => {
601
+ exporter.export([span], () => resolve());
602
+ });
603
+ const activities = await exporter.getActivitiesByTraceId("ttl-trace");
604
+ assertEquals(activities.length, 1);
605
+ });
606
+ await t.step("keyPrefix option customizes storage keys", async () => {
607
+ const kv = new MemoryKvStore();
608
+ const exporter = new FedifySpanExporter(kv, { keyPrefix: ["custom", "prefix"] });
609
+ const activityJson = JSON.stringify({
610
+ "@context": "https://www.w3.org/ns/activitystreams",
611
+ type: "Announce"
612
+ });
613
+ const span = createMockSpan({
614
+ traceId: "prefix-trace",
615
+ spanId: "prefix-span",
616
+ name: "activitypub.inbox",
617
+ events: [createActivityReceivedEvent({ activityJson })]
618
+ });
619
+ await new Promise((resolve) => {
620
+ exporter.export([span], () => resolve());
621
+ });
622
+ const activities = await exporter.getActivitiesByTraceId("prefix-trace");
623
+ assertEquals(activities.length, 1);
624
+ assertEquals(activities[0].activityType, "Announce");
625
+ });
626
+ await t.step("separate exporter instances share state via same KvStore (distributed simulation)", async () => {
627
+ const sharedKv = new MemoryKvStore();
628
+ const webServerExporter = new FedifySpanExporter(sharedKv);
629
+ const workerExporter = new FedifySpanExporter(sharedKv);
630
+ const dashboardExporter = new FedifySpanExporter(sharedKv);
631
+ const inboundActivity = JSON.stringify({
632
+ "@context": "https://www.w3.org/ns/activitystreams",
633
+ type: "Follow",
634
+ id: "https://remote.example/activities/follow-1",
635
+ actor: "https://remote.example/users/alice",
636
+ object: "https://local.example/users/bob"
637
+ });
638
+ const inboxSpan = createMockSpan({
639
+ traceId: "distributed-trace-001",
640
+ spanId: "inbox-span",
641
+ name: "activitypub.inbox",
642
+ events: [createActivityReceivedEvent({
643
+ activityJson: inboundActivity,
644
+ verified: true
645
+ })]
646
+ });
647
+ await new Promise((resolve) => {
648
+ webServerExporter.export([inboxSpan], () => resolve());
649
+ });
650
+ const outboundActivity = JSON.stringify({
651
+ "@context": "https://www.w3.org/ns/activitystreams",
652
+ type: "Accept",
653
+ id: "https://local.example/activities/accept-1",
654
+ actor: "https://local.example/users/bob",
655
+ object: "https://remote.example/activities/follow-1"
656
+ });
657
+ const sendSpan = createMockSpan({
658
+ traceId: "distributed-trace-001",
659
+ spanId: "send-span",
660
+ parentSpanId: "inbox-span",
661
+ name: "activitypub.send_activity",
662
+ events: [createActivitySentEvent({
663
+ activityJson: outboundActivity,
664
+ inboxUrl: "https://remote.example/users/alice/inbox",
665
+ activityId: "https://local.example/activities/accept-1"
666
+ })]
667
+ });
668
+ await new Promise((resolve) => {
669
+ workerExporter.export([sendSpan], () => resolve());
670
+ });
671
+ const activities = await dashboardExporter.getActivitiesByTraceId("distributed-trace-001");
672
+ assertEquals(activities.length, 2);
673
+ const follow = activities.find((a) => a.activityType === "Follow");
674
+ const accept = activities.find((a) => a.activityType === "Accept");
675
+ assertEquals(follow != null, true);
676
+ assertEquals(follow?.direction, "inbound");
677
+ assertEquals(follow?.verified, true);
678
+ assertEquals(accept != null, true);
679
+ assertEquals(accept?.direction, "outbound");
680
+ assertEquals(accept?.inboxUrl, "https://remote.example/users/alice/inbox");
681
+ assertEquals(accept?.parentSpanId, "inbox-span");
682
+ const recentTraces = await dashboardExporter.getRecentTraces();
683
+ const ourTrace = recentTraces.find((t$1) => t$1.traceId === "distributed-trace-001");
684
+ assertEquals(ourTrace != null, true);
685
+ assertEquals(ourTrace?.activityCount, 2);
686
+ assertEquals(ourTrace?.activityTypes.includes("Follow"), true);
687
+ assertEquals(ourTrace?.activityTypes.includes("Accept"), true);
688
+ });
689
+ await t.step("multiple workers writing to same trace concurrently", async () => {
690
+ const sharedKv = new MemoryKvStore();
691
+ const workers = [
692
+ new FedifySpanExporter(sharedKv),
693
+ new FedifySpanExporter(sharedKv),
694
+ new FedifySpanExporter(sharedKv)
695
+ ];
696
+ const traceId = "concurrent-fanout-trace";
697
+ const exportPromises = workers.map((worker, i) => {
698
+ const activityJson = JSON.stringify({
699
+ "@context": "https://www.w3.org/ns/activitystreams",
700
+ type: "Create",
701
+ id: "https://local.example/activities/post-1"
702
+ });
703
+ const span = createMockSpan({
704
+ traceId,
705
+ spanId: `worker-${i}-span`,
706
+ name: "activitypub.send_activity",
707
+ events: [createActivitySentEvent({
708
+ activityJson,
709
+ inboxUrl: `https://follower-${i}.example/inbox`
710
+ })]
711
+ });
712
+ return new Promise((resolve) => {
713
+ worker.export([span], () => resolve());
714
+ });
715
+ });
716
+ await Promise.all(exportPromises);
717
+ const dashboard = new FedifySpanExporter(sharedKv);
718
+ const activities = await dashboard.getActivitiesByTraceId(traceId);
719
+ assertEquals(activities.length, 3);
720
+ const inboxUrls = activities.map((a) => a.inboxUrl).sort();
721
+ assertEquals(inboxUrls, [
722
+ "https://follower-0.example/inbox",
723
+ "https://follower-1.example/inbox",
724
+ "https://follower-2.example/inbox"
725
+ ]);
726
+ });
727
+ await t.step("extracts actorId from activity with string actor", async () => {
728
+ const kv = new MemoryKvStore();
729
+ const exporter = new FedifySpanExporter(kv);
730
+ const activityJson = JSON.stringify({
731
+ "@context": "https://www.w3.org/ns/activitystreams",
732
+ type: "Create",
733
+ id: "https://example.com/activities/123",
734
+ actor: "https://example.com/users/alice",
735
+ object: {
736
+ type: "Note",
737
+ content: "Hello!"
738
+ }
739
+ });
740
+ const span = createMockSpan({
741
+ traceId: "actor-string-trace",
742
+ spanId: "span1",
743
+ name: "activitypub.inbox",
744
+ events: [createActivityReceivedEvent({ activityJson })]
745
+ });
746
+ await new Promise((resolve) => {
747
+ exporter.export([span], () => resolve());
748
+ });
749
+ const activities = await exporter.getActivitiesByTraceId("actor-string-trace");
750
+ assertEquals(activities.length, 1);
751
+ assertEquals(activities[0].actorId, "https://example.com/users/alice");
752
+ });
753
+ await t.step("extracts actorId from activity with object actor", async () => {
754
+ const kv = new MemoryKvStore();
755
+ const exporter = new FedifySpanExporter(kv);
756
+ const activityJson = JSON.stringify({
757
+ "@context": "https://www.w3.org/ns/activitystreams",
758
+ type: "Create",
759
+ id: "https://example.com/activities/456",
760
+ actor: {
761
+ type: "Person",
762
+ id: "https://example.com/users/bob",
763
+ name: "Bob"
764
+ },
765
+ object: {
766
+ type: "Note",
767
+ content: "Hello!"
768
+ }
769
+ });
770
+ const span = createMockSpan({
771
+ traceId: "actor-object-trace",
772
+ spanId: "span1",
773
+ name: "activitypub.inbox",
774
+ events: [createActivityReceivedEvent({ activityJson })]
775
+ });
776
+ await new Promise((resolve) => {
777
+ exporter.export([span], () => resolve());
778
+ });
779
+ const activities = await exporter.getActivitiesByTraceId("actor-object-trace");
780
+ assertEquals(activities.length, 1);
781
+ assertEquals(activities[0].actorId, "https://example.com/users/bob");
782
+ });
783
+ await t.step("extracts actorId from outbound activity", async () => {
784
+ const kv = new MemoryKvStore();
785
+ const exporter = new FedifySpanExporter(kv);
786
+ const activityJson = JSON.stringify({
787
+ "@context": "https://www.w3.org/ns/activitystreams",
788
+ type: "Follow",
789
+ id: "https://myserver.com/activities/789",
790
+ actor: "https://myserver.com/users/charlie",
791
+ object: "https://example.com/users/alice"
792
+ });
793
+ const span = createMockSpan({
794
+ traceId: "outbound-actor-trace",
795
+ spanId: "span1",
796
+ name: "activitypub.send_activity",
797
+ events: [createActivitySentEvent({
798
+ activityJson,
799
+ inboxUrl: "https://example.com/users/alice/inbox"
800
+ })]
801
+ });
802
+ await new Promise((resolve) => {
803
+ exporter.export([span], () => resolve());
804
+ });
805
+ const activities = await exporter.getActivitiesByTraceId("outbound-actor-trace");
806
+ assertEquals(activities.length, 1);
807
+ assertEquals(activities[0].actorId, "https://myserver.com/users/charlie");
808
+ });
809
+ await t.step("extracts signature verification details for inbound activity", async () => {
810
+ const kv = new MemoryKvStore();
811
+ const exporter = new FedifySpanExporter(kv);
812
+ const activityJson = JSON.stringify({
813
+ "@context": "https://www.w3.org/ns/activitystreams",
814
+ type: "Create",
815
+ id: "https://example.com/activities/sig-test",
816
+ actor: "https://example.com/users/alice"
817
+ });
818
+ const span = createMockSpan({
819
+ traceId: "sig-details-trace",
820
+ spanId: "span1",
821
+ name: "activitypub.inbox",
822
+ events: [createActivityReceivedEvent({
823
+ activityJson,
824
+ verified: true,
825
+ httpSigVerified: true,
826
+ httpSigKeyId: "https://example.com/users/alice#main-key",
827
+ ldSigVerified: false
828
+ })]
829
+ });
830
+ await new Promise((resolve) => {
831
+ exporter.export([span], () => resolve());
832
+ });
833
+ const activities = await exporter.getActivitiesByTraceId("sig-details-trace");
834
+ assertEquals(activities.length, 1);
835
+ assertEquals(activities[0].verified, true);
836
+ assertEquals(activities[0].signatureDetails != null, true);
837
+ assertEquals(activities[0].signatureDetails?.httpSignaturesVerified, true);
838
+ assertEquals(activities[0].signatureDetails?.httpSignaturesKeyId, "https://example.com/users/alice#main-key");
839
+ assertEquals(activities[0].signatureDetails?.ldSignaturesVerified, false);
840
+ });
841
+ await t.step("signature details with LD signatures verified", async () => {
842
+ const kv = new MemoryKvStore();
843
+ const exporter = new FedifySpanExporter(kv);
844
+ const activityJson = JSON.stringify({
845
+ "@context": "https://www.w3.org/ns/activitystreams",
846
+ type: "Delete",
847
+ id: "https://example.com/activities/ld-sig-test",
848
+ actor: "https://example.com/users/alice"
849
+ });
850
+ const span = createMockSpan({
851
+ traceId: "ld-sig-trace",
852
+ spanId: "span1",
853
+ name: "activitypub.inbox",
854
+ events: [createActivityReceivedEvent({
855
+ activityJson,
856
+ verified: true,
857
+ httpSigVerified: false,
858
+ ldSigVerified: true
859
+ })]
860
+ });
861
+ await new Promise((resolve) => {
862
+ exporter.export([span], () => resolve());
863
+ });
864
+ const activities = await exporter.getActivitiesByTraceId("ld-sig-trace");
865
+ assertEquals(activities.length, 1);
866
+ assertEquals(activities[0].signatureDetails?.httpSignaturesVerified, false);
867
+ assertEquals(activities[0].signatureDetails?.ldSignaturesVerified, true);
868
+ });
869
+ await t.step("handles activity without actor field", async () => {
870
+ const kv = new MemoryKvStore();
871
+ const exporter = new FedifySpanExporter(kv);
872
+ const activityJson = JSON.stringify({
873
+ "@context": "https://www.w3.org/ns/activitystreams",
874
+ type: "Delete",
875
+ id: "https://example.com/activities/no-actor",
876
+ object: "https://example.com/posts/123"
877
+ });
878
+ const span = createMockSpan({
879
+ traceId: "no-actor-trace",
880
+ spanId: "span1",
881
+ name: "activitypub.inbox",
882
+ events: [createActivityReceivedEvent({ activityJson })]
883
+ });
884
+ await new Promise((resolve) => {
885
+ exporter.export([span], () => resolve());
886
+ });
887
+ const activities = await exporter.getActivitiesByTraceId("no-actor-trace");
888
+ assertEquals(activities.length, 1);
889
+ assertEquals(activities[0].actorId, void 0);
890
+ });
891
+ });
892
+
893
+ //#endregion