Comparing version 0.9.4 to 0.10.0
@@ -0,1 +1,5 @@ | ||
0.10.0 - 2015-02-03 | ||
- Add optional SockJS support | ||
0.9.4 - 2014-12-22 | ||
@@ -2,0 +6,0 @@ |
@@ -9,3 +9,2 @@ "use strict"; | ||
port : 3000, | ||
path : "websocket", | ||
ssl : false, | ||
@@ -15,3 +14,9 @@ autoReconnect : true, | ||
maintainCollections : true, | ||
ddpVersion : "1" // ["1", "pre2", "pre1"] available | ||
ddpVersion : "1", // ["1", "pre2", "pre1"] available, | ||
// uses the sockJs protocol to create the connection | ||
// this still uses websockets, but allows to get the benefits | ||
// from projects like meteorhacks:cluster | ||
// (load balancing and service discovery) | ||
// do not use `path` option when you are using useSockJs | ||
useSockJs: true | ||
}); | ||
@@ -18,0 +23,0 @@ |
@@ -7,2 +7,4 @@ "use strict"; | ||
var EJSON = require("ddp-ejson"); | ||
var request = require('request'); | ||
var pathJoin = require('path').join; | ||
var _ = require("ddp-underscore-patched"); | ||
@@ -25,4 +27,5 @@ | ||
self.port = opts.port || 3000; | ||
self.path = opts.path || "websocket"; | ||
self.path = opts.path; | ||
self.ssl = opts.ssl || self.port === 443; | ||
self.useSockJs = opts.useSockJs || false; | ||
self.autoReconnect = ("autoReconnect" in opts) ? opts.autoReconnect : true; | ||
@@ -314,9 +317,63 @@ self.autoReconnectTimer = ("autoReconnectTimer" in opts) ? opts.autoReconnectTimer : 500; | ||
// websocket | ||
if (self.useSockJs) { | ||
self._makeSockJSConnection(); | ||
} else { | ||
var url = self._buildWsUrl(); | ||
self._makeWebSocketConnection(url); | ||
} | ||
}; | ||
DDPClient.prototype._makeSockJSConnection = function() { | ||
var self = this; | ||
// do the info hit | ||
var protocol = self.ssl ? "https://" : "http://"; | ||
var randomValue = "" + Math.ceil(Math.random() * 9999999); | ||
var path = pathJoin("/", self.path || "", "sockjs/info"); | ||
var url = protocol + self.host + ":" + self.port + path; | ||
request.get(url, function(err, res, body) { | ||
if (err) { | ||
self._recoverNetworkError(); | ||
} else if (body) { | ||
var info = JSON.parse(body); | ||
if(!info.base_url) { | ||
// no base_url, then use pure WS handling | ||
var url = self._buildWsUrl(); | ||
self._makeWebSocketConnection(url); | ||
} else if (info.base_url.indexOf("http") === 0) { | ||
// base url for a different host | ||
var url = info.base_url + "/websocket"; | ||
url = url.replace(/^http/, "ws"); | ||
self._makeWebSocketConnection(url); | ||
} else { | ||
// base url for the same host | ||
var path = info.base_url + "/websocket"; | ||
var url = self._buildWsUrl(path); | ||
self._makeWebSocketConnection(url); | ||
} | ||
} else { | ||
// no body. weird. use pure WS handling | ||
var url = self._buildWsUrl(); | ||
self._makeWebSocketConnection(url); | ||
} | ||
}); | ||
}; | ||
DDPClient.prototype._buildWsUrl = function(path) { | ||
var self = this; | ||
path = path || self.path || "websocket"; | ||
var protocol = self.ssl ? "wss://" : "ws://"; | ||
self.socket = new WebSocket.Client(protocol + self.host + ":" + self.port + "/" + self.path); | ||
var url = protocol + self.host + ":" + self.port; | ||
url += (path.indexOf("/") === 0)? path : "/" + path; | ||
return url; | ||
}; | ||
DDPClient.prototype._makeWebSocketConnection = function(url) { | ||
var self = this; | ||
self.socket = new WebSocket.Client(url); | ||
self._prepareHandlers(); | ||
}; | ||
DDPClient.prototype.close = function() { | ||
@@ -323,0 +380,0 @@ var self = this; |
{ | ||
"name": "ddp", | ||
"version": "0.9.4", | ||
"version": "0.10.0", | ||
"description": "Node.js module to connect to servers using DDP protocol.", | ||
@@ -29,3 +29,4 @@ "author": "Tom Coleman <tom@thesnail.org> (http://tom.thesnail.org)", | ||
"ddp-ejson": "0.8.1-3", | ||
"faye-websocket": "~0.7.1" | ||
"faye-websocket": "~0.7.1", | ||
"request": "2.53.x" | ||
}, | ||
@@ -32,0 +33,0 @@ "devDependencies": { |
@@ -269,1 +269,144 @@ var assert = require('assert'), | ||
}); | ||
describe("SockJS", function() { | ||
it("should use direct WS connection if there is a path", function() { | ||
var ddpclient = new DDPClient(); | ||
ddpclient._makeWebSocketConnection = sinon.stub(); | ||
ddpclient.connect(); | ||
assert.ok(ddpclient._makeWebSocketConnection.called); | ||
}); | ||
it("should fallback to sockjs if there useSockJS option", function() { | ||
var ddpclient = new DDPClient({ useSockJs: true }); | ||
ddpclient._makeSockJSConnection = sinon.stub(); | ||
ddpclient.connect(); | ||
assert.ok(ddpclient._makeSockJSConnection.called); | ||
}); | ||
describe("after info hit", function() { | ||
var request = require("request"); | ||
it("should connect to the correct url", function(done) { | ||
var get = function(url, callback) { | ||
assert.equal(url, "http://the-host:9000/sockjs/info"); | ||
done(); | ||
}; | ||
WithRequestGet(get, function() { | ||
var opts = { | ||
host: "the-host", | ||
port: 9000 | ||
}; | ||
var ddpclient = new DDPClient(opts); | ||
ddpclient._makeSockJSConnection(); | ||
}); | ||
}); | ||
it("should support custom paths", function(done) { | ||
var get = function(url, callback) { | ||
assert.equal(url, "http://the-host:9000/search/sockjs/info"); | ||
done(); | ||
}; | ||
WithRequestGet(get, function() { | ||
var opts = { | ||
host: "the-host", | ||
port: 9000, | ||
path: "search" | ||
}; | ||
var ddpclient = new DDPClient(opts); | ||
ddpclient._makeSockJSConnection(); | ||
}); | ||
}); | ||
it("should retry if there is an error", function() { | ||
var error = { message: "error" }; | ||
var get = function(url, callback) { | ||
callback(error); | ||
}; | ||
WithRequestGet(get, function() { | ||
var ddpclient = new DDPClient(); | ||
ddpclient._recoverNetworkError = sinon.stub(); | ||
ddpclient._makeSockJSConnection(); | ||
assert.ok(ddpclient._recoverNetworkError.called); | ||
}); | ||
}); | ||
it("should use direct WS if there is no body", function() { | ||
var info = null; | ||
var get = function(url, callback) { | ||
callback(null, null, info); | ||
}; | ||
WithRequestGet(get, function() { | ||
var ddpclient = new DDPClient(); | ||
ddpclient._makeWebSocketConnection = sinon.stub(); | ||
ddpclient._makeSockJSConnection(); | ||
var wsUrl = "ws://localhost:3000/websocket"; | ||
assert.ok(ddpclient._makeWebSocketConnection.calledWith(wsUrl)); | ||
}); | ||
}); | ||
it("should use direct WS if there is no base_url", function() { | ||
var info = '{}'; | ||
var get = function(url, callback) { | ||
callback(null, null, info); | ||
}; | ||
WithRequestGet(get, function() { | ||
var ddpclient = new DDPClient(); | ||
ddpclient._makeWebSocketConnection = sinon.stub(); | ||
ddpclient._makeSockJSConnection(); | ||
var wsUrl = "ws://localhost:3000/websocket"; | ||
assert.ok(ddpclient._makeWebSocketConnection.calledWith(wsUrl)); | ||
}); | ||
}); | ||
it("should use full base url if it's starts with http", function() { | ||
var info = '{"base_url": "https://somepath"}'; | ||
var get = function(url, callback) { | ||
callback(null, null, info); | ||
}; | ||
WithRequestGet(get, function() { | ||
var ddpclient = new DDPClient(); | ||
ddpclient._makeWebSocketConnection = sinon.stub(); | ||
ddpclient._makeSockJSConnection(); | ||
var wsUrl = "wss://somepath/websocket"; | ||
assert.ok(ddpclient._makeWebSocketConnection.calledWith(wsUrl)); | ||
}); | ||
}); | ||
it("should compute url based on the base_url if it's not starts with http", function() { | ||
var info = '{"base_url": "/somepath"}'; | ||
var get = function(url, callback) { | ||
callback(null, null, info); | ||
}; | ||
WithRequestGet(get, function() { | ||
var ddpclient = new DDPClient(); | ||
ddpclient._makeWebSocketConnection = sinon.stub(); | ||
ddpclient._makeSockJSConnection(); | ||
var wsUrl = "ws://localhost:3000/somepath/websocket"; | ||
assert.ok(ddpclient._makeWebSocketConnection.calledWith(wsUrl)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
function WithRequestGet(getFn, fn) { | ||
var request = require("request"); | ||
var originalGet = request.get; | ||
request.get = getFn; | ||
fn(); | ||
request.get = originalGet; | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45273
18
846
214
4
+ Addedrequest@2.53.x
+ Addedasn1@0.1.11(transitive)
+ Addedassert-plus@0.1.5(transitive)
+ Addedasync@0.9.2(transitive)
+ Addedaws-sign2@0.5.0(transitive)
+ Addedbl@0.9.5(transitive)
+ Addedboom@2.10.1(transitive)
+ Addedcaseless@0.9.0(transitive)
+ Addedcombined-stream@0.0.7(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedcryptiles@2.0.5(transitive)
+ Addedctype@0.5.3(transitive)
+ Addeddelayed-stream@0.0.5(transitive)
+ Addedforever-agent@0.5.2(transitive)
+ Addedform-data@0.2.0(transitive)
+ Addedhawk@2.3.1(transitive)
+ Addedhoek@2.16.3(transitive)
+ Addedhttp-signature@0.10.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedmime-db@1.12.0(transitive)
+ Addedmime-types@2.0.14(transitive)
+ Addednode-uuid@1.4.8(transitive)
+ Addedoauth-sign@0.6.0(transitive)
+ Addedqs@2.3.3(transitive)
+ Addedreadable-stream@1.0.34(transitive)
+ Addedrequest@2.53.0(transitive)
+ Addedsntp@1.0.9(transitive)
+ Addedstring_decoder@0.10.31(transitive)
+ Addedstringstream@0.0.6(transitive)
+ Addedtldts@6.1.52(transitive)
+ Addedtldts-core@6.1.52(transitive)
+ Addedtough-cookie@5.0.0(transitive)
+ Addedtunnel-agent@0.4.3(transitive)