Created
September 4, 2017 23:56
-
-
Save wallabra/5d1f0169e6ad2dbdf408409e96e4888a to your computer and use it in GitHub Desktop.
Latest version of Dynasite + websocket support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _eval, app, bodyParser, done, endsWith, express, fs, io, mime, modules, parse5, path, recurseJXML, serve, serveJXML, server, startsWith, webFolder; | |
express = require('express'); | |
bodyParser = require('body-parser'); | |
fs = require('fs'); | |
path = require('path'); | |
mime = require('mime'); | |
_eval = require('eval'); | |
parse5 = require('parse5'); | |
server = require('http').Server(app); | |
io = null; | |
app = express(); | |
modules = []; | |
app.use(bodyParser.text()); | |
app.use(bodyParser.urlencoded({ | |
extended: false | |
})); | |
app.use(bodyParser.json()); | |
recurseJXML = function(_super, obj, req, y) { | |
var i, j, k, len, n, r, ref, res; | |
i = 0; | |
if (obj.childNodes == null) { | |
return; | |
} | |
ref = obj.childNodes; | |
for (j = 0, len = ref.length; j < len; j++) { | |
n = ref[j]; | |
k = n.nodeName; | |
if (k === "DYNAMIC") { | |
res = _eval(n.value)(req); | |
_super.childNodes[y] = { | |
nodeName: '#text', | |
value: res, | |
parentnode: _super | |
}; | |
console.log("Replacing dynamic node to " + res + "..."); | |
} else if (n.childNodes !== []) { | |
r = recurseJXML(obj, n, req, i); | |
if (r != null) { | |
obj.childNodes[i] = r; | |
} | |
} | |
i++; | |
} | |
return obj; | |
}; | |
serveJXML = function(folder, name, structure) { | |
return app.get('/' + path.join(folder, name).replace('\\', '/'), function(req, res) { | |
structure.childNodes[1] = recurseJXML({ | |
childNodes: [] | |
}, structure.childNodes[1], req, 0); | |
return res.send(parse5.serialize(structure)); | |
}); | |
}; | |
startsWith = function(s, sub) { | |
return s.slice(0, sub.length) === sub; | |
}; | |
endsWith = function(s, sub) { | |
sub = sub.split("").reverse().join(""); | |
s = s.split("").reverse().join(""); | |
return startsWith(s, sub); | |
}; | |
serve = function(folder, fname, base) { | |
var a, fp, module; | |
if (base == null) { | |
base = ""; | |
} | |
if (fs.lstatSync(path.join(folder, fname)).isDirectory()) { | |
return webFolder(path.join(folder, fname), void 0, path.join(base, fname)); | |
} | |
if (endsWith(fname, ".jxml")) { | |
fp = "./" + path.join(folder, fname); | |
fs.readFile(fp, { | |
encoding: 'utf8' | |
}, function(err, data) { | |
if (err != null) { | |
throw err; | |
} else { | |
return serveJXML(base, fname.slice(0, -5), parse5.parse(data)); | |
} | |
}); | |
console.log("Using dynamic page '" + fname + "' at address '/" + (path.join(base, fname.slice(0, -5)).replace(/\\/g, '/')) + "'..."); | |
} else if (endsWith(fname, ".node") || endsWith(fname, ".node.js")) { | |
module = require("./" + path.join(folder, fname)); | |
console.log("Using '" + fname + "' at address '/" + module.address + "'"); | |
if (module.ws != null) { | |
console.log("Establishing Websockets address '/" + module.address + "'."); | |
a = io.of("/" + module.address); | |
a.on("connection", function(socket) { | |
return module.ws(socket); | |
}); | |
} | |
if (module.get != null) { | |
app.get('/' + module.address, function(req, res) { | |
var r; | |
req.remoteIP = function() { | |
var o; | |
o = this.get("X-FORWARDED-FOR"); | |
if (o != null) { | |
return o; | |
} else { | |
return this.ip; | |
} | |
}; | |
console.log("Attending GET request from " + req.ip + " (through URL " + (req.get("referer")) + " and remote IP " + (req.remoteIP()) + ") for " + fname); | |
r = module.get(req, res); | |
if ((r == null) || (r.mimetype == null)) { | |
if (module.mimetype != null) { | |
res.setHeader('Content-Type', module.mimetype); | |
} else { | |
res.setHeader('Content-Type', 'text/html'); | |
} | |
} else { | |
res.setHeader('Content-Type', r.mimetype); | |
} | |
if ((r != null) && (r.data != null)) { | |
return res.send(r.data); | |
} else if ((r != null) && r.sendable) { | |
return res.send(r); | |
} | |
}); | |
} | |
if (module.post != null) { | |
app.post('/' + module.address, function(req, res) { | |
var r; | |
req.remoteIP = function() { | |
var o; | |
o = this.get("X-FORWARDED-FOR"); | |
if (o != null) { | |
return o; | |
} else { | |
return this.ip; | |
} | |
}; | |
console.log("Attending POST request from " + req.ip + " (through URL " + (req.get("referer")) + " and remote IP " + (req.remoteIP()) + ") for " + fname); | |
r = module.post(req, res); | |
if ((r == null) || (r.mimetype == null)) { | |
if (module.mimetype != null) { | |
res.setHeader('Content-Type', module.mimetype); | |
} else { | |
res.setHeader('Content-Type', 'text/html'); | |
} | |
} else { | |
res.setHeader('Content-Type', r.mimetype); | |
} | |
if ((r != null) && (r.data != null)) { | |
return res.send(r.data); | |
} else if ((r != null) && r.sendable) { | |
return res.send(r); | |
} | |
}); | |
} | |
modules.push(module); | |
console.log("Also using '/" + (path.join(base, fname).replace(/\\/g, "/")) + "'...'"); | |
} else { | |
console.log("Using '/" + (path.join(base, fname).replace(/\\/g, "/")) + "'...'"); | |
} | |
return app.get('/' + path.join(base, fname).replace(/\\/g, "/"), function(req, res) { | |
req.remoteIP = function() { | |
var o; | |
o = this.get("X-FORWARDED-FOR"); | |
if (o != null) { | |
return o; | |
} else { | |
return this.ip; | |
} | |
}; | |
console.log("Attending GET request from " + req.ip + " (through URL " + (req.get("referer")) + " and remote IP " + (req.remoteIP()) + " for " + fname); | |
res.setHeader('Content-Type', mime.lookup(path.join(folder, fname))); | |
return res.send(fs.readFileSync(path.join(folder, fname))); | |
}); | |
}; | |
webFolder = function(f, next, base) { | |
console.log("Serving '" + f + "'..."); | |
if (base == null) { | |
base = ""; | |
} | |
return fs.readdir(f, function(err, files) { | |
var fn, j, len; | |
if (err) { | |
throw err; | |
} | |
for (j = 0, len = files.length; j < len; j++) { | |
fn = files[j]; | |
serve(f, path.basename(fn), base); | |
} | |
if (next != null) { | |
return next(); | |
} | |
}); | |
}; | |
done = function(port) { | |
if (port == null) { | |
port = 3000; | |
} | |
return io = require('socket.io')(app.listen(port, function() { | |
var j, len, m, results; | |
console.log("Listening on port " + port + "."); | |
results = []; | |
for (j = 0, len = modules.length; j < len; j++) { | |
m = modules[j]; | |
if (m.init != null) { | |
results.push(m.init(port)); | |
} else { | |
results.push(void 0); | |
} | |
} | |
return results; | |
})); | |
}; | |
module.exports = { | |
done: done, | |
webFolder: webFolder, | |
app: app, | |
serve: serve | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment