Repository navigation
Expand file tree
/
Copy pathhandler.ts
More file actions
73 lines (69 loc) · 2.34 KB
/
Copy pathhandler.ts
File metadata and controls
73 lines (69 loc) · 2.34 KB
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
import type { Link, ResourceDescriptor } from "@fedify/webfinger";
import { RouterError } from "@fedify/uri-template";
import type { NodeInfoDispatcher } from "../federation/callback.ts";
import type { RequestContext } from "../federation/context.ts";
import { nodeInfoToJson } from "./types.ts";
/**
* Parameters for {@link handleNodeInfo}.
*/
export interface NodeInfoHandlerParameters<TContextData> {
/**
* The request context.
*/
context: RequestContext<TContextData>;
/**
* The callback for dispatching the NodeInfo.
*/
nodeInfoDispatcher: NodeInfoDispatcher<TContextData>;
}
/**
* Handles a NodeInfo request. You would not typically call this function
* directly, but instead use {@link Federation.handle} method.
* @param request The NodeInfo request to handle.
* @param parameters The parameters for handling the request.
* @returns The response to the request.
*/
export async function handleNodeInfo<TContextData>(
_request: Request,
{ context, nodeInfoDispatcher }: NodeInfoHandlerParameters<TContextData>,
): Promise<Response> {
const promise = nodeInfoDispatcher(context);
const nodeInfo = promise instanceof Promise ? await promise : promise;
const json = nodeInfoToJson(nodeInfo);
return new Response(JSON.stringify(json), {
headers: {
"Content-Type": "application/json;" +
' profile="http://nodeinfo.diaspora.software/ns/schema/2.1#"',
},
});
}
/**
* Handles a request to `/.well-known/nodeinfo`. You would not typically call
* this function directly, but instead use {@link Federation.handle} method.
* @param request The request to handle.
* @param context The request context.
* @returns The response to the request.
*/
export function handleNodeInfoJrd<TContextData>(
_request: Request,
context: RequestContext<TContextData>,
): Promise<Response> {
const links: Link[] = [];
try {
links.push({
rel: "http://nodeinfo.diaspora.software/ns/schema/2.1",
href: context.getNodeInfoUri().href,
type: "application/json;" +
' profile="http://nodeinfo.diaspora.software/ns/schema/2.1#"',
});
} catch (e) {
if (!(e instanceof RouterError)) throw e;
}
const jrd: ResourceDescriptor = { links };
const response = new Response(JSON.stringify(jrd), {
headers: {
"Content-Type": "application/jrd+json",
},
});
return Promise.resolve(response);
}