Public
HTTP (deprecated)
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
83
84
85
86
87
88
import { decodeBase64Url, encodeBase64Url } from "https://deno.land/std@0.208.0/encoding/base64url.ts";
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo?v=6";
import { getCrateInfo } from "https://esm.town/v/vladimyr/crates_io?v=14";
import { getPkgInfo, HTTPError } from "https://esm.town/v/vladimyr/npmRegistry?v=23";
import { Hono } from "npm:hono@3.11.2";
const { author, name } = extractValInfo(import.meta.url);
const BASE_URL = `https://${author}-${name}.web.val.run`;
const app = new Hono();
app.get("/-/:params{x;[^/]+}/:filename", (c) => {
const params = Object.fromEntries(new URLSearchParams(c.req.param("params").slice(2).replace(/;/g, "&")));
try {
const redirectUrl = new TextDecoder().decode(decodeBase64Url(params.url));
return c.redirect(new URL(redirectUrl).href);
} catch {
return c.text("400 Bad Request", 400);
}
});
app.get("/crates/:name{[^@\/]+}", async (c) => {
const { name } = c.req.param();
try {
const crateInfo = await getCrateInfo(name);
if (!crateInfo) return c.notFound();
const redirectUrl = encodeBase64Url(new TextEncoder().encode(crateInfo.downloadUrl));
const params = new URLSearchParams({ url: redirectUrl }).toString().replace(/&/, ";");
return c.redirect(`https://dezip.org/${BASE_URL}/-/x;${params}/${name}-${crateInfo.version}.tgz`);
} catch (err) {
if (err instanceof HTTPError) {
const resp = err.response;
return c.text(`${resp.status} ${resp.statusText}`, resp.status);
}
}
});
app.get("/crates/:nameWithVersion{[^@]+@[^@\/]+}", async (c) => {
const { nameWithVersion } = c.req.param();
const [name, version] = nameWithVersion.split("@");
try {
const crateInfo = await getCrateInfo(name, version);
if (!crateInfo) return c.notFound();
const encodedUrl = encodeBase64Url(new TextEncoder().encode(crateInfo.downloadUrl));
const params = new URLSearchParams({ url: encodedUrl }).toString().replace(/&/, ";");
return c.redirect(`https://dezip.org/${BASE_URL}/-/x;${params}/${name}-${crateInfo.version}.tgz`);
} catch (err) {
if (err instanceof HTTPError) {
const resp = err.response;
return c.text(`${resp.status} ${resp.statusText}`, resp.status);
}
}
});
app.get("/npm/:name{[^@\/]+}", (c) => {
const { name } = c.req.param();
return c.redirect(`/npm/${name}@latest`);
});
app.get("/npm/:scope{@[^@\/]+}/:name{[^@\/]+}", (c) => {
const { scope, name } = c.req.param();
return c.redirect(`/npm/${scope}/${name}@latest`);
});
app.get("/npm/:nameWithVersion{[^@]+@[^@\/]+}", async (c) => {
const { nameWithVersion } = c.req.param();
const [name, version] = nameWithVersion.split("@");
try {
const pkgInfo = await getPkgInfo(name, version);
if (!pkgInfo) return c.notFound();
return c.redirect(`https://dezip.org/${pkgInfo.dist.tarball}`);
} catch (err) {
if (err instanceof HTTPError) {
const resp = err.response;
return c.text(`${resp.status} ${resp.statusText}`, resp.status);
}
}
});
app.get("/npm/:scope{@[^@\/]+}/:nameWithVersion{[^@\/]+@[^@\/]+}", async (c) => {
const { scope, nameWithVersion } = c.req.param();
const [name, version] = nameWithVersion.split("@");
try {
const pkgInfo = await getPkgInfo(`${scope}/${name}`, version);
if (!pkgInfo) return c.notFound();
return c.redirect(`https://dezip.org/${pkgInfo.dist.tarball}`);
} catch (err) {
if (err instanceof HTTPError) {
const resp = err.response;
return c.text(`${resp.status} ${resp.statusText}`, resp.status);
}
}
});
export default app.fetch;
vladimyr-unpkg.web.val.run
December 6, 2023