-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
61 lines (54 loc) · 1.54 KB
/
main.js
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
/*
* Copyright (c) 2019 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK.
*
* This work is licensed under the
* Creative Commons Attribution 4.0 International License.
* To view a copy of this license, visit
* <http://creativecommons.org/licenses/by/4.0>.
* or send a letter to Creative Commons, PO Box 1866,
* Mountain View, CA 94042, USA.
*
*/
import Net from "net";
import MDNS from "mdns";
import {Server} from "websocket"
import Preference from "preference";
let compartment;
class ModDevServer extends Server {
callback(message, value) {
if (Server.handshake === message) {
ModDevServer.debug(this.detach());
ModDevServer.runMod("debug");
}
else if (Server.subprotocol === message)
return "x-xsbug";
}
static runMod(when) {
if (when && (when !== (Preference.get("config", "when") || "boot")))
return;
try {
compartment = undefined;
compartment = new Compartment("mod", globalThis, Compartment.map);
}
catch (e) {
trace(`exception loading mod: ${e}\n`);
}
}
static debug(socket) @ "xs_debug"; // hand-off native socket to debugger
}
Object.freeze(ModDevServer.prototype);
export default function() {
trace(`host ready on serial\n`);
trace(`Date is ${new Date}\n`);
if (Net.get("IP")) {
const hostName = Preference.get("config", "name") || "runmod";
new ModDevServer({port: 8080});
new MDNS({hostName}, function(message, value) {
if ((1 === message) && value)
trace(`host ready at ws://${hostName}.local:8080\n`);
});
}
ModDevServer.runMod("boot")
}