Update server.js
Showing
1 changed file
with
48 additions
and
47 deletions
... | @@ -4,61 +4,62 @@ | ... | @@ -4,61 +4,62 @@ |
4 | * MIT Licensed (C) QXIP 2020 | 4 | * MIT Licensed (C) QXIP 2020 |
5 | */ | 5 | */ |
6 | 6 | ||
7 | const url = require('url'); | 7 | const url = require("url"); |
8 | var rimraf = require("rimraf"); | 8 | var rimraf = require("rimraf"); |
9 | const Gun = require('gun/gun'); | 9 | const Gun = require("gun/gun"); // do not load storage adaptors by default |
10 | require('./gun-ws.js'); | 10 | require("./gun-ws.js"); // required to allow external websockets into gun constructor |
11 | require('./memdisk.js'); | 11 | require("./mem.js"); // disable to allow file writing for debug |
12 | const http = require('http'); | 12 | const http = require("http"); |
13 | const WebSocket = require('ws'); | 13 | const WebSocket = require("ws"); |
14 | var server = http.createServer(); | 14 | var server = http.createServer(); |
15 | 15 | ||
16 | // LRU with last used sockets | 16 | // LRU with last used sockets |
17 | const QuickLRU = require('quick-lru'); | 17 | const QuickLRU = require("quick-lru"); |
18 | var evict = function(key,value){ | 18 | var evict = function(key, value) { |
19 | console.log('Garbage Collect',key); | 19 | console.log("Garbage Collect", key); |
20 | if(key) rimraf("/tmp/"+key, function () { console.log("Cleaned up ID",key); }); | 20 | if (key) |
21 | } | 21 | rimraf("tmp/" + key, function() { |
22 | const lru = new QuickLRU({maxSize: 10, onEviction: evict }); | 22 | console.log("Cleaned up ID", key); |
23 | }); | ||
24 | }; | ||
25 | const lru = new QuickLRU({ maxSize: 10, onEviction: evict }); | ||
23 | 26 | ||
24 | server.on('upgrade', async function (request, socket, head) { | 27 | server.on("upgrade", async function(request, socket, head) { |
25 | var pathname = url.parse(request.url).pathname || '/gun'; | 28 | var pathname = url.parse(request.url).pathname || "/gun"; |
26 | console.log('Got WS request',pathname); | 29 | console.log("Got WS request", pathname); |
27 | var gun = { gun: false, server: false}; | 30 | var gun = { gun: false, server: false }; |
28 | if (pathname){ | 31 | if (pathname) { |
29 | if (lru.has(pathname)){ | 32 | if (lru.has(pathname)) { |
30 | // Existing Node | 33 | // Existing Node |
31 | console.log('Recycle id',pathname); | 34 | console.log("Recycle id", pathname); |
32 | gun = await lru.get(pathname); | 35 | gun = await lru.get(pathname); |
33 | } else { | 36 | } else { |
34 | // Create Node | 37 | // Create Node |
35 | console.log('Create id',pathname); | 38 | console.log("Create id", pathname); |
36 | // NOTE: Only works with lib/ws.js shim allowing a predefined WS as ws.web parameter in Gun constructor | 39 | // NOTE: Only works with lib/ws.js shim allowing a predefined WS as ws.web parameter in Gun constructor |
37 | gun.server = new WebSocket.Server({ noServer: true, path: pathname}); | 40 | gun.server = new WebSocket.Server({ noServer: true, path: pathname }); |
38 | console.log('set peer',request.headers.host+pathname); | 41 | console.log("set peer", request.headers.host + pathname); |
39 | gun.gun = new Gun({ | 42 | gun.gun = new Gun({ |
40 | peers:[], // should we use self as peer? | 43 | peers: [], // should we use self as peer? |
41 | localStorage: false, | 44 | localStorage: false, |
42 | file: "tmp/"+pathname, | 45 | file: "tmp/" + pathname, |
43 | multicast: false, | 46 | multicast: false, |
44 | ws: { noServer: true, path: pathname, web: gun.server }, | 47 | ws: { noServer: true, path: pathname, web: gun.server }, |
45 | web: gun.server | 48 | web: gun.server |
46 | }); | ||
47 | // gun.gun.on("in", function(msg) { console.log('got',msg ) }); | ||
48 | lru.set(pathname,gun); | ||
49 | } | ||
50 | } | ||
51 | if (gun.server){ | ||
52 | // Handle Request | ||
53 | gun.server.handleUpgrade(request, socket, head, function (ws) { | ||
54 | console.log('connecting to gun instance', gun.gun.opt()._.opt.ws.path ) | ||
55 | gun.server.emit('connection', ws, request); | ||
56 | }); | 49 | }); |
57 | 50 | lru.set(pathname, gun); | |
51 | } | ||
52 | } | ||
53 | if (gun.server) { | ||
54 | // Handle Request | ||
55 | gun.server.handleUpgrade(request, socket, head, function(ws) { | ||
56 | console.log("connecting to gun instance", gun.gun.opt()._.opt.ws.path); | ||
57 | gun.server.emit("connection", ws, request); | ||
58 | }); | ||
58 | } else { | 59 | } else { |
59 | socket.destroy(); | 60 | socket.destroy(); |
60 | } | 61 | } |
61 | }); | 62 | }); |
62 | 63 | ||
63 | // | 64 | // |
64 | server.listen(3000); | 65 | server.listen(3000); | ... | ... |
-
Please register or sign in to post a comment