File size: 999 Bytes
9b03ff0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
import sys

FILE = Path("/usr/local/lib/python3.10/site-packages/gradio/templates/node/build/index.js")

if not FILE.exists():
    print("index.js not found")
    sys.exit(1)

text = FILE.read_text(encoding="utf-8")

MARKER = """
\tif (route === "python") {
\t\tproxy.web(req, res, { target: pythonTarget });
\t\treturn;
\t}
"""

PATCH = """
\tif (route === "python") {
\t\tproxy.web(req, res, { target: pythonTarget });
\t\treturn;
\t}

\t// ===== CUSTOM NODE PROXY =====

\tif (path === "/node" || path.startsWith("/node/")) {
\t\treq.url = req.url.replace(/^\\/node/, "") || "/";

\t\tproxy.web(req, res, {
\t\t\ttarget: "http://127.0.0.1:8888",
\t\t\tchangeOrigin: true
\t\t});

\t\treturn;
\t}
"""

if "// ===== CUSTOM NODE PROXY =====" in text:
    print("Already patched")
    sys.exit(0)

if MARKER not in text:
    print("Marker not found")
    sys.exit(1)

text = text.replace(MARKER, PATCH)

FILE.write_text(text, encoding="utf-8")

print("Patched successfully.")