WIP refactor TelebitRemote with EventEmitters and Duplexes

This commit is contained in:
AJ ONeal 2018-09-04 00:31:01 -06:00
parent d39ebf88a2
commit d8aedb39c2
3 changed files with 31 additions and 12 deletions

View File

@ -835,6 +835,24 @@ function startTelebitRemote(rawCb) {
state.insecure = state.config.relay_ignore_invalid_certificates; state.insecure = state.config.relay_ignore_invalid_certificates;
// { relay, config, servernames, ports, sortingHat, net, insecure, token, handlers, greenlockConfig } // { relay, config, servernames, ports, sortingHat, net, insecure, token, handlers, greenlockConfig }
function onError(err) {
// Likely causes:
// * DNS lookup failed (no Internet)
// * Rejected (bad authn)
if ('ENOTFOUND' === err.code) {
// DNS issue, probably network is disconnected
setTimeout(function () {
startTelebitRemote(rawCb);
}, 90 * 1000);
return;
}
if ('function' === typeof rawCb) {
rawCb(err);
} else {
console.error('Unhandled TelebitRemote Error:');
console.error(err);
}
}
console.log("[DEBUG] token", typeof token, token); console.log("[DEBUG] token", typeof token, token);
myRemote = TelebitRemote.createConnection({ myRemote = TelebitRemote.createConnection({
relay: state.relay relay: state.relay
@ -850,19 +868,10 @@ function startTelebitRemote(rawCb) {
, handlers: state.handlers , handlers: state.handlers
, greenlockConfig: state.greenlockConfig , greenlockConfig: state.greenlockConfig
}, function () { }, function () {
myRemote.removeListener('error', onError);
rawCb(null, myRemote); rawCb(null, myRemote);
}); });
myRemote.once('error', function (err) { myRemote.once('error', onError);
// Likely causes:
// * DNS lookup failed (no Internet)
// * Rejected (bad authn)
if ('function' === typeof rawCb) {
rawCb(err);
} else {
console.error('Unhandled TelebitRemote Error:');
console.error(err);
}
});
}); });
} }

View File

@ -477,12 +477,14 @@ function TelebitRemote(state) {
pausedClients.length = 0; pausedClients.length = 0;
}); });
/*
//Call either Open or Reconnect handlers. //Call either Open or Reconnect handlers.
if(state.handlers.onOpen && initialConnect) { if(state.handlers.onOpen && initialConnect) {
state.handlers.onOpen(); state.handlers.onOpen();
} else if (state.handlers.onReconnect && !initialConnect) { } else if (state.handlers.onReconnect && !initialConnect) {
state.handlers.onReconnect(); state.handlers.onReconnect();
} }
*/
initialConnect = false; initialConnect = false;
} }
@ -500,6 +502,9 @@ function TelebitRemote(state) {
connCallback(error); connCallback(error);
} }
me.emit('close');
/*
return;
if (!authenticated) { if (!authenticated) {
if(state.handlers.onError) { if(state.handlers.onError) {
var err = new Error('Failed to connect on first attempt... check authentication'); var err = new Error('Failed to connect on first attempt... check authentication');
@ -522,9 +527,13 @@ function TelebitRemote(state) {
state.handlers.onClose(); state.handlers.onClose();
} }
} }
*/
} }
, onError: function (err) { , onError: function (err) {
me.emit('error', err);
/*
return;
if ('ENOTFOUND' === err.code) { if ('ENOTFOUND' === err.code) {
// DNS issue, probably network is disconnected // DNS issue, probably network is disconnected
timeoutId = setTimeout(connect, 90 * 1000); timeoutId = setTimeout(connect, 90 * 1000);
@ -535,6 +544,7 @@ function TelebitRemote(state) {
if (connCallback) { if (connCallback) {
connCallback(err); connCallback(err);
} }
*/
} }
, sendMessage: function (msg) { , sendMessage: function (msg) {

View File

@ -68,7 +68,7 @@
"serve-tpl-attachment": "^1.0.4", "serve-tpl-attachment": "^1.0.4",
"sni": "^1.0.0", "sni": "^1.0.0",
"socket-pair": "^1.0.3", "socket-pair": "^1.0.3",
"ws": "^2.3.1" "ws": "^6.0.0"
}, },
"trulyOptionalDependencies": { "trulyOptionalDependencies": {
"bluebird": "^3.5.1" "bluebird": "^3.5.1"