gun/lib/serve.js
akaoio f0cce073a8
New SEA features! (many new features) (#1400)
* feat: create pair with seed, content addressing with shorter hash

* feat: create pair using priv/epriv

* optimize SEA.pair

* feat: globalThis along with window

* white labeling

* feat: add WebAuthn example and enhance SEA.sign, SEA.verify, SEA check.pub, for WebAuthn support

* feat: enhance WebAuthn integration with new put options and improved signature handling

* polish SEA.sign and SEA.verify

* feat: localize options in SEA.check.pub to enhance security and prevent attacks

* fix: correct destructuring of user object to enhance security in SEA

* rebuild SEA

* feat: support ArrayBuffer as seed for key pair generation in SEA

* test: add unit test for hashing ArrayBuffer in SEA

* fix: create deterministic key pair from seed

* fix: add missing B parameter for ECC curve and implement point validation

* feat: add ArrayBuffer support for hashing in SEA and implement corresponding unit test

* fix: convert numeric salt to string in PBKDF2 implementation

* fix: convert numeric salt option to string in PBKDF2 implementation

* improve hashing tests

* improve sea.work

* rebuild SEA

* improve SEA.work and rebuild SEA

* enhance SEA encryption handling and improve test coverage for SEA functions

---------

Co-authored-by: noname <x@null.com>
Co-authored-by: x <x@mimiza.com>
Co-authored-by: x <null>
Co-authored-by: noname <no@name.com>
2025-03-24 11:41:36 -07:00

57 lines
2.0 KiB
JavaScript

var fs = require('fs');
var path = require('path');
var dot = /\.\.+/g;
var slash = /\/\/+/g;
function CDN(dir){
return function(req, res){
req.url = (req.url||'').replace(dot,'').replace(slash,'/');
if(serve(req, res)){ return } // filters GUN requests!
if (req.url.slice(-3) === '.js') {
res.writeHead(200, {'Content-Type': 'text/javascript'});
}
fs.createReadStream(path.join(dir, req.url)).on('error',function(tmp){ // static files!
fs.readFile(path.join(dir, 'index.html'), function(err, tmp){
try{ res.writeHead(200, {'Content-Type': 'text/html'});
res.end(tmp+''); }catch(e){} // or default to index
})}).pipe(res); // stream
}
}
function serve(req, res, next){ var tmp;
if(typeof req === 'string'){
return CDN(req);
}
if(!req || !res){ return false }
next = next || serve;
if(!req.url){ return next() }
if(res.setHeader){ res.setHeader('Access-Control-Allow-Origin', '*') }
if(0 <= req.url.indexOf('gun.js')){
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.end(serve.js = serve.js || require('fs').readFileSync(__dirname + '/../gun.js'));
return true;
}
if(0 <= req.url.indexOf('gun/')){
var path = __dirname + '/../' + req.url.split('/').slice(2).join('/');
if('/' === path.slice(-1)){
fs.readdir(path, function(err, dir){ res.end((dir || (err && 404))+'') });
return true;
}
var S = +new Date;
var rs = fs.createReadStream(path);
if(req.url.slice(-3) === '.js'){ res.writeHead(200, {'Content-Type': 'text/javascript'}) }
rs.on('open', function(){ console.STAT && console.STAT(S, +new Date - S, 'serve file open'); rs.pipe(res) });
rs.on('error', function(err){ res.end(404+'') });
rs.on('end', function(){ console.STAT && console.STAT(S, +new Date - S, 'serve file end') });
return true;
}
if((tmp = req.socket) && (tmp = tmp.server) && (tmp = tmp.route)){ var url;
if(tmp = tmp[(((req.url||'').slice(1)).split('/')[0]||'').split('.')[0]]){
try{ return tmp(req, res, next) }catch(e){ console.log(req.url+' crashed with '+e) }
}
}
return next();
}
module.exports = serve;