Skip to content
This repository has been archived by the owner on Jan 31, 2018. It is now read-only.

Commit

Permalink
Fix compressed transport data passing, and add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Ellis committed May 18, 2015
1 parent 0f061c2 commit 2bcd4d5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/transports/client/childProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ function ChildProcessTransport(child, config) {
}

function compressedMessageHandler(message) {
zlib.gunzip(message, function(err, uncompressedJSON) {
var buf = new Buffer(message, 'base64');
zlib.gunzip(buf, function(err, uncompressedJSON) {
if (err) return this.emit('error', err.message);
var obj = JSON.parse(uncompressedJSON.toString('utf8'));
if (obj && this.requests[obj.id]) {
Expand Down Expand Up @@ -65,7 +66,7 @@ ChildProcessTransport.prototype.request = function request(body, callback) {
if (this.compressed) {
zlib.gzip(new Buffer(JSON.stringify(body)), function(err, compressedJSON) {
if (err) return this.emit('error', err.message);
this.child.send(compressedJSON);
this.child.send(compressedJSON.toString('base64'));
}.bind(this));
} else {
this.child.send(body);
Expand Down
5 changes: 3 additions & 2 deletions lib/transports/server/childProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ function ChildProcessTransport(config) {
}

function compressedMessageHandler(json) {
zlib.gunzip(json, function(err, uncompressedJSON) {
var buf = new Buffer(json, 'base64');
zlib.gunzip(buf, function(err, uncompressedJSON) {
if (err) return this.emit('error', err.message);
var obj = JSON.parse(uncompressedJSON.toString('utf8'));
this.handler(obj, function(jsonrpcObj) {
zlib.gzip(new Buffer(JSON.stringify(jsonrpcObj)), function(err, compressedJSON) {
if (err) return this.emit('error', err.message);
process.send(compressedJSON);
process.send(compressedJSON.toString('base64'));
}.bind(this));
}.bind(this));
}.bind(this));
Expand Down
14 changes: 14 additions & 0 deletions test/child/child-compressed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var jsonrpc = require('../../lib/index');
var JsonRpcServer = jsonrpc.server;
var JsonRpcChildProcTransport = jsonrpc.transports.server.childProcess;

var server = new JsonRpcServer(new JsonRpcChildProcTransport({ compressed: true }), {
loopback: function(obj, callback) {
callback(null, obj);
},
failure: function(obj, callback) {
var error = new Error("Whatchoo talkin' 'bout, Willis?");
error.prop = 1;
callback(error);
}
});
28 changes: 28 additions & 0 deletions test/client-childProccess-compressed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var jsonrpc = require('../lib/index');
var ChildProcessTransport = jsonrpc.transports.client.childProcess;
var JsonRpcClient = jsonrpc.client;
var childProcess = require('child_process');

var child = childProcess.fork(__dirname + '/child/child-compressed.js');
var jsonRpcClient = new JsonRpcClient(new ChildProcessTransport(child, { compressed: true }));
jsonRpcClient.register(['loopback', 'failure']);

exports.loopback = function(test) {
test.expect(2);
jsonRpcClient.loopback({foo: 'bar'}, function(err, result) {
test.ok(!!result, 'result exists');
test.equal(result.foo, 'bar', 'Looped back correctly');
test.done();
});
};

exports.failureTcp = function(test) {
test.expect(3);
jsonRpcClient.failure({foo: 'bar'}, function(err) {
test.ok(!!err, 'error exists');
test.equal("Whatchoo talkin' 'bout, Willis?", err.message, 'The error message was received correctly');
test.equal(1, err.prop, 'The error message was received correctly');
child.kill();
test.done();
});
};

0 comments on commit 2bcd4d5

Please sign in to comment.