-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.js
130 lines (123 loc) · 3.43 KB
/
service.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
class XError extends Error {}
const Config = {
atscan: "https://api.atscan.net",
defaultPds: "https://bsky.social",
lexicons: [
"com.atproto.admin",
"com.atproto.identity",
"com.atproto.label",
"com.atproto.moderation",
"com.atproto.repo",
"com.atproto.server",
"com.atproto.sync",
"app.bsky.actor",
"app.bsky.embed",
"app.bsky.feed",
"app.bsky.graph",
"app.bsky.notification",
"app.bsky.richtext",
],
};
const router = new Router();
router
// ----------------------------
// START OF ROUTES
// ----------------------------
// route /
// ----------------------------
.get("/", (ctx) => {
ctx.response.redirect("https://github.com/atscan/xrpc.link");
})
// ----------------------------
// route /docs - AT Proto Documentation
// ----------------------------
.get("/docs", (ctx) => {
ctx.response.redirect("https://atproto.com/docs");
})
// ----------------------------
// route /spec - AT Proto Specification
// ----------------------------
.get("/(specs|spec)/:page?", (ctx) => {
ctx.response.redirect(
`https://atproto.com/specs/${ctx.params.page || "atp"}`,
);
})
// ----------------------------
// route /spec - AT Proto Specification
// ----------------------------
.get("/(lex|lexicon|lexicons)/:lexicon?", (ctx) => {
ctx.response.redirect(
`https://atproto.com/lexicons/${
findLex(ctx.params.lexicon).replace(/\./g, "-") || "com-atproto-admin"
}`,
);
})
// ----------------------------
// route /ds/:did - Describe Server
// ----------------------------
.get("/(ds|describeServer)/:pds?", async (ctx) => {
const pds = ctx.params.pds || Config.defaultPds;
return ctx.response.redirect(
`https://${
pds.replace(/^https?:\/\//, "")
}/xrpc/com.atproto.server.describeServer`,
);
})
// ----------------------------
// route /r/:did - (Get) Repo
// ----------------------------
.get("/(r|repo)/:did", async (ctx) => {
const { did, pds } = await params(ctx);
return ctx.response.redirect(
`${pds}/xrpc/com.atproto.sync.getRepo?did=${did}`,
);
})
// ----------------------------
// route /c/:did - Checkout
// ----------------------------
.get("/(c|checkout)/:did", async (ctx) => {
const { did, pds } = await params(ctx);
return ctx.response.redirect(
`${pds}/xrpc/com.atproto.sync.getCheckout?did=${did}`,
);
});
// ----------------------------
// END OF ROUTES
// ----------------------------
function findLex(str) {
return Config.lexicons.find((l) => l.match(new RegExp(str, "i")));
}
async function params(ctx) {
let did = ctx.params.did;
let pds = Config.defaultPds;
if (!did.match(/^did:plc:/)) {
const fd = await didInfo(did);
if (!fd) {
ctx.response.status = 404;
throw new XError("DID not found");
}
did = fd.did;
pds = fd.pds;
}
return { did, pds };
}
async function didInfo(str) {
return fetch([Config.atscan, str].join("/")).then((r) => {
return r.status === 200 ? r.json() : null;
});
}
const app = new Application();
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
console.error(`[${ctx.url}]`);
console.error(err);
ctx.response.body = `${err}`;
}
});
app.use(router.routes());
app.use(router.allowedMethods());
console.log(`Listening at :8764`);
await app.listen({ port: 8764 });