Skip to content

Commit

Permalink
Merge pull request #418 from primus/sparky
Browse files Browse the repository at this point in the history
Prevent the Spark prototype from being mutated
  • Loading branch information
3rd-Eden committed Jan 13, 2016
2 parents a7a090f + beb86df commit 31beacf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
10 changes: 7 additions & 3 deletions index.js
Expand Up @@ -78,10 +78,14 @@ function Primus(server, options) {

this.Spark.prototype = Object.create(Spark.prototype, {
constructor: {
configurable: true,
value: this.Spark,
writable: true,
enumerable: false,
configurable: true
writable: true
},
__initialise: {
value: Spark.prototype.__initialise.slice(),
configurable: true,
writable: true
}
});

Expand Down
38 changes: 22 additions & 16 deletions test/plugin.test.js
Expand Up @@ -35,9 +35,7 @@ describe('Plugin', function () {
primus.on('connection', function (spark) {
spark.on('custom event', function (data) {
expect(data).to.equal('custom data');

spark.end();
server.close(done);
primus.destroy(done);
});
});

Expand All @@ -64,13 +62,11 @@ describe('Plugin', function () {

primus.on('connection', function (spark) {
expect(spark.query.foo).to.equal('bar');
spark.end();
server.close(done);
primus.destroy(done);
});

server.listen(port, function () {
var Socket = primus.Socket
, socket = new Socket('http://localhost:'+ port);
new primus.Socket('http://localhost:'+ port);
});
});

Expand All @@ -90,14 +86,26 @@ describe('Plugin', function () {

primus.on('connection', function (spark) {
expect(spark.join).to.be.a('function');
spark.end();
server.close(done);
primus.destroy(done);
});

server.listen(port, function () {
var Socket = primus.Socket
, socket = new Socket('http://localhost:'+ port);
new primus.Socket('http://localhost:'+ port);
});
});

it('doesn\'t mutate the Spark prototype', function () {
var expected = Primus.Spark.prototype.__initialise.slice()
, server = http.createServer()
, primus = new Primus(server);

primus.use('dummy', {
server: function (primus) {
primus.Spark.prototype.initialise = function init() {};
}
});

expect(Primus.Spark.prototype.__initialise).to.eql(expected);
});

it('can instantly modify the prototype', function () {
Expand All @@ -117,8 +125,7 @@ describe('Plugin', function () {

it('emits an `plugin` event when a new plugin is added', function (next) {
var server = http.createServer()
, primus = new Primus(server)
, port = common.port;
, primus = new Primus(server);

primus.on('plugin', function (name, obj) {
expect(name).to.equal('foo');
Expand All @@ -135,8 +142,7 @@ describe('Plugin', function () {
describe('#plugout', function () {
it('emits a `plugout` event when removing a plugin', function (next) {
var server = http.createServer()
, primus = new Primus(server)
, port = common.port;
, primus = new Primus(server);

primus.on('plugout', function (name, obj) {
expect(name).to.equal('foo');
Expand All @@ -159,7 +165,7 @@ describe('Plugin', function () {
, primus = new Primus(server);

var plugin = {
server: function (primus) {}
server: function () {}
};

primus.use('spark', plugin);
Expand Down

0 comments on commit 31beacf

Please sign in to comment.