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.10.0 to 0.11.0

6

CHANGELOG.md

@@ -0,1 +1,7 @@

0.11.0 — 2015-03-23
- Allow passing url to websocket connection string (#52)
- Allow passing TLS options to websocket connection (#53)
- Track method calls so it incomplete calls can be handled (#54)
0.10.0 - 2015-02-03

@@ -2,0 +8,0 @@

5

examples/example.js

@@ -19,3 +19,6 @@ "use strict";

// do not use `path` option when you are using useSockJs
useSockJs: true
useSockJs: true,
// Use a full url instead of a set of `host`, `port` and `ssl`
// do not set `useSockJs` option if `url` is used
url: 'wss://example.com/websocket'
});

@@ -22,0 +25,0 @@

67

lib/ddp-client.js

@@ -28,2 +28,3 @@ "use strict";

self.ssl = opts.ssl || self.port === 443;
self.tlsOpts = opts.tlsOpts || {};
self.useSockJs = opts.useSockJs || false;

@@ -33,3 +34,3 @@ self.autoReconnect = ("autoReconnect" in opts) ? opts.autoReconnect : true;

self.maintainCollections = ("maintainCollections" in opts) ? opts.maintainCollections : true;
self.url = opts.url;
// support multiple ddp versions

@@ -53,5 +54,12 @@ self.ddpVersion = ("ddpVersion" in opts) ? opts.ddpVersion : "1";

self._updatedCallbacks = {};
self._pendingMethods = {};
self._observers = {};
};
DDPClient.ERRORS = {
DISCONNECTED: new Error("DDPClient: Disconnected from DDP server")
};
/**

@@ -86,2 +94,3 @@ * Inherits from EventEmitter

self.emit("socket-close", event.code, event.reason);
self._endPendingMethodCalls();
self._recoverNetworkError();

@@ -327,2 +336,20 @@ });

DDPClient.prototype._endPendingMethodCalls = function() {
var self = this;
var ids = _.keys(self._pendingMethods);
self._pendingMethods = {};
ids.forEach(function (id) {
if (self._callbacks[id]) {
self._callbacks[id](DDPClient.ERRORS.DISCONNECTED);
delete self._callbacks[id];
}
if (self._updatedCallbacks[id]) {
self._updatedCallbacks[id]();
delete self._updatedCallbacks[id];
}
});
};
DDPClient.prototype._makeSockJSConnection = function() {

@@ -337,3 +364,5 @@ var self = this;

request.get(url, function(err, res, body) {
var requestOpts = { 'url': url, 'agentOptions': self.tlsOpts };
request.get(requestOpts, function(err, res, body) {
if (err) {

@@ -368,7 +397,11 @@ self._recoverNetworkError();

var self = this;
var url;
path = path || self.path || "websocket";
var protocol = self.ssl ? "wss://" : "ws://";
var url = protocol + self.host + ":" + self.port;
url += (path.indexOf("/") === 0)? path : "/" + path;
if (self.url && !self.useSockJs) {
url = self.url;
} else {
url = protocol + self.host + ":" + self.port;
url += (path.indexOf("/") === 0)? path : "/" + path;
}
return url;

@@ -379,3 +412,3 @@ };

var self = this;
self.socket = new WebSocket.Client(url);
self.socket = new WebSocket.Client(url, null, self.tlsOpts);
self._prepareHandlers();

@@ -400,10 +433,20 @@ };

if (callback) {
self._callbacks[id] = callback;
}
self._callbacks[id] = function () {
delete self._pendingMethods[id];
if (updatedCallback) {
self._updatedCallbacks[id] = updatedCallback;
}
if (callback) {
callback.apply(this, arguments);
}
};
self._updatedCallbacks[id] = function () {
delete self._pendingMethods[id];
if (updatedCallback) {
updatedCallback.apply(this, arguments);
}
};
self._pendingMethods[id] = true;
self._send({

@@ -410,0 +453,0 @@ msg : "method",

{
"name": "ddp",
"version": "0.10.0",
"version": "0.11.0",
"description": "Node.js module to connect to servers using DDP protocol.",

@@ -29,9 +29,9 @@ "author": "Tom Coleman <tom@thesnail.org> (http://tom.thesnail.org)",

"ddp-ejson": "0.8.1-3",
"faye-websocket": "~0.7.1",
"faye-websocket": "~0.9.4",
"request": "2.53.x"
},
"devDependencies": {
"mocha": "1.9.x",
"sinon": "1.7.x",
"rewire": "1.1.x"
"mocha": "~2.2.1",
"sinon": "~1.14.1",
"rewire": "~2.3.1"
},

@@ -38,0 +38,0 @@ "scripts": {

@@ -33,3 +33,3 @@ var assert = require('assert'),

assert(wsConstructor.call);
assert.deepEqual(wsConstructor.args, [['ws://localhost:3000/websocket']]);
assert.deepEqual(wsConstructor.args, [['ws://localhost:3000/websocket', null, {}]]);
});

@@ -39,3 +39,3 @@

new DDPClient({'host': 'myserver.com'}).connect();
assert.deepEqual(wsConstructor.args, [['ws://myserver.com:3000/websocket']]);
assert.deepEqual(wsConstructor.args, [['ws://myserver.com:3000/websocket', null, {}]]);
});

@@ -45,3 +45,3 @@

new DDPClient({'host': 'myserver.com', 'port': 42}).connect();
assert.deepEqual(wsConstructor.args, [['ws://myserver.com:42/websocket']]);
assert.deepEqual(wsConstructor.args, [['ws://myserver.com:42/websocket', null, {}]]);
});

@@ -51,5 +51,25 @@

new DDPClient({'host': 'myserver.com', 'port': 443}).connect();
assert.deepEqual(wsConstructor.args, [['wss://myserver.com:443/websocket']]);
assert.deepEqual(wsConstructor.args, [['wss://myserver.com:443/websocket', null, {}]]);
});
it('should propagate tls options if specified', function() {
var tlsOpts = {
'ca': ['fake_pem_content']
}
new DDPClient({'host': 'myserver.com', 'port': 443, 'tlsOpts': tlsOpts}).connect();
assert.deepEqual(wsConstructor.args, [['wss://myserver.com:443/websocket', null, tlsOpts]]);
});
it('should connect to the provided url', function() {
new DDPClient({'url': 'wss://myserver.com/websocket'}).connect();
assert.deepEqual(wsConstructor.args, [['wss://myserver.com/websocket', null, {} ]]);
});
it('should fallback to sockjs if url and useSockJs:true are provided', function() {
var ddpclient = new DDPClient({'url': 'wss://myserver.com/websocket', 'useSockJs': true});
ddpclient._makeSockJSConnection = sinon.stub();
ddpclient.connect();
assert.ok(ddpclient._makeSockJSConnection.called);
});
it('should clear event listeners on close', function(done) {

@@ -141,2 +161,64 @@ var ddpclient = new DDPClient();

});
it('should save currently running method calls', function() {
var ddpclient = new DDPClient();
ddpclient._getNextId = sinon.stub().returns('_test');
ddpclient._send = Function.prototype;
ddpclient.connect();
ddpclient.call();
assert("_test" in ddpclient._pendingMethods)
});
it('should remove id when callback is called', function() {
var ddpclient = new DDPClient();
ddpclient._getNextId = sinon.stub().returns('_test');
ddpclient._send = Function.prototype;
ddpclient.connect();
ddpclient.call();
assert("_test" in ddpclient._pendingMethods)
ddpclient._callbacks._test();
assert(!("_test" in ddpclient._pendingMethods))
});
it('should remove id when updated-callback is called', function() {
var ddpclient = new DDPClient();
ddpclient._getNextId = sinon.stub().returns('_test');
ddpclient._send = Function.prototype;
ddpclient.connect();
ddpclient.call();
assert("_test" in ddpclient._pendingMethods)
ddpclient._updatedCallbacks._test();
assert(!("_test" in ddpclient._pendingMethods))
});
it('should end method calls which could not be completed', function() {
var ddpclient = new DDPClient();
var callback = sinon.spy();
var updatedCallback = sinon.spy();
ddpclient._pendingMethods = { _test: true };
ddpclient._callbacks = { _test: callback };
ddpclient._updatedCallbacks = { _test: updatedCallback };
ddpclient.connect();
ddpclient.socket.emit('close', {});
assert(callback.calledOnce);
assert(callback.calledWithExactly(DDPClient.ERRORS.DISCONNECTED));
assert(updatedCallback.calledOnce);
// callbacks should be removed after calling them
assert(!("_test" in ddpclient._callbacks));
assert(!("_test" in ddpclient._updatedCallbacks));
assert(!("_test" in ddpclient._pendingMethods));
});
});

@@ -296,4 +378,4 @@

it("should connect to the correct url", function(done) {
var get = function(url, callback) {
assert.equal(url, "http://the-host:9000/sockjs/info");
var get = function(opts, callback) {
assert.equal(opts.url, "http://the-host:9000/sockjs/info");
done();

@@ -313,4 +395,4 @@ };

it("should support custom paths", function(done) {
var get = function(url, callback) {
assert.equal(url, "http://the-host:9000/search/sockjs/info");
var get = function(opts, callback) {
assert.equal(opts.url, "http://the-host:9000/search/sockjs/info");
done();

@@ -332,3 +414,3 @@ };

var error = { message: "error" };
var get = function(url, callback) {
var get = function(opts, callback) {
callback(error);

@@ -347,3 +429,3 @@ };

var info = null;
var get = function(url, callback) {
var get = function(opts, callback) {
callback(null, null, info);

@@ -364,3 +446,3 @@ };

var info = '{}';
var get = function(url, callback) {
var get = function(opts, callback) {
callback(null, null, info);

@@ -381,3 +463,3 @@ };

var info = '{"base_url": "https://somepath"}';
var get = function(url, callback) {
var get = function(opts, callback) {
callback(null, null, info);

@@ -398,3 +480,3 @@ };

var info = '{"base_url": "/somepath"}';
var get = function(url, callback) {
var get = function(opts, callback) {
callback(null, null, info);

@@ -412,2 +494,21 @@ };

});
it("should propagate tls options", function(done) {
var tlsOpts = {'ca': ['fake_pem_content']};
var get = function(opts, callback) {
assert.equal(opts.agentOptions, tlsOpts);
done();
};
WithRequestGet(get, function() {
var opts = {
host: "the-host",
port: 9000,
path: "search",
tlsOpts: tlsOpts
};
var ddpclient = new DDPClient(opts);
ddpclient._makeSockJSConnection();
});
});
});

@@ -424,2 +525,2 @@ });

request.get = originalGet;
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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