Changes for compatibility with recent versions of NodeJS.

The pty.js module is replaced with node-pty, now-mandatory callbacks
are added to various fs functions, and deprecated Buffer() calls are
replaced with Buffer.from() or Buffer.alloc().
This commit is contained in:
John "Elwin" Edwards 2019-08-25 21:27:31 -04:00
parent 4940bf86ae
commit f2256500e1
2 changed files with 32 additions and 19 deletions

View file

@ -5,9 +5,9 @@ browser. It is intended to be compatible with dgamelaunch.
Node
---
RLGWebD currently works with Node v0.10.
RLGWebD is currently being updated to work with Node v10.x.
It requires the 'posix', 'pty.js', and 'websocket' modules. Currently,
It requires the 'posix', 'node-pty', and 'websocket' modules. Currently,
it expects them to be installed in "/var/local/lib/node_modules". It
is not recommended to run npm as root when installing the modules.

47
rlgwebd
View file

@ -10,7 +10,7 @@ var events = require('events');
var child_process = require('child_process');
// Dependencies
var posix = require("posix");
var pty = require("pty.js");
var pty = require("node-pty");
var WebSocketServer = require("websocket").server;
/* Default options */
@ -44,8 +44,8 @@ if ("domain_name" in rlgwebd_options && "keyfile" in rlgwebd_options &&
rlgwebd_options["use_https"] = true;
var clearbufs = [
new Buffer([27, 91, 72, 27, 91, 50, 74]), // xterm: CSI H CSI 2J
new Buffer([27, 91, 72, 27, 91, 74]) // screen: CSI H CSI J
Buffer.from([27, 91, 72, 27, 91, 50, 74]), // xterm: CSI H CSI 2J
Buffer.from([27, 91, 72, 27, 91, 74]) // screen: CSI H CSI J
];
/* Data on the games available. */
@ -109,7 +109,7 @@ function BaseGame() {
this.watchers = [];
/* replaybuf holds the output since the last screen clear, so watchers can
* begin with a complete screen. replaylen is the number of bytes stored. */
this.replaybuf = new Buffer(1024);
this.replaybuf = Buffer.alloc(1024);
this.replaylen = 0;
/* Time of last activity. */
this.lasttime = new Date();
@ -125,12 +125,12 @@ BaseGame.prototype.tag = function () {
BaseGame.prototype.framepush = function(chunk) {
/* If this chunk resets the screen, discard what preceded it. */
if (isclear(chunk)) {
this.replaybuf = new Buffer(1024);
this.replaybuf = Buffer.alloc(1024);
this.replaylen = 0;
}
/* Make sure there's space. */
while (this.replaybuf.length < chunk.length + this.replaylen) {
var nbuf = new Buffer(this.replaybuf.length * 2);
var nbuf = Buffer.alloc(this.replaybuf.length * 2);
this.replaybuf.copy(nbuf, 0, 0, this.replaylen);
this.replaybuf = nbuf;
if (this.replaybuf.length > 65536) {
@ -210,7 +210,9 @@ function TermSession(gname, pname, wsReq) {
var progressdir = path.join("/dgldir/inprogress", this.gname);
this.lock = path.join(progressdir, this.pname + ":node:" + ts + ".ttyrec");
var lmsg = this.term.pid.toString() + '\n' + this.h + '\n' + this.w + '\n';
fs.writeFile(this.lock, lmsg, "utf8");
fs.writeFile(this.lock, lmsg, "utf8", function (err) {
if (err) tslog("Locking failed: %s", err);
});
var ttyrec = path.join("/dgldir/ttyrec", this.pname, this.gname,
ts + ".ttyrec");
this.record = fs.createWriteStream(ttyrec, { mode: 0664 });
@ -238,8 +240,8 @@ TermSession.prototype = new BaseGame();
/* Currently this also sends to the player and any watchers. */
TermSession.prototype.write_ttyrec = function (datastr) {
this.lasttime = new Date();
var buf = new Buffer(datastr);
var chunk = new Buffer(buf.length + 12);
var buf = Buffer.from(datastr);
var chunk = Buffer.alloc(buf.length + 12);
/* TTYREC headers */
chunk.writeUInt32LE(Math.floor(this.lasttime.getTime() / 1000), 0);
chunk.writeUInt32LE(1000 * (this.lasttime.getTime() % 1000), 4);
@ -273,7 +275,7 @@ TermSession.prototype.input_msg = function (message) {
if (hexstr.length % 2 != 0) {
hexstr = hexstr.slice(0, -1);
}
var keybuf = new Buffer(hexstr, "hex");
var keybuf = Buffer.from(hexstr, "hex");
this.write(keybuf);
}
};
@ -286,7 +288,9 @@ TermSession.prototype.close = function () {
TermSession.prototype.destroy = function () {
var tag = this.tag();
fs.unlink(this.lock);
fs.unlink(this.lock, function (err) {
if (err) tslog("Lock removal failed: %s", err);
});
this.record.end();
var watchsocks = this.watchers;
this.watchers = [];
@ -355,7 +359,7 @@ DglSession.prototype.startchunk = function () {
if (this.reading)
return;
this.reading = true;
var header = new Buffer(12);
var header = Buffer.alloc(12);
fs.read(this.fd, header, 0, 12, this.rpos, this.datachunk.bind(this));
};
@ -376,7 +380,7 @@ DglSession.prototype.datachunk = function (err, n, buf) {
// Something is probably wrong...
tslog("DGL %s: looking for %d bytes", this.tag(), datalen);
}
var databuf = new Buffer(datalen);
var databuf = Buffer.alloc(datalen);
fs.read(this.fd, databuf, 0, datalen, this.rpos, this.handledata.bind(this));
};
@ -419,7 +423,9 @@ DglSession.prototype.close = function () {
if (connlist[i].connected)
connlist[i].close();
}
fs.close(this.fd);
fs.close(this.fd, function (err) {
if (err) tslog("PTY close failed: %s", err);
});
this.emit("close");
gamemux.emit('end', this.gname, this.pname);
tslog("DGL %s: closed", this.tag());
@ -690,10 +696,15 @@ function login(req, res, formdata) {
function regsetup(username) {
function regsetup_l2(err) {
for (var g in games) {
fs.mkdir(path.join("/dgldir/ttyrec", username, games[g].uname), 0755);
fs.mkdir(path.join("/dgldir/ttyrec", username, games[g].uname), 0755,
function (err) {
if (err) tslog("ttyrec mkdir failed: %s", err);
});
}
}
fs.mkdir(path.join("/dgldir/userdata", username), 0755);
fs.mkdir(path.join("/dgldir/userdata", username), 0755, function (err) {
if (err) tslog("Userdata mkdir failed: %s", err);
});
fs.mkdir(path.join("/dgldir/ttyrec/", username), 0755, regsetup_l2);
}
@ -781,7 +792,9 @@ function stopgame(res, formdata) {
if (err.code == "ESRCH") {
var nodere = RegExp("^" + pname + ":node:");
if (fname.match(nodere)) {
fs.unlink(fullfile);
fs.unlink(fullfile, function (err) {
if (err) tslog("Stale lock removal failed: %s", err);
});
}
}
}