2bd31504 by Lorenzo Mangani Committed by GitHub

Update server.js

1 parent 621198f3
Showing 1 changed file with 32 additions and 31 deletions
......@@ -4,57 +4,58 @@
* MIT Licensed (C) QXIP 2020
*/
const url = require('url');
const url = require("url");
var rimraf = require("rimraf");
const Gun = require('gun/gun');
require('./gun-ws.js');
require('./memdisk.js');
const http = require('http');
const WebSocket = require('ws');
const Gun = require("gun/gun"); // do not load storage adaptors by default
require("./gun-ws.js"); // required to allow external websockets into gun constructor
require("./mem.js"); // disable to allow file writing for debug
const http = require("http");
const WebSocket = require("ws");
var server = http.createServer();
// LRU with last used sockets
const QuickLRU = require('quick-lru');
var evict = function(key,value){
console.log('Garbage Collect',key);
if(key) rimraf("/tmp/"+key, function () { console.log("Cleaned up ID",key); });
}
const lru = new QuickLRU({maxSize: 10, onEviction: evict });
const QuickLRU = require("quick-lru");
var evict = function(key, value) {
console.log("Garbage Collect", key);
if (key)
rimraf("tmp/" + key, function() {
console.log("Cleaned up ID", key);
});
};
const lru = new QuickLRU({ maxSize: 10, onEviction: evict });
server.on('upgrade', async function (request, socket, head) {
var pathname = url.parse(request.url).pathname || '/gun';
console.log('Got WS request',pathname);
var gun = { gun: false, server: false};
if (pathname){
if (lru.has(pathname)){
server.on("upgrade", async function(request, socket, head) {
var pathname = url.parse(request.url).pathname || "/gun";
console.log("Got WS request", pathname);
var gun = { gun: false, server: false };
if (pathname) {
if (lru.has(pathname)) {
// Existing Node
console.log('Recycle id',pathname);
console.log("Recycle id", pathname);
gun = await lru.get(pathname);
} else {
// Create Node
console.log('Create id',pathname);
console.log("Create id", pathname);
// NOTE: Only works with lib/ws.js shim allowing a predefined WS as ws.web parameter in Gun constructor
gun.server = new WebSocket.Server({ noServer: true, path: pathname});
console.log('set peer',request.headers.host+pathname);
gun.server = new WebSocket.Server({ noServer: true, path: pathname });
console.log("set peer", request.headers.host + pathname);
gun.gun = new Gun({
peers:[], // should we use self as peer?
peers: [], // should we use self as peer?
localStorage: false,
file: "tmp/"+pathname,
file: "tmp/" + pathname,
multicast: false,
ws: { noServer: true, path: pathname, web: gun.server },
web: gun.server
});
// gun.gun.on("in", function(msg) { console.log('got',msg ) });
lru.set(pathname,gun);
lru.set(pathname, gun);
}
}
if (gun.server){
if (gun.server) {
// Handle Request
gun.server.handleUpgrade(request, socket, head, function (ws) {
console.log('connecting to gun instance', gun.gun.opt()._.opt.ws.path )
gun.server.emit('connection', ws, request);
gun.server.handleUpgrade(request, socket, head, function(ws) {
console.log("connecting to gun instance", gun.gun.opt()._.opt.ws.path);
gun.server.emit("connection", ws, request);
});
} else {
socket.destroy();
}
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!