better readyStates

canary
Marcus 2021-03-20 12:40:43 -04:00
parent 541eb7847a
commit 99fc805557
1 changed files with 19 additions and 18 deletions

View File

@ -55,11 +55,15 @@ class Server extends EventEmitter {
class STPSocket extends EventEmitter {
tcpSocket;
readyState = 0;
buffer = '';
externalKey;
identity;
CONNECTING = Symbol('connecting');
EXCHANGE = Symbol('exchange');
SECURED = Symbol('secured');
readyState = this.CONNECTING;
get loopback() {
return this.identity.publicKey ===
this.externalKey.exportKey('pkcs8-public-pem');
@ -112,24 +116,21 @@ class STPSocket extends EventEmitter {
}
processMessage(obj) {
switch(obj.cmd) {
case 'KEY': {
if(this.readyState === 0) {
if(this.readyState === this.CONNECTING && obj.cmd === 'KEY') {
this.externalKey = new NodeRSA();
this.externalKey.importKey(obj.data.key, 'pkcs8-public-pem');
this.tcpSocket.write(new AckPacket().toBuffer());
this.readyState = 1;
this.readyState = this.EXCHANGE;
return;
}
break;
}
case 'ACK': {
if(this.readyState === 1) {
this.readyState = 2;
if(this.readyState === this.EXCHANGE && obj.cmd === 'ACK') {
this.readyState = this.SECURED;
this.emit('ready');
return;
}
break;
}
}
}
handshake() {