Refactor for clarity

// FREEBIE
This commit is contained in:
lilia 2015-12-04 10:25:50 -08:00
parent dac084b604
commit 44824df6cb
2 changed files with 42 additions and 34 deletions

View file

@ -36758,7 +36758,7 @@ MessageReceiver.prototype = {
this.socket.onerror = this.onerror.bind(this); this.socket.onerror = this.onerror.bind(this);
this.socket.onopen = this.onopen.bind(this); this.socket.onopen = this.onopen.bind(this);
this.wsr = new WebSocketResource(this.socket, { this.wsr = new WebSocketResource(this.socket, {
handleRequest: this.queueRequest.bind(this), handleRequest: this.handleRequest.bind(this),
keepalive: { path: '/v1/keepalive', disconnect: true } keepalive: { path: '/v1/keepalive', disconnect: true }
}); });
this.pending = Promise.resolve(); this.pending = Promise.resolve();
@ -36788,20 +36788,33 @@ MessageReceiver.prototype = {
eventTarget.dispatchEvent(ev); eventTarget.dispatchEvent(ev);
}); });
}, },
queueRequest: function(request) { handleRequest: function(request) {
// We do the message decryption here, instead of in the ordered pending queue, // We do the message decryption here, instead of in the ordered pending queue,
// to avoid exposing the time it took us to process messages through the time-to-ack. // to avoid exposing the time it took us to process messages through the time-to-ack.
// TODO: handle different types of requests. for now we blindly assume // TODO: handle different types of requests. for now we blindly assume
// PUT /messages <encrypted IncomingPushMessageSignal> // PUT /messages <encrypted Envelope>
textsecure.crypto.decryptWebsocketMessage(request.body, this.signalingKey).then(function(plaintext) { textsecure.crypto.decryptWebsocketMessage(request.body, this.signalingKey).then(function(plaintext) {
// After this point, decoding errors are not the server's // After this point, decoding errors are not the server's
// fault, and we should handle them gracefully and tell the // fault, and we should handle them gracefully and tell the
// user they received an invalid message // user they received an invalid message
request.respond(200, 'OK'); request.respond(200, 'OK');
this.queueEnvelope(plaintext);
var handleRequest = function() { }.bind(this)).catch(function(e) {
var envelope = textsecure.protobuf.Envelope.decode(plaintext); request.respond(500, 'Bad encrypted websocket message');
console.log("Error handling incoming message:", e);
var ev = new Event('error');
ev.error = e;
this.dispatchEvent(ev);
}.bind(this));
},
queueEnvelope: function(envelope) {
var handleEnvelope = this.handleEnvelope.bind(this, envelope);
this.pending = this.pending.then(handleEnvelope, handleEnvelope);
},
handleEnvelope: function(encodedEnvelope) {
var envelope = textsecure.protobuf.Envelope.decode(encodedEnvelope);
if (envelope.type === textsecure.protobuf.Envelope.Type.RECEIPT) { if (envelope.type === textsecure.protobuf.Envelope.Type.RECEIPT) {
return this.onDeliveryReceipt(envelope); return this.onDeliveryReceipt(envelope);
@ -36812,15 +36825,6 @@ MessageReceiver.prototype = {
} else { } else {
throw new Error('Received message with no content and no legacyMessage'); throw new Error('Received message with no content and no legacyMessage');
} }
}.bind(this);
this.pending = this.pending.then(handleRequest, handleRequest);
}.bind(this)).catch(function(e) {
request.respond(500, 'Bad encrypted websocket message');
console.log("Error handling incoming message:", e);
var ev = new Event('error');
ev.error = e;
this.dispatchEvent(ev);
}.bind(this));
}, },
getStatus: function() { getStatus: function() {
if (this.socket) { if (this.socket) {

View file

@ -26,7 +26,7 @@ MessageReceiver.prototype = {
this.socket.onerror = this.onerror.bind(this); this.socket.onerror = this.onerror.bind(this);
this.socket.onopen = this.onopen.bind(this); this.socket.onopen = this.onopen.bind(this);
this.wsr = new WebSocketResource(this.socket, { this.wsr = new WebSocketResource(this.socket, {
handleRequest: this.queueRequest.bind(this), handleRequest: this.handleRequest.bind(this),
keepalive: { path: '/v1/keepalive', disconnect: true } keepalive: { path: '/v1/keepalive', disconnect: true }
}); });
this.pending = Promise.resolve(); this.pending = Promise.resolve();
@ -56,20 +56,33 @@ MessageReceiver.prototype = {
eventTarget.dispatchEvent(ev); eventTarget.dispatchEvent(ev);
}); });
}, },
queueRequest: function(request) { handleRequest: function(request) {
// We do the message decryption here, instead of in the ordered pending queue, // We do the message decryption here, instead of in the ordered pending queue,
// to avoid exposing the time it took us to process messages through the time-to-ack. // to avoid exposing the time it took us to process messages through the time-to-ack.
// TODO: handle different types of requests. for now we blindly assume // TODO: handle different types of requests. for now we blindly assume
// PUT /messages <encrypted IncomingPushMessageSignal> // PUT /messages <encrypted Envelope>
textsecure.crypto.decryptWebsocketMessage(request.body, this.signalingKey).then(function(plaintext) { textsecure.crypto.decryptWebsocketMessage(request.body, this.signalingKey).then(function(plaintext) {
// After this point, decoding errors are not the server's // After this point, decoding errors are not the server's
// fault, and we should handle them gracefully and tell the // fault, and we should handle them gracefully and tell the
// user they received an invalid message // user they received an invalid message
request.respond(200, 'OK'); request.respond(200, 'OK');
this.queueEnvelope(plaintext);
var handleRequest = function() { }.bind(this)).catch(function(e) {
var envelope = textsecure.protobuf.Envelope.decode(plaintext); request.respond(500, 'Bad encrypted websocket message');
console.log("Error handling incoming message:", e);
var ev = new Event('error');
ev.error = e;
this.dispatchEvent(ev);
}.bind(this));
},
queueEnvelope: function(envelope) {
var handleEnvelope = this.handleEnvelope.bind(this, envelope);
this.pending = this.pending.then(handleEnvelope, handleEnvelope);
},
handleEnvelope: function(encodedEnvelope) {
var envelope = textsecure.protobuf.Envelope.decode(encodedEnvelope);
if (envelope.type === textsecure.protobuf.Envelope.Type.RECEIPT) { if (envelope.type === textsecure.protobuf.Envelope.Type.RECEIPT) {
return this.onDeliveryReceipt(envelope); return this.onDeliveryReceipt(envelope);
@ -80,15 +93,6 @@ MessageReceiver.prototype = {
} else { } else {
throw new Error('Received message with no content and no legacyMessage'); throw new Error('Received message with no content and no legacyMessage');
} }
}.bind(this);
this.pending = this.pending.then(handleRequest, handleRequest);
}.bind(this)).catch(function(e) {
request.respond(500, 'Bad encrypted websocket message');
console.log("Error handling incoming message:", e);
var ev = new Event('error');
ev.error = e;
this.dispatchEvent(ev);
}.bind(this));
}, },
getStatus: function() { getStatus: function() {
if (this.socket) { if (this.socket) {