Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
added json middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
skenqbx committed Jun 26, 2012
1 parent 2e0b0b7 commit c1614aa
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/index.js
Expand Up @@ -26,6 +26,9 @@
var Caster = require('./caster');
var Node = require('./node');

/* export modules */
exports.middleware = require('./middleware');


/**
*
Expand Down
26 changes: 26 additions & 0 deletions lib/middleware/index.js
@@ -0,0 +1,26 @@
// Copyright (c) 2012 Malte-Thorben Bruns <skenqbx@googlemail.com>

// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// 'Software'), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:

// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/* enable ECMA-262 strict mode */
'use strict';

/* export modules */
exports.json = require('./json');
57 changes: 57 additions & 0 deletions lib/middleware/json.js
@@ -0,0 +1,57 @@
// Copyright (c) 2012 Malte-Thorben Bruns <skenqbx@googlemail.com>

// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// 'Software'), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:

// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/* enable ECMA-262 strict mode */
'use strict';


/**
* @param {Object} opt_options
*/
function jsonMiddleware(opt_options) {

function rx(message, remote, next) {
var json;
try {
json = JSON.parse(message.toString());
} catch (err) {
return next(null, message);
}
next(null, json);
}

function tx(message, next) {
if (Buffer.isBuffer(message)) {
return next(null, message);
}

var json;
try {
json = JSON.stringify(message);
} catch (err) {
return next(err, null);
}
next(null, new Buffer(json));
}

return {name: 'json', rx: rx, tx: tx};
}
module.exports = jsonMiddleware;
38 changes: 37 additions & 1 deletion test/test-caster.js
Expand Up @@ -54,7 +54,7 @@ describe('Caster', function() {
});

describe('#onMessage()', function() {
it('send 3x1 & receive 3x3', function(done) {
it('binary', function(done) {
var count = 0;

function add() {
Expand All @@ -75,6 +75,42 @@ describe('Caster', function() {
socketB.removeAllListeners('message');
socketC.removeAllListeners('message');
});

it('middleware.json', function(done) {
var count = 0;

function add() {
if (++count === 9) {
done();
}
}

var middleware = caster.middleware.json();
socketA.use(middleware);
socketB.use(middleware);
socketC.use(middleware);
socketA.on('message', add);
socketB.on('message', add);
socketC.on('message', add);
socketA.send({hello: 'world'});
socketB.send({hello: 'world'});
socketC.send({hello: 'world'});
});

after(function() {
socketA.removeAllListeners('message');
socketA._middleware = {};
socketA._rx = [];
socketA._tx = [];
socketB.removeAllListeners('message');
socketB._middleware = {};
socketB._rx = [];
socketB._tx = [];
socketC.removeAllListeners('message');
socketC._middleware = {};
socketC._rx = [];
socketC._tx = [];
});
});
});
});

0 comments on commit c1614aa

Please sign in to comment.