This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
231 lines (188 loc) · 5.71 KB
/
server.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { ServerResponse, Server } from 'http'
import { URL } from 'url'
import path from 'path'
import fs from 'fs'
// import * as queryString from 'query-string'
// import ReziDB from 'rezidb'
// @ts-ignore
import dotenv from 'dotenv'
import fetch from 'cross-fetch'
import { HttpRouter } from './Router'
// Database
// const db = new ReziDB({
// name: 'aspkg-db-1',
// path: './database',
// cluster: false,
// cache: { maxSize: 1000 },
// })
//db.clear()
// Import .env
dotenv.config({
path: './config.env',
})
// Get secrets
const clientId = process.env.id
const clientSecret = process.env.secret
console.log('env vars:', clientId, clientSecret)
const server = new Server()
server.listen(3000, () => console.log('listening on http://localhost:3000'))
const router = new HttpRouter()
router.listen(server)
router.get('/login', (url, req, res) => {
redirect(res, `https://github.com/login/oauth/authorize?client_id=${clientId}`)
})
router.get('/api-login', async (url, req, res) => {
const code = url.searchParams.get('code')
const token = url.searchParams.get('token')
if (code && !token) {
const body = {
client_id: clientId,
client_secret: clientSecret,
code,
}
const headers = {
'Content-Type': 'application/json',
}
const url = `https://github.com/login/oauth/access_token`
// const resp = await axios.post(url, body, opts)
const resp = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(body),
})
// TODO undici-fetch does not support response.formData() yet
// const data = await resp.formData()
// const token = data.get('access_token')
const data = await resp.text()
const token = data
.split('&')
.find((s) => s.includes('access_token'))!
.split('=')[1]
// This works too:
// const origin = `http://${req.headers.host}`
// const token = new URL(req.url!, origin).searchParams.get('access_token')
setCookie(res, 'token', token)
}
redirect(res, 'http://localhost:3000')
})
router.get('/api-get', (url, req, res) => {
const pkgName = url.searchParams.get('name')
if (!pkgName) {
res.writeHead(200, { 'Content-Type': mimeTypes['.json'] })
return res.end('{}')
}
// TODO
// db.get(pkgName).then(result => {
// res.writeHead(200, { 'Content-Type': mimeTypes['.json'] })
// res.end(JSON.stringify(result) || '{}')
// })
})
// catch-all (at the end, so other handlers have the oppotunity to handle routes first).
router.get('*', (url, req, res) => {
// IDEA: Make static serving into a plugin (similar to how fastify-static is a plugin) instead of inlining it all here.
let filePath = '.' + url.pathname
// Default serving of index.html files.
if (filePath.endsWith('/')) filePath += 'index.html'
const extname = String(path.extname(filePath)).toLowerCase()
// The default mime type.
let contentType = 'application/octet-stream'
if (isSupportedExtension(extname)) contentType = mimeTypes[extname]
fs.readFile(filePath, function (error, content) {
if (error) {
if (error.code == 'ENOENT') {
// TODO, client-side 404 page.
// fs.readFile('./404.html', function (error, content) {
// res.writeHead(404, { 'Content-Type': 'text/html' })
// res.end(content, 'utf-8')
// })
res.writeHead(404, { 'Content-Type': 'text/html' })
res.end('<h1>404 not found</h1>', 'utf-8')
} else {
res.writeHead(500)
res.end('500 error: ' + error.code + ' ..\n')
}
} else {
res.writeHead(200, { 'Content-Type': contentType })
res.end(content, 'utf-8')
}
})
})
function redirect(res: ServerResponse, url: string): void {
res.statusCode = 302
res.setHeader('Location', url)
res.end()
}
function setCookie(res: ServerResponse, key: string, value: string): void {
res.setHeader('Set-Cookie', key + '=' + value)
}
//// old code below /////////////////////////////////////////////////
// // Create server
// const app = fastify()
// app.register(require('fastify-static'), {
// root: __dirname,
// prefix: '/',
// })
// console.log(process.env.cookieSecret)
// // Set cookie handler
// app.register(require('fastify-cookie'), {
// secret: process.env.cookieSecret,
// })
// app.register(require('fastify-cors'), {
// // put your options here
// })
// // Package Searching
// app.get('/search', async (req, res) => {
// const query = req.query['query']
// if (!query) {
// // Send an empty list of results
// res.send(JSON.stringify({}))
// return
// }
// res.send(await db.search(req.query['query']))
// })
// async function logout(req, res) {
// // Erasing token logs the user out.
// res.clearCookie('token')
// res.send('Logged out.')
// }
// app.get('/logout', logout)
// app.get('/api-logout', logout)
// app.post('/api-publish', async (req, res) => {
// const pkg = req.body
// console.log('Body: ', pkg)
// await db.set(pkg['name'].toString(), JSON.stringify(pkg))
// res.code(200)
// res.send('Published')
// })
// app.get('/api-search', async (req, res) => {
// if (!req.query['query']) res.send([])
// res.send(await db.search(req.query['query']), req.query.limit || 5)
// })
// app.get('/api-list', async (req, res) => {
// res.send(await db.toJSON())
// })
// app.addHook('onError', (req, res, err, next) => {
// console.log(err)
// next()
// })
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.wav': 'audio/wav',
'.mp4': 'video/mp4',
'.woff': 'application/font-woff',
'.ttf': 'application/font-ttf',
'.eot': 'application/vnd.ms-fontobject',
'.otf': 'application/font-otf',
'.wasm': 'application/wasm',
// ... add whatever we need ...
}
function isSupportedExtension(ext: string): ext is keyof typeof mimeTypes {
return ext in mimeTypes
}