-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
62 lines (52 loc) · 2.17 KB
/
Copy pathserver.mjs
File metadata and controls
62 lines (52 loc) · 2.17 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
// Custom Syncwave server: Next.js (UI + API routes) + Socket.io (realtime sync
// & chat) + a raw /audio route (range-served, yt-dlp-backed) on one HTTP server,
// so the whole app is a single process / single container.
import { createServer } from "http";
import next from "next";
import { Server as IOServer } from "socket.io";
import { registerRoomHandlers } from "./lib/rooms.mjs";
import { handleAudioRequest, sweepCache } from "./lib/resolver.mjs";
import { ffmpegMissing } from "./lib/ffmpeg.mjs";
import { jsRuntimeMissing } from "./lib/jsruntime.mjs";
import { clearPublicUrl } from "./lib/publicurl.mjs";
const dev = process.env.NODE_ENV !== "production";
const port = parseInt(process.env.PORT || "3000", 10);
const app = next({ dev });
const handle = app.getRequestHandler();
await app.prepare();
const server = createServer((req, res) => {
if (req.url && req.url.startsWith("/audio/")) {
handleAudioRequest(req, res);
return;
}
handle(req, res);
});
// Unset means "reflect the requesting origin", which is what you want on a LAN
// where the host is reached by IP, hostname, and tailnet name interchangeably.
const publicUrl = process.env.PUBLIC_URL || "";
const io = new IOServer(server, {
cors: { origin: publicUrl || true },
});
registerRoomHandlers(io);
// Any tunnel from a previous run died with it. Drop the address now so nobody
// is handed a dead link if that process was killed before it could clean up;
// the launcher writes a fresh one once its tunnel is actually serving.
clearPublicUrl();
// Evict cache files not played within 72h — sweep at boot and hourly.
sweepCache();
setInterval(sweepCache, 60 * 60 * 1000);
server.listen(port, () => {
console.log(`> Syncwave ready on http://localhost:${port} (${dev ? "dev" : "prod"})`);
if (ffmpegMissing()) {
console.warn(
"! ffmpeg was not found. Tracks will fail to convert.\n" +
" Install it and restart, or run `npm install` to fetch the bundled copy."
);
}
if (jsRuntimeMissing()) {
console.warn(
"! No JavaScript runtime (deno) found. YouTube will bot-check this server\n" +
" and tracks will fail. Run `npm install` to fetch the bundled copy."
);
}
});