-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathtern
More file actions
executable file
·118 lines (102 loc) · 3.77 KB
/
tern
File metadata and controls
executable file
·118 lines (102 loc) · 3.77 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
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
116
117
118
#!/usr/bin/env node
// HTTP server for desktop editors
// Starts a Tern server and wraps it in an HTTP wrapper
// so that editor plug-ins can talk to it.
var bootstrapServer = require('../lib/bootstrap');
var fs = require("fs"), path = require("path"), url = require("url");
var portFileName = ".tern-port";
var maxIdleTime = 6e4 * 5; // Shut down after five minutes of inactivity
var persistent = process.argv.indexOf("--persistent") > -1;
var stripCRs = process.argv.indexOf("--strip-crs") > -1;
var disableLoadingLocal = process.argv.indexOf("--disable-loading-local") > -1;
var verbose = process.argv.indexOf("--verbose") > -1;
var debug = verbose || process.argv.indexOf("--debug") > -1;
var noPortFile = process.argv.indexOf("--no-port-file") > -1;
var host = "127.0.0.1";
var hostArg = process.argv.indexOf("--host");
if (hostArg > -1) {
host = process.argv[hostArg + 1];
if (host == "null" || host == "any") host = null;
}
var port = 0;
var portArg = process.argv.indexOf("--port");
if (portArg > -1) {
port = Number(process.argv[portArg + 1]);
if (isNaN(port)) port = 0;
}
var ignoreStdin = process.argv.indexOf("--ignore-stdin") > -1;
var httpServer = require("http").createServer(function(req, resp) {
clearTimeout(shutdown);
shutdown = setTimeout(doShutdown, maxIdleTime);
var target = url.parse(req.url, true);
if (target.pathname == "/ping") return respondSimple(resp, 200, "pong");
if (target.pathname != "/") return respondSimple(resp, 404, "No service at " + target.pathname);
if (req.method == "POST") {
var body = "";
req.on("data", function (data) { body += data; });
req.on("end", function() { respond(resp, body); });
} else if (req.method == "GET") {
if (target.query.doc) respond(resp, target.query.doc);
else respondSimple(resp, 400, "Missing query document");
}
});
var serverConfig = {
disableLoadingLocal: disableLoadingLocal,
tern: {
stripCRs: stripCRs,
debug: debug,
parent: { httpServer: httpServer }
}
};
var server = bootstrapServer(serverConfig);
function doShutdown() {
if (persistent) return;
console.log("Was idle for " + Math.floor(maxIdleTime / 6e4) + " minutes. Shutting down.");
process.exit();
}
var shutdown = setTimeout(doShutdown, maxIdleTime);
if (!ignoreStdin) {
process.stdin.on("end", function() {
console.log("stdin pipe closed, killing tern");
process.exit();
});
process.stdin.resume();
}
httpServer.listen(port, host, function() {
var port = httpServer.address().port;
if (!noPortFile) {
var portFile = path.resolve(server.projectDir, portFileName);
fs.writeFileSync(portFile, String(port), "utf8");
process.on("exit", function() {
try {
var cur = Number(fs.readFileSync(portFile, "utf8"));
if (cur == port) fs.unlinkSync(portFile);
} catch(e) {}
});
}
process.on("SIGINT", function() {
console.log("SIGINT trapped, killing tern");
process.exit();
});
process.on("SIGTERM", function() {
console.log("SIGTERM trapped, killing tern");
process.exit();
});
console.log("Listening on port " + port);
});
function respondSimple(resp, status, text) {
resp.writeHead(status, {"content-type": "text/plain; charset=utf-8"});
resp.end(text);
if (verbose) console.log("Response: " + status + " " + text);
}
function respond(resp, doc) {
try { var doc = JSON.parse(doc); }
catch(e) { return respondSimple(resp, 400, "JSON parse error: " + e.message); }
if (verbose) console.log("Request: " + JSON.stringify(doc, null, 2));
server.request(doc, function(err, data) {
if (err) return respondSimple(resp, 400, String(err));
resp.writeHead(200, {"content-type": "application/json; charset=utf-8"});
if (verbose) console.log("Response: " + JSON.stringify(data, null, 2) + "\n");
resp.end(JSON.stringify(data));
});
}