-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackend.js
115 lines (91 loc) · 3.24 KB
/
backend.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"use strict";
var http = require("http"),
url = require("url"),
treekill = require("treekill"),
exec = require("child_process").exec,
config = require("./config.json"),
server = null,
EOL = "\n",
API_VERSION = 1;
server = http.createServer(function (req, res) {
var query = url.parse(req.url, true);
// Path in query is required and only or eventsource
if(query && query.path && req.headers.accept === "text/event-stream") {
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache"
});
// Send data to the eventsource
var send = function (data) {
var d = data.toString().split(EOL);
for(var i = 0, l = d.length; i < l; i++) {
// Support for ansi colors and text decorations
var di = d[i].replace(/\x1B\[/g, "\\x1B[");
res.write("data: " + di + EOL + EOL + EOL);
}
};
// Handle error by sending it to the client
var handleErr = function(err) {
send("Internal brackets-es6 error (please report on Github):");
send(err.stack);
res.end();
};
// Test api version
if(!query.query.apiversion || query.query.apiversion != API_VERSION) {
send("Client's api version (" + query.query.apiversion + ") does not match the server's one (" + API_VERSION + ").");
send("Please restart Bracket's process (close all instances).");
send("If a restart does not solve the problem, please report it on Github.");
res.end();
return;
}
try {
var command = __dirname + "/node_modules/.bin/" + query.query.command, //Prepends path to traceur library to the command
cwd = query.query.cwd,
dir;
if(cwd) {
dir = cwd;
}
var child = exec(command, {
cwd: dir,
silent: true
});
child.stdout.on("data", send);
child.stderr.on("data", send);
child.stdout.on("end", function (code) {
res.end();
});
child.on("error", handleErr);
} catch(err) {
handleErr(err);
}
// Kill child process at end of request
// if it's still running
req.on("close", function () {
send("Process terminated.");
treekill(child.pid);
});
} else {
// Response with some basic text
res.writeHead(200, {
"Content-Type": "text/plain"
});
res.end("This is just an event-server");
}
});
// Remove timeout
server.timeout = 0;
server.listen(config.port);
// Another server may be running on this port
// inside another brackets instance.
// Instead of crashing the process and causing
// the interface to throw errors keep
// running and smile :)
process.on("uncaughtException", function (err) {
if(err.code == "EADDRINUSE") {
console.log("Server unable to listen. Address already in use");
// Keep the event loop running using some noop-stuff
setInterval(function () {}, 60 * 1000);
return;
}
throw err;
});