mirror of
https://github.com/smogon/pokemon-showdown.git
synced 2026-05-19 21:48:23 -05:00
Now that nodejs/node#3072 is mostly fixed, we can finally start using Node 4+ features. This refactor: - uses arrow functions where appropriate Note that arrow functions still aren't used in Mocha, where `this` is sometimes meaningful. This also removes the need for .bind() nearly everywhere, as well as the `self = this` trick. - refactors Validator and Connection into ES6 classes - no longer uses Array#forEach for iterating arrays We strongly prefer for (let i = 0; i < arr.length; i++) because of performance reasons. Most forEaches have been replaced with for..of, though, which is 5x slower than the long-form loop but 2x faster than forEach, which is good enough outside of most inner loops. The only exception is tournaments, which is due for a more invasive refactor soon anyway.
90 lines
2.2 KiB
JavaScript
90 lines
2.2 KiB
JavaScript
'use strict';
|
|
|
|
const REPL_ENABLED = false;
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const net = require('net');
|
|
|
|
let sockets = [];
|
|
|
|
function cleanup() {
|
|
for (let s = 0; s < sockets.length; ++s) {
|
|
try {
|
|
fs.unlinkSync(sockets[s]);
|
|
} catch (e) {}
|
|
}
|
|
}
|
|
process.on("exit", cleanup);
|
|
|
|
// exit handlers aren't called by the default handler
|
|
if (process.listeners('SIGHUP').length === 0) {
|
|
process.on('SIGHUP', () => process.exit(128 + 1));
|
|
}
|
|
if (process.listeners('SIGINT').length === 0) {
|
|
process.on('SIGINT', () => process.exit(128 + 2));
|
|
}
|
|
|
|
// 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 (!REPL_ENABLED) return;
|
|
if (process.platform === 'win32') return; // Windows doesn't support sockets mounted in the filesystem
|
|
|
|
let resolvedPrefix = path.resolve(__dirname, Config.replsocketprefix || 'logs/repl', prefix);
|
|
if (!evalFunction) {
|
|
evalFunction = suffix;
|
|
suffix = "";
|
|
}
|
|
let name = resolvedPrefix + suffix;
|
|
|
|
if (prefix === 'app') {
|
|
// Clear out any old sockets
|
|
let directory = path.dirname(resolvedPrefix);
|
|
for (let file of fs.readdirSync(directory)) {
|
|
let stat = fs.statSync(directory + '/' + file);
|
|
if (!stat.isSocket()) {
|
|
continue;
|
|
}
|
|
|
|
let socket = net.connect(directory + '/' + file, () => {
|
|
socket.end();
|
|
socket.destroy();
|
|
}).on('error', () => {
|
|
fs.unlink(directory + '/' + file, () => {});
|
|
});
|
|
}
|
|
}
|
|
|
|
net.createServer(socket => {
|
|
require('repl').start({
|
|
input: socket,
|
|
output: socket,
|
|
eval: (cmd, context, filename, callback) => {
|
|
try {
|
|
callback(null, evalFunction(cmd));
|
|
} catch (e) {
|
|
callback(e);
|
|
}
|
|
},
|
|
}).on('exit', () => socket.end());
|
|
socket.on('error', () => socket.destroy());
|
|
}).listen(name, () => {
|
|
fs.chmodSync(name, Config.replsocketmode || 0o600);
|
|
sockets.push(name);
|
|
}).on('error', e => {
|
|
if (e.code === "EADDRINUSE") {
|
|
fs.unlink(name, e => {
|
|
if (e && e.code !== "ENOENT") {
|
|
require('./crashlogger.js')(e, 'REPL: ' + name);
|
|
return;
|
|
}
|
|
|
|
exports.start(prefix, suffix, evalFunction);
|
|
});
|
|
return;
|
|
}
|
|
|
|
require('./crashlogger.js')(e, 'REPL: ' + name);
|
|
});
|
|
};
|