-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.ts
66 lines (60 loc) · 2.06 KB
/
mod.ts
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
// Copyright 2021 TANIGUCHI Masaya. All rights reserved. git.io/mit-license
import * as semver from "https://deno.land/x/[email protected]/mod.ts";
// import 'https://deno.land/x/fetch_event_adapter/listen.ts';
const re_name = "[^/@]+";
const re_range = "[^/@]+";
const re_path = "|/.*";
const re_x = "(?:x/)?";
const re_pathname = new RegExp(
`^/(${re_x})(${re_name})@(${re_range})(${re_path})$`,
);
type FetchTags = (name: string) => Promise<string[]>;
async function fetchTagsFromDenoLand(name: string): Promise<string[]> {
const request = `https://cdn.deno.land/${name}/meta/versions.json`;
try {
const response = await fetch(request);
const json = await response.json() as { versions: string[] };
return json.versions;
} catch {
console.warn(`failed to fetch data from ${request}`);
return [];
}
}
async function redirect(url: string, fetchTags: FetchTags): Promise<string> {
const loose = true;
const _url = new URL(url);
const match = decodeURIComponent(_url.pathname).match(re_pathname);
if (match) {
const [, x, name, range, path] = match;
const tags = (await fetchTags(name))
.filter((tag: string) => {
return semver.valid(tag, loose) && semver.satisfies(tag, range, loose);
}).sort((l, r) => {
return semver.compare(l, r, loose);
});
if (tags.length > 0) {
const tag = tags[tags.length - 1];
return `https://deno.land/${x}${name}@${tag}${path}`;
}
if (range === "latest") {
return `https://deno.land/${x}${name}${path}`;
}
}
return `https://deno.land${_url.pathname}`;
}
export { re_pathname, redirect };
self.addEventListener("fetch", async (event) => {
let dest = "https://github.com/tani/lib.deno.dev";
if (new URL(event.request.url).pathname !== "/") {
dest = await redirect(event.request.url, fetchTagsFromDenoLand);
}
const res = Response.redirect(dest, 302);
event.respondWith(new Response(res.body, {
status: res.status,
statusText: res.statusText,
headers: [
...res.headers,
["Access-Control-Allow-Origin", "*"]
]
}));
});