substack/dnode — Issue #74: bug with EventEmitter and .toJSON()
Author: humanchimp
Created: 2011-11-04 18:21:57+00:00
State: open

Hi there —

I have run into an issue, but haven't gotten to the bottom of it yet. It can be reproduced by defining an EventEmitter that has a prototype.toJSON implementation, adding a listener to it, and then sending it through dnode. Although I can work around the issue, it appears to be a bug, so I thought I would make a ticket.

server.js

````javascript
var EventEmitter = require('events').EventEmitter
, dnode = require('dnode')
, collection
, api
;

function ArrayLike() {
EventEmitter.call(this);
}

ArrayLike.prototype = new EventEmitter;

ArrayLike.prototype.toJSON = function () {
return [].slice.call(this);
};

collection = new ArrayLike;

// commenting the following line will bypass the bug.
collection.on('event', function () {});
// alternatively, you can comment out the .toJSON() implementation, above, to bypass it.

api = {
bug: function (callback) {
callback(collection);
}
};

dnode(api).listen(31415);
````
client.js

````javascript
var util = require('util')
, dnode = require('dnode')
;

dnode.connect(31415, function (remote){
remote.bug(function(collection){
console.log(util.inspect(collection));
});
});
````