Skip to content

Commit

Permalink
Merge pull request #6 from carlessistare/add-metadata-support
Browse files Browse the repository at this point in the history
Add metadata support into promisifyAll
  • Loading branch information
carlessistare committed Aug 29, 2018
2 parents 0b49441 + 5fdb7d2 commit e95256f
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 26 deletions.
37 changes: 37 additions & 0 deletions README.md
Expand Up @@ -98,6 +98,7 @@ message TestResponse {
Implementation of `TestSimpleSimple` message

Server side:

```js
const grpc = require('grpc');
const test_proto = grpc.load(__dirname + '/protobuf/test.proto').test;
Expand All @@ -121,6 +122,7 @@ main();
```

Client side:

```js
const grpc = require('grpc');
const grpc_promise = require('grpc-promise');
Expand Down Expand Up @@ -148,6 +150,7 @@ main();
Implementation of `TestStreamSimple` message

Server side:

```js
const grpc = require('grpc');
const test_proto = grpc.load(__dirname + '/protobuf/test.proto').test;
Expand Down Expand Up @@ -177,6 +180,7 @@ main();
```

Client side:

```js
const grpc = require('grpc');
const grpc_promise = require('grpc-promise');
Expand Down Expand Up @@ -207,6 +211,7 @@ main();
Implementation of `TestSimpleStream` message

Server side:

```js
const grpc = require('grpc');
const test_proto = grpc.load(__dirname + '/protobuf/test.proto').test;
Expand All @@ -232,6 +237,7 @@ main();
```

Client side:

```js
const grpc = require('grpc');
const grpc_promise = require('grpc-promise');
Expand Down Expand Up @@ -262,6 +268,7 @@ Implementation of `TestStreamStream` message
the server implementation needs to answer with the same id received

Server side:

```js
const grpc = require('grpc');
const test_proto = grpc.load(__dirname + '/protobuf/test.proto').test;
Expand Down Expand Up @@ -295,6 +302,7 @@ main();
```

Client side:

```js
const grpc = require('grpc');
const grpc_promise = require('grpc-promise');
Expand Down Expand Up @@ -324,6 +332,35 @@ function main() {
main();
```

### Request with Metadata

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.promisifyAll(client, {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

Copyright (c) 2017 Carles Sistare
Expand Down
5 changes: 4 additions & 1 deletion examples/client-bidi-stream.js
Expand Up @@ -5,7 +5,10 @@ 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
const meta = new grpc.Metadata();
meta.add('key', 'value');

grpc_promise.promisifyAll(client, {timeout: 100, metadata: meta}); // Optional timeout definition, defaults = 50

let t = client.testStreamStream();
t.sendMessage({})
Expand Down
7 changes: 5 additions & 2 deletions examples/client-client-stream.js
Expand Up @@ -5,9 +5,12 @@ 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);
const meta = new grpc.Metadata();
meta.add('key', 'value');

client.testStreamSimple()
grpc_promise.promisifyAll(client, {metadata: meta});

client.testStreamSimple(meta)
.sendMessage({id: 1})
.sendMessage({id: 2})
.sendMessage({id: 3})
Expand Down
5 changes: 4 additions & 1 deletion examples/client-server-stream.js
Expand Up @@ -5,7 +5,10 @@ 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);
const meta = new grpc.Metadata();
meta.add('key', 'value');

grpc_promise.promisifyAll(client, {metadata: meta});

client.testSimpleStream()
.sendMessage({id: 1})
Expand Down
5 changes: 4 additions & 1 deletion examples/client-unary.js
Expand Up @@ -5,7 +5,10 @@ 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);
const meta = new grpc.Metadata();
meta.add('key', 'value');

grpc_promise.promisifyAll(client, {metadata: meta});

client.testSimpleSimple()
.sendMessage({id: 1})
Expand Down
4 changes: 4 additions & 0 deletions examples/server.js
Expand Up @@ -3,13 +3,15 @@ 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}
console.log('Server: With Metadata key = ' + call.metadata.get('key'));
callback(null, call.request);
};

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}
console.log('Server: With Metadata key = ' + call.metadata.get('key'));
messages.push(m);
});
call.on('end', function () {
Expand All @@ -19,6 +21,7 @@ const testStreamSimple = function (call, callback) {

const testSimpleStream = function (call) {
console.log('Server: Simple Message Received = ', call.request); // Server: Simple Message Received = {id: 1}
console.log('Server: With Metadata key = ' + call.metadata.get('key'));
call.write(call.request);
call.write(call.request);
call.end();
Expand All @@ -27,6 +30,7 @@ const testSimpleStream = function (call) {
const testStreamStream = function (call) {
call.on('data', function (message) {
console.log('Server: Stream Message Received = ', message); // Server: Stream Message Received = {id: 1}
console.log('Server: With Metadata key = ' + call.metadata.get('key'));
setTimeout(function () {
call.write({
id: message.id // IMPORTANT only for Bidirectional Stream Request
Expand Down
8 changes: 3 additions & 5 deletions lib/request-types/bidi-stream-request.js
Expand Up @@ -5,14 +5,12 @@ class BidiStreamRequest {
return 2147483647;
}

constructor (client, original_function, options) {
if (options == null) {
options = {};
}
constructor (client, original_function, options = {}) {
if (options == null) options = {};
this.queue = {};
this.correlationId = 0;
this.timeout = options.timeout || 50;
this.stream = original_function.call(client);
this.stream = original_function.call(client, options.metadata);

this.stream.on('error', () => {});
this.stream.on('data', data => {
Expand Down
9 changes: 5 additions & 4 deletions lib/request-types/client-stream-request.js
@@ -1,9 +1,10 @@

class ClientStreamRequest {

constructor (client, original_function) {
constructor (client, original_function, options = {}) {
if (options == null) options = {};
this.promise = new Promise((resolve, reject) => {
this.stream = original_function.call(client, function (error, response) {
this.stream = original_function.call(client, options.metadata, function (error, response) {
if (error) {
reject(error);
} else {
Expand All @@ -25,9 +26,9 @@ class ClientStreamRequest {

}

const makeClientStreamRequest = function (client, originalFunction) {
const makeClientStreamRequest = function (client, originalFunction, options) {
return function () {
return new ClientStreamRequest(client, originalFunction);
return new ClientStreamRequest(client, originalFunction, options);
};
};

Expand Down
10 changes: 6 additions & 4 deletions lib/request-types/server-stream-request.js
@@ -1,15 +1,17 @@

class ServerStreamRequest {

constructor (client, original_function) {
constructor (client, original_function, options = {}) {
if (options == null) options = {};
this.queue = [];
this.client = client;
this.metadata = options.metadata;
this.original_function = original_function;
}

sendMessage (content = {}) {
return new Promise((resolve, reject) => {
this.stream = this.original_function.call(this.client, content);
this.stream = this.original_function.call(this.client, content, this.metadata);
this.stream.on('error', error => {
reject(error);
});
Expand All @@ -24,9 +26,9 @@ class ServerStreamRequest {

}

const makeServerStreamRequest = function (client, originalFunction) {
const makeServerStreamRequest = function (client, originalFunction, options) {
return function () {
return new ServerStreamRequest(client, originalFunction);
return new ServerStreamRequest(client, originalFunction, options);
};
};

Expand Down
10 changes: 6 additions & 4 deletions lib/request-types/unary-request.js
@@ -1,14 +1,16 @@

class UnaryRequest {

constructor (client, original_function) {
constructor (client, original_function, options = {}) {
if (options == null) options = {};
this.client = client;
this.metadata = options.metadata;
this.original_function = original_function;
}

sendMessage (content = {}) {
return new Promise((resolve, reject) => {
this.original_function.call(this.client, content, function (error, response) {
this.original_function.call(this.client, content, this.metadata, function (error, response) {
if (error) {
reject(error);
} else {
Expand All @@ -20,9 +22,9 @@ class UnaryRequest {

}

const makeUnaryRequest = function (client, originalFunction) {
const makeUnaryRequest = function (client, originalFunction, options) {
return function () {
return new UnaryRequest(client, originalFunction);
return new UnaryRequest(client, originalFunction, options);
};
};

Expand Down
4 changes: 2 additions & 2 deletions test/lib/request-types/client-stream-request.js
Expand Up @@ -5,7 +5,7 @@ describe('Client Stream Request', function () {

it('Test ok', function () {
const client = {};
const makeClientStreamRequest = function (callback) {
const makeClientStreamRequest = function (metadata, callback) {
return new ClientStreamMock({callback: callback});
};
makeClientStreamRequest.requestStream = true;
Expand All @@ -27,7 +27,7 @@ describe('Client Stream Request', function () {

it('Test ko', function () {
const client = {};
const makeClientStreamRequest = function (callback) {
const makeClientStreamRequest = function (metadata, callback) {
var stream = new ClientStreamMock();
stream.on('finish', function () {
callback('some_error');
Expand Down
4 changes: 2 additions & 2 deletions test/lib/request-types/unary-request.js
Expand Up @@ -4,7 +4,7 @@ describe('Unary Request', function () {

it('Test ok', function () {
const client = {};
const makeUnaryRequest = function (request, callback) {
const makeUnaryRequest = function (request, metadata, callback) {
callback(null, request);
};
makeUnaryRequest.requestStream = false;
Expand All @@ -22,7 +22,7 @@ describe('Unary Request', function () {

it('Test ko', function () {
const client = {};
const makeUnaryRequest = function (request, callback) {
const makeUnaryRequest = function (request, metadata, callback) {
callback('timeout');
};
makeUnaryRequest.requestStream = false;
Expand Down

0 comments on commit e95256f

Please sign in to comment.