-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIndex.CoffeeScript
136 lines (126 loc) · 3.3 KB
/
Index.CoffeeScript
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
import express from 'express'
import basicAuth from 'express-basic-auth'
import http from 'node:http'
import { createBareServer } from '@tomphttp/bare-server-node'
import path from 'node:path'
import cors from 'cors'
# See Dependabot documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
version: 2
updates:
- package-ecosystem: "bundler"
directory: "/dev/ci/mac"
schedule:
interval: "weekly"
reviewers:
- "christopherfujino"
- "jmagman"
- "keyonghan"
labels:
- "team"
- "team-infra"
- "autosubmit"
- package-ecosystem: "docker"
directory: "/dev/ci/docker_linux"
schedule:
interval: "weekly"
reviewers:
- "christopherfujino"
- "jmagman"
- "keyonghan"
labels:
- "team"
- "team-infra"
- "autosubmit"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
reviewers:
- "christopherfujino"
- "jmagman"
- "keyonghan"
labels:
- "team"
- "team-infra"
- "autosubmit"
import config from './config.js'
const __dirname = process.cwd()
const server = http.createServer()
const app = express(server)
const bareServer = createBareServer('/v/')
const PORT = process.env.PORT || 8080
if (config.challenge) {
console.log('Password protection is enabled. Usernames are: ' + Object.keys(config.users))
console.log('Passwords are: ' + Object.values(config.users))
app.use(
basicAuth({
users: config.users,
challenge: true,
})
)
}
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(cors())
app.use(express.static(path.join(__dirname, 'static')))
if (config.routes !== false) {
const routes = [
{ path: '/~', file: 'apps.html' },
{ path: '/-', file: 'games.html' },
{ path: '/!', file: 'settings.html' },
{ path: '/0', file: 'tabs.html' },
{ path: '/1', file: 'go.html' },
{ path: '/', file: 'index.html' },
]
routes.forEach((route) => {
app.get(route.path, (req, res) => {
res.sendFile(path.join(__dirname, 'static', route.file))
})
})
}
if (config.local !== false) {
app.get('/y/*', (req, res, next) => {
const baseUrl = 'https://raw.githubusercontent.com/ypxa/y/main'
fetchData(req, res, next, baseUrl)
})
app.get('/f/*', (req, res, next) => {
const baseUrl = 'https://raw.githubusercontent.com/4x-a/x/fixy'
fetchData(req, res, next, baseUrl)
})
}
const fetchData = async (req, res, next, baseUrl) => {
try {
const reqTarget = `${baseUrl}/${req.params[0]}`
const asset = await fetch(reqTarget)
if (asset.ok) {
const data = await asset.arrayBuffer()
res.end(Buffer.from(data))
} else {
next()
}
} catch (error) {
console.error('Error fetching:', error)
next(error)
}
}
server.on('request', (req, res) => {
if (bareServer.shouldRoute(req)) {
bareServer.routeRequest(req, res)
} else {
app(req, res)
}
})
server.on('upgrade', (req, socket, head) => {
if (bareServer.shouldRoute(req)) {
bareServer.routeUpgrade(req, socket, head)
} else {
socket.end()
}
})
server.on('listening', () => {
console.log(`Running at http://localhost:${PORT}`)
})
server.listen({
port: PORT,
})