forked from AllenDBB/infinite
-
Notifications
You must be signed in to change notification settings - Fork 1
/
repl.js
39 lines (35 loc) · 1.19 KB
/
repl.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
var fs = require('graceful-fs');
var path = require('path');
var clearedPrefixes = {};
// The eval function is passed in because there is no other way to access a file's non-global context
exports.start = function (prefix, suffix, evalFunction) {
if (process.platform === 'win32') return; // Windows doesn't support sockets mounted in the filesystem
prefix = (Config.replsocketprefix || './logs/repl/') + prefix;
if (!evalFunction) {
evalFunction = suffix;
suffix = "";
}
var name = prefix + suffix;
if (!clearedPrefixes[prefix]) {
// Clear out any old sockets
var directory = path.dirname(prefix);
var basename = path.basename(prefix);
fs.readdirSync(directory).forEach(function (file) {
if (file.indexOf(basename) === 0) fs.unlinkSync(directory + '/' + file);
});
clearedPrefixes[prefix] = true;
}
require('net').createServer(function (socket) {
require('repl').start({
input: socket,
output: socket,
eval: function (cmd, context, filename, callback) {
try {
callback(null, evalFunction(cmd));
} catch (e) {
callback(e);
}
}
}).on('exit', socket.end.bind(socket));
}).listen(name, fs.chmodSync.bind(fs, name, Config.replsocketmode || 0600));
};