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