Update wire.js

This commit is contained in:
Bradley Matusiak 2023-11-23 19:21:09 -05:00 committed by GitHub
parent efb2552997
commit 22a23806cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -56,17 +56,26 @@ Gun.on('opt', function(root){
return; return;
} }
var url = require('url');
opt.mesh = opt.mesh || Gun.Mesh(root); opt.mesh = opt.mesh || Gun.Mesh(root);
opt.WebSocket = opt.WebSocket || require('ws'); opt.WebSocket = opt.WebSocket || require('ws');
var ws = opt.ws = opt.ws || {}; var ws = opt.ws = opt.ws || {};
ws.path = ws.path || '/gun'; ws.path = ws.path || '/gun';
// if we DO need an HTTP server, then choose ws specific one or GUN default one. // if we DO need an HTTP server, then choose ws specific one or GUN default one.
if(!ws.noServer){ if (!opt.web || ws.noServer) {
ws.server = ws.server || opt.web; return;// no server no sockets
if(!ws.server){ this.to.next(root); return } // ugh, bug fix for @jamierez & unstoppable ryan.
} }
ws.web = ws.web || new opt.WebSocket.Server(ws); // we still need a WS server. ws.noServer = true;//workaround for ws.path
ws.web.on('connection', function(wire, req){ var peer; ws.web = ws.web || new opt.WebSocket.Server(ws);
opt.web.on('upgrade', (req, socket, head) => {
if (req.url == ws.path) {
ws.web.handleUpgrade(req, socket, head, function done(ws) {
open(ws, req);
});
}
});
function open(wire, req) {
var peer;
wire.headers = wire.headers || (req || '').headers || ''; wire.headers = wire.headers || (req || '').headers || '';
console.STAT && ((console.STAT.sites || (console.STAT.sites = {}))[wire.headers.origin] = 1); console.STAT && ((console.STAT.sites || (console.STAT.sites = {}))[wire.headers.origin] = 1);
opt.mesh.hi(peer = { wire: wire }); opt.mesh.hi(peer = { wire: wire });
@ -78,6 +87,6 @@ Gun.on('opt', function(root){
}); });
wire.on('error', function (e) { }); wire.on('error', function (e) { });
setTimeout(function heart() { if (!opt.peers[peer.id]) { return } try { wire.send("[]") } catch (e) { }; setTimeout(heart, 1000 * 20) }, 1000 * 20); // Some systems, like Heroku, require heartbeats to not time out. // TODO: Make this configurable? // TODO: PERF: Find better approach than try/timeouts? setTimeout(function heart() { if (!opt.peers[peer.id]) { return } try { wire.send("[]") } catch (e) { }; setTimeout(heart, 1000 * 20) }, 1000 * 20); // Some systems, like Heroku, require heartbeats to not time out. // TODO: Make this configurable? // TODO: PERF: Find better approach than try/timeouts?
}); }
this.to.next(root); this.to.next(root);
}); });