hsync 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +0 -4
- package/cli.js +43 -0
- package/config.js +6 -3
- package/connection.js +57 -7
- package/dist/hsync.js +371 -0
- package/dist/hsync.min.js +2 -0
- package/dist/hsync.min.js.LICENSE.txt +10 -0
- package/hsync-web.js +60 -0
- package/hsync.js +23 -0
- package/index.js +8 -4
- package/lib/socket-listen-handler.js +10 -4
- package/lib/socket-relay-handler.js +11 -3
- package/lib/web-handler.js +16 -8
- package/lib/web-net.js +67 -0
- package/package.json +18 -4
- package/webpack.config.js +16 -0
package/Readme.md
CHANGED
package/cli.js
CHANGED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { program, Option } = require('commander');
|
|
4
|
+
const pack = require('./package.json');
|
|
5
|
+
const config = require('./config');
|
|
6
|
+
const { createConnection } = require('./hsync');
|
|
7
|
+
|
|
8
|
+
program
|
|
9
|
+
.name(pack.name)
|
|
10
|
+
.description(pack.description)
|
|
11
|
+
.version(pack.version)
|
|
12
|
+
.addOption(new Option('-p, --port <number>', 'port for local webserver', 3000).env('PORT'))
|
|
13
|
+
.addOption(new Option('-d, --dynamic-host <url>', 'host to get a dynamic connection from').env('HSYNC_DYNAMIC_HOST'))
|
|
14
|
+
.addOption(new Option('-s, --hsync-server <url>', 'hsync-server location ex: wss://sub.mydomain.com').env('HSYNC_SERVER'))
|
|
15
|
+
.addOption(new Option('-hs, --hsync-secret <url>', 'password to connect to hsync-server').env('HSYNC_SECRET'));
|
|
16
|
+
|
|
17
|
+
program.parse();
|
|
18
|
+
|
|
19
|
+
const options = program.opts();
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
if(options.port) {
|
|
23
|
+
options.port = Number(options.port);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let [defaultCon] = config.connections;
|
|
27
|
+
defaultCon = {...defaultCon, ...options};
|
|
28
|
+
|
|
29
|
+
if (!defaultCon.hsyncServer && !defaultCon.dynamicHost) {
|
|
30
|
+
defaultCon.dynamicHost = config.defaultDynamicHost;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
config.connections[0] = defaultCon;
|
|
34
|
+
|
|
35
|
+
config.connections.forEach(async (conConfig) => {
|
|
36
|
+
const con = await createConnection(conConfig);
|
|
37
|
+
console.log();
|
|
38
|
+
console.log('Listening for requests on: ', con.webUrl);
|
|
39
|
+
console.log('And forwarding to: ', 'http://localhost:' + con.port);
|
|
40
|
+
console.log();
|
|
41
|
+
console.log('Admin ui at: ', con.webAdmin);
|
|
42
|
+
console.log('Secret: ', con.hsyncSecret);
|
|
43
|
+
});
|
package/config.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
const baseConfig = {
|
|
2
|
-
hsyncServer: process.env.HSYNC_SERVER
|
|
2
|
+
hsyncServer: process.env.HSYNC_SERVER, // something like 'wss://mydevice.mydomain.com'
|
|
3
3
|
hsyncSecret: process.env.HSYNC_SECRET, // keep it secret, keep it safe!
|
|
4
4
|
localHost: process.env.LOCAL_HOST || 'localhost', // host of local server
|
|
5
5
|
port: process.env.PORT || 3000, // port of local server
|
|
6
6
|
hsyncBase: process.env.HSYNC_BASE || '_hs',
|
|
7
|
-
keepalive: parseInt(process.env.HSYNC_KEEP_ALIVE) ||
|
|
7
|
+
keepalive: parseInt(process.env.HSYNC_KEEP_ALIVE) || 300,
|
|
8
|
+
dynamicHost: process.env.HSYNC_DYNAMIC_HOST,
|
|
9
|
+
defaultDynamicHost: 'https://demo.hsync.tech',
|
|
8
10
|
};
|
|
9
11
|
|
|
10
12
|
|
|
@@ -23,7 +25,8 @@ keys.forEach((k) => {
|
|
|
23
25
|
port: process.env['PORT_' + name] || baseConfig.port,
|
|
24
26
|
hsyncBase: process.env['HSYNC_BASE_' + name] || baseConfig.hsyncBase,
|
|
25
27
|
keepalive: parseInt(process.env['HSYNC_KEEP_ALIVE_' + name]) || baseConfig.keepalive,
|
|
26
|
-
|
|
28
|
+
dynamicHost: process.env['HSYNC_DYNAMIC_HOST_' + name],
|
|
29
|
+
});
|
|
27
30
|
}
|
|
28
31
|
}
|
|
29
32
|
})
|
package/connection.js
CHANGED
|
@@ -1,27 +1,61 @@
|
|
|
1
|
-
const
|
|
1
|
+
const fetch = require('isomorphic-fetch');
|
|
2
2
|
const EventEmitter = require('events').EventEmitter;
|
|
3
3
|
const b64id = require('b64id');
|
|
4
4
|
const debug = require('debug')('hsync:info');
|
|
5
5
|
const debugVerbose = require('debug')('hsync:verbose');
|
|
6
6
|
const debugError = require('debug')('hsync:error');
|
|
7
7
|
const { createRPCPeer, createServerReplyPeer } = require('./lib/rpc');
|
|
8
|
-
const createWebHandler = require('./lib/web-handler');
|
|
9
|
-
const createSocketListenHandler = require('./lib/socket-listen-handler');
|
|
8
|
+
const { createWebHandler, setNet: webSetNet } = require('./lib/web-handler');
|
|
9
|
+
const { createSocketListenHandler, setNet: listenSetNet } = require('./lib/socket-listen-handler');
|
|
10
10
|
|
|
11
11
|
debug.color = 3;
|
|
12
12
|
debugVerbose.color = 2;
|
|
13
13
|
debugError.color = 1;
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
let mqtt;
|
|
16
|
+
|
|
17
|
+
function setNet(netImpl) {
|
|
18
|
+
webSetNet(netImpl);
|
|
19
|
+
listenSetNet(netImpl);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function setMqtt(mqttImpl) {
|
|
23
|
+
mqtt = mqttImpl;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function createHsync(config) {
|
|
27
|
+
let {
|
|
17
28
|
hsyncServer,
|
|
18
29
|
hsyncSecret,
|
|
19
30
|
localHost,
|
|
20
31
|
port,
|
|
21
32
|
hsyncBase,
|
|
22
|
-
keepalive
|
|
33
|
+
keepalive,
|
|
34
|
+
dynamicHost,
|
|
23
35
|
} = config;
|
|
24
36
|
|
|
37
|
+
let dynamicTimeout;
|
|
38
|
+
|
|
39
|
+
if (dynamicHost) {
|
|
40
|
+
const options = {
|
|
41
|
+
headers: {
|
|
42
|
+
'Content-Type': 'application/json',
|
|
43
|
+
},
|
|
44
|
+
method: 'POST',
|
|
45
|
+
body: '{}',
|
|
46
|
+
};
|
|
47
|
+
const resp = await fetch(`${dynamicHost}/${hsyncBase}/dyn`, options);
|
|
48
|
+
const result = await resp.json();
|
|
49
|
+
// console.log('resutl', result);
|
|
50
|
+
if (dynamicHost.toLowerCase().startsWith('https')) {
|
|
51
|
+
hsyncServer = `wss://${result.url}`;
|
|
52
|
+
} else {
|
|
53
|
+
hsyncServer = `ws://${result.url}`;
|
|
54
|
+
}
|
|
55
|
+
hsyncSecret = result.secret;
|
|
56
|
+
dynamicTimeout = result.timeout;
|
|
57
|
+
}
|
|
58
|
+
|
|
25
59
|
const hsyncClient = {};
|
|
26
60
|
hsyncClient.config = config;
|
|
27
61
|
const peers = {};
|
|
@@ -53,7 +87,11 @@ function createHsync(config) {
|
|
|
53
87
|
});
|
|
54
88
|
|
|
55
89
|
mqConn.on('error', (error) => {
|
|
56
|
-
debugError('error on mqConn', myHostName, error);
|
|
90
|
+
debugError('error on mqConn', myHostName, error.code, error);
|
|
91
|
+
if ((error.code === 4) || (error.code === 5)) {
|
|
92
|
+
debug('ending');
|
|
93
|
+
mqConn.end();
|
|
94
|
+
}
|
|
57
95
|
});
|
|
58
96
|
|
|
59
97
|
mqConn.on('message', (topic, message) => {
|
|
@@ -180,10 +218,22 @@ function createHsync(config) {
|
|
|
180
218
|
hsyncClient.serverReplyMethods = serverReplyMethods;
|
|
181
219
|
hsyncClient.getPeer = getPeer;
|
|
182
220
|
hsyncClient.peerMethods = peerMethods;
|
|
221
|
+
hsyncClient.hsyncSecret = hsyncSecret;
|
|
222
|
+
hsyncClient.hsyncServer = hsyncServer;
|
|
223
|
+
hsyncClient.dynamicTimeout = dynamicTimeout;
|
|
224
|
+
if (hsyncServer.toLowerCase().startsWith('wss://')) {
|
|
225
|
+
hsyncClient.webUrl = `https://${myHostName}`;
|
|
226
|
+
} else {
|
|
227
|
+
hsyncClient.webUrl = `https://${myHostName}`;
|
|
228
|
+
}
|
|
229
|
+
hsyncClient.webAdmin = `${hsyncClient.webUrl}/${hsyncBase}/admin`;
|
|
230
|
+
hsyncClient.port = port;
|
|
183
231
|
|
|
184
232
|
return hsyncClient;
|
|
185
233
|
}
|
|
186
234
|
|
|
187
235
|
module.exports = {
|
|
188
236
|
createHsync,
|
|
237
|
+
setNet,
|
|
238
|
+
setMqtt,
|
|
189
239
|
};
|