-
Notifications
You must be signed in to change notification settings - Fork 0
/
testserver.ts
103 lines (96 loc) · 2.98 KB
/
testserver.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
import { Hono } from 'hono'
import { logger } from 'hono/logger'
import { cors } from 'hono/cors'
import { HTMLRewriter } from 'html-rewriter-wasm'
const app = new Hono()
const baseUrl = Deno.env.get("BASE_URL")||"http://localhost:8000"
app.use('*', logger())
app.use('/api/*', cors())
app.get('/', (c) => {
return c.text('Hello Hono!')
})
app.get("/manifest.json",(c)=>{return c.text("404",404)})
app.get('/*', async (c) => {
// 新しいリクエストを作成してfetch
const request = new Request(c.req.path.slice(1), {
method: c.req.raw.method,
headers: c.req.raw.headers,
body: c.req.raw.body,
});
const _res = await fetch(request);
const res = new Response(_res.body, _res);
const redirectDetect = res.headers.get('Location');
if (redirectDetect && redirectDetect.includes(baseUrl)) {
res.headers.set('Location', redirectDetect.replace(baseUrl, ''));
}
const decoder = new TextDecoder();
const encoder = new TextEncoder();
let output = "";
const rewriter = new HTMLRewriter((outputChunk) => {
output += decoder.decode(outputChunk);
});
rewriter.on("a",{
element(element) {
let newURL ="";
const old = element.getAttribute("href") || "#";
if (old.startsWith("http://")||old.startsWith("https://")) {
newURL = baseUrl+"/"+old;
} else {
newURL = baseUrl+"/"+new URL(old, c.req.path.slice(1)).href;
}
element.removeAttribute("href");
element.setAttribute("href", newURL);
},
})
.on("img",{
element(element) {
let newURL ="";
const old = element.getAttribute("src") || "#";
if (old.startsWith("http://")||old.startsWith("https://")) {
newURL = baseUrl+"/"+old;
} else {
newURL = baseUrl+"/"+new URL(old, c.req.path.slice(1)).href;
}
element.removeAttribute("src");
element.setAttribute("src", newURL);
},
})
.on("link",{
element(element) {
let newURL ="";
const old = element.getAttribute("href") || "#";
if (old.startsWith("http://")||old.startsWith("https://")) {
newURL = baseUrl+"/"+old;
} else {
newURL = baseUrl+"/"+new URL(old, c.req.path.slice(1)).href;
}
element.removeAttribute("href");
element.setAttribute("href", newURL);
},
})
.on("script",{
element(element) {
let newURL ="";
const old = element.getAttribute("src") || "#";
if (old.startsWith("http://")||old.startsWith("https://")) {
newURL = baseUrl+"/"+old;
} else {
newURL = baseUrl+"/"+new URL(old, c.req.path.slice(1)).href;
}
element.removeAttribute("src");
element.setAttribute("src", newURL);
},
})
try {
await rewriter.write(encoder.encode(await res.text()));
await rewriter.end();
res.headers.set('access-control-allow-origin', '*');
res.headers.forEach((value, key) => {
c.header(key, value);
})
return c.body(output);
} finally {
rewriter.free(); // Remember to free memory
}
});
Deno.serve(app.fetch)