diff options
author | murgatroid99 <mlumish@google.com> | 2015-04-13 12:59:17 -0700 |
---|---|---|
committer | murgatroid99 <mlumish@google.com> | 2015-04-13 12:59:17 -0700 |
commit | 353d422a7628002b8d821cf49c4ab1aeea712237 (patch) | |
tree | 8dd6a15c4a3610f18cde705491d6d1cfbdded60a /src/node/examples/math_server.js | |
parent | 9a70c1aaf33944418a6661d4a80afa11a56541c0 (diff) |
Fixed bugs in trailing metadata handling and math server example
Diffstat (limited to 'src/node/examples/math_server.js')
-rw-r--r-- | src/node/examples/math_server.js | 40 |
1 files changed, 16 insertions, 24 deletions
diff --git a/src/node/examples/math_server.js b/src/node/examples/math_server.js index ae548c89e4..3fac193d64 100644 --- a/src/node/examples/math_server.js +++ b/src/node/examples/math_server.js @@ -33,10 +33,6 @@ 'use strict'; -var util = require('util'); - -var Transform = require('stream').Transform; - var grpc = require('..'); var math = grpc.load(__dirname + '/math.proto').math; @@ -54,11 +50,12 @@ function mathDiv(call, cb) { // Unary + is explicit coersion to integer if (+req.divisor === 0) { cb(new Error('cannot divide by zero')); + } else { + cb(null, { + quotient: req.dividend / req.divisor, + remainder: req.dividend % req.divisor + }); } - cb(null, { - quotient: req.dividend / req.divisor, - remainder: req.dividend % req.divisor - }); } /** @@ -97,24 +94,19 @@ function mathSum(call, cb) { } function mathDivMany(stream) { - // Here, call is a standard duplex Node object Stream - util.inherits(DivTransform, Transform); - function DivTransform() { - var options = {objectMode: true}; - Transform.call(this, options); - } - DivTransform.prototype._transform = function(div_args, encoding, callback) { + stream.on('data', function(div_args) { if (+div_args.divisor === 0) { - callback(new Error('cannot divide by zero')); + stream.emit('error', new Error('cannot divide by zero')); + } else { + stream.write({ + quotient: div_args.dividend / div_args.divisor, + remainder: div_args.dividend % div_args.divisor + }); } - callback(null, { - quotient: div_args.dividend / div_args.divisor, - remainder: div_args.dividend % div_args.divisor - }); - }; - var transform = new DivTransform(); - stream.pipe(transform); - transform.pipe(stream); + }); + stream.on('end', function() { + stream.end(); + }); } var server = new Server({ |