Oh sorry and you would use it like so

socket.on('data', bufferJson(key, function(data) {
//do something with json object
}))

On Monday, April 30, 2012 6:23:11 PM UTC-4, Tim Dickinson wrote:
>
> Hey all.
>
> How can i better write this snippet of code?
>
> var bufferJson = function(key, requestEvent) {
> var buffer = []
> var cryptoFn = function(message) {
> var decipher = crypto.createDecipher('aes-256-cbc', key)
> return decipher.update(message, 'hex', 'utf8') + decipher['final']('utf8')
> }
> function onData(data) {
> if(data.indexOf('\n') > -1) {
>
> var message = buffer.join('');
> data = data.split('\n');
> message += data.shift();
> buffer = [];
>
> try {
> console.log(cryptoFn(message))
> var json = JSON.parse(cryptoFn(message));
> } catch(e) {
> return util.error('Could not parse message: ' + e.message);
> }
>
> requestEvent(json)
> data = data.join('\n');
>
> if(data.length) {
> onData(data);
> }
>
> } else {
> buffer.push(data);
> }
> }
>
> return onData
> }
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to