Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ddp.js

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ddp.js - npm Package Compare versions

Comparing version 0.3.1 to 0.4.5

.travis.yml

2

bower.json
{
"name": "ddp.js",
"main": "ddp.js",
"version": "0.3.1",
"version": "0.4.5",
"homepage": "https://github.com/mondora/ddp.js",

@@ -6,0 +6,0 @@ "authors": [

@@ -1,111 +0,148 @@

(function () {
(function (root, factory) {
if (typeof define === "function" && define.amd) {
define(factory);
} else if (typeof exports === "object") {
module.exports = factory();
} else {
root.DDP = factory();
}
}(this, function () {
"use strict";
"use strict";
var uniqueId = (function () {
var i = 0;
return function () {
return (i++).toString();
};
})();
var uniqueId = (function () {
var i = 0;
return function () {
return (i++).toString();
};
})();
var INIT_DDP_MESSAGE = "{\"server_id\":\"0\"}";
var MAX_RECONNECT_ATTEMPTS = 10;
var TIMER_INCREMENT = 500;
var INIT_DDP_MESSAGE = "{\"server_id\":\"0\"}";
var MAX_RECONNECT_ATTEMPTS = 10;
var TIMER_INCREMENT = 500;
var DEFAULT_PING_INTERVAL = 10000;
var DDP_SERVER_MESSAGES = [
"added", "changed", "connected", "error", "failed",
"nosub", "ready", "removed", "result", "updated"
"nosub", "ready", "removed", "result", "updated",
"ping", "pong"
];
var DDP = function (options) {
this._endpoint = options.endpoint;
this._SocketConstructor = options.SocketConstructor;
this._autoreconnect = !options.do_not_autoreconnect;
var DDP = function (options) {
// Configuration
this._endpoint = options.endpoint;
this._SocketConstructor = options.SocketConstructor;
this._autoreconnect = !options.do_not_autoreconnect;
this._ping_interval = options._ping_interval || DEFAULT_PING_INTERVAL;
this._debug = options.debug;
this._onReadyCallbacks = {};
this._onResultCallbacks = {};
this._onUpdatedCallbacks = {};
this._events = {};
// Subscriptions callbacks
this._onReadyCallbacks = {};
this._onStopCallbacks = {};
this._onErrorCallbacks = {};
// Methods callbacks
this._onResultCallbacks = {};
this._onUpdatedCallbacks = {};
this._events = {};
this._queue = [];
// Setup
this.readyState = -1;
this._reconnect_count = 0;
this._reconnect_incremental_timer = 0;
if (!options.do_not_autoconnect) this.connect();
};
DDP.prototype.constructor = DDP;
DDP.prototype.connect = function () {
this._socket = new this._SocketConstructor(this._endpoint);
this._socket.onopen = this._on_socket_open.bind(this);
this._socket.onmessage = this._on_socket_message.bind(this);
this._socket.onerror = this._on_socket_error.bind(this);
this._socket.onclose = this._on_socket_close.bind(this);
};
// Init
if (!options.do_not_autoconnect) this.connect();
};
DDP.prototype.constructor = DDP;
DDP.prototype.method = function (name, params, onResult, onUpdated) {
var id = uniqueId();
this._onResultCallbacks[id] = onResult;
this._onUpdatedCallbacks[id] = onUpdated;
this._send({
msg: "method",
id: id,
method: name,
params: params
});
};
DDP.prototype.connect = function () {
this.readyState = 0;
this._socket = new this._SocketConstructor(this._endpoint);
this._socket.onopen = this._on_socket_open.bind(this);
this._socket.onmessage = this._on_socket_message.bind(this);
this._socket.onerror = this._on_socket_error.bind(this);
this._socket.onclose = this._on_socket_close.bind(this);
};
DDP.prototype.sub = function (name, params, onReady) {
var id = uniqueId();
this._onReadyCallbacks[id] = onReady;
this._send({
msg: "sub",
id: id,
name: name,
params: params
});
};
DDP.prototype.method = function (name, params, onResult, onUpdated) {
var id = uniqueId();
this._onResultCallbacks[id] = onResult;
this._onUpdatedCallbacks[id] = onUpdated;
this._send({
msg: "method",
id: id,
method: name,
params: params
});
return id;
};
DDP.prototype.unsub = function (id) {
this._send({
msg: "unsub",
id: id
});
};
DDP.prototype.sub = function (name, params, onReady, onStop, onError) {
var id = uniqueId();
this._onReadyCallbacks[id] = onReady;
this._onStopCallbacks[id] = onStop;
this._onErrorCallbacks[id] = onError;
this._send({
msg: "sub",
id: id,
name: name,
params: params
});
return id;
};
DDP.prototype.on = function (name, handler) {
this._events[name] = this._events[name] || [];
this._events[name].push(handler);
};
DDP.prototype.unsub = function (id) {
this._send({
msg: "unsub",
id: id
});
};
DDP.prototype.off = function (name, handler) {
if (!this._events[name]) return;
this._events[name].splice(this._events[name].indexOf(handler), 1);
};
DDP.prototype.on = function (name, handler) {
this._events[name] = this._events[name] || [];
this._events[name].push(handler);
};
DDP.prototype._emit = function (name /* , arguments */) {
if (!this._events[name]) return;
var args = arguments;
var self = this;
this._events[name].forEach(function (handler) {
handler.apply(self, Array.prototype.slice.call(args, 1));
});
};
DDP.prototype.off = function (name, handler) {
if (!this._events[name]) return;
this._events[name].splice(this._events[name].indexOf(handler), 1);
};
DDP.prototype._send = function (object) {
var message;
if (typeof EJSON === "undefined") {
message = JSON.stringify(object);
} else {
message = EJSON.stringify(object);
}
this._socket.send(message);
};
DDP.prototype._emit = function (name /* , arguments */) {
if (!this._events[name]) return;
var args = arguments;
var self = this;
this._events[name].forEach(function (handler) {
handler.apply(self, Array.prototype.slice.call(args, 1));
});
};
DDP.prototype._try_reconnect = function () {
if (this._reconnect_count < MAX_RECONNECT_ATTEMPTS) {
setTimeout(this.connect.bind(this), this._reconnect_incremental_timer);
}
this._reconnect_count += 1;
this._reconnect_incremental_timer += TIMER_INCREMENT * this._reconnect_count;
};
DDP.prototype._send = function (object) {
if (this.readyState !== 1 && object.msg !== "connect") {
this._queue.push(object);
return;
}
var message;
if (typeof EJSON === "undefined") {
message = JSON.stringify(object);
} else {
message = EJSON.stringify(object);
}
if (this._debug) {
console.log(message);
}
this._socket.send(message);
};
DDP.prototype._on_result = function (data) {
DDP.prototype._try_reconnect = function () {
if (this._reconnect_count < MAX_RECONNECT_ATTEMPTS) {
setTimeout(this.connect.bind(this), this._reconnect_incremental_timer);
}
this._reconnect_count += 1;
this._reconnect_incremental_timer += TIMER_INCREMENT * this._reconnect_count;
};
DDP.prototype._on_result = function (data) {
if (this._onResultCallbacks[data.id]) {

@@ -118,9 +155,9 @@ this._onResultCallbacks[data.id](data.error, data.result);

delete this._onUpdatedCallbacks[data.id];
throw new Error(JSON.stringify(data.error));
throw data.error;
}
}
};
DDP.prototype._on_updated = function (data) {
var self = this;
data.methods.forEach(function (id) {
};
DDP.prototype._on_updated = function (data) {
var self = this;
data.methods.forEach(function (id) {
if (self._onUpdatedCallbacks[id]) {

@@ -130,15 +167,27 @@ self._onUpdatedCallbacks[id]();

}
});
};
DDP.prototype._on_nosub = function (data) {
if (this._onReadyCallbacks[data.id]) {
this._onReadyCallbacks[data.id](data.error);
});
};
DDP.prototype._on_nosub = function (data) {
if (data.error) {
if (!this._onErrorCallbacks[data.id]) {
delete this._onReadyCallbacks[data.id];
delete this._onStopCallbacks[data.id];
throw new Error(data.error);
}
this._onErrorCallbacks[data.id](data.error);
delete this._onReadyCallbacks[data.id];
} else {
throw new Error(JSON.stringify(data.error));
delete this._onStopCallbacks[data.id];
delete this._onErrorCallbacks[data.id];
return;
}
};
DDP.prototype._on_ready = function (data) {
var self = this;
data.subs.forEach(function (id) {
if (this._onStopCallbacks[data.id]) {
this._onStopCallbacks[data.id]();
}
delete this._onReadyCallbacks[data.id];
delete this._onStopCallbacks[data.id];
delete this._onErrorCallbacks[data.id];
};
DDP.prototype._on_ready = function (data) {
var self = this;
data.subs.forEach(function (id) {
if (self._onReadyCallbacks[id]) {

@@ -149,54 +198,83 @@ self._onReadyCallbacks[id]();

});
};
};
DDP.prototype._on_error = function (data) {
this._emit("error", data);
};
DDP.prototype._on_connected = function (data) {
this._reconnect_count = 0;
this._reconnect_incremental_timer = 0;
this._emit("connected", data);
};
DDP.prototype._on_failed = function (data) {
this._emit("failed", data);
};
DDP.prototype._on_added = function (data) {
this._emit("added", data);
};
DDP.prototype._on_removed = function (data) {
this._emit("removed", data);
};
DDP.prototype._on_changed = function (data) {
this._emit("changed", data);
};
DDP.prototype._on_error = function (data) {
this._emit("error", data);
};
DDP.prototype._on_connected = function (data) {
var self = this;
var firstCon = self._reconnect_count === 0;
var eventName = firstCon ? "connected" : "reconnected";
self.readyState = 1;
self._reconnect_count = 0;
self._reconnect_incremental_timer = 0;
var length = self._queue.length;
for (var i=0; i<length; i++) {
self._send(self._queue.shift());
}
self._emit(eventName, data);
// Set up keepalive ping-s
self._ping_interval_handle = setInterval(function () {
var id = uniqueId();
self._send({
msg: "ping",
id: id
});
}, self._ping_interval);
};
DDP.prototype._on_failed = function (data) {
this.readyState = 4;
this._emit("failed", data);
};
DDP.prototype._on_added = function (data) {
this._emit("added", data);
};
DDP.prototype._on_removed = function (data) {
this._emit("removed", data);
};
DDP.prototype._on_changed = function (data) {
this._emit("changed", data);
};
DDP.prototype._on_ping = function (data) {
this._send({
msg: "pong",
id: data.id
});
};
DDP.prototype._on_pong = function (data) {
// For now, do nothing.
};
DDP.prototype._on_socket_close = function () {
this._emit("socket_close");
if (this._autoreconnect) this._try_reconnect();
};
DDP.prototype._on_socket_error = function (e) {
this._emit("socket_error", e);
if (this._autoreconnect) this._try_reconnect();
};
DDP.prototype._on_socket_open = function () {
this._send({
msg: "connect",
version: "pre1",
support: ["pre1"]
});
};
DDP.prototype._on_socket_message = function (message) {
var data;
DDP.prototype._on_socket_close = function () {
clearInterval(this._ping_interval_handle);
this.readyState = 4;
this._emit("socket_close");
if (this._autoreconnect) this._try_reconnect();
};
DDP.prototype._on_socket_error = function (e) {
clearInterval(this._ping_interval_handle);
this.readyState = 4;
this._emit("socket_error", e);
};
DDP.prototype._on_socket_open = function () {
this._send({
msg: "connect",
version: "pre1",
support: ["pre1"]
});
};
DDP.prototype._on_socket_message = function (message) {
var data;
if (this._debug) console.log(message);
if (message.data === INIT_DDP_MESSAGE) return;
try {
if (typeof EJSON === "undefined") {
data = JSON.parse(message.data);
} else {
data = EJSON.parse(message.data);
}
if (message.data === INIT_DDP_MESSAGE) return;
try {
if (typeof EJSON === "undefined") {
data = JSON.parse(message.data);
} else {
data = EJSON.parse(message.data);
}
if (DDP_SERVER_MESSAGES.indexOf(data.msg) === -1) throw new Error();
} catch (e) {
console.warn("Non DDP message received:");
console.warn(message.data);
} catch (e) {
console.warn("Non DDP message received:");
console.warn(message.data);
return;

@@ -207,8 +285,4 @@ }

if (typeof module !== "undefined" && module.exports) {
module.exports = DDP;
} else {
window.DDP = DDP;
}
return DDP;
})();
}));
var gulp = require("gulp");
var tinyLr = require("tiny-lr");
var static = require("node-static");
var http = require("http");
var plugins = require("gulp-load-plugins")();
var concat = require("gulp-concat");
var lrServer = tinyLr();
var dvServer = http.createServer(function (req, res) {
var stServer = new static.Server("./test/", {cache: false});
req.on("end", function () {
stServer.serve(req, res);
});
req.resume();
});
gulp.task("_reload_target", function () {
gulp.src("test/ddp.js")
.pipe(plugins.livereload(lrServer));
});
gulp.task("_reload_tests", function () {
gulp.task("default", function () {
gulp.src("test/unit/*.unit.js")
.pipe(plugins.concat("ddp.unit.js"))
.pipe(gulp.dest("test/"))
.pipe(plugins.livereload(lrServer));
.pipe(concat("ddp.unit.js"))
.pipe(gulp.dest("test/"));
});
gulp.task("dev", function () {
dvServer.listen(8080);
lrServer.listen(35729);
gulp.watch("ddp.js", ["_reload_target"]);
gulp.watch("test/unit/**/*.unit.js", ["_reload_tests"]);
});
gulp.task("test-node", function () {
gulp.src("test/ddp.unit.js")
.pipe(plugins.mocha({reporter: "nyan"}));
});
gulp.task("test-browser", function () {
dvServer.listen(8080);
var options = {
url: "http://localhost:8080"
};
gulp.src("./test/index.html")
.pipe(plugins.open("", options));
});
gulp.task("default", function () {
console.log("");
console.log("Usage: gulp [TASK]");
console.log("");
console.log("Available tasks:");
console.log(" test-node run tests with mocha");
console.log(" test-browser run tests in the browser");
console.log(" dev set up dev environment with auto-recompiling and testing");
console.log("");
});
{
"name": "ddp.js",
"version": "0.3.1",
"version": "0.4.5",
"description": "ddp javascript client",
"main": "ddp.js",
"scripts": {
"test-node": "./node_modules/gulp/bin/gulp.js test-node",
"test-browser": "./node_modules/gulp/bin/gulp.js test-browser"
"test": "./node_modules/mocha/bin/mocha ./test/ddp.unit.js",
"test-node": "./node_modules/mocha/bin/mocha -R nyan ./test/ddp.unit.js",
"test-browser": "./node_modules/karma/bin/karma start test/karma.conf.js"
},

@@ -24,20 +25,19 @@ "repository": {

"devDependencies": {
"blanket": "^1.1.6",
"faye-websocket": "^0.7.2",
"gulp": "^3.5.6",
"coveralls": "^2.10.0",
"gulp": "^3.6.2",
"gulp-concat": "^2.2.0",
"gulp-livereload": "^1.3.1",
"gulp-load-plugins": "^0.4.0",
"gulp-mocha": "^0.4.1",
"gulp-open": "^0.2.8",
"gulp-rename": "^1.2.0",
"gulp-uglify": "^0.2.1",
"istanbul": "^0.2.10",
"karma": "^0.12.14",
"karma-chrome-launcher": "^0.1.3",
"karma-coverage": "^0.2.1",
"karma-firefox-launcher": "^0.1.3",
"karma-mocha": "^0.1.3",
"karma-safari-launcher": "^0.1.1",
"lodash": "^2.4.1",
"mocha": "^1.18.2",
"node-static": "^0.7.3",
"mocha-lcov-reporter": "0.0.1",
"should": "^3.2.0-beta1",
"sinon": "^1.9.0",
"tiny-lr": "0.0.5"
"sinon": "^1.9.0"
},
"dependencies": {}
}

@@ -0,1 +1,5 @@

[![Build Status](https://travis-ci.org/mondora/ddp.js.svg?branch=master)](https://travis-ci.org/mondora/ddp.js)
[![Coverage Status](https://coveralls.io/repos/mondora/ddp.js/badge.png)](https://coveralls.io/r/mondora/ddp.js)
[![Code Climate](https://codeclimate.com/github/mondora/ddp.js.png)](https://codeclimate.com/github/mondora/ddp.js)
#ddp.js

@@ -26,24 +30,25 @@

var options = {
endpoint: "http://localhost:3000/websocket",
SocketConstructor: WebSocket
};
var ddp = new DDP(options);
```javascript
var options = {
endpoint: "http://localhost:3000/websocket",
SocketConstructor: WebSocket
};
var ddp = new DDP(options);
ddp.on("connected", function () {
console.log("Connected");
ddp.on("connected", function () {
console.log("Connected");
ddp.sub("myCollection");
ddp.on("added", function (data) {
console.log(data.collection);
});
ddp.sub("myCollection");
ddp.on("added", function (data) {
console.log(data.collection);
});
var myLoginParams = { ... };
ddp.method("login", [myLoginParams], function (err, res) {
if (err) throw err;
console.log("Logged in!");
});
var myLoginParams = { ... };
ddp.method("login", [myLoginParams], function (err, res) {
if (err) throw err;
console.log("Logged in!");
});
});
```
##Tests

@@ -112,2 +117,7 @@

`connect` also sets the readyState property of the DDP instance
to 0 (connecting).
If the user tries to send DDP messages before the connection
is open (readyState equals 1), those messages get queued up
and sent, in order, once the connection is established.

@@ -117,2 +127,3 @@

###DDP.method(name, params, onResult, onUpdated)

@@ -119,0 +130,0 @@

@@ -8,3 +8,3 @@ if (typeof window === "undefined") {

GLB.SockJS = require("./mocks/sockjs.js");
GLB.DDP = require("./ddp.js");
GLB.DDP = require("../ddp.js");
} else {

@@ -111,2 +111,8 @@ ENV = "browser";

it("should set the readyState to 0", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp.connect();
ddp.readyState.should.equal(0);
});
it("should instanciate a new _SocketConstructor instance and sotre a reference to it in the _socket property", function () {

@@ -192,2 +198,12 @@ var ddp = new DDP(optionsAutoconnect);

it("should return the id of the method", function (done) {
var ddp = new DDP(optionsAutoconnect);
var handler = function () {};
ddp.on("connected", function () {
var id = ddp.method("method", [""], handler);
ddp._onResultCallbacks[id].should.equal(handler);
done();
});
});
});

@@ -212,8 +228,8 @@

it("should register its thrid argument as handler for the \"ready\" and \"nosub\" events", function (done) {
it("should register its thrid argument as handler for the \"ready\" event", function (done) {
var ddp = new DDP(optionsAutoconnect);
var handler = function () {};
var onReady = function () {};
ddp.on("connected", function () {
ddp.sub("method", [""], handler);
_.contains(ddp._onReadyCallbacks, handler).should.be.true;
ddp.sub("method", [""], onReady);
_.contains(ddp._onReadyCallbacks, onReady).should.be.true;
done();

@@ -223,2 +239,24 @@ });

it("should register its fourth and fifth arguments as handlers for the \"nosub\" event", function (done) {
var ddp = new DDP(optionsAutoconnect);
var onStop = function () {};
var onError = function () {};
ddp.on("connected", function () {
ddp.sub("method", [""], null, onStop, onError);
_.contains(ddp._onStopCallbacks, onStop).should.be.true;
_.contains(ddp._onErrorCallbacks, onError).should.be.true;
done();
});
});
it("should return the id of the subscription", function (done) {
var ddp = new DDP(optionsAutoconnect);
var onReady = function () {};
ddp.on("connected", function () {
var id = ddp.sub("method", [""], onReady);
ddp._onReadyCallbacks[id].should.equal(onReady);
done();
});
});
});

@@ -311,31 +349,49 @@

it("should stringify the first argument passed to it with EJSON if available", function () {
var ddp = new DDP(optionsAutoconnect);
ddp._socket.send = _.noop;
var obj = {};
GLB.EJSON = {
stringify: sinon.spy()
};
ddp._send(obj);
EJSON.stringify.calledWith(obj).should.be.true;
delete GLB.EJSON;
});
describe("if the \"readyState\" property of the DDP instance equals 1", function () {
it("should stringify the first argument passed to it with JSON if EJSON is not available", function () {
var ddp = new DDP(optionsAutoconnect);
ddp._socket.send = _.noop;
var obj = {};
var tmp = JSON.stringify;
JSON.stringify = sinon.spy();
ddp._send(obj);
JSON.stringify.calledWith(obj).should.be.true;
JSON.stringify = tmp;
it("should stringify the first argument passed to it with EJSON if available", function () {
var ddp = new DDP(optionsAutoconnect);
ddp._socket.send = _.noop;
ddp.readyState = 1;
var obj = {};
GLB.EJSON = {
stringify: sinon.spy()
};
ddp._send(obj);
EJSON.stringify.calledWith(obj).should.be.true;
delete GLB.EJSON;
});
it("should stringify the first argument passed to it with JSON if EJSON is not available", function () {
var ddp = new DDP(optionsAutoconnect);
ddp._socket.send = _.noop;
ddp.readyState = 1;
var obj = {};
var tmp = JSON.stringify;
JSON.stringify = sinon.spy();
ddp._send(obj);
JSON.stringify.calledWith(obj).should.be.true;
JSON.stringify = tmp;
});
it("should call the _socket.send method with the stringified object as first argument", function () {
var ddp = new DDP(optionsAutoconnect);
ddp._socket.send = sinon.spy();
ddp.readyState = 1;
var obj = {};
ddp._send(obj);
ddp._socket.send.calledWith("{}").should.be.true;
});
});
it("should call the _socket.send method with the stringified object as first argument", function () {
var ddp = new DDP(optionsAutoconnect);
ddp._socket.send = sinon.spy();
var obj = {};
ddp._send(obj);
ddp._socket.send.calledWith("{}").should.be.true;
describe("if the \"readyState\" property of the DDP instance does not equal 1", function () {
it("should add its first argument at the end of the \"_queue\" array", function () {
var ddp = new DDP(optionsAutoconnect);
ddp.readyState = 0;
var obj = {};
ddp._send(obj);
ddp._queue[ddp._queue.length - 1].should.equal(obj);
});
});

@@ -509,7 +565,40 @@

describe("receives as only argument an object containing the properties id and error, and", function () {
describe("receives as only argument an object containing the properties id and an optional error, and", function () {
describe("if _onReadyCallbacks[id] exists", function () {
describe("if error is defined", function () {
it("should call it with error as first argument", function () {
describe("if an error handler exists", function () {
it("should call the error handler with error as first argument", function () {
var ddp = new DDP(optionsDontAutoconnect);
var obj = {
id: "0",
error: {}
};
var cb = sinon.spy();
ddp._onErrorCallbacks[0] = cb;
ddp._on_nosub(obj);
cb.calledWith(obj.error).should.be.true;
});
});
describe("if an error handler doesn't exist", function () {
it("should throw the error", function () {
var ddp = new DDP(optionsDontAutoconnect);
var obj = {
id: "0",
error: {}
};
var cb = sinon.spy();
var troublemaker = function () {
ddp._on_nosub(obj);
};
troublemaker.should.throw(obj.error);
});
});
it("should delete all handlers for the subscription", function () {
var ddp = new DDP(optionsDontAutoconnect);

@@ -520,34 +609,38 @@ var obj = {

};
var cb = sinon.spy();
ddp._onReadyCallbacks[0] = cb;
ddp._onReadyCallbacks[0] = _.noop;
ddp._onStopCallbacks[0] = _.noop;
ddp._onErrorCallbacks[0] = _.noop;
ddp._on_nosub(obj);
cb.calledWith(obj.error).should.be.true;
_.isUndefined(ddp._onReadyCallbacks[0]).should.be.true;
_.isUndefined(ddp._onStopCallbacks[0]).should.be.true;
_.isUndefined(ddp._onErrorCallbacks[0]).should.be.true;
});
it("should delete that reference to the function after calling it", function () {
});
describe("if error is not defined", function () {
it("should call the stop handler", function () {
var ddp = new DDP(optionsDontAutoconnect);
var obj = {
id: "0",
error: {}
id: "0"
};
var cb = sinon.spy();
ddp._onReadyCallbacks[0] = cb;
ddp._onStopCallbacks[0] = cb;
ddp._on_nosub(obj);
cb.calledWith(obj.error).should.be.true;
_.isUndefined(ddp._onReadyCallbacks[0]).should.be.true;
cb.called.should.be.true;
});
});
describe("if _onReadyCallbacks[id] doesn't exist", function () {
it("should throw an error", function () {
it("should delete all handlers for the subscription", function () {
var ddp = new DDP(optionsDontAutoconnect);
var obj = {
id: "0",
error: {}
id: "0"
};
(function () {
ddp._on_nosub(obj);
}).should.throw(obj.error);
ddp._onReadyCallbacks[0] = _.noop;
ddp._onStopCallbacks[0] = _.noop;
ddp._onErrorCallbacks[0] = _.noop;
ddp._on_nosub(obj);
_.isUndefined(ddp._onReadyCallbacks[0]).should.be.true;
_.isUndefined(ddp._onStopCallbacks[0]).should.be.true;
_.isUndefined(ddp._onErrorCallbacks[0]).should.be.true;
});

@@ -625,16 +718,32 @@

it("should call the emit method, with \"connected\" as the first argument and its first argument as second argument", function () {
var ddp = new DDP(optionsDontAutoconnect);
var arg = {};
ddp._emit = sinon.spy();
ddp._on_connected(arg);
ddp._emit.calledWith("connected", arg).should.be.true;
describe("if this is a first connection", function () {
it("should call the emit method, with \"connected\" as the first argument and its first argument as second argument", function () {
var ddp = new DDP(optionsDontAutoconnect);
var arg = {};
ddp._emit = sinon.spy();
ddp._on_connected(arg);
ddp._emit.calledWith("connected", arg).should.be.true;
});
});
describe("if this is a reconnection", function () {
it("should call the emit method, with \"reconnected\" as the first argument and its first argument as second argument", function () {
var ddp = new DDP(optionsDontAutoconnect);
var arg = {};
ddp._emit = sinon.spy();
ddp._reconnect_count = 1;
ddp._on_connected(arg);
ddp._emit.calledWith("reconnected", arg).should.be.true;
});
});
it("should reset _reconnect_count and _reconnect_incremental_timer to 0", function () {
var ddp = new DDP(optionsDontAutoconnect);
var arg = {};
ddp._reconnect_count = 1;
ddp._reconnect_incremental_timer = 1;
ddp._on_connected(arg);
ddp._on_connected();
ddp._reconnect_count.should.equal(0);

@@ -644,2 +753,28 @@ ddp._reconnect_incremental_timer.should.equal(0);

it("should set the readyState to 1", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp._on_connected();
ddp.readyState.should.equal(1);
});
it("should send (calling the \"_send\" method) all queued messages in order", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp._queue = [0, 1, 2, 3];
var length = ddp._queue.length;
ddp._send = sinon.spy();
ddp._on_connected();
ddp._send.callCount.should.equal(length);
for (var i=0; i<length; i++) {
ddp._send.getCall(i).args[0].should.equal(i);
}
});
it("should empty the queue", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp._queue = [0, 1, 2, 3];
ddp._send = _.noop;
ddp._on_connected();
ddp._queue.length.should.equal(0);
});
});

@@ -657,2 +792,8 @@

it("should set the readyState to 4", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp._on_failed();
ddp.readyState.should.equal(4);
});
});

@@ -706,2 +847,8 @@

it("should set the readyState to 4", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp._on_socket_close();
ddp.readyState.should.equal(4);
});
it("should call the _try_reconnect method if _autoreconnect is truthy", function () {

@@ -736,19 +883,8 @@ var ddp = new DDP(optionsDontAutoconnect);

it("should call the _try_reconnect method if _autoreconnect is truthy", function () {
it("should set the readyState to 4", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp._emit = _.noop;
ddp._try_reconnect = sinon.spy();
ddp._on_socket_error();
ddp._try_reconnect.called.should.be.true;
ddp.readyState.should.equal(4);
});
it("should not call the _try_reconnect method if _autoreconnect is falsy", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp._autoreconnect = false;
ddp._emit = _.noop;
ddp._try_reconnect = sinon.spy();
ddp._on_socket_error();
ddp._try_reconnect.called.should.be.false;
});
});

@@ -755,0 +891,0 @@

@@ -8,3 +8,3 @@ if (typeof window === "undefined") {

GLB.SockJS = require("./mocks/sockjs.js");
GLB.DDP = require("./ddp.js");
GLB.DDP = require("../ddp.js");
} else {

@@ -11,0 +11,0 @@ ENV = "browser";

@@ -11,2 +11,8 @@ describe("The connect method", function () {

it("should set the readyState to 0", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp.connect();
ddp.readyState.should.equal(0);
});
it("should instanciate a new _SocketConstructor instance and sotre a reference to it in the _socket property", function () {

@@ -13,0 +19,0 @@ var ddp = new DDP(optionsAutoconnect);

@@ -38,2 +38,12 @@ describe("The method method", function () {

it("should return the id of the method", function (done) {
var ddp = new DDP(optionsAutoconnect);
var handler = function () {};
ddp.on("connected", function () {
var id = ddp.method("method", [""], handler);
ddp._onResultCallbacks[id].should.equal(handler);
done();
});
});
});

@@ -18,8 +18,8 @@ describe("The sub method", function () {

it("should register its thrid argument as handler for the \"ready\" and \"nosub\" events", function (done) {
it("should register its thrid argument as handler for the \"ready\" event", function (done) {
var ddp = new DDP(optionsAutoconnect);
var handler = function () {};
var onReady = function () {};
ddp.on("connected", function () {
ddp.sub("method", [""], handler);
_.contains(ddp._onReadyCallbacks, handler).should.be.true;
ddp.sub("method", [""], onReady);
_.contains(ddp._onReadyCallbacks, onReady).should.be.true;
done();

@@ -29,2 +29,24 @@ });

it("should register its fourth and fifth arguments as handlers for the \"nosub\" event", function (done) {
var ddp = new DDP(optionsAutoconnect);
var onStop = function () {};
var onError = function () {};
ddp.on("connected", function () {
ddp.sub("method", [""], null, onStop, onError);
_.contains(ddp._onStopCallbacks, onStop).should.be.true;
_.contains(ddp._onErrorCallbacks, onError).should.be.true;
done();
});
});
it("should return the id of the subscription", function (done) {
var ddp = new DDP(optionsAutoconnect);
var onReady = function () {};
ddp.on("connected", function () {
var id = ddp.sub("method", [""], onReady);
ddp._onReadyCallbacks[id].should.equal(onReady);
done();
});
});
});
describe("The _send private method", function () {
it("should stringify the first argument passed to it with EJSON if available", function () {
var ddp = new DDP(optionsAutoconnect);
ddp._socket.send = _.noop;
var obj = {};
GLB.EJSON = {
stringify: sinon.spy()
};
ddp._send(obj);
EJSON.stringify.calledWith(obj).should.be.true;
delete GLB.EJSON;
});
describe("if the \"readyState\" property of the DDP instance equals 1", function () {
it("should stringify the first argument passed to it with JSON if EJSON is not available", function () {
var ddp = new DDP(optionsAutoconnect);
ddp._socket.send = _.noop;
var obj = {};
var tmp = JSON.stringify;
JSON.stringify = sinon.spy();
ddp._send(obj);
JSON.stringify.calledWith(obj).should.be.true;
JSON.stringify = tmp;
it("should stringify the first argument passed to it with EJSON if available", function () {
var ddp = new DDP(optionsAutoconnect);
ddp._socket.send = _.noop;
ddp.readyState = 1;
var obj = {};
GLB.EJSON = {
stringify: sinon.spy()
};
ddp._send(obj);
EJSON.stringify.calledWith(obj).should.be.true;
delete GLB.EJSON;
});
it("should stringify the first argument passed to it with JSON if EJSON is not available", function () {
var ddp = new DDP(optionsAutoconnect);
ddp._socket.send = _.noop;
ddp.readyState = 1;
var obj = {};
var tmp = JSON.stringify;
JSON.stringify = sinon.spy();
ddp._send(obj);
JSON.stringify.calledWith(obj).should.be.true;
JSON.stringify = tmp;
});
it("should call the _socket.send method with the stringified object as first argument", function () {
var ddp = new DDP(optionsAutoconnect);
ddp._socket.send = sinon.spy();
ddp.readyState = 1;
var obj = {};
ddp._send(obj);
ddp._socket.send.calledWith("{}").should.be.true;
});
});
it("should call the _socket.send method with the stringified object as first argument", function () {
var ddp = new DDP(optionsAutoconnect);
ddp._socket.send = sinon.spy();
var obj = {};
ddp._send(obj);
ddp._socket.send.calledWith("{}").should.be.true;
describe("if the \"readyState\" property of the DDP instance does not equal 1", function () {
it("should add its first argument at the end of the \"_queue\" array", function () {
var ddp = new DDP(optionsAutoconnect);
ddp.readyState = 0;
var obj = {};
ddp._send(obj);
ddp._queue[ddp._queue.length - 1].should.equal(obj);
});
});
});
describe("The _on_nosub private method", function () {
describe("receives as only argument an object containing the properties id and error, and", function () {
describe("receives as only argument an object containing the properties id and an optional error, and", function () {
describe("if _onReadyCallbacks[id] exists", function () {
describe("if error is defined", function () {
it("should call it with error as first argument", function () {
describe("if an error handler exists", function () {
it("should call the error handler with error as first argument", function () {
var ddp = new DDP(optionsDontAutoconnect);
var obj = {
id: "0",
error: {}
};
var cb = sinon.spy();
ddp._onErrorCallbacks[0] = cb;
ddp._on_nosub(obj);
cb.calledWith(obj.error).should.be.true;
});
});
describe("if an error handler doesn't exist", function () {
it("should throw the error", function () {
var ddp = new DDP(optionsDontAutoconnect);
var obj = {
id: "0",
error: {}
};
var cb = sinon.spy();
var troublemaker = function () {
ddp._on_nosub(obj);
};
troublemaker.should.throw(obj.error);
});
});
it("should delete all handlers for the subscription", function () {
var ddp = new DDP(optionsDontAutoconnect);

@@ -13,34 +46,38 @@ var obj = {

};
var cb = sinon.spy();
ddp._onReadyCallbacks[0] = cb;
ddp._onReadyCallbacks[0] = _.noop;
ddp._onStopCallbacks[0] = _.noop;
ddp._onErrorCallbacks[0] = _.noop;
ddp._on_nosub(obj);
cb.calledWith(obj.error).should.be.true;
_.isUndefined(ddp._onReadyCallbacks[0]).should.be.true;
_.isUndefined(ddp._onStopCallbacks[0]).should.be.true;
_.isUndefined(ddp._onErrorCallbacks[0]).should.be.true;
});
it("should delete that reference to the function after calling it", function () {
});
describe("if error is not defined", function () {
it("should call the stop handler", function () {
var ddp = new DDP(optionsDontAutoconnect);
var obj = {
id: "0",
error: {}
id: "0"
};
var cb = sinon.spy();
ddp._onReadyCallbacks[0] = cb;
ddp._onStopCallbacks[0] = cb;
ddp._on_nosub(obj);
cb.calledWith(obj.error).should.be.true;
_.isUndefined(ddp._onReadyCallbacks[0]).should.be.true;
cb.called.should.be.true;
});
});
describe("if _onReadyCallbacks[id] doesn't exist", function () {
it("should throw an error", function () {
it("should delete all handlers for the subscription", function () {
var ddp = new DDP(optionsDontAutoconnect);
var obj = {
id: "0",
error: {}
id: "0"
};
(function () {
ddp._on_nosub(obj);
}).should.throw(obj.error);
ddp._onReadyCallbacks[0] = _.noop;
ddp._onStopCallbacks[0] = _.noop;
ddp._onErrorCallbacks[0] = _.noop;
ddp._on_nosub(obj);
_.isUndefined(ddp._onReadyCallbacks[0]).should.be.true;
_.isUndefined(ddp._onStopCallbacks[0]).should.be.true;
_.isUndefined(ddp._onErrorCallbacks[0]).should.be.true;
});

@@ -47,0 +84,0 @@

describe("The _on_connected private method", function () {
it("should call the emit method, with \"connected\" as the first argument and its first argument as second argument", function () {
var ddp = new DDP(optionsDontAutoconnect);
var arg = {};
ddp._emit = sinon.spy();
ddp._on_connected(arg);
ddp._emit.calledWith("connected", arg).should.be.true;
describe("if this is a first connection", function () {
it("should call the emit method, with \"connected\" as the first argument and its first argument as second argument", function () {
var ddp = new DDP(optionsDontAutoconnect);
var arg = {};
ddp._emit = sinon.spy();
ddp._on_connected(arg);
ddp._emit.calledWith("connected", arg).should.be.true;
});
});
describe("if this is a reconnection", function () {
it("should call the emit method, with \"reconnected\" as the first argument and its first argument as second argument", function () {
var ddp = new DDP(optionsDontAutoconnect);
var arg = {};
ddp._emit = sinon.spy();
ddp._reconnect_count = 1;
ddp._on_connected(arg);
ddp._emit.calledWith("reconnected", arg).should.be.true;
});
});
it("should reset _reconnect_count and _reconnect_incremental_timer to 0", function () {
var ddp = new DDP(optionsDontAutoconnect);
var arg = {};
ddp._reconnect_count = 1;
ddp._reconnect_incremental_timer = 1;
ddp._on_connected(arg);
ddp._on_connected();
ddp._reconnect_count.should.equal(0);

@@ -21,2 +37,28 @@ ddp._reconnect_incremental_timer.should.equal(0);

it("should set the readyState to 1", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp._on_connected();
ddp.readyState.should.equal(1);
});
it("should send (calling the \"_send\" method) all queued messages in order", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp._queue = [0, 1, 2, 3];
var length = ddp._queue.length;
ddp._send = sinon.spy();
ddp._on_connected();
ddp._send.callCount.should.equal(length);
for (var i=0; i<length; i++) {
ddp._send.getCall(i).args[0].should.equal(i);
}
});
it("should empty the queue", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp._queue = [0, 1, 2, 3];
ddp._send = _.noop;
ddp._on_connected();
ddp._queue.length.should.equal(0);
});
});

@@ -11,2 +11,8 @@ describe("The _on_failed private method", function () {

it("should set the readyState to 4", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp._on_failed();
ddp.readyState.should.equal(4);
});
});

@@ -11,2 +11,8 @@ describe("The _on_socket_close private method", function () {

it("should set the readyState to 4", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp._on_socket_close();
ddp.readyState.should.equal(4);
});
it("should call the _try_reconnect method if _autoreconnect is truthy", function () {

@@ -13,0 +19,0 @@ var ddp = new DDP(optionsDontAutoconnect);

@@ -11,19 +11,8 @@ describe("The _on_socket_error private method", function () {

it("should call the _try_reconnect method if _autoreconnect is truthy", function () {
it("should set the readyState to 4", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp._emit = _.noop;
ddp._try_reconnect = sinon.spy();
ddp._on_socket_error();
ddp._try_reconnect.called.should.be.true;
ddp.readyState.should.equal(4);
});
it("should not call the _try_reconnect method if _autoreconnect is falsy", function () {
var ddp = new DDP(optionsDontAutoconnect);
ddp._autoreconnect = false;
ddp._emit = _.noop;
ddp._try_reconnect = sinon.spy();
ddp._on_socket_error();
ddp._try_reconnect.called.should.be.false;
});
});

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