Readme

Run a Github Gist

Usage

  1. Fork this val
  2. Create a Gist on Github, containing a javascript module
  • the module must have either exactly one export or a default export
  • the export must be a function that accept a request and return a response
  1. In the gist url, replace gist.github.com/<username> by <username>-rungist.web.val.run

For security reasons, only gists with a github username matching your val username are allowed to run on your account.

Example

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
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=42";
export default async function (req: Request) {
const url = new URL(req.url);
const [, id, action] = url.pathname.split("/");
if (!id) {
return new Response("Invalid Request", { status: 400 });
}
const { author, url: valUrl } = extractValInfo(import.meta.url);
if (action == "gist") {
return Response.redirect(`https://gist.github.com/${author}/${id}`);
}
if (action == "val") {
return Response.redirect(valUrl);
}
if (action == "edit") {
return Response.redirect(`https://gist.github.com/${author}/${id}/edit`);
}
if (action == "raw") {
Response.redirect(`https://gist.github.com/${author}/${id}/raw`);
}
const gist = await fetchJSON(`https://api.github.com/gists/${id}`);
if (gist.owner.login != author) {
return new Response("Unauthorized", {
status: 401,
});
}
const files = Object.values(gist.files) as any[];
if (files.length > 1) {
return new Response("Gist should only contain one file", {
status: 422,
});
}
const module = await import(files[0].raw_url);
if (module.default) {
return module.default(req);
}
const exports = Object.values(module) as any[];
if (exports.length > 0) {
return new Response("Gists should only contain one export", {
status: 422,
});
}
return exports[0](req);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
pomdtr-rungist.web.val.run
v18
December 25, 2023