/
static.ts
144 lines (129 loc) · 2.97 KB
/
static.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { Row } from "../types.d.ts";
import { contentType, extname, Status, STATUS_TEXT } from "./deps.ts";
export function handleStaticFile(
reqUrl: string,
staticURL: string,
maxAge: number,
cache: Row,
) {
const staticID = "static" + reqUrl;
const extID = "ext" + staticID + reqUrl;
let path;
let extName = "";
if (cache[staticID]) {
path = cache[staticID];
} else {
path = getPathUrl(reqUrl, staticURL);
cache[staticID] = path;
}
if (cache[extID]) {
extName = cache[extID];
} else {
extName = extname(path);
cache[extID] = extName;
}
const txtExt = [
".js",
".css",
".html",
".txt",
".xml",
".svg",
".md",
".ts",
"",
];
const r = txtExt.find((val) => val === extName);
if (r || r === "") {
return handleTextFile(extName, maxAge, path, cache, reqUrl);
}
return handleNonText(path, maxAge);
}
async function handleNonText(
path: string,
maxAge: number,
) {
let file;
try {
file = await Deno.open(`.${path}`, { read: true });
} catch (err) {
console.error(err);
return new Response(STATUS_TEXT[Status.NotFound], {
status: Status.NotFound,
});
}
const ct = contentType(extname(path)) || "application/octet-stream";
return new Response(file.readable, {
headers: {
"Content-Type": ct,
"Cache-Control": `max-age=${maxAge}`,
},
});
}
async function handleTextFile(
ext: string,
maxAge: number,
path: string,
cache: Row,
url: string,
) {
let text;
const txtID = "txt" + url;
if (cache[txtID]) {
text = cache[txtID];
} else {
try {
text = await Deno.readTextFile(`.${path}`);
cache[txtID] = text;
} catch {
return handleIndex(path, url, cache);
}
}
return new Response(text, {
headers: {
"Content-Type": <string> contentType(ext),
"Cache-Control": `max-age=${maxAge}`,
},
});
}
async function handleIndex(path: string, url: string, cache: Row) {
let text;
const idxID = "index" + url;
if (cache[idxID]) {
text = cache[idxID];
if (text === STATUS_TEXT[Status.NotFound]) {
return new Response(text, {
status: Status.NotFound,
});
}
} else {
try {
path = path === "/" ? "" : path;
text = await Deno.readTextFile(`.${path}/index.html`);
cache[idxID] = text;
} catch (err) {
console.error(err);
text = STATUS_TEXT[Status.NotFound];
cache[idxID] = text;
return new Response(text, {
status: Status.NotFound,
});
}
}
return new Response(text, {
headers: {
"Content-Type": "text/html; charset=utf-8",
},
});
}
function getPathUrl(url: string, staticURL: string) {
const res = <RegExpMatchArray> url.match(/^https?:\/\/[^/]+/);
const [baseUrl] = res;
if (staticURL !== "/") {
const p = `${staticURL}/:file*`;
const pattern = new URLPattern(p, baseUrl);
const r = pattern.test(url);
if (!r) return "";
}
return url.substring(baseUrl.length);
}