merge updates with @JK0N 's

This commit is contained in:
Mark Nadal 2019-04-16 18:27:16 -07:00
parent 8bc815e827
commit d8ce093e9f
2 changed files with 29 additions and 19 deletions

View File

@ -1,8 +1,22 @@
var normalize = require('path').normalize;
var UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/
var fs = require('fs');
var path = require('path');
var dot = /\.\.+/g;
var slash = /\/\/+/g;
module.exports = function serve(req, res, next){
function CDN(dir){
return function(req, res){
req.url = (req.url||'').replace(dot,'').replace(slash,'/');
if(serve(req, res)){ return } // filters GUN requests!
fs.createReadStream(path.join(dir, req.url)).on('error',function(tmp){ // static files!
try{ tmp = fs.readFileSync(path.join(dir, 'index.html')) }catch(e){}
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(tmp+''); // or default to index
}).pipe(res); // stream
}
}
function serve(req, res, next){
if(typeof req === 'string'){ return CDN(req) }
if(!req || !res){ return false }
next = next || serve;
if(!req.url){ return next() }
@ -11,21 +25,16 @@ module.exports = function serve(req, res, next){
res.end(serve.js = serve.js || require('fs').readFileSync(__dirname + '/../gun.js'));
return true;
}
if(0 === req.url.indexOf('/gun/')){
var root = normalize(__dirname + '/../'), file;
var path = root + req.url.split('/').slice(2).join('/');
if (UP_PATH_REGEXP.test(path)) {
res.status(403).end();
return true;
}
if(0 <= req.url.indexOf('gun/')){
res.writeHead(200, {'Content-Type': 'text/javascript'});
var path = __dirname + '/../' + req.url.split('/').slice(2).join('/'), file;
try{file = require('fs').readFileSync(path)}catch(e){}
if(file){
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.end(file);
return true;
}
}
return next();
}
}
module.exports = serve;

View File

@ -34,9 +34,8 @@ function Mesh(ctx){
if(!raw){ return }
var dup = ctx.dup, id, hash, msg, tmp = raw[0];
if(opt.pack <= raw.length){ return mesh.say({dam: '!', err: "Message too big!"}, peer) }
try{msg = JSON.parse(raw);
}catch(e){opt.log('DAM JSON parse error', e)}
if('{' === tmp){
try{msg = JSON.parse(raw);}catch(e){opt.log('DAM JSON parse error', e)}
if(!msg){ return }
if(dup.check(id = msg['#'])){ return }
dup.track(id, true).it = msg; // GUN core also dedups, so `true` is needed.
@ -62,6 +61,7 @@ function Mesh(ctx){
return;
} else
if('[' === tmp){
try{msg = JSON.parse(raw);}catch(e){opt.log('DAM JSON parse error', e)}
if(!msg){ return }
var i = 0, m;
while(m = msg[i++]){
@ -121,11 +121,12 @@ function Mesh(ctx){
function send(raw, peer){
var wire = peer.wire;
try{
if(wire.send){
wire.send(raw);
} else
if(peer.say){
peer.say(raw);
} else
if(wire.send){
if(wire.readyState && 1 != wire.readyState){ throw "socket not ready yet!" }
wire.send(raw);
}
}catch(e){
(peer.queue = peer.queue || []).push(raw);