changeset 212:e6af951def94

rlgwebd: use some newer Javascript features.
author John "Elwin" Edwards
date Fri, 03 Apr 2020 15:15:02 -0400
parents d60063a674e1
children 08665c56c6a0
files rlgwebd
diffstat 1 files changed, 28 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/rlgwebd	Thu Sep 05 15:19:27 2019 -0400
+++ b/rlgwebd	Fri Apr 03 15:15:02 2020 -0400
@@ -1,17 +1,21 @@
 #!/usr/bin/env node
 
-var http = require('http');
-var https = require('https');
-var net = require('net');
-var url = require('url');
-var path = require('path');
-var fs = require('fs');
-var events = require('events');
-var child_process = require('child_process');
+const http = require('http');
+const https = require('https');
+const net = require('net');
+const url = require('url');
+const path = require('path');
+const fs = require('fs');
+const events = require('events');
+const child_process = require('child_process');
 // Dependencies
-var posix = require("posix");
-var pty = require("node-pty");
-var WebSocketServer = require("websocket").server;
+const posix = require("posix");
+const pty = require("node-pty");
+const WebSocketServer = require("websocket").server;
+
+const errorcodes = [ "Generic Error", "Not logged in", "Invalid data", 
+        "Login failed", "Already playing", "Game launch failed",
+        "Server shutting down", "Game not in progress" ];
 
 /* Default options */
 var rlgwebd_options = { 
@@ -23,12 +27,12 @@
 };
 
 /* Read configuration from a file */
-var config_file = "/etc/rlgwebd.conf";
+const config_file = "/etc/rlgwebd.conf";
 var config_lines = read_or_die(config_file, "Configuration file").toString().split('\n');
 
-for (var i = 0; i < config_lines.length; i++) {
-  if (config_lines[i].length > 0 && config_lines[i][0] != '#') {
-    var config_fields = config_lines[i].split('=');
+for (let conf_line of config_lines) {
+  if (conf_line.length > 0 && conf_line[0] != '#') {
+    var config_fields = conf_line.split('=');
     if (config_fields.length < 2)
       continue;
     var option_name = config_fields[0].trim();
@@ -43,13 +47,13 @@
     "certfile" in rlgwebd_options)
   rlgwebd_options["use_https"] = true;
 
-var clearbufs = [
+const clearbufs = [
   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. */
-var games = {
+const games = {
   "rogue3": {
     "name": "Rogue V3",
     "uname": "rogue3",
@@ -397,9 +401,9 @@
   /* Process the data */
   this.framepush(buf);
   var wmsg = JSON.stringify({"t": "d", "d": buf.toString("hex")});
-  for (var i = 0; i < this.watchers.length; i++) {
-    if (this.watchers[i].connected)
-      this.watchers[i].sendUTF(wmsg);
+  for (let watcher of this.watchers) {
+    if (watcher.connected)
+      watcher.sendUTF(wmsg);
   }
   this.emit("data", buf);
   /* Recurse. */
@@ -575,8 +579,8 @@
 }
 
 function isclear(buf) {
-  for (var i = 0; i < clearbufs.length; i++) {
-    if (bufncmp(buf, clearbufs[i], clearbufs[i].length))
+  for (let clearer of clearbufs) {
+    if (bufncmp(buf, clearer, clearer.length))
       return true;
   }
   return false;
@@ -847,6 +851,8 @@
 }
 
 function serveStatic(req, res, fname) {
+  if (fname[0] !== "/")
+    fname = "/" + fname;
   var nname = path.normalize(fname);
   if (nname == "" || nname == "/")
     nname = "index.html";
@@ -1039,10 +1045,6 @@
   }
 }
 
-var errorcodes = [ "Generic Error", "Not logged in", "Invalid data", 
-        "Login failed", "Already playing", "Game launch failed",
-        "Server shutting down", "Game not in progress" ];
-
 function sendError(res, ecode, msg, box) {
   res.writeHead(200, { "Content-Type": "application/json" });
   var edict = {"t": "E"};