fix: use proxy sources from jacac-docs

This commit is contained in:
pelikhan 2022-01-08 19:12:13 -08:00
Родитель 3caefde76e
Коммит 0856b76477
5 изменённых файлов: 31 добавлений и 176 удалений

Просмотреть файл

@ -1 +0,0 @@
<svg viewBox="0 0 24 24" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="2" xmlns="http://www.w3.org/2000/svg"><path fill="gold" d="M22.369 14.045H4.037a2.618 2.618 0 0 1-2.656.173A2.493 2.493 0 0 1 0 12c0-.933.533-1.788 1.381-2.218a2.618 2.618 0 0 1 2.656.173h18.332c.9 0 1.631.712 1.631 1.59v.91c0 .878-.73 1.59-1.631 1.59zM4.124 12c0-.404-.164-.791-.457-1.077a1.581 1.581 0 0 0-1.104-.446c-.414 0-.811.16-1.104.446A1.504 1.504 0 0 0 1.002 12c0 .404.164.791.457 1.077.293.285.69.446 1.104.446.414 0 .811-.16 1.104-.446.293-.286.457-.673.457-1.077zM4.187 18c0-.404-.167-.791-.464-1.077a1.618 1.618 0 0 0-1.121-.446c-.42 0-.824.16-1.12.446A1.493 1.493 0 0 0 1.016 18c0 .404.167.791.464 1.077.297.285.7.446 1.121.446.42 0 .823-.16 1.12-.446.298-.286.465-.673.465-1.077zm16.157 2.045H4.098a2.692 2.692 0 0 1-2.695.173C.54 19.788 0 18.933 0 18s.54-1.788 1.403-2.218a2.692 2.692 0 0 1 2.695.173h16.246c.915 0 1.656.712 1.656 1.59v.91c0 .878-.741 1.59-1.656 1.59zM24 6.455c0 .878-.73 1.59-1.631 1.59H4.037a2.618 2.618 0 0 1-2.656.173A2.493 2.493 0 0 1 0 6c0-.933.533-1.788 1.381-2.218a2.618 2.618 0 0 1 2.65.168l.006.005h18.332c.9 0 1.631.712 1.631 1.59ZM4.124 6c0-.404-.164-.791-.457-1.077a1.581 1.581 0 0 0-1.104-.446c-.414 0-.811.16-1.104.446A1.504 1.504 0 0 0 1.002 6c0 .404.164.791.457 1.077.293.285.69.446 1.104.446.414 0 .811-.16 1.104-.446.293-.286.457-.673.457-1.077z"/></svg>

До

Ширина:  |  Высота:  |  Размер: 1.4 KiB

Просмотреть файл

@ -1,19 +0,0 @@
<html lang="en">
<head>
<link rel="icon" href="/favicon.svg" type="image/x-icon">
</head>
<style>
iframe {
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 100%;
border: none;
}
</style>
<body>
<iframe id="frame" alt="Jacdac Dashboard" allow="usb;serial;bluetooth;vr" allowfullscreen sandbox="allow-scripts allow-downloads allow-same-origin"></iframe>
<script src="./index.js"></script>
</body>
</html>

Просмотреть файл

@ -1,57 +0,0 @@
(function () {
var frame = document.getElementById("frame");
var sender = Math.random() + "";
frame.src = "https://microsoft.github.io/jacdac-docs/dashboard/#" + sender;
var location = window.location;
var secure = location.protocol === "https:";
var protocol = secure ? "wss:" : "ws:";
var hostname = location.hostname;
var port = secure ? 443 : 8081;
var wsurl = "".concat(protocol, "//").concat(hostname, ":").concat(port, "/");
var ws;
var connectSocket = function () {
// already connected or connecting
if (ws)
return;
// node.js -> iframe dashboard
var opened = false;
ws = new WebSocket(wsurl);
ws.binaryType = "arraybuffer";
console.debug("devtools: connecting ".concat(wsurl, "..."));
ws.addEventListener("open", function () {
console.debug("devtools: connected ".concat(ws.url));
opened = true;
});
ws.addEventListener("message", function (msg) {
var data = new Uint8Array(msg.data);
var pktMsg = {
type: "messagepacket",
channel: "jacdac",
data: data,
sender: sender
};
frame.contentWindow.postMessage(pktMsg, "*");
});
ws.addEventListener("close", function () {
console.debug("devtools: connection closed");
ws = undefined;
});
ws.addEventListener("error", function (e) {
if (opened)
console.debug("devtools: error", e);
ws === null || ws === void 0 ? void 0 : ws.close();
});
// iframe dashboard -> node.js
window.addEventListener("message", function (msg) {
var data = msg.data;
if (data && data.type === "messagepacket" && data.channel === "jacdac") {
if ((ws === null || ws === void 0 ? void 0 : ws.readyState) === WebSocket.OPEN) {
ws.send(data.data);
}
}
});
};
// start background connection
setInterval(connectSocket, 5000);
connectSocket();
})();

Просмотреть файл

@ -1,60 +0,0 @@
(function () {
const frame = document.getElementById("frame") as HTMLIFrameElement
const sender = Math.random() + ""
frame.src = "https://microsoft.github.io/jacdac-docs/dashboard/#" + sender
const location = window.location
const secure = location.protocol === "https:"
const protocol = secure ? "wss:" : "ws:"
const hostname = location.hostname
const port = secure ? 443 : 8081
const wsurl = `${protocol}//${hostname}:${port}/`
let ws: WebSocket
const connectSocket = () => {
// already connected or connecting
if (ws) return
// node.js -> iframe dashboard
let opened = false
ws = new WebSocket(wsurl)
ws.binaryType = "arraybuffer"
console.debug(`devtools: connecting ${wsurl}...`)
ws.addEventListener("open", () => {
console.debug(`devtools: connected ${ws.url}`)
opened = true
})
ws.addEventListener("message", (msg) => {
const data = new Uint8Array(msg.data)
const pktMsg = {
type: "messagepacket",
channel: "jacdac",
data,
sender,
}
frame.contentWindow.postMessage(pktMsg, "*")
})
ws.addEventListener("close", () => {
console.debug(`devtools: connection closed`)
ws = undefined
})
ws.addEventListener("error", (e: Event) => {
if (opened)
console.debug(`devtools: error`, e)
ws?.close()
})
// iframe dashboard -> node.js
window.addEventListener("message", msg => {
const data = msg.data
if (data && data.type === "messagepacket" && data.channel === "jacdac") {
if (ws?.readyState === WebSocket.OPEN) {
ws.send(data.data)
}
}
})
}
// start background connection
setInterval(connectSocket, 5000);
connectSocket();
})()

Просмотреть файл

@ -12,15 +12,29 @@ import { createTransports, TransportsOptions } from "./transports"
/* eslint-disable @typescript-eslint/no-var-requires */
const WebSocket = require("faye-websocket")
const http = require("http")
const fs = require("fs")
const https = require("https");
const url = require("url")
const path = require("path")
const net = require("net")
const log = console.log
const debug = console.debug
const error = console.error
function fetchProxy(): Promise<string> {
const url = "https://microsoft.github.io/jacdac-docs/devtools/proxy";
return new Promise<string>((resolve, reject) => {
https.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", () => resolve(body));
res.on("error", e => reject(e));
});
})
}
export async function devToolsCommand(
options?: {
packets?: boolean
@ -34,52 +48,30 @@ export async function devToolsCommand(
const transports = createTransports(options)
debug(`starting dev tools...`)
log(`Jacdac dev tools`)
log(` dashboard: http://localhost:${port}`)
log(` websocket: ws://localhost:${port}`)
log(` raw socket: tcp://localhost:${tcpPort}`)
// download proxy sources
//debug(`downloading proxy sources`)
const proxyHtml = await fetchProxy()
// start http server
//debug(`starting proxy web server`)
const clients: WebSocket[] = []
const server = http.createServer(function (req, res) {
//debug(`${req.method} ${req.url}`)
// parse URL
const parsedUrl = url.parse(req.url)
// extract URL path
let pathname = `.${parsedUrl.pathname}`
if (pathname === "./") pathname = "./index.html"
// based on the URL path, extract the file extension. e.g. .js, .doc, ...
const ext = path.parse(pathname).ext
// maps file extension to MIME typere
const map = {
".ico": "image/x-icon",
".html": "text/html",
".js": "text/javascript",
const pathname = parsedUrl.pathname
if (pathname === "/") {
res.setHeader("Cache-control", "no-cache")
res.setHeader("Content-type", "text/html")
res.end(proxyHtml);
}
const fname = path.join(__dirname, "../public", pathname)
fs.exists(fname, exist => {
if (!exist) {
// if the file is not found, return 404
res.statusCode = 404
debug(`not found`)
return
}
// read file from file system
fs.readFile(fname, (err, data) => {
if (err) {
res.statusCode = 500
debug(`error reading file`)
} else {
// if the file is found, set Content-type and send data
res.setHeader("Content-type", map[ext] || "text/plain")
res.end(data)
}
})
})
})
else {
res.statusCode = 404
}
});
// passive bus to sniff packets
const bus = new JDBus(transports, {