Skip to content

Commit

Permalink
Merge pull request grpc#2041 from murgatroid99/backport_node_0.9.2
Browse files Browse the repository at this point in the history
Backport Node library JS changes to the release branch
  • Loading branch information
nicolasnoble committed Jun 12, 2015
2 parents 1528ba5 + b9f6948 commit 0ca9acb
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 37 deletions.
26 changes: 17 additions & 9 deletions src/node/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# Node.js gRPC Library

## Status

Alpha : Ready for early adopters

## Prerequisites

This requires `node` to be installed. If you instead have the `nodejs` executable on Debian, you should install the [`nodejs-legacy`](https://packages.debian.org/sid/nodejs-legacy) package.
## PREREQUISITES
- `node`: This requires `node` to be installed. If you instead have the `nodejs` executable on Debian, you should install the [`nodejs-legacy`](https://packages.debian.org/sid/nodejs-legacy) package.
- [homebrew][] on Mac OS X, [linuxbrew][] on Linux. These simplify the installation of the gRPC C core.

## Installation
## INSTALLATION
On Mac OS X, install [homebrew][]. On Linux, install [linuxbrew][].
Run the following command to install gRPC Node.js.
```sh
$ curl -fsSL https://goo.gl/getgrpc | bash -s nodejs
```
This will download and run the [gRPC install script][], then install the latest version of gRPC Nodejs npm package.

1. Clone [the grpc repository](https://github.com/grpc/grpc).
## BUILD FROM SOURCE
1. Clone [the grpc Git Repository](https://github.com/grpc/grpc).
2. Follow the instructions in the `INSTALL` file in the root of that repository to install the C core library that this package depends on.
3. Run `npm install`.

Expand All @@ -20,12 +26,10 @@ If you install the gRPC C core library in a custom location, then you need to se
CXXFLAGS=-I<custom location>/include LDFLAGS=-L<custom location>/lib npm install [grpc]
```

## Tests

## TESTING
To run the test suite, simply run `npm test` in the install location.

## API

This library internally uses [ProtoBuf.js](https://github.com/dcodeIO/ProtoBuf.js), and some structures it exports match those exported by that library

If you require this module, you will get an object with the following members
Expand Down Expand Up @@ -82,3 +86,7 @@ ServerCredentials
```

An object with factory methods fro creating credential objects for servers.

[homebrew]:http://brew.sh
[linuxbrew]:https://github.com/Homebrew/linuxbrew#installation
[gRPC install script]:https://raw.githubusercontent.com/grpc/homebrew-grpc/master/scripts/install
2 changes: 1 addition & 1 deletion src/node/cli/service_packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
var fs = require('fs');
var path = require('path');

var _ = require('underscore');
var _ = require('lodash');
var async = require('async');
var pbjs = require('protobufjs/cli/pbjs');
var parseArgs = require('minimist');
Expand Down
2 changes: 1 addition & 1 deletion src/node/examples/perf_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

var grpc = require('..');
var testProto = grpc.load(__dirname + '/../interop/test.proto').grpc.testing;
var _ = require('underscore');
var _ = require('lodash');
var interop_server = require('../interop/interop_server.js');

function runTest(iterations, callback) {
Expand Down
2 changes: 1 addition & 1 deletion src/node/examples/route_guide_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var async = require('async');
var fs = require('fs');
var parseArgs = require('minimist');
var path = require('path');
var _ = require('underscore');
var _ = require('lodash');
var grpc = require('..');
var examples = grpc.load(__dirname + '/route_guide.proto').examples;
var client = new examples.RouteGuide('localhost:50051');
Expand Down
2 changes: 1 addition & 1 deletion src/node/examples/route_guide_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
var fs = require('fs');
var parseArgs = require('minimist');
var path = require('path');
var _ = require('underscore');
var _ = require('lodash');
var grpc = require('..');
var examples = grpc.load(__dirname + '/route_guide.proto').examples;

Expand Down
2 changes: 1 addition & 1 deletion src/node/examples/stock_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

'use strict';

var _ = require('underscore');
var _ = require('lodash');
var grpc = require('..');
var examples = grpc.load(__dirname + '/stock.proto').examples;

Expand Down
2 changes: 1 addition & 1 deletion src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

'use strict';

var _ = require('underscore');
var _ = require('lodash');

var ProtoBuf = require('protobufjs');

Expand Down
2 changes: 1 addition & 1 deletion src/node/interop/interop_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

var fs = require('fs');
var path = require('path');
var _ = require('underscore');
var _ = require('lodash');
var grpc = require('..');
var testProto = grpc.load(__dirname + '/test.proto').grpc.testing;
var GoogleAuth = require('google-auth-library');
Expand Down
2 changes: 1 addition & 1 deletion src/node/interop/interop_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

var fs = require('fs');
var path = require('path');
var _ = require('underscore');
var _ = require('lodash');
var grpc = require('..');
var testProto = grpc.load(__dirname + '/test.proto').grpc.testing;
var Server = grpc.buildServer([testProto.TestService.service]);
Expand Down
7 changes: 3 additions & 4 deletions src/node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "grpc",
"version": "0.9.0",
"version": "0.9.2",
"author": "Google Inc.",
"description": "gRPC Library for Node",
"homepage": "http://www.grpc.io/",
Expand All @@ -25,10 +25,9 @@
},
"dependencies": {
"bindings": "^1.2.0",
"lodash": "^3.9.3",
"nan": "^1.5.0",
"protobufjs": "dcodeIO/ProtoBuf.js",
"underscore": "^1.6.0",
"underscore.string": "^3.0.0"
"protobufjs": "dcodeIO/ProtoBuf.js"
},
"devDependencies": {
"async": "^0.9.0",
Expand Down
2 changes: 1 addition & 1 deletion src/node/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

'use strict';

var _ = require('underscore');
var _ = require('lodash');

var grpc = require('bindings')('grpc.node');

Expand Down
15 changes: 7 additions & 8 deletions src/node/src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@

'use strict';

var _ = require('underscore');

var capitalize = require('underscore.string/capitalize');
var decapitalize = require('underscore.string/decapitalize');
var _ = require('lodash');

/**
* Get a function that deserializes a specific type of protobuf.
Expand All @@ -50,7 +47,9 @@ function deserializeCls(cls) {
* @return {cls} The resulting object
*/
return function deserialize(arg_buf) {
return cls.decode(arg_buf).toRaw();
// Convert to a native object with binary fields as Buffers (first argument)
// and longs as strings (second argument)
return cls.decode(arg_buf).toRaw(false, true);
};
}

Expand Down Expand Up @@ -81,7 +80,7 @@ function fullyQualifiedName(value) {
}
var name = value.name;
if (value.className === 'Service.RPCMethod') {
name = capitalize(name);
name = _.capitalize(name);
}
if (value.hasOwnProperty('parent')) {
var parent_name = fullyQualifiedName(value.parent);
Expand Down Expand Up @@ -118,8 +117,8 @@ function wrapIgnoreNull(func) {
function getProtobufServiceAttrs(service) {
var prefix = '/' + fullyQualifiedName(service) + '/';
return _.object(_.map(service.children, function(method) {
return [decapitalize(method.name), {
path: prefix + capitalize(method.name),
return [_.camelCase(method.name), {
path: prefix + _.capitalize(method.name),
requestStream: method.requestStream,
responseStream: method.responseStream,
requestSerialize: serializeCls(method.resolvedRequestType.build()),
Expand Down
4 changes: 1 addition & 3 deletions src/node/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

'use strict';

var _ = require('underscore');
var _ = require('lodash');

var grpc = require('bindings')('grpc.node');

Expand All @@ -48,8 +48,6 @@ var util = require('util');

var EventEmitter = require('events').EventEmitter;

var common = require('./common.js');

/**
* Handle an error on a call by sending it as a status
* @param {grpc.Call} call The call to send the error on
Expand Down
90 changes: 90 additions & 0 deletions src/node/test/common_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

'use strict';

var assert = require('assert');

var common = require('../src/common.js');

var ProtoBuf = require('protobufjs');

var messages_proto = ProtoBuf.loadProtoFile(
__dirname + '/test_messages.proto').build();

describe('Proto message serialize and deserialize', function() {
var longSerialize = common.serializeCls(messages_proto.LongValues);
var longDeserialize = common.deserializeCls(messages_proto.LongValues);
var pos_value = '314159265358979';
var neg_value = '-27182818284590';
it('should preserve positive int64 values', function() {
var serialized = longSerialize({int_64: pos_value});
assert.strictEqual(longDeserialize(serialized).int_64.toString(),
pos_value);
});
it('should preserve negative int64 values', function() {
var serialized = longSerialize({int_64: neg_value});
assert.strictEqual(longDeserialize(serialized).int_64.toString(),
neg_value);
});
it('should preserve uint64 values', function() {
var serialized = longSerialize({uint_64: pos_value});
assert.strictEqual(longDeserialize(serialized).uint_64.toString(),
pos_value);
});
it('should preserve positive sint64 values', function() {
var serialized = longSerialize({sint_64: pos_value});
assert.strictEqual(longDeserialize(serialized).sint_64.toString(),
pos_value);
});
it('should preserve negative sint64 values', function() {
var serialized = longSerialize({sint_64: neg_value});
assert.strictEqual(longDeserialize(serialized).sint_64.toString(),
neg_value);
});
it('should preserve fixed64 values', function() {
var serialized = longSerialize({fixed_64: pos_value});
assert.strictEqual(longDeserialize(serialized).fixed_64.toString(),
pos_value);
});
it('should preserve positive sfixed64 values', function() {
var serialized = longSerialize({sfixed_64: pos_value});
assert.strictEqual(longDeserialize(serialized).sfixed_64.toString(),
pos_value);
});
it('should preserve negative sfixed64 values', function() {
var serialized = longSerialize({sfixed_64: neg_value});
assert.strictEqual(longDeserialize(serialized).sfixed_64.toString(),
neg_value);
});
});
6 changes: 2 additions & 4 deletions src/node/test/surface_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ var math_proto = ProtoBuf.loadProtoFile(__dirname + '/../examples/math.proto');

var mathService = math_proto.lookup('math.Math');

var capitalize = require('underscore.string/capitalize');

var _ = require('underscore');
var _ = require('lodash');

describe('File loader', function() {
it('Should load a proto file by default', function() {
Expand Down Expand Up @@ -159,7 +157,7 @@ describe('Generic client and server', function() {
server = new Server({
string: {
capitalize: function(call, callback) {
callback(null, capitalize(call.request));
callback(null, _.capitalize(call.request));
}
}
});
Expand Down
38 changes: 38 additions & 0 deletions src/node/test/test_messages.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2015, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

syntax = "proto3";

message LongValues {
int64 int_64 = 1;
uint64 uint_64 = 2;
sint64 sint_64 = 3;
fixed64 fixed_64 = 4;
sfixed64 sfixed_64 = 5;
}

0 comments on commit 0ca9acb

Please sign in to comment.