-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fec1a63
Showing
12 changed files
with
546 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
repo_token: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
node_modules | ||
.npmignore | ||
.DS_Store | ||
coverage | ||
bench.js | ||
.nyc_output | ||
*.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
language: node_js | ||
node_js: | ||
- 6 | ||
- 7 | ||
branches: | ||
only: | ||
- master | ||
notifications: | ||
email: | ||
- joaquim.serafim@gmail.com | ||
script: npm test | ||
after_success: npm run coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
The ISC License | ||
|
||
Copyright (c) 2017, Joaquim José F. Serafim | ||
|
||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR | ||
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# grpc.server | ||
|
||
a simple abstraction around grpc.Server | ||
|
||
|
||
---- | ||
<a href="https://nodei.co/npm/grpc.server/"><img src="https://nodei.co/npm/grpc.server.png?downloads=true"></a> | ||
|
||
[![Build Status](https://travis-ci.org/joaquimserafim/grpc.server.svg?branch=master)](https://travis-ci.org/joaquimserafim/grpc.server)[![Coverage Status](https://coveralls.io/repos/github/joaquimserafim/grpc.server/badge.svg)](https://coveralls.io/github/joaquimserafim/grpc.server)[![ISC License](https://img.shields.io/badge/license-ISC-blue.svg?style=flat-square)](https://github.com/joaquimserafim/grpc.server/blob/master/LICENSE)[![NodeJS](https://img.shields.io/badge/node-6.x.x-brightgreen.svg?style=flat-square)](https://github.com/joaquimserafim/grpc.server/blob/master/package.json#L53) | ||
|
||
[![JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) | ||
|
||
|
||
### api | ||
`const server = require('grpc.server')` | ||
|
||
* **server(an optional {})** | ||
- **address** string with the format `url:port` | ||
- **creedentials** can use `credentials.createInsecure` or with certificates through an object { ca, key, client } | ||
- **metadata** set metadata that can be used in all calls, an array with { key: value } | ||
|
||
**methods** | ||
* **addServices(an array with the services object)** | ||
- **package** proto package name | ||
- **name** service name | ||
- **proto** proto file | ||
- **methods** an object with the methods | ||
* **start(callback function)** | ||
* **stop(callback function)** | ||
|
||
|
||
**note:** you can use `unary`, `stream` or `bidirecional` since the comm type should be handled on the proto files and in the methods | ||
|
||
### example | ||
|
||
|
||
```js | ||
const server = require('grpc.server') | ||
|
||
// proto file | ||
/* | ||
syntax = "proto3"; | ||
package helloWorld; | ||
// The greeting service definition. | ||
service Greeter { | ||
// Sends a greeting | ||
rpc sayHello (HelloRequest) returns (HelloReply) {} | ||
} | ||
// The request message containing the user's name. | ||
message HelloRequest { | ||
string name = 1; | ||
} | ||
// The response message containing the greetings | ||
message HelloReply { | ||
string message = 1; | ||
} | ||
*/ | ||
|
||
const serverA = server() | ||
|
||
const services = [ | ||
{ | ||
proto: protos.helloWorld, | ||
package: 'helloWorld', | ||
name: 'Greeter', | ||
methods: { sayHello: sayHello } | ||
} | ||
] | ||
|
||
serverA. | ||
.addServices(services) | ||
.startc(cb) | ||
|
||
|
||
// to stop the server | ||
serverA.stop(cb) | ||
|
||
``` | ||
|
||
|
||
#### ISC License (ISC) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
eslint | ||
no-multi-spaces: ["error", {exceptions: {"VariableDeclarator": true}}] | ||
padded-blocks: ["error", {"classes": "always"}] | ||
max-len: ["error", 80] | ||
*/ | ||
'use strict' | ||
|
||
const { ServerCredentials, Server, load } = require('grpc') | ||
const isObject = require('is.object') | ||
const getPropValue = require('get-property-value') | ||
|
||
class GrpcServer { | ||
|
||
constructor (config = {}) { | ||
const address = config.address || '0.0.0.0:50051' | ||
const creedentials = setAuthentication(config.creedentials) || | ||
ServerCredentials.createInsecure() | ||
|
||
this._server = new Server() | ||
this._server.bind(address, creedentials) | ||
} | ||
|
||
addServices (services = []) { | ||
if (!services.length) { | ||
this._server.forceShutdown() | ||
throw new Error('you must add some services') | ||
} | ||
|
||
try { | ||
for (let svc in services) { | ||
const service = `${services[svc].package}.${services[svc].name}.service` | ||
const proto = getPropValue(load(services[svc].proto), service) | ||
|
||
this._server.addService(proto, services[svc].methods) | ||
} | ||
} catch (ex) { | ||
this._server.forceShutdown() | ||
throw new Error(`check your service config and proto file: ${ex.message}`) | ||
} | ||
|
||
return this | ||
} | ||
|
||
start (cb) { | ||
this._server.start() | ||
|
||
cb() | ||
} | ||
|
||
stop (cb) { | ||
this._server.tryShutdown(shutdow) | ||
|
||
function shutdow () { | ||
setTimeout(cb, 50) | ||
} | ||
} | ||
|
||
} | ||
|
||
module.exports = function factory (config) { | ||
return new GrpcServer(config) | ||
} | ||
|
||
// | ||
// help functions | ||
// | ||
|
||
function setAuthentication (certs) { | ||
|
||
return isObject(certs) && ServerCredentials.createSsl( | ||
Buffer.from(certs.ca), | ||
[ | ||
{ | ||
'cert_chain': Buffer.from(certs.server), | ||
'private_key': Buffer.from(certs.key) | ||
} | ||
] | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{ | ||
"name": "grpc.server", | ||
"version": "0.0.0", | ||
"description": "a simple abstraction around grpc.Server", | ||
"main": "index.js", | ||
"files": [ | ||
"LICENSE", | ||
"README.md", | ||
"index.js" | ||
], | ||
"scripts": { | ||
"coverage:open": "open coverage/index.html", | ||
"coverage:check": "nyc check-coverage --statements 100 --functions 100 --lines 100 --branches 100", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls", | ||
"test": "standard --fix && nyc --reporter=html --reporter=text mocha" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/joaquimserafim/grpc.server.git" | ||
}, | ||
"keywords": [ | ||
"grpc", | ||
"server", | ||
"services" | ||
], | ||
"author": "@JoaquimSerafim", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/joaquimserafim/grpc.server/issues" | ||
}, | ||
"homepage": "https://github.com/joaquimserafim/grpc.server#readme", | ||
"devDependencies": { | ||
"chai": "^4.0.2", | ||
"coveralls": "^2.13.1", | ||
"grpc.client": "^2.0.2", | ||
"mocha": "^3.4.2", | ||
"node-forge": "^0.7.1", | ||
"nyc": "^11.0.2", | ||
"pre-commit": "^1.2.2", | ||
"standard": "^10.0.2" | ||
}, | ||
"dependencies": { | ||
"get-property-value": "^2.0.0", | ||
"grpc": "^1.3.7", | ||
"is.object": "^1.0.0" | ||
}, | ||
"nyc": { | ||
"include": [ | ||
"index.js" | ||
] | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"pre-commit": [ | ||
"test", | ||
"coverage:check" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
'use strict' | ||
|
||
const { pki } = require('node-forge') | ||
|
||
const attrs = [ | ||
{ | ||
name: 'commonName', | ||
value: 'example.org' | ||
}, | ||
{ | ||
name: 'countryName', | ||
value: 'GB' | ||
}, | ||
{ | ||
shortName: 'ST', | ||
value: 'ElDorado' | ||
}, | ||
{ | ||
name: 'localityName', | ||
value: 'Benfica' | ||
}, | ||
{ | ||
name: 'organizationName', | ||
value: 'Test' | ||
}, | ||
{ | ||
shortName: 'OU', | ||
value: 'Test' | ||
} | ||
] | ||
|
||
const exts = [ | ||
{ | ||
name: 'basicConstraints', | ||
cA: true | ||
}, | ||
{ | ||
name: 'keyUsage', | ||
keyCertSign: true, | ||
digitalSignature: true, | ||
nonRepudiation: true, | ||
keyEncipherment: true, | ||
dataEncipherment: true | ||
}, | ||
{ | ||
name: 'extKeyUsage', | ||
serverAuth: true, | ||
clientAuth: true, | ||
codeSigning: true, | ||
emailProtection: true, | ||
timeStamping: true | ||
}, | ||
{ | ||
name: 'nsCertType', | ||
client: true, | ||
server: true, | ||
email: true, | ||
objsign: true, | ||
sslCA: true, | ||
emailCA: true, | ||
objCA: true | ||
}, | ||
{ | ||
name: 'subjectAltName', | ||
altNames: [ | ||
{ | ||
type: 6, // URI | ||
value: 'http://example.org/webid#me' | ||
}, | ||
{ | ||
type: 7, // IP | ||
ip: '127.0.0.1' | ||
} | ||
] | ||
}, | ||
{ | ||
name: 'subjectKeyIdentifier' | ||
} | ||
] | ||
|
||
module.exports = createCertificate | ||
|
||
function createCertificate (cb) { | ||
pki.rsa.generateKeyPair({ bits: 1024 }, generateKeyPair) | ||
|
||
function generateKeyPair (err, keyPair) { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
const cert = pki.createCertificate() | ||
|
||
cert.publicKey = keyPair.publicKey | ||
cert.serialNumber = '01' | ||
cert.validity.notBefore = new Date() | ||
cert.validity.notAfter = new Date() | ||
cert.validity.notAfter | ||
.setFullYear(cert.validity.notBefore.getFullYear() + 1) | ||
cert.setSubject(attrs) | ||
cert.setIssuer(attrs) | ||
cert.setExtensions(exts) | ||
|
||
cert.sign(keyPair.privateKey) | ||
|
||
const pem = { | ||
privateKey: pki.privateKeyToPem(keyPair.privateKey), | ||
publicKey: pki.publicKeyToPem(keyPair.publicKey), | ||
certificate: pki.certificateToPem(cert) | ||
} | ||
|
||
cb(null, pem) | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.