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
import { readGlob } from "https://esm.town/v/omgwtfbrblolttyl/readGlob";
import { tokenAllows } from "https://esm.town/v/omgwtfbrblolttyl/tokenAllows";
export const readGlobEndpoint = async (
req: Request,
res: Response,
) => {
if (req.method === "OPTIONS") {
const res = new Response("", {
status: 200,
headers: {
"Allow": "GET",
"Access-Control-Allow-Orgin": "*",
},
});
return res;
}
if (req.method !== "GET") {
return new Response("Not found", { status: 404 });
}
const token = req.headers["token"];
if (!token) {
console.log(req.headers);
return new Response("Missing token header v2", { status: 401 });
}
const glob = new URL(req.url).searchParams["glob"];
if (!glob) {
return new Response("Missing glob pattern", { status: 400 });
}
if (typeof glob !== "string") {
return new Response("Bad pattern", { status: 400 });
}
const { allowed, error } = await tokenAllows(
token,
glob,
"read",
);
if (!allowed) {
return new Response(error.msg, { status: error.code });
}
const results = await readGlob(glob);
return new Response(JSON.stringify(results), { status: 200 });
};