@fedify/fedify 2.1.31 → 2.1.32

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 (97) hide show
  1. package/dist/attachment-arrays-DZ4JHJD8.mjs +111 -0
  2. package/dist/{builder-isMRh_I6.mjs → builder--sc-rozj.mjs} +3 -3
  3. package/dist/callback-BARPujV7.d.mts +2388 -0
  4. package/dist/{client-BIBzqEIp.d.cts → client-9DPcy_Yx.d.ts} +16 -0
  5. package/dist/{client-jE7oGvRh.d.ts → client-B3ruDgWg.d.cts} +16 -0
  6. package/dist/{client-BZr7TGyu.mjs → client-cpPaW7Ww.mjs} +86 -11
  7. package/dist/compat/attachment-arrays.test.d.mts +2 -0
  8. package/dist/compat/attachment-arrays.test.mjs +254 -0
  9. package/dist/compat/mod.d.cts +1 -1
  10. package/dist/compat/mod.d.ts +1 -1
  11. package/dist/compat/transformers.test.mjs +1 -1
  12. package/dist/{context-C0uGVzgk.d.ts → context-DE2YrQwM.d.ts} +30 -13
  13. package/dist/{context-CLaLxi49.d.cts → context-y6ZUQCMf.d.cts} +30 -13
  14. package/dist/{deno-CiaJqTcu.mjs → deno-L23b3Hba.mjs} +1 -1
  15. package/dist/{docloader-DOrwedT7.mjs → docloader-DUXr9k_O.mjs} +2 -2
  16. package/dist/federation/attachment-responses.test.d.mts +2 -0
  17. package/dist/federation/attachment-responses.test.mjs +182 -0
  18. package/dist/federation/builder.test.mjs +1 -1
  19. package/dist/federation/collection.test.mjs +1 -1
  20. package/dist/federation/delivery-attachments.test.d.mts +2 -0
  21. package/dist/federation/delivery-attachments.test.mjs +280 -0
  22. package/dist/federation/handler.test.mjs +208 -16
  23. package/dist/federation/idempotency.test.mjs +2 -2
  24. package/dist/federation/inbox.test.mjs +1 -1
  25. package/dist/federation/middleware.test.mjs +1185 -8
  26. package/dist/federation/mod.cjs +1 -1
  27. package/dist/federation/mod.d.cts +2 -2
  28. package/dist/federation/mod.d.ts +2 -2
  29. package/dist/federation/mod.js +1 -1
  30. package/dist/federation/negotiation.test.mjs +1 -1
  31. package/dist/federation/retry.test.mjs +1 -1
  32. package/dist/federation/send.test.mjs +283 -3
  33. package/dist/federation/temporal.test.mjs +25 -1
  34. package/dist/federation/webfinger.test.mjs +1 -1
  35. package/dist/{http-Crml3gei.js → http-BtbMAgTA.js} +6 -4
  36. package/dist/{http-9IFUxdkp.d.cts → http-C8qLHs_Z.d.cts} +3 -3
  37. package/dist/{http-CRiVlZd5.cjs → http-D5woFKA_.cjs} +6 -4
  38. package/dist/{http-SbfezERn.d.ts → http-D8WmcRAF.d.ts} +3 -3
  39. package/dist/{http-A9mP2uM7.mjs → http-HX6wVyJh.mjs} +6 -4
  40. package/dist/{inbox-KESLudq4.mjs → inbox-CL7A0HXT.mjs} +1 -1
  41. package/dist/{key-Dg6-DV_O.mjs → key-Dai3f5yI.mjs} +3 -2
  42. package/dist/{kv-cache-Cqmm-yQa.cjs → kv-cache-B0V_e231.cjs} +1 -1
  43. package/dist/{kv-cache-DQcyZL5T.js → kv-cache-BsDg-cT8.js} +1 -1
  44. package/dist/{ld-COjDN6Nb.mjs → ld-DN6x1iZ8.mjs} +153 -21
  45. package/dist/ld-signature-DoZcbsZR.mjs +23 -0
  46. package/dist/{middleware-BtDQUCcz.cjs → middleware-Cl6k5Hrm.cjs} +1 -1
  47. package/dist/{middleware-BbgR7xOI.cjs → middleware-CrfRuKCf.cjs} +561 -101
  48. package/dist/{middleware-COFlKeYn.mjs → middleware-DiquMgtt.mjs} +378 -89
  49. package/dist/{middleware-DIW5S_5L.js → middleware-DunNtbo9.js} +560 -101
  50. package/dist/{middleware-CuQvczZd.mjs → middleware-SnQS8eIb.mjs} +1 -1
  51. package/dist/{mod-LOEOKbYd.d.cts → mod-CehHJOV4.d.cts} +30 -13
  52. package/dist/{mod-CFRocFeQ.d.ts → mod-DLJpnqdw.d.ts} +1 -1
  53. package/dist/{mod-C7w-QVUv.d.ts → mod-Mh3oIuRK.d.ts} +30 -13
  54. package/dist/{mod-BRnwh2oy.d.ts → mod-REfljt0T.d.ts} +3 -3
  55. package/dist/{mod-Co7LEK9X.d.cts → mod-UvIGUHqW.d.cts} +3 -3
  56. package/dist/{mod-CGUH8Ujp.d.cts → mod-sTa1Zzxk.d.cts} +1 -1
  57. package/dist/mod.cjs +5 -5
  58. package/dist/mod.d.cts +8 -8
  59. package/dist/mod.d.ts +8 -8
  60. package/dist/mod.js +5 -5
  61. package/dist/nodeinfo/client.test.mjs +432 -5
  62. package/dist/nodeinfo/handler.test.mjs +1 -1
  63. package/dist/nodeinfo/mod.cjs +1 -1
  64. package/dist/nodeinfo/mod.d.cts +1 -1
  65. package/dist/nodeinfo/mod.d.ts +1 -1
  66. package/dist/nodeinfo/mod.js +1 -1
  67. package/dist/nodeinfo/types.test.mjs +1 -1
  68. package/dist/{owner-CFHq61cq.mjs → owner-DwyDorZI.mjs} +2 -2
  69. package/dist/{proof-0JkSC_pT.cjs → proof-Be-4K4jn.cjs} +193 -46
  70. package/dist/{proof-KSHlbPCG.mjs → proof-DYjmyrHh.mjs} +38 -13
  71. package/dist/{proof-DzksJFK7.js → proof-Du4-5SHY.js} +187 -46
  72. package/dist/{send-DaDe2YCb.mjs → send-BGSfQkIK.mjs} +92 -21
  73. package/dist/sig/http.test.mjs +2 -2
  74. package/dist/sig/key-types.test.d.mts +218 -0
  75. package/dist/sig/key-types.test.mjs +8 -0
  76. package/dist/sig/key.test.mjs +2 -2
  77. package/dist/sig/ld.test.mjs +573 -8
  78. package/dist/sig/mod.cjs +2 -2
  79. package/dist/sig/mod.d.cts +3 -3
  80. package/dist/sig/mod.d.ts +3 -3
  81. package/dist/sig/mod.js +2 -2
  82. package/dist/sig/owner.test.mjs +1 -1
  83. package/dist/sig/proof.test.mjs +95 -3
  84. package/dist/{temporal-DNGH8o2E.mjs → temporal-CNCchWly.mjs} +3 -5
  85. package/dist/testing/mod.d.mts +2 -2203
  86. package/dist/{types-DOvyymM4.cjs → types-B5wRhemi.cjs} +85 -10
  87. package/dist/{types-B1_pRFE_.js → types-CXy2lRVU.js} +86 -11
  88. package/dist/utils/docloader.test.mjs +2 -2
  89. package/dist/utils/mod.cjs +1 -1
  90. package/dist/utils/mod.d.cts +1 -1
  91. package/dist/utils/mod.d.ts +1 -1
  92. package/dist/utils/mod.js +1 -1
  93. package/package.json +6 -6
  94. /package/dist/{collection-BD6-SZ6O.mjs → collection-DnoYNwBC.mjs} +0 -0
  95. /package/dist/{negotiation-BehA2uul.mjs → negotiation-xb0QR3u_.mjs} +0 -0
  96. /package/dist/{retry-B_E3V_Dx.mjs → retry-CI1flGDD.mjs} +0 -0
  97. /package/dist/{types-DCP0WLdt.mjs → types-s4mCTGkT.mjs} +0 -0
@@ -9,18 +9,21 @@ import { r as assertFalse, t as assertThrows } from "../assert_throws-D4DNWrux.m
9
9
  import { n as assertInstanceOf, t as assert } from "../assert-BhQd8QHP.mjs";
10
10
  import { t as assertNotEquals } from "../assert_not_equals-BkxW5QJI.mjs";
11
11
  import { t as assertRejects } from "../assert_rejects-WyYSTDDX.mjs";
12
- import { c as signRequest, u as verifyRequest } from "../http-A9mP2uM7.mjs";
12
+ import { c as signRequest, u as verifyRequest } from "../http-HX6wVyJh.mjs";
13
13
  import { a as rsaPrivateKey3, c as rsaPublicKey3, i as rsaPrivateKey2, n as ed25519PrivateKey, r as ed25519PublicKey, s as rsaPublicKey2, t as ed25519Multikey } from "../keys-CtZLJq76.mjs";
14
- import { n as getAuthenticatedDocumentLoader } from "../docloader-DOrwedT7.mjs";
14
+ import { n as getAuthenticatedDocumentLoader } from "../docloader-DUXr9k_O.mjs";
15
15
  import { t as MemoryKvStore } from "../kv-HvthQAHr.mjs";
16
- import { a as compactJsonLd, f as signJsonLd, m as verifyJsonLd, s as detachSignature } from "../ld-COjDN6Nb.mjs";
17
- import { t as doesActorOwnKey } from "../owner-CFHq61cq.mjs";
18
- import { n as signObject, r as verifyObject } from "../proof-KSHlbPCG.mjs";
19
- import { a as createFederation, i as KvSpecDeterminer, n as FederationImpl, r as InboxContextImpl, t as ContextImpl } from "../middleware-COFlKeYn.mjs";
16
+ import { a as compactJsonLd, c as detachSignature, g as verifyJsonLd, i as attachSignature, m as signJsonLd } from "../ld-DN6x1iZ8.mjs";
17
+ import { t as ldSignatureWithoutCreated } from "../ld-signature-DoZcbsZR.mjs";
18
+ import { t as doesActorOwnKey } from "../owner-DwyDorZI.mjs";
19
+ import { n as signObject, r as verifyObject } from "../proof-DYjmyrHh.mjs";
20
+ import { a as createFederation, i as KvSpecDeterminer, n as FederationImpl, r as InboxContextImpl, t as ContextImpl } from "../middleware-DiquMgtt.mjs";
21
+ import { t as SendActivityError } from "../send-BGSfQkIK.mjs";
20
22
  import { createTestTracerProvider, mockDocumentLoader, test } from "@fedify/fixture";
21
23
  import * as vocab from "@fedify/vocab";
22
24
  import { Create, Offer, Person, getTypeId, lookupObject } from "@fedify/vocab";
23
25
  import { FetchError, UrlError, getDocumentLoader } from "@fedify/vocab-runtime";
26
+ import { configure, reset } from "@logtape/logtape";
24
27
  import dns from "node:dns/promises";
25
28
  //#region ../fixture/src/fixtures/example.com/create.json
26
29
  var id$2 = "https://example.com/create";
@@ -194,6 +197,104 @@ test("createFederation()", async (t) => {
194
197
  }), TypeError);
195
198
  });
196
199
  });
200
+ test("FederationImpl.startQueue()", async (t) => {
201
+ function createCountingQueue() {
202
+ const messages = [];
203
+ let listenCount = 0;
204
+ return {
205
+ messages,
206
+ get listenCount() {
207
+ return listenCount;
208
+ },
209
+ enqueue(message) {
210
+ messages.push(message);
211
+ return Promise.resolve();
212
+ },
213
+ listen() {
214
+ listenCount++;
215
+ return Promise.resolve();
216
+ }
217
+ };
218
+ }
219
+ await t.step("starts a fanout-only queue with an explicit selector", async () => {
220
+ const queue = createCountingQueue();
221
+ const federation = new FederationImpl({
222
+ kv: new MemoryKvStore(),
223
+ queue: { fanout: queue },
224
+ manuallyStartQueue: true
225
+ });
226
+ await federation.startQueue(void 0, { queue: "fanout" });
227
+ assertEquals(queue.listenCount, 1);
228
+ await federation.startQueue(void 0, { queue: "fanout" });
229
+ assertEquals(queue.listenCount, 1);
230
+ });
231
+ await t.step("starts a fanout-only queue without a selector", async () => {
232
+ const queue = createCountingQueue();
233
+ await new FederationImpl({
234
+ kv: new MemoryKvStore(),
235
+ queue: { fanout: queue },
236
+ manuallyStartQueue: true
237
+ }).startQueue(void 0);
238
+ assertEquals(queue.listenCount, 1);
239
+ });
240
+ await t.step("starts a fanout-only queue after a fanout enqueue", async () => {
241
+ const queue = createCountingQueue();
242
+ const ctx = new ContextImpl({
243
+ data: void 0,
244
+ federation: new FederationImpl({
245
+ kv: new MemoryKvStore(),
246
+ contextLoaderFactory: () => mockDocumentLoader,
247
+ queue: { fanout: queue }
248
+ }),
249
+ url: new URL("https://example.com/"),
250
+ documentLoader: mockDocumentLoader,
251
+ contextLoader: mockDocumentLoader
252
+ });
253
+ const activity = new vocab.Create({
254
+ id: new URL("https://example.com/activity/1"),
255
+ actor: new URL("https://example.com/person")
256
+ });
257
+ await ctx.sendActivity([{
258
+ privateKey: rsaPrivateKey2,
259
+ keyId: rsaPublicKey2.id
260
+ }], {
261
+ id: new URL("https://example.com/recipient"),
262
+ inboxId: new URL("https://example.com/inbox")
263
+ }, activity, { fanout: "force" });
264
+ assertEquals(queue.listenCount, 1);
265
+ assertEquals(queue.messages.length, 1);
266
+ assertEquals(queue.messages[0].type, "fanout");
267
+ });
268
+ await t.step("does not start a fanout-only queue automatically when manually started", async () => {
269
+ const queue = createCountingQueue();
270
+ const ctx = new ContextImpl({
271
+ data: void 0,
272
+ federation: new FederationImpl({
273
+ kv: new MemoryKvStore(),
274
+ contextLoaderFactory: () => mockDocumentLoader,
275
+ queue: { fanout: queue },
276
+ manuallyStartQueue: true
277
+ }),
278
+ url: new URL("https://example.com/"),
279
+ documentLoader: mockDocumentLoader,
280
+ contextLoader: mockDocumentLoader
281
+ });
282
+ const activity = new vocab.Create({
283
+ id: new URL("https://example.com/activity/1"),
284
+ actor: new URL("https://example.com/person")
285
+ });
286
+ await ctx.sendActivity([{
287
+ privateKey: rsaPrivateKey2,
288
+ keyId: rsaPublicKey2.id
289
+ }], {
290
+ id: new URL("https://example.com/recipient"),
291
+ inboxId: new URL("https://example.com/inbox")
292
+ }, activity, { fanout: "force" });
293
+ assertEquals(queue.listenCount, 0);
294
+ assertEquals(queue.messages.length, 1);
295
+ assertEquals(queue.messages[0].type, "fanout");
296
+ });
297
+ });
197
298
  test({
198
299
  name: "Federation.createContext()",
199
300
  permissions: {
@@ -628,6 +729,22 @@ test({
628
729
  localComments: 456
629
730
  }
630
731
  });
732
+ await assertRejects(() => ctx.lookupNodeInfo("https://example.com/", { timeout: 0 }), RangeError);
733
+ const caller = new AbortController();
734
+ caller.abort();
735
+ assertEquals(await ctx.lookupNodeInfo("https://example.com/", {
736
+ signal: caller.signal,
737
+ timeout: null
738
+ }), void 0);
739
+ assertEquals(await ctx.lookupNodeInfo("https://example.com/", {
740
+ parse: "none",
741
+ signal: caller.signal,
742
+ timeout: null
743
+ }), void 0);
744
+ await assertRejects(() => ctx.lookupNodeInfo("https://example.com/", {
745
+ parse: "none",
746
+ timeout: 0
747
+ }), RangeError);
631
748
  });
632
749
  await t.step("RequestContext", async () => {
633
750
  const federation = createFederation({
@@ -1789,6 +1906,356 @@ test("FederationImpl.sendActivity()", async (t) => {
1789
1906
  });
1790
1907
  esm_default.hardReset();
1791
1908
  });
1909
+ const QUEUE_STARTUP_WARNING = "Failed to start queue workers automatically: {error}";
1910
+ /**
1911
+ * Captures unhandled promise rejections for the duration of a test step.
1912
+ *
1913
+ * Deno dispatches `unhandledrejection` on `globalThis`, while Node.js and Bun
1914
+ * dispatch `unhandledRejection` on `process`. Bun's test runner currently
1915
+ * fires neither, so there the native runner's own unhandled-rejection failure
1916
+ * remains the signal; the assertion is harmless otherwise.
1917
+ */
1918
+ function captureUnhandledRejections() {
1919
+ const reasons = [];
1920
+ const isDeno = "Deno" in globalThis;
1921
+ const onRejectionEvent = (event) => {
1922
+ event.preventDefault();
1923
+ reasons.push(event.reason);
1924
+ };
1925
+ const onProcessRejection = (reason) => {
1926
+ reasons.push(reason);
1927
+ };
1928
+ const globalTarget = globalThis;
1929
+ const proc = globalThis.process;
1930
+ if (isDeno) globalTarget.addEventListener?.("unhandledrejection", onRejectionEvent);
1931
+ else proc?.on?.("unhandledRejection", onProcessRejection);
1932
+ async function settle() {
1933
+ await new Promise((resolve) => setTimeout(resolve, 0));
1934
+ await new Promise((resolve) => setTimeout(resolve, 0));
1935
+ }
1936
+ return {
1937
+ reasons,
1938
+ settle,
1939
+ stop() {
1940
+ if (isDeno) globalTarget.removeEventListener?.("unhandledrejection", onRejectionEvent);
1941
+ else proc?.off?.("unhandledRejection", onProcessRejection);
1942
+ }
1943
+ };
1944
+ }
1945
+ async function configureQueueStartupWarningCapture(records) {
1946
+ await configure({
1947
+ sinks: { buffer(record) {
1948
+ records.push(record);
1949
+ } },
1950
+ loggers: [{
1951
+ category: "fedify",
1952
+ sinks: ["buffer"],
1953
+ lowestLevel: "warning"
1954
+ }, {
1955
+ category: ["logtape", "meta"],
1956
+ sinks: []
1957
+ }],
1958
+ reset: true
1959
+ });
1960
+ }
1961
+ function queueStartupWarnings(records, error) {
1962
+ return records.filter((record) => record.level === "warning" && record.category.length === 3 && record.category[0] === "fedify" && record.category[1] === "federation" && record.category[2] === "queue" && record.rawMessage === QUEUE_STARTUP_WARNING && record.properties.error === error);
1963
+ }
1964
+ test("Federation.fetch() reports automatic queue startup failures", async (t) => {
1965
+ const listenError = /* @__PURE__ */ new TypeError("simulated listen() failure");
1966
+ const queue = {
1967
+ enqueue() {
1968
+ return Promise.resolve();
1969
+ },
1970
+ listen() {
1971
+ throw listenError;
1972
+ }
1973
+ };
1974
+ const capture = captureUnhandledRejections();
1975
+ const records = [];
1976
+ await configureQueueStartupWarningCapture(records);
1977
+ try {
1978
+ const federation = createFederation({
1979
+ kv: new MemoryKvStore(),
1980
+ queue,
1981
+ skipSignatureVerification: true,
1982
+ documentLoaderFactory: () => mockDocumentLoader,
1983
+ contextLoaderFactory: () => mockDocumentLoader
1984
+ });
1985
+ federation.setActorDispatcher("/actors/{identifier}", () => new Person({ id: new URL("https://example.com/actor") }));
1986
+ federation.setInboxListeners("/actors/{identifier}/inbox", "/inbox").on(Create, () => {});
1987
+ const post = (id) => federation.fetch(new Request("https://example.com/inbox", {
1988
+ method: "POST",
1989
+ headers: { "Content-Type": "application/activity+json" },
1990
+ body: JSON.stringify({
1991
+ "@context": "https://www.w3.org/ns/activitystreams",
1992
+ id,
1993
+ type: "Create",
1994
+ actor: "https://example.com/actor"
1995
+ })
1996
+ }), { contextData: void 0 });
1997
+ await t.step("synchronous failure", async () => {
1998
+ assertEquals((await post("https://example.com/activities/queued-1")).status, 202);
1999
+ await capture.settle();
2000
+ assertEquals(queueStartupWarnings(records, listenError).length, 1);
2001
+ assertEquals(capture.reasons, []);
2002
+ assertEquals((await post("https://example.com/activities/queued-2")).status, 202);
2003
+ await capture.settle();
2004
+ assertEquals(queueStartupWarnings(records, listenError).length, 1, "the warning must be logged only once per federation instance");
2005
+ assertEquals(capture.reasons, []);
2006
+ });
2007
+ await t.step("asynchronous failure", async () => {
2008
+ const asyncError = /* @__PURE__ */ new TypeError("simulated async listen() failure");
2009
+ let rejectListen;
2010
+ const asyncFederation = createFederation({
2011
+ kv: new MemoryKvStore(),
2012
+ queue: {
2013
+ enqueue() {
2014
+ return Promise.resolve();
2015
+ },
2016
+ listen() {
2017
+ return new Promise((_resolve, reject) => {
2018
+ rejectListen = reject;
2019
+ });
2020
+ }
2021
+ },
2022
+ skipSignatureVerification: true,
2023
+ documentLoaderFactory: () => mockDocumentLoader,
2024
+ contextLoaderFactory: () => mockDocumentLoader
2025
+ });
2026
+ asyncFederation.setActorDispatcher("/actors/{identifier}", () => new Person({ id: new URL("https://example.com/actor") }));
2027
+ asyncFederation.setInboxListeners("/actors/{identifier}/inbox", "/inbox").on(Create, () => {});
2028
+ assertEquals((await asyncFederation.fetch(new Request("https://example.com/inbox", {
2029
+ method: "POST",
2030
+ headers: { "Content-Type": "application/activity+json" },
2031
+ body: JSON.stringify({
2032
+ "@context": "https://www.w3.org/ns/activitystreams",
2033
+ id: "https://example.com/activities/queued-async",
2034
+ type: "Create",
2035
+ actor: "https://example.com/actor"
2036
+ })
2037
+ }), { contextData: void 0 })).status, 202);
2038
+ await capture.settle();
2039
+ assertEquals(queueStartupWarnings(records, asyncError).length, 0);
2040
+ assert(rejectListen != null);
2041
+ rejectListen(asyncError);
2042
+ await capture.settle();
2043
+ assertEquals(queueStartupWarnings(records, asyncError).length, 1, "a listener that rejects after startup must still be reported");
2044
+ assertEquals(capture.reasons, []);
2045
+ });
2046
+ } finally {
2047
+ await capture.settle();
2048
+ capture.stop();
2049
+ await reset();
2050
+ }
2051
+ });
2052
+ test("Federation.startQueue() rejects when a queue cannot listen", async () => {
2053
+ const listenError = /* @__PURE__ */ new TypeError("simulated listen() failure");
2054
+ const federation = createFederation({
2055
+ kv: new MemoryKvStore(),
2056
+ queue: {
2057
+ enqueue() {
2058
+ return Promise.resolve();
2059
+ },
2060
+ listen() {
2061
+ throw listenError;
2062
+ }
2063
+ },
2064
+ manuallyStartQueue: true
2065
+ });
2066
+ assertStrictEquals(await assertRejects(() => federation.startQueue(void 0)), listenError);
2067
+ });
2068
+ test("Federation.startQueue() keeps other queue workers running", async () => {
2069
+ const outboxError = /* @__PURE__ */ new TypeError("simulated outbox listen() failure");
2070
+ const lateInboxError = /* @__PURE__ */ new TypeError("simulated late inbox failure");
2071
+ let rejectInbox;
2072
+ let fanoutStarted = false;
2073
+ const inbox = {
2074
+ enqueue() {
2075
+ return Promise.resolve();
2076
+ },
2077
+ listen() {
2078
+ return new Promise((_resolve, reject) => {
2079
+ rejectInbox = reject;
2080
+ });
2081
+ }
2082
+ };
2083
+ const outbox = {
2084
+ enqueue() {
2085
+ return Promise.resolve();
2086
+ },
2087
+ listen() {
2088
+ throw outboxError;
2089
+ }
2090
+ };
2091
+ const fanout = {
2092
+ enqueue() {
2093
+ return Promise.resolve();
2094
+ },
2095
+ listen() {
2096
+ fanoutStarted = true;
2097
+ return new Promise(() => {});
2098
+ }
2099
+ };
2100
+ const capture = captureUnhandledRejections();
2101
+ try {
2102
+ const federation = createFederation({
2103
+ kv: new MemoryKvStore(),
2104
+ queue: {
2105
+ inbox,
2106
+ outbox,
2107
+ fanout
2108
+ },
2109
+ manuallyStartQueue: true
2110
+ });
2111
+ assertStrictEquals(await assertRejects(() => federation.startQueue(void 0)), outboxError);
2112
+ assert(fanoutStarted, "a synchronous throw from one queue must not prevent later queues from starting");
2113
+ assert(rejectInbox != null);
2114
+ rejectInbox(lateInboxError);
2115
+ await capture.settle();
2116
+ assertEquals(capture.reasons, [], "an earlier listener rejection must remain handled after startup fails");
2117
+ } finally {
2118
+ await capture.settle();
2119
+ capture.stop();
2120
+ }
2121
+ });
2122
+ test("ContextImpl.sendActivity() reports automatic queue startup failures", async (t) => {
2123
+ const sender = [{
2124
+ privateKey: ed25519PrivateKey,
2125
+ keyId: ed25519Multikey.id
2126
+ }];
2127
+ const recipient = {
2128
+ id: new URL("https://example.com/recipient"),
2129
+ inboxId: new URL("https://example.com/inbox")
2130
+ };
2131
+ const createActivity = () => new Create({
2132
+ id: new URL("https://example.com/activities/queue-startup-send"),
2133
+ actor: new URL("https://example.com/actor")
2134
+ });
2135
+ await t.step("outbox", async () => {
2136
+ const listenError = /* @__PURE__ */ new TypeError("simulated outbox listen() failure");
2137
+ const messages = [];
2138
+ const queue = {
2139
+ enqueue(message) {
2140
+ messages.push(message);
2141
+ return Promise.resolve();
2142
+ },
2143
+ listen() {
2144
+ throw listenError;
2145
+ }
2146
+ };
2147
+ const capture = captureUnhandledRejections();
2148
+ const records = [];
2149
+ await configureQueueStartupWarningCapture(records);
2150
+ try {
2151
+ await new ContextImpl({
2152
+ data: void 0,
2153
+ federation: new FederationImpl({
2154
+ kv: new MemoryKvStore(),
2155
+ queue: { outbox: queue },
2156
+ documentLoaderFactory: () => mockDocumentLoader,
2157
+ contextLoaderFactory: () => mockDocumentLoader
2158
+ }),
2159
+ url: new URL("https://example.com/"),
2160
+ documentLoader: mockDocumentLoader,
2161
+ contextLoader: mockDocumentLoader
2162
+ }).sendActivity(sender, recipient, createActivity());
2163
+ assertEquals(messages.map((message) => message.type), ["outbox"]);
2164
+ await capture.settle();
2165
+ assertEquals(queueStartupWarnings(records, listenError).length, 1);
2166
+ assertEquals(capture.reasons, []);
2167
+ } finally {
2168
+ await capture.settle();
2169
+ capture.stop();
2170
+ await reset();
2171
+ }
2172
+ });
2173
+ await t.step("fanout", async () => {
2174
+ const listenError = /* @__PURE__ */ new TypeError("simulated fanout listen() failure");
2175
+ const messages = [];
2176
+ const outbox = {
2177
+ enqueue() {
2178
+ return Promise.resolve();
2179
+ },
2180
+ listen() {
2181
+ return Promise.resolve();
2182
+ }
2183
+ };
2184
+ const fanout = {
2185
+ enqueue(message) {
2186
+ messages.push(message);
2187
+ return Promise.resolve();
2188
+ },
2189
+ listen() {
2190
+ throw listenError;
2191
+ }
2192
+ };
2193
+ const capture = captureUnhandledRejections();
2194
+ const records = [];
2195
+ await configureQueueStartupWarningCapture(records);
2196
+ try {
2197
+ await new ContextImpl({
2198
+ data: void 0,
2199
+ federation: new FederationImpl({
2200
+ kv: new MemoryKvStore(),
2201
+ queue: {
2202
+ outbox,
2203
+ fanout
2204
+ },
2205
+ documentLoaderFactory: () => mockDocumentLoader,
2206
+ contextLoaderFactory: () => mockDocumentLoader
2207
+ }),
2208
+ url: new URL("https://example.com/"),
2209
+ documentLoader: mockDocumentLoader,
2210
+ contextLoader: mockDocumentLoader
2211
+ }).sendActivity(sender, recipient, createActivity(), { fanout: "force" });
2212
+ assertEquals(messages.map((message) => message.type), ["fanout"]);
2213
+ await capture.settle();
2214
+ assertEquals(queueStartupWarnings(records, listenError).length, 1);
2215
+ assertEquals(capture.reasons, []);
2216
+ } finally {
2217
+ await capture.settle();
2218
+ capture.stop();
2219
+ await reset();
2220
+ }
2221
+ });
2222
+ });
2223
+ test("queued Linked Data signatures without created use normalization contexts", async (t) => {
2224
+ for (const legacy of [false, true]) await t.step(legacy ? "legacy compacted payload" : "verified normalized cache", async () => {
2225
+ const normalized = await compactJsonLd(ldSignatureWithoutCreated, mockDocumentLoader);
2226
+ const raw = legacy ? normalized : structuredClone(ldSignatureWithoutCreated);
2227
+ const federation = new FederationImpl({
2228
+ kv: new MemoryKvStore(),
2229
+ contextLoaderFactory: () => async (url) => {
2230
+ if (url === "https://www.w3.org/ns/activitystreams" || url === "https://w3id.org/identity/v1") return await mockDocumentLoader(url);
2231
+ throw new Error(`Unexpected context: ${url}`);
2232
+ }
2233
+ });
2234
+ let deliveries = 0;
2235
+ let receivedRaw;
2236
+ federation.setInboxListeners("/users/{identifier}/inbox", "/inbox").on(Create, (ctx, activity) => {
2237
+ deliveries++;
2238
+ receivedRaw = ctx.activity;
2239
+ assertEquals(activity.id?.href, ldSignatureWithoutCreated.id);
2240
+ });
2241
+ await federation.processQueuedTask(void 0, {
2242
+ type: "inbox",
2243
+ id: crypto.randomUUID(),
2244
+ baseUrl: "https://example.com",
2245
+ activity: raw,
2246
+ ...legacy ? {} : {
2247
+ normalizedActivity: normalized,
2248
+ ldSignatureVerified: true
2249
+ },
2250
+ started: (/* @__PURE__ */ new Date()).toISOString(),
2251
+ attempt: 0,
2252
+ identifier: null,
2253
+ traceContext: {}
2254
+ });
2255
+ assertEquals(deliveries, 1);
2256
+ assertEquals(receivedRaw, raw);
2257
+ });
2258
+ });
1792
2259
  test("FederationImpl.processQueuedTask()", async (t) => {
1793
2260
  await t.step("with MessageQueue having nativeRetrial", async () => {
1794
2261
  const kv = new MemoryKvStore();
@@ -3808,15 +4275,19 @@ test("ContextImpl.sendActivity()", async (t) => {
3808
4275
  queue
3809
4276
  });
3810
4277
  federation2.setActorDispatcher("/{identifier}", async (ctx, identifier) => {
3811
- if (identifier !== "john") return null;
4278
+ if (identifier !== "john" && identifier !== "rsa-only") return null;
3812
4279
  const keys = await ctx.getActorKeyPairs(identifier);
3813
4280
  return new vocab.Person({
3814
4281
  id: ctx.getActorUri(identifier),
3815
- preferredUsername: "john",
4282
+ preferredUsername: identifier,
3816
4283
  publicKey: keys[0].cryptographicKey,
3817
4284
  assertionMethods: keys.map((k) => k.multikey)
3818
4285
  });
3819
4286
  }).setKeyPairsDispatcher((_ctx, identifier) => {
4287
+ if (identifier === "rsa-only") return [{
4288
+ privateKey: rsaPrivateKey2,
4289
+ publicKey: rsaPublicKey2.publicKey
4290
+ }];
3820
4291
  if (identifier !== "john") return [];
3821
4292
  return [{
3822
4293
  privateKey: rsaPrivateKey2,
@@ -3865,6 +4336,375 @@ test("ContextImpl.sendActivity()", async (t) => {
3865
4336
  proofCount++;
3866
4337
  }
3867
4338
  assertEquals(proofCount, 1);
4339
+ const oldContext = [
4340
+ "https://w3id.org/identity/v1",
4341
+ "https://www.w3.org/ns/activitystreams",
4342
+ "https://w3id.org/security/data-integrity/v1"
4343
+ ];
4344
+ const oldJson = await (await signObject(new vocab.Create({
4345
+ id: new URL("https://example.com/activity/1"),
4346
+ actor: new URL("https://example.com/john")
4347
+ }), ed25519PrivateKey, ed25519Multikey.id, {
4348
+ contextLoader: documentLoader,
4349
+ context: oldContext
4350
+ })).toJsonLd({
4351
+ format: "compact",
4352
+ contextLoader: documentLoader,
4353
+ context: oldContext
4354
+ });
4355
+ queue.clear();
4356
+ await federation2.processQueuedTask(void 0, {
4357
+ ...fanoutMsg,
4358
+ id: crypto.randomUUID(),
4359
+ activity: oldJson
4360
+ });
4361
+ const replayed = queue.messages.at(-1);
4362
+ assert(replayed != null && replayed.type === "outbox");
4363
+ assertEquals(detachSignature(replayed.activity), oldJson);
4364
+ const proofless = { ...oldJson };
4365
+ delete proofless.proof;
4366
+ queue.clear();
4367
+ await federation2.processQueuedTask(void 0, {
4368
+ ...fanoutMsg,
4369
+ id: crypto.randomUUID(),
4370
+ activity: proofless
4371
+ });
4372
+ const signedReplay = queue.messages.at(-1);
4373
+ assert(signedReplay != null && signedReplay.type === "outbox");
4374
+ assert(typeof signedReplay.activity === "object" && signedReplay.activity != null && "proof" in signedReplay.activity);
4375
+ const edKey = (await ctx2.getActorKeyPairs("john")).find((key) => key.privateKey.algorithm.name === "Ed25519");
4376
+ assert(edKey != null && edKey.multikey.id != null);
4377
+ const restoredJson = await (await signObject(new vocab.Create({
4378
+ id: new URL("https://example.com/activity/old"),
4379
+ actor: ctx2.getActorUri("john")
4380
+ }), edKey.privateKey, edKey.multikey.id, {
4381
+ contextLoader: documentLoader,
4382
+ context: oldContext
4383
+ })).toJsonLd({
4384
+ format: "compact",
4385
+ contextLoader: documentLoader,
4386
+ context: oldContext
4387
+ });
4388
+ const restored = await vocab.Create.fromJsonLd(restoredJson, {
4389
+ contextLoader: documentLoader,
4390
+ documentLoader
4391
+ });
4392
+ queue.clear();
4393
+ await ctx2.sendActivity([{
4394
+ privateKey: rsaPrivateKey2,
4395
+ keyId: rsaPublicKey2.id
4396
+ }], {
4397
+ id: new URL("https://example.com/recipient"),
4398
+ inboxId: new URL("https://example.com/inbox")
4399
+ }, restored);
4400
+ const resent = queue.messages.at(-1);
4401
+ assert(resent != null && resent.type === "outbox");
4402
+ assertEquals(detachSignature(resent.activity), restoredJson);
4403
+ queue.clear();
4404
+ await ctx2.sendActivity([{
4405
+ privateKey: rsaPrivateKey2,
4406
+ keyId: rsaPublicKey2.id
4407
+ }], {
4408
+ id: new URL("https://example.com/recipient"),
4409
+ inboxId: new URL("https://example.com/inbox")
4410
+ }, restored, { fanout: "force" });
4411
+ const queuedFanout = queue.messages.at(-1);
4412
+ assert(queuedFanout != null && queuedFanout.type === "fanout");
4413
+ assertEquals(queuedFanout.activity, restoredJson);
4414
+ const restoredWithSignature = await vocab.Create.fromJsonLd(attachSignature(restoredJson, {
4415
+ "@context": "https://w3id.org/identity/v1",
4416
+ type: "RsaSignature2017",
4417
+ creator: "https://example.com/person2#key4",
4418
+ created: "2026-01-01T00:00:00Z",
4419
+ signatureValue: "abc"
4420
+ }), {
4421
+ contextLoader: documentLoader,
4422
+ documentLoader
4423
+ });
4424
+ queue.clear();
4425
+ await ctx2.sendActivity([{
4426
+ privateKey: rsaPrivateKey2,
4427
+ keyId: rsaPublicKey2.id
4428
+ }], {
4429
+ id: new URL("https://example.com/recipient"),
4430
+ inboxId: new URL("https://example.com/inbox")
4431
+ }, restoredWithSignature);
4432
+ const resigned = queue.messages.at(-1);
4433
+ assert(resigned != null && resigned.type === "outbox");
4434
+ assertEquals(detachSignature(resigned.activity), restoredJson);
4435
+ const rsaOnlyJson = await (await signObject(new vocab.Create({
4436
+ id: new URL("https://example.com/activity/rsa-only"),
4437
+ actor: new URL("https://example.com/rsa-only")
4438
+ }), ed25519PrivateKey, ed25519Multikey.id, {
4439
+ contextLoader: documentLoader,
4440
+ context: oldContext
4441
+ })).toJsonLd({
4442
+ format: "compact",
4443
+ contextLoader: documentLoader,
4444
+ context: oldContext
4445
+ });
4446
+ const rsaOnlyRestored = await vocab.Create.fromJsonLd(rsaOnlyJson, {
4447
+ contextLoader: documentLoader,
4448
+ documentLoader
4449
+ });
4450
+ queue.clear();
4451
+ await ctx2.sendActivity({ identifier: "rsa-only" }, {
4452
+ id: new URL("https://example.com/recipient"),
4453
+ inboxId: new URL("https://example.com/inbox")
4454
+ }, rsaOnlyRestored, { fanout: "force" });
4455
+ const rsaOnlyMessage = queue.messages.at(-1);
4456
+ assert(rsaOnlyMessage != null && rsaOnlyMessage.type === "fanout");
4457
+ assertEquals(rsaOnlyMessage.activity, rsaOnlyJson);
4458
+ queue.clear();
4459
+ await ctx2.sendActivity({ username: "john" }, {
4460
+ id: new URL("https://example.com/recipient"),
4461
+ inboxId: new URL("https://example.com/inbox")
4462
+ }, restored, { fanout: "force" });
4463
+ const namedFanout = queue.messages.at(-1);
4464
+ assert(namedFanout != null && namedFanout.type === "fanout");
4465
+ const namedJson = detachSignature(namedFanout.activity);
4466
+ assertEquals((Array.isArray(namedJson.proof) ? namedJson.proof : [namedJson.proof]).length, 1);
4467
+ assert(namedJson["@context"].includes("https://w3id.org/security/v1"));
4468
+ const noIdJson = await (await signObject(new vocab.Create({ actor: ctx2.getActorUri("john") }), edKey.privateKey, edKey.multikey.id, {
4469
+ contextLoader: documentLoader,
4470
+ context: oldContext
4471
+ })).toJsonLd({
4472
+ format: "compact",
4473
+ contextLoader: documentLoader,
4474
+ context: oldContext
4475
+ });
4476
+ const noIdRestored = await vocab.Create.fromJsonLd(noIdJson, {
4477
+ contextLoader: documentLoader,
4478
+ documentLoader
4479
+ });
4480
+ queue.clear();
4481
+ await ctx2.sendActivity([{
4482
+ privateKey: rsaPrivateKey2,
4483
+ keyId: rsaPublicKey2.id
4484
+ }], {
4485
+ id: new URL("https://example.com/recipient"),
4486
+ inboxId: new URL("https://example.com/inbox")
4487
+ }, noIdRestored);
4488
+ const noIdQueued = queue.messages.at(-1);
4489
+ assert(noIdQueued != null && noIdQueued.type === "outbox");
4490
+ assert(typeof detachSignature(noIdQueued.activity).id === "string");
4491
+ const currentJson = await (await signObject(new vocab.Create({
4492
+ id: new URL("https://example.com/activity/current"),
4493
+ actor: ctx2.getActorUri("john")
4494
+ }), edKey.privateKey, edKey.multikey.id, { contextLoader: documentLoader })).toJsonLd({
4495
+ format: "compact",
4496
+ contextLoader: documentLoader
4497
+ });
4498
+ const currentRestored = await vocab.Create.fromJsonLd(currentJson, {
4499
+ contextLoader: documentLoader,
4500
+ documentLoader
4501
+ });
4502
+ queue.clear();
4503
+ await ctx2.sendActivity({ username: "john" }, {
4504
+ id: new URL("https://example.com/recipient"),
4505
+ inboxId: new URL("https://example.com/inbox")
4506
+ }, currentRestored, { fanout: "force" });
4507
+ const currentFanout = queue.messages.at(-1);
4508
+ assert(currentFanout != null && currentFanout.type === "fanout");
4509
+ const currentQueued = detachSignature(currentFanout.activity);
4510
+ assertEquals((Array.isArray(currentQueued.proof) ? currentQueued.proof : [currentQueued.proof]).length, 2);
4511
+ });
4512
+ queue.clear();
4513
+ await t.step("activity transformers preserve proofs", async () => {
4514
+ const originalProof = new vocab.DataIntegrityProof({
4515
+ cryptosuite: "eddsa-jcs-2022",
4516
+ verificationMethod: new URL("https://example.com/rsa-only#multikey-2"),
4517
+ proofPurpose: "assertionMethod",
4518
+ proofValue: new Uint8Array([
4519
+ 1,
4520
+ 2,
4521
+ 3,
4522
+ 4
4523
+ ]),
4524
+ created: Temporal.Instant.from("2026-01-01T00:00:00Z")
4525
+ });
4526
+ const addedProof = new vocab.DataIntegrityProof({
4527
+ cryptosuite: "eddsa-jcs-2022",
4528
+ verificationMethod: new URL("https://example.com/rsa-only#multikey-2"),
4529
+ proofPurpose: "assertionMethod",
4530
+ proofValue: new Uint8Array([
4531
+ 5,
4532
+ 6,
4533
+ 7,
4534
+ 8
4535
+ ]),
4536
+ created: Temporal.Instant.from("2026-01-02T00:00:00Z")
4537
+ });
4538
+ const replacementProof = new vocab.DataIntegrityProof({
4539
+ cryptosuite: "eddsa-jcs-2022",
4540
+ verificationMethod: new URL("https://example.com/rsa-only#multikey-2"),
4541
+ proofPurpose: "assertionMethod",
4542
+ proofValue: new Uint8Array([
4543
+ 9,
4544
+ 10,
4545
+ 11,
4546
+ 12
4547
+ ]),
4548
+ created: Temporal.Instant.from("2026-01-03T00:00:00Z")
4549
+ });
4550
+ const restoredProof = new vocab.DataIntegrityProof({
4551
+ cryptosuite: "eddsa-jcs-2022",
4552
+ verificationMethod: new URL("https://example.com/rsa-only#multikey-2"),
4553
+ proofPurpose: "assertionMethod",
4554
+ proofValue: new Uint8Array([
4555
+ 13,
4556
+ 14,
4557
+ 15,
4558
+ 16
4559
+ ]),
4560
+ created: Temporal.Instant.from("2026-01-04T00:00:00Z")
4561
+ });
4562
+ const restoredProofHolder = {};
4563
+ const federation3 = new FederationImpl({
4564
+ kv,
4565
+ allowPrivateAddress: true,
4566
+ queue,
4567
+ activityTransformers: [(activity) => {
4568
+ if (activity.id?.href === "https://example.com/activity/transformed-proof") return activity.clone({ proofs: [originalProof, addedProof] });
4569
+ if (activity.id?.href === "https://example.com/activity/replaced-proof") return new vocab.Create({
4570
+ id: activity.id,
4571
+ actor: new URL("https://example.com/rsa-only"),
4572
+ object: new URL("https://example.com/note/replaced"),
4573
+ proofs: [replacementProof]
4574
+ });
4575
+ if (activity.id?.href === "https://example.com/activity/restored-proof") return activity.clone({ proofs: [restoredProofHolder.old, restoredProof] });
4576
+ return activity;
4577
+ }]
4578
+ });
4579
+ federation3.setActorDispatcher("/{identifier}", async (ctx, identifier) => {
4580
+ if (identifier !== "rsa-only") return null;
4581
+ const keys = await ctx.getActorKeyPairs(identifier);
4582
+ return new vocab.Person({
4583
+ id: ctx.getActorUri(identifier),
4584
+ preferredUsername: identifier,
4585
+ publicKey: keys[0].cryptographicKey,
4586
+ assertionMethods: keys.map((k) => k.multikey)
4587
+ });
4588
+ }).setKeyPairsDispatcher((_ctx, identifier) => identifier === "rsa-only" ? [{
4589
+ privateKey: rsaPrivateKey2,
4590
+ publicKey: rsaPublicKey2.publicKey
4591
+ }] : []);
4592
+ const ctx3 = new ContextImpl({
4593
+ data: void 0,
4594
+ federation: federation3,
4595
+ url: new URL("https://example.com/"),
4596
+ documentLoader,
4597
+ contextLoader: documentLoader
4598
+ });
4599
+ const activity = new vocab.Create({
4600
+ id: new URL("https://example.com/activity/transformed-proof"),
4601
+ actor: ctx3.getActorUri("rsa-only"),
4602
+ proofs: [originalProof]
4603
+ });
4604
+ await ctx3.sendActivity({ identifier: "rsa-only" }, {
4605
+ id: new URL("https://example.com/recipient"),
4606
+ inboxId: new URL("https://example.com/inbox")
4607
+ }, activity, { fanout: "force" });
4608
+ const fanout = queue.messages.at(-1);
4609
+ assert(fanout != null && fanout.type === "fanout");
4610
+ const fanoutJson = detachSignature(fanout.activity);
4611
+ assertEquals((Array.isArray(fanoutJson.proof) ? fanoutJson.proof : [fanoutJson.proof]).length, 2);
4612
+ const replacedActivity = new vocab.Create({
4613
+ id: new URL("https://example.com/activity/replaced-proof"),
4614
+ actor: ctx3.getActorUri("rsa-only"),
4615
+ proofs: [originalProof]
4616
+ });
4617
+ queue.clear();
4618
+ await ctx3.sendActivity({ identifier: "rsa-only" }, {
4619
+ id: new URL("https://example.com/recipient"),
4620
+ inboxId: new URL("https://example.com/inbox")
4621
+ }, replacedActivity, { fanout: "force" });
4622
+ const replacedFanout = queue.messages.at(-1);
4623
+ assert(replacedFanout != null && replacedFanout.type === "fanout");
4624
+ const replacedJson = detachSignature(replacedFanout.activity);
4625
+ assertEquals(replacedJson.object, "https://example.com/note/replaced");
4626
+ const replacedProofs = Array.isArray(replacedJson.proof) ? replacedJson.proof : [replacedJson.proof];
4627
+ assertEquals(replacedProofs.length, 1);
4628
+ const [keptProof] = replacedProofs;
4629
+ assertEquals(keptProof.created, "2026-01-03T00:00:00Z");
4630
+ const oldContext = [
4631
+ "https://w3id.org/identity/v1",
4632
+ "https://www.w3.org/ns/activitystreams",
4633
+ "https://w3id.org/security/data-integrity/v1"
4634
+ ];
4635
+ const restoredJson = await (await signObject(new vocab.Create({
4636
+ id: new URL("https://example.com/activity/restored-proof"),
4637
+ actor: ctx3.getActorUri("rsa-only")
4638
+ }), ed25519PrivateKey, ed25519Multikey.id, {
4639
+ contextLoader: documentLoader,
4640
+ context: oldContext
4641
+ })).toJsonLd({
4642
+ format: "compact",
4643
+ contextLoader: documentLoader,
4644
+ context: oldContext
4645
+ });
4646
+ const restored = await vocab.Create.fromJsonLd(restoredJson, {
4647
+ contextLoader: documentLoader,
4648
+ documentLoader
4649
+ });
4650
+ for await (const proof of restored.getProofs({ contextLoader: documentLoader })) {
4651
+ restoredProofHolder.old = proof;
4652
+ break;
4653
+ }
4654
+ queue.clear();
4655
+ await ctx3.sendActivity({ identifier: "rsa-only" }, {
4656
+ id: new URL("https://example.com/recipient"),
4657
+ inboxId: new URL("https://example.com/inbox")
4658
+ }, restored, { fanout: "force" });
4659
+ const restoredFanout = queue.messages.at(-1);
4660
+ assert(restoredFanout != null && restoredFanout.type === "fanout");
4661
+ const restoredFanoutJson = detachSignature(restoredFanout.activity);
4662
+ const restoredProofs = Array.isArray(restoredFanoutJson.proof) ? restoredFanoutJson.proof : [restoredFanoutJson.proof];
4663
+ assertEquals(restoredProofs.length, 1);
4664
+ const [transformedProof] = restoredProofs;
4665
+ assertEquals(transformedProof.created, "2026-01-04T00:00:00Z");
4666
+ assert(Array.isArray(restoredFanoutJson["@context"]) && restoredFanoutJson["@context"].includes("https://w3id.org/security/v1"));
4667
+ const staleTimestampJson = await (await signObject(new vocab.Create({
4668
+ id: new URL("https://example.com/activity/stale-timestamp"),
4669
+ actor: ctx3.getActorUri("rsa-only")
4670
+ }), ed25519PrivateKey, ed25519Multikey.id, {
4671
+ contextLoader: documentLoader,
4672
+ context: oldContext
4673
+ })).toJsonLd({
4674
+ format: "compact",
4675
+ contextLoader: documentLoader,
4676
+ context: oldContext
4677
+ });
4678
+ const staleTimestampProof = Array.isArray(staleTimestampJson.proof) ? staleTimestampJson.proof[0] : staleTimestampJson.proof;
4679
+ staleTimestampProof.created = "2026-01-01T00:00:00+00:00";
4680
+ const staleTimestamp = await vocab.Create.fromJsonLd(staleTimestampJson, {
4681
+ contextLoader: documentLoader,
4682
+ documentLoader
4683
+ });
4684
+ queue.clear();
4685
+ await ctx3.sendActivity({ identifier: "rsa-only" }, {
4686
+ id: new URL("https://example.com/recipient"),
4687
+ inboxId: new URL("https://example.com/inbox")
4688
+ }, staleTimestamp, { fanout: "force" });
4689
+ const staleTimestampFanout = queue.messages.at(-1);
4690
+ assert(staleTimestampFanout != null && staleTimestampFanout.type === "fanout");
4691
+ assertEquals(detachSignature(staleTimestampFanout.activity), staleTimestampJson);
4692
+ const proofless = new vocab.Create({
4693
+ id: new URL("https://example.com/activity/proofless"),
4694
+ actor: ctx3.getActorUri("rsa-only")
4695
+ });
4696
+ let unformattedSerializations = 0;
4697
+ const originalToJsonLd = proofless.toJsonLd.bind(proofless);
4698
+ Object.defineProperty(proofless, "toJsonLd", { value: async (options) => {
4699
+ if (options?.format == null) unformattedSerializations++;
4700
+ return await originalToJsonLd(options);
4701
+ } });
4702
+ queue.clear();
4703
+ await ctx3.sendActivity({ identifier: "rsa-only" }, {
4704
+ id: new URL("https://example.com/recipient"),
4705
+ inboxId: new URL("https://example.com/inbox")
4706
+ }, proofless, { fanout: "skip" });
4707
+ assertEquals(unformattedSerializations, 0);
3868
4708
  });
3869
4709
  queue.clear();
3870
4710
  await t.step("fanout: \"skip\"", async () => {
@@ -4546,6 +5386,63 @@ test("InboxContextImpl.forwardActivity()", async (t) => {
4546
5386
  });
4547
5387
  esm_default.hardReset();
4548
5388
  });
5389
+ test("InboxContextImpl.forwardActivity() without created", async (t) => {
5390
+ for (const immediate of [true, false]) await t.step(immediate ? "immediate delivery" : "queued delivery", async () => {
5391
+ const delivered = [];
5392
+ const queued = [];
5393
+ esm_default.spyGlobal();
5394
+ esm_default.post("https://example.com/inbox", async (call) => {
5395
+ delivered.push(await call.request.json());
5396
+ return new Response(null, { status: 202 });
5397
+ });
5398
+ try {
5399
+ const federation = new FederationImpl({
5400
+ kv: new MemoryKvStore(),
5401
+ allowPrivateAddress: true,
5402
+ manuallyStartQueue: true,
5403
+ queue: { outbox: {
5404
+ enqueue(message) {
5405
+ queued.push(message);
5406
+ return Promise.resolve();
5407
+ },
5408
+ async listen() {}
5409
+ } }
5410
+ });
5411
+ const activity = structuredClone(ldSignatureWithoutCreated);
5412
+ assert(await verifyJsonLd(activity, {
5413
+ documentLoader: mockDocumentLoader,
5414
+ contextLoader: mockDocumentLoader
5415
+ }));
5416
+ await new InboxContextImpl(null, activity, activity.id, "https://www.w3.org/ns/activitystreams#Create", {
5417
+ data: void 0,
5418
+ federation,
5419
+ url: new URL("https://example.com/"),
5420
+ documentLoader: mockDocumentLoader,
5421
+ contextLoader: mockDocumentLoader
5422
+ }).forwardActivity([{
5423
+ privateKey: rsaPrivateKey2,
5424
+ keyId: rsaPublicKey2.id
5425
+ }], {
5426
+ id: new URL("https://example.com/recipient"),
5427
+ inboxId: new URL("https://example.com/inbox")
5428
+ }, {
5429
+ skipIfUnsigned: true,
5430
+ immediate
5431
+ });
5432
+ if (immediate) {
5433
+ assertEquals(delivered, [activity]);
5434
+ assertEquals(queued, []);
5435
+ } else {
5436
+ assertEquals(delivered, []);
5437
+ assertEquals(queued.length, 1);
5438
+ assertEquals(queued[0].type, "outbox");
5439
+ assertEquals(queued[0].activity, activity);
5440
+ }
5441
+ } finally {
5442
+ esm_default.hardReset();
5443
+ }
5444
+ });
5445
+ });
4549
5446
  test("KvSpecDeterminer", async (t) => {
4550
5447
  await t.step("should use default spec when not found in KV", async () => {
4551
5448
  assertEquals(await new KvSpecDeterminer(new MemoryKvStore(), ["test", "spec"]).determineSpec("example.com"), "rfc9421");
@@ -4600,6 +5497,286 @@ test("ContextImpl.sendActivity() honors the private-address policy", async (t) =
4600
5497
  }
4601
5498
  });
4602
5499
  });
5500
+ function deferredSignal() {
5501
+ let resolve;
5502
+ return {
5503
+ promise: new Promise((r) => resolve = r),
5504
+ resolve
5505
+ };
5506
+ }
5507
+ test("ContextImpl.sendActivity() waits for sibling deliveries on failure", async () => {
5508
+ const failingInbox = "http://127.0.0.1:3001/inbox/fail";
5509
+ const slowInbox = "http://127.0.0.1:3002/inbox/slow";
5510
+ const failingEntered = deferredSignal();
5511
+ const slowEntered = deferredSignal();
5512
+ const slowCompleted = deferredSignal();
5513
+ let releaseSlow;
5514
+ const slowGate = new Promise((resolve) => releaseSlow = resolve);
5515
+ let slowStarted = false;
5516
+ let slowSettled = false;
5517
+ esm_default.spyGlobal();
5518
+ esm_default.post(failingInbox, () => {
5519
+ failingEntered.resolve();
5520
+ return new Response(null, { status: 500 });
5521
+ });
5522
+ esm_default.post(slowInbox, async () => {
5523
+ slowStarted = true;
5524
+ slowEntered.resolve();
5525
+ await slowGate;
5526
+ slowSettled = true;
5527
+ slowCompleted.resolve();
5528
+ return new Response(null, { status: 202 });
5529
+ });
5530
+ let observed;
5531
+ let released = false;
5532
+ try {
5533
+ const ctx = createFederation({
5534
+ kv: new MemoryKvStore(),
5535
+ allowPrivateAddress: true
5536
+ }).createContext(new URL("https://example.com/"));
5537
+ const activity = new Create({
5538
+ id: new URL("https://example.com/activity/fanout"),
5539
+ actor: new URL("https://example.com/person")
5540
+ });
5541
+ let outerSettled = false;
5542
+ let outerError;
5543
+ observed = ctx.sendActivity([{
5544
+ privateKey: ed25519PrivateKey,
5545
+ keyId: ed25519Multikey.id
5546
+ }], [new Person({
5547
+ id: new URL("https://example.com/recipients/failing"),
5548
+ inbox: new URL(failingInbox)
5549
+ }), new Person({
5550
+ id: new URL("https://example.com/recipients/slow"),
5551
+ inbox: new URL(slowInbox)
5552
+ })], activity, { immediate: true }).then(() => {
5553
+ outerSettled = true;
5554
+ }, (error) => {
5555
+ outerSettled = true;
5556
+ outerError = error;
5557
+ });
5558
+ await Promise.all([failingEntered.promise, slowEntered.promise]);
5559
+ await new Promise((resolve) => setTimeout(resolve, 0));
5560
+ assertFalse(outerSettled, "sendActivity() must wait for sibling deliveries before rejecting");
5561
+ releaseSlow();
5562
+ released = true;
5563
+ await observed;
5564
+ assert(slowSettled, "the delayed delivery should have settled");
5565
+ assertInstanceOf(outerError, SendActivityError);
5566
+ } finally {
5567
+ if (!released) releaseSlow();
5568
+ if (slowStarted) await slowCompleted.promise;
5569
+ await observed;
5570
+ esm_default.hardReset();
5571
+ }
5572
+ });
5573
+ test("InboxContextImpl.forwardActivity() waits for sibling deliveries on failure", async () => {
5574
+ const failingInbox = "http://127.0.0.1:3003/inbox/fail";
5575
+ const slowInbox = "http://127.0.0.1:3004/inbox/slow";
5576
+ const failingEntered = deferredSignal();
5577
+ const slowEntered = deferredSignal();
5578
+ const slowCompleted = deferredSignal();
5579
+ let releaseSlow;
5580
+ const slowGate = new Promise((resolve) => releaseSlow = resolve);
5581
+ let slowStarted = false;
5582
+ let slowSettled = false;
5583
+ esm_default.spyGlobal();
5584
+ esm_default.post(failingInbox, () => {
5585
+ failingEntered.resolve();
5586
+ return new Response(null, { status: 500 });
5587
+ });
5588
+ esm_default.post(slowInbox, async () => {
5589
+ slowStarted = true;
5590
+ slowEntered.resolve();
5591
+ await slowGate;
5592
+ slowSettled = true;
5593
+ slowCompleted.resolve();
5594
+ return new Response(null, { status: 202 });
5595
+ });
5596
+ let observed;
5597
+ let released = false;
5598
+ try {
5599
+ const federation = new FederationImpl({
5600
+ kv: new MemoryKvStore(),
5601
+ allowPrivateAddress: true
5602
+ });
5603
+ const activity = await signObject(new vocab.Create({
5604
+ id: new URL("https://example.com/activity/forward"),
5605
+ actor: new URL("https://example.com/person2")
5606
+ }), ed25519PrivateKey, ed25519Multikey.id, {
5607
+ contextLoader: mockDocumentLoader,
5608
+ documentLoader: mockDocumentLoader
5609
+ });
5610
+ const ctx = new InboxContextImpl(null, await activity.toJsonLd({ contextLoader: mockDocumentLoader }), activity.id?.href, getTypeId(activity).href, {
5611
+ data: void 0,
5612
+ federation,
5613
+ url: new URL("https://example.com/"),
5614
+ documentLoader: mockDocumentLoader,
5615
+ contextLoader: mockDocumentLoader
5616
+ });
5617
+ let outerSettled = false;
5618
+ let outerError;
5619
+ observed = ctx.forwardActivity([{
5620
+ privateKey: ed25519PrivateKey,
5621
+ keyId: ed25519Multikey.id
5622
+ }], [{
5623
+ id: new URL("https://example.com/recipients/failing"),
5624
+ inboxId: new URL(failingInbox)
5625
+ }, {
5626
+ id: new URL("https://example.com/recipients/slow"),
5627
+ inboxId: new URL(slowInbox)
5628
+ }], {
5629
+ immediate: true,
5630
+ skipIfUnsigned: true
5631
+ }).then(() => {
5632
+ outerSettled = true;
5633
+ }, (error) => {
5634
+ outerSettled = true;
5635
+ outerError = error;
5636
+ });
5637
+ await Promise.all([failingEntered.promise, slowEntered.promise]);
5638
+ await new Promise((resolve) => setTimeout(resolve, 0));
5639
+ assertFalse(outerSettled, "forwardActivity() must wait for sibling deliveries before rejecting");
5640
+ releaseSlow();
5641
+ released = true;
5642
+ await observed;
5643
+ assert(slowSettled, "the delayed delivery should have settled");
5644
+ assertInstanceOf(outerError, SendActivityError);
5645
+ } finally {
5646
+ if (!released) releaseSlow();
5647
+ if (slowStarted) await slowCompleted.promise;
5648
+ await observed;
5649
+ esm_default.hardReset();
5650
+ }
5651
+ });
5652
+ test("FederationImpl.sendActivity() drains deliveries when fan-out setup fails", async () => {
5653
+ const slowInbox = "http://127.0.0.1:3005/inbox/slow";
5654
+ const slowEntered = deferredSignal();
5655
+ const slowCompleted = deferredSignal();
5656
+ let releaseSlow;
5657
+ const slowGate = new Promise((resolve) => releaseSlow = resolve);
5658
+ let slowStarted = false;
5659
+ let slowSettled = false;
5660
+ esm_default.spyGlobal();
5661
+ esm_default.post(slowInbox, async () => {
5662
+ slowStarted = true;
5663
+ slowEntered.resolve();
5664
+ await slowGate;
5665
+ slowSettled = true;
5666
+ slowCompleted.resolve();
5667
+ return new Response(null, { status: 202 });
5668
+ });
5669
+ let observed;
5670
+ let released = false;
5671
+ try {
5672
+ const federation = new FederationImpl({
5673
+ kv: new MemoryKvStore(),
5674
+ allowPrivateAddress: true
5675
+ });
5676
+ const context = federation.createContext(new URL("https://example.com/"));
5677
+ const activity = new Create({
5678
+ id: new URL("https://example.com/activity/setup-failure"),
5679
+ actor: new URL("https://example.com/person")
5680
+ });
5681
+ let outerSettled = false;
5682
+ let outerError;
5683
+ observed = federation.sendActivity([{
5684
+ privateKey: ed25519PrivateKey,
5685
+ keyId: ed25519Multikey.id
5686
+ }], {
5687
+ [slowInbox]: {
5688
+ actorIds: ["https://example.com/recipients/slow"],
5689
+ sharedInbox: false
5690
+ },
5691
+ "not a url": {
5692
+ actorIds: ["https://example.com/recipients/invalid"],
5693
+ sharedInbox: false
5694
+ }
5695
+ }, activity, {
5696
+ context,
5697
+ immediate: true
5698
+ }).then(() => {
5699
+ outerSettled = true;
5700
+ }, (error) => {
5701
+ outerSettled = true;
5702
+ outerError = error;
5703
+ });
5704
+ await slowEntered.promise;
5705
+ await new Promise((resolve) => setTimeout(resolve, 0));
5706
+ assertFalse(outerSettled, "sendActivity() must drain started deliveries when fan-out setup fails");
5707
+ releaseSlow();
5708
+ released = true;
5709
+ await observed;
5710
+ assert(slowSettled, "the started delivery should have settled");
5711
+ assertInstanceOf(outerError, TypeError);
5712
+ } finally {
5713
+ if (!released) releaseSlow();
5714
+ if (slowStarted) await slowCompleted.promise;
5715
+ await observed;
5716
+ esm_default.hardReset();
5717
+ }
5718
+ });
5719
+ test("FederationImpl.sendActivity() handles rejections while building headers", async () => {
5720
+ const federation = new FederationImpl({ kv: new MemoryKvStore() });
5721
+ const context = federation.createContext(new URL("https://example.com/"));
5722
+ const activity = await signObject(new Create({
5723
+ id: new URL("https://example.com/activity/header-failure"),
5724
+ actor: new URL("https://example.com/person")
5725
+ }), ed25519PrivateKey, ed25519Multikey.id, {
5726
+ contextLoader: mockDocumentLoader,
5727
+ documentLoader: mockDocumentLoader
5728
+ });
5729
+ const subtle = crypto.subtle;
5730
+ const originalDigest = subtle.digest;
5731
+ let digestCalls = 0;
5732
+ let releaseSecondDigest;
5733
+ const secondDigestGate = new Promise((resolve) => {
5734
+ releaseSecondDigest = resolve;
5735
+ });
5736
+ let secondDigestStarted;
5737
+ const secondDigestEntered = new Promise((resolve) => {
5738
+ secondDigestStarted = resolve;
5739
+ });
5740
+ subtle.digest = async (algorithm, data) => {
5741
+ digestCalls++;
5742
+ if (digestCalls === 2) {
5743
+ secondDigestStarted();
5744
+ await secondDigestGate;
5745
+ }
5746
+ return await originalDigest.call(subtle, algorithm, data);
5747
+ };
5748
+ const capture = captureUnhandledRejections();
5749
+ let released = false;
5750
+ try {
5751
+ const observed = federation.sendActivity([{
5752
+ privateKey: ed25519PrivateKey,
5753
+ keyId: ed25519Multikey.id
5754
+ }], {
5755
+ "ftp://example.com/fail": {
5756
+ actorIds: ["https://example.com/recipients/failing"],
5757
+ sharedInbox: false
5758
+ },
5759
+ "http://127.0.0.1:3006/inbox": {
5760
+ actorIds: ["https://example.com/recipients/slow"],
5761
+ sharedInbox: false
5762
+ }
5763
+ }, activity, {
5764
+ context,
5765
+ immediate: true,
5766
+ collectionSync: "https://example.com/followers"
5767
+ }).catch((error) => error);
5768
+ await secondDigestEntered;
5769
+ await capture.settle();
5770
+ assertEquals(capture.reasons, [], "an early rejection must not surface as an unhandled rejection");
5771
+ releaseSecondDigest();
5772
+ released = true;
5773
+ await observed;
5774
+ } finally {
5775
+ if (!released) releaseSecondDigest();
5776
+ subtle.digest = originalDigest;
5777
+ capture.stop();
5778
+ }
5779
+ });
4603
5780
  test("Federation.fetch() bounds inbox bodies before dispatch", async () => {
4604
5781
  let dispatched = false;
4605
5782
  const json = JSON.stringify({