-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
111 lines (95 loc) · 4.09 KB
/
Copy pathserver.js
File metadata and controls
111 lines (95 loc) · 4.09 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { createServer } from "node:http";
import { createReadStream, existsSync, statSync } from "node:fs";
import { extname, join, resolve } from "node:path";
import { Readable } from "node:stream";
import { fileURLToPath } from "node:url";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
const publicDir = join(__dirname, "public");
const port = Number.parseInt(process.env.PORT ?? "5177", 10);
const ffmpegProxyPrefix = "/vendor/ffmpeg/";
const ffmpegPackages = new Map([
["ffmpeg", { name: "@ffmpeg/ffmpeg", versions: new Set(["0.12.10"]) }],
["util", { name: "@ffmpeg/util", versions: new Set(["0.12.1"]) }],
["core", { name: "@ffmpeg/core", versions: new Set(["0.12.10"]) }]
]);
const types = new Map([
[".html", "text/html; charset=utf-8"],
[".css", "text/css; charset=utf-8"],
[".js", "text/javascript; charset=utf-8"],
[".json", "application/json; charset=utf-8"],
[".mjs", "text/javascript; charset=utf-8"],
[".txt", "text/plain; charset=utf-8"],
[".webmanifest", "application/manifest+json; charset=utf-8"],
[".xml", "application/xml; charset=utf-8"],
[".svg", "image/svg+xml"],
[".wasm", "application/wasm"]
]);
async function proxyFfmpegAsset(pathname, response) {
if (!pathname.startsWith(ffmpegProxyPrefix)) return false;
const localPath = resolve(publicDir, decodeURIComponent(pathname).replace(/^\/+/, ""));
if (localPath.startsWith(resolve(publicDir)) && existsSync(localPath)) {
return false;
}
const [packageKey, version, ...assetParts] = pathname
.slice(ffmpegProxyPrefix.length)
.split("/");
const packageConfig = ffmpegPackages.get(packageKey);
const invalidAssetPath = assetParts.length === 0 || assetParts.some((part) => !part || part === "." || part === "..");
if (!packageConfig || !packageConfig.versions.has(version) || invalidAssetPath) {
response.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
response.end("Not found");
return true;
}
const assetPath = assetParts.map((part) => encodeURIComponent(part)).join("/");
const upstreamURL = `https://cdn.jsdelivr.net/npm/${packageConfig.name}@${version}/${assetPath}`;
try {
const upstream = await fetch(upstreamURL);
if (!upstream.ok || !upstream.body) {
response.writeHead(upstream.status || 502, { "Content-Type": "text/plain; charset=utf-8" });
response.end("Failed to load FFmpeg asset");
return true;
}
response.writeHead(200, {
"Content-Type": upstream.headers.get("content-type") ?? "application/octet-stream"
});
Readable.fromWeb(upstream.body).pipe(response);
} catch {
response.writeHead(502, { "Content-Type": "text/plain; charset=utf-8" });
response.end("Failed to load FFmpeg asset");
}
return true;
}
const server = createServer(async (request, response) => {
const url = new URL(request.url ?? "/", `http://${request.headers.host}`);
response.setHeader("Cross-Origin-Opener-Policy", "same-origin");
response.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
response.setHeader("Cross-Origin-Resource-Policy", "cross-origin");
response.setHeader("Cache-Control", "no-store");
try {
if (await proxyFfmpegAsset(url.pathname, response)) return;
const requestedPath = url.pathname === "/" ? "index.html" : decodeURIComponent(url.pathname).replace(/^\/+/, "");
const filePath = resolve(publicDir, requestedPath);
if (!filePath.startsWith(resolve(publicDir))) {
response.writeHead(403, { "Content-Type": "text/plain; charset=utf-8" });
response.end("Forbidden");
return;
}
const stat = statSync(filePath);
if (!stat.isFile()) {
response.writeHead(404);
response.end("Not found");
return;
}
response.writeHead(200, {
"Content-Type": types.get(extname(filePath)) ?? "application/octet-stream",
"Content-Length": stat.size
});
createReadStream(filePath).pipe(response);
} catch {
response.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
response.end("Not found");
}
});
server.listen(port, "127.0.0.1", () => {
console.log(`AVSync running at http://127.0.0.1:${port}`);
});