forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
executable file
·41 lines (37 loc) · 1.11 KB
/
index.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
import { createServer } from 'node:http'
import pico from 'picocolors'
const server = createServer(async (req, res) => {
let url = decodeURIComponent(req.url!.slice(1))
let sent = false
try {
let proxy = await fetch(url, {
headers: {
...(req.headers as HeadersInit),
host: new URL(url).host
},
method: req.method
})
res.writeHead(proxy.status, {
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': 'OPTIONS, POST, GET, PUT, DELETE',
'Access-Control-Allow-Origin': '*',
'Content-Type': proxy.headers.get('content-type') ?? 'text/plain'
})
sent = true
res.write(await proxy.text())
res.end()
} catch (e) {
if (e instanceof Error) {
process.stderr.write(pico.red(e.stack) + '\n')
if (!sent) {
res.writeHead(500, { 'Content-Type': 'text/plain' })
res.end('Internal Server Error')
}
} else if (typeof e === 'string') {
process.stderr.write(pico.red(e) + '\n')
}
}
})
server.listen(5284, () => {
process.stderr.write(pico.green('Proxy server running on port 5284\n'))
})