This repository was archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
89 lines (77 loc) · 2.28 KB
/
index.mjs
File metadata and controls
89 lines (77 loc) · 2.28 KB
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
import fastify from 'fastify';
import { fastifyStatic } from '@fastify/static';
import { createServer } from 'node:http';
import { createBareServer } from '@tomphttp/bare-server-node';
import path, { dirname } from 'node:path';
import createRammerhead from 'rammerhead/src/server/index.js';
import { fileURLToPath } from 'node:url';
import { uvPath } from '@titaniumnetwork-dev/ultraviolet';
const server = createServer();
const bare = createBareServer('/~/bare/');
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const rammerhead = createRammerhead();
const rammerheadScopes = [
'/rammerhead.js',
'/hammerhead.js',
'/transport-worker.js',
'/task.js',
'/iframe-task.js',
'/worker-hammerhead.js',
'/messaging',
'/sessionexists',
'/deletesession',
'/newsession',
'/editsession',
'/needpassword',
'/syncLocalStorage',
'/api/shuffleDict',
'/mainport'
];
const rammerheadSession = /^\/[a-z0-9]{32}/;
function shouldRouteRammerhead(req) {
const url = new URL(req.url, 'http://0.0.0.0');
return (
rammerheadScopes.includes(url.pathname) ||
rammerheadSession.test(url.pathname)
);
}
function routeRammerheadRequest(req, res) {
rammerhead.emit('request', req, res);
}
function routeRammerheadUpgrade(req, socket, head) {
rammerhead.emit('upgrade', req, socket, head);
}
const serverFactory = (handler, opts) => {
return server
.on('request', (req, res) => {
if (bare.shouldRoute(req)) {
bare.routeRequest(req, res);
} else if (shouldRouteRammerhead(req)) {
routeRammerheadRequest(req, res);
} else {
handler(req, res);
}
})
.on('upgrade', (req, socket, head) => {
if (bare.shouldRoute(req)) {
bare.routeUpgrade(req, socket, head);
} else if (shouldRouteRammerhead(req)) {
routeRammerheadUpgrade(req, socket, head);
}
});
};
const app = fastify({ logger: true, serverFactory });
app.register(fastifyStatic, {
root: path.join(__dirname, 'public'),
decorateReply: false
});
app.register(fastifyStatic, {
root: uvPath,
prefix: '/uv/',
decorateReply: false
});
app.listen({
host: '0.0.0.0',
port: 8000
});