grpc-promise
Advanced tools
Comparing version 1.2.2 to 1.3.0
@@ -6,2 +6,6 @@ const requestTypes = require('./request-types/request-types'); | ||
const originalFunction = client[functionName]; | ||
if (originalFunction.requestStream === undefined && originalFunction.responseStream === undefined) { | ||
return; | ||
} | ||
const genericFunctionSelector = | ||
@@ -29,4 +33,37 @@ (originalFunction.requestStream ? 2 : 0) | (originalFunction.responseStream ? 1 : 0); | ||
const promisify = function (client, methods, options) { | ||
Object.keys(Object.getPrototypeOf(client)).forEach(function (functionName) { | ||
const originalFunction = client[functionName]; | ||
if (originalFunction.requestStream === undefined && originalFunction.responseStream === undefined) { | ||
return; | ||
} | ||
if (methods.indexOf(functionName) === -1) { | ||
return; | ||
} | ||
const genericFunctionSelector = | ||
(originalFunction.requestStream ? 2 : 0) | (originalFunction.responseStream ? 1 : 0); | ||
let genericFunctionName; | ||
switch (genericFunctionSelector) { | ||
case 0: | ||
genericFunctionName = 'makeUnaryRequest'; | ||
break; | ||
case 1: | ||
genericFunctionName = 'makeServerStreamRequest'; | ||
break; | ||
case 2: | ||
genericFunctionName = 'makeClientStreamRequest'; | ||
break; | ||
case 3: | ||
genericFunctionName = 'makeBidiStreamRequest'; | ||
break; | ||
} | ||
client[functionName] = requestTypes[genericFunctionName](client, originalFunction, options); | ||
}); | ||
}; | ||
module.exports = { | ||
promisifyAll: promisifyAll | ||
promisifyAll: promisifyAll, | ||
promisify: promisify | ||
}; |
{ | ||
"name": "grpc-promise", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "GRPC promisify module for all Request/Response types: standard and stream", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -21,2 +21,3 @@ # grpc-promise | ||
- [Request with Metadata](#request-with-metadata) | ||
- [Promisify single function](#promisify-single-function) | ||
- [MIT License](#mit-license) | ||
@@ -366,2 +367,31 @@ | ||
### Promisify single function | ||
Client side: | ||
```js | ||
const grpc = require('grpc'); | ||
const grpc_promise = require('grpc-promise'); | ||
const test_proto = grpc.load(__dirname + '/protobuf/test.proto').test; | ||
function main() { | ||
const client = new test_proto.Test('localhost:50052', grpc.credentials.createInsecure()); | ||
const meta = new grpc.Metadata(); | ||
meta.add('key', 'value'); | ||
grpc_promise.promisify(client, ['testSimpleSimple'] {metadata: meta}); | ||
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(); | ||
``` | ||
## MIT License | ||
@@ -368,0 +398,0 @@ |
@@ -6,86 +6,130 @@ const BidiStreamMock = require('../../tools/grpc-mock/BidiStreamMock'); | ||
it('If no delay, test ok', function () { | ||
const client = {}; | ||
const makeBidiStreamRequest = function () { | ||
return new BidiStreamMock(); | ||
}; | ||
makeBidiStreamRequest.requestStream = true; | ||
makeBidiStreamRequest.responseStream = true; | ||
Object.setPrototypeOf(client, { | ||
bidiStreamReq: makeBidiStreamRequest | ||
describe('With promisifyAll', function () { | ||
it('If no delay, test ok', function () { | ||
const client = {}; | ||
const makeBidiStreamRequest = function () { | ||
return new BidiStreamMock(); | ||
}; | ||
makeBidiStreamRequest.requestStream = true; | ||
makeBidiStreamRequest.responseStream = true; | ||
Object.setPrototypeOf(client, { | ||
bidiStreamReq: makeBidiStreamRequest | ||
}); | ||
grpc_promise.promisifyAll(client); | ||
const s = client.bidiStreamReq(); | ||
return Promise.all([s.sendMessage({}), s.sendMessage({})]) | ||
.then(res => { | ||
res[0].id.should.equal(0); | ||
res[1].id.should.equal(1); | ||
}); | ||
}); | ||
grpc_promise.promisifyAll(client); | ||
const s = client.bidiStreamReq(); | ||
return Promise.all([s.sendMessage({}), s.sendMessage({})]) | ||
.then(res => { | ||
res[0].id.should.equal(0); | ||
res[1].id.should.equal(1); | ||
it('If a bit of delay, test ok', function () { | ||
const client = {}; | ||
const makeBidiStreamRequest = function () { | ||
return new BidiStreamMock({delay: 5}); | ||
}; | ||
makeBidiStreamRequest.requestStream = true; | ||
makeBidiStreamRequest.responseStream = true; | ||
Object.setPrototypeOf(client, { | ||
bidiStreamReq: makeBidiStreamRequest | ||
}); | ||
}); | ||
it('If a bit of delay, test ok', function () { | ||
const client = {}; | ||
const makeBidiStreamRequest = function () { | ||
return new BidiStreamMock({delay: 5}); | ||
}; | ||
makeBidiStreamRequest.requestStream = true; | ||
makeBidiStreamRequest.responseStream = true; | ||
Object.setPrototypeOf(client, { | ||
bidiStreamReq: makeBidiStreamRequest | ||
grpc_promise.promisifyAll(client); | ||
const s = client.bidiStreamReq(); | ||
return Promise.all([s.sendMessage({}), s.sendMessage({})]) | ||
.then(res => { | ||
res[0].id.should.equal(0); | ||
res[1].id.should.equal(1); | ||
}); | ||
}); | ||
grpc_promise.promisifyAll(client); | ||
const s = client.bidiStreamReq(); | ||
return Promise.all([s.sendMessage({}), s.sendMessage({})]) | ||
.then(res => { | ||
res[0].id.should.equal(0); | ||
res[1].id.should.equal(1); | ||
it('If a lot of delay, test ko', function () { | ||
const client = {}; | ||
const makeBidiStreamRequest = function () { | ||
return new BidiStreamMock({delay: 60}); | ||
}; | ||
makeBidiStreamRequest.requestStream = true; | ||
makeBidiStreamRequest.responseStream = true; | ||
Object.setPrototypeOf(client, { | ||
bidiStreamReq: makeBidiStreamRequest | ||
}); | ||
}); | ||
it('If a lot of delay, test ko', function () { | ||
const client = {}; | ||
const makeBidiStreamRequest = function () { | ||
return new BidiStreamMock({delay: 60}); | ||
}; | ||
makeBidiStreamRequest.requestStream = true; | ||
makeBidiStreamRequest.responseStream = true; | ||
Object.setPrototypeOf(client, { | ||
bidiStreamReq: makeBidiStreamRequest | ||
grpc_promise.promisifyAll(client); | ||
return client.bidiStreamReq() | ||
.sendMessage({}) | ||
.then(() => { | ||
throw new Error('Should not happen'); | ||
}) | ||
.catch(err => err.should.equal('timeout')); | ||
}); | ||
grpc_promise.promisifyAll(client); | ||
it('If a lot of delay and a lot of timeout, test ko', function () { | ||
const client = {}; | ||
const makeBidiStreamRequest = function () { | ||
return new BidiStreamMock({delay: 75}); | ||
}; | ||
makeBidiStreamRequest.requestStream = true; | ||
makeBidiStreamRequest.responseStream = true; | ||
Object.setPrototypeOf(client, { | ||
bidiStreamReq: makeBidiStreamRequest | ||
}); | ||
return client.bidiStreamReq() | ||
.sendMessage({}) | ||
.then(() => { | ||
throw new Error('Should not happen'); | ||
}) | ||
.catch(err => err.should.equal('timeout')); | ||
}); | ||
grpc_promise.promisifyAll(client, {timeout: 80}); | ||
it('If a lot of delay and a lot of timeout, test ko', function () { | ||
const client = {}; | ||
const makeBidiStreamRequest = function () { | ||
return new BidiStreamMock({delay: 75}); | ||
}; | ||
makeBidiStreamRequest.requestStream = true; | ||
makeBidiStreamRequest.responseStream = true; | ||
Object.setPrototypeOf(client, { | ||
bidiStreamReq: makeBidiStreamRequest | ||
const s = client.bidiStreamReq(); | ||
return Promise.all([s.sendMessage({}), s.sendMessage({})]) | ||
.then(res => { | ||
res[0].id.should.equal(0); | ||
res[1].id.should.equal(1); | ||
}); | ||
}); | ||
grpc_promise.promisifyAll(client, {timeout: 80}); | ||
}); | ||
const s = client.bidiStreamReq(); | ||
return Promise.all([s.sendMessage({}), s.sendMessage({})]) | ||
.then(res => { | ||
res[0].id.should.equal(0); | ||
res[1].id.should.equal(1); | ||
describe('With promisify', function () { | ||
it('Test ok', function (done) { | ||
const client = {}; | ||
const makeBidiStreamRequest = function () { | ||
return new BidiStreamMock(); | ||
}; | ||
makeBidiStreamRequest.requestStream = true; | ||
makeBidiStreamRequest.responseStream = true; | ||
const makeBidiStreamRequest2 = function () { | ||
return new BidiStreamMock(); | ||
}; | ||
makeBidiStreamRequest2.requestStream = true; | ||
makeBidiStreamRequest2.responseStream = true; | ||
Object.setPrototypeOf(client, { | ||
bidiStreamReq: makeBidiStreamRequest, | ||
bidiStreamReq2: makeBidiStreamRequest2 | ||
}); | ||
grpc_promise.promisify(client, ['bidiStreamReq']); | ||
const s = client.bidiStreamReq(); | ||
Promise.all([s.sendMessage({}), s.sendMessage({})]) | ||
.then(res => { | ||
res[0].id.should.equal(0); | ||
res[1].id.should.equal(1); | ||
const call = client.bidiStreamReq2(); | ||
call.write({id: 1}); | ||
let responseCounter = 1; | ||
call.on('data', function (res) { | ||
res.id.should.equal(responseCounter++); | ||
if (responseCounter === 2) { | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -6,51 +6,94 @@ const ClientStreamMock = require('../../tools/grpc-mock/ClientStreamMock'); | ||
it('Test ok', function () { | ||
const client = {}; | ||
const makeClientStreamRequest = function (metadata, callback) { | ||
return new ClientStreamMock({callback: callback}); | ||
}; | ||
makeClientStreamRequest.requestStream = true; | ||
makeClientStreamRequest.responseStream = false; | ||
Object.setPrototypeOf(client, { | ||
clientStreamReq: makeClientStreamRequest | ||
describe('With promisifyAll', function () { | ||
it('Test ok', function () { | ||
const client = {}; | ||
const makeClientStreamRequest = function (metadata, callback) { | ||
return new ClientStreamMock({callback: callback}); | ||
}; | ||
makeClientStreamRequest.requestStream = true; | ||
makeClientStreamRequest.responseStream = false; | ||
Object.setPrototypeOf(client, { | ||
clientStreamReq: makeClientStreamRequest | ||
}); | ||
grpc_promise.promisifyAll(client); | ||
return client.clientStreamReq() | ||
.sendMessage({id: 1}) | ||
.sendMessage({id: 2}) | ||
.end() | ||
.then(res => { | ||
res.id.should.equal(2); | ||
}); | ||
}); | ||
grpc_promise.promisifyAll(client); | ||
it('Test ko', function () { | ||
const client = {}; | ||
const makeClientStreamRequest = function (metadata, callback) { | ||
var stream = new ClientStreamMock(); | ||
stream.on('finish', function () { | ||
callback('some_error'); | ||
}); | ||
return stream; | ||
}; | ||
makeClientStreamRequest.requestStream = true; | ||
makeClientStreamRequest.responseStream = false; | ||
Object.setPrototypeOf(client, { | ||
clientStreamReq: makeClientStreamRequest | ||
}); | ||
return client.clientStreamReq() | ||
.sendMessage({id: 1}) | ||
.sendMessage({id: 2}) | ||
.end() | ||
.then(res => { | ||
res.id.should.equal(2); | ||
}); | ||
grpc_promise.promisifyAll(client); | ||
return client.clientStreamReq() | ||
.sendMessage({id: 1}) | ||
.sendMessage({id: 2}) | ||
.end() | ||
.then(() => { | ||
throw new Error('Should not happen'); | ||
}) | ||
.catch(err => err.should.equal('some_error')); | ||
}); | ||
}); | ||
it('Test ko', function () { | ||
const client = {}; | ||
const makeClientStreamRequest = function (metadata, callback) { | ||
var stream = new ClientStreamMock(); | ||
stream.on('finish', function () { | ||
callback('some_error'); | ||
describe('With promisify', function () { | ||
it('Test ok', function (done) { | ||
const client = {}; | ||
const makeClientStreamRequest = function (metadata, callback) { | ||
return new ClientStreamMock({callback: callback}); | ||
}; | ||
makeClientStreamRequest.requestStream = true; | ||
makeClientStreamRequest.responseStream = false; | ||
const makeClientStreamRequest2 = function (metadata, callback) { | ||
return new ClientStreamMock({callback: callback}); | ||
}; | ||
makeClientStreamRequest2.requestStream = true; | ||
makeClientStreamRequest2.responseStream = false; | ||
Object.setPrototypeOf(client, { | ||
clientStreamReq: makeClientStreamRequest, | ||
clientStreamReq2: makeClientStreamRequest2 | ||
}); | ||
return stream; | ||
}; | ||
makeClientStreamRequest.requestStream = true; | ||
makeClientStreamRequest.responseStream = false; | ||
Object.setPrototypeOf(client, { | ||
clientStreamReq: makeClientStreamRequest | ||
grpc_promise.promisify(client, ['clientStreamReq']); | ||
client.clientStreamReq() | ||
.sendMessage({id: 1}) | ||
.sendMessage({id: 2}) | ||
.end() | ||
.then(res => { | ||
res.id.should.equal(2); | ||
const call = client.clientStreamReq2(null, function (err, res) { | ||
res.id.should.equal(1); | ||
done(); | ||
}); | ||
call.write({id: 1}); | ||
call.end(); | ||
}); | ||
}); | ||
grpc_promise.promisifyAll(client); | ||
return client.clientStreamReq() | ||
.sendMessage({id: 1}) | ||
.sendMessage({id: 2}) | ||
.end() | ||
.then(() => { | ||
throw new Error('Should not happen'); | ||
}) | ||
.catch(err => err.should.equal('some_error')); | ||
}); | ||
}); |
@@ -6,45 +6,89 @@ const ServerStreamMock = require('../../tools/grpc-mock/ServerStreamMock'); | ||
it('Test ok', function () { | ||
const client = {}; | ||
const makeServerStreamRequest = function () { | ||
return new ServerStreamMock({response: [{id: 1}, {id: 2}]}); | ||
}; | ||
makeServerStreamRequest.requestStream = false; | ||
makeServerStreamRequest.responseStream = true; | ||
Object.setPrototypeOf(client, { | ||
serverStreamReq: makeServerStreamRequest | ||
describe('With promisifyAll', function () { | ||
it('Test ok', function () { | ||
const client = {}; | ||
const makeServerStreamRequest = function () { | ||
return new ServerStreamMock({response: [{id: 1}, {id: 2}]}); | ||
}; | ||
makeServerStreamRequest.requestStream = false; | ||
makeServerStreamRequest.responseStream = true; | ||
Object.setPrototypeOf(client, { | ||
serverStreamReq: makeServerStreamRequest | ||
}); | ||
grpc_promise.promisifyAll(client); | ||
return Promise.all([client.serverStreamReq().sendMessage({}), client.serverStreamReq().sendMessage({})]) | ||
.then(res => { | ||
res[0][0].id.should.equal(1); | ||
res[0][1].id.should.equal(2); | ||
res[1][0].id.should.equal(1); | ||
res[1][1].id.should.equal(2); | ||
}); | ||
}); | ||
grpc_promise.promisifyAll(client); | ||
it('Test ko', function () { | ||
const client = {}; | ||
const makeServerStreamRequest = function () { | ||
return new ServerStreamMock(); | ||
}; | ||
makeServerStreamRequest.requestStream = false; | ||
makeServerStreamRequest.responseStream = true; | ||
Object.setPrototypeOf(client, { | ||
serverStreamReq: makeServerStreamRequest | ||
}); | ||
return Promise.all([client.serverStreamReq().sendMessage({}), client.serverStreamReq().sendMessage({})]) | ||
.then(res => { | ||
res[0][0].id.should.equal(1); | ||
res[0][1].id.should.equal(2); | ||
res[1][0].id.should.equal(1); | ||
res[1][1].id.should.equal(2); | ||
}); | ||
grpc_promise.promisifyAll(client); | ||
return client.serverStreamReq() | ||
.sendMessage({}) | ||
.then(() => { | ||
throw new Error('Should not happen'); | ||
}) | ||
.catch(err => err.should.equal('some_error')); | ||
}); | ||
}); | ||
it('Test ko', function () { | ||
const client = {}; | ||
const makeServerStreamRequest = function () { | ||
return new ServerStreamMock(); | ||
}; | ||
makeServerStreamRequest.requestStream = false; | ||
makeServerStreamRequest.responseStream = true; | ||
Object.setPrototypeOf(client, { | ||
serverStreamReq: makeServerStreamRequest | ||
describe('With promisify', function () { | ||
it('Test ok', function (done) { | ||
const client = {}; | ||
const makeServerStreamRequest = function () { | ||
return new ServerStreamMock({response: [{id: 1}, {id: 2}]}); | ||
}; | ||
makeServerStreamRequest.requestStream = false; | ||
makeServerStreamRequest.responseStream = true; | ||
const makeServerStreamRequest2 = function () { | ||
return new ServerStreamMock({response: [{id: 1}, {id: 2}]}); | ||
}; | ||
makeServerStreamRequest2.requestStream = false; | ||
makeServerStreamRequest2.responseStream = true; | ||
Object.setPrototypeOf(client, { | ||
serverStreamReq: makeServerStreamRequest, | ||
serverStreamReq2: makeServerStreamRequest2 | ||
}); | ||
grpc_promise.promisify(client, ['serverStreamReq']); | ||
Promise.all([client.serverStreamReq().sendMessage({}), client.serverStreamReq().sendMessage({})]) | ||
.then(res => { | ||
res[0][0].id.should.equal(1); | ||
res[0][1].id.should.equal(2); | ||
res[1][0].id.should.equal(1); | ||
res[1][1].id.should.equal(2); | ||
const call = client.serverStreamReq2({id: 1}); | ||
let responseCounter = 1; | ||
call.on('data', function (res) { | ||
res.id.should.equal(responseCounter++); | ||
if (responseCounter === 2) { | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
grpc_promise.promisifyAll(client); | ||
return client.serverStreamReq() | ||
.sendMessage({}) | ||
.then(() => { | ||
throw new Error('Should not happen'); | ||
}) | ||
.catch(err => err.should.equal('some_error')); | ||
}); | ||
}); |
@@ -5,41 +5,78 @@ const grpc_promise = require('../../../lib/index'); | ||
it('Test ok', function () { | ||
const client = {}; | ||
const makeUnaryRequest = function (request, metadata, callback) { | ||
callback(null, request); | ||
}; | ||
makeUnaryRequest.requestStream = false; | ||
makeUnaryRequest.responseStream = false; | ||
Object.setPrototypeOf(client, { | ||
unaryReq: makeUnaryRequest | ||
describe('With promisifyAll', function () { | ||
it('Test ok', function () { | ||
const client = {}; | ||
const makeUnaryRequest = function (request, metadata, callback) { | ||
callback(null, request); | ||
}; | ||
makeUnaryRequest.requestStream = false; | ||
makeUnaryRequest.responseStream = false; | ||
Object.setPrototypeOf(client, { | ||
unaryReq: makeUnaryRequest | ||
}); | ||
grpc_promise.promisifyAll(client); | ||
return client.unaryReq() | ||
.sendMessage({id: 1}) | ||
.then(res => res.id.should.equal(1)); | ||
}); | ||
grpc_promise.promisifyAll(client); | ||
it('Test ko', function () { | ||
const client = {}; | ||
const makeUnaryRequest = function (request, metadata, callback) { | ||
callback('timeout'); | ||
}; | ||
makeUnaryRequest.requestStream = false; | ||
makeUnaryRequest.responseStream = false; | ||
Object.setPrototypeOf(client, { | ||
unaryReq: makeUnaryRequest | ||
}); | ||
return client.unaryReq() | ||
.sendMessage({id: 1}) | ||
.then(res => res.id.should.equal(1)); | ||
grpc_promise.promisifyAll(client); | ||
return client.unaryReq() | ||
.sendMessage({id: 1}) | ||
.then(() => { | ||
throw new Error('Should not happen'); | ||
}) | ||
.catch(err => err.should.equal('timeout')); | ||
}); | ||
}); | ||
it('Test ko', function () { | ||
const client = {}; | ||
const makeUnaryRequest = function (request, metadata, callback) { | ||
callback('timeout'); | ||
}; | ||
makeUnaryRequest.requestStream = false; | ||
makeUnaryRequest.responseStream = false; | ||
Object.setPrototypeOf(client, { | ||
unaryReq: makeUnaryRequest | ||
describe('With promisify', function () { | ||
it('Test ok', function () { | ||
const client = {}; | ||
const makeUnaryRequest = function (request, metadata, callback) { | ||
callback(null, request); | ||
}; | ||
makeUnaryRequest.requestStream = false; | ||
makeUnaryRequest.responseStream = false; | ||
const makeUnaryRequest2 = function (request, metadata, callback) { | ||
callback(null, request); | ||
}; | ||
makeUnaryRequest2.requestStream = false; | ||
makeUnaryRequest2.responseStream = false; | ||
Object.setPrototypeOf(client, { | ||
unaryReq: makeUnaryRequest, | ||
unaryReq2: makeUnaryRequest2 | ||
}); | ||
grpc_promise.promisify(client, ['unaryReq']); | ||
return client.unaryReq() | ||
.sendMessage({id: 1}) | ||
.then(res => { | ||
res.id.should.equal(1); | ||
client.unaryReq2({id: 1}, null, function (err, res) { | ||
res.id.should.equal(1); | ||
}); | ||
}); | ||
}); | ||
grpc_promise.promisifyAll(client); | ||
return client.unaryReq() | ||
.sendMessage({id: 1}) | ||
.then(() => { | ||
throw new Error('Should not happen'); | ||
}) | ||
.catch(err => err.should.equal('timeout')); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43279
28
879
416