Skip to content

Commit bbf467b

Browse files
author
Tankred Hase
committed
Merge pull request #22 from whiteout-io/dev/WO-995
[WO-995] Use workaround for native chrome.sockets.tcp.secure for TLS
2 parents a19d23a + 3cab15e commit bbf467b

File tree

3 files changed

+47
-38
lines changed

3 files changed

+47
-38
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tcp-socket",
3-
"version": "0.5.4",
3+
"version": "0.5.5",
44
"main": "src/tcp-socket",
55
"description": "This shim brings the W3C Raw Socket API to node.js and Chromium. Its purpose is to enable apps to use the same api in Firefox OS, Chrome OS, and on the server.",
66
"repository": {

src/tcp-socket.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,16 @@
490490
}
491491
});
492492

493-
chrome.sockets.tcp.connect(self._socketId, self.host, self.port, function(result) {
494-
if (result < 0) {
495-
self.readyState = 'closed';
496-
self._emit('error', new Error('Unable to connect'));
497-
return;
498-
}
493+
chrome.sockets.tcp.setPaused(self._socketId, true, function() {
494+
chrome.sockets.tcp.connect(self._socketId, self.host, self.port, function(result) {
495+
if (result < 0) {
496+
self.readyState = 'closed';
497+
self._emit('error', new Error('Unable to connect'));
498+
return;
499+
}
499500

500-
self._onSocketConnected();
501+
self._onSocketConnected();
502+
});
501503
});
502504
});
503505
};
@@ -510,25 +512,24 @@
510512
TCPSocket.prototype._onSocketConnected = function() {
511513
var self = this;
512514

515+
if (!self._useTLS) {
516+
return read();
517+
}
518+
513519
// do an immediate TLS handshake if self._useTLS === true
514-
if (self._useTLS) {
515-
self._upgradeToSecure(function() {
516-
if (!self._useForgeTls) {
517-
// chrome.socket is up and running by now, while forge needs to be
518-
// fed traffic and emits 'open' at a later point
519-
self._emit('open');
520+
self._upgradeToSecure(function() {
521+
read();
522+
});
520523

521-
// the tls handshake is done let's start reading from the legacy socket
522-
if (self._useLegacySocket) {
523-
self._readLegacySocket();
524-
}
525-
}
526-
});
527-
} else {
528-
// socket is up and running
529-
self._emit('open');
524+
function read() {
530525
if (self._useLegacySocket) {
531-
self._readLegacySocket(); // let's start reading
526+
// the tls handshake is done let's start reading from the legacy socket
527+
self._readLegacySocket();
528+
self._emit('open');
529+
} else {
530+
chrome.sockets.tcp.setPaused(self._socketId, false, function() {
531+
self._emit('open');
532+
});
532533
}
533534
}
534535
};
@@ -542,14 +543,17 @@
542543

543544
callback = callback || function() {};
544545

545-
if (self._useForgeTls) {
546-
// setup the forge tls client or webworker as tls fallback
547-
createTls.bind(self)();
548-
callback();
549-
} else if (!self._useLegacySocket) {
546+
if (!self._useLegacySocket && self.readyState !== 'open') {
547+
// use chrome.sockets.tcp.secure for TLS, not for STARTTLS!
548+
// use forge only for STARTTLS
549+
self._useForgeTls = false;
550550
chrome.sockets.tcp.secure(self._socketId, onUpgraded);
551551
} else if (self._useLegacySocket) {
552552
chrome.socket.secure(self._socketId, onUpgraded);
553+
} else if (self._useForgeTls) {
554+
// setup the forge tls client or webworker as tls fallback
555+
createTls.bind(self)();
556+
callback();
553557
}
554558

555559
// invoked after chrome.socket.secure or chrome.sockets.tcp.secure have been upgraded

test/unit/tcp-socket-browser-test.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,16 @@ define(function(require) {
131131
describe('chrome.sockets', function() {
132132
beforeEach(function() {
133133
// create chrome.socket stub
134-
var ChromeLegacySocket = function() {};
135-
ChromeLegacySocket.prototype.create = function() {};
136-
ChromeLegacySocket.prototype.connect = function() {};
137-
ChromeLegacySocket.prototype.disconnect = function() {};
138-
ChromeLegacySocket.prototype.send = function() {};
139-
ChromeLegacySocket.prototype.secure = function() {};
134+
var ChromeSocket = function() {};
135+
ChromeSocket.prototype.create = function() {};
136+
ChromeSocket.prototype.connect = function() {};
137+
ChromeSocket.prototype.disconnect = function() {};
138+
ChromeSocket.prototype.send = function() {};
139+
ChromeSocket.prototype.secure = function() {};
140+
ChromeSocket.prototype.setPaused = function() {};
140141

141142
window.chrome.socket = undefined;
142-
socketStub = sinon.createStubInstance(ChromeLegacySocket);
143+
socketStub = sinon.createStubInstance(ChromeSocket);
143144
window.chrome.sockets = {
144145
tcp: socketStub
145146
};
@@ -159,7 +160,7 @@ define(function(require) {
159160
socketId: 42,
160161
data: testData.buffer
161162
});
162-
}, 10);
163+
}, 50);
163164
}
164165
};
165166

@@ -172,6 +173,8 @@ define(function(require) {
172173
});
173174
socketStub.connect.withArgs(42, '127.0.0.1', 9000).yieldsAsync(0);
174175
socketStub.secure.withArgs(42).yieldsAsync(0);
176+
socketStub.setPaused.withArgs(42, true).yieldsAsync();
177+
socketStub.setPaused.withArgs(42, false).yieldsAsync();
175178
socketStub.send.withArgs(42).yieldsAsync({
176179
bytesWritten: 3
177180
});
@@ -211,12 +214,13 @@ define(function(require) {
211214
expect(socketStub.secure.called).to.be.false;
212215
expect(socketStub.send.calledOnce).to.be.true;
213216
expect(socketStub.disconnect.calledOnce).to.be.true;
217+
expect(socketStub.setPaused.calledTwice).to.be.true;
214218

215219
done();
216220
};
217221
});
218222

219-
it.skip('should open, read, write, close with ssl', function(done) {
223+
it('should open, read, write, close with ssl', function(done) {
220224
var sent = false;
221225

222226
socket = TcpSocket.open('127.0.0.1', 9000, {
@@ -250,6 +254,7 @@ define(function(require) {
250254
expect(socketStub.secure.calledOnce).to.be.true;
251255
expect(socketStub.send.calledOnce).to.be.true;
252256
expect(socketStub.disconnect.calledOnce).to.be.true;
257+
expect(socketStub.setPaused.calledTwice).to.be.true;
253258

254259
done();
255260
};

0 commit comments

Comments
 (0)