@fedify/fedify 2.0.0-pr.490.2 → 2.0.0-pr.559.4

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 (267) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +64 -37
  3. package/dist/{builder-4syLV1-z.js → builder-DTlQwmVF.js} +10 -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-PxGADCsD.d.cts → context-B6X-7loD.d.cts} +206 -74
  13. package/dist/{context-V-XS2_6O.d.ts → context-CJaICYPw.d.ts} +206 -74
  14. package/dist/context-CZ5llAss.js +109 -0
  15. package/dist/deno-DGx1JZHr.js +124 -0
  16. package/dist/{testing-BslrM_9E.js → dist-B5f6a8Tt.js} +90 -110
  17. package/dist/{docloader-DndkGj0O.js → docloader-D8UHsyqD.js} +3 -3
  18. package/dist/{esm-VlKMJQqV.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 +24 -23
  22. package/dist/federation/idempotency.test.js +59 -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 +307 -93
  27. package/dist/federation/mod.cjs +9 -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 +167 -16
  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 +93 -11
  36. package/dist/{webfinger/handler.test.js → federation/webfinger.test.js} +24 -22
  37. package/dist/{federation-CRpdnOMS.cjs → federation-CE0CJ_0G.cjs} +116 -10
  38. package/dist/{federation-jcR8-ZxP.js → federation-D6FVaeAR.js} +122 -16
  39. package/dist/{http-YhR_TMMQ.js → http-CL3G0rnf.js} +126 -9
  40. package/dist/{http-M8k5mKc0.d.cts → http-ClB3pLcL.d.cts} +1 -1
  41. package/dist/{http-Dxpqz4hE.cjs → http-DKBUv5zZ.cjs} +139 -16
  42. package/dist/{http-BbO0ejuk.d.ts → http-DLBDPal9.d.ts} +1 -1
  43. package/dist/{http-DH47B-h3.js → http-LGtYlSfN.js} +3 -2
  44. package/dist/{inbox-CEyHvxOo.js → inbox-DbtWQY2D.js} +2 -1
  45. package/dist/{key-x7E5PYI0.js → key-BCtt1Ugy.js} +3 -3
  46. package/dist/{keycache-BRXuBDuy.js → keycache-DRxpZ5r9.js} +1 -1
  47. package/dist/{keys-DLk_8H-l.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-HFnFIjSD.js → kv-cache-0786BfqY.js} +3 -3
  52. package/dist/{kv-cache-DN9pfMBe.js → kv-cache-B__dHl7g.js} +15 -2
  53. package/dist/{kv-cache-BMpfJFTx.cjs → kv-cache-DCJojeTn.cjs} +3 -3
  54. package/dist/{ld-CRPaU6c8.js → ld-QlZPwGEH.js} +4 -3
  55. package/dist/middleware-B3jUPnDa.js +12 -0
  56. package/dist/middleware-BFiwWMA2.cjs +12 -0
  57. package/dist/middleware-DMx6DyIw.js +26 -0
  58. package/dist/{middleware-DfLpMu7C.js → middleware-Dm58nObp.js} +280 -166
  59. package/dist/{middleware-BIqFwRwI.js → middleware-WokE4qxc.js} +245 -178
  60. package/dist/{middleware-Ck7O6mb0.cjs → middleware-hWyKOO_6.cjs} +332 -206
  61. package/dist/{mod-DMpuiKXi.d.cts → mod-BHXq4Q3x.d.cts} +7 -7
  62. package/dist/{mod-DgxG-byT.d.cts → mod-BrS8tiad.d.cts} +2 -2
  63. package/dist/mod-CoMP50Rf.d.ts +64 -0
  64. package/dist/{mod-BoRKfJPE.d.cts → mod-DScazwCW.d.cts} +4 -4
  65. package/dist/mod-DTzN6Pv3.d.cts +62 -0
  66. package/dist/{mod-aAE2wOWV.d.ts → mod-DZmuPaKv.d.ts} +7 -7
  67. package/dist/{mod-D5Z2tISD.d.ts → mod-jOa7W503.d.ts} +2 -2
  68. package/dist/{mod-Cdo6SYlJ.d.ts → mod-xKJ57rwu.d.ts} +4 -4
  69. package/dist/mod.cjs +12 -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-CIWnopkT.js → owner-Cejm-F7S.js} +2 -2
  88. package/dist/{proof-D-5ri6rf.js → proof-BOQBHd-i.js} +3 -2
  89. package/dist/{proof-fEwcA7LA.cjs → proof-Bmi8ZIcW.cjs} +24 -25
  90. package/dist/{proof-C8-2l0zH.js → proof-CnaEQ_Ev.js} +4 -5
  91. package/dist/router-D9eI0s4b.js +118 -0
  92. package/dist/{send-CPGk9QKZ.js → send-jFxXfsN8.js} +38 -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 +6 -7
  101. package/dist/sig/proof.test.js +6 -6
  102. package/dist/testing/mod.d.ts +173 -7006
  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/dist/vocab/mod.cjs +8 -81
  116. package/dist/vocab/mod.d.cts +1 -4
  117. package/dist/vocab/mod.d.ts +1 -4
  118. package/dist/vocab/mod.js +1 -5
  119. package/package.json +27 -27
  120. package/dist/actor-BT-e5fn9.js +0 -146
  121. package/dist/actor-B_gRMloq.js +0 -41647
  122. package/dist/actor-CBfPjuWj.cjs +0 -42079
  123. package/dist/actor-DqFajh9s.d.ts +0 -130
  124. package/dist/actor-f2NtjyCg.d.cts +0 -128
  125. package/dist/fixtures/activitypub.academy/users/brauca_darradiul.json +0 -83
  126. package/dist/fixtures/example.com/announce.json +0 -6
  127. package/dist/fixtures/example.com/collection.json +0 -19
  128. package/dist/fixtures/example.com/create.json +0 -6
  129. package/dist/fixtures/example.com/cross-origin-actor.json +0 -6
  130. package/dist/fixtures/example.com/hong-gildong.json +0 -11
  131. package/dist/fixtures/example.com/invite.json +0 -7
  132. package/dist/fixtures/example.com/key.json +0 -7
  133. package/dist/fixtures/example.com/key2.json +0 -6
  134. package/dist/fixtures/example.com/object.json +0 -6
  135. package/dist/fixtures/example.com/orderedcollectionpage.json +0 -24
  136. package/dist/fixtures/example.com/paged/a.json +0 -13
  137. package/dist/fixtures/example.com/paged/b.json +0 -16
  138. package/dist/fixtures/example.com/paged-collection.json +0 -6
  139. package/dist/fixtures/example.com/person.json +0 -22
  140. package/dist/fixtures/example.com/person2.json +0 -40
  141. package/dist/fixtures/example.com/test.json +0 -5
  142. package/dist/fixtures/example.com/users/handle.json +0 -16
  143. package/dist/fixtures/example.com/wrong-type.json +0 -3
  144. package/dist/fixtures/media.example.com/avatars/test-avatar.jpg.json +0 -6
  145. package/dist/fixtures/oeee.cafe/ap/users/3609fd4e-d51d-4db8-9f04-4189815864dd.json +0 -24
  146. package/dist/fixtures/remote.domain/users/bob.json +0 -20
  147. package/dist/fixtures/server.example/users/alice.json +0 -20
  148. package/dist/fixtures/w3id.org/identity/v1.json +0 -152
  149. package/dist/fixtures/w3id.org/security/data-integrity/v1.json +0 -74
  150. package/dist/fixtures/w3id.org/security/multikey/v1.json +0 -35
  151. package/dist/fixtures/w3id.org/security/v1.json +0 -50
  152. package/dist/fixtures/wizard.casa/users/hongminhee.json +0 -69
  153. package/dist/fixtures/www.w3.org/ns/activitystreams.json +0 -379
  154. package/dist/fixtures/www.w3.org/ns/did/v1.json +0 -58
  155. package/dist/lookup-BTqtVATt.cjs +0 -266
  156. package/dist/lookup-DOSnR912.js +0 -254
  157. package/dist/lookup-Dj9-mgOn.js +0 -42184
  158. package/dist/middleware-CxswDtQn.js +0 -15
  159. package/dist/middleware-CyITsnX0.js +0 -26
  160. package/dist/middleware-Z8lc_drL.cjs +0 -15
  161. package/dist/mod-BlVovdcy.d.ts +0 -309
  162. package/dist/mod-BxRCHTz-.d.cts +0 -307
  163. package/dist/mod-C58MZ7Wx.d.cts +0 -113
  164. package/dist/mod-CcDPcLJW.d.cts +0 -1
  165. package/dist/mod-Ds0mpFZU.d.ts +0 -115
  166. package/dist/mod-bjzj5QIb.d.ts +0 -2
  167. package/dist/otel-1BmGPuZc.js +0 -64
  168. package/dist/src/vocab/accept.yaml +0 -15
  169. package/dist/src/vocab/activity.yaml +0 -98
  170. package/dist/src/vocab/add.yaml +0 -16
  171. package/dist/src/vocab/announce.yaml +0 -30
  172. package/dist/src/vocab/application.yaml +0 -324
  173. package/dist/src/vocab/arrive.yaml +0 -15
  174. package/dist/src/vocab/article.yaml +0 -46
  175. package/dist/src/vocab/audio.yaml +0 -11
  176. package/dist/src/vocab/block.yaml +0 -16
  177. package/dist/src/vocab/chatmessage.yaml +0 -50
  178. package/dist/src/vocab/collection.yaml +0 -154
  179. package/dist/src/vocab/collectionpage.yaml +0 -55
  180. package/dist/src/vocab/create.yaml +0 -28
  181. package/dist/src/vocab/dataintegrityproof.yaml +0 -56
  182. package/dist/src/vocab/delete.yaml +0 -27
  183. package/dist/src/vocab/didservice.yaml +0 -22
  184. package/dist/src/vocab/dislike.yaml +0 -14
  185. package/dist/src/vocab/document.yaml +0 -31
  186. package/dist/src/vocab/emoji.yaml +0 -12
  187. package/dist/src/vocab/emojireact.yaml +0 -17
  188. package/dist/src/vocab/endpoints.yaml +0 -85
  189. package/dist/src/vocab/event.yaml +0 -11
  190. package/dist/src/vocab/export.yaml +0 -9
  191. package/dist/src/vocab/flag.yaml +0 -15
  192. package/dist/src/vocab/follow.yaml +0 -19
  193. package/dist/src/vocab/group.yaml +0 -324
  194. package/dist/src/vocab/hashtag.yaml +0 -14
  195. package/dist/src/vocab/ignore.yaml +0 -14
  196. package/dist/src/vocab/image.yaml +0 -9
  197. package/dist/src/vocab/intransitiveactivity.yaml +0 -15
  198. package/dist/src/vocab/invite.yaml +0 -14
  199. package/dist/src/vocab/join.yaml +0 -14
  200. package/dist/src/vocab/key.yaml +0 -28
  201. package/dist/src/vocab/leave.yaml +0 -14
  202. package/dist/src/vocab/like.yaml +0 -16
  203. package/dist/src/vocab/link.yaml +0 -101
  204. package/dist/src/vocab/listen.yaml +0 -12
  205. package/dist/src/vocab/mention.yaml +0 -9
  206. package/dist/src/vocab/move.yaml +0 -15
  207. package/dist/src/vocab/multikey.yaml +0 -36
  208. package/dist/src/vocab/note.yaml +0 -48
  209. package/dist/src/vocab/object.yaml +0 -404
  210. package/dist/src/vocab/offer.yaml +0 -15
  211. package/dist/src/vocab/orderedcollection.yaml +0 -39
  212. package/dist/src/vocab/orderedcollectionpage.yaml +0 -50
  213. package/dist/src/vocab/organization.yaml +0 -324
  214. package/dist/src/vocab/page.yaml +0 -11
  215. package/dist/src/vocab/person.yaml +0 -324
  216. package/dist/src/vocab/place.yaml +0 -75
  217. package/dist/src/vocab/profile.yaml +0 -26
  218. package/dist/src/vocab/propertyvalue.yaml +0 -32
  219. package/dist/src/vocab/question.yaml +0 -103
  220. package/dist/src/vocab/read.yaml +0 -13
  221. package/dist/src/vocab/reject.yaml +0 -14
  222. package/dist/src/vocab/relationship.yaml +0 -52
  223. package/dist/src/vocab/remove.yaml +0 -14
  224. package/dist/src/vocab/service.yaml +0 -324
  225. package/dist/src/vocab/source.yaml +0 -26
  226. package/dist/src/vocab/tentativeaccept.yaml +0 -14
  227. package/dist/src/vocab/tentativereject.yaml +0 -14
  228. package/dist/src/vocab/tombstone.yaml +0 -24
  229. package/dist/src/vocab/travel.yaml +0 -16
  230. package/dist/src/vocab/undo.yaml +0 -26
  231. package/dist/src/vocab/update.yaml +0 -58
  232. package/dist/src/vocab/video.yaml +0 -11
  233. package/dist/src/vocab/view.yaml +0 -13
  234. package/dist/testing/docloader.test.js +0 -22
  235. package/dist/vocab/actor.test.js +0 -5963
  236. package/dist/vocab/lookup.test.d.ts +0 -3
  237. package/dist/vocab/lookup.test.js +0 -476
  238. package/dist/vocab/type.test.d.ts +0 -3
  239. package/dist/vocab/type.test.js +0 -24
  240. package/dist/vocab/vocab.test.d.ts +0 -3
  241. package/dist/vocab/vocab.test.js +0 -9397
  242. package/dist/vocab-BCWe1Ih5.d.ts +0 -14905
  243. package/dist/vocab-ByUp-A2_.js +0 -260
  244. package/dist/vocab-CeDBzu-f.d.cts +0 -14903
  245. package/dist/vocab-X_X5T8D3.cjs +0 -296
  246. package/dist/webfinger/handler.test.d.ts +0 -3
  247. package/dist/webfinger/lookup.test.d.ts +0 -3
  248. package/dist/webfinger/lookup.test.js +0 -193
  249. package/dist/webfinger/mod.cjs +0 -8
  250. package/dist/webfinger/mod.d.cts +0 -2
  251. package/dist/webfinger/mod.d.ts +0 -4
  252. package/dist/webfinger/mod.js +0 -8
  253. package/dist/webfinger-C72Y8lrh.js +0 -4
  254. package/dist/webfinger-vAtLmxOF.cjs +0 -4
  255. /package/dist/{collection-BzWsN9pB.js → collection-CcnIw1qY.js} +0 -0
  256. /package/dist/{testing/docloader.test.d.ts → federation/webfinger.test.d.ts} +0 -0
  257. /package/dist/{mod-CVgZgliM.d.ts → mod-1E3W847c.d.ts} +0 -0
  258. /package/dist/{mod-B-hUPT2N.d.cts → mod-C81L6_lQ.d.cts} +0 -0
  259. /package/dist/{negotiation-C4nFufNk.js → negotiation-5NPJL6zp.js} +0 -0
  260. /package/dist/{nodeinfo-BnthBobC.js → nodeinfo-BlLsRSiT.js} +0 -0
  261. /package/dist/{nodeinfo-CdN0rEnZ.cjs → nodeinfo-DuMYTpbZ.cjs} +0 -0
  262. /package/dist/{vocab/actor.test.d.ts → otel/exporter.test.d.ts} +0 -0
  263. /package/dist/{retry-CfF8Gn4d.js → retry-D4GJ670a.js} +0 -0
  264. /package/dist/{sig-C34-oHBl.js → sig-CwuONEzF.js} +0 -0
  265. /package/dist/{sig-YYj5tCnr.cjs → sig-DeXX2xnj.cjs} +0 -0
  266. /package/dist/{utils-DyRU1gdZ.cjs → utils-Db0ZmjcD.cjs} +0 -0
  267. /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