Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
grpc-promise-meta
Advanced tools
GRPC promisify module for all Request/Response types: standard and stream
This module is an extension of the original grpc-promise with the added support for passing Metadata objects for the grpc resolver.
High performance, open source, general-purpose RPC framework.
Calling service types
Implementations
GRPC promisify module for all Request/Response types: standard and stream.
This module unifies the way the grpc are implemented in NodeJS.
Depending on which kind of RPC call is implemented (simple vs streaming), we need to manage communication differently:
The goal of grpc-promise is to standardize all kind of calls, whatever they are, from the client side.
$ npm install grpc-promise-meta
Let's take a simple proto file exposing all kind of requests:
syntax = "proto3";
package test;
service Test {
rpc TestStreamSimple (stream TestRequest) returns (TestResponse) {}
rpc TestSimpleStream (TestRequest) returns (stream TestResponse) {}
rpc TestSimpleSimple (TestRequest) returns (TestResponse) {}
rpc TestStreamStream (stream TestRequest) returns (stream TestResponse) {}
}
message TestRequest {
int32 id = 1;
}
message TestResponse {
int32 id = 1;
}
Implementation of TestSimpleSimple
message
Server side:
const grpc = require('grpc');
const test_proto = grpc.load(__dirname + '/protobuf/test.proto').test;
const testSimpleSimple = function (call, callback) {
console.log('Server: Simple Message Received = ', call.request); // Server: Simple Message Received = {id: 1}
callback(null, call.request);
};
main = function () {
server = new grpc.Server();
server.addService(test_proto.Test.service, {
testSimpleSimple: testSimpleSimple
});
server.bind('0.0.0.0:50052', grpc.ServerCredentials.createInsecure());
server.start();
}
main();
Client side:
const grpc = require('grpc');
const grpc_promise = require('grpc-promise-meta');
const test_proto = grpc.load(__dirname + '/protobuf/test.proto').test;
function main() {
const client = new test_proto.Test('localhost:50052', grpc.credentials.createInsecure());
grpc_promise.promisifyAll(client);
client.testSimpleSimple()
.sendMessage({id: 1})
.then(res => {
console.log('Client: Simple Message Received = ', res) // Client: Simple Message Received = {id: 1}
})
.catch(err => console.error(err))
;
}
main();
Implementation of TestStreamSimple
message
Server side:
const grpc = require('grpc');
const test_proto = grpc.load(__dirname + '/protobuf/test.proto').test;
const testStreamSimple = function (call, callback) {
var messages = [];
call.on('data', function (m) {
console.log('Server: Stream Message Received = ', m); // Server: Stream Message Received = {id: 1}
messages.push(m);
});
call.on('end', function () {
callback(null, messages.pop());
});
};
main = function () {
server = new grpc.Server();
server.addService(test_proto.Test.service, {
testStreamSimple: testStreamSimple
});
server.bind('0.0.0.0:50052', grpc.ServerCredentials.createInsecure());
server.start();
}
main();
Client side:
const grpc = require('grpc');
const grpc_promise = require('grpc-promise-meta');
const test_proto = grpc.load(__dirname + '/protobuf/test.proto').test;
function main() {
const client = new test_proto.Test('localhost:50052', grpc.credentials.createInsecure());
grpc_promise.promisifyAll(client);
client.testStreamSimple()
.sendMessage({id: 1})
.sendMessage({id: 2})
.sendMessage({id: 3})
.end()
.then(res => {
console.log('Client: Simple Message Received = ', res); // Client: Simple Message Received = {id: 3}
})
.catch(err => console.error(err))
;
}
main();
Implementation of TestSimpleStream
message
Server side:
const grpc = require('grpc');
const test_proto = grpc.load(__dirname + '/protobuf/test.proto').test;
const testSimpleStream = function (call) {
console.log('Server: Simple Message Received = ', call.request); // Server: Simple Message Received = {id: 1}
call.write(call.request);
call.write(call.request);
call.end();
};
main = function () {
server = new grpc.Server();
server.addService(test_proto.Test.service, {
testSimpleStream: testSimpleStream
});
server.bind('0.0.0.0:50052', grpc.ServerCredentials.createInsecure());
server.start();
}
main();
Client side:
const grpc = require('grpc');
const grpc_promise = require('grpc-promise-meta');
const test_proto = grpc.load(__dirname + '/protobuf/test.proto').test;
function main() {
const client = new test_proto.Test('localhost:50052', grpc.credentials.createInsecure());
grpc_promise.promisifyAll(client);
client.testSimpleStream()
.sendMessage({id: 1})
.then(res => {
console.log('Client: Stream Message Received = ', res); // Client: Stream Message Received = [{id: 1},{id: 1}]
})
.catch(err => console.error(err))
;
}
main();
Implementation of TestStreamStream
message
IMPORTANT: In order to keep track of the messages sent and to be able to callback all the requests, the server implementation needs to answer with the same id received
Server side:
const grpc = require('grpc');
const test_proto = grpc.load(__dirname + '/protobuf/test.proto').test;
const testStreamStream = function (call) {
call.on('data', function (message) {
console.log('Server: Stream Message Received = ', message); // Server: Stream Message Received = {id: 1}
setTimeout(function () {
call.write({
id: message.id // IMPORTANT only for Bidirectional Stream Request
});
}, 10);
});
call.on('end', function () {
call.end();
});
};
main = function () {
server = new grpc.Server();
server.addService(test_proto.Test.service, {
testStreamStream: testStreamStream
});
server.bind('0.0.0.0:50052', grpc.ServerCredentials.createInsecure());
server.start();
}
main();
Client side:
const grpc = require('grpc');
const grpc_promise = require('grpc-promise-meta');
const test_proto = grpc.load(__dirname + '/protobuf/test.proto').test;
function main() {
const client = new test_proto.Test('localhost:50052', grpc.credentials.createInsecure());
grpc_promise.promisifyAll(client, {timeout: 100}); // Optional timeout definition, defaults = 50
t = client.testStreamStream();
t.sendMessage({})
.then(res => {
console.log('Client: Stream Message Received = ', res); // Client: Stream Message Received = {id: 0}
})
.catch(err => console.error(err))
;
t.sendMessage({})
.then(res => {
console.log('Client: Stream Message Received = ', res); // Client: Stream Message Received = {id: 1}
})
.catch(err => console.error(err))
;
t.end();
}
main();
Copyright (c) 2017 Carles Sistare
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
GRPC promisify module for all Request/Response types: standard and stream
The npm package grpc-promise-meta receives a total of 4 weekly downloads. As such, grpc-promise-meta popularity was classified as not popular.
We found that grpc-promise-meta demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.