forked from Nam3IsTak3n/Metallic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
127 lines (106 loc) · 3.12 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
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
import { createServer } from 'node:http';
import express from 'express';
import fs from "fs";
import { execSync } from "child_process";
import chalk from "chalk";
import path from 'path';
import { createBareServer } from '@tomphttp/bare-server-node';
//@ts-ignore
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
import wisp from "wisp-server-node";
import { Request, Response } from "express";
//@ts-ignore
import { Socket, Head } from "ws";
import createRammerhead from 'rammerhead/src/server/index.js';
import pages from "./src/pages.json";
import themes from "./src/themes.json";
const __dirname = path.resolve();
if (!fs.existsSync("build")) {
console.log("No build found. Building...");
execSync("npm run build");
console.log("Built!");
}
const port = process.env.PORT || 8080;
const rh = 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"
];
const rammerheadSession = /^\/[a-z0-9]{32}/;
function shouldRouteRh(req: Request) {
const url = new URL(req.url, 'http://0.0.0.0');
return (
rammerheadScopes.includes(url.pathname) ||
rammerheadSession.test(url.pathname)
);
}
function routeRhRequest(req: Request, res: Response) {
rh.emit("request", req, res);
}
function routeRhUpgrade(req: Request, socket: Socket, head: Head) {
rh.emit("upgrade", req, socket, head);
}
const app = express();
app.use(express.static("build"));
app.use("/epoxy/", express.static(epoxyPath));
app.use((req: Request, res: Response) => {
if (pages.includes(req.url)) {
return res.sendFile(__dirname + "/build/index.html");
} else {
return res.status(404).sendFile(__dirname + "/build/index.html");
}
});
const server = createServer();
const bare = createBareServer('/bare/');
server.on('request', (req: Request, res: Response) => {
if (bare.shouldRoute(req)) {
bare.routeRequest(req, res);
} else if (shouldRouteRh(req)) {
routeRhRequest(req, res);
} else {
app(req, res);
}
});
server.on('upgrade', (req: Request, socket: Socket, head: Head) => {
if (req.url && req.url.endsWith("/wisp/")) {
wisp.routeRequest(req, socket, head);
} else if (bare.shouldRoute(req)) {
bare.routeUpgrade(req, socket, head);
} else if (shouldRouteRh(req)) {
routeRhUpgrade(req, socket, head);
} else {
socket.end();
}
});
server.on('listening', () => {
const theme = chalk.hex(themes.filter((theme) => theme.id = "default")[0].theme.primary);
console.log(`${chalk.bold(theme('Metallic'))}`)
console.log(
`- ${chalk.bold('Local:')} http://localhost:${port}`
);
if (process.env.REPL_SLUG && process.env.REPL_OWNER) {
console.log(
`- ${chalk.bold('Replit:')} https://${process.env.REPL_SLUG}.${process.env.REPL_OWNER}.repl.co`
);
}
if (process.env.HOSTNAME && process.env.GITPOD_WORKSPACE_CLUSTER_HOST) {
console.log(
`- ${chalk.bold('Gitpod:')} https://${port}-${process.env.HOSTNAME}.${process.env.GITPOD_WORKSPACE_CLUSTER_HOST}`
);
}
});
server.listen({
port: port
});