@fedify/relay 2.2.0-dev.613 → 2.2.0-dev.622
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.
- package/dist/litepub.test.d.ts +1 -2
- package/dist/litepub.test.js +22 -41
- package/dist/mastodon.test.d.ts +1 -2
- package/dist/mastodon.test.js +26 -49
- package/dist/mod.cjs +53 -93
- package/dist/mod.d.cts +119 -19
- package/dist/mod.d.ts +119 -19
- package/dist/mod.js +25 -43
- package/dist/{types-B94EmP_a.js → types-PxzSnHFx.js} +1759 -1932
- package/package.json +6 -6
package/dist/mod.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
import { Temporal } from "@js-temporal/polyfill";
|
|
3
|
-
|
|
1
|
+
import { Temporal } from "@js-temporal/polyfill";
|
|
4
2
|
import { createFederationBuilder, exportJwk, generateCryptoKeyPair, importJwk } from "@fedify/fedify";
|
|
5
3
|
import { Accept, Announce, Application, Create, Delete, Follow, Move, Object as Object$1, PUBLIC_COLLECTION, Reject, Undo, Update, isActor } from "@fedify/vocab";
|
|
6
4
|
import { getLogger } from "@logtape/logtape";
|
|
7
|
-
|
|
8
5
|
//#region src/types.ts
|
|
9
6
|
const RELAY_SERVER_ACTOR = "relay";
|
|
10
7
|
/**
|
|
@@ -20,12 +17,11 @@ function isRelayFollowerData(value) {
|
|
|
20
17
|
const obj = value;
|
|
21
18
|
return "actor" in obj && "state" in obj && typeof obj.state === "string" && (obj.state === "pending" || obj.state === "accepted");
|
|
22
19
|
}
|
|
23
|
-
|
|
24
20
|
//#endregion
|
|
25
21
|
//#region src/builder.ts
|
|
26
22
|
const relayBuilder = createFederationBuilder();
|
|
27
23
|
relayBuilder.setActorDispatcher("/users/{identifier}", async (ctx, identifier) => {
|
|
28
|
-
if (identifier !==
|
|
24
|
+
if (identifier !== "relay") return null;
|
|
29
25
|
const keys = await ctx.getActorKeyPairs(identifier);
|
|
30
26
|
return new Application({
|
|
31
27
|
id: ctx.getActorUri(identifier),
|
|
@@ -39,7 +35,7 @@ relayBuilder.setActorDispatcher("/users/{identifier}", async (ctx, identifier) =
|
|
|
39
35
|
assertionMethods: keys.map((k) => k.multikey)
|
|
40
36
|
});
|
|
41
37
|
}).setKeyPairsDispatcher(async (ctx, identifier) => {
|
|
42
|
-
if (identifier !==
|
|
38
|
+
if (identifier !== "relay") return [];
|
|
43
39
|
const rsaPairJson = await ctx.data.kv.get([
|
|
44
40
|
"keypair",
|
|
45
41
|
"rsa",
|
|
@@ -51,35 +47,33 @@ relayBuilder.setActorDispatcher("/users/{identifier}", async (ctx, identifier) =
|
|
|
51
47
|
identifier
|
|
52
48
|
]);
|
|
53
49
|
if (rsaPairJson == null || ed25519PairJson == null) {
|
|
54
|
-
const rsaPair
|
|
55
|
-
const ed25519Pair
|
|
50
|
+
const rsaPair = await generateCryptoKeyPair("RSASSA-PKCS1-v1_5");
|
|
51
|
+
const ed25519Pair = await generateCryptoKeyPair("Ed25519");
|
|
56
52
|
await ctx.data.kv.set([
|
|
57
53
|
"keypair",
|
|
58
54
|
"rsa",
|
|
59
55
|
identifier
|
|
60
56
|
], {
|
|
61
|
-
privateKey: await exportJwk(rsaPair
|
|
62
|
-
publicKey: await exportJwk(rsaPair
|
|
57
|
+
privateKey: await exportJwk(rsaPair.privateKey),
|
|
58
|
+
publicKey: await exportJwk(rsaPair.publicKey)
|
|
63
59
|
});
|
|
64
60
|
await ctx.data.kv.set([
|
|
65
61
|
"keypair",
|
|
66
62
|
"ed25519",
|
|
67
63
|
identifier
|
|
68
64
|
], {
|
|
69
|
-
privateKey: await exportJwk(ed25519Pair
|
|
70
|
-
publicKey: await exportJwk(ed25519Pair
|
|
65
|
+
privateKey: await exportJwk(ed25519Pair.privateKey),
|
|
66
|
+
publicKey: await exportJwk(ed25519Pair.publicKey)
|
|
71
67
|
});
|
|
72
|
-
return [rsaPair
|
|
68
|
+
return [rsaPair, ed25519Pair];
|
|
73
69
|
}
|
|
74
|
-
|
|
70
|
+
return [{
|
|
75
71
|
privateKey: await importJwk(rsaPairJson.privateKey, "private"),
|
|
76
72
|
publicKey: await importJwk(rsaPairJson.publicKey, "public")
|
|
77
|
-
}
|
|
78
|
-
const ed25519Pair = {
|
|
73
|
+
}, {
|
|
79
74
|
privateKey: await importJwk(ed25519PairJson.privateKey, "private"),
|
|
80
75
|
publicKey: await importJwk(ed25519PairJson.publicKey, "public")
|
|
81
|
-
};
|
|
82
|
-
return [rsaPair, ed25519Pair];
|
|
76
|
+
}];
|
|
83
77
|
});
|
|
84
78
|
async function getFollowerActors(ctx) {
|
|
85
79
|
const actors = [];
|
|
@@ -93,13 +87,11 @@ async function getFollowerActors(ctx) {
|
|
|
93
87
|
return actors;
|
|
94
88
|
}
|
|
95
89
|
async function dispatchRelayActors(ctx, identifier) {
|
|
96
|
-
if (identifier !==
|
|
97
|
-
|
|
98
|
-
return { items: actors };
|
|
90
|
+
if (identifier !== "relay") return null;
|
|
91
|
+
return { items: await getFollowerActors(ctx) };
|
|
99
92
|
}
|
|
100
93
|
relayBuilder.setFollowersDispatcher("/users/{identifier}/followers", dispatchRelayActors);
|
|
101
94
|
relayBuilder.setFollowingDispatcher("/users/{identifier}/following", dispatchRelayActors);
|
|
102
|
-
|
|
103
95
|
//#endregion
|
|
104
96
|
//#region src/base.ts
|
|
105
97
|
/**
|
|
@@ -112,9 +104,9 @@ var BaseRelay = class {
|
|
|
112
104
|
federationBuilder;
|
|
113
105
|
options;
|
|
114
106
|
federation;
|
|
115
|
-
constructor(options, relayBuilder
|
|
107
|
+
constructor(options, relayBuilder) {
|
|
116
108
|
this.options = options;
|
|
117
|
-
this.federationBuilder = relayBuilder
|
|
109
|
+
this.federationBuilder = relayBuilder;
|
|
118
110
|
}
|
|
119
111
|
async fetch(request) {
|
|
120
112
|
return await (await this.#getFederation()).fetch(request, { contextData: this.options });
|
|
@@ -211,19 +203,15 @@ var BaseRelay = class {
|
|
|
211
203
|
return this.federation;
|
|
212
204
|
}
|
|
213
205
|
async #createContext() {
|
|
214
|
-
|
|
215
|
-
return context;
|
|
206
|
+
return (await this.#getFederation()).createContext(new URL(this.options.origin), this.options);
|
|
216
207
|
}
|
|
217
208
|
async getActorUri() {
|
|
218
|
-
|
|
219
|
-
return context.getActorUri(RELAY_SERVER_ACTOR);
|
|
209
|
+
return (await this.#createContext()).getActorUri(RELAY_SERVER_ACTOR);
|
|
220
210
|
}
|
|
221
211
|
async getSharedInboxUri() {
|
|
222
|
-
|
|
223
|
-
return context.getInboxUri();
|
|
212
|
+
return (await this.#createContext()).getInboxUri();
|
|
224
213
|
}
|
|
225
214
|
};
|
|
226
|
-
|
|
227
215
|
//#endregion
|
|
228
216
|
//#region src/follow.ts
|
|
229
217
|
/**
|
|
@@ -237,8 +225,7 @@ var BaseRelay = class {
|
|
|
237
225
|
async function validateFollowActivity(ctx, follow) {
|
|
238
226
|
if (follow.id == null || follow.objectId == null) return null;
|
|
239
227
|
const parsed = ctx.parseUri(follow.objectId);
|
|
240
|
-
|
|
241
|
-
if (!isPublicFollow && parsed?.type !== "actor") return null;
|
|
228
|
+
if (!(follow.objectId.href === "https://www.w3.org/ns/activitystreams#Public") && parsed?.type !== "actor") return null;
|
|
242
229
|
const follower = await follow.getActor(ctx);
|
|
243
230
|
if (follower == null || follower.id == null || follower.preferredUsername == null || follower.inboxId == null) return null;
|
|
244
231
|
return follower;
|
|
@@ -270,7 +257,7 @@ async function sendFollowResponse(ctx, follow, follower, approved) {
|
|
|
270
257
|
* @param undo The Undo activity to handle
|
|
271
258
|
* @param logger The logger instance to use for warnings
|
|
272
259
|
*/
|
|
273
|
-
async function handleUndoFollow(ctx, undo, logger
|
|
260
|
+
async function handleUndoFollow(ctx, undo, logger) {
|
|
274
261
|
const activity = await undo.getObject({
|
|
275
262
|
crossOrigin: "trust",
|
|
276
263
|
...ctx
|
|
@@ -278,12 +265,11 @@ async function handleUndoFollow(ctx, undo, logger$2) {
|
|
|
278
265
|
if (activity instanceof Follow) {
|
|
279
266
|
if (activity.id == null || activity.actorId == null) return;
|
|
280
267
|
await ctx.data.kv.delete(["follower", activity.actorId.href]);
|
|
281
|
-
} else logger
|
|
268
|
+
} else logger.warn("Unsupported object type ({type}) for Undo activity: {object}", {
|
|
282
269
|
type: activity?.constructor.name,
|
|
283
270
|
object: activity
|
|
284
271
|
});
|
|
285
272
|
}
|
|
286
|
-
|
|
287
273
|
//#endregion
|
|
288
274
|
//#region src/litepub.ts
|
|
289
275
|
const logger$1 = getLogger([
|
|
@@ -318,8 +304,7 @@ var LitePubRelay = class extends BaseRelay {
|
|
|
318
304
|
if (this.federation != null) this.federation.setInboxListeners("/users/{identifier}/inbox", "/inbox").on(Follow, async (ctx, follow) => {
|
|
319
305
|
const follower = await validateFollowActivity(ctx, follow);
|
|
320
306
|
if (!follower || !follower.id) return;
|
|
321
|
-
|
|
322
|
-
if (existingFollow?.state === "pending") return;
|
|
307
|
+
if ((await ctx.data.kv.get(["follower", follower.id.href]))?.state === "pending") return;
|
|
323
308
|
const approved = await this.options.subscriptionHandler(ctx, follower);
|
|
324
309
|
if (approved) {
|
|
325
310
|
await ctx.data.kv.set(["follower", follower.id.href], {
|
|
@@ -356,7 +341,6 @@ var LitePubRelay = class extends BaseRelay {
|
|
|
356
341
|
}).on(Undo, async (ctx, undo) => await handleUndoFollow(ctx, undo, logger$1)).on(Create, async (ctx, create) => await this.#announceToFollowers(ctx, create)).on(Update, async (ctx, update) => await this.#announceToFollowers(ctx, update)).on(Move, async (ctx, move) => await this.#announceToFollowers(ctx, move)).on(Delete, async (ctx, deleteActivity) => await this.#announceToFollowers(ctx, deleteActivity)).on(Announce, async (ctx, announce) => await this.#announceToFollowers(ctx, announce));
|
|
357
342
|
}
|
|
358
343
|
};
|
|
359
|
-
|
|
360
344
|
//#endregion
|
|
361
345
|
//#region src/mastodon.ts
|
|
362
346
|
const logger = getLogger([
|
|
@@ -394,7 +378,6 @@ var MastodonRelay = class extends BaseRelay {
|
|
|
394
378
|
}).on(Undo, async (ctx, undo) => await handleUndoFollow(ctx, undo, logger)).on(Create, async (ctx, create) => await this.#forwardToFollowers(ctx, create)).on(Delete, async (ctx, deleteActivity) => await this.#forwardToFollowers(ctx, deleteActivity)).on(Move, async (ctx, move) => await this.#forwardToFollowers(ctx, move)).on(Update, async (ctx, update) => await this.#forwardToFollowers(ctx, update)).on(Announce, async (ctx, announce) => await this.#forwardToFollowers(ctx, announce));
|
|
395
379
|
}
|
|
396
380
|
};
|
|
397
|
-
|
|
398
381
|
//#endregion
|
|
399
382
|
//#region src/factory.ts
|
|
400
383
|
/**
|
|
@@ -424,6 +407,5 @@ function createRelay(type, options) {
|
|
|
424
407
|
case "litepub": return new LitePubRelay(options, relayBuilder);
|
|
425
408
|
}
|
|
426
409
|
}
|
|
427
|
-
|
|
428
410
|
//#endregion
|
|
429
|
-
export { RELAY_SERVER_ACTOR, createRelay };
|
|
411
|
+
export { RELAY_SERVER_ACTOR, createRelay };
|