-
Notifications
You must be signed in to change notification settings - Fork 1
/
dev-server.mjs
231 lines (202 loc) · 5.67 KB
/
dev-server.mjs
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 path from 'path';
import http from 'http';
import https from 'https';
import fs from 'fs';
import url from 'url';
import child_process from 'child_process';
import express from 'express';
import * as vite from 'vite';
import {
AiServer,
} from './src/servers/ai-server.js';
import {
YoutubeServer,
} from './src/servers/youtube-server.js';
//
const isProduction = process.env.NODE_ENV === 'production';
const vercelJson = JSON.parse(fs.readFileSync('./vercel.json', 'utf8'));
const SERVER_NAME = 'local.webaverse.com';
const SERVER_PORT = parseInt(process.env.PORT, 10) || 9999;
const MULTIPLAYER_PORT = 2222;
//
const aiServer = new AiServer();
const youtubeServer = new YoutubeServer();
//
/* class DatabaseServer {
constructor() {
const cp = child_process.spawn(path.join(
'target',
'release',
'qdrant',
), [], {
cwd: path.join(
'bin',
'qdrant',
),
});
cp.stdout.pipe(process.stdout);
cp.stderr.pipe(process.stderr);
cp.on('error', err => {
console.warn(err.stack);
});
this.cp = cp;
}
destroy() {
this.cp.kill();
}
}
const databaseServer = new DatabaseServer();
process.on('exit', () => {
databaseServer.destroy();
}); */
//
/* class MultiplayerServer {
// You can load the multiplayer-do example app to check that the server is running: http://127.0.0.1:2222/
constructor() {
const dirname = path.dirname(import.meta.url.replace(/^file:\/\//, ''));
const multiplayerPath = path.join(dirname, 'packages', 'multiplayer-do');
const wranglerPath = path.join(dirname, 'node_modules', 'wrangler');
const cp = child_process.spawn(
process.argv[0],
[wranglerPath, 'dev', '-l', '--port', MULTIPLAYER_PORT + ''],
{
cwd: multiplayerPath,
env: {
...process.env,
PORT: MULTIPLAYER_PORT,
},
}
);
cp.stdout.pipe(process.stdout);
cp.stderr.pipe(process.stderr);
cp.on('error', err => {
console.warn(err.stack);
});
this.cp = cp;
}
destroy() {
this.cp.kill();
}
}
const multiplayerServer = new MultiplayerServer();
process.on('exit', () => {
multiplayerServer.destroy();
}); */
//
const _tryReadFile = p => {
try {
return fs.readFileSync(p);
} catch(err) {
// console.warn(err);
return null;
}
};
// use import.meta to get the base directory
let baseDir = path.join(decodeURI(import.meta.url).replace('file://', ''), '..');
baseDir = path.normalize(baseDir);
const certs = {
key: _tryReadFile(path.join(baseDir, './certs/privkey.pem')) ||
_tryReadFile(path.join(baseDir, './certs-local/privkey.pem')),
cert: _tryReadFile(path.join(baseDir, './certs/fullchain.pem')) ||
_tryReadFile(path.join(baseDir, './certs-local/fullchain.pem')),
};
const tmpDir = `/tmp/webaverse-dev-server`;
fs.mkdirSync(tmpDir, {
recursive: true,
});
//
const {headers: headerSpecs} = vercelJson;
const headerSpec0 = headerSpecs[0];
const {headers} = headerSpec0;
const _setHeaders = res => {
for (const {key, value} of headers) {
res.setHeader(key, value);
}
};
//
const _proxyTmp = (req, res) => {
const o = url.parse(req.url);
const p = path.join(tmpDir, o.path.replace(/^\/tmp\//, ''));
// console.log('got tmp request', req.method, req.url, p);
if (req.method === 'GET') {
const rs = fs.createReadStream(p);
rs.on('error', err => {
console.warn(err);
res.statusCode = 500;
res.end(err.stack);
});
rs.pipe(res);
} else if (['PUT', 'POST'].includes(req.method)) {
const ws = fs.createWriteStream(p);
ws.on('error', err => {
console.warn(err);
res.statusCode = 500;
res.end(err.stack);
});
ws.on('finish', () => {
res.end();
});
req.pipe(ws);
} else if (req.method === 'OPTIONS') {
res.end();
} else {
res.statusCode = 400;
res.end('not implemented');
}
};
// main
(async () => {
const app = express();
app.all('*', async (req, res, next) => {
_setHeaders(res);
if (req.url.startsWith('/tmp/')) {
_proxyTmp(req, res);
} else if ([
'/api/ai/',
'/api/image-ai/',
].some(prefix => req.url.startsWith(prefix))) {
await aiServer.handleRequest(req, res);
} else if ([
'/api/youtube/',
].some(prefix => req.url.startsWith(prefix))) {
await youtubeServer.handleRequest(req, res);
} else {
next();
}
});
const isHttps = !process.env.HTTP_ONLY && (!!certs.key && !!certs.cert);
// const wsPort = SERVER_PORT + 1;
const _makeHttpServer = () => isHttps ? https.createServer(certs, app) : http.createServer(app);
const httpServer = _makeHttpServer();
const viteServer = await vite.createServer({
mode: isProduction ? 'production' : 'development',
// root: process.cwd(),
server: {
middlewareMode: true,
// force: true,
hmr: {
server: httpServer,
port: SERVER_PORT,
// overlay: false,
},
},
// appType: 'custom',
});
app.use(viteServer.middlewares);
await new Promise((accept, reject) => {
httpServer.listen(SERVER_PORT, '0.0.0.0', () => {
accept();
});
httpServer.on('error', reject);
});
// console.log('pid', process.pid);
console.log(` > Local ready: http${isHttps ? 's' : ''}://${SERVER_NAME}:${SERVER_PORT}/`);
})();
process.on('disconnect', function() {
console.log('dev-server parent exited')
process.exit();
});
process.on('SIGINT', function() {
console.log('dev-server SIGINT')
process.exit();
});