forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevtoolsproxy.ts
More file actions
54 lines (52 loc) · 1.96 KB
/
devtoolsproxy.ts
File metadata and controls
54 lines (52 loc) · 1.96 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
import http from "http"
import https from "https"
const DASHBOARD_PATH = "tools/devicescript-devtools"
const VSCODE_DASHBOARD_PATH = "tools/devicescript-devtools-vscode"
const CONNECT_PATH = "tools/devicescript-connect"
export function fetchDevToolsProxy(
localhost: boolean,
path: "vscode" | "connect" | "dashboard"
): Promise<string> {
const protocol = localhost ? http : https
const url = localhost
? "http://127.0.0.1:8000/devtools/proxy.html"
: "https://microsoft.github.io/jacdac-docs/devtools/proxy"
const dashboardPath =
path === "vscode"
? VSCODE_DASHBOARD_PATH
: path === "connect"
? CONNECT_PATH
: DASHBOARD_PATH
const root = localhost
? "http://127.0.0.1:8000"
: "https://microsoft.github.io/jacdac-docs"
return new Promise<string>((resolve, reject) => {
protocol
.get(url, res => {
if (res.statusCode != 200)
reject(
new Error(`proxy download failed (${res.statusCode})`)
)
res.setEncoding("utf8")
let body = ""
res.on("data", data => (body += data))
res.on("end", () => {
body = body
.replace(
/https:\/\/microsoft.github.io\/jacdac-docs\/dashboard\//g,
`${root}/${dashboardPath}/`
)
.replace("Jacdac DevTools", "DeviceScript DevTools")
.replace(
"https://microsoft.github.io/jacdac-docs/favicon.svg",
"https://microsoft.github.io/devicescript/img/favicon.svg"
)
resolve(body)
})
res.on("error", reject)
})
.on("error", reject)
}).then(body => {
return body
})
}