fd6cac3a by root

extend server base

1 parent 7f0f6c0e
1 { 1 {
2 "name": "gundb-multisocket", 2 "name": "gundb-multisocket",
3 "version": "0.0.2", 3 "version": "0.0.3",
4 "author": "Lorenzo Mangani <lorenzo.mangani@gmail.com>", 4 "author": "Lorenzo Mangani <lorenzo.mangani@gmail.com>",
5 "description": "Serve multiple Gun WS instances over a single HTTP socket", 5 "description": "Serve multiple Gun WS instances over a single HTTP socket",
6 "main": "server.js", 6 "main": "server.js",
...@@ -10,8 +10,7 @@ ...@@ -10,8 +10,7 @@
10 "dependencies": { 10 "dependencies": {
11 "express": "^4.17.1", 11 "express": "^4.17.1",
12 "gun": "^0.2020.401", 12 "gun": "^0.2020.401",
13 "quick-lru": "^5.1.0", 13 "quick-lru": "^5.1.0"
14 "rimraf": "^3.0.2"
15 }, 14 },
16 "engines": { 15 "engines": {
17 "node": "12.x" 16 "node": "12.x"
......
...@@ -5,44 +5,50 @@ ...@@ -5,44 +5,50 @@
5 */ 5 */
6 6
7 const url = require("url"); 7 const url = require("url");
8 var rimraf = require("rimraf");
9 const Gun = require("gun/gun"); // do not load storage adaptors by default 8 const Gun = require("gun/gun"); // do not load storage adaptors by default
10 require("./gun-ws.js"); // required to allow external websockets into gun constructor 9 require("./gun-ws.js"); // required to allow external websockets into gun constructor
11 require("./mem.js"); // disable to allow file writing for debug 10 require("./mem.js"); // disable to allow file writing for debug
12 const http = require("http"); 11 const http = require("http");
12 const https = require("https");
13 const WebSocket = require("ws"); 13 const WebSocket = require("ws");
14 var server = http.createServer(); 14 var debug = process.env.DEBUG || false;
15
16 config.options = {
17 key: process.env.SSLKEY ? fs.readFileSync(process.env.SSLKEY) : fs.readFileSync('src/assets/server.key'),
18 cert: process.env.SSLCERT ? fs.readFileSync(process.env.SSLCERT) : fs.readFileSync('src/assets/server.cert')
19 }
20
21 if (!process.env.SSL) {
22 var server = http.createServer();
23 server.listen(process.env.port || 3000);
24 } else {
25 var server = https.createServer(config.options);
26 server.listen(process.env.PORT || 443);
27 }
15 28
16 // LRU with last used sockets 29 // LRU with last used sockets
17 const QuickLRU = require("quick-lru"); 30 const QuickLRU = require("quick-lru");
18 var evict = function(key, value) { 31 const lru = new QuickLRU({ maxSize: 10, onEviction: false });
19 console.log("Garbage Collect", key);
20 if (key)
21 rimraf("tmp/" + key, function() {
22 console.log("Cleaned up ID", key);
23 });
24 };
25 const lru = new QuickLRU({ maxSize: 10, onEviction: evict });
26 32
27 server.on("upgrade", async function(request, socket, head) { 33 server.on("upgrade", async function(request, socket, head) {
28 var pathname = url.parse(request.url).pathname || "/gun"; 34 var pathname = url.parse(request.url).pathname || "/gun";
29 console.log("Got WS request", pathname); 35 if (debug) console.log("Got WS request", pathname);
30 var gun = { gun: false, server: false }; 36 var gun = { gun: false, server: false };
31 if (pathname) { 37 if (pathname) {
32 if (lru.has(pathname)) { 38 if (lru.has(pathname)) {
33 // Existing Node 39 // Existing Node
34 console.log("Recycle id", pathname); 40 if (debug) console.log("Recycle id", pathname);
35 gun = await lru.get(pathname); 41 gun = await lru.get(pathname);
36 } else { 42 } else {
37 // Create Node 43 // Create Node
38 console.log("Create id", pathname); 44 if (debug) console.log("Create id", pathname);
39 // NOTE: Only works with lib/ws.js shim allowing a predefined WS as ws.web parameter in Gun constructor 45 // NOTE: Only works with lib/ws.js shim allowing a predefined WS as ws.web parameter in Gun constructor
40 gun.server = new WebSocket.Server({ noServer: true, path: pathname }); 46 gun.server = new WebSocket.Server({ noServer: true, path: pathname });
41 console.log("set peer", request.headers.host + pathname); 47 if (debug) console.log("set peer", request.headers.host + pathname);
42 gun.gun = new Gun({ 48 gun.gun = new Gun({
43 peers: [], // should we use self as peer? 49 peers: [], // should we use self as peer?
44 localStorage: false, 50 localStorage: false,
45 file: "tmp/" + pathname, 51 file: false, // "tmp/" + pathname,
46 multicast: false, 52 multicast: false,
47 ws: { noServer: true, path: pathname, web: gun.server }, 53 ws: { noServer: true, path: pathname, web: gun.server },
48 web: gun.server 54 web: gun.server
...@@ -53,13 +59,10 @@ server.on("upgrade", async function(request, socket, head) { ...@@ -53,13 +59,10 @@ server.on("upgrade", async function(request, socket, head) {
53 if (gun.server) { 59 if (gun.server) {
54 // Handle Request 60 // Handle Request
55 gun.server.handleUpgrade(request, socket, head, function(ws) { 61 gun.server.handleUpgrade(request, socket, head, function(ws) {
56 console.log("connecting to gun instance", gun.gun.opt()._.opt.ws.path); 62 if (debug) console.log("connecting to gun instance", gun.gun.opt()._.opt.ws.path);
57 gun.server.emit("connection", ws, request); 63 gun.server.emit("connection", ws, request);
58 }); 64 });
59 } else { 65 } else {
60 socket.destroy(); 66 socket.destroy();
61 } 67 }
62 }); 68 });
63
64 //
65 server.listen(3000);
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!