@fedify/fedify 2.5.0-dev.2664 → 2.5.0-dev.2710

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/dist/compat/mod.d.cts +1 -1
  2. package/dist/compat/mod.d.ts +1 -1
  3. package/dist/{context-xrqq4Hrq.d.ts → context-CuLvR1Os.d.ts} +2 -2
  4. package/dist/{context-C1NqcIct.d.cts → context-apq0IBg2.d.cts} +2 -2
  5. package/dist/federation/mod.cjs +1 -1
  6. package/dist/federation/mod.d.cts +3 -3
  7. package/dist/federation/mod.d.ts +3 -3
  8. package/dist/federation/mod.js +1 -1
  9. package/dist/{federation-FbwcnRHg.cjs → federation-JvzYDx0P.cjs} +5 -5
  10. package/dist/{http-DbvOPsFZ.cjs → http-BG5cDmTS.cjs} +1 -1
  11. package/dist/{http-CWn0K5jD.js → http-MUf125Jc.js} +1 -1
  12. package/dist/{key-dwwAg5bA.d.ts → key-BZw-YiK5.d.ts} +2 -2
  13. package/dist/{key-DlHT4R6k.d.cts → key-D5fbQkKM.d.cts} +2 -2
  14. package/dist/{kv-cache-B-fOrhb-.cjs → kv-cache-A2po7h0_.cjs} +1 -1
  15. package/dist/{kv-cache-BC837EqC.js → kv-cache-CbWc1BZA.js} +1 -1
  16. package/dist/{ld-CWVpo_NM.d.cts → ld-DEi9ERHf.d.cts} +1 -1
  17. package/dist/{ld-o4LnhCYY.d.ts → ld-DlC78ioJ.d.ts} +1 -1
  18. package/dist/{middleware-n2UrG62B.js → middleware-DzzdJ7Wz.js} +5 -5
  19. package/dist/{mod-D22ZsiKi.d.ts → mod-BJRtsw7l.d.ts} +1 -1
  20. package/dist/{mod-C1nT74IG.d.cts → mod-BamwZjJx.d.cts} +1 -1
  21. package/dist/{mod-DO0epJGH.d.ts → mod-BkTi19dI.d.ts} +1 -1
  22. package/dist/{mod-CQC9xlLZ.d.ts → mod-CMtF0VVy.d.ts} +1 -1
  23. package/dist/{mod-YvA4Rk8J.d.cts → mod-D2Z9MiCm.d.cts} +1 -1
  24. package/dist/{mod-TwiQgEf3.d.cts → mod-DB2NOzQF.d.cts} +1 -1
  25. package/dist/mod.cjs +4 -4
  26. package/dist/mod.d.cts +6 -6
  27. package/dist/mod.d.ts +6 -6
  28. package/dist/mod.js +4 -4
  29. package/dist/{portable-collection-DQhIkX5a.js → portable-collection-CAer9RWN.js} +1 -1
  30. package/dist/{portable-collection-DseoiPPj.cjs → portable-collection-d8w7TCn9.cjs} +1 -1
  31. package/dist/sig/mod.cjs +2 -2
  32. package/dist/sig/mod.d.cts +3 -3
  33. package/dist/sig/mod.d.ts +3 -3
  34. package/dist/sig/mod.js +2 -2
  35. package/dist/utils/mod.cjs +1 -1
  36. package/dist/utils/mod.d.cts +1 -1
  37. package/dist/utils/mod.d.ts +1 -1
  38. package/dist/utils/mod.js +1 -1
  39. package/package.json +22 -9
  40. package/skills/fedify/SKILL.md +10 -0
  41. package/dist/accept-CPVqki78.mjs +0 -145
  42. package/dist/activity-listener-CELFAodc.mjs +0 -44
  43. package/dist/assert-D1Eafmd_.mjs +0 -23
  44. package/dist/assert_equals-Z6IcEbWU.mjs +0 -987
  45. package/dist/assert_exists-cffSpJ7X.mjs +0 -28
  46. package/dist/assert_false-CPdRnc3E.mjs +0 -23
  47. package/dist/assert_greater_or_equal-BlafOF4-.mjs +0 -31
  48. package/dist/assert_instance_of-mrXWWNY-.mjs +0 -37
  49. package/dist/assert_not_equals-CpPK2t89.mjs +0 -30
  50. package/dist/assert_rejects-CTptdYck.mjs +0 -39
  51. package/dist/assert_strict_equals-CYvBmkpC.mjs +0 -42
  52. package/dist/assert_throws-BYITOvPO.mjs +0 -74
  53. package/dist/assertion_error-B-cx0fFA.mjs +0 -31
  54. package/dist/audience-CHQ-FlLD.mjs +0 -95
  55. package/dist/builder-CGJ-NXcZ.mjs +0 -911
  56. package/dist/circuit-breaker-XJ53md4S.mjs +0 -544
  57. package/dist/circuit-breaker-test-utils-DW6YC_Pe.mjs +0 -14
  58. package/dist/client-5SIFzW3q.mjs +0 -441
  59. package/dist/codec-C6rTrpIp.mjs +0 -147
  60. package/dist/collection-CnnP1UAv.mjs +0 -44
  61. package/dist/compat/outgoing-jsonld.test.d.mts +0 -2
  62. package/dist/compat/outgoing-jsonld.test.mjs +0 -257
  63. package/dist/compat/public-audience.test.d.mts +0 -2
  64. package/dist/compat/public-audience.test.mjs +0 -187
  65. package/dist/compat/secured-document.test.d.mts +0 -2
  66. package/dist/compat/secured-document.test.mjs +0 -58
  67. package/dist/compat/transformers.test.d.mts +0 -2
  68. package/dist/compat/transformers.test.mjs +0 -62
  69. package/dist/compound-proof-CnzwOddk.mjs +0 -1019
  70. package/dist/context-LgGFFSz7.mjs +0 -146
  71. package/dist/deno-eoFpPk7W.mjs +0 -8
  72. package/dist/dist-_geWsuaI.mjs +0 -189
  73. package/dist/docloader-DwkipyL1.mjs +0 -136
  74. package/dist/esm-BHXXx4Wx.mjs +0 -1233
  75. package/dist/exporter-DVG68ozH.mjs +0 -263
  76. package/dist/federation/audience.test.d.mts +0 -2
  77. package/dist/federation/audience.test.mjs +0 -130
  78. package/dist/federation/builder.test.d.mts +0 -2
  79. package/dist/federation/builder.test.mjs +0 -396
  80. package/dist/federation/circuit-breaker.test.d.mts +0 -2
  81. package/dist/federation/circuit-breaker.test.mjs +0 -1277
  82. package/dist/federation/collection.test.d.mts +0 -2
  83. package/dist/federation/collection.test.mjs +0 -21
  84. package/dist/federation/compound-fanout.test.d.mts +0 -2
  85. package/dist/federation/compound-fanout.test.mjs +0 -231
  86. package/dist/federation/handler-compound-proof.test.d.mts +0 -2
  87. package/dist/federation/handler-compound-proof.test.mjs +0 -384
  88. package/dist/federation/handler.test.d.mts +0 -2
  89. package/dist/federation/handler.test.mjs +0 -5445
  90. package/dist/federation/hashlink-media.test.d.mts +0 -2
  91. package/dist/federation/hashlink-media.test.mjs +0 -422
  92. package/dist/federation/idempotency.test.d.mts +0 -2
  93. package/dist/federation/idempotency.test.mjs +0 -207
  94. package/dist/federation/inbox-actorless.test.d.mts +0 -2
  95. package/dist/federation/inbox-actorless.test.mjs +0 -522
  96. package/dist/federation/inbox-authentication.test.d.mts +0 -2
  97. package/dist/federation/inbox-authentication.test.mjs +0 -220
  98. package/dist/federation/inbox-report.test.d.mts +0 -2
  99. package/dist/federation/inbox-report.test.mjs +0 -719
  100. package/dist/federation/inbox-view.test.d.mts +0 -2
  101. package/dist/federation/inbox-view.test.mjs +0 -369
  102. package/dist/federation/inbox.test.d.mts +0 -2
  103. package/dist/federation/inbox.test.mjs +0 -139
  104. package/dist/federation/keycache.test.d.mts +0 -2
  105. package/dist/federation/keycache.test.mjs +0 -337
  106. package/dist/federation/kv.test.d.mts +0 -2
  107. package/dist/federation/kv.test.mjs +0 -88
  108. package/dist/federation/metrics.test.d.mts +0 -2
  109. package/dist/federation/metrics.test.mjs +0 -634
  110. package/dist/federation/middleware.test.d.mts +0 -2
  111. package/dist/federation/middleware.test.mjs +0 -9314
  112. package/dist/federation/mq.test.d.mts +0 -2
  113. package/dist/federation/mq.test.mjs +0 -591
  114. package/dist/federation/negotiation.test.d.mts +0 -2
  115. package/dist/federation/negotiation.test.mjs +0 -18
  116. package/dist/federation/outbound-body.test.d.mts +0 -2
  117. package/dist/federation/outbound-body.test.mjs +0 -149
  118. package/dist/federation/outbound-capture.test.d.mts +0 -2
  119. package/dist/federation/outbound-capture.test.mjs +0 -394
  120. package/dist/federation/outbound-dispatch.test.d.mts +0 -2
  121. package/dist/federation/outbound-dispatch.test.mjs +0 -368
  122. package/dist/federation/outbound-document.test.d.mts +0 -2
  123. package/dist/federation/outbound-document.test.mjs +0 -171
  124. package/dist/federation/outbound-provenance.test.d.mts +0 -2
  125. package/dist/federation/outbound-provenance.test.mjs +0 -543
  126. package/dist/federation/outbound-report.test.d.mts +0 -2
  127. package/dist/federation/outbound-report.test.mjs +0 -5475
  128. package/dist/federation/outbound-workers.test.d.mts +0 -2
  129. package/dist/federation/outbound-workers.test.mjs +0 -64
  130. package/dist/federation/outbound-workflow.test.d.mts +0 -2
  131. package/dist/federation/outbound-workflow.test.mjs +0 -154
  132. package/dist/federation/outgoing-proof.test.d.mts +0 -2
  133. package/dist/federation/outgoing-proof.test.mjs +0 -933
  134. package/dist/federation/portable-actor.test.d.mts +0 -2
  135. package/dist/federation/portable-actor.test.mjs +0 -496
  136. package/dist/federation/portable-chain.test.d.mts +0 -2
  137. package/dist/federation/portable-chain.test.mjs +0 -370
  138. package/dist/federation/portable-collection.test.d.mts +0 -2
  139. package/dist/federation/portable-collection.test.mjs +0 -832
  140. package/dist/federation/portable-delivery.test.d.mts +0 -2
  141. package/dist/federation/portable-delivery.test.mjs +0 -845
  142. package/dist/federation/portable-gateway-keys.test.d.mts +0 -2
  143. package/dist/federation/portable-gateway-keys.test.mjs +0 -692
  144. package/dist/federation/portable-inbox.test.d.mts +0 -2
  145. package/dist/federation/portable-inbox.test.mjs +0 -1960
  146. package/dist/federation/portable-parse-uri.test.d.mts +0 -2
  147. package/dist/federation/portable-parse-uri.test.mjs +0 -348
  148. package/dist/federation/portable-route-activity.test.d.mts +0 -2
  149. package/dist/federation/portable-route-activity.test.mjs +0 -259
  150. package/dist/federation/portable.test.d.mts +0 -2
  151. package/dist/federation/portable.test.mjs +0 -1072
  152. package/dist/federation/retry.test.d.mts +0 -2
  153. package/dist/federation/retry.test.mjs +0 -42
  154. package/dist/federation/router.test.d.mts +0 -2
  155. package/dist/federation/router.test.mjs +0 -123
  156. package/dist/federation/send.test.d.mts +0 -2
  157. package/dist/federation/send.test.mjs +0 -1421
  158. package/dist/federation/tasks/codec.test.d.mts +0 -2
  159. package/dist/federation/tasks/codec.test.mjs +0 -302
  160. package/dist/federation/tasks/enqueue.test.d.mts +0 -2
  161. package/dist/federation/tasks/enqueue.test.mjs +0 -1095
  162. package/dist/federation/tasks/tasks.test.d.mts +0 -2
  163. package/dist/federation/tasks/tasks.test.mjs +0 -1229
  164. package/dist/federation/temporal.test.d.mts +0 -2
  165. package/dist/federation/temporal.test.mjs +0 -71
  166. package/dist/federation/webfinger.test.d.mts +0 -2
  167. package/dist/federation/webfinger.test.mjs +0 -944
  168. package/dist/http-l1zKGCll.mjs +0 -1449
  169. package/dist/inbox-CWX3EBTO.mjs +0 -399
  170. package/dist/inbox-authentication-CgonVRkg.mjs +0 -198
  171. package/dist/inbox-view-DkPCF71K.mjs +0 -385
  172. package/dist/key-owner-evidence-APB6NpSk.mjs +0 -108
  173. package/dist/keycache-N2opu4-3.mjs +0 -221
  174. package/dist/keys-DIJNHW-Y.mjs +0 -120
  175. package/dist/kv-DBRWVx3Q.mjs +0 -104
  176. package/dist/kv-DCHkaJ9-.mjs +0 -92
  177. package/dist/kv-cache-8eJh4Xum.mjs +0 -121
  178. package/dist/ld-Cdt8qD-r.mjs +0 -1752
  179. package/dist/map-local-context-conflict-DOYEEJ3u.mjs +0 -204
  180. package/dist/map-local-create-note-B0SeXJms.mjs +0 -214
  181. package/dist/metrics-BnfvSPj9.mjs +0 -818
  182. package/dist/middleware-CwnXJze2.mjs +0 -5
  183. package/dist/middleware-D_W_LdJN.mjs +0 -9971
  184. package/dist/mq-eF96QxUT.mjs +0 -288
  185. package/dist/negotiation-BAKO9nih.mjs +0 -96
  186. package/dist/nodeinfo/client.test.d.mts +0 -2
  187. package/dist/nodeinfo/client.test.mjs +0 -1364
  188. package/dist/nodeinfo/handler.test.d.mts +0 -2
  189. package/dist/nodeinfo/handler.test.mjs +0 -94
  190. package/dist/nodeinfo/types.test.d.mts +0 -2
  191. package/dist/nodeinfo/types.test.mjs +0 -192
  192. package/dist/otel/exporter.test.d.mts +0 -2
  193. package/dist/otel/exporter.test.mjs +0 -887
  194. package/dist/outbound-body-CckPA5gc.mjs +0 -83
  195. package/dist/outbound-capture-DVgnRzlf.mjs +0 -184
  196. package/dist/outbound-document-BMgwLXTY.mjs +0 -179
  197. package/dist/outgoing-jsonld-4IJqPYos.mjs +0 -205
  198. package/dist/owner-DukEQnv3.mjs +0 -267
  199. package/dist/portable-collection-OHaqa11n.mjs +0 -418
  200. package/dist/portable-key-BkOLJxVz.mjs +0 -549
  201. package/dist/portable-key-id-Ca30-hOa.mjs +0 -248
  202. package/dist/proof-Y7omSVvc.mjs +0 -1420
  203. package/dist/public-audience--klBIOk1.mjs +0 -197
  204. package/dist/retry-zX5xw5e0.mjs +0 -34
  205. package/dist/router-u5O5KCQr.mjs +0 -156
  206. package/dist/secured-document-CxzZZ9nL.mjs +0 -35
  207. package/dist/send-ccQynabr.mjs +0 -710
  208. package/dist/sig/accept.test.d.mts +0 -2
  209. package/dist/sig/accept.test.mjs +0 -416
  210. package/dist/sig/compound-proof-discovery.test.d.mts +0 -2
  211. package/dist/sig/compound-proof-discovery.test.mjs +0 -331
  212. package/dist/sig/compound-proof-producer.test.d.mts +0 -2
  213. package/dist/sig/compound-proof-producer.test.mjs +0 -107
  214. package/dist/sig/compound-proof-verification.test.d.mts +0 -2
  215. package/dist/sig/compound-proof-verification.test.mjs +0 -1017
  216. package/dist/sig/compound-proof.test.d.mts +0 -2
  217. package/dist/sig/compound-proof.test.mjs +0 -411
  218. package/dist/sig/http.test.d.mts +0 -2
  219. package/dist/sig/http.test.mjs +0 -3328
  220. package/dist/sig/key-owner-evidence.test.d.mts +0 -2
  221. package/dist/sig/key-owner-evidence.test.mjs +0 -100
  222. package/dist/sig/key.test.d.mts +0 -2
  223. package/dist/sig/key.test.mjs +0 -1250
  224. package/dist/sig/ld.test.d.mts +0 -2
  225. package/dist/sig/ld.test.mjs +0 -1044
  226. package/dist/sig/mitra-receive-interop.test.d.mts +0 -2
  227. package/dist/sig/mitra-receive-interop.test.mjs +0 -260
  228. package/dist/sig/mitra-send-interop.test.d.mts +0 -2
  229. package/dist/sig/mitra-send-interop.test.mjs +0 -461
  230. package/dist/sig/owner.test.d.mts +0 -2
  231. package/dist/sig/owner.test.mjs +0 -202
  232. package/dist/sig/portable-collection.test.d.mts +0 -2
  233. package/dist/sig/portable-collection.test.mjs +0 -697
  234. package/dist/sig/portable-interop.test.d.mts +0 -2
  235. package/dist/sig/portable-interop.test.mjs +0 -156
  236. package/dist/sig/portable-key-id.test.d.mts +0 -2
  237. package/dist/sig/portable-key-id.test.mjs +0 -67
  238. package/dist/sig/portable-key.test.d.mts +0 -2
  239. package/dist/sig/portable-key.test.mjs +0 -1438
  240. package/dist/sig/proof.test.d.mts +0 -2
  241. package/dist/sig/proof.test.mjs +0 -2638
  242. package/dist/sig/tootik-request-interop.test.d.mts +0 -2
  243. package/dist/sig/tootik-request-interop.test.mjs +0 -190
  244. package/dist/sig/tootik-send-interop.test.d.mts +0 -2
  245. package/dist/sig/tootik-send-interop.test.mjs +0 -331
  246. package/dist/sig/verification.test.d.mts +0 -2
  247. package/dist/sig/verification.test.mjs +0 -310
  248. package/dist/std__assert-DvMDRPes.mjs +0 -8
  249. package/dist/tasks-BCUmQbbz.mjs +0 -77
  250. package/dist/temporal-f_K4lAVR.mjs +0 -95
  251. package/dist/testing/context.test.d.mts +0 -2
  252. package/dist/testing/context.test.mjs +0 -19
  253. package/dist/testing/kv.test.d.mts +0 -2
  254. package/dist/testing/kv.test.mjs +0 -101
  255. package/dist/testing/mod.d.mts +0 -6099
  256. package/dist/testing/mod.mjs +0 -9
  257. package/dist/testing-vrnD5Uaa.mjs +0 -8
  258. package/dist/types-DE_LV1JB.mjs +0 -46
  259. package/dist/utils/docloader.test.d.mts +0 -2
  260. package/dist/utils/docloader.test.mjs +0 -1076
  261. package/dist/utils/kv-cache.test.d.mts +0 -2
  262. package/dist/utils/kv-cache.test.mjs +0 -261
  263. package/dist/verification-C3954hQh.mjs +0 -5
  264. package/dist/verification-Dpfcw8ml.mjs +0 -105
  265. package/dist/vocab/cjs.test.d.mts +0 -2
  266. package/dist/vocab/cjs.test.mjs +0 -27
@@ -1,544 +0,0 @@
1
- import { Temporal } from "temporal-polyfill";
2
- import "urlpattern-polyfill";
3
- globalThis.addEventListener = () => {};
4
- import { getLogger } from "@logtape/logtape";
5
- //#region src/federation/circuit-breaker.ts
6
- const LEGACY_SWEEP_MARKER = ["__fedify_meta", "circuit_breaker_state_ttl_sweep_v2"];
7
- const LEGACY_SWEEP_DELETING_MARKER = { __fedifyDeletingCircuitBreakerLegacyState: true };
8
- const CIRCUIT_BREAKER_STATE_VERSION = 1;
9
- const LEGACY_SWEEP_LOCK_TTL = Temporal.Duration.from({ minutes: 5 });
10
- const LEGACY_SWEEP_RENEW_INTERVAL = Temporal.Duration.from({ minutes: 1 });
11
- const LEGACY_SWEEP_RETRY_WINDOW = Temporal.Duration.from({ hours: 168 });
12
- const LEGACY_SWEEP_WAIT_INTERVAL = 10;
13
- const LEGACY_SWEEP_CAS_ATTEMPTS = 3;
14
- /**
15
- * Tracks reachability state for remote outbox delivery hosts.
16
- * @since 2.3.0
17
- */
18
- var CircuitBreaker = class {
19
- #kv;
20
- #prefix;
21
- #options;
22
- #now;
23
- #legacySweep;
24
- #legacySweepDone;
25
- #stateChangeObserver;
26
- constructor(options) {
27
- this.#kv = options.kv;
28
- this.#prefix = options.prefix;
29
- this.#options = normalizeCircuitBreakerOptions(options.options ?? {});
30
- this.#now = options.now ?? (() => Temporal.Now.instant());
31
- this.#stateChangeObserver = options.stateChangeObserver;
32
- }
33
- get options() {
34
- return this.#options;
35
- }
36
- capHeldDelay(heldSince, delay) {
37
- const now = this.#now();
38
- return now.until(this.#capHeldRetryAt(now, heldSince, now.add(delay)));
39
- }
40
- async beforeSend(remoteHost, message) {
41
- this.#sweepLegacyStates();
42
- const heldSince = parseHeldSince(message.circuitHeldSince);
43
- const now = this.#now();
44
- if (heldSince != null && Temporal.Instant.compare(heldSince.add(this.#options.heldActivityTtl), now) <= 0) return {
45
- type: "drop",
46
- heldSince
47
- };
48
- let lastConflictingState;
49
- for (let attempt = 0; attempt < 10; attempt++) {
50
- const { state: oldState, raw: oldValue } = await this.#get(remoteHost);
51
- if (oldState == null || oldState.state === "closed") return {
52
- type: "send",
53
- probe: false
54
- };
55
- if (oldState.state === "half-open") {
56
- const halfOpened = oldState.halfOpened == null ? void 0 : Temporal.Instant.from(oldState.halfOpened);
57
- if (halfOpened != null) {
58
- const staleAt = halfOpened.add(this.#options.recoveryDelay);
59
- if (Temporal.Instant.compare(now, staleAt) < 0) {
60
- const releaseAt = now.add(this.#options.releaseInterval);
61
- const retryAt = Temporal.Instant.compare(releaseAt, staleAt) < 0 ? releaseAt : staleAt;
62
- const cappedRetryAt = this.#capHeldRetryAt(now, heldSince, retryAt);
63
- return {
64
- type: "hold",
65
- state: "half-open",
66
- delay: now.until(cappedRetryAt),
67
- heldSince: heldSince ?? now
68
- };
69
- }
70
- }
71
- const newState = {
72
- ...oldState,
73
- state: "half-open",
74
- halfOpened: now.toString()
75
- };
76
- if (await this.#replace(remoteHost, oldValue, newState)) return {
77
- type: "send",
78
- probe: true
79
- };
80
- lastConflictingState = "half-open";
81
- continue;
82
- }
83
- const probeAt = (oldState.opened == null ? now : Temporal.Instant.from(oldState.opened)).add(this.#options.recoveryDelay);
84
- if (Temporal.Instant.compare(now, probeAt) < 0) {
85
- const retryAt = this.#capHeldRetryAt(now, heldSince, probeAt);
86
- return {
87
- type: "hold",
88
- state: "open",
89
- delay: now.until(retryAt),
90
- heldSince: heldSince ?? now
91
- };
92
- }
93
- const newState = {
94
- ...oldState,
95
- state: "half-open",
96
- halfOpened: now.toString()
97
- };
98
- if (await this.#replace(remoteHost, oldValue, newState)) {
99
- await this.#notifyStateChange(remoteHost, "open", "half-open");
100
- return {
101
- type: "send",
102
- probe: true,
103
- stateChange: {
104
- previousState: "open",
105
- newState: "half-open"
106
- }
107
- };
108
- }
109
- lastConflictingState = "open";
110
- }
111
- if (lastConflictingState != null) {
112
- const retryAt = this.#capHeldRetryAt(now, heldSince, now.add(this.#options.releaseInterval));
113
- return {
114
- type: "hold",
115
- state: lastConflictingState,
116
- delay: now.until(retryAt),
117
- heldSince: heldSince ?? now
118
- };
119
- }
120
- throw new Error(`Failed to update circuit breaker state for ${remoteHost}`);
121
- }
122
- async recordSuccess(remoteHost) {
123
- this.#sweepLegacyStates();
124
- for (let attempt = 0; attempt < 10; attempt++) {
125
- const { state: oldState, raw: oldValue } = await this.#get(remoteHost);
126
- if (oldState == null) return void 0;
127
- if (await this.#replace(remoteHost, oldValue, void 0)) {
128
- if (oldState.state !== "closed") {
129
- await this.#notifyStateChange(remoteHost, oldState.state, "closed");
130
- return {
131
- previousState: oldState.state,
132
- newState: "closed"
133
- };
134
- }
135
- return;
136
- }
137
- }
138
- throw new Error(`Failed to update circuit breaker state for ${remoteHost}`);
139
- }
140
- async recordReachableFailure(remoteHost) {
141
- return await this.recordSuccess(remoteHost);
142
- }
143
- async recordFailure(remoteHost) {
144
- this.#sweepLegacyStates();
145
- const now = this.#now();
146
- for (let attempt = 0; attempt < 10; attempt++) {
147
- const { state: oldState, raw: oldValue } = await this.#get(remoteHost);
148
- if (oldState?.state === "open") return void 0;
149
- const oldFailures = oldState?.failures.map(Temporal.Instant.from) ?? [];
150
- const failures = this.#options.pruneFailures([...oldFailures, now], now);
151
- let newState;
152
- let transition;
153
- if (oldState?.state === "half-open" || this.#options.failure(failures)) {
154
- newState = {
155
- state: "open",
156
- failures: failures.map((t) => t.toString()),
157
- opened: now.toString()
158
- };
159
- transition = [oldState?.state ?? "closed", "open"];
160
- } else newState = {
161
- state: "closed",
162
- failures: failures.map((t) => t.toString())
163
- };
164
- if (await this.#replace(remoteHost, oldValue, newState)) {
165
- if (transition != null) {
166
- await this.#notifyStateChange(remoteHost, transition[0], transition[1]);
167
- return {
168
- previousState: transition[0],
169
- newState: transition[1]
170
- };
171
- }
172
- return;
173
- }
174
- }
175
- throw new Error(`Failed to update circuit breaker state for ${remoteHost}`);
176
- }
177
- async dropActivity(remoteHost, details) {
178
- try {
179
- await this.#options.onActivityDrop?.(remoteHost, details);
180
- } catch (error) {
181
- getLogger([
182
- "fedify",
183
- "federation",
184
- "circuit"
185
- ]).error("An unexpected error occurred in circuit breaker activity drop handler:\n{error}", {
186
- remoteHost,
187
- error
188
- });
189
- }
190
- }
191
- async getState(remoteHost) {
192
- this.#sweepLegacyStates();
193
- return stripStoredCircuitBreakerState((await this.#get(remoteHost)).state);
194
- }
195
- /**
196
- * The currently running background legacy sweep, if any.
197
- * @internal
198
- */
199
- get pendingSweep() {
200
- return this.#legacySweep;
201
- }
202
- #key(remoteHost) {
203
- return [...this.#prefix, remoteHost];
204
- }
205
- #legacySweepMarkerKey() {
206
- return [...this.#prefix, ...LEGACY_SWEEP_MARKER];
207
- }
208
- #sweepLegacyStates() {
209
- if (this.#kv.cas == null) return;
210
- if (this.#options.stateTtl == null) return;
211
- if (this.#legacySweep != null) return;
212
- if (this.#isLegacySweepDone()) return;
213
- this.#legacySweep = this.#sweepLegacyStatesImpl().catch((error) => {
214
- getLogger([
215
- "fedify",
216
- "federation",
217
- "circuit"
218
- ]).warn("Failed to sweep legacy circuit breaker state:\n{error}", { error });
219
- }).finally(() => {
220
- this.#legacySweep = void 0;
221
- });
222
- }
223
- async #sweepLegacyStatesImpl() {
224
- const markerKey = this.#legacySweepMarkerKey();
225
- const acquired = await this.#acquireLegacySweep(markerKey);
226
- if (acquired === "done") return;
227
- let marker = acquired;
228
- let leaseRenewedAt = this.#now();
229
- try {
230
- for await (const { key, value } of this.#kv.list(this.#prefix)) {
231
- if (key.length !== this.#prefix.length + 1) continue;
232
- await this.#migrateLegacyState(key, value);
233
- if (Temporal.Instant.compare(this.#now(), leaseRenewedAt.add(LEGACY_SWEEP_RENEW_INTERVAL)) >= 0) {
234
- const renewed = await this.#renewLegacySweep(markerKey, marker);
235
- if (renewed == null) return;
236
- marker = renewed;
237
- leaseRenewedAt = this.#now();
238
- }
239
- }
240
- } catch (error) {
241
- await this.#deleteIfUnchanged(markerKey, marker);
242
- throw error;
243
- }
244
- const finishedMarker = this.#finishLegacySweepMarker(marker);
245
- if (await this.#kv.cas(markerKey, marker, finishedMarker)) this.#rememberLegacySweepMarker(finishedMarker);
246
- }
247
- async #renewLegacySweep(markerKey, marker) {
248
- const renewed = {
249
- state: "sweeping",
250
- started: this.#now().toString(),
251
- retryUntil: getLegacySweepRetryUntil(marker) ?? this.#now().add(LEGACY_SWEEP_RETRY_WINDOW).toString()
252
- };
253
- if (await this.#kv.cas(markerKey, marker, renewed, { ttl: LEGACY_SWEEP_LOCK_TTL })) return renewed;
254
- }
255
- #finishLegacySweepMarker(marker) {
256
- const retryUntil = getLegacySweepRetryUntil(marker);
257
- if (retryUntil != null && Temporal.Instant.compare(this.#now(), Temporal.Instant.from(retryUntil)) < 0) return {
258
- state: "done",
259
- retryUntil
260
- };
261
- return { state: "final" };
262
- }
263
- async #migrateLegacyState(key, value) {
264
- if (isCurrentCircuitBreakerState(value)) {
265
- await this.#kv.cas(key, value, value, this.#setOptions());
266
- return;
267
- }
268
- const state = parseCircuitBreakerKvState(value);
269
- if (state != null) {
270
- await this.#kv.cas(key, value, markCircuitBreakerState(state), this.#setOptions());
271
- return;
272
- }
273
- await this.#deleteIfUnchanged(key, value);
274
- }
275
- async #deleteIfUnchanged(key, value) {
276
- if (await this.#kv.cas(key, value, LEGACY_SWEEP_DELETING_MARKER, { ttl: LEGACY_SWEEP_LOCK_TTL })) await this.#kv.delete(key);
277
- }
278
- async #acquireLegacySweep(markerKey) {
279
- for (let attempt = 0; attempt < LEGACY_SWEEP_CAS_ATTEMPTS; attempt++) {
280
- const marker = await this.#kv.get(markerKey);
281
- const now = this.#now();
282
- if (isLegacySweepActive(marker, now)) {
283
- this.#rememberLegacySweepMarker(marker);
284
- return "done";
285
- }
286
- const retryUntil = isLegacySweepRetryDue(marker, now) ? marker.retryUntil : getLegacySweepRetryUntil(marker) ?? now.add(LEGACY_SWEEP_RETRY_WINDOW).toString();
287
- const sweeping = {
288
- state: "sweeping",
289
- started: now.toString(),
290
- retryUntil
291
- };
292
- if (await this.#kv.cas(markerKey, marker ?? void 0, sweeping, { ttl: LEGACY_SWEEP_LOCK_TTL })) return sweeping;
293
- await delay(LEGACY_SWEEP_WAIT_INTERVAL);
294
- }
295
- return "done";
296
- }
297
- #isLegacySweepDone() {
298
- if (this.#legacySweepDone === "final") return true;
299
- if (this.#legacySweepDone == null) return false;
300
- if (Temporal.Instant.compare(this.#now(), this.#legacySweepDone) < 0) return true;
301
- this.#legacySweepDone = void 0;
302
- return false;
303
- }
304
- #rememberLegacySweepMarker(marker) {
305
- const doneUntil = getLegacySweepDoneUntil(marker, this.#now());
306
- if (doneUntil != null) this.#legacySweepDone = doneUntil;
307
- }
308
- #capHeldRetryAt(now, heldSince, retryAt) {
309
- const expiresAt = (heldSince ?? now).add(this.#options.heldActivityTtl);
310
- return Temporal.Instant.compare(expiresAt, retryAt) < 0 ? expiresAt : retryAt;
311
- }
312
- async #get(remoteHost) {
313
- const value = await this.#kv.get(this.#key(remoteHost));
314
- const state = parseStoredCircuitBreakerKvState(value);
315
- return {
316
- state,
317
- raw: state == null ? void 0 : value
318
- };
319
- }
320
- async #replace(remoteHost, oldValue, newState) {
321
- const key = this.#key(remoteHost);
322
- const storedState = newState == null ? void 0 : markCircuitBreakerState(newState);
323
- if (this.#kv.cas == null) {
324
- if (storedState == null) await this.#kv.delete(key);
325
- else await this.#kv.set(key, storedState, this.#setOptions());
326
- return true;
327
- }
328
- return await this.#kv.cas(key, oldValue, storedState, storedState == null ? void 0 : this.#setOptions());
329
- }
330
- #setOptions() {
331
- return this.#options.stateTtl == null ? void 0 : { ttl: this.#options.stateTtl };
332
- }
333
- async #notifyStateChange(remoteHost, previousState, newState) {
334
- try {
335
- await this.#options.onStateChange?.(remoteHost, previousState, newState);
336
- } catch (error) {
337
- getLogger([
338
- "fedify",
339
- "federation",
340
- "circuit"
341
- ]).error("An unexpected error occurred in circuit breaker state change handler:\n{error}", {
342
- remoteHost,
343
- previousState,
344
- newState,
345
- error
346
- });
347
- }
348
- try {
349
- await this.#stateChangeObserver?.(remoteHost, previousState, newState);
350
- } catch (error) {
351
- getLogger([
352
- "fedify",
353
- "federation",
354
- "circuit"
355
- ]).error("An unexpected error occurred in circuit breaker state change observer:\n{error}", {
356
- remoteHost,
357
- previousState,
358
- newState,
359
- error
360
- });
361
- }
362
- }
363
- };
364
- /**
365
- * Normalizes user-provided circuit breaker options into the internal policy
366
- * shape used while processing queued outbox deliveries.
367
- *
368
- * @param options The public circuit breaker options supplied to Fedify.
369
- * @returns The normalized failure predicate, failure pruning function,
370
- * duration values, and optional callbacks with defaults applied.
371
- * @throws {RangeError} If any configured duration is not positive, or if
372
- * `stateTtl` is shorter than `recoveryDelay`.
373
- * @throws {TypeError} If `failureThreshold` is not a positive integer.
374
- */
375
- function normalizeCircuitBreakerOptions(options) {
376
- const recoveryDelay = toInstantDuration(options.recoveryDelay ?? { minutes: 30 });
377
- const heldActivityTtl = toInstantDuration(options.heldActivityTtl ?? { hours: 168 });
378
- const releaseInterval = toInstantDuration(options.releaseInterval ?? { seconds: 1 });
379
- const configuredStateTtl = options.stateTtl == null ? void 0 : toInstantDuration(options.stateTtl);
380
- assertPositiveDuration(recoveryDelay, "recoveryDelay");
381
- assertPositiveDuration(heldActivityTtl, "heldActivityTtl");
382
- assertPositiveDuration(releaseInterval, "releaseInterval");
383
- if (configuredStateTtl != null) {
384
- assertPositiveDuration(configuredStateTtl, "stateTtl");
385
- if (Temporal.Duration.compare(configuredStateTtl, recoveryDelay) < 0) throw new RangeError("stateTtl must be at least recoveryDelay.");
386
- }
387
- let failure;
388
- let pruneFailures;
389
- let stateTtl;
390
- if (options.failure == null) {
391
- const failureThreshold = options.failureThreshold ?? 5;
392
- if (!Number.isInteger(failureThreshold) || failureThreshold <= 0) throw new TypeError("failureThreshold must be a positive integer.");
393
- const failureWindow = toInstantDuration(options.failureWindow ?? { minutes: 10 });
394
- assertPositiveDuration(failureWindow, "failureWindow");
395
- pruneFailures = (timestamps, now) => {
396
- const earliest = now.subtract(failureWindow);
397
- return timestamps.filter((timestamp) => Temporal.Instant.compare(timestamp, earliest) >= 0).slice(-failureThreshold);
398
- };
399
- failure = (timestamps) => {
400
- if (timestamps.length < failureThreshold) return false;
401
- const first = timestamps[timestamps.length - failureThreshold];
402
- const last = timestamps[timestamps.length - 1];
403
- return Temporal.Duration.compare(first.until(last), failureWindow) <= 0;
404
- };
405
- stateTtl = configuredStateTtl ?? maxDuration(recoveryDelay.add(failureWindow), recoveryDelay.add(heldActivityTtl));
406
- } else {
407
- failure = options.failure;
408
- pruneFailures = (timestamps) => timestamps.slice(-100);
409
- stateTtl = configuredStateTtl ?? recoveryDelay.add(heldActivityTtl);
410
- }
411
- return {
412
- failure,
413
- pruneFailures,
414
- recoveryDelay,
415
- heldActivityTtl,
416
- releaseInterval,
417
- stateTtl,
418
- onStateChange: options.onStateChange,
419
- onActivityDrop: options.onActivityDrop
420
- };
421
- }
422
- function maxDuration(duration, ...durations) {
423
- return durations.reduce((max, candidate) => Temporal.Duration.compare(candidate, max) > 0 ? candidate : max, duration);
424
- }
425
- function isLegacySweepDone(value, now) {
426
- return getLegacySweepDoneUntil(value, now) != null;
427
- }
428
- function getLegacySweepDoneUntil(value, now) {
429
- if (typeof value !== "object" || value == null || !("state" in value)) return;
430
- if (value.state === "final") return "final";
431
- if (value.state === "done" && "retryUntil" in value && typeof value.retryUntil === "string") try {
432
- const retryUntil = Temporal.Instant.from(value.retryUntil);
433
- return Temporal.Instant.compare(now, retryUntil) < 0 ? retryUntil : void 0;
434
- } catch {
435
- return;
436
- }
437
- }
438
- function isLegacySweepRetryDue(value, now) {
439
- if (typeof value !== "object" || value == null || !("state" in value) || value.state !== "done" || !("retryUntil" in value) || typeof value.retryUntil !== "string") return false;
440
- try {
441
- return Temporal.Instant.compare(now, Temporal.Instant.from(value.retryUntil)) >= 0;
442
- } catch {
443
- return false;
444
- }
445
- }
446
- function isLegacySweepActive(value, now) {
447
- return isLegacySweepDone(value, now) || isLegacySweepInProgress(value, now);
448
- }
449
- function isLegacySweepInProgress(value, now) {
450
- if (typeof value !== "object" || value == null || !("state" in value) || value.state !== "sweeping" || !("started" in value) || typeof value.started !== "string") return false;
451
- try {
452
- return Temporal.Instant.compare(now, Temporal.Instant.from(value.started).add(LEGACY_SWEEP_LOCK_TTL)) < 0;
453
- } catch {
454
- return false;
455
- }
456
- }
457
- function getLegacySweepRetryUntil(value) {
458
- if (typeof value !== "object" || value == null || !("retryUntil" in value) || typeof value.retryUntil !== "string") return;
459
- try {
460
- Temporal.Instant.from(value.retryUntil);
461
- return value.retryUntil;
462
- } catch {
463
- return;
464
- }
465
- }
466
- function isCurrentCircuitBreakerState(value) {
467
- return typeof value === "object" && value != null && "__fedifyCircuitBreakerStateVersion" in value && value.__fedifyCircuitBreakerStateVersion === CIRCUIT_BREAKER_STATE_VERSION;
468
- }
469
- function markCircuitBreakerState(state) {
470
- return {
471
- ...state,
472
- __fedifyCircuitBreakerStateVersion: CIRCUIT_BREAKER_STATE_VERSION
473
- };
474
- }
475
- function stripStoredCircuitBreakerState(state) {
476
- if (state == null) return void 0;
477
- const { __fedifyCircuitBreakerStateVersion: _, ...publicState } = state;
478
- return publicState;
479
- }
480
- function delay(milliseconds) {
481
- return new Promise((resolve) => setTimeout(resolve, milliseconds));
482
- }
483
- function toInstantDuration(duration) {
484
- const parsed = Temporal.Duration.from(duration);
485
- return Temporal.Duration.from({ milliseconds: Math.trunc(parsed.total({
486
- unit: "millisecond",
487
- relativeTo: Temporal.PlainDateTime.from("2026-01-01T00:00:00")
488
- })) });
489
- }
490
- function assertPositiveDuration(duration, name) {
491
- if (Temporal.Duration.compare(duration, { seconds: 0 }) <= 0) throw new RangeError(`${name} must be a positive duration.`);
492
- }
493
- function parseHeldSince(value) {
494
- if (value == null) return void 0;
495
- try {
496
- return Temporal.Instant.from(value);
497
- } catch (error) {
498
- getLogger([
499
- "fedify",
500
- "federation",
501
- "circuit"
502
- ]).warn("Invalid circuitHeldSince value in queued outbox message: {value}", {
503
- value,
504
- error
505
- });
506
- return;
507
- }
508
- }
509
- /**
510
- * Parses a value loaded from the circuit breaker KV store.
511
- *
512
- * @param value The raw KV value to validate.
513
- * @returns A circuit breaker state when `value` has a recognized state and
514
- * valid instant strings, or `undefined` when the stored value is malformed.
515
- */
516
- function parseCircuitBreakerKvState(value) {
517
- return stripStoredCircuitBreakerState(parseStoredCircuitBreakerKvState(value));
518
- }
519
- function parseStoredCircuitBreakerKvState(value) {
520
- const isInstantString = (v) => {
521
- if (typeof v !== "string") return false;
522
- try {
523
- Temporal.Instant.from(v);
524
- return true;
525
- } catch {
526
- return false;
527
- }
528
- };
529
- if (typeof value !== "object" || value == null) return void 0;
530
- const record = value;
531
- if (record.state !== "closed" && record.state !== "open" && record.state !== "half-open") return;
532
- if (!Array.isArray(record.failures) || !record.failures.every((failure) => isInstantString(failure))) return;
533
- if (record.opened != null && !isInstantString(record.opened)) return;
534
- if (record.halfOpened != null && !isInstantString(record.halfOpened)) return;
535
- return {
536
- state: record.state,
537
- failures: record.failures,
538
- ...record.opened == null ? {} : { opened: record.opened },
539
- ...record.halfOpened == null ? {} : { halfOpened: record.halfOpened },
540
- ...record.__fedifyCircuitBreakerStateVersion === CIRCUIT_BREAKER_STATE_VERSION ? { __fedifyCircuitBreakerStateVersion: CIRCUIT_BREAKER_STATE_VERSION } : {}
541
- };
542
- }
543
- //#endregion
544
- export { normalizeCircuitBreakerOptions as n, parseCircuitBreakerKvState as r, CircuitBreaker as t };
@@ -1,14 +0,0 @@
1
- import "temporal-polyfill";
2
- import "urlpattern-polyfill";
3
- globalThis.addEventListener = () => {};
4
- //#region src/federation/circuit-breaker-test-utils.ts
5
- function markCircuitBreakerLegacySweepDone(kv) {
6
- return kv.set([
7
- "_fedify",
8
- "circuit",
9
- "__fedify_meta",
10
- "circuit_breaker_state_ttl_sweep_v2"
11
- ], { state: "final" });
12
- }
13
- //#endregion
14
- export { markCircuitBreakerLegacySweepDone as t };