-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (93 loc) · 3.23 KB
/
index.js
File metadata and controls
107 lines (93 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import Gun from 'gun/gun'
import 'gun/sea'
import 'gun/lib/radix'
import 'gun/lib/radisk'
import 'gun/lib/store'
import 'gun/lib/rindexed'
import Channel from 'trystereo'
function GunProxy(opts) {
const debug = opts.debug
let urlProxy
const channel = new Channel(opts.url, opts.hash, opts.trystereo)
const connect = (chan) => {console.log('connected: ' + chan.id)}
const err = (e) => {console.error(e.id, e)}
const disconnect = (chan) => {console.log('disconnected: ' + chan.id)}
channel.on('connect', connect)
channel.on('error', err)
channel.on('disconnect', disconnect)
// WebSocketProxy definition
const WebSocketProxy = function (url) {
const websocketproxy = {};
websocketproxy.url = url || 'ws:proxy';
urlProxy = url || 'ws:proxy';
websocketproxy.CONNECTING = 0;
websocketproxy.OPEN = 1;
websocketproxy.CLOSING = 2;
websocketproxy.CLOSED = 3;
websocketproxy.readyState = 1;
websocketproxy.bufferedAmount = 0;
websocketproxy.onopen = function () { };
websocketproxy.onerror = function () { };
websocketproxy.onclose = function () { };
websocketproxy.extensions = '';
websocketproxy.protocol = '';
websocketproxy.close = { code: '4', reason: 'Closed' };
websocketproxy.onmessage = function () { }; //overwritten by gun
websocketproxy.binaryType = 'blob';
websocketproxy.send = sendMessage;
return websocketproxy
}
let gunMessage
function attachGun(gun){
setTimeout(() => {
if(urlProxy){
if(debug){
console.log('proxy', urlProxy)
}
gunMessage = gun._.opt.peers[urlProxy].wire.onmessage
channel.on('data', onMessage)
gun.shutdown = shutdown(gun)
gun.status = true
} else {
setTimeout(() => {attachGun(gun)}, 5000)
}
}, 5000)
}
function sendMessage(data){
if(debug){
console.log('Sending Data: ', typeof(data), data)
}
channel.onSend(data)
}
function onMessage(data){
if(debug){
console.log('Received Message: ', typeof(data), data)
}
gunMessage(data)
}
function shutdown(gun){
return function(){
channel.off('connect', connect)
channel.off('data', onMessage)
channel.off('error', err)
channel.off('disconnect', disconnect)
var mesh = gun.back('opt.mesh'); // DAM
var peers = gun.back('opt.peers');
Object.keys(peers).forEach((id) => {mesh.bye(id)});
gun.status = false
channel.quit()
}
}
return {WebSocketProxy, attachGun, shutdown}
};
export default function(config){
// instantiate module
const {WebSocketProxy, attachGun} = GunProxy(config)
// configure websocket
// const proxyWebSocket = WebSocketProxy(config)
// pass websocket as custom websocket to gun instance
// make sure localStorage / indexedDB is on
const gun = Gun({ ...config.gun, peers: ["proxy:websocket"], WebSocket: WebSocketProxy })
attachGun(gun)
return gun
}