[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [pluggable-transports/snowflake-webext] 04/15: refactor: better encapsulation for `ProxyPair`
This is an automated email from the git hooks/post-receive script.
cohosh pushed a commit to branch main
in repository pluggable-transports/snowflake-webext.
commit 2a102fb50d221069024079fe9dcea451ce448b7c
Author: WofWca <wofwca@xxxxxxxxxxxxxx>
AuthorDate: Fri Nov 4 16:27:38 2022 +0400
refactor: better encapsulation for `ProxyPair`
---
proxypair.js | 10 ++++++++++
snowflake.js | 32 +++++---------------------------
spec/snowflake.spec.js | 38 ++++++++++++++++++++++++++------------
3 files changed, 41 insertions(+), 39 deletions(-)
diff --git a/proxypair.js b/proxypair.js
index 493d9e3..ecce907 100644
--- a/proxypair.js
+++ b/proxypair.js
@@ -66,6 +66,16 @@ class ProxyPair {
}
dbg('SDP ' + offer.type + ' successfully received.');
+ this.pc.createAnswer()
+ .then((sdp) => {
+ dbg('webrtc: Answer ready.');
+ return this.pc.setLocalDescription(sdp);
+ })
+ .catch(() => {
+ this.close();
+ dbg('webrtc: Failed to create or set Answer');
+ });
+
// Send the answer when ready.
const onceSendAnswer = () => {
sendAnswer(this.pc.localDescription);
diff --git a/snowflake.js b/snowflake.js
index 7342856..0fd2acc 100644
--- a/snowflake.js
+++ b/snowflake.js
@@ -154,39 +154,17 @@ class Snowflake {
const offer = JSON.parse(desc);
dbg('Received:\n\n' + offer.sdp + '\n');
const sdp = new RTCSessionDescription(offer);
- if (
- pair.receiveWebRTCOffer(
- sdp,
- answer => this.broker.sendAnswer(pair.id, answer)
- )
- ) {
- this.sendAnswer(pair);
- return true;
- } else {
- return false;
- }
+ const result = pair.receiveWebRTCOffer(
+ sdp,
+ answer => this.broker.sendAnswer(pair.id, answer)
+ );
+ return result;
} catch (e) {
log('ERROR: Unable to receive Offer: ' + e);
return false;
}
}
- /**
- * @param {ProxyPair} pair
- */
- sendAnswer(pair) {
- /** @param {RTCLocalSessionDescriptionInit} sdp */
- const next = function (sdp) {
- dbg('webrtc: Answer ready.');
- pair.pc.setLocalDescription(sdp).catch(fail);
- };
- const fail = function () {
- pair.close();
- dbg('webrtc: Failed to create or set Answer');
- };
- pair.pc.createAnswer().then(next).catch(fail);
- }
-
/**
* @returns {ProxyPair}
*/
diff --git a/spec/snowflake.spec.js b/spec/snowflake.spec.js
index 812430d..54ecb02 100644
--- a/spec/snowflake.spec.js
+++ b/spec/snowflake.spec.js
@@ -9,6 +9,12 @@ class RTCPeerConnection {
setRemoteDescription() {
return true;
}
+ createAnswer() {
+ return Promise.resolve('foo');
+ }
+ setLocalDescription() {
+ return Promise.resolve();
+ }
send() {}
}
@@ -38,6 +44,7 @@ class FakeBroker {
}
setNATType(natType) {
}
+ sendAnswer() {}
}
describe('Snowflake', function() {
@@ -68,27 +75,34 @@ describe('Snowflake', function() {
});
it('receives SDP offer and sends answer', function() {
- var pair, s;
- s = new Snowflake(config, ui, new FakeBroker());
+ var broker, pair, s;
+ broker = new FakeBroker();
+ s = new Snowflake(config, ui, broker);
pair = {
- receiveWebRTCOffer: function() {}
+ id: 'foo',
+ receiveWebRTCOffer: function(_offer, sendAnswer) {
+ sendAnswer('bar');
+ return true;
+ }
};
- spyOn(pair, 'receiveWebRTCOffer').and.returnValue(true);
- spyOn(s, 'sendAnswer');
+ spyOn(broker, 'sendAnswer');
s.receiveOffer(pair, '{"type":"offer","sdp":"foo"}');
- expect(s.sendAnswer).toHaveBeenCalled();
+ expect(broker.sendAnswer).toHaveBeenCalled();
});
it('does not send answer when receiving invalid offer', function() {
- var pair, s;
- s = new Snowflake(config, ui, new FakeBroker());
+ var broker, pair, s;
+ broker = new FakeBroker();
+ s = new Snowflake(config, ui, broker);
pair = {
- receiveWebRTCOffer: function() {}
+ id: 'foo',
+ receiveWebRTCOffer: function(_offer, sendAnswer) {
+ return false;
+ }
};
- spyOn(pair, 'receiveWebRTCOffer').and.returnValue(false);
- spyOn(s, 'sendAnswer');
+ spyOn(broker, 'sendAnswer');
s.receiveOffer(pair, '{"type":"not a good offer","sdp":"foo"}');
- expect(s.sendAnswer).not.toHaveBeenCalled();
+ expect(broker.sendAnswer).not.toHaveBeenCalled();
});
it('can make a proxypair', function() {
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits