Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// SPDX-License-Identifier: 0BSD
type InstanceActorConfig = {
hostname: string;
publicKeyPem: string;
};
const createAcctUri = (hostname: string) => `acct:${hostname}@${hostname}`;
const createId = (hostname: string) => new URL(`https://${hostname}/actor`);
const createPublicKeyId = (hostname: string) => {
const publicKeyId = createId(hostname);
publicKeyId.hash = "main-key";
return publicKeyId;
};
export function createInstanceActor(config: InstanceActorConfig) {
const jrd = createJRD(config.hostname);
const actor = createActor(config);
return {
acctUri: jrd.subject,
id: actor.id,
publicKeyId: actor.publicKey.id,
activityPubHandler,
webfingerHandler,
};
function activityPubHandler() {
const res = Response.json(actor);
res.headers.set("content-type", "application/activity+json");
return res;
}
function webfingerHandler() {
const res = Response.json(jrd);
res.headers.set("content-type", "application/jrd+json; charset=utf-8");
return res;
}
}
function createActor({ hostname, publicKeyPem }: InstanceActorConfig) {
const actorId = createId(hostname);
const publicKeyId = createPublicKeyId(hostname);
return {
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
],
id: actorId.href,
type: "Application",
inbox: `https://${hostname}/actor/inbox`,
outbox: `https://${hostname}/actor/outbox`,
preferredUsername: hostname,
url: actorId.href,
manuallyApprovesFollowers: true,
publicKey: {
id: publicKeyId.href,
owner: actorId.href,
publicKeyPem,
},
endpoints: {
sharedInbox: `https://${hostname}/inbox`,
},
};
}
function createJRD(hostname: string) {
const actorId = createId(hostname);
const subject = createAcctUri(hostname);
return {
subject,
aliases: [actorId.href],
links: [{
rel: "self",
type: "application/activity+json",
href: actorId.href,
}, {
rel: "self",
type: "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
href: actorId.href,
}],
};
}
March 30, 2024