Socket
Socket
Sign inDemoInstall

ddp

Package Overview
Dependencies
Maintainers
5
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ddp - npm Package Compare versions

Comparing version 0.9.4 to 0.10.0

examples/example-meteor-server/.meteor/.finished-upgraders

4

CHANGELOG.md

@@ -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

examples/example.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc