File size: 2,302 Bytes
970fc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { send, body } from "./http.mjs";
import { serveStatic } from "./static.mjs";
import { handleReadRequest } from "./routes/read.mjs";
import { handleMediaRequest } from "./routes/media.mjs";
import { handleSessionRequest } from "./routes/session.mjs";
import { handleCinemaRequest } from "./routes/cinema.mjs";
import { handleTimelineRequest } from "./routes/timeline.mjs";
import { handleSettingsRequest } from "./routes/settings.mjs";
import { handleSceneRequest } from "./routes/scene.mjs";
import { handleRoomRequest } from "./routes/room.mjs";
import { handleDialogueRequest } from "./routes/dialogue.mjs";
import { handleJobsRequest } from "./routes/jobs.mjs";
const readHandlers = [handleReadRequest, handleMediaRequest];
const editHandlers = [handleCinemaRequest, handleTimelineRequest, handleSettingsRequest, handleSceneRequest, handleRoomRequest, handleDialogueRequest, handleJobsRequest];
function createHandler(appRuntime) {
  return async (req, res) => {
    try {
      const host = req.headers.host || "";
      if (!/^(localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/.test(host)) return send(res, 403, { error: "Only loopback access is accepted; connect Quest through adb reverse" });
      const url = new URL(req.url, `http://${host}`);
      if (req.headers.origin && req.headers.origin !== url.origin) return send(res, 403, { error: "Cross-origin requests are not allowed" });
      for (const handle of readHandlers) {
        await handle(appRuntime, req, res, url);
        if (res.headersSent) return;
      }
      if (req.method === "POST" && url.pathname.startsWith("/api/")) {
        const input = await body(req);
        await handleSessionRequest(appRuntime, req, res, url, input);
        if (res.headersSent) return;
        const authoringScope = appRuntime.authoringAccess.check(input, appRuntime.editEndpoints.has(url.pathname));
        for (const handle of editHandlers) {
          await handle(appRuntime, req, res, url, input, authoringScope);
          if (res.headersSent) return;
        }
        return send(res, 404, { error: "Endpoint not found" });
      }
      await serveStatic(appRuntime, req, res, url);
    } catch (error) {
      send(res, error.code === "ENOENT" ? 404 : 400, { error: error.message });
    }
  };
}
export {
  createHandler
};