gun/lib/wire.js
Mark Nadal b1b408971d
Master into Deploys (#1391)
* Thank you Murage Martin @murageyun for donating!!!

* Fix opt.s3.fakes3 parsing issue (#1318)

* Fix opt.s3.fakes3 parsing issue

* Fix second typo within if block

* Support variable number of auth retry attempts through opt.retries (#1325)

Maintain default to 9 to ensure backwards compatibility

* Thanks Jason Stallings @octalmage !!!

* Remove unused imports (#1337)

* Update README.md

* yay format change

* encode objects

* WS ws.path fix  (#1343)

* Update wire.js

* Update wire.js

* Update wire.js

* add one click deploy to readme (#1342)

* update src/index (#1254)

* update src/index

* update

* src/index fix

* added src/core

* is ??? this a MVP of book & rad ???? thanks to @rogowski

* book & rad APIs stabilizing

* RAD & Book promoted! + buggy example: test/rad/book.html

* bump path

* cleaned up Book results & sorting & caching

* sea blobs! (#1353)

* sea blobs!

* and null origins

* null fix

* null check is last

* add a way to select stats file from url (#1351)

* react-native detection, and load needed shims (#1349)

* react-native detection

* added lib mobile

* changed back to gun. for another solution

* have unbuild function wrap to prevent scope leaks & allow RETURN hehehe so I can reject @bmatusiak 's lS change O:) O:) I love you you're a hero!

later with @bmatusiak check sea.then for '../gun.js' vs '../' vs ...
note: src/index -> core.js
TODO: something about WebRTC candidates hitting ack decrement limits?

* quick-fix (#1355)

* Fix SEA certificate verification, allow multiple pubs (#1358)

* Create SECURITY.md (#1364)

* ... works (#1357)

* Loading fix (#1356)

* does this load better

* check window.Gun too in rfs

* update SECURITY.md file and change the versions to 0.2020.x (#1365)

* webrtc accept getUserMedia streams as peer

* Check atom exists in graph when deciding to read from disk (#1371)

* fix: ERROR: Radisk needs `store.put` interface (#1374)

* Update STUN servers (#1381)

Commented out sipgate.net STUN server.
Added Cloudflare STUN server.

* universal notification system

---------

Co-authored-by: ritchia1 <andrew.ritchie@estimateone.com>
Co-authored-by: Anton <dev@atjn.dk>
Co-authored-by: Bradley Matusiak <bmatusiak@gmail.com>
Co-authored-by: Jay Byoun <jay8061@pm.me>
Co-authored-by: mimiza <dev@mimiza.com>
Co-authored-by: Simardeep Singh <1003simar@gmail.com>
Co-authored-by: Malcolm Blaney <mblaney@gmail.com>
Co-authored-by: Andreas Heissenberger <andreas@heissenberger.at>
Co-authored-by: carlin978 <120719190+carlin978@users.noreply.github.com>
2024-11-23 17:52:28 -08:00

93 lines
3.2 KiB
JavaScript

var Gun = require('../gun');
/*
An Ad-Hoc Mesh-Network Daisy-Chain
should work even if humans are
communicating with each other blind.
To prevent infinite broadcast loops,
we use a deduplication process
based on the message's identifier.
This is currently implemented in core.
However, because this still creates a
N*2 (where N is the number of connections)
flood, it is not scalable for traditional
services that have a hub network topology.
Does this mean we have to abandon mesh
algorithms? No, we can simply layer more
efficient optimizations in based on constraints.
If these constraints exist, it automatically
upgrades, but if not, it falls back to the
brute-force mesh based robust algorithm.
A simple example is to limit peer connections
and rely upon daisy chaining to relay messages.
Another example, is if peers are willing to
identify themselves, then we can improve the
efficiency of the network by having each peer
include the names of peers it is connected in
each message. Then each subsequent peer will
not relay it to them, since it is unnecessary.
This should create N (where N is the number of
peers) messages (or possibly N+ if there is a
common peer of uncommon peers that receives it
and relays at exact latency timings), which is
optimal.
Since computer networks aren't actually blind,
we will implement the above method to improve
the performance of the ad-hoc mesh network.
But why not have every message contain the
whole history of peers that it relayed through?
Because in sufficiently large enough networks,
with extensive daisy chaining, this will cause
the message to become prohibitively slow and
increase indefinitely in size.
*/
Gun.on('opt', function (root) {
var opt = root.opt;
if (false === opt.ws || opt.once) {
this.to.next(root);
return;
}
opt.mesh = opt.mesh || Gun.Mesh(root);
opt.WebSocket = opt.WebSocket || require('ws');
var ws = opt.ws = opt.ws || {};
ws.path = ws.path || '/gun';
// if we DO need an HTTP server, then choose ws specific one or GUN default one.
if (!opt.web || ws.noServer) {
this.to.next(root);
return;// no server no sockets
}
ws.noServer = true;//workaround for ws.path
ws.web = ws.web || new opt.WebSocket.Server(ws);
opt.web.on('upgrade', (req, socket, head) => {
opt.web.host = opt.web.host || (req.headers||'').origin || (req.headers||'').host;
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 || '';
console.STAT && ((console.STAT.sites || (console.STAT.sites = {}))[wire.headers.origin] = 1);
opt.mesh.hi(peer = { wire: wire });
wire.on('message', function (msg) {
opt.mesh.hear(msg.data || msg, peer);
});
wire.on('close', function () {
opt.mesh.bye(peer);
});
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?
}
this.to.next(root);
});