Comparing version 0.4.6 to 0.5.0
@@ -0,1 +1,13 @@ | ||
0.5.0 - | ||
- Use ddp-ejson instead of meteor-ejson. ddp-ejson is a repackage of Meteor's latest EJSON package | ||
- Use ddp-srp insead of node-srp. ddp-srp is a repackage of Meteor's latest SRP package | ||
- Added second callback to ddpclient.call, executed when the DDP `updated` message is received | ||
- Allow automatic EJSON serialization/deserialization of ObjectIDs | ||
- Expose EJSON package to allow for addition of custom EJSON types | ||
- added DDP pre2 support | ||
- DDP version negotiation | ||
- DDP heartbeat support (reply only) | ||
- `ddpclient.callWithRandomSeed` supports client-generated `_id`s | ||
0.4.6 - 2014-04-28 | ||
@@ -7,3 +19,3 @@ | ||
- Fix login with password method to return login token | ||
- Fix login with password method to return login token | ||
@@ -10,0 +22,0 @@ 0.4.4 - 2014-02-09 |
var DDPClient = require("../lib/ddp-client"); | ||
var ddpclient = new DDPClient({ | ||
host: "localhost", | ||
host: "localhost", | ||
port: 3000, | ||
@@ -9,8 +9,11 @@ /* optional: */ | ||
auto_reconnect_timer: 500, | ||
use_ejson: true // default is false | ||
use_ejson: true, // Use Meteor's EJSON to preserve certain data types. | ||
use_ssl: false, | ||
maintain_collections: true // Set to false to maintain your own collections. | ||
}); | ||
/* | ||
* Connect to the Meteor Server | ||
*/ | ||
ddpclient.connect(function(error) { | ||
console.log('connected!'); | ||
if (error) { | ||
@@ -21,10 +24,61 @@ console.log('DDP connection error!'); | ||
ddpclient.call('test-function', ['foo', 'bar'], function(err, result) { | ||
console.log('called function, result: ' + result); | ||
}); | ||
console.log('connected!'); | ||
ddpclient.subscribe('posts', [], function() { | ||
console.log('posts complete:'); | ||
console.log(ddpclient.collections.posts); | ||
}); | ||
/* | ||
* Uncomment to log in with username/password | ||
*/ | ||
// ddpclient.loginWithUsername("username", "password", function (err, result) { | ||
// result contains your auth token | ||
setTimeout(function () { | ||
/* | ||
* Call a Meteor Method | ||
*/ | ||
ddpclient.call( | ||
'deletePosts', // name of Meteor Method being called | ||
['foo', 'bar'], // parameters to send to Meteor Method | ||
function (err, result) { // callback which returns the method call results | ||
console.log('called function, result: ' + result); | ||
}, | ||
function () { // callback which fires when server has finished | ||
console.log('updated'); // sending any updated documents as a result of | ||
console.log(ddpclient.collections.posts); // calling this method | ||
} | ||
); | ||
}, 3000); | ||
/* | ||
* Call a Meteor Method while passing in a random seed. | ||
* Added in DDP pre2, the random seed will be used on the server to generate | ||
* repeatable IDs. This allows the same id to be generated on the client and server | ||
*/ | ||
var Random = require("ddp-random"), | ||
random = Random.createWithSeeds("randomSeed"); // seed an id generator | ||
ddpclient.callWithRandomSeed( | ||
'createPost', // name of Meteor Method being called | ||
[{ _id : random.id(), // generate the id on the client | ||
body : "asdf" }], | ||
"randomSeed", // pass the same seed to the server | ||
function (err, result) { // callback which returns the method call results | ||
console.log('called function, result: ' + result); | ||
}, | ||
function () { // callback which fires when server has finished | ||
console.log('updated'); // sending any updated documents as a result of | ||
console.log(ddpclient.collections.posts); // calling this method | ||
} | ||
); | ||
/* | ||
* Subscribe to a Meteor Collection | ||
*/ | ||
ddpclient.subscribe( | ||
'posts', // name of Meteor Publish function to subscribe to | ||
[], // any parameters used by the Publish function | ||
function () { // callback when the subscription is complete | ||
console.log('posts complete:'); | ||
console.log(ddpclient.collections.posts); | ||
} | ||
); | ||
// }); | ||
}); | ||
@@ -35,3 +89,3 @@ | ||
*/ | ||
ddpclient.on('message', function(msg) { | ||
ddpclient.on('message', function (msg) { | ||
console.log("ddp message: " + msg); | ||
@@ -38,0 +92,0 @@ }); |
@@ -5,4 +5,5 @@ var WebSocket = require('faye-websocket'), | ||
events = require('events'), | ||
EJSON = require('meteor-ejson'), | ||
SRP = require('node-srp'), | ||
EJSON = require('ddp-ejson'), | ||
SRP = require('ddp-srp'), | ||
Random = require('ddp-random'), | ||
@@ -21,3 +22,3 @@ DDPClient = function(opts) { | ||
self.maintain_collections = ('maintain_collections' in opts) ? opts.maintain_collections : true; | ||
//May not work with faye-websockets | ||
@@ -29,2 +30,10 @@ self.use_ssl_strict = ('use_ssl_strict' in opts) ? opts.use_ssl_strict : true; | ||
// support multiple ddp versions | ||
self.ddp_version = ('ddp_version' in opts) ? opts.ddp_version : 'pre2'; | ||
self.supported_ddp_versions = ['pre2', 'pre1']; | ||
// Expose EJSON object, so client can use EJSON.addType(...) | ||
if (self.use_ejson) | ||
self.EJSON = EJSON | ||
// very very simple collections (name -> [{id -> document}]) | ||
@@ -37,2 +46,3 @@ if (self.maintain_collections) | ||
self._callbacks = {}; | ||
self._updatedCallbacks = {}; | ||
}; | ||
@@ -45,2 +55,3 @@ | ||
DDPClient.prototype._prepareHandlers = function() { | ||
@@ -51,17 +62,7 @@ var self = this; | ||
// just go ahead and open the connection on connect | ||
var connectionPayload = { | ||
self._send({ | ||
msg: 'connect', | ||
version: 'pre1', | ||
support: ['pre1'] | ||
}; | ||
// if reconnecting, try existing DDP session | ||
// removed for now, per conversation with sixolet on IRC, | ||
// reconnect on server side still needs work | ||
/* | ||
if (self.session) connectionPayload.session = self.session; | ||
*/ | ||
self._send(connectionPayload); | ||
version: self.ddp_version, | ||
support: self.supported_ddp_versions | ||
}); | ||
}); | ||
@@ -91,3 +92,3 @@ | ||
} | ||
} | ||
}; | ||
@@ -107,8 +108,3 @@ DDPClient.prototype._recoverNetworkError = function() { | ||
if (self.use_ejson) { | ||
data = EJSON.stringify(data); | ||
} else { | ||
data = JSON.stringify(data); | ||
} | ||
data = self.use_ejson ? EJSON.stringify(data) : JSON.stringify(data); | ||
this.socket.send(data); | ||
@@ -130,4 +126,2 @@ | ||
// TODO: 'error' | ||
// TODO: 'updated' -- not sure exactly what the point is here | ||
// TODO: 'addedBefore' -- not yet implemented in Meteor | ||
@@ -140,3 +134,9 @@ // TODO: 'movedBefore' -- not yet implemented in Meteor | ||
} else if (data.msg === 'failed') { | ||
self.emit('failed', data); | ||
if (self.supported_ddp_versions.indexOf(data.version) !== -1) { | ||
this.ddp_version = data.version; | ||
self.connect(); | ||
} else { | ||
self.auto_reconnect = false; | ||
self.emit('error', 'cannot negotiate ddp version'); | ||
} | ||
@@ -156,2 +156,13 @@ } else if (data.msg === 'connected') { | ||
// method updated | ||
} else if (data.msg === 'updated') { | ||
_.each(data.methods, function (method) { | ||
var cb = self._updatedCallbacks[method]; | ||
if (cb) { | ||
cb(); | ||
delete self._updatedCallbacks[method]; | ||
} | ||
}); | ||
// missing subscription | ||
@@ -224,2 +235,8 @@ } else if (data.msg === 'nosub') { | ||
}); | ||
// minimal heartbeat response for ddp pre2 | ||
} else if (data.msg === 'ping') { | ||
self._send( | ||
_.has(data, "id") ? { msg : 'pong', id : data.id } : { msg : 'pong' } | ||
); | ||
} | ||
@@ -262,5 +279,2 @@ }; | ||
var protocol = self.use_ssl ? 'wss://' : 'ws://'; | ||
//XXX: These aren't in faye-websocket. Are they still needed? | ||
//var options = self.use_ssl ? {rejectUnauthorized: self.use_ssl_strict} : {}; | ||
//self.socket = new WebSocket.Client(protocol + self.host + ':' + self.port + '/' + self.path, options); | ||
self.socket = new WebSocket.Client(protocol + self.host + ':' + self.port + '/' + self.path); | ||
@@ -297,3 +311,3 @@ self._prepareHandlers(); | ||
} else { | ||
callback({error:"The HAMK doesn't match. Possible MITM attack"}); | ||
callback({ error : "The HAMK doesn't match. Possible MITM attack" }); | ||
} | ||
@@ -303,11 +317,11 @@ | ||
}); | ||
} | ||
}; | ||
DDPClient.prototype.loginWithEmail = function(email, password, callback) { | ||
this._login({email: email}, password, callback); | ||
} | ||
}; | ||
DDPClient.prototype.loginWithUsername = function(username, password, callback) { | ||
this._login({username: username}, password, callback); | ||
} | ||
}; | ||
@@ -317,3 +331,3 @@ DDPClient.prototype.loginWithToken = function(token, callback) { | ||
self.call("login", [{resume: token}], callback); | ||
} | ||
}; | ||
@@ -329,3 +343,3 @@ DDPClient.prototype.close = function() { | ||
// callback = function(err, result) | ||
DDPClient.prototype.call = function(name, params, callback) { | ||
DDPClient.prototype.call = function(name, params, callback, updatedCallback) { | ||
var self = this; | ||
@@ -337,5 +351,21 @@ var id = self._nextId(); | ||
if (updatedCallback) | ||
self._updatedCallbacks[id] = updatedCallback; | ||
self._send({msg: 'method', id: id, method: name, params: params}); | ||
}; | ||
DDPClient.prototype.callWithRandomSeed = function(name, params, randomSeed, callback, updatedCallback) { | ||
var self = this; | ||
var id = self._nextId(); | ||
if (callback) | ||
self._callbacks[id] = callback; | ||
if (updatedCallback) | ||
self._updatedCallbacks[id] = updatedCallback; | ||
self._send({msg: 'method', id: id, method: name, randomSeed: randomSeed, params: params}); | ||
}; | ||
// open a subscription on the server, callback should handle on ready and nosub | ||
@@ -342,0 +372,0 @@ DDPClient.prototype.subscribe = function(name, params, callback) { |
{ | ||
"name": "ddp", | ||
"version": "0.4.6", | ||
"version": "0.5.0", | ||
"description": "Node.js module to connect to servers using DDP protocol.", | ||
@@ -24,6 +24,7 @@ "author": "Tom Coleman <tom@thesnail.org> (http://tom.thesnail.org)", | ||
"dependencies": { | ||
"underscore": ">=1.3.3", | ||
"meteor-ejson": ">=0.6.3", | ||
"node-srp": ">=0.0.1", | ||
"faye-websocket": "~0.7.1" | ||
"underscore": "1.5.2", | ||
"ddp-ejson": "0.8.1-2", | ||
"ddp-srp": "0.8.1-1", | ||
"faye-websocket": "~0.7.1", | ||
"ddp-random": "~0.8.1-1" | ||
}, | ||
@@ -30,0 +31,0 @@ "devDependencies": { |
@@ -5,3 +5,3 @@ var assert = require('assert'), | ||
events = require('events'), | ||
EJSON = require('meteor-ejson'); | ||
EJSON = require('ddp-ejson'); | ||
@@ -106,2 +106,11 @@ var DDPClient = rewire("../lib/ddp-client"); | ||
it('should expose the EJSON object', function(done) { | ||
var ddpclient = new DDPClient(); | ||
assert(ddpclient.EJSON); | ||
assert(ddpclient.EJSON.addType); | ||
done(); | ||
}); | ||
it('should not be used when disabled', function(done) { | ||
@@ -108,0 +117,0 @@ var ddpclient = new DDPClient({ use_ejson : false }); |
Sorry, the diff of this file is not supported yet
30212
13
586
147
5
+ Addedddp-ejson@0.8.1-2
+ Addedddp-random@~0.8.1-1
+ Addedddp-srp@0.8.1-1
+ Addedddp-ejson@0.8.1-2(transitive)
+ Addedddp-random@0.8.1-1(transitive)
+ Addedddp-srp@0.8.1-1(transitive)
+ Addedunderscore@1.5.2(transitive)
- Removedmeteor-ejson@>=0.6.3
- Removednode-srp@>=0.0.1
- Removedmeteor-ejson@0.6.3(transitive)
- Removednode-srp@0.0.1(transitive)
- Removedunderscore@1.13.71.4.4(transitive)
Updatedunderscore@1.5.2